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;
24 static int cgroup_enable_systemd_services = CONFIG_ONDEMAND_YES;
25 static int cgroup_enable_systemd_services_detailed_memory = CONFIG_ONDEMAND_NO;
26 static int cgroup_used_memory_without_cache = CONFIG_ONDEMAND_YES;
28 static int cgroup_search_in_devices = 1;
30 static int cgroup_enable_new_cgroups_detected_at_runtime = 1;
31 static int cgroup_check_for_new_every = 10;
32 static int cgroup_update_every = 1;
34 static int cgroup_recheck_zero_blkio_every_iterations = 10;
35 static int cgroup_recheck_zero_mem_failcnt_every_iterations = 10;
36 static int cgroup_recheck_zero_mem_detailed_every_iterations = 10;
38 static char *cgroup_cpuacct_base = NULL;
39 static char *cgroup_blkio_base = NULL;
40 static char *cgroup_memory_base = NULL;
41 static char *cgroup_devices_base = NULL;
43 static int cgroup_root_count = 0;
44 static int cgroup_root_max = 500;
45 static int cgroup_max_depth = 0;
47 static SIMPLE_PATTERN *enabled_cgroup_patterns = NULL;
48 static SIMPLE_PATTERN *enabled_cgroup_paths = NULL;
49 static SIMPLE_PATTERN *enabled_cgroup_renames = NULL;
50 static SIMPLE_PATTERN *systemd_services_cgroups = NULL;
52 static char *cgroups_rename_script = PLUGINS_DIR "/cgroup-name.sh";
54 static uint32_t Read_hash = 0;
55 static uint32_t Write_hash = 0;
56 static uint32_t Sync_hash = 0;
57 static uint32_t Async_hash = 0;
58 static uint32_t Total_hash = 0;
59 static uint32_t user_hash = 0;
60 static uint32_t system_hash = 0;
61 static uint32_t cache_hash = 0;
62 static uint32_t rss_hash = 0;
63 static uint32_t rss_huge_hash = 0;
64 static uint32_t mapped_file_hash = 0;
65 static uint32_t writeback_hash = 0;
66 static uint32_t dirty_hash = 0;
67 static uint32_t swap_hash = 0;
68 static uint32_t pgpgin_hash = 0;
69 static uint32_t pgpgout_hash = 0;
70 static uint32_t pgfault_hash = 0;
71 static uint32_t pgmajfault_hash = 0;
72 static uint32_t inactive_anon_hash = 0;
73 static uint32_t active_anon_hash = 0;
74 static uint32_t inactive_file_hash = 0;
75 static uint32_t active_file_hash = 0;
76 static uint32_t unevictable_hash = 0;
77 static uint32_t hierarchical_memory_limit_hash = 0;
78 static uint32_t total_cache_hash = 0;
79 static uint32_t total_rss_hash = 0;
80 static uint32_t total_rss_huge_hash = 0;
81 static uint32_t total_mapped_file_hash = 0;
82 static uint32_t total_writeback_hash = 0;
83 static uint32_t total_dirty_hash = 0;
84 static uint32_t total_swap_hash = 0;
85 static uint32_t total_pgpgin_hash = 0;
86 static uint32_t total_pgpgout_hash = 0;
87 static uint32_t total_pgfault_hash = 0;
88 static uint32_t total_pgmajfault_hash = 0;
89 static uint32_t total_inactive_anon_hash = 0;
90 static uint32_t total_active_anon_hash = 0;
91 static uint32_t total_inactive_file_hash = 0;
92 static uint32_t total_active_file_hash = 0;
93 static uint32_t total_unevictable_hash = 0;
95 void read_cgroup_plugin_configuration() {
96 system_page_size = sysconf(_SC_PAGESIZE);
98 Read_hash = simple_hash("Read");
99 Write_hash = simple_hash("Write");
100 Sync_hash = simple_hash("Sync");
101 Async_hash = simple_hash("Async");
102 Total_hash = simple_hash("Total");
103 user_hash = simple_hash("user");
104 system_hash = simple_hash("system");
105 cache_hash = simple_hash("cache");
106 rss_hash = simple_hash("rss");
107 rss_huge_hash = simple_hash("rss_huge");
108 mapped_file_hash = simple_hash("mapped_file");
109 writeback_hash = simple_hash("writeback");
110 dirty_hash = simple_hash("dirty");
111 swap_hash = simple_hash("swap");
112 pgpgin_hash = simple_hash("pgpgin");
113 pgpgout_hash = simple_hash("pgpgout");
114 pgfault_hash = simple_hash("pgfault");
115 pgmajfault_hash = simple_hash("pgmajfault");
116 inactive_anon_hash = simple_hash("inactive_anon");
117 active_anon_hash = simple_hash("active_anon");
118 inactive_file_hash = simple_hash("inactive_file");
119 active_file_hash = simple_hash("active_file");
120 unevictable_hash = simple_hash("unevictable");
121 hierarchical_memory_limit_hash = simple_hash("hierarchical_memory_limit");
122 total_cache_hash = simple_hash("total_cache");
123 total_rss_hash = simple_hash("total_rss");
124 total_rss_huge_hash = simple_hash("total_rss_huge");
125 total_mapped_file_hash = simple_hash("total_mapped_file");
126 total_writeback_hash = simple_hash("total_writeback");
127 total_dirty_hash = simple_hash("total_dirty");
128 total_swap_hash = simple_hash("total_swap");
129 total_pgpgin_hash = simple_hash("total_pgpgin");
130 total_pgpgout_hash = simple_hash("total_pgpgout");
131 total_pgfault_hash = simple_hash("total_pgfault");
132 total_pgmajfault_hash = simple_hash("total_pgmajfault");
133 total_inactive_anon_hash = simple_hash("total_inactive_anon");
134 total_active_anon_hash = simple_hash("total_active_anon");
135 total_inactive_file_hash = simple_hash("total_inactive_file");
136 total_active_file_hash = simple_hash("total_active_file");
137 total_unevictable_hash = simple_hash("total_unevictable");
139 cgroup_update_every = (int)config_get_number("plugin:cgroups", "update every", rrd_update_every);
140 if(cgroup_update_every < rrd_update_every)
141 cgroup_update_every = rrd_update_every;
143 cgroup_check_for_new_every = (int)config_get_number("plugin:cgroups", "check for new cgroups every", cgroup_check_for_new_every * cgroup_update_every);
144 if(cgroup_check_for_new_every < cgroup_update_every)
145 cgroup_check_for_new_every = cgroup_update_every;
147 cgroup_enable_cpuacct_stat = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct stat (total CPU)", cgroup_enable_cpuacct_stat);
148 cgroup_enable_cpuacct_usage = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct usage (per core CPU)", cgroup_enable_cpuacct_usage);
150 cgroup_enable_memory = config_get_boolean_ondemand("plugin:cgroups", "enable memory (used mem including cache)", cgroup_enable_memory);
151 cgroup_enable_detailed_memory = config_get_boolean_ondemand("plugin:cgroups", "enable detailed memory", cgroup_enable_detailed_memory);
152 cgroup_enable_memory_failcnt = config_get_boolean_ondemand("plugin:cgroups", "enable memory limits fail count", cgroup_enable_memory_failcnt);
153 cgroup_enable_swap = config_get_boolean_ondemand("plugin:cgroups", "enable swap memory", cgroup_enable_swap);
155 cgroup_enable_blkio_io = config_get_boolean_ondemand("plugin:cgroups", "enable blkio bandwidth", cgroup_enable_blkio_io);
156 cgroup_enable_blkio_ops = config_get_boolean_ondemand("plugin:cgroups", "enable blkio operations", cgroup_enable_blkio_ops);
157 cgroup_enable_blkio_throttle_io = config_get_boolean_ondemand("plugin:cgroups", "enable blkio throttle bandwidth", cgroup_enable_blkio_throttle_io);
158 cgroup_enable_blkio_throttle_ops = config_get_boolean_ondemand("plugin:cgroups", "enable blkio throttle operations", cgroup_enable_blkio_throttle_ops);
159 cgroup_enable_blkio_queued_ops = config_get_boolean_ondemand("plugin:cgroups", "enable blkio queued operations", cgroup_enable_blkio_queued_ops);
160 cgroup_enable_blkio_merged_ops = config_get_boolean_ondemand("plugin:cgroups", "enable blkio merged operations", cgroup_enable_blkio_merged_ops);
162 cgroup_recheck_zero_blkio_every_iterations = (int)config_get_number("plugin:cgroups", "recheck zero blkio every iterations", cgroup_recheck_zero_blkio_every_iterations);
163 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);
164 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);
166 cgroup_enable_systemd_services = config_get_boolean("plugin:cgroups", "enable systemd services", cgroup_enable_systemd_services);
167 cgroup_enable_systemd_services_detailed_memory = config_get_boolean("plugin:cgroups", "enable systemd services detailed memory", cgroup_enable_systemd_services_detailed_memory);
168 cgroup_used_memory_without_cache = config_get_boolean("plugin:cgroups", "report used memory without cache", cgroup_used_memory_without_cache);
170 char filename[FILENAME_MAX + 1], *s;
171 struct mountinfo *mi, *root = mountinfo_read(0);
173 mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "cpuacct");
174 if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "cpuacct");
176 error("Cannot find cgroup cpuacct mountinfo. Assuming default: /sys/fs/cgroup/cpuacct");
177 s = "/sys/fs/cgroup/cpuacct";
179 else s = mi->mount_point;
180 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
181 cgroup_cpuacct_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/cpuacct", filename);
183 mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "blkio");
184 if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "blkio");
186 error("Cannot find cgroup blkio mountinfo. Assuming default: /sys/fs/cgroup/blkio");
187 s = "/sys/fs/cgroup/blkio";
189 else s = mi->mount_point;
190 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
191 cgroup_blkio_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/blkio", filename);
193 mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "memory");
194 if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "memory");
196 error("Cannot find cgroup memory mountinfo. Assuming default: /sys/fs/cgroup/memory");
197 s = "/sys/fs/cgroup/memory";
199 else s = mi->mount_point;
200 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
201 cgroup_memory_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/memory", filename);
203 mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "devices");
204 if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "devices");
206 error("Cannot find cgroup devices mountinfo. Assuming default: /sys/fs/cgroup/devices");
207 s = "/sys/fs/cgroup/devices";
209 else s = mi->mount_point;
210 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
211 cgroup_devices_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/devices", filename);
213 cgroup_root_max = (int)config_get_number("plugin:cgroups", "max cgroups to allow", cgroup_root_max);
214 cgroup_max_depth = (int)config_get_number("plugin:cgroups", "max cgroups depth to monitor", cgroup_max_depth);
216 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);
218 enabled_cgroup_patterns = simple_pattern_create(
219 config_get("plugin:cgroups", "enable by default cgroups matching",
231 " !/lxc/*/ns " // #1397
237 " * " // enable anything else
238 ), SIMPLE_PATTERN_EXACT);
240 enabled_cgroup_paths = simple_pattern_create(
241 config_get("plugin:cgroups", "search for cgroups in subpaths matching",
249 ), SIMPLE_PATTERN_EXACT);
251 cgroups_rename_script = config_get("plugin:cgroups", "script to get cgroup names", cgroups_rename_script);
253 enabled_cgroup_renames = simple_pattern_create(
254 config_get("plugin:cgroups", "run script to rename cgroups matching",
264 ), SIMPLE_PATTERN_EXACT);
266 if(cgroup_enable_systemd_services) {
267 systemd_services_cgroups = simple_pattern_create(
268 config_get("plugin:cgroups", "cgroups to match as systemd services",
269 " !/system.slice/*/*.service "
270 " /system.slice/*.service "
271 ), SIMPLE_PATTERN_EXACT);
274 mountinfo_free(root);
277 // ----------------------------------------------------------------------------
282 int enabled; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
287 unsigned long long Read;
288 unsigned long long Write;
290 unsigned long long Sync;
291 unsigned long long Async;
292 unsigned long long Total;
296 // https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt
298 int updated_detailed;
299 int updated_usage_in_bytes;
300 int updated_msw_usage_in_bytes;
303 int enabled_detailed; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
304 int enabled_usage_in_bytes; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
305 int enabled_msw_usage_in_bytes; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
306 int enabled_failcnt; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
308 int delay_counter_detailed;
309 int delay_counter_failcnt;
311 char *filename_detailed;
312 char *filename_usage_in_bytes;
313 char *filename_msw_usage_in_bytes;
314 char *filename_failcnt;
316 int detailed_has_dirty;
317 int detailed_has_swap;
320 unsigned long long cache;
321 unsigned long long rss;
322 unsigned long long rss_huge;
323 unsigned long long mapped_file;
324 unsigned long long writeback;
325 unsigned long long dirty;
326 unsigned long long swap;
327 unsigned long long pgpgin;
328 unsigned long long pgpgout;
329 unsigned long long pgfault;
330 unsigned long long pgmajfault;
332 unsigned long long inactive_anon;
333 unsigned long long active_anon;
334 unsigned long long inactive_file;
335 unsigned long long active_file;
336 unsigned long long unevictable;
337 unsigned long long hierarchical_memory_limit;
338 unsigned long long total_cache;
339 unsigned long long total_rss;
340 unsigned long long total_rss_huge;
341 unsigned long long total_mapped_file;
342 unsigned long long total_writeback;
343 unsigned long long total_dirty;
344 unsigned long long total_swap;
345 unsigned long long total_pgpgin;
346 unsigned long long total_pgpgout;
347 unsigned long long total_pgfault;
348 unsigned long long total_pgmajfault;
349 unsigned long long total_inactive_anon;
350 unsigned long long total_active_anon;
351 unsigned long long total_inactive_file;
352 unsigned long long total_active_file;
353 unsigned long long total_unevictable;
356 // single file metrics
357 unsigned long long usage_in_bytes;
358 unsigned long long msw_usage_in_bytes;
359 unsigned long long failcnt;
362 // https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt
363 struct cpuacct_stat {
365 int enabled; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
369 unsigned long long user;
370 unsigned long long system;
373 // https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt
374 struct cpuacct_usage {
376 int enabled; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
381 unsigned long long *cpu_percpu;
384 #define CGROUP_OPTIONS_DISABLED_DUPLICATE 0x00000001
385 #define CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE 0x00000002
390 char available; // found in the filesystem
391 char enabled; // enabled in the config
401 struct cpuacct_stat cpuacct_stat;
402 struct cpuacct_usage cpuacct_usage;
404 struct memory memory;
406 struct blkio io_service_bytes; // bytes
407 struct blkio io_serviced; // operations
409 struct blkio throttle_io_service_bytes; // bytes
410 struct blkio throttle_io_serviced; // operations
412 struct blkio io_merged; // operations
413 struct blkio io_queued; // operations
417 RRDSET *st_cpu_per_core;
419 RRDSET *st_writeback;
420 RRDSET *st_mem_activity;
422 RRDSET *st_mem_usage;
423 RRDSET *st_mem_failcnt;
425 RRDSET *st_serviced_ops;
426 RRDSET *st_throttle_io;
427 RRDSET *st_throttle_serviced_ops;
428 RRDSET *st_queued_ops;
429 RRDSET *st_merged_ops;
433 RRDDIM *rd_mem_usage;
434 RRDDIM *rd_mem_failcnt;
435 RRDDIM *rd_swap_usage;
437 RRDDIM *rd_mem_detailed_cache;
438 RRDDIM *rd_mem_detailed_rss;
439 RRDDIM *rd_mem_detailed_mapped;
440 RRDDIM *rd_mem_detailed_writeback;
441 RRDDIM *rd_mem_detailed_dirty;
442 RRDDIM *rd_mem_detailed_swap;
443 RRDDIM *rd_mem_detailed_pgpgin;
444 RRDDIM *rd_mem_detailed_pgpgout;
445 RRDDIM *rd_mem_detailed_pgfault;
446 RRDDIM *rd_mem_detailed_pgmajfault;
448 RRDDIM *rd_io_service_bytes_read;
449 RRDDIM *rd_io_serviced_read;
450 RRDDIM *rd_throttle_io_read;
451 RRDDIM *rd_throttle_io_serviced_read;
452 RRDDIM *rd_io_queued_read;
453 RRDDIM *rd_io_merged_read;
455 RRDDIM *rd_io_service_bytes_write;
456 RRDDIM *rd_io_serviced_write;
457 RRDDIM *rd_throttle_io_write;
458 RRDDIM *rd_throttle_io_serviced_write;
459 RRDDIM *rd_io_queued_write;
460 RRDDIM *rd_io_merged_write;
464 } *cgroup_root = NULL;
466 // ----------------------------------------------------------------------------
467 // read values from /sys
469 static inline void cgroup_read_cpuacct_stat(struct cpuacct_stat *cp) {
470 static procfile *ff = NULL;
472 if(likely(cp->filename)) {
473 ff = procfile_reopen(ff, cp->filename, NULL, PROCFILE_FLAG_DEFAULT);
479 ff = procfile_readall(ff);
485 unsigned long i, lines = procfile_lines(ff);
487 if(unlikely(lines < 1)) {
488 error("File '%s' should have 1+ lines.", cp->filename);
493 for(i = 0; i < lines ; i++) {
494 char *s = procfile_lineword(ff, i, 0);
495 uint32_t hash = simple_hash(s);
497 if(unlikely(hash == user_hash && !strcmp(s, "user")))
498 cp->user = str2ull(procfile_lineword(ff, i, 1));
500 else if(unlikely(hash == system_hash && !strcmp(s, "system")))
501 cp->system = str2ull(procfile_lineword(ff, i, 1));
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 = str2ull(procfile_lineword(ff, 0, i));
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 += str2ull(procfile_lineword(ff, i, 2));
610 else if(unlikely(hash == Write_hash && !strcmp(s, "Write")))
611 io->Write += str2ull(procfile_lineword(ff, i, 2));
614 else if(unlikely(hash == Sync_hash && !strcmp(s, "Sync")))
615 io->Sync += str2ull(procfile_lineword(ff, i, 2));
617 else if(unlikely(hash == Async_hash && !strcmp(s, "Async")))
618 io->Async += str2ull(procfile_lineword(ff, i, 2));
620 else if(unlikely(hash == Total_hash && !strcmp(s, "Total")))
621 io->Total += str2ull(procfile_lineword(ff, i, 2));
627 if(unlikely(io->enabled == CONFIG_ONDEMAND_ONDEMAND)) {
628 if(unlikely(io->Read || io->Write))
629 io->enabled = CONFIG_ONDEMAND_YES;
631 io->delay_counter = cgroup_recheck_zero_blkio_every_iterations;
636 static inline void cgroup_read_memory(struct memory *mem) {
637 static procfile *ff = NULL;
639 // read detailed ram usage
640 if(likely(mem->filename_detailed)) {
641 if(unlikely(mem->enabled_detailed == CONFIG_ONDEMAND_ONDEMAND && mem->delay_counter_detailed > 0)) {
642 mem->delay_counter_detailed--;
646 ff = procfile_reopen(ff, mem->filename_detailed, NULL, PROCFILE_FLAG_DEFAULT);
648 mem->updated_detailed = 0;
652 ff = procfile_readall(ff);
654 mem->updated_detailed = 0;
658 unsigned long i, lines = procfile_lines(ff);
660 if(unlikely(lines < 1)) {
661 error("File '%s' should have 1+ lines.", mem->filename_detailed);
662 mem->updated_detailed = 0;
666 for(i = 0; i < lines ; i++) {
667 char *s = procfile_lineword(ff, i, 0);
668 uint32_t hash = simple_hash(s);
670 if(unlikely(hash == cache_hash && !strcmp(s, "cache")))
671 mem->cache = str2ull(procfile_lineword(ff, i, 1));
673 else if(unlikely(hash == rss_hash && !strcmp(s, "rss")))
674 mem->rss = str2ull(procfile_lineword(ff, i, 1));
676 else if(unlikely(hash == rss_huge_hash && !strcmp(s, "rss_huge")))
677 mem->rss_huge = str2ull(procfile_lineword(ff, i, 1));
679 else if(unlikely(hash == mapped_file_hash && !strcmp(s, "mapped_file")))
680 mem->mapped_file = str2ull(procfile_lineword(ff, i, 1));
682 else if(unlikely(hash == writeback_hash && !strcmp(s, "writeback")))
683 mem->writeback = str2ull(procfile_lineword(ff, i, 1));
685 else if(unlikely(hash == dirty_hash && !strcmp(s, "dirty"))) {
686 mem->dirty = str2ull(procfile_lineword(ff, i, 1));
687 mem->detailed_has_dirty = 1;
690 else if(unlikely(hash == swap_hash && !strcmp(s, "swap"))) {
691 mem->swap = str2ull(procfile_lineword(ff, i, 1));
692 mem->detailed_has_swap = 1;
695 else if(unlikely(hash == pgpgin_hash && !strcmp(s, "pgpgin")))
696 mem->pgpgin = str2ull(procfile_lineword(ff, i, 1));
698 else if(unlikely(hash == pgpgout_hash && !strcmp(s, "pgpgout")))
699 mem->pgpgout = str2ull(procfile_lineword(ff, i, 1));
701 else if(unlikely(hash == pgfault_hash && !strcmp(s, "pgfault")))
702 mem->pgfault = str2ull(procfile_lineword(ff, i, 1));
704 else if(unlikely(hash == pgmajfault_hash && !strcmp(s, "pgmajfault")))
705 mem->pgmajfault = str2ull(procfile_lineword(ff, i, 1));
708 else if(unlikely(hash == inactive_anon_hash && !strcmp(s, "inactive_anon")))
709 mem->inactive_anon = str2ull(procfile_lineword(ff, i, 1));
711 else if(unlikely(hash == active_anon_hash && !strcmp(s, "active_anon")))
712 mem->active_anon = str2ull(procfile_lineword(ff, i, 1));
714 else if(unlikely(hash == inactive_file_hash && !strcmp(s, "inactive_file")))
715 mem->inactive_file = str2ull(procfile_lineword(ff, i, 1));
717 else if(unlikely(hash == active_file_hash && !strcmp(s, "active_file")))
718 mem->active_file = str2ull(procfile_lineword(ff, i, 1));
720 else if(unlikely(hash == unevictable_hash && !strcmp(s, "unevictable")))
721 mem->unevictable = str2ull(procfile_lineword(ff, i, 1));
723 else if(unlikely(hash == hierarchical_memory_limit_hash && !strcmp(s, "hierarchical_memory_limit")))
724 mem->hierarchical_memory_limit = str2ull(procfile_lineword(ff, i, 1));
726 else if(unlikely(hash == total_cache_hash && !strcmp(s, "total_cache")))
727 mem->total_cache = str2ull(procfile_lineword(ff, i, 1));
729 else if(unlikely(hash == total_rss_hash && !strcmp(s, "total_rss")))
730 mem->total_rss = str2ull(procfile_lineword(ff, i, 1));
732 else if(unlikely(hash == total_rss_huge_hash && !strcmp(s, "total_rss_huge")))
733 mem->total_rss_huge = str2ull(procfile_lineword(ff, i, 1));
735 else if(unlikely(hash == total_mapped_file_hash && !strcmp(s, "total_mapped_file")))
736 mem->total_mapped_file = str2ull(procfile_lineword(ff, i, 1));
738 else if(unlikely(hash == total_writeback_hash && !strcmp(s, "total_writeback")))
739 mem->total_writeback = str2ull(procfile_lineword(ff, i, 1));
741 else if(unlikely(hash == total_dirty_hash && !strcmp(s, "total_dirty")))
742 mem->total_dirty = str2ull(procfile_lineword(ff, i, 1));
744 else if(unlikely(hash == total_swap_hash && !strcmp(s, "total_swap")))
745 mem->total_swap = str2ull(procfile_lineword(ff, i, 1));
747 else if(unlikely(hash == total_pgpgin_hash && !strcmp(s, "total_pgpgin")))
748 mem->total_pgpgin = str2ull(procfile_lineword(ff, i, 1), NULL, 10);
750 else if(unlikely(hash == total_pgpgout_hash && !strcmp(s, "total_pgpgout")))
751 mem->total_pgpgout = str2ull(procfile_lineword(ff, i, 1));
753 else if(unlikely(hash == total_pgfault_hash && !strcmp(s, "total_pgfault")))
754 mem->total_pgfault = str2ull(procfile_lineword(ff, i, 1));
756 else if(unlikely(hash == total_pgmajfault_hash && !strcmp(s, "total_pgmajfault")))
757 mem->total_pgmajfault = str2ull(procfile_lineword(ff, i, 1));
759 else if(unlikely(hash == total_inactive_anon_hash && !strcmp(s, "total_inactive_anon")))
760 mem->total_inactive_anon = str2ull(procfile_lineword(ff, i, 1));
762 else if(unlikely(hash == total_active_anon_hash && !strcmp(s, "total_active_anon")))
763 mem->total_active_anon = str2ull(procfile_lineword(ff, i, 1));
765 else if(unlikely(hash == total_inactive_file_hash && !strcmp(s, "total_inactive_file")))
766 mem->total_inactive_file = str2ull(procfile_lineword(ff, i, 1));
768 else if(unlikely(hash == total_active_file_hash && !strcmp(s, "total_active_file")))
769 mem->total_active_file = str2ull(procfile_lineword(ff, i, 1));
771 else if(unlikely(hash == total_unevictable_hash && !strcmp(s, "total_unevictable")))
772 mem->total_unevictable = str2ull(procfile_lineword(ff, i, 1));
776 // 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);
778 mem->updated_detailed = 1;
780 if(unlikely(mem->enabled_detailed == CONFIG_ONDEMAND_ONDEMAND)) {
781 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)
782 mem->enabled_detailed = CONFIG_ONDEMAND_YES;
784 mem->delay_counter_detailed = cgroup_recheck_zero_mem_detailed_every_iterations;
790 // read usage_in_bytes
791 if(likely(mem->filename_usage_in_bytes)) {
792 mem->updated_usage_in_bytes = !read_single_number_file(mem->filename_usage_in_bytes, &mem->usage_in_bytes);
793 if(unlikely(mem->updated_usage_in_bytes && mem->enabled_usage_in_bytes == CONFIG_ONDEMAND_ONDEMAND && mem->usage_in_bytes))
794 mem->enabled_usage_in_bytes = CONFIG_ONDEMAND_YES;
797 // read msw_usage_in_bytes
798 if(likely(mem->filename_msw_usage_in_bytes)) {
799 mem->updated_msw_usage_in_bytes = !read_single_number_file(mem->filename_msw_usage_in_bytes, &mem->msw_usage_in_bytes);
800 if(unlikely(mem->updated_msw_usage_in_bytes && mem->enabled_msw_usage_in_bytes == CONFIG_ONDEMAND_ONDEMAND && mem->msw_usage_in_bytes))
801 mem->enabled_msw_usage_in_bytes = CONFIG_ONDEMAND_YES;
805 if(likely(mem->filename_failcnt)) {
806 if(unlikely(mem->enabled_failcnt == CONFIG_ONDEMAND_ONDEMAND && mem->delay_counter_failcnt > 0)) {
807 mem->updated_failcnt = 0;
808 mem->delay_counter_failcnt--;
811 mem->updated_failcnt = !read_single_number_file(mem->filename_failcnt, &mem->failcnt);
812 if(unlikely(mem->updated_failcnt && mem->enabled_failcnt == CONFIG_ONDEMAND_ONDEMAND)) {
813 if(unlikely(!mem->failcnt))
814 mem->delay_counter_failcnt = cgroup_recheck_zero_mem_failcnt_every_iterations;
816 mem->enabled_failcnt = CONFIG_ONDEMAND_YES;
822 static inline void cgroup_read(struct cgroup *cg) {
823 debug(D_CGROUP, "reading metrics for cgroups '%s'", cg->id);
825 cgroup_read_cpuacct_stat(&cg->cpuacct_stat);
826 cgroup_read_cpuacct_usage(&cg->cpuacct_usage);
827 cgroup_read_memory(&cg->memory);
828 cgroup_read_blkio(&cg->io_service_bytes);
829 cgroup_read_blkio(&cg->io_serviced);
830 cgroup_read_blkio(&cg->throttle_io_service_bytes);
831 cgroup_read_blkio(&cg->throttle_io_serviced);
832 cgroup_read_blkio(&cg->io_merged);
833 cgroup_read_blkio(&cg->io_queued);
836 static inline void read_all_cgroups(struct cgroup *root) {
837 debug(D_CGROUP, "reading metrics for all cgroups");
841 for(cg = root; cg ; cg = cg->next)
842 if(cg->enabled && cg->available)
846 // ----------------------------------------------------------------------------
847 // add/remove/find cgroup objects
849 #define CGROUP_CHARTID_LINE_MAX 1024
851 static inline char *cgroup_title_strdupz(const char *s) {
852 if(!s || !*s) s = "/";
854 if(*s == '/' && s[1] != '\0') s++;
856 char *r = strdupz(s);
857 netdata_fix_chart_name(r);
862 static inline char *cgroup_chart_id_strdupz(const char *s) {
863 if(!s || !*s) s = "/";
865 if(*s == '/' && s[1] != '\0') s++;
867 char *r = strdupz(s);
868 netdata_fix_chart_id(r);
873 static inline void cgroup_get_chart_name(struct cgroup *cg) {
874 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);
877 char buffer[CGROUP_CHARTID_LINE_MAX + 1];
879 snprintfz(buffer, CGROUP_CHARTID_LINE_MAX, "exec %s '%s'", cgroups_rename_script, cg->chart_id);
881 debug(D_CGROUP, "executing command '%s' for cgroup '%s'", buffer, cg->id);
882 FILE *fp = mypopen(buffer, &cgroup_pid);
884 // debug(D_CGROUP, "reading from command '%s' for cgroup '%s'", buffer, cg->id);
885 char *s = fgets(buffer, CGROUP_CHARTID_LINE_MAX, fp);
886 // debug(D_CGROUP, "closing command for cgroup '%s'", cg->id);
887 mypclose(fp, cgroup_pid);
888 // debug(D_CGROUP, "closed command for cgroup '%s'", cg->id);
890 if(s && *s && *s != '\n') {
891 debug(D_CGROUP, "cgroup '%s' should be renamed to '%s'", cg->id, s);
895 freez(cg->chart_title);
896 cg->chart_title = cgroup_title_strdupz(s);
899 cg->chart_id = cgroup_chart_id_strdupz(s);
900 cg->hash_chart = simple_hash(cg->chart_id);
904 error("CGROUP: Cannot popen(\"%s\", \"r\").", buffer);
907 static inline struct cgroup *cgroup_add(const char *id) {
908 if(!id || !*id) id = "/";
909 debug(D_CGROUP, "adding to list, cgroup with id '%s'", id);
911 if(cgroup_root_count >= cgroup_root_max) {
912 info("Maximum number of cgroups reached (%d). Not adding cgroup '%s'", cgroup_root_count, id);
916 int def = simple_pattern_matches(enabled_cgroup_patterns, id)?cgroup_enable_new_cgroups_detected_at_runtime:0;
917 struct cgroup *cg = callocz(1, sizeof(struct cgroup));
919 cg->id = strdupz(id);
920 cg->hash = simple_hash(cg->id);
922 cg->chart_title = cgroup_title_strdupz(id);
924 cg->chart_id = cgroup_chart_id_strdupz(id);
925 cg->hash_chart = simple_hash(cg->chart_id);
932 for(e = cgroup_root; e->next ;e = e->next) ;
938 // fix the chart_id and title by calling the external script
939 if(simple_pattern_matches(enabled_cgroup_renames, cg->id)) {
941 cgroup_get_chart_name(cg);
943 debug(D_CGROUP, "cgroup '%s' renamed to '%s' (title: '%s')", cg->id, cg->chart_id, cg->chart_title);
946 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);
948 int user_configurable = 1;
950 // check if this cgroup should be a systemd service
951 if(cgroup_enable_systemd_services) {
952 if(simple_pattern_matches(systemd_services_cgroups, cg->id) ||
953 simple_pattern_matches(systemd_services_cgroups, cg->chart_id)) {
954 debug(D_CGROUP, "cgroup '%s' with chart id '%s' (title: '%s') matches systemd services cgroups", cg->id, cg->chart_id, cg->chart_title);
956 char buffer[CGROUP_CHARTID_LINE_MAX + 1];
957 cg->options |= CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE;
959 strncpy(buffer, cg->id, CGROUP_CHARTID_LINE_MAX);
962 //freez(cg->chart_id);
963 //cg->chart_id = cgroup_chart_id_strdupz(s);
964 //cg->hash_chart = simple_hash(cg->chart_id);
966 // skip to the last slash
967 size_t len = strlen(s);
968 while(len--) if(unlikely(s[len] == '/')) break;
969 if(len) s = &s[len + 1];
973 while(len--) if(unlikely(s[len] == '.')) break;
974 if(len) s[len] = '\0';
976 freez(cg->chart_title);
977 cg->chart_title = cgroup_title_strdupz(s);
980 user_configurable = 0;
982 debug(D_CGROUP, "cgroup '%s' renamed to '%s' (title: '%s')", cg->id, cg->chart_id, cg->chart_title);
985 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);
988 if(user_configurable) {
989 // allow the user to enable/disable this individualy
990 char option[FILENAME_MAX + 1];
991 snprintfz(option, FILENAME_MAX, "enable cgroup %s", cg->chart_title);
992 cg->enabled = (char) config_get_boolean("plugin:cgroups", option, def);
995 // detect duplicate cgroups
998 for (t = cgroup_root; t; t = t->next) {
999 if (t != cg && t->enabled && t->hash_chart == cg->hash_chart && !strcmp(t->chart_id, cg->chart_id)) {
1000 if (!strncmp(t->chart_id, "/system.slice/", 14) && !strncmp(cg->chart_id, "/init.scope/system.slice/", 25)) {
1001 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'.",
1002 cg->chart_id, t->id, cg->id, t->id);
1003 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'.",
1004 cg->chart_id, t->id, cg->id, t->id);
1006 t->options |= CGROUP_OPTIONS_DISABLED_DUPLICATE;
1009 error("Control group with chart id '%s' already exists with id '%s' and is enabled and available. Disabling cgroup with id '%s'.",
1010 cg->chart_id, t->id, cg->id);
1011 debug(D_CGROUP, "Control group with chart id '%s' already exists with id '%s' and is enabled and available. Disabling cgroup with id '%s'.",
1012 cg->chart_id, t->id, cg->id);
1014 cg->options |= CGROUP_OPTIONS_DISABLED_DUPLICATE;
1022 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");
1027 static inline void cgroup_free(struct cgroup *cg) {
1028 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");
1030 freez(cg->cpuacct_usage.cpu_percpu);
1032 freez(cg->cpuacct_stat.filename);
1033 freez(cg->cpuacct_usage.filename);
1035 freez(cg->memory.filename_detailed);
1036 freez(cg->memory.filename_failcnt);
1037 freez(cg->memory.filename_usage_in_bytes);
1038 freez(cg->memory.filename_msw_usage_in_bytes);
1040 freez(cg->io_service_bytes.filename);
1041 freez(cg->io_serviced.filename);
1043 freez(cg->throttle_io_service_bytes.filename);
1044 freez(cg->throttle_io_serviced.filename);
1046 freez(cg->io_merged.filename);
1047 freez(cg->io_queued.filename);
1050 freez(cg->chart_id);
1051 freez(cg->chart_title);
1055 cgroup_root_count--;
1058 // find if a given cgroup exists
1059 static inline struct cgroup *cgroup_find(const char *id) {
1060 debug(D_CGROUP, "searching for cgroup '%s'", id);
1062 uint32_t hash = simple_hash(id);
1065 for(cg = cgroup_root; cg ; cg = cg->next) {
1066 if(hash == cg->hash && strcmp(id, cg->id) == 0)
1070 debug(D_CGROUP, "cgroup '%s' %s in memory", id, (cg)?"found":"not found");
1074 // ----------------------------------------------------------------------------
1075 // detect running cgroups
1077 // callback for find_file_in_subdirs()
1078 static inline void found_subdir_in_dir(const char *dir) {
1079 debug(D_CGROUP, "examining cgroup dir '%s'", dir);
1081 struct cgroup *cg = cgroup_find(dir);
1083 if(*dir && cgroup_max_depth > 0) {
1087 for(s = dir; *s ;s++)
1088 if(unlikely(*s == '/'))
1091 if(depth > cgroup_max_depth) {
1092 info("cgroup '%s' is too deep (%d, while max is %d)", dir, depth, cgroup_max_depth);
1096 // debug(D_CGROUP, "will add dir '%s' as cgroup", dir);
1097 cg = cgroup_add(dir);
1100 if(cg) cg->available = 1;
1103 static inline int find_dir_in_subdirs(const char *base, const char *this, void (*callback)(const char *)) {
1104 if(!this) this = base;
1105 debug(D_CGROUP, "searching for directories in '%s' (base '%s')", this?this:"", base);
1107 size_t dirlen = strlen(this), baselen = strlen(base);
1112 const char *relative_path = &this[baselen];
1113 if(!*relative_path) relative_path = "/";
1115 DIR *dir = opendir(this);
1117 error("Cannot read cgroups directory '%s'", base);
1122 callback(relative_path);
1124 struct dirent *de = NULL;
1125 while((de = readdir(dir))) {
1126 if(de->d_type == DT_DIR
1128 (de->d_name[0] == '.' && de->d_name[1] == '\0')
1129 || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
1133 if(de->d_type == DT_DIR) {
1135 const char *r = relative_path;
1136 if(*r == '\0') r = "/";
1138 // do not decent in directories we are not interested
1139 int def = simple_pattern_matches(enabled_cgroup_paths, r);
1141 // we check for this option here
1142 // so that the config will not have settings
1143 // for leaf directories
1144 char option[FILENAME_MAX + 1];
1145 snprintfz(option, FILENAME_MAX, "search for cgroups under %s", r);
1146 option[FILENAME_MAX] = '\0';
1147 enabled = config_get_boolean("plugin:cgroups", option, def);
1151 char *s = mallocz(dirlen + strlen(de->d_name) + 2);
1154 strcat(s, de->d_name);
1155 int ret2 = find_dir_in_subdirs(base, s, callback);
1156 if(ret2 > 0) ret += ret2;
1166 static inline void mark_all_cgroups_as_not_available() {
1167 debug(D_CGROUP, "marking all cgroups as not available");
1171 // mark all as not available
1172 for(cg = cgroup_root; cg ; cg = cg->next) {
1177 static inline void cleanup_all_cgroups() {
1178 struct cgroup *cg = cgroup_root, *last = NULL;
1181 if(!cg->available) {
1182 // enable the first duplicate cgroup
1185 for(t = cgroup_root; t ; t = t->next) {
1186 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)) {
1187 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);
1189 t->options &= ~CGROUP_OPTIONS_DISABLED_DUPLICATE;
1196 cgroup_root = cg->next;
1198 last->next = cg->next;
1214 static inline void find_all_cgroups() {
1215 debug(D_CGROUP, "searching for cgroups");
1217 mark_all_cgroups_as_not_available();
1219 if(cgroup_enable_cpuacct_stat || cgroup_enable_cpuacct_usage) {
1220 if(find_dir_in_subdirs(cgroup_cpuacct_base, NULL, found_subdir_in_dir) == -1) {
1221 cgroup_enable_cpuacct_stat =
1222 cgroup_enable_cpuacct_usage = CONFIG_ONDEMAND_NO;
1223 error("disabled CGROUP cpu statistics.");
1227 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) {
1228 if(find_dir_in_subdirs(cgroup_blkio_base, NULL, found_subdir_in_dir) == -1) {
1229 cgroup_enable_blkio_io =
1230 cgroup_enable_blkio_ops =
1231 cgroup_enable_blkio_throttle_io =
1232 cgroup_enable_blkio_throttle_ops =
1233 cgroup_enable_blkio_merged_ops =
1234 cgroup_enable_blkio_queued_ops = CONFIG_ONDEMAND_NO;
1235 error("disabled CGROUP blkio statistics.");
1239 if(cgroup_enable_memory || cgroup_enable_detailed_memory || cgroup_enable_swap || cgroup_enable_memory_failcnt) {
1240 if(find_dir_in_subdirs(cgroup_memory_base, NULL, found_subdir_in_dir) == -1) {
1241 cgroup_enable_memory =
1242 cgroup_enable_detailed_memory =
1243 cgroup_enable_swap =
1244 cgroup_enable_memory_failcnt = CONFIG_ONDEMAND_NO;
1245 error("disabled CGROUP memory statistics.");
1249 if(cgroup_search_in_devices) {
1250 if(find_dir_in_subdirs(cgroup_devices_base, NULL, found_subdir_in_dir) == -1) {
1251 cgroup_search_in_devices = 0;
1252 error("disabled CGROUP devices statistics.");
1256 // remove any non-existing cgroups
1257 cleanup_all_cgroups();
1261 for(cg = cgroup_root; cg ; cg = cg->next) {
1262 // fprintf(stderr, " >>> CGROUP '%s' (%u - %s) with name '%s'\n", cg->id, cg->hash, cg->available?"available":"stopped", cg->name);
1264 if(unlikely(!cg->available))
1267 debug(D_CGROUP, "checking paths for cgroup '%s'", cg->id);
1269 // check for newly added cgroups
1270 // and update the filenames they read
1271 char filename[FILENAME_MAX + 1];
1272 if(unlikely(cgroup_enable_cpuacct_stat && !cg->cpuacct_stat.filename)) {
1273 snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.stat", cgroup_cpuacct_base, cg->id);
1274 if(likely(stat(filename, &buf) != -1)) {
1275 cg->cpuacct_stat.filename = strdupz(filename);
1276 cg->cpuacct_stat.enabled = cgroup_enable_cpuacct_stat;
1277 debug(D_CGROUP, "cpuacct.stat filename for cgroup '%s': '%s'", cg->id, cg->cpuacct_stat.filename);
1280 debug(D_CGROUP, "cpuacct.stat file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1283 if(unlikely(cgroup_enable_cpuacct_usage && !cg->cpuacct_usage.filename && !(cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE))) {
1284 snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.usage_percpu", cgroup_cpuacct_base, cg->id);
1285 if(likely(stat(filename, &buf) != -1)) {
1286 cg->cpuacct_usage.filename = strdupz(filename);
1287 cg->cpuacct_usage.enabled = cgroup_enable_cpuacct_usage;
1288 debug(D_CGROUP, "cpuacct.usage_percpu filename for cgroup '%s': '%s'", cg->id, cg->cpuacct_usage.filename);
1291 debug(D_CGROUP, "cpuacct.usage_percpu file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1294 if(unlikely((cgroup_enable_detailed_memory || cgroup_used_memory_without_cache) && !cg->memory.filename_detailed && (cgroup_used_memory_without_cache || cgroup_enable_systemd_services_detailed_memory || !(cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE)))) {
1295 snprintfz(filename, FILENAME_MAX, "%s%s/memory.stat", cgroup_memory_base, cg->id);
1296 if(likely(stat(filename, &buf) != -1)) {
1297 cg->memory.filename_detailed = strdupz(filename);
1298 cg->memory.enabled_detailed = (cgroup_enable_detailed_memory == CONFIG_ONDEMAND_YES)?CONFIG_ONDEMAND_YES:CONFIG_ONDEMAND_ONDEMAND;
1299 debug(D_CGROUP, "memory.stat filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_detailed);
1302 debug(D_CGROUP, "memory.stat file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1305 if(unlikely(cgroup_enable_memory && !cg->memory.filename_usage_in_bytes)) {
1306 snprintfz(filename, FILENAME_MAX, "%s%s/memory.usage_in_bytes", cgroup_memory_base, cg->id);
1307 if(likely(stat(filename, &buf) != -1)) {
1308 cg->memory.filename_usage_in_bytes = strdupz(filename);
1309 cg->memory.enabled_usage_in_bytes = cgroup_enable_memory;
1310 debug(D_CGROUP, "memory.usage_in_bytes filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_usage_in_bytes);
1313 debug(D_CGROUP, "memory.usage_in_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1316 if(unlikely(cgroup_enable_swap && !cg->memory.filename_msw_usage_in_bytes)) {
1317 snprintfz(filename, FILENAME_MAX, "%s%s/memory.msw_usage_in_bytes", cgroup_memory_base, cg->id);
1318 if(likely(stat(filename, &buf) != -1)) {
1319 cg->memory.filename_msw_usage_in_bytes = strdupz(filename);
1320 cg->memory.enabled_msw_usage_in_bytes = cgroup_enable_swap;
1321 debug(D_CGROUP, "memory.msw_usage_in_bytes filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_msw_usage_in_bytes);
1324 debug(D_CGROUP, "memory.msw_usage_in_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1327 if(unlikely(cgroup_enable_memory_failcnt && !cg->memory.filename_failcnt)) {
1328 snprintfz(filename, FILENAME_MAX, "%s%s/memory.failcnt", cgroup_memory_base, cg->id);
1329 if(likely(stat(filename, &buf) != -1)) {
1330 cg->memory.filename_failcnt = strdupz(filename);
1331 cg->memory.enabled_failcnt = cgroup_enable_memory_failcnt;
1332 debug(D_CGROUP, "memory.failcnt filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_failcnt);
1335 debug(D_CGROUP, "memory.failcnt file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1338 if(unlikely(cgroup_enable_blkio_io && !cg->io_service_bytes.filename)) {
1339 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_service_bytes", cgroup_blkio_base, cg->id);
1340 if(likely(stat(filename, &buf) != -1)) {
1341 cg->io_service_bytes.filename = strdupz(filename);
1342 cg->io_service_bytes.enabled = cgroup_enable_blkio_io;
1343 debug(D_CGROUP, "io_service_bytes filename for cgroup '%s': '%s'", cg->id, cg->io_service_bytes.filename);
1346 debug(D_CGROUP, "io_service_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1349 if(unlikely(cgroup_enable_blkio_ops && !cg->io_serviced.filename)) {
1350 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_serviced", cgroup_blkio_base, cg->id);
1351 if(likely(stat(filename, &buf) != -1)) {
1352 cg->io_serviced.filename = strdupz(filename);
1353 cg->io_serviced.enabled = cgroup_enable_blkio_ops;
1354 debug(D_CGROUP, "io_serviced filename for cgroup '%s': '%s'", cg->id, cg->io_serviced.filename);
1357 debug(D_CGROUP, "io_serviced file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1360 if(unlikely(cgroup_enable_blkio_throttle_io && !cg->throttle_io_service_bytes.filename)) {
1361 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.throttle.io_service_bytes", cgroup_blkio_base, cg->id);
1362 if(likely(stat(filename, &buf) != -1)) {
1363 cg->throttle_io_service_bytes.filename = strdupz(filename);
1364 cg->throttle_io_service_bytes.enabled = cgroup_enable_blkio_throttle_io;
1365 debug(D_CGROUP, "throttle_io_service_bytes filename for cgroup '%s': '%s'", cg->id, cg->throttle_io_service_bytes.filename);
1368 debug(D_CGROUP, "throttle_io_service_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1371 if(unlikely(cgroup_enable_blkio_throttle_ops && !cg->throttle_io_serviced.filename)) {
1372 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.throttle.io_serviced", cgroup_blkio_base, cg->id);
1373 if(likely(stat(filename, &buf) != -1)) {
1374 cg->throttle_io_serviced.filename = strdupz(filename);
1375 cg->throttle_io_serviced.enabled = cgroup_enable_blkio_throttle_ops;
1376 debug(D_CGROUP, "throttle_io_serviced filename for cgroup '%s': '%s'", cg->id, cg->throttle_io_serviced.filename);
1379 debug(D_CGROUP, "throttle_io_serviced file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1382 if(unlikely(cgroup_enable_blkio_merged_ops && !cg->io_merged.filename)) {
1383 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_merged", cgroup_blkio_base, cg->id);
1384 if(likely(stat(filename, &buf) != -1)) {
1385 cg->io_merged.filename = strdupz(filename);
1386 cg->io_merged.enabled = cgroup_enable_blkio_merged_ops;
1387 debug(D_CGROUP, "io_merged filename for cgroup '%s': '%s'", cg->id, cg->io_merged.filename);
1390 debug(D_CGROUP, "io_merged file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1393 if(unlikely(cgroup_enable_blkio_queued_ops && !cg->io_queued.filename)) {
1394 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_queued", cgroup_blkio_base, cg->id);
1395 if(likely(stat(filename, &buf) != -1)) {
1396 cg->io_queued.filename = strdupz(filename);
1397 cg->io_queued.enabled = cgroup_enable_blkio_queued_ops;
1398 debug(D_CGROUP, "io_queued filename for cgroup '%s': '%s'", cg->id, cg->io_queued.filename);
1401 debug(D_CGROUP, "io_queued file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1405 debug(D_CGROUP, "done searching for cgroups");
1409 // ----------------------------------------------------------------------------
1412 #define CHART_TITLE_MAX 300
1414 void update_services_charts(int update_every,
1417 int do_mem_detailed,
1423 int do_throttle_ops,
1429 *st_mem_usage = NULL,
1430 *st_mem_failcnt = NULL,
1431 *st_swap_usage = NULL,
1433 *st_mem_detailed_cache = NULL,
1434 *st_mem_detailed_rss = NULL,
1435 *st_mem_detailed_mapped = NULL,
1436 *st_mem_detailed_writeback = NULL,
1437 *st_mem_detailed_pgfault = NULL,
1438 *st_mem_detailed_pgmajfault = NULL,
1439 *st_mem_detailed_pgpgin = NULL,
1440 *st_mem_detailed_pgpgout = NULL,
1443 *st_io_serviced_read = NULL,
1444 *st_throttle_io_read = NULL,
1445 *st_throttle_ops_read = NULL,
1446 *st_queued_ops_read = NULL,
1447 *st_merged_ops_read = NULL,
1449 *st_io_write = NULL,
1450 *st_io_serviced_write = NULL,
1451 *st_throttle_io_write = NULL,
1452 *st_throttle_ops_write = NULL,
1453 *st_queued_ops_write = NULL,
1454 *st_merged_ops_write = NULL;
1456 // create the charts
1458 if(likely(do_cpu)) {
1459 if(unlikely(!st_cpu)) {
1460 char title[CHART_TITLE_MAX + 1];
1462 st_cpu = rrdset_find_bytype("services", "cpu");
1463 if(likely(!st_cpu)) {
1464 snprintfz(title, CHART_TITLE_MAX, "Systemd Services CPU utilization (%d%% = %d core%s)", (processors * 100), processors, (processors > 1) ? "s" : "");
1465 st_cpu = rrdset_create("services", "cpu", NULL, "cpu", "services.cpu", title, "%", CHART_PRIORITY_SYSTEMD_SERVICES, update_every, RRDSET_TYPE_STACKED);
1469 rrdset_next(st_cpu);
1472 if(likely(do_mem_usage)) {
1473 if(unlikely(!st_mem_usage)) {
1474 st_mem_usage = rrdset_find_bytype("services", "mem_usage");
1475 if(likely(!st_mem_usage))
1476 st_mem_usage = rrdset_create("services", "mem_usage", NULL, "mem", "services.mem_usage", (cgroup_used_memory_without_cache)?"Systemd Services Used Memory without Cache":"Systemd Services Used Memory", "MB", CHART_PRIORITY_SYSTEMD_SERVICES + 10, update_every, RRDSET_TYPE_STACKED);
1479 rrdset_next(st_mem_usage);
1482 if(likely(do_mem_detailed)) {
1483 if(unlikely(!st_mem_detailed_rss)) {
1484 st_mem_detailed_rss = rrdset_find_bytype("services", "mem_rss");
1485 if(likely(!st_mem_detailed_rss))
1486 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);
1489 rrdset_next(st_mem_detailed_rss);
1491 if(unlikely(!st_mem_detailed_mapped)) {
1492 st_mem_detailed_mapped = rrdset_find_bytype("services", "mem_mapped");
1493 if(likely(!st_mem_detailed_mapped))
1494 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);
1497 rrdset_next(st_mem_detailed_mapped);
1499 if(unlikely(!st_mem_detailed_cache)) {
1500 st_mem_detailed_cache = rrdset_find_bytype("services", "mem_cache");
1501 if(likely(!st_mem_detailed_cache))
1502 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);
1505 rrdset_next(st_mem_detailed_cache);
1507 if(unlikely(!st_mem_detailed_writeback)) {
1508 st_mem_detailed_writeback = rrdset_find_bytype("services", "mem_writeback");
1509 if(likely(!st_mem_detailed_writeback))
1510 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);
1513 rrdset_next(st_mem_detailed_writeback);
1515 if(unlikely(!st_mem_detailed_pgfault)) {
1516 st_mem_detailed_pgfault = rrdset_find_bytype("services", "mem_pgfault");
1517 if(likely(!st_mem_detailed_pgfault))
1518 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);
1521 rrdset_next(st_mem_detailed_pgfault);
1523 if(unlikely(!st_mem_detailed_pgmajfault)) {
1524 st_mem_detailed_pgmajfault = rrdset_find_bytype("services", "mem_pgmajfault");
1525 if(likely(!st_mem_detailed_pgmajfault))
1526 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);
1529 rrdset_next(st_mem_detailed_pgmajfault);
1531 if(unlikely(!st_mem_detailed_pgpgin)) {
1532 st_mem_detailed_pgpgin = rrdset_find_bytype("services", "mem_pgpgin");
1533 if(likely(!st_mem_detailed_pgpgin))
1534 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);
1537 rrdset_next(st_mem_detailed_pgpgin);
1539 if(unlikely(!st_mem_detailed_pgpgout)) {
1540 st_mem_detailed_pgpgout = rrdset_find_bytype("services", "mem_pgpgout");
1541 if(likely(!st_mem_detailed_pgpgout))
1542 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);
1545 rrdset_next(st_mem_detailed_pgpgout);
1548 if(likely(do_mem_failcnt)) {
1549 if(unlikely(!st_mem_failcnt)) {
1550 st_mem_failcnt = rrdset_find_bytype("services", "mem_failcnt");
1551 if(likely(!st_mem_failcnt))
1552 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);
1555 rrdset_next(st_mem_failcnt);
1558 if(likely(do_swap_usage)) {
1559 if(unlikely(!st_swap_usage)) {
1560 st_swap_usage = rrdset_find_bytype("services", "swap_usage");
1561 if(likely(!st_swap_usage))
1562 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);
1565 rrdset_next(st_swap_usage);
1569 if(unlikely(!st_io_read)) {
1570 st_io_read = rrdset_find_bytype("services", "io_read");
1571 if(likely(!st_io_read))
1572 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);
1575 rrdset_next(st_io_read);
1577 if(unlikely(!st_io_write)) {
1578 st_io_write = rrdset_find_bytype("services", "io_write");
1579 if(likely(!st_io_write))
1580 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);
1583 rrdset_next(st_io_write);
1586 if(likely(do_io_ops)) {
1587 if(unlikely(!st_io_serviced_read)) {
1588 st_io_serviced_read = rrdset_find_bytype("services", "io_ops_read");
1589 if(likely(!st_io_serviced_read))
1590 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);
1593 rrdset_next(st_io_serviced_read);
1595 if(unlikely(!st_io_serviced_write)) {
1596 st_io_serviced_write = rrdset_find_bytype("services", "io_ops_write");
1597 if(likely(!st_io_serviced_write))
1598 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);
1601 rrdset_next(st_io_serviced_write);
1604 if(likely(do_throttle_io)) {
1605 if(unlikely(!st_throttle_io_read)) {
1606 st_throttle_io_read = rrdset_find_bytype("services", "throttle_io_read");
1607 if(likely(!st_throttle_io_read))
1608 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);
1611 rrdset_next(st_throttle_io_read);
1613 if(unlikely(!st_throttle_io_write)) {
1614 st_throttle_io_write = rrdset_find_bytype("services", "throttle_io_write");
1615 if(likely(!st_throttle_io_write))
1616 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);
1619 rrdset_next(st_throttle_io_write);
1622 if(likely(do_throttle_ops)) {
1623 if(unlikely(!st_throttle_ops_read)) {
1624 st_throttle_ops_read = rrdset_find_bytype("services", "throttle_io_ops_read");
1625 if(likely(!st_throttle_ops_read))
1626 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);
1629 rrdset_next(st_throttle_ops_read);
1631 if(unlikely(!st_throttle_ops_write)) {
1632 st_throttle_ops_write = rrdset_find_bytype("services", "throttle_io_ops_write");
1633 if(likely(!st_throttle_ops_write))
1634 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);
1637 rrdset_next(st_throttle_ops_write);
1640 if(likely(do_queued_ops)) {
1641 if(unlikely(!st_queued_ops_read)) {
1642 st_queued_ops_read = rrdset_find_bytype("services", "queued_io_ops_read");
1643 if(likely(!st_queued_ops_read))
1644 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);
1647 rrdset_next(st_queued_ops_read);
1649 if(unlikely(!st_queued_ops_write)) {
1650 st_queued_ops_write = rrdset_find_bytype("services", "queued_io_ops_write");
1651 if(likely(!st_queued_ops_write))
1652 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);
1655 rrdset_next(st_queued_ops_write);
1658 if(likely(do_merged_ops)) {
1659 if(unlikely(!st_merged_ops_read)) {
1660 st_merged_ops_read = rrdset_find_bytype("services", "merged_io_ops_read");
1661 if(likely(!st_merged_ops_read))
1662 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);
1665 rrdset_next(st_merged_ops_read);
1667 if(unlikely(!st_merged_ops_write)) {
1668 st_merged_ops_write = rrdset_find_bytype("services", "merged_io_ops_write");
1669 if(likely(!st_merged_ops_write))
1670 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);
1673 rrdset_next(st_merged_ops_write);
1676 // update the values
1678 for(cg = cgroup_root; cg ; cg = cg->next) {
1679 if(unlikely(!cg->available || !cg->enabled || !(cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE)))
1682 if(likely(do_cpu && cg->cpuacct_stat.updated)) {
1683 if(unlikely(!cg->rd_cpu))
1684 cg->rd_cpu = rrddim_add(st_cpu, cg->chart_id, cg->chart_title, 100, hz, RRDDIM_INCREMENTAL);
1686 rrddim_set_by_pointer(st_cpu, cg->rd_cpu, cg->cpuacct_stat.user + cg->cpuacct_stat.system);
1689 if(likely(do_mem_usage && cg->memory.updated_usage_in_bytes)) {
1690 if(unlikely(!cg->rd_mem_usage))
1691 cg->rd_mem_usage = rrddim_add(st_mem_usage, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1693 rrddim_set_by_pointer(st_mem_usage, cg->rd_mem_usage, cg->memory.usage_in_bytes - ((cgroup_used_memory_without_cache)?cg->memory.cache:0));
1696 if(likely(do_mem_detailed && cg->memory.updated_detailed)) {
1697 if(unlikely(!cg->rd_mem_detailed_rss))
1698 cg->rd_mem_detailed_rss = rrddim_add(st_mem_detailed_rss, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1699 rrddim_set_by_pointer(st_mem_detailed_rss, cg->rd_mem_detailed_rss, cg->memory.rss + cg->memory.rss_huge);
1701 if(unlikely(!cg->rd_mem_detailed_mapped))
1702 cg->rd_mem_detailed_mapped = rrddim_add(st_mem_detailed_mapped, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1703 rrddim_set_by_pointer(st_mem_detailed_mapped, cg->rd_mem_detailed_mapped, cg->memory.mapped_file);
1705 if(unlikely(!cg->rd_mem_detailed_cache))
1706 cg->rd_mem_detailed_cache = rrddim_add(st_mem_detailed_cache, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1707 rrddim_set_by_pointer(st_mem_detailed_cache, cg->rd_mem_detailed_cache, cg->memory.cache);
1709 if(unlikely(!cg->rd_mem_detailed_writeback))
1710 cg->rd_mem_detailed_writeback = rrddim_add(st_mem_detailed_writeback, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1711 rrddim_set_by_pointer(st_mem_detailed_writeback, cg->rd_mem_detailed_writeback, cg->memory.writeback);
1713 if(unlikely(!cg->rd_mem_detailed_pgfault))
1714 cg->rd_mem_detailed_pgfault = rrddim_add(st_mem_detailed_pgfault, cg->chart_id, cg->chart_title, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1715 rrddim_set_by_pointer(st_mem_detailed_pgfault, cg->rd_mem_detailed_pgfault, cg->memory.pgfault);
1717 if(unlikely(!cg->rd_mem_detailed_pgmajfault))
1718 cg->rd_mem_detailed_pgmajfault = rrddim_add(st_mem_detailed_pgmajfault, cg->chart_id, cg->chart_title, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1719 rrddim_set_by_pointer(st_mem_detailed_pgmajfault, cg->rd_mem_detailed_pgmajfault, cg->memory.pgmajfault);
1721 if(unlikely(!cg->rd_mem_detailed_pgpgin))
1722 cg->rd_mem_detailed_pgpgin = rrddim_add(st_mem_detailed_pgpgin, cg->chart_id, cg->chart_title, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1723 rrddim_set_by_pointer(st_mem_detailed_pgpgin, cg->rd_mem_detailed_pgpgin, cg->memory.pgpgin);
1725 if(unlikely(!cg->rd_mem_detailed_pgpgout))
1726 cg->rd_mem_detailed_pgpgout = rrddim_add(st_mem_detailed_pgpgout, cg->chart_id, cg->chart_title, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1727 rrddim_set_by_pointer(st_mem_detailed_pgpgout, cg->rd_mem_detailed_pgpgout, cg->memory.pgpgout);
1730 if(likely(do_mem_failcnt && cg->memory.updated_failcnt)) {
1731 if(unlikely(!cg->rd_mem_failcnt))
1732 cg->rd_mem_failcnt = rrddim_add(st_mem_failcnt, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1734 rrddim_set_by_pointer(st_mem_failcnt, cg->rd_mem_failcnt, cg->memory.failcnt);
1737 if(likely(do_swap_usage && cg->memory.updated_msw_usage_in_bytes)) {
1738 if(unlikely(!cg->rd_swap_usage))
1739 cg->rd_swap_usage = rrddim_add(st_swap_usage, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1741 rrddim_set_by_pointer(st_swap_usage, cg->rd_swap_usage, cg->memory.msw_usage_in_bytes);
1744 if(likely(do_io && cg->io_service_bytes.updated)) {
1745 if(unlikely(!cg->rd_io_service_bytes_read))
1746 cg->rd_io_service_bytes_read = rrddim_add(st_io_read, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1748 rrddim_set_by_pointer(st_io_read, cg->rd_io_service_bytes_read, cg->io_service_bytes.Read);
1750 if(unlikely(!cg->rd_io_service_bytes_write))
1751 cg->rd_io_service_bytes_write = rrddim_add(st_io_write, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1753 rrddim_set_by_pointer(st_io_write, cg->rd_io_service_bytes_write, cg->io_service_bytes.Write);
1756 if(likely(do_io_ops && cg->io_serviced.updated)) {
1757 if(unlikely(!cg->rd_io_serviced_read))
1758 cg->rd_io_serviced_read = rrddim_add(st_io_serviced_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1760 rrddim_set_by_pointer(st_io_serviced_read, cg->rd_io_serviced_read, cg->io_serviced.Read);
1762 if(unlikely(!cg->rd_io_serviced_write))
1763 cg->rd_io_serviced_write = rrddim_add(st_io_serviced_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1765 rrddim_set_by_pointer(st_io_serviced_write, cg->rd_io_serviced_write, cg->io_serviced.Write);
1768 if(likely(do_throttle_io && cg->throttle_io_service_bytes.updated)) {
1769 if(unlikely(!cg->rd_throttle_io_read))
1770 cg->rd_throttle_io_read = rrddim_add(st_throttle_io_read, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1772 rrddim_set_by_pointer(st_throttle_io_read, cg->rd_throttle_io_read, cg->throttle_io_service_bytes.Read);
1774 if(unlikely(!cg->rd_throttle_io_write))
1775 cg->rd_throttle_io_write = rrddim_add(st_throttle_io_write, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1777 rrddim_set_by_pointer(st_throttle_io_write, cg->rd_throttle_io_write, cg->throttle_io_service_bytes.Write);
1780 if(likely(do_throttle_ops && cg->throttle_io_serviced.updated)) {
1781 if(unlikely(!cg->rd_throttle_io_serviced_read))
1782 cg->rd_throttle_io_serviced_read = rrddim_add(st_throttle_ops_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1784 rrddim_set_by_pointer(st_throttle_ops_read, cg->rd_throttle_io_serviced_read, cg->throttle_io_serviced.Read);
1786 if(unlikely(!cg->rd_throttle_io_serviced_write))
1787 cg->rd_throttle_io_serviced_write = rrddim_add(st_throttle_ops_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1789 rrddim_set_by_pointer(st_throttle_ops_write, cg->rd_throttle_io_serviced_write, cg->throttle_io_serviced.Write);
1792 if(likely(do_queued_ops && cg->io_queued.updated)) {
1793 if(unlikely(!cg->rd_io_queued_read))
1794 cg->rd_io_queued_read = rrddim_add(st_queued_ops_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1796 rrddim_set_by_pointer(st_queued_ops_read, cg->rd_io_queued_read, cg->io_queued.Read);
1798 if(unlikely(!cg->rd_io_queued_write))
1799 cg->rd_io_queued_write = rrddim_add(st_queued_ops_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1801 rrddim_set_by_pointer(st_queued_ops_write, cg->rd_io_queued_write, cg->io_queued.Write);
1804 if(likely(do_merged_ops && cg->io_merged.updated)) {
1805 if(unlikely(!cg->rd_io_merged_read))
1806 cg->rd_io_merged_read = rrddim_add(st_merged_ops_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1808 rrddim_set_by_pointer(st_merged_ops_read, cg->rd_io_merged_read, cg->io_merged.Read);
1810 if(unlikely(!cg->rd_io_merged_write))
1811 cg->rd_io_merged_write = rrddim_add(st_merged_ops_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1813 rrddim_set_by_pointer(st_merged_ops_write, cg->rd_io_merged_write, cg->io_merged.Write);
1817 // complete the iteration
1819 rrdset_done(st_cpu);
1821 if(likely(do_mem_usage))
1822 rrdset_done(st_mem_usage);
1824 if(unlikely(do_mem_detailed)) {
1825 rrdset_done(st_mem_detailed_cache);
1826 rrdset_done(st_mem_detailed_rss);
1827 rrdset_done(st_mem_detailed_mapped);
1828 rrdset_done(st_mem_detailed_writeback);
1829 rrdset_done(st_mem_detailed_pgfault);
1830 rrdset_done(st_mem_detailed_pgmajfault);
1831 rrdset_done(st_mem_detailed_pgpgin);
1832 rrdset_done(st_mem_detailed_pgpgout);
1835 if(likely(do_mem_failcnt))
1836 rrdset_done(st_mem_failcnt);
1838 if(likely(do_swap_usage))
1839 rrdset_done(st_swap_usage);
1842 rrdset_done(st_io_read);
1843 rrdset_done(st_io_write);
1846 if(likely(do_io_ops)) {
1847 rrdset_done(st_io_serviced_read);
1848 rrdset_done(st_io_serviced_write);
1851 if(likely(do_throttle_io)) {
1852 rrdset_done(st_throttle_io_read);
1853 rrdset_done(st_throttle_io_write);
1856 if(likely(do_throttle_ops)) {
1857 rrdset_done(st_throttle_ops_read);
1858 rrdset_done(st_throttle_ops_write);
1861 if(likely(do_queued_ops)) {
1862 rrdset_done(st_queued_ops_read);
1863 rrdset_done(st_queued_ops_write);
1866 if(likely(do_merged_ops)) {
1867 rrdset_done(st_merged_ops_read);
1868 rrdset_done(st_merged_ops_write);
1872 static inline char *cgroup_chart_type(char *buffer, const char *id, size_t len) {
1873 if(buffer[0]) return buffer;
1875 if(id[0] == '\0' || (id[0] == '/' && id[1] == '\0'))
1876 strncpy(buffer, "cgroup_root", len);
1878 snprintfz(buffer, len, "cgroup_%s", id);
1880 netdata_fix_chart_id(buffer);
1884 void update_cgroup_charts(int update_every) {
1885 debug(D_CGROUP, "updating cgroups charts");
1887 char type[RRD_ID_LENGTH_MAX + 1];
1888 char title[CHART_TITLE_MAX + 1];
1890 int services_do_cpu = 0,
1891 services_do_mem_usage = 0,
1892 services_do_mem_detailed = 0,
1893 services_do_mem_failcnt = 0,
1894 services_do_swap_usage = 0,
1896 services_do_io_ops = 0,
1897 services_do_throttle_io = 0,
1898 services_do_throttle_ops = 0,
1899 services_do_queued_ops = 0,
1900 services_do_merged_ops = 0;
1903 for(cg = cgroup_root; cg ; cg = cg->next) {
1904 if(unlikely(!cg->available || !cg->enabled))
1907 if(likely(cgroup_enable_systemd_services && cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE)) {
1908 if(cg->cpuacct_stat.updated && cg->cpuacct_stat.enabled == CONFIG_ONDEMAND_YES) services_do_cpu++;
1910 if(cgroup_enable_systemd_services_detailed_memory && cg->memory.updated_detailed && cg->memory.enabled_detailed) services_do_mem_detailed++;
1911 if(cg->memory.updated_usage_in_bytes && cg->memory.enabled_usage_in_bytes == CONFIG_ONDEMAND_YES) services_do_mem_usage++;
1912 if(cg->memory.updated_failcnt && cg->memory.enabled_failcnt == CONFIG_ONDEMAND_YES) services_do_mem_failcnt++;
1913 if(cg->memory.updated_msw_usage_in_bytes && cg->memory.enabled_msw_usage_in_bytes == CONFIG_ONDEMAND_YES) services_do_swap_usage++;
1915 if(cg->io_service_bytes.updated && cg->io_service_bytes.enabled == CONFIG_ONDEMAND_YES) services_do_io++;
1916 if(cg->io_serviced.updated && cg->io_serviced.enabled == CONFIG_ONDEMAND_YES) services_do_io_ops++;
1917 if(cg->throttle_io_service_bytes.updated && cg->throttle_io_service_bytes.enabled == CONFIG_ONDEMAND_YES) services_do_throttle_io++;
1918 if(cg->throttle_io_serviced.updated && cg->throttle_io_serviced.enabled == CONFIG_ONDEMAND_YES) services_do_throttle_ops++;
1919 if(cg->io_queued.updated && cg->io_queued.enabled == CONFIG_ONDEMAND_YES) services_do_queued_ops++;
1920 if(cg->io_merged.updated && cg->io_merged.enabled == CONFIG_ONDEMAND_YES) services_do_merged_ops++;
1926 if(likely(cg->cpuacct_stat.updated && cg->cpuacct_stat.enabled == CONFIG_ONDEMAND_YES)) {
1927 if(unlikely(!cg->st_cpu)) {
1928 cg->st_cpu = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "cpu");
1929 if(likely(!cg->st_cpu)) {
1930 snprintfz(title, CHART_TITLE_MAX, "CPU Usage (%d%% = %d core%s) for cgroup %s", (processors * 100), processors, (processors > 1) ? "s" : "", cg->chart_title);
1931 cg->st_cpu = rrdset_create(type, "cpu", NULL, "cpu", "cgroup.cpu", title, "%", CHART_PRIORITY_CONTAINERS, update_every, RRDSET_TYPE_STACKED);
1933 rrddim_add(cg->st_cpu, "user", NULL, 100, hz, RRDDIM_INCREMENTAL);
1934 rrddim_add(cg->st_cpu, "system", NULL, 100, hz, RRDDIM_INCREMENTAL);
1937 rrdset_next(cg->st_cpu);
1939 rrddim_set(cg->st_cpu, "user", cg->cpuacct_stat.user);
1940 rrddim_set(cg->st_cpu, "system", cg->cpuacct_stat.system);
1941 rrdset_done(cg->st_cpu);
1944 if(likely(cg->cpuacct_usage.updated && cg->cpuacct_usage.enabled == CONFIG_ONDEMAND_YES)) {
1945 char id[RRD_ID_LENGTH_MAX + 1];
1948 if(unlikely(!cg->st_cpu_per_core)) {
1949 cg->st_cpu_per_core = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "cpu_per_core");
1950 if(likely(!cg->st_cpu_per_core)) {
1951 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);
1952 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);
1954 for(i = 0; i < cg->cpuacct_usage.cpus; i++) {
1955 snprintfz(id, CHART_TITLE_MAX, "cpu%u", i);
1956 rrddim_add(cg->st_cpu_per_core, id, NULL, 100, 1000000000, RRDDIM_INCREMENTAL);
1960 rrdset_next(cg->st_cpu_per_core);
1962 for(i = 0; i < cg->cpuacct_usage.cpus ;i++) {
1963 snprintfz(id, CHART_TITLE_MAX, "cpu%u", i);
1964 rrddim_set(cg->st_cpu_per_core, id, cg->cpuacct_usage.cpu_percpu[i]);
1966 rrdset_done(cg->st_cpu_per_core);
1969 if(likely(cg->memory.updated_detailed && cg->memory.enabled_detailed == CONFIG_ONDEMAND_YES)) {
1970 if(unlikely(!cg->st_mem)) {
1971 cg->st_mem = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem");
1972 if(likely(!cg->st_mem)) {
1973 snprintfz(title, CHART_TITLE_MAX, "Memory Usage for cgroup %s", cg->chart_title);
1974 cg->st_mem = rrdset_create(type, "mem", NULL, "mem", "cgroup.mem", title, "MB", CHART_PRIORITY_CONTAINERS + 210, update_every, RRDSET_TYPE_STACKED);
1977 rrddim_add(cg->st_mem, "cache", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1978 rrddim_add(cg->st_mem, "rss", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1979 if(cg->memory.detailed_has_swap)
1980 rrddim_add(cg->st_mem, "swap", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1981 rrddim_add(cg->st_mem, "rss_huge", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1982 rrddim_add(cg->st_mem, "mapped_file", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1985 rrdset_next(cg->st_mem);
1987 rrddim_set(cg->st_mem, "cache", cg->memory.cache);
1988 rrddim_set(cg->st_mem, "rss", cg->memory.rss);
1989 if(cg->memory.detailed_has_swap)
1990 rrddim_set(cg->st_mem, "swap", cg->memory.swap);
1991 rrddim_set(cg->st_mem, "rss_huge", cg->memory.rss_huge);
1992 rrddim_set(cg->st_mem, "mapped_file", cg->memory.mapped_file);
1993 rrdset_done(cg->st_mem);
1995 if(unlikely(!cg->st_writeback)) {
1996 cg->st_writeback = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "writeback");
1997 if(likely(!cg->st_writeback)) {
1998 snprintfz(title, CHART_TITLE_MAX, "Writeback Memory for cgroup %s", cg->chart_title);
1999 cg->st_writeback = rrdset_create(type, "writeback", NULL, "mem", "cgroup.writeback", title, "MB", CHART_PRIORITY_CONTAINERS + 300, update_every, RRDSET_TYPE_AREA);
2002 if(cg->memory.detailed_has_dirty)
2003 rrddim_add(cg->st_writeback, "dirty", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2004 rrddim_add(cg->st_writeback, "writeback", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2007 rrdset_next(cg->st_writeback);
2009 if(cg->memory.detailed_has_dirty)
2010 rrddim_set(cg->st_writeback, "dirty", cg->memory.dirty);
2011 rrddim_set(cg->st_writeback, "writeback", cg->memory.writeback);
2012 rrdset_done(cg->st_writeback);
2014 if(unlikely(!cg->st_mem_activity)) {
2015 cg->st_mem_activity = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_activity");
2016 if(likely(!cg->st_mem_activity)) {
2017 snprintfz(title, CHART_TITLE_MAX, "Memory Activity for cgroup %s", cg->chart_title);
2018 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);
2020 rrddim_add(cg->st_mem_activity, "pgpgin", "in", system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
2021 rrddim_add(cg->st_mem_activity, "pgpgout", "out", -system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
2024 rrdset_next(cg->st_mem_activity);
2026 rrddim_set(cg->st_mem_activity, "pgpgin", cg->memory.pgpgin);
2027 rrddim_set(cg->st_mem_activity, "pgpgout", cg->memory.pgpgout);
2028 rrdset_done(cg->st_mem_activity);
2030 if(unlikely(!cg->st_pgfaults)) {
2031 cg->st_pgfaults = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "pgfaults");
2032 if(likely(!cg->st_pgfaults)) {
2033 snprintfz(title, CHART_TITLE_MAX, "Memory Page Faults for cgroup %s", cg->chart_title);
2034 cg->st_pgfaults = rrdset_create(type, "pgfaults", NULL, "mem", "cgroup.pgfaults", title, "MB/s", CHART_PRIORITY_CONTAINERS + 500, update_every, RRDSET_TYPE_LINE);
2036 rrddim_add(cg->st_pgfaults, "pgfault", NULL, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
2037 rrddim_add(cg->st_pgfaults, "pgmajfault", "swap", -system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
2040 rrdset_next(cg->st_pgfaults);
2042 rrddim_set(cg->st_pgfaults, "pgfault", cg->memory.pgfault);
2043 rrddim_set(cg->st_pgfaults, "pgmajfault", cg->memory.pgmajfault);
2044 rrdset_done(cg->st_pgfaults);
2047 if(likely(cg->memory.updated_usage_in_bytes && cg->memory.enabled_usage_in_bytes == CONFIG_ONDEMAND_YES)) {
2048 if(unlikely(!cg->st_mem_usage)) {
2049 cg->st_mem_usage = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_usage");
2050 if(likely(!cg->st_mem_usage)) {
2051 snprintfz(title, CHART_TITLE_MAX, "Used Memory %sfor cgroup %s", (cgroup_used_memory_without_cache && cg->memory.updated_detailed)?"without Cache ":"", cg->chart_title);
2052 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);
2054 rrddim_add(cg->st_mem_usage, "ram", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2055 rrddim_add(cg->st_mem_usage, "swap", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2058 rrdset_next(cg->st_mem_usage);
2060 rrddim_set(cg->st_mem_usage, "ram", cg->memory.usage_in_bytes - ((cgroup_used_memory_without_cache)?cg->memory.cache:0));
2061 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);
2062 rrdset_done(cg->st_mem_usage);
2065 if(likely(cg->memory.updated_failcnt && cg->memory.enabled_failcnt == CONFIG_ONDEMAND_YES)) {
2066 if(unlikely(!cg->st_mem_failcnt)) {
2067 cg->st_mem_failcnt = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_failcnt");
2068 if(likely(!cg->st_mem_failcnt)) {
2069 snprintfz(title, CHART_TITLE_MAX, "Memory Limit Failures for cgroup %s", cg->chart_title);
2070 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);
2072 rrddim_add(cg->st_mem_failcnt, "failures", NULL, 1, 1, RRDDIM_INCREMENTAL);
2075 rrdset_next(cg->st_mem_failcnt);
2077 rrddim_set(cg->st_mem_failcnt, "failures", cg->memory.failcnt);
2078 rrdset_done(cg->st_mem_failcnt);
2081 if(likely(cg->io_service_bytes.updated && cg->io_service_bytes.enabled == CONFIG_ONDEMAND_YES)) {
2082 if(unlikely(!cg->st_io)) {
2083 cg->st_io = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "io");
2084 if(likely(!cg->st_io)) {
2085 snprintfz(title, CHART_TITLE_MAX, "I/O Bandwidth (all disks) for cgroup %s", cg->chart_title);
2086 cg->st_io = rrdset_create(type, "io", NULL, "disk", "cgroup.io", title, "KB/s", CHART_PRIORITY_CONTAINERS + 1200, update_every, RRDSET_TYPE_AREA);
2088 rrddim_add(cg->st_io, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
2089 rrddim_add(cg->st_io, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
2092 rrdset_next(cg->st_io);
2094 rrddim_set(cg->st_io, "read", cg->io_service_bytes.Read);
2095 rrddim_set(cg->st_io, "write", cg->io_service_bytes.Write);
2096 rrdset_done(cg->st_io);
2099 if(likely(cg->io_serviced.updated && cg->io_serviced.enabled == CONFIG_ONDEMAND_YES)) {
2100 if(unlikely(!cg->st_serviced_ops)) {
2101 cg->st_serviced_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "serviced_ops");
2102 if(likely(!cg->st_serviced_ops)) {
2103 snprintfz(title, CHART_TITLE_MAX, "Serviced I/O Operations (all disks) for cgroup %s", cg->chart_title);
2104 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);
2106 rrddim_add(cg->st_serviced_ops, "read", NULL, 1, 1, RRDDIM_INCREMENTAL);
2107 rrddim_add(cg->st_serviced_ops, "write", NULL, -1, 1, RRDDIM_INCREMENTAL);
2110 rrdset_next(cg->st_serviced_ops);
2112 rrddim_set(cg->st_serviced_ops, "read", cg->io_serviced.Read);
2113 rrddim_set(cg->st_serviced_ops, "write", cg->io_serviced.Write);
2114 rrdset_done(cg->st_serviced_ops);
2117 if(likely(cg->throttle_io_service_bytes.updated && cg->throttle_io_service_bytes.enabled == CONFIG_ONDEMAND_YES)) {
2118 if(unlikely(!cg->st_throttle_io)) {
2119 cg->st_throttle_io = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "throttle_io");
2120 if(likely(!cg->st_throttle_io)) {
2121 snprintfz(title, CHART_TITLE_MAX, "Throttle I/O Bandwidth (all disks) for cgroup %s", cg->chart_title);
2122 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);
2124 rrddim_add(cg->st_throttle_io, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
2125 rrddim_add(cg->st_throttle_io, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
2128 rrdset_next(cg->st_throttle_io);
2130 rrddim_set(cg->st_throttle_io, "read", cg->throttle_io_service_bytes.Read);
2131 rrddim_set(cg->st_throttle_io, "write", cg->throttle_io_service_bytes.Write);
2132 rrdset_done(cg->st_throttle_io);
2135 if(likely(cg->throttle_io_serviced.updated && cg->throttle_io_serviced.enabled == CONFIG_ONDEMAND_YES)) {
2136 if(unlikely(!cg->st_throttle_serviced_ops)) {
2137 cg->st_throttle_serviced_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "throttle_serviced_ops");
2138 if(likely(!cg->st_throttle_serviced_ops)) {
2139 snprintfz(title, CHART_TITLE_MAX, "Throttle Serviced I/O Operations (all disks) for cgroup %s", cg->chart_title);
2140 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);
2142 rrddim_add(cg->st_throttle_serviced_ops, "read", NULL, 1, 1, RRDDIM_INCREMENTAL);
2143 rrddim_add(cg->st_throttle_serviced_ops, "write", NULL, -1, 1, RRDDIM_INCREMENTAL);
2146 rrdset_next(cg->st_throttle_serviced_ops);
2148 rrddim_set(cg->st_throttle_serviced_ops, "read", cg->throttle_io_serviced.Read);
2149 rrddim_set(cg->st_throttle_serviced_ops, "write", cg->throttle_io_serviced.Write);
2150 rrdset_done(cg->st_throttle_serviced_ops);
2153 if(likely(cg->io_queued.updated && cg->io_queued.enabled == CONFIG_ONDEMAND_YES)) {
2154 if(unlikely(!cg->st_queued_ops)) {
2155 cg->st_queued_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "queued_ops");
2156 if(likely(!cg->st_queued_ops)) {
2157 snprintfz(title, CHART_TITLE_MAX, "Queued I/O Operations (all disks) for cgroup %s", cg->chart_title);
2158 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);
2160 rrddim_add(cg->st_queued_ops, "read", NULL, 1, 1, RRDDIM_ABSOLUTE);
2161 rrddim_add(cg->st_queued_ops, "write", NULL, -1, 1, RRDDIM_ABSOLUTE);
2164 rrdset_next(cg->st_queued_ops);
2166 rrddim_set(cg->st_queued_ops, "read", cg->io_queued.Read);
2167 rrddim_set(cg->st_queued_ops, "write", cg->io_queued.Write);
2168 rrdset_done(cg->st_queued_ops);
2171 if(likely(cg->io_merged.updated && cg->io_merged.enabled == CONFIG_ONDEMAND_YES)) {
2172 if(unlikely(!cg->st_merged_ops)) {
2173 cg->st_merged_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "merged_ops");
2174 if(likely(!cg->st_merged_ops)) {
2175 snprintfz(title, CHART_TITLE_MAX, "Merged I/O Operations (all disks) for cgroup %s", cg->chart_title);
2176 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);
2178 rrddim_add(cg->st_merged_ops, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
2179 rrddim_add(cg->st_merged_ops, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
2182 rrdset_next(cg->st_merged_ops);
2184 rrddim_set(cg->st_merged_ops, "read", cg->io_merged.Read);
2185 rrddim_set(cg->st_merged_ops, "write", cg->io_merged.Write);
2186 rrdset_done(cg->st_merged_ops);
2190 if(likely(cgroup_enable_systemd_services))
2191 update_services_charts(update_every,
2193 services_do_mem_usage,
2194 services_do_mem_detailed,
2195 services_do_mem_failcnt,
2196 services_do_swap_usage,
2199 services_do_throttle_io,
2200 services_do_throttle_ops,
2201 services_do_queued_ops,
2202 services_do_merged_ops
2205 debug(D_CGROUP, "done updating cgroups charts");
2208 // ----------------------------------------------------------------------------
2211 void *cgroups_main(void *ptr) {
2212 struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
2214 info("CGROUP Plugin thread created with task id %d", gettid());
2216 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
2217 error("Cannot set pthread cancel type to DEFERRED.");
2219 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
2220 error("Cannot set pthread cancel state to ENABLE.");
2222 struct rusage thread;
2224 // when ZERO, attempt to do it
2225 int vdo_cpu_netdata = config_get_boolean("plugin:cgroups", "cgroups plugin resource charts", 1);
2227 read_cgroup_plugin_configuration();
2229 RRDSET *stcpu_thread = NULL;
2231 usec_t step = cgroup_update_every * USEC_PER_SEC;
2232 usec_t find_every = cgroup_check_for_new_every * USEC_PER_SEC, find_next = 0;
2234 usec_t now = now_monotonic_usec();
2235 usec_t next = now - (now % step) + step;
2238 sleep_usec(next - now);
2239 now = now_monotonic_usec();
2242 if(unlikely(netdata_exit)) break;
2244 // BEGIN -- the job to be done
2246 if(unlikely(now >= find_next)) {
2248 find_next = now + find_every;
2251 read_all_cgroups(cgroup_root);
2252 update_cgroup_charts(cgroup_update_every);
2254 // END -- the job is done
2256 // --------------------------------------------------------------------
2258 if(vdo_cpu_netdata) {
2259 getrusage(RUSAGE_THREAD, &thread);
2261 if(unlikely(!stcpu_thread)) {
2262 stcpu_thread = rrdset_find("netdata.plugin_cgroups_cpu");
2263 if(unlikely(!stcpu_thread))
2264 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);
2266 rrddim_add(stcpu_thread, "user", NULL, 1, 1000, RRDDIM_INCREMENTAL);
2267 rrddim_add(stcpu_thread, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
2270 rrdset_next(stcpu_thread);
2272 rrddim_set(stcpu_thread, "user" , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
2273 rrddim_set(stcpu_thread, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
2274 rrdset_done(stcpu_thread);
2278 info("CGROUP thread exiting");
2280 static_thread->enabled = 0;