3 // ----------------------------------------------------------------------------
6 static int cgroup_enable_cpuacct_stat = CONFIG_ONDEMAND_ONDEMAND;
7 static int cgroup_enable_cpuacct_usage = CONFIG_ONDEMAND_ONDEMAND;
8 static int cgroup_enable_memory = CONFIG_ONDEMAND_ONDEMAND;
9 static int cgroup_enable_devices = CONFIG_ONDEMAND_ONDEMAND;
10 static int cgroup_enable_blkio = CONFIG_ONDEMAND_ONDEMAND;
11 static int cgroup_enable_systemd_services = CONFIG_ONDEMAND_YES;
12 static int cgroup_enable_new_cgroups_detected_at_runtime = 1;
13 static int cgroup_check_for_new_every = 10;
14 static int cgroup_update_every = 1;
16 static char *cgroup_cpuacct_base = NULL;
17 static char *cgroup_blkio_base = NULL;
18 static char *cgroup_memory_base = NULL;
19 static char *cgroup_devices_base = NULL;
21 static int cgroup_root_count = 0;
22 static int cgroup_root_max = 500;
23 static int cgroup_max_depth = 0;
25 static SIMPLE_PATTERN *disabled_cgroups_patterns = NULL;
26 static SIMPLE_PATTERN *disabled_cgroup_paths = NULL;
27 static SIMPLE_PATTERN *disabled_cgroup_renames = NULL;
28 static SIMPLE_PATTERN *systemd_services_cgroups = NULL;
30 static char *cgroups_rename_script = PLUGINS_DIR "/cgroup-name.sh";
32 void read_cgroup_plugin_configuration() {
33 cgroup_update_every = (int)config_get_number("plugin:cgroups", "update every", rrd_update_every);
34 if(cgroup_update_every < rrd_update_every)
35 cgroup_update_every = rrd_update_every;
37 cgroup_check_for_new_every = (int)config_get_number("plugin:cgroups", "check for new cgroups every", cgroup_check_for_new_every * cgroup_update_every);
38 if(cgroup_check_for_new_every < cgroup_update_every)
39 cgroup_check_for_new_every = cgroup_update_every;
41 cgroup_enable_cpuacct_stat = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct stat", cgroup_enable_cpuacct_stat);
42 cgroup_enable_cpuacct_usage = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct usage", cgroup_enable_cpuacct_usage);
43 cgroup_enable_memory = config_get_boolean_ondemand("plugin:cgroups", "enable memory", cgroup_enable_memory);
44 cgroup_enable_blkio = config_get_boolean_ondemand("plugin:cgroups", "enable blkio", cgroup_enable_blkio);
46 cgroup_enable_systemd_services = config_get_boolean_ondemand("plugin:cgroups", "enable systemd services", cgroup_enable_systemd_services);
48 char filename[FILENAME_MAX + 1], *s;
49 struct mountinfo *mi, *root = mountinfo_read(0);
51 mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "cpuacct");
52 if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "cpuacct");
54 error("Cannot find cgroup cpuacct mountinfo. Assuming default: /sys/fs/cgroup/cpuacct");
55 s = "/sys/fs/cgroup/cpuacct";
57 else s = mi->mount_point;
58 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
59 cgroup_cpuacct_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/cpuacct", filename);
61 mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "blkio");
62 if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "blkio");
64 error("Cannot find cgroup blkio mountinfo. Assuming default: /sys/fs/cgroup/blkio");
65 s = "/sys/fs/cgroup/blkio";
67 else s = mi->mount_point;
68 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
69 cgroup_blkio_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/blkio", filename);
71 mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "memory");
72 if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "memory");
74 error("Cannot find cgroup memory mountinfo. Assuming default: /sys/fs/cgroup/memory");
75 s = "/sys/fs/cgroup/memory";
77 else s = mi->mount_point;
78 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
79 cgroup_memory_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/memory", filename);
81 mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "devices");
82 if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "devices");
84 error("Cannot find cgroup devices mountinfo. Assuming default: /sys/fs/cgroup/devices");
85 s = "/sys/fs/cgroup/devices";
87 else s = mi->mount_point;
88 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
89 cgroup_devices_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/devices", filename);
91 cgroup_root_max = (int)config_get_number("plugin:cgroups", "max cgroups to allow", cgroup_root_max);
92 cgroup_max_depth = (int)config_get_number("plugin:cgroups", "max cgroups depth to monitor", cgroup_max_depth);
94 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);
96 disabled_cgroups_patterns = simple_pattern_create(
97 config_get("plugin:cgroups", "disable by default cgroups matching", " *.mount "
108 " /lxc/*/ns " // #1397
117 ), SIMPLE_PATTERN_EXACT);
119 disabled_cgroup_paths = simple_pattern_create(
120 config_get("plugin:cgroups", "do not search for cgroups in paths matching"
127 ), SIMPLE_PATTERN_EXACT);
129 cgroups_rename_script = config_get("plugin:cgroups", "script to get cgroup names", cgroups_rename_script);
131 disabled_cgroup_renames = simple_pattern_create(
132 config_get("plugin:cgroups", "do not run script to rename cgroups matching", " / "
140 ), SIMPLE_PATTERN_EXACT);
142 if(cgroup_enable_systemd_services)
143 systemd_services_cgroups = simple_pattern_create(
144 config_get("plugin:cgroups", "cgroups to match as systemd services"
145 , " !/system.slice/*/*.service /system.slice/*.service "
146 ), SIMPLE_PATTERN_EXACT);
148 mountinfo_free(root);
151 // ----------------------------------------------------------------------------
159 unsigned long long Read;
160 unsigned long long Write;
162 unsigned long long Sync;
163 unsigned long long Async;
164 unsigned long long Total;
168 // https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt
176 unsigned long long cache;
177 unsigned long long rss;
178 unsigned long long rss_huge;
179 unsigned long long mapped_file;
180 unsigned long long writeback;
181 unsigned long long dirty;
182 unsigned long long swap;
183 unsigned long long pgpgin;
184 unsigned long long pgpgout;
185 unsigned long long pgfault;
186 unsigned long long pgmajfault;
188 unsigned long long inactive_anon;
189 unsigned long long active_anon;
190 unsigned long long inactive_file;
191 unsigned long long active_file;
192 unsigned long long unevictable;
193 unsigned long long hierarchical_memory_limit;
194 unsigned long long total_cache;
195 unsigned long long total_rss;
196 unsigned long long total_rss_huge;
197 unsigned long long total_mapped_file;
198 unsigned long long total_writeback;
199 unsigned long long total_dirty;
200 unsigned long long total_swap;
201 unsigned long long total_pgpgin;
202 unsigned long long total_pgpgout;
203 unsigned long long total_pgfault;
204 unsigned long long total_pgmajfault;
205 unsigned long long total_inactive_anon;
206 unsigned long long total_active_anon;
207 unsigned long long total_inactive_file;
208 unsigned long long total_active_file;
209 unsigned long long total_unevictable;
212 int usage_in_bytes_updated;
213 char *filename_usage_in_bytes;
214 unsigned long long usage_in_bytes;
216 int msw_usage_in_bytes_updated;
217 char *filename_msw_usage_in_bytes;
218 unsigned long long msw_usage_in_bytes;
221 char *filename_failcnt;
222 unsigned long long failcnt;
225 // https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt
226 struct cpuacct_stat {
231 unsigned long long user;
232 unsigned long long system;
235 // https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt
236 struct cpuacct_usage {
242 unsigned long long *cpu_percpu;
245 #define CGROUP_OPTIONS_DISABLED_DUPLICATE 0x00000001
246 #define CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE 0x00000002
251 char available; // found in the filesystem
252 char enabled; // enabled in the config
262 struct cpuacct_stat cpuacct_stat;
263 struct cpuacct_usage cpuacct_usage;
265 struct memory memory;
267 struct blkio io_service_bytes; // bytes
268 struct blkio io_serviced; // operations
270 struct blkio throttle_io_service_bytes; // bytes
271 struct blkio throttle_io_serviced; // operations
273 struct blkio io_merged; // operations
274 struct blkio io_queued; // operations
278 RRDSET *st_cpu_per_core;
280 RRDSET *st_writeback;
281 RRDSET *st_mem_activity;
283 RRDSET *st_mem_usage;
284 RRDSET *st_mem_failcnt;
286 RRDSET *st_serviced_ops;
287 RRDSET *st_throttle_io;
288 RRDSET *st_throttle_serviced_ops;
289 RRDSET *st_queued_ops;
290 RRDSET *st_merged_ops;
294 RRDDIM *rd_mem_usage;
296 RRDDIM *rd_io_service_bytes_read;
297 RRDDIM *rd_io_serviced_read;
298 RRDDIM *rd_throttle_io_read;
299 RRDDIM *rd_throttle_io_serviced_read;
300 RRDDIM *rd_io_queued_read;
301 RRDDIM *rd_io_merged_read;
303 RRDDIM *rd_io_service_bytes_write;
304 RRDDIM *rd_io_serviced_write;
305 RRDDIM *rd_throttle_io_write;
306 RRDDIM *rd_throttle_io_serviced_write;
307 RRDDIM *rd_io_queued_write;
308 RRDDIM *rd_io_merged_write;
312 } *cgroup_root = NULL;
314 // ----------------------------------------------------------------------------
315 // read values from /sys
317 void cgroup_read_cpuacct_stat(struct cpuacct_stat *cp) {
318 static procfile *ff = NULL;
320 static uint32_t user_hash = 0;
321 static uint32_t system_hash = 0;
323 if(unlikely(user_hash == 0)) {
324 user_hash = simple_hash("user");
325 system_hash = simple_hash("system");
330 ff = procfile_reopen(ff, cp->filename, NULL, PROCFILE_FLAG_DEFAULT);
333 ff = procfile_readall(ff);
336 unsigned long i, lines = procfile_lines(ff);
339 error("File '%s' should have 1+ lines.", cp->filename);
343 for(i = 0; i < lines ; i++) {
344 char *s = procfile_lineword(ff, i, 0);
345 uint32_t hash = simple_hash(s);
347 if(hash == user_hash && !strcmp(s, "user"))
348 cp->user = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
350 else if(hash == system_hash && !strcmp(s, "system"))
351 cp->system = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
356 // fprintf(stderr, "READ '%s': user: %llu, system: %llu\n", cp->filename, cp->user, cp->system);
360 void cgroup_read_cpuacct_usage(struct cpuacct_usage *ca) {
361 static procfile *ff = NULL;
365 ff = procfile_reopen(ff, ca->filename, NULL, PROCFILE_FLAG_DEFAULT);
368 ff = procfile_readall(ff);
371 if(procfile_lines(ff) < 1) {
372 error("File '%s' should have 1+ lines but has %u.", ca->filename, procfile_lines(ff));
376 unsigned long i = procfile_linewords(ff, 0);
379 // we may have 1 more CPU reported
381 char *s = procfile_lineword(ff, 0, i - 1);
387 freez(ca->cpu_percpu);
388 ca->cpu_percpu = mallocz(sizeof(unsigned long long) * i);
389 ca->cpus = (unsigned int)i;
392 for(i = 0; i < ca->cpus ;i++) {
393 ca->cpu_percpu[i] = strtoull(procfile_lineword(ff, 0, i), NULL, 10);
394 // fprintf(stderr, "READ '%s': cpu%d/%d: %llu ('%s')\n", ca->filename, i, ca->cpus, ca->cpu_percpu[i], procfile_lineword(ff, 0, i));
401 void cgroup_read_blkio(struct blkio *io) {
402 static procfile *ff = NULL;
404 static uint32_t Read_hash = 0;
405 static uint32_t Write_hash = 0;
407 static uint32_t Sync_hash = 0;
408 static uint32_t Async_hash = 0;
409 static uint32_t Total_hash = 0;
412 if(unlikely(Read_hash == 0)) {
413 Read_hash = simple_hash("Read");
414 Write_hash = simple_hash("Write");
416 Sync_hash = simple_hash("Sync");
417 Async_hash = simple_hash("Async");
418 Total_hash = simple_hash("Total");
424 ff = procfile_reopen(ff, io->filename, NULL, PROCFILE_FLAG_DEFAULT);
427 ff = procfile_readall(ff);
430 unsigned long i, lines = procfile_lines(ff);
433 error("File '%s' should have 1+ lines.", io->filename);
445 for(i = 0; i < lines ; i++) {
446 char *s = procfile_lineword(ff, i, 1);
447 uint32_t hash = simple_hash(s);
449 if(hash == Read_hash && !strcmp(s, "Read"))
450 io->Read += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
452 else if(hash == Write_hash && !strcmp(s, "Write"))
453 io->Write += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
456 else if(hash == Sync_hash && !strcmp(s, "Sync"))
457 io->Sync += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
459 else if(hash == Async_hash && !strcmp(s, "Async"))
460 io->Async += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
462 else if(hash == Total_hash && !strcmp(s, "Total"))
463 io->Total += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
468 // fprintf(stderr, "READ '%s': Read: %llu, Write: %llu, Sync: %llu, Async: %llu, Total: %llu\n", io->filename, io->Read, io->Write, io->Sync, io->Async, io->Total);
472 void cgroup_read_memory(struct memory *mem) {
473 static procfile *ff = NULL;
475 static uint32_t cache_hash = 0;
476 static uint32_t rss_hash = 0;
477 static uint32_t rss_huge_hash = 0;
478 static uint32_t mapped_file_hash = 0;
479 static uint32_t writeback_hash = 0;
480 static uint32_t dirty_hash = 0;
481 static uint32_t swap_hash = 0;
482 static uint32_t pgpgin_hash = 0;
483 static uint32_t pgpgout_hash = 0;
484 static uint32_t pgfault_hash = 0;
485 static uint32_t pgmajfault_hash = 0;
487 static uint32_t inactive_anon_hash = 0;
488 static uint32_t active_anon_hash = 0;
489 static uint32_t inactive_file_hash = 0;
490 static uint32_t active_file_hash = 0;
491 static uint32_t unevictable_hash = 0;
492 static uint32_t hierarchical_memory_limit_hash = 0;
493 static uint32_t total_cache_hash = 0;
494 static uint32_t total_rss_hash = 0;
495 static uint32_t total_rss_huge_hash = 0;
496 static uint32_t total_mapped_file_hash = 0;
497 static uint32_t total_writeback_hash = 0;
498 static uint32_t total_dirty_hash = 0;
499 static uint32_t total_swap_hash = 0;
500 static uint32_t total_pgpgin_hash = 0;
501 static uint32_t total_pgpgout_hash = 0;
502 static uint32_t total_pgfault_hash = 0;
503 static uint32_t total_pgmajfault_hash = 0;
504 static uint32_t total_inactive_anon_hash = 0;
505 static uint32_t total_active_anon_hash = 0;
506 static uint32_t total_inactive_file_hash = 0;
507 static uint32_t total_active_file_hash = 0;
508 static uint32_t total_unevictable_hash = 0;
510 if(unlikely(cache_hash == 0)) {
511 cache_hash = simple_hash("cache");
512 rss_hash = simple_hash("rss");
513 rss_huge_hash = simple_hash("rss_huge");
514 mapped_file_hash = simple_hash("mapped_file");
515 writeback_hash = simple_hash("writeback");
516 dirty_hash = simple_hash("dirty");
517 swap_hash = simple_hash("swap");
518 pgpgin_hash = simple_hash("pgpgin");
519 pgpgout_hash = simple_hash("pgpgout");
520 pgfault_hash = simple_hash("pgfault");
521 pgmajfault_hash = simple_hash("pgmajfault");
523 inactive_anon_hash = simple_hash("inactive_anon");
524 active_anon_hash = simple_hash("active_anon");
525 inactive_file_hash = simple_hash("inactive_file");
526 active_file_hash = simple_hash("active_file");
527 unevictable_hash = simple_hash("unevictable");
528 hierarchical_memory_limit_hash = simple_hash("hierarchical_memory_limit");
529 total_cache_hash = simple_hash("total_cache");
530 total_rss_hash = simple_hash("total_rss");
531 total_rss_huge_hash = simple_hash("total_rss_huge");
532 total_mapped_file_hash = simple_hash("total_mapped_file");
533 total_writeback_hash = simple_hash("total_writeback");
534 total_dirty_hash = simple_hash("total_dirty");
535 total_swap_hash = simple_hash("total_swap");
536 total_pgpgin_hash = simple_hash("total_pgpgin");
537 total_pgpgout_hash = simple_hash("total_pgpgout");
538 total_pgfault_hash = simple_hash("total_pgfault");
539 total_pgmajfault_hash = simple_hash("total_pgmajfault");
540 total_inactive_anon_hash = simple_hash("total_inactive_anon");
541 total_active_anon_hash = simple_hash("total_active_anon");
542 total_inactive_file_hash = simple_hash("total_inactive_file");
543 total_active_file_hash = simple_hash("total_active_file");
544 total_unevictable_hash = simple_hash("total_unevictable");
550 ff = procfile_reopen(ff, mem->filename, NULL, PROCFILE_FLAG_DEFAULT);
553 ff = procfile_readall(ff);
556 unsigned long i, lines = procfile_lines(ff);
559 error("File '%s' should have 1+ lines.", mem->filename);
563 for(i = 0; i < lines ; i++) {
564 char *s = procfile_lineword(ff, i, 0);
565 uint32_t hash = simple_hash(s);
567 if(hash == cache_hash && !strcmp(s, "cache"))
568 mem->cache = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
570 else if(hash == rss_hash && !strcmp(s, "rss"))
571 mem->rss = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
573 else if(hash == rss_huge_hash && !strcmp(s, "rss_huge"))
574 mem->rss_huge = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
576 else if(hash == mapped_file_hash && !strcmp(s, "mapped_file"))
577 mem->mapped_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
579 else if(hash == writeback_hash && !strcmp(s, "writeback"))
580 mem->writeback = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
582 else if(hash == dirty_hash && !strcmp(s, "dirty")) {
583 mem->dirty = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
584 mem->has_dirty_swap = 1;
587 else if(hash == swap_hash && !strcmp(s, "swap")) {
588 mem->swap = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
589 mem->has_dirty_swap = 1;
592 else if(hash == pgpgin_hash && !strcmp(s, "pgpgin"))
593 mem->pgpgin = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
595 else if(hash == pgpgout_hash && !strcmp(s, "pgpgout"))
596 mem->pgpgout = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
598 else if(hash == pgfault_hash && !strcmp(s, "pgfault"))
599 mem->pgfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
601 else if(hash == pgmajfault_hash && !strcmp(s, "pgmajfault"))
602 mem->pgmajfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
605 else if(hash == inactive_anon_hash && !strcmp(s, "inactive_anon"))
606 mem->inactive_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
608 else if(hash == active_anon_hash && !strcmp(s, "active_anon"))
609 mem->active_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
611 else if(hash == inactive_file_hash && !strcmp(s, "inactive_file"))
612 mem->inactive_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
614 else if(hash == active_file_hash && !strcmp(s, "active_file"))
615 mem->active_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
617 else if(hash == unevictable_hash && !strcmp(s, "unevictable"))
618 mem->unevictable = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
620 else if(hash == hierarchical_memory_limit_hash && !strcmp(s, "hierarchical_memory_limit"))
621 mem->hierarchical_memory_limit = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
623 else if(hash == total_cache_hash && !strcmp(s, "total_cache"))
624 mem->total_cache = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
626 else if(hash == total_rss_hash && !strcmp(s, "total_rss"))
627 mem->total_rss = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
629 else if(hash == total_rss_huge_hash && !strcmp(s, "total_rss_huge"))
630 mem->total_rss_huge = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
632 else if(hash == total_mapped_file_hash && !strcmp(s, "total_mapped_file"))
633 mem->total_mapped_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
635 else if(hash == total_writeback_hash && !strcmp(s, "total_writeback"))
636 mem->total_writeback = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
638 else if(hash == total_dirty_hash && !strcmp(s, "total_dirty"))
639 mem->total_dirty = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
641 else if(hash == total_swap_hash && !strcmp(s, "total_swap"))
642 mem->total_swap = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
644 else if(hash == total_pgpgin_hash && !strcmp(s, "total_pgpgin"))
645 mem->total_pgpgin = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
647 else if(hash == total_pgpgout_hash && !strcmp(s, "total_pgpgout"))
648 mem->total_pgpgout = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
650 else if(hash == total_pgfault_hash && !strcmp(s, "total_pgfault"))
651 mem->total_pgfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
653 else if(hash == total_pgmajfault_hash && !strcmp(s, "total_pgmajfault"))
654 mem->total_pgmajfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
656 else if(hash == total_inactive_anon_hash && !strcmp(s, "total_inactive_anon"))
657 mem->total_inactive_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
659 else if(hash == total_active_anon_hash && !strcmp(s, "total_active_anon"))
660 mem->total_active_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
662 else if(hash == total_inactive_file_hash && !strcmp(s, "total_inactive_file"))
663 mem->total_inactive_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
665 else if(hash == total_active_file_hash && !strcmp(s, "total_active_file"))
666 mem->total_active_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
668 else if(hash == total_unevictable_hash && !strcmp(s, "total_unevictable"))
669 mem->total_unevictable = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
673 // 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);
678 mem->usage_in_bytes_updated = 0;
679 if(mem->filename_usage_in_bytes) {
680 if(likely(!read_single_number_file(mem->filename_usage_in_bytes, &mem->usage_in_bytes)))
681 mem->usage_in_bytes_updated = 1;
684 mem->msw_usage_in_bytes_updated = 0;
685 if(mem->filename_msw_usage_in_bytes) {
686 if(likely(!read_single_number_file(mem->filename_msw_usage_in_bytes, &mem->msw_usage_in_bytes)))
687 mem->msw_usage_in_bytes_updated = 1;
690 mem->failcnt_updated = 0;
691 if(mem->filename_failcnt) {
692 if(likely(!read_single_number_file(mem->filename_failcnt, &mem->failcnt)))
693 mem->failcnt_updated = 1;
697 void cgroup_read(struct cgroup *cg) {
698 debug(D_CGROUP, "reading metrics for cgroups '%s'", cg->id);
700 cgroup_read_cpuacct_stat(&cg->cpuacct_stat);
701 cgroup_read_cpuacct_usage(&cg->cpuacct_usage);
702 cgroup_read_memory(&cg->memory);
703 cgroup_read_blkio(&cg->io_service_bytes);
704 cgroup_read_blkio(&cg->io_serviced);
705 cgroup_read_blkio(&cg->throttle_io_service_bytes);
706 cgroup_read_blkio(&cg->throttle_io_serviced);
707 cgroup_read_blkio(&cg->io_merged);
708 cgroup_read_blkio(&cg->io_queued);
711 void read_all_cgroups(struct cgroup *root) {
712 debug(D_CGROUP, "reading metrics for all cgroups");
716 for(cg = root; cg ; cg = cg->next)
717 if(cg->enabled && cg->available)
721 // ----------------------------------------------------------------------------
722 // add/remove/find cgroup objects
724 #define CGROUP_CHARTID_LINE_MAX 1024
726 static char *cgroup_title_strdupz(const char *s) {
727 if(!s || !*s) s = "/";
729 if(*s == '/' && s[1] != '\0') s++;
731 char *r = strdupz(s);
732 netdata_fix_chart_name(r);
737 static char *cgroup_chart_id_strdupz(const char *s) {
738 if(!s || !*s) s = "/";
740 if(*s == '/' && s[1] != '\0') s++;
742 char *r = strdupz(s);
743 netdata_fix_chart_id(r);
748 void cgroup_get_chart_name(struct cgroup *cg) {
749 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);
752 char buffer[CGROUP_CHARTID_LINE_MAX + 1];
754 snprintfz(buffer, CGROUP_CHARTID_LINE_MAX, "exec %s '%s'", cgroups_rename_script, cg->chart_id);
756 debug(D_CGROUP, "executing command '%s' for cgroup '%s'", buffer, cg->id);
757 FILE *fp = mypopen(buffer, &cgroup_pid);
759 // debug(D_CGROUP, "reading from command '%s' for cgroup '%s'", buffer, cg->id);
760 char *s = fgets(buffer, CGROUP_CHARTID_LINE_MAX, fp);
761 // debug(D_CGROUP, "closing command for cgroup '%s'", cg->id);
762 mypclose(fp, cgroup_pid);
763 // debug(D_CGROUP, "closed command for cgroup '%s'", cg->id);
765 if(s && *s && *s != '\n') {
766 debug(D_CGROUP, "cgroup '%s' should be renamed to '%s'", cg->id, s);
770 freez(cg->chart_title);
771 cg->chart_title = cgroup_title_strdupz(s);
774 cg->chart_id = cgroup_chart_id_strdupz(s);
775 cg->hash_chart = simple_hash(cg->chart_id);
779 error("CGROUP: Cannot popen(\"%s\", \"r\").", buffer);
782 struct cgroup *cgroup_add(const char *id) {
783 if(!id || !*id) id = "/";
784 debug(D_CGROUP, "adding to list, cgroup with id '%s'", id);
786 if(cgroup_root_count >= cgroup_root_max) {
787 info("Maximum number of cgroups reached (%d). Not adding cgroup '%s'", cgroup_root_count, id);
791 int def = simple_pattern_matches(disabled_cgroups_patterns, id)?0:cgroup_enable_new_cgroups_detected_at_runtime;
792 struct cgroup *cg = callocz(1, sizeof(struct cgroup));
794 cg->id = strdupz(id);
795 cg->hash = simple_hash(cg->id);
797 cg->chart_title = cgroup_title_strdupz(id);
799 cg->chart_id = cgroup_chart_id_strdupz(id);
800 cg->hash_chart = simple_hash(cg->chart_id);
807 for(e = cgroup_root; e->next ;e = e->next) ;
813 // fix the chart_id and title by calling the external script
814 if(!simple_pattern_matches(disabled_cgroup_renames, cg->id) &&
815 !simple_pattern_matches(disabled_cgroup_renames, cg->chart_id)) {
817 cgroup_get_chart_name(cg);
819 debug(D_CGROUP, "cgroup '%s' renamed to '%s' (title: '%s')", cg->id, cg->chart_id, cg->chart_title);
822 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);
824 int user_configurable = 1;
826 // check if this cgroup should be a systemd service
827 if(cgroup_enable_systemd_services) {
828 if(simple_pattern_matches(systemd_services_cgroups, cg->id) ||
829 simple_pattern_matches(systemd_services_cgroups, cg->chart_id)) {
830 debug(D_CGROUP, "cgroup '%s' with chart id '%s' (title: '%s') matches systemd services cgroups", cg->id, cg->chart_id, cg->chart_title);
832 char buffer[CGROUP_CHARTID_LINE_MAX + 1];
833 cg->options |= CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE;
835 strncpy(buffer, cg->id, CGROUP_CHARTID_LINE_MAX);
838 //freez(cg->chart_id);
839 //cg->chart_id = cgroup_chart_id_strdupz(s);
840 //cg->hash_chart = simple_hash(cg->chart_id);
842 // skip to the last slash
843 size_t len = strlen(s);
844 while(len--) if(unlikely(s[len] == '/')) break;
845 if(len) s = &s[len + 1];
849 while(len--) if(unlikely(s[len] == '.')) break;
850 if(len) s[len] = '\0';
852 freez(cg->chart_title);
853 cg->chart_title = cgroup_title_strdupz(s);
856 user_configurable = 0;
858 debug(D_CGROUP, "cgroup '%s' renamed to '%s' (title: '%s')", cg->id, cg->chart_id, cg->chart_title);
861 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);
864 if(user_configurable) {
865 // allow the user to enable/disable this individualy
866 char option[FILENAME_MAX + 1];
867 snprintfz(option, FILENAME_MAX, "enable cgroup %s", cg->chart_title);
868 cg->enabled = (char) config_get_boolean("plugin:cgroups", option, def);
871 // detect duplicate cgroups
874 for (t = cgroup_root; t; t = t->next) {
875 if (t != cg && t->enabled && t->hash_chart == cg->hash_chart && !strcmp(t->chart_id, cg->chart_id)) {
876 if (!strncmp(t->chart_id, "/system.slice/", 14) && !strncmp(cg->chart_id, "/init.scope/system.slice/", 25)) {
877 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'.",
878 cg->chart_id, t->id, cg->id, t->id);
879 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'.",
880 cg->chart_id, t->id, cg->id, t->id);
882 t->options |= CGROUP_OPTIONS_DISABLED_DUPLICATE;
885 error("Control group with chart id '%s' already exists with id '%s' and is enabled and available. Disabling cgroup with id '%s'.",
886 cg->chart_id, t->id, cg->id);
887 debug(D_CGROUP, "Control group with chart id '%s' already exists with id '%s' and is enabled and available. Disabling cgroup with id '%s'.",
888 cg->chart_id, t->id, cg->id);
890 cg->options |= CGROUP_OPTIONS_DISABLED_DUPLICATE;
898 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");
903 void cgroup_free(struct cgroup *cg) {
904 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");
906 freez(cg->cpuacct_usage.cpu_percpu);
908 freez(cg->cpuacct_stat.filename);
909 freez(cg->cpuacct_usage.filename);
911 freez(cg->memory.filename);
912 freez(cg->memory.filename_failcnt);
913 freez(cg->memory.filename_usage_in_bytes);
914 freez(cg->memory.filename_msw_usage_in_bytes);
916 freez(cg->io_service_bytes.filename);
917 freez(cg->io_serviced.filename);
919 freez(cg->throttle_io_service_bytes.filename);
920 freez(cg->throttle_io_serviced.filename);
922 freez(cg->io_merged.filename);
923 freez(cg->io_queued.filename);
927 freez(cg->chart_title);
934 // find if a given cgroup exists
935 struct cgroup *cgroup_find(const char *id) {
936 debug(D_CGROUP, "searching for cgroup '%s'", id);
938 uint32_t hash = simple_hash(id);
941 for(cg = cgroup_root; cg ; cg = cg->next) {
942 if(hash == cg->hash && strcmp(id, cg->id) == 0)
946 debug(D_CGROUP, "cgroup '%s' %s in memory", id, (cg)?"found":"not found");
950 // ----------------------------------------------------------------------------
951 // detect running cgroups
953 // callback for find_file_in_subdirs()
954 void found_subdir_in_dir(const char *dir) {
955 debug(D_CGROUP, "examining cgroup dir '%s'", dir);
957 struct cgroup *cg = cgroup_find(dir);
959 if(*dir && cgroup_max_depth > 0) {
963 for(s = dir; *s ;s++)
964 if(unlikely(*s == '/'))
967 if(depth > cgroup_max_depth) {
968 info("cgroup '%s' is too deep (%d, while max is %d)", dir, depth, cgroup_max_depth);
972 // debug(D_CGROUP, "will add dir '%s' as cgroup", dir);
973 cg = cgroup_add(dir);
976 if(cg) cg->available = 1;
979 int find_dir_in_subdirs(const char *base, const char *this, void (*callback)(const char *)) {
980 if(!this) this = base;
981 debug(D_CGROUP, "searching for directories in '%s' (base '%s')", this?this:"", base);
983 size_t dirlen = strlen(this), baselen = strlen(base);
988 const char *relative_path = &this[baselen];
989 if(!*relative_path) relative_path = "/";
991 DIR *dir = opendir(this);
993 error("Cannot read cgroups directory '%s'", base);
998 callback(relative_path);
1000 struct dirent *de = NULL;
1001 while((de = readdir(dir))) {
1002 if(de->d_type == DT_DIR
1004 (de->d_name[0] == '.' && de->d_name[1] == '\0')
1005 || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
1009 if(de->d_type == DT_DIR) {
1011 const char *r = relative_path;
1012 if(*r == '\0') r = "/";
1014 // do not decent in directories we are not interested
1016 if(simple_pattern_matches(disabled_cgroup_paths, r))
1019 // we check for this option here
1020 // so that the config will not have settings
1021 // for leaf directories
1022 char option[FILENAME_MAX + 1];
1023 snprintfz(option, FILENAME_MAX, "search for cgroups under %s", r);
1024 option[FILENAME_MAX] = '\0';
1025 enabled = config_get_boolean("plugin:cgroups", option, def);
1029 char *s = mallocz(dirlen + strlen(de->d_name) + 2);
1032 strcat(s, de->d_name);
1033 int ret2 = find_dir_in_subdirs(base, s, callback);
1034 if(ret2 > 0) ret += ret2;
1044 void mark_all_cgroups_as_not_available() {
1045 debug(D_CGROUP, "marking all cgroups as not available");
1049 // mark all as not available
1050 for(cg = cgroup_root; cg ; cg = cg->next) {
1055 void cleanup_all_cgroups() {
1056 struct cgroup *cg = cgroup_root, *last = NULL;
1059 if(!cg->available) {
1060 // enable the first duplicate cgroup
1063 for(t = cgroup_root; t ; t = t->next) {
1064 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)) {
1065 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);
1067 t->options &= ~CGROUP_OPTIONS_DISABLED_DUPLICATE;
1074 cgroup_root = cg->next;
1076 last->next = cg->next;
1092 void find_all_cgroups() {
1093 debug(D_CGROUP, "searching for cgroups");
1095 mark_all_cgroups_as_not_available();
1097 if(cgroup_enable_cpuacct_stat || cgroup_enable_cpuacct_usage) {
1098 if (find_dir_in_subdirs(cgroup_cpuacct_base, NULL, found_subdir_in_dir) == -1) {
1099 cgroup_enable_cpuacct_stat = cgroup_enable_cpuacct_usage = 0;
1100 error("disabled cgroup cpu statistics.");
1104 if(cgroup_enable_blkio) {
1105 if (find_dir_in_subdirs(cgroup_blkio_base, NULL, found_subdir_in_dir) == -1) {
1106 cgroup_enable_blkio = 0;
1107 error("disabled cgroup blkio statistics.");
1111 if(cgroup_enable_memory) {
1112 if(find_dir_in_subdirs(cgroup_memory_base, NULL, found_subdir_in_dir) == -1) {
1113 cgroup_enable_memory = 0;
1114 error("disabled cgroup memory statistics.");
1118 if(cgroup_enable_devices) {
1119 if(find_dir_in_subdirs(cgroup_devices_base, NULL, found_subdir_in_dir) == -1) {
1120 cgroup_enable_devices = 0;
1121 error("disabled cgroup devices statistics.");
1125 // remove any non-existing cgroups
1126 cleanup_all_cgroups();
1130 for(cg = cgroup_root; cg ; cg = cg->next) {
1131 // fprintf(stderr, " >>> CGROUP '%s' (%u - %s) with name '%s'\n", cg->id, cg->hash, cg->available?"available":"stopped", cg->name);
1133 if(unlikely(!cg->available))
1136 debug(D_CGROUP, "checking paths for cgroup '%s'", cg->id);
1138 // check for newly added cgroups
1139 // and update the filenames they read
1140 char filename[FILENAME_MAX + 1];
1141 if(unlikely(cgroup_enable_cpuacct_stat && !cg->cpuacct_stat.filename)) {
1142 snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.stat", cgroup_cpuacct_base, cg->id);
1143 if(stat(filename, &buf) != -1) {
1144 cg->cpuacct_stat.filename = strdupz(filename);
1145 debug(D_CGROUP, "cpuacct.stat filename for cgroup '%s': '%s'", cg->id, cg->cpuacct_stat.filename);
1147 else debug(D_CGROUP, "cpuacct.stat file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1150 if(unlikely(cgroup_enable_cpuacct_usage && !cg->cpuacct_usage.filename)) {
1151 snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.usage_percpu", cgroup_cpuacct_base, cg->id);
1152 if(stat(filename, &buf) != -1) {
1153 cg->cpuacct_usage.filename = strdupz(filename);
1154 debug(D_CGROUP, "cpuacct.usage_percpu filename for cgroup '%s': '%s'", cg->id, cg->cpuacct_usage.filename);
1156 else debug(D_CGROUP, "cpuacct.usage_percpu file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1159 if(unlikely(cgroup_enable_memory)) {
1160 if(unlikely(!cg->memory.filename)) {
1161 snprintfz(filename, FILENAME_MAX, "%s%s/memory.stat", cgroup_memory_base, cg->id);
1162 if(stat(filename, &buf) != -1) {
1163 cg->memory.filename = strdupz(filename);
1164 debug(D_CGROUP, "memory.stat filename for cgroup '%s': '%s'", cg->id, cg->memory.filename);
1167 debug(D_CGROUP, "memory.stat file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1169 if(unlikely(!cg->memory.filename_usage_in_bytes)) {
1170 snprintfz(filename, FILENAME_MAX, "%s%s/memory.usage_in_bytes", cgroup_memory_base, cg->id);
1171 if(stat(filename, &buf) != -1) {
1172 cg->memory.filename_usage_in_bytes = strdupz(filename);
1173 debug(D_CGROUP, "memory.usage_in_bytes filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_usage_in_bytes);
1176 debug(D_CGROUP, "memory.usage_in_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1178 if(unlikely(!cg->memory.filename_msw_usage_in_bytes)) {
1179 snprintfz(filename, FILENAME_MAX, "%s%s/memory.msw_usage_in_bytes", cgroup_memory_base, cg->id);
1180 if(stat(filename, &buf) != -1) {
1181 cg->memory.filename_msw_usage_in_bytes = strdupz(filename);
1182 debug(D_CGROUP, "memory.msw_usage_in_bytes filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_msw_usage_in_bytes);
1185 debug(D_CGROUP, "memory.msw_usage_in_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1187 if(unlikely(!cg->memory.filename_failcnt)) {
1188 snprintfz(filename, FILENAME_MAX, "%s%s/memory.failcnt", cgroup_memory_base, cg->id);
1189 if(stat(filename, &buf) != -1) {
1190 cg->memory.filename_failcnt = strdupz(filename);
1191 debug(D_CGROUP, "memory.failcnt filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_failcnt);
1194 debug(D_CGROUP, "memory.failcnt file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1198 if(unlikely(cgroup_enable_blkio)) {
1199 if(unlikely(!cg->io_service_bytes.filename)) {
1200 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_service_bytes", cgroup_blkio_base, cg->id);
1201 if(stat(filename, &buf) != -1) {
1202 cg->io_service_bytes.filename = strdupz(filename);
1203 debug(D_CGROUP, "io_service_bytes filename for cgroup '%s': '%s'", cg->id, cg->io_service_bytes.filename);
1205 else debug(D_CGROUP, "io_service_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1207 if(unlikely(!cg->io_serviced.filename)) {
1208 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_serviced", cgroup_blkio_base, cg->id);
1209 if(stat(filename, &buf) != -1) {
1210 cg->io_serviced.filename = strdupz(filename);
1211 debug(D_CGROUP, "io_serviced filename for cgroup '%s': '%s'", cg->id, cg->io_serviced.filename);
1213 else debug(D_CGROUP, "io_serviced file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1215 if(unlikely(!cg->throttle_io_service_bytes.filename)) {
1216 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.throttle.io_service_bytes", cgroup_blkio_base, cg->id);
1217 if(stat(filename, &buf) != -1) {
1218 cg->throttle_io_service_bytes.filename = strdupz(filename);
1219 debug(D_CGROUP, "throttle_io_service_bytes filename for cgroup '%s': '%s'", cg->id, cg->throttle_io_service_bytes.filename);
1221 else debug(D_CGROUP, "throttle_io_service_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1223 if(unlikely(!cg->throttle_io_serviced.filename)) {
1224 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.throttle.io_serviced", cgroup_blkio_base, cg->id);
1225 if(stat(filename, &buf) != -1) {
1226 cg->throttle_io_serviced.filename = strdupz(filename);
1227 debug(D_CGROUP, "throttle_io_serviced filename for cgroup '%s': '%s'", cg->id, cg->throttle_io_serviced.filename);
1229 else debug(D_CGROUP, "throttle_io_serviced file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1231 if(unlikely(!cg->io_merged.filename)) {
1232 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_merged", cgroup_blkio_base, cg->id);
1233 if(stat(filename, &buf) != -1) {
1234 cg->io_merged.filename = strdupz(filename);
1235 debug(D_CGROUP, "io_merged filename for cgroup '%s': '%s'", cg->id, cg->io_merged.filename);
1237 else debug(D_CGROUP, "io_merged file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1239 if(unlikely(!cg->io_queued.filename)) {
1240 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_queued", cgroup_blkio_base, cg->id);
1241 if(stat(filename, &buf) != -1) {
1242 cg->io_queued.filename = strdupz(filename);
1243 debug(D_CGROUP, "io_queued filename for cgroup '%s': '%s'", cg->id, cg->io_queued.filename);
1245 else debug(D_CGROUP, "io_queued file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1250 debug(D_CGROUP, "done searching for cgroups");
1254 // ----------------------------------------------------------------------------
1257 #define CHART_TITLE_MAX 300
1259 void update_services_charts(int update_every,
1265 int do_throttle_ops /*,
1267 int do_merged_ops */
1271 *st_mem_usage = NULL,
1274 *st_io_serviced_read = NULL,
1275 *st_throttle_io_read = NULL,
1276 *st_throttle_ops_read = NULL,
1278 *st_queued_ops_read = NULL,
1279 *st_merged_ops_read = NULL,
1282 *st_io_write = NULL,
1283 *st_io_serviced_write = NULL,
1284 *st_throttle_io_write = NULL,
1285 *st_throttle_ops_write = NULL/*,
1286 *st_queued_ops_write = NULL,
1287 *st_merged_ops_write = NULL*/;
1289 // create the charts
1292 if(unlikely(!st_cpu)) {
1293 char title[CHART_TITLE_MAX + 1];
1295 st_cpu = rrdset_find_bytype("services", "cpu");
1297 snprintfz(title, CHART_TITLE_MAX, "Systemd Services CPU utilization (%d%% = %d core%s)", (processors * 100), processors, (processors > 1) ? "s" : "");
1298 st_cpu = rrdset_create("services", "cpu", NULL, "cpu", "services.cpu", title, "%", 30000, update_every, RRDSET_TYPE_STACKED);
1301 else rrdset_next(st_cpu);
1305 if(unlikely(!st_mem_usage)) {
1306 st_mem_usage = rrdset_find_bytype("services", "mem_usage");
1308 st_mem_usage = rrdset_create("services", "mem_usage", NULL, "mem", "services.mem_usage", "Systemd Services RAM Usage", "MB", 30001, update_every, RRDSET_TYPE_STACKED);
1310 else rrdset_next(st_mem_usage);
1314 if(unlikely(!st_io_read)) {
1315 st_io_read = rrdset_find_bytype("services", "io_read");
1317 st_io_read = rrdset_create("services", "io_read", NULL, "disk", "services.io_read", "Systemd Services Disk Read Bandwidth", "KB/s", 30012, update_every, RRDSET_TYPE_STACKED);
1319 else rrdset_next(st_io_read);
1321 if(unlikely(!st_io_write)) {
1322 st_io_write = rrdset_find_bytype("services", "io_write");
1324 st_io_write = rrdset_create("services", "io_write", NULL, "disk", "services.io_write", "Systemd Services Disk Write Bandwidth", "KB/s", 30013, update_every, RRDSET_TYPE_STACKED);
1326 else rrdset_next(st_io_write);
1330 if(unlikely(!st_io_serviced_read)) {
1331 st_io_serviced_read = rrdset_find_bytype("services", "io_ops_read");
1332 if(!st_io_serviced_read)
1333 st_io_serviced_read = rrdset_create("services", "io_ops_read", NULL, "disk", "services.io_ops_read", "Systemd Services Disk Read Operations", "operations/s", 30014, update_every, RRDSET_TYPE_STACKED);
1335 else rrdset_next(st_io_serviced_read);
1337 if(unlikely(!st_io_serviced_write)) {
1338 st_io_serviced_write = rrdset_find_bytype("services", "io_ops_write");
1339 if(!st_io_serviced_write)
1340 st_io_serviced_write = rrdset_create("services", "io_ops_write", NULL, "disk", "services.io_ops_write", "Systemd Services Disk Write Operations", "operations/s", 30015, update_every, RRDSET_TYPE_STACKED);
1342 else rrdset_next(st_io_serviced_write);
1345 if(do_throttle_io) {
1346 if(unlikely(!st_throttle_io_read)) {
1347 st_throttle_io_read = rrdset_find_bytype("services", "throttle_io_read");
1348 if(!st_throttle_io_read)
1349 st_throttle_io_read = rrdset_create("services", "throttle_io_read", NULL, "disk", "services.throttle_io_read", "Systemd Services Throttle Disk Read Bandwidth", "KB/s", 30016, update_every, RRDSET_TYPE_STACKED);
1351 else rrdset_next(st_throttle_io_read);
1353 if(unlikely(!st_throttle_io_write)) {
1354 st_throttle_io_write = rrdset_find_bytype("services", "throttle_io_write");
1355 if(!st_throttle_io_write)
1356 st_throttle_io_write = rrdset_create("services", "throttle_io_write", NULL, "disk", "services.throttle_io_write", "Systemd Services Throttle Disk Write Bandwidth", "KB/s", 30017, update_every, RRDSET_TYPE_STACKED);
1358 else rrdset_next(st_throttle_io_write);
1361 if(do_throttle_ops) {
1362 if(unlikely(!st_throttle_ops_read)) {
1363 st_throttle_ops_read = rrdset_find_bytype("services", "throttle_io_ops_read");
1364 if(!st_throttle_ops_read)
1365 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", 30018, update_every, RRDSET_TYPE_STACKED);
1367 else rrdset_next(st_throttle_ops_read);
1369 if(unlikely(!st_throttle_ops_write)) {
1370 st_throttle_ops_write = rrdset_find_bytype("services", "throttle_io_ops_write");
1371 if(!st_throttle_ops_write)
1372 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", 30019, update_every, RRDSET_TYPE_STACKED);
1374 else rrdset_next(st_throttle_ops_write);
1377 // update the values
1379 for(cg = cgroup_root; cg ; cg = cg->next) {
1380 if(!cg->available || !cg->enabled || !(cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE)) continue;
1382 if(do_cpu && cg->cpuacct_stat.updated) {
1383 if(unlikely(!cg->rd_cpu))
1384 cg->rd_cpu = rrddim_add(st_cpu, cg->chart_id, cg->chart_title, 100, hz, RRDDIM_INCREMENTAL);
1386 rrddim_set_by_pointer(st_cpu, cg->rd_cpu, cg->cpuacct_stat.user + cg->cpuacct_stat.system);
1389 if(do_mem_usage && cg->memory.updated) {
1390 if(unlikely(!cg->rd_mem_usage))
1391 cg->rd_mem_usage = rrddim_add(st_mem_usage, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1393 rrddim_set_by_pointer(st_mem_usage, cg->rd_mem_usage, cg->memory.usage_in_bytes);
1396 if(do_io && cg->io_service_bytes.updated) {
1397 if(unlikely(!cg->rd_io_service_bytes_read))
1398 cg->rd_io_service_bytes_read = rrddim_add(st_io_read, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1400 rrddim_set_by_pointer(st_io_read, cg->rd_io_service_bytes_read, cg->io_service_bytes.Read);
1402 if(unlikely(!cg->rd_io_service_bytes_write))
1403 cg->rd_io_service_bytes_write = rrddim_add(st_io_write, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1405 rrddim_set_by_pointer(st_io_write, cg->rd_io_service_bytes_write, cg->io_service_bytes.Write);
1408 if(do_io_ops && cg->io_serviced.updated) {
1409 if(unlikely(!cg->rd_io_serviced_read))
1410 cg->rd_io_serviced_read = rrddim_add(st_io_serviced_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1412 rrddim_set_by_pointer(st_io_serviced_read, cg->rd_io_serviced_read, cg->io_serviced.Read);
1414 if(unlikely(!cg->rd_io_serviced_write))
1415 cg->rd_io_serviced_write = rrddim_add(st_io_serviced_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1417 rrddim_set_by_pointer(st_io_serviced_write, cg->rd_io_serviced_write, cg->io_serviced.Write);
1420 if(do_throttle_io && cg->throttle_io_service_bytes.updated) {
1421 if(unlikely(!cg->rd_throttle_io_read))
1422 cg->rd_throttle_io_read = rrddim_add(st_throttle_io_read, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1424 rrddim_set_by_pointer(st_throttle_io_read, cg->rd_throttle_io_read, cg->throttle_io_service_bytes.Read);
1426 if(unlikely(!cg->rd_throttle_io_write))
1427 cg->rd_throttle_io_write = rrddim_add(st_throttle_io_write, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1429 rrddim_set_by_pointer(st_throttle_io_write, cg->rd_throttle_io_write, cg->throttle_io_service_bytes.Write);
1432 if(do_throttle_ops && cg->throttle_io_serviced.updated) {
1433 if(unlikely(!cg->rd_throttle_io_serviced_read))
1434 cg->rd_throttle_io_serviced_read = rrddim_add(st_throttle_ops_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1436 rrddim_set_by_pointer(st_throttle_ops_read, cg->rd_throttle_io_serviced_read, cg->throttle_io_serviced.Read);
1438 if(unlikely(!cg->rd_throttle_io_serviced_write))
1439 cg->rd_throttle_io_serviced_write = rrddim_add(st_throttle_ops_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1441 rrddim_set_by_pointer(st_throttle_ops_write, cg->rd_throttle_io_serviced_write, cg->throttle_io_serviced.Write);
1445 // complete the iteration
1446 if(do_cpu) rrdset_done(st_cpu);
1447 if(do_mem_usage) rrdset_done(st_mem_usage);
1448 if(do_io) { rrdset_done(st_io_read); rrdset_done(st_io_write); }
1449 if(do_io_ops) { rrdset_done(st_io_serviced_read); rrdset_done(st_io_serviced_write); }
1450 if(do_throttle_io) { rrdset_done(st_throttle_io_read); rrdset_done(st_throttle_io_write); }
1451 if(do_throttle_ops) { rrdset_done(st_throttle_ops_read); rrdset_done(st_throttle_ops_write); }
1454 static inline char *cgroup_chart_type(char *buffer, const char *id, size_t len) {
1455 if(buffer[0]) return buffer;
1457 if(id[0] == '\0' || (id[0] == '/' && id[1] == '\0'))
1458 strncpy(buffer, "cgroup_root", len);
1460 snprintfz(buffer, len, "cgroup_%s", id);
1462 netdata_fix_chart_id(buffer);
1466 void update_cgroup_charts(int update_every) {
1467 debug(D_CGROUP, "updating cgroups charts");
1469 char type[RRD_ID_LENGTH_MAX + 1];
1470 char title[CHART_TITLE_MAX + 1];
1472 int services_do_cpu = 0,
1473 services_do_mem_usage = 0,
1475 services_do_io_ops = 0,
1476 services_do_throttle_io = 0,
1477 services_do_throttle_ops = 0,
1478 services_do_queued_ops = 0,
1479 services_do_merged_ops = 0;
1482 for(cg = cgroup_root; cg ; cg = cg->next) {
1483 if(!cg->available || !cg->enabled) continue;
1485 if(cgroup_enable_systemd_services && cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE) {
1486 if(cg->cpuacct_stat.updated && (cg->cpuacct_stat.user || cg->cpuacct_stat.system)) services_do_cpu++;
1487 if(cg->memory.usage_in_bytes_updated && (cg->memory.usage_in_bytes)) services_do_mem_usage++;
1488 if(cg->io_service_bytes.updated && (cg->io_service_bytes.Read || cg->io_service_bytes.Write)) services_do_io++;
1489 if(cg->io_serviced.updated && (cg->io_serviced.Read || cg->io_serviced.Write)) services_do_io_ops++;
1490 if(cg->throttle_io_service_bytes.updated && (cg->throttle_io_service_bytes.Read || cg->throttle_io_service_bytes.Write)) services_do_throttle_io++;
1491 if(cg->throttle_io_serviced.updated && (cg->throttle_io_serviced.Read || cg->throttle_io_serviced.Write)) services_do_throttle_ops++;
1492 if(cg->io_queued.updated && (cg->io_queued.Read || cg->io_queued.Write)) services_do_queued_ops++;
1493 if(cg->io_merged.updated && (cg->io_merged.Read || cg->io_merged.Write)) services_do_merged_ops++;
1499 if(cg->cpuacct_stat.updated) {
1500 if(unlikely(!cg->st_cpu)) {
1501 cg->st_cpu = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "cpu");
1503 snprintfz(title, CHART_TITLE_MAX, "CPU Usage (%d%% = %d core%s) for cgroup %s", (processors * 100), processors, (processors > 1) ? "s" : "", cg->chart_title);
1504 cg->st_cpu = rrdset_create(type, "cpu", NULL, "cpu", "cgroup.cpu", title, "%", 40000, update_every, RRDSET_TYPE_STACKED);
1506 rrddim_add(cg->st_cpu, "user", NULL, 100, hz, RRDDIM_INCREMENTAL);
1507 rrddim_add(cg->st_cpu, "system", NULL, 100, hz, RRDDIM_INCREMENTAL);
1510 else rrdset_next(cg->st_cpu);
1512 rrddim_set(cg->st_cpu, "user", cg->cpuacct_stat.user);
1513 rrddim_set(cg->st_cpu, "system", cg->cpuacct_stat.system);
1514 rrdset_done(cg->st_cpu);
1517 if(cg->cpuacct_usage.updated) {
1518 char id[RRD_ID_LENGTH_MAX + 1];
1521 if(unlikely(!cg->st_cpu_per_core)) {
1522 cg->st_cpu_per_core = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "cpu_per_core");
1523 if(!cg->st_cpu_per_core) {
1524 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);
1525 cg->st_cpu_per_core = rrdset_create(type, "cpu_per_core", NULL, "cpu", "cgroup.cpu_per_core", title, "%", 40100, update_every, RRDSET_TYPE_STACKED);
1527 for(i = 0; i < cg->cpuacct_usage.cpus; i++) {
1528 snprintfz(id, CHART_TITLE_MAX, "cpu%u", i);
1529 rrddim_add(cg->st_cpu_per_core, id, NULL, 100, 1000000000, RRDDIM_INCREMENTAL);
1533 else rrdset_next(cg->st_cpu_per_core);
1535 for(i = 0; i < cg->cpuacct_usage.cpus ;i++) {
1536 snprintfz(id, CHART_TITLE_MAX, "cpu%u", i);
1537 rrddim_set(cg->st_cpu_per_core, id, cg->cpuacct_usage.cpu_percpu[i]);
1539 rrdset_done(cg->st_cpu_per_core);
1542 if(cg->memory.updated) {
1543 if(cg->memory.cache || cg->memory.rss || cg->memory.rss_huge || cg->memory.mapped_file) {
1544 if(unlikely(!cg->st_mem)) {
1545 cg->st_mem = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem");
1547 snprintfz(title, CHART_TITLE_MAX, "Memory Usage for cgroup %s", cg->chart_title);
1548 cg->st_mem = rrdset_create(type, "mem", NULL, "mem", "cgroup.mem", title, "MB", 40210, update_every, RRDSET_TYPE_STACKED);
1550 rrddim_add(cg->st_mem, "cache", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1551 rrddim_add(cg->st_mem, "rss", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1552 if(cg->memory.has_dirty_swap)
1553 rrddim_add(cg->st_mem, "swap", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1554 rrddim_add(cg->st_mem, "rss_huge", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1555 rrddim_add(cg->st_mem, "mapped_file", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1558 else rrdset_next(cg->st_mem);
1560 rrddim_set(cg->st_mem, "cache", cg->memory.cache);
1561 rrddim_set(cg->st_mem, "rss", cg->memory.rss);
1562 if(cg->memory.has_dirty_swap)
1563 rrddim_set(cg->st_mem, "swap", cg->memory.swap);
1564 rrddim_set(cg->st_mem, "rss_huge", cg->memory.rss_huge);
1565 rrddim_set(cg->st_mem, "mapped_file", cg->memory.mapped_file);
1566 rrdset_done(cg->st_mem);
1569 if(unlikely(!cg->st_writeback)) {
1570 cg->st_writeback = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "writeback");
1571 if(!cg->st_writeback) {
1572 snprintfz(title, CHART_TITLE_MAX, "Writeback Memory for cgroup %s", cg->chart_title);
1573 cg->st_writeback = rrdset_create(type, "writeback", NULL, "mem", "cgroup.writeback", title, "MB", 40300, update_every, RRDSET_TYPE_AREA);
1575 if(cg->memory.has_dirty_swap)
1576 rrddim_add(cg->st_writeback, "dirty", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1577 rrddim_add(cg->st_writeback, "writeback", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1580 else rrdset_next(cg->st_writeback);
1582 if(cg->memory.has_dirty_swap)
1583 rrddim_set(cg->st_writeback, "dirty", cg->memory.dirty);
1584 rrddim_set(cg->st_writeback, "writeback", cg->memory.writeback);
1585 rrdset_done(cg->st_writeback);
1587 if(cg->memory.pgpgin || cg->memory.pgpgout) {
1588 if(unlikely(!cg->st_mem_activity)) {
1589 cg->st_mem_activity = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_activity");
1590 if(!cg->st_mem_activity) {
1591 snprintfz(title, CHART_TITLE_MAX, "Memory Activity for cgroup %s", cg->chart_title);
1592 cg->st_mem_activity = rrdset_create(type, "mem_activity", NULL, "mem", "cgroup.mem_activity", title, "MB/s", 40400, update_every, RRDSET_TYPE_LINE);
1594 rrddim_add(cg->st_mem_activity, "pgpgin", "in", sysconf(_SC_PAGESIZE), 1024 * 1024, RRDDIM_INCREMENTAL);
1595 rrddim_add(cg->st_mem_activity, "pgpgout", "out", -sysconf(_SC_PAGESIZE), 1024 * 1024, RRDDIM_INCREMENTAL);
1598 else rrdset_next(cg->st_mem_activity);
1600 rrddim_set(cg->st_mem_activity, "pgpgin", cg->memory.pgpgin);
1601 rrddim_set(cg->st_mem_activity, "pgpgout", cg->memory.pgpgout);
1602 rrdset_done(cg->st_mem_activity);
1605 if(cg->memory.pgfault || cg->memory.pgmajfault) {
1606 if(unlikely(!cg->st_pgfaults)) {
1607 cg->st_pgfaults = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "pgfaults");
1608 if(!cg->st_pgfaults) {
1609 snprintfz(title, CHART_TITLE_MAX, "Memory Page Faults for cgroup %s", cg->chart_title);
1610 cg->st_pgfaults = rrdset_create(type, "pgfaults", NULL, "mem", "cgroup.pgfaults", title, "MB/s", 40500, update_every, RRDSET_TYPE_LINE);
1612 rrddim_add(cg->st_pgfaults, "pgfault", NULL, sysconf(_SC_PAGESIZE), 1024 * 1024, RRDDIM_INCREMENTAL);
1613 rrddim_add(cg->st_pgfaults, "pgmajfault", "swap", -sysconf(_SC_PAGESIZE), 1024 * 1024, RRDDIM_INCREMENTAL);
1616 else rrdset_next(cg->st_pgfaults);
1618 rrddim_set(cg->st_pgfaults, "pgfault", cg->memory.pgfault);
1619 rrddim_set(cg->st_pgfaults, "pgmajfault", cg->memory.pgmajfault);
1620 rrdset_done(cg->st_pgfaults);
1624 if(cg->memory.usage_in_bytes_updated) {
1625 if(unlikely(!cg->st_mem_usage)) {
1626 cg->st_mem_usage = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_usage");
1627 if(!cg->st_mem_usage) {
1628 snprintfz(title, CHART_TITLE_MAX, "Total Memory for cgroup %s", cg->chart_title);
1629 cg->st_mem_usage = rrdset_create(type, "mem_usage", NULL, "mem", "cgroup.mem_usage", title, "MB", 40200, update_every, RRDSET_TYPE_STACKED);
1631 rrddim_add(cg->st_mem_usage, "ram", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1632 rrddim_add(cg->st_mem_usage, "swap", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1635 else rrdset_next(cg->st_mem_usage);
1637 rrddim_set(cg->st_mem_usage, "ram", cg->memory.usage_in_bytes);
1638 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);
1639 rrdset_done(cg->st_mem_usage);
1642 if(cg->memory.failcnt_updated && cg->memory.failcnt) {
1643 if(unlikely(!cg->st_mem_failcnt)) {
1644 cg->st_mem_failcnt = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_failcnt");
1645 if(!cg->st_mem_failcnt) {
1646 snprintfz(title, CHART_TITLE_MAX, "Memory Limit Failures for cgroup %s", cg->chart_title);
1647 cg->st_mem_failcnt = rrdset_create(type, "mem_failcnt", NULL, "mem", "cgroup.mem_failcnt", title, "MB", 40250, update_every, RRDSET_TYPE_LINE);
1649 rrddim_add(cg->st_mem_failcnt, "failures", NULL, 1, 1, RRDDIM_INCREMENTAL);
1652 else rrdset_next(cg->st_mem_failcnt);
1654 rrddim_set(cg->st_mem_failcnt, "failures", cg->memory.failcnt);
1655 rrdset_done(cg->st_mem_failcnt);
1658 if(cg->io_service_bytes.updated && (cg->io_service_bytes.Read || cg->io_service_bytes.Write)) {
1659 if(unlikely(!cg->st_io)) {
1660 cg->st_io = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "io");
1662 snprintfz(title, CHART_TITLE_MAX, "I/O Bandwidth (all disks) for cgroup %s", cg->chart_title);
1663 cg->st_io = rrdset_create(type, "io", NULL, "disk", "cgroup.io", title, "KB/s", 41200, update_every, RRDSET_TYPE_AREA);
1665 rrddim_add(cg->st_io, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
1666 rrddim_add(cg->st_io, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
1669 else rrdset_next(cg->st_io);
1671 rrddim_set(cg->st_io, "read", cg->io_service_bytes.Read);
1672 rrddim_set(cg->st_io, "write", cg->io_service_bytes.Write);
1673 rrdset_done(cg->st_io);
1676 if(cg->io_serviced.updated && (cg->io_serviced.Read || cg->io_serviced.Write)) {
1677 if(unlikely(!cg->st_serviced_ops)) {
1678 cg->st_serviced_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "serviced_ops");
1679 if(!cg->st_serviced_ops) {
1680 snprintfz(title, CHART_TITLE_MAX, "Serviced I/O Operations (all disks) for cgroup %s"
1682 cg->st_serviced_ops = rrdset_create(type, "serviced_ops", NULL, "disk", "cgroup.serviced_ops", title, "operations/s", 41200, update_every, RRDSET_TYPE_LINE);
1684 rrddim_add(cg->st_serviced_ops, "read", NULL, 1, 1, RRDDIM_INCREMENTAL);
1685 rrddim_add(cg->st_serviced_ops, "write", NULL, -1, 1, RRDDIM_INCREMENTAL);
1688 else rrdset_next(cg->st_serviced_ops);
1690 rrddim_set(cg->st_serviced_ops, "read", cg->io_serviced.Read);
1691 rrddim_set(cg->st_serviced_ops, "write", cg->io_serviced.Write);
1692 rrdset_done(cg->st_serviced_ops);
1695 if(cg->throttle_io_service_bytes.updated && (cg->throttle_io_service_bytes.Read || cg->throttle_io_service_bytes.Write)) {
1696 if(unlikely(!cg->st_throttle_io)) {
1697 cg->st_throttle_io = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "throttle_io");
1698 if(!cg->st_throttle_io) {
1699 snprintfz(title, CHART_TITLE_MAX, "Throttle I/O Bandwidth (all disks) for cgroup %s", cg->chart_title);
1700 cg->st_throttle_io = rrdset_create(type, "throttle_io", NULL, "disk", "cgroup.throttle_io", title, "KB/s", 41200, update_every, RRDSET_TYPE_AREA);
1702 rrddim_add(cg->st_throttle_io, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
1703 rrddim_add(cg->st_throttle_io, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
1706 else rrdset_next(cg->st_throttle_io);
1708 rrddim_set(cg->st_throttle_io, "read", cg->throttle_io_service_bytes.Read);
1709 rrddim_set(cg->st_throttle_io, "write", cg->throttle_io_service_bytes.Write);
1710 rrdset_done(cg->st_throttle_io);
1713 if(cg->throttle_io_serviced.updated && (cg->throttle_io_serviced.Read || cg->throttle_io_serviced.Write)) {
1714 if(unlikely(!cg->st_throttle_serviced_ops)) {
1715 cg->st_throttle_serviced_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "throttle_serviced_ops");
1716 if(!cg->st_throttle_serviced_ops) {
1717 snprintfz(title, CHART_TITLE_MAX, "Throttle Serviced I/O Operations (all disks) for cgroup %s", cg->chart_title);
1718 cg->st_throttle_serviced_ops = rrdset_create(type, "throttle_serviced_ops", NULL, "disk", "cgroup.throttle_serviced_ops", title, "operations/s", 41200, update_every, RRDSET_TYPE_LINE);
1720 rrddim_add(cg->st_throttle_serviced_ops, "read", NULL, 1, 1, RRDDIM_INCREMENTAL);
1721 rrddim_add(cg->st_throttle_serviced_ops, "write", NULL, -1, 1, RRDDIM_INCREMENTAL);
1724 else rrdset_next(cg->st_throttle_serviced_ops);
1726 rrddim_set(cg->st_throttle_serviced_ops, "read", cg->throttle_io_serviced.Read);
1727 rrddim_set(cg->st_throttle_serviced_ops, "write", cg->throttle_io_serviced.Write);
1728 rrdset_done(cg->st_throttle_serviced_ops);
1731 if(cg->io_queued.updated) {
1732 if(unlikely(!cg->st_queued_ops)) {
1733 cg->st_queued_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "queued_ops");
1734 if(!cg->st_queued_ops) {
1735 snprintfz(title, CHART_TITLE_MAX, "Queued I/O Operations (all disks) for cgroup %s", cg->chart_title);
1736 cg->st_queued_ops = rrdset_create(type, "queued_ops", NULL, "disk", "cgroup.queued_ops", title, "operations", 42000, update_every, RRDSET_TYPE_LINE);
1738 rrddim_add(cg->st_queued_ops, "read", NULL, 1, 1, RRDDIM_ABSOLUTE);
1739 rrddim_add(cg->st_queued_ops, "write", NULL, -1, 1, RRDDIM_ABSOLUTE);
1742 else rrdset_next(cg->st_queued_ops);
1744 rrddim_set(cg->st_queued_ops, "read", cg->io_queued.Read);
1745 rrddim_set(cg->st_queued_ops, "write", cg->io_queued.Write);
1746 rrdset_done(cg->st_queued_ops);
1749 if(cg->io_merged.updated && (cg->io_merged.Read || cg->io_merged.Write)) {
1750 if(unlikely(!cg->st_merged_ops)) {
1751 cg->st_merged_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "merged_ops");
1752 if(!cg->st_merged_ops) {
1753 snprintfz(title, CHART_TITLE_MAX, "Merged I/O Operations (all disks) for cgroup %s", cg->chart_title);
1754 cg->st_merged_ops = rrdset_create(type, "merged_ops", NULL, "disk", "cgroup.merged_ops", title, "operations/s", 42100, update_every, RRDSET_TYPE_LINE);
1756 rrddim_add(cg->st_merged_ops, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
1757 rrddim_add(cg->st_merged_ops, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
1760 else rrdset_next(cg->st_merged_ops);
1762 rrddim_set(cg->st_merged_ops, "read", cg->io_merged.Read);
1763 rrddim_set(cg->st_merged_ops, "write", cg->io_merged.Write);
1764 rrdset_done(cg->st_merged_ops);
1768 debug(D_CGROUP, "done updating cgroups charts");
1770 if(cgroup_enable_systemd_services)
1771 update_services_charts(update_every,
1773 services_do_mem_usage,
1776 services_do_throttle_io,
1777 services_do_throttle_ops/*,
1778 services_do_queued_ops,
1779 services_do_merged_ops*/
1783 // ----------------------------------------------------------------------------
1786 void *cgroups_main(void *ptr) {
1787 struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
1789 info("CGROUP Plugin thread created with task id %d", gettid());
1791 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
1792 error("Cannot set pthread cancel type to DEFERRED.");
1794 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
1795 error("Cannot set pthread cancel state to ENABLE.");
1797 struct rusage thread;
1799 // when ZERO, attempt to do it
1800 int vdo_cpu_netdata = config_get_boolean("plugin:cgroups", "cgroups plugin resource charts", 1);
1802 read_cgroup_plugin_configuration();
1804 RRDSET *stcpu_thread = NULL;
1806 usec_t step = cgroup_update_every * USEC_PER_SEC;
1807 usec_t find_every = cgroup_check_for_new_every * USEC_PER_SEC, find_next = 0;
1809 usec_t now = now_monotonic_usec();
1810 usec_t next = now - (now % step) + step;
1813 sleep_usec(next - now);
1814 now = now_monotonic_usec();
1817 if(unlikely(netdata_exit)) break;
1819 // BEGIN -- the job to be done
1821 if(unlikely(now >= find_next)) {
1823 find_next = now + find_every;
1826 read_all_cgroups(cgroup_root);
1827 update_cgroup_charts(cgroup_update_every);
1829 // END -- the job is done
1831 // --------------------------------------------------------------------
1833 if(vdo_cpu_netdata) {
1834 getrusage(RUSAGE_THREAD, &thread);
1836 if(!stcpu_thread) stcpu_thread = rrdset_find("netdata.plugin_cgroups_cpu");
1838 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);
1840 rrddim_add(stcpu_thread, "user", NULL, 1, 1000, RRDDIM_INCREMENTAL);
1841 rrddim_add(stcpu_thread, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
1843 else rrdset_next(stcpu_thread);
1845 rrddim_set(stcpu_thread, "user" , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
1846 rrddim_set(stcpu_thread, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
1847 rrdset_done(stcpu_thread);
1851 info("CGROUP thread exiting");
1853 static_thread->enabled = 0;