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 = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
502 if(unlikely(hash == system_hash && !strcmp(s, "system"))) {
503 cp->system = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
510 if(unlikely(cp->enabled == CONFIG_ONDEMAND_ONDEMAND && (cp->user || cp->system)))
511 cp->enabled = CONFIG_ONDEMAND_YES;
515 static inline void cgroup_read_cpuacct_usage(struct cpuacct_usage *ca) {
516 static procfile *ff = NULL;
518 if(likely(ca->filename)) {
519 ff = procfile_reopen(ff, ca->filename, NULL, PROCFILE_FLAG_DEFAULT);
525 ff = procfile_readall(ff);
531 if(unlikely(procfile_lines(ff) < 1)) {
532 error("File '%s' should have 1+ lines but has %u.", ca->filename, procfile_lines(ff));
537 unsigned long i = procfile_linewords(ff, 0);
538 if(unlikely(i == 0)) {
543 // we may have 1 more CPU reported
545 char *s = procfile_lineword(ff, 0, i - 1);
550 if(unlikely(i != ca->cpus)) {
551 freez(ca->cpu_percpu);
552 ca->cpu_percpu = mallocz(sizeof(unsigned long long) * i);
553 ca->cpus = (unsigned int)i;
556 unsigned long long total = 0;
557 for(i = 0; i < ca->cpus ;i++) {
558 unsigned long long n = strtoull(procfile_lineword(ff, 0, i), NULL, 10);
559 ca->cpu_percpu[i] = n;
565 if(unlikely(ca->enabled == CONFIG_ONDEMAND_ONDEMAND && total))
566 ca->enabled = CONFIG_ONDEMAND_YES;
570 static inline void cgroup_read_blkio(struct blkio *io) {
571 static procfile *ff = NULL;
573 if(unlikely(io->enabled == CONFIG_ONDEMAND_ONDEMAND && io->delay_counter > 0)) {
578 if(likely(io->filename)) {
579 ff = procfile_reopen(ff, io->filename, NULL, PROCFILE_FLAG_DEFAULT);
585 ff = procfile_readall(ff);
591 unsigned long i, lines = procfile_lines(ff);
593 if(unlikely(lines < 1)) {
594 error("File '%s' should have 1+ lines.", io->filename);
607 for(i = 0; i < lines ; i++) {
608 char *s = procfile_lineword(ff, i, 1);
609 uint32_t hash = simple_hash(s);
611 if(unlikely(hash == Read_hash && !strcmp(s, "Read"))) {
612 io->Read += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
616 if(unlikely(hash == Write_hash && !strcmp(s, "Write"))) {
617 io->Write += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
622 if(hash == Sync_hash && !strcmp(s, "Sync")) {
623 io->Sync += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
627 if(hash == Async_hash && !strcmp(s, "Async")) {
628 io->Async += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
632 if(hash == Total_hash && !strcmp(s, "Total")) {
633 io->Total += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
641 if(unlikely(io->enabled == CONFIG_ONDEMAND_ONDEMAND)) {
642 if(unlikely(io->Read || io->Write))
643 io->enabled = CONFIG_ONDEMAND_YES;
645 io->delay_counter = cgroup_recheck_zero_blkio_every_iterations;
650 static inline void cgroup_read_memory(struct memory *mem) {
651 static procfile *ff = NULL;
653 // read detailed ram usage
654 if(likely(mem->filename_detailed)) {
655 if(unlikely(mem->enabled_detailed == CONFIG_ONDEMAND_ONDEMAND && mem->delay_counter_detailed > 0)) {
656 mem->delay_counter_detailed--;
660 ff = procfile_reopen(ff, mem->filename_detailed, NULL, PROCFILE_FLAG_DEFAULT);
662 mem->updated_detailed = 0;
666 ff = procfile_readall(ff);
668 mem->updated_detailed = 0;
672 unsigned long i, lines = procfile_lines(ff);
674 if(unlikely(lines < 1)) {
675 error("File '%s' should have 1+ lines.", mem->filename_detailed);
676 mem->updated_detailed = 0;
680 for(i = 0; i < lines ; i++) {
681 char *s = procfile_lineword(ff, i, 0);
682 uint32_t hash = simple_hash(s);
684 if(unlikely(hash == cache_hash && !strcmp(s, "cache"))) {
685 mem->cache = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
689 if(unlikely(hash == rss_hash && !strcmp(s, "rss"))) {
690 mem->rss = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
694 if(unlikely(hash == rss_huge_hash && !strcmp(s, "rss_huge"))) {
695 mem->rss_huge = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
699 if(unlikely(hash == mapped_file_hash && !strcmp(s, "mapped_file"))) {
700 mem->mapped_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
704 if(unlikely(hash == writeback_hash && !strcmp(s, "writeback"))) {
705 mem->writeback = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
709 if(unlikely(hash == dirty_hash && !strcmp(s, "dirty"))) {
710 mem->dirty = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
711 mem->detailed_has_dirty = 1;
715 if(unlikely(hash == swap_hash && !strcmp(s, "swap"))) {
716 mem->swap = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
717 mem->detailed_has_swap = 1;
721 if(unlikely(hash == pgpgin_hash && !strcmp(s, "pgpgin"))) {
722 mem->pgpgin = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
726 if(unlikely(hash == pgpgout_hash && !strcmp(s, "pgpgout"))) {
727 mem->pgpgout = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
731 if(unlikely(hash == pgfault_hash && !strcmp(s, "pgfault"))) {
732 mem->pgfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
736 if(unlikely(hash == pgmajfault_hash && !strcmp(s, "pgmajfault"))) {
737 mem->pgmajfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
742 if(unlikely(hash == inactive_anon_hash && !strcmp(s, "inactive_anon"))) {
743 mem->inactive_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
747 if(unlikely(hash == active_anon_hash && !strcmp(s, "active_anon"))) {
748 mem->active_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
752 if(unlikely(hash == inactive_file_hash && !strcmp(s, "inactive_file"))) {
753 mem->inactive_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
757 if(unlikely(hash == active_file_hash && !strcmp(s, "active_file"))) {
758 mem->active_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
762 if(unlikely(hash == unevictable_hash && !strcmp(s, "unevictable"))) {
763 mem->unevictable = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
767 if(unlikely(hash == hierarchical_memory_limit_hash && !strcmp(s, "hierarchical_memory_limit"))) {
768 mem->hierarchical_memory_limit = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
772 if(unlikely(hash == total_cache_hash && !strcmp(s, "total_cache"))) {
773 mem->total_cache = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
777 if(unlikely(hash == total_rss_hash && !strcmp(s, "total_rss"))) {
778 mem->total_rss = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
782 if(unlikely(hash == total_rss_huge_hash && !strcmp(s, "total_rss_huge"))) {
783 mem->total_rss_huge = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
787 if(unlikely(hash == total_mapped_file_hash && !strcmp(s, "total_mapped_file"))) {
788 mem->total_mapped_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
792 if(unlikely(hash == total_writeback_hash && !strcmp(s, "total_writeback"))) {
793 mem->total_writeback = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
797 if(unlikely(hash == total_dirty_hash && !strcmp(s, "total_dirty"))) {
798 mem->total_dirty = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
802 if(unlikely(hash == total_swap_hash && !strcmp(s, "total_swap"))) {
803 mem->total_swap = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
807 if(unlikely(hash == total_pgpgin_hash && !strcmp(s, "total_pgpgin"))) {
808 mem->total_pgpgin = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
812 if(unlikely(hash == total_pgpgout_hash && !strcmp(s, "total_pgpgout"))) {
813 mem->total_pgpgout = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
817 if(unlikely(hash == total_pgfault_hash && !strcmp(s, "total_pgfault"))) {
818 mem->total_pgfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
822 if(unlikely(hash == total_pgmajfault_hash && !strcmp(s, "total_pgmajfault"))) {
823 mem->total_pgmajfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
827 if(unlikely(hash == total_inactive_anon_hash && !strcmp(s, "total_inactive_anon"))) {
828 mem->total_inactive_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
832 if(unlikely(hash == total_active_anon_hash && !strcmp(s, "total_active_anon"))) {
833 mem->total_active_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
837 if(unlikely(hash == total_inactive_file_hash && !strcmp(s, "total_inactive_file"))) {
838 mem->total_inactive_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
842 if(unlikely(hash == total_active_file_hash && !strcmp(s, "total_active_file"))) {
843 mem->total_active_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
847 if(unlikely(hash == total_unevictable_hash && !strcmp(s, "total_unevictable"))) {
848 mem->total_unevictable = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
854 // 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);
856 mem->updated_detailed = 1;
858 if(unlikely(mem->enabled_detailed == CONFIG_ONDEMAND_ONDEMAND)) {
859 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)
860 mem->enabled_detailed = CONFIG_ONDEMAND_YES;
862 mem->delay_counter_detailed = cgroup_recheck_zero_mem_detailed_every_iterations;
868 // read usage_in_bytes
869 if(likely(mem->filename_usage_in_bytes)) {
870 mem->updated_usage_in_bytes = !read_single_number_file(mem->filename_usage_in_bytes, &mem->usage_in_bytes);
871 if(unlikely(mem->updated_usage_in_bytes && mem->enabled_usage_in_bytes == CONFIG_ONDEMAND_ONDEMAND && mem->usage_in_bytes))
872 mem->enabled_usage_in_bytes = CONFIG_ONDEMAND_YES;
875 // read msw_usage_in_bytes
876 if(likely(mem->filename_msw_usage_in_bytes)) {
877 mem->updated_msw_usage_in_bytes = !read_single_number_file(mem->filename_msw_usage_in_bytes, &mem->msw_usage_in_bytes);
878 if(unlikely(mem->updated_msw_usage_in_bytes && mem->enabled_msw_usage_in_bytes == CONFIG_ONDEMAND_ONDEMAND && mem->msw_usage_in_bytes))
879 mem->enabled_msw_usage_in_bytes = CONFIG_ONDEMAND_YES;
883 if(likely(mem->filename_failcnt)) {
884 if(unlikely(mem->enabled_failcnt == CONFIG_ONDEMAND_ONDEMAND && mem->delay_counter_failcnt > 0)) {
885 mem->updated_failcnt = 0;
886 mem->delay_counter_failcnt--;
889 mem->updated_failcnt = !read_single_number_file(mem->filename_failcnt, &mem->failcnt);
890 if(unlikely(mem->updated_failcnt && mem->enabled_failcnt == CONFIG_ONDEMAND_ONDEMAND)) {
891 if(unlikely(!mem->failcnt))
892 mem->delay_counter_failcnt = cgroup_recheck_zero_mem_failcnt_every_iterations;
894 mem->enabled_failcnt = CONFIG_ONDEMAND_YES;
900 static inline void cgroup_read(struct cgroup *cg) {
901 debug(D_CGROUP, "reading metrics for cgroups '%s'", cg->id);
903 cgroup_read_cpuacct_stat(&cg->cpuacct_stat);
904 cgroup_read_cpuacct_usage(&cg->cpuacct_usage);
905 cgroup_read_memory(&cg->memory);
906 cgroup_read_blkio(&cg->io_service_bytes);
907 cgroup_read_blkio(&cg->io_serviced);
908 cgroup_read_blkio(&cg->throttle_io_service_bytes);
909 cgroup_read_blkio(&cg->throttle_io_serviced);
910 cgroup_read_blkio(&cg->io_merged);
911 cgroup_read_blkio(&cg->io_queued);
914 static inline void read_all_cgroups(struct cgroup *root) {
915 debug(D_CGROUP, "reading metrics for all cgroups");
919 for(cg = root; cg ; cg = cg->next)
920 if(cg->enabled && cg->available)
924 // ----------------------------------------------------------------------------
925 // add/remove/find cgroup objects
927 #define CGROUP_CHARTID_LINE_MAX 1024
929 static inline char *cgroup_title_strdupz(const char *s) {
930 if(!s || !*s) s = "/";
932 if(*s == '/' && s[1] != '\0') s++;
934 char *r = strdupz(s);
935 netdata_fix_chart_name(r);
940 static inline char *cgroup_chart_id_strdupz(const char *s) {
941 if(!s || !*s) s = "/";
943 if(*s == '/' && s[1] != '\0') s++;
945 char *r = strdupz(s);
946 netdata_fix_chart_id(r);
951 static inline void cgroup_get_chart_name(struct cgroup *cg) {
952 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);
955 char buffer[CGROUP_CHARTID_LINE_MAX + 1];
957 snprintfz(buffer, CGROUP_CHARTID_LINE_MAX, "exec %s '%s'", cgroups_rename_script, cg->chart_id);
959 debug(D_CGROUP, "executing command '%s' for cgroup '%s'", buffer, cg->id);
960 FILE *fp = mypopen(buffer, &cgroup_pid);
962 // debug(D_CGROUP, "reading from command '%s' for cgroup '%s'", buffer, cg->id);
963 char *s = fgets(buffer, CGROUP_CHARTID_LINE_MAX, fp);
964 // debug(D_CGROUP, "closing command for cgroup '%s'", cg->id);
965 mypclose(fp, cgroup_pid);
966 // debug(D_CGROUP, "closed command for cgroup '%s'", cg->id);
968 if(s && *s && *s != '\n') {
969 debug(D_CGROUP, "cgroup '%s' should be renamed to '%s'", cg->id, s);
973 freez(cg->chart_title);
974 cg->chart_title = cgroup_title_strdupz(s);
977 cg->chart_id = cgroup_chart_id_strdupz(s);
978 cg->hash_chart = simple_hash(cg->chart_id);
982 error("CGROUP: Cannot popen(\"%s\", \"r\").", buffer);
985 static inline struct cgroup *cgroup_add(const char *id) {
986 if(!id || !*id) id = "/";
987 debug(D_CGROUP, "adding to list, cgroup with id '%s'", id);
989 if(cgroup_root_count >= cgroup_root_max) {
990 info("Maximum number of cgroups reached (%d). Not adding cgroup '%s'", cgroup_root_count, id);
994 int def = simple_pattern_matches(enabled_cgroup_patterns, id)?cgroup_enable_new_cgroups_detected_at_runtime:0;
995 struct cgroup *cg = callocz(1, sizeof(struct cgroup));
997 cg->id = strdupz(id);
998 cg->hash = simple_hash(cg->id);
1000 cg->chart_title = cgroup_title_strdupz(id);
1002 cg->chart_id = cgroup_chart_id_strdupz(id);
1003 cg->hash_chart = simple_hash(cg->chart_id);
1010 for(e = cgroup_root; e->next ;e = e->next) ;
1014 cgroup_root_count++;
1016 // fix the chart_id and title by calling the external script
1017 if(simple_pattern_matches(enabled_cgroup_renames, cg->id)) {
1019 cgroup_get_chart_name(cg);
1021 debug(D_CGROUP, "cgroup '%s' renamed to '%s' (title: '%s')", cg->id, cg->chart_id, cg->chart_title);
1024 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);
1026 int user_configurable = 1;
1028 // check if this cgroup should be a systemd service
1029 if(cgroup_enable_systemd_services) {
1030 if(simple_pattern_matches(systemd_services_cgroups, cg->id) ||
1031 simple_pattern_matches(systemd_services_cgroups, cg->chart_id)) {
1032 debug(D_CGROUP, "cgroup '%s' with chart id '%s' (title: '%s') matches systemd services cgroups", cg->id, cg->chart_id, cg->chart_title);
1034 char buffer[CGROUP_CHARTID_LINE_MAX + 1];
1035 cg->options |= CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE;
1037 strncpy(buffer, cg->id, CGROUP_CHARTID_LINE_MAX);
1040 //freez(cg->chart_id);
1041 //cg->chart_id = cgroup_chart_id_strdupz(s);
1042 //cg->hash_chart = simple_hash(cg->chart_id);
1044 // skip to the last slash
1045 size_t len = strlen(s);
1046 while(len--) if(unlikely(s[len] == '/')) break;
1047 if(len) s = &s[len + 1];
1051 while(len--) if(unlikely(s[len] == '.')) break;
1052 if(len) s[len] = '\0';
1054 freez(cg->chart_title);
1055 cg->chart_title = cgroup_title_strdupz(s);
1058 user_configurable = 0;
1060 debug(D_CGROUP, "cgroup '%s' renamed to '%s' (title: '%s')", cg->id, cg->chart_id, cg->chart_title);
1063 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);
1066 if(user_configurable) {
1067 // allow the user to enable/disable this individualy
1068 char option[FILENAME_MAX + 1];
1069 snprintfz(option, FILENAME_MAX, "enable cgroup %s", cg->chart_title);
1070 cg->enabled = (char) config_get_boolean("plugin:cgroups", option, def);
1073 // detect duplicate cgroups
1076 for (t = cgroup_root; t; t = t->next) {
1077 if (t != cg && t->enabled && t->hash_chart == cg->hash_chart && !strcmp(t->chart_id, cg->chart_id)) {
1078 if (!strncmp(t->chart_id, "/system.slice/", 14) && !strncmp(cg->chart_id, "/init.scope/system.slice/", 25)) {
1079 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'.",
1080 cg->chart_id, t->id, cg->id, t->id);
1081 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'.",
1082 cg->chart_id, t->id, cg->id, t->id);
1084 t->options |= CGROUP_OPTIONS_DISABLED_DUPLICATE;
1087 error("Control group with chart id '%s' already exists with id '%s' and is enabled and available. Disabling cgroup with id '%s'.",
1088 cg->chart_id, t->id, cg->id);
1089 debug(D_CGROUP, "Control group with chart id '%s' already exists with id '%s' and is enabled and available. Disabling cgroup with id '%s'.",
1090 cg->chart_id, t->id, cg->id);
1092 cg->options |= CGROUP_OPTIONS_DISABLED_DUPLICATE;
1100 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");
1105 static inline void cgroup_free(struct cgroup *cg) {
1106 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");
1108 freez(cg->cpuacct_usage.cpu_percpu);
1110 freez(cg->cpuacct_stat.filename);
1111 freez(cg->cpuacct_usage.filename);
1113 freez(cg->memory.filename_detailed);
1114 freez(cg->memory.filename_failcnt);
1115 freez(cg->memory.filename_usage_in_bytes);
1116 freez(cg->memory.filename_msw_usage_in_bytes);
1118 freez(cg->io_service_bytes.filename);
1119 freez(cg->io_serviced.filename);
1121 freez(cg->throttle_io_service_bytes.filename);
1122 freez(cg->throttle_io_serviced.filename);
1124 freez(cg->io_merged.filename);
1125 freez(cg->io_queued.filename);
1128 freez(cg->chart_id);
1129 freez(cg->chart_title);
1133 cgroup_root_count--;
1136 // find if a given cgroup exists
1137 static inline struct cgroup *cgroup_find(const char *id) {
1138 debug(D_CGROUP, "searching for cgroup '%s'", id);
1140 uint32_t hash = simple_hash(id);
1143 for(cg = cgroup_root; cg ; cg = cg->next) {
1144 if(hash == cg->hash && strcmp(id, cg->id) == 0)
1148 debug(D_CGROUP, "cgroup '%s' %s in memory", id, (cg)?"found":"not found");
1152 // ----------------------------------------------------------------------------
1153 // detect running cgroups
1155 // callback for find_file_in_subdirs()
1156 static inline void found_subdir_in_dir(const char *dir) {
1157 debug(D_CGROUP, "examining cgroup dir '%s'", dir);
1159 struct cgroup *cg = cgroup_find(dir);
1161 if(*dir && cgroup_max_depth > 0) {
1165 for(s = dir; *s ;s++)
1166 if(unlikely(*s == '/'))
1169 if(depth > cgroup_max_depth) {
1170 info("cgroup '%s' is too deep (%d, while max is %d)", dir, depth, cgroup_max_depth);
1174 // debug(D_CGROUP, "will add dir '%s' as cgroup", dir);
1175 cg = cgroup_add(dir);
1178 if(cg) cg->available = 1;
1181 static inline int find_dir_in_subdirs(const char *base, const char *this, void (*callback)(const char *)) {
1182 if(!this) this = base;
1183 debug(D_CGROUP, "searching for directories in '%s' (base '%s')", this?this:"", base);
1185 size_t dirlen = strlen(this), baselen = strlen(base);
1190 const char *relative_path = &this[baselen];
1191 if(!*relative_path) relative_path = "/";
1193 DIR *dir = opendir(this);
1195 error("Cannot read cgroups directory '%s'", base);
1200 callback(relative_path);
1202 struct dirent *de = NULL;
1203 while((de = readdir(dir))) {
1204 if(de->d_type == DT_DIR
1206 (de->d_name[0] == '.' && de->d_name[1] == '\0')
1207 || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
1211 if(de->d_type == DT_DIR) {
1213 const char *r = relative_path;
1214 if(*r == '\0') r = "/";
1216 // do not decent in directories we are not interested
1217 int def = simple_pattern_matches(enabled_cgroup_paths, r);
1219 // we check for this option here
1220 // so that the config will not have settings
1221 // for leaf directories
1222 char option[FILENAME_MAX + 1];
1223 snprintfz(option, FILENAME_MAX, "search for cgroups under %s", r);
1224 option[FILENAME_MAX] = '\0';
1225 enabled = config_get_boolean("plugin:cgroups", option, def);
1229 char *s = mallocz(dirlen + strlen(de->d_name) + 2);
1232 strcat(s, de->d_name);
1233 int ret2 = find_dir_in_subdirs(base, s, callback);
1234 if(ret2 > 0) ret += ret2;
1244 static inline void mark_all_cgroups_as_not_available() {
1245 debug(D_CGROUP, "marking all cgroups as not available");
1249 // mark all as not available
1250 for(cg = cgroup_root; cg ; cg = cg->next) {
1255 static inline void cleanup_all_cgroups() {
1256 struct cgroup *cg = cgroup_root, *last = NULL;
1259 if(!cg->available) {
1260 // enable the first duplicate cgroup
1263 for(t = cgroup_root; t ; t = t->next) {
1264 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)) {
1265 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);
1267 t->options &= ~CGROUP_OPTIONS_DISABLED_DUPLICATE;
1274 cgroup_root = cg->next;
1276 last->next = cg->next;
1292 static inline void find_all_cgroups() {
1293 debug(D_CGROUP, "searching for cgroups");
1295 mark_all_cgroups_as_not_available();
1297 if(cgroup_enable_cpuacct_stat || cgroup_enable_cpuacct_usage) {
1298 if(find_dir_in_subdirs(cgroup_cpuacct_base, NULL, found_subdir_in_dir) == -1) {
1299 cgroup_enable_cpuacct_stat =
1300 cgroup_enable_cpuacct_usage = CONFIG_ONDEMAND_NO;
1301 error("disabled CGROUP cpu statistics.");
1305 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) {
1306 if(find_dir_in_subdirs(cgroup_blkio_base, NULL, found_subdir_in_dir) == -1) {
1307 cgroup_enable_blkio_io =
1308 cgroup_enable_blkio_ops =
1309 cgroup_enable_blkio_throttle_io =
1310 cgroup_enable_blkio_throttle_ops =
1311 cgroup_enable_blkio_merged_ops =
1312 cgroup_enable_blkio_queued_ops = CONFIG_ONDEMAND_NO;
1313 error("disabled CGROUP blkio statistics.");
1317 if(cgroup_enable_memory || cgroup_enable_detailed_memory || cgroup_enable_swap || cgroup_enable_memory_failcnt) {
1318 if(find_dir_in_subdirs(cgroup_memory_base, NULL, found_subdir_in_dir) == -1) {
1319 cgroup_enable_memory =
1320 cgroup_enable_detailed_memory =
1321 cgroup_enable_swap =
1322 cgroup_enable_memory_failcnt = CONFIG_ONDEMAND_NO;
1323 error("disabled CGROUP memory statistics.");
1327 if(cgroup_search_in_devices) {
1328 if(find_dir_in_subdirs(cgroup_devices_base, NULL, found_subdir_in_dir) == -1) {
1329 cgroup_search_in_devices = 0;
1330 error("disabled CGROUP devices statistics.");
1334 // remove any non-existing cgroups
1335 cleanup_all_cgroups();
1339 for(cg = cgroup_root; cg ; cg = cg->next) {
1340 // fprintf(stderr, " >>> CGROUP '%s' (%u - %s) with name '%s'\n", cg->id, cg->hash, cg->available?"available":"stopped", cg->name);
1342 if(unlikely(!cg->available))
1345 debug(D_CGROUP, "checking paths for cgroup '%s'", cg->id);
1347 // check for newly added cgroups
1348 // and update the filenames they read
1349 char filename[FILENAME_MAX + 1];
1350 if(unlikely(cgroup_enable_cpuacct_stat && !cg->cpuacct_stat.filename)) {
1351 snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.stat", cgroup_cpuacct_base, cg->id);
1352 if(likely(stat(filename, &buf) != -1)) {
1353 cg->cpuacct_stat.filename = strdupz(filename);
1354 cg->cpuacct_stat.enabled = cgroup_enable_cpuacct_stat;
1355 debug(D_CGROUP, "cpuacct.stat filename for cgroup '%s': '%s'", cg->id, cg->cpuacct_stat.filename);
1358 debug(D_CGROUP, "cpuacct.stat file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1361 if(unlikely(cgroup_enable_cpuacct_usage && !cg->cpuacct_usage.filename && !(cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE))) {
1362 snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.usage_percpu", cgroup_cpuacct_base, cg->id);
1363 if(likely(stat(filename, &buf) != -1)) {
1364 cg->cpuacct_usage.filename = strdupz(filename);
1365 cg->cpuacct_usage.enabled = cgroup_enable_cpuacct_usage;
1366 debug(D_CGROUP, "cpuacct.usage_percpu filename for cgroup '%s': '%s'", cg->id, cg->cpuacct_usage.filename);
1369 debug(D_CGROUP, "cpuacct.usage_percpu file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1372 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)))) {
1373 snprintfz(filename, FILENAME_MAX, "%s%s/memory.stat", cgroup_memory_base, cg->id);
1374 if(likely(stat(filename, &buf) != -1)) {
1375 cg->memory.filename_detailed = strdupz(filename);
1376 cg->memory.enabled_detailed = (cgroup_enable_detailed_memory == CONFIG_ONDEMAND_YES)?CONFIG_ONDEMAND_YES:CONFIG_ONDEMAND_ONDEMAND;
1377 debug(D_CGROUP, "memory.stat filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_detailed);
1380 debug(D_CGROUP, "memory.stat file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1383 if(unlikely(cgroup_enable_memory && !cg->memory.filename_usage_in_bytes)) {
1384 snprintfz(filename, FILENAME_MAX, "%s%s/memory.usage_in_bytes", cgroup_memory_base, cg->id);
1385 if(likely(stat(filename, &buf) != -1)) {
1386 cg->memory.filename_usage_in_bytes = strdupz(filename);
1387 cg->memory.enabled_usage_in_bytes = cgroup_enable_memory;
1388 debug(D_CGROUP, "memory.usage_in_bytes filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_usage_in_bytes);
1391 debug(D_CGROUP, "memory.usage_in_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1394 if(unlikely(cgroup_enable_swap && !cg->memory.filename_msw_usage_in_bytes)) {
1395 snprintfz(filename, FILENAME_MAX, "%s%s/memory.msw_usage_in_bytes", cgroup_memory_base, cg->id);
1396 if(likely(stat(filename, &buf) != -1)) {
1397 cg->memory.filename_msw_usage_in_bytes = strdupz(filename);
1398 cg->memory.enabled_msw_usage_in_bytes = cgroup_enable_swap;
1399 debug(D_CGROUP, "memory.msw_usage_in_bytes filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_msw_usage_in_bytes);
1402 debug(D_CGROUP, "memory.msw_usage_in_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1405 if(unlikely(cgroup_enable_memory_failcnt && !cg->memory.filename_failcnt)) {
1406 snprintfz(filename, FILENAME_MAX, "%s%s/memory.failcnt", cgroup_memory_base, cg->id);
1407 if(likely(stat(filename, &buf) != -1)) {
1408 cg->memory.filename_failcnt = strdupz(filename);
1409 cg->memory.enabled_failcnt = cgroup_enable_memory_failcnt;
1410 debug(D_CGROUP, "memory.failcnt filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_failcnt);
1413 debug(D_CGROUP, "memory.failcnt file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1416 if(unlikely(cgroup_enable_blkio_io && !cg->io_service_bytes.filename)) {
1417 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_service_bytes", cgroup_blkio_base, cg->id);
1418 if(likely(stat(filename, &buf) != -1)) {
1419 cg->io_service_bytes.filename = strdupz(filename);
1420 cg->io_service_bytes.enabled = cgroup_enable_blkio_io;
1421 debug(D_CGROUP, "io_service_bytes filename for cgroup '%s': '%s'", cg->id, cg->io_service_bytes.filename);
1424 debug(D_CGROUP, "io_service_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1427 if(unlikely(cgroup_enable_blkio_ops && !cg->io_serviced.filename)) {
1428 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_serviced", cgroup_blkio_base, cg->id);
1429 if(likely(stat(filename, &buf) != -1)) {
1430 cg->io_serviced.filename = strdupz(filename);
1431 cg->io_serviced.enabled = cgroup_enable_blkio_ops;
1432 debug(D_CGROUP, "io_serviced filename for cgroup '%s': '%s'", cg->id, cg->io_serviced.filename);
1435 debug(D_CGROUP, "io_serviced file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1438 if(unlikely(cgroup_enable_blkio_throttle_io && !cg->throttle_io_service_bytes.filename)) {
1439 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.throttle.io_service_bytes", cgroup_blkio_base, cg->id);
1440 if(likely(stat(filename, &buf) != -1)) {
1441 cg->throttle_io_service_bytes.filename = strdupz(filename);
1442 cg->throttle_io_service_bytes.enabled = cgroup_enable_blkio_throttle_io;
1443 debug(D_CGROUP, "throttle_io_service_bytes filename for cgroup '%s': '%s'", cg->id, cg->throttle_io_service_bytes.filename);
1446 debug(D_CGROUP, "throttle_io_service_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1449 if(unlikely(cgroup_enable_blkio_throttle_ops && !cg->throttle_io_serviced.filename)) {
1450 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.throttle.io_serviced", cgroup_blkio_base, cg->id);
1451 if(likely(stat(filename, &buf) != -1)) {
1452 cg->throttle_io_serviced.filename = strdupz(filename);
1453 cg->throttle_io_serviced.enabled = cgroup_enable_blkio_throttle_ops;
1454 debug(D_CGROUP, "throttle_io_serviced filename for cgroup '%s': '%s'", cg->id, cg->throttle_io_serviced.filename);
1457 debug(D_CGROUP, "throttle_io_serviced file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1460 if(unlikely(cgroup_enable_blkio_merged_ops && !cg->io_merged.filename)) {
1461 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_merged", cgroup_blkio_base, cg->id);
1462 if(likely(stat(filename, &buf) != -1)) {
1463 cg->io_merged.filename = strdupz(filename);
1464 cg->io_merged.enabled = cgroup_enable_blkio_merged_ops;
1465 debug(D_CGROUP, "io_merged filename for cgroup '%s': '%s'", cg->id, cg->io_merged.filename);
1468 debug(D_CGROUP, "io_merged file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1471 if(unlikely(cgroup_enable_blkio_queued_ops && !cg->io_queued.filename)) {
1472 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_queued", cgroup_blkio_base, cg->id);
1473 if(likely(stat(filename, &buf) != -1)) {
1474 cg->io_queued.filename = strdupz(filename);
1475 cg->io_queued.enabled = cgroup_enable_blkio_queued_ops;
1476 debug(D_CGROUP, "io_queued filename for cgroup '%s': '%s'", cg->id, cg->io_queued.filename);
1479 debug(D_CGROUP, "io_queued file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1483 debug(D_CGROUP, "done searching for cgroups");
1487 // ----------------------------------------------------------------------------
1490 #define CHART_TITLE_MAX 300
1492 void update_services_charts(int update_every,
1495 int do_mem_detailed,
1501 int do_throttle_ops,
1507 *st_mem_usage = NULL,
1508 *st_mem_failcnt = NULL,
1509 *st_swap_usage = NULL,
1511 *st_mem_detailed_cache = NULL,
1512 *st_mem_detailed_rss = NULL,
1513 *st_mem_detailed_mapped = NULL,
1514 *st_mem_detailed_writeback = NULL,
1515 *st_mem_detailed_pgfault = NULL,
1516 *st_mem_detailed_pgmajfault = NULL,
1517 *st_mem_detailed_pgpgin = NULL,
1518 *st_mem_detailed_pgpgout = NULL,
1521 *st_io_serviced_read = NULL,
1522 *st_throttle_io_read = NULL,
1523 *st_throttle_ops_read = NULL,
1524 *st_queued_ops_read = NULL,
1525 *st_merged_ops_read = NULL,
1527 *st_io_write = NULL,
1528 *st_io_serviced_write = NULL,
1529 *st_throttle_io_write = NULL,
1530 *st_throttle_ops_write = NULL,
1531 *st_queued_ops_write = NULL,
1532 *st_merged_ops_write = NULL;
1534 // create the charts
1536 if(likely(do_cpu)) {
1537 if(unlikely(!st_cpu)) {
1538 char title[CHART_TITLE_MAX + 1];
1540 st_cpu = rrdset_find_bytype("services", "cpu");
1541 if(likely(!st_cpu)) {
1542 snprintfz(title, CHART_TITLE_MAX, "Systemd Services CPU utilization (%d%% = %d core%s)", (processors * 100), processors, (processors > 1) ? "s" : "");
1543 st_cpu = rrdset_create("services", "cpu", NULL, "cpu", "services.cpu", title, "%", CHART_PRIORITY_SYSTEMD_SERVICES, update_every, RRDSET_TYPE_STACKED);
1547 rrdset_next(st_cpu);
1550 if(likely(do_mem_usage)) {
1551 if(unlikely(!st_mem_usage)) {
1552 st_mem_usage = rrdset_find_bytype("services", "mem_usage");
1553 if(likely(!st_mem_usage))
1554 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);
1557 rrdset_next(st_mem_usage);
1560 if(likely(do_mem_detailed)) {
1561 if(unlikely(!st_mem_detailed_rss)) {
1562 st_mem_detailed_rss = rrdset_find_bytype("services", "mem_rss");
1563 if(likely(!st_mem_detailed_rss))
1564 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);
1567 rrdset_next(st_mem_detailed_rss);
1569 if(unlikely(!st_mem_detailed_mapped)) {
1570 st_mem_detailed_mapped = rrdset_find_bytype("services", "mem_mapped");
1571 if(likely(!st_mem_detailed_mapped))
1572 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);
1575 rrdset_next(st_mem_detailed_mapped);
1577 if(unlikely(!st_mem_detailed_cache)) {
1578 st_mem_detailed_cache = rrdset_find_bytype("services", "mem_cache");
1579 if(likely(!st_mem_detailed_cache))
1580 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);
1583 rrdset_next(st_mem_detailed_cache);
1585 if(unlikely(!st_mem_detailed_writeback)) {
1586 st_mem_detailed_writeback = rrdset_find_bytype("services", "mem_writeback");
1587 if(likely(!st_mem_detailed_writeback))
1588 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);
1591 rrdset_next(st_mem_detailed_writeback);
1593 if(unlikely(!st_mem_detailed_pgfault)) {
1594 st_mem_detailed_pgfault = rrdset_find_bytype("services", "mem_pgfault");
1595 if(likely(!st_mem_detailed_pgfault))
1596 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);
1599 rrdset_next(st_mem_detailed_pgfault);
1601 if(unlikely(!st_mem_detailed_pgmajfault)) {
1602 st_mem_detailed_pgmajfault = rrdset_find_bytype("services", "mem_pgmajfault");
1603 if(likely(!st_mem_detailed_pgmajfault))
1604 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);
1607 rrdset_next(st_mem_detailed_pgmajfault);
1609 if(unlikely(!st_mem_detailed_pgpgin)) {
1610 st_mem_detailed_pgpgin = rrdset_find_bytype("services", "mem_pgpgin");
1611 if(likely(!st_mem_detailed_pgpgin))
1612 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);
1615 rrdset_next(st_mem_detailed_pgpgin);
1617 if(unlikely(!st_mem_detailed_pgpgout)) {
1618 st_mem_detailed_pgpgout = rrdset_find_bytype("services", "mem_pgpgout");
1619 if(likely(!st_mem_detailed_pgpgout))
1620 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);
1623 rrdset_next(st_mem_detailed_pgpgout);
1626 if(likely(do_mem_failcnt)) {
1627 if(unlikely(!st_mem_failcnt)) {
1628 st_mem_failcnt = rrdset_find_bytype("services", "mem_failcnt");
1629 if(likely(!st_mem_failcnt))
1630 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);
1633 rrdset_next(st_mem_failcnt);
1636 if(likely(do_swap_usage)) {
1637 if(unlikely(!st_swap_usage)) {
1638 st_swap_usage = rrdset_find_bytype("services", "swap_usage");
1639 if(likely(!st_swap_usage))
1640 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);
1643 rrdset_next(st_swap_usage);
1647 if(unlikely(!st_io_read)) {
1648 st_io_read = rrdset_find_bytype("services", "io_read");
1649 if(likely(!st_io_read))
1650 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);
1653 rrdset_next(st_io_read);
1655 if(unlikely(!st_io_write)) {
1656 st_io_write = rrdset_find_bytype("services", "io_write");
1657 if(likely(!st_io_write))
1658 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);
1661 rrdset_next(st_io_write);
1664 if(likely(do_io_ops)) {
1665 if(unlikely(!st_io_serviced_read)) {
1666 st_io_serviced_read = rrdset_find_bytype("services", "io_ops_read");
1667 if(likely(!st_io_serviced_read))
1668 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);
1671 rrdset_next(st_io_serviced_read);
1673 if(unlikely(!st_io_serviced_write)) {
1674 st_io_serviced_write = rrdset_find_bytype("services", "io_ops_write");
1675 if(likely(!st_io_serviced_write))
1676 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);
1679 rrdset_next(st_io_serviced_write);
1682 if(likely(do_throttle_io)) {
1683 if(unlikely(!st_throttle_io_read)) {
1684 st_throttle_io_read = rrdset_find_bytype("services", "throttle_io_read");
1685 if(likely(!st_throttle_io_read))
1686 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);
1689 rrdset_next(st_throttle_io_read);
1691 if(unlikely(!st_throttle_io_write)) {
1692 st_throttle_io_write = rrdset_find_bytype("services", "throttle_io_write");
1693 if(likely(!st_throttle_io_write))
1694 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);
1697 rrdset_next(st_throttle_io_write);
1700 if(likely(do_throttle_ops)) {
1701 if(unlikely(!st_throttle_ops_read)) {
1702 st_throttle_ops_read = rrdset_find_bytype("services", "throttle_io_ops_read");
1703 if(likely(!st_throttle_ops_read))
1704 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);
1707 rrdset_next(st_throttle_ops_read);
1709 if(unlikely(!st_throttle_ops_write)) {
1710 st_throttle_ops_write = rrdset_find_bytype("services", "throttle_io_ops_write");
1711 if(likely(!st_throttle_ops_write))
1712 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);
1715 rrdset_next(st_throttle_ops_write);
1718 if(likely(do_queued_ops)) {
1719 if(unlikely(!st_queued_ops_read)) {
1720 st_queued_ops_read = rrdset_find_bytype("services", "queued_io_ops_read");
1721 if(likely(!st_queued_ops_read))
1722 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);
1725 rrdset_next(st_queued_ops_read);
1727 if(unlikely(!st_queued_ops_write)) {
1728 st_queued_ops_write = rrdset_find_bytype("services", "queued_io_ops_write");
1729 if(likely(!st_queued_ops_write))
1730 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);
1733 rrdset_next(st_queued_ops_write);
1736 if(likely(do_merged_ops)) {
1737 if(unlikely(!st_merged_ops_read)) {
1738 st_merged_ops_read = rrdset_find_bytype("services", "merged_io_ops_read");
1739 if(likely(!st_merged_ops_read))
1740 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);
1743 rrdset_next(st_merged_ops_read);
1745 if(unlikely(!st_merged_ops_write)) {
1746 st_merged_ops_write = rrdset_find_bytype("services", "merged_io_ops_write");
1747 if(likely(!st_merged_ops_write))
1748 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);
1751 rrdset_next(st_merged_ops_write);
1754 // update the values
1756 for(cg = cgroup_root; cg ; cg = cg->next) {
1757 if(unlikely(!cg->available || !cg->enabled || !(cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE)))
1760 if(likely(do_cpu && cg->cpuacct_stat.updated)) {
1761 if(unlikely(!cg->rd_cpu))
1762 cg->rd_cpu = rrddim_add(st_cpu, cg->chart_id, cg->chart_title, 100, hz, RRDDIM_INCREMENTAL);
1764 rrddim_set_by_pointer(st_cpu, cg->rd_cpu, cg->cpuacct_stat.user + cg->cpuacct_stat.system);
1767 if(likely(do_mem_usage && cg->memory.updated_usage_in_bytes)) {
1768 if(unlikely(!cg->rd_mem_usage))
1769 cg->rd_mem_usage = rrddim_add(st_mem_usage, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1771 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));
1774 if(likely(do_mem_detailed && cg->memory.updated_detailed)) {
1775 if(unlikely(!cg->rd_mem_detailed_rss))
1776 cg->rd_mem_detailed_rss = rrddim_add(st_mem_detailed_rss, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1777 rrddim_set_by_pointer(st_mem_detailed_rss, cg->rd_mem_detailed_rss, cg->memory.rss + cg->memory.rss_huge);
1779 if(unlikely(!cg->rd_mem_detailed_mapped))
1780 cg->rd_mem_detailed_mapped = rrddim_add(st_mem_detailed_mapped, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1781 rrddim_set_by_pointer(st_mem_detailed_mapped, cg->rd_mem_detailed_mapped, cg->memory.mapped_file);
1783 if(unlikely(!cg->rd_mem_detailed_cache))
1784 cg->rd_mem_detailed_cache = rrddim_add(st_mem_detailed_cache, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1785 rrddim_set_by_pointer(st_mem_detailed_cache, cg->rd_mem_detailed_cache, cg->memory.cache);
1787 if(unlikely(!cg->rd_mem_detailed_writeback))
1788 cg->rd_mem_detailed_writeback = rrddim_add(st_mem_detailed_writeback, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1789 rrddim_set_by_pointer(st_mem_detailed_writeback, cg->rd_mem_detailed_writeback, cg->memory.writeback);
1791 if(unlikely(!cg->rd_mem_detailed_pgfault))
1792 cg->rd_mem_detailed_pgfault = rrddim_add(st_mem_detailed_pgfault, cg->chart_id, cg->chart_title, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1793 rrddim_set_by_pointer(st_mem_detailed_pgfault, cg->rd_mem_detailed_pgfault, cg->memory.pgfault);
1795 if(unlikely(!cg->rd_mem_detailed_pgmajfault))
1796 cg->rd_mem_detailed_pgmajfault = rrddim_add(st_mem_detailed_pgmajfault, cg->chart_id, cg->chart_title, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1797 rrddim_set_by_pointer(st_mem_detailed_pgmajfault, cg->rd_mem_detailed_pgmajfault, cg->memory.pgmajfault);
1799 if(unlikely(!cg->rd_mem_detailed_pgpgin))
1800 cg->rd_mem_detailed_pgpgin = rrddim_add(st_mem_detailed_pgpgin, cg->chart_id, cg->chart_title, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1801 rrddim_set_by_pointer(st_mem_detailed_pgpgin, cg->rd_mem_detailed_pgpgin, cg->memory.pgpgin);
1803 if(unlikely(!cg->rd_mem_detailed_pgpgout))
1804 cg->rd_mem_detailed_pgpgout = rrddim_add(st_mem_detailed_pgpgout, cg->chart_id, cg->chart_title, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1805 rrddim_set_by_pointer(st_mem_detailed_pgpgout, cg->rd_mem_detailed_pgpgout, cg->memory.pgpgout);
1808 if(likely(do_mem_failcnt && cg->memory.updated_failcnt)) {
1809 if(unlikely(!cg->rd_mem_failcnt))
1810 cg->rd_mem_failcnt = rrddim_add(st_mem_failcnt, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1812 rrddim_set_by_pointer(st_mem_failcnt, cg->rd_mem_failcnt, cg->memory.failcnt);
1815 if(likely(do_swap_usage && cg->memory.updated_msw_usage_in_bytes)) {
1816 if(unlikely(!cg->rd_swap_usage))
1817 cg->rd_swap_usage = rrddim_add(st_swap_usage, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1819 rrddim_set_by_pointer(st_swap_usage, cg->rd_swap_usage, cg->memory.msw_usage_in_bytes);
1822 if(likely(do_io && cg->io_service_bytes.updated)) {
1823 if(unlikely(!cg->rd_io_service_bytes_read))
1824 cg->rd_io_service_bytes_read = rrddim_add(st_io_read, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1826 rrddim_set_by_pointer(st_io_read, cg->rd_io_service_bytes_read, cg->io_service_bytes.Read);
1828 if(unlikely(!cg->rd_io_service_bytes_write))
1829 cg->rd_io_service_bytes_write = rrddim_add(st_io_write, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1831 rrddim_set_by_pointer(st_io_write, cg->rd_io_service_bytes_write, cg->io_service_bytes.Write);
1834 if(likely(do_io_ops && cg->io_serviced.updated)) {
1835 if(unlikely(!cg->rd_io_serviced_read))
1836 cg->rd_io_serviced_read = rrddim_add(st_io_serviced_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1838 rrddim_set_by_pointer(st_io_serviced_read, cg->rd_io_serviced_read, cg->io_serviced.Read);
1840 if(unlikely(!cg->rd_io_serviced_write))
1841 cg->rd_io_serviced_write = rrddim_add(st_io_serviced_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1843 rrddim_set_by_pointer(st_io_serviced_write, cg->rd_io_serviced_write, cg->io_serviced.Write);
1846 if(likely(do_throttle_io && cg->throttle_io_service_bytes.updated)) {
1847 if(unlikely(!cg->rd_throttle_io_read))
1848 cg->rd_throttle_io_read = rrddim_add(st_throttle_io_read, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1850 rrddim_set_by_pointer(st_throttle_io_read, cg->rd_throttle_io_read, cg->throttle_io_service_bytes.Read);
1852 if(unlikely(!cg->rd_throttle_io_write))
1853 cg->rd_throttle_io_write = rrddim_add(st_throttle_io_write, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1855 rrddim_set_by_pointer(st_throttle_io_write, cg->rd_throttle_io_write, cg->throttle_io_service_bytes.Write);
1858 if(likely(do_throttle_ops && cg->throttle_io_serviced.updated)) {
1859 if(unlikely(!cg->rd_throttle_io_serviced_read))
1860 cg->rd_throttle_io_serviced_read = rrddim_add(st_throttle_ops_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1862 rrddim_set_by_pointer(st_throttle_ops_read, cg->rd_throttle_io_serviced_read, cg->throttle_io_serviced.Read);
1864 if(unlikely(!cg->rd_throttle_io_serviced_write))
1865 cg->rd_throttle_io_serviced_write = rrddim_add(st_throttle_ops_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1867 rrddim_set_by_pointer(st_throttle_ops_write, cg->rd_throttle_io_serviced_write, cg->throttle_io_serviced.Write);
1870 if(likely(do_queued_ops && cg->io_queued.updated)) {
1871 if(unlikely(!cg->rd_io_queued_read))
1872 cg->rd_io_queued_read = rrddim_add(st_queued_ops_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1874 rrddim_set_by_pointer(st_queued_ops_read, cg->rd_io_queued_read, cg->io_queued.Read);
1876 if(unlikely(!cg->rd_io_queued_write))
1877 cg->rd_io_queued_write = rrddim_add(st_queued_ops_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1879 rrddim_set_by_pointer(st_queued_ops_write, cg->rd_io_queued_write, cg->io_queued.Write);
1882 if(likely(do_merged_ops && cg->io_merged.updated)) {
1883 if(unlikely(!cg->rd_io_merged_read))
1884 cg->rd_io_merged_read = rrddim_add(st_merged_ops_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1886 rrddim_set_by_pointer(st_merged_ops_read, cg->rd_io_merged_read, cg->io_merged.Read);
1888 if(unlikely(!cg->rd_io_merged_write))
1889 cg->rd_io_merged_write = rrddim_add(st_merged_ops_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1891 rrddim_set_by_pointer(st_merged_ops_write, cg->rd_io_merged_write, cg->io_merged.Write);
1895 // complete the iteration
1897 rrdset_done(st_cpu);
1899 if(likely(do_mem_usage))
1900 rrdset_done(st_mem_usage);
1902 if(unlikely(do_mem_detailed)) {
1903 rrdset_done(st_mem_detailed_cache);
1904 rrdset_done(st_mem_detailed_rss);
1905 rrdset_done(st_mem_detailed_mapped);
1906 rrdset_done(st_mem_detailed_writeback);
1907 rrdset_done(st_mem_detailed_pgfault);
1908 rrdset_done(st_mem_detailed_pgmajfault);
1909 rrdset_done(st_mem_detailed_pgpgin);
1910 rrdset_done(st_mem_detailed_pgpgout);
1913 if(likely(do_mem_failcnt))
1914 rrdset_done(st_mem_failcnt);
1916 if(likely(do_swap_usage))
1917 rrdset_done(st_swap_usage);
1920 rrdset_done(st_io_read);
1921 rrdset_done(st_io_write);
1924 if(likely(do_io_ops)) {
1925 rrdset_done(st_io_serviced_read);
1926 rrdset_done(st_io_serviced_write);
1929 if(likely(do_throttle_io)) {
1930 rrdset_done(st_throttle_io_read);
1931 rrdset_done(st_throttle_io_write);
1934 if(likely(do_throttle_ops)) {
1935 rrdset_done(st_throttle_ops_read);
1936 rrdset_done(st_throttle_ops_write);
1939 if(likely(do_queued_ops)) {
1940 rrdset_done(st_queued_ops_read);
1941 rrdset_done(st_queued_ops_write);
1944 if(likely(do_merged_ops)) {
1945 rrdset_done(st_merged_ops_read);
1946 rrdset_done(st_merged_ops_write);
1950 static inline char *cgroup_chart_type(char *buffer, const char *id, size_t len) {
1951 if(buffer[0]) return buffer;
1953 if(id[0] == '\0' || (id[0] == '/' && id[1] == '\0'))
1954 strncpy(buffer, "cgroup_root", len);
1956 snprintfz(buffer, len, "cgroup_%s", id);
1958 netdata_fix_chart_id(buffer);
1962 void update_cgroup_charts(int update_every) {
1963 debug(D_CGROUP, "updating cgroups charts");
1965 char type[RRD_ID_LENGTH_MAX + 1];
1966 char title[CHART_TITLE_MAX + 1];
1968 int services_do_cpu = 0,
1969 services_do_mem_usage = 0,
1970 services_do_mem_detailed = 0,
1971 services_do_mem_failcnt = 0,
1972 services_do_swap_usage = 0,
1974 services_do_io_ops = 0,
1975 services_do_throttle_io = 0,
1976 services_do_throttle_ops = 0,
1977 services_do_queued_ops = 0,
1978 services_do_merged_ops = 0;
1981 for(cg = cgroup_root; cg ; cg = cg->next) {
1982 if(unlikely(!cg->available || !cg->enabled))
1985 if(likely(cgroup_enable_systemd_services && cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE)) {
1986 if(cg->cpuacct_stat.updated && cg->cpuacct_stat.enabled == CONFIG_ONDEMAND_YES) services_do_cpu++;
1988 if(cgroup_enable_systemd_services_detailed_memory && cg->memory.updated_detailed && cg->memory.enabled_detailed) services_do_mem_detailed++;
1989 if(cg->memory.updated_usage_in_bytes && cg->memory.enabled_usage_in_bytes == CONFIG_ONDEMAND_YES) services_do_mem_usage++;
1990 if(cg->memory.updated_failcnt && cg->memory.enabled_failcnt == CONFIG_ONDEMAND_YES) services_do_mem_failcnt++;
1991 if(cg->memory.updated_msw_usage_in_bytes && cg->memory.enabled_msw_usage_in_bytes == CONFIG_ONDEMAND_YES) services_do_swap_usage++;
1993 if(cg->io_service_bytes.updated && cg->io_service_bytes.enabled == CONFIG_ONDEMAND_YES) services_do_io++;
1994 if(cg->io_serviced.updated && cg->io_serviced.enabled == CONFIG_ONDEMAND_YES) services_do_io_ops++;
1995 if(cg->throttle_io_service_bytes.updated && cg->throttle_io_service_bytes.enabled == CONFIG_ONDEMAND_YES) services_do_throttle_io++;
1996 if(cg->throttle_io_serviced.updated && cg->throttle_io_serviced.enabled == CONFIG_ONDEMAND_YES) services_do_throttle_ops++;
1997 if(cg->io_queued.updated && cg->io_queued.enabled == CONFIG_ONDEMAND_YES) services_do_queued_ops++;
1998 if(cg->io_merged.updated && cg->io_merged.enabled == CONFIG_ONDEMAND_YES) services_do_merged_ops++;
2004 if(likely(cg->cpuacct_stat.updated && cg->cpuacct_stat.enabled == CONFIG_ONDEMAND_YES)) {
2005 if(unlikely(!cg->st_cpu)) {
2006 cg->st_cpu = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "cpu");
2007 if(likely(!cg->st_cpu)) {
2008 snprintfz(title, CHART_TITLE_MAX, "CPU Usage (%d%% = %d core%s) for cgroup %s", (processors * 100), processors, (processors > 1) ? "s" : "", cg->chart_title);
2009 cg->st_cpu = rrdset_create(type, "cpu", NULL, "cpu", "cgroup.cpu", title, "%", CHART_PRIORITY_CONTAINERS, update_every, RRDSET_TYPE_STACKED);
2011 rrddim_add(cg->st_cpu, "user", NULL, 100, hz, RRDDIM_INCREMENTAL);
2012 rrddim_add(cg->st_cpu, "system", NULL, 100, hz, RRDDIM_INCREMENTAL);
2015 rrdset_next(cg->st_cpu);
2017 rrddim_set(cg->st_cpu, "user", cg->cpuacct_stat.user);
2018 rrddim_set(cg->st_cpu, "system", cg->cpuacct_stat.system);
2019 rrdset_done(cg->st_cpu);
2022 if(likely(cg->cpuacct_usage.updated && cg->cpuacct_usage.enabled == CONFIG_ONDEMAND_YES)) {
2023 char id[RRD_ID_LENGTH_MAX + 1];
2026 if(unlikely(!cg->st_cpu_per_core)) {
2027 cg->st_cpu_per_core = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "cpu_per_core");
2028 if(likely(!cg->st_cpu_per_core)) {
2029 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);
2030 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);
2032 for(i = 0; i < cg->cpuacct_usage.cpus; i++) {
2033 snprintfz(id, CHART_TITLE_MAX, "cpu%u", i);
2034 rrddim_add(cg->st_cpu_per_core, id, NULL, 100, 1000000000, RRDDIM_INCREMENTAL);
2038 rrdset_next(cg->st_cpu_per_core);
2040 for(i = 0; i < cg->cpuacct_usage.cpus ;i++) {
2041 snprintfz(id, CHART_TITLE_MAX, "cpu%u", i);
2042 rrddim_set(cg->st_cpu_per_core, id, cg->cpuacct_usage.cpu_percpu[i]);
2044 rrdset_done(cg->st_cpu_per_core);
2047 if(likely(cg->memory.updated_detailed && cg->memory.enabled_detailed == CONFIG_ONDEMAND_YES)) {
2048 if(unlikely(!cg->st_mem)) {
2049 cg->st_mem = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem");
2050 if(likely(!cg->st_mem)) {
2051 snprintfz(title, CHART_TITLE_MAX, "Memory Usage for cgroup %s", cg->chart_title);
2052 cg->st_mem = rrdset_create(type, "mem", NULL, "mem", "cgroup.mem", title, "MB", CHART_PRIORITY_CONTAINERS + 210, update_every, RRDSET_TYPE_STACKED);
2055 rrddim_add(cg->st_mem, "cache", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2056 rrddim_add(cg->st_mem, "rss", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2057 if(cg->memory.detailed_has_swap)
2058 rrddim_add(cg->st_mem, "swap", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2059 rrddim_add(cg->st_mem, "rss_huge", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2060 rrddim_add(cg->st_mem, "mapped_file", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2063 rrdset_next(cg->st_mem);
2065 rrddim_set(cg->st_mem, "cache", cg->memory.cache);
2066 rrddim_set(cg->st_mem, "rss", cg->memory.rss);
2067 if(cg->memory.detailed_has_swap)
2068 rrddim_set(cg->st_mem, "swap", cg->memory.swap);
2069 rrddim_set(cg->st_mem, "rss_huge", cg->memory.rss_huge);
2070 rrddim_set(cg->st_mem, "mapped_file", cg->memory.mapped_file);
2071 rrdset_done(cg->st_mem);
2073 if(unlikely(!cg->st_writeback)) {
2074 cg->st_writeback = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "writeback");
2075 if(likely(!cg->st_writeback)) {
2076 snprintfz(title, CHART_TITLE_MAX, "Writeback Memory for cgroup %s", cg->chart_title);
2077 cg->st_writeback = rrdset_create(type, "writeback", NULL, "mem", "cgroup.writeback", title, "MB", CHART_PRIORITY_CONTAINERS + 300, update_every, RRDSET_TYPE_AREA);
2080 if(cg->memory.detailed_has_dirty)
2081 rrddim_add(cg->st_writeback, "dirty", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2082 rrddim_add(cg->st_writeback, "writeback", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2085 rrdset_next(cg->st_writeback);
2087 if(cg->memory.detailed_has_dirty)
2088 rrddim_set(cg->st_writeback, "dirty", cg->memory.dirty);
2089 rrddim_set(cg->st_writeback, "writeback", cg->memory.writeback);
2090 rrdset_done(cg->st_writeback);
2092 if(unlikely(!cg->st_mem_activity)) {
2093 cg->st_mem_activity = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_activity");
2094 if(likely(!cg->st_mem_activity)) {
2095 snprintfz(title, CHART_TITLE_MAX, "Memory Activity for cgroup %s", cg->chart_title);
2096 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);
2098 rrddim_add(cg->st_mem_activity, "pgpgin", "in", system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
2099 rrddim_add(cg->st_mem_activity, "pgpgout", "out", -system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
2102 rrdset_next(cg->st_mem_activity);
2104 rrddim_set(cg->st_mem_activity, "pgpgin", cg->memory.pgpgin);
2105 rrddim_set(cg->st_mem_activity, "pgpgout", cg->memory.pgpgout);
2106 rrdset_done(cg->st_mem_activity);
2108 if(unlikely(!cg->st_pgfaults)) {
2109 cg->st_pgfaults = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "pgfaults");
2110 if(likely(!cg->st_pgfaults)) {
2111 snprintfz(title, CHART_TITLE_MAX, "Memory Page Faults for cgroup %s", cg->chart_title);
2112 cg->st_pgfaults = rrdset_create(type, "pgfaults", NULL, "mem", "cgroup.pgfaults", title, "MB/s", CHART_PRIORITY_CONTAINERS + 500, update_every, RRDSET_TYPE_LINE);
2114 rrddim_add(cg->st_pgfaults, "pgfault", NULL, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
2115 rrddim_add(cg->st_pgfaults, "pgmajfault", "swap", -system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
2118 rrdset_next(cg->st_pgfaults);
2120 rrddim_set(cg->st_pgfaults, "pgfault", cg->memory.pgfault);
2121 rrddim_set(cg->st_pgfaults, "pgmajfault", cg->memory.pgmajfault);
2122 rrdset_done(cg->st_pgfaults);
2125 if(likely(cg->memory.updated_usage_in_bytes && cg->memory.enabled_usage_in_bytes == CONFIG_ONDEMAND_YES)) {
2126 if(unlikely(!cg->st_mem_usage)) {
2127 cg->st_mem_usage = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_usage");
2128 if(likely(!cg->st_mem_usage)) {
2129 snprintfz(title, CHART_TITLE_MAX, "Used Memory %sfor cgroup %s", (cgroup_used_memory_without_cache && cg->memory.updated_detailed)?"without Cache ":"", cg->chart_title);
2130 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);
2132 rrddim_add(cg->st_mem_usage, "ram", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2133 rrddim_add(cg->st_mem_usage, "swap", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
2136 rrdset_next(cg->st_mem_usage);
2138 rrddim_set(cg->st_mem_usage, "ram", cg->memory.usage_in_bytes - ((cgroup_used_memory_without_cache)?cg->memory.cache:0));
2139 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);
2140 rrdset_done(cg->st_mem_usage);
2143 if(likely(cg->memory.updated_failcnt && cg->memory.enabled_failcnt == CONFIG_ONDEMAND_YES)) {
2144 if(unlikely(!cg->st_mem_failcnt)) {
2145 cg->st_mem_failcnt = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_failcnt");
2146 if(likely(!cg->st_mem_failcnt)) {
2147 snprintfz(title, CHART_TITLE_MAX, "Memory Limit Failures for cgroup %s", cg->chart_title);
2148 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);
2150 rrddim_add(cg->st_mem_failcnt, "failures", NULL, 1, 1, RRDDIM_INCREMENTAL);
2153 rrdset_next(cg->st_mem_failcnt);
2155 rrddim_set(cg->st_mem_failcnt, "failures", cg->memory.failcnt);
2156 rrdset_done(cg->st_mem_failcnt);
2159 if(likely(cg->io_service_bytes.updated && cg->io_service_bytes.enabled == CONFIG_ONDEMAND_YES)) {
2160 if(unlikely(!cg->st_io)) {
2161 cg->st_io = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "io");
2162 if(likely(!cg->st_io)) {
2163 snprintfz(title, CHART_TITLE_MAX, "I/O Bandwidth (all disks) for cgroup %s", cg->chart_title);
2164 cg->st_io = rrdset_create(type, "io", NULL, "disk", "cgroup.io", title, "KB/s", CHART_PRIORITY_CONTAINERS + 1200, update_every, RRDSET_TYPE_AREA);
2166 rrddim_add(cg->st_io, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
2167 rrddim_add(cg->st_io, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
2170 rrdset_next(cg->st_io);
2172 rrddim_set(cg->st_io, "read", cg->io_service_bytes.Read);
2173 rrddim_set(cg->st_io, "write", cg->io_service_bytes.Write);
2174 rrdset_done(cg->st_io);
2177 if(likely(cg->io_serviced.updated && cg->io_serviced.enabled == CONFIG_ONDEMAND_YES)) {
2178 if(unlikely(!cg->st_serviced_ops)) {
2179 cg->st_serviced_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "serviced_ops");
2180 if(likely(!cg->st_serviced_ops)) {
2181 snprintfz(title, CHART_TITLE_MAX, "Serviced I/O Operations (all disks) for cgroup %s", cg->chart_title);
2182 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);
2184 rrddim_add(cg->st_serviced_ops, "read", NULL, 1, 1, RRDDIM_INCREMENTAL);
2185 rrddim_add(cg->st_serviced_ops, "write", NULL, -1, 1, RRDDIM_INCREMENTAL);
2188 rrdset_next(cg->st_serviced_ops);
2190 rrddim_set(cg->st_serviced_ops, "read", cg->io_serviced.Read);
2191 rrddim_set(cg->st_serviced_ops, "write", cg->io_serviced.Write);
2192 rrdset_done(cg->st_serviced_ops);
2195 if(likely(cg->throttle_io_service_bytes.updated && cg->throttle_io_service_bytes.enabled == CONFIG_ONDEMAND_YES)) {
2196 if(unlikely(!cg->st_throttle_io)) {
2197 cg->st_throttle_io = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "throttle_io");
2198 if(likely(!cg->st_throttle_io)) {
2199 snprintfz(title, CHART_TITLE_MAX, "Throttle I/O Bandwidth (all disks) for cgroup %s", cg->chart_title);
2200 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);
2202 rrddim_add(cg->st_throttle_io, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
2203 rrddim_add(cg->st_throttle_io, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
2206 rrdset_next(cg->st_throttle_io);
2208 rrddim_set(cg->st_throttle_io, "read", cg->throttle_io_service_bytes.Read);
2209 rrddim_set(cg->st_throttle_io, "write", cg->throttle_io_service_bytes.Write);
2210 rrdset_done(cg->st_throttle_io);
2213 if(likely(cg->throttle_io_serviced.updated && cg->throttle_io_serviced.enabled == CONFIG_ONDEMAND_YES)) {
2214 if(unlikely(!cg->st_throttle_serviced_ops)) {
2215 cg->st_throttle_serviced_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "throttle_serviced_ops");
2216 if(likely(!cg->st_throttle_serviced_ops)) {
2217 snprintfz(title, CHART_TITLE_MAX, "Throttle Serviced I/O Operations (all disks) for cgroup %s", cg->chart_title);
2218 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);
2220 rrddim_add(cg->st_throttle_serviced_ops, "read", NULL, 1, 1, RRDDIM_INCREMENTAL);
2221 rrddim_add(cg->st_throttle_serviced_ops, "write", NULL, -1, 1, RRDDIM_INCREMENTAL);
2224 rrdset_next(cg->st_throttle_serviced_ops);
2226 rrddim_set(cg->st_throttle_serviced_ops, "read", cg->throttle_io_serviced.Read);
2227 rrddim_set(cg->st_throttle_serviced_ops, "write", cg->throttle_io_serviced.Write);
2228 rrdset_done(cg->st_throttle_serviced_ops);
2231 if(likely(cg->io_queued.updated && cg->io_queued.enabled == CONFIG_ONDEMAND_YES)) {
2232 if(unlikely(!cg->st_queued_ops)) {
2233 cg->st_queued_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "queued_ops");
2234 if(likely(!cg->st_queued_ops)) {
2235 snprintfz(title, CHART_TITLE_MAX, "Queued I/O Operations (all disks) for cgroup %s", cg->chart_title);
2236 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);
2238 rrddim_add(cg->st_queued_ops, "read", NULL, 1, 1, RRDDIM_ABSOLUTE);
2239 rrddim_add(cg->st_queued_ops, "write", NULL, -1, 1, RRDDIM_ABSOLUTE);
2242 rrdset_next(cg->st_queued_ops);
2244 rrddim_set(cg->st_queued_ops, "read", cg->io_queued.Read);
2245 rrddim_set(cg->st_queued_ops, "write", cg->io_queued.Write);
2246 rrdset_done(cg->st_queued_ops);
2249 if(likely(cg->io_merged.updated && cg->io_merged.enabled == CONFIG_ONDEMAND_YES)) {
2250 if(unlikely(!cg->st_merged_ops)) {
2251 cg->st_merged_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "merged_ops");
2252 if(likely(!cg->st_merged_ops)) {
2253 snprintfz(title, CHART_TITLE_MAX, "Merged I/O Operations (all disks) for cgroup %s", cg->chart_title);
2254 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);
2256 rrddim_add(cg->st_merged_ops, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
2257 rrddim_add(cg->st_merged_ops, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
2260 rrdset_next(cg->st_merged_ops);
2262 rrddim_set(cg->st_merged_ops, "read", cg->io_merged.Read);
2263 rrddim_set(cg->st_merged_ops, "write", cg->io_merged.Write);
2264 rrdset_done(cg->st_merged_ops);
2268 if(likely(cgroup_enable_systemd_services))
2269 update_services_charts(update_every,
2271 services_do_mem_usage,
2272 services_do_mem_detailed,
2273 services_do_mem_failcnt,
2274 services_do_swap_usage,
2277 services_do_throttle_io,
2278 services_do_throttle_ops,
2279 services_do_queued_ops,
2280 services_do_merged_ops
2283 debug(D_CGROUP, "done updating cgroups charts");
2286 // ----------------------------------------------------------------------------
2289 void *cgroups_main(void *ptr) {
2290 struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
2292 info("CGROUP Plugin thread created with task id %d", gettid());
2294 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
2295 error("Cannot set pthread cancel type to DEFERRED.");
2297 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
2298 error("Cannot set pthread cancel state to ENABLE.");
2300 struct rusage thread;
2302 // when ZERO, attempt to do it
2303 int vdo_cpu_netdata = config_get_boolean("plugin:cgroups", "cgroups plugin resource charts", 1);
2305 read_cgroup_plugin_configuration();
2307 RRDSET *stcpu_thread = NULL;
2309 usec_t step = cgroup_update_every * USEC_PER_SEC;
2310 usec_t find_every = cgroup_check_for_new_every * USEC_PER_SEC, find_next = 0;
2312 usec_t now = now_monotonic_usec();
2313 usec_t next = now - (now % step) + step;
2316 sleep_usec(next - now);
2317 now = now_monotonic_usec();
2320 if(unlikely(netdata_exit)) break;
2322 // BEGIN -- the job to be done
2324 if(unlikely(now >= find_next)) {
2326 find_next = now + find_every;
2329 read_all_cgroups(cgroup_root);
2330 update_cgroup_charts(cgroup_update_every);
2332 // END -- the job is done
2334 // --------------------------------------------------------------------
2336 if(vdo_cpu_netdata) {
2337 getrusage(RUSAGE_THREAD, &thread);
2339 if(unlikely(!stcpu_thread)) {
2340 stcpu_thread = rrdset_find("netdata.plugin_cgroups_cpu");
2341 if(unlikely(!stcpu_thread))
2342 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);
2344 rrddim_add(stcpu_thread, "user", NULL, 1, 1000, RRDDIM_INCREMENTAL);
2345 rrddim_add(stcpu_thread, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
2348 rrdset_next(stcpu_thread);
2350 rrddim_set(stcpu_thread, "user" , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
2351 rrddim_set(stcpu_thread, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
2352 rrdset_done(stcpu_thread);
2356 info("CGROUP thread exiting");
2358 static_thread->enabled = 0;