]> arthur.barton.de Git - netdata.git/blob - src/sys_fs_cgroup.c
updated cgroups default configuation
[netdata.git] / src / sys_fs_cgroup.c
1 #include "common.h"
2
3 // ----------------------------------------------------------------------------
4 // cgroup globals
5
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_systemd_services = CONFIG_ONDEMAND_NO;
12 static int cgroup_enable_new_cgroups_detected_at_runtime = 1;
13 static int cgroup_check_for_new_every = 10;
14 static int cgroup_update_every = 1;
15 static char *cgroup_cpuacct_base = NULL;
16 static char *cgroup_blkio_base = NULL;
17 static char *cgroup_memory_base = NULL;
18 static char *cgroup_devices_base = NULL;
19
20 static int cgroup_root_count = 0;
21 static int cgroup_root_max = 500;
22 static int cgroup_max_depth = 0;
23
24 static NETDATA_SIMPLE_PATTERN *disabled_cgroups_patterns = NULL;
25 static NETDATA_SIMPLE_PATTERN *disabled_cgroup_paths = NULL;
26 static NETDATA_SIMPLE_PATTERN *disabled_cgroup_renames = NULL;
27 static NETDATA_SIMPLE_PATTERN *systemd_services_cgroups = NULL;
28
29 static char *cgroups_rename_script = PLUGINS_DIR "/cgroup-name.sh";
30
31 void read_cgroup_plugin_configuration() {
32     cgroup_update_every = (int)config_get_number("plugin:cgroups", "update every", rrd_update_every);
33     if(cgroup_update_every < rrd_update_every)
34         cgroup_update_every = rrd_update_every;
35
36     cgroup_check_for_new_every = (int)config_get_number("plugin:cgroups", "check for new cgroups every", cgroup_check_for_new_every * cgroup_update_every);
37     if(cgroup_check_for_new_every < cgroup_update_every)
38         cgroup_check_for_new_every = cgroup_update_every;
39
40     cgroup_enable_cpuacct_stat = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct stat", cgroup_enable_cpuacct_stat);
41     cgroup_enable_cpuacct_usage = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct usage", cgroup_enable_cpuacct_usage);
42     cgroup_enable_memory = config_get_boolean_ondemand("plugin:cgroups", "enable memory", cgroup_enable_memory);
43     cgroup_enable_blkio = config_get_boolean_ondemand("plugin:cgroups", "enable blkio", cgroup_enable_blkio);
44
45     cgroup_enable_systemd_services = config_get_boolean_ondemand("plugin:cgroups", "enable systemd services", cgroup_enable_systemd_services);
46
47     char filename[FILENAME_MAX + 1], *s;
48     struct mountinfo *mi, *root = mountinfo_read(0);
49
50     mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "cpuacct");
51     if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "cpuacct");
52     if(!mi) {
53         error("Cannot find cgroup cpuacct mountinfo. Assuming default: /sys/fs/cgroup/cpuacct");
54         s = "/sys/fs/cgroup/cpuacct";
55     }
56     else s = mi->mount_point;
57     snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
58     cgroup_cpuacct_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/cpuacct", filename);
59
60     mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "blkio");
61     if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "blkio");
62     if(!mi) {
63         error("Cannot find cgroup blkio mountinfo. Assuming default: /sys/fs/cgroup/blkio");
64         s = "/sys/fs/cgroup/blkio";
65     }
66     else s = mi->mount_point;
67     snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
68     cgroup_blkio_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/blkio", filename);
69
70     mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "memory");
71     if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "memory");
72     if(!mi) {
73         error("Cannot find cgroup memory mountinfo. Assuming default: /sys/fs/cgroup/memory");
74         s = "/sys/fs/cgroup/memory";
75     }
76     else s = mi->mount_point;
77     snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
78     cgroup_memory_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/memory", filename);
79
80     mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "devices");
81     if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "devices");
82     if(!mi) {
83         error("Cannot find cgroup devices mountinfo. Assuming default: /sys/fs/cgroup/devices");
84         s = "/sys/fs/cgroup/devices";
85     }
86     else s = mi->mount_point;
87     snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
88     cgroup_devices_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/devices", filename);
89
90     cgroup_root_max = (int)config_get_number("plugin:cgroups", "max cgroups to allow", cgroup_root_max);
91     cgroup_max_depth = (int)config_get_number("plugin:cgroups", "max cgroups depth to monitor", cgroup_max_depth);
92
93     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);
94
95     disabled_cgroups_patterns = netdata_simple_pattern_list_create(
96             config_get("plugin:cgroups", "disable by default cgroups matching",
97                     " *.mount "
98                     " *.partition "
99                     " *.service "
100                     " *.slice "
101                     " *.swap "
102                     " *.user "
103                     " / "
104                     " /docker "
105                     " /init.scope "
106                     " /libvirt "
107                     " /lxc "
108                     " /lxc/*/ns "                          //  #1397
109                     " /machine "
110                     " /machine.slice "
111                     " /qemu "
112                     " /system "
113                     " /system.slice "
114                     " /systemd "
115                     " /user "
116                     " /user.slice "
117             ), NETDATA_SIMPLE_PATTERN_MODE_EXACT);
118
119     disabled_cgroup_paths = netdata_simple_pattern_list_create(
120             config_get("plugin:cgroups", "do not search for cgroups in paths matching",
121                     " *-qemu "                             //  #345
122                     " /init.scope "
123                     " /system "
124                     " /systemd "
125                     " /user "
126                     " /user.slice "
127             ), NETDATA_SIMPLE_PATTERN_MODE_EXACT);
128
129     cgroups_rename_script = config_get("plugin:cgroups", "script to get cgroup names", cgroups_rename_script);
130
131     disabled_cgroup_renames = netdata_simple_pattern_list_create(
132             config_get("plugin:cgroups", "do not run script to rename cgroups matching",
133                     " / "
134                     " *.mount "
135                     " *.partition "
136                     " *.scope "
137                     " *.service "
138                     " *.slice "
139                     " *.swap "
140                     " *.user "
141             ), NETDATA_SIMPLE_PATTERN_MODE_EXACT);
142
143     if(cgroup_enable_systemd_services)
144         systemd_services_cgroups = netdata_simple_pattern_list_create(
145                 config_get("plugin:cgroups", "cgroups to match as systemd services",
146                         " /system.slice/*.service "
147                 ), NETDATA_SIMPLE_PATTERN_MODE_EXACT);
148
149     mountinfo_free(root);
150 }
151
152 // ----------------------------------------------------------------------------
153 // cgroup objects
154
155 struct blkio {
156     int updated;
157
158     char *filename;
159
160     unsigned long long Read;
161     unsigned long long Write;
162 /*
163     unsigned long long Sync;
164     unsigned long long Async;
165     unsigned long long Total;
166 */
167 };
168
169 // https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt
170 struct memory {
171     int updated;
172
173     char *filename;
174
175     int has_dirty_swap;
176
177     unsigned long long cache;
178     unsigned long long rss;
179     unsigned long long rss_huge;
180     unsigned long long mapped_file;
181     unsigned long long writeback;
182     unsigned long long dirty;
183     unsigned long long swap;
184     unsigned long long pgpgin;
185     unsigned long long pgpgout;
186     unsigned long long pgfault;
187     unsigned long long pgmajfault;
188 /*
189     unsigned long long inactive_anon;
190     unsigned long long active_anon;
191     unsigned long long inactive_file;
192     unsigned long long active_file;
193     unsigned long long unevictable;
194     unsigned long long hierarchical_memory_limit;
195     unsigned long long total_cache;
196     unsigned long long total_rss;
197     unsigned long long total_rss_huge;
198     unsigned long long total_mapped_file;
199     unsigned long long total_writeback;
200     unsigned long long total_dirty;
201     unsigned long long total_swap;
202     unsigned long long total_pgpgin;
203     unsigned long long total_pgpgout;
204     unsigned long long total_pgfault;
205     unsigned long long total_pgmajfault;
206     unsigned long long total_inactive_anon;
207     unsigned long long total_active_anon;
208     unsigned long long total_inactive_file;
209     unsigned long long total_active_file;
210     unsigned long long total_unevictable;
211 */
212
213     int usage_in_bytes_updated;
214     char *filename_usage_in_bytes;
215     unsigned long long usage_in_bytes;
216
217     int msw_usage_in_bytes_updated;
218     char *filename_msw_usage_in_bytes;
219     unsigned long long msw_usage_in_bytes;
220
221     int failcnt_updated;
222     char *filename_failcnt;
223     unsigned long long failcnt;
224 };
225
226 // https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt
227 struct cpuacct_stat {
228     int updated;
229
230     char *filename;
231
232     unsigned long long user;
233     unsigned long long system;
234 };
235
236 // https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt
237 struct cpuacct_usage {
238     int updated;
239
240     char *filename;
241
242     unsigned int cpus;
243     unsigned long long *cpu_percpu;
244 };
245
246 #define CGROUP_OPTIONS_DISABLED_DUPLICATE   0x00000001
247 #define CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE 0x00000002
248
249 struct cgroup {
250     uint32_t options;
251
252     char available;      // found in the filesystem
253     char enabled;        // enabled in the config
254
255     char *id;
256     uint32_t hash;
257
258     char *chart_id;
259     uint32_t hash_chart;
260
261     char *chart_title;
262
263     struct cpuacct_stat cpuacct_stat;
264     struct cpuacct_usage cpuacct_usage;
265
266     struct memory memory;
267
268     struct blkio io_service_bytes;              // bytes
269     struct blkio io_serviced;                   // operations
270
271     struct blkio throttle_io_service_bytes;     // bytes
272     struct blkio throttle_io_serviced;          // operations
273
274     struct blkio io_merged;                     // operations
275     struct blkio io_queued;                     // operations
276
277     RRDSET *st_cpu;
278     RRDSET *st_cpu_per_core;
279     RRDSET *st_mem;
280     RRDSET *st_writeback;
281     RRDSET *st_mem_activity;
282     RRDSET *st_pgfaults;
283     RRDSET *st_mem_usage;
284     RRDSET *st_mem_failcnt;
285     RRDSET *st_io;
286     RRDSET *st_serviced_ops;
287     RRDSET *st_throttle_io;
288     RRDSET *st_throttle_serviced_ops;
289     RRDSET *st_queued_ops;
290     RRDSET *st_merged_ops;
291
292     struct cgroup *next;
293
294 } *cgroup_root = NULL;
295
296 // ----------------------------------------------------------------------------
297 // read values from /sys
298
299 void cgroup_read_cpuacct_stat(struct cpuacct_stat *cp) {
300     static procfile *ff = NULL;
301
302     static uint32_t user_hash = 0;
303     static uint32_t system_hash = 0;
304
305     if(unlikely(user_hash == 0)) {
306         user_hash = simple_hash("user");
307         system_hash = simple_hash("system");
308     }
309
310     cp->updated = 0;
311     if(cp->filename) {
312         ff = procfile_reopen(ff, cp->filename, NULL, PROCFILE_FLAG_DEFAULT);
313         if(!ff) return;
314
315         ff = procfile_readall(ff);
316         if(!ff) return;
317
318         unsigned long i, lines = procfile_lines(ff);
319
320         if(lines < 1) {
321             error("File '%s' should have 1+ lines.", cp->filename);
322             return;
323         }
324
325         for(i = 0; i < lines ; i++) {
326             char *s = procfile_lineword(ff, i, 0);
327             uint32_t hash = simple_hash(s);
328
329             if(hash == user_hash && !strcmp(s, "user"))
330                 cp->user = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
331
332             else if(hash == system_hash && !strcmp(s, "system"))
333                 cp->system = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
334         }
335
336         cp->updated = 1;
337
338         // fprintf(stderr, "READ '%s': user: %llu, system: %llu\n", cp->filename, cp->user, cp->system);
339     }
340 }
341
342 void cgroup_read_cpuacct_usage(struct cpuacct_usage *ca) {
343     static procfile *ff = NULL;
344
345     ca->updated = 0;
346     if(ca->filename) {
347         ff = procfile_reopen(ff, ca->filename, NULL, PROCFILE_FLAG_DEFAULT);
348         if(!ff) return;
349
350         ff = procfile_readall(ff);
351         if(!ff) return;
352
353         if(procfile_lines(ff) < 1) {
354             error("File '%s' should have 1+ lines but has %u.", ca->filename, procfile_lines(ff));
355             return;
356         }
357
358         unsigned long i = procfile_linewords(ff, 0);
359         if(i == 0) return;
360
361         // we may have 1 more CPU reported
362         while(i > 0) {
363             char *s = procfile_lineword(ff, 0, i - 1);
364             if(!*s) i--;
365             else break;
366         }
367
368         if(i != ca->cpus) {
369             freez(ca->cpu_percpu);
370             ca->cpu_percpu = mallocz(sizeof(unsigned long long) * i);
371             ca->cpus = (unsigned int)i;
372         }
373
374         for(i = 0; i < ca->cpus ;i++) {
375             ca->cpu_percpu[i] = strtoull(procfile_lineword(ff, 0, i), NULL, 10);
376             // fprintf(stderr, "READ '%s': cpu%d/%d: %llu ('%s')\n", ca->filename, i, ca->cpus, ca->cpu_percpu[i], procfile_lineword(ff, 0, i));
377         }
378
379         ca->updated = 1;
380     }
381 }
382
383 void cgroup_read_blkio(struct blkio *io) {
384     static procfile *ff = NULL;
385
386     static uint32_t Read_hash = 0;
387     static uint32_t Write_hash = 0;
388 /*
389     static uint32_t Sync_hash = 0;
390     static uint32_t Async_hash = 0;
391     static uint32_t Total_hash = 0;
392 */
393
394     if(unlikely(Read_hash == 0)) {
395         Read_hash = simple_hash("Read");
396         Write_hash = simple_hash("Write");
397 /*
398         Sync_hash = simple_hash("Sync");
399         Async_hash = simple_hash("Async");
400         Total_hash = simple_hash("Total");
401 */
402     }
403
404     io->updated = 0;
405     if(io->filename) {
406         ff = procfile_reopen(ff, io->filename, NULL, PROCFILE_FLAG_DEFAULT);
407         if(!ff) return;
408
409         ff = procfile_readall(ff);
410         if(!ff) return;
411
412         unsigned long i, lines = procfile_lines(ff);
413
414         if(lines < 1) {
415             error("File '%s' should have 1+ lines.", io->filename);
416             return;
417         }
418
419         io->Read = 0;
420         io->Write = 0;
421 /*
422         io->Sync = 0;
423         io->Async = 0;
424         io->Total = 0;
425 */
426
427         for(i = 0; i < lines ; i++) {
428             char *s = procfile_lineword(ff, i, 1);
429             uint32_t hash = simple_hash(s);
430
431             if(hash == Read_hash && !strcmp(s, "Read"))
432                 io->Read += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
433
434             else if(hash == Write_hash && !strcmp(s, "Write"))
435                 io->Write += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
436
437 /*
438             else if(hash == Sync_hash && !strcmp(s, "Sync"))
439                 io->Sync += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
440
441             else if(hash == Async_hash && !strcmp(s, "Async"))
442                 io->Async += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
443
444             else if(hash == Total_hash && !strcmp(s, "Total"))
445                 io->Total += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
446 */
447         }
448
449         io->updated = 1;
450         // 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);
451     }
452 }
453
454 void cgroup_read_memory(struct memory *mem) {
455     static procfile *ff = NULL;
456
457     static uint32_t cache_hash = 0;
458     static uint32_t rss_hash = 0;
459     static uint32_t rss_huge_hash = 0;
460     static uint32_t mapped_file_hash = 0;
461     static uint32_t writeback_hash = 0;
462     static uint32_t dirty_hash = 0;
463     static uint32_t swap_hash = 0;
464     static uint32_t pgpgin_hash = 0;
465     static uint32_t pgpgout_hash = 0;
466     static uint32_t pgfault_hash = 0;
467     static uint32_t pgmajfault_hash = 0;
468 /*
469     static uint32_t inactive_anon_hash = 0;
470     static uint32_t active_anon_hash = 0;
471     static uint32_t inactive_file_hash = 0;
472     static uint32_t active_file_hash = 0;
473     static uint32_t unevictable_hash = 0;
474     static uint32_t hierarchical_memory_limit_hash = 0;
475     static uint32_t total_cache_hash = 0;
476     static uint32_t total_rss_hash = 0;
477     static uint32_t total_rss_huge_hash = 0;
478     static uint32_t total_mapped_file_hash = 0;
479     static uint32_t total_writeback_hash = 0;
480     static uint32_t total_dirty_hash = 0;
481     static uint32_t total_swap_hash = 0;
482     static uint32_t total_pgpgin_hash = 0;
483     static uint32_t total_pgpgout_hash = 0;
484     static uint32_t total_pgfault_hash = 0;
485     static uint32_t total_pgmajfault_hash = 0;
486     static uint32_t total_inactive_anon_hash = 0;
487     static uint32_t total_active_anon_hash = 0;
488     static uint32_t total_inactive_file_hash = 0;
489     static uint32_t total_active_file_hash = 0;
490     static uint32_t total_unevictable_hash = 0;
491 */
492     if(unlikely(cache_hash == 0)) {
493         cache_hash = simple_hash("cache");
494         rss_hash = simple_hash("rss");
495         rss_huge_hash = simple_hash("rss_huge");
496         mapped_file_hash = simple_hash("mapped_file");
497         writeback_hash = simple_hash("writeback");
498         dirty_hash = simple_hash("dirty");
499         swap_hash = simple_hash("swap");
500         pgpgin_hash = simple_hash("pgpgin");
501         pgpgout_hash = simple_hash("pgpgout");
502         pgfault_hash = simple_hash("pgfault");
503         pgmajfault_hash = simple_hash("pgmajfault");
504 /*
505         inactive_anon_hash = simple_hash("inactive_anon");
506         active_anon_hash = simple_hash("active_anon");
507         inactive_file_hash = simple_hash("inactive_file");
508         active_file_hash = simple_hash("active_file");
509         unevictable_hash = simple_hash("unevictable");
510         hierarchical_memory_limit_hash = simple_hash("hierarchical_memory_limit");
511         total_cache_hash = simple_hash("total_cache");
512         total_rss_hash = simple_hash("total_rss");
513         total_rss_huge_hash = simple_hash("total_rss_huge");
514         total_mapped_file_hash = simple_hash("total_mapped_file");
515         total_writeback_hash = simple_hash("total_writeback");
516         total_dirty_hash = simple_hash("total_dirty");
517         total_swap_hash = simple_hash("total_swap");
518         total_pgpgin_hash = simple_hash("total_pgpgin");
519         total_pgpgout_hash = simple_hash("total_pgpgout");
520         total_pgfault_hash = simple_hash("total_pgfault");
521         total_pgmajfault_hash = simple_hash("total_pgmajfault");
522         total_inactive_anon_hash = simple_hash("total_inactive_anon");
523         total_active_anon_hash = simple_hash("total_active_anon");
524         total_inactive_file_hash = simple_hash("total_inactive_file");
525         total_active_file_hash = simple_hash("total_active_file");
526         total_unevictable_hash = simple_hash("total_unevictable");
527 */
528     }
529
530     mem->updated = 0;
531     if(mem->filename) {
532         ff = procfile_reopen(ff, mem->filename, NULL, PROCFILE_FLAG_DEFAULT);
533         if(!ff) return;
534
535         ff = procfile_readall(ff);
536         if(!ff) return;
537
538         unsigned long i, lines = procfile_lines(ff);
539
540         if(lines < 1) {
541             error("File '%s' should have 1+ lines.", mem->filename);
542             return;
543         }
544
545         for(i = 0; i < lines ; i++) {
546             char *s = procfile_lineword(ff, i, 0);
547             uint32_t hash = simple_hash(s);
548
549             if(hash == cache_hash && !strcmp(s, "cache"))
550                 mem->cache = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
551
552             else if(hash == rss_hash && !strcmp(s, "rss"))
553                 mem->rss = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
554
555             else if(hash == rss_huge_hash && !strcmp(s, "rss_huge"))
556                 mem->rss_huge = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
557
558             else if(hash == mapped_file_hash && !strcmp(s, "mapped_file"))
559                 mem->mapped_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
560
561             else if(hash == writeback_hash && !strcmp(s, "writeback"))
562                 mem->writeback = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
563
564             else if(hash == dirty_hash && !strcmp(s, "dirty")) {
565                 mem->dirty = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
566                 mem->has_dirty_swap = 1;
567             }
568
569             else if(hash == swap_hash && !strcmp(s, "swap")) {
570                 mem->swap = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
571                 mem->has_dirty_swap = 1;
572             }
573
574             else if(hash == pgpgin_hash && !strcmp(s, "pgpgin"))
575                 mem->pgpgin = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
576
577             else if(hash == pgpgout_hash && !strcmp(s, "pgpgout"))
578                 mem->pgpgout = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
579
580             else if(hash == pgfault_hash && !strcmp(s, "pgfault"))
581                 mem->pgfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
582
583             else if(hash == pgmajfault_hash && !strcmp(s, "pgmajfault"))
584                 mem->pgmajfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
585
586 /*
587             else if(hash == inactive_anon_hash && !strcmp(s, "inactive_anon"))
588                 mem->inactive_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
589
590             else if(hash == active_anon_hash && !strcmp(s, "active_anon"))
591                 mem->active_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
592
593             else if(hash == inactive_file_hash && !strcmp(s, "inactive_file"))
594                 mem->inactive_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
595
596             else if(hash == active_file_hash && !strcmp(s, "active_file"))
597                 mem->active_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
598
599             else if(hash == unevictable_hash && !strcmp(s, "unevictable"))
600                 mem->unevictable = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
601
602             else if(hash == hierarchical_memory_limit_hash && !strcmp(s, "hierarchical_memory_limit"))
603                 mem->hierarchical_memory_limit = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
604
605             else if(hash == total_cache_hash && !strcmp(s, "total_cache"))
606                 mem->total_cache = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
607
608             else if(hash == total_rss_hash && !strcmp(s, "total_rss"))
609                 mem->total_rss = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
610
611             else if(hash == total_rss_huge_hash && !strcmp(s, "total_rss_huge"))
612                 mem->total_rss_huge = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
613
614             else if(hash == total_mapped_file_hash && !strcmp(s, "total_mapped_file"))
615                 mem->total_mapped_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
616
617             else if(hash == total_writeback_hash && !strcmp(s, "total_writeback"))
618                 mem->total_writeback = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
619
620             else if(hash == total_dirty_hash && !strcmp(s, "total_dirty"))
621                 mem->total_dirty = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
622
623             else if(hash == total_swap_hash && !strcmp(s, "total_swap"))
624                 mem->total_swap = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
625
626             else if(hash == total_pgpgin_hash && !strcmp(s, "total_pgpgin"))
627                 mem->total_pgpgin = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
628
629             else if(hash == total_pgpgout_hash && !strcmp(s, "total_pgpgout"))
630                 mem->total_pgpgout = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
631
632             else if(hash == total_pgfault_hash && !strcmp(s, "total_pgfault"))
633                 mem->total_pgfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
634
635             else if(hash == total_pgmajfault_hash && !strcmp(s, "total_pgmajfault"))
636                 mem->total_pgmajfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
637
638             else if(hash == total_inactive_anon_hash && !strcmp(s, "total_inactive_anon"))
639                 mem->total_inactive_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
640
641             else if(hash == total_active_anon_hash && !strcmp(s, "total_active_anon"))
642                 mem->total_active_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
643
644             else if(hash == total_inactive_file_hash && !strcmp(s, "total_inactive_file"))
645                 mem->total_inactive_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
646
647             else if(hash == total_active_file_hash && !strcmp(s, "total_active_file"))
648                 mem->total_active_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
649
650             else if(hash == total_unevictable_hash && !strcmp(s, "total_unevictable"))
651                 mem->total_unevictable = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
652 */
653         }
654
655         // 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);
656
657         mem->updated = 1;
658     }
659
660     mem->usage_in_bytes_updated = 0;
661     if(mem->filename_usage_in_bytes) {
662         if(likely(!read_single_number_file(mem->filename_usage_in_bytes, &mem->usage_in_bytes)))
663             mem->usage_in_bytes_updated = 1;
664     }
665
666     mem->msw_usage_in_bytes_updated = 0;
667     if(mem->filename_msw_usage_in_bytes) {
668         if(likely(!read_single_number_file(mem->filename_msw_usage_in_bytes, &mem->msw_usage_in_bytes)))
669             mem->msw_usage_in_bytes_updated = 1;
670     }
671
672     mem->failcnt_updated = 0;
673     if(mem->filename_failcnt) {
674         if(likely(!read_single_number_file(mem->filename_failcnt, &mem->failcnt)))
675             mem->failcnt_updated = 1;
676     }
677 }
678
679 void cgroup_read(struct cgroup *cg) {
680     debug(D_CGROUP, "reading metrics for cgroups '%s'", cg->id);
681
682     cgroup_read_cpuacct_stat(&cg->cpuacct_stat);
683     cgroup_read_cpuacct_usage(&cg->cpuacct_usage);
684     cgroup_read_memory(&cg->memory);
685     cgroup_read_blkio(&cg->io_service_bytes);
686     cgroup_read_blkio(&cg->io_serviced);
687     cgroup_read_blkio(&cg->throttle_io_service_bytes);
688     cgroup_read_blkio(&cg->throttle_io_serviced);
689     cgroup_read_blkio(&cg->io_merged);
690     cgroup_read_blkio(&cg->io_queued);
691 }
692
693 void read_all_cgroups(struct cgroup *root) {
694     debug(D_CGROUP, "reading metrics for all cgroups");
695
696     struct cgroup *cg;
697
698     for(cg = root; cg ; cg = cg->next)
699         if(cg->enabled && cg->available)
700             cgroup_read(cg);
701 }
702
703 // ----------------------------------------------------------------------------
704 // add/remove/find cgroup objects
705
706 #define CGROUP_CHARTID_LINE_MAX 1024
707
708 static char *cgroup_title_strdupz(const char *s) {
709     if(!s || !*s) s = "/";
710
711     if(*s == '/' && s[1] != '\0') s++;
712
713     char *r = strdupz(s);
714     netdata_fix_chart_name(r);
715
716     return r;
717 }
718
719 static char *cgroup_chart_id_strdupz(const char *s) {
720     if(!s || !*s) s = "/";
721
722     if(*s == '/' && s[1] != '\0') s++;
723
724     char *r = strdupz(s);
725     netdata_fix_chart_id(r);
726
727     return r;
728 }
729
730 void cgroup_get_chart_name(struct cgroup *cg) {
731     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);
732
733     if(!netdata_simple_pattern_list_matches(disabled_cgroup_renames, cg->id) &&
734             !netdata_simple_pattern_list_matches(disabled_cgroup_renames, cg->chart_id)) {
735
736         pid_t cgroup_pid;
737         char buffer[CGROUP_CHARTID_LINE_MAX + 1];
738
739         snprintfz(buffer, CGROUP_CHARTID_LINE_MAX, "exec %s '%s'", cgroups_rename_script, cg->chart_id);
740
741         debug(D_CGROUP, "executing command '%s' for cgroup '%s'", buffer, cg->id);
742         FILE *fp = mypopen(buffer, &cgroup_pid);
743         if(fp) {
744             // debug(D_CGROUP, "reading from command '%s' for cgroup '%s'", buffer, cg->id);
745             char *s = fgets(buffer, CGROUP_CHARTID_LINE_MAX, fp);
746             // debug(D_CGROUP, "closing command for cgroup '%s'", cg->id);
747             mypclose(fp, cgroup_pid);
748             // debug(D_CGROUP, "closed command for cgroup '%s'", cg->id);
749
750             if(s && *s && *s != '\n') {
751                 debug(D_CGROUP, "cgroup '%s' should be renamed to '%s'", cg->id, s);
752
753                 trim(s);
754
755                 freez(cg->chart_title);
756                 cg->chart_title = cgroup_title_strdupz(s);
757
758                 freez(cg->chart_id);
759                 cg->chart_id = cgroup_chart_id_strdupz(s);
760                 cg->hash_chart = simple_hash(cg->chart_id);
761             }
762         }
763         else
764             error("CGROUP: Cannot popen(\"%s\", \"r\").", buffer);
765
766         debug(D_CGROUP, "cgroup '%s' renamed to '%s' (title: '%s')", cg->id, cg->chart_id, cg->chart_title);
767     }
768     else
769         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);
770
771
772     if(cgroup_enable_systemd_services) {
773         if(netdata_simple_pattern_list_matches(systemd_services_cgroups, cg->id) ||
774            netdata_simple_pattern_list_matches(systemd_services_cgroups, cg->chart_id)) {
775             debug(D_CGROUP, "cgroup '%s' with chart id '%s' (title: '%s') matches systemd services cgroups", cg->id, cg->chart_id, cg->chart_title);
776
777             char buffer[CGROUP_CHARTID_LINE_MAX + 1];
778             cg->options |= CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE;
779
780             strncpy(buffer, cg->id, CGROUP_CHARTID_LINE_MAX);
781             char *s = buffer;
782
783             // skip to the last slash
784             size_t len = strlen(s);
785             while(len--) if(unlikely(s[len] == '/')) break;
786             if(len) s = &s[len + 1];
787
788             // remove extension
789             //len = strlen(s);
790             //while(len--) if(unlikely(s[len] == '.')) break;
791             //if(len) s[len] = '\0';
792
793             freez(cg->chart_title);
794             cg->chart_title = cgroup_title_strdupz(s);
795
796             freez(cg->chart_id);
797             cg->chart_id = cgroup_chart_id_strdupz(s);
798             cg->hash_chart = simple_hash(cg->chart_id);
799             cg->enabled = 1;
800
801             debug(D_CGROUP, "cgroup '%s' renamed to '%s' (title: '%s')", cg->id, cg->chart_id, cg->chart_title);
802         }
803         else
804             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);
805     }
806 }
807
808 struct cgroup *cgroup_add(const char *id) {
809     if(!id || !*id) id = "/";
810     debug(D_CGROUP, "adding to list, cgroup with id '%s'", id);
811
812     if(cgroup_root_count >= cgroup_root_max) {
813         info("Maximum number of cgroups reached (%d). Not adding cgroup '%s'", cgroup_root_count, id);
814         return NULL;
815     }
816
817     int def = netdata_simple_pattern_list_matches(disabled_cgroups_patterns, id)?0:cgroup_enable_new_cgroups_detected_at_runtime;
818     struct cgroup *cg = callocz(1, sizeof(struct cgroup));
819
820     cg->id = strdupz(id);
821     cg->hash = simple_hash(cg->id);
822
823     cg->chart_title = cgroup_title_strdupz(id);
824
825     cg->chart_id = cgroup_chart_id_strdupz(id);
826     cg->hash_chart = simple_hash(cg->chart_id);
827
828     if(!cgroup_root)
829         cgroup_root = cg;
830     else {
831         // append it
832         struct cgroup *e;
833         for(e = cgroup_root; e->next ;e = e->next) ;
834         e->next = cg;
835     }
836
837     cgroup_root_count++;
838
839     // fix the name by calling the external script
840     cgroup_get_chart_name(cg);
841
842     char option[FILENAME_MAX + 1];
843     snprintfz(option, FILENAME_MAX, "enable cgroup %s", cg->chart_title);
844     cg->enabled = (char)config_get_boolean("plugin:cgroups", option, def);
845
846     if(cg->enabled) {
847         struct cgroup *t;
848         for (t = cgroup_root; t; t = t->next) {
849             if (t != cg && t->enabled && t->hash_chart == cg->hash_chart && !strcmp(t->chart_id, cg->chart_id)) {
850                 if (!strncmp(t->chart_id, "/system.slice/", 14) && !strncmp(cg->chart_id, "/init.scope/system.slice/", 25)) {
851                     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'.",
852                           cg->chart_id, t->id, cg->id, t->id);
853                     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'.",
854                           cg->chart_id, t->id, cg->id, t->id);
855                     t->enabled = 0;
856                     t->options |= CGROUP_OPTIONS_DISABLED_DUPLICATE;
857                 }
858                 else {
859                     error("Control group with chart id '%s' already exists with id '%s' and is enabled and available. Disabling cgroup with id '%s'.",
860                           cg->chart_id, t->id, cg->id);
861                     debug(D_CGROUP, "Control group with chart id '%s' already exists with id '%s' and is enabled and available. Disabling cgroup with id '%s'.",
862                           cg->chart_id, t->id, cg->id);
863                     cg->enabled = 0;
864                     cg->options |= CGROUP_OPTIONS_DISABLED_DUPLICATE;
865                 }
866
867                 break;
868             }
869         }
870     }
871
872     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");
873
874     return cg;
875 }
876
877 void cgroup_free(struct cgroup *cg) {
878     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");
879
880     freez(cg->cpuacct_usage.cpu_percpu);
881
882     freez(cg->cpuacct_stat.filename);
883     freez(cg->cpuacct_usage.filename);
884
885     freez(cg->memory.filename);
886     freez(cg->memory.filename_failcnt);
887     freez(cg->memory.filename_usage_in_bytes);
888     freez(cg->memory.filename_msw_usage_in_bytes);
889
890     freez(cg->io_service_bytes.filename);
891     freez(cg->io_serviced.filename);
892
893     freez(cg->throttle_io_service_bytes.filename);
894     freez(cg->throttle_io_serviced.filename);
895
896     freez(cg->io_merged.filename);
897     freez(cg->io_queued.filename);
898
899     freez(cg->id);
900     freez(cg->chart_id);
901     freez(cg->chart_title);
902
903     freez(cg);
904
905     cgroup_root_count--;
906 }
907
908 // find if a given cgroup exists
909 struct cgroup *cgroup_find(const char *id) {
910     debug(D_CGROUP, "searching for cgroup '%s'", id);
911
912     uint32_t hash = simple_hash(id);
913
914     struct cgroup *cg;
915     for(cg = cgroup_root; cg ; cg = cg->next) {
916         if(hash == cg->hash && strcmp(id, cg->id) == 0)
917             break;
918     }
919
920     debug(D_CGROUP, "cgroup '%s' %s in memory", id, (cg)?"found":"not found");
921     return cg;
922 }
923
924 // ----------------------------------------------------------------------------
925 // detect running cgroups
926
927 // callback for find_file_in_subdirs()
928 void found_subdir_in_dir(const char *dir) {
929     debug(D_CGROUP, "examining cgroup dir '%s'", dir);
930
931     struct cgroup *cg = cgroup_find(dir);
932     if(!cg) {
933         if(*dir && cgroup_max_depth > 0) {
934             int depth = 0;
935             const char *s;
936
937             for(s = dir; *s ;s++)
938                 if(unlikely(*s == '/'))
939                     depth++;
940
941             if(depth > cgroup_max_depth) {
942                 info("cgroup '%s' is too deep (%d, while max is %d)", dir, depth, cgroup_max_depth);
943                 return;
944             }
945         }
946         // debug(D_CGROUP, "will add dir '%s' as cgroup", dir);
947         cg = cgroup_add(dir);
948     }
949
950     if(cg) cg->available = 1;
951 }
952
953 int find_dir_in_subdirs(const char *base, const char *this, void (*callback)(const char *)) {
954     if(!this) this = base;
955     debug(D_CGROUP, "searching for directories in '%s' (base '%s')", this?this:"", base);
956
957     size_t dirlen = strlen(this), baselen = strlen(base);
958
959     int ret = -1;
960     int enabled = -1;
961
962     const char *relative_path = &this[baselen];
963     if(!*relative_path) relative_path = "/";
964
965     DIR *dir = opendir(this);
966     if(!dir) {
967         error("Cannot read cgroups directory '%s'", base);
968         return ret;
969     }
970     ret = 1;
971
972     callback(relative_path);
973
974     struct dirent *de = NULL;
975     while((de = readdir(dir))) {
976         if(de->d_type == DT_DIR
977             && (
978                 (de->d_name[0] == '.' && de->d_name[1] == '\0')
979                 || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
980                 ))
981             continue;
982
983         if(de->d_type == DT_DIR) {
984             if(enabled == -1) {
985                 const char *r = relative_path;
986                 if(*r == '\0') r = "/";
987
988                 // do not decent in directories we are not interested
989                 int def = 1;
990                 if(netdata_simple_pattern_list_matches(disabled_cgroup_paths, r))
991                     def = 0;
992
993                 // we check for this option here
994                 // so that the config will not have settings
995                 // for leaf directories
996                 char option[FILENAME_MAX + 1];
997                 snprintfz(option, FILENAME_MAX, "search for cgroups under %s", r);
998                 option[FILENAME_MAX] = '\0';
999                 enabled = config_get_boolean("plugin:cgroups", option, def);
1000             }
1001
1002             if(enabled) {
1003                 char *s = mallocz(dirlen + strlen(de->d_name) + 2);
1004                 strcpy(s, this);
1005                 strcat(s, "/");
1006                 strcat(s, de->d_name);
1007                 int ret2 = find_dir_in_subdirs(base, s, callback);
1008                 if(ret2 > 0) ret += ret2;
1009                 freez(s);
1010             }
1011         }
1012     }
1013
1014     closedir(dir);
1015     return ret;
1016 }
1017
1018 void mark_all_cgroups_as_not_available() {
1019     debug(D_CGROUP, "marking all cgroups as not available");
1020
1021     struct cgroup *cg;
1022
1023     // mark all as not available
1024     for(cg = cgroup_root; cg ; cg = cg->next) {
1025         cg->available = 0;
1026     }
1027 }
1028
1029 void cleanup_all_cgroups() {
1030     struct cgroup *cg = cgroup_root, *last = NULL;
1031
1032     for(; cg ;) {
1033         if(!cg->available) {
1034             // enable the first duplicate cgroup
1035             {
1036                 struct cgroup *t;
1037                 for(t = cgroup_root; t ; t = t->next) {
1038                     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)) {
1039                         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);
1040                         t->enabled = 1;
1041                         t->options &= ~CGROUP_OPTIONS_DISABLED_DUPLICATE;
1042                         break;
1043                     }
1044                 }
1045             }
1046
1047             if(!last)
1048                 cgroup_root = cg->next;
1049             else
1050                 last->next = cg->next;
1051
1052             cgroup_free(cg);
1053
1054             if(!last)
1055                 cg = cgroup_root;
1056             else
1057                 cg = last->next;
1058         }
1059         else {
1060             last = cg;
1061             cg = cg->next;
1062         }
1063     }
1064 }
1065
1066 void find_all_cgroups() {
1067     debug(D_CGROUP, "searching for cgroups");
1068
1069     mark_all_cgroups_as_not_available();
1070
1071     if(cgroup_enable_cpuacct_stat || cgroup_enable_cpuacct_usage) {
1072         if (find_dir_in_subdirs(cgroup_cpuacct_base, NULL, found_subdir_in_dir) == -1) {
1073             cgroup_enable_cpuacct_stat = cgroup_enable_cpuacct_usage = 0;
1074             error("disabled cgroup cpu statistics.");
1075         }
1076     }
1077
1078     if(cgroup_enable_blkio) {
1079         if (find_dir_in_subdirs(cgroup_blkio_base, NULL, found_subdir_in_dir) == -1) {
1080             cgroup_enable_blkio = 0;
1081             error("disabled cgroup blkio statistics.");
1082         }
1083     }
1084
1085     if(cgroup_enable_memory) {
1086         if(find_dir_in_subdirs(cgroup_memory_base, NULL, found_subdir_in_dir) == -1) {
1087             cgroup_enable_memory = 0;
1088             error("disabled cgroup memory statistics.");
1089         }
1090     }
1091
1092     if(cgroup_enable_devices) {
1093         if(find_dir_in_subdirs(cgroup_devices_base, NULL, found_subdir_in_dir) == -1) {
1094             cgroup_enable_devices = 0;
1095             error("disabled cgroup devices statistics.");
1096         }
1097     }
1098
1099     // remove any non-existing cgroups
1100     cleanup_all_cgroups();
1101
1102     struct cgroup *cg;
1103     struct stat buf;
1104     for(cg = cgroup_root; cg ; cg = cg->next) {
1105         // fprintf(stderr, " >>> CGROUP '%s' (%u - %s) with name '%s'\n", cg->id, cg->hash, cg->available?"available":"stopped", cg->name);
1106
1107         if(unlikely(!cg->available))
1108             continue;
1109
1110         debug(D_CGROUP, "checking paths for cgroup '%s'", cg->id);
1111
1112         // check for newly added cgroups
1113         // and update the filenames they read
1114         char filename[FILENAME_MAX + 1];
1115         if(unlikely(cgroup_enable_cpuacct_stat && !cg->cpuacct_stat.filename)) {
1116             snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.stat", cgroup_cpuacct_base, cg->id);
1117             if(stat(filename, &buf) != -1) {
1118                 cg->cpuacct_stat.filename = strdupz(filename);
1119                 debug(D_CGROUP, "cpuacct.stat filename for cgroup '%s': '%s'", cg->id, cg->cpuacct_stat.filename);
1120             }
1121             else debug(D_CGROUP, "cpuacct.stat file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1122         }
1123
1124         if(unlikely(cgroup_enable_cpuacct_usage && !cg->cpuacct_usage.filename)) {
1125             snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.usage_percpu", cgroup_cpuacct_base, cg->id);
1126             if(stat(filename, &buf) != -1) {
1127                 cg->cpuacct_usage.filename = strdupz(filename);
1128                 debug(D_CGROUP, "cpuacct.usage_percpu filename for cgroup '%s': '%s'", cg->id, cg->cpuacct_usage.filename);
1129             }
1130             else debug(D_CGROUP, "cpuacct.usage_percpu file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1131         }
1132
1133         if(unlikely(cgroup_enable_memory)) {
1134             if(unlikely(!cg->memory.filename)) {
1135                 snprintfz(filename, FILENAME_MAX, "%s%s/memory.stat", cgroup_memory_base, cg->id);
1136                 if(stat(filename, &buf) != -1) {
1137                     cg->memory.filename = strdupz(filename);
1138                     debug(D_CGROUP, "memory.stat filename for cgroup '%s': '%s'", cg->id, cg->memory.filename);
1139                 }
1140                 else
1141                     debug(D_CGROUP, "memory.stat file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1142             }
1143             if(unlikely(!cg->memory.filename_usage_in_bytes)) {
1144                 snprintfz(filename, FILENAME_MAX, "%s%s/memory.usage_in_bytes", cgroup_memory_base, cg->id);
1145                 if(stat(filename, &buf) != -1) {
1146                     cg->memory.filename_usage_in_bytes = strdupz(filename);
1147                     debug(D_CGROUP, "memory.usage_in_bytes filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_usage_in_bytes);
1148                 }
1149                 else
1150                     debug(D_CGROUP, "memory.usage_in_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1151             }
1152             if(unlikely(!cg->memory.filename_msw_usage_in_bytes)) {
1153                 snprintfz(filename, FILENAME_MAX, "%s%s/memory.msw_usage_in_bytes", cgroup_memory_base, cg->id);
1154                 if(stat(filename, &buf) != -1) {
1155                     cg->memory.filename_msw_usage_in_bytes = strdupz(filename);
1156                     debug(D_CGROUP, "memory.msw_usage_in_bytes filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_msw_usage_in_bytes);
1157                 }
1158                 else
1159                     debug(D_CGROUP, "memory.msw_usage_in_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1160             }
1161             if(unlikely(!cg->memory.filename_failcnt)) {
1162                 snprintfz(filename, FILENAME_MAX, "%s%s/memory.failcnt", cgroup_memory_base, cg->id);
1163                 if(stat(filename, &buf) != -1) {
1164                     cg->memory.filename_failcnt = strdupz(filename);
1165                     debug(D_CGROUP, "memory.failcnt filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_failcnt);
1166                 }
1167                 else
1168                     debug(D_CGROUP, "memory.failcnt file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1169             }
1170         }
1171
1172         if(unlikely(cgroup_enable_blkio)) {
1173             if(unlikely(!cg->io_service_bytes.filename)) {
1174                 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_service_bytes", cgroup_blkio_base, cg->id);
1175                 if(stat(filename, &buf) != -1) {
1176                     cg->io_service_bytes.filename = strdupz(filename);
1177                     debug(D_CGROUP, "io_service_bytes filename for cgroup '%s': '%s'", cg->id, cg->io_service_bytes.filename);
1178                 }
1179                 else debug(D_CGROUP, "io_service_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1180             }
1181             if(unlikely(!cg->io_serviced.filename)) {
1182                 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_serviced", cgroup_blkio_base, cg->id);
1183                 if(stat(filename, &buf) != -1) {
1184                     cg->io_serviced.filename = strdupz(filename);
1185                     debug(D_CGROUP, "io_serviced filename for cgroup '%s': '%s'", cg->id, cg->io_serviced.filename);
1186                 }
1187                 else debug(D_CGROUP, "io_serviced file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1188             }
1189             if(unlikely(!cg->throttle_io_service_bytes.filename)) {
1190                 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.throttle.io_service_bytes", cgroup_blkio_base, cg->id);
1191                 if(stat(filename, &buf) != -1) {
1192                     cg->throttle_io_service_bytes.filename = strdupz(filename);
1193                     debug(D_CGROUP, "throttle_io_service_bytes filename for cgroup '%s': '%s'", cg->id, cg->throttle_io_service_bytes.filename);
1194                 }
1195                 else debug(D_CGROUP, "throttle_io_service_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1196             }
1197             if(unlikely(!cg->throttle_io_serviced.filename)) {
1198                 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.throttle.io_serviced", cgroup_blkio_base, cg->id);
1199                 if(stat(filename, &buf) != -1) {
1200                     cg->throttle_io_serviced.filename = strdupz(filename);
1201                     debug(D_CGROUP, "throttle_io_serviced filename for cgroup '%s': '%s'", cg->id, cg->throttle_io_serviced.filename);
1202                 }
1203                 else debug(D_CGROUP, "throttle_io_serviced file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1204             }
1205             if(unlikely(!cg->io_merged.filename)) {
1206                 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_merged", cgroup_blkio_base, cg->id);
1207                 if(stat(filename, &buf) != -1) {
1208                     cg->io_merged.filename = strdupz(filename);
1209                     debug(D_CGROUP, "io_merged filename for cgroup '%s': '%s'", cg->id, cg->io_merged.filename);
1210                 }
1211                 else debug(D_CGROUP, "io_merged file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1212             }
1213             if(unlikely(!cg->io_queued.filename)) {
1214                 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_queued", cgroup_blkio_base, cg->id);
1215                 if(stat(filename, &buf) != -1) {
1216                     cg->io_queued.filename = strdupz(filename);
1217                     debug(D_CGROUP, "io_queued filename for cgroup '%s': '%s'", cg->id, cg->io_queued.filename);
1218                 }
1219                 else debug(D_CGROUP, "io_queued file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1220             }
1221         }
1222     }
1223
1224     debug(D_CGROUP, "done searching for cgroups");
1225     return;
1226 }
1227
1228 // ----------------------------------------------------------------------------
1229 // generate charts
1230
1231 #define CHART_TITLE_MAX 300
1232
1233 static inline char *cgroup_chart_type(char *buffer, const char *id, size_t len) {
1234     if(buffer[0]) return buffer;
1235
1236     if(id[0] == '\0' || (id[0] == '/' && id[1] == '\0'))
1237         strncpy(buffer, "cgroup_root", len);
1238     else
1239         snprintfz(buffer, len, "cgroup_%s", id);
1240
1241     netdata_fix_chart_id(buffer);
1242     return buffer;
1243 }
1244
1245 void update_cgroup_charts(int update_every) {
1246     debug(D_CGROUP, "updating cgroups charts");
1247
1248     char type[RRD_ID_LENGTH_MAX + 1];
1249     char title[CHART_TITLE_MAX + 1];
1250
1251     struct cgroup *cg;
1252
1253     for(cg = cgroup_root; cg ; cg = cg->next) {
1254         if(!cg->available || !cg->enabled || cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE)
1255             continue;
1256
1257         type[0] = '\0';
1258
1259         if(cg->cpuacct_stat.updated) {
1260             if(unlikely(!cg->st_cpu)) {
1261                 cg->st_cpu = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "cpu");
1262                 if(!cg->st_cpu) {
1263                     snprintfz(title, CHART_TITLE_MAX, "CPU Usage (%d%% = %d core%s) for cgroup %s", (processors * 100), processors, (processors > 1) ? "s" : "", cg->chart_title);
1264                     cg->st_cpu = rrdset_create(type, "cpu", NULL, "cpu", "cgroup.cpu", title, "%", 40000, update_every, RRDSET_TYPE_STACKED);
1265
1266                     rrddim_add(cg->st_cpu, "user", NULL, 100, hz, RRDDIM_INCREMENTAL);
1267                     rrddim_add(cg->st_cpu, "system", NULL, 100, hz, RRDDIM_INCREMENTAL);
1268                 }
1269             }
1270             else rrdset_next(cg->st_cpu);
1271
1272             rrddim_set(cg->st_cpu, "user", cg->cpuacct_stat.user);
1273             rrddim_set(cg->st_cpu, "system", cg->cpuacct_stat.system);
1274             rrdset_done(cg->st_cpu);
1275         }
1276
1277         if(cg->cpuacct_usage.updated) {
1278             char id[RRD_ID_LENGTH_MAX + 1];
1279             unsigned int i;
1280
1281             if(unlikely(!cg->st_cpu_per_core)) {
1282                 cg->st_cpu_per_core = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "cpu_per_core");
1283                 if(!cg->st_cpu_per_core) {
1284                     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);
1285                     cg->st_cpu_per_core = rrdset_create(type, "cpu_per_core", NULL, "cpu", "cgroup.cpu_per_core", title, "%", 40100, update_every, RRDSET_TYPE_STACKED);
1286
1287                     for(i = 0; i < cg->cpuacct_usage.cpus; i++) {
1288                         snprintfz(id, CHART_TITLE_MAX, "cpu%u", i);
1289                         rrddim_add(cg->st_cpu_per_core, id, NULL, 100, 1000000000, RRDDIM_INCREMENTAL);
1290                     }
1291                 }
1292             }
1293             else rrdset_next(cg->st_cpu_per_core);
1294
1295             for(i = 0; i < cg->cpuacct_usage.cpus ;i++) {
1296                 snprintfz(id, CHART_TITLE_MAX, "cpu%u", i);
1297                 rrddim_set(cg->st_cpu_per_core, id, cg->cpuacct_usage.cpu_percpu[i]);
1298             }
1299             rrdset_done(cg->st_cpu_per_core);
1300         }
1301
1302         if(cg->memory.updated) {
1303             if(cg->memory.cache + cg->memory.rss + cg->memory.rss_huge + cg->memory.mapped_file > 0) {
1304                 if(unlikely(!cg->st_mem)) {
1305                     cg->st_mem = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem");
1306                     if(!cg->st_mem) {
1307                         snprintfz(title, CHART_TITLE_MAX, "Memory Usage for cgroup %s", cg->chart_title);
1308                         cg->st_mem = rrdset_create(type, "mem", NULL, "mem", "cgroup.mem", title, "MB", 40210, update_every, RRDSET_TYPE_STACKED);
1309
1310                         rrddim_add(cg->st_mem, "cache", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1311                         rrddim_add(cg->st_mem, "rss", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1312                         if(cg->memory.has_dirty_swap)
1313                             rrddim_add(cg->st_mem, "swap", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1314                         rrddim_add(cg->st_mem, "rss_huge", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1315                         rrddim_add(cg->st_mem, "mapped_file", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1316                     }
1317                 }
1318                 else rrdset_next(cg->st_mem);
1319
1320                 rrddim_set(cg->st_mem, "cache", cg->memory.cache);
1321                 rrddim_set(cg->st_mem, "rss", cg->memory.rss);
1322                 if(cg->memory.has_dirty_swap)
1323                     rrddim_set(cg->st_mem, "swap", cg->memory.swap);
1324                 rrddim_set(cg->st_mem, "rss_huge", cg->memory.rss_huge);
1325                 rrddim_set(cg->st_mem, "mapped_file", cg->memory.mapped_file);
1326                 rrdset_done(cg->st_mem);
1327             }
1328
1329             if(unlikely(!cg->st_writeback)) {
1330                 cg->st_writeback = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "writeback");
1331                 if(!cg->st_writeback) {
1332                     snprintfz(title, CHART_TITLE_MAX, "Writeback Memory for cgroup %s", cg->chart_title);
1333                     cg->st_writeback = rrdset_create(type, "writeback", NULL, "mem", "cgroup.writeback", title, "MB", 40300, update_every, RRDSET_TYPE_AREA);
1334
1335                     if(cg->memory.has_dirty_swap)
1336                         rrddim_add(cg->st_writeback, "dirty", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1337                     rrddim_add(cg->st_writeback, "writeback", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1338                 }
1339             }
1340             else rrdset_next(cg->st_writeback);
1341
1342             if(cg->memory.has_dirty_swap)
1343                 rrddim_set(cg->st_writeback, "dirty", cg->memory.dirty);
1344             rrddim_set(cg->st_writeback, "writeback", cg->memory.writeback);
1345             rrdset_done(cg->st_writeback);
1346
1347             if(cg->memory.pgpgin + cg->memory.pgpgout > 0) {
1348                 if(unlikely(!cg->st_mem_activity)) {
1349                     cg->st_mem_activity = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_activity");
1350                     if(!cg->st_mem_activity) {
1351                         snprintfz(title, CHART_TITLE_MAX, "Memory Activity for cgroup %s", cg->chart_title);
1352                         cg->st_mem_activity = rrdset_create(type, "mem_activity", NULL, "mem", "cgroup.mem_activity", title, "MB/s", 40400, update_every, RRDSET_TYPE_LINE);
1353
1354                         rrddim_add(cg->st_mem_activity, "pgpgin", "in", sysconf(_SC_PAGESIZE), 1024 * 1024, RRDDIM_INCREMENTAL);
1355                         rrddim_add(cg->st_mem_activity, "pgpgout", "out", -sysconf(_SC_PAGESIZE), 1024 * 1024, RRDDIM_INCREMENTAL);
1356                     }
1357                 }
1358                 else rrdset_next(cg->st_mem_activity);
1359
1360                 rrddim_set(cg->st_mem_activity, "pgpgin", cg->memory.pgpgin);
1361                 rrddim_set(cg->st_mem_activity, "pgpgout", cg->memory.pgpgout);
1362                 rrdset_done(cg->st_mem_activity);
1363             }
1364
1365             if(cg->memory.pgfault + cg->memory.pgmajfault > 0) {
1366                 if(unlikely(!cg->st_pgfaults)) {
1367                     cg->st_pgfaults = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "pgfaults");
1368                     if(!cg->st_pgfaults) {
1369                         snprintfz(title, CHART_TITLE_MAX, "Memory Page Faults for cgroup %s", cg->chart_title);
1370                         cg->st_pgfaults = rrdset_create(type, "pgfaults", NULL, "mem", "cgroup.pgfaults", title, "MB/s", 40500, update_every, RRDSET_TYPE_LINE);
1371
1372                         rrddim_add(cg->st_pgfaults, "pgfault", NULL, sysconf(_SC_PAGESIZE), 1024 * 1024, RRDDIM_INCREMENTAL);
1373                         rrddim_add(cg->st_pgfaults, "pgmajfault", "swap", -sysconf(_SC_PAGESIZE), 1024 * 1024, RRDDIM_INCREMENTAL);
1374                     }
1375                 }
1376                 else rrdset_next(cg->st_pgfaults);
1377
1378                 rrddim_set(cg->st_pgfaults, "pgfault", cg->memory.pgfault);
1379                 rrddim_set(cg->st_pgfaults, "pgmajfault", cg->memory.pgmajfault);
1380                 rrdset_done(cg->st_pgfaults);
1381             }
1382         }
1383
1384         if(cg->memory.usage_in_bytes_updated) {
1385             if(unlikely(!cg->st_mem_usage)) {
1386                 cg->st_mem_usage = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_usage");
1387                 if(!cg->st_mem_usage) {
1388                     snprintfz(title, CHART_TITLE_MAX, "Total Memory for cgroup %s", cg->chart_title);
1389                     cg->st_mem_usage = rrdset_create(type, "mem_usage", NULL, "mem", "cgroup.mem_usage", title, "MB", 40200, update_every, RRDSET_TYPE_STACKED);
1390
1391                     rrddim_add(cg->st_mem_usage, "ram", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1392                     rrddim_add(cg->st_mem_usage, "swap", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1393                 }
1394             }
1395             else rrdset_next(cg->st_mem_usage);
1396
1397             rrddim_set(cg->st_mem_usage, "ram", cg->memory.usage_in_bytes);
1398             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);
1399             rrdset_done(cg->st_mem_usage);
1400         }
1401
1402         if(cg->memory.failcnt_updated && cg->memory.failcnt > 0) {
1403             if(unlikely(!cg->st_mem_failcnt)) {
1404                 cg->st_mem_failcnt = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_failcnt");
1405                 if(!cg->st_mem_failcnt) {
1406                     snprintfz(title, CHART_TITLE_MAX, "Memory Limit Failures for cgroup %s", cg->chart_title);
1407                     cg->st_mem_failcnt = rrdset_create(type, "mem_failcnt", NULL, "mem", "cgroup.mem_failcnt", title, "MB", 40250, update_every, RRDSET_TYPE_LINE);
1408
1409                     rrddim_add(cg->st_mem_failcnt, "failures", NULL, 1, 1, RRDDIM_INCREMENTAL);
1410                 }
1411             }
1412             else rrdset_next(cg->st_mem_failcnt);
1413
1414             rrddim_set(cg->st_mem_failcnt, "failures", cg->memory.failcnt);
1415             rrdset_done(cg->st_mem_failcnt);
1416         }
1417
1418         if(cg->io_service_bytes.updated && cg->io_service_bytes.Read + cg->io_service_bytes.Write > 0) {
1419             if(unlikely(!cg->st_io)) {
1420                 cg->st_io = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "io");
1421                 if(!cg->st_io) {
1422                     snprintfz(title, CHART_TITLE_MAX, "I/O Bandwidth (all disks) for cgroup %s", cg->chart_title);
1423                     cg->st_io = rrdset_create(type, "io", NULL, "disk", "cgroup.io", title, "KB/s", 41200, update_every, RRDSET_TYPE_AREA);
1424
1425                     rrddim_add(cg->st_io, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
1426                     rrddim_add(cg->st_io, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
1427                 }
1428             }
1429             else rrdset_next(cg->st_io);
1430
1431             rrddim_set(cg->st_io, "read", cg->io_service_bytes.Read);
1432             rrddim_set(cg->st_io, "write", cg->io_service_bytes.Write);
1433             rrdset_done(cg->st_io);
1434         }
1435
1436         if(cg->io_serviced.updated && cg->io_serviced.Read + cg->io_serviced.Write > 0) {
1437             if(unlikely(!cg->st_serviced_ops)) {
1438                 cg->st_serviced_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "serviced_ops");
1439                 if(!cg->st_serviced_ops) {
1440                     snprintfz(title, CHART_TITLE_MAX, "Serviced I/O Operations (all disks) for cgroup %s"
1441                               , cg->chart_title);
1442                     cg->st_serviced_ops = rrdset_create(type, "serviced_ops", NULL, "disk", "cgroup.serviced_ops", title, "operations/s", 41200, update_every, RRDSET_TYPE_LINE);
1443
1444                     rrddim_add(cg->st_serviced_ops, "read", NULL, 1, 1, RRDDIM_INCREMENTAL);
1445                     rrddim_add(cg->st_serviced_ops, "write", NULL, -1, 1, RRDDIM_INCREMENTAL);
1446                 }
1447             }
1448             else rrdset_next(cg->st_serviced_ops);
1449
1450             rrddim_set(cg->st_serviced_ops, "read", cg->io_serviced.Read);
1451             rrddim_set(cg->st_serviced_ops, "write", cg->io_serviced.Write);
1452             rrdset_done(cg->st_serviced_ops);
1453         }
1454
1455         if(cg->throttle_io_service_bytes.updated && cg->throttle_io_service_bytes.Read + cg->throttle_io_service_bytes.Write > 0) {
1456             if(unlikely(!cg->st_throttle_io)) {
1457                 cg->st_throttle_io = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "throttle_io");
1458                 if(!cg->st_throttle_io) {
1459                     snprintfz(title, CHART_TITLE_MAX, "Throttle I/O Bandwidth (all disks) for cgroup %s", cg->chart_title);
1460                     cg->st_throttle_io = rrdset_create(type, "throttle_io", NULL, "disk", "cgroup.throttle_io", title, "KB/s", 41200, update_every, RRDSET_TYPE_AREA);
1461
1462                     rrddim_add(cg->st_throttle_io, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
1463                     rrddim_add(cg->st_throttle_io, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
1464                 }
1465             }
1466             else rrdset_next(cg->st_throttle_io);
1467
1468             rrddim_set(cg->st_throttle_io, "read", cg->throttle_io_service_bytes.Read);
1469             rrddim_set(cg->st_throttle_io, "write", cg->throttle_io_service_bytes.Write);
1470             rrdset_done(cg->st_throttle_io);
1471         }
1472
1473
1474         if(cg->throttle_io_serviced.updated && cg->throttle_io_serviced.Read + cg->throttle_io_serviced.Write > 0) {
1475             if(unlikely(!cg->st_throttle_serviced_ops)) {
1476                 cg->st_throttle_serviced_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "throttle_serviced_ops");
1477                 if(!cg->st_throttle_serviced_ops) {
1478                     snprintfz(title, CHART_TITLE_MAX, "Throttle Serviced I/O Operations (all disks) for cgroup %s", cg->chart_title);
1479                     cg->st_throttle_serviced_ops = rrdset_create(type, "throttle_serviced_ops", NULL, "disk", "cgroup.throttle_serviced_ops", title, "operations/s", 41200, update_every, RRDSET_TYPE_LINE);
1480
1481                     rrddim_add(cg->st_throttle_serviced_ops, "read", NULL, 1, 1, RRDDIM_INCREMENTAL);
1482                     rrddim_add(cg->st_throttle_serviced_ops, "write", NULL, -1, 1, RRDDIM_INCREMENTAL);
1483                 }
1484             }
1485             else rrdset_next(cg->st_throttle_serviced_ops);
1486
1487             rrddim_set(cg->st_throttle_serviced_ops, "read", cg->throttle_io_serviced.Read);
1488             rrddim_set(cg->st_throttle_serviced_ops, "write", cg->throttle_io_serviced.Write);
1489             rrdset_done(cg->st_throttle_serviced_ops);
1490         }
1491
1492         if(cg->io_queued.updated) {
1493             if(unlikely(!cg->st_queued_ops)) {
1494                 cg->st_queued_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "queued_ops");
1495                 if(!cg->st_queued_ops) {
1496                     snprintfz(title, CHART_TITLE_MAX, "Queued I/O Operations (all disks) for cgroup %s", cg->chart_title);
1497                     cg->st_queued_ops = rrdset_create(type, "queued_ops", NULL, "disk", "cgroup.queued_ops", title, "operations", 42000, update_every, RRDSET_TYPE_LINE);
1498
1499                     rrddim_add(cg->st_queued_ops, "read", NULL, 1, 1, RRDDIM_ABSOLUTE);
1500                     rrddim_add(cg->st_queued_ops, "write", NULL, -1, 1, RRDDIM_ABSOLUTE);
1501                 }
1502             }
1503             else rrdset_next(cg->st_queued_ops);
1504
1505             rrddim_set(cg->st_queued_ops, "read", cg->io_queued.Read);
1506             rrddim_set(cg->st_queued_ops, "write", cg->io_queued.Write);
1507             rrdset_done(cg->st_queued_ops);
1508         }
1509
1510         if(cg->io_merged.updated && cg->io_merged.Read + cg->io_merged.Write > 0) {
1511             if(unlikely(!cg->st_merged_ops)) {
1512                 cg->st_merged_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "merged_ops");
1513                 if(!cg->st_merged_ops) {
1514                     snprintfz(title, CHART_TITLE_MAX, "Merged I/O Operations (all disks) for cgroup %s", cg->chart_title);
1515                     cg->st_merged_ops = rrdset_create(type, "merged_ops", NULL, "disk", "cgroup.merged_ops", title, "operations/s", 42100, update_every, RRDSET_TYPE_LINE);
1516
1517                     rrddim_add(cg->st_merged_ops, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
1518                     rrddim_add(cg->st_merged_ops, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
1519                 }
1520             }
1521             else rrdset_next(cg->st_merged_ops);
1522
1523             rrddim_set(cg->st_merged_ops, "read", cg->io_merged.Read);
1524             rrddim_set(cg->st_merged_ops, "write", cg->io_merged.Write);
1525             rrdset_done(cg->st_merged_ops);
1526         }
1527     }
1528
1529     debug(D_CGROUP, "done updating cgroups charts");
1530 }
1531
1532 // ----------------------------------------------------------------------------
1533 // cgroups main
1534
1535 void *cgroups_main(void *ptr) {
1536     struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
1537
1538     info("CGROUP Plugin thread created with task id %d", gettid());
1539
1540     if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
1541         error("Cannot set pthread cancel type to DEFERRED.");
1542
1543     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
1544         error("Cannot set pthread cancel state to ENABLE.");
1545
1546     struct rusage thread;
1547
1548     // when ZERO, attempt to do it
1549     int vdo_cpu_netdata = config_get_boolean("plugin:cgroups", "cgroups plugin resources", 1);
1550
1551     read_cgroup_plugin_configuration();
1552
1553     RRDSET *stcpu_thread = NULL;
1554
1555     usec_t step = cgroup_update_every * USEC_PER_SEC;
1556     usec_t find_every = cgroup_check_for_new_every * USEC_PER_SEC, find_next = 0;
1557     for(;;) {
1558         usec_t now = now_monotonic_usec();
1559         usec_t next = now - (now % step) + step;
1560
1561         while(now < next) {
1562             sleep_usec(next - now);
1563             now = now_monotonic_usec();
1564         }
1565
1566         if(unlikely(netdata_exit)) break;
1567
1568         // BEGIN -- the job to be done
1569
1570         if(unlikely(now >= find_next)) {
1571             find_all_cgroups();
1572             find_next = now + find_every;
1573         }
1574
1575         read_all_cgroups(cgroup_root);
1576         update_cgroup_charts(cgroup_update_every);
1577
1578         // END -- the job is done
1579
1580         // --------------------------------------------------------------------
1581
1582         if(vdo_cpu_netdata) {
1583             getrusage(RUSAGE_THREAD, &thread);
1584
1585             if(!stcpu_thread) stcpu_thread = rrdset_find("netdata.plugin_cgroups_cpu");
1586             if(!stcpu_thread) {
1587                 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);
1588
1589                 rrddim_add(stcpu_thread, "user",  NULL,  1, 1000, RRDDIM_INCREMENTAL);
1590                 rrddim_add(stcpu_thread, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
1591             }
1592             else rrdset_next(stcpu_thread);
1593
1594             rrddim_set(stcpu_thread, "user"  , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
1595             rrddim_set(stcpu_thread, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
1596             rrdset_done(stcpu_thread);
1597         }
1598     }
1599
1600     info("CGROUP thread exiting");
1601
1602     static_thread->enabled = 0;
1603     pthread_exit(NULL);
1604     return NULL;
1605 }