2 #include "adaptive_resortable_list.h"
4 // ----------------------------------------------------------------------------
7 #define CHART_PRIORITY_SYSTEMD_SERVICES 19000
8 #define CHART_PRIORITY_CONTAINERS 40000
10 static long system_page_size = 4096; // system will be queried via sysconf() in configuration()
12 static int cgroup_enable_cpuacct_stat = CONFIG_ONDEMAND_ONDEMAND;
13 static int cgroup_enable_cpuacct_usage = CONFIG_ONDEMAND_ONDEMAND;
14 static int cgroup_enable_memory = CONFIG_ONDEMAND_ONDEMAND;
15 static int cgroup_enable_detailed_memory = CONFIG_ONDEMAND_ONDEMAND;
16 static int cgroup_enable_memory_failcnt = CONFIG_ONDEMAND_ONDEMAND;
17 static int cgroup_enable_swap = CONFIG_ONDEMAND_ONDEMAND;
18 static int cgroup_enable_blkio_io = CONFIG_ONDEMAND_ONDEMAND;
19 static int cgroup_enable_blkio_ops = CONFIG_ONDEMAND_ONDEMAND;
20 static int cgroup_enable_blkio_throttle_io = CONFIG_ONDEMAND_ONDEMAND;
21 static int cgroup_enable_blkio_throttle_ops = CONFIG_ONDEMAND_ONDEMAND;
22 static int cgroup_enable_blkio_merged_ops = CONFIG_ONDEMAND_ONDEMAND;
23 static int cgroup_enable_blkio_queued_ops = CONFIG_ONDEMAND_ONDEMAND;
25 static int cgroup_enable_systemd_services = CONFIG_ONDEMAND_YES;
26 static int cgroup_enable_systemd_services_detailed_memory = CONFIG_ONDEMAND_NO;
27 static int cgroup_used_memory_without_cache = CONFIG_ONDEMAND_YES;
29 static int cgroup_search_in_devices = 1;
31 static int cgroup_enable_new_cgroups_detected_at_runtime = 1;
32 static int cgroup_check_for_new_every = 10;
33 static int cgroup_update_every = 1;
35 static int cgroup_recheck_zero_blkio_every_iterations = 10;
36 static int cgroup_recheck_zero_mem_failcnt_every_iterations = 10;
37 static int cgroup_recheck_zero_mem_detailed_every_iterations = 10;
39 static char *cgroup_cpuacct_base = NULL;
40 static char *cgroup_blkio_base = NULL;
41 static char *cgroup_memory_base = NULL;
42 static char *cgroup_devices_base = NULL;
44 static int cgroup_root_count = 0;
45 static int cgroup_root_max = 500;
46 static int cgroup_max_depth = 0;
48 static SIMPLE_PATTERN *enabled_cgroup_patterns = NULL;
49 static SIMPLE_PATTERN *enabled_cgroup_paths = NULL;
50 static SIMPLE_PATTERN *enabled_cgroup_renames = NULL;
51 static SIMPLE_PATTERN *systemd_services_cgroups = NULL;
53 static char *cgroups_rename_script = PLUGINS_DIR "/cgroup-name.sh";
55 static uint32_t Read_hash = 0;
56 static uint32_t Write_hash = 0;
57 static uint32_t Sync_hash = 0;
58 static uint32_t Async_hash = 0;
59 static uint32_t Total_hash = 0;
60 static uint32_t user_hash = 0;
61 static uint32_t system_hash = 0;
62 static uint32_t cache_hash = 0;
63 static uint32_t rss_hash = 0;
64 static uint32_t rss_huge_hash = 0;
65 static uint32_t mapped_file_hash = 0;
66 static uint32_t writeback_hash = 0;
67 static uint32_t dirty_hash = 0;
68 static uint32_t swap_hash = 0;
69 static uint32_t pgpgin_hash = 0;
70 static uint32_t pgpgout_hash = 0;
71 static uint32_t pgfault_hash = 0;
72 static uint32_t pgmajfault_hash = 0;
73 static uint32_t inactive_anon_hash = 0;
74 static uint32_t active_anon_hash = 0;
75 static uint32_t inactive_file_hash = 0;
76 static uint32_t active_file_hash = 0;
77 static uint32_t unevictable_hash = 0;
78 static uint32_t hierarchical_memory_limit_hash = 0;
79 static uint32_t total_cache_hash = 0;
80 static uint32_t total_rss_hash = 0;
81 static uint32_t total_rss_huge_hash = 0;
82 static uint32_t total_mapped_file_hash = 0;
83 static uint32_t total_writeback_hash = 0;
84 static uint32_t total_dirty_hash = 0;
85 static uint32_t total_swap_hash = 0;
86 static uint32_t total_pgpgin_hash = 0;
87 static uint32_t total_pgpgout_hash = 0;
88 static uint32_t total_pgfault_hash = 0;
89 static uint32_t total_pgmajfault_hash = 0;
90 static uint32_t total_inactive_anon_hash = 0;
91 static uint32_t total_active_anon_hash = 0;
92 static uint32_t total_inactive_file_hash = 0;
93 static uint32_t total_active_file_hash = 0;
94 static uint32_t total_unevictable_hash = 0;
96 void read_cgroup_plugin_configuration() {
97 system_page_size = sysconf(_SC_PAGESIZE);
99 Read_hash = simple_hash("Read");
100 Write_hash = simple_hash("Write");
101 Sync_hash = simple_hash("Sync");
102 Async_hash = simple_hash("Async");
103 Total_hash = simple_hash("Total");
104 user_hash = simple_hash("user");
105 system_hash = simple_hash("system");
106 cache_hash = simple_hash("cache");
107 rss_hash = simple_hash("rss");
108 rss_huge_hash = simple_hash("rss_huge");
109 mapped_file_hash = simple_hash("mapped_file");
110 writeback_hash = simple_hash("writeback");
111 dirty_hash = simple_hash("dirty");
112 swap_hash = simple_hash("swap");
113 pgpgin_hash = simple_hash("pgpgin");
114 pgpgout_hash = simple_hash("pgpgout");
115 pgfault_hash = simple_hash("pgfault");
116 pgmajfault_hash = simple_hash("pgmajfault");
117 inactive_anon_hash = simple_hash("inactive_anon");
118 active_anon_hash = simple_hash("active_anon");
119 inactive_file_hash = simple_hash("inactive_file");
120 active_file_hash = simple_hash("active_file");
121 unevictable_hash = simple_hash("unevictable");
122 hierarchical_memory_limit_hash = simple_hash("hierarchical_memory_limit");
123 total_cache_hash = simple_hash("total_cache");
124 total_rss_hash = simple_hash("total_rss");
125 total_rss_huge_hash = simple_hash("total_rss_huge");
126 total_mapped_file_hash = simple_hash("total_mapped_file");
127 total_writeback_hash = simple_hash("total_writeback");
128 total_dirty_hash = simple_hash("total_dirty");
129 total_swap_hash = simple_hash("total_swap");
130 total_pgpgin_hash = simple_hash("total_pgpgin");
131 total_pgpgout_hash = simple_hash("total_pgpgout");
132 total_pgfault_hash = simple_hash("total_pgfault");
133 total_pgmajfault_hash = simple_hash("total_pgmajfault");
134 total_inactive_anon_hash = simple_hash("total_inactive_anon");
135 total_active_anon_hash = simple_hash("total_active_anon");
136 total_inactive_file_hash = simple_hash("total_inactive_file");
137 total_active_file_hash = simple_hash("total_active_file");
138 total_unevictable_hash = simple_hash("total_unevictable");
140 cgroup_update_every = (int)config_get_number("plugin:cgroups", "update every", rrd_update_every);
141 if(cgroup_update_every < rrd_update_every)
142 cgroup_update_every = rrd_update_every;
144 cgroup_check_for_new_every = (int)config_get_number("plugin:cgroups", "check for new cgroups every", cgroup_check_for_new_every * cgroup_update_every);
145 if(cgroup_check_for_new_every < cgroup_update_every)
146 cgroup_check_for_new_every = cgroup_update_every;
148 cgroup_enable_cpuacct_stat = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct stat (total CPU)", cgroup_enable_cpuacct_stat);
149 cgroup_enable_cpuacct_usage = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct usage (per core CPU)", cgroup_enable_cpuacct_usage);
151 cgroup_enable_memory = config_get_boolean_ondemand("plugin:cgroups", "enable memory (used mem including cache)", cgroup_enable_memory);
152 cgroup_enable_detailed_memory = config_get_boolean_ondemand("plugin:cgroups", "enable detailed memory", cgroup_enable_detailed_memory);
153 cgroup_enable_memory_failcnt = config_get_boolean_ondemand("plugin:cgroups", "enable memory limits fail count", cgroup_enable_memory_failcnt);
154 cgroup_enable_swap = config_get_boolean_ondemand("plugin:cgroups", "enable swap memory", cgroup_enable_swap);
156 cgroup_enable_blkio_io = config_get_boolean_ondemand("plugin:cgroups", "enable blkio bandwidth", cgroup_enable_blkio_io);
157 cgroup_enable_blkio_ops = config_get_boolean_ondemand("plugin:cgroups", "enable blkio operations", cgroup_enable_blkio_ops);
158 cgroup_enable_blkio_throttle_io = config_get_boolean_ondemand("plugin:cgroups", "enable blkio throttle bandwidth", cgroup_enable_blkio_throttle_io);
159 cgroup_enable_blkio_throttle_ops = config_get_boolean_ondemand("plugin:cgroups", "enable blkio throttle operations", cgroup_enable_blkio_throttle_ops);
160 cgroup_enable_blkio_queued_ops = config_get_boolean_ondemand("plugin:cgroups", "enable blkio queued operations", cgroup_enable_blkio_queued_ops);
161 cgroup_enable_blkio_merged_ops = config_get_boolean_ondemand("plugin:cgroups", "enable blkio merged operations", cgroup_enable_blkio_merged_ops);
163 cgroup_recheck_zero_blkio_every_iterations = (int)config_get_number("plugin:cgroups", "recheck zero blkio every iterations", cgroup_recheck_zero_blkio_every_iterations);
164 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);
165 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);
167 cgroup_enable_systemd_services = config_get_boolean("plugin:cgroups", "enable systemd services", cgroup_enable_systemd_services);
168 cgroup_enable_systemd_services_detailed_memory = config_get_boolean("plugin:cgroups", "enable systemd services detailed memory", cgroup_enable_systemd_services_detailed_memory);
169 cgroup_used_memory_without_cache = config_get_boolean("plugin:cgroups", "report used memory without cache", cgroup_used_memory_without_cache);
171 char filename[FILENAME_MAX + 1], *s;
172 struct mountinfo *mi, *root = mountinfo_read(0);
174 mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "cpuacct");
175 if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "cpuacct");
177 error("Cannot find cgroup cpuacct mountinfo. Assuming default: /sys/fs/cgroup/cpuacct");
178 s = "/sys/fs/cgroup/cpuacct";
180 else s = mi->mount_point;
181 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
182 cgroup_cpuacct_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/cpuacct", filename);
184 mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "blkio");
185 if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "blkio");
187 error("Cannot find cgroup blkio mountinfo. Assuming default: /sys/fs/cgroup/blkio");
188 s = "/sys/fs/cgroup/blkio";
190 else s = mi->mount_point;
191 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
192 cgroup_blkio_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/blkio", filename);
194 mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "memory");
195 if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "memory");
197 error("Cannot find cgroup memory mountinfo. Assuming default: /sys/fs/cgroup/memory");
198 s = "/sys/fs/cgroup/memory";
200 else s = mi->mount_point;
201 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
202 cgroup_memory_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/memory", filename);
204 mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "devices");
205 if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "devices");
207 error("Cannot find cgroup devices mountinfo. Assuming default: /sys/fs/cgroup/devices");
208 s = "/sys/fs/cgroup/devices";
210 else s = mi->mount_point;
211 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
212 cgroup_devices_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/devices", filename);
214 cgroup_root_max = (int)config_get_number("plugin:cgroups", "max cgroups to allow", cgroup_root_max);
215 cgroup_max_depth = (int)config_get_number("plugin:cgroups", "max cgroups depth to monitor", cgroup_max_depth);
217 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);
219 enabled_cgroup_patterns = simple_pattern_create(
220 config_get("plugin:cgroups", "enable by default cgroups matching",
232 " !/lxc/*/ns " // #1397
238 " * " // enable anything else
239 ), SIMPLE_PATTERN_EXACT);
241 enabled_cgroup_paths = simple_pattern_create(
242 config_get("plugin:cgroups", "search for cgroups in subpaths matching",
250 ), SIMPLE_PATTERN_EXACT);
252 cgroups_rename_script = config_get("plugin:cgroups", "script to get cgroup names", cgroups_rename_script);
254 enabled_cgroup_renames = simple_pattern_create(
255 config_get("plugin:cgroups", "run script to rename cgroups matching",
265 ), SIMPLE_PATTERN_EXACT);
267 if(cgroup_enable_systemd_services) {
268 systemd_services_cgroups = simple_pattern_create(
269 config_get("plugin:cgroups", "cgroups to match as systemd services",
270 " !/system.slice/*/*.service "
271 " /system.slice/*.service "
272 ), SIMPLE_PATTERN_EXACT);
275 mountinfo_free(root);
278 // ----------------------------------------------------------------------------
283 int enabled; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
288 unsigned long long Read;
289 unsigned long long Write;
291 unsigned long long Sync;
292 unsigned long long Async;
293 unsigned long long Total;
297 // https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt
300 ARL_ENTRY *arl_dirty;
303 int updated_detailed;
304 int updated_usage_in_bytes;
305 int updated_msw_usage_in_bytes;
308 int enabled_detailed; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
309 int enabled_usage_in_bytes; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
310 int enabled_msw_usage_in_bytes; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
311 int enabled_failcnt; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
313 int delay_counter_detailed;
314 int delay_counter_failcnt;
316 char *filename_detailed;
317 char *filename_usage_in_bytes;
318 char *filename_msw_usage_in_bytes;
319 char *filename_failcnt;
321 int detailed_has_dirty;
322 int detailed_has_swap;
325 unsigned long long cache;
326 unsigned long long rss;
327 unsigned long long rss_huge;
328 unsigned long long mapped_file;
329 unsigned long long writeback;
330 unsigned long long dirty;
331 unsigned long long swap;
332 unsigned long long pgpgin;
333 unsigned long long pgpgout;
334 unsigned long long pgfault;
335 unsigned long long pgmajfault;
337 unsigned long long inactive_anon;
338 unsigned long long active_anon;
339 unsigned long long inactive_file;
340 unsigned long long active_file;
341 unsigned long long unevictable;
342 unsigned long long hierarchical_memory_limit;
343 unsigned long long total_cache;
344 unsigned long long total_rss;
345 unsigned long long total_rss_huge;
346 unsigned long long total_mapped_file;
347 unsigned long long total_writeback;
348 unsigned long long total_dirty;
349 unsigned long long total_swap;
350 unsigned long long total_pgpgin;
351 unsigned long long total_pgpgout;
352 unsigned long long total_pgfault;
353 unsigned long long total_pgmajfault;
354 unsigned long long total_inactive_anon;
355 unsigned long long total_active_anon;
356 unsigned long long total_inactive_file;
357 unsigned long long total_active_file;
358 unsigned long long total_unevictable;
361 // single file metrics
362 unsigned long long usage_in_bytes;
363 unsigned long long msw_usage_in_bytes;
364 unsigned long long failcnt;
367 // https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt
368 struct cpuacct_stat {
370 int enabled; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
374 unsigned long long user;
375 unsigned long long system;
378 // https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt
379 struct cpuacct_usage {
381 int enabled; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
386 unsigned long long *cpu_percpu;
389 #define CGROUP_OPTIONS_DISABLED_DUPLICATE 0x00000001
390 #define CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE 0x00000002
395 char available; // found in the filesystem
396 char enabled; // enabled in the config
406 struct cpuacct_stat cpuacct_stat;
407 struct cpuacct_usage cpuacct_usage;
409 struct memory memory;
411 struct blkio io_service_bytes; // bytes
412 struct blkio io_serviced; // operations
414 struct blkio throttle_io_service_bytes; // bytes
415 struct blkio throttle_io_serviced; // operations
417 struct blkio io_merged; // operations
418 struct blkio io_queued; // operations
422 RRDSET *st_cpu_per_core;
424 RRDSET *st_writeback;
425 RRDSET *st_mem_activity;
427 RRDSET *st_mem_usage;
428 RRDSET *st_mem_failcnt;
430 RRDSET *st_serviced_ops;
431 RRDSET *st_throttle_io;
432 RRDSET *st_throttle_serviced_ops;
433 RRDSET *st_queued_ops;
434 RRDSET *st_merged_ops;
438 RRDDIM *rd_mem_usage;
439 RRDDIM *rd_mem_failcnt;
440 RRDDIM *rd_swap_usage;
442 RRDDIM *rd_mem_detailed_cache;
443 RRDDIM *rd_mem_detailed_rss;
444 RRDDIM *rd_mem_detailed_mapped;
445 RRDDIM *rd_mem_detailed_writeback;
446 RRDDIM *rd_mem_detailed_dirty;
447 RRDDIM *rd_mem_detailed_swap;
448 RRDDIM *rd_mem_detailed_pgpgin;
449 RRDDIM *rd_mem_detailed_pgpgout;
450 RRDDIM *rd_mem_detailed_pgfault;
451 RRDDIM *rd_mem_detailed_pgmajfault;
453 RRDDIM *rd_io_service_bytes_read;
454 RRDDIM *rd_io_serviced_read;
455 RRDDIM *rd_throttle_io_read;
456 RRDDIM *rd_throttle_io_serviced_read;
457 RRDDIM *rd_io_queued_read;
458 RRDDIM *rd_io_merged_read;
460 RRDDIM *rd_io_service_bytes_write;
461 RRDDIM *rd_io_serviced_write;
462 RRDDIM *rd_throttle_io_write;
463 RRDDIM *rd_throttle_io_serviced_write;
464 RRDDIM *rd_io_queued_write;
465 RRDDIM *rd_io_merged_write;
469 } *cgroup_root = NULL;
471 // ----------------------------------------------------------------------------
472 // read values from /sys
474 static inline void cgroup_read_cpuacct_stat(struct cpuacct_stat *cp) {
475 static procfile *ff = NULL;
477 if(likely(cp->filename)) {
478 ff = procfile_reopen(ff, cp->filename, NULL, PROCFILE_FLAG_DEFAULT);
484 ff = procfile_readall(ff);
490 unsigned long i, lines = procfile_lines(ff);
492 if(unlikely(lines < 1)) {
493 error("File '%s' should have 1+ lines.", cp->filename);
498 for(i = 0; i < lines ; i++) {
499 char *s = procfile_lineword(ff, i, 0);
500 uint32_t hash = simple_hash(s);
502 if(unlikely(hash == user_hash && !strcmp(s, "user")))
503 cp->user = str2ull(procfile_lineword(ff, i, 1));
505 else if(unlikely(hash == system_hash && !strcmp(s, "system")))
506 cp->system = str2ull(procfile_lineword(ff, i, 1));
511 if(unlikely(cp->enabled == CONFIG_ONDEMAND_ONDEMAND && (cp->user || cp->system)))
512 cp->enabled = CONFIG_ONDEMAND_YES;
516 static inline void cgroup_read_cpuacct_usage(struct cpuacct_usage *ca) {
517 static procfile *ff = NULL;
519 if(likely(ca->filename)) {
520 ff = procfile_reopen(ff, ca->filename, NULL, PROCFILE_FLAG_DEFAULT);
526 ff = procfile_readall(ff);
532 if(unlikely(procfile_lines(ff) < 1)) {
533 error("File '%s' should have 1+ lines but has %u.", ca->filename, procfile_lines(ff));
538 unsigned long i = procfile_linewords(ff, 0);
539 if(unlikely(i == 0)) {
544 // we may have 1 more CPU reported
546 char *s = procfile_lineword(ff, 0, i - 1);
551 if(unlikely(i != ca->cpus)) {
552 freez(ca->cpu_percpu);
553 ca->cpu_percpu = mallocz(sizeof(unsigned long long) * i);
554 ca->cpus = (unsigned int)i;
557 unsigned long long total = 0;
558 for(i = 0; i < ca->cpus ;i++) {
559 unsigned long long n = str2ull(procfile_lineword(ff, 0, i));
560 ca->cpu_percpu[i] = n;
566 if(unlikely(ca->enabled == CONFIG_ONDEMAND_ONDEMAND && total))
567 ca->enabled = CONFIG_ONDEMAND_YES;
571 static inline void cgroup_read_blkio(struct blkio *io) {
572 static procfile *ff = NULL;
574 if(unlikely(io->enabled == CONFIG_ONDEMAND_ONDEMAND && io->delay_counter > 0)) {
579 if(likely(io->filename)) {
580 ff = procfile_reopen(ff, io->filename, NULL, PROCFILE_FLAG_DEFAULT);
586 ff = procfile_readall(ff);
592 unsigned long i, lines = procfile_lines(ff);
594 if(unlikely(lines < 1)) {
595 error("File '%s' should have 1+ lines.", io->filename);
608 for(i = 0; i < lines ; i++) {
609 char *s = procfile_lineword(ff, i, 1);
610 uint32_t hash = simple_hash(s);
612 if(unlikely(hash == Read_hash && !strcmp(s, "Read")))
613 io->Read += str2ull(procfile_lineword(ff, i, 2));
615 else if(unlikely(hash == Write_hash && !strcmp(s, "Write")))
616 io->Write += str2ull(procfile_lineword(ff, i, 2));
619 else if(unlikely(hash == Sync_hash && !strcmp(s, "Sync")))
620 io->Sync += str2ull(procfile_lineword(ff, i, 2));
622 else if(unlikely(hash == Async_hash && !strcmp(s, "Async")))
623 io->Async += str2ull(procfile_lineword(ff, i, 2));
625 else if(unlikely(hash == Total_hash && !strcmp(s, "Total")))
626 io->Total += str2ull(procfile_lineword(ff, i, 2));
632 if(unlikely(io->enabled == CONFIG_ONDEMAND_ONDEMAND)) {
633 if(unlikely(io->Read || io->Write))
634 io->enabled = CONFIG_ONDEMAND_YES;
636 io->delay_counter = cgroup_recheck_zero_blkio_every_iterations;
641 static inline void cgroup_read_memory(struct memory *mem) {
642 static procfile *ff = NULL;
644 // read detailed ram usage
645 if(likely(mem->filename_detailed)) {
646 if(unlikely(mem->enabled_detailed == CONFIG_ONDEMAND_ONDEMAND && mem->delay_counter_detailed > 0)) {
647 mem->delay_counter_detailed--;
651 ff = procfile_reopen(ff, mem->filename_detailed, NULL, PROCFILE_FLAG_DEFAULT);
653 mem->updated_detailed = 0;
657 ff = procfile_readall(ff);
659 mem->updated_detailed = 0;
663 unsigned long i, lines = procfile_lines(ff);
665 if(unlikely(lines < 1)) {
666 error("File '%s' should have 1+ lines.", mem->filename_detailed);
667 mem->updated_detailed = 0;
671 if(unlikely(!mem->arl_base)) {
672 mem->arl_base = arl_create(NULL, 60);
674 arl_expect(mem->arl_base, "cache", &mem->cache);
675 arl_expect(mem->arl_base, "rss", &mem->rss);
676 arl_expect(mem->arl_base, "rss_huge", &mem->rss_huge);
677 arl_expect(mem->arl_base, "mapped_file", &mem->mapped_file);
678 arl_expect(mem->arl_base, "writeback", &mem->writeback);
679 mem->arl_dirty = arl_expect(mem->arl_base, "dirty", &mem->dirty);
680 mem->arl_swap = arl_expect(mem->arl_base, "swap", &mem->swap);
681 arl_expect(mem->arl_base, "pgpgin", &mem->pgpgin);
682 arl_expect(mem->arl_base, "pgpgout", &mem->pgpgout);
683 arl_expect(mem->arl_base, "pgfault", &mem->pgfault);
684 arl_expect(mem->arl_base, "pgmajfault", &mem->pgmajfault);
687 arl_begin(mem->arl_base);
689 for(i = 0; i < lines ; i++) {
690 if(arl_check(mem->arl_base,
691 procfile_lineword(ff, i, 0),
692 procfile_lineword(ff, i, 1))) break;
695 if(unlikely(mem->arl_dirty->flags & ARL_ENTRY_FLAG_FOUND))
696 mem->detailed_has_dirty = 1;
698 if(unlikely(mem->arl_swap->flags & ARL_ENTRY_FLAG_FOUND))
699 mem->detailed_has_swap = 1;
701 // 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);
703 mem->updated_detailed = 1;
705 if(unlikely(mem->enabled_detailed == CONFIG_ONDEMAND_ONDEMAND)) {
706 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)
707 mem->enabled_detailed = CONFIG_ONDEMAND_YES;
709 mem->delay_counter_detailed = cgroup_recheck_zero_mem_detailed_every_iterations;
715 // read usage_in_bytes
716 if(likely(mem->filename_usage_in_bytes)) {
717 mem->updated_usage_in_bytes = !read_single_number_file(mem->filename_usage_in_bytes, &mem->usage_in_bytes);
718 if(unlikely(mem->updated_usage_in_bytes && mem->enabled_usage_in_bytes == CONFIG_ONDEMAND_ONDEMAND && mem->usage_in_bytes))
719 mem->enabled_usage_in_bytes = CONFIG_ONDEMAND_YES;
722 // read msw_usage_in_bytes
723 if(likely(mem->filename_msw_usage_in_bytes)) {
724 mem->updated_msw_usage_in_bytes = !read_single_number_file(mem->filename_msw_usage_in_bytes, &mem->msw_usage_in_bytes);
725 if(unlikely(mem->updated_msw_usage_in_bytes && mem->enabled_msw_usage_in_bytes == CONFIG_ONDEMAND_ONDEMAND && mem->msw_usage_in_bytes))
726 mem->enabled_msw_usage_in_bytes = CONFIG_ONDEMAND_YES;
730 if(likely(mem->filename_failcnt)) {
731 if(unlikely(mem->enabled_failcnt == CONFIG_ONDEMAND_ONDEMAND && mem->delay_counter_failcnt > 0)) {
732 mem->updated_failcnt = 0;
733 mem->delay_counter_failcnt--;
736 mem->updated_failcnt = !read_single_number_file(mem->filename_failcnt, &mem->failcnt);
737 if(unlikely(mem->updated_failcnt && mem->enabled_failcnt == CONFIG_ONDEMAND_ONDEMAND)) {
738 if(unlikely(!mem->failcnt))
739 mem->delay_counter_failcnt = cgroup_recheck_zero_mem_failcnt_every_iterations;
741 mem->enabled_failcnt = CONFIG_ONDEMAND_YES;
747 static inline void cgroup_read(struct cgroup *cg) {
748 debug(D_CGROUP, "reading metrics for cgroups '%s'", cg->id);
750 cgroup_read_cpuacct_stat(&cg->cpuacct_stat);
751 cgroup_read_cpuacct_usage(&cg->cpuacct_usage);
752 cgroup_read_memory(&cg->memory);
753 cgroup_read_blkio(&cg->io_service_bytes);
754 cgroup_read_blkio(&cg->io_serviced);
755 cgroup_read_blkio(&cg->throttle_io_service_bytes);
756 cgroup_read_blkio(&cg->throttle_io_serviced);
757 cgroup_read_blkio(&cg->io_merged);
758 cgroup_read_blkio(&cg->io_queued);
761 static inline void read_all_cgroups(struct cgroup *root) {
762 debug(D_CGROUP, "reading metrics for all cgroups");
766 for(cg = root; cg ; cg = cg->next)
767 if(cg->enabled && cg->available)
771 // ----------------------------------------------------------------------------
772 // add/remove/find cgroup objects
774 #define CGROUP_CHARTID_LINE_MAX 1024
776 static inline char *cgroup_title_strdupz(const char *s) {
777 if(!s || !*s) s = "/";
779 if(*s == '/' && s[1] != '\0') s++;
781 char *r = strdupz(s);
782 netdata_fix_chart_name(r);
787 static inline char *cgroup_chart_id_strdupz(const char *s) {
788 if(!s || !*s) s = "/";
790 if(*s == '/' && s[1] != '\0') s++;
792 char *r = strdupz(s);
793 netdata_fix_chart_id(r);
798 static inline void cgroup_get_chart_name(struct cgroup *cg) {
799 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);
802 char buffer[CGROUP_CHARTID_LINE_MAX + 1];
804 snprintfz(buffer, CGROUP_CHARTID_LINE_MAX, "exec %s '%s'", cgroups_rename_script, cg->chart_id);
806 debug(D_CGROUP, "executing command '%s' for cgroup '%s'", buffer, cg->id);
807 FILE *fp = mypopen(buffer, &cgroup_pid);
809 // debug(D_CGROUP, "reading from command '%s' for cgroup '%s'", buffer, cg->id);
810 char *s = fgets(buffer, CGROUP_CHARTID_LINE_MAX, fp);
811 // debug(D_CGROUP, "closing command for cgroup '%s'", cg->id);
812 mypclose(fp, cgroup_pid);
813 // debug(D_CGROUP, "closed command for cgroup '%s'", cg->id);
815 if(s && *s && *s != '\n') {
816 debug(D_CGROUP, "cgroup '%s' should be renamed to '%s'", cg->id, s);
820 freez(cg->chart_title);
821 cg->chart_title = cgroup_title_strdupz(s);
824 cg->chart_id = cgroup_chart_id_strdupz(s);
825 cg->hash_chart = simple_hash(cg->chart_id);
829 error("CGROUP: Cannot popen(\"%s\", \"r\").", buffer);
832 static inline struct cgroup *cgroup_add(const char *id) {
833 if(!id || !*id) id = "/";
834 debug(D_CGROUP, "adding to list, cgroup with id '%s'", id);
836 if(cgroup_root_count >= cgroup_root_max) {
837 info("Maximum number of cgroups reached (%d). Not adding cgroup '%s'", cgroup_root_count, id);
841 int def = simple_pattern_matches(enabled_cgroup_patterns, id)?cgroup_enable_new_cgroups_detected_at_runtime:0;
842 struct cgroup *cg = callocz(1, sizeof(struct cgroup));
844 cg->id = strdupz(id);
845 cg->hash = simple_hash(cg->id);
847 cg->chart_title = cgroup_title_strdupz(id);
849 cg->chart_id = cgroup_chart_id_strdupz(id);
850 cg->hash_chart = simple_hash(cg->chart_id);
857 for(e = cgroup_root; e->next ;e = e->next) ;
863 // fix the chart_id and title by calling the external script
864 if(simple_pattern_matches(enabled_cgroup_renames, cg->id)) {
866 cgroup_get_chart_name(cg);
868 debug(D_CGROUP, "cgroup '%s' renamed to '%s' (title: '%s')", cg->id, cg->chart_id, cg->chart_title);
871 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);
873 int user_configurable = 1;
875 // check if this cgroup should be a systemd service
876 if(cgroup_enable_systemd_services) {
877 if(simple_pattern_matches(systemd_services_cgroups, cg->id) ||
878 simple_pattern_matches(systemd_services_cgroups, cg->chart_id)) {
879 debug(D_CGROUP, "cgroup '%s' with chart id '%s' (title: '%s') matches systemd services cgroups", cg->id, cg->chart_id, cg->chart_title);
881 char buffer[CGROUP_CHARTID_LINE_MAX + 1];
882 cg->options |= CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE;
884 strncpy(buffer, cg->id, CGROUP_CHARTID_LINE_MAX);
887 //freez(cg->chart_id);
888 //cg->chart_id = cgroup_chart_id_strdupz(s);
889 //cg->hash_chart = simple_hash(cg->chart_id);
891 // skip to the last slash
892 size_t len = strlen(s);
893 while(len--) if(unlikely(s[len] == '/')) break;
894 if(len) s = &s[len + 1];
898 while(len--) if(unlikely(s[len] == '.')) break;
899 if(len) s[len] = '\0';
901 freez(cg->chart_title);
902 cg->chart_title = cgroup_title_strdupz(s);
905 user_configurable = 0;
907 debug(D_CGROUP, "cgroup '%s' renamed to '%s' (title: '%s')", cg->id, cg->chart_id, cg->chart_title);
910 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);
913 if(user_configurable) {
914 // allow the user to enable/disable this individualy
915 char option[FILENAME_MAX + 1];
916 snprintfz(option, FILENAME_MAX, "enable cgroup %s", cg->chart_title);
917 cg->enabled = (char) config_get_boolean("plugin:cgroups", option, def);
920 // detect duplicate cgroups
923 for (t = cgroup_root; t; t = t->next) {
924 if (t != cg && t->enabled && t->hash_chart == cg->hash_chart && !strcmp(t->chart_id, cg->chart_id)) {
925 if (!strncmp(t->chart_id, "/system.slice/", 14) && !strncmp(cg->chart_id, "/init.scope/system.slice/", 25)) {
926 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'.",
927 cg->chart_id, t->id, cg->id, t->id);
928 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'.",
929 cg->chart_id, t->id, cg->id, t->id);
931 t->options |= CGROUP_OPTIONS_DISABLED_DUPLICATE;
934 error("Control group with chart id '%s' already exists with id '%s' and is enabled and available. Disabling cgroup with id '%s'.",
935 cg->chart_id, t->id, cg->id);
936 debug(D_CGROUP, "Control group with chart id '%s' already exists with id '%s' and is enabled and available. Disabling cgroup with id '%s'.",
937 cg->chart_id, t->id, cg->id);
939 cg->options |= CGROUP_OPTIONS_DISABLED_DUPLICATE;
947 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");
952 static inline void cgroup_free(struct cgroup *cg) {
953 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");
955 freez(cg->cpuacct_usage.cpu_percpu);
957 freez(cg->cpuacct_stat.filename);
958 freez(cg->cpuacct_usage.filename);
960 arl_free(cg->memory.arl_base);
961 freez(cg->memory.filename_detailed);
962 freez(cg->memory.filename_failcnt);
963 freez(cg->memory.filename_usage_in_bytes);
964 freez(cg->memory.filename_msw_usage_in_bytes);
966 freez(cg->io_service_bytes.filename);
967 freez(cg->io_serviced.filename);
969 freez(cg->throttle_io_service_bytes.filename);
970 freez(cg->throttle_io_serviced.filename);
972 freez(cg->io_merged.filename);
973 freez(cg->io_queued.filename);
977 freez(cg->chart_title);
984 // find if a given cgroup exists
985 static inline struct cgroup *cgroup_find(const char *id) {
986 debug(D_CGROUP, "searching for cgroup '%s'", id);
988 uint32_t hash = simple_hash(id);
991 for(cg = cgroup_root; cg ; cg = cg->next) {
992 if(hash == cg->hash && strcmp(id, cg->id) == 0)
996 debug(D_CGROUP, "cgroup '%s' %s in memory", id, (cg)?"found":"not found");
1000 // ----------------------------------------------------------------------------
1001 // detect running cgroups
1003 // callback for find_file_in_subdirs()
1004 static inline void found_subdir_in_dir(const char *dir) {
1005 debug(D_CGROUP, "examining cgroup dir '%s'", dir);
1007 struct cgroup *cg = cgroup_find(dir);
1009 if(*dir && cgroup_max_depth > 0) {
1013 for(s = dir; *s ;s++)
1014 if(unlikely(*s == '/'))
1017 if(depth > cgroup_max_depth) {
1018 info("cgroup '%s' is too deep (%d, while max is %d)", dir, depth, cgroup_max_depth);
1022 // debug(D_CGROUP, "will add dir '%s' as cgroup", dir);
1023 cg = cgroup_add(dir);
1026 if(cg) cg->available = 1;
1029 static inline int find_dir_in_subdirs(const char *base, const char *this, void (*callback)(const char *)) {
1030 if(!this) this = base;
1031 debug(D_CGROUP, "searching for directories in '%s' (base '%s')", this?this:"", base);
1033 size_t dirlen = strlen(this), baselen = strlen(base);
1038 const char *relative_path = &this[baselen];
1039 if(!*relative_path) relative_path = "/";
1041 DIR *dir = opendir(this);
1043 error("Cannot read cgroups directory '%s'", base);
1048 callback(relative_path);
1050 struct dirent *de = NULL;
1051 while((de = readdir(dir))) {
1052 if(de->d_type == DT_DIR
1054 (de->d_name[0] == '.' && de->d_name[1] == '\0')
1055 || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
1059 if(de->d_type == DT_DIR) {
1061 const char *r = relative_path;
1062 if(*r == '\0') r = "/";
1064 // do not decent in directories we are not interested
1065 int def = simple_pattern_matches(enabled_cgroup_paths, r);
1067 // we check for this option here
1068 // so that the config will not have settings
1069 // for leaf directories
1070 char option[FILENAME_MAX + 1];
1071 snprintfz(option, FILENAME_MAX, "search for cgroups under %s", r);
1072 option[FILENAME_MAX] = '\0';
1073 enabled = config_get_boolean("plugin:cgroups", option, def);
1077 char *s = mallocz(dirlen + strlen(de->d_name) + 2);
1080 strcat(s, de->d_name);
1081 int ret2 = find_dir_in_subdirs(base, s, callback);
1082 if(ret2 > 0) ret += ret2;
1092 static inline void mark_all_cgroups_as_not_available() {
1093 debug(D_CGROUP, "marking all cgroups as not available");
1097 // mark all as not available
1098 for(cg = cgroup_root; cg ; cg = cg->next) {
1103 static inline void cleanup_all_cgroups() {
1104 struct cgroup *cg = cgroup_root, *last = NULL;
1107 if(!cg->available) {
1108 // enable the first duplicate cgroup
1111 for(t = cgroup_root; t ; t = t->next) {
1112 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)) {
1113 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);
1115 t->options &= ~CGROUP_OPTIONS_DISABLED_DUPLICATE;
1122 cgroup_root = cg->next;
1124 last->next = cg->next;
1140 static inline void find_all_cgroups() {
1141 debug(D_CGROUP, "searching for cgroups");
1143 mark_all_cgroups_as_not_available();
1145 if(cgroup_enable_cpuacct_stat || cgroup_enable_cpuacct_usage) {
1146 if(find_dir_in_subdirs(cgroup_cpuacct_base, NULL, found_subdir_in_dir) == -1) {
1147 cgroup_enable_cpuacct_stat =
1148 cgroup_enable_cpuacct_usage = CONFIG_ONDEMAND_NO;
1149 error("disabled CGROUP cpu statistics.");
1153 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) {
1154 if(find_dir_in_subdirs(cgroup_blkio_base, NULL, found_subdir_in_dir) == -1) {
1155 cgroup_enable_blkio_io =
1156 cgroup_enable_blkio_ops =
1157 cgroup_enable_blkio_throttle_io =
1158 cgroup_enable_blkio_throttle_ops =
1159 cgroup_enable_blkio_merged_ops =
1160 cgroup_enable_blkio_queued_ops = CONFIG_ONDEMAND_NO;
1161 error("disabled CGROUP blkio statistics.");
1165 if(cgroup_enable_memory || cgroup_enable_detailed_memory || cgroup_enable_swap || cgroup_enable_memory_failcnt) {
1166 if(find_dir_in_subdirs(cgroup_memory_base, NULL, found_subdir_in_dir) == -1) {
1167 cgroup_enable_memory =
1168 cgroup_enable_detailed_memory =
1169 cgroup_enable_swap =
1170 cgroup_enable_memory_failcnt = CONFIG_ONDEMAND_NO;
1171 error("disabled CGROUP memory statistics.");
1175 if(cgroup_search_in_devices) {
1176 if(find_dir_in_subdirs(cgroup_devices_base, NULL, found_subdir_in_dir) == -1) {
1177 cgroup_search_in_devices = 0;
1178 error("disabled CGROUP devices statistics.");
1182 // remove any non-existing cgroups
1183 cleanup_all_cgroups();
1187 for(cg = cgroup_root; cg ; cg = cg->next) {
1188 // fprintf(stderr, " >>> CGROUP '%s' (%u - %s) with name '%s'\n", cg->id, cg->hash, cg->available?"available":"stopped", cg->name);
1190 if(unlikely(!cg->available))
1193 debug(D_CGROUP, "checking paths for cgroup '%s'", cg->id);
1195 // check for newly added cgroups
1196 // and update the filenames they read
1197 char filename[FILENAME_MAX + 1];
1198 if(unlikely(cgroup_enable_cpuacct_stat && !cg->cpuacct_stat.filename)) {
1199 snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.stat", cgroup_cpuacct_base, cg->id);
1200 if(likely(stat(filename, &buf) != -1)) {
1201 cg->cpuacct_stat.filename = strdupz(filename);
1202 cg->cpuacct_stat.enabled = cgroup_enable_cpuacct_stat;
1203 debug(D_CGROUP, "cpuacct.stat filename for cgroup '%s': '%s'", cg->id, cg->cpuacct_stat.filename);
1206 debug(D_CGROUP, "cpuacct.stat file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1209 if(unlikely(cgroup_enable_cpuacct_usage && !cg->cpuacct_usage.filename && !(cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE))) {
1210 snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.usage_percpu", cgroup_cpuacct_base, cg->id);
1211 if(likely(stat(filename, &buf) != -1)) {
1212 cg->cpuacct_usage.filename = strdupz(filename);
1213 cg->cpuacct_usage.enabled = cgroup_enable_cpuacct_usage;
1214 debug(D_CGROUP, "cpuacct.usage_percpu filename for cgroup '%s': '%s'", cg->id, cg->cpuacct_usage.filename);
1217 debug(D_CGROUP, "cpuacct.usage_percpu file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1220 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)))) {
1221 snprintfz(filename, FILENAME_MAX, "%s%s/memory.stat", cgroup_memory_base, cg->id);
1222 if(likely(stat(filename, &buf) != -1)) {
1223 cg->memory.filename_detailed = strdupz(filename);
1224 cg->memory.enabled_detailed = (cgroup_enable_detailed_memory == CONFIG_ONDEMAND_YES)?CONFIG_ONDEMAND_YES:CONFIG_ONDEMAND_ONDEMAND;
1225 debug(D_CGROUP, "memory.stat filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_detailed);
1228 debug(D_CGROUP, "memory.stat file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1231 if(unlikely(cgroup_enable_memory && !cg->memory.filename_usage_in_bytes)) {
1232 snprintfz(filename, FILENAME_MAX, "%s%s/memory.usage_in_bytes", cgroup_memory_base, cg->id);
1233 if(likely(stat(filename, &buf) != -1)) {
1234 cg->memory.filename_usage_in_bytes = strdupz(filename);
1235 cg->memory.enabled_usage_in_bytes = cgroup_enable_memory;
1236 debug(D_CGROUP, "memory.usage_in_bytes filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_usage_in_bytes);
1239 debug(D_CGROUP, "memory.usage_in_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1242 if(unlikely(cgroup_enable_swap && !cg->memory.filename_msw_usage_in_bytes)) {
1243 snprintfz(filename, FILENAME_MAX, "%s%s/memory.msw_usage_in_bytes", cgroup_memory_base, cg->id);
1244 if(likely(stat(filename, &buf) != -1)) {
1245 cg->memory.filename_msw_usage_in_bytes = strdupz(filename);
1246 cg->memory.enabled_msw_usage_in_bytes = cgroup_enable_swap;
1247 debug(D_CGROUP, "memory.msw_usage_in_bytes filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_msw_usage_in_bytes);
1250 debug(D_CGROUP, "memory.msw_usage_in_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1253 if(unlikely(cgroup_enable_memory_failcnt && !cg->memory.filename_failcnt)) {
1254 snprintfz(filename, FILENAME_MAX, "%s%s/memory.failcnt", cgroup_memory_base, cg->id);
1255 if(likely(stat(filename, &buf) != -1)) {
1256 cg->memory.filename_failcnt = strdupz(filename);
1257 cg->memory.enabled_failcnt = cgroup_enable_memory_failcnt;
1258 debug(D_CGROUP, "memory.failcnt filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_failcnt);
1261 debug(D_CGROUP, "memory.failcnt file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1264 if(unlikely(cgroup_enable_blkio_io && !cg->io_service_bytes.filename)) {
1265 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_service_bytes", cgroup_blkio_base, cg->id);
1266 if(likely(stat(filename, &buf) != -1)) {
1267 cg->io_service_bytes.filename = strdupz(filename);
1268 cg->io_service_bytes.enabled = cgroup_enable_blkio_io;
1269 debug(D_CGROUP, "io_service_bytes filename for cgroup '%s': '%s'", cg->id, cg->io_service_bytes.filename);
1272 debug(D_CGROUP, "io_service_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1275 if(unlikely(cgroup_enable_blkio_ops && !cg->io_serviced.filename)) {
1276 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_serviced", cgroup_blkio_base, cg->id);
1277 if(likely(stat(filename, &buf) != -1)) {
1278 cg->io_serviced.filename = strdupz(filename);
1279 cg->io_serviced.enabled = cgroup_enable_blkio_ops;
1280 debug(D_CGROUP, "io_serviced filename for cgroup '%s': '%s'", cg->id, cg->io_serviced.filename);
1283 debug(D_CGROUP, "io_serviced file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1286 if(unlikely(cgroup_enable_blkio_throttle_io && !cg->throttle_io_service_bytes.filename)) {
1287 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.throttle.io_service_bytes", cgroup_blkio_base, cg->id);
1288 if(likely(stat(filename, &buf) != -1)) {
1289 cg->throttle_io_service_bytes.filename = strdupz(filename);
1290 cg->throttle_io_service_bytes.enabled = cgroup_enable_blkio_throttle_io;
1291 debug(D_CGROUP, "throttle_io_service_bytes filename for cgroup '%s': '%s'", cg->id, cg->throttle_io_service_bytes.filename);
1294 debug(D_CGROUP, "throttle_io_service_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1297 if(unlikely(cgroup_enable_blkio_throttle_ops && !cg->throttle_io_serviced.filename)) {
1298 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.throttle.io_serviced", cgroup_blkio_base, cg->id);
1299 if(likely(stat(filename, &buf) != -1)) {
1300 cg->throttle_io_serviced.filename = strdupz(filename);
1301 cg->throttle_io_serviced.enabled = cgroup_enable_blkio_throttle_ops;
1302 debug(D_CGROUP, "throttle_io_serviced filename for cgroup '%s': '%s'", cg->id, cg->throttle_io_serviced.filename);
1305 debug(D_CGROUP, "throttle_io_serviced file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1308 if(unlikely(cgroup_enable_blkio_merged_ops && !cg->io_merged.filename)) {
1309 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_merged", cgroup_blkio_base, cg->id);
1310 if(likely(stat(filename, &buf) != -1)) {
1311 cg->io_merged.filename = strdupz(filename);
1312 cg->io_merged.enabled = cgroup_enable_blkio_merged_ops;
1313 debug(D_CGROUP, "io_merged filename for cgroup '%s': '%s'", cg->id, cg->io_merged.filename);
1316 debug(D_CGROUP, "io_merged file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1319 if(unlikely(cgroup_enable_blkio_queued_ops && !cg->io_queued.filename)) {
1320 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_queued", cgroup_blkio_base, cg->id);
1321 if(likely(stat(filename, &buf) != -1)) {
1322 cg->io_queued.filename = strdupz(filename);
1323 cg->io_queued.enabled = cgroup_enable_blkio_queued_ops;
1324 debug(D_CGROUP, "io_queued filename for cgroup '%s': '%s'", cg->id, cg->io_queued.filename);
1327 debug(D_CGROUP, "io_queued file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1331 debug(D_CGROUP, "done searching for cgroups");
1335 // ----------------------------------------------------------------------------
1338 #define CHART_TITLE_MAX 300
1340 void update_services_charts(int update_every,
1343 int do_mem_detailed,
1349 int do_throttle_ops,
1355 *st_mem_usage = NULL,
1356 *st_mem_failcnt = NULL,
1357 *st_swap_usage = NULL,
1359 *st_mem_detailed_cache = NULL,
1360 *st_mem_detailed_rss = NULL,
1361 *st_mem_detailed_mapped = NULL,
1362 *st_mem_detailed_writeback = NULL,
1363 *st_mem_detailed_pgfault = NULL,
1364 *st_mem_detailed_pgmajfault = NULL,
1365 *st_mem_detailed_pgpgin = NULL,
1366 *st_mem_detailed_pgpgout = NULL,
1369 *st_io_serviced_read = NULL,
1370 *st_throttle_io_read = NULL,
1371 *st_throttle_ops_read = NULL,
1372 *st_queued_ops_read = NULL,
1373 *st_merged_ops_read = NULL,
1375 *st_io_write = NULL,
1376 *st_io_serviced_write = NULL,
1377 *st_throttle_io_write = NULL,
1378 *st_throttle_ops_write = NULL,
1379 *st_queued_ops_write = NULL,
1380 *st_merged_ops_write = NULL;
1382 // create the charts
1384 if(likely(do_cpu)) {
1385 if(unlikely(!st_cpu)) {
1386 char title[CHART_TITLE_MAX + 1];
1388 st_cpu = rrdset_find_bytype("services", "cpu");
1389 if(likely(!st_cpu)) {
1390 snprintfz(title, CHART_TITLE_MAX, "Systemd Services CPU utilization (%d%% = %d core%s)", (processors * 100), processors, (processors > 1) ? "s" : "");
1391 st_cpu = rrdset_create("services", "cpu", NULL, "cpu", "services.cpu", title, "%", CHART_PRIORITY_SYSTEMD_SERVICES, update_every, RRDSET_TYPE_STACKED);
1395 rrdset_next(st_cpu);
1398 if(likely(do_mem_usage)) {
1399 if(unlikely(!st_mem_usage)) {
1400 st_mem_usage = rrdset_find_bytype("services", "mem_usage");
1401 if(likely(!st_mem_usage))
1402 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);
1405 rrdset_next(st_mem_usage);
1408 if(likely(do_mem_detailed)) {
1409 if(unlikely(!st_mem_detailed_rss)) {
1410 st_mem_detailed_rss = rrdset_find_bytype("services", "mem_rss");
1411 if(likely(!st_mem_detailed_rss))
1412 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);
1415 rrdset_next(st_mem_detailed_rss);
1417 if(unlikely(!st_mem_detailed_mapped)) {
1418 st_mem_detailed_mapped = rrdset_find_bytype("services", "mem_mapped");
1419 if(likely(!st_mem_detailed_mapped))
1420 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);
1423 rrdset_next(st_mem_detailed_mapped);
1425 if(unlikely(!st_mem_detailed_cache)) {
1426 st_mem_detailed_cache = rrdset_find_bytype("services", "mem_cache");
1427 if(likely(!st_mem_detailed_cache))
1428 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);
1431 rrdset_next(st_mem_detailed_cache);
1433 if(unlikely(!st_mem_detailed_writeback)) {
1434 st_mem_detailed_writeback = rrdset_find_bytype("services", "mem_writeback");
1435 if(likely(!st_mem_detailed_writeback))
1436 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);
1439 rrdset_next(st_mem_detailed_writeback);
1441 if(unlikely(!st_mem_detailed_pgfault)) {
1442 st_mem_detailed_pgfault = rrdset_find_bytype("services", "mem_pgfault");
1443 if(likely(!st_mem_detailed_pgfault))
1444 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);
1447 rrdset_next(st_mem_detailed_pgfault);
1449 if(unlikely(!st_mem_detailed_pgmajfault)) {
1450 st_mem_detailed_pgmajfault = rrdset_find_bytype("services", "mem_pgmajfault");
1451 if(likely(!st_mem_detailed_pgmajfault))
1452 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);
1455 rrdset_next(st_mem_detailed_pgmajfault);
1457 if(unlikely(!st_mem_detailed_pgpgin)) {
1458 st_mem_detailed_pgpgin = rrdset_find_bytype("services", "mem_pgpgin");
1459 if(likely(!st_mem_detailed_pgpgin))
1460 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);
1463 rrdset_next(st_mem_detailed_pgpgin);
1465 if(unlikely(!st_mem_detailed_pgpgout)) {
1466 st_mem_detailed_pgpgout = rrdset_find_bytype("services", "mem_pgpgout");
1467 if(likely(!st_mem_detailed_pgpgout))
1468 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);
1471 rrdset_next(st_mem_detailed_pgpgout);
1474 if(likely(do_mem_failcnt)) {
1475 if(unlikely(!st_mem_failcnt)) {
1476 st_mem_failcnt = rrdset_find_bytype("services", "mem_failcnt");
1477 if(likely(!st_mem_failcnt))
1478 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);
1481 rrdset_next(st_mem_failcnt);
1484 if(likely(do_swap_usage)) {
1485 if(unlikely(!st_swap_usage)) {
1486 st_swap_usage = rrdset_find_bytype("services", "swap_usage");
1487 if(likely(!st_swap_usage))
1488 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);
1491 rrdset_next(st_swap_usage);
1495 if(unlikely(!st_io_read)) {
1496 st_io_read = rrdset_find_bytype("services", "io_read");
1497 if(likely(!st_io_read))
1498 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);
1501 rrdset_next(st_io_read);
1503 if(unlikely(!st_io_write)) {
1504 st_io_write = rrdset_find_bytype("services", "io_write");
1505 if(likely(!st_io_write))
1506 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);
1509 rrdset_next(st_io_write);
1512 if(likely(do_io_ops)) {
1513 if(unlikely(!st_io_serviced_read)) {
1514 st_io_serviced_read = rrdset_find_bytype("services", "io_ops_read");
1515 if(likely(!st_io_serviced_read))
1516 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);
1519 rrdset_next(st_io_serviced_read);
1521 if(unlikely(!st_io_serviced_write)) {
1522 st_io_serviced_write = rrdset_find_bytype("services", "io_ops_write");
1523 if(likely(!st_io_serviced_write))
1524 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);
1527 rrdset_next(st_io_serviced_write);
1530 if(likely(do_throttle_io)) {
1531 if(unlikely(!st_throttle_io_read)) {
1532 st_throttle_io_read = rrdset_find_bytype("services", "throttle_io_read");
1533 if(likely(!st_throttle_io_read))
1534 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);
1537 rrdset_next(st_throttle_io_read);
1539 if(unlikely(!st_throttle_io_write)) {
1540 st_throttle_io_write = rrdset_find_bytype("services", "throttle_io_write");
1541 if(likely(!st_throttle_io_write))
1542 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);
1545 rrdset_next(st_throttle_io_write);
1548 if(likely(do_throttle_ops)) {
1549 if(unlikely(!st_throttle_ops_read)) {
1550 st_throttle_ops_read = rrdset_find_bytype("services", "throttle_io_ops_read");
1551 if(likely(!st_throttle_ops_read))
1552 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);
1555 rrdset_next(st_throttle_ops_read);
1557 if(unlikely(!st_throttle_ops_write)) {
1558 st_throttle_ops_write = rrdset_find_bytype("services", "throttle_io_ops_write");
1559 if(likely(!st_throttle_ops_write))
1560 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);
1563 rrdset_next(st_throttle_ops_write);
1566 if(likely(do_queued_ops)) {
1567 if(unlikely(!st_queued_ops_read)) {
1568 st_queued_ops_read = rrdset_find_bytype("services", "queued_io_ops_read");
1569 if(likely(!st_queued_ops_read))
1570 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);
1573 rrdset_next(st_queued_ops_read);
1575 if(unlikely(!st_queued_ops_write)) {
1576 st_queued_ops_write = rrdset_find_bytype("services", "queued_io_ops_write");
1577 if(likely(!st_queued_ops_write))
1578 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);
1581 rrdset_next(st_queued_ops_write);
1584 if(likely(do_merged_ops)) {
1585 if(unlikely(!st_merged_ops_read)) {
1586 st_merged_ops_read = rrdset_find_bytype("services", "merged_io_ops_read");
1587 if(likely(!st_merged_ops_read))
1588 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);
1591 rrdset_next(st_merged_ops_read);
1593 if(unlikely(!st_merged_ops_write)) {
1594 st_merged_ops_write = rrdset_find_bytype("services", "merged_io_ops_write");
1595 if(likely(!st_merged_ops_write))
1596 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);
1599 rrdset_next(st_merged_ops_write);
1602 // update the values
1604 for(cg = cgroup_root; cg ; cg = cg->next) {
1605 if(unlikely(!cg->available || !cg->enabled || !(cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE)))
1608 if(likely(do_cpu && cg->cpuacct_stat.updated)) {
1609 if(unlikely(!cg->rd_cpu))
1610 cg->rd_cpu = rrddim_add(st_cpu, cg->chart_id, cg->chart_title, 100, hz, RRDDIM_INCREMENTAL);
1612 rrddim_set_by_pointer(st_cpu, cg->rd_cpu, cg->cpuacct_stat.user + cg->cpuacct_stat.system);
1615 if(likely(do_mem_usage && cg->memory.updated_usage_in_bytes)) {
1616 if(unlikely(!cg->rd_mem_usage))
1617 cg->rd_mem_usage = rrddim_add(st_mem_usage, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1619 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));
1622 if(likely(do_mem_detailed && cg->memory.updated_detailed)) {
1623 if(unlikely(!cg->rd_mem_detailed_rss))
1624 cg->rd_mem_detailed_rss = rrddim_add(st_mem_detailed_rss, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1625 rrddim_set_by_pointer(st_mem_detailed_rss, cg->rd_mem_detailed_rss, cg->memory.rss + cg->memory.rss_huge);
1627 if(unlikely(!cg->rd_mem_detailed_mapped))
1628 cg->rd_mem_detailed_mapped = rrddim_add(st_mem_detailed_mapped, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1629 rrddim_set_by_pointer(st_mem_detailed_mapped, cg->rd_mem_detailed_mapped, cg->memory.mapped_file);
1631 if(unlikely(!cg->rd_mem_detailed_cache))
1632 cg->rd_mem_detailed_cache = rrddim_add(st_mem_detailed_cache, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1633 rrddim_set_by_pointer(st_mem_detailed_cache, cg->rd_mem_detailed_cache, cg->memory.cache);
1635 if(unlikely(!cg->rd_mem_detailed_writeback))
1636 cg->rd_mem_detailed_writeback = rrddim_add(st_mem_detailed_writeback, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1637 rrddim_set_by_pointer(st_mem_detailed_writeback, cg->rd_mem_detailed_writeback, cg->memory.writeback);
1639 if(unlikely(!cg->rd_mem_detailed_pgfault))
1640 cg->rd_mem_detailed_pgfault = rrddim_add(st_mem_detailed_pgfault, cg->chart_id, cg->chart_title, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1641 rrddim_set_by_pointer(st_mem_detailed_pgfault, cg->rd_mem_detailed_pgfault, cg->memory.pgfault);
1643 if(unlikely(!cg->rd_mem_detailed_pgmajfault))
1644 cg->rd_mem_detailed_pgmajfault = rrddim_add(st_mem_detailed_pgmajfault, cg->chart_id, cg->chart_title, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1645 rrddim_set_by_pointer(st_mem_detailed_pgmajfault, cg->rd_mem_detailed_pgmajfault, cg->memory.pgmajfault);
1647 if(unlikely(!cg->rd_mem_detailed_pgpgin))
1648 cg->rd_mem_detailed_pgpgin = rrddim_add(st_mem_detailed_pgpgin, cg->chart_id, cg->chart_title, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1649 rrddim_set_by_pointer(st_mem_detailed_pgpgin, cg->rd_mem_detailed_pgpgin, cg->memory.pgpgin);
1651 if(unlikely(!cg->rd_mem_detailed_pgpgout))
1652 cg->rd_mem_detailed_pgpgout = rrddim_add(st_mem_detailed_pgpgout, cg->chart_id, cg->chart_title, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1653 rrddim_set_by_pointer(st_mem_detailed_pgpgout, cg->rd_mem_detailed_pgpgout, cg->memory.pgpgout);
1656 if(likely(do_mem_failcnt && cg->memory.updated_failcnt)) {
1657 if(unlikely(!cg->rd_mem_failcnt))
1658 cg->rd_mem_failcnt = rrddim_add(st_mem_failcnt, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1660 rrddim_set_by_pointer(st_mem_failcnt, cg->rd_mem_failcnt, cg->memory.failcnt);
1663 if(likely(do_swap_usage && cg->memory.updated_msw_usage_in_bytes)) {
1664 if(unlikely(!cg->rd_swap_usage))
1665 cg->rd_swap_usage = rrddim_add(st_swap_usage, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1667 rrddim_set_by_pointer(st_swap_usage, cg->rd_swap_usage, cg->memory.msw_usage_in_bytes);
1670 if(likely(do_io && cg->io_service_bytes.updated)) {
1671 if(unlikely(!cg->rd_io_service_bytes_read))
1672 cg->rd_io_service_bytes_read = rrddim_add(st_io_read, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1674 rrddim_set_by_pointer(st_io_read, cg->rd_io_service_bytes_read, cg->io_service_bytes.Read);
1676 if(unlikely(!cg->rd_io_service_bytes_write))
1677 cg->rd_io_service_bytes_write = rrddim_add(st_io_write, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1679 rrddim_set_by_pointer(st_io_write, cg->rd_io_service_bytes_write, cg->io_service_bytes.Write);
1682 if(likely(do_io_ops && cg->io_serviced.updated)) {
1683 if(unlikely(!cg->rd_io_serviced_read))
1684 cg->rd_io_serviced_read = rrddim_add(st_io_serviced_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1686 rrddim_set_by_pointer(st_io_serviced_read, cg->rd_io_serviced_read, cg->io_serviced.Read);
1688 if(unlikely(!cg->rd_io_serviced_write))
1689 cg->rd_io_serviced_write = rrddim_add(st_io_serviced_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1691 rrddim_set_by_pointer(st_io_serviced_write, cg->rd_io_serviced_write, cg->io_serviced.Write);
1694 if(likely(do_throttle_io && cg->throttle_io_service_bytes.updated)) {
1695 if(unlikely(!cg->rd_throttle_io_read))
1696 cg->rd_throttle_io_read = rrddim_add(st_throttle_io_read, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1698 rrddim_set_by_pointer(st_throttle_io_read, cg->rd_throttle_io_read, cg->throttle_io_service_bytes.Read);
1700 if(unlikely(!cg->rd_throttle_io_write))
1701 cg->rd_throttle_io_write = rrddim_add(st_throttle_io_write, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1703 rrddim_set_by_pointer(st_throttle_io_write, cg->rd_throttle_io_write, cg->throttle_io_service_bytes.Write);
1706 if(likely(do_throttle_ops && cg->throttle_io_serviced.updated)) {
1707 if(unlikely(!cg->rd_throttle_io_serviced_read))
1708 cg->rd_throttle_io_serviced_read = rrddim_add(st_throttle_ops_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1710 rrddim_set_by_pointer(st_throttle_ops_read, cg->rd_throttle_io_serviced_read, cg->throttle_io_serviced.Read);
1712 if(unlikely(!cg->rd_throttle_io_serviced_write))
1713 cg->rd_throttle_io_serviced_write = rrddim_add(st_throttle_ops_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1715 rrddim_set_by_pointer(st_throttle_ops_write, cg->rd_throttle_io_serviced_write, cg->throttle_io_serviced.Write);
1718 if(likely(do_queued_ops && cg->io_queued.updated)) {
1719 if(unlikely(!cg->rd_io_queued_read))
1720 cg->rd_io_queued_read = rrddim_add(st_queued_ops_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1722 rrddim_set_by_pointer(st_queued_ops_read, cg->rd_io_queued_read, cg->io_queued.Read);
1724 if(unlikely(!cg->rd_io_queued_write))
1725 cg->rd_io_queued_write = rrddim_add(st_queued_ops_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1727 rrddim_set_by_pointer(st_queued_ops_write, cg->rd_io_queued_write, cg->io_queued.Write);
1730 if(likely(do_merged_ops && cg->io_merged.updated)) {
1731 if(unlikely(!cg->rd_io_merged_read))
1732 cg->rd_io_merged_read = rrddim_add(st_merged_ops_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1734 rrddim_set_by_pointer(st_merged_ops_read, cg->rd_io_merged_read, cg->io_merged.Read);
1736 if(unlikely(!cg->rd_io_merged_write))
1737 cg->rd_io_merged_write = rrddim_add(st_merged_ops_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1739 rrddim_set_by_pointer(st_merged_ops_write, cg->rd_io_merged_write, cg->io_merged.Write);
1743 // complete the iteration
1745 rrdset_done(st_cpu);
1747 if(likely(do_mem_usage))
1748 rrdset_done(st_mem_usage);
1750 if(unlikely(do_mem_detailed)) {
1751 rrdset_done(st_mem_detailed_cache);
1752 rrdset_done(st_mem_detailed_rss);
1753 rrdset_done(st_mem_detailed_mapped);
1754 rrdset_done(st_mem_detailed_writeback);
1755 rrdset_done(st_mem_detailed_pgfault);
1756 rrdset_done(st_mem_detailed_pgmajfault);
1757 rrdset_done(st_mem_detailed_pgpgin);
1758 rrdset_done(st_mem_detailed_pgpgout);
1761 if(likely(do_mem_failcnt))
1762 rrdset_done(st_mem_failcnt);
1764 if(likely(do_swap_usage))
1765 rrdset_done(st_swap_usage);
1768 rrdset_done(st_io_read);
1769 rrdset_done(st_io_write);
1772 if(likely(do_io_ops)) {
1773 rrdset_done(st_io_serviced_read);
1774 rrdset_done(st_io_serviced_write);
1777 if(likely(do_throttle_io)) {
1778 rrdset_done(st_throttle_io_read);
1779 rrdset_done(st_throttle_io_write);
1782 if(likely(do_throttle_ops)) {
1783 rrdset_done(st_throttle_ops_read);
1784 rrdset_done(st_throttle_ops_write);
1787 if(likely(do_queued_ops)) {
1788 rrdset_done(st_queued_ops_read);
1789 rrdset_done(st_queued_ops_write);
1792 if(likely(do_merged_ops)) {
1793 rrdset_done(st_merged_ops_read);
1794 rrdset_done(st_merged_ops_write);
1798 static inline char *cgroup_chart_type(char *buffer, const char *id, size_t len) {
1799 if(buffer[0]) return buffer;
1801 if(id[0] == '\0' || (id[0] == '/' && id[1] == '\0'))
1802 strncpy(buffer, "cgroup_root", len);
1804 snprintfz(buffer, len, "cgroup_%s", id);
1806 netdata_fix_chart_id(buffer);
1810 void update_cgroup_charts(int update_every) {
1811 debug(D_CGROUP, "updating cgroups charts");
1813 char type[RRD_ID_LENGTH_MAX + 1];
1814 char title[CHART_TITLE_MAX + 1];
1816 int services_do_cpu = 0,
1817 services_do_mem_usage = 0,
1818 services_do_mem_detailed = 0,
1819 services_do_mem_failcnt = 0,
1820 services_do_swap_usage = 0,
1822 services_do_io_ops = 0,
1823 services_do_throttle_io = 0,
1824 services_do_throttle_ops = 0,
1825 services_do_queued_ops = 0,
1826 services_do_merged_ops = 0;
1829 for(cg = cgroup_root; cg ; cg = cg->next) {
1830 if(unlikely(!cg->available || !cg->enabled))
1833 if(likely(cgroup_enable_systemd_services && cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE)) {
1834 if(cg->cpuacct_stat.updated && cg->cpuacct_stat.enabled == CONFIG_ONDEMAND_YES) services_do_cpu++;
1836 if(cgroup_enable_systemd_services_detailed_memory && cg->memory.updated_detailed && cg->memory.enabled_detailed) services_do_mem_detailed++;
1837 if(cg->memory.updated_usage_in_bytes && cg->memory.enabled_usage_in_bytes == CONFIG_ONDEMAND_YES) services_do_mem_usage++;
1838 if(cg->memory.updated_failcnt && cg->memory.enabled_failcnt == CONFIG_ONDEMAND_YES) services_do_mem_failcnt++;
1839 if(cg->memory.updated_msw_usage_in_bytes && cg->memory.enabled_msw_usage_in_bytes == CONFIG_ONDEMAND_YES) services_do_swap_usage++;
1841 if(cg->io_service_bytes.updated && cg->io_service_bytes.enabled == CONFIG_ONDEMAND_YES) services_do_io++;
1842 if(cg->io_serviced.updated && cg->io_serviced.enabled == CONFIG_ONDEMAND_YES) services_do_io_ops++;
1843 if(cg->throttle_io_service_bytes.updated && cg->throttle_io_service_bytes.enabled == CONFIG_ONDEMAND_YES) services_do_throttle_io++;
1844 if(cg->throttle_io_serviced.updated && cg->throttle_io_serviced.enabled == CONFIG_ONDEMAND_YES) services_do_throttle_ops++;
1845 if(cg->io_queued.updated && cg->io_queued.enabled == CONFIG_ONDEMAND_YES) services_do_queued_ops++;
1846 if(cg->io_merged.updated && cg->io_merged.enabled == CONFIG_ONDEMAND_YES) services_do_merged_ops++;
1852 if(likely(cg->cpuacct_stat.updated && cg->cpuacct_stat.enabled == CONFIG_ONDEMAND_YES)) {
1853 if(unlikely(!cg->st_cpu)) {
1854 cg->st_cpu = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "cpu");
1855 if(likely(!cg->st_cpu)) {
1856 snprintfz(title, CHART_TITLE_MAX, "CPU Usage (%d%% = %d core%s) for cgroup %s", (processors * 100), processors, (processors > 1) ? "s" : "", cg->chart_title);
1857 cg->st_cpu = rrdset_create(type, "cpu", NULL, "cpu", "cgroup.cpu", title, "%", CHART_PRIORITY_CONTAINERS, update_every, RRDSET_TYPE_STACKED);
1859 rrddim_add(cg->st_cpu, "user", NULL, 100, hz, RRDDIM_INCREMENTAL);
1860 rrddim_add(cg->st_cpu, "system", NULL, 100, hz, RRDDIM_INCREMENTAL);
1863 rrdset_next(cg->st_cpu);
1865 rrddim_set(cg->st_cpu, "user", cg->cpuacct_stat.user);
1866 rrddim_set(cg->st_cpu, "system", cg->cpuacct_stat.system);
1867 rrdset_done(cg->st_cpu);
1870 if(likely(cg->cpuacct_usage.updated && cg->cpuacct_usage.enabled == CONFIG_ONDEMAND_YES)) {
1871 char id[RRD_ID_LENGTH_MAX + 1];
1874 if(unlikely(!cg->st_cpu_per_core)) {
1875 cg->st_cpu_per_core = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "cpu_per_core");
1876 if(likely(!cg->st_cpu_per_core)) {
1877 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);
1878 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);
1880 for(i = 0; i < cg->cpuacct_usage.cpus; i++) {
1881 snprintfz(id, CHART_TITLE_MAX, "cpu%u", i);
1882 rrddim_add(cg->st_cpu_per_core, id, NULL, 100, 1000000000, RRDDIM_INCREMENTAL);
1886 rrdset_next(cg->st_cpu_per_core);
1888 for(i = 0; i < cg->cpuacct_usage.cpus ;i++) {
1889 snprintfz(id, CHART_TITLE_MAX, "cpu%u", i);
1890 rrddim_set(cg->st_cpu_per_core, id, cg->cpuacct_usage.cpu_percpu[i]);
1892 rrdset_done(cg->st_cpu_per_core);
1895 if(likely(cg->memory.updated_detailed && cg->memory.enabled_detailed == CONFIG_ONDEMAND_YES)) {
1896 if(unlikely(!cg->st_mem)) {
1897 cg->st_mem = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem");
1898 if(likely(!cg->st_mem)) {
1899 snprintfz(title, CHART_TITLE_MAX, "Memory Usage for cgroup %s", cg->chart_title);
1900 cg->st_mem = rrdset_create(type, "mem", NULL, "mem", "cgroup.mem", title, "MB", CHART_PRIORITY_CONTAINERS + 210, update_every, RRDSET_TYPE_STACKED);
1903 rrddim_add(cg->st_mem, "cache", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1904 rrddim_add(cg->st_mem, "rss", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1905 if(cg->memory.detailed_has_swap)
1906 rrddim_add(cg->st_mem, "swap", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1907 rrddim_add(cg->st_mem, "rss_huge", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1908 rrddim_add(cg->st_mem, "mapped_file", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1911 rrdset_next(cg->st_mem);
1913 rrddim_set(cg->st_mem, "cache", cg->memory.cache);
1914 rrddim_set(cg->st_mem, "rss", cg->memory.rss);
1915 if(cg->memory.detailed_has_swap)
1916 rrddim_set(cg->st_mem, "swap", cg->memory.swap);
1917 rrddim_set(cg->st_mem, "rss_huge", cg->memory.rss_huge);
1918 rrddim_set(cg->st_mem, "mapped_file", cg->memory.mapped_file);
1919 rrdset_done(cg->st_mem);
1921 if(unlikely(!cg->st_writeback)) {
1922 cg->st_writeback = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "writeback");
1923 if(likely(!cg->st_writeback)) {
1924 snprintfz(title, CHART_TITLE_MAX, "Writeback Memory for cgroup %s", cg->chart_title);
1925 cg->st_writeback = rrdset_create(type, "writeback", NULL, "mem", "cgroup.writeback", title, "MB", CHART_PRIORITY_CONTAINERS + 300, update_every, RRDSET_TYPE_AREA);
1928 if(cg->memory.detailed_has_dirty)
1929 rrddim_add(cg->st_writeback, "dirty", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1930 rrddim_add(cg->st_writeback, "writeback", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1933 rrdset_next(cg->st_writeback);
1935 if(cg->memory.detailed_has_dirty)
1936 rrddim_set(cg->st_writeback, "dirty", cg->memory.dirty);
1937 rrddim_set(cg->st_writeback, "writeback", cg->memory.writeback);
1938 rrdset_done(cg->st_writeback);
1940 if(unlikely(!cg->st_mem_activity)) {
1941 cg->st_mem_activity = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_activity");
1942 if(likely(!cg->st_mem_activity)) {
1943 snprintfz(title, CHART_TITLE_MAX, "Memory Activity for cgroup %s", cg->chart_title);
1944 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);
1946 rrddim_add(cg->st_mem_activity, "pgpgin", "in", system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1947 rrddim_add(cg->st_mem_activity, "pgpgout", "out", -system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1950 rrdset_next(cg->st_mem_activity);
1952 rrddim_set(cg->st_mem_activity, "pgpgin", cg->memory.pgpgin);
1953 rrddim_set(cg->st_mem_activity, "pgpgout", cg->memory.pgpgout);
1954 rrdset_done(cg->st_mem_activity);
1956 if(unlikely(!cg->st_pgfaults)) {
1957 cg->st_pgfaults = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "pgfaults");
1958 if(likely(!cg->st_pgfaults)) {
1959 snprintfz(title, CHART_TITLE_MAX, "Memory Page Faults for cgroup %s", cg->chart_title);
1960 cg->st_pgfaults = rrdset_create(type, "pgfaults", NULL, "mem", "cgroup.pgfaults", title, "MB/s", CHART_PRIORITY_CONTAINERS + 500, update_every, RRDSET_TYPE_LINE);
1962 rrddim_add(cg->st_pgfaults, "pgfault", NULL, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1963 rrddim_add(cg->st_pgfaults, "pgmajfault", "swap", -system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1966 rrdset_next(cg->st_pgfaults);
1968 rrddim_set(cg->st_pgfaults, "pgfault", cg->memory.pgfault);
1969 rrddim_set(cg->st_pgfaults, "pgmajfault", cg->memory.pgmajfault);
1970 rrdset_done(cg->st_pgfaults);
1973 if(likely(cg->memory.updated_usage_in_bytes && cg->memory.enabled_usage_in_bytes == CONFIG_ONDEMAND_YES)) {
1974 if(unlikely(!cg->st_mem_usage)) {
1975 cg->st_mem_usage = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_usage");
1976 if(likely(!cg->st_mem_usage)) {
1977 snprintfz(title, CHART_TITLE_MAX, "Used Memory %sfor cgroup %s", (cgroup_used_memory_without_cache && cg->memory.updated_detailed)?"without Cache ":"", cg->chart_title);
1978 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);
1980 rrddim_add(cg->st_mem_usage, "ram", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1981 rrddim_add(cg->st_mem_usage, "swap", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1984 rrdset_next(cg->st_mem_usage);
1986 rrddim_set(cg->st_mem_usage, "ram", cg->memory.usage_in_bytes - ((cgroup_used_memory_without_cache)?cg->memory.cache:0));
1987 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);
1988 rrdset_done(cg->st_mem_usage);
1991 if(likely(cg->memory.updated_failcnt && cg->memory.enabled_failcnt == CONFIG_ONDEMAND_YES)) {
1992 if(unlikely(!cg->st_mem_failcnt)) {
1993 cg->st_mem_failcnt = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_failcnt");
1994 if(likely(!cg->st_mem_failcnt)) {
1995 snprintfz(title, CHART_TITLE_MAX, "Memory Limit Failures for cgroup %s", cg->chart_title);
1996 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);
1998 rrddim_add(cg->st_mem_failcnt, "failures", NULL, 1, 1, RRDDIM_INCREMENTAL);
2001 rrdset_next(cg->st_mem_failcnt);
2003 rrddim_set(cg->st_mem_failcnt, "failures", cg->memory.failcnt);
2004 rrdset_done(cg->st_mem_failcnt);
2007 if(likely(cg->io_service_bytes.updated && cg->io_service_bytes.enabled == CONFIG_ONDEMAND_YES)) {
2008 if(unlikely(!cg->st_io)) {
2009 cg->st_io = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "io");
2010 if(likely(!cg->st_io)) {
2011 snprintfz(title, CHART_TITLE_MAX, "I/O Bandwidth (all disks) for cgroup %s", cg->chart_title);
2012 cg->st_io = rrdset_create(type, "io", NULL, "disk", "cgroup.io", title, "KB/s", CHART_PRIORITY_CONTAINERS + 1200, update_every, RRDSET_TYPE_AREA);
2014 rrddim_add(cg->st_io, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
2015 rrddim_add(cg->st_io, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
2018 rrdset_next(cg->st_io);
2020 rrddim_set(cg->st_io, "read", cg->io_service_bytes.Read);
2021 rrddim_set(cg->st_io, "write", cg->io_service_bytes.Write);
2022 rrdset_done(cg->st_io);
2025 if(likely(cg->io_serviced.updated && cg->io_serviced.enabled == CONFIG_ONDEMAND_YES)) {
2026 if(unlikely(!cg->st_serviced_ops)) {
2027 cg->st_serviced_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "serviced_ops");
2028 if(likely(!cg->st_serviced_ops)) {
2029 snprintfz(title, CHART_TITLE_MAX, "Serviced I/O Operations (all disks) for cgroup %s", cg->chart_title);
2030 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);
2032 rrddim_add(cg->st_serviced_ops, "read", NULL, 1, 1, RRDDIM_INCREMENTAL);
2033 rrddim_add(cg->st_serviced_ops, "write", NULL, -1, 1, RRDDIM_INCREMENTAL);
2036 rrdset_next(cg->st_serviced_ops);
2038 rrddim_set(cg->st_serviced_ops, "read", cg->io_serviced.Read);
2039 rrddim_set(cg->st_serviced_ops, "write", cg->io_serviced.Write);
2040 rrdset_done(cg->st_serviced_ops);
2043 if(likely(cg->throttle_io_service_bytes.updated && cg->throttle_io_service_bytes.enabled == CONFIG_ONDEMAND_YES)) {
2044 if(unlikely(!cg->st_throttle_io)) {
2045 cg->st_throttle_io = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "throttle_io");
2046 if(likely(!cg->st_throttle_io)) {
2047 snprintfz(title, CHART_TITLE_MAX, "Throttle I/O Bandwidth (all disks) for cgroup %s", cg->chart_title);
2048 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);
2050 rrddim_add(cg->st_throttle_io, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
2051 rrddim_add(cg->st_throttle_io, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
2054 rrdset_next(cg->st_throttle_io);
2056 rrddim_set(cg->st_throttle_io, "read", cg->throttle_io_service_bytes.Read);
2057 rrddim_set(cg->st_throttle_io, "write", cg->throttle_io_service_bytes.Write);
2058 rrdset_done(cg->st_throttle_io);
2061 if(likely(cg->throttle_io_serviced.updated && cg->throttle_io_serviced.enabled == CONFIG_ONDEMAND_YES)) {
2062 if(unlikely(!cg->st_throttle_serviced_ops)) {
2063 cg->st_throttle_serviced_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "throttle_serviced_ops");
2064 if(likely(!cg->st_throttle_serviced_ops)) {
2065 snprintfz(title, CHART_TITLE_MAX, "Throttle Serviced I/O Operations (all disks) for cgroup %s", cg->chart_title);
2066 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);
2068 rrddim_add(cg->st_throttle_serviced_ops, "read", NULL, 1, 1, RRDDIM_INCREMENTAL);
2069 rrddim_add(cg->st_throttle_serviced_ops, "write", NULL, -1, 1, RRDDIM_INCREMENTAL);
2072 rrdset_next(cg->st_throttle_serviced_ops);
2074 rrddim_set(cg->st_throttle_serviced_ops, "read", cg->throttle_io_serviced.Read);
2075 rrddim_set(cg->st_throttle_serviced_ops, "write", cg->throttle_io_serviced.Write);
2076 rrdset_done(cg->st_throttle_serviced_ops);
2079 if(likely(cg->io_queued.updated && cg->io_queued.enabled == CONFIG_ONDEMAND_YES)) {
2080 if(unlikely(!cg->st_queued_ops)) {
2081 cg->st_queued_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "queued_ops");
2082 if(likely(!cg->st_queued_ops)) {
2083 snprintfz(title, CHART_TITLE_MAX, "Queued I/O Operations (all disks) for cgroup %s", cg->chart_title);
2084 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);
2086 rrddim_add(cg->st_queued_ops, "read", NULL, 1, 1, RRDDIM_ABSOLUTE);
2087 rrddim_add(cg->st_queued_ops, "write", NULL, -1, 1, RRDDIM_ABSOLUTE);
2090 rrdset_next(cg->st_queued_ops);
2092 rrddim_set(cg->st_queued_ops, "read", cg->io_queued.Read);
2093 rrddim_set(cg->st_queued_ops, "write", cg->io_queued.Write);
2094 rrdset_done(cg->st_queued_ops);
2097 if(likely(cg->io_merged.updated && cg->io_merged.enabled == CONFIG_ONDEMAND_YES)) {
2098 if(unlikely(!cg->st_merged_ops)) {
2099 cg->st_merged_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "merged_ops");
2100 if(likely(!cg->st_merged_ops)) {
2101 snprintfz(title, CHART_TITLE_MAX, "Merged I/O Operations (all disks) for cgroup %s", cg->chart_title);
2102 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);
2104 rrddim_add(cg->st_merged_ops, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
2105 rrddim_add(cg->st_merged_ops, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
2108 rrdset_next(cg->st_merged_ops);
2110 rrddim_set(cg->st_merged_ops, "read", cg->io_merged.Read);
2111 rrddim_set(cg->st_merged_ops, "write", cg->io_merged.Write);
2112 rrdset_done(cg->st_merged_ops);
2116 if(likely(cgroup_enable_systemd_services))
2117 update_services_charts(update_every,
2119 services_do_mem_usage,
2120 services_do_mem_detailed,
2121 services_do_mem_failcnt,
2122 services_do_swap_usage,
2125 services_do_throttle_io,
2126 services_do_throttle_ops,
2127 services_do_queued_ops,
2128 services_do_merged_ops
2131 debug(D_CGROUP, "done updating cgroups charts");
2134 // ----------------------------------------------------------------------------
2137 void *cgroups_main(void *ptr) {
2138 struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
2140 info("CGROUP Plugin thread created with task id %d", gettid());
2142 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
2143 error("Cannot set pthread cancel type to DEFERRED.");
2145 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
2146 error("Cannot set pthread cancel state to ENABLE.");
2148 struct rusage thread;
2150 // when ZERO, attempt to do it
2151 int vdo_cpu_netdata = config_get_boolean("plugin:cgroups", "cgroups plugin resource charts", 1);
2153 read_cgroup_plugin_configuration();
2155 RRDSET *stcpu_thread = NULL;
2157 usec_t step = cgroup_update_every * USEC_PER_SEC;
2158 usec_t find_every = cgroup_check_for_new_every * USEC_PER_SEC, find_next = 0;
2160 usec_t now = now_monotonic_usec();
2161 usec_t next = now - (now % step) + step;
2164 sleep_usec(next - now);
2165 now = now_monotonic_usec();
2168 if(unlikely(netdata_exit)) break;
2170 // BEGIN -- the job to be done
2172 if(unlikely(now >= find_next)) {
2174 find_next = now + find_every;
2177 read_all_cgroups(cgroup_root);
2178 update_cgroup_charts(cgroup_update_every);
2180 // END -- the job is done
2182 // --------------------------------------------------------------------
2184 if(vdo_cpu_netdata) {
2185 getrusage(RUSAGE_THREAD, &thread);
2187 if(unlikely(!stcpu_thread)) {
2188 stcpu_thread = rrdset_find("netdata.plugin_cgroups_cpu");
2189 if(unlikely(!stcpu_thread))
2190 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);
2192 rrddim_add(stcpu_thread, "user", NULL, 1, 1000, RRDDIM_INCREMENTAL);
2193 rrddim_add(stcpu_thread, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
2196 rrdset_next(stcpu_thread);
2198 rrddim_set(stcpu_thread, "user" , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
2199 rrddim_set(stcpu_thread, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
2200 rrdset_done(stcpu_thread);
2204 info("CGROUP thread exiting");
2206 static_thread->enabled = 0;