forked from netdata/netdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sys_fs_cgroup.c
3462 lines (2875 loc) · 143 KB
/
sys_fs_cgroup.c
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
// SPDX-License-Identifier: GPL-3.0-or-later
#include "sys_fs_cgroup.h"
#define PLUGIN_CGROUPS_NAME "cgroups.plugin"
#define PLUGIN_CGROUPS_MODULE_SYSTEMD_NAME "systemd"
#define PLUGIN_CGROUPS_MODULE_CGROUPS_NAME "/sys/fs/cgroup"
// ----------------------------------------------------------------------------
// cgroup globals
static long system_page_size = 4096; // system will be queried via sysconf() in configuration()
static int cgroup_enable_cpuacct_stat = CONFIG_BOOLEAN_AUTO;
static int cgroup_enable_cpuacct_usage = CONFIG_BOOLEAN_AUTO;
static int cgroup_enable_memory = CONFIG_BOOLEAN_AUTO;
static int cgroup_enable_detailed_memory = CONFIG_BOOLEAN_AUTO;
static int cgroup_enable_memory_failcnt = CONFIG_BOOLEAN_AUTO;
static int cgroup_enable_swap = CONFIG_BOOLEAN_AUTO;
static int cgroup_enable_blkio_io = CONFIG_BOOLEAN_AUTO;
static int cgroup_enable_blkio_ops = CONFIG_BOOLEAN_AUTO;
static int cgroup_enable_blkio_throttle_io = CONFIG_BOOLEAN_AUTO;
static int cgroup_enable_blkio_throttle_ops = CONFIG_BOOLEAN_AUTO;
static int cgroup_enable_blkio_merged_ops = CONFIG_BOOLEAN_AUTO;
static int cgroup_enable_blkio_queued_ops = CONFIG_BOOLEAN_AUTO;
static int cgroup_enable_systemd_services = CONFIG_BOOLEAN_YES;
static int cgroup_enable_systemd_services_detailed_memory = CONFIG_BOOLEAN_NO;
static int cgroup_used_memory_without_cache = CONFIG_BOOLEAN_YES;
static int cgroup_use_unified_cgroups = CONFIG_BOOLEAN_NO;
static int cgroup_unified_exist = CONFIG_BOOLEAN_AUTO;
static int cgroup_search_in_devices = 1;
static int cgroup_enable_new_cgroups_detected_at_runtime = 1;
static int cgroup_check_for_new_every = 10;
static int cgroup_update_every = 1;
static int cgroup_containers_chart_priority = NETDATA_CHART_PRIO_CGROUPS_CONTAINERS;
static int cgroup_recheck_zero_blkio_every_iterations = 10;
static int cgroup_recheck_zero_mem_failcnt_every_iterations = 10;
static int cgroup_recheck_zero_mem_detailed_every_iterations = 10;
static char *cgroup_cpuacct_base = NULL;
static char *cgroup_cpuset_base = NULL;
static char *cgroup_blkio_base = NULL;
static char *cgroup_memory_base = NULL;
static char *cgroup_devices_base = NULL;
static char *cgroup_unified_base = NULL;
static int cgroup_root_count = 0;
static int cgroup_root_max = 1000;
static int cgroup_max_depth = 0;
static SIMPLE_PATTERN *enabled_cgroup_patterns = NULL;
static SIMPLE_PATTERN *enabled_cgroup_paths = NULL;
static SIMPLE_PATTERN *enabled_cgroup_renames = NULL;
static SIMPLE_PATTERN *systemd_services_cgroups = NULL;
static char *cgroups_rename_script = NULL;
static char *cgroups_network_interface_script = NULL;
static int cgroups_check = 0;
static uint32_t Read_hash = 0;
static uint32_t Write_hash = 0;
static uint32_t user_hash = 0;
static uint32_t system_hash = 0;
void read_cgroup_plugin_configuration() {
system_page_size = sysconf(_SC_PAGESIZE);
Read_hash = simple_hash("Read");
Write_hash = simple_hash("Write");
user_hash = simple_hash("user");
system_hash = simple_hash("system");
cgroup_update_every = (int)config_get_number("plugin:cgroups", "update every", localhost->rrd_update_every);
if(cgroup_update_every < localhost->rrd_update_every)
cgroup_update_every = localhost->rrd_update_every;
cgroup_check_for_new_every = (int)config_get_number("plugin:cgroups", "check for new cgroups every", (long long)cgroup_check_for_new_every * (long long)cgroup_update_every);
if(cgroup_check_for_new_every < cgroup_update_every)
cgroup_check_for_new_every = cgroup_update_every;
cgroup_use_unified_cgroups = config_get_boolean_ondemand("plugin:cgroups", "use unified cgroups", cgroup_use_unified_cgroups);
cgroup_containers_chart_priority = (int)config_get_number("plugin:cgroups", "containers priority", cgroup_containers_chart_priority);
if(cgroup_containers_chart_priority < 1)
cgroup_containers_chart_priority = NETDATA_CHART_PRIO_CGROUPS_CONTAINERS;
cgroup_enable_cpuacct_stat = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct stat (total CPU)", cgroup_enable_cpuacct_stat);
cgroup_enable_cpuacct_usage = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct usage (per core CPU)", cgroup_enable_cpuacct_usage);
cgroup_enable_memory = config_get_boolean_ondemand("plugin:cgroups", "enable memory (used mem including cache)", cgroup_enable_memory);
cgroup_enable_detailed_memory = config_get_boolean_ondemand("plugin:cgroups", "enable detailed memory", cgroup_enable_detailed_memory);
cgroup_enable_memory_failcnt = config_get_boolean_ondemand("plugin:cgroups", "enable memory limits fail count", cgroup_enable_memory_failcnt);
cgroup_enable_swap = config_get_boolean_ondemand("plugin:cgroups", "enable swap memory", cgroup_enable_swap);
cgroup_enable_blkio_io = config_get_boolean_ondemand("plugin:cgroups", "enable blkio bandwidth", cgroup_enable_blkio_io);
cgroup_enable_blkio_ops = config_get_boolean_ondemand("plugin:cgroups", "enable blkio operations", cgroup_enable_blkio_ops);
cgroup_enable_blkio_throttle_io = config_get_boolean_ondemand("plugin:cgroups", "enable blkio throttle bandwidth", cgroup_enable_blkio_throttle_io);
cgroup_enable_blkio_throttle_ops = config_get_boolean_ondemand("plugin:cgroups", "enable blkio throttle operations", cgroup_enable_blkio_throttle_ops);
cgroup_enable_blkio_queued_ops = config_get_boolean_ondemand("plugin:cgroups", "enable blkio queued operations", cgroup_enable_blkio_queued_ops);
cgroup_enable_blkio_merged_ops = config_get_boolean_ondemand("plugin:cgroups", "enable blkio merged operations", cgroup_enable_blkio_merged_ops);
cgroup_recheck_zero_blkio_every_iterations = (int)config_get_number("plugin:cgroups", "recheck zero blkio every iterations", cgroup_recheck_zero_blkio_every_iterations);
cgroup_recheck_zero_mem_failcnt_every_iterations = (int)config_get_number("plugin:cgroups", "recheck zero memory failcnt every iterations", cgroup_recheck_zero_mem_failcnt_every_iterations);
cgroup_recheck_zero_mem_detailed_every_iterations = (int)config_get_number("plugin:cgroups", "recheck zero detailed memory every iterations", cgroup_recheck_zero_mem_detailed_every_iterations);
cgroup_enable_systemd_services = config_get_boolean("plugin:cgroups", "enable systemd services", cgroup_enable_systemd_services);
cgroup_enable_systemd_services_detailed_memory = config_get_boolean("plugin:cgroups", "enable systemd services detailed memory", cgroup_enable_systemd_services_detailed_memory);
cgroup_used_memory_without_cache = config_get_boolean("plugin:cgroups", "report used memory without cache", cgroup_used_memory_without_cache);
char filename[FILENAME_MAX + 1], *s;
struct mountinfo *mi, *root = mountinfo_read(0);
if(!cgroup_use_unified_cgroups) {
mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "cpuacct");
if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "cpuacct");
if(!mi) {
error("CGROUP: cannot find cpuacct mountinfo. Assuming default: /sys/fs/cgroup/cpuacct");
s = "/sys/fs/cgroup/cpuacct";
}
else s = mi->mount_point;
snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, s);
cgroup_cpuacct_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/cpuacct", filename);
mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "cpuset");
if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "cpuset");
if(!mi) {
error("CGROUP: cannot find cpuset mountinfo. Assuming default: /sys/fs/cgroup/cpuset");
s = "/sys/fs/cgroup/cpuset";
}
else s = mi->mount_point;
snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, s);
cgroup_cpuset_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/cpuset", filename);
mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "blkio");
if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "blkio");
if(!mi) {
error("CGROUP: cannot find blkio mountinfo. Assuming default: /sys/fs/cgroup/blkio");
s = "/sys/fs/cgroup/blkio";
}
else s = mi->mount_point;
snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, s);
cgroup_blkio_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/blkio", filename);
mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "memory");
if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "memory");
if(!mi) {
error("CGROUP: cannot find memory mountinfo. Assuming default: /sys/fs/cgroup/memory");
s = "/sys/fs/cgroup/memory";
}
else s = mi->mount_point;
snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, s);
cgroup_memory_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/memory", filename);
mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "devices");
if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "devices");
if(!mi) {
error("CGROUP: cannot find devices mountinfo. Assuming default: /sys/fs/cgroup/devices");
s = "/sys/fs/cgroup/devices";
}
else s = mi->mount_point;
snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, s);
cgroup_devices_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/devices", filename);
}
else {
//cgroup_enable_cpuacct_stat =
cgroup_enable_cpuacct_usage =
//cgroup_enable_memory =
//cgroup_enable_detailed_memory =
cgroup_enable_memory_failcnt =
//cgroup_enable_swap =
//cgroup_enable_blkio_io =
//cgroup_enable_blkio_ops =
cgroup_enable_blkio_throttle_io =
cgroup_enable_blkio_throttle_ops =
cgroup_enable_blkio_merged_ops =
cgroup_enable_blkio_queued_ops = CONFIG_BOOLEAN_NO;
cgroup_search_in_devices = 0;
cgroup_enable_systemd_services_detailed_memory = CONFIG_BOOLEAN_NO;
cgroup_used_memory_without_cache = CONFIG_BOOLEAN_NO; //unified cgroups use different values
//TODO: can there be more than 1 cgroup2 mount point?
mi = mountinfo_find_by_filesystem_super_option(root, "cgroup2", "rw"); //there is no cgroup2 specific super option - for now use 'rw' option
if(mi) debug(D_CGROUP, "found unified cgroup root using super options, with path: '%s'", mi->mount_point);
if(!mi) {
mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup2", "cgroup");
if(mi) debug(D_CGROUP, "found unified cgroup root using mountsource info, with path: '%s'", mi->mount_point);
}
if(!mi) {
error("CGROUP: cannot find cgroup2 mountinfo. Assuming default: /sys/fs/cgroup");
s = "/sys/fs/cgroup";
}
else s = mi->mount_point;
snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, s);
cgroup_unified_base = config_get("plugin:cgroups", "path to unified cgroups", filename);
debug(D_CGROUP, "using cgroup root: '%s'", cgroup_unified_base);
}
cgroup_root_max = (int)config_get_number("plugin:cgroups", "max cgroups to allow", cgroup_root_max);
cgroup_max_depth = (int)config_get_number("plugin:cgroups", "max cgroups depth to monitor", cgroup_max_depth);
cgroup_enable_new_cgroups_detected_at_runtime = config_get_boolean("plugin:cgroups", "enable new cgroups detected at run time", cgroup_enable_new_cgroups_detected_at_runtime);
enabled_cgroup_patterns = simple_pattern_create(
config_get("plugin:cgroups", "enable by default cgroups matching",
// ----------------------------------------------------------------
" !*/init.scope " // ignore init.scope
" !/system.slice/run-*.scope " // ignore system.slice/run-XXXX.scope
" *.scope " // we need all other *.scope for sure
// ----------------------------------------------------------------
" /machine.slice/*.service " // #3367 systemd-nspawn
// ----------------------------------------------------------------
" !*/vcpu* " // libvirtd adds these sub-cgroups
" !*/emulator " // libvirtd adds these sub-cgroups
" !*.mount "
" !*.partition "
" !*.service "
" !*.socket "
" !*.slice "
" !*.swap "
" !*.user "
" !/ "
" !/docker "
" !/libvirt "
" !/lxc "
" !/lxc/*/* " // #1397 #2649
" !/machine "
" !/qemu "
" !/system "
" !/systemd "
" !/user "
" * " // enable anything else
), NULL, SIMPLE_PATTERN_EXACT);
enabled_cgroup_paths = simple_pattern_create(
config_get("plugin:cgroups", "search for cgroups in subpaths matching",
" !*/init.scope " // ignore init.scope
" !*-qemu " // #345
" !*.libvirt-qemu " // #3010
" !/init.scope "
" !/system "
" !/systemd "
" !/user "
" !/user.slice "
" !/lxc/*/* " // #2161 #2649
" * "
), NULL, SIMPLE_PATTERN_EXACT);
snprintfz(filename, FILENAME_MAX, "%s/cgroup-name.sh", netdata_configured_primary_plugins_dir);
cgroups_rename_script = config_get("plugin:cgroups", "script to get cgroup names", filename);
snprintfz(filename, FILENAME_MAX, "%s/cgroup-network", netdata_configured_primary_plugins_dir);
cgroups_network_interface_script = config_get("plugin:cgroups", "script to get cgroup network interfaces", filename);
enabled_cgroup_renames = simple_pattern_create(
config_get("plugin:cgroups", "run script to rename cgroups matching",
" !/ "
" !*.mount "
" !*.socket "
" !*.partition "
" /machine.slice/*.service " // #3367 systemd-nspawn
" !*.service "
" !*.slice "
" !*.swap "
" !*.user "
" !init.scope "
" !*.scope/vcpu* " // libvirtd adds these sub-cgroups
" !*.scope/emulator " // libvirtd adds these sub-cgroups
" *.scope "
" *docker* "
" *lxc* "
" *qemu* "
" *kubepods* " // #3396 kubernetes
" *.libvirt-qemu " // #3010
" * "
), NULL, SIMPLE_PATTERN_EXACT);
if(cgroup_enable_systemd_services) {
systemd_services_cgroups = simple_pattern_create(
config_get("plugin:cgroups", "cgroups to match as systemd services",
" !/system.slice/*/*.service "
" /system.slice/*.service "
), NULL, SIMPLE_PATTERN_EXACT);
}
mountinfo_free_all(root);
}
// ----------------------------------------------------------------------------
// cgroup objects
struct blkio {
int updated;
int enabled; // CONFIG_BOOLEAN_YES or CONFIG_BOOLEAN_AUTO
int delay_counter;
char *filename;
unsigned long long Read;
unsigned long long Write;
/*
unsigned long long Sync;
unsigned long long Async;
unsigned long long Total;
*/
};
// https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt
struct memory {
ARL_BASE *arl_base;
ARL_ENTRY *arl_dirty;
ARL_ENTRY *arl_swap;
int updated_detailed;
int updated_usage_in_bytes;
int updated_msw_usage_in_bytes;
int updated_failcnt;
int enabled_detailed; // CONFIG_BOOLEAN_YES or CONFIG_BOOLEAN_AUTO
int enabled_usage_in_bytes; // CONFIG_BOOLEAN_YES or CONFIG_BOOLEAN_AUTO
int enabled_msw_usage_in_bytes; // CONFIG_BOOLEAN_YES or CONFIG_BOOLEAN_AUTO
int enabled_failcnt; // CONFIG_BOOLEAN_YES or CONFIG_BOOLEAN_AUTO
int delay_counter_detailed;
int delay_counter_failcnt;
char *filename_detailed;
char *filename_usage_in_bytes;
char *filename_msw_usage_in_bytes;
char *filename_failcnt;
int detailed_has_dirty;
int detailed_has_swap;
// detailed metrics
/*
unsigned long long cache;
unsigned long long rss;
unsigned long long rss_huge;
unsigned long long mapped_file;
unsigned long long writeback;
unsigned long long dirty;
unsigned long long swap;
unsigned long long pgpgin;
unsigned long long pgpgout;
unsigned long long pgfault;
unsigned long long pgmajfault;
unsigned long long inactive_anon;
unsigned long long active_anon;
unsigned long long inactive_file;
unsigned long long active_file;
unsigned long long unevictable;
unsigned long long hierarchical_memory_limit;
*/
//unified cgroups metrics
unsigned long long anon;
unsigned long long kernel_stack;
unsigned long long slab;
unsigned long long sock;
unsigned long long shmem;
unsigned long long anon_thp;
//unsigned long long file_writeback;
//unsigned long long file_dirty;
//unsigned long long file;
unsigned long long total_cache;
unsigned long long total_rss;
unsigned long long total_rss_huge;
unsigned long long total_mapped_file;
unsigned long long total_writeback;
unsigned long long total_dirty;
unsigned long long total_swap;
unsigned long long total_pgpgin;
unsigned long long total_pgpgout;
unsigned long long total_pgfault;
unsigned long long total_pgmajfault;
/*
unsigned long long total_inactive_anon;
unsigned long long total_active_anon;
unsigned long long total_inactive_file;
unsigned long long total_active_file;
unsigned long long total_unevictable;
*/
// single file metrics
unsigned long long usage_in_bytes;
unsigned long long msw_usage_in_bytes;
unsigned long long failcnt;
};
// https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt
struct cpuacct_stat {
int updated;
int enabled; // CONFIG_BOOLEAN_YES or CONFIG_BOOLEAN_AUTO
char *filename;
unsigned long long user;
unsigned long long system;
};
// https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt
struct cpuacct_usage {
int updated;
int enabled; // CONFIG_BOOLEAN_YES or CONFIG_BOOLEAN_AUTO
char *filename;
unsigned int cpus;
unsigned long long *cpu_percpu;
};
struct cgroup_network_interface {
const char *host_device;
const char *container_device;
struct cgroup_network_interface *next;
};
#define CGROUP_OPTIONS_DISABLED_DUPLICATE 0x00000001
#define CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE 0x00000002
#define CGROUP_OPTIONS_IS_UNIFIED 0x00000004
struct cgroup {
uint32_t options;
char available; // found in the filesystem
char enabled; // enabled in the config
char pending_renames;
char *id;
uint32_t hash;
char *chart_id;
uint32_t hash_chart;
char *chart_title;
struct cpuacct_stat cpuacct_stat;
struct cpuacct_usage cpuacct_usage;
struct memory memory;
struct blkio io_service_bytes; // bytes
struct blkio io_serviced; // operations
struct blkio throttle_io_service_bytes; // bytes
struct blkio throttle_io_serviced; // operations
struct blkio io_merged; // operations
struct blkio io_queued; // operations
struct cgroup_network_interface *interfaces;
// per cgroup charts
RRDSET *st_cpu;
RRDSET *st_cpu_limit;
RRDSET *st_cpu_per_core;
RRDSET *st_mem;
RRDSET *st_writeback;
RRDSET *st_mem_activity;
RRDSET *st_pgfaults;
RRDSET *st_mem_usage;
RRDSET *st_mem_usage_limit;
RRDSET *st_mem_failcnt;
RRDSET *st_io;
RRDSET *st_serviced_ops;
RRDSET *st_throttle_io;
RRDSET *st_throttle_serviced_ops;
RRDSET *st_queued_ops;
RRDSET *st_merged_ops;
// per cgroup chart variables
char *filename_cpuset_cpus;
unsigned long long cpuset_cpus;
char *filename_cpu_cfs_period;
unsigned long long cpu_cfs_period;
char *filename_cpu_cfs_quota;
unsigned long long cpu_cfs_quota;
RRDSETVAR *chart_var_cpu_limit;
calculated_number prev_cpu_usage;
char *filename_memory_limit;
unsigned long long memory_limit;
RRDSETVAR *chart_var_memory_limit;
char *filename_memoryswap_limit;
unsigned long long memoryswap_limit;
RRDSETVAR *chart_var_memoryswap_limit;
// services
RRDDIM *rd_cpu;
RRDDIM *rd_mem_usage;
RRDDIM *rd_mem_failcnt;
RRDDIM *rd_swap_usage;
RRDDIM *rd_mem_detailed_cache;
RRDDIM *rd_mem_detailed_rss;
RRDDIM *rd_mem_detailed_mapped;
RRDDIM *rd_mem_detailed_writeback;
RRDDIM *rd_mem_detailed_pgpgin;
RRDDIM *rd_mem_detailed_pgpgout;
RRDDIM *rd_mem_detailed_pgfault;
RRDDIM *rd_mem_detailed_pgmajfault;
RRDDIM *rd_io_service_bytes_read;
RRDDIM *rd_io_serviced_read;
RRDDIM *rd_throttle_io_read;
RRDDIM *rd_throttle_io_serviced_read;
RRDDIM *rd_io_queued_read;
RRDDIM *rd_io_merged_read;
RRDDIM *rd_io_service_bytes_write;
RRDDIM *rd_io_serviced_write;
RRDDIM *rd_throttle_io_write;
RRDDIM *rd_throttle_io_serviced_write;
RRDDIM *rd_io_queued_write;
RRDDIM *rd_io_merged_write;
struct cgroup *next;
} *cgroup_root = NULL;
// ----------------------------------------------------------------------------
// read values from /sys
static inline void cgroup_read_cpuacct_stat(struct cpuacct_stat *cp) {
static procfile *ff = NULL;
if(likely(cp->filename)) {
ff = procfile_reopen(ff, cp->filename, NULL, PROCFILE_FLAG_DEFAULT);
if(unlikely(!ff)) {
cp->updated = 0;
cgroups_check = 1;
return;
}
ff = procfile_readall(ff);
if(unlikely(!ff)) {
cp->updated = 0;
cgroups_check = 1;
return;
}
unsigned long i, lines = procfile_lines(ff);
if(unlikely(lines < 1)) {
error("CGROUP: file '%s' should have 1+ lines.", cp->filename);
cp->updated = 0;
return;
}
for(i = 0; i < lines ; i++) {
char *s = procfile_lineword(ff, i, 0);
uint32_t hash = simple_hash(s);
if(unlikely(hash == user_hash && !strcmp(s, "user")))
cp->user = str2ull(procfile_lineword(ff, i, 1));
else if(unlikely(hash == system_hash && !strcmp(s, "system")))
cp->system = str2ull(procfile_lineword(ff, i, 1));
}
cp->updated = 1;
if(unlikely(cp->enabled == CONFIG_BOOLEAN_AUTO &&
(cp->user || cp->system || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)))
cp->enabled = CONFIG_BOOLEAN_YES;
}
}
static inline void cgroup2_read_cpuacct_stat(struct cpuacct_stat *cp) {
static procfile *ff = NULL;
if(likely(cp->filename)) {
ff = procfile_reopen(ff, cp->filename, NULL, PROCFILE_FLAG_DEFAULT);
if(unlikely(!ff)) {
cp->updated = 0;
cgroups_check = 1;
return;
}
ff = procfile_readall(ff);
if(unlikely(!ff)) {
cp->updated = 0;
cgroups_check = 1;
return;
}
unsigned long lines = procfile_lines(ff);
if(unlikely(lines < 3)) {
error("CGROUP: file '%s' should have 3+ lines.", cp->filename);
cp->updated = 0;
return;
}
cp->user = str2ull(procfile_lineword(ff, 1, 1));
cp->system = str2ull(procfile_lineword(ff, 2, 1));
cp->updated = 1;
if(unlikely(cp->enabled == CONFIG_BOOLEAN_AUTO &&
(cp->user || cp->system || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)))
cp->enabled = CONFIG_BOOLEAN_YES;
}
}
static inline void cgroup_read_cpuacct_usage(struct cpuacct_usage *ca) {
static procfile *ff = NULL;
if(likely(ca->filename)) {
ff = procfile_reopen(ff, ca->filename, NULL, PROCFILE_FLAG_DEFAULT);
if(unlikely(!ff)) {
ca->updated = 0;
cgroups_check = 1;
return;
}
ff = procfile_readall(ff);
if(unlikely(!ff)) {
ca->updated = 0;
cgroups_check = 1;
return;
}
if(unlikely(procfile_lines(ff) < 1)) {
error("CGROUP: file '%s' should have 1+ lines but has %zu.", ca->filename, procfile_lines(ff));
ca->updated = 0;
return;
}
unsigned long i = procfile_linewords(ff, 0);
if(unlikely(i == 0)) {
ca->updated = 0;
return;
}
// we may have 1 more CPU reported
while(i > 0) {
char *s = procfile_lineword(ff, 0, i - 1);
if(!*s) i--;
else break;
}
if(unlikely(i != ca->cpus)) {
freez(ca->cpu_percpu);
ca->cpu_percpu = mallocz(sizeof(unsigned long long) * i);
ca->cpus = (unsigned int)i;
}
unsigned long long total = 0;
for(i = 0; i < ca->cpus ;i++) {
unsigned long long n = str2ull(procfile_lineword(ff, 0, i));
ca->cpu_percpu[i] = n;
total += n;
}
ca->updated = 1;
if(unlikely(ca->enabled == CONFIG_BOOLEAN_AUTO &&
(total || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)))
ca->enabled = CONFIG_BOOLEAN_YES;
}
}
static inline void cgroup_read_blkio(struct blkio *io) {
if(unlikely(io->enabled == CONFIG_BOOLEAN_AUTO && io->delay_counter > 0)) {
io->delay_counter--;
return;
}
if(likely(io->filename)) {
static procfile *ff = NULL;
ff = procfile_reopen(ff, io->filename, NULL, PROCFILE_FLAG_DEFAULT);
if(unlikely(!ff)) {
io->updated = 0;
cgroups_check = 1;
return;
}
ff = procfile_readall(ff);
if(unlikely(!ff)) {
io->updated = 0;
cgroups_check = 1;
return;
}
unsigned long i, lines = procfile_lines(ff);
if(unlikely(lines < 1)) {
error("CGROUP: file '%s' should have 1+ lines.", io->filename);
io->updated = 0;
return;
}
io->Read = 0;
io->Write = 0;
/*
io->Sync = 0;
io->Async = 0;
io->Total = 0;
*/
for(i = 0; i < lines ; i++) {
char *s = procfile_lineword(ff, i, 1);
uint32_t hash = simple_hash(s);
if(unlikely(hash == Read_hash && !strcmp(s, "Read")))
io->Read += str2ull(procfile_lineword(ff, i, 2));
else if(unlikely(hash == Write_hash && !strcmp(s, "Write")))
io->Write += str2ull(procfile_lineword(ff, i, 2));
/*
else if(unlikely(hash == Sync_hash && !strcmp(s, "Sync")))
io->Sync += str2ull(procfile_lineword(ff, i, 2));
else if(unlikely(hash == Async_hash && !strcmp(s, "Async")))
io->Async += str2ull(procfile_lineword(ff, i, 2));
else if(unlikely(hash == Total_hash && !strcmp(s, "Total")))
io->Total += str2ull(procfile_lineword(ff, i, 2));
*/
}
io->updated = 1;
if(unlikely(io->enabled == CONFIG_BOOLEAN_AUTO)) {
if(unlikely(io->Read || io->Write || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))
io->enabled = CONFIG_BOOLEAN_YES;
else
io->delay_counter = cgroup_recheck_zero_blkio_every_iterations;
}
}
}
static inline void cgroup2_read_blkio(struct blkio *io, unsigned int word_offset) {
if(unlikely(io->enabled == CONFIG_BOOLEAN_AUTO && io->delay_counter > 0)) {
io->delay_counter--;
return;
}
if(likely(io->filename)) {
static procfile *ff = NULL;
ff = procfile_reopen(ff, io->filename, NULL, PROCFILE_FLAG_DEFAULT);
if(unlikely(!ff)) {
io->updated = 0;
cgroups_check = 1;
return;
}
ff = procfile_readall(ff);
if(unlikely(!ff)) {
io->updated = 0;
cgroups_check = 1;
return;
}
unsigned long i, lines = procfile_lines(ff);
if (unlikely(lines < 1)) {
error("CGROUP: file '%s' should have 1+ lines.", io->filename);
io->updated = 0;
return;
}
io->Read = 0;
io->Write = 0;
for (i = 0; i < lines; i++) {
io->Read += str2ull(procfile_lineword(ff, i, 2 + word_offset));
io->Write += str2ull(procfile_lineword(ff, i, 4 + word_offset));
}
io->updated = 1;
if(unlikely(io->enabled == CONFIG_BOOLEAN_AUTO)) {
if(unlikely(io->Read || io->Write || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))
io->enabled = CONFIG_BOOLEAN_YES;
else
io->delay_counter = cgroup_recheck_zero_blkio_every_iterations;
}
}
}
static inline void cgroup_read_memory(struct memory *mem, char parent_cg_is_unified) {
static procfile *ff = NULL;
// read detailed ram usage
if(likely(mem->filename_detailed)) {
if(unlikely(mem->enabled_detailed == CONFIG_BOOLEAN_AUTO && mem->delay_counter_detailed > 0)) {
mem->delay_counter_detailed--;
goto memory_next;
}
ff = procfile_reopen(ff, mem->filename_detailed, NULL, PROCFILE_FLAG_DEFAULT);
if(unlikely(!ff)) {
mem->updated_detailed = 0;
cgroups_check = 1;
goto memory_next;
}
ff = procfile_readall(ff);
if(unlikely(!ff)) {
mem->updated_detailed = 0;
cgroups_check = 1;
goto memory_next;
}
unsigned long i, lines = procfile_lines(ff);
if(unlikely(lines < 1)) {
error("CGROUP: file '%s' should have 1+ lines.", mem->filename_detailed);
mem->updated_detailed = 0;
goto memory_next;
}
if(unlikely(!mem->arl_base)) {
if(parent_cg_is_unified == 0){
mem->arl_base = arl_create("cgroup/memory", NULL, 60);
arl_expect(mem->arl_base, "total_cache", &mem->total_cache);
arl_expect(mem->arl_base, "total_rss", &mem->total_rss);
arl_expect(mem->arl_base, "total_rss_huge", &mem->total_rss_huge);
arl_expect(mem->arl_base, "total_mapped_file", &mem->total_mapped_file);
arl_expect(mem->arl_base, "total_writeback", &mem->total_writeback);
mem->arl_dirty = arl_expect(mem->arl_base, "total_dirty", &mem->total_dirty);
mem->arl_swap = arl_expect(mem->arl_base, "total_swap", &mem->total_swap);
arl_expect(mem->arl_base, "total_pgpgin", &mem->total_pgpgin);
arl_expect(mem->arl_base, "total_pgpgout", &mem->total_pgpgout);
arl_expect(mem->arl_base, "total_pgfault", &mem->total_pgfault);
arl_expect(mem->arl_base, "total_pgmajfault", &mem->total_pgmajfault);
} else {
mem->arl_base = arl_create("cgroup/memory", NULL, 60);
arl_expect(mem->arl_base, "anon", &mem->anon);
arl_expect(mem->arl_base, "kernel_stack", &mem->kernel_stack);
arl_expect(mem->arl_base, "slab", &mem->slab);
arl_expect(mem->arl_base, "sock", &mem->sock);
arl_expect(mem->arl_base, "anon_thp", &mem->anon_thp);
arl_expect(mem->arl_base, "file", &mem->total_mapped_file);
arl_expect(mem->arl_base, "file_writeback", &mem->total_writeback);
mem->arl_dirty = arl_expect(mem->arl_base, "file_dirty", &mem->total_dirty);
arl_expect(mem->arl_base, "pgfault", &mem->total_pgfault);
arl_expect(mem->arl_base, "pgmajfault", &mem->total_pgmajfault);
}
}
arl_begin(mem->arl_base);
for(i = 0; i < lines ; i++) {
if(arl_check(mem->arl_base,
procfile_lineword(ff, i, 0),
procfile_lineword(ff, i, 1))) break;
}
if(unlikely(mem->arl_dirty->flags & ARL_ENTRY_FLAG_FOUND))
mem->detailed_has_dirty = 1;
if(unlikely(parent_cg_is_unified == 0 && mem->arl_swap->flags & ARL_ENTRY_FLAG_FOUND))
mem->detailed_has_swap = 1;
// fprintf(stderr, "READ: '%s', cache: %llu, rss: %llu, rss_huge: %llu, mapped_file: %llu, writeback: %llu, dirty: %llu, swap: %llu, pgpgin: %llu, pgpgout: %llu, pgfault: %llu, pgmajfault: %llu, inactive_anon: %llu, active_anon: %llu, inactive_file: %llu, active_file: %llu, unevictable: %llu, hierarchical_memory_limit: %llu, total_cache: %llu, total_rss: %llu, total_rss_huge: %llu, total_mapped_file: %llu, total_writeback: %llu, total_dirty: %llu, total_swap: %llu, total_pgpgin: %llu, total_pgpgout: %llu, total_pgfault: %llu, total_pgmajfault: %llu, total_inactive_anon: %llu, total_active_anon: %llu, total_inactive_file: %llu, total_active_file: %llu, total_unevictable: %llu\n", mem->filename, mem->cache, mem->rss, mem->rss_huge, mem->mapped_file, mem->writeback, mem->dirty, mem->swap, mem->pgpgin, mem->pgpgout, mem->pgfault, mem->pgmajfault, mem->inactive_anon, mem->active_anon, mem->inactive_file, mem->active_file, mem->unevictable, mem->hierarchical_memory_limit, mem->total_cache, mem->total_rss, mem->total_rss_huge, mem->total_mapped_file, mem->total_writeback, mem->total_dirty, mem->total_swap, mem->total_pgpgin, mem->total_pgpgout, mem->total_pgfault, mem->total_pgmajfault, mem->total_inactive_anon, mem->total_active_anon, mem->total_inactive_file, mem->total_active_file, mem->total_unevictable);
mem->updated_detailed = 1;
if(unlikely(mem->enabled_detailed == CONFIG_BOOLEAN_AUTO)) {
if(( (!parent_cg_is_unified) && ( mem->total_cache || mem->total_dirty || mem->total_rss || mem->total_rss_huge || mem->total_mapped_file || mem->total_writeback
|| mem->total_swap || mem->total_pgpgin || mem->total_pgpgout || mem->total_pgfault || mem->total_pgmajfault))
|| (parent_cg_is_unified && ( mem->anon || mem->total_dirty || mem->kernel_stack || mem->slab || mem->sock || mem->total_writeback
|| mem->anon_thp || mem->total_pgfault || mem->total_pgmajfault))
|| netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)
mem->enabled_detailed = CONFIG_BOOLEAN_YES;
else
mem->delay_counter_detailed = cgroup_recheck_zero_mem_detailed_every_iterations;
}
}
memory_next:
// read usage_in_bytes
if(likely(mem->filename_usage_in_bytes)) {
mem->updated_usage_in_bytes = !read_single_number_file(mem->filename_usage_in_bytes, &mem->usage_in_bytes);
if(unlikely(mem->updated_usage_in_bytes && mem->enabled_usage_in_bytes == CONFIG_BOOLEAN_AUTO &&
(mem->usage_in_bytes || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)))
mem->enabled_usage_in_bytes = CONFIG_BOOLEAN_YES;
}
// read msw_usage_in_bytes
if(likely(mem->filename_msw_usage_in_bytes)) {
mem->updated_msw_usage_in_bytes = !read_single_number_file(mem->filename_msw_usage_in_bytes, &mem->msw_usage_in_bytes);
if(unlikely(mem->updated_msw_usage_in_bytes && mem->enabled_msw_usage_in_bytes == CONFIG_BOOLEAN_AUTO &&
(mem->msw_usage_in_bytes || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)))
mem->enabled_msw_usage_in_bytes = CONFIG_BOOLEAN_YES;
}
// read failcnt
if(likely(mem->filename_failcnt)) {
if(unlikely(mem->enabled_failcnt == CONFIG_BOOLEAN_AUTO && mem->delay_counter_failcnt > 0)) {
mem->updated_failcnt = 0;
mem->delay_counter_failcnt--;
}
else {
mem->updated_failcnt = !read_single_number_file(mem->filename_failcnt, &mem->failcnt);
if(unlikely(mem->updated_failcnt && mem->enabled_failcnt == CONFIG_BOOLEAN_AUTO)) {
if(unlikely(mem->failcnt || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))
mem->enabled_failcnt = CONFIG_BOOLEAN_YES;
else
mem->delay_counter_failcnt = cgroup_recheck_zero_mem_failcnt_every_iterations;
}
}
}
}
static inline void cgroup_read(struct cgroup *cg) {
debug(D_CGROUP, "reading metrics for cgroups '%s'", cg->id);
if(!(cg->options & CGROUP_OPTIONS_IS_UNIFIED)) {
cgroup_read_cpuacct_stat(&cg->cpuacct_stat);
cgroup_read_cpuacct_usage(&cg->cpuacct_usage);
cgroup_read_memory(&cg->memory, 0);
cgroup_read_blkio(&cg->io_service_bytes);
cgroup_read_blkio(&cg->io_serviced);
cgroup_read_blkio(&cg->throttle_io_service_bytes);
cgroup_read_blkio(&cg->throttle_io_serviced);
cgroup_read_blkio(&cg->io_merged);
cgroup_read_blkio(&cg->io_queued);
}
else {
//TODO: io_service_bytes and io_serviced use same file merge into 1 function
cgroup2_read_blkio(&cg->io_service_bytes, 0);
cgroup2_read_blkio(&cg->io_serviced, 4);
cgroup2_read_cpuacct_stat(&cg->cpuacct_stat);
cgroup_read_memory(&cg->memory, 1);
}
}
static inline void read_all_cgroups(struct cgroup *root) {
debug(D_CGROUP, "reading metrics for all cgroups");
struct cgroup *cg;
for(cg = root; cg ; cg = cg->next)
if(cg->enabled && cg->available && !cg->pending_renames)
cgroup_read(cg);
}
// ----------------------------------------------------------------------------
// cgroup network interfaces
#define CGROUP_NETWORK_INTERFACE_MAX_LINE 2048
static inline void read_cgroup_network_interfaces(struct cgroup *cg) {
debug(D_CGROUP, "looking for the network interfaces of cgroup '%s' with chart id '%s' and title '%s'", cg->id, cg->chart_id, cg->chart_title);
pid_t cgroup_pid;
char command[CGROUP_NETWORK_INTERFACE_MAX_LINE + 1];
if(!(cg->options & CGROUP_OPTIONS_IS_UNIFIED)) {
snprintfz(command, CGROUP_NETWORK_INTERFACE_MAX_LINE, "exec %s --cgroup '%s%s'", cgroups_network_interface_script, cgroup_cpuacct_base, cg->id);
}
else {
snprintfz(command, CGROUP_NETWORK_INTERFACE_MAX_LINE, "exec %s --cgroup '%s%s'", cgroups_network_interface_script, cgroup_unified_base, cg->id);
}
debug(D_CGROUP, "executing command '%s' for cgroup '%s'", command, cg->id);
FILE *fp = mypopen(command, &cgroup_pid);
if(!fp) {
error("CGROUP: cannot popen(\"%s\", \"r\").", command);
return;
}
char *s;
char buffer[CGROUP_NETWORK_INTERFACE_MAX_LINE + 1];
while((s = fgets(buffer, CGROUP_NETWORK_INTERFACE_MAX_LINE, fp))) {
trim(s);
if(*s && *s != '\n') {
char *t = s;
while(*t && *t != ' ') t++;
if(*t == ' ') {
*t = '\0';
t++;
}
if(!*s) {