summaryrefslogtreecommitdiffstats
path: root/src/collectors/windows.plugin/perflib-rrd.c
blob: d425307eecaf44b2e81bf70138268bae75f618ea (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
// SPDX-License-Identifier: GPL-3.0-or-later

#include "perflib-rrd.h"

#define COLLECTED_NUMBER_PRECISION 10000

RRDDIM *perflib_rrddim_add(RRDSET *st, const char *id, const char *name, collected_number multiplier, collected_number divider, COUNTER_DATA *cd) {
    RRD_ALGORITHM algorithm = RRD_ALGORITHM_ABSOLUTE;

    switch (cd->current.CounterType) {
        case PERF_COUNTER_COUNTER:
        case PERF_SAMPLE_COUNTER:
        case PERF_COUNTER_BULK_COUNT:
            // (N1 - N0) / ((D1 - D0) / F)
            // multiplier *= cd->current.Frequency / 10000000;
            // tested, the frequency is not that useful for netdata
            // we get right results without it.
            algorithm = RRD_ALGORITHM_INCREMENTAL;
            break;

        case PERF_COUNTER_QUEUELEN_TYPE:
        case PERF_COUNTER_100NS_QUEUELEN_TYPE:
        case PERF_COUNTER_OBJ_TIME_QUEUELEN_TYPE:
        case PERF_COUNTER_LARGE_QUEUELEN_TYPE:
        case PERF_AVERAGE_BULK:  // normally not displayed
            // (N1 - N0) / (D1 - D0)
            algorithm = RRD_ALGORITHM_INCREMENTAL;
            break;

        case PERF_OBJ_TIME_TIMER:
        case PERF_COUNTER_TIMER:
        case PERF_100NSEC_TIMER:
        case PERF_PRECISION_SYSTEM_TIMER:
        case PERF_PRECISION_100NS_TIMER:
        case PERF_PRECISION_OBJECT_TIMER:
        case PERF_SAMPLE_FRACTION:
            // 100 * (N1 - N0) / (D1 - D0)
            multiplier *= 100;
            algorithm = RRD_ALGORITHM_INCREMENTAL;
            break;

        case PERF_COUNTER_TIMER_INV:
        case PERF_100NSEC_TIMER_INV:
            // 100 * (1 - ((N1 - N0) / (D1 - D0)))
            divider *= COLLECTED_NUMBER_PRECISION;
            algorithm = RRD_ALGORITHM_ABSOLUTE;
            break;

        case PERF_COUNTER_MULTI_TIMER:
            // 100 * ((N1 - N0) / ((D1 - D0) / TB)) / B1
            divider *= COLLECTED_NUMBER_PRECISION;
            algorithm = RRD_ALGORITHM_ABSOLUTE;
            break;

        case PERF_100NSEC_MULTI_TIMER:
            // 100 * ((N1 - N0) / (D1 - D0)) / B1
            divider *= COLLECTED_NUMBER_PRECISION;
            algorithm = RRD_ALGORITHM_ABSOLUTE;
            break;

        case PERF_COUNTER_MULTI_TIMER_INV:
        case PERF_100NSEC_MULTI_TIMER_INV:
            // 100 * (B1 - ((N1 - N0) / (D1 - D0)))
            divider *= COLLECTED_NUMBER_PRECISION;
            algorithm = RRD_ALGORITHM_ABSOLUTE;
            break;

        case PERF_COUNTER_RAWCOUNT:
        case PERF_COUNTER_LARGE_RAWCOUNT:
            // N as decimal
            algorithm = RRD_ALGORITHM_ABSOLUTE;
            break;

        case PERF_COUNTER_RAWCOUNT_HEX:
        case PERF_COUNTER_LARGE_RAWCOUNT_HEX:
            // N as hexadecimal
            algorithm = RRD_ALGORITHM_ABSOLUTE;
            break;

        case PERF_COUNTER_DELTA:
        case PERF_COUNTER_LARGE_DELTA:
            // N1 - N0
            algorithm = RRD_ALGORITHM_ABSOLUTE;
            break;

        case PERF_RAW_FRACTION:
        case PERF_LARGE_RAW_FRACTION:
            // 100 * N / B
            algorithm = RRD_ALGORITHM_ABSOLUTE;
            divider *= COLLECTED_NUMBER_PRECISION;
            break;

        case PERF_AVERAGE_TIMER:
            // ((N1 - N0) / TB) / (B1 - B0)
            // divider *= cd->current.Frequency / 10000000;
            algorithm = RRD_ALGORITHM_INCREMENTAL;
            break;

        case PERF_ELAPSED_TIME:
            // (D0 - N0) / F
            algorithm = RRD_ALGORITHM_ABSOLUTE;
            break;

        case PERF_COUNTER_TEXT:
        case PERF_SAMPLE_BASE:
        case PERF_AVERAGE_BASE:
        case PERF_COUNTER_MULTI_BASE:
        case PERF_RAW_BASE:
        case PERF_COUNTER_NODATA:
        case PERF_PRECISION_TIMESTAMP:
        default:
            break;
    }

    return rrddim_add(st, id, name, multiplier, divider, algorithm);
}

#define VALID_DELTA(cd) \
    ((cd)->previous.Time > 0 && (cd)->current.Data >= (cd)->previous.Data && (cd)->current.Time > (cd)->previous.Time)

collected_number perflib_rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, COUNTER_DATA *cd) {
    ULONGLONG numerator = 0;
    LONGLONG denominator = 0;
    double doubleValue = 0.0;
    collected_number value;

    switch(cd->current.CounterType) {
        case PERF_COUNTER_COUNTER:
        case PERF_SAMPLE_COUNTER:
        case PERF_COUNTER_BULK_COUNT:
            // (N1 - N0) / ((D1 - D0) / F)
            value = (collected_number)cd->current.Data;
            break;

        case PERF_COUNTER_QUEUELEN_TYPE:
        case PERF_COUNTER_100NS_QUEUELEN_TYPE:
        case PERF_COUNTER_OBJ_TIME_QUEUELEN_TYPE:
        case PERF_COUNTER_LARGE_QUEUELEN_TYPE:
        case PERF_AVERAGE_BULK:  // normally not displayed
            // (N1 - N0) / (D1 - D0)
            value = (collected_number)cd->current.Data;
            break;

        case PERF_OBJ_TIME_TIMER:
        case PERF_COUNTER_TIMER:
        case PERF_100NSEC_TIMER:
        case PERF_PRECISION_SYSTEM_TIMER:
        case PERF_PRECISION_100NS_TIMER:
        case PERF_PRECISION_OBJECT_TIMER:
        case PERF_SAMPLE_FRACTION:
            // 100 * (N1 - N0) / (D1 - D0)
            value = (collected_number)cd->current.Data;
            break;

        case PERF_COUNTER_TIMER_INV:
        case PERF_100NSEC_TIMER_INV:
            // 100 * (1 - ((N1 - N0) / (D1 - D0)))
            if(!VALID_DELTA(cd)) return 0;
            numerator = cd->current.Data - cd->previous.Data;
            denominator = cd->current.Time - cd->previous.Time;
            doubleValue = 100.0 * (1.0 - ((double)numerator / (double)denominator));
            // printf("Display value is (timer-inv): %f%%\n", doubleValue);
            value = (collected_number)(doubleValue * COLLECTED_NUMBER_PRECISION);
            break;

        case PERF_COUNTER_MULTI_TIMER:
            // 100 * ((N1 - N0) / ((D1 - D0) / TB)) / B1
            if(!VALID_DELTA(cd)) return 0;
            numerator = cd->current.Data - cd->previous.Data;
            denominator = cd->current.Time - cd->previous.Time;
            denominator /= cd->current.Frequency;
            doubleValue = 100.0 * ((double)numerator / (double)denominator) / cd->current.MultiCounterData;
            // printf("Display value is (multi-timer): %f%%\n", doubleValue);
            value = (collected_number)(doubleValue * COLLECTED_NUMBER_PRECISION);
            break;

        case PERF_100NSEC_MULTI_TIMER:
            // 100 * ((N1 - N0) / (D1 - D0)) / B1
            if(!VALID_DELTA(cd)) return 0;
            numerator = cd->current.Data - cd->previous.Data;
            denominator = cd->current.Time - cd->previous.Time;
            doubleValue = 100.0 * ((double)numerator / (double)denominator) / (double)cd->current.MultiCounterData;
            // printf("Display value is (100ns multi-timer): %f%%\n", doubleValue);
            value = (collected_number)(doubleValue * COLLECTED_NUMBER_PRECISION);
            break;

        case PERF_COUNTER_MULTI_TIMER_INV:
        case PERF_100NSEC_MULTI_TIMER_INV:
            // 100 * (B1 - ((N1 - N0) / (D1 - D0)))
            if(!VALID_DELTA(cd)) return 0;
            numerator = cd->current.Data - cd->previous.Data;
            denominator = cd->current.Time - cd->previous.Time;
            doubleValue = 100.0 * ((double)cd->current.MultiCounterData - ((double)numerator / (double)denominator));
            // printf("Display value is (multi-timer-inv): %f%%\n", doubleValue);
            value = (collected_number)(doubleValue * COLLECTED_NUMBER_PRECISION);
            break;

        case PERF_COUNTER_RAWCOUNT:
        case PERF_COUNTER_LARGE_RAWCOUNT:
            // N as decimal
            value = (collected_number)cd->current.Data;
            break;

        case PERF_COUNTER_RAWCOUNT_HEX:
        case PERF_COUNTER_LARGE_RAWCOUNT_HEX:
            // N as hexadecimal
            value = (collected_number)cd->current.Data;
            break;

        case PERF_COUNTER_DELTA:
        case PERF_COUNTER_LARGE_DELTA:
            if(!VALID_DELTA(cd)) return 0;
            value = (collected_number)(cd->current.Data - cd->previous.Data);
            break;

        case PERF_RAW_FRACTION:
        case PERF_LARGE_RAW_FRACTION:
            // 100 * N / B
            if(!cd->current.Time) return 0;
            doubleValue = 100.0 * (double)cd->current.Data / (double)cd->current.Time;
            // printf("Display value is (fraction): %f%%\n", doubleValue);
            value = (collected_number)(doubleValue * COLLECTED_NUMBER_PRECISION);
            break;

        default:
            return 0;
    }

    return rrddim_set_by_pointer(st, rd, value);
}

/*
double perflibCalculateValue(RAW_DATA *current, RAW_DATA *previous) {
    ULONGLONG numerator = 0;
    LONGLONG denominator = 0;
    double doubleValue = 0.0;
    DWORD dwordValue = 0;

    if (NULL == previous) {
        // Return error if the counter type requires two samples to calculate the value.
        switch (current->CounterType) {
            default:
                if (PERF_DELTA_COUNTER != (current->CounterType & PERF_DELTA_COUNTER))
                    break;
                __fallthrough;
                // fallthrough

            case PERF_AVERAGE_TIMER: // Special case.
            case PERF_AVERAGE_BULK:  // Special case.
                // printf(" > The counter type requires two samples but only one sample was provided.\n");
                return NAN;
        }
    }
    else {
        if (current->CounterType != previous->CounterType) {
            // printf(" > The samples have inconsistent counter types.\n");
            return NAN;
        }

        // Check for integer overflow or bad data from provider (the data from
        // sample 2 must be greater than the data from sample 1).
        if (current->Data < previous->Data)
        {
            // Can happen for various reasons. Commonly occurs with the Process counterset when
            // multiple processes have the same name and one of them starts or stops.
            // Normally you'll just drop the older sample and continue.
            // printf("> current (%llu) is smaller than previous (%llu).\n", current->Data, previous->Data);
            return NAN;
        }
    }

    switch (current->CounterType) {
        case PERF_COUNTER_COUNTER:
        case PERF_SAMPLE_COUNTER:
        case PERF_COUNTER_BULK_COUNT:
            // (N1 - N0) / ((D1 - D0) / F)
            numerator = current->Data - previous->Data;
            denominator = current->Time - previous->Time;
            dwordValue = (DWORD)(numerator / ((double)denominator / current->Frequency));
            //printf("Display value is (counter): %lu%s\n", (unsigned long)dwordValue,
            //       (previous->CounterType == PERF_SAMPLE_COUNTER) ? "" : "/sec");
            return (double)dwordValue;

        case PERF_COUNTER_QUEUELEN_TYPE:
        case PERF_COUNTER_100NS_QUEUELEN_TYPE:
        case PERF_COUNTER_OBJ_TIME_QUEUELEN_TYPE:
        case PERF_COUNTER_LARGE_QUEUELEN_TYPE:
        case PERF_AVERAGE_BULK:  // normally not displayed
            // (N1 - N0) / (D1 - D0)
            numerator = current->Data - previous->Data;
            denominator = current->Time - previous->Time;
            doubleValue = (double)numerator / denominator;
            if (previous->CounterType != PERF_AVERAGE_BULK) {
                // printf("Display value is (queuelen): %f\n", doubleValue);
                return doubleValue;
            }
            return NAN;

        case PERF_OBJ_TIME_TIMER:
        case PERF_COUNTER_TIMER:
        case PERF_100NSEC_TIMER:
        case PERF_PRECISION_SYSTEM_TIMER:
        case PERF_PRECISION_100NS_TIMER:
        case PERF_PRECISION_OBJECT_TIMER:
        case PERF_SAMPLE_FRACTION:
            // 100 * (N1 - N0) / (D1 - D0)
            numerator = current->Data - previous->Data;
            denominator = current->Time - previous->Time;
            doubleValue = (double)(100 * numerator) / denominator;
            // printf("Display value is (timer): %f%%\n", doubleValue);
            return doubleValue;

        case PERF_COUNTER_TIMER_INV:
            // 100 * (1 - ((N1 - N0) / (D1 - D0)))
            numerator = current->Data - previous->Data;
            denominator = current->Time - previous->Time;
            doubleValue = 100 * (1 - ((double)numerator / denominator));
            // printf("Display value is (timer-inv): %f%%\n", doubleValue);
            return doubleValue;

        case PERF_100NSEC_TIMER_INV:
            // 100 * (1- (N1 - N0) / (D1 - D0))
            numerator = current->Data - previous->Data;
            denominator = current->Time - previous->Time;
            doubleValue = 100 * (1 - (double)numerator / denominator);
            // printf("Display value is (100ns-timer-inv): %f%%\n", doubleValue);
            return doubleValue;

        case PERF_COUNTER_MULTI_TIMER:
            // 100 * ((N1 - N0) / ((D1 - D0) / TB)) / B1
            numerator = current->Data - previous->Data;
            denominator = current->Time - previous->Time;
            denominator /= current->Frequency;
            doubleValue = 100 * ((double)numerator / denominator) / current->MultiCounterData;
            // printf("Display value is (multi-timer): %f%%\n", doubleValue);
            return doubleValue;

        case PERF_100NSEC_MULTI_TIMER:
            // 100 * ((N1 - N0) / (D1 - D0)) / B1
            numerator = current->Data - previous->Data;
            denominator = current->Time - previous->Time;
            doubleValue = 100 * ((double)numerator / (double)denominator) / (double)current->MultiCounterData;
            // printf("Display value is (100ns multi-timer): %f%%\n", doubleValue);
            return doubleValue;

        case PERF_COUNTER_MULTI_TIMER_INV:
        case PERF_100NSEC_MULTI_TIMER_INV:
            // 100 * (B1 - ((N1 - N0) / (D1 - D0)))
            numerator = current->Data - previous->Data;
            denominator = current->Time - previous->Time;
            doubleValue = 100.0 * ((double)current->MultiCounterData - ((double)numerator / (double)denominator));
            // printf("Display value is (multi-timer-inv): %f%%\n", doubleValue);
            return doubleValue;

        case PERF_COUNTER_RAWCOUNT:
        case PERF_COUNTER_LARGE_RAWCOUNT:
            // N as decimal
            // printf("Display value is (rawcount): %llu\n", current->Data);
            return (double)current->Data;

        case PERF_COUNTER_RAWCOUNT_HEX:
        case PERF_COUNTER_LARGE_RAWCOUNT_HEX:
            // N as hexadecimal
            // printf("Display value is (hex): 0x%llx\n", current->Data);
            return (double)current->Data;

        case PERF_COUNTER_DELTA:
        case PERF_COUNTER_LARGE_DELTA:
            // N1 - N0
            // printf("Display value is (delta): %llu\n", current->Data - previous->Data);
            return (double)(current->Data - previous->Data);

        case PERF_RAW_FRACTION:
        case PERF_LARGE_RAW_FRACTION:
            // 100 * N / B
            doubleValue = 100.0 * (double)current->Data / (double)current->Time;
            // printf("Display value is (fraction): %f%%\n", doubleValue);
            return doubleValue;

        case PERF_AVERAGE_TIMER:
            // ((N1 - N0) / TB) / (B1 - B0)
            numerator = current->Data - previous->Data;
            denominator = current->Time - previous->Time;
            doubleValue = (double)numerator / (double)current->Frequency / (double)denominator;
            // printf("Display value is (average timer): %f seconds\n", doubleValue);
            return doubleValue;

        case PERF_ELAPSED_TIME:
            // (D0 - N0) / F
            doubleValue = (double)(current->Time - current->Data) / (double)current->Frequency;
            // printf("Display value is (elapsed time): %f seconds\n", doubleValue);
            return doubleValue;

        case PERF_COUNTER_TEXT:
        case PERF_SAMPLE_BASE:
        case PERF_AVERAGE_BASE:
        case PERF_COUNTER_MULTI_BASE:
        case PERF_RAW_BASE:
        case PERF_COUNTER_NODATA:
        case PERF_PRECISION_TIMESTAMP:
            // printf(" > Non-printing counter type: 0x%08x\n", current->CounterType);
            return NAN;
            break;

        default:
            // printf(" > Unrecognized counter type: 0x%08x\n", current->CounterType);
            return NAN;
            break;
    }
}
*/