3 // ----------------------------------------------------------------------------
6 static int cgroup_enable_cpuacct_stat = CONFIG_ONDEMAND_ONDEMAND;
7 static int cgroup_enable_cpuacct_usage = CONFIG_ONDEMAND_ONDEMAND;
8 static int cgroup_enable_memory = CONFIG_ONDEMAND_ONDEMAND;
9 static int cgroup_enable_devices = CONFIG_ONDEMAND_ONDEMAND;
10 static int cgroup_enable_blkio = CONFIG_ONDEMAND_ONDEMAND;
11 static int cgroup_enable_new_cgroups_detected_at_runtime = 1;
12 static int cgroup_check_for_new_every = 10;
13 static char *cgroup_cpuacct_base = NULL;
14 static char *cgroup_blkio_base = NULL;
15 static char *cgroup_memory_base = NULL;
16 static char *cgroup_devices_base = NULL;
18 static int cgroup_root_count = 0;
19 static int cgroup_root_max = 500;
20 static int cgroup_max_depth = 0;
22 static NETDATA_SIMPLE_PATTERN *disabled_cgroups_patterns = NULL;
23 static NETDATA_SIMPLE_PATTERN *disabled_cgroup_paths = NULL;
25 void read_cgroup_plugin_configuration() {
26 cgroup_check_for_new_every = (int)config_get_number("plugin:cgroups", "check for new cgroups every", cgroup_check_for_new_every);
28 cgroup_enable_cpuacct_stat = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct stat", cgroup_enable_cpuacct_stat);
29 cgroup_enable_cpuacct_usage = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct usage", cgroup_enable_cpuacct_usage);
30 cgroup_enable_memory = config_get_boolean_ondemand("plugin:cgroups", "enable memory", cgroup_enable_memory);
31 cgroup_enable_blkio = config_get_boolean_ondemand("plugin:cgroups", "enable blkio", cgroup_enable_blkio);
33 char filename[FILENAME_MAX + 1], *s;
34 struct mountinfo *mi, *root = mountinfo_read(0);
36 mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "cpuacct");
37 if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "cpuacct");
39 error("Cannot find cgroup cpuacct mountinfo. Assuming default: /sys/fs/cgroup/cpuacct");
40 s = "/sys/fs/cgroup/cpuacct";
42 else s = mi->mount_point;
43 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
44 cgroup_cpuacct_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/cpuacct", filename);
46 mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "blkio");
47 if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "blkio");
49 error("Cannot find cgroup blkio mountinfo. Assuming default: /sys/fs/cgroup/blkio");
50 s = "/sys/fs/cgroup/blkio";
52 else s = mi->mount_point;
53 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
54 cgroup_blkio_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/blkio", filename);
56 mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "memory");
57 if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "memory");
59 error("Cannot find cgroup memory mountinfo. Assuming default: /sys/fs/cgroup/memory");
60 s = "/sys/fs/cgroup/memory";
62 else s = mi->mount_point;
63 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
64 cgroup_memory_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/memory", filename);
66 mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "devices");
67 if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "devices");
69 error("Cannot find cgroup devices mountinfo. Assuming default: /sys/fs/cgroup/devices");
70 s = "/sys/fs/cgroup/devices";
72 else s = mi->mount_point;
73 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
74 cgroup_devices_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/devices", filename);
76 cgroup_root_max = (int)config_get_number("plugin:cgroups", "max cgroups to allow", cgroup_root_max);
77 cgroup_max_depth = (int)config_get_number("plugin:cgroups", "max cgroups depth to monitor", cgroup_max_depth);
79 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);
81 disabled_cgroups_patterns = netdata_simple_pattern_list_create(
82 config_get("plugin:cgroups", "disable by default cgroups matching",
83 " lxc docker libvirt qemu systemd "
84 " system system.slice "
85 " machine machine.slice "
94 " */ns " // lxc/*/ns #1397
95 ), NETDATA_SIMPLE_PATTERN_MODE_EXACT);
97 disabled_cgroup_paths = netdata_simple_pattern_list_create(
98 config_get("plugin:cgroups", "disable by default cgroup paths matching",
106 ), NETDATA_SIMPLE_PATTERN_MODE_EXACT);
108 mountinfo_free(root);
111 // ----------------------------------------------------------------------------
119 unsigned long long Read;
120 unsigned long long Write;
122 unsigned long long Sync;
123 unsigned long long Async;
124 unsigned long long Total;
128 // https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt
136 unsigned long long cache;
137 unsigned long long rss;
138 unsigned long long rss_huge;
139 unsigned long long mapped_file;
140 unsigned long long writeback;
141 unsigned long long dirty;
142 unsigned long long swap;
143 unsigned long long pgpgin;
144 unsigned long long pgpgout;
145 unsigned long long pgfault;
146 unsigned long long pgmajfault;
148 unsigned long long inactive_anon;
149 unsigned long long active_anon;
150 unsigned long long inactive_file;
151 unsigned long long active_file;
152 unsigned long long unevictable;
153 unsigned long long hierarchical_memory_limit;
154 unsigned long long total_cache;
155 unsigned long long total_rss;
156 unsigned long long total_rss_huge;
157 unsigned long long total_mapped_file;
158 unsigned long long total_writeback;
159 unsigned long long total_dirty;
160 unsigned long long total_swap;
161 unsigned long long total_pgpgin;
162 unsigned long long total_pgpgout;
163 unsigned long long total_pgfault;
164 unsigned long long total_pgmajfault;
165 unsigned long long total_inactive_anon;
166 unsigned long long total_active_anon;
167 unsigned long long total_inactive_file;
168 unsigned long long total_active_file;
169 unsigned long long total_unevictable;
172 int usage_in_bytes_updated;
173 char *filename_usage_in_bytes;
174 unsigned long long usage_in_bytes;
176 int msw_usage_in_bytes_updated;
177 char *filename_msw_usage_in_bytes;
178 unsigned long long msw_usage_in_bytes;
181 char *filename_failcnt;
182 unsigned long long failcnt;
185 // https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt
186 struct cpuacct_stat {
191 unsigned long long user;
192 unsigned long long system;
195 // https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt
196 struct cpuacct_usage {
202 unsigned long long *cpu_percpu;
205 #define CGROUP_OPTIONS_DISABLED_DUPLICATE 0x00000001
206 #define CGROUP_OPTIONS_PART_OF_GROUP 0x00000002
211 char available; // found in the filesystem
212 char enabled; // enabled in the config
222 struct cpuacct_stat cpuacct_stat;
223 struct cpuacct_usage cpuacct_usage;
225 struct memory memory;
227 struct blkio io_service_bytes; // bytes
228 struct blkio io_serviced; // operations
230 struct blkio throttle_io_service_bytes; // bytes
231 struct blkio throttle_io_serviced; // operations
233 struct blkio io_merged; // operations
234 struct blkio io_queued; // operations
238 } *cgroup_root = NULL;
240 // ----------------------------------------------------------------------------
241 // read values from /sys
243 void cgroup_read_cpuacct_stat(struct cpuacct_stat *cp) {
244 static procfile *ff = NULL;
246 static uint32_t user_hash = 0;
247 static uint32_t system_hash = 0;
249 if(unlikely(user_hash == 0)) {
250 user_hash = simple_hash("user");
251 system_hash = simple_hash("system");
256 ff = procfile_reopen(ff, cp->filename, NULL, PROCFILE_FLAG_DEFAULT);
259 ff = procfile_readall(ff);
262 unsigned long i, lines = procfile_lines(ff);
265 error("File '%s' should have 1+ lines.", cp->filename);
269 for(i = 0; i < lines ; i++) {
270 char *s = procfile_lineword(ff, i, 0);
271 uint32_t hash = simple_hash(s);
273 if(hash == user_hash && !strcmp(s, "user"))
274 cp->user = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
276 else if(hash == system_hash && !strcmp(s, "system"))
277 cp->system = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
282 // fprintf(stderr, "READ '%s': user: %llu, system: %llu\n", cp->filename, cp->user, cp->system);
286 void cgroup_read_cpuacct_usage(struct cpuacct_usage *ca) {
287 static procfile *ff = NULL;
291 ff = procfile_reopen(ff, ca->filename, NULL, PROCFILE_FLAG_DEFAULT);
294 ff = procfile_readall(ff);
297 if(procfile_lines(ff) < 1) {
298 error("File '%s' should have 1+ lines but has %u.", ca->filename, procfile_lines(ff));
302 unsigned long i = procfile_linewords(ff, 0);
305 // we may have 1 more CPU reported
307 char *s = procfile_lineword(ff, 0, i - 1);
313 freez(ca->cpu_percpu);
314 ca->cpu_percpu = mallocz(sizeof(unsigned long long) * i);
315 ca->cpus = (unsigned int)i;
318 for(i = 0; i < ca->cpus ;i++) {
319 ca->cpu_percpu[i] = strtoull(procfile_lineword(ff, 0, i), NULL, 10);
320 // fprintf(stderr, "READ '%s': cpu%d/%d: %llu ('%s')\n", ca->filename, i, ca->cpus, ca->cpu_percpu[i], procfile_lineword(ff, 0, i));
327 void cgroup_read_blkio(struct blkio *io) {
328 static procfile *ff = NULL;
330 static uint32_t Read_hash = 0;
331 static uint32_t Write_hash = 0;
333 static uint32_t Sync_hash = 0;
334 static uint32_t Async_hash = 0;
335 static uint32_t Total_hash = 0;
338 if(unlikely(Read_hash == 0)) {
339 Read_hash = simple_hash("Read");
340 Write_hash = simple_hash("Write");
342 Sync_hash = simple_hash("Sync");
343 Async_hash = simple_hash("Async");
344 Total_hash = simple_hash("Total");
350 ff = procfile_reopen(ff, io->filename, NULL, PROCFILE_FLAG_DEFAULT);
353 ff = procfile_readall(ff);
356 unsigned long i, lines = procfile_lines(ff);
359 error("File '%s' should have 1+ lines.", io->filename);
371 for(i = 0; i < lines ; i++) {
372 char *s = procfile_lineword(ff, i, 1);
373 uint32_t hash = simple_hash(s);
375 if(hash == Read_hash && !strcmp(s, "Read"))
376 io->Read += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
378 else if(hash == Write_hash && !strcmp(s, "Write"))
379 io->Write += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
382 else if(hash == Sync_hash && !strcmp(s, "Sync"))
383 io->Sync += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
385 else if(hash == Async_hash && !strcmp(s, "Async"))
386 io->Async += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
388 else if(hash == Total_hash && !strcmp(s, "Total"))
389 io->Total += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
394 // fprintf(stderr, "READ '%s': Read: %llu, Write: %llu, Sync: %llu, Async: %llu, Total: %llu\n", io->filename, io->Read, io->Write, io->Sync, io->Async, io->Total);
398 void cgroup_read_memory(struct memory *mem) {
399 static procfile *ff = NULL;
401 static uint32_t cache_hash = 0;
402 static uint32_t rss_hash = 0;
403 static uint32_t rss_huge_hash = 0;
404 static uint32_t mapped_file_hash = 0;
405 static uint32_t writeback_hash = 0;
406 static uint32_t dirty_hash = 0;
407 static uint32_t swap_hash = 0;
408 static uint32_t pgpgin_hash = 0;
409 static uint32_t pgpgout_hash = 0;
410 static uint32_t pgfault_hash = 0;
411 static uint32_t pgmajfault_hash = 0;
413 static uint32_t inactive_anon_hash = 0;
414 static uint32_t active_anon_hash = 0;
415 static uint32_t inactive_file_hash = 0;
416 static uint32_t active_file_hash = 0;
417 static uint32_t unevictable_hash = 0;
418 static uint32_t hierarchical_memory_limit_hash = 0;
419 static uint32_t total_cache_hash = 0;
420 static uint32_t total_rss_hash = 0;
421 static uint32_t total_rss_huge_hash = 0;
422 static uint32_t total_mapped_file_hash = 0;
423 static uint32_t total_writeback_hash = 0;
424 static uint32_t total_dirty_hash = 0;
425 static uint32_t total_swap_hash = 0;
426 static uint32_t total_pgpgin_hash = 0;
427 static uint32_t total_pgpgout_hash = 0;
428 static uint32_t total_pgfault_hash = 0;
429 static uint32_t total_pgmajfault_hash = 0;
430 static uint32_t total_inactive_anon_hash = 0;
431 static uint32_t total_active_anon_hash = 0;
432 static uint32_t total_inactive_file_hash = 0;
433 static uint32_t total_active_file_hash = 0;
434 static uint32_t total_unevictable_hash = 0;
436 if(unlikely(cache_hash == 0)) {
437 cache_hash = simple_hash("cache");
438 rss_hash = simple_hash("rss");
439 rss_huge_hash = simple_hash("rss_huge");
440 mapped_file_hash = simple_hash("mapped_file");
441 writeback_hash = simple_hash("writeback");
442 dirty_hash = simple_hash("dirty");
443 swap_hash = simple_hash("swap");
444 pgpgin_hash = simple_hash("pgpgin");
445 pgpgout_hash = simple_hash("pgpgout");
446 pgfault_hash = simple_hash("pgfault");
447 pgmajfault_hash = simple_hash("pgmajfault");
449 inactive_anon_hash = simple_hash("inactive_anon");
450 active_anon_hash = simple_hash("active_anon");
451 inactive_file_hash = simple_hash("inactive_file");
452 active_file_hash = simple_hash("active_file");
453 unevictable_hash = simple_hash("unevictable");
454 hierarchical_memory_limit_hash = simple_hash("hierarchical_memory_limit");
455 total_cache_hash = simple_hash("total_cache");
456 total_rss_hash = simple_hash("total_rss");
457 total_rss_huge_hash = simple_hash("total_rss_huge");
458 total_mapped_file_hash = simple_hash("total_mapped_file");
459 total_writeback_hash = simple_hash("total_writeback");
460 total_dirty_hash = simple_hash("total_dirty");
461 total_swap_hash = simple_hash("total_swap");
462 total_pgpgin_hash = simple_hash("total_pgpgin");
463 total_pgpgout_hash = simple_hash("total_pgpgout");
464 total_pgfault_hash = simple_hash("total_pgfault");
465 total_pgmajfault_hash = simple_hash("total_pgmajfault");
466 total_inactive_anon_hash = simple_hash("total_inactive_anon");
467 total_active_anon_hash = simple_hash("total_active_anon");
468 total_inactive_file_hash = simple_hash("total_inactive_file");
469 total_active_file_hash = simple_hash("total_active_file");
470 total_unevictable_hash = simple_hash("total_unevictable");
476 ff = procfile_reopen(ff, mem->filename, NULL, PROCFILE_FLAG_DEFAULT);
479 ff = procfile_readall(ff);
482 unsigned long i, lines = procfile_lines(ff);
485 error("File '%s' should have 1+ lines.", mem->filename);
489 for(i = 0; i < lines ; i++) {
490 char *s = procfile_lineword(ff, i, 0);
491 uint32_t hash = simple_hash(s);
493 if(hash == cache_hash && !strcmp(s, "cache"))
494 mem->cache = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
496 else if(hash == rss_hash && !strcmp(s, "rss"))
497 mem->rss = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
499 else if(hash == rss_huge_hash && !strcmp(s, "rss_huge"))
500 mem->rss_huge = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
502 else if(hash == mapped_file_hash && !strcmp(s, "mapped_file"))
503 mem->mapped_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
505 else if(hash == writeback_hash && !strcmp(s, "writeback"))
506 mem->writeback = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
508 else if(hash == dirty_hash && !strcmp(s, "dirty")) {
509 mem->dirty = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
510 mem->has_dirty_swap = 1;
513 else if(hash == swap_hash && !strcmp(s, "swap")) {
514 mem->swap = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
515 mem->has_dirty_swap = 1;
518 else if(hash == pgpgin_hash && !strcmp(s, "pgpgin"))
519 mem->pgpgin = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
521 else if(hash == pgpgout_hash && !strcmp(s, "pgpgout"))
522 mem->pgpgout = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
524 else if(hash == pgfault_hash && !strcmp(s, "pgfault"))
525 mem->pgfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
527 else if(hash == pgmajfault_hash && !strcmp(s, "pgmajfault"))
528 mem->pgmajfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
531 else if(hash == inactive_anon_hash && !strcmp(s, "inactive_anon"))
532 mem->inactive_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
534 else if(hash == active_anon_hash && !strcmp(s, "active_anon"))
535 mem->active_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
537 else if(hash == inactive_file_hash && !strcmp(s, "inactive_file"))
538 mem->inactive_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
540 else if(hash == active_file_hash && !strcmp(s, "active_file"))
541 mem->active_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
543 else if(hash == unevictable_hash && !strcmp(s, "unevictable"))
544 mem->unevictable = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
546 else if(hash == hierarchical_memory_limit_hash && !strcmp(s, "hierarchical_memory_limit"))
547 mem->hierarchical_memory_limit = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
549 else if(hash == total_cache_hash && !strcmp(s, "total_cache"))
550 mem->total_cache = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
552 else if(hash == total_rss_hash && !strcmp(s, "total_rss"))
553 mem->total_rss = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
555 else if(hash == total_rss_huge_hash && !strcmp(s, "total_rss_huge"))
556 mem->total_rss_huge = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
558 else if(hash == total_mapped_file_hash && !strcmp(s, "total_mapped_file"))
559 mem->total_mapped_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
561 else if(hash == total_writeback_hash && !strcmp(s, "total_writeback"))
562 mem->total_writeback = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
564 else if(hash == total_dirty_hash && !strcmp(s, "total_dirty"))
565 mem->total_dirty = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
567 else if(hash == total_swap_hash && !strcmp(s, "total_swap"))
568 mem->total_swap = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
570 else if(hash == total_pgpgin_hash && !strcmp(s, "total_pgpgin"))
571 mem->total_pgpgin = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
573 else if(hash == total_pgpgout_hash && !strcmp(s, "total_pgpgout"))
574 mem->total_pgpgout = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
576 else if(hash == total_pgfault_hash && !strcmp(s, "total_pgfault"))
577 mem->total_pgfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
579 else if(hash == total_pgmajfault_hash && !strcmp(s, "total_pgmajfault"))
580 mem->total_pgmajfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
582 else if(hash == total_inactive_anon_hash && !strcmp(s, "total_inactive_anon"))
583 mem->total_inactive_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
585 else if(hash == total_active_anon_hash && !strcmp(s, "total_active_anon"))
586 mem->total_active_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
588 else if(hash == total_inactive_file_hash && !strcmp(s, "total_inactive_file"))
589 mem->total_inactive_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
591 else if(hash == total_active_file_hash && !strcmp(s, "total_active_file"))
592 mem->total_active_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
594 else if(hash == total_unevictable_hash && !strcmp(s, "total_unevictable"))
595 mem->total_unevictable = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
599 // 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);
604 mem->usage_in_bytes_updated = 0;
605 if(mem->filename_usage_in_bytes) {
606 if(likely(!read_single_number_file(mem->filename_usage_in_bytes, &mem->usage_in_bytes)))
607 mem->usage_in_bytes_updated = 1;
610 mem->msw_usage_in_bytes_updated = 0;
611 if(mem->filename_msw_usage_in_bytes) {
612 if(likely(!read_single_number_file(mem->filename_msw_usage_in_bytes, &mem->msw_usage_in_bytes)))
613 mem->msw_usage_in_bytes_updated = 1;
616 mem->failcnt_updated = 0;
617 if(mem->filename_failcnt) {
618 if(likely(!read_single_number_file(mem->filename_failcnt, &mem->failcnt)))
619 mem->failcnt_updated = 1;
623 void cgroup_read(struct cgroup *cg) {
624 debug(D_CGROUP, "reading metrics for cgroups '%s'", cg->id);
626 cgroup_read_cpuacct_stat(&cg->cpuacct_stat);
627 cgroup_read_cpuacct_usage(&cg->cpuacct_usage);
628 cgroup_read_memory(&cg->memory);
629 cgroup_read_blkio(&cg->io_service_bytes);
630 cgroup_read_blkio(&cg->io_serviced);
631 cgroup_read_blkio(&cg->throttle_io_service_bytes);
632 cgroup_read_blkio(&cg->throttle_io_serviced);
633 cgroup_read_blkio(&cg->io_merged);
634 cgroup_read_blkio(&cg->io_queued);
637 void read_all_cgroups(struct cgroup *root) {
638 debug(D_CGROUP, "reading metrics for all cgroups");
642 for(cg = root; cg ; cg = cg->next)
643 if(cg->enabled && cg->available)
647 // ----------------------------------------------------------------------------
648 // add/remove/find cgroup objects
650 #define CGROUP_CHARTID_LINE_MAX 1024
652 void cgroup_get_chart_id(struct cgroup *cg) {
653 debug(D_CGROUP, "getting the name of cgroup '%s'", cg->id);
656 char buffer[CGROUP_CHARTID_LINE_MAX + 1];
658 snprintfz(buffer, CGROUP_CHARTID_LINE_MAX, "exec %s '%s'",
659 config_get("plugin:cgroups", "script to get cgroup names", PLUGINS_DIR "/cgroup-name.sh"), cg->chart_id);
661 debug(D_CGROUP, "executing command '%s' for cgroup '%s'", buffer, cg->id);
662 FILE *fp = mypopen(buffer, &cgroup_pid);
664 error("CGROUP: Cannot popen(\"%s\", \"r\").", buffer);
667 debug(D_CGROUP, "reading from command '%s' for cgroup '%s'", buffer, cg->id);
668 char *s = fgets(buffer, CGROUP_CHARTID_LINE_MAX, fp);
669 debug(D_CGROUP, "closing command for cgroup '%s'", cg->id);
670 mypclose(fp, cgroup_pid);
671 debug(D_CGROUP, "closed command for cgroup '%s'", cg->id);
673 if(s && *s && *s != '\n') {
674 debug(D_CGROUP, "cgroup '%s' should be renamed to '%s'", cg->id, s);
678 freez(cg->chart_title);
679 cg->chart_title = strdupz(s);
680 netdata_fix_chart_name(cg->chart_title);
683 cg->chart_id = strdupz(s);
684 netdata_fix_chart_id(cg->chart_id);
685 cg->hash_chart = simple_hash(cg->chart_id);
687 debug(D_CGROUP, "cgroup '%s' renamed to '%s' (title: '%s')", cg->id, cg->chart_id, cg->chart_title);
689 else debug(D_CGROUP, "cgroup '%s' is not to be renamed (will be shown as '%s')", cg->id, cg->chart_id);
692 struct cgroup *cgroup_add(const char *id) {
693 debug(D_CGROUP, "adding cgroup with id '%s'", id);
695 if(cgroup_root_count >= cgroup_root_max) {
696 info("Maximum number of cgroups reached (%d). Not adding cgroup '%s'", cgroup_root_count, id);
700 int def = cgroup_enable_new_cgroups_detected_at_runtime;
701 const char *chart_id = id;
705 // disable by default the root cgroup
709 if(*chart_id == '/') chart_id++;
710 if(netdata_simple_pattern_list_matches(disabled_cgroups_patterns, chart_id))
714 debug(D_CGROUP, "cgroup with id '%s' (chart_id '%s') is (by default) %s", id, chart_id, (def)?"enabled":"disabled");
716 struct cgroup *cg = callocz(1, sizeof(struct cgroup));
718 cg->id = strdupz(id);
719 cg->hash = simple_hash(cg->id);
721 cg->chart_id = strdupz(chart_id);
722 netdata_fix_chart_id(cg->chart_id);
723 cg->hash_chart = simple_hash(cg->chart_id);
725 cg->chart_title = strdupz(chart_id);
732 for(e = cgroup_root; e->next ;e = e->next) ;
738 // fix the name by calling the external script
739 cgroup_get_chart_id(cg);
741 debug(D_CGROUP, "adding cgroup '%s' ('%s') with chart id '%s' ('%s')", cg->id, id, cg->chart_id, chart_id);
743 char option[FILENAME_MAX + 1];
744 snprintfz(option, FILENAME_MAX, "enable cgroup %s", cg->chart_title);
745 cg->enabled = (char)config_get_boolean("plugin:cgroups", option, def);
749 for (t = cgroup_root; t; t = t->next) {
750 if (t != cg && t->enabled && t->hash_chart == cg->hash_chart && !strcmp(t->chart_id, cg->chart_id)) {
751 if (!strncmp(t->chart_id, "/system.slice/", 14) && !strncmp(cg->chart_id, "/init.scope/system.slice/", 25)) {
752 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'.",
753 cg->chart_id, t->id, cg->id, t->id);
754 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'.",
755 cg->chart_id, t->id, cg->id, t->id);
757 t->options |= CGROUP_OPTIONS_DISABLED_DUPLICATE;
760 error("Control group with chart id '%s' already exists with id '%s' and is enabled and available. Disabling cgroup with id '%s'.",
761 cg->chart_id, t->id, cg->id);
762 debug(D_CGROUP, "Control group with chart id '%s' already exists with id '%s' and is enabled and available. Disabling cgroup with id '%s'.",
763 cg->chart_id, t->id, cg->id);
765 cg->options |= CGROUP_OPTIONS_DISABLED_DUPLICATE;
773 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");
778 void cgroup_free(struct cgroup *cg) {
779 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");
781 freez(cg->cpuacct_usage.cpu_percpu);
783 freez(cg->cpuacct_stat.filename);
784 freez(cg->cpuacct_usage.filename);
786 freez(cg->memory.filename);
787 freez(cg->memory.filename_failcnt);
788 freez(cg->memory.filename_usage_in_bytes);
789 freez(cg->memory.filename_msw_usage_in_bytes);
791 freez(cg->io_service_bytes.filename);
792 freez(cg->io_serviced.filename);
794 freez(cg->throttle_io_service_bytes.filename);
795 freez(cg->throttle_io_serviced.filename);
797 freez(cg->io_merged.filename);
798 freez(cg->io_queued.filename);
802 freez(cg->chart_title);
809 // find if a given cgroup exists
810 struct cgroup *cgroup_find(const char *id) {
811 debug(D_CGROUP, "searching for cgroup '%s'", id);
813 uint32_t hash = simple_hash(id);
816 for(cg = cgroup_root; cg ; cg = cg->next) {
817 if(hash == cg->hash && strcmp(id, cg->id) == 0)
821 debug(D_CGROUP, "cgroup_find('%s') %s", id, (cg)?"found":"not found");
825 // ----------------------------------------------------------------------------
826 // detect running cgroups
828 // callback for find_file_in_subdirs()
829 void found_subdir_in_dir(const char *dir) {
830 debug(D_CGROUP, "examining cgroup dir '%s'", dir);
832 struct cgroup *cg = cgroup_find(dir);
834 if(*dir && cgroup_max_depth > 0) {
838 for(s = dir; *s ;s++)
839 if(unlikely(*s == '/'))
842 if(depth > cgroup_max_depth) {
843 info("cgroup '%s' is too deep (%d, while max is %d)", dir, depth, cgroup_max_depth);
847 debug(D_CGROUP, "will add dir '%s' as cgroup", dir);
848 cg = cgroup_add(dir);
851 if(cg) cg->available = 1;
854 int find_dir_in_subdirs(const char *base, const char *this, void (*callback)(const char *)) {
855 debug(D_CGROUP, "searching for directories in '%s'", base);
859 if(!this) this = base;
860 size_t dirlen = strlen(this), baselen = strlen(base);
861 const char *relative_path = &this[baselen];
863 DIR *dir = opendir(this);
865 error("Cannot read cgroups directory '%s'", base);
870 callback(relative_path);
872 struct dirent *de = NULL;
873 while((de = readdir(dir))) {
874 if(de->d_type == DT_DIR
876 (de->d_name[0] == '.' && de->d_name[1] == '\0')
877 || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
881 debug(D_CGROUP, "examining '%s/%s'", this, de->d_name);
883 if(de->d_type == DT_DIR) {
885 const char *r = relative_path;
886 if(*r == '\0') r = "/";
887 else if (*r == '/') r++;
889 // do not decent in directories we are not interested
890 // https://github.com/firehol/netdata/issues/345
892 if(netdata_simple_pattern_list_matches(disabled_cgroup_paths, r))
895 // we check for this option here
896 // so that the config will not have settings
897 // for leaf directories
898 char option[FILENAME_MAX + 1];
899 snprintfz(option, FILENAME_MAX, "search for cgroups under %s", r);
900 option[FILENAME_MAX] = '\0';
901 enabled = config_get_boolean("plugin:cgroups", option, def);
905 char *s = mallocz(dirlen + strlen(de->d_name) + 2);
908 strcat(s, de->d_name);
909 int ret2 = find_dir_in_subdirs(base, s, callback);
910 if(ret2 > 0) ret += ret2;
920 void mark_all_cgroups_as_not_available() {
921 debug(D_CGROUP, "marking all cgroups as not available");
925 // mark all as not available
926 for(cg = cgroup_root; cg ; cg = cg->next) {
931 void cleanup_all_cgroups() {
932 struct cgroup *cg = cgroup_root, *last = NULL;
936 // enable the first duplicate cgroup
939 for(t = cgroup_root; t ; t = t->next) {
940 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)) {
941 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);
943 t->options &= ~CGROUP_OPTIONS_DISABLED_DUPLICATE;
950 cgroup_root = cg->next;
952 last->next = cg->next;
968 void find_all_cgroups() {
969 debug(D_CGROUP, "searching for cgroups");
971 mark_all_cgroups_as_not_available();
973 if(cgroup_enable_cpuacct_stat || cgroup_enable_cpuacct_usage) {
974 if (find_dir_in_subdirs(cgroup_cpuacct_base, NULL, found_subdir_in_dir) == -1) {
975 cgroup_enable_cpuacct_stat = cgroup_enable_cpuacct_usage = 0;
976 error("disabled cgroup cpu statistics.");
980 if(cgroup_enable_blkio) {
981 if (find_dir_in_subdirs(cgroup_blkio_base, NULL, found_subdir_in_dir) == -1) {
982 cgroup_enable_blkio = 0;
983 error("disabled cgroup blkio statistics.");
987 if(cgroup_enable_memory) {
988 if(find_dir_in_subdirs(cgroup_memory_base, NULL, found_subdir_in_dir) == -1) {
989 cgroup_enable_memory = 0;
990 error("disabled cgroup memory statistics.");
994 if(cgroup_enable_devices) {
995 if(find_dir_in_subdirs(cgroup_devices_base, NULL, found_subdir_in_dir) == -1) {
996 cgroup_enable_devices = 0;
997 error("disabled cgroup devices statistics.");
1001 // remove any non-existing cgroups
1002 cleanup_all_cgroups();
1006 for(cg = cgroup_root; cg ; cg = cg->next) {
1007 // fprintf(stderr, " >>> CGROUP '%s' (%u - %s) with name '%s'\n", cg->id, cg->hash, cg->available?"available":"stopped", cg->name);
1009 if(unlikely(!cg->available))
1012 debug(D_CGROUP, "checking paths for cgroup '%s'", cg->id);
1014 // check for newly added cgroups
1015 // and update the filenames they read
1016 char filename[FILENAME_MAX + 1];
1017 if(unlikely(cgroup_enable_cpuacct_stat && !cg->cpuacct_stat.filename)) {
1018 snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.stat", cgroup_cpuacct_base, cg->id);
1019 if(stat(filename, &buf) != -1) {
1020 cg->cpuacct_stat.filename = strdupz(filename);
1021 debug(D_CGROUP, "cpuacct.stat filename for cgroup '%s': '%s'", cg->id, cg->cpuacct_stat.filename);
1023 else debug(D_CGROUP, "cpuacct.stat file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1026 if(unlikely(cgroup_enable_cpuacct_usage && !cg->cpuacct_usage.filename)) {
1027 snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.usage_percpu", cgroup_cpuacct_base, cg->id);
1028 if(stat(filename, &buf) != -1) {
1029 cg->cpuacct_usage.filename = strdupz(filename);
1030 debug(D_CGROUP, "cpuacct.usage_percpu filename for cgroup '%s': '%s'", cg->id, cg->cpuacct_usage.filename);
1032 else debug(D_CGROUP, "cpuacct.usage_percpu file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1035 if(unlikely(cgroup_enable_memory)) {
1036 if(unlikely(!cg->memory.filename)) {
1037 snprintfz(filename, FILENAME_MAX, "%s%s/memory.stat", cgroup_memory_base, cg->id);
1038 if(stat(filename, &buf) != -1) {
1039 cg->memory.filename = strdupz(filename);
1040 debug(D_CGROUP, "memory.stat filename for cgroup '%s': '%s'", cg->id, cg->memory.filename);
1043 debug(D_CGROUP, "memory.stat file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1045 if(unlikely(!cg->memory.filename_usage_in_bytes)) {
1046 snprintfz(filename, FILENAME_MAX, "%s%s/memory.usage_in_bytes", cgroup_memory_base, cg->id);
1047 if(stat(filename, &buf) != -1) {
1048 cg->memory.filename_usage_in_bytes = strdupz(filename);
1049 debug(D_CGROUP, "memory.usage_in_bytes filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_usage_in_bytes);
1052 debug(D_CGROUP, "memory.usage_in_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1054 if(unlikely(!cg->memory.filename_msw_usage_in_bytes)) {
1055 snprintfz(filename, FILENAME_MAX, "%s%s/memory.msw_usage_in_bytes", cgroup_memory_base, cg->id);
1056 if(stat(filename, &buf) != -1) {
1057 cg->memory.filename_msw_usage_in_bytes = strdupz(filename);
1058 debug(D_CGROUP, "memory.msw_usage_in_bytes filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_msw_usage_in_bytes);
1061 debug(D_CGROUP, "memory.msw_usage_in_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1063 if(unlikely(!cg->memory.filename_failcnt)) {
1064 snprintfz(filename, FILENAME_MAX, "%s%s/memory.failcnt", cgroup_memory_base, cg->id);
1065 if(stat(filename, &buf) != -1) {
1066 cg->memory.filename_failcnt = strdupz(filename);
1067 debug(D_CGROUP, "memory.failcnt filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_failcnt);
1070 debug(D_CGROUP, "memory.failcnt file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1074 if(unlikely(cgroup_enable_blkio)) {
1075 if(unlikely(!cg->io_service_bytes.filename)) {
1076 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_service_bytes", cgroup_blkio_base, cg->id);
1077 if(stat(filename, &buf) != -1) {
1078 cg->io_service_bytes.filename = strdupz(filename);
1079 debug(D_CGROUP, "io_service_bytes filename for cgroup '%s': '%s'", cg->id, cg->io_service_bytes.filename);
1081 else debug(D_CGROUP, "io_service_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1083 if(unlikely(!cg->io_serviced.filename)) {
1084 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_serviced", cgroup_blkio_base, cg->id);
1085 if(stat(filename, &buf) != -1) {
1086 cg->io_serviced.filename = strdupz(filename);
1087 debug(D_CGROUP, "io_serviced filename for cgroup '%s': '%s'", cg->id, cg->io_serviced.filename);
1089 else debug(D_CGROUP, "io_serviced file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1091 if(unlikely(!cg->throttle_io_service_bytes.filename)) {
1092 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.throttle.io_service_bytes", cgroup_blkio_base, cg->id);
1093 if(stat(filename, &buf) != -1) {
1094 cg->throttle_io_service_bytes.filename = strdupz(filename);
1095 debug(D_CGROUP, "throttle_io_service_bytes filename for cgroup '%s': '%s'", cg->id, cg->throttle_io_service_bytes.filename);
1097 else debug(D_CGROUP, "throttle_io_service_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1099 if(unlikely(!cg->throttle_io_serviced.filename)) {
1100 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.throttle.io_serviced", cgroup_blkio_base, cg->id);
1101 if(stat(filename, &buf) != -1) {
1102 cg->throttle_io_serviced.filename = strdupz(filename);
1103 debug(D_CGROUP, "throttle_io_serviced filename for cgroup '%s': '%s'", cg->id, cg->throttle_io_serviced.filename);
1105 else debug(D_CGROUP, "throttle_io_serviced file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1107 if(unlikely(!cg->io_merged.filename)) {
1108 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_merged", cgroup_blkio_base, cg->id);
1109 if(stat(filename, &buf) != -1) {
1110 cg->io_merged.filename = strdupz(filename);
1111 debug(D_CGROUP, "io_merged filename for cgroup '%s': '%s'", cg->id, cg->io_merged.filename);
1113 else debug(D_CGROUP, "io_merged file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1115 if(unlikely(!cg->io_queued.filename)) {
1116 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_queued", cgroup_blkio_base, cg->id);
1117 if(stat(filename, &buf) != -1) {
1118 cg->io_queued.filename = strdupz(filename);
1119 debug(D_CGROUP, "io_queued filename for cgroup '%s': '%s'", cg->id, cg->io_queued.filename);
1121 else debug(D_CGROUP, "io_queued file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1126 debug(D_CGROUP, "done searching for cgroups");
1130 // ----------------------------------------------------------------------------
1133 #define CHART_TITLE_MAX 300
1135 void update_cgroup_charts(int update_every) {
1136 debug(D_CGROUP, "updating cgroups charts");
1138 char type[RRD_ID_LENGTH_MAX + 1];
1139 char title[CHART_TITLE_MAX + 1];
1144 for(cg = cgroup_root; cg ; cg = cg->next) {
1145 if(!cg->available || !cg->enabled || cg->options & CGROUP_OPTIONS_PART_OF_GROUP)
1148 if(cg->id[0] == '\0')
1149 strcpy(type, "cgroup_root");
1150 //else if(cg->id[0] == '/')
1151 // snprintfz(type, RRD_ID_LENGTH_MAX, "cgroup_%s", cg->chart_id);
1153 snprintfz(type, RRD_ID_LENGTH_MAX, "cgroup_%s", cg->chart_id);
1155 netdata_fix_chart_id(type);
1157 if(cg->cpuacct_stat.updated) {
1158 st = rrdset_find_bytype(type, "cpu");
1160 snprintfz(title, CHART_TITLE_MAX, "CPU Usage (%d%% = %d core%s) for cgroup %s", (processors * 100), processors, (processors>1)?"s":"", cg->chart_title);
1161 st = rrdset_create(type, "cpu", NULL, "cpu", "cgroup.cpu", title, "%", 40000, update_every, RRDSET_TYPE_STACKED);
1163 rrddim_add(st, "user", NULL, 100, hz, RRDDIM_INCREMENTAL);
1164 rrddim_add(st, "system", NULL, 100, hz, RRDDIM_INCREMENTAL);
1166 else rrdset_next(st);
1168 rrddim_set(st, "user", cg->cpuacct_stat.user);
1169 rrddim_set(st, "system", cg->cpuacct_stat.system);
1173 if(cg->cpuacct_usage.updated) {
1174 char id[RRD_ID_LENGTH_MAX + 1];
1177 st = rrdset_find_bytype(type, "cpu_per_core");
1179 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);
1180 st = rrdset_create(type, "cpu_per_core", NULL, "cpu", "cgroup.cpu_per_core", title, "%", 40100, update_every, RRDSET_TYPE_STACKED);
1182 for(i = 0; i < cg->cpuacct_usage.cpus ;i++) {
1183 snprintfz(id, CHART_TITLE_MAX, "cpu%u", i);
1184 rrddim_add(st, id, NULL, 100, 1000000000, RRDDIM_INCREMENTAL);
1187 else rrdset_next(st);
1189 for(i = 0; i < cg->cpuacct_usage.cpus ;i++) {
1190 snprintfz(id, CHART_TITLE_MAX, "cpu%u", i);
1191 rrddim_set(st, id, cg->cpuacct_usage.cpu_percpu[i]);
1196 if(cg->memory.updated) {
1197 if(cg->memory.cache + cg->memory.rss + cg->memory.rss_huge + cg->memory.mapped_file > 0) {
1198 st = rrdset_find_bytype(type, "mem");
1200 snprintfz(title, CHART_TITLE_MAX, "Memory Usage for cgroup %s", cg->chart_title);
1201 st = rrdset_create(type, "mem", NULL, "mem", "cgroup.mem", title, "MB", 40210, update_every,
1202 RRDSET_TYPE_STACKED);
1204 rrddim_add(st, "cache", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1205 rrddim_add(st, "rss", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1206 if(cg->memory.has_dirty_swap)
1207 rrddim_add(st, "swap", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1208 rrddim_add(st, "rss_huge", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1209 rrddim_add(st, "mapped_file", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1211 else rrdset_next(st);
1213 rrddim_set(st, "cache", cg->memory.cache);
1214 rrddim_set(st, "rss", cg->memory.rss);
1215 if(cg->memory.has_dirty_swap)
1216 rrddim_set(st, "swap", cg->memory.swap);
1217 rrddim_set(st, "rss_huge", cg->memory.rss_huge);
1218 rrddim_set(st, "mapped_file", cg->memory.mapped_file);
1222 st = rrdset_find_bytype(type, "writeback");
1224 snprintfz(title, CHART_TITLE_MAX, "Writeback Memory for cgroup %s", cg->chart_title);
1225 st = rrdset_create(type, "writeback", NULL, "mem", "cgroup.writeback", title, "MB", 40300,
1226 update_every, RRDSET_TYPE_AREA);
1228 if(cg->memory.has_dirty_swap)
1229 rrddim_add(st, "dirty", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1230 rrddim_add(st, "writeback", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1232 else rrdset_next(st);
1234 if(cg->memory.has_dirty_swap)
1235 rrddim_set(st, "dirty", cg->memory.dirty);
1236 rrddim_set(st, "writeback", cg->memory.writeback);
1239 if(cg->memory.pgpgin + cg->memory.pgpgout > 0) {
1240 st = rrdset_find_bytype(type, "mem_activity");
1242 snprintfz(title, CHART_TITLE_MAX, "Memory Activity for cgroup %s", cg->chart_title);
1243 st = rrdset_create(type, "mem_activity", NULL, "mem", "cgroup.mem_activity", title, "MB/s",
1244 40400, update_every, RRDSET_TYPE_LINE);
1246 rrddim_add(st, "pgpgin", "in", sysconf(_SC_PAGESIZE), 1024 * 1024, RRDDIM_INCREMENTAL);
1247 rrddim_add(st, "pgpgout", "out", -sysconf(_SC_PAGESIZE), 1024 * 1024, RRDDIM_INCREMENTAL);
1249 else rrdset_next(st);
1251 rrddim_set(st, "pgpgin", cg->memory.pgpgin);
1252 rrddim_set(st, "pgpgout", cg->memory.pgpgout);
1256 if(cg->memory.pgfault + cg->memory.pgmajfault > 0) {
1257 st = rrdset_find_bytype(type, "pgfaults");
1259 snprintfz(title, CHART_TITLE_MAX, "Memory Page Faults for cgroup %s", cg->chart_title);
1260 st = rrdset_create(type, "pgfaults", NULL, "mem", "cgroup.pgfaults", title, "MB/s", 40500,
1261 update_every, RRDSET_TYPE_LINE);
1263 rrddim_add(st, "pgfault", NULL, sysconf(_SC_PAGESIZE), 1024 * 1024, RRDDIM_INCREMENTAL);
1264 rrddim_add(st, "pgmajfault", "swap", -sysconf(_SC_PAGESIZE), 1024 * 1024, RRDDIM_INCREMENTAL);
1266 else rrdset_next(st);
1268 rrddim_set(st, "pgfault", cg->memory.pgfault);
1269 rrddim_set(st, "pgmajfault", cg->memory.pgmajfault);
1274 if(cg->memory.usage_in_bytes_updated) {
1275 st = rrdset_find_bytype(type, "mem_usage");
1277 snprintfz(title, CHART_TITLE_MAX, "Total Memory for cgroup %s", cg->chart_title);
1278 st = rrdset_create(type, "mem_usage", NULL, "mem", "cgroup.mem_usage", title, "MB", 40200,
1279 update_every, RRDSET_TYPE_STACKED);
1281 rrddim_add(st, "ram", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1282 rrddim_add(st, "swap", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1284 else rrdset_next(st);
1286 rrddim_set(st, "ram", cg->memory.usage_in_bytes);
1287 rrddim_set(st, "swap", (cg->memory.msw_usage_in_bytes > cg->memory.usage_in_bytes)?cg->memory.msw_usage_in_bytes - cg->memory.usage_in_bytes:0);
1291 if(cg->memory.failcnt_updated && cg->memory.failcnt > 0) {
1292 st = rrdset_find_bytype(type, "mem_failcnt");
1294 snprintfz(title, CHART_TITLE_MAX, "Memory Limit Failures for cgroup %s", cg->chart_title);
1295 st = rrdset_create(type, "mem_failcnt", NULL, "mem", "cgroup.mem_failcnt", title, "MB", 40250,
1296 update_every, RRDSET_TYPE_LINE);
1298 rrddim_add(st, "failures", NULL, 1, 1, RRDDIM_INCREMENTAL);
1300 else rrdset_next(st);
1302 rrddim_set(st, "failures", cg->memory.failcnt);
1306 if(cg->io_service_bytes.updated && cg->io_service_bytes.Read + cg->io_service_bytes.Write > 0) {
1307 st = rrdset_find_bytype(type, "io");
1309 snprintfz(title, CHART_TITLE_MAX, "I/O Bandwidth (all disks) for cgroup %s", cg->chart_title);
1310 st = rrdset_create(type, "io", NULL, "disk", "cgroup.io", title, "KB/s", 41200,
1311 update_every, RRDSET_TYPE_AREA);
1313 rrddim_add(st, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
1314 rrddim_add(st, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
1316 else rrdset_next(st);
1318 rrddim_set(st, "read", cg->io_service_bytes.Read);
1319 rrddim_set(st, "write", cg->io_service_bytes.Write);
1323 if(cg->io_serviced.updated && cg->io_serviced.Read + cg->io_serviced.Write > 0) {
1324 st = rrdset_find_bytype(type, "serviced_ops");
1326 snprintfz(title, CHART_TITLE_MAX, "Serviced I/O Operations (all disks) for cgroup %s", cg->chart_title);
1327 st = rrdset_create(type, "serviced_ops", NULL, "disk", "cgroup.serviced_ops", title, "operations/s", 41200,
1328 update_every, RRDSET_TYPE_LINE);
1330 rrddim_add(st, "read", NULL, 1, 1, RRDDIM_INCREMENTAL);
1331 rrddim_add(st, "write", NULL, -1, 1, RRDDIM_INCREMENTAL);
1333 else rrdset_next(st);
1335 rrddim_set(st, "read", cg->io_serviced.Read);
1336 rrddim_set(st, "write", cg->io_serviced.Write);
1340 if(cg->throttle_io_service_bytes.updated && cg->throttle_io_service_bytes.Read + cg->throttle_io_service_bytes.Write > 0) {
1341 st = rrdset_find_bytype(type, "throttle_io");
1343 snprintfz(title, CHART_TITLE_MAX, "Throttle I/O Bandwidth (all disks) for cgroup %s", cg->chart_title);
1344 st = rrdset_create(type, "throttle_io", NULL, "disk", "cgroup.throttle_io", title, "KB/s", 41200,
1345 update_every, RRDSET_TYPE_AREA);
1347 rrddim_add(st, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
1348 rrddim_add(st, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
1350 else rrdset_next(st);
1352 rrddim_set(st, "read", cg->throttle_io_service_bytes.Read);
1353 rrddim_set(st, "write", cg->throttle_io_service_bytes.Write);
1358 if(cg->throttle_io_serviced.updated && cg->throttle_io_serviced.Read + cg->throttle_io_serviced.Write > 0) {
1359 st = rrdset_find_bytype(type, "throttle_serviced_ops");
1361 snprintfz(title, CHART_TITLE_MAX, "Throttle Serviced I/O Operations (all disks) for cgroup %s", cg->chart_title);
1362 st = rrdset_create(type, "throttle_serviced_ops", NULL, "disk", "cgroup.throttle_serviced_ops", title, "operations/s", 41200,
1363 update_every, RRDSET_TYPE_LINE);
1365 rrddim_add(st, "read", NULL, 1, 1, RRDDIM_INCREMENTAL);
1366 rrddim_add(st, "write", NULL, -1, 1, RRDDIM_INCREMENTAL);
1368 else rrdset_next(st);
1370 rrddim_set(st, "read", cg->throttle_io_serviced.Read);
1371 rrddim_set(st, "write", cg->throttle_io_serviced.Write);
1375 if(cg->io_queued.updated) {
1376 st = rrdset_find_bytype(type, "queued_ops");
1378 snprintfz(title, CHART_TITLE_MAX, "Queued I/O Operations (all disks) for cgroup %s", cg->chart_title);
1379 st = rrdset_create(type, "queued_ops", NULL, "disk", "cgroup.queued_ops", title, "operations", 42000,
1380 update_every, RRDSET_TYPE_LINE);
1382 rrddim_add(st, "read", NULL, 1, 1, RRDDIM_ABSOLUTE);
1383 rrddim_add(st, "write", NULL, -1, 1, RRDDIM_ABSOLUTE);
1385 else rrdset_next(st);
1387 rrddim_set(st, "read", cg->io_queued.Read);
1388 rrddim_set(st, "write", cg->io_queued.Write);
1392 if(cg->io_merged.updated && cg->io_merged.Read + cg->io_merged.Write > 0) {
1393 st = rrdset_find_bytype(type, "merged_ops");
1395 snprintfz(title, CHART_TITLE_MAX, "Merged I/O Operations (all disks) for cgroup %s", cg->chart_title);
1396 st = rrdset_create(type, "merged_ops", NULL, "disk", "cgroup.merged_ops", title, "operations/s", 42100,
1397 update_every, RRDSET_TYPE_LINE);
1399 rrddim_add(st, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
1400 rrddim_add(st, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
1402 else rrdset_next(st);
1404 rrddim_set(st, "read", cg->io_merged.Read);
1405 rrddim_set(st, "write", cg->io_merged.Write);
1410 debug(D_CGROUP, "done updating cgroups charts");
1413 // ----------------------------------------------------------------------------
1416 int do_sys_fs_cgroup(int update_every, usec_t dt) {
1419 static int cgroup_global_config_read = 0;
1420 static time_t last_run = 0;
1421 time_t now = now_realtime_sec();
1423 if(unlikely(!cgroup_global_config_read)) {
1424 read_cgroup_plugin_configuration();
1425 cgroup_global_config_read = 1;
1428 if(unlikely(cgroup_enable_new_cgroups_detected_at_runtime && now - last_run > cgroup_check_for_new_every)) {
1433 read_all_cgroups(cgroup_root);
1434 update_cgroup_charts(update_every);
1439 void *cgroups_main(void *ptr) {
1440 struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
1442 info("CGROUP Plugin thread created with task id %d", gettid());
1444 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
1445 error("Cannot set pthread cancel type to DEFERRED.");
1447 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
1448 error("Cannot set pthread cancel state to ENABLE.");
1450 struct rusage thread;
1452 // when ZERO, attempt to do it
1453 int vdo_sys_fs_cgroup = 0;
1454 int vdo_cpu_netdata = !config_get_boolean("plugin:cgroups", "cgroups plugin resources", 1);
1456 // keep track of the time each module was called
1457 usec_t sutime_sys_fs_cgroup = 0ULL;
1459 // the next time we will run - aligned properly
1460 usec_t sunext = (now_realtime_sec() - (now_realtime_sec() % rrd_update_every) + rrd_update_every) * USEC_PER_SEC;
1462 RRDSET *stcpu_thread = NULL;
1466 if(unlikely(netdata_exit)) break;
1468 // delay until it is our time to run
1469 while((sunow = now_realtime_usec()) < sunext)
1470 sleep_usec(sunext - sunow);
1472 // find the next time we need to run
1473 while(now_realtime_usec() > sunext)
1474 sunext += rrd_update_every * USEC_PER_SEC;
1476 if(unlikely(netdata_exit)) break;
1478 // BEGIN -- the job to be done
1480 if(!vdo_sys_fs_cgroup) {
1481 debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_sys_fs_cgroup().");
1482 sunow = now_realtime_usec();
1483 vdo_sys_fs_cgroup = do_sys_fs_cgroup(rrd_update_every, (sutime_sys_fs_cgroup > 0)?sunow - sutime_sys_fs_cgroup:0ULL);
1484 sutime_sys_fs_cgroup = sunow;
1486 if(unlikely(netdata_exit)) break;
1488 // END -- the job is done
1490 // --------------------------------------------------------------------
1492 if(!vdo_cpu_netdata) {
1493 getrusage(RUSAGE_THREAD, &thread);
1495 if(!stcpu_thread) stcpu_thread = rrdset_find("netdata.plugin_cgroups_cpu");
1497 stcpu_thread = rrdset_create("netdata", "plugin_cgroups_cpu", NULL, "cgroups", NULL, "NetData CGroups Plugin CPU usage", "milliseconds/s", 132000, rrd_update_every, RRDSET_TYPE_STACKED);
1499 rrddim_add(stcpu_thread, "user", NULL, 1, 1000, RRDDIM_INCREMENTAL);
1500 rrddim_add(stcpu_thread, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
1502 else rrdset_next(stcpu_thread);
1504 rrddim_set(stcpu_thread, "user" , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
1505 rrddim_set(stcpu_thread, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
1506 rrdset_done(stcpu_thread);
1510 info("CGROUP thread exiting");
1512 static_thread->enabled = 0;
1513 static_thread->thread = NULL;