3 // ----------------------------------------------------------------------------
6 #define CHART_PRIORITY_SYSTEMD_SERVICES 19000
7 #define CHART_PRIORITY_CONTAINERS 40000
9 static long system_page_size = 4096; // system will be queried via sysconf() in configuration()
11 static int cgroup_enable_cpuacct_stat = CONFIG_ONDEMAND_ONDEMAND;
12 static int cgroup_enable_cpuacct_usage = CONFIG_ONDEMAND_ONDEMAND;
13 static int cgroup_enable_memory = CONFIG_ONDEMAND_ONDEMAND;
14 static int cgroup_enable_detailed_memory = CONFIG_ONDEMAND_ONDEMAND;
15 static int cgroup_enable_memory_failcnt = CONFIG_ONDEMAND_ONDEMAND;
16 static int cgroup_enable_swap = CONFIG_ONDEMAND_ONDEMAND;
17 static int cgroup_enable_blkio_io = CONFIG_ONDEMAND_ONDEMAND;
18 static int cgroup_enable_blkio_ops = CONFIG_ONDEMAND_ONDEMAND;
19 static int cgroup_enable_blkio_throttle_io = CONFIG_ONDEMAND_ONDEMAND;
20 static int cgroup_enable_blkio_throttle_ops = CONFIG_ONDEMAND_ONDEMAND;
21 static int cgroup_enable_blkio_merged_ops = CONFIG_ONDEMAND_ONDEMAND;
22 static int cgroup_enable_blkio_queued_ops = CONFIG_ONDEMAND_ONDEMAND;
23 static int cgroup_enable_systemd_services = CONFIG_ONDEMAND_YES;
24 static int cgroup_enable_systemd_services_detailed_memory = CONFIG_ONDEMAND_NO;
26 static int cgroup_search_in_devices = 1;
28 static int cgroup_enable_new_cgroups_detected_at_runtime = 1;
29 static int cgroup_check_for_new_every = 10;
30 static int cgroup_update_every = 1;
32 static int cgroup_recheck_zero_blkio_every_iterations = 10;
33 static int cgroup_recheck_zero_mem_failcnt_every_iterations = 10;
34 static int cgroup_recheck_zero_mem_detailed_every_iterations = 10;
36 static char *cgroup_cpuacct_base = NULL;
37 static char *cgroup_blkio_base = NULL;
38 static char *cgroup_memory_base = NULL;
39 static char *cgroup_devices_base = NULL;
41 static int cgroup_root_count = 0;
42 static int cgroup_root_max = 500;
43 static int cgroup_max_depth = 0;
45 static SIMPLE_PATTERN *enabled_cgroup_patterns = NULL;
46 static SIMPLE_PATTERN *enabled_cgroup_paths = NULL;
47 static SIMPLE_PATTERN *enabled_cgroup_renames = NULL;
48 static SIMPLE_PATTERN *systemd_services_cgroups = NULL;
50 static char *cgroups_rename_script = PLUGINS_DIR "/cgroup-name.sh";
52 static uint32_t Read_hash = 0;
53 static uint32_t Write_hash = 0;
54 static uint32_t Sync_hash = 0;
55 static uint32_t Async_hash = 0;
56 static uint32_t Total_hash = 0;
57 static uint32_t user_hash = 0;
58 static uint32_t system_hash = 0;
59 static uint32_t cache_hash = 0;
60 static uint32_t rss_hash = 0;
61 static uint32_t rss_huge_hash = 0;
62 static uint32_t mapped_file_hash = 0;
63 static uint32_t writeback_hash = 0;
64 static uint32_t dirty_hash = 0;
65 static uint32_t swap_hash = 0;
66 static uint32_t pgpgin_hash = 0;
67 static uint32_t pgpgout_hash = 0;
68 static uint32_t pgfault_hash = 0;
69 static uint32_t pgmajfault_hash = 0;
70 static uint32_t inactive_anon_hash = 0;
71 static uint32_t active_anon_hash = 0;
72 static uint32_t inactive_file_hash = 0;
73 static uint32_t active_file_hash = 0;
74 static uint32_t unevictable_hash = 0;
75 static uint32_t hierarchical_memory_limit_hash = 0;
76 static uint32_t total_cache_hash = 0;
77 static uint32_t total_rss_hash = 0;
78 static uint32_t total_rss_huge_hash = 0;
79 static uint32_t total_mapped_file_hash = 0;
80 static uint32_t total_writeback_hash = 0;
81 static uint32_t total_dirty_hash = 0;
82 static uint32_t total_swap_hash = 0;
83 static uint32_t total_pgpgin_hash = 0;
84 static uint32_t total_pgpgout_hash = 0;
85 static uint32_t total_pgfault_hash = 0;
86 static uint32_t total_pgmajfault_hash = 0;
87 static uint32_t total_inactive_anon_hash = 0;
88 static uint32_t total_active_anon_hash = 0;
89 static uint32_t total_inactive_file_hash = 0;
90 static uint32_t total_active_file_hash = 0;
91 static uint32_t total_unevictable_hash = 0;
93 void read_cgroup_plugin_configuration() {
94 system_page_size = sysconf(_SC_PAGESIZE);
96 Read_hash = simple_hash("Read");
97 Write_hash = simple_hash("Write");
98 Sync_hash = simple_hash("Sync");
99 Async_hash = simple_hash("Async");
100 Total_hash = simple_hash("Total");
101 user_hash = simple_hash("user");
102 system_hash = simple_hash("system");
103 cache_hash = simple_hash("cache");
104 rss_hash = simple_hash("rss");
105 rss_huge_hash = simple_hash("rss_huge");
106 mapped_file_hash = simple_hash("mapped_file");
107 writeback_hash = simple_hash("writeback");
108 dirty_hash = simple_hash("dirty");
109 swap_hash = simple_hash("swap");
110 pgpgin_hash = simple_hash("pgpgin");
111 pgpgout_hash = simple_hash("pgpgout");
112 pgfault_hash = simple_hash("pgfault");
113 pgmajfault_hash = simple_hash("pgmajfault");
114 inactive_anon_hash = simple_hash("inactive_anon");
115 active_anon_hash = simple_hash("active_anon");
116 inactive_file_hash = simple_hash("inactive_file");
117 active_file_hash = simple_hash("active_file");
118 unevictable_hash = simple_hash("unevictable");
119 hierarchical_memory_limit_hash = simple_hash("hierarchical_memory_limit");
120 total_cache_hash = simple_hash("total_cache");
121 total_rss_hash = simple_hash("total_rss");
122 total_rss_huge_hash = simple_hash("total_rss_huge");
123 total_mapped_file_hash = simple_hash("total_mapped_file");
124 total_writeback_hash = simple_hash("total_writeback");
125 total_dirty_hash = simple_hash("total_dirty");
126 total_swap_hash = simple_hash("total_swap");
127 total_pgpgin_hash = simple_hash("total_pgpgin");
128 total_pgpgout_hash = simple_hash("total_pgpgout");
129 total_pgfault_hash = simple_hash("total_pgfault");
130 total_pgmajfault_hash = simple_hash("total_pgmajfault");
131 total_inactive_anon_hash = simple_hash("total_inactive_anon");
132 total_active_anon_hash = simple_hash("total_active_anon");
133 total_inactive_file_hash = simple_hash("total_inactive_file");
134 total_active_file_hash = simple_hash("total_active_file");
135 total_unevictable_hash = simple_hash("total_unevictable");
137 cgroup_update_every = (int)config_get_number("plugin:cgroups", "update every", rrd_update_every);
138 if(cgroup_update_every < rrd_update_every)
139 cgroup_update_every = rrd_update_every;
141 cgroup_check_for_new_every = (int)config_get_number("plugin:cgroups", "check for new cgroups every", cgroup_check_for_new_every * cgroup_update_every);
142 if(cgroup_check_for_new_every < cgroup_update_every)
143 cgroup_check_for_new_every = cgroup_update_every;
145 cgroup_enable_cpuacct_stat = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct stat (total CPU)", cgroup_enable_cpuacct_stat);
146 cgroup_enable_cpuacct_usage = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct usage (per core CPU)", cgroup_enable_cpuacct_usage);
148 cgroup_enable_memory = config_get_boolean_ondemand("plugin:cgroups", "enable memory (used mem including cache)", cgroup_enable_memory);
149 cgroup_enable_detailed_memory = config_get_boolean_ondemand("plugin:cgroups", "enable detailed memory", cgroup_enable_detailed_memory);
150 cgroup_enable_memory_failcnt = config_get_boolean_ondemand("plugin:cgroups", "enable memory limits fail count", cgroup_enable_memory_failcnt);
151 cgroup_enable_swap = config_get_boolean_ondemand("plugin:cgroups", "enable swap memory", cgroup_enable_swap);
153 cgroup_enable_blkio_io = config_get_boolean_ondemand("plugin:cgroups", "enable blkio bandwidth", cgroup_enable_blkio_io);
154 cgroup_enable_blkio_ops = config_get_boolean_ondemand("plugin:cgroups", "enable blkio operations", cgroup_enable_blkio_ops);
155 cgroup_enable_blkio_throttle_io = config_get_boolean_ondemand("plugin:cgroups", "enable blkio throttle bandwidth", cgroup_enable_blkio_throttle_io);
156 cgroup_enable_blkio_throttle_ops = config_get_boolean_ondemand("plugin:cgroups", "enable blkio throttle operations", cgroup_enable_blkio_throttle_ops);
157 cgroup_enable_blkio_queued_ops = config_get_boolean_ondemand("plugin:cgroups", "enable blkio queued operations", cgroup_enable_blkio_queued_ops);
158 cgroup_enable_blkio_merged_ops = config_get_boolean_ondemand("plugin:cgroups", "enable blkio merged operations", cgroup_enable_blkio_merged_ops);
160 cgroup_recheck_zero_blkio_every_iterations = (int)config_get_number("plugin:cgroups", "recheck zero blkio every iterations", cgroup_recheck_zero_blkio_every_iterations);
161 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);
162 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);
164 cgroup_enable_systemd_services = config_get_boolean("plugin:cgroups", "enable systemd services", cgroup_enable_systemd_services);
165 cgroup_enable_systemd_services_detailed_memory = config_get_boolean("plugin:cgroups", "enable systemd services detailed memory", cgroup_enable_systemd_services_detailed_memory);
167 char filename[FILENAME_MAX + 1], *s;
168 struct mountinfo *mi, *root = mountinfo_read(0);
170 mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "cpuacct");
171 if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "cpuacct");
173 error("Cannot find cgroup cpuacct mountinfo. Assuming default: /sys/fs/cgroup/cpuacct");
174 s = "/sys/fs/cgroup/cpuacct";
176 else s = mi->mount_point;
177 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
178 cgroup_cpuacct_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/cpuacct", filename);
180 mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "blkio");
181 if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "blkio");
183 error("Cannot find cgroup blkio mountinfo. Assuming default: /sys/fs/cgroup/blkio");
184 s = "/sys/fs/cgroup/blkio";
186 else s = mi->mount_point;
187 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
188 cgroup_blkio_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/blkio", filename);
190 mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "memory");
191 if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "memory");
193 error("Cannot find cgroup memory mountinfo. Assuming default: /sys/fs/cgroup/memory");
194 s = "/sys/fs/cgroup/memory";
196 else s = mi->mount_point;
197 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
198 cgroup_memory_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/memory", filename);
200 mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "devices");
201 if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "devices");
203 error("Cannot find cgroup devices mountinfo. Assuming default: /sys/fs/cgroup/devices");
204 s = "/sys/fs/cgroup/devices";
206 else s = mi->mount_point;
207 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
208 cgroup_devices_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/devices", filename);
210 cgroup_root_max = (int)config_get_number("plugin:cgroups", "max cgroups to allow", cgroup_root_max);
211 cgroup_max_depth = (int)config_get_number("plugin:cgroups", "max cgroups depth to monitor", cgroup_max_depth);
213 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);
215 enabled_cgroup_patterns = simple_pattern_create(
216 config_get("plugin:cgroups", "enable by default cgroups matching",
228 " !/lxc/*/ns " // #1397
234 " * " // enable anything else
235 ), SIMPLE_PATTERN_EXACT);
237 enabled_cgroup_paths = simple_pattern_create(
238 config_get("plugin:cgroups", "search for cgroups in subpaths matching",
246 ), SIMPLE_PATTERN_EXACT);
248 cgroups_rename_script = config_get("plugin:cgroups", "script to get cgroup names", cgroups_rename_script);
250 enabled_cgroup_renames = simple_pattern_create(
251 config_get("plugin:cgroups", "run script to rename cgroups matching",
261 ), SIMPLE_PATTERN_EXACT);
263 if(cgroup_enable_systemd_services) {
264 systemd_services_cgroups = simple_pattern_create(
265 config_get("plugin:cgroups", "cgroups to match as systemd services",
266 " !/system.slice/*/*.service "
267 " /system.slice/*.service "
268 ), SIMPLE_PATTERN_EXACT);
271 mountinfo_free(root);
274 // ----------------------------------------------------------------------------
279 int enabled; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
284 unsigned long long Read;
285 unsigned long long Write;
287 unsigned long long Sync;
288 unsigned long long Async;
289 unsigned long long Total;
293 // https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt
295 int updated_detailed;
296 int updated_usage_in_bytes;
297 int updated_msw_usage_in_bytes;
300 int enabled_detailed; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
301 int enabled_usage_in_bytes; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
302 int enabled_msw_usage_in_bytes; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
303 int enabled_failcnt; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
305 int delay_counter_detailed;
306 int delay_counter_failcnt;
308 char *filename_detailed;
309 char *filename_usage_in_bytes;
310 char *filename_msw_usage_in_bytes;
311 char *filename_failcnt;
313 int detailed_has_dirty_swap;
316 unsigned long long cache;
317 unsigned long long rss;
318 unsigned long long rss_huge;
319 unsigned long long mapped_file;
320 unsigned long long writeback;
321 unsigned long long dirty;
322 unsigned long long swap;
323 unsigned long long pgpgin;
324 unsigned long long pgpgout;
325 unsigned long long pgfault;
326 unsigned long long pgmajfault;
328 unsigned long long inactive_anon;
329 unsigned long long active_anon;
330 unsigned long long inactive_file;
331 unsigned long long active_file;
332 unsigned long long unevictable;
333 unsigned long long hierarchical_memory_limit;
334 unsigned long long total_cache;
335 unsigned long long total_rss;
336 unsigned long long total_rss_huge;
337 unsigned long long total_mapped_file;
338 unsigned long long total_writeback;
339 unsigned long long total_dirty;
340 unsigned long long total_swap;
341 unsigned long long total_pgpgin;
342 unsigned long long total_pgpgout;
343 unsigned long long total_pgfault;
344 unsigned long long total_pgmajfault;
345 unsigned long long total_inactive_anon;
346 unsigned long long total_active_anon;
347 unsigned long long total_inactive_file;
348 unsigned long long total_active_file;
349 unsigned long long total_unevictable;
352 // single file metrics
353 unsigned long long usage_in_bytes;
354 unsigned long long msw_usage_in_bytes;
355 unsigned long long failcnt;
358 // https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt
359 struct cpuacct_stat {
361 int enabled; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
365 unsigned long long user;
366 unsigned long long system;
369 // https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt
370 struct cpuacct_usage {
372 int enabled; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
377 unsigned long long *cpu_percpu;
380 #define CGROUP_OPTIONS_DISABLED_DUPLICATE 0x00000001
381 #define CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE 0x00000002
386 char available; // found in the filesystem
387 char enabled; // enabled in the config
397 struct cpuacct_stat cpuacct_stat;
398 struct cpuacct_usage cpuacct_usage;
400 struct memory memory;
402 struct blkio io_service_bytes; // bytes
403 struct blkio io_serviced; // operations
405 struct blkio throttle_io_service_bytes; // bytes
406 struct blkio throttle_io_serviced; // operations
408 struct blkio io_merged; // operations
409 struct blkio io_queued; // operations
413 RRDSET *st_cpu_per_core;
415 RRDSET *st_writeback;
416 RRDSET *st_mem_activity;
418 RRDSET *st_mem_usage;
419 RRDSET *st_mem_failcnt;
421 RRDSET *st_serviced_ops;
422 RRDSET *st_throttle_io;
423 RRDSET *st_throttle_serviced_ops;
424 RRDSET *st_queued_ops;
425 RRDSET *st_merged_ops;
429 RRDDIM *rd_mem_usage;
430 RRDDIM *rd_mem_failcnt;
431 RRDDIM *rd_swap_usage;
433 RRDDIM *rd_mem_detailed_cache;
434 RRDDIM *rd_mem_detailed_rss;
435 RRDDIM *rd_mem_detailed_mapped;
436 RRDDIM *rd_mem_detailed_writeback;
437 RRDDIM *rd_mem_detailed_dirty;
438 RRDDIM *rd_mem_detailed_swap;
439 RRDDIM *rd_mem_detailed_pgpgin;
440 RRDDIM *rd_mem_detailed_pgpgout;
441 RRDDIM *rd_mem_detailed_pgfault;
442 RRDDIM *rd_mem_detailed_pgmajfault;
444 RRDDIM *rd_io_service_bytes_read;
445 RRDDIM *rd_io_serviced_read;
446 RRDDIM *rd_throttle_io_read;
447 RRDDIM *rd_throttle_io_serviced_read;
448 RRDDIM *rd_io_queued_read;
449 RRDDIM *rd_io_merged_read;
451 RRDDIM *rd_io_service_bytes_write;
452 RRDDIM *rd_io_serviced_write;
453 RRDDIM *rd_throttle_io_write;
454 RRDDIM *rd_throttle_io_serviced_write;
455 RRDDIM *rd_io_queued_write;
456 RRDDIM *rd_io_merged_write;
460 } *cgroup_root = NULL;
462 // ----------------------------------------------------------------------------
463 // read values from /sys
465 static inline void cgroup_read_cpuacct_stat(struct cpuacct_stat *cp) {
466 static procfile *ff = NULL;
468 if(likely(cp->filename)) {
469 ff = procfile_reopen(ff, cp->filename, NULL, PROCFILE_FLAG_DEFAULT);
475 ff = procfile_readall(ff);
481 unsigned long i, lines = procfile_lines(ff);
483 if(unlikely(lines < 1)) {
484 error("File '%s' should have 1+ lines.", cp->filename);
489 for(i = 0; i < lines ; i++) {
490 char *s = procfile_lineword(ff, i, 0);
491 uint32_t hash = simple_hash(s);
493 if(unlikely(hash == user_hash && !strcmp(s, "user"))) {
494 cp->user = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
498 if(unlikely(hash == system_hash && !strcmp(s, "system"))) {
499 cp->system = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
506 if(unlikely(cp->enabled == CONFIG_ONDEMAND_ONDEMAND && (cp->user || cp->system)))
507 cp->enabled = CONFIG_ONDEMAND_YES;
511 static inline void cgroup_read_cpuacct_usage(struct cpuacct_usage *ca) {
512 static procfile *ff = NULL;
514 if(likely(ca->filename)) {
515 ff = procfile_reopen(ff, ca->filename, NULL, PROCFILE_FLAG_DEFAULT);
521 ff = procfile_readall(ff);
527 if(unlikely(procfile_lines(ff) < 1)) {
528 error("File '%s' should have 1+ lines but has %u.", ca->filename, procfile_lines(ff));
533 unsigned long i = procfile_linewords(ff, 0);
534 if(unlikely(i == 0)) {
539 // we may have 1 more CPU reported
541 char *s = procfile_lineword(ff, 0, i - 1);
546 if(unlikely(i != ca->cpus)) {
547 freez(ca->cpu_percpu);
548 ca->cpu_percpu = mallocz(sizeof(unsigned long long) * i);
549 ca->cpus = (unsigned int)i;
552 unsigned long long total = 0;
553 for(i = 0; i < ca->cpus ;i++) {
554 unsigned long long n = strtoull(procfile_lineword(ff, 0, i), NULL, 10);
555 ca->cpu_percpu[i] = n;
561 if(unlikely(ca->enabled == CONFIG_ONDEMAND_ONDEMAND && total))
562 ca->enabled = CONFIG_ONDEMAND_YES;
566 static inline void cgroup_read_blkio(struct blkio *io) {
567 static procfile *ff = NULL;
569 if(unlikely(io->enabled == CONFIG_ONDEMAND_ONDEMAND && io->delay_counter > 0)) {
574 if(likely(io->filename)) {
575 ff = procfile_reopen(ff, io->filename, NULL, PROCFILE_FLAG_DEFAULT);
581 ff = procfile_readall(ff);
587 unsigned long i, lines = procfile_lines(ff);
589 if(unlikely(lines < 1)) {
590 error("File '%s' should have 1+ lines.", io->filename);
603 for(i = 0; i < lines ; i++) {
604 char *s = procfile_lineword(ff, i, 1);
605 uint32_t hash = simple_hash(s);
607 if(unlikely(hash == Read_hash && !strcmp(s, "Read"))) {
608 io->Read += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
612 if(unlikely(hash == Write_hash && !strcmp(s, "Write"))) {
613 io->Write += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
618 if(hash == Sync_hash && !strcmp(s, "Sync")) {
619 io->Sync += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
623 if(hash == Async_hash && !strcmp(s, "Async")) {
624 io->Async += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
628 if(hash == Total_hash && !strcmp(s, "Total")) {
629 io->Total += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
637 if(unlikely(io->enabled == CONFIG_ONDEMAND_ONDEMAND)) {
638 if(unlikely(io->Read || io->Write))
639 io->enabled = CONFIG_ONDEMAND_YES;
641 io->delay_counter = cgroup_recheck_zero_blkio_every_iterations;
646 static inline void cgroup_read_memory(struct memory *mem) {
647 static procfile *ff = NULL;
649 // read detailed ram usage
650 if(likely(mem->filename_detailed)) {
651 if(unlikely(mem->enabled_detailed == CONFIG_ONDEMAND_ONDEMAND && mem->delay_counter_detailed > 0)) {
652 mem->delay_counter_detailed--;
656 ff = procfile_reopen(ff, mem->filename_detailed, NULL, PROCFILE_FLAG_DEFAULT);
658 mem->updated_detailed = 0;
662 ff = procfile_readall(ff);
664 mem->updated_detailed = 0;
668 unsigned long i, lines = procfile_lines(ff);
670 if(unlikely(lines < 1)) {
671 error("File '%s' should have 1+ lines.", mem->filename_detailed);
672 mem->updated_detailed = 0;
676 for(i = 0; i < lines ; i++) {
677 char *s = procfile_lineword(ff, i, 0);
678 uint32_t hash = simple_hash(s);
680 if(unlikely(hash == cache_hash && !strcmp(s, "cache"))) {
681 mem->cache = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
685 if(unlikely(hash == rss_hash && !strcmp(s, "rss"))) {
686 mem->rss = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
690 if(unlikely(hash == rss_huge_hash && !strcmp(s, "rss_huge"))) {
691 mem->rss_huge = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
695 if(unlikely(hash == mapped_file_hash && !strcmp(s, "mapped_file"))) {
696 mem->mapped_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
700 if(unlikely(hash == writeback_hash && !strcmp(s, "writeback"))) {
701 mem->writeback = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
705 if(unlikely(hash == dirty_hash && !strcmp(s, "dirty"))) {
706 mem->dirty = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
707 mem->detailed_has_dirty_swap = 1;
711 if(unlikely(hash == swap_hash && !strcmp(s, "swap"))) {
712 mem->swap = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
713 mem->detailed_has_dirty_swap = 1;
717 if(unlikely(hash == pgpgin_hash && !strcmp(s, "pgpgin"))) {
718 mem->pgpgin = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
722 if(unlikely(hash == pgpgout_hash && !strcmp(s, "pgpgout"))) {
723 mem->pgpgout = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
727 if(unlikely(hash == pgfault_hash && !strcmp(s, "pgfault"))) {
728 mem->pgfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
732 if(unlikely(hash == pgmajfault_hash && !strcmp(s, "pgmajfault"))) {
733 mem->pgmajfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
738 if(unlikely(hash == inactive_anon_hash && !strcmp(s, "inactive_anon"))) {
739 mem->inactive_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
743 if(unlikely(hash == active_anon_hash && !strcmp(s, "active_anon"))) {
744 mem->active_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
748 if(unlikely(hash == inactive_file_hash && !strcmp(s, "inactive_file"))) {
749 mem->inactive_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
753 if(unlikely(hash == active_file_hash && !strcmp(s, "active_file"))) {
754 mem->active_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
758 if(unlikely(hash == unevictable_hash && !strcmp(s, "unevictable"))) {
759 mem->unevictable = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
763 if(unlikely(hash == hierarchical_memory_limit_hash && !strcmp(s, "hierarchical_memory_limit"))) {
764 mem->hierarchical_memory_limit = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
768 if(unlikely(hash == total_cache_hash && !strcmp(s, "total_cache"))) {
769 mem->total_cache = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
773 if(unlikely(hash == total_rss_hash && !strcmp(s, "total_rss"))) {
774 mem->total_rss = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
778 if(unlikely(hash == total_rss_huge_hash && !strcmp(s, "total_rss_huge"))) {
779 mem->total_rss_huge = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
783 if(unlikely(hash == total_mapped_file_hash && !strcmp(s, "total_mapped_file"))) {
784 mem->total_mapped_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
788 if(unlikely(hash == total_writeback_hash && !strcmp(s, "total_writeback"))) {
789 mem->total_writeback = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
793 if(unlikely(hash == total_dirty_hash && !strcmp(s, "total_dirty"))) {
794 mem->total_dirty = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
798 if(unlikely(hash == total_swap_hash && !strcmp(s, "total_swap"))) {
799 mem->total_swap = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
803 if(unlikely(hash == total_pgpgin_hash && !strcmp(s, "total_pgpgin"))) {
804 mem->total_pgpgin = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
808 if(unlikely(hash == total_pgpgout_hash && !strcmp(s, "total_pgpgout"))) {
809 mem->total_pgpgout = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
813 if(unlikely(hash == total_pgfault_hash && !strcmp(s, "total_pgfault"))) {
814 mem->total_pgfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
818 if(unlikely(hash == total_pgmajfault_hash && !strcmp(s, "total_pgmajfault"))) {
819 mem->total_pgmajfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
823 if(unlikely(hash == total_inactive_anon_hash && !strcmp(s, "total_inactive_anon"))) {
824 mem->total_inactive_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
828 if(unlikely(hash == total_active_anon_hash && !strcmp(s, "total_active_anon"))) {
829 mem->total_active_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
833 if(unlikely(hash == total_inactive_file_hash && !strcmp(s, "total_inactive_file"))) {
834 mem->total_inactive_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
838 if(unlikely(hash == total_active_file_hash && !strcmp(s, "total_active_file"))) {
839 mem->total_active_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
843 if(unlikely(hash == total_unevictable_hash && !strcmp(s, "total_unevictable"))) {
844 mem->total_unevictable = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
850 // 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);
852 mem->updated_detailed = 1;
854 if(unlikely(mem->enabled_detailed == CONFIG_ONDEMAND_ONDEMAND)) {
855 if(mem->cache || mem->dirty || mem->rss || mem->rss_huge || mem->mapped_file || mem->writeback || mem->swap || mem->pgpgin || mem->pgpgout || mem->pgfault || mem->pgmajfault)
856 mem->enabled_detailed = CONFIG_ONDEMAND_YES;
858 mem->delay_counter_detailed = cgroup_recheck_zero_mem_detailed_every_iterations;
864 // read usage_in_bytes
865 if(likely(mem->filename_usage_in_bytes)) {
866 if(likely(!read_single_number_file(mem->filename_usage_in_bytes, &mem->usage_in_bytes))) {
867 mem->updated_usage_in_bytes = 1;
869 if(unlikely(mem->enabled_usage_in_bytes == CONFIG_ONDEMAND_ONDEMAND && mem->usage_in_bytes))
870 mem->enabled_usage_in_bytes = CONFIG_ONDEMAND_YES;
873 mem->updated_usage_in_bytes = 0;
876 // read msw_usage_in_bytes
877 if(likely(mem->filename_msw_usage_in_bytes)) {
878 if(likely(!read_single_number_file(mem->filename_msw_usage_in_bytes, &mem->msw_usage_in_bytes))) {
879 mem->updated_msw_usage_in_bytes = 1;
881 if(unlikely(mem->enabled_msw_usage_in_bytes == CONFIG_ONDEMAND_ONDEMAND && mem->msw_usage_in_bytes))
882 mem->enabled_msw_usage_in_bytes = CONFIG_ONDEMAND_YES;
885 mem->updated_msw_usage_in_bytes = 0;
889 if(likely(mem->filename_failcnt)) {
890 if(unlikely(mem->enabled_failcnt == CONFIG_ONDEMAND_ONDEMAND && mem->delay_counter_failcnt > 0)) {
891 mem->updated_failcnt = 0;
892 mem->delay_counter_failcnt--;
894 else if(likely(!read_single_number_file(mem->filename_failcnt, &mem->failcnt))) {
895 mem->updated_failcnt = 1;
897 if(unlikely(mem->enabled_failcnt == CONFIG_ONDEMAND_ONDEMAND)) {
898 if(unlikely(!mem->failcnt))
899 mem->delay_counter_failcnt = cgroup_recheck_zero_mem_failcnt_every_iterations;
901 mem->enabled_failcnt = CONFIG_ONDEMAND_YES;
907 static inline void cgroup_read(struct cgroup *cg) {
908 debug(D_CGROUP, "reading metrics for cgroups '%s'", cg->id);
910 cgroup_read_cpuacct_stat(&cg->cpuacct_stat);
911 cgroup_read_cpuacct_usage(&cg->cpuacct_usage);
912 cgroup_read_memory(&cg->memory);
913 cgroup_read_blkio(&cg->io_service_bytes);
914 cgroup_read_blkio(&cg->io_serviced);
915 cgroup_read_blkio(&cg->throttle_io_service_bytes);
916 cgroup_read_blkio(&cg->throttle_io_serviced);
917 cgroup_read_blkio(&cg->io_merged);
918 cgroup_read_blkio(&cg->io_queued);
921 static inline void read_all_cgroups(struct cgroup *root) {
922 debug(D_CGROUP, "reading metrics for all cgroups");
926 for(cg = root; cg ; cg = cg->next)
927 if(cg->enabled && cg->available)
931 // ----------------------------------------------------------------------------
932 // add/remove/find cgroup objects
934 #define CGROUP_CHARTID_LINE_MAX 1024
936 static inline char *cgroup_title_strdupz(const char *s) {
937 if(!s || !*s) s = "/";
939 if(*s == '/' && s[1] != '\0') s++;
941 char *r = strdupz(s);
942 netdata_fix_chart_name(r);
947 static inline char *cgroup_chart_id_strdupz(const char *s) {
948 if(!s || !*s) s = "/";
950 if(*s == '/' && s[1] != '\0') s++;
952 char *r = strdupz(s);
953 netdata_fix_chart_id(r);
958 static inline void cgroup_get_chart_name(struct cgroup *cg) {
959 debug(D_CGROUP, "looking for the name of cgroup '%s' with chart id '%s' and title '%s'", cg->id, cg->chart_id, cg->chart_title);
962 char buffer[CGROUP_CHARTID_LINE_MAX + 1];
964 snprintfz(buffer, CGROUP_CHARTID_LINE_MAX, "exec %s '%s'", cgroups_rename_script, cg->chart_id);
966 debug(D_CGROUP, "executing command '%s' for cgroup '%s'", buffer, cg->id);
967 FILE *fp = mypopen(buffer, &cgroup_pid);
969 // debug(D_CGROUP, "reading from command '%s' for cgroup '%s'", buffer, cg->id);
970 char *s = fgets(buffer, CGROUP_CHARTID_LINE_MAX, fp);
971 // debug(D_CGROUP, "closing command for cgroup '%s'", cg->id);
972 mypclose(fp, cgroup_pid);
973 // debug(D_CGROUP, "closed command for cgroup '%s'", cg->id);
975 if(s && *s && *s != '\n') {
976 debug(D_CGROUP, "cgroup '%s' should be renamed to '%s'", cg->id, s);
980 freez(cg->chart_title);
981 cg->chart_title = cgroup_title_strdupz(s);
984 cg->chart_id = cgroup_chart_id_strdupz(s);
985 cg->hash_chart = simple_hash(cg->chart_id);
989 error("CGROUP: Cannot popen(\"%s\", \"r\").", buffer);
992 static inline struct cgroup *cgroup_add(const char *id) {
993 if(!id || !*id) id = "/";
994 debug(D_CGROUP, "adding to list, cgroup with id '%s'", id);
996 if(cgroup_root_count >= cgroup_root_max) {
997 info("Maximum number of cgroups reached (%d). Not adding cgroup '%s'", cgroup_root_count, id);
1001 int def = simple_pattern_matches(enabled_cgroup_patterns, id)?cgroup_enable_new_cgroups_detected_at_runtime:0;
1002 struct cgroup *cg = callocz(1, sizeof(struct cgroup));
1004 cg->id = strdupz(id);
1005 cg->hash = simple_hash(cg->id);
1007 cg->chart_title = cgroup_title_strdupz(id);
1009 cg->chart_id = cgroup_chart_id_strdupz(id);
1010 cg->hash_chart = simple_hash(cg->chart_id);
1017 for(e = cgroup_root; e->next ;e = e->next) ;
1021 cgroup_root_count++;
1023 // fix the chart_id and title by calling the external script
1024 if(simple_pattern_matches(enabled_cgroup_renames, cg->id) ||
1025 simple_pattern_matches(enabled_cgroup_renames, cg->chart_id)) {
1027 cgroup_get_chart_name(cg);
1029 debug(D_CGROUP, "cgroup '%s' renamed to '%s' (title: '%s')", cg->id, cg->chart_id, cg->chart_title);
1032 debug(D_CGROUP, "cgroup '%s' will not be renamed - it matches the list of disabled cgroup renames (will be shown as '%s')", cg->id, cg->chart_id);
1034 int user_configurable = 1;
1036 // check if this cgroup should be a systemd service
1037 if(cgroup_enable_systemd_services) {
1038 if(simple_pattern_matches(systemd_services_cgroups, cg->id) ||
1039 simple_pattern_matches(systemd_services_cgroups, cg->chart_id)) {
1040 debug(D_CGROUP, "cgroup '%s' with chart id '%s' (title: '%s') matches systemd services cgroups", cg->id, cg->chart_id, cg->chart_title);
1042 char buffer[CGROUP_CHARTID_LINE_MAX + 1];
1043 cg->options |= CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE;
1045 strncpy(buffer, cg->id, CGROUP_CHARTID_LINE_MAX);
1048 //freez(cg->chart_id);
1049 //cg->chart_id = cgroup_chart_id_strdupz(s);
1050 //cg->hash_chart = simple_hash(cg->chart_id);
1052 // skip to the last slash
1053 size_t len = strlen(s);
1054 while(len--) if(unlikely(s[len] == '/')) break;
1055 if(len) s = &s[len + 1];
1059 while(len--) if(unlikely(s[len] == '.')) break;
1060 if(len) s[len] = '\0';
1062 freez(cg->chart_title);
1063 cg->chart_title = cgroup_title_strdupz(s);
1066 user_configurable = 0;
1068 debug(D_CGROUP, "cgroup '%s' renamed to '%s' (title: '%s')", cg->id, cg->chart_id, cg->chart_title);
1071 debug(D_CGROUP, "cgroup '%s' with chart id '%s' (title: '%s') does not match systemd services groups", cg->id, cg->chart_id, cg->chart_title);
1074 if(user_configurable) {
1075 // allow the user to enable/disable this individualy
1076 char option[FILENAME_MAX + 1];
1077 snprintfz(option, FILENAME_MAX, "enable cgroup %s", cg->chart_title);
1078 cg->enabled = (char) config_get_boolean("plugin:cgroups", option, def);
1081 // detect duplicate cgroups
1084 for (t = cgroup_root; t; t = t->next) {
1085 if (t != cg && t->enabled && t->hash_chart == cg->hash_chart && !strcmp(t->chart_id, cg->chart_id)) {
1086 if (!strncmp(t->chart_id, "/system.slice/", 14) && !strncmp(cg->chart_id, "/init.scope/system.slice/", 25)) {
1087 error("Control group with chart id '%s' already exists with id '%s' and is enabled. Swapping them by enabling cgroup with id '%s' and disabling cgroup with id '%s'.",
1088 cg->chart_id, t->id, cg->id, t->id);
1089 debug(D_CGROUP, "Control group with chart id '%s' already exists with id '%s' and is enabled. Swapping them by enabling cgroup with id '%s' and disabling cgroup with id '%s'.",
1090 cg->chart_id, t->id, cg->id, t->id);
1092 t->options |= CGROUP_OPTIONS_DISABLED_DUPLICATE;
1095 error("Control group with chart id '%s' already exists with id '%s' and is enabled and available. Disabling cgroup with id '%s'.",
1096 cg->chart_id, t->id, cg->id);
1097 debug(D_CGROUP, "Control group with chart id '%s' already exists with id '%s' and is enabled and available. Disabling cgroup with id '%s'.",
1098 cg->chart_id, t->id, cg->id);
1100 cg->options |= CGROUP_OPTIONS_DISABLED_DUPLICATE;
1108 debug(D_CGROUP, "ADDED CGROUP: '%s' with chart id '%s' and title '%s' as %s (default was %s)", cg->id, cg->chart_id, cg->chart_title, (cg->enabled)?"enabled":"disabled", (def)?"enabled":"disabled");
1113 static inline void cgroup_free(struct cgroup *cg) {
1114 debug(D_CGROUP, "Removing cgroup '%s' with chart id '%s' (was %s and %s)", cg->id, cg->chart_id, (cg->enabled)?"enabled":"disabled", (cg->available)?"available":"not available");
1116 freez(cg->cpuacct_usage.cpu_percpu);
1118 freez(cg->cpuacct_stat.filename);
1119 freez(cg->cpuacct_usage.filename);
1121 freez(cg->memory.filename_detailed);
1122 freez(cg->memory.filename_failcnt);
1123 freez(cg->memory.filename_usage_in_bytes);
1124 freez(cg->memory.filename_msw_usage_in_bytes);
1126 freez(cg->io_service_bytes.filename);
1127 freez(cg->io_serviced.filename);
1129 freez(cg->throttle_io_service_bytes.filename);
1130 freez(cg->throttle_io_serviced.filename);
1132 freez(cg->io_merged.filename);
1133 freez(cg->io_queued.filename);
1136 freez(cg->chart_id);
1137 freez(cg->chart_title);
1141 cgroup_root_count--;
1144 // find if a given cgroup exists
1145 static inline struct cgroup *cgroup_find(const char *id) {
1146 debug(D_CGROUP, "searching for cgroup '%s'", id);
1148 uint32_t hash = simple_hash(id);
1151 for(cg = cgroup_root; cg ; cg = cg->next) {
1152 if(hash == cg->hash && strcmp(id, cg->id) == 0)
1156 debug(D_CGROUP, "cgroup '%s' %s in memory", id, (cg)?"found":"not found");
1160 // ----------------------------------------------------------------------------
1161 // detect running cgroups
1163 // callback for find_file_in_subdirs()
1164 static inline void found_subdir_in_dir(const char *dir) {
1165 debug(D_CGROUP, "examining cgroup dir '%s'", dir);
1167 struct cgroup *cg = cgroup_find(dir);
1169 if(*dir && cgroup_max_depth > 0) {
1173 for(s = dir; *s ;s++)
1174 if(unlikely(*s == '/'))
1177 if(depth > cgroup_max_depth) {
1178 info("cgroup '%s' is too deep (%d, while max is %d)", dir, depth, cgroup_max_depth);
1182 // debug(D_CGROUP, "will add dir '%s' as cgroup", dir);
1183 cg = cgroup_add(dir);
1186 if(cg) cg->available = 1;
1189 static inline int find_dir_in_subdirs(const char *base, const char *this, void (*callback)(const char *)) {
1190 if(!this) this = base;
1191 debug(D_CGROUP, "searching for directories in '%s' (base '%s')", this?this:"", base);
1193 size_t dirlen = strlen(this), baselen = strlen(base);
1198 const char *relative_path = &this[baselen];
1199 if(!*relative_path) relative_path = "/";
1201 DIR *dir = opendir(this);
1203 error("Cannot read cgroups directory '%s'", base);
1208 callback(relative_path);
1210 struct dirent *de = NULL;
1211 while((de = readdir(dir))) {
1212 if(de->d_type == DT_DIR
1214 (de->d_name[0] == '.' && de->d_name[1] == '\0')
1215 || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
1219 if(de->d_type == DT_DIR) {
1221 const char *r = relative_path;
1222 if(*r == '\0') r = "/";
1224 // do not decent in directories we are not interested
1225 int def = simple_pattern_matches(enabled_cgroup_paths, r);
1227 // we check for this option here
1228 // so that the config will not have settings
1229 // for leaf directories
1230 char option[FILENAME_MAX + 1];
1231 snprintfz(option, FILENAME_MAX, "search for cgroups under %s", r);
1232 option[FILENAME_MAX] = '\0';
1233 enabled = config_get_boolean("plugin:cgroups", option, def);
1237 char *s = mallocz(dirlen + strlen(de->d_name) + 2);
1240 strcat(s, de->d_name);
1241 int ret2 = find_dir_in_subdirs(base, s, callback);
1242 if(ret2 > 0) ret += ret2;
1252 static inline void mark_all_cgroups_as_not_available() {
1253 debug(D_CGROUP, "marking all cgroups as not available");
1257 // mark all as not available
1258 for(cg = cgroup_root; cg ; cg = cg->next) {
1263 static inline void cleanup_all_cgroups() {
1264 struct cgroup *cg = cgroup_root, *last = NULL;
1267 if(!cg->available) {
1268 // enable the first duplicate cgroup
1271 for(t = cgroup_root; t ; t = t->next) {
1272 if(t != cg && t->available && !t->enabled && t->options & CGROUP_OPTIONS_DISABLED_DUPLICATE && t->hash_chart == cg->hash_chart && !strcmp(t->chart_id, cg->chart_id)) {
1273 debug(D_CGROUP, "Enabling duplicate of cgroup '%s' with id '%s', because the original with id '%s' stopped.", t->chart_id, t->id, cg->id);
1275 t->options &= ~CGROUP_OPTIONS_DISABLED_DUPLICATE;
1282 cgroup_root = cg->next;
1284 last->next = cg->next;
1300 static inline void find_all_cgroups() {
1301 debug(D_CGROUP, "searching for cgroups");
1303 mark_all_cgroups_as_not_available();
1305 if(cgroup_enable_cpuacct_stat || cgroup_enable_cpuacct_usage) {
1306 if(find_dir_in_subdirs(cgroup_cpuacct_base, NULL, found_subdir_in_dir) == -1) {
1307 cgroup_enable_cpuacct_stat =
1308 cgroup_enable_cpuacct_usage = CONFIG_ONDEMAND_NO;
1309 error("disabled CGROUP cpu statistics.");
1313 if(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) {
1314 if(find_dir_in_subdirs(cgroup_blkio_base, NULL, found_subdir_in_dir) == -1) {
1315 cgroup_enable_blkio_io =
1316 cgroup_enable_blkio_ops =
1317 cgroup_enable_blkio_throttle_io =
1318 cgroup_enable_blkio_throttle_ops =
1319 cgroup_enable_blkio_merged_ops =
1320 cgroup_enable_blkio_queued_ops = CONFIG_ONDEMAND_NO;
1321 error("disabled CGROUP blkio statistics.");
1325 if(cgroup_enable_memory || cgroup_enable_detailed_memory || cgroup_enable_swap || cgroup_enable_memory_failcnt) {
1326 if(find_dir_in_subdirs(cgroup_memory_base, NULL, found_subdir_in_dir) == -1) {
1327 cgroup_enable_memory =
1328 cgroup_enable_detailed_memory =
1329 cgroup_enable_swap =
1330 cgroup_enable_memory_failcnt = CONFIG_ONDEMAND_NO;
1331 error("disabled CGROUP memory statistics.");
1335 if(cgroup_search_in_devices) {
1336 if(find_dir_in_subdirs(cgroup_devices_base, NULL, found_subdir_in_dir) == -1) {
1337 cgroup_search_in_devices = 0;
1338 error("disabled CGROUP devices statistics.");
1342 // remove any non-existing cgroups
1343 cleanup_all_cgroups();
1347 for(cg = cgroup_root; cg ; cg = cg->next) {
1348 // fprintf(stderr, " >>> CGROUP '%s' (%u - %s) with name '%s'\n", cg->id, cg->hash, cg->available?"available":"stopped", cg->name);
1350 if(unlikely(!cg->available))
1353 debug(D_CGROUP, "checking paths for cgroup '%s'", cg->id);
1355 // check for newly added cgroups
1356 // and update the filenames they read
1357 char filename[FILENAME_MAX + 1];
1358 if(unlikely(cgroup_enable_cpuacct_stat && !cg->cpuacct_stat.filename)) {
1359 snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.stat", cgroup_cpuacct_base, cg->id);
1360 if(likely(stat(filename, &buf) != -1)) {
1361 cg->cpuacct_stat.filename = strdupz(filename);
1362 cg->cpuacct_stat.enabled = cgroup_enable_cpuacct_stat;
1363 debug(D_CGROUP, "cpuacct.stat filename for cgroup '%s': '%s'", cg->id, cg->cpuacct_stat.filename);
1366 debug(D_CGROUP, "cpuacct.stat file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1369 if(unlikely(cgroup_enable_cpuacct_usage && !cg->cpuacct_usage.filename && !(cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE))) {
1370 snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.usage_percpu", cgroup_cpuacct_base, cg->id);
1371 if(likely(stat(filename, &buf) != -1)) {
1372 cg->cpuacct_usage.filename = strdupz(filename);
1373 cg->cpuacct_usage.enabled = cgroup_enable_cpuacct_usage;
1374 debug(D_CGROUP, "cpuacct.usage_percpu filename for cgroup '%s': '%s'", cg->id, cg->cpuacct_usage.filename);
1377 debug(D_CGROUP, "cpuacct.usage_percpu file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1380 if(unlikely(cgroup_enable_detailed_memory && !cg->memory.filename_detailed && (cgroup_enable_systemd_services_detailed_memory || !(cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE)))) {
1381 snprintfz(filename, FILENAME_MAX, "%s%s/memory.stat", cgroup_memory_base, cg->id);
1382 if(likely(stat(filename, &buf) != -1)) {
1383 cg->memory.filename_detailed = strdupz(filename);
1384 cg->memory.enabled_detailed = cgroup_enable_detailed_memory;
1385 debug(D_CGROUP, "memory.stat filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_detailed);
1388 debug(D_CGROUP, "memory.stat file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1391 if(unlikely(cgroup_enable_memory && !cg->memory.filename_usage_in_bytes)) {
1392 snprintfz(filename, FILENAME_MAX, "%s%s/memory.usage_in_bytes", cgroup_memory_base, cg->id);
1393 if(likely(stat(filename, &buf) != -1)) {
1394 cg->memory.filename_usage_in_bytes = strdupz(filename);
1395 cg->memory.enabled_usage_in_bytes = cgroup_enable_memory;
1396 debug(D_CGROUP, "memory.usage_in_bytes filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_usage_in_bytes);
1399 debug(D_CGROUP, "memory.usage_in_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1402 if(unlikely(cgroup_enable_swap && !cg->memory.filename_msw_usage_in_bytes)) {
1403 snprintfz(filename, FILENAME_MAX, "%s%s/memory.msw_usage_in_bytes", cgroup_memory_base, cg->id);
1404 if(likely(stat(filename, &buf) != -1)) {
1405 cg->memory.filename_msw_usage_in_bytes = strdupz(filename);
1406 cg->memory.enabled_msw_usage_in_bytes = cgroup_enable_swap;
1407 debug(D_CGROUP, "memory.msw_usage_in_bytes filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_msw_usage_in_bytes);
1410 debug(D_CGROUP, "memory.msw_usage_in_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1413 if(unlikely(cgroup_enable_memory_failcnt && !cg->memory.filename_failcnt)) {
1414 snprintfz(filename, FILENAME_MAX, "%s%s/memory.failcnt", cgroup_memory_base, cg->id);
1415 if(likely(stat(filename, &buf) != -1)) {
1416 cg->memory.filename_failcnt = strdupz(filename);
1417 cg->memory.enabled_failcnt = cgroup_enable_memory_failcnt;
1418 debug(D_CGROUP, "memory.failcnt filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_failcnt);
1421 debug(D_CGROUP, "memory.failcnt file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1424 if(unlikely(cgroup_enable_blkio_io && !cg->io_service_bytes.filename)) {
1425 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_service_bytes", cgroup_blkio_base, cg->id);
1426 if(likely(stat(filename, &buf) != -1)) {
1427 cg->io_service_bytes.filename = strdupz(filename);
1428 cg->io_service_bytes.enabled = cgroup_enable_blkio_io;
1429 debug(D_CGROUP, "io_service_bytes filename for cgroup '%s': '%s'", cg->id, cg->io_service_bytes.filename);
1432 debug(D_CGROUP, "io_service_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1435 if(unlikely(cgroup_enable_blkio_ops && !cg->io_serviced.filename)) {
1436 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_serviced", cgroup_blkio_base, cg->id);
1437 if(likely(stat(filename, &buf) != -1)) {
1438 cg->io_serviced.filename = strdupz(filename);
1439 cg->io_serviced.enabled = cgroup_enable_blkio_ops;
1440 debug(D_CGROUP, "io_serviced filename for cgroup '%s': '%s'", cg->id, cg->io_serviced.filename);
1443 debug(D_CGROUP, "io_serviced file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1446 if(unlikely(cgroup_enable_blkio_throttle_io && !cg->throttle_io_service_bytes.filename)) {
1447 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.throttle.io_service_bytes", cgroup_blkio_base, cg->id);
1448 if(likely(stat(filename, &buf) != -1)) {
1449 cg->throttle_io_service_bytes.filename = strdupz(filename);
1450 cg->throttle_io_service_bytes.enabled = cgroup_enable_blkio_throttle_io;
1451 debug(D_CGROUP, "throttle_io_service_bytes filename for cgroup '%s': '%s'", cg->id, cg->throttle_io_service_bytes.filename);
1454 debug(D_CGROUP, "throttle_io_service_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1457 if(unlikely(cgroup_enable_blkio_throttle_ops && !cg->throttle_io_serviced.filename)) {
1458 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.throttle.io_serviced", cgroup_blkio_base, cg->id);
1459 if(likely(stat(filename, &buf) != -1)) {
1460 cg->throttle_io_serviced.filename = strdupz(filename);
1461 cg->throttle_io_serviced.enabled = cgroup_enable_blkio_throttle_ops;
1462 debug(D_CGROUP, "throttle_io_serviced filename for cgroup '%s': '%s'", cg->id, cg->throttle_io_serviced.filename);
1465 debug(D_CGROUP, "throttle_io_serviced file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1468 if(unlikely(cgroup_enable_blkio_merged_ops && !cg->io_merged.filename)) {
1469 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_merged", cgroup_blkio_base, cg->id);
1470 if(likely(stat(filename, &buf) != -1)) {
1471 cg->io_merged.filename = strdupz(filename);
1472 cg->io_merged.enabled = cgroup_enable_blkio_merged_ops;
1473 debug(D_CGROUP, "io_merged filename for cgroup '%s': '%s'", cg->id, cg->io_merged.filename);
1476 debug(D_CGROUP, "io_merged file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1479 if(unlikely(cgroup_enable_blkio_queued_ops && !cg->io_queued.filename)) {
1480 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_queued", cgroup_blkio_base, cg->id);
1481 if(likely(stat(filename, &buf) != -1)) {
1482 cg->io_queued.filename = strdupz(filename);
1483 cg->io_queued.enabled = cgroup_enable_blkio_queued_ops;
1484 debug(D_CGROUP, "io_queued filename for cgroup '%s': '%s'", cg->id, cg->io_queued.filename);
1487 debug(D_CGROUP, "io_queued file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1491 debug(D_CGROUP, "done searching for cgroups");
1495 // ----------------------------------------------------------------------------
1498 #define CHART_TITLE_MAX 300
1500 void update_services_charts(int update_every,
1503 int do_mem_detailed,
1509 int do_throttle_ops,
1515 *st_mem_usage = NULL,
1516 *st_mem_failcnt = NULL,
1517 *st_swap_usage = NULL,
1519 *st_mem_detailed_cache = NULL,
1520 *st_mem_detailed_rss = NULL,
1521 *st_mem_detailed_mapped = NULL,
1522 *st_mem_detailed_writeback = NULL,
1523 *st_mem_detailed_pgfault = NULL,
1524 *st_mem_detailed_pgmajfault = NULL,
1525 *st_mem_detailed_pgpgin = NULL,
1526 *st_mem_detailed_pgpgout = NULL,
1529 *st_io_serviced_read = NULL,
1530 *st_throttle_io_read = NULL,
1531 *st_throttle_ops_read = NULL,
1532 *st_queued_ops_read = NULL,
1533 *st_merged_ops_read = NULL,
1535 *st_io_write = NULL,
1536 *st_io_serviced_write = NULL,
1537 *st_throttle_io_write = NULL,
1538 *st_throttle_ops_write = NULL,
1539 *st_queued_ops_write = NULL,
1540 *st_merged_ops_write = NULL;
1542 // create the charts
1544 if(likely(do_cpu)) {
1545 if(unlikely(!st_cpu)) {
1546 char title[CHART_TITLE_MAX + 1];
1548 st_cpu = rrdset_find_bytype("services", "cpu");
1549 if(likely(!st_cpu)) {
1550 snprintfz(title, CHART_TITLE_MAX, "Systemd Services CPU utilization (%d%% = %d core%s)", (processors * 100), processors, (processors > 1) ? "s" : "");
1551 st_cpu = rrdset_create("services", "cpu", NULL, "cpu", "services.cpu", title, "%", CHART_PRIORITY_SYSTEMD_SERVICES, update_every, RRDSET_TYPE_STACKED);
1555 rrdset_next(st_cpu);
1558 if(likely(do_mem_usage)) {
1559 if(unlikely(!st_mem_usage)) {
1560 st_mem_usage = rrdset_find_bytype("services", "mem_usage");
1561 if(likely(!st_mem_usage))
1562 st_mem_usage = rrdset_create("services", "mem_usage", NULL, "mem", "services.mem_usage", "Systemd Services Used Memory", "MB", CHART_PRIORITY_SYSTEMD_SERVICES + 10, update_every, RRDSET_TYPE_STACKED);
1565 rrdset_next(st_mem_usage);
1568 if(likely(do_mem_detailed)) {
1569 if(unlikely(!st_mem_detailed_rss)) {
1570 st_mem_detailed_rss = rrdset_find_bytype("services", "mem_rss");
1571 if(likely(!st_mem_detailed_rss))
1572 st_mem_detailed_rss = rrdset_create("services", "mem_rss", NULL, "mem", "services.mem_rss", "Systemd Services RSS Memory", "MB", CHART_PRIORITY_SYSTEMD_SERVICES + 20, update_every, RRDSET_TYPE_STACKED);
1575 rrdset_next(st_mem_detailed_rss);
1577 if(unlikely(!st_mem_detailed_mapped)) {
1578 st_mem_detailed_mapped = rrdset_find_bytype("services", "mem_mapped");
1579 if(likely(!st_mem_detailed_mapped))
1580 st_mem_detailed_mapped = rrdset_create("services", "mem_mapped", NULL, "mem", "services.mem_mapped", "Systemd Services Mapped Memory", "MB", CHART_PRIORITY_SYSTEMD_SERVICES + 30, update_every, RRDSET_TYPE_STACKED);
1583 rrdset_next(st_mem_detailed_mapped);
1585 if(unlikely(!st_mem_detailed_cache)) {
1586 st_mem_detailed_cache = rrdset_find_bytype("services", "mem_cache");
1587 if(likely(!st_mem_detailed_cache))
1588 st_mem_detailed_cache = rrdset_create("services", "mem_cache", NULL, "mem", "services.mem_cache", "Systemd Services Cache Memory", "MB", CHART_PRIORITY_SYSTEMD_SERVICES + 40, update_every, RRDSET_TYPE_STACKED);
1591 rrdset_next(st_mem_detailed_cache);
1593 if(unlikely(!st_mem_detailed_writeback)) {
1594 st_mem_detailed_writeback = rrdset_find_bytype("services", "mem_writeback");
1595 if(likely(!st_mem_detailed_writeback))
1596 st_mem_detailed_writeback = rrdset_create("services", "mem_writeback", NULL, "mem", "services.mem_writeback", "Systemd Services Writeback Memory", "MB", CHART_PRIORITY_SYSTEMD_SERVICES + 50, update_every, RRDSET_TYPE_STACKED);
1599 rrdset_next(st_mem_detailed_writeback);
1601 if(unlikely(!st_mem_detailed_pgfault)) {
1602 st_mem_detailed_pgfault = rrdset_find_bytype("services", "mem_pgfault");
1603 if(likely(!st_mem_detailed_pgfault))
1604 st_mem_detailed_pgfault = rrdset_create("services", "mem_pgfault", NULL, "mem", "services.mem_pgfault", "Systemd Services Memory Minor Page Faults", "MB/s", CHART_PRIORITY_SYSTEMD_SERVICES + 60, update_every, RRDSET_TYPE_STACKED);
1607 rrdset_next(st_mem_detailed_pgfault);
1609 if(unlikely(!st_mem_detailed_pgmajfault)) {
1610 st_mem_detailed_pgmajfault = rrdset_find_bytype("services", "mem_pgmajfault");
1611 if(likely(!st_mem_detailed_pgmajfault))
1612 st_mem_detailed_pgmajfault = rrdset_create("services", "mem_pgmajfault", NULL, "mem", "services.mem_pgmajfault", "Systemd Services Memory Major Page Faults", "MB/s", CHART_PRIORITY_SYSTEMD_SERVICES + 70, update_every, RRDSET_TYPE_STACKED);
1615 rrdset_next(st_mem_detailed_pgmajfault);
1617 if(unlikely(!st_mem_detailed_pgpgin)) {
1618 st_mem_detailed_pgpgin = rrdset_find_bytype("services", "mem_pgpgin");
1619 if(likely(!st_mem_detailed_pgpgin))
1620 st_mem_detailed_pgpgin = rrdset_create("services", "mem_pgpgin", NULL, "mem", "services.mem_pgpgin", "Systemd Services Memory Charging Activity", "MB/s", CHART_PRIORITY_SYSTEMD_SERVICES + 80, update_every, RRDSET_TYPE_STACKED);
1623 rrdset_next(st_mem_detailed_pgpgin);
1625 if(unlikely(!st_mem_detailed_pgpgout)) {
1626 st_mem_detailed_pgpgout = rrdset_find_bytype("services", "mem_pgpgout");
1627 if(likely(!st_mem_detailed_pgpgout))
1628 st_mem_detailed_pgpgout = rrdset_create("services", "mem_pgpgout", NULL, "mem", "services.mem_pgpgout", "Systemd Services Memory Uncharging Activity", "MB/s", CHART_PRIORITY_SYSTEMD_SERVICES + 90, update_every, RRDSET_TYPE_STACKED);
1631 rrdset_next(st_mem_detailed_pgpgout);
1634 if(likely(do_mem_failcnt)) {
1635 if(unlikely(!st_mem_failcnt)) {
1636 st_mem_failcnt = rrdset_find_bytype("services", "mem_failcnt");
1637 if(likely(!st_mem_failcnt))
1638 st_mem_failcnt = rrdset_create("services", "mem_failcnt", NULL, "mem", "services.mem_failcnt", "Systemd Services Memory Limit Failures", "MB", CHART_PRIORITY_SYSTEMD_SERVICES + 110, update_every, RRDSET_TYPE_STACKED);
1641 rrdset_next(st_mem_failcnt);
1644 if(likely(do_swap_usage)) {
1645 if(unlikely(!st_swap_usage)) {
1646 st_swap_usage = rrdset_find_bytype("services", "swap_usage");
1647 if(likely(!st_swap_usage))
1648 st_swap_usage = rrdset_create("services", "swap_usage", NULL, "swap", "services.swap_usage", "Systemd Services Swap Memory Used", "MB", CHART_PRIORITY_SYSTEMD_SERVICES + 100, update_every, RRDSET_TYPE_STACKED);
1651 rrdset_next(st_swap_usage);
1655 if(unlikely(!st_io_read)) {
1656 st_io_read = rrdset_find_bytype("services", "io_read");
1657 if(likely(!st_io_read))
1658 st_io_read = rrdset_create("services", "io_read", NULL, "disk", "services.io_read", "Systemd Services Disk Read Bandwidth", "KB/s", CHART_PRIORITY_SYSTEMD_SERVICES + 120, update_every, RRDSET_TYPE_STACKED);
1661 rrdset_next(st_io_read);
1663 if(unlikely(!st_io_write)) {
1664 st_io_write = rrdset_find_bytype("services", "io_write");
1665 if(likely(!st_io_write))
1666 st_io_write = rrdset_create("services", "io_write", NULL, "disk", "services.io_write", "Systemd Services Disk Write Bandwidth", "KB/s", CHART_PRIORITY_SYSTEMD_SERVICES + 130, update_every, RRDSET_TYPE_STACKED);
1669 rrdset_next(st_io_write);
1672 if(likely(do_io_ops)) {
1673 if(unlikely(!st_io_serviced_read)) {
1674 st_io_serviced_read = rrdset_find_bytype("services", "io_ops_read");
1675 if(likely(!st_io_serviced_read))
1676 st_io_serviced_read = rrdset_create("services", "io_ops_read", NULL, "disk", "services.io_ops_read", "Systemd Services Disk Read Operations", "operations/s", CHART_PRIORITY_SYSTEMD_SERVICES + 140, update_every, RRDSET_TYPE_STACKED);
1679 rrdset_next(st_io_serviced_read);
1681 if(unlikely(!st_io_serviced_write)) {
1682 st_io_serviced_write = rrdset_find_bytype("services", "io_ops_write");
1683 if(likely(!st_io_serviced_write))
1684 st_io_serviced_write = rrdset_create("services", "io_ops_write", NULL, "disk", "services.io_ops_write", "Systemd Services Disk Write Operations", "operations/s", CHART_PRIORITY_SYSTEMD_SERVICES + 150, update_every, RRDSET_TYPE_STACKED);
1687 rrdset_next(st_io_serviced_write);
1690 if(likely(do_throttle_io)) {
1691 if(unlikely(!st_throttle_io_read)) {
1692 st_throttle_io_read = rrdset_find_bytype("services", "throttle_io_read");
1693 if(likely(!st_throttle_io_read))
1694 st_throttle_io_read = rrdset_create("services", "throttle_io_read", NULL, "disk", "services.throttle_io_read", "Systemd Services Throttle Disk Read Bandwidth", "KB/s", CHART_PRIORITY_SYSTEMD_SERVICES + 160, update_every, RRDSET_TYPE_STACKED);
1697 rrdset_next(st_throttle_io_read);
1699 if(unlikely(!st_throttle_io_write)) {
1700 st_throttle_io_write = rrdset_find_bytype("services", "throttle_io_write");
1701 if(likely(!st_throttle_io_write))
1702 st_throttle_io_write = rrdset_create("services", "throttle_io_write", NULL, "disk", "services.throttle_io_write", "Systemd Services Throttle Disk Write Bandwidth", "KB/s", CHART_PRIORITY_SYSTEMD_SERVICES + 170, update_every, RRDSET_TYPE_STACKED);
1705 rrdset_next(st_throttle_io_write);
1708 if(likely(do_throttle_ops)) {
1709 if(unlikely(!st_throttle_ops_read)) {
1710 st_throttle_ops_read = rrdset_find_bytype("services", "throttle_io_ops_read");
1711 if(likely(!st_throttle_ops_read))
1712 st_throttle_ops_read = rrdset_create("services", "throttle_io_ops_read", NULL, "disk", "services.throttle_io_ops_read", "Systemd Services Throttle Disk Read Operations", "operations/s", CHART_PRIORITY_SYSTEMD_SERVICES + 180, update_every, RRDSET_TYPE_STACKED);
1715 rrdset_next(st_throttle_ops_read);
1717 if(unlikely(!st_throttle_ops_write)) {
1718 st_throttle_ops_write = rrdset_find_bytype("services", "throttle_io_ops_write");
1719 if(likely(!st_throttle_ops_write))
1720 st_throttle_ops_write = rrdset_create("services", "throttle_io_ops_write", NULL, "disk", "services.throttle_io_ops_write", "Systemd Services Throttle Disk Write Operations", "operations/s", CHART_PRIORITY_SYSTEMD_SERVICES + 190, update_every, RRDSET_TYPE_STACKED);
1723 rrdset_next(st_throttle_ops_write);
1726 if(likely(do_queued_ops)) {
1727 if(unlikely(!st_queued_ops_read)) {
1728 st_queued_ops_read = rrdset_find_bytype("services", "queued_io_ops_read");
1729 if(likely(!st_queued_ops_read))
1730 st_queued_ops_read = rrdset_create("services", "queued_io_ops_read", NULL, "disk", "services.queued_io_ops_read", "Systemd Services Queued Disk Read Operations", "operations/s", CHART_PRIORITY_SYSTEMD_SERVICES + 200, update_every, RRDSET_TYPE_STACKED);
1733 rrdset_next(st_queued_ops_read);
1735 if(unlikely(!st_queued_ops_write)) {
1736 st_queued_ops_write = rrdset_find_bytype("services", "queued_io_ops_write");
1737 if(likely(!st_queued_ops_write))
1738 st_queued_ops_write = rrdset_create("services", "queued_io_ops_write", NULL, "disk", "services.queued_io_ops_write", "Systemd Services Queued Disk Write Operations", "operations/s", CHART_PRIORITY_SYSTEMD_SERVICES + 210, update_every, RRDSET_TYPE_STACKED);
1741 rrdset_next(st_queued_ops_write);
1744 if(likely(do_merged_ops)) {
1745 if(unlikely(!st_merged_ops_read)) {
1746 st_merged_ops_read = rrdset_find_bytype("services", "merged_io_ops_read");
1747 if(likely(!st_merged_ops_read))
1748 st_merged_ops_read = rrdset_create("services", "merged_io_ops_read", NULL, "disk", "services.merged_io_ops_read", "Systemd Services Merged Disk Read Operations", "operations/s", CHART_PRIORITY_SYSTEMD_SERVICES + 220, update_every, RRDSET_TYPE_STACKED);
1751 rrdset_next(st_merged_ops_read);
1753 if(unlikely(!st_merged_ops_write)) {
1754 st_merged_ops_write = rrdset_find_bytype("services", "merged_io_ops_write");
1755 if(likely(!st_merged_ops_write))
1756 st_merged_ops_write = rrdset_create("services", "merged_io_ops_write", NULL, "disk", "services.merged_io_ops_write", "Systemd Services Merged Disk Write Operations", "operations/s", CHART_PRIORITY_SYSTEMD_SERVICES + 230, update_every, RRDSET_TYPE_STACKED);
1759 rrdset_next(st_merged_ops_write);
1762 // update the values
1764 for(cg = cgroup_root; cg ; cg = cg->next) {
1765 if(unlikely(!cg->available || !cg->enabled || !(cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE)))
1768 if(likely(do_cpu && cg->cpuacct_stat.updated)) {
1769 if(unlikely(!cg->rd_cpu))
1770 cg->rd_cpu = rrddim_add(st_cpu, cg->chart_id, cg->chart_title, 100, hz, RRDDIM_INCREMENTAL);
1772 rrddim_set_by_pointer(st_cpu, cg->rd_cpu, cg->cpuacct_stat.user + cg->cpuacct_stat.system);
1775 if(likely(do_mem_usage && cg->memory.updated_usage_in_bytes)) {
1776 if(unlikely(!cg->rd_mem_usage))
1777 cg->rd_mem_usage = rrddim_add(st_mem_usage, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1779 rrddim_set_by_pointer(st_mem_usage, cg->rd_mem_usage, cg->memory.usage_in_bytes);
1782 if(likely(do_mem_detailed && cg->memory.updated_detailed)) {
1783 if(unlikely(!cg->rd_mem_detailed_rss))
1784 cg->rd_mem_detailed_rss = rrddim_add(st_mem_detailed_rss, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1785 rrddim_set_by_pointer(st_mem_detailed_rss, cg->rd_mem_detailed_rss, cg->memory.rss + cg->memory.rss_huge);
1787 if(unlikely(!cg->rd_mem_detailed_mapped))
1788 cg->rd_mem_detailed_mapped = rrddim_add(st_mem_detailed_mapped, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1789 rrddim_set_by_pointer(st_mem_detailed_mapped, cg->rd_mem_detailed_mapped, cg->memory.mapped_file);
1791 if(unlikely(!cg->rd_mem_detailed_cache))
1792 cg->rd_mem_detailed_cache = rrddim_add(st_mem_detailed_cache, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1793 rrddim_set_by_pointer(st_mem_detailed_cache, cg->rd_mem_detailed_cache, cg->memory.cache);
1795 if(unlikely(!cg->rd_mem_detailed_writeback))
1796 cg->rd_mem_detailed_writeback = rrddim_add(st_mem_detailed_writeback, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1797 rrddim_set_by_pointer(st_mem_detailed_writeback, cg->rd_mem_detailed_writeback, cg->memory.writeback);
1799 if(unlikely(!cg->rd_mem_detailed_pgfault))
1800 cg->rd_mem_detailed_pgfault = rrddim_add(st_mem_detailed_pgfault, cg->chart_id, cg->chart_title, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1801 rrddim_set_by_pointer(st_mem_detailed_pgfault, cg->rd_mem_detailed_pgfault, cg->memory.pgfault);
1803 if(unlikely(!cg->rd_mem_detailed_pgmajfault))
1804 cg->rd_mem_detailed_pgmajfault = rrddim_add(st_mem_detailed_pgmajfault, cg->chart_id, cg->chart_title, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1805 rrddim_set_by_pointer(st_mem_detailed_pgmajfault, cg->rd_mem_detailed_pgmajfault, cg->memory.pgmajfault);
1807 if(unlikely(!cg->rd_mem_detailed_pgpgin))
1808 cg->rd_mem_detailed_pgpgin = rrddim_add(st_mem_detailed_pgpgin, cg->chart_id, cg->chart_title, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1809 rrddim_set_by_pointer(st_mem_detailed_pgpgin, cg->rd_mem_detailed_pgpgin, cg->memory.pgpgin);
1811 if(unlikely(!cg->rd_mem_detailed_pgpgout))
1812 cg->rd_mem_detailed_pgpgout = rrddim_add(st_mem_detailed_pgpgout, cg->chart_id, cg->chart_title, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1813 rrddim_set_by_pointer(st_mem_detailed_pgpgout, cg->rd_mem_detailed_pgpgout, cg->memory.pgpgout);
1816 if(likely(do_mem_failcnt && cg->memory.updated_failcnt)) {
1817 if(unlikely(!cg->rd_mem_failcnt))
1818 cg->rd_mem_failcnt = rrddim_add(st_mem_failcnt, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1820 rrddim_set_by_pointer(st_mem_failcnt, cg->rd_mem_failcnt, cg->memory.failcnt);
1823 if(likely(do_swap_usage && cg->memory.updated_msw_usage_in_bytes)) {
1824 if(unlikely(!cg->rd_swap_usage))
1825 cg->rd_swap_usage = rrddim_add(st_swap_usage, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1827 rrddim_set_by_pointer(st_swap_usage, cg->rd_swap_usage, cg->memory.msw_usage_in_bytes);
1830 if(likely(do_io && cg->io_service_bytes.updated)) {
1831 if(unlikely(!cg->rd_io_service_bytes_read))
1832 cg->rd_io_service_bytes_read = rrddim_add(st_io_read, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1834 rrddim_set_by_pointer(st_io_read, cg->rd_io_service_bytes_read, cg->io_service_bytes.Read);
1836 if(unlikely(!cg->rd_io_service_bytes_write))
1837 cg->rd_io_service_bytes_write = rrddim_add(st_io_write, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1839 rrddim_set_by_pointer(st_io_write, cg->rd_io_service_bytes_write, cg->io_service_bytes.Write);
1842 if(likely(do_io_ops && cg->io_serviced.updated)) {
1843 if(unlikely(!cg->rd_io_serviced_read))
1844 cg->rd_io_serviced_read = rrddim_add(st_io_serviced_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1846 rrddim_set_by_pointer(st_io_serviced_read, cg->rd_io_serviced_read, cg->io_serviced.Read);
1848 if(unlikely(!cg->rd_io_serviced_write))
1849 cg->rd_io_serviced_write = rrddim_add(st_io_serviced_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1851 rrddim_set_by_pointer(st_io_serviced_write, cg->rd_io_serviced_write, cg->io_serviced.Write);
1854 if(likely(do_throttle_io && cg->throttle_io_service_bytes.updated)) {
1855 if(unlikely(!cg->rd_throttle_io_read))
1856 cg->rd_throttle_io_read = rrddim_add(st_throttle_io_read, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1858 rrddim_set_by_pointer(st_throttle_io_read, cg->rd_throttle_io_read, cg->throttle_io_service_bytes.Read);
1860 if(unlikely(!cg->rd_throttle_io_write))
1861 cg->rd_throttle_io_write = rrddim_add(st_throttle_io_write, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1863 rrddim_set_by_pointer(st_throttle_io_write, cg->rd_throttle_io_write, cg->throttle_io_service_bytes.Write);
1866 if(likely(do_throttle_ops && cg->throttle_io_serviced.updated)) {
1867 if(unlikely(!cg->rd_throttle_io_serviced_read))
1868 cg->rd_throttle_io_serviced_read = rrddim_add(st_throttle_ops_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1870 rrddim_set_by_pointer(st_throttle_ops_read, cg->rd_throttle_io_serviced_read, cg->throttle_io_serviced.Read);
1872 if(unlikely(!cg->rd_throttle_io_serviced_write))
1873 cg->rd_throttle_io_serviced_write = rrddim_add(st_throttle_ops_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1875 rrddim_set_by_pointer(st_throttle_ops_write, cg->rd_throttle_io_serviced_write, cg->throttle_io_serviced.Write);
1878 if(likely(do_queued_ops && cg->io_queued.updated)) {
1879 if(unlikely(!cg->rd_io_queued_read))
1880 cg->rd_io_queued_read = rrddim_add(st_queued_ops_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1882 rrddim_set_by_pointer(st_queued_ops_read, cg->rd_io_queued_read, cg->io_queued.Read);
1884 if(unlikely(!cg->rd_io_queued_write))
1885 cg->rd_io_queued_write = rrddim_add(st_queued_ops_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1887 rrddim_set_by_pointer(st_queued_ops_write, cg->rd_io_queued_write, cg->io_queued.Write);
1890 if(likely(do_merged_ops && cg->io_merged.updated)) {
1891 if(unlikely(!cg->rd_io_merged_read))
1892 cg->rd_io_merged_read = rrddim_add(st_merged_ops_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1894 rrddim_set_by_pointer(st_merged_ops_read, cg->rd_io_merged_read, cg->io_merged.Read);
1896 if(unlikely(!cg->rd_io_merged_write))
1897 cg->rd_io_merged_write = rrddim_add(st_merged_ops_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1899 rrddim_set_by_pointer(st_merged_ops_write, cg->rd_io_merged_write, cg->io_merged.Write);
1903 // complete the iteration
1905 rrdset_done(st_cpu);
1907 if(likely(do_mem_usage))
1908 rrdset_done(st_mem_usage);
1910 if(unlikely(do_mem_detailed)) {
1911 rrdset_done(st_mem_detailed_cache);
1912 rrdset_done(st_mem_detailed_rss);
1913 rrdset_done(st_mem_detailed_mapped);
1914 rrdset_done(st_mem_detailed_writeback);
1915 rrdset_done(st_mem_detailed_pgfault);
1916 rrdset_done(st_mem_detailed_pgmajfault);
1917 rrdset_done(st_mem_detailed_pgpgin);
1918 rrdset_done(st_mem_detailed_pgpgout);
1921 if(likely(do_mem_failcnt))
1922 rrdset_done(st_mem_failcnt);
1924 if(likely(do_swap_usage))
1925 rrdset_done(st_swap_usage);
1928 rrdset_done(st_io_read);
1929 rrdset_done(st_io_write);
1932 if(likely(do_io_ops)) {
1933 rrdset_done(st_io_serviced_read);
1934 rrdset_done(st_io_serviced_write);
1937 if(likely(do_throttle_io)) {
1938 rrdset_done(st_throttle_io_read);
1939 rrdset_done(st_throttle_io_write);
1942 if(likely(do_throttle_ops)) {
1943 rrdset_done(st_throttle_ops_read);
1944 rrdset_done(st_throttle_ops_write);
1947 if(likely(do_queued_ops)) {
1948 rrdset_done(st_queued_ops_read);
1949 rrdset_done(st_queued_ops_write);
1952 if(likely(do_merged_ops)) {
1953 rrdset_done(st_merged_ops_read);
1954 rrdset_done(st_merged_ops_write);
1958 static inline char *cgroup_chart_type(char *buffer, const char *id, size_t len) {
1959 if(buffer[0]) return buffer;
1961 if(id[0] == '\0' || (id[0] == '/' && id[1] == '\0'))
1962 strncpy(buffer, "cgroup_root", len);
1964 snprintfz(buffer, len, "cgroup_%s", id);
1966 netdata_fix_chart_id(buffer);
1970 void update_cgroup_charts(int update_every) {
1971 debug(D_CGROUP, "updating cgroups charts");
1973 char type[RRD_ID_LENGTH_MAX + 1];
1974 char title[CHART_TITLE_MAX + 1];
1976 int services_do_cpu = 0,
1977 services_do_mem_usage = 0,
1978 services_do_mem_detailed = 0,
1979 services_do_mem_failcnt = 0,
1980 services_do_swap_usage = 0,
1982 services_do_io_ops = 0,
1983 services_do_throttle_io = 0,
1984 services_do_throttle_ops = 0,
1985 services_do_queued_ops = 0,
1986 services_do_merged_ops = 0;
1989 for(cg = cgroup_root; cg ; cg = cg->next) {
1990 if(unlikely(!cg->available || !cg->enabled))
1993 if(likely(cgroup_enable_systemd_services && cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE)) {
1994 if(cg->cpuacct_stat.updated && cg->cpuacct_stat.enabled == CONFIG_ONDEMAND_YES) services_do_cpu++;
1996 if(cg->memory.updated_detailed && cg->memory.enabled_detailed) services_do_mem_detailed++;
1997 if(cg->memory.updated_usage_in_bytes && cg->memory.enabled_usage_in_bytes == CONFIG_ONDEMAND_YES) services_do_mem_usage++;
1998 if(cg->memory.updated_failcnt && cg->memory.enabled_failcnt == CONFIG_ONDEMAND_YES) services_do_mem_failcnt++;
1999 if(cg->memory.updated_msw_usage_in_bytes && cg->memory.enabled_msw_usage_in_bytes == CONFIG_ONDEMAND_YES) services_do_swap_usage++;
2001 if(cg->io_service_bytes.updated && cg->io_service_bytes.enabled == CONFIG_ONDEMAND_YES) services_do_io++;
2002 if(cg->io_serviced.updated && cg->io_serviced.enabled == CONFIG_ONDEMAND_YES) services_do_io_ops++;
2003 if(cg->throttle_io_service_bytes.updated && cg->throttle_io_service_bytes.enabled == CONFIG_ONDEMAND_YES) services_do_throttle_io++;
2004 if(cg->throttle_io_serviced.updated && cg->throttle_io_serviced.enabled == CONFIG_ONDEMAND_YES) services_do_throttle_ops++;
2005 if(cg->io_queued.updated && cg->io_queued.enabled == CONFIG_ONDEMAND_YES) services_do_queued_ops++;
2006 if(cg->io_merged.updated && cg->io_merged.enabled == CONFIG_ONDEMAND_YES) services_do_merged_ops++;
2012 if(likely(cg->cpuacct_stat.updated && cg->cpuacct_stat.enabled == CONFIG_ONDEMAND_YES)) {
2013 if(unlikely(!cg->st_cpu)) {
2014 cg->st_cpu = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "cpu");
2015 if(likely(!cg->st_cpu)) {
2016 snprintfz(title, CHART_TITLE_MAX, "CPU Usage (%d%% = %d core%s) for cgroup %s", (processors * 100), processors, (processors > 1) ? "s" : "", cg->chart_title);
2017 cg->st_cpu = rrdset_create(type, "cpu", NULL, "cpu", "cgroup.cpu", title, "%", CHART_PRIORITY_CONTAINERS, update_every, RRDSET_TYPE_STACKED);
2019 rrddim_add(cg->st_cpu, "user", NULL, 100, hz, RRDDIM_INCREMENTAL);
2020 rrddim_add(cg->st_cpu, "system", NULL, 100, hz, RRDDIM_INCREMENTAL);
2023 rrdset_next(cg->st_cpu);
2025 rrddim_set(cg->st_cpu, "user", cg->cpuacct_stat.user);
2026 rrddim_set(cg->st_cpu, "system", cg->cpuacct_stat.system);
2027 rrdset_done(cg->st_cpu);
2030 if(likely(cg->cpuacct_usage.updated && cg->cpuacct_usage.enabled == CONFIG_ONDEMAND_YES)) {
2031 char id[RRD_ID_LENGTH_MAX + 1];
2034 if(unlikely(!cg->st_cpu_per_core)) {
2035 cg->st_cpu_per_core = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "cpu_per_core");
2036 if(likely(!cg->st_cpu_per_core)) {
2037 snprintfz(title, CHART_TITLE_MAX, "CPU Usage (%d%% = %d core%s) Per Core for cgroup %s", (processors * 100), processors, (processors > 1) ? "s" : "", cg->chart_title);
2038 cg->st_cpu_per_core = rrdset_create(type, "cpu_per_core", NULL, "cpu", "cgroup.cpu_per_core", title, "%", CHART_PRIORITY_CONTAINERS + 100, update_every, RRDSET_TYPE_STACKED);
2040 for(i = 0; i < cg->cpuacct_usage.cpus; i++) {
2041 snprintfz(id, CHART_TITLE_MAX, "cpu%u", i);
2042 rrddim_add(cg->st_cpu_per_core, id, NULL, 100, 1000000000, RRDDIM_INCREMENTAL);
2046 rrdset_next(cg->st_cpu_per_core);
2048 for(i = 0; i < cg->cpuacct_usage.cpus ;i++) {
2049 snprintfz(id, CHART_TITLE_MAX, "cpu%u", i);
2050 rrddim_set(cg->st_cpu_per_core, id, cg->cpuacct_usage.cpu_percpu[i]);
2052 rrdset_done(cg->st_cpu_per_core);
2055 if(likely(cg->memory.updated_detailed && cg->memory.enabled_detailed == CONFIG_ONDEMAND_YES)) {
2056 if(unlikely(!cg->st_mem)) {
2057 cg->st_mem = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem");
2058 if(likely(!cg->st_mem)) {
2059 snprintfz(title, CHART_TITLE_MAX, "Memory Usage for cgroup %s", cg->chart_title);
2060 cg->st_mem = rrdset_create(type, "mem", NULL, "mem", "cgroup.mem", title, "MB", CHART_PRIORITY_CONTAINERS + 210, update_every, RRDSET_TYPE_STACKED);
2063 rrddim_add(cg->st_mem, "cache", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2064 rrddim_add(cg->st_mem, "rss", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2065 if(cg->memory.detailed_has_dirty_swap)
2066 rrddim_add(cg->st_mem, "swap", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2067 rrddim_add(cg->st_mem, "rss_huge", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2068 rrddim_add(cg->st_mem, "mapped_file", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2071 rrdset_next(cg->st_mem);
2073 rrddim_set(cg->st_mem, "cache", cg->memory.cache);
2074 rrddim_set(cg->st_mem, "rss", cg->memory.rss);
2075 if(cg->memory.detailed_has_dirty_swap)
2076 rrddim_set(cg->st_mem, "swap", cg->memory.swap);
2077 rrddim_set(cg->st_mem, "rss_huge", cg->memory.rss_huge);
2078 rrddim_set(cg->st_mem, "mapped_file", cg->memory.mapped_file);
2079 rrdset_done(cg->st_mem);
2081 if(unlikely(!cg->st_writeback)) {
2082 cg->st_writeback = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "writeback");
2083 if(likely(!cg->st_writeback)) {
2084 snprintfz(title, CHART_TITLE_MAX, "Writeback Memory for cgroup %s", cg->chart_title);
2085 cg->st_writeback = rrdset_create(type, "writeback", NULL, "mem", "cgroup.writeback", title, "MB", CHART_PRIORITY_CONTAINERS + 300, update_every, RRDSET_TYPE_AREA);
2088 if(cg->memory.detailed_has_dirty_swap)
2089 rrddim_add(cg->st_writeback, "dirty", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2090 rrddim_add(cg->st_writeback, "writeback", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2093 rrdset_next(cg->st_writeback);
2095 if(cg->memory.detailed_has_dirty_swap)
2096 rrddim_set(cg->st_writeback, "dirty", cg->memory.dirty);
2097 rrddim_set(cg->st_writeback, "writeback", cg->memory.writeback);
2098 rrdset_done(cg->st_writeback);
2100 if(unlikely(!cg->st_mem_activity)) {
2101 cg->st_mem_activity = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_activity");
2102 if(likely(!cg->st_mem_activity)) {
2103 snprintfz(title, CHART_TITLE_MAX, "Memory Activity for cgroup %s", cg->chart_title);
2104 cg->st_mem_activity = rrdset_create(type, "mem_activity", NULL, "mem", "cgroup.mem_activity", title, "MB/s", CHART_PRIORITY_CONTAINERS + 400, update_every, RRDSET_TYPE_LINE);
2106 rrddim_add(cg->st_mem_activity, "pgpgin", "in", system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
2107 rrddim_add(cg->st_mem_activity, "pgpgout", "out", -system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
2110 rrdset_next(cg->st_mem_activity);
2112 rrddim_set(cg->st_mem_activity, "pgpgin", cg->memory.pgpgin);
2113 rrddim_set(cg->st_mem_activity, "pgpgout", cg->memory.pgpgout);
2114 rrdset_done(cg->st_mem_activity);
2116 if(unlikely(!cg->st_pgfaults)) {
2117 cg->st_pgfaults = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "pgfaults");
2118 if(likely(!cg->st_pgfaults)) {
2119 snprintfz(title, CHART_TITLE_MAX, "Memory Page Faults for cgroup %s", cg->chart_title);
2120 cg->st_pgfaults = rrdset_create(type, "pgfaults", NULL, "mem", "cgroup.pgfaults", title, "MB/s", CHART_PRIORITY_CONTAINERS + 500, update_every, RRDSET_TYPE_LINE);
2122 rrddim_add(cg->st_pgfaults, "pgfault", NULL, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
2123 rrddim_add(cg->st_pgfaults, "pgmajfault", "swap", -system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
2126 rrdset_next(cg->st_pgfaults);
2128 rrddim_set(cg->st_pgfaults, "pgfault", cg->memory.pgfault);
2129 rrddim_set(cg->st_pgfaults, "pgmajfault", cg->memory.pgmajfault);
2130 rrdset_done(cg->st_pgfaults);
2133 if(likely(cg->memory.updated_usage_in_bytes && cg->memory.enabled_usage_in_bytes == CONFIG_ONDEMAND_YES)) {
2134 if(unlikely(!cg->st_mem_usage)) {
2135 cg->st_mem_usage = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_usage");
2136 if(likely(!cg->st_mem_usage)) {
2137 snprintfz(title, CHART_TITLE_MAX, "Total Memory for cgroup %s", cg->chart_title);
2138 cg->st_mem_usage = rrdset_create(type, "mem_usage", NULL, "mem", "cgroup.mem_usage", title, "MB", CHART_PRIORITY_CONTAINERS + 200, update_every, RRDSET_TYPE_STACKED);
2140 rrddim_add(cg->st_mem_usage, "ram", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2141 rrddim_add(cg->st_mem_usage, "swap", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2144 rrdset_next(cg->st_mem_usage);
2146 rrddim_set(cg->st_mem_usage, "ram", cg->memory.usage_in_bytes);
2147 rrddim_set(cg->st_mem_usage, "swap", (cg->memory.msw_usage_in_bytes > cg->memory.usage_in_bytes)?cg->memory.msw_usage_in_bytes - cg->memory.usage_in_bytes:0);
2148 rrdset_done(cg->st_mem_usage);
2151 if(likely(cg->memory.updated_failcnt && cg->memory.enabled_failcnt == CONFIG_ONDEMAND_YES)) {
2152 if(unlikely(!cg->st_mem_failcnt)) {
2153 cg->st_mem_failcnt = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_failcnt");
2154 if(likely(!cg->st_mem_failcnt)) {
2155 snprintfz(title, CHART_TITLE_MAX, "Memory Limit Failures for cgroup %s", cg->chart_title);
2156 cg->st_mem_failcnt = rrdset_create(type, "mem_failcnt", NULL, "mem", "cgroup.mem_failcnt", title, "count", CHART_PRIORITY_CONTAINERS + 250, update_every, RRDSET_TYPE_LINE);
2158 rrddim_add(cg->st_mem_failcnt, "failures", NULL, 1, 1, RRDDIM_INCREMENTAL);
2161 rrdset_next(cg->st_mem_failcnt);
2163 rrddim_set(cg->st_mem_failcnt, "failures", cg->memory.failcnt);
2164 rrdset_done(cg->st_mem_failcnt);
2167 if(likely(cg->io_service_bytes.updated && cg->io_service_bytes.enabled == CONFIG_ONDEMAND_YES)) {
2168 if(unlikely(!cg->st_io)) {
2169 cg->st_io = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "io");
2170 if(likely(!cg->st_io)) {
2171 snprintfz(title, CHART_TITLE_MAX, "I/O Bandwidth (all disks) for cgroup %s", cg->chart_title);
2172 cg->st_io = rrdset_create(type, "io", NULL, "disk", "cgroup.io", title, "KB/s", CHART_PRIORITY_CONTAINERS + 1200, update_every, RRDSET_TYPE_AREA);
2174 rrddim_add(cg->st_io, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
2175 rrddim_add(cg->st_io, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
2178 rrdset_next(cg->st_io);
2180 rrddim_set(cg->st_io, "read", cg->io_service_bytes.Read);
2181 rrddim_set(cg->st_io, "write", cg->io_service_bytes.Write);
2182 rrdset_done(cg->st_io);
2185 if(likely(cg->io_serviced.updated && cg->io_serviced.enabled == CONFIG_ONDEMAND_YES)) {
2186 if(unlikely(!cg->st_serviced_ops)) {
2187 cg->st_serviced_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "serviced_ops");
2188 if(likely(!cg->st_serviced_ops)) {
2189 snprintfz(title, CHART_TITLE_MAX, "Serviced I/O Operations (all disks) for cgroup %s", cg->chart_title);
2190 cg->st_serviced_ops = rrdset_create(type, "serviced_ops", NULL, "disk", "cgroup.serviced_ops", title, "operations/s", CHART_PRIORITY_CONTAINERS + 1200, update_every, RRDSET_TYPE_LINE);
2192 rrddim_add(cg->st_serviced_ops, "read", NULL, 1, 1, RRDDIM_INCREMENTAL);
2193 rrddim_add(cg->st_serviced_ops, "write", NULL, -1, 1, RRDDIM_INCREMENTAL);
2196 rrdset_next(cg->st_serviced_ops);
2198 rrddim_set(cg->st_serviced_ops, "read", cg->io_serviced.Read);
2199 rrddim_set(cg->st_serviced_ops, "write", cg->io_serviced.Write);
2200 rrdset_done(cg->st_serviced_ops);
2203 if(likely(cg->throttle_io_service_bytes.updated && cg->throttle_io_service_bytes.enabled == CONFIG_ONDEMAND_YES)) {
2204 if(unlikely(!cg->st_throttle_io)) {
2205 cg->st_throttle_io = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "throttle_io");
2206 if(likely(!cg->st_throttle_io)) {
2207 snprintfz(title, CHART_TITLE_MAX, "Throttle I/O Bandwidth (all disks) for cgroup %s", cg->chart_title);
2208 cg->st_throttle_io = rrdset_create(type, "throttle_io", NULL, "disk", "cgroup.throttle_io", title, "KB/s", CHART_PRIORITY_CONTAINERS + 1200, update_every, RRDSET_TYPE_AREA);
2210 rrddim_add(cg->st_throttle_io, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
2211 rrddim_add(cg->st_throttle_io, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
2214 rrdset_next(cg->st_throttle_io);
2216 rrddim_set(cg->st_throttle_io, "read", cg->throttle_io_service_bytes.Read);
2217 rrddim_set(cg->st_throttle_io, "write", cg->throttle_io_service_bytes.Write);
2218 rrdset_done(cg->st_throttle_io);
2221 if(likely(cg->throttle_io_serviced.updated && cg->throttle_io_serviced.enabled == CONFIG_ONDEMAND_YES)) {
2222 if(unlikely(!cg->st_throttle_serviced_ops)) {
2223 cg->st_throttle_serviced_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "throttle_serviced_ops");
2224 if(likely(!cg->st_throttle_serviced_ops)) {
2225 snprintfz(title, CHART_TITLE_MAX, "Throttle Serviced I/O Operations (all disks) for cgroup %s", cg->chart_title);
2226 cg->st_throttle_serviced_ops = rrdset_create(type, "throttle_serviced_ops", NULL, "disk", "cgroup.throttle_serviced_ops", title, "operations/s", CHART_PRIORITY_CONTAINERS + 1200, update_every, RRDSET_TYPE_LINE);
2228 rrddim_add(cg->st_throttle_serviced_ops, "read", NULL, 1, 1, RRDDIM_INCREMENTAL);
2229 rrddim_add(cg->st_throttle_serviced_ops, "write", NULL, -1, 1, RRDDIM_INCREMENTAL);
2232 rrdset_next(cg->st_throttle_serviced_ops);
2234 rrddim_set(cg->st_throttle_serviced_ops, "read", cg->throttle_io_serviced.Read);
2235 rrddim_set(cg->st_throttle_serviced_ops, "write", cg->throttle_io_serviced.Write);
2236 rrdset_done(cg->st_throttle_serviced_ops);
2239 if(likely(cg->io_queued.updated && cg->io_queued.enabled == CONFIG_ONDEMAND_YES)) {
2240 if(unlikely(!cg->st_queued_ops)) {
2241 cg->st_queued_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "queued_ops");
2242 if(likely(!cg->st_queued_ops)) {
2243 snprintfz(title, CHART_TITLE_MAX, "Queued I/O Operations (all disks) for cgroup %s", cg->chart_title);
2244 cg->st_queued_ops = rrdset_create(type, "queued_ops", NULL, "disk", "cgroup.queued_ops", title, "operations", CHART_PRIORITY_CONTAINERS + 2000, update_every, RRDSET_TYPE_LINE);
2246 rrddim_add(cg->st_queued_ops, "read", NULL, 1, 1, RRDDIM_ABSOLUTE);
2247 rrddim_add(cg->st_queued_ops, "write", NULL, -1, 1, RRDDIM_ABSOLUTE);
2250 rrdset_next(cg->st_queued_ops);
2252 rrddim_set(cg->st_queued_ops, "read", cg->io_queued.Read);
2253 rrddim_set(cg->st_queued_ops, "write", cg->io_queued.Write);
2254 rrdset_done(cg->st_queued_ops);
2257 if(likely(cg->io_merged.updated && cg->io_merged.enabled == CONFIG_ONDEMAND_YES)) {
2258 if(unlikely(!cg->st_merged_ops)) {
2259 cg->st_merged_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "merged_ops");
2260 if(likely(!cg->st_merged_ops)) {
2261 snprintfz(title, CHART_TITLE_MAX, "Merged I/O Operations (all disks) for cgroup %s", cg->chart_title);
2262 cg->st_merged_ops = rrdset_create(type, "merged_ops", NULL, "disk", "cgroup.merged_ops", title, "operations/s", CHART_PRIORITY_CONTAINERS + 2100, update_every, RRDSET_TYPE_LINE);
2264 rrddim_add(cg->st_merged_ops, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
2265 rrddim_add(cg->st_merged_ops, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
2268 rrdset_next(cg->st_merged_ops);
2270 rrddim_set(cg->st_merged_ops, "read", cg->io_merged.Read);
2271 rrddim_set(cg->st_merged_ops, "write", cg->io_merged.Write);
2272 rrdset_done(cg->st_merged_ops);
2276 if(likely(cgroup_enable_systemd_services))
2277 update_services_charts(update_every,
2279 services_do_mem_usage,
2280 services_do_mem_detailed,
2281 services_do_mem_failcnt,
2282 services_do_swap_usage,
2285 services_do_throttle_io,
2286 services_do_throttle_ops,
2287 services_do_queued_ops,
2288 services_do_merged_ops
2291 debug(D_CGROUP, "done updating cgroups charts");
2294 // ----------------------------------------------------------------------------
2297 void *cgroups_main(void *ptr) {
2298 struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
2300 info("CGROUP Plugin thread created with task id %d", gettid());
2302 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
2303 error("Cannot set pthread cancel type to DEFERRED.");
2305 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
2306 error("Cannot set pthread cancel state to ENABLE.");
2308 struct rusage thread;
2310 // when ZERO, attempt to do it
2311 int vdo_cpu_netdata = config_get_boolean("plugin:cgroups", "cgroups plugin resource charts", 1);
2313 read_cgroup_plugin_configuration();
2315 RRDSET *stcpu_thread = NULL;
2317 usec_t step = cgroup_update_every * USEC_PER_SEC;
2318 usec_t find_every = cgroup_check_for_new_every * USEC_PER_SEC, find_next = 0;
2320 usec_t now = now_monotonic_usec();
2321 usec_t next = now - (now % step) + step;
2324 sleep_usec(next - now);
2325 now = now_monotonic_usec();
2328 if(unlikely(netdata_exit)) break;
2330 // BEGIN -- the job to be done
2332 if(unlikely(now >= find_next)) {
2334 find_next = now + find_every;
2337 read_all_cgroups(cgroup_root);
2338 update_cgroup_charts(cgroup_update_every);
2340 // END -- the job is done
2342 // --------------------------------------------------------------------
2344 if(vdo_cpu_netdata) {
2345 getrusage(RUSAGE_THREAD, &thread);
2347 if(unlikely(!stcpu_thread)) {
2348 stcpu_thread = rrdset_find("netdata.plugin_cgroups_cpu");
2349 if(unlikely(!stcpu_thread))
2350 stcpu_thread = rrdset_create("netdata", "plugin_cgroups_cpu", NULL, "cgroups", NULL, "NetData CGroups Plugin CPU usage", "milliseconds/s", 132000, cgroup_update_every, RRDSET_TYPE_STACKED);
2352 rrddim_add(stcpu_thread, "user", NULL, 1, 1000, RRDDIM_INCREMENTAL);
2353 rrddim_add(stcpu_thread, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
2356 rrdset_next(stcpu_thread);
2358 rrddim_set(stcpu_thread, "user" , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
2359 rrddim_set(stcpu_thread, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
2360 rrdset_done(stcpu_thread);
2364 info("CGROUP thread exiting");
2366 static_thread->enabled = 0;