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_swap;
319 unsigned long long cache;
320 unsigned long long rss;
321 unsigned long long rss_huge;
322 unsigned long long mapped_file;
323 unsigned long long writeback;
324 unsigned long long dirty;
325 unsigned long long swap;
326 unsigned long long pgpgin;
327 unsigned long long pgpgout;
328 unsigned long long pgfault;
329 unsigned long long pgmajfault;
331 unsigned long long inactive_anon;
332 unsigned long long active_anon;
333 unsigned long long inactive_file;
334 unsigned long long active_file;
335 unsigned long long unevictable;
336 unsigned long long hierarchical_memory_limit;
337 unsigned long long total_cache;
338 unsigned long long total_rss;
339 unsigned long long total_rss_huge;
340 unsigned long long total_mapped_file;
341 unsigned long long total_writeback;
342 unsigned long long total_dirty;
343 unsigned long long total_swap;
344 unsigned long long total_pgpgin;
345 unsigned long long total_pgpgout;
346 unsigned long long total_pgfault;
347 unsigned long long total_pgmajfault;
348 unsigned long long total_inactive_anon;
349 unsigned long long total_active_anon;
350 unsigned long long total_inactive_file;
351 unsigned long long total_active_file;
352 unsigned long long total_unevictable;
355 // single file metrics
356 unsigned long long usage_in_bytes;
357 unsigned long long msw_usage_in_bytes;
358 unsigned long long failcnt;
361 // https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt
362 struct cpuacct_stat {
364 int enabled; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
368 unsigned long long user;
369 unsigned long long system;
372 // https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt
373 struct cpuacct_usage {
375 int enabled; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
380 unsigned long long *cpu_percpu;
383 #define CGROUP_OPTIONS_DISABLED_DUPLICATE 0x00000001
384 #define CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE 0x00000002
389 char available; // found in the filesystem
390 char enabled; // enabled in the config
400 struct cpuacct_stat cpuacct_stat;
401 struct cpuacct_usage cpuacct_usage;
403 struct memory memory;
405 struct blkio io_service_bytes; // bytes
406 struct blkio io_serviced; // operations
408 struct blkio throttle_io_service_bytes; // bytes
409 struct blkio throttle_io_serviced; // operations
411 struct blkio io_merged; // operations
412 struct blkio io_queued; // operations
416 RRDSET *st_cpu_per_core;
418 RRDSET *st_writeback;
419 RRDSET *st_mem_activity;
421 RRDSET *st_mem_usage;
422 RRDSET *st_mem_failcnt;
424 RRDSET *st_serviced_ops;
425 RRDSET *st_throttle_io;
426 RRDSET *st_throttle_serviced_ops;
427 RRDSET *st_queued_ops;
428 RRDSET *st_merged_ops;
432 RRDDIM *rd_mem_usage;
433 RRDDIM *rd_mem_failcnt;
434 RRDDIM *rd_swap_usage;
436 RRDDIM *rd_mem_detailed_cache;
437 RRDDIM *rd_mem_detailed_rss;
438 RRDDIM *rd_mem_detailed_mapped;
439 RRDDIM *rd_mem_detailed_writeback;
440 RRDDIM *rd_mem_detailed_dirty;
441 RRDDIM *rd_mem_detailed_swap;
442 RRDDIM *rd_mem_detailed_pgpgin;
443 RRDDIM *rd_mem_detailed_pgpgout;
444 RRDDIM *rd_mem_detailed_pgfault;
445 RRDDIM *rd_mem_detailed_pgmajfault;
447 RRDDIM *rd_io_service_bytes_read;
448 RRDDIM *rd_io_serviced_read;
449 RRDDIM *rd_throttle_io_read;
450 RRDDIM *rd_throttle_io_serviced_read;
451 RRDDIM *rd_io_queued_read;
452 RRDDIM *rd_io_merged_read;
454 RRDDIM *rd_io_service_bytes_write;
455 RRDDIM *rd_io_serviced_write;
456 RRDDIM *rd_throttle_io_write;
457 RRDDIM *rd_throttle_io_serviced_write;
458 RRDDIM *rd_io_queued_write;
459 RRDDIM *rd_io_merged_write;
463 } *cgroup_root = NULL;
465 // ----------------------------------------------------------------------------
466 // read values from /sys
468 static inline void cgroup_read_cpuacct_stat(struct cpuacct_stat *cp) {
469 static procfile *ff = NULL;
471 if(likely(cp->filename)) {
472 ff = procfile_reopen(ff, cp->filename, NULL, PROCFILE_FLAG_DEFAULT);
478 ff = procfile_readall(ff);
484 unsigned long i, lines = procfile_lines(ff);
486 if(unlikely(lines < 1)) {
487 error("File '%s' should have 1+ lines.", cp->filename);
492 for(i = 0; i < lines ; i++) {
493 char *s = procfile_lineword(ff, i, 0);
494 uint32_t hash = simple_hash(s);
496 if(unlikely(hash == user_hash && !strcmp(s, "user"))) {
497 cp->user = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
501 if(unlikely(hash == system_hash && !strcmp(s, "system"))) {
502 cp->system = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
509 if(unlikely(cp->enabled == CONFIG_ONDEMAND_ONDEMAND && (cp->user || cp->system)))
510 cp->enabled = CONFIG_ONDEMAND_YES;
514 static inline void cgroup_read_cpuacct_usage(struct cpuacct_usage *ca) {
515 static procfile *ff = NULL;
517 if(likely(ca->filename)) {
518 ff = procfile_reopen(ff, ca->filename, NULL, PROCFILE_FLAG_DEFAULT);
524 ff = procfile_readall(ff);
530 if(unlikely(procfile_lines(ff) < 1)) {
531 error("File '%s' should have 1+ lines but has %u.", ca->filename, procfile_lines(ff));
536 unsigned long i = procfile_linewords(ff, 0);
537 if(unlikely(i == 0)) {
542 // we may have 1 more CPU reported
544 char *s = procfile_lineword(ff, 0, i - 1);
549 if(unlikely(i != ca->cpus)) {
550 freez(ca->cpu_percpu);
551 ca->cpu_percpu = mallocz(sizeof(unsigned long long) * i);
552 ca->cpus = (unsigned int)i;
555 unsigned long long total = 0;
556 for(i = 0; i < ca->cpus ;i++) {
557 unsigned long long n = strtoull(procfile_lineword(ff, 0, i), NULL, 10);
558 ca->cpu_percpu[i] = n;
564 if(unlikely(ca->enabled == CONFIG_ONDEMAND_ONDEMAND && total))
565 ca->enabled = CONFIG_ONDEMAND_YES;
569 static inline void cgroup_read_blkio(struct blkio *io) {
570 static procfile *ff = NULL;
572 if(unlikely(io->enabled == CONFIG_ONDEMAND_ONDEMAND && io->delay_counter > 0)) {
577 if(likely(io->filename)) {
578 ff = procfile_reopen(ff, io->filename, NULL, PROCFILE_FLAG_DEFAULT);
584 ff = procfile_readall(ff);
590 unsigned long i, lines = procfile_lines(ff);
592 if(unlikely(lines < 1)) {
593 error("File '%s' should have 1+ lines.", io->filename);
606 for(i = 0; i < lines ; i++) {
607 char *s = procfile_lineword(ff, i, 1);
608 uint32_t hash = simple_hash(s);
610 if(unlikely(hash == Read_hash && !strcmp(s, "Read"))) {
611 io->Read += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
615 if(unlikely(hash == Write_hash && !strcmp(s, "Write"))) {
616 io->Write += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
621 if(hash == Sync_hash && !strcmp(s, "Sync")) {
622 io->Sync += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
626 if(hash == Async_hash && !strcmp(s, "Async")) {
627 io->Async += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
631 if(hash == Total_hash && !strcmp(s, "Total")) {
632 io->Total += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
640 if(unlikely(io->enabled == CONFIG_ONDEMAND_ONDEMAND)) {
641 if(unlikely(io->Read || io->Write))
642 io->enabled = CONFIG_ONDEMAND_YES;
644 io->delay_counter = cgroup_recheck_zero_blkio_every_iterations;
649 static inline void cgroup_read_memory(struct memory *mem) {
650 static procfile *ff = NULL;
652 // read detailed ram usage
653 if(likely(mem->filename_detailed)) {
654 if(unlikely(mem->enabled_detailed == CONFIG_ONDEMAND_ONDEMAND && mem->delay_counter_detailed > 0)) {
655 mem->delay_counter_detailed--;
659 ff = procfile_reopen(ff, mem->filename_detailed, NULL, PROCFILE_FLAG_DEFAULT);
661 mem->updated_detailed = 0;
665 ff = procfile_readall(ff);
667 mem->updated_detailed = 0;
671 unsigned long i, lines = procfile_lines(ff);
673 if(unlikely(lines < 1)) {
674 error("File '%s' should have 1+ lines.", mem->filename_detailed);
675 mem->updated_detailed = 0;
679 for(i = 0; i < lines ; i++) {
680 char *s = procfile_lineword(ff, i, 0);
681 uint32_t hash = simple_hash(s);
683 if(unlikely(hash == cache_hash && !strcmp(s, "cache"))) {
684 mem->cache = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
688 if(unlikely(hash == rss_hash && !strcmp(s, "rss"))) {
689 mem->rss = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
693 if(unlikely(hash == rss_huge_hash && !strcmp(s, "rss_huge"))) {
694 mem->rss_huge = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
698 if(unlikely(hash == mapped_file_hash && !strcmp(s, "mapped_file"))) {
699 mem->mapped_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
703 if(unlikely(hash == writeback_hash && !strcmp(s, "writeback"))) {
704 mem->writeback = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
708 if(unlikely(hash == dirty_hash && !strcmp(s, "dirty"))) {
709 mem->dirty = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
710 mem->detailed_has_dirty_swap = 1;
714 if(unlikely(hash == swap_hash && !strcmp(s, "swap"))) {
715 mem->swap = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
716 mem->detailed_has_dirty_swap = 1;
720 if(unlikely(hash == pgpgin_hash && !strcmp(s, "pgpgin"))) {
721 mem->pgpgin = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
725 if(unlikely(hash == pgpgout_hash && !strcmp(s, "pgpgout"))) {
726 mem->pgpgout = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
730 if(unlikely(hash == pgfault_hash && !strcmp(s, "pgfault"))) {
731 mem->pgfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
735 if(unlikely(hash == pgmajfault_hash && !strcmp(s, "pgmajfault"))) {
736 mem->pgmajfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
741 if(unlikely(hash == inactive_anon_hash && !strcmp(s, "inactive_anon"))) {
742 mem->inactive_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
746 if(unlikely(hash == active_anon_hash && !strcmp(s, "active_anon"))) {
747 mem->active_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
751 if(unlikely(hash == inactive_file_hash && !strcmp(s, "inactive_file"))) {
752 mem->inactive_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
756 if(unlikely(hash == active_file_hash && !strcmp(s, "active_file"))) {
757 mem->active_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
761 if(unlikely(hash == unevictable_hash && !strcmp(s, "unevictable"))) {
762 mem->unevictable = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
766 if(unlikely(hash == hierarchical_memory_limit_hash && !strcmp(s, "hierarchical_memory_limit"))) {
767 mem->hierarchical_memory_limit = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
771 if(unlikely(hash == total_cache_hash && !strcmp(s, "total_cache"))) {
772 mem->total_cache = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
776 if(unlikely(hash == total_rss_hash && !strcmp(s, "total_rss"))) {
777 mem->total_rss = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
781 if(unlikely(hash == total_rss_huge_hash && !strcmp(s, "total_rss_huge"))) {
782 mem->total_rss_huge = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
786 if(unlikely(hash == total_mapped_file_hash && !strcmp(s, "total_mapped_file"))) {
787 mem->total_mapped_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
791 if(unlikely(hash == total_writeback_hash && !strcmp(s, "total_writeback"))) {
792 mem->total_writeback = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
796 if(unlikely(hash == total_dirty_hash && !strcmp(s, "total_dirty"))) {
797 mem->total_dirty = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
801 if(unlikely(hash == total_swap_hash && !strcmp(s, "total_swap"))) {
802 mem->total_swap = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
806 if(unlikely(hash == total_pgpgin_hash && !strcmp(s, "total_pgpgin"))) {
807 mem->total_pgpgin = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
811 if(unlikely(hash == total_pgpgout_hash && !strcmp(s, "total_pgpgout"))) {
812 mem->total_pgpgout = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
816 if(unlikely(hash == total_pgfault_hash && !strcmp(s, "total_pgfault"))) {
817 mem->total_pgfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
821 if(unlikely(hash == total_pgmajfault_hash && !strcmp(s, "total_pgmajfault"))) {
822 mem->total_pgmajfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
826 if(unlikely(hash == total_inactive_anon_hash && !strcmp(s, "total_inactive_anon"))) {
827 mem->total_inactive_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
831 if(unlikely(hash == total_active_anon_hash && !strcmp(s, "total_active_anon"))) {
832 mem->total_active_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
836 if(unlikely(hash == total_inactive_file_hash && !strcmp(s, "total_inactive_file"))) {
837 mem->total_inactive_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
841 if(unlikely(hash == total_active_file_hash && !strcmp(s, "total_active_file"))) {
842 mem->total_active_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
846 if(unlikely(hash == total_unevictable_hash && !strcmp(s, "total_unevictable"))) {
847 mem->total_unevictable = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
853 // 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);
855 mem->updated_detailed = 1;
857 if(unlikely(mem->enabled_detailed == CONFIG_ONDEMAND_ONDEMAND)) {
858 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)
859 mem->enabled_detailed = CONFIG_ONDEMAND_YES;
861 mem->delay_counter_detailed = cgroup_recheck_zero_mem_detailed_every_iterations;
867 // read usage_in_bytes
868 if(likely(mem->filename_usage_in_bytes)) {
869 if(likely(!read_single_number_file(mem->filename_usage_in_bytes, &mem->usage_in_bytes))) {
870 mem->updated_usage_in_bytes = 1;
872 if(unlikely(mem->enabled_usage_in_bytes == CONFIG_ONDEMAND_ONDEMAND && mem->usage_in_bytes))
873 mem->enabled_usage_in_bytes = CONFIG_ONDEMAND_YES;
876 mem->updated_usage_in_bytes = 0;
879 // read msw_usage_in_bytes
880 if(likely(mem->filename_msw_usage_in_bytes)) {
881 if(likely(!read_single_number_file(mem->filename_msw_usage_in_bytes, &mem->msw_usage_in_bytes))) {
882 mem->updated_msw_usage_in_bytes = 1;
884 if(unlikely(mem->enabled_msw_usage_in_bytes == CONFIG_ONDEMAND_ONDEMAND && mem->msw_usage_in_bytes))
885 mem->enabled_msw_usage_in_bytes = CONFIG_ONDEMAND_YES;
888 mem->updated_msw_usage_in_bytes = 0;
892 if(likely(mem->filename_failcnt)) {
893 if(unlikely(mem->enabled_failcnt == CONFIG_ONDEMAND_ONDEMAND && mem->delay_counter_failcnt > 0)) {
894 mem->updated_failcnt = 0;
895 mem->delay_counter_failcnt--;
897 else if(likely(!read_single_number_file(mem->filename_failcnt, &mem->failcnt))) {
898 mem->updated_failcnt = 1;
900 if(unlikely(mem->enabled_failcnt == CONFIG_ONDEMAND_ONDEMAND)) {
901 if(unlikely(!mem->failcnt))
902 mem->delay_counter_failcnt = cgroup_recheck_zero_mem_failcnt_every_iterations;
904 mem->enabled_failcnt = CONFIG_ONDEMAND_YES;
910 static inline void cgroup_read(struct cgroup *cg) {
911 debug(D_CGROUP, "reading metrics for cgroups '%s'", cg->id);
913 cgroup_read_cpuacct_stat(&cg->cpuacct_stat);
914 cgroup_read_cpuacct_usage(&cg->cpuacct_usage);
915 cgroup_read_memory(&cg->memory);
916 cgroup_read_blkio(&cg->io_service_bytes);
917 cgroup_read_blkio(&cg->io_serviced);
918 cgroup_read_blkio(&cg->throttle_io_service_bytes);
919 cgroup_read_blkio(&cg->throttle_io_serviced);
920 cgroup_read_blkio(&cg->io_merged);
921 cgroup_read_blkio(&cg->io_queued);
924 static inline void read_all_cgroups(struct cgroup *root) {
925 debug(D_CGROUP, "reading metrics for all cgroups");
929 for(cg = root; cg ; cg = cg->next)
930 if(cg->enabled && cg->available)
934 // ----------------------------------------------------------------------------
935 // add/remove/find cgroup objects
937 #define CGROUP_CHARTID_LINE_MAX 1024
939 static inline char *cgroup_title_strdupz(const char *s) {
940 if(!s || !*s) s = "/";
942 if(*s == '/' && s[1] != '\0') s++;
944 char *r = strdupz(s);
945 netdata_fix_chart_name(r);
950 static inline char *cgroup_chart_id_strdupz(const char *s) {
951 if(!s || !*s) s = "/";
953 if(*s == '/' && s[1] != '\0') s++;
955 char *r = strdupz(s);
956 netdata_fix_chart_id(r);
961 static inline void cgroup_get_chart_name(struct cgroup *cg) {
962 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);
965 char buffer[CGROUP_CHARTID_LINE_MAX + 1];
967 snprintfz(buffer, CGROUP_CHARTID_LINE_MAX, "exec %s '%s'", cgroups_rename_script, cg->chart_id);
969 debug(D_CGROUP, "executing command '%s' for cgroup '%s'", buffer, cg->id);
970 FILE *fp = mypopen(buffer, &cgroup_pid);
972 // debug(D_CGROUP, "reading from command '%s' for cgroup '%s'", buffer, cg->id);
973 char *s = fgets(buffer, CGROUP_CHARTID_LINE_MAX, fp);
974 // debug(D_CGROUP, "closing command for cgroup '%s'", cg->id);
975 mypclose(fp, cgroup_pid);
976 // debug(D_CGROUP, "closed command for cgroup '%s'", cg->id);
978 if(s && *s && *s != '\n') {
979 debug(D_CGROUP, "cgroup '%s' should be renamed to '%s'", cg->id, s);
983 freez(cg->chart_title);
984 cg->chart_title = cgroup_title_strdupz(s);
987 cg->chart_id = cgroup_chart_id_strdupz(s);
988 cg->hash_chart = simple_hash(cg->chart_id);
992 error("CGROUP: Cannot popen(\"%s\", \"r\").", buffer);
995 static inline struct cgroup *cgroup_add(const char *id) {
996 if(!id || !*id) id = "/";
997 debug(D_CGROUP, "adding to list, cgroup with id '%s'", id);
999 if(cgroup_root_count >= cgroup_root_max) {
1000 info("Maximum number of cgroups reached (%d). Not adding cgroup '%s'", cgroup_root_count, id);
1004 int def = simple_pattern_matches(enabled_cgroup_patterns, id)?cgroup_enable_new_cgroups_detected_at_runtime:0;
1005 struct cgroup *cg = callocz(1, sizeof(struct cgroup));
1007 cg->id = strdupz(id);
1008 cg->hash = simple_hash(cg->id);
1010 cg->chart_title = cgroup_title_strdupz(id);
1012 cg->chart_id = cgroup_chart_id_strdupz(id);
1013 cg->hash_chart = simple_hash(cg->chart_id);
1020 for(e = cgroup_root; e->next ;e = e->next) ;
1024 cgroup_root_count++;
1026 // fix the chart_id and title by calling the external script
1027 if(simple_pattern_matches(enabled_cgroup_renames, cg->id)) {
1029 cgroup_get_chart_name(cg);
1031 debug(D_CGROUP, "cgroup '%s' renamed to '%s' (title: '%s')", cg->id, cg->chart_id, cg->chart_title);
1034 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);
1036 int user_configurable = 1;
1038 // check if this cgroup should be a systemd service
1039 if(cgroup_enable_systemd_services) {
1040 if(simple_pattern_matches(systemd_services_cgroups, cg->id) ||
1041 simple_pattern_matches(systemd_services_cgroups, cg->chart_id)) {
1042 debug(D_CGROUP, "cgroup '%s' with chart id '%s' (title: '%s') matches systemd services cgroups", cg->id, cg->chart_id, cg->chart_title);
1044 char buffer[CGROUP_CHARTID_LINE_MAX + 1];
1045 cg->options |= CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE;
1047 strncpy(buffer, cg->id, CGROUP_CHARTID_LINE_MAX);
1050 //freez(cg->chart_id);
1051 //cg->chart_id = cgroup_chart_id_strdupz(s);
1052 //cg->hash_chart = simple_hash(cg->chart_id);
1054 // skip to the last slash
1055 size_t len = strlen(s);
1056 while(len--) if(unlikely(s[len] == '/')) break;
1057 if(len) s = &s[len + 1];
1061 while(len--) if(unlikely(s[len] == '.')) break;
1062 if(len) s[len] = '\0';
1064 freez(cg->chart_title);
1065 cg->chart_title = cgroup_title_strdupz(s);
1068 user_configurable = 0;
1070 debug(D_CGROUP, "cgroup '%s' renamed to '%s' (title: '%s')", cg->id, cg->chart_id, cg->chart_title);
1073 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);
1076 if(user_configurable) {
1077 // allow the user to enable/disable this individualy
1078 char option[FILENAME_MAX + 1];
1079 snprintfz(option, FILENAME_MAX, "enable cgroup %s", cg->chart_title);
1080 cg->enabled = (char) config_get_boolean("plugin:cgroups", option, def);
1083 // detect duplicate cgroups
1086 for (t = cgroup_root; t; t = t->next) {
1087 if (t != cg && t->enabled && t->hash_chart == cg->hash_chart && !strcmp(t->chart_id, cg->chart_id)) {
1088 if (!strncmp(t->chart_id, "/system.slice/", 14) && !strncmp(cg->chart_id, "/init.scope/system.slice/", 25)) {
1089 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'.",
1090 cg->chart_id, t->id, cg->id, t->id);
1091 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'.",
1092 cg->chart_id, t->id, cg->id, t->id);
1094 t->options |= CGROUP_OPTIONS_DISABLED_DUPLICATE;
1097 error("Control group with chart id '%s' already exists with id '%s' and is enabled and available. Disabling cgroup with id '%s'.",
1098 cg->chart_id, t->id, cg->id);
1099 debug(D_CGROUP, "Control group with chart id '%s' already exists with id '%s' and is enabled and available. Disabling cgroup with id '%s'.",
1100 cg->chart_id, t->id, cg->id);
1102 cg->options |= CGROUP_OPTIONS_DISABLED_DUPLICATE;
1110 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");
1115 static inline void cgroup_free(struct cgroup *cg) {
1116 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");
1118 freez(cg->cpuacct_usage.cpu_percpu);
1120 freez(cg->cpuacct_stat.filename);
1121 freez(cg->cpuacct_usage.filename);
1123 freez(cg->memory.filename_detailed);
1124 freez(cg->memory.filename_failcnt);
1125 freez(cg->memory.filename_usage_in_bytes);
1126 freez(cg->memory.filename_msw_usage_in_bytes);
1128 freez(cg->io_service_bytes.filename);
1129 freez(cg->io_serviced.filename);
1131 freez(cg->throttle_io_service_bytes.filename);
1132 freez(cg->throttle_io_serviced.filename);
1134 freez(cg->io_merged.filename);
1135 freez(cg->io_queued.filename);
1138 freez(cg->chart_id);
1139 freez(cg->chart_title);
1143 cgroup_root_count--;
1146 // find if a given cgroup exists
1147 static inline struct cgroup *cgroup_find(const char *id) {
1148 debug(D_CGROUP, "searching for cgroup '%s'", id);
1150 uint32_t hash = simple_hash(id);
1153 for(cg = cgroup_root; cg ; cg = cg->next) {
1154 if(hash == cg->hash && strcmp(id, cg->id) == 0)
1158 debug(D_CGROUP, "cgroup '%s' %s in memory", id, (cg)?"found":"not found");
1162 // ----------------------------------------------------------------------------
1163 // detect running cgroups
1165 // callback for find_file_in_subdirs()
1166 static inline void found_subdir_in_dir(const char *dir) {
1167 debug(D_CGROUP, "examining cgroup dir '%s'", dir);
1169 struct cgroup *cg = cgroup_find(dir);
1171 if(*dir && cgroup_max_depth > 0) {
1175 for(s = dir; *s ;s++)
1176 if(unlikely(*s == '/'))
1179 if(depth > cgroup_max_depth) {
1180 info("cgroup '%s' is too deep (%d, while max is %d)", dir, depth, cgroup_max_depth);
1184 // debug(D_CGROUP, "will add dir '%s' as cgroup", dir);
1185 cg = cgroup_add(dir);
1188 if(cg) cg->available = 1;
1191 static inline int find_dir_in_subdirs(const char *base, const char *this, void (*callback)(const char *)) {
1192 if(!this) this = base;
1193 debug(D_CGROUP, "searching for directories in '%s' (base '%s')", this?this:"", base);
1195 size_t dirlen = strlen(this), baselen = strlen(base);
1200 const char *relative_path = &this[baselen];
1201 if(!*relative_path) relative_path = "/";
1203 DIR *dir = opendir(this);
1205 error("Cannot read cgroups directory '%s'", base);
1210 callback(relative_path);
1212 struct dirent *de = NULL;
1213 while((de = readdir(dir))) {
1214 if(de->d_type == DT_DIR
1216 (de->d_name[0] == '.' && de->d_name[1] == '\0')
1217 || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
1221 if(de->d_type == DT_DIR) {
1223 const char *r = relative_path;
1224 if(*r == '\0') r = "/";
1226 // do not decent in directories we are not interested
1227 int def = simple_pattern_matches(enabled_cgroup_paths, r);
1229 // we check for this option here
1230 // so that the config will not have settings
1231 // for leaf directories
1232 char option[FILENAME_MAX + 1];
1233 snprintfz(option, FILENAME_MAX, "search for cgroups under %s", r);
1234 option[FILENAME_MAX] = '\0';
1235 enabled = config_get_boolean("plugin:cgroups", option, def);
1239 char *s = mallocz(dirlen + strlen(de->d_name) + 2);
1242 strcat(s, de->d_name);
1243 int ret2 = find_dir_in_subdirs(base, s, callback);
1244 if(ret2 > 0) ret += ret2;
1254 static inline void mark_all_cgroups_as_not_available() {
1255 debug(D_CGROUP, "marking all cgroups as not available");
1259 // mark all as not available
1260 for(cg = cgroup_root; cg ; cg = cg->next) {
1265 static inline void cleanup_all_cgroups() {
1266 struct cgroup *cg = cgroup_root, *last = NULL;
1269 if(!cg->available) {
1270 // enable the first duplicate cgroup
1273 for(t = cgroup_root; t ; t = t->next) {
1274 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)) {
1275 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);
1277 t->options &= ~CGROUP_OPTIONS_DISABLED_DUPLICATE;
1284 cgroup_root = cg->next;
1286 last->next = cg->next;
1302 static inline void find_all_cgroups() {
1303 debug(D_CGROUP, "searching for cgroups");
1305 mark_all_cgroups_as_not_available();
1307 if(cgroup_enable_cpuacct_stat || cgroup_enable_cpuacct_usage) {
1308 if(find_dir_in_subdirs(cgroup_cpuacct_base, NULL, found_subdir_in_dir) == -1) {
1309 cgroup_enable_cpuacct_stat =
1310 cgroup_enable_cpuacct_usage = CONFIG_ONDEMAND_NO;
1311 error("disabled CGROUP cpu statistics.");
1315 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) {
1316 if(find_dir_in_subdirs(cgroup_blkio_base, NULL, found_subdir_in_dir) == -1) {
1317 cgroup_enable_blkio_io =
1318 cgroup_enable_blkio_ops =
1319 cgroup_enable_blkio_throttle_io =
1320 cgroup_enable_blkio_throttle_ops =
1321 cgroup_enable_blkio_merged_ops =
1322 cgroup_enable_blkio_queued_ops = CONFIG_ONDEMAND_NO;
1323 error("disabled CGROUP blkio statistics.");
1327 if(cgroup_enable_memory || cgroup_enable_detailed_memory || cgroup_enable_swap || cgroup_enable_memory_failcnt) {
1328 if(find_dir_in_subdirs(cgroup_memory_base, NULL, found_subdir_in_dir) == -1) {
1329 cgroup_enable_memory =
1330 cgroup_enable_detailed_memory =
1331 cgroup_enable_swap =
1332 cgroup_enable_memory_failcnt = CONFIG_ONDEMAND_NO;
1333 error("disabled CGROUP memory statistics.");
1337 if(cgroup_search_in_devices) {
1338 if(find_dir_in_subdirs(cgroup_devices_base, NULL, found_subdir_in_dir) == -1) {
1339 cgroup_search_in_devices = 0;
1340 error("disabled CGROUP devices statistics.");
1344 // remove any non-existing cgroups
1345 cleanup_all_cgroups();
1349 for(cg = cgroup_root; cg ; cg = cg->next) {
1350 // fprintf(stderr, " >>> CGROUP '%s' (%u - %s) with name '%s'\n", cg->id, cg->hash, cg->available?"available":"stopped", cg->name);
1352 if(unlikely(!cg->available))
1355 debug(D_CGROUP, "checking paths for cgroup '%s'", cg->id);
1357 // check for newly added cgroups
1358 // and update the filenames they read
1359 char filename[FILENAME_MAX + 1];
1360 if(unlikely(cgroup_enable_cpuacct_stat && !cg->cpuacct_stat.filename)) {
1361 snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.stat", cgroup_cpuacct_base, cg->id);
1362 if(likely(stat(filename, &buf) != -1)) {
1363 cg->cpuacct_stat.filename = strdupz(filename);
1364 cg->cpuacct_stat.enabled = cgroup_enable_cpuacct_stat;
1365 debug(D_CGROUP, "cpuacct.stat filename for cgroup '%s': '%s'", cg->id, cg->cpuacct_stat.filename);
1368 debug(D_CGROUP, "cpuacct.stat file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1371 if(unlikely(cgroup_enable_cpuacct_usage && !cg->cpuacct_usage.filename && !(cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE))) {
1372 snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.usage_percpu", cgroup_cpuacct_base, cg->id);
1373 if(likely(stat(filename, &buf) != -1)) {
1374 cg->cpuacct_usage.filename = strdupz(filename);
1375 cg->cpuacct_usage.enabled = cgroup_enable_cpuacct_usage;
1376 debug(D_CGROUP, "cpuacct.usage_percpu filename for cgroup '%s': '%s'", cg->id, cg->cpuacct_usage.filename);
1379 debug(D_CGROUP, "cpuacct.usage_percpu file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1382 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)))) {
1383 snprintfz(filename, FILENAME_MAX, "%s%s/memory.stat", cgroup_memory_base, cg->id);
1384 if(likely(stat(filename, &buf) != -1)) {
1385 cg->memory.filename_detailed = strdupz(filename);
1386 cg->memory.enabled_detailed = (cgroup_enable_detailed_memory == CONFIG_ONDEMAND_YES)?CONFIG_ONDEMAND_YES:CONFIG_ONDEMAND_ONDEMAND;
1387 debug(D_CGROUP, "memory.stat filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_detailed);
1390 debug(D_CGROUP, "memory.stat file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1393 if(unlikely(cgroup_enable_memory && !cg->memory.filename_usage_in_bytes)) {
1394 snprintfz(filename, FILENAME_MAX, "%s%s/memory.usage_in_bytes", cgroup_memory_base, cg->id);
1395 if(likely(stat(filename, &buf) != -1)) {
1396 cg->memory.filename_usage_in_bytes = strdupz(filename);
1397 cg->memory.enabled_usage_in_bytes = cgroup_enable_memory;
1398 debug(D_CGROUP, "memory.usage_in_bytes filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_usage_in_bytes);
1401 debug(D_CGROUP, "memory.usage_in_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1404 if(unlikely(cgroup_enable_swap && !cg->memory.filename_msw_usage_in_bytes)) {
1405 snprintfz(filename, FILENAME_MAX, "%s%s/memory.msw_usage_in_bytes", cgroup_memory_base, cg->id);
1406 if(likely(stat(filename, &buf) != -1)) {
1407 cg->memory.filename_msw_usage_in_bytes = strdupz(filename);
1408 cg->memory.enabled_msw_usage_in_bytes = cgroup_enable_swap;
1409 debug(D_CGROUP, "memory.msw_usage_in_bytes filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_msw_usage_in_bytes);
1412 debug(D_CGROUP, "memory.msw_usage_in_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1415 if(unlikely(cgroup_enable_memory_failcnt && !cg->memory.filename_failcnt)) {
1416 snprintfz(filename, FILENAME_MAX, "%s%s/memory.failcnt", cgroup_memory_base, cg->id);
1417 if(likely(stat(filename, &buf) != -1)) {
1418 cg->memory.filename_failcnt = strdupz(filename);
1419 cg->memory.enabled_failcnt = cgroup_enable_memory_failcnt;
1420 debug(D_CGROUP, "memory.failcnt filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_failcnt);
1423 debug(D_CGROUP, "memory.failcnt file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1426 if(unlikely(cgroup_enable_blkio_io && !cg->io_service_bytes.filename)) {
1427 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_service_bytes", cgroup_blkio_base, cg->id);
1428 if(likely(stat(filename, &buf) != -1)) {
1429 cg->io_service_bytes.filename = strdupz(filename);
1430 cg->io_service_bytes.enabled = cgroup_enable_blkio_io;
1431 debug(D_CGROUP, "io_service_bytes filename for cgroup '%s': '%s'", cg->id, cg->io_service_bytes.filename);
1434 debug(D_CGROUP, "io_service_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1437 if(unlikely(cgroup_enable_blkio_ops && !cg->io_serviced.filename)) {
1438 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_serviced", cgroup_blkio_base, cg->id);
1439 if(likely(stat(filename, &buf) != -1)) {
1440 cg->io_serviced.filename = strdupz(filename);
1441 cg->io_serviced.enabled = cgroup_enable_blkio_ops;
1442 debug(D_CGROUP, "io_serviced filename for cgroup '%s': '%s'", cg->id, cg->io_serviced.filename);
1445 debug(D_CGROUP, "io_serviced file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1448 if(unlikely(cgroup_enable_blkio_throttle_io && !cg->throttle_io_service_bytes.filename)) {
1449 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.throttle.io_service_bytes", cgroup_blkio_base, cg->id);
1450 if(likely(stat(filename, &buf) != -1)) {
1451 cg->throttle_io_service_bytes.filename = strdupz(filename);
1452 cg->throttle_io_service_bytes.enabled = cgroup_enable_blkio_throttle_io;
1453 debug(D_CGROUP, "throttle_io_service_bytes filename for cgroup '%s': '%s'", cg->id, cg->throttle_io_service_bytes.filename);
1456 debug(D_CGROUP, "throttle_io_service_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1459 if(unlikely(cgroup_enable_blkio_throttle_ops && !cg->throttle_io_serviced.filename)) {
1460 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.throttle.io_serviced", cgroup_blkio_base, cg->id);
1461 if(likely(stat(filename, &buf) != -1)) {
1462 cg->throttle_io_serviced.filename = strdupz(filename);
1463 cg->throttle_io_serviced.enabled = cgroup_enable_blkio_throttle_ops;
1464 debug(D_CGROUP, "throttle_io_serviced filename for cgroup '%s': '%s'", cg->id, cg->throttle_io_serviced.filename);
1467 debug(D_CGROUP, "throttle_io_serviced file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1470 if(unlikely(cgroup_enable_blkio_merged_ops && !cg->io_merged.filename)) {
1471 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_merged", cgroup_blkio_base, cg->id);
1472 if(likely(stat(filename, &buf) != -1)) {
1473 cg->io_merged.filename = strdupz(filename);
1474 cg->io_merged.enabled = cgroup_enable_blkio_merged_ops;
1475 debug(D_CGROUP, "io_merged filename for cgroup '%s': '%s'", cg->id, cg->io_merged.filename);
1478 debug(D_CGROUP, "io_merged file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1481 if(unlikely(cgroup_enable_blkio_queued_ops && !cg->io_queued.filename)) {
1482 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_queued", cgroup_blkio_base, cg->id);
1483 if(likely(stat(filename, &buf) != -1)) {
1484 cg->io_queued.filename = strdupz(filename);
1485 cg->io_queued.enabled = cgroup_enable_blkio_queued_ops;
1486 debug(D_CGROUP, "io_queued filename for cgroup '%s': '%s'", cg->id, cg->io_queued.filename);
1489 debug(D_CGROUP, "io_queued file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1493 debug(D_CGROUP, "done searching for cgroups");
1497 // ----------------------------------------------------------------------------
1500 #define CHART_TITLE_MAX 300
1502 void update_services_charts(int update_every,
1505 int do_mem_detailed,
1511 int do_throttle_ops,
1517 *st_mem_usage = NULL,
1518 *st_mem_failcnt = NULL,
1519 *st_swap_usage = NULL,
1521 *st_mem_detailed_cache = NULL,
1522 *st_mem_detailed_rss = NULL,
1523 *st_mem_detailed_mapped = NULL,
1524 *st_mem_detailed_writeback = NULL,
1525 *st_mem_detailed_pgfault = NULL,
1526 *st_mem_detailed_pgmajfault = NULL,
1527 *st_mem_detailed_pgpgin = NULL,
1528 *st_mem_detailed_pgpgout = NULL,
1531 *st_io_serviced_read = NULL,
1532 *st_throttle_io_read = NULL,
1533 *st_throttle_ops_read = NULL,
1534 *st_queued_ops_read = NULL,
1535 *st_merged_ops_read = NULL,
1537 *st_io_write = NULL,
1538 *st_io_serviced_write = NULL,
1539 *st_throttle_io_write = NULL,
1540 *st_throttle_ops_write = NULL,
1541 *st_queued_ops_write = NULL,
1542 *st_merged_ops_write = NULL;
1544 // create the charts
1546 if(likely(do_cpu)) {
1547 if(unlikely(!st_cpu)) {
1548 char title[CHART_TITLE_MAX + 1];
1550 st_cpu = rrdset_find_bytype("services", "cpu");
1551 if(likely(!st_cpu)) {
1552 snprintfz(title, CHART_TITLE_MAX, "Systemd Services CPU utilization (%d%% = %d core%s)", (processors * 100), processors, (processors > 1) ? "s" : "");
1553 st_cpu = rrdset_create("services", "cpu", NULL, "cpu", "services.cpu", title, "%", CHART_PRIORITY_SYSTEMD_SERVICES, update_every, RRDSET_TYPE_STACKED);
1557 rrdset_next(st_cpu);
1560 if(likely(do_mem_usage)) {
1561 if(unlikely(!st_mem_usage)) {
1562 st_mem_usage = rrdset_find_bytype("services", "mem_usage");
1563 if(likely(!st_mem_usage))
1564 st_mem_usage = rrdset_create("services", "mem_usage", NULL, "mem", "services.mem_usage", "Systemd Services Used Memory", "MB", CHART_PRIORITY_SYSTEMD_SERVICES + 10, update_every, RRDSET_TYPE_STACKED);
1567 rrdset_next(st_mem_usage);
1570 if(likely(do_mem_detailed)) {
1571 if(unlikely(!st_mem_detailed_rss)) {
1572 st_mem_detailed_rss = rrdset_find_bytype("services", "mem_rss");
1573 if(likely(!st_mem_detailed_rss))
1574 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);
1577 rrdset_next(st_mem_detailed_rss);
1579 if(unlikely(!st_mem_detailed_mapped)) {
1580 st_mem_detailed_mapped = rrdset_find_bytype("services", "mem_mapped");
1581 if(likely(!st_mem_detailed_mapped))
1582 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);
1585 rrdset_next(st_mem_detailed_mapped);
1587 if(unlikely(!st_mem_detailed_cache)) {
1588 st_mem_detailed_cache = rrdset_find_bytype("services", "mem_cache");
1589 if(likely(!st_mem_detailed_cache))
1590 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);
1593 rrdset_next(st_mem_detailed_cache);
1595 if(unlikely(!st_mem_detailed_writeback)) {
1596 st_mem_detailed_writeback = rrdset_find_bytype("services", "mem_writeback");
1597 if(likely(!st_mem_detailed_writeback))
1598 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);
1601 rrdset_next(st_mem_detailed_writeback);
1603 if(unlikely(!st_mem_detailed_pgfault)) {
1604 st_mem_detailed_pgfault = rrdset_find_bytype("services", "mem_pgfault");
1605 if(likely(!st_mem_detailed_pgfault))
1606 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);
1609 rrdset_next(st_mem_detailed_pgfault);
1611 if(unlikely(!st_mem_detailed_pgmajfault)) {
1612 st_mem_detailed_pgmajfault = rrdset_find_bytype("services", "mem_pgmajfault");
1613 if(likely(!st_mem_detailed_pgmajfault))
1614 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);
1617 rrdset_next(st_mem_detailed_pgmajfault);
1619 if(unlikely(!st_mem_detailed_pgpgin)) {
1620 st_mem_detailed_pgpgin = rrdset_find_bytype("services", "mem_pgpgin");
1621 if(likely(!st_mem_detailed_pgpgin))
1622 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);
1625 rrdset_next(st_mem_detailed_pgpgin);
1627 if(unlikely(!st_mem_detailed_pgpgout)) {
1628 st_mem_detailed_pgpgout = rrdset_find_bytype("services", "mem_pgpgout");
1629 if(likely(!st_mem_detailed_pgpgout))
1630 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);
1633 rrdset_next(st_mem_detailed_pgpgout);
1636 if(likely(do_mem_failcnt)) {
1637 if(unlikely(!st_mem_failcnt)) {
1638 st_mem_failcnt = rrdset_find_bytype("services", "mem_failcnt");
1639 if(likely(!st_mem_failcnt))
1640 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);
1643 rrdset_next(st_mem_failcnt);
1646 if(likely(do_swap_usage)) {
1647 if(unlikely(!st_swap_usage)) {
1648 st_swap_usage = rrdset_find_bytype("services", "swap_usage");
1649 if(likely(!st_swap_usage))
1650 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);
1653 rrdset_next(st_swap_usage);
1657 if(unlikely(!st_io_read)) {
1658 st_io_read = rrdset_find_bytype("services", "io_read");
1659 if(likely(!st_io_read))
1660 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);
1663 rrdset_next(st_io_read);
1665 if(unlikely(!st_io_write)) {
1666 st_io_write = rrdset_find_bytype("services", "io_write");
1667 if(likely(!st_io_write))
1668 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);
1671 rrdset_next(st_io_write);
1674 if(likely(do_io_ops)) {
1675 if(unlikely(!st_io_serviced_read)) {
1676 st_io_serviced_read = rrdset_find_bytype("services", "io_ops_read");
1677 if(likely(!st_io_serviced_read))
1678 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);
1681 rrdset_next(st_io_serviced_read);
1683 if(unlikely(!st_io_serviced_write)) {
1684 st_io_serviced_write = rrdset_find_bytype("services", "io_ops_write");
1685 if(likely(!st_io_serviced_write))
1686 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);
1689 rrdset_next(st_io_serviced_write);
1692 if(likely(do_throttle_io)) {
1693 if(unlikely(!st_throttle_io_read)) {
1694 st_throttle_io_read = rrdset_find_bytype("services", "throttle_io_read");
1695 if(likely(!st_throttle_io_read))
1696 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);
1699 rrdset_next(st_throttle_io_read);
1701 if(unlikely(!st_throttle_io_write)) {
1702 st_throttle_io_write = rrdset_find_bytype("services", "throttle_io_write");
1703 if(likely(!st_throttle_io_write))
1704 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);
1707 rrdset_next(st_throttle_io_write);
1710 if(likely(do_throttle_ops)) {
1711 if(unlikely(!st_throttle_ops_read)) {
1712 st_throttle_ops_read = rrdset_find_bytype("services", "throttle_io_ops_read");
1713 if(likely(!st_throttle_ops_read))
1714 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);
1717 rrdset_next(st_throttle_ops_read);
1719 if(unlikely(!st_throttle_ops_write)) {
1720 st_throttle_ops_write = rrdset_find_bytype("services", "throttle_io_ops_write");
1721 if(likely(!st_throttle_ops_write))
1722 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);
1725 rrdset_next(st_throttle_ops_write);
1728 if(likely(do_queued_ops)) {
1729 if(unlikely(!st_queued_ops_read)) {
1730 st_queued_ops_read = rrdset_find_bytype("services", "queued_io_ops_read");
1731 if(likely(!st_queued_ops_read))
1732 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);
1735 rrdset_next(st_queued_ops_read);
1737 if(unlikely(!st_queued_ops_write)) {
1738 st_queued_ops_write = rrdset_find_bytype("services", "queued_io_ops_write");
1739 if(likely(!st_queued_ops_write))
1740 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);
1743 rrdset_next(st_queued_ops_write);
1746 if(likely(do_merged_ops)) {
1747 if(unlikely(!st_merged_ops_read)) {
1748 st_merged_ops_read = rrdset_find_bytype("services", "merged_io_ops_read");
1749 if(likely(!st_merged_ops_read))
1750 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);
1753 rrdset_next(st_merged_ops_read);
1755 if(unlikely(!st_merged_ops_write)) {
1756 st_merged_ops_write = rrdset_find_bytype("services", "merged_io_ops_write");
1757 if(likely(!st_merged_ops_write))
1758 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);
1761 rrdset_next(st_merged_ops_write);
1764 // update the values
1766 for(cg = cgroup_root; cg ; cg = cg->next) {
1767 if(unlikely(!cg->available || !cg->enabled || !(cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE)))
1770 if(likely(do_cpu && cg->cpuacct_stat.updated)) {
1771 if(unlikely(!cg->rd_cpu))
1772 cg->rd_cpu = rrddim_add(st_cpu, cg->chart_id, cg->chart_title, 100, hz, RRDDIM_INCREMENTAL);
1774 rrddim_set_by_pointer(st_cpu, cg->rd_cpu, cg->cpuacct_stat.user + cg->cpuacct_stat.system);
1777 if(likely(do_mem_usage && cg->memory.updated_usage_in_bytes)) {
1778 if(unlikely(!cg->rd_mem_usage))
1779 cg->rd_mem_usage = rrddim_add(st_mem_usage, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1781 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));
1784 if(likely(do_mem_detailed && cg->memory.updated_detailed)) {
1785 if(unlikely(!cg->rd_mem_detailed_rss))
1786 cg->rd_mem_detailed_rss = rrddim_add(st_mem_detailed_rss, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1787 rrddim_set_by_pointer(st_mem_detailed_rss, cg->rd_mem_detailed_rss, cg->memory.rss + cg->memory.rss_huge);
1789 if(unlikely(!cg->rd_mem_detailed_mapped))
1790 cg->rd_mem_detailed_mapped = rrddim_add(st_mem_detailed_mapped, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1791 rrddim_set_by_pointer(st_mem_detailed_mapped, cg->rd_mem_detailed_mapped, cg->memory.mapped_file);
1793 if(unlikely(!cg->rd_mem_detailed_cache))
1794 cg->rd_mem_detailed_cache = rrddim_add(st_mem_detailed_cache, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1795 rrddim_set_by_pointer(st_mem_detailed_cache, cg->rd_mem_detailed_cache, cg->memory.cache);
1797 if(unlikely(!cg->rd_mem_detailed_writeback))
1798 cg->rd_mem_detailed_writeback = rrddim_add(st_mem_detailed_writeback, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1799 rrddim_set_by_pointer(st_mem_detailed_writeback, cg->rd_mem_detailed_writeback, cg->memory.writeback);
1801 if(unlikely(!cg->rd_mem_detailed_pgfault))
1802 cg->rd_mem_detailed_pgfault = rrddim_add(st_mem_detailed_pgfault, cg->chart_id, cg->chart_title, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1803 rrddim_set_by_pointer(st_mem_detailed_pgfault, cg->rd_mem_detailed_pgfault, cg->memory.pgfault);
1805 if(unlikely(!cg->rd_mem_detailed_pgmajfault))
1806 cg->rd_mem_detailed_pgmajfault = rrddim_add(st_mem_detailed_pgmajfault, cg->chart_id, cg->chart_title, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1807 rrddim_set_by_pointer(st_mem_detailed_pgmajfault, cg->rd_mem_detailed_pgmajfault, cg->memory.pgmajfault);
1809 if(unlikely(!cg->rd_mem_detailed_pgpgin))
1810 cg->rd_mem_detailed_pgpgin = rrddim_add(st_mem_detailed_pgpgin, cg->chart_id, cg->chart_title, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1811 rrddim_set_by_pointer(st_mem_detailed_pgpgin, cg->rd_mem_detailed_pgpgin, cg->memory.pgpgin);
1813 if(unlikely(!cg->rd_mem_detailed_pgpgout))
1814 cg->rd_mem_detailed_pgpgout = rrddim_add(st_mem_detailed_pgpgout, cg->chart_id, cg->chart_title, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1815 rrddim_set_by_pointer(st_mem_detailed_pgpgout, cg->rd_mem_detailed_pgpgout, cg->memory.pgpgout);
1818 if(likely(do_mem_failcnt && cg->memory.updated_failcnt)) {
1819 if(unlikely(!cg->rd_mem_failcnt))
1820 cg->rd_mem_failcnt = rrddim_add(st_mem_failcnt, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1822 rrddim_set_by_pointer(st_mem_failcnt, cg->rd_mem_failcnt, cg->memory.failcnt);
1825 if(likely(do_swap_usage && cg->memory.updated_msw_usage_in_bytes)) {
1826 if(unlikely(!cg->rd_swap_usage))
1827 cg->rd_swap_usage = rrddim_add(st_swap_usage, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1829 rrddim_set_by_pointer(st_swap_usage, cg->rd_swap_usage, cg->memory.msw_usage_in_bytes);
1832 if(likely(do_io && cg->io_service_bytes.updated)) {
1833 if(unlikely(!cg->rd_io_service_bytes_read))
1834 cg->rd_io_service_bytes_read = rrddim_add(st_io_read, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1836 rrddim_set_by_pointer(st_io_read, cg->rd_io_service_bytes_read, cg->io_service_bytes.Read);
1838 if(unlikely(!cg->rd_io_service_bytes_write))
1839 cg->rd_io_service_bytes_write = rrddim_add(st_io_write, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1841 rrddim_set_by_pointer(st_io_write, cg->rd_io_service_bytes_write, cg->io_service_bytes.Write);
1844 if(likely(do_io_ops && cg->io_serviced.updated)) {
1845 if(unlikely(!cg->rd_io_serviced_read))
1846 cg->rd_io_serviced_read = rrddim_add(st_io_serviced_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1848 rrddim_set_by_pointer(st_io_serviced_read, cg->rd_io_serviced_read, cg->io_serviced.Read);
1850 if(unlikely(!cg->rd_io_serviced_write))
1851 cg->rd_io_serviced_write = rrddim_add(st_io_serviced_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1853 rrddim_set_by_pointer(st_io_serviced_write, cg->rd_io_serviced_write, cg->io_serviced.Write);
1856 if(likely(do_throttle_io && cg->throttle_io_service_bytes.updated)) {
1857 if(unlikely(!cg->rd_throttle_io_read))
1858 cg->rd_throttle_io_read = rrddim_add(st_throttle_io_read, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1860 rrddim_set_by_pointer(st_throttle_io_read, cg->rd_throttle_io_read, cg->throttle_io_service_bytes.Read);
1862 if(unlikely(!cg->rd_throttle_io_write))
1863 cg->rd_throttle_io_write = rrddim_add(st_throttle_io_write, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1865 rrddim_set_by_pointer(st_throttle_io_write, cg->rd_throttle_io_write, cg->throttle_io_service_bytes.Write);
1868 if(likely(do_throttle_ops && cg->throttle_io_serviced.updated)) {
1869 if(unlikely(!cg->rd_throttle_io_serviced_read))
1870 cg->rd_throttle_io_serviced_read = rrddim_add(st_throttle_ops_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1872 rrddim_set_by_pointer(st_throttle_ops_read, cg->rd_throttle_io_serviced_read, cg->throttle_io_serviced.Read);
1874 if(unlikely(!cg->rd_throttle_io_serviced_write))
1875 cg->rd_throttle_io_serviced_write = rrddim_add(st_throttle_ops_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1877 rrddim_set_by_pointer(st_throttle_ops_write, cg->rd_throttle_io_serviced_write, cg->throttle_io_serviced.Write);
1880 if(likely(do_queued_ops && cg->io_queued.updated)) {
1881 if(unlikely(!cg->rd_io_queued_read))
1882 cg->rd_io_queued_read = rrddim_add(st_queued_ops_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1884 rrddim_set_by_pointer(st_queued_ops_read, cg->rd_io_queued_read, cg->io_queued.Read);
1886 if(unlikely(!cg->rd_io_queued_write))
1887 cg->rd_io_queued_write = rrddim_add(st_queued_ops_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1889 rrddim_set_by_pointer(st_queued_ops_write, cg->rd_io_queued_write, cg->io_queued.Write);
1892 if(likely(do_merged_ops && cg->io_merged.updated)) {
1893 if(unlikely(!cg->rd_io_merged_read))
1894 cg->rd_io_merged_read = rrddim_add(st_merged_ops_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1896 rrddim_set_by_pointer(st_merged_ops_read, cg->rd_io_merged_read, cg->io_merged.Read);
1898 if(unlikely(!cg->rd_io_merged_write))
1899 cg->rd_io_merged_write = rrddim_add(st_merged_ops_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1901 rrddim_set_by_pointer(st_merged_ops_write, cg->rd_io_merged_write, cg->io_merged.Write);
1905 // complete the iteration
1907 rrdset_done(st_cpu);
1909 if(likely(do_mem_usage))
1910 rrdset_done(st_mem_usage);
1912 if(unlikely(do_mem_detailed)) {
1913 rrdset_done(st_mem_detailed_cache);
1914 rrdset_done(st_mem_detailed_rss);
1915 rrdset_done(st_mem_detailed_mapped);
1916 rrdset_done(st_mem_detailed_writeback);
1917 rrdset_done(st_mem_detailed_pgfault);
1918 rrdset_done(st_mem_detailed_pgmajfault);
1919 rrdset_done(st_mem_detailed_pgpgin);
1920 rrdset_done(st_mem_detailed_pgpgout);
1923 if(likely(do_mem_failcnt))
1924 rrdset_done(st_mem_failcnt);
1926 if(likely(do_swap_usage))
1927 rrdset_done(st_swap_usage);
1930 rrdset_done(st_io_read);
1931 rrdset_done(st_io_write);
1934 if(likely(do_io_ops)) {
1935 rrdset_done(st_io_serviced_read);
1936 rrdset_done(st_io_serviced_write);
1939 if(likely(do_throttle_io)) {
1940 rrdset_done(st_throttle_io_read);
1941 rrdset_done(st_throttle_io_write);
1944 if(likely(do_throttle_ops)) {
1945 rrdset_done(st_throttle_ops_read);
1946 rrdset_done(st_throttle_ops_write);
1949 if(likely(do_queued_ops)) {
1950 rrdset_done(st_queued_ops_read);
1951 rrdset_done(st_queued_ops_write);
1954 if(likely(do_merged_ops)) {
1955 rrdset_done(st_merged_ops_read);
1956 rrdset_done(st_merged_ops_write);
1960 static inline char *cgroup_chart_type(char *buffer, const char *id, size_t len) {
1961 if(buffer[0]) return buffer;
1963 if(id[0] == '\0' || (id[0] == '/' && id[1] == '\0'))
1964 strncpy(buffer, "cgroup_root", len);
1966 snprintfz(buffer, len, "cgroup_%s", id);
1968 netdata_fix_chart_id(buffer);
1972 void update_cgroup_charts(int update_every) {
1973 debug(D_CGROUP, "updating cgroups charts");
1975 char type[RRD_ID_LENGTH_MAX + 1];
1976 char title[CHART_TITLE_MAX + 1];
1978 int services_do_cpu = 0,
1979 services_do_mem_usage = 0,
1980 services_do_mem_detailed = 0,
1981 services_do_mem_failcnt = 0,
1982 services_do_swap_usage = 0,
1984 services_do_io_ops = 0,
1985 services_do_throttle_io = 0,
1986 services_do_throttle_ops = 0,
1987 services_do_queued_ops = 0,
1988 services_do_merged_ops = 0;
1991 for(cg = cgroup_root; cg ; cg = cg->next) {
1992 if(unlikely(!cg->available || !cg->enabled))
1995 if(likely(cgroup_enable_systemd_services && cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE)) {
1996 if(cg->cpuacct_stat.updated && cg->cpuacct_stat.enabled == CONFIG_ONDEMAND_YES) services_do_cpu++;
1998 if(cgroup_enable_systemd_services_detailed_memory && cg->memory.updated_detailed && cg->memory.enabled_detailed) services_do_mem_detailed++;
1999 if(cg->memory.updated_usage_in_bytes && cg->memory.enabled_usage_in_bytes == CONFIG_ONDEMAND_YES) services_do_mem_usage++;
2000 if(cg->memory.updated_failcnt && cg->memory.enabled_failcnt == CONFIG_ONDEMAND_YES) services_do_mem_failcnt++;
2001 if(cg->memory.updated_msw_usage_in_bytes && cg->memory.enabled_msw_usage_in_bytes == CONFIG_ONDEMAND_YES) services_do_swap_usage++;
2003 if(cg->io_service_bytes.updated && cg->io_service_bytes.enabled == CONFIG_ONDEMAND_YES) services_do_io++;
2004 if(cg->io_serviced.updated && cg->io_serviced.enabled == CONFIG_ONDEMAND_YES) services_do_io_ops++;
2005 if(cg->throttle_io_service_bytes.updated && cg->throttle_io_service_bytes.enabled == CONFIG_ONDEMAND_YES) services_do_throttle_io++;
2006 if(cg->throttle_io_serviced.updated && cg->throttle_io_serviced.enabled == CONFIG_ONDEMAND_YES) services_do_throttle_ops++;
2007 if(cg->io_queued.updated && cg->io_queued.enabled == CONFIG_ONDEMAND_YES) services_do_queued_ops++;
2008 if(cg->io_merged.updated && cg->io_merged.enabled == CONFIG_ONDEMAND_YES) services_do_merged_ops++;
2014 if(likely(cg->cpuacct_stat.updated && cg->cpuacct_stat.enabled == CONFIG_ONDEMAND_YES)) {
2015 if(unlikely(!cg->st_cpu)) {
2016 cg->st_cpu = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "cpu");
2017 if(likely(!cg->st_cpu)) {
2018 snprintfz(title, CHART_TITLE_MAX, "CPU Usage (%d%% = %d core%s) for cgroup %s", (processors * 100), processors, (processors > 1) ? "s" : "", cg->chart_title);
2019 cg->st_cpu = rrdset_create(type, "cpu", NULL, "cpu", "cgroup.cpu", title, "%", CHART_PRIORITY_CONTAINERS, update_every, RRDSET_TYPE_STACKED);
2021 rrddim_add(cg->st_cpu, "user", NULL, 100, hz, RRDDIM_INCREMENTAL);
2022 rrddim_add(cg->st_cpu, "system", NULL, 100, hz, RRDDIM_INCREMENTAL);
2025 rrdset_next(cg->st_cpu);
2027 rrddim_set(cg->st_cpu, "user", cg->cpuacct_stat.user);
2028 rrddim_set(cg->st_cpu, "system", cg->cpuacct_stat.system);
2029 rrdset_done(cg->st_cpu);
2032 if(likely(cg->cpuacct_usage.updated && cg->cpuacct_usage.enabled == CONFIG_ONDEMAND_YES)) {
2033 char id[RRD_ID_LENGTH_MAX + 1];
2036 if(unlikely(!cg->st_cpu_per_core)) {
2037 cg->st_cpu_per_core = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "cpu_per_core");
2038 if(likely(!cg->st_cpu_per_core)) {
2039 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);
2040 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);
2042 for(i = 0; i < cg->cpuacct_usage.cpus; i++) {
2043 snprintfz(id, CHART_TITLE_MAX, "cpu%u", i);
2044 rrddim_add(cg->st_cpu_per_core, id, NULL, 100, 1000000000, RRDDIM_INCREMENTAL);
2048 rrdset_next(cg->st_cpu_per_core);
2050 for(i = 0; i < cg->cpuacct_usage.cpus ;i++) {
2051 snprintfz(id, CHART_TITLE_MAX, "cpu%u", i);
2052 rrddim_set(cg->st_cpu_per_core, id, cg->cpuacct_usage.cpu_percpu[i]);
2054 rrdset_done(cg->st_cpu_per_core);
2057 if(likely(cg->memory.updated_detailed && cg->memory.enabled_detailed == CONFIG_ONDEMAND_YES)) {
2058 if(unlikely(!cg->st_mem)) {
2059 cg->st_mem = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem");
2060 if(likely(!cg->st_mem)) {
2061 snprintfz(title, CHART_TITLE_MAX, "Memory Usage for cgroup %s", cg->chart_title);
2062 cg->st_mem = rrdset_create(type, "mem", NULL, "mem", "cgroup.mem", title, "MB", CHART_PRIORITY_CONTAINERS + 210, update_every, RRDSET_TYPE_STACKED);
2065 rrddim_add(cg->st_mem, "cache", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2066 rrddim_add(cg->st_mem, "rss", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2067 if(cg->memory.detailed_has_dirty_swap)
2068 rrddim_add(cg->st_mem, "swap", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2069 rrddim_add(cg->st_mem, "rss_huge", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2070 rrddim_add(cg->st_mem, "mapped_file", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2073 rrdset_next(cg->st_mem);
2075 rrddim_set(cg->st_mem, "cache", cg->memory.cache);
2076 rrddim_set(cg->st_mem, "rss", cg->memory.rss);
2077 if(cg->memory.detailed_has_dirty_swap)
2078 rrddim_set(cg->st_mem, "swap", cg->memory.swap);
2079 rrddim_set(cg->st_mem, "rss_huge", cg->memory.rss_huge);
2080 rrddim_set(cg->st_mem, "mapped_file", cg->memory.mapped_file);
2081 rrdset_done(cg->st_mem);
2083 if(unlikely(!cg->st_writeback)) {
2084 cg->st_writeback = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "writeback");
2085 if(likely(!cg->st_writeback)) {
2086 snprintfz(title, CHART_TITLE_MAX, "Writeback Memory for cgroup %s", cg->chart_title);
2087 cg->st_writeback = rrdset_create(type, "writeback", NULL, "mem", "cgroup.writeback", title, "MB", CHART_PRIORITY_CONTAINERS + 300, update_every, RRDSET_TYPE_AREA);
2090 if(cg->memory.detailed_has_dirty_swap)
2091 rrddim_add(cg->st_writeback, "dirty", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2092 rrddim_add(cg->st_writeback, "writeback", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2095 rrdset_next(cg->st_writeback);
2097 if(cg->memory.detailed_has_dirty_swap)
2098 rrddim_set(cg->st_writeback, "dirty", cg->memory.dirty);
2099 rrddim_set(cg->st_writeback, "writeback", cg->memory.writeback);
2100 rrdset_done(cg->st_writeback);
2102 if(unlikely(!cg->st_mem_activity)) {
2103 cg->st_mem_activity = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_activity");
2104 if(likely(!cg->st_mem_activity)) {
2105 snprintfz(title, CHART_TITLE_MAX, "Memory Activity for cgroup %s", cg->chart_title);
2106 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);
2108 rrddim_add(cg->st_mem_activity, "pgpgin", "in", system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
2109 rrddim_add(cg->st_mem_activity, "pgpgout", "out", -system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
2112 rrdset_next(cg->st_mem_activity);
2114 rrddim_set(cg->st_mem_activity, "pgpgin", cg->memory.pgpgin);
2115 rrddim_set(cg->st_mem_activity, "pgpgout", cg->memory.pgpgout);
2116 rrdset_done(cg->st_mem_activity);
2118 if(unlikely(!cg->st_pgfaults)) {
2119 cg->st_pgfaults = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "pgfaults");
2120 if(likely(!cg->st_pgfaults)) {
2121 snprintfz(title, CHART_TITLE_MAX, "Memory Page Faults for cgroup %s", cg->chart_title);
2122 cg->st_pgfaults = rrdset_create(type, "pgfaults", NULL, "mem", "cgroup.pgfaults", title, "MB/s", CHART_PRIORITY_CONTAINERS + 500, update_every, RRDSET_TYPE_LINE);
2124 rrddim_add(cg->st_pgfaults, "pgfault", NULL, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
2125 rrddim_add(cg->st_pgfaults, "pgmajfault", "swap", -system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
2128 rrdset_next(cg->st_pgfaults);
2130 rrddim_set(cg->st_pgfaults, "pgfault", cg->memory.pgfault);
2131 rrddim_set(cg->st_pgfaults, "pgmajfault", cg->memory.pgmajfault);
2132 rrdset_done(cg->st_pgfaults);
2135 if(likely(cg->memory.updated_usage_in_bytes && cg->memory.enabled_usage_in_bytes == CONFIG_ONDEMAND_YES)) {
2136 if(unlikely(!cg->st_mem_usage)) {
2137 cg->st_mem_usage = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_usage");
2138 if(likely(!cg->st_mem_usage)) {
2139 snprintfz(title, CHART_TITLE_MAX, "Total Memory for cgroup %s", cg->chart_title);
2140 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);
2142 rrddim_add(cg->st_mem_usage, "ram", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2143 rrddim_add(cg->st_mem_usage, "swap", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2146 rrdset_next(cg->st_mem_usage);
2148 rrddim_set(cg->st_mem_usage, "ram", cg->memory.usage_in_bytes - ((cgroup_used_memory_without_cache)?cg->memory.cache:0));
2149 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);
2150 rrdset_done(cg->st_mem_usage);
2153 if(likely(cg->memory.updated_failcnt && cg->memory.enabled_failcnt == CONFIG_ONDEMAND_YES)) {
2154 if(unlikely(!cg->st_mem_failcnt)) {
2155 cg->st_mem_failcnt = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_failcnt");
2156 if(likely(!cg->st_mem_failcnt)) {
2157 snprintfz(title, CHART_TITLE_MAX, "Memory Limit Failures for cgroup %s", cg->chart_title);
2158 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);
2160 rrddim_add(cg->st_mem_failcnt, "failures", NULL, 1, 1, RRDDIM_INCREMENTAL);
2163 rrdset_next(cg->st_mem_failcnt);
2165 rrddim_set(cg->st_mem_failcnt, "failures", cg->memory.failcnt);
2166 rrdset_done(cg->st_mem_failcnt);
2169 if(likely(cg->io_service_bytes.updated && cg->io_service_bytes.enabled == CONFIG_ONDEMAND_YES)) {
2170 if(unlikely(!cg->st_io)) {
2171 cg->st_io = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "io");
2172 if(likely(!cg->st_io)) {
2173 snprintfz(title, CHART_TITLE_MAX, "I/O Bandwidth (all disks) for cgroup %s", cg->chart_title);
2174 cg->st_io = rrdset_create(type, "io", NULL, "disk", "cgroup.io", title, "KB/s", CHART_PRIORITY_CONTAINERS + 1200, update_every, RRDSET_TYPE_AREA);
2176 rrddim_add(cg->st_io, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
2177 rrddim_add(cg->st_io, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
2180 rrdset_next(cg->st_io);
2182 rrddim_set(cg->st_io, "read", cg->io_service_bytes.Read);
2183 rrddim_set(cg->st_io, "write", cg->io_service_bytes.Write);
2184 rrdset_done(cg->st_io);
2187 if(likely(cg->io_serviced.updated && cg->io_serviced.enabled == CONFIG_ONDEMAND_YES)) {
2188 if(unlikely(!cg->st_serviced_ops)) {
2189 cg->st_serviced_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "serviced_ops");
2190 if(likely(!cg->st_serviced_ops)) {
2191 snprintfz(title, CHART_TITLE_MAX, "Serviced I/O Operations (all disks) for cgroup %s", cg->chart_title);
2192 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);
2194 rrddim_add(cg->st_serviced_ops, "read", NULL, 1, 1, RRDDIM_INCREMENTAL);
2195 rrddim_add(cg->st_serviced_ops, "write", NULL, -1, 1, RRDDIM_INCREMENTAL);
2198 rrdset_next(cg->st_serviced_ops);
2200 rrddim_set(cg->st_serviced_ops, "read", cg->io_serviced.Read);
2201 rrddim_set(cg->st_serviced_ops, "write", cg->io_serviced.Write);
2202 rrdset_done(cg->st_serviced_ops);
2205 if(likely(cg->throttle_io_service_bytes.updated && cg->throttle_io_service_bytes.enabled == CONFIG_ONDEMAND_YES)) {
2206 if(unlikely(!cg->st_throttle_io)) {
2207 cg->st_throttle_io = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "throttle_io");
2208 if(likely(!cg->st_throttle_io)) {
2209 snprintfz(title, CHART_TITLE_MAX, "Throttle I/O Bandwidth (all disks) for cgroup %s", cg->chart_title);
2210 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);
2212 rrddim_add(cg->st_throttle_io, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
2213 rrddim_add(cg->st_throttle_io, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
2216 rrdset_next(cg->st_throttle_io);
2218 rrddim_set(cg->st_throttle_io, "read", cg->throttle_io_service_bytes.Read);
2219 rrddim_set(cg->st_throttle_io, "write", cg->throttle_io_service_bytes.Write);
2220 rrdset_done(cg->st_throttle_io);
2223 if(likely(cg->throttle_io_serviced.updated && cg->throttle_io_serviced.enabled == CONFIG_ONDEMAND_YES)) {
2224 if(unlikely(!cg->st_throttle_serviced_ops)) {
2225 cg->st_throttle_serviced_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "throttle_serviced_ops");
2226 if(likely(!cg->st_throttle_serviced_ops)) {
2227 snprintfz(title, CHART_TITLE_MAX, "Throttle Serviced I/O Operations (all disks) for cgroup %s", cg->chart_title);
2228 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);
2230 rrddim_add(cg->st_throttle_serviced_ops, "read", NULL, 1, 1, RRDDIM_INCREMENTAL);
2231 rrddim_add(cg->st_throttle_serviced_ops, "write", NULL, -1, 1, RRDDIM_INCREMENTAL);
2234 rrdset_next(cg->st_throttle_serviced_ops);
2236 rrddim_set(cg->st_throttle_serviced_ops, "read", cg->throttle_io_serviced.Read);
2237 rrddim_set(cg->st_throttle_serviced_ops, "write", cg->throttle_io_serviced.Write);
2238 rrdset_done(cg->st_throttle_serviced_ops);
2241 if(likely(cg->io_queued.updated && cg->io_queued.enabled == CONFIG_ONDEMAND_YES)) {
2242 if(unlikely(!cg->st_queued_ops)) {
2243 cg->st_queued_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "queued_ops");
2244 if(likely(!cg->st_queued_ops)) {
2245 snprintfz(title, CHART_TITLE_MAX, "Queued I/O Operations (all disks) for cgroup %s", cg->chart_title);
2246 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);
2248 rrddim_add(cg->st_queued_ops, "read", NULL, 1, 1, RRDDIM_ABSOLUTE);
2249 rrddim_add(cg->st_queued_ops, "write", NULL, -1, 1, RRDDIM_ABSOLUTE);
2252 rrdset_next(cg->st_queued_ops);
2254 rrddim_set(cg->st_queued_ops, "read", cg->io_queued.Read);
2255 rrddim_set(cg->st_queued_ops, "write", cg->io_queued.Write);
2256 rrdset_done(cg->st_queued_ops);
2259 if(likely(cg->io_merged.updated && cg->io_merged.enabled == CONFIG_ONDEMAND_YES)) {
2260 if(unlikely(!cg->st_merged_ops)) {
2261 cg->st_merged_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "merged_ops");
2262 if(likely(!cg->st_merged_ops)) {
2263 snprintfz(title, CHART_TITLE_MAX, "Merged I/O Operations (all disks) for cgroup %s", cg->chart_title);
2264 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);
2266 rrddim_add(cg->st_merged_ops, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
2267 rrddim_add(cg->st_merged_ops, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
2270 rrdset_next(cg->st_merged_ops);
2272 rrddim_set(cg->st_merged_ops, "read", cg->io_merged.Read);
2273 rrddim_set(cg->st_merged_ops, "write", cg->io_merged.Write);
2274 rrdset_done(cg->st_merged_ops);
2278 if(likely(cgroup_enable_systemd_services))
2279 update_services_charts(update_every,
2281 services_do_mem_usage,
2282 services_do_mem_detailed,
2283 services_do_mem_failcnt,
2284 services_do_swap_usage,
2287 services_do_throttle_io,
2288 services_do_throttle_ops,
2289 services_do_queued_ops,
2290 services_do_merged_ops
2293 debug(D_CGROUP, "done updating cgroups charts");
2296 // ----------------------------------------------------------------------------
2299 void *cgroups_main(void *ptr) {
2300 struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
2302 info("CGROUP Plugin thread created with task id %d", gettid());
2304 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
2305 error("Cannot set pthread cancel type to DEFERRED.");
2307 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
2308 error("Cannot set pthread cancel state to ENABLE.");
2310 struct rusage thread;
2312 // when ZERO, attempt to do it
2313 int vdo_cpu_netdata = config_get_boolean("plugin:cgroups", "cgroups plugin resource charts", 1);
2315 read_cgroup_plugin_configuration();
2317 RRDSET *stcpu_thread = NULL;
2319 usec_t step = cgroup_update_every * USEC_PER_SEC;
2320 usec_t find_every = cgroup_check_for_new_every * USEC_PER_SEC, find_next = 0;
2322 usec_t now = now_monotonic_usec();
2323 usec_t next = now - (now % step) + step;
2326 sleep_usec(next - now);
2327 now = now_monotonic_usec();
2330 if(unlikely(netdata_exit)) break;
2332 // BEGIN -- the job to be done
2334 if(unlikely(now >= find_next)) {
2336 find_next = now + find_every;
2339 read_all_cgroups(cgroup_root);
2340 update_cgroup_charts(cgroup_update_every);
2342 // END -- the job is done
2344 // --------------------------------------------------------------------
2346 if(vdo_cpu_netdata) {
2347 getrusage(RUSAGE_THREAD, &thread);
2349 if(unlikely(!stcpu_thread)) {
2350 stcpu_thread = rrdset_find("netdata.plugin_cgroups_cpu");
2351 if(unlikely(!stcpu_thread))
2352 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);
2354 rrddim_add(stcpu_thread, "user", NULL, 1, 1000, RRDDIM_INCREMENTAL);
2355 rrddim_add(stcpu_thread, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
2358 rrdset_next(stcpu_thread);
2360 rrddim_set(stcpu_thread, "user" , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
2361 rrddim_set(stcpu_thread, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
2362 rrdset_done(stcpu_thread);
2366 info("CGROUP thread exiting");
2368 static_thread->enabled = 0;