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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "common.h"
#define GLOBAL_STATS_RESET_WEB_USEC_MAX 0x01
#define WORKER_JOB_GLOBAL 0
#define WORKER_JOB_REGISTRY 1
#define WORKER_JOB_WORKERS 2
#define WORKER_JOB_DBENGINE 3
#define WORKER_JOB_HEARTBEAT 4
#if WORKER_UTILIZATION_MAX_JOB_TYPES < 5
#error WORKER_UTILIZATION_MAX_JOB_TYPES has to be at least 5
#endif
static struct global_statistics {
volatile uint16_t connected_clients;
volatile uint64_t web_requests;
volatile uint64_t web_usec;
volatile uint64_t web_usec_max;
volatile uint64_t bytes_received;
volatile uint64_t bytes_sent;
volatile uint64_t content_size;
volatile uint64_t compressed_content_size;
volatile uint64_t web_client_count;
volatile uint64_t rrdr_queries_made;
volatile uint64_t rrdr_db_points_read;
volatile uint64_t rrdr_result_points_generated;
} global_statistics = {
.connected_clients = 0,
.web_requests = 0,
.web_usec = 0,
.bytes_received = 0,
.bytes_sent = 0,
.content_size = 0,
.compressed_content_size = 0,
.web_client_count = 1,
.rrdr_queries_made = 0,
.rrdr_db_points_read = 0,
.rrdr_result_points_generated = 0,
};
void rrdr_query_completed(uint64_t db_points_read, uint64_t result_points_generated) {
__atomic_fetch_add(&global_statistics.rrdr_queries_made, 1, __ATOMIC_SEQ_CST);
__atomic_fetch_add(&global_statistics.rrdr_db_points_read, db_points_read, __ATOMIC_SEQ_CST);
__atomic_fetch_add(&global_statistics.rrdr_result_points_generated, result_points_generated, __ATOMIC_SEQ_CST);
}
void finished_web_request_statistics(uint64_t dt,
uint64_t bytes_received,
uint64_t bytes_sent,
uint64_t content_size,
uint64_t compressed_content_size) {
uint64_t old_web_usec_max = global_statistics.web_usec_max;
while(dt > old_web_usec_max)
__atomic_compare_exchange(&global_statistics.web_usec_max, &old_web_usec_max, &dt, 1, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
__atomic_fetch_add(&global_statistics.web_requests, 1, __ATOMIC_SEQ_CST);
__atomic_fetch_add(&global_statistics.web_usec, dt, __ATOMIC_SEQ_CST);
__atomic_fetch_add(&global_statistics.bytes_received, bytes_received, __ATOMIC_SEQ_CST);
__atomic_fetch_add(&global_statistics.bytes_sent, bytes_sent, __ATOMIC_SEQ_CST);
__atomic_fetch_add(&global_statistics.content_size, content_size, __ATOMIC_SEQ_CST);
__atomic_fetch_add(&global_statistics.compressed_content_size, compressed_content_size, __ATOMIC_SEQ_CST);
}
uint64_t web_client_connected(void) {
__atomic_fetch_add(&global_statistics.connected_clients, 1, __ATOMIC_SEQ_CST);
return __atomic_fetch_add(&global_statistics.web_client_count, 1, __ATOMIC_SEQ_CST);
}
void web_client_disconnected(void) {
__atomic_fetch_sub(&global_statistics.connected_clients, 1, __ATOMIC_SEQ_CST);
}
static inline void global_statistics_copy(struct global_statistics *gs, uint8_t options) {
gs->connected_clients = __atomic_fetch_add(&global_statistics.connected_clients, 0, __ATOMIC_SEQ_CST);
gs->web_requests = __atomic_fetch_add(&global_statistics.web_requests, 0, __ATOMIC_SEQ_CST);
gs->web_usec = __atomic_fetch_add(&global_statistics.web_usec, 0, __ATOMIC_SEQ_CST);
gs->web_usec_max = __atomic_fetch_add(&global_statistics.web_usec_max, 0, __ATOMIC_SEQ_CST);
gs->bytes_received = __atomic_fetch_add(&global_statistics.bytes_received, 0, __ATOMIC_SEQ_CST);
gs->bytes_sent = __atomic_fetch_add(&global_statistics.bytes_sent, 0, __ATOMIC_SEQ_CST);
gs->content_size = __atomic_fetch_add(&global_statistics.content_size, 0, __ATOMIC_SEQ_CST);
gs->compressed_content_size = __atomic_fetch_add(&global_statistics.compressed_content_size, 0, __ATOMIC_SEQ_CST);
gs->web_client_count = __atomic_fetch_add(&global_statistics.web_client_count, 0, __ATOMIC_SEQ_CST);
gs->rrdr_queries_made = __atomic_fetch_add(&global_statistics.rrdr_queries_made, 0, __ATOMIC_SEQ_CST);
gs->rrdr_db_points_read = __atomic_fetch_add(&global_statistics.rrdr_db_points_read, 0, __ATOMIC_SEQ_CST);
gs->rrdr_result_points_generated = __atomic_fetch_add(&global_statistics.rrdr_result_points_generated, 0, __ATOMIC_SEQ_CST);
if(options & GLOBAL_STATS_RESET_WEB_USEC_MAX) {
uint64_t n = 0;
__atomic_compare_exchange(&global_statistics.web_usec_max, (uint64_t *) &gs->web_usec_max, &n, 1, __ATOMIC_SEQ_CST,
__ATOMIC_SEQ_CST);
}
}
static void global_statistics_charts(void) {
static unsigned long long old_web_requests = 0,
old_web_usec = 0,
old_content_size = 0,
old_compressed_content_size = 0;
static collected_number compression_ratio = -1,
average_response_time = -1;
static time_t netdata_start_time = 0;
if (!netdata_start_time)
netdata_start_time = now_boottime_sec();
time_t netdata_uptime = now_boottime_sec() - netdata_start_time;
struct global_statistics gs;
struct rusage me;
global_statistics_copy(&gs, GLOBAL_STATS_RESET_WEB_USEC_MAX);
getrusage(RUSAGE_SELF, &me);
// ----------------------------------------------------------------
{
static RRDSET *st_cpu = NULL;
static RRDDIM *rd_cpu_user = NULL,
*rd_cpu_system = NULL;
if (unlikely(!st_cpu)) {
st_cpu = rrdset_create_localhost(
"netdata"
, "server_cpu"
, NULL
, "netdata"
, NULL
, "Netdata CPU usage"
, "milliseconds/s"
, "netdata"
, "stats"
, 130000
, localhost->rrd_update_every
, RRDSET_TYPE_STACKED
);
rd_cpu_user = rrddim_add(st_cpu, "user", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL);
rd_cpu_system = rrddim_add(st_cpu, "system", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL);
}
else
rrdset_next(st_cpu);
rrddim_set_by_pointer(st_cpu, rd_cpu_user, me.ru_utime.tv_sec * 1000000ULL + me.ru_utime.tv_usec);
rrddim_set_by_pointer(st_cpu, rd_cpu_system, me.ru_stime.tv_sec * 1000000ULL + me.ru_stime.tv_usec);
rrdset_done(st_cpu);
}
// ----------------------------------------------------------------
{
static RRDSET *st_uptime = NULL;
static RRDDIM *rd_uptime = NULL;
if (unlikely(!st_uptime)) {
st_uptime = rrdset_create_localhost(
"netdata",
"uptime",
NULL,
"netdata",
NULL,
"Netdata uptime",
"seconds",
"netdata",
"stats",
130100,
localhost->rrd_update_every,
RRDSET_TYPE_LINE);
rd_uptime = rrddim_add(st_uptime, "uptime", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
} else
rrdset_next(st_uptime);
rrddim_set_by_pointer(st_uptime, rd_uptime, netdata_uptime);
rrdset_done(st_uptime);
}
// ----------------------------------------------------------------
{
static RRDSET *st_clients = NULL;
static RRDDIM *rd_clients = NULL;
if (unlikely(!st_clients)) {
st_clients = rrdset_create_localhost(
"netdata"
, "clients"
, NULL
, "api"
, NULL
, "Netdata Web Clients"
, "connected clients"
, "netdata"
, "stats"
, 130200
, localhost->rrd_update_every
, RRDSET_TYPE_LINE
);
rd_clients = rrddim_add(st_clients, "clients", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
}
else
rrdset_next(st_clients);
rrddim_set_by_pointer(st_clients, rd_clients, gs.connected_clients);
rrdset_done(st_clients);
}
// ----------------------------------------------------------------
{
static RRDSET *st_reqs = NULL;
static RRDDIM *rd_requests = NULL;
if (unlikely(!st_reqs)) {
st_reqs = rrdset_create_localhost(
"netdata"
, "requests"
, NULL
, "api"
, NULL
, "Netdata Web Requests"
, "requests/s"
, "netdata"
, "stats"
, 130300
, localhost->rrd_update_every
, RRDSET_TYPE_LINE
);
rd_requests = rrddim_add(st_reqs, "requests", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
}
else
rrdset_next(st_reqs);
rrddim_set_by_pointer(st_reqs, rd_requests, (collected_number) gs.web_requests);
rrdset_done(st_reqs);
}
// ----------------------------------------------------------------
{
static RRDSET *st_bytes = NULL;
static RRDDIM *rd_in = NULL,
*rd_out = NULL;
if (unlikely(!st_bytes)) {
st_bytes = rrdset_create_localhost(
"netdata"
, "net"
, NULL
, "api"
, NULL
, "Netdata Network Traffic"
, "kilobits/s"
, "netdata"
, "stats"
, 130400
, localhost->rrd_update_every
, RRDSET_TYPE_AREA
);
rd_in = rrddim_add(st_bytes, "in", NULL, 8, BITS_IN_A_KILOBIT, RRD_ALGORITHM_INCREMENTAL);
rd_out = rrddim_add(st_bytes, "out", NULL, -8, BITS_IN_A_KILOBIT, RRD_ALGORITHM_INCREMENTAL);
}
else
rrdset_next(st_bytes);
rrddim_set_by_pointer(st_bytes, rd_in, (collected_number) gs.bytes_received);
rrddim_set_by_pointer(st_bytes, rd_out, (collected_number) gs.bytes_sent);
rrdset_done(st_bytes);
}
// ----------------------------------------------------------------
{
static RRDSET *st_duration = NULL;
static RRDDIM *rd_average = NULL,
*rd_max = NULL;
if (unlikely(!st_duration)) {
st_duration = rrdset_create_localhost(
"netdata"
, "response_time"
, NULL
, "api"
, NULL
, "Netdata API Response Time"
, "milliseconds/request"
, "netdata"
, "stats"
, 130500
, localhost->rrd_update_every
, RRDSET_TYPE_LINE
);
rd_average = rrddim_add(st_duration, "average", NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
rd_max = rrddim_add(st_duration, "max", NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
}
else
rrdset_next(st_duration);
uint64_t gweb_usec = gs.web_usec;
uint64_t gweb_requests = gs.web_requests;
uint64_t web_usec = (gweb_usec >= old_web_usec) ? gweb_usec - old_web_usec : 0;
uint64_t web_requests = (gweb_requests >= old_web_requests) ? gweb_requests - old_web_requests : 0;
old_web_usec = gweb_usec;
old_web_requests = gweb_requests;
if (web_requests)
average_response_time = (collected_number) (web_usec / web_requests);
if (unlikely(average_response_time != -1))
rrddim_set_by_pointer(st_duration, rd_average, average_response_time);
else
rrddim_set_by_pointer(st_duration, rd_average, 0);
rrddim_set_by_pointer(st_duration, rd_max, ((gs.web_usec_max)?(collected_number)gs.web_usec_max:average_response_time));
rrdset_done(st_duration);
}
// ----------------------------------------------------------------
{
static RRDSET *st_compression = NULL;
static RRDDIM *rd_savings = NULL;
if (unlikely(!st_compression)) {
st_compression = rrdset_create_localhost(
"netdata"
, "compression_ratio"
, NULL
, "api"
, NULL
, "Netdata API Responses Compression Savings Ratio"
, "percentage"
, "netdata"
, "stats"
, 130600
, localhost->rrd_update_every
, RRDSET_TYPE_LINE
);
rd_savings = rrddim_add(st_compression, "savings", NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
}
else
rrdset_next(st_compression);
// since we don't lock here to read the global statistics
// read the smaller value first
unsigned long long gcompressed_content_size = gs.compressed_content_size;
unsigned long long gcontent_size = gs.content_size;
unsigned long long compressed_content_size = gcompressed_content_size - old_compressed_content_size;
unsigned long long content_size = gcontent_size - old_content_size;
old_compressed_content_size = gcompressed_content_size;
old_content_size = gcontent_size;
if (content_size && content_size >= compressed_content_size)
compression_ratio = ((content_size - compressed_content_size) * 100 * 1000) / content_size;
if (compression_ratio != -1)
rrddim_set_by_pointer(st_compression, rd_savings, compression_ratio);
rrdset_done(st_compression);
}
// ----------------------------------------------------------------
if(gs.rrdr_queries_made) {
static RRDSET *st_rrdr_queries = NULL;
static RRDDIM *rd_queries = NULL;
if (unlikely(!st_rrdr_queries)) {
st_rrdr_queries = rrdset_create_localhost(
"netdata"
, "queries"
, NULL
, "queries"
, NULL
, "Netdata API Queries"
, "queries/s"
, "netdata"
, "stats"
, 131000
, localhost->rrd_update_every
, RRDSET_TYPE_LINE
);
rd_queries = rrddim_add(st_rrdr_queries, "queries", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
}
else
rrdset_next(st_rrdr_queries);
rrddim_set_by_pointer(st_rrdr_queries, rd_queries, (collected_number)gs.rrdr_queries_made);
rrdset_done(st_rrdr_queries);
}
// ----------------------------------------------------------------
if(gs.rrdr_db_points_read || gs.rrdr_result_points_generated) {
static RRDSET *st_rrdr_points = NULL;
static RRDDIM *rd_points_read = NULL;
static RRDDIM *rd_points_generated = NULL;
if (unlikely(!st_rrdr_points)) {
st_rrdr_points = rrdset_create_localhost(
"netdata"
, "db_points"
, NULL
, "queries"
, NULL
, "Netdata API Points"
, "points/s"
, "netdata"
, "stats"
, 131001
, localhost->rrd_update_every
, RRDSET_TYPE_AREA
);
rd_points_read = rrddim_add(st_rrdr_points, "read", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
rd_points_generated = rrddim_add(st_rrdr_points, "generated", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
}
else
rrdset_next(st_rrdr_points);
rrddim_set_by_pointer(st_rrdr_points, rd_points_read, (collected_number)gs.rrdr_db_points_read);
rrddim_set_by_pointer(st_rrdr_points, rd_points_generated, (collected_number)gs.rrdr_result_points_generated);
rrdset_done(st_rrdr_points);
}
// ----------------------------------------------------------------
}
static void dbengine_statistics_charts(void) {
#ifdef ENABLE_DBENGINE
if(netdata_rwlock_tryrdlock(&rrd_rwlock) == 0) {
RRDHOST *host;
unsigned long long stats_array[RRDENG_NR_STATS] = {0};
unsigned long long local_stats_array[RRDENG_NR_STATS];
unsigned dbengine_contexts = 0, counted_multihost_db = 0, i;
rrdhost_foreach_read(host) {
if (host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE && !rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED)) {
if (&multidb_ctx == host->rrdeng_ctx) {
if (counted_multihost_db)
continue; /* Only count multi-host DB once */
counted_multihost_db = 1;
}
++dbengine_contexts;
/* get localhost's DB engine's statistics */
rrdeng_get_37_statistics(host->rrdeng_ctx, local_stats_array);
for (i = 0; i < RRDENG_NR_STATS; ++i) {
/* aggregate statistics across hosts */
stats_array[i] += local_stats_array[i];
}
}
}
rrd_unlock();
if (dbengine_contexts) {
/* deduplicate global statistics by getting the ones from the last context */
stats_array[30] = local_stats_array[30];
stats_array[31] = local_stats_array[31];
stats_array[32] = local_stats_array[32];
stats_array[34] = local_stats_array[34];
stats_array[36] = local_stats_array[36];
// ----------------------------------------------------------------
{
static RRDSET *st_compression = NULL;
static RRDDIM *rd_savings = NULL;
if (unlikely(!st_compression)) {
st_compression = rrdset_create_localhost(
"netdata",
"dbengine_compression_ratio",
NULL,
"dbengine",
NULL,
"Netdata DB engine data extents' compression savings ratio",
"percentage",
"netdata",
"stats",
132000,
localhost->rrd_update_every,
RRDSET_TYPE_LINE);
rd_savings = rrddim_add(st_compression, "savings", NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
} else
rrdset_next(st_compression);
unsigned long long ratio;
unsigned long long compressed_content_size = stats_array[12];
unsigned long long content_size = stats_array[11];
if (content_size) {
// allow negative savings
ratio = ((content_size - compressed_content_size) * 100 * 1000) / content_size;
} else {
ratio = 0;
}
rrddim_set_by_pointer(st_compression, rd_savings, ratio);
rrdset_done(st_compression);
}
// ----------------------------------------------------------------
{
static RRDSET *st_pg_cache_hit_ratio = NULL;
static RRDDIM *rd_hit_ratio = NULL;
if (unlikely(!st_pg_cache_hit_ratio)) {
st_pg_cache_hit_ratio = rrdset_create_localhost(
"netdata",
"page_cache_hit_ratio",
NULL,
"dbengine",
NULL,
"Netdata DB engine page cache hit ratio",
"percentage",
"netdata",
"stats",
132003,
localhost->rrd_update_every,
RRDSET_TYPE_LINE);
rd_hit_ratio = rrddim_add(st_pg_cache_hit_ratio, "ratio", NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
} else
rrdset_next(st_pg_cache_hit_ratio);
static unsigned long long old_hits = 0;
static unsigned long long old_misses = 0;
unsigned long long hits = stats_array[7];
unsigned long long misses = stats_array[8];
unsigned long long hits_delta;
unsigned long long misses_delta;
unsigned long long ratio;
hits_delta = hits - old_hits;
misses_delta = misses - old_misses;
old_hits = hits;
old_misses = misses;
if (hits_delta + misses_delta) {
ratio = (hits_delta * 100 * 1000) / (hits_delta + misses_delta);
} else {
ratio = 0;
}
rrddim_set_by_pointer(st_pg_cache_hit_ratio, rd_hit_ratio, ratio);
rrdset_done(st_pg_cache_hit_ratio);
}
// ----------------------------------------------------------------
{
static RRDSET *st_pg_cache_pages = NULL;
static RRDDIM *rd_descriptors = NULL;
static RRDDIM *rd_populated = NULL;
static RRDDIM *rd_dirty = NULL;
static RRDDIM *rd_backfills = NULL;
static RRDDIM *rd_evictions = NULL;
static RRDDIM *rd_used_by_collectors = NULL;
if (unlikely(!st_pg_cache_pages)) {
st_pg_cache_pages = rrdset_create_localhost(
"netdata",
"page_cache_stats",
NULL,
"dbengine",
NULL,
"Netdata dbengine page cache statistics",
"pages",
"netdata",
"stats",
132004,
localhost->rrd_update_every,
RRDSET_TYPE_LINE);
rd_descriptors = rrddim_add(st_pg_cache_pages, "descriptors", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
rd_populated = rrddim_add(st_pg_cache_pages, "populated", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
rd_dirty = rrddim_add(st_pg_cache_pages, "dirty", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
rd_backfills = rrddim_add(st_pg_cache_pages, "backfills", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
rd_evictions = rrddim_add(st_pg_cache_pages, "evictions", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
rd_used_by_collectors =
rrddim_add(st_pg_cache_pages, "used_by_collectors", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
} else
rrdset_next(st_pg_cache_pages);
rrddim_set_by_pointer(st_pg_cache_pages, rd_descriptors, (collected_number)stats_array[27]);
rrddim_set_by_pointer(st_pg_cache_pages, rd_populated, (collected_number)stats_array[3]);
rrddim_set_by_pointer(st_pg_cache_pages, rd_dirty, (collected_number)stats_array[0] + stats_array[4]);
rrddim_set_by_pointer(st_pg_cache_pages, rd_backfills, (collected_number)stats_array[9]);
rrddim_set_by_pointer(st_pg_cache_pages, rd_evictions, (collected_number)stats_array[10]);
rrddim_set_by_pointer(st_pg_cache_pages, rd_used_by_collectors, (collected_number)stats_array[0]);
rrdset_done(st_pg_cache_pages);
}
// ----------------------------------------------------------------
{
static RRDSET *st_long_term_pages = NULL;
static RRDDIM *rd_total = NULL;
static RRDDIM *rd_insertions = NULL;
static RRDDIM *rd_deletions = NULL;
static RRDDIM *rd_flushing_pressure_deletions = NULL;
if (unlikely(!st_long_term_pages)) {
st_long_term_pages = rrdset_create_localhost(
"netdata",
"dbengine_long_term_page_stats",
NULL,
"dbengine",
NULL,
"Netdata dbengine long-term page statistics",
"pages",
"netdata",
"stats",
132005,
localhost->rrd_update_every,
RRDSET_TYPE_LINE);
rd_total = rrddim_add(st_long_term_pages, "total", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
rd_insertions = rrddim_add(st_long_term_pages, "insertions", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
rd_deletions = rrddim_add(st_long_term_pages, "deletions", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
rd_flushing_pressure_deletions = rrddim_add(
st_long_term_pages, "flushing_pressure_deletions", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
} else
rrdset_next(st_long_term_pages);
rrddim_set_by_pointer(st_long_term_pages, rd_total, (collected_number)stats_array[2]);
rrddim_set_by_pointer(st_long_term_pages, rd_insertions, (collected_number)stats_array[5]);
rrddim_set_by_pointer(st_long_term_pages, rd_deletions, (collected_number)stats_array[6]);
rrddim_set_by_pointer(
st_long_term_pages, rd_flushing_pressure_deletions, (collected_number)stats_array[36]);
rrdset_done(st_long_term_pages);
}
// ----------------------------------------------------------------
{
static RRDSET *st_io_stats = NULL;
static RRDDIM *rd_reads = NULL;
static RRDDIM *rd_writes = NULL;
if (unlikely(!st_io_stats)) {
st_io_stats = rrdset_create_localhost(
"netdata",
"dbengine_io_throughput",
NULL,
"dbengine",
NULL,
"Netdata DB engine I/O throughput",
"MiB/s",
"netdata",
"stats",
132006,
localhost->rrd_update_every,
RRDSET_TYPE_LINE);
rd_reads = rrddim_add(st_io_stats, "reads", NULL, 1, 1024 * 1024, RRD_ALGORITHM_INCREMENTAL);
rd_writes = rrddim_add(st_io_stats, "writes", NULL, -1, 1024 * 1024, RRD_ALGORITHM_INCREMENTAL);
} else
rrdset_next(st_io_stats);
rrddim_set_by_pointer(st_io_stats, rd_reads, (collected_number)stats_array[17]);
rrddim_set_by_pointer(st_io_stats, rd_writes, (collected_number)stats_array[15]);
rrdset_done(st_io_stats);
}
// ----------------------------------------------------------------
{
static RRDSET *st_io_stats = NULL;
static RRDDIM *rd_reads = NULL;
static RRDDIM *rd_writes = NULL;
if (unlikely(!st_io_stats)) {
st_io_stats = rrdset_create_localhost(
"netdata",
"dbengine_io_operations",
NULL,
"dbengine",
NULL,
"Netdata DB engine I/O operations",
"operations/s",
"netdata",
"stats",
132007,
localhost->rrd_update_every,
RRDSET_TYPE_LINE);
rd_reads = rrddim_add(st_io_stats, "reads", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
rd_writes = rrddim_add(st_io_stats, "writes", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
} else
rrdset_next(st_io_stats);
rrddim_set_by_pointer(st_io_stats, rd_reads, (collected_number)stats_array[18]);
rrddim_set_by_pointer(st_io_stats, rd_writes, (collected_number)stats_array[16]);
rrdset_done(st_io_stats);
}
// ----------------------------------------------------------------
{
static RRDSET *st_errors = NULL;
static RRDDIM *rd_fs_errors = NULL;
static RRDDIM *rd_io_errors = NULL;
static RRDDIM *pg_cache_over_half_dirty_events = NULL;
if (unlikely(!st_errors)) {
st_errors = rrdset_create_localhost(
"netdata",
"dbengine_global_errors",
NULL,
"dbengine",
NULL,
"Netdata DB engine errors",
"errors/s",
"netdata",
"stats",
132008,
localhost->rrd_update_every,
RRDSET_TYPE_LINE);
rd_io_errors = rrddim_add(st_errors, "io_errors", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
rd_fs_errors = rrddim_add(st_errors, "fs_errors", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
pg_cache_over_half_dirty_events =
rrddim_add(st_errors, "pg_cache_over_half_dirty_events", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
} else
rrdset_next(st_errors);
rrddim_set_by_pointer(st_errors, rd_io_errors, (collected_number)stats_array[30]);
rrddim_set_by_pointer(st_errors, rd_fs_errors, (collected_number)stats_array[31]);
rrddim_set_by_pointer(st_errors, pg_cache_over_half_dirty_events, (collected_number)stats_array[34]);
rrdset_done(st_errors);
}
// ----------------------------------------------------------------
{
static RRDSET *st_fd = NULL;
static RRDDIM *rd_fd_current = NULL;
static RRDDIM *rd_fd_max = NULL;
if (unlikely(!st_fd)) {
st_fd = rrdset_create_localhost(
"netdata",
"dbengine_global_file_descriptors",
NULL,
"dbengine",
NULL,
"Netdata DB engine File Descriptors",
"descriptors",
"netdata",
"stats",
132009,
localhost->rrd_update_every,
RRDSET_TYPE_LINE);
rd_fd_current = rrddim_add(st_fd, "current", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
rd_fd_max = rrddim_add(st_fd, "max", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
} else
rrdset_next(st_fd);
rrddim_set_by_pointer(st_fd, rd_fd_current, (collected_number)stats_array[32]);
/* Careful here, modify this accordingly if the File-Descriptor budget ever changes */
rrddim_set_by_pointer(st_fd, rd_fd_max, (collected_number)rlimit_nofile.rlim_cur / 4);
rrdset_done(st_fd);
}
// ----------------------------------------------------------------
{
static RRDSET *st_ram_usage = NULL;
static RRDDIM *rd_cached = NULL;
static RRDDIM *rd_pinned = NULL;
static RRDDIM *rd_cache_metadata = NULL;
static RRDDIM *rd_index_metadata = NULL;
static RRDDIM *rd_pages_metadata = NULL;
collected_number cached_pages, pinned_pages, API_producers, populated_pages, cache_metadata, pages_on_disk,
page_cache_descriptors, index_metadata, pages_metadata;
if (unlikely(!st_ram_usage)) {
st_ram_usage = rrdset_create_localhost(
"netdata",
"dbengine_ram",
NULL,
"dbengine",
NULL,
"Netdata DB engine RAM usage",
"MiB",
"netdata",
"stats",
132010,
localhost->rrd_update_every,
RRDSET_TYPE_STACKED);
rd_cached = rrddim_add(st_ram_usage, "cache", NULL, RRDENG_BLOCK_SIZE, 1024*1024, RRD_ALGORITHM_ABSOLUTE);
rd_pinned = rrddim_add(st_ram_usage, "collectors", NULL, RRDENG_BLOCK_SIZE, 1024*1024, RRD_ALGORITHM_ABSOLUTE);
rd_cache_metadata = rrddim_add(st_ram_usage, "cache metadata", NULL, 1, 1024*1024, RRD_ALGORITHM_ABSOLUTE);
rd_pages_metadata = rrddim_add(st_ram_usage, "pages metadata", NULL, 1, 1024*1024, RRD_ALGORITHM_ABSOLUTE);
rd_index_metadata = rrddim_add(st_ram_usage, "index metadata", NULL, 1, 1024*1024, RRD_ALGORITHM_ABSOLUTE);
} else
rrdset_next(st_ram_usage);
API_producers = (collected_number)stats_array[0];
pages_on_disk = (collected_number)stats_array[2];
populated_pages = (collected_number)stats_array[3];
page_cache_descriptors = (collected_number)stats_array[27];
if (API_producers * 2 > populated_pages) {
pinned_pages = API_producers;
} else {
pinned_pages = API_producers * 2;
}
cached_pages = populated_pages - pinned_pages;
cache_metadata = page_cache_descriptors * sizeof(struct page_cache_descr);
pages_metadata = pages_on_disk * sizeof(struct rrdeng_page_descr);
/* This is an empirical estimation for Judy array indexing and extent structures */
index_metadata = pages_on_disk * 58;
rrddim_set_by_pointer(st_ram_usage, rd_cached, cached_pages);
rrddim_set_by_pointer(st_ram_usage, rd_pinned, pinned_pages);
rrddim_set_by_pointer(st_ram_usage, rd_cache_metadata, cache_metadata);
rrddim_set_by_pointer(st_ram_usage, rd_pages_metadata, pages_metadata);
rrddim_set_by_pointer(st_ram_usage, rd_index_metadata, index_metadata);
rrdset_done(st_ram_usage);
}
}
}
#endif
}
static void update_heartbeat_charts() {
static RRDSET *st_heartbeat = NULL;
static RRDDIM *rd_heartbeat_min = NULL;
static RRDDIM *rd_heartbeat_max = NULL;
static RRDDIM *rd_heartbeat_avg = NULL;
if (unlikely(!st_heartbeat)) {
st_heartbeat = rrdset_create_localhost(
"netdata"
, "heartbeat"
, NULL
, "heartbeat"
, NULL
, "System clock jitter"
, "microseconds"
, "netdata"
, "stats"
, 900000
, localhost->rrd_update_every
, RRDSET_TYPE_AREA);
rd_heartbeat_min = rrddim_add(st_heartbeat, "min", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
rd_heartbeat_max = rrddim_add(st_heartbeat, "max", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
rd_heartbeat_avg = rrddim_add(st_heartbeat, "average", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
} else
rrdset_next(st_heartbeat);
usec_t min, max, average;
size_t count;
heartbeat_statistics(&min, &max, &average, &count);
rrddim_set_by_pointer(st_heartbeat, rd_heartbeat_min, (collected_number)min);
rrddim_set_by_pointer(st_heartbeat, rd_heartbeat_max, (collected_number)max);
rrddim_set_by_pointer(st_heartbeat, rd_heartbeat_avg, (collected_number)average);
rrdset_done(st_heartbeat);
}
// ---------------------------------------------------------------------------------------------------------------------
// worker utilization
#define WORKERS_MIN_PERCENT_DEFAULT 10000.0
struct worker_job_type {
char name[WORKER_UTILIZATION_MAX_JOB_NAME_LENGTH + 1];
size_t jobs_started;
usec_t busy_time;
RRDDIM *rd_jobs_started;
RRDDIM *rd_busy_time;
};
struct worker_thread {
pid_t pid;
int enabled;
int cpu_enabled;
double cpu;
kernel_uint_t utime;
kernel_uint_t stime;
kernel_uint_t utime_old;
kernel_uint_t stime_old;
usec_t collected_time;
usec_t collected_time_old;
size_t jobs_started;
usec_t busy_time;
struct worker_thread *next;
};
struct worker_utilization {
const char *name;
const char *family;
size_t priority;
uint32_t flags;
char *name_lowercase;
struct worker_job_type per_job_type[WORKER_UTILIZATION_MAX_JOB_TYPES];
size_t workers_registered;
size_t workers_busy;
usec_t workers_total_busy_time;
usec_t workers_total_duration;
size_t workers_total_jobs_started;
double workers_min_busy_time;
double workers_max_busy_time;
size_t workers_cpu_registered;
double workers_cpu_min;
double workers_cpu_max;
double workers_cpu_total;
struct worker_thread *threads;
RRDSET *st_workers_time;
RRDDIM *rd_workers_time_avg;
RRDDIM *rd_workers_time_min;
RRDDIM *rd_workers_time_max;
RRDSET *st_workers_cpu;
RRDDIM *rd_workers_cpu_avg;
RRDDIM *rd_workers_cpu_min;
RRDDIM *rd_workers_cpu_max;
RRDSET *st_workers_threads;
RRDDIM *rd_workers_threads_free;
RRDDIM *rd_workers_threads_busy;
RRDSET *st_workers_jobs_per_job_type;
RRDSET *st_workers_busy_per_job_type;
RRDDIM *rd_total_cpu_utilizaton;
};
static struct worker_utilization all_workers_utilization[] = {
{ .name = "STATS", .family = "workers global statistics", .priority = 1000000 },
{ .name = "HEALTH", .family = "workers health alarms", .priority = 1000000 },
{ .name = "MLTRAIN", .family = "workers ML training", .priority = 1000000 },
{ .name = "MLDETECT", .family = "workers ML detection", .priority = 1000000 },
{ .name = "STREAMRCV", .family = "workers streaming receive", .priority = 1000000 },
{ .name = "STREAMSND", .family = "workers streaming send", .priority = 1000000 },
{ .name = "DBENGINE", .family = "workers dbengine instances", .priority = 1000000 },
{ .name = "WEB", .family = "workers web server", .priority = 1000000 },
{ .name = "ACLKQUERY", .family = "workers aclk query", .priority = 1000000 },
{ .name = "ACLKSYNC", .family = "workers aclk host sync", .priority = 1000000 },
{ .name = "PLUGINSD", .family = "workers plugins.d", .priority = 1000000 },
{ .name = "STATSD", .family = "workers plugin statsd", .priority = 1000000 },
{ .name = "STATSDFLUSH", .family = "workers plugin statsd flush", .priority = 1000000 },
{ .name = "PROC", .family = "workers plugin proc", .priority = 1000000 },
{ .name = "NETDEV", .family = "workers plugin proc netdev", .priority = 1000000 },
{ .name = "FREEBSD", .family = "workers plugin freebsd", .priority = 1000000 },
{ .name = "MACOS", .family = "workers plugin macos", .priority = 1000000 },
{ .name = "CGROUPS", .family = "workers plugin cgroups", .priority = 1000000 },
{ .name = "CGROUPSDISC", .family = "workers plugin cgroups find", .priority = 1000000 },
{ .name = "DISKSPACE", .family = "workers plugin diskspace", .priority = 1000000 },
{ .name = "TC", .family = "workers plugin tc", .priority = 1000000 },
{ .name = "TIMEX", .family = "workers plugin timex", .priority = 1000000 },
{ .name = "IDLEJITTER", .family = "workers plugin idlejitter", .priority = 1000000 },
// has to be terminated with a NULL
{ .name = NULL, .family = NULL }
};
static void workers_total_cpu_utilization_chart(void) {
size_t i, cpu_enabled = 0;
for(i = 0; all_workers_utilization[i].name ;i++)
if(all_workers_utilization[i].workers_cpu_registered) cpu_enabled++;
if(!cpu_enabled) return;
static RRDSET *st = NULL;
if(!st) {
st = rrdset_create_localhost(
"netdata",
"workers_cpu",
NULL,
"workers",
"netdata.workers.cpu_total",
"Netdata Workers CPU Utilization (100% = 1 core)",
"%",
"netdata",
"stats",
999000,
localhost->rrd_update_every,
RRDSET_TYPE_STACKED);
}
rrdset_next(st);
for(i = 0; all_workers_utilization[i].name ;i++) {
struct worker_utilization *wu = &all_workers_utilization[i];
if(!wu->workers_cpu_registered) continue;
if(!wu->rd_total_cpu_utilizaton)
wu->rd_total_cpu_utilizaton = rrddim_add(st, wu->name_lowercase, NULL, 1, 10000ULL, RRD_ALGORITHM_ABSOLUTE);
rrddim_set_by_pointer(st, wu->rd_total_cpu_utilizaton, (collected_number)((double)wu->workers_cpu_total * 10000.0));
}
rrdset_done(st);
}
static void workers_utilization_update_chart(struct worker_utilization *wu) {
if(!wu->workers_registered) return;
//fprintf(stderr, "%-12s WORKER UTILIZATION: %-3.2f%%, %zu jobs done, %zu running, on %zu workers, min %-3.02f%%, max %-3.02f%%.\n",
// wu->name,
// (double)wu->workers_total_busy_time * 100.0 / (double)wu->workers_total_duration,
// wu->workers_total_jobs_started, wu->workers_busy, wu->workers_registered,
// wu->workers_min_busy_time, wu->workers_max_busy_time);
// ----------------------------------------------------------------------
if(unlikely(!wu->st_workers_time)) {
char name[RRD_ID_LENGTH_MAX + 1];
snprintfz(name, RRD_ID_LENGTH_MAX, "workers_time_%s", wu->name_lowercase);
char context[RRD_ID_LENGTH_MAX + 1];
snprintf(context, RRD_ID_LENGTH_MAX, "netdata.workers.%s.time", wu->name_lowercase);
wu->st_workers_time = rrdset_create_localhost(
"netdata"
, name
, NULL
, wu->family
, context
, "Netdata Workers Busy Time (100% = all workers busy)"
, "%"
, "netdata"
, "stats"
, wu->priority
, localhost->rrd_update_every
, RRDSET_TYPE_AREA
);
}
// we add the min and max dimensions only when we have multiple workers
if(unlikely(!wu->rd_workers_time_min && wu->workers_registered > 1))
wu->rd_workers_time_min = rrddim_add(wu->st_workers_time, "min", NULL, 1, 10000, RRD_ALGORITHM_ABSOLUTE);
if(unlikely(!wu->rd_workers_time_max && wu->workers_registered > 1))
wu->rd_workers_time_max = rrddim_add(wu->st_workers_time, "max", NULL, 1, 10000, RRD_ALGORITHM_ABSOLUTE);
if(unlikely(!wu->rd_workers_time_avg))
wu->rd_workers_time_avg = rrddim_add(wu->st_workers_time, "average", NULL, 1, 10000, RRD_ALGORITHM_ABSOLUTE);
rrdset_next(wu->st_workers_time);
if(unlikely(wu->workers_min_busy_time == WORKERS_MIN_PERCENT_DEFAULT)) wu->workers_min_busy_time = 0.0;
if(wu->rd_workers_time_min)
rrddim_set_by_pointer(wu->st_workers_time, wu->rd_workers_time_min, (collected_number)((double)wu->workers_min_busy_time * 10000.0));
if(wu->rd_workers_time_max)
rrddim_set_by_pointer(wu->st_workers_time, wu->rd_workers_time_max, (collected_number)((double)wu->workers_max_busy_time * 10000.0));
if(wu->workers_total_duration == 0)
rrddim_set_by_pointer(wu->st_workers_time, wu->rd_workers_time_avg, 0);
else
rrddim_set_by_pointer(wu->st_workers_time, wu->rd_workers_time_avg, (collected_number)((double)wu->workers_total_busy_time * 100.0 * 10000.0 / (double)wu->workers_total_duration));
rrdset_done(wu->st_workers_time);
// ----------------------------------------------------------------------
#ifdef __linux__
if(wu->workers_cpu_registered || wu->st_workers_cpu) {
if(unlikely(!wu->st_workers_cpu)) {
char name[RRD_ID_LENGTH_MAX + 1];
snprintfz(name, RRD_ID_LENGTH_MAX, "workers_cpu_%s", wu->name_lowercase);
char context[RRD_ID_LENGTH_MAX + 1];
snprintf(context, RRD_ID_LENGTH_MAX, "netdata.workers.%s.cpu", wu->name_lowercase);
wu->st_workers_cpu = rrdset_create_localhost(
"netdata"
, name
, NULL
, wu->family
, context
, "Netdata Workers CPU Utilization (100% = all workers busy)"
, "%"
, "netdata"
, "stats"
, wu->priority + 1
, localhost->rrd_update_every
, RRDSET_TYPE_AREA
);
}
if (unlikely(!wu->rd_workers_cpu_min && wu->workers_registered > 1))
wu->rd_workers_cpu_min = rrddim_add(wu->st_workers_cpu, "min", NULL, 1, 10000ULL, RRD_ALGORITHM_ABSOLUTE);
if (unlikely(!wu->rd_workers_cpu_max && wu->workers_registered > 1))
wu->rd_workers_cpu_max = rrddim_add(wu->st_workers_cpu, "max", NULL, 1, 10000ULL, RRD_ALGORITHM_ABSOLUTE);
if(unlikely(!wu->rd_workers_cpu_avg))
wu->rd_workers_cpu_avg = rrddim_add(wu->st_workers_cpu, "average", NULL, 1, 10000ULL, RRD_ALGORITHM_ABSOLUTE);
rrdset_next(wu->st_workers_cpu);
if(unlikely(wu->workers_cpu_min == WORKERS_MIN_PERCENT_DEFAULT)) wu->workers_cpu_min = 0.0;
if(wu->rd_workers_cpu_min)
rrddim_set_by_pointer(wu->st_workers_cpu, wu->rd_workers_cpu_min, (collected_number)(wu->workers_cpu_min * 10000ULL));
if(wu->rd_workers_cpu_max)
rrddim_set_by_pointer(wu->st_workers_cpu, wu->rd_workers_cpu_max, (collected_number)(wu->workers_cpu_max * 10000ULL));
if(wu->workers_cpu_registered == 0)
rrddim_set_by_pointer(wu->st_workers_cpu, wu->rd_workers_cpu_avg, 0);
else
rrddim_set_by_pointer(wu->st_workers_cpu, wu->rd_workers_cpu_avg, (collected_number)( wu->workers_cpu_total * 10000ULL / (calculated_number)wu->workers_cpu_registered ));
rrdset_done(wu->st_workers_cpu);
}
#endif
// ----------------------------------------------------------------------
if(unlikely(!wu->st_workers_jobs_per_job_type)) {
char name[RRD_ID_LENGTH_MAX + 1];
snprintfz(name, RRD_ID_LENGTH_MAX, "workers_jobs_by_type_%s", wu->name_lowercase);
char context[RRD_ID_LENGTH_MAX + 1];
snprintf(context, RRD_ID_LENGTH_MAX, "netdata.workers.%s.jobs_started_by_type", wu->name_lowercase);
wu->st_workers_jobs_per_job_type = rrdset_create_localhost(
"netdata"
, name
, NULL
, wu->family
, context
, "Netdata Workers Jobs Started by Type"
, "jobs"
, "netdata"
, "stats"
, wu->priority + 2
, localhost->rrd_update_every
, RRDSET_TYPE_STACKED
);
}
rrdset_next(wu->st_workers_jobs_per_job_type);
{
size_t i;
for(i = 0; i < WORKER_UTILIZATION_MAX_JOB_TYPES ;i++) {
if (wu->per_job_type[i].name[0]) {
if(unlikely(!wu->per_job_type[i].rd_jobs_started))
wu->per_job_type[i].rd_jobs_started = rrddim_add(wu->st_workers_jobs_per_job_type, wu->per_job_type[i].name, NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
rrddim_set_by_pointer(wu->st_workers_jobs_per_job_type, wu->per_job_type[i].rd_jobs_started, (collected_number)(wu->per_job_type[i].jobs_started));
}
}
}
rrdset_done(wu->st_workers_jobs_per_job_type);
// ----------------------------------------------------------------------
if(unlikely(!wu->st_workers_busy_per_job_type)) {
char name[RRD_ID_LENGTH_MAX + 1];
snprintfz(name, RRD_ID_LENGTH_MAX, "workers_busy_time_by_type_%s", wu->name_lowercase);
char context[RRD_ID_LENGTH_MAX + 1];
snprintf(context, RRD_ID_LENGTH_MAX, "netdata.workers.%s.time_by_type", wu->name_lowercase);
wu->st_workers_busy_per_job_type = rrdset_create_localhost(
"netdata"
, name
, NULL
, wu->family
, context
, "Netdata Workers Busy Time by Type"
, "ms"
, "netdata"
, "stats"
, wu->priority + 3
, localhost->rrd_update_every
, RRDSET_TYPE_STACKED
);
}
rrdset_next(wu->st_workers_busy_per_job_type);
{
size_t i;
for(i = 0; i < WORKER_UTILIZATION_MAX_JOB_TYPES ;i++) {
if (wu->per_job_type[i].name[0]) {
if(unlikely(!wu->per_job_type[i].rd_busy_time))
wu->per_job_type[i].rd_busy_time = rrddim_add(wu->st_workers_busy_per_job_type, wu->per_job_type[i].name, NULL, 1, USEC_PER_MS, RRD_ALGORITHM_ABSOLUTE);
rrddim_set_by_pointer(wu->st_workers_busy_per_job_type, wu->per_job_type[i].rd_busy_time, (collected_number)(wu->per_job_type[i].busy_time));
}
}
}
rrdset_done(wu->st_workers_busy_per_job_type);
// ----------------------------------------------------------------------
if(wu->st_workers_threads || wu->workers_registered > 1) {
if(unlikely(!wu->st_workers_threads)) {
char name[RRD_ID_LENGTH_MAX + 1];
snprintfz(name, RRD_ID_LENGTH_MAX, "workers_threads_%s", wu->name_lowercase);
char context[RRD_ID_LENGTH_MAX + 1];
snprintf(context, RRD_ID_LENGTH_MAX, "netdata.workers.%s.threads", wu->name_lowercase);
wu->st_workers_threads = rrdset_create_localhost(
"netdata"
, name
, NULL
, wu->family
, context
, "Netdata Workers Threads"
, "threads"
, "netdata"
, "stats"
, wu->priority + 4
, localhost->rrd_update_every
, RRDSET_TYPE_STACKED
);
wu->rd_workers_threads_free = rrddim_add(wu->st_workers_threads, "free", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
wu->rd_workers_threads_busy = rrddim_add(wu->st_workers_threads, "busy", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
}
else
rrdset_next(wu->st_workers_threads);
rrddim_set_by_pointer(wu->st_workers_threads, wu->rd_workers_threads_free, (collected_number)(wu->workers_registered - wu->workers_busy));
rrddim_set_by_pointer(wu->st_workers_threads, wu->rd_workers_threads_busy, (collected_number)(wu->workers_busy));
rrdset_done(wu->st_workers_threads);
}
}
static void workers_utilization_reset_statistics(struct worker_utilization *wu) {
wu->workers_registered = 0;
wu->workers_busy = 0;
wu->workers_total_busy_time = 0;
wu->workers_total_duration = 0;
wu->workers_total_jobs_started = 0;
wu->workers_min_busy_time = WORKERS_MIN_PERCENT_DEFAULT;
wu->workers_max_busy_time = 0;
wu->workers_cpu_registered = 0;
wu->workers_cpu_min = WORKERS_MIN_PERCENT_DEFAULT;
wu->workers_cpu_max = 0;
wu->workers_cpu_total = 0;
size_t i;
for(i = 0; i < WORKER_UTILIZATION_MAX_JOB_TYPES ;i++) {
if(unlikely(!wu->name_lowercase)) {
wu->name_lowercase = strdupz(wu->name);
char *s = wu->name_lowercase;
for( ; *s ; s++) *s = tolower(*s);
}
wu->per_job_type[i].jobs_started = 0;
wu->per_job_type[i].busy_time = 0;
}
struct worker_thread *wt;
for(wt = wu->threads; wt ; wt = wt->next) {
wt->enabled = 0;
wt->cpu_enabled = 0;
}
}
static int read_thread_cpu_time_from_proc_stat(pid_t pid __maybe_unused, kernel_uint_t *utime __maybe_unused, kernel_uint_t *stime __maybe_unused) {
#ifdef __linux__
char filename[200 + 1];
snprintfz(filename, 200, "/proc/self/task/%d/stat", pid);
procfile *ff = procfile_open(filename, " ", PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
if(!ff) return -1;
ff = procfile_readall(ff);
if(!ff) return -1;
*utime = str2kernel_uint_t(procfile_lineword(ff, 0, 13));
*stime = str2kernel_uint_t(procfile_lineword(ff, 0, 14));
procfile_close(ff);
return 0;
#else
// TODO: add here cpu time detection per thread, for FreeBSD and MacOS
*utime = 0;
*stime = 0;
return 1;
#endif
}
static void workers_threads_cleanup(struct worker_utilization *wu) {
struct worker_thread *t;
// free threads at the beginning of the linked list
while(wu->threads && !wu->threads->enabled) {
t = wu->threads;
wu->threads = t->next;
t->next = NULL;
freez(t);
}
// free threads in the middle of the linked list
for(t = wu->threads; t && t->next ; t = t->next) {
if(t->next->enabled) continue;
struct worker_thread *to_remove = t->next;
t->next = to_remove->next;
to_remove->next = NULL;
freez(to_remove);
}
}
static struct worker_thread *worker_thread_find(struct worker_utilization *wu, pid_t pid) {
struct worker_thread *wt;
for(wt = wu->threads; wt && wt->pid != pid ; wt = wt->next) ;
return wt;
}
static struct worker_thread *worker_thread_create(struct worker_utilization *wu, pid_t pid) {
struct worker_thread *wt;
wt = (struct worker_thread *)callocz(1, sizeof(struct worker_thread));
wt->pid = pid;
// link it
wt->next = wu->threads;
wu->threads = wt;
return wt;
}
static struct worker_thread *worker_thread_find_or_create(struct worker_utilization *wu, pid_t pid) {
struct worker_thread *wt;
wt = worker_thread_find(wu, pid);
if(!wt) wt = worker_thread_create(wu, pid);
return wt;
}
static void worker_utilization_charts_callback(void *ptr, pid_t pid __maybe_unused, const char *thread_tag __maybe_unused, size_t utilization_usec __maybe_unused, size_t duration_usec __maybe_unused, size_t jobs_started __maybe_unused, size_t is_running __maybe_unused, const char **job_types_names __maybe_unused, size_t *job_types_jobs_started __maybe_unused, usec_t *job_types_busy_time __maybe_unused) {
struct worker_utilization *wu = (struct worker_utilization *)ptr;
// find the worker_thread in the list
struct worker_thread *wt = worker_thread_find_or_create(wu, pid);
wt->enabled = 1;
wt->busy_time = utilization_usec;
wt->jobs_started = jobs_started;
wt->utime_old = wt->utime;
wt->stime_old = wt->stime;
wt->collected_time_old = wt->collected_time;
wu->workers_total_busy_time += utilization_usec;
wu->workers_total_duration += duration_usec;
wu->workers_total_jobs_started += jobs_started;
wu->workers_busy += is_running;
wu->workers_registered++;
double util = (double)utilization_usec * 100.0 / (double)duration_usec;
if(util > wu->workers_max_busy_time)
wu->workers_max_busy_time = util;
if(util < wu->workers_min_busy_time)
wu->workers_min_busy_time = util;
// accumulate per job type statistics
size_t i;
for(i = 0; i < WORKER_UTILIZATION_MAX_JOB_TYPES ;i++) {
wu->per_job_type[i].jobs_started += job_types_jobs_started[i];
wu->per_job_type[i].busy_time += job_types_busy_time[i];
// new job type found
if(unlikely(!wu->per_job_type[i].name[0] && job_types_names[i]))
strncpyz(wu->per_job_type[i].name, job_types_names[i], WORKER_UTILIZATION_MAX_JOB_NAME_LENGTH);
}
// find its CPU utilization
if((!read_thread_cpu_time_from_proc_stat(pid, &wt->utime, &wt->stime))) {
wt->collected_time = now_realtime_usec();
usec_t delta = wt->collected_time - wt->collected_time_old;
double utime = (double)(wt->utime - wt->utime_old) / (double)system_hz * 100.0 * (double)USEC_PER_SEC / (double)delta;
double stime = (double)(wt->stime - wt->stime_old) / (double)system_hz * 100.0 * (double)USEC_PER_SEC / (double)delta;
double cpu = utime + stime;
wt->cpu = cpu;
wt->cpu_enabled = 1;
wu->workers_cpu_total += cpu;
if(cpu < wu->workers_cpu_min) wu->workers_cpu_min = cpu;
if(cpu > wu->workers_cpu_max) wu->workers_cpu_max = cpu;
}
wu->workers_cpu_registered += wt->cpu_enabled;
}
static void worker_utilization_charts(void) {
static size_t iterations = 0;
iterations++;
int i;
for(i = 0; all_workers_utilization[i].name ;i++) {
workers_utilization_reset_statistics(&all_workers_utilization[i]);
workers_foreach(all_workers_utilization[i].name, worker_utilization_charts_callback, &all_workers_utilization[i]);
// skip the first iteration, so that we don't accumulate startup utilization to our charts
if(likely(iterations > 1))
workers_utilization_update_chart(&all_workers_utilization[i]);
workers_threads_cleanup(&all_workers_utilization[i]);
}
workers_total_cpu_utilization_chart();
}
static void worker_utilization_finish(void) {
int i;
for(i = 0; all_workers_utilization[i].name ;i++) {
struct worker_utilization *wu = &all_workers_utilization[i];
if(wu->name_lowercase) {
freez(wu->name_lowercase);
wu->name_lowercase = NULL;
}
// mark all threads as not enabled
struct worker_thread *t;
for(t = wu->threads; t ; t = t->next) t->enabled = 0;
// let the cleanup job free them
workers_threads_cleanup(wu);
}
}
// ---------------------------------------------------------------------------------------------------------------------
static void global_statistics_cleanup(void *ptr)
{
worker_unregister();
struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
info("cleaning up...");
worker_utilization_finish();
static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
}
void *global_statistics_main(void *ptr)
{
worker_register("STATS");
worker_register_job_name(WORKER_JOB_GLOBAL, "global");
worker_register_job_name(WORKER_JOB_REGISTRY, "registry");
worker_register_job_name(WORKER_JOB_WORKERS, "workers");
worker_register_job_name(WORKER_JOB_DBENGINE, "dbengine");
netdata_thread_cleanup_push(global_statistics_cleanup, ptr);
int update_every =
(int)config_get_number(CONFIG_SECTION_GLOBAL_STATISTICS, "update every", localhost->rrd_update_every);
if (update_every < localhost->rrd_update_every)
update_every = localhost->rrd_update_every;
usec_t step = update_every * USEC_PER_SEC;
heartbeat_t hb;
heartbeat_init(&hb);
// keep the randomness at zero
// to make sure we are not close to any other thread
hb.randomness = 0;
while (!netdata_exit) {
worker_is_idle();
heartbeat_next(&hb, step);
worker_is_busy(WORKER_JOB_WORKERS);
worker_utilization_charts();
worker_is_busy(WORKER_JOB_GLOBAL);
global_statistics_charts();
worker_is_busy(WORKER_JOB_REGISTRY);
registry_statistics();
worker_is_busy(WORKER_JOB_DBENGINE);
dbengine_statistics_charts();
worker_is_busy(WORKER_JOB_HEARTBEAT);
update_heartbeat_charts();
}
netdata_thread_cleanup_pop(1);
return NULL;
}
|