summaryrefslogtreecommitdiff
path: root/src/mongo/util/perfctr_collect_test.cpp
blob: 0d93bf957ef7d9a7e1b53e051436ebdba311cade (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/**
 * Copyright (C) 2016 MongoDB Inc.
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * As a special exception, the copyright holders give permission to link the
 * code of portions of this program with the OpenSSL library under certain
 * conditions as described in each individual source file and distribute
 * linked combinations including the program with the OpenSSL library. You
 * must comply with the GNU Affero General Public License in all respects
 * for all of the code used other than as permitted herein. If you modify
 * file(s) with this exception, you may extend this exception to your
 * version of the file(s), but you are not obligated to do so. If you do not
 * wish to do so, delete this exception statement from your version. If you
 * delete this exception statement from all source files in the program,
 * then also delete it in the license file.
 */

#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kFTDC

#include "mongo/platform/basic.h"

#include "mongo/util/perfctr_collect.h"

#include <boost/filesystem.hpp>
#include <map>

#include "mongo/bson/bsonobj.h"
#include "mongo/bson/bsonobjbuilder.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/log.h"

namespace mongo {

namespace {
using StringMap = std::map<std::string, uint64_t>;

/**
 * Convert BSON document nested up to 3 levels into a map where keys are dot separated paths to
 * values.
 */
StringMap toNestedStringMap(BSONObj& obj) {
    StringMap map;

    for (const auto& parent : obj) {

        if (parent.isABSONObj()) {
            std::string parentNamePrefix = std::string(parent.fieldName()) + ".";

            for (const auto& child : parent.Obj()) {

                if (child.isABSONObj()) {
                    std::string childNamePrefix = parentNamePrefix + child.fieldName() + ".";

                    for (const auto& grandChild : child.Obj()) {
                        map[childNamePrefix + grandChild.fieldName()] = grandChild.numberLong();
                    }

                } else {
                    map[parentNamePrefix + child.fieldName()] = child.numberLong();
                }
            }
        } else {
            map[parent.fieldName()] = parent.numberLong();
        }
    }

    return map;
}

#define ASSERT_KEY(_key) ASSERT_TRUE(stringMap.find(_key) != stringMap.end());
#define ASSERT_NO_KEY(_key) ASSERT_TRUE(stringMap.find(_key) == stringMap.end());

#define ASSERT_TIMEBASE ASSERT_KEY("timebase");
#define ASSERT_NO_TIMEBASE ASSERT_NO_KEY("timebase");

#define ASSERT_GROUP_AND_RAW_COUNTER(g, c) \
    ASSERT_KEY(g "." c);                   \
    ASSERT_NO_KEY(g "." c " Base");

#define ASSERT_GROUP_AND_NON_RAW_COUNTER(g, c) \
    ASSERT_KEY(g "." c);                       \
    ASSERT_KEY(g "." c " Base");

#define ASSERT_NESTED_GROUP_AND_RAW_COUNTER(g, p, c) \
    ASSERT_KEY(g "." p "." c);                       \
    ASSERT_NO_KEY(g "." p "." c " Base");

#define ASSERT_NO_NESTED_GROUP_AND_RAW_COUNTER(g, p, c) \
    ASSERT_NO_KEY(g "." p "." c);                       \
    ASSERT_NO_KEY(g "." p "." c " Base");

#define ASSERT_NESTED_GROUP_AND_NON_RAW_COUNTER(g, p, c) \
    ASSERT_KEY(g "." p "." c);                           \
    ASSERT_KEY(g "." p "." c " Base");

#define ASSERT_NO_NESTED_GROUP_AND_NON_RAW_COUNTER(g, p, c) \
    ASSERT_NO_KEY(g "." p "." c);                           \
    ASSERT_NO_KEY(g "." p "." c " Base");

#define COLLECT_COUNTERS_VERBOSE                             \
    BSONObjBuilder builder;                                  \
    ASSERT_OK(collector->collect(&builder));                 \
    auto obj = builder.obj();                                \
    log() << "OBJ:" << obj;                                  \
    auto stringMap = toNestedStringMap(obj);                 \
    for (const auto& kvp : stringMap) {                      \
        log() << "kvp " << kvp.first << " - " << kvp.second; \
    }

#define COLLECT_COUNTERS_QUIET               \
    BSONObjBuilder builder;                  \
    ASSERT_OK(collector->collect(&builder)); \
    auto obj = builder.obj();                \
    auto stringMap = toNestedStringMap(obj);

#define COLLECT_COUNTERS COLLECT_COUNTERS_QUIET

size_t kDefaultCollectionCount = 2;

// Simple verification test
TEST(FTDCPerfCollector, TestSingleCounter) {

    PerfCounterCollection collection;
    // PERF_100NSEC_TIMER
    ASSERT_OK(collection.addCountersGroup("cpu", {"\\Processor(0)\\% Idle Time"}));

    auto swCollector = PerfCounterCollector::create(std::move(collection));
    ASSERT_OK(swCollector.getStatus());
    auto collector = std::move(swCollector.getValue());

    for (size_t i = 0; i < kDefaultCollectionCount; i++) {
        COLLECT_COUNTERS;

        ASSERT_NO_TIMEBASE;
        ASSERT_GROUP_AND_RAW_COUNTER("cpu", "\\Processor\\% Idle Time");
    }
}


// Simple verification test
TEST(FTDCPerfCollector, TestSingleRawCounter) {

    PerfCounterCollection collection;
    // PERF_COUNTER_RAWCOUNT
    ASSERT_OK(collection.addCountersGroup("cpu", {"\\System\\Processes"}));

    auto swCollector = PerfCounterCollector::create(std::move(collection));
    ASSERT_OK(swCollector.getStatus());
    auto collector = std::move(swCollector.getValue());

    for (size_t i = 0; i < kDefaultCollectionCount; i++) {
        COLLECT_COUNTERS;

        ASSERT_NO_TIMEBASE;
        ASSERT_GROUP_AND_RAW_COUNTER("cpu", "\\System\\Processes");
    }
}

// Test negative cases for collection
TEST(FTDCPerfCollector, TestBadCollectionInput) {

    PerfCounterCollection collection;
    ASSERT_OK(collection.addCountersGroup("cpu", {"\\Processor(0)\\% Idle Time"}));

    // Duplicate group
    ASSERT_NOT_OK(collection.addCountersGroup("cpu", {"\\Processor(0)\\% Idle Time"}));

    // Duplicate counter
    ASSERT_NOT_OK(collection.addCountersGroup(
        "cpu2",
        {
            "\\Processor(0)\\% Idle Time", "\\Processor(0)\\% Idle Time",
        }));

    // Duplicate group
    ASSERT_NOT_OK(
        collection.addCountersGroupedByInstanceName("cpu", {"\\Processor(0)\\% Idle Time"}));

    // Duplicate counter
    ASSERT_NOT_OK(collection.addCountersGroupedByInstanceName(
        "cpu2",
        {
            "\\Processor(0)\\% Idle Time", "\\Processor(0)\\% Idle Time",
        }));
}

// Test negative collector input
TEST(FTDCPerfCollector, TestBadCollectorInput) {
    // Bad counter name
    {
        PerfCounterCollection collection;
        ASSERT_OK(collection.addCountersGroup("cpu", {"\\Processor(0)\\DOES NOT EXIST"}));

        auto swCollector = PerfCounterCollector::create(std::move(collection));
        ASSERT_NOT_OK(swCollector.getStatus());
    }

    // Bad wild card
    {
        PerfCounterCollection collection;
        ASSERT_OK(collection.addCountersGroup("cpu", {"\\Processor(0)\\DOES*"}));

        auto swCollector = PerfCounterCollector::create(std::move(collection));
        ASSERT_NOT_OK(swCollector.getStatus());
    }

    // Use addCounterGroup with instance wildcard
    {
        PerfCounterCollection collection;
        ASSERT_OK(collection.addCountersGroup("cpu", {"\\Processor(*)\\\\% Idle Time"}));

        auto swCollector = PerfCounterCollector::create(std::move(collection));
        ASSERT_NOT_OK(swCollector.getStatus());
    }

    // Use addCountersGroupedByInstanceName without instance name
    {
        PerfCounterCollection collection;
        ASSERT_OK(collection.addCountersGroupedByInstanceName("cpu", {"\\System\\Processes"}));

        auto swCollector = PerfCounterCollector::create(std::move(collection));
        ASSERT_NOT_OK(swCollector.getStatus());
    }
}

// Test all the different counter types we use in the MongoDB code
TEST(FTDCPerfCollector, TestCounterTypes) {

    PerfCounterCollection collection;
    ASSERT_OK(collection.addCountersGroup(
        "misc",
        {
            "\\Processor(0)\\% Idle Time",                          // PERF_100NSEC_TIMER
            "\\Processor(0)\\% Processor Time",                     // PERF_100NSEC_TIMER_INV
            "\\System\\Processes",                                  // PERF_COUNTER_RAWCOUNT
            "\\System\\System Up Time",                             // PERF_ELAPSED_TIME
            "\\Memory\\Available Bytes",                            // PERF_COUNTER_LARGE_RAWCOUNT
            "\\PhysicalDisk(_Total)\\% Disk Write Time",            // PERF_PRECISION_100NS_TIMER
            "\\PhysicalDisk(_Total)\\Avg. Disk Bytes/Write",        // PERF_AVERAGE_BULK
            "\\PhysicalDisk(_Total)\\Avg. Disk Read Queue Length",  // PERF_COUNTER_LARGE_QUEUELEN_TYPE
            "\\PhysicalDisk(_Total)\\Avg. Disk sec/Write",          // PERF_AVERAGE_TIMER
            "\\PhysicalDisk(_Total)\\Disk Write Bytes/sec",         // PERF_COUNTER_BULK_COUNT
            "\\PhysicalDisk(_Total)\\Disk Writes/sec",              // PERF_COUNTER_COUNTER
        }));

    auto swCollector = PerfCounterCollector::create(std::move(collection));
    ASSERT_OK(swCollector.getStatus());
    auto collector = std::move(swCollector.getValue());

    for (size_t i = 0; i < kDefaultCollectionCount; i++) {
        COLLECT_COUNTERS;

        ASSERT_TIMEBASE
        ASSERT_GROUP_AND_RAW_COUNTER("misc", "\\Processor\\% Idle Time");
        ASSERT_GROUP_AND_RAW_COUNTER("misc", "\\Processor\\% Processor Time");
        ASSERT_GROUP_AND_RAW_COUNTER("misc", "\\System\\System Up Time");
        ASSERT_GROUP_AND_NON_RAW_COUNTER("misc", "\\PhysicalDisk\\% Disk Write Time");
        ASSERT_GROUP_AND_RAW_COUNTER("misc", "\\PhysicalDisk\\Avg. Disk Bytes/Write");
        ASSERT_GROUP_AND_RAW_COUNTER("misc", "\\PhysicalDisk\\Avg. Disk Read Queue Length");
        ASSERT_GROUP_AND_RAW_COUNTER("misc", "\\PhysicalDisk\\Avg. Disk sec/Write");
        ASSERT_GROUP_AND_RAW_COUNTER("misc", "\\PhysicalDisk\\Disk Write Bytes/sec");
        ASSERT_GROUP_AND_RAW_COUNTER("misc", "\\PhysicalDisk\\Disk Writes/sec");

        ASSERT_GROUP_AND_RAW_COUNTER("misc", "\\System\\Processes");
    }
}

// Test multiple counter groups
TEST(FTDCPerfCollector, TestMultipleCounterGroups) {

    PerfCounterCollection collection;
    ASSERT_OK(collection.addCountersGroup(
        "cpu", {"\\Processor(0)\\% Idle Time", "\\Processor(0)\\% Processor Time"}));
    ASSERT_OK(
        collection.addCountersGroup("sys", {"\\System\\Processes", "\\System\\System Up Time"}));

    auto swCollector = PerfCounterCollector::create(std::move(collection));
    ASSERT_OK(swCollector.getStatus());
    auto collector = std::move(swCollector.getValue());

    for (size_t i = 0; i < kDefaultCollectionCount; i++) {
        COLLECT_COUNTERS;

        ASSERT_TIMEBASE
        ASSERT_GROUP_AND_RAW_COUNTER("cpu", "\\Processor\\% Idle Time");
        ASSERT_GROUP_AND_RAW_COUNTER("cpu", "\\Processor\\% Processor Time");
        ASSERT_GROUP_AND_RAW_COUNTER("sys", "\\System\\System Up Time");

        ASSERT_GROUP_AND_RAW_COUNTER("sys", "\\System\\Processes");
    }
}

// Test multiple nested counter groups
TEST(FTDCPerfCollector, TestMultipleNestedCounterGroups) {

    PerfCounterCollection collection;
    ASSERT_OK(collection.addCountersGroupedByInstanceName(
        "cpu", {"\\Processor(*)\\% Idle Time", "\\Processor(*)\\% Processor Time"}));
    ASSERT_OK(
        collection.addCountersGroup("sys", {"\\System\\Processes", "\\System\\System Up Time"}));

    auto swCollector = PerfCounterCollector::create(std::move(collection));
    ASSERT_OK(swCollector.getStatus());
    auto collector = std::move(swCollector.getValue());

    for (size_t i = 0; i < kDefaultCollectionCount; i++) {
        COLLECT_COUNTERS;
        ASSERT_TIMEBASE

        // We boldly assume that machines we test on have at least two processors
        ASSERT_NESTED_GROUP_AND_RAW_COUNTER("cpu", "0", "\\Processor\\% Idle Time");
        ASSERT_NESTED_GROUP_AND_RAW_COUNTER("cpu", "0", "\\Processor\\% Processor Time");

        ASSERT_NESTED_GROUP_AND_RAW_COUNTER("cpu", "1", "\\Processor\\% Idle Time");
        ASSERT_NESTED_GROUP_AND_RAW_COUNTER("cpu", "1", "\\Processor\\% Processor Time");

        ASSERT_NO_NESTED_GROUP_AND_RAW_COUNTER("cpu", "_Total", "\\Processor\\% Idle Time");
        ASSERT_NO_NESTED_GROUP_AND_RAW_COUNTER("cpu", "_Total", "\\Processor\\% Processor Time");

        ASSERT_GROUP_AND_RAW_COUNTER("sys", "\\System\\System Up Time");
        ASSERT_GROUP_AND_RAW_COUNTER("sys", "\\System\\Processes");
    }
}

// Test Counters we use in MongoDB
TEST(FTDCPerfCollector, TestLocalCounters) {

    PerfCounterCollection collection;
    ASSERT_OK(collection.addCountersGroup("cpu",
                                          {
                                              "\\Processor(_Total)\\% Idle Time",
                                              "\\Processor(_Total)\\% Interrupt Time",
                                              "\\Processor(_Total)\\% Privileged Time",
                                              "\\Processor(_Total)\\% Processor Time",
                                              "\\Processor(_Total)\\% User Time",
                                              "\\Processor(_Total)\\Interrupts/sec",
                                              "\\System\\Context Switches/sec",
                                              "\\System\\Processes",
                                              "\\System\\Processor Queue Length",
                                              "\\System\\System Up Time",
                                              "\\System\\Threads",
                                          }));

    // TODO: Should we capture the Heap Counters for the current process?
    ASSERT_OK(collection.addCountersGroup("memory",
                                          {
                                              "\\Memory\\Available Bytes",
                                              "\\Memory\\Cache Bytes",
                                              "\\Memory\\Cache Faults/sec",
                                              "\\Memory\\Committed Bytes",
                                              "\\Memory\\Commit Limit",
                                              "\\Memory\\Page Reads/sec",
                                              "\\Memory\\Page Writes/sec",
                                              "\\Memory\\Pages Input/sec",
                                              "\\Memory\\Pages Output/sec",
                                              "\\Memory\\Pool Nonpaged Bytes",
                                              "\\Memory\\Pool Paged Bytes",
                                              "\\Memory\\Pool Paged Resident Bytes",
                                              "\\Memory\\System Cache Resident Bytes",
                                              "\\Memory\\System Code Total Bytes",
                                          }));

    ASSERT_OK(collection.addCountersGroupedByInstanceName(
        "disks",
        {
            "\\PhysicalDisk(*)\\% Disk Read Time",
            "\\PhysicalDisk(*)\\% Disk Write Time",
            "\\PhysicalDisk(*)\\Avg. Disk Bytes/Read",
            "\\PhysicalDisk(*)\\Avg. Disk Bytes/Write",
            "\\PhysicalDisk(*)\\Avg. Disk Read Queue Length",
            "\\PhysicalDisk(*)\\Avg. Disk Write Queue Length",
            "\\PhysicalDisk(*)\\Avg. Disk sec/Read",
            "\\PhysicalDisk(*)\\Avg. Disk sec/Write",
            "\\PhysicalDisk(*)\\Disk Read Bytes/sec",
            "\\PhysicalDisk(*)\\Disk Write Bytes/sec",
            "\\PhysicalDisk(*)\\Disk Reads/sec",
            "\\PhysicalDisk(*)\\Disk Writes/sec",
            "\\PhysicalDisk(*)\\Current Disk Queue Length",
        }));

    auto swCollector = PerfCounterCollector::create(std::move(collection));
    ASSERT_OK(swCollector.getStatus());
    auto collector = std::move(swCollector.getValue());

    for (size_t i = 0; i < kDefaultCollectionCount; i++) {
        COLLECT_COUNTERS;
        ASSERT_TIMEBASE

        ASSERT_GROUP_AND_RAW_COUNTER("cpu", "\\Processor\\% Idle Time");
        ASSERT_GROUP_AND_RAW_COUNTER("cpu", "\\Processor\\% Interrupt Time");
        ASSERT_GROUP_AND_RAW_COUNTER("cpu", "\\Processor\\% Privileged Time");
        ASSERT_GROUP_AND_RAW_COUNTER("cpu", "\\Processor\\% Processor Time");
        ASSERT_GROUP_AND_RAW_COUNTER("cpu", "\\Processor\\% User Time");
        ASSERT_GROUP_AND_RAW_COUNTER("cpu", "\\Processor\\Interrupts/sec");

        ASSERT_GROUP_AND_RAW_COUNTER("cpu", "\\System\\Context Switches/sec");
        ASSERT_GROUP_AND_RAW_COUNTER("cpu", "\\System\\Processes");
        ASSERT_GROUP_AND_RAW_COUNTER("cpu", "\\System\\Processor Queue Length");
        ASSERT_GROUP_AND_RAW_COUNTER("cpu", "\\System\\System Up Time");
        ASSERT_GROUP_AND_RAW_COUNTER("cpu", "\\System\\Threads");

        ASSERT_GROUP_AND_RAW_COUNTER("memory", "\\Memory\\Available Bytes");
        ASSERT_GROUP_AND_RAW_COUNTER("memory", "\\Memory\\Cache Bytes");
        ASSERT_GROUP_AND_RAW_COUNTER("memory", "\\Memory\\Cache Faults/sec");
        ASSERT_GROUP_AND_RAW_COUNTER("memory", "\\Memory\\Commit Limit");
        ASSERT_GROUP_AND_RAW_COUNTER("memory", "\\Memory\\Committed Bytes");
        ASSERT_GROUP_AND_RAW_COUNTER("memory", "\\Memory\\Page Reads/sec");
        ASSERT_GROUP_AND_RAW_COUNTER("memory", "\\Memory\\Page Writes/sec");
        ASSERT_GROUP_AND_RAW_COUNTER("memory", "\\Memory\\Pages Input/sec");
        ASSERT_GROUP_AND_RAW_COUNTER("memory", "\\Memory\\Pages Output/sec");
        ASSERT_GROUP_AND_RAW_COUNTER("memory", "\\Memory\\Pool Paged Resident Bytes");
        ASSERT_GROUP_AND_RAW_COUNTER("memory", "\\Memory\\System Cache Resident Bytes");
        ASSERT_GROUP_AND_RAW_COUNTER("memory", "\\Memory\\System Code Total Bytes");
    }
}

}  // namespace
}  // namespace mongo