]> arthur.barton.de Git - netdata.git/blob - src/sys_fs_cgroup.c
2050756b0aae4475ac691fbe574cc37abab46bbf
[netdata.git] / src / sys_fs_cgroup.c
1 #include "common.h"
2
3 // ----------------------------------------------------------------------------
4 // cgroup globals
5
6 #define CHART_PRIORITY_SYSTEMD_SERVICES 19000
7 #define CHART_PRIORITY_CONTAINERS       40000
8
9 static long system_page_size = 4096; // system will be queried via sysconf() in configuration()
10
11 static int cgroup_enable_cpuacct_stat = CONFIG_ONDEMAND_ONDEMAND;
12 static int cgroup_enable_cpuacct_usage = CONFIG_ONDEMAND_ONDEMAND;
13 static int cgroup_enable_memory = CONFIG_ONDEMAND_ONDEMAND;
14 static int cgroup_enable_detailed_memory = CONFIG_ONDEMAND_ONDEMAND;
15 static int cgroup_enable_memory_failcnt = CONFIG_ONDEMAND_ONDEMAND;
16 static int cgroup_enable_swap = CONFIG_ONDEMAND_ONDEMAND;
17 static int cgroup_enable_blkio_io = CONFIG_ONDEMAND_ONDEMAND;
18 static int cgroup_enable_blkio_ops = CONFIG_ONDEMAND_ONDEMAND;
19 static int cgroup_enable_blkio_throttle_io = CONFIG_ONDEMAND_ONDEMAND;
20 static int cgroup_enable_blkio_throttle_ops = CONFIG_ONDEMAND_ONDEMAND;
21 static int cgroup_enable_blkio_merged_ops = CONFIG_ONDEMAND_ONDEMAND;
22 static int cgroup_enable_blkio_queued_ops = CONFIG_ONDEMAND_ONDEMAND;
23
24 static int cgroup_enable_systemd_services = CONFIG_ONDEMAND_YES;
25 static int cgroup_enable_systemd_services_detailed_memory = CONFIG_ONDEMAND_NO;
26 static int cgroup_used_memory_without_cache = CONFIG_ONDEMAND_YES;
27
28 static int cgroup_search_in_devices = 1;
29
30 static int cgroup_enable_new_cgroups_detected_at_runtime = 1;
31 static int cgroup_check_for_new_every = 10;
32 static int cgroup_update_every = 1;
33
34 static int cgroup_recheck_zero_blkio_every_iterations = 10;
35 static int cgroup_recheck_zero_mem_failcnt_every_iterations = 10;
36 static int cgroup_recheck_zero_mem_detailed_every_iterations = 10;
37
38 static char *cgroup_cpuacct_base = NULL;
39 static char *cgroup_blkio_base = NULL;
40 static char *cgroup_memory_base = NULL;
41 static char *cgroup_devices_base = NULL;
42
43 static int cgroup_root_count = 0;
44 static int cgroup_root_max = 500;
45 static int cgroup_max_depth = 0;
46
47 static SIMPLE_PATTERN *enabled_cgroup_patterns = NULL;
48 static SIMPLE_PATTERN *enabled_cgroup_paths = NULL;
49 static SIMPLE_PATTERN *enabled_cgroup_renames = NULL;
50 static SIMPLE_PATTERN *systemd_services_cgroups = NULL;
51
52 static char *cgroups_rename_script = PLUGINS_DIR "/cgroup-name.sh";
53
54 static uint32_t Read_hash = 0;
55 static uint32_t Write_hash = 0;
56 static uint32_t user_hash = 0;
57 static uint32_t system_hash = 0;
58
59 void read_cgroup_plugin_configuration() {
60     system_page_size = sysconf(_SC_PAGESIZE);
61
62     Read_hash = simple_hash("Read");
63     Write_hash = simple_hash("Write");
64     user_hash = simple_hash("user");
65     system_hash = simple_hash("system");
66
67     cgroup_update_every = (int)config_get_number("plugin:cgroups", "update every", rrd_update_every);
68     if(cgroup_update_every < rrd_update_every)
69         cgroup_update_every = rrd_update_every;
70
71     cgroup_check_for_new_every = (int)config_get_number("plugin:cgroups", "check for new cgroups every", cgroup_check_for_new_every * cgroup_update_every);
72     if(cgroup_check_for_new_every < cgroup_update_every)
73         cgroup_check_for_new_every = cgroup_update_every;
74
75     cgroup_enable_cpuacct_stat = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct stat (total CPU)", cgroup_enable_cpuacct_stat);
76     cgroup_enable_cpuacct_usage = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct usage (per core CPU)", cgroup_enable_cpuacct_usage);
77
78     cgroup_enable_memory = config_get_boolean_ondemand("plugin:cgroups", "enable memory (used mem including cache)", cgroup_enable_memory);
79     cgroup_enable_detailed_memory = config_get_boolean_ondemand("plugin:cgroups", "enable detailed memory", cgroup_enable_detailed_memory);
80     cgroup_enable_memory_failcnt = config_get_boolean_ondemand("plugin:cgroups", "enable memory limits fail count", cgroup_enable_memory_failcnt);
81     cgroup_enable_swap = config_get_boolean_ondemand("plugin:cgroups", "enable swap memory", cgroup_enable_swap);
82
83     cgroup_enable_blkio_io = config_get_boolean_ondemand("plugin:cgroups", "enable blkio bandwidth", cgroup_enable_blkio_io);
84     cgroup_enable_blkio_ops = config_get_boolean_ondemand("plugin:cgroups", "enable blkio operations", cgroup_enable_blkio_ops);
85     cgroup_enable_blkio_throttle_io = config_get_boolean_ondemand("plugin:cgroups", "enable blkio throttle bandwidth", cgroup_enable_blkio_throttle_io);
86     cgroup_enable_blkio_throttle_ops = config_get_boolean_ondemand("plugin:cgroups", "enable blkio throttle operations", cgroup_enable_blkio_throttle_ops);
87     cgroup_enable_blkio_queued_ops = config_get_boolean_ondemand("plugin:cgroups", "enable blkio queued operations", cgroup_enable_blkio_queued_ops);
88     cgroup_enable_blkio_merged_ops = config_get_boolean_ondemand("plugin:cgroups", "enable blkio merged operations", cgroup_enable_blkio_merged_ops);
89
90     cgroup_recheck_zero_blkio_every_iterations = (int)config_get_number("plugin:cgroups", "recheck zero blkio every iterations", cgroup_recheck_zero_blkio_every_iterations);
91     cgroup_recheck_zero_mem_failcnt_every_iterations = (int)config_get_number("plugin:cgroups", "recheck zero memory failcnt every iterations", cgroup_recheck_zero_mem_failcnt_every_iterations);
92     cgroup_recheck_zero_mem_detailed_every_iterations = (int)config_get_number("plugin:cgroups", "recheck zero detailed memory every iterations", cgroup_recheck_zero_mem_detailed_every_iterations);
93
94     cgroup_enable_systemd_services = config_get_boolean("plugin:cgroups", "enable systemd services", cgroup_enable_systemd_services);
95     cgroup_enable_systemd_services_detailed_memory = config_get_boolean("plugin:cgroups", "enable systemd services detailed memory", cgroup_enable_systemd_services_detailed_memory);
96     cgroup_used_memory_without_cache = config_get_boolean("plugin:cgroups", "report used memory without cache", cgroup_used_memory_without_cache);
97
98     char filename[FILENAME_MAX + 1], *s;
99     struct mountinfo *mi, *root = mountinfo_read(0);
100
101     mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "cpuacct");
102     if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "cpuacct");
103     if(!mi) {
104         error("Cannot find cgroup cpuacct mountinfo. Assuming default: /sys/fs/cgroup/cpuacct");
105         s = "/sys/fs/cgroup/cpuacct";
106     }
107     else s = mi->mount_point;
108     snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
109     cgroup_cpuacct_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/cpuacct", filename);
110
111     mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "blkio");
112     if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "blkio");
113     if(!mi) {
114         error("Cannot find cgroup blkio mountinfo. Assuming default: /sys/fs/cgroup/blkio");
115         s = "/sys/fs/cgroup/blkio";
116     }
117     else s = mi->mount_point;
118     snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
119     cgroup_blkio_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/blkio", filename);
120
121     mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "memory");
122     if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "memory");
123     if(!mi) {
124         error("Cannot find cgroup memory mountinfo. Assuming default: /sys/fs/cgroup/memory");
125         s = "/sys/fs/cgroup/memory";
126     }
127     else s = mi->mount_point;
128     snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
129     cgroup_memory_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/memory", filename);
130
131     mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "devices");
132     if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "devices");
133     if(!mi) {
134         error("Cannot find cgroup devices mountinfo. Assuming default: /sys/fs/cgroup/devices");
135         s = "/sys/fs/cgroup/devices";
136     }
137     else s = mi->mount_point;
138     snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
139     cgroup_devices_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/devices", filename);
140
141     cgroup_root_max = (int)config_get_number("plugin:cgroups", "max cgroups to allow", cgroup_root_max);
142     cgroup_max_depth = (int)config_get_number("plugin:cgroups", "max cgroups depth to monitor", cgroup_max_depth);
143
144     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);
145
146     enabled_cgroup_patterns = simple_pattern_create(
147             config_get("plugin:cgroups", "enable by default cgroups matching",
148                     " /system.slice/docker-*.scope "
149                     " !*.mount "
150                     " !*.partition "
151                     " !*.scope "
152                     " !*.service "
153                     " !*.slice "
154                     " !*.swap "
155                     " !*.user "
156                     " !/ "
157                     " !/docker "
158                     " !/libvirt "
159                     " !/lxc "
160                     " !/lxc/*/ns "         //  #1397
161                     " !/machine "
162                     " !/qemu "
163                     " !/system "
164                     " !/systemd "
165                     " !/user "
166                     " * "                  // enable anything else
167             ), SIMPLE_PATTERN_EXACT);
168
169     enabled_cgroup_paths = simple_pattern_create(
170             config_get("plugin:cgroups", "search for cgroups in subpaths matching",
171                     " !*-qemu "            //  #345
172                     " !/init.scope "
173                     " !/system "
174                     " !/systemd "
175                     " !/user "
176                     " !/user.slice "
177                     " * "
178             ), SIMPLE_PATTERN_EXACT);
179
180     cgroups_rename_script = config_get("plugin:cgroups", "script to get cgroup names", cgroups_rename_script);
181
182     enabled_cgroup_renames = simple_pattern_create(
183             config_get("plugin:cgroups", "run script to rename cgroups matching",
184                     " /system.slice/docker-*.scope "
185                     " !/ "
186                     " !*.mount "
187                     " !*.partition "
188                     " !*.scope "
189                     " !*.service "
190                     " !*.slice "
191                     " !*.swap "
192                     " !*.user "
193                     " * "
194             ), SIMPLE_PATTERN_EXACT);
195
196     if(cgroup_enable_systemd_services) {
197         systemd_services_cgroups = simple_pattern_create(
198                 config_get("plugin:cgroups", "cgroups to match as systemd services",
199                         " !/system.slice/*/*.service "
200                         " /system.slice/*.service "
201                 ), SIMPLE_PATTERN_EXACT);
202     }
203
204     mountinfo_free(root);
205 }
206
207 // ----------------------------------------------------------------------------
208 // cgroup objects
209
210 struct blkio {
211     int updated;
212     int enabled; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
213     int delay_counter;
214
215     char *filename;
216
217     unsigned long long Read;
218     unsigned long long Write;
219 /*
220     unsigned long long Sync;
221     unsigned long long Async;
222     unsigned long long Total;
223 */
224 };
225
226 // https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt
227 struct memory {
228     ARL_BASE *arl_base;
229     ARL_ENTRY *arl_dirty;
230     ARL_ENTRY *arl_swap;
231
232     int updated_detailed;
233     int updated_usage_in_bytes;
234     int updated_msw_usage_in_bytes;
235     int updated_failcnt;
236
237     int enabled_detailed;           // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
238     int enabled_usage_in_bytes;     // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
239     int enabled_msw_usage_in_bytes; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
240     int enabled_failcnt;            // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
241
242     int delay_counter_detailed;
243     int delay_counter_failcnt;
244
245     char *filename_detailed;
246     char *filename_usage_in_bytes;
247     char *filename_msw_usage_in_bytes;
248     char *filename_failcnt;
249
250     int detailed_has_dirty;
251     int detailed_has_swap;
252
253     // detailed metrics
254     unsigned long long cache;
255     unsigned long long rss;
256     unsigned long long rss_huge;
257     unsigned long long mapped_file;
258     unsigned long long writeback;
259     unsigned long long dirty;
260     unsigned long long swap;
261     unsigned long long pgpgin;
262     unsigned long long pgpgout;
263     unsigned long long pgfault;
264     unsigned long long pgmajfault;
265 /*
266     unsigned long long inactive_anon;
267     unsigned long long active_anon;
268     unsigned long long inactive_file;
269     unsigned long long active_file;
270     unsigned long long unevictable;
271     unsigned long long hierarchical_memory_limit;
272     unsigned long long total_cache;
273     unsigned long long total_rss;
274     unsigned long long total_rss_huge;
275     unsigned long long total_mapped_file;
276     unsigned long long total_writeback;
277     unsigned long long total_dirty;
278     unsigned long long total_swap;
279     unsigned long long total_pgpgin;
280     unsigned long long total_pgpgout;
281     unsigned long long total_pgfault;
282     unsigned long long total_pgmajfault;
283     unsigned long long total_inactive_anon;
284     unsigned long long total_active_anon;
285     unsigned long long total_inactive_file;
286     unsigned long long total_active_file;
287     unsigned long long total_unevictable;
288 */
289
290     // single file metrics
291     unsigned long long usage_in_bytes;
292     unsigned long long msw_usage_in_bytes;
293     unsigned long long failcnt;
294 };
295
296 // https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt
297 struct cpuacct_stat {
298     int updated;
299     int enabled; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
300
301     char *filename;
302
303     unsigned long long user;
304     unsigned long long system;
305 };
306
307 // https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt
308 struct cpuacct_usage {
309     int updated;
310     int enabled; // CONFIG_ONDEMAND_YES or CONFIG_ONDEMAND_ONDEMAND
311
312     char *filename;
313
314     unsigned int cpus;
315     unsigned long long *cpu_percpu;
316 };
317
318 #define CGROUP_OPTIONS_DISABLED_DUPLICATE   0x00000001
319 #define CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE 0x00000002
320
321 struct cgroup {
322     uint32_t options;
323
324     char available;      // found in the filesystem
325     char enabled;        // enabled in the config
326
327     char *id;
328     uint32_t hash;
329
330     char *chart_id;
331     uint32_t hash_chart;
332
333     char *chart_title;
334
335     struct cpuacct_stat cpuacct_stat;
336     struct cpuacct_usage cpuacct_usage;
337
338     struct memory memory;
339
340     struct blkio io_service_bytes;              // bytes
341     struct blkio io_serviced;                   // operations
342
343     struct blkio throttle_io_service_bytes;     // bytes
344     struct blkio throttle_io_serviced;          // operations
345
346     struct blkio io_merged;                     // operations
347     struct blkio io_queued;                     // operations
348
349     // per cgroup charts
350     RRDSET *st_cpu;
351     RRDSET *st_cpu_per_core;
352     RRDSET *st_mem;
353     RRDSET *st_writeback;
354     RRDSET *st_mem_activity;
355     RRDSET *st_pgfaults;
356     RRDSET *st_mem_usage;
357     RRDSET *st_mem_failcnt;
358     RRDSET *st_io;
359     RRDSET *st_serviced_ops;
360     RRDSET *st_throttle_io;
361     RRDSET *st_throttle_serviced_ops;
362     RRDSET *st_queued_ops;
363     RRDSET *st_merged_ops;
364
365     // services
366     RRDDIM *rd_cpu;
367     RRDDIM *rd_mem_usage;
368     RRDDIM *rd_mem_failcnt;
369     RRDDIM *rd_swap_usage;
370
371     RRDDIM *rd_mem_detailed_cache;
372     RRDDIM *rd_mem_detailed_rss;
373     RRDDIM *rd_mem_detailed_mapped;
374     RRDDIM *rd_mem_detailed_writeback;
375     RRDDIM *rd_mem_detailed_pgpgin;
376     RRDDIM *rd_mem_detailed_pgpgout;
377     RRDDIM *rd_mem_detailed_pgfault;
378     RRDDIM *rd_mem_detailed_pgmajfault;
379
380     RRDDIM *rd_io_service_bytes_read;
381     RRDDIM *rd_io_serviced_read;
382     RRDDIM *rd_throttle_io_read;
383     RRDDIM *rd_throttle_io_serviced_read;
384     RRDDIM *rd_io_queued_read;
385     RRDDIM *rd_io_merged_read;
386
387     RRDDIM *rd_io_service_bytes_write;
388     RRDDIM *rd_io_serviced_write;
389     RRDDIM *rd_throttle_io_write;
390     RRDDIM *rd_throttle_io_serviced_write;
391     RRDDIM *rd_io_queued_write;
392     RRDDIM *rd_io_merged_write;
393
394     struct cgroup *next;
395
396 } *cgroup_root = NULL;
397
398 // ----------------------------------------------------------------------------
399 // read values from /sys
400
401 static inline void cgroup_read_cpuacct_stat(struct cpuacct_stat *cp) {
402     static procfile *ff = NULL;
403
404     if(likely(cp->filename)) {
405         ff = procfile_reopen(ff, cp->filename, NULL, PROCFILE_FLAG_DEFAULT);
406         if(unlikely(!ff)) {
407             cp->updated = 0;
408             return;
409         }
410
411         ff = procfile_readall(ff);
412         if(unlikely(!ff)) {
413             cp->updated = 0;
414             return;
415         }
416
417         unsigned long i, lines = procfile_lines(ff);
418
419         if(unlikely(lines < 1)) {
420             error("File '%s' should have 1+ lines.", cp->filename);
421             cp->updated = 0;
422             return;
423         }
424
425         for(i = 0; i < lines ; i++) {
426             char *s = procfile_lineword(ff, i, 0);
427             uint32_t hash = simple_hash(s);
428
429             if(unlikely(hash == user_hash && !strcmp(s, "user")))
430                 cp->user = str2ull(procfile_lineword(ff, i, 1));
431
432             else if(unlikely(hash == system_hash && !strcmp(s, "system")))
433                 cp->system = str2ull(procfile_lineword(ff, i, 1));
434         }
435
436         cp->updated = 1;
437
438         if(unlikely(cp->enabled == CONFIG_ONDEMAND_ONDEMAND && (cp->user || cp->system)))
439             cp->enabled = CONFIG_ONDEMAND_YES;
440     }
441 }
442
443 static inline void cgroup_read_cpuacct_usage(struct cpuacct_usage *ca) {
444     static procfile *ff = NULL;
445
446     if(likely(ca->filename)) {
447         ff = procfile_reopen(ff, ca->filename, NULL, PROCFILE_FLAG_DEFAULT);
448         if(unlikely(!ff)) {
449             ca->updated = 0;
450             return;
451         }
452
453         ff = procfile_readall(ff);
454         if(unlikely(!ff)) {
455             ca->updated = 0;
456             return;
457         }
458
459         if(unlikely(procfile_lines(ff) < 1)) {
460             error("File '%s' should have 1+ lines but has %zu.", ca->filename, procfile_lines(ff));
461             ca->updated = 0;
462             return;
463         }
464
465         unsigned long i = procfile_linewords(ff, 0);
466         if(unlikely(i == 0)) {
467             return;
468             ca->updated = 0;
469         }
470
471         // we may have 1 more CPU reported
472         while(i > 0) {
473             char *s = procfile_lineword(ff, 0, i - 1);
474             if(!*s) i--;
475             else break;
476         }
477
478         if(unlikely(i != ca->cpus)) {
479             freez(ca->cpu_percpu);
480             ca->cpu_percpu = mallocz(sizeof(unsigned long long) * i);
481             ca->cpus = (unsigned int)i;
482         }
483
484         unsigned long long total = 0;
485         for(i = 0; i < ca->cpus ;i++) {
486             unsigned long long n = str2ull(procfile_lineword(ff, 0, i));
487             ca->cpu_percpu[i] = n;
488             total += n;
489         }
490
491         ca->updated = 1;
492
493         if(unlikely(ca->enabled == CONFIG_ONDEMAND_ONDEMAND && total))
494             ca->enabled = CONFIG_ONDEMAND_YES;
495     }
496 }
497
498 static inline void cgroup_read_blkio(struct blkio *io) {
499     static procfile *ff = NULL;
500
501     if(unlikely(io->enabled == CONFIG_ONDEMAND_ONDEMAND && io->delay_counter > 0)) {
502         io->delay_counter--;
503         return;
504     }
505
506     if(likely(io->filename)) {
507         ff = procfile_reopen(ff, io->filename, NULL, PROCFILE_FLAG_DEFAULT);
508         if(unlikely(!ff)) {
509             io->updated = 0;
510             return;
511         }
512
513         ff = procfile_readall(ff);
514         if(unlikely(!ff)) {
515             io->updated = 0;
516             return;
517         }
518
519         unsigned long i, lines = procfile_lines(ff);
520
521         if(unlikely(lines < 1)) {
522             error("File '%s' should have 1+ lines.", io->filename);
523             io->updated = 0;
524             return;
525         }
526
527         io->Read = 0;
528         io->Write = 0;
529 /*
530         io->Sync = 0;
531         io->Async = 0;
532         io->Total = 0;
533 */
534
535         for(i = 0; i < lines ; i++) {
536             char *s = procfile_lineword(ff, i, 1);
537             uint32_t hash = simple_hash(s);
538
539             if(unlikely(hash == Read_hash && !strcmp(s, "Read")))
540                 io->Read += str2ull(procfile_lineword(ff, i, 2));
541
542             else if(unlikely(hash == Write_hash && !strcmp(s, "Write")))
543                 io->Write += str2ull(procfile_lineword(ff, i, 2));
544
545 /*
546             else if(unlikely(hash == Sync_hash && !strcmp(s, "Sync")))
547                 io->Sync += str2ull(procfile_lineword(ff, i, 2));
548
549             else if(unlikely(hash == Async_hash && !strcmp(s, "Async")))
550                 io->Async += str2ull(procfile_lineword(ff, i, 2));
551
552             else if(unlikely(hash == Total_hash && !strcmp(s, "Total")))
553                 io->Total += str2ull(procfile_lineword(ff, i, 2));
554 */
555         }
556
557         io->updated = 1;
558
559         if(unlikely(io->enabled == CONFIG_ONDEMAND_ONDEMAND)) {
560             if(unlikely(io->Read || io->Write))
561                 io->enabled = CONFIG_ONDEMAND_YES;
562             else
563                 io->delay_counter = cgroup_recheck_zero_blkio_every_iterations;
564         }
565     }
566 }
567
568 static inline void cgroup_read_memory(struct memory *mem) {
569     static procfile *ff = NULL;
570
571     // read detailed ram usage
572     if(likely(mem->filename_detailed)) {
573         if(unlikely(mem->enabled_detailed == CONFIG_ONDEMAND_ONDEMAND && mem->delay_counter_detailed > 0)) {
574             mem->delay_counter_detailed--;
575             goto memory_next;
576         }
577
578         ff = procfile_reopen(ff, mem->filename_detailed, NULL, PROCFILE_FLAG_DEFAULT);
579         if(unlikely(!ff)) {
580             mem->updated_detailed = 0;
581             goto memory_next;
582         }
583
584         ff = procfile_readall(ff);
585         if(unlikely(!ff)) {
586             mem->updated_detailed = 0;
587             goto memory_next;
588         }
589
590         unsigned long i, lines = procfile_lines(ff);
591
592         if(unlikely(lines < 1)) {
593             error("File '%s' should have 1+ lines.", mem->filename_detailed);
594             mem->updated_detailed = 0;
595             goto memory_next;
596         }
597
598         if(unlikely(!mem->arl_base)) {
599             mem->arl_base = arl_create("cgroup/memory", NULL, 60);
600
601             arl_expect(mem->arl_base, "cache", &mem->cache);
602             arl_expect(mem->arl_base, "rss", &mem->rss);
603             arl_expect(mem->arl_base, "rss_huge", &mem->rss_huge);
604             arl_expect(mem->arl_base, "mapped_file", &mem->mapped_file);
605             arl_expect(mem->arl_base, "writeback", &mem->writeback);
606             mem->arl_dirty = arl_expect(mem->arl_base, "dirty", &mem->dirty);
607             mem->arl_swap  = arl_expect(mem->arl_base, "swap", &mem->swap);
608             arl_expect(mem->arl_base, "pgpgin", &mem->pgpgin);
609             arl_expect(mem->arl_base, "pgpgout", &mem->pgpgout);
610             arl_expect(mem->arl_base, "pgfault", &mem->pgfault);
611             arl_expect(mem->arl_base, "pgmajfault", &mem->pgmajfault);
612         }
613
614         arl_begin(mem->arl_base);
615
616         for(i = 0; i < lines ; i++) {
617             if(arl_check(mem->arl_base,
618                     procfile_lineword(ff, i, 0),
619                     procfile_lineword(ff, i, 1))) break;
620         }
621
622         if(unlikely(mem->arl_dirty->flags & ARL_ENTRY_FLAG_FOUND))
623             mem->detailed_has_dirty = 1;
624
625         if(unlikely(mem->arl_swap->flags & ARL_ENTRY_FLAG_FOUND))
626             mem->detailed_has_swap = 1;
627
628         // 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);
629
630         mem->updated_detailed = 1;
631
632         if(unlikely(mem->enabled_detailed == CONFIG_ONDEMAND_ONDEMAND)) {
633             if(mem->cache || mem->dirty || mem->rss || mem->rss_huge || mem->mapped_file || mem->writeback || mem->swap || mem->pgpgin || mem->pgpgout || mem->pgfault || mem->pgmajfault)
634                 mem->enabled_detailed = CONFIG_ONDEMAND_YES;
635             else
636                 mem->delay_counter_detailed = cgroup_recheck_zero_mem_detailed_every_iterations;
637         }
638     }
639
640 memory_next:
641
642     // read usage_in_bytes
643     if(likely(mem->filename_usage_in_bytes)) {
644         mem->updated_usage_in_bytes = !read_single_number_file(mem->filename_usage_in_bytes, &mem->usage_in_bytes);
645         if(unlikely(mem->updated_usage_in_bytes && mem->enabled_usage_in_bytes == CONFIG_ONDEMAND_ONDEMAND && mem->usage_in_bytes))
646             mem->enabled_usage_in_bytes = CONFIG_ONDEMAND_YES;
647     }
648
649     // read msw_usage_in_bytes
650     if(likely(mem->filename_msw_usage_in_bytes)) {
651         mem->updated_msw_usage_in_bytes = !read_single_number_file(mem->filename_msw_usage_in_bytes, &mem->msw_usage_in_bytes);
652         if(unlikely(mem->updated_msw_usage_in_bytes && mem->enabled_msw_usage_in_bytes == CONFIG_ONDEMAND_ONDEMAND && mem->msw_usage_in_bytes))
653             mem->enabled_msw_usage_in_bytes = CONFIG_ONDEMAND_YES;
654     }
655
656     // read failcnt
657     if(likely(mem->filename_failcnt)) {
658         if(unlikely(mem->enabled_failcnt == CONFIG_ONDEMAND_ONDEMAND && mem->delay_counter_failcnt > 0)) {
659             mem->updated_failcnt = 0;
660             mem->delay_counter_failcnt--;
661         }
662         else {
663             mem->updated_failcnt = !read_single_number_file(mem->filename_failcnt, &mem->failcnt);
664             if(unlikely(mem->updated_failcnt && mem->enabled_failcnt == CONFIG_ONDEMAND_ONDEMAND)) {
665                 if(unlikely(!mem->failcnt))
666                     mem->delay_counter_failcnt = cgroup_recheck_zero_mem_failcnt_every_iterations;
667                 else
668                     mem->enabled_failcnt = CONFIG_ONDEMAND_YES;
669             }
670         }
671     }
672 }
673
674 static inline void cgroup_read(struct cgroup *cg) {
675     debug(D_CGROUP, "reading metrics for cgroups '%s'", cg->id);
676
677     cgroup_read_cpuacct_stat(&cg->cpuacct_stat);
678     cgroup_read_cpuacct_usage(&cg->cpuacct_usage);
679     cgroup_read_memory(&cg->memory);
680     cgroup_read_blkio(&cg->io_service_bytes);
681     cgroup_read_blkio(&cg->io_serviced);
682     cgroup_read_blkio(&cg->throttle_io_service_bytes);
683     cgroup_read_blkio(&cg->throttle_io_serviced);
684     cgroup_read_blkio(&cg->io_merged);
685     cgroup_read_blkio(&cg->io_queued);
686 }
687
688 static inline void read_all_cgroups(struct cgroup *root) {
689     debug(D_CGROUP, "reading metrics for all cgroups");
690
691     struct cgroup *cg;
692
693     for(cg = root; cg ; cg = cg->next)
694         if(cg->enabled && cg->available)
695             cgroup_read(cg);
696 }
697
698 // ----------------------------------------------------------------------------
699 // add/remove/find cgroup objects
700
701 #define CGROUP_CHARTID_LINE_MAX 1024
702
703 static inline char *cgroup_title_strdupz(const char *s) {
704     if(!s || !*s) s = "/";
705
706     if(*s == '/' && s[1] != '\0') s++;
707
708     char *r = strdupz(s);
709     netdata_fix_chart_name(r);
710
711     return r;
712 }
713
714 static inline char *cgroup_chart_id_strdupz(const char *s) {
715     if(!s || !*s) s = "/";
716
717     if(*s == '/' && s[1] != '\0') s++;
718
719     char *r = strdupz(s);
720     netdata_fix_chart_id(r);
721
722     return r;
723 }
724
725 static inline void cgroup_get_chart_name(struct cgroup *cg) {
726     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);
727
728     pid_t cgroup_pid;
729     char buffer[CGROUP_CHARTID_LINE_MAX + 1];
730
731     snprintfz(buffer, CGROUP_CHARTID_LINE_MAX, "exec %s '%s'", cgroups_rename_script, cg->chart_id);
732
733     debug(D_CGROUP, "executing command '%s' for cgroup '%s'", buffer, cg->id);
734     FILE *fp = mypopen(buffer, &cgroup_pid);
735     if(fp) {
736         // debug(D_CGROUP, "reading from command '%s' for cgroup '%s'", buffer, cg->id);
737         char *s = fgets(buffer, CGROUP_CHARTID_LINE_MAX, fp);
738         // debug(D_CGROUP, "closing command for cgroup '%s'", cg->id);
739         mypclose(fp, cgroup_pid);
740         // debug(D_CGROUP, "closed command for cgroup '%s'", cg->id);
741
742         if(s && *s && *s != '\n') {
743             debug(D_CGROUP, "cgroup '%s' should be renamed to '%s'", cg->id, s);
744
745             trim(s);
746
747             freez(cg->chart_title);
748             cg->chart_title = cgroup_title_strdupz(s);
749
750             freez(cg->chart_id);
751             cg->chart_id = cgroup_chart_id_strdupz(s);
752             cg->hash_chart = simple_hash(cg->chart_id);
753         }
754     }
755     else
756         error("CGROUP: Cannot popen(\"%s\", \"r\").", buffer);
757 }
758
759 static inline struct cgroup *cgroup_add(const char *id) {
760     if(!id || !*id) id = "/";
761     debug(D_CGROUP, "adding to list, cgroup with id '%s'", id);
762
763     if(cgroup_root_count >= cgroup_root_max) {
764         info("Maximum number of cgroups reached (%d). Not adding cgroup '%s'", cgroup_root_count, id);
765         return NULL;
766     }
767
768     int def = simple_pattern_matches(enabled_cgroup_patterns, id)?cgroup_enable_new_cgroups_detected_at_runtime:0;
769     struct cgroup *cg = callocz(1, sizeof(struct cgroup));
770
771     cg->id = strdupz(id);
772     cg->hash = simple_hash(cg->id);
773
774     cg->chart_title = cgroup_title_strdupz(id);
775
776     cg->chart_id = cgroup_chart_id_strdupz(id);
777     cg->hash_chart = simple_hash(cg->chart_id);
778
779     if(!cgroup_root)
780         cgroup_root = cg;
781     else {
782         // append it
783         struct cgroup *e;
784         for(e = cgroup_root; e->next ;e = e->next) ;
785         e->next = cg;
786     }
787
788     cgroup_root_count++;
789
790     // fix the chart_id and title by calling the external script
791     if(simple_pattern_matches(enabled_cgroup_renames, cg->id)) {
792
793         cgroup_get_chart_name(cg);
794
795         debug(D_CGROUP, "cgroup '%s' renamed to '%s' (title: '%s')", cg->id, cg->chart_id, cg->chart_title);
796     }
797     else
798         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);
799
800     int user_configurable = 1;
801
802     // check if this cgroup should be a systemd service
803     if(cgroup_enable_systemd_services) {
804         if(simple_pattern_matches(systemd_services_cgroups, cg->id) ||
805                 simple_pattern_matches(systemd_services_cgroups, cg->chart_id)) {
806             debug(D_CGROUP, "cgroup '%s' with chart id '%s' (title: '%s') matches systemd services cgroups", cg->id, cg->chart_id, cg->chart_title);
807
808             char buffer[CGROUP_CHARTID_LINE_MAX + 1];
809             cg->options |= CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE;
810
811             strncpy(buffer, cg->id, CGROUP_CHARTID_LINE_MAX);
812             char *s = buffer;
813
814             //freez(cg->chart_id);
815             //cg->chart_id = cgroup_chart_id_strdupz(s);
816             //cg->hash_chart = simple_hash(cg->chart_id);
817
818             // skip to the last slash
819             size_t len = strlen(s);
820             while(len--) if(unlikely(s[len] == '/')) break;
821             if(len) s = &s[len + 1];
822
823             // remove extension
824             len = strlen(s);
825             while(len--) if(unlikely(s[len] == '.')) break;
826             if(len) s[len] = '\0';
827
828             freez(cg->chart_title);
829             cg->chart_title = cgroup_title_strdupz(s);
830
831             cg->enabled = 1;
832             user_configurable = 0;
833
834             debug(D_CGROUP, "cgroup '%s' renamed to '%s' (title: '%s')", cg->id, cg->chart_id, cg->chart_title);
835         }
836         else
837             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);
838     }
839
840     if(user_configurable) {
841         // allow the user to enable/disable this individualy
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
847     // detect duplicate cgroups
848     if(cg->enabled) {
849         struct cgroup *t;
850         for (t = cgroup_root; t; t = t->next) {
851             if (t != cg && t->enabled && t->hash_chart == cg->hash_chart && !strcmp(t->chart_id, cg->chart_id)) {
852                 if (!strncmp(t->chart_id, "/system.slice/", 14) && !strncmp(cg->chart_id, "/init.scope/system.slice/", 25)) {
853                     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'.",
854                           cg->chart_id, t->id, cg->id, t->id);
855                     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'.",
856                           cg->chart_id, t->id, cg->id, t->id);
857                     t->enabled = 0;
858                     t->options |= CGROUP_OPTIONS_DISABLED_DUPLICATE;
859                 }
860                 else {
861                     error("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                     debug(D_CGROUP, "Control group with chart id '%s' already exists with id '%s' and is enabled and available. Disabling cgroup with id '%s'.",
864                           cg->chart_id, t->id, cg->id);
865                     cg->enabled = 0;
866                     cg->options |= CGROUP_OPTIONS_DISABLED_DUPLICATE;
867                 }
868
869                 break;
870             }
871         }
872     }
873
874     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");
875
876     return cg;
877 }
878
879 static inline void cgroup_free(struct cgroup *cg) {
880     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");
881
882     freez(cg->cpuacct_usage.cpu_percpu);
883
884     freez(cg->cpuacct_stat.filename);
885     freez(cg->cpuacct_usage.filename);
886
887     arl_free(cg->memory.arl_base);
888     freez(cg->memory.filename_detailed);
889     freez(cg->memory.filename_failcnt);
890     freez(cg->memory.filename_usage_in_bytes);
891     freez(cg->memory.filename_msw_usage_in_bytes);
892
893     freez(cg->io_service_bytes.filename);
894     freez(cg->io_serviced.filename);
895
896     freez(cg->throttle_io_service_bytes.filename);
897     freez(cg->throttle_io_serviced.filename);
898
899     freez(cg->io_merged.filename);
900     freez(cg->io_queued.filename);
901
902     freez(cg->id);
903     freez(cg->chart_id);
904     freez(cg->chart_title);
905
906     freez(cg);
907
908     cgroup_root_count--;
909 }
910
911 // find if a given cgroup exists
912 static inline struct cgroup *cgroup_find(const char *id) {
913     debug(D_CGROUP, "searching for cgroup '%s'", id);
914
915     uint32_t hash = simple_hash(id);
916
917     struct cgroup *cg;
918     for(cg = cgroup_root; cg ; cg = cg->next) {
919         if(hash == cg->hash && strcmp(id, cg->id) == 0)
920             break;
921     }
922
923     debug(D_CGROUP, "cgroup '%s' %s in memory", id, (cg)?"found":"not found");
924     return cg;
925 }
926
927 // ----------------------------------------------------------------------------
928 // detect running cgroups
929
930 // callback for find_file_in_subdirs()
931 static inline void found_subdir_in_dir(const char *dir) {
932     debug(D_CGROUP, "examining cgroup dir '%s'", dir);
933
934     struct cgroup *cg = cgroup_find(dir);
935     if(!cg) {
936         if(*dir && cgroup_max_depth > 0) {
937             int depth = 0;
938             const char *s;
939
940             for(s = dir; *s ;s++)
941                 if(unlikely(*s == '/'))
942                     depth++;
943
944             if(depth > cgroup_max_depth) {
945                 info("cgroup '%s' is too deep (%d, while max is %d)", dir, depth, cgroup_max_depth);
946                 return;
947             }
948         }
949         // debug(D_CGROUP, "will add dir '%s' as cgroup", dir);
950         cg = cgroup_add(dir);
951     }
952
953     if(cg) cg->available = 1;
954 }
955
956 static inline int find_dir_in_subdirs(const char *base, const char *this, void (*callback)(const char *)) {
957     if(!this) this = base;
958     debug(D_CGROUP, "searching for directories in '%s' (base '%s')", this?this:"", base);
959
960     size_t dirlen = strlen(this), baselen = strlen(base);
961
962     int ret = -1;
963     int enabled = -1;
964
965     const char *relative_path = &this[baselen];
966     if(!*relative_path) relative_path = "/";
967
968     DIR *dir = opendir(this);
969     if(!dir) {
970         error("Cannot read cgroups directory '%s'", base);
971         return ret;
972     }
973     ret = 1;
974
975     callback(relative_path);
976
977     struct dirent *de = NULL;
978     while((de = readdir(dir))) {
979         if(de->d_type == DT_DIR
980             && (
981                 (de->d_name[0] == '.' && de->d_name[1] == '\0')
982                 || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
983                 ))
984             continue;
985
986         if(de->d_type == DT_DIR) {
987             if(enabled == -1) {
988                 const char *r = relative_path;
989                 if(*r == '\0') r = "/";
990
991                 // do not decent in directories we are not interested
992                 int def = simple_pattern_matches(enabled_cgroup_paths, r);
993
994                 // we check for this option here
995                 // so that the config will not have settings
996                 // for leaf directories
997                 char option[FILENAME_MAX + 1];
998                 snprintfz(option, FILENAME_MAX, "search for cgroups under %s", r);
999                 option[FILENAME_MAX] = '\0';
1000                 enabled = config_get_boolean("plugin:cgroups", option, def);
1001             }
1002
1003             if(enabled) {
1004                 char *s = mallocz(dirlen + strlen(de->d_name) + 2);
1005                 strcpy(s, this);
1006                 strcat(s, "/");
1007                 strcat(s, de->d_name);
1008                 int ret2 = find_dir_in_subdirs(base, s, callback);
1009                 if(ret2 > 0) ret += ret2;
1010                 freez(s);
1011             }
1012         }
1013     }
1014
1015     closedir(dir);
1016     return ret;
1017 }
1018
1019 static inline void mark_all_cgroups_as_not_available() {
1020     debug(D_CGROUP, "marking all cgroups as not available");
1021
1022     struct cgroup *cg;
1023
1024     // mark all as not available
1025     for(cg = cgroup_root; cg ; cg = cg->next) {
1026         cg->available = 0;
1027     }
1028 }
1029
1030 static inline void cleanup_all_cgroups() {
1031     struct cgroup *cg = cgroup_root, *last = NULL;
1032
1033     for(; cg ;) {
1034         if(!cg->available) {
1035             // enable the first duplicate cgroup
1036             {
1037                 struct cgroup *t;
1038                 for(t = cgroup_root; t ; t = t->next) {
1039                     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)) {
1040                         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);
1041                         t->enabled = 1;
1042                         t->options &= ~CGROUP_OPTIONS_DISABLED_DUPLICATE;
1043                         break;
1044                     }
1045                 }
1046             }
1047
1048             if(!last)
1049                 cgroup_root = cg->next;
1050             else
1051                 last->next = cg->next;
1052
1053             cgroup_free(cg);
1054
1055             if(!last)
1056                 cg = cgroup_root;
1057             else
1058                 cg = last->next;
1059         }
1060         else {
1061             last = cg;
1062             cg = cg->next;
1063         }
1064     }
1065 }
1066
1067 static inline void find_all_cgroups() {
1068     debug(D_CGROUP, "searching for cgroups");
1069
1070     mark_all_cgroups_as_not_available();
1071
1072     if(cgroup_enable_cpuacct_stat || cgroup_enable_cpuacct_usage) {
1073         if(find_dir_in_subdirs(cgroup_cpuacct_base, NULL, found_subdir_in_dir) == -1) {
1074             cgroup_enable_cpuacct_stat =
1075             cgroup_enable_cpuacct_usage = CONFIG_ONDEMAND_NO;
1076             error("disabled CGROUP cpu statistics.");
1077         }
1078     }
1079
1080     if(cgroup_enable_blkio_io || cgroup_enable_blkio_ops || cgroup_enable_blkio_throttle_io || cgroup_enable_blkio_throttle_ops || cgroup_enable_blkio_merged_ops || cgroup_enable_blkio_queued_ops) {
1081         if(find_dir_in_subdirs(cgroup_blkio_base, NULL, found_subdir_in_dir) == -1) {
1082             cgroup_enable_blkio_io =
1083             cgroup_enable_blkio_ops =
1084             cgroup_enable_blkio_throttle_io =
1085             cgroup_enable_blkio_throttle_ops =
1086             cgroup_enable_blkio_merged_ops =
1087             cgroup_enable_blkio_queued_ops = CONFIG_ONDEMAND_NO;
1088             error("disabled CGROUP blkio statistics.");
1089         }
1090     }
1091
1092     if(cgroup_enable_memory || cgroup_enable_detailed_memory || cgroup_enable_swap || cgroup_enable_memory_failcnt) {
1093         if(find_dir_in_subdirs(cgroup_memory_base, NULL, found_subdir_in_dir) == -1) {
1094             cgroup_enable_memory =
1095             cgroup_enable_detailed_memory =
1096             cgroup_enable_swap =
1097             cgroup_enable_memory_failcnt = CONFIG_ONDEMAND_NO;
1098             error("disabled CGROUP memory statistics.");
1099         }
1100     }
1101
1102     if(cgroup_search_in_devices) {
1103         if(find_dir_in_subdirs(cgroup_devices_base, NULL, found_subdir_in_dir) == -1) {
1104             cgroup_search_in_devices = 0;
1105             error("disabled CGROUP devices statistics.");
1106         }
1107     }
1108
1109     // remove any non-existing cgroups
1110     cleanup_all_cgroups();
1111
1112     struct cgroup *cg;
1113     struct stat buf;
1114     for(cg = cgroup_root; cg ; cg = cg->next) {
1115         // fprintf(stderr, " >>> CGROUP '%s' (%u - %s) with name '%s'\n", cg->id, cg->hash, cg->available?"available":"stopped", cg->name);
1116
1117         if(unlikely(!cg->available))
1118             continue;
1119
1120         debug(D_CGROUP, "checking paths for cgroup '%s'", cg->id);
1121
1122         // check for newly added cgroups
1123         // and update the filenames they read
1124         char filename[FILENAME_MAX + 1];
1125         if(unlikely(cgroup_enable_cpuacct_stat && !cg->cpuacct_stat.filename)) {
1126             snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.stat", cgroup_cpuacct_base, cg->id);
1127             if(likely(stat(filename, &buf) != -1)) {
1128                 cg->cpuacct_stat.filename = strdupz(filename);
1129                 cg->cpuacct_stat.enabled = cgroup_enable_cpuacct_stat;
1130                 debug(D_CGROUP, "cpuacct.stat filename for cgroup '%s': '%s'", cg->id, cg->cpuacct_stat.filename);
1131             }
1132             else
1133                 debug(D_CGROUP, "cpuacct.stat file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1134         }
1135
1136         if(unlikely(cgroup_enable_cpuacct_usage && !cg->cpuacct_usage.filename && !(cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE))) {
1137             snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.usage_percpu", cgroup_cpuacct_base, cg->id);
1138             if(likely(stat(filename, &buf) != -1)) {
1139                 cg->cpuacct_usage.filename = strdupz(filename);
1140                 cg->cpuacct_usage.enabled = cgroup_enable_cpuacct_usage;
1141                 debug(D_CGROUP, "cpuacct.usage_percpu filename for cgroup '%s': '%s'", cg->id, cg->cpuacct_usage.filename);
1142             }
1143             else
1144                 debug(D_CGROUP, "cpuacct.usage_percpu file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1145         }
1146
1147         if(unlikely((cgroup_enable_detailed_memory || cgroup_used_memory_without_cache) && !cg->memory.filename_detailed && (cgroup_used_memory_without_cache || cgroup_enable_systemd_services_detailed_memory || !(cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE)))) {
1148             snprintfz(filename, FILENAME_MAX, "%s%s/memory.stat", cgroup_memory_base, cg->id);
1149             if(likely(stat(filename, &buf) != -1)) {
1150                 cg->memory.filename_detailed = strdupz(filename);
1151                 cg->memory.enabled_detailed = (cgroup_enable_detailed_memory == CONFIG_ONDEMAND_YES)?CONFIG_ONDEMAND_YES:CONFIG_ONDEMAND_ONDEMAND;
1152                 debug(D_CGROUP, "memory.stat filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_detailed);
1153             }
1154             else
1155                 debug(D_CGROUP, "memory.stat file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1156         }
1157
1158         if(unlikely(cgroup_enable_memory && !cg->memory.filename_usage_in_bytes)) {
1159             snprintfz(filename, FILENAME_MAX, "%s%s/memory.usage_in_bytes", cgroup_memory_base, cg->id);
1160             if(likely(stat(filename, &buf) != -1)) {
1161                 cg->memory.filename_usage_in_bytes = strdupz(filename);
1162                 cg->memory.enabled_usage_in_bytes = cgroup_enable_memory;
1163                 debug(D_CGROUP, "memory.usage_in_bytes filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_usage_in_bytes);
1164             }
1165             else
1166                 debug(D_CGROUP, "memory.usage_in_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1167         }
1168
1169         if(unlikely(cgroup_enable_swap && !cg->memory.filename_msw_usage_in_bytes)) {
1170             snprintfz(filename, FILENAME_MAX, "%s%s/memory.msw_usage_in_bytes", cgroup_memory_base, cg->id);
1171             if(likely(stat(filename, &buf) != -1)) {
1172                 cg->memory.filename_msw_usage_in_bytes = strdupz(filename);
1173                 cg->memory.enabled_msw_usage_in_bytes = cgroup_enable_swap;
1174                 debug(D_CGROUP, "memory.msw_usage_in_bytes filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_msw_usage_in_bytes);
1175             }
1176             else
1177                 debug(D_CGROUP, "memory.msw_usage_in_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1178         }
1179
1180         if(unlikely(cgroup_enable_memory_failcnt && !cg->memory.filename_failcnt)) {
1181             snprintfz(filename, FILENAME_MAX, "%s%s/memory.failcnt", cgroup_memory_base, cg->id);
1182             if(likely(stat(filename, &buf) != -1)) {
1183                 cg->memory.filename_failcnt = strdupz(filename);
1184                 cg->memory.enabled_failcnt = cgroup_enable_memory_failcnt;
1185                 debug(D_CGROUP, "memory.failcnt filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_failcnt);
1186             }
1187             else
1188                 debug(D_CGROUP, "memory.failcnt file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1189         }
1190
1191         if(unlikely(cgroup_enable_blkio_io && !cg->io_service_bytes.filename)) {
1192             snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_service_bytes", cgroup_blkio_base, cg->id);
1193             if(likely(stat(filename, &buf) != -1)) {
1194                 cg->io_service_bytes.filename = strdupz(filename);
1195                 cg->io_service_bytes.enabled = cgroup_enable_blkio_io;
1196                 debug(D_CGROUP, "io_service_bytes filename for cgroup '%s': '%s'", cg->id, cg->io_service_bytes.filename);
1197             }
1198             else
1199                 debug(D_CGROUP, "io_service_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1200         }
1201
1202         if(unlikely(cgroup_enable_blkio_ops && !cg->io_serviced.filename)) {
1203             snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_serviced", cgroup_blkio_base, cg->id);
1204             if(likely(stat(filename, &buf) != -1)) {
1205                 cg->io_serviced.filename = strdupz(filename);
1206                 cg->io_serviced.enabled = cgroup_enable_blkio_ops;
1207                 debug(D_CGROUP, "io_serviced filename for cgroup '%s': '%s'", cg->id, cg->io_serviced.filename);
1208             }
1209             else
1210                 debug(D_CGROUP, "io_serviced file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1211         }
1212
1213         if(unlikely(cgroup_enable_blkio_throttle_io && !cg->throttle_io_service_bytes.filename)) {
1214             snprintfz(filename, FILENAME_MAX, "%s%s/blkio.throttle.io_service_bytes", cgroup_blkio_base, cg->id);
1215             if(likely(stat(filename, &buf) != -1)) {
1216                 cg->throttle_io_service_bytes.filename = strdupz(filename);
1217                 cg->throttle_io_service_bytes.enabled = cgroup_enable_blkio_throttle_io;
1218                 debug(D_CGROUP, "throttle_io_service_bytes filename for cgroup '%s': '%s'", cg->id, cg->throttle_io_service_bytes.filename);
1219             }
1220             else
1221                 debug(D_CGROUP, "throttle_io_service_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1222         }
1223
1224         if(unlikely(cgroup_enable_blkio_throttle_ops && !cg->throttle_io_serviced.filename)) {
1225             snprintfz(filename, FILENAME_MAX, "%s%s/blkio.throttle.io_serviced", cgroup_blkio_base, cg->id);
1226             if(likely(stat(filename, &buf) != -1)) {
1227                 cg->throttle_io_serviced.filename = strdupz(filename);
1228                 cg->throttle_io_serviced.enabled = cgroup_enable_blkio_throttle_ops;
1229                 debug(D_CGROUP, "throttle_io_serviced filename for cgroup '%s': '%s'", cg->id, cg->throttle_io_serviced.filename);
1230             }
1231             else
1232                 debug(D_CGROUP, "throttle_io_serviced file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1233         }
1234
1235         if(unlikely(cgroup_enable_blkio_merged_ops && !cg->io_merged.filename)) {
1236             snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_merged", cgroup_blkio_base, cg->id);
1237             if(likely(stat(filename, &buf) != -1)) {
1238                 cg->io_merged.filename = strdupz(filename);
1239                 cg->io_merged.enabled = cgroup_enable_blkio_merged_ops;
1240                 debug(D_CGROUP, "io_merged filename for cgroup '%s': '%s'", cg->id, cg->io_merged.filename);
1241             }
1242             else
1243                 debug(D_CGROUP, "io_merged file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1244         }
1245
1246         if(unlikely(cgroup_enable_blkio_queued_ops && !cg->io_queued.filename)) {
1247             snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_queued", cgroup_blkio_base, cg->id);
1248             if(likely(stat(filename, &buf) != -1)) {
1249                 cg->io_queued.filename = strdupz(filename);
1250                 cg->io_queued.enabled = cgroup_enable_blkio_queued_ops;
1251                 debug(D_CGROUP, "io_queued filename for cgroup '%s': '%s'", cg->id, cg->io_queued.filename);
1252             }
1253             else
1254                 debug(D_CGROUP, "io_queued file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1255         }
1256     }
1257
1258     debug(D_CGROUP, "done searching for cgroups");
1259     return;
1260 }
1261
1262 // ----------------------------------------------------------------------------
1263 // generate charts
1264
1265 #define CHART_TITLE_MAX 300
1266
1267 void update_services_charts(int update_every,
1268         int do_cpu,
1269         int do_mem_usage,
1270         int do_mem_detailed,
1271         int do_mem_failcnt,
1272         int do_swap_usage,
1273         int do_io,
1274         int do_io_ops,
1275         int do_throttle_io,
1276         int do_throttle_ops,
1277         int do_queued_ops,
1278         int do_merged_ops
1279 ) {
1280     static RRDSET
1281         *st_cpu = NULL,
1282         *st_mem_usage = NULL,
1283         *st_mem_failcnt = NULL,
1284         *st_swap_usage = NULL,
1285
1286         *st_mem_detailed_cache = NULL,
1287         *st_mem_detailed_rss = NULL,
1288         *st_mem_detailed_mapped = NULL,
1289         *st_mem_detailed_writeback = NULL,
1290         *st_mem_detailed_pgfault = NULL,
1291         *st_mem_detailed_pgmajfault = NULL,
1292         *st_mem_detailed_pgpgin = NULL,
1293         *st_mem_detailed_pgpgout = NULL,
1294
1295         *st_io_read = NULL,
1296         *st_io_serviced_read = NULL,
1297         *st_throttle_io_read = NULL,
1298         *st_throttle_ops_read = NULL,
1299         *st_queued_ops_read = NULL,
1300         *st_merged_ops_read = NULL,
1301
1302         *st_io_write = NULL,
1303         *st_io_serviced_write = NULL,
1304         *st_throttle_io_write = NULL,
1305         *st_throttle_ops_write = NULL,
1306         *st_queued_ops_write = NULL,
1307         *st_merged_ops_write = NULL;
1308
1309     // create the charts
1310
1311     if(likely(do_cpu)) {
1312         if(unlikely(!st_cpu)) {
1313             char title[CHART_TITLE_MAX + 1];
1314
1315             st_cpu = rrdset_find_bytype("services", "cpu");
1316             if(likely(!st_cpu)) {
1317                 snprintfz(title, CHART_TITLE_MAX, "Systemd Services CPU utilization (%d%% = %d core%s)", (processors * 100), processors, (processors > 1) ? "s" : "");
1318                 st_cpu = rrdset_create("services", "cpu", NULL, "cpu", "services.cpu", title, "%", CHART_PRIORITY_SYSTEMD_SERVICES, update_every, RRDSET_TYPE_STACKED);
1319             }
1320         }
1321         else
1322             rrdset_next(st_cpu);
1323     }
1324
1325     if(likely(do_mem_usage)) {
1326         if(unlikely(!st_mem_usage)) {
1327             st_mem_usage = rrdset_find_bytype("services", "mem_usage");
1328             if(likely(!st_mem_usage))
1329                 st_mem_usage = rrdset_create("services", "mem_usage", NULL, "mem", "services.mem_usage", (cgroup_used_memory_without_cache)?"Systemd Services Used Memory without Cache":"Systemd Services Used Memory", "MB", CHART_PRIORITY_SYSTEMD_SERVICES + 10, update_every, RRDSET_TYPE_STACKED);
1330         }
1331         else
1332             rrdset_next(st_mem_usage);
1333     }
1334
1335     if(likely(do_mem_detailed)) {
1336         if(unlikely(!st_mem_detailed_rss)) {
1337             st_mem_detailed_rss = rrdset_find_bytype("services", "mem_rss");
1338             if(likely(!st_mem_detailed_rss))
1339                 st_mem_detailed_rss = rrdset_create("services", "mem_rss", NULL, "mem", "services.mem_rss", "Systemd Services RSS Memory", "MB", CHART_PRIORITY_SYSTEMD_SERVICES + 20, update_every, RRDSET_TYPE_STACKED);
1340         }
1341         else
1342             rrdset_next(st_mem_detailed_rss);
1343
1344         if(unlikely(!st_mem_detailed_mapped)) {
1345             st_mem_detailed_mapped = rrdset_find_bytype("services", "mem_mapped");
1346             if(likely(!st_mem_detailed_mapped))
1347                 st_mem_detailed_mapped = rrdset_create("services", "mem_mapped", NULL, "mem", "services.mem_mapped", "Systemd Services Mapped Memory", "MB", CHART_PRIORITY_SYSTEMD_SERVICES + 30, update_every, RRDSET_TYPE_STACKED);
1348         }
1349         else
1350             rrdset_next(st_mem_detailed_mapped);
1351
1352         if(unlikely(!st_mem_detailed_cache)) {
1353             st_mem_detailed_cache = rrdset_find_bytype("services", "mem_cache");
1354             if(likely(!st_mem_detailed_cache))
1355                 st_mem_detailed_cache = rrdset_create("services", "mem_cache", NULL, "mem", "services.mem_cache", "Systemd Services Cache Memory", "MB", CHART_PRIORITY_SYSTEMD_SERVICES + 40, update_every, RRDSET_TYPE_STACKED);
1356         }
1357         else
1358             rrdset_next(st_mem_detailed_cache);
1359
1360         if(unlikely(!st_mem_detailed_writeback)) {
1361             st_mem_detailed_writeback = rrdset_find_bytype("services", "mem_writeback");
1362             if(likely(!st_mem_detailed_writeback))
1363                 st_mem_detailed_writeback = rrdset_create("services", "mem_writeback", NULL, "mem", "services.mem_writeback", "Systemd Services Writeback Memory", "MB", CHART_PRIORITY_SYSTEMD_SERVICES + 50, update_every, RRDSET_TYPE_STACKED);
1364         }
1365         else
1366             rrdset_next(st_mem_detailed_writeback);
1367
1368         if(unlikely(!st_mem_detailed_pgfault)) {
1369             st_mem_detailed_pgfault = rrdset_find_bytype("services", "mem_pgfault");
1370             if(likely(!st_mem_detailed_pgfault))
1371                 st_mem_detailed_pgfault = rrdset_create("services", "mem_pgfault", NULL, "mem", "services.mem_pgfault", "Systemd Services Memory Minor Page Faults", "MB/s", CHART_PRIORITY_SYSTEMD_SERVICES + 60, update_every, RRDSET_TYPE_STACKED);
1372         }
1373         else
1374             rrdset_next(st_mem_detailed_pgfault);
1375
1376         if(unlikely(!st_mem_detailed_pgmajfault)) {
1377             st_mem_detailed_pgmajfault = rrdset_find_bytype("services", "mem_pgmajfault");
1378             if(likely(!st_mem_detailed_pgmajfault))
1379                 st_mem_detailed_pgmajfault = rrdset_create("services", "mem_pgmajfault", NULL, "mem", "services.mem_pgmajfault", "Systemd Services Memory Major Page Faults", "MB/s", CHART_PRIORITY_SYSTEMD_SERVICES + 70, update_every, RRDSET_TYPE_STACKED);
1380         }
1381         else
1382             rrdset_next(st_mem_detailed_pgmajfault);
1383
1384         if(unlikely(!st_mem_detailed_pgpgin)) {
1385             st_mem_detailed_pgpgin = rrdset_find_bytype("services", "mem_pgpgin");
1386             if(likely(!st_mem_detailed_pgpgin))
1387                 st_mem_detailed_pgpgin = rrdset_create("services", "mem_pgpgin", NULL, "mem", "services.mem_pgpgin", "Systemd Services Memory Charging Activity", "MB/s", CHART_PRIORITY_SYSTEMD_SERVICES + 80, update_every, RRDSET_TYPE_STACKED);
1388         }
1389         else
1390             rrdset_next(st_mem_detailed_pgpgin);
1391
1392         if(unlikely(!st_mem_detailed_pgpgout)) {
1393             st_mem_detailed_pgpgout = rrdset_find_bytype("services", "mem_pgpgout");
1394             if(likely(!st_mem_detailed_pgpgout))
1395                 st_mem_detailed_pgpgout = rrdset_create("services", "mem_pgpgout", NULL, "mem", "services.mem_pgpgout", "Systemd Services Memory Uncharging Activity", "MB/s", CHART_PRIORITY_SYSTEMD_SERVICES + 90, update_every, RRDSET_TYPE_STACKED);
1396         }
1397         else
1398             rrdset_next(st_mem_detailed_pgpgout);
1399     }
1400
1401     if(likely(do_mem_failcnt)) {
1402         if(unlikely(!st_mem_failcnt)) {
1403             st_mem_failcnt = rrdset_find_bytype("services", "mem_failcnt");
1404             if(likely(!st_mem_failcnt))
1405                 st_mem_failcnt = rrdset_create("services", "mem_failcnt", NULL, "mem", "services.mem_failcnt", "Systemd Services Memory Limit Failures", "MB", CHART_PRIORITY_SYSTEMD_SERVICES + 110, update_every, RRDSET_TYPE_STACKED);
1406         }
1407         else
1408             rrdset_next(st_mem_failcnt);
1409     }
1410
1411     if(likely(do_swap_usage)) {
1412         if(unlikely(!st_swap_usage)) {
1413             st_swap_usage = rrdset_find_bytype("services", "swap_usage");
1414             if(likely(!st_swap_usage))
1415                 st_swap_usage = rrdset_create("services", "swap_usage", NULL, "swap", "services.swap_usage", "Systemd Services Swap Memory Used", "MB", CHART_PRIORITY_SYSTEMD_SERVICES + 100, update_every, RRDSET_TYPE_STACKED);
1416         }
1417         else
1418             rrdset_next(st_swap_usage);
1419     }
1420
1421     if(likely(do_io)) {
1422         if(unlikely(!st_io_read)) {
1423             st_io_read = rrdset_find_bytype("services", "io_read");
1424             if(likely(!st_io_read))
1425                 st_io_read = rrdset_create("services", "io_read", NULL, "disk", "services.io_read", "Systemd Services Disk Read Bandwidth", "KB/s", CHART_PRIORITY_SYSTEMD_SERVICES + 120, update_every, RRDSET_TYPE_STACKED);
1426         }
1427         else
1428             rrdset_next(st_io_read);
1429
1430         if(unlikely(!st_io_write)) {
1431             st_io_write = rrdset_find_bytype("services", "io_write");
1432             if(likely(!st_io_write))
1433                 st_io_write = rrdset_create("services", "io_write", NULL, "disk", "services.io_write", "Systemd Services Disk Write Bandwidth", "KB/s", CHART_PRIORITY_SYSTEMD_SERVICES + 130, update_every, RRDSET_TYPE_STACKED);
1434         }
1435         else
1436             rrdset_next(st_io_write);
1437     }
1438
1439     if(likely(do_io_ops)) {
1440         if(unlikely(!st_io_serviced_read)) {
1441             st_io_serviced_read = rrdset_find_bytype("services", "io_ops_read");
1442             if(likely(!st_io_serviced_read))
1443                 st_io_serviced_read = rrdset_create("services", "io_ops_read", NULL, "disk", "services.io_ops_read", "Systemd Services Disk Read Operations", "operations/s", CHART_PRIORITY_SYSTEMD_SERVICES + 140, update_every, RRDSET_TYPE_STACKED);
1444         }
1445         else
1446             rrdset_next(st_io_serviced_read);
1447
1448         if(unlikely(!st_io_serviced_write)) {
1449             st_io_serviced_write = rrdset_find_bytype("services", "io_ops_write");
1450             if(likely(!st_io_serviced_write))
1451                 st_io_serviced_write = rrdset_create("services", "io_ops_write", NULL, "disk", "services.io_ops_write", "Systemd Services Disk Write Operations", "operations/s", CHART_PRIORITY_SYSTEMD_SERVICES + 150, update_every, RRDSET_TYPE_STACKED);
1452         }
1453         else
1454             rrdset_next(st_io_serviced_write);
1455     }
1456
1457     if(likely(do_throttle_io)) {
1458         if(unlikely(!st_throttle_io_read)) {
1459             st_throttle_io_read = rrdset_find_bytype("services", "throttle_io_read");
1460             if(likely(!st_throttle_io_read))
1461                 st_throttle_io_read = rrdset_create("services", "throttle_io_read", NULL, "disk", "services.throttle_io_read", "Systemd Services Throttle Disk Read Bandwidth", "KB/s", CHART_PRIORITY_SYSTEMD_SERVICES + 160, update_every, RRDSET_TYPE_STACKED);
1462         }
1463         else
1464             rrdset_next(st_throttle_io_read);
1465
1466         if(unlikely(!st_throttle_io_write)) {
1467             st_throttle_io_write = rrdset_find_bytype("services", "throttle_io_write");
1468             if(likely(!st_throttle_io_write))
1469                 st_throttle_io_write = rrdset_create("services", "throttle_io_write", NULL, "disk", "services.throttle_io_write", "Systemd Services Throttle Disk Write Bandwidth", "KB/s", CHART_PRIORITY_SYSTEMD_SERVICES + 170, update_every, RRDSET_TYPE_STACKED);
1470         }
1471         else
1472             rrdset_next(st_throttle_io_write);
1473     }
1474
1475     if(likely(do_throttle_ops)) {
1476         if(unlikely(!st_throttle_ops_read)) {
1477             st_throttle_ops_read = rrdset_find_bytype("services", "throttle_io_ops_read");
1478             if(likely(!st_throttle_ops_read))
1479                 st_throttle_ops_read = rrdset_create("services", "throttle_io_ops_read", NULL, "disk", "services.throttle_io_ops_read", "Systemd Services Throttle Disk Read Operations", "operations/s", CHART_PRIORITY_SYSTEMD_SERVICES + 180, update_every, RRDSET_TYPE_STACKED);
1480         }
1481         else
1482             rrdset_next(st_throttle_ops_read);
1483
1484         if(unlikely(!st_throttle_ops_write)) {
1485             st_throttle_ops_write = rrdset_find_bytype("services", "throttle_io_ops_write");
1486             if(likely(!st_throttle_ops_write))
1487                 st_throttle_ops_write = rrdset_create("services", "throttle_io_ops_write", NULL, "disk", "services.throttle_io_ops_write", "Systemd Services Throttle Disk Write Operations", "operations/s", CHART_PRIORITY_SYSTEMD_SERVICES + 190, update_every, RRDSET_TYPE_STACKED);
1488         }
1489         else
1490             rrdset_next(st_throttle_ops_write);
1491     }
1492
1493     if(likely(do_queued_ops)) {
1494         if(unlikely(!st_queued_ops_read)) {
1495             st_queued_ops_read = rrdset_find_bytype("services", "queued_io_ops_read");
1496             if(likely(!st_queued_ops_read))
1497                 st_queued_ops_read = rrdset_create("services", "queued_io_ops_read", NULL, "disk", "services.queued_io_ops_read", "Systemd Services Queued Disk Read Operations", "operations/s", CHART_PRIORITY_SYSTEMD_SERVICES + 200, update_every, RRDSET_TYPE_STACKED);
1498         }
1499         else
1500             rrdset_next(st_queued_ops_read);
1501
1502         if(unlikely(!st_queued_ops_write)) {
1503             st_queued_ops_write = rrdset_find_bytype("services", "queued_io_ops_write");
1504             if(likely(!st_queued_ops_write))
1505                 st_queued_ops_write = rrdset_create("services", "queued_io_ops_write", NULL, "disk", "services.queued_io_ops_write", "Systemd Services Queued Disk Write Operations", "operations/s", CHART_PRIORITY_SYSTEMD_SERVICES + 210, update_every, RRDSET_TYPE_STACKED);
1506         }
1507         else
1508             rrdset_next(st_queued_ops_write);
1509     }
1510
1511     if(likely(do_merged_ops)) {
1512         if(unlikely(!st_merged_ops_read)) {
1513             st_merged_ops_read = rrdset_find_bytype("services", "merged_io_ops_read");
1514             if(likely(!st_merged_ops_read))
1515                 st_merged_ops_read = rrdset_create("services", "merged_io_ops_read", NULL, "disk", "services.merged_io_ops_read", "Systemd Services Merged Disk Read Operations", "operations/s", CHART_PRIORITY_SYSTEMD_SERVICES + 220, update_every, RRDSET_TYPE_STACKED);
1516         }
1517         else
1518             rrdset_next(st_merged_ops_read);
1519
1520         if(unlikely(!st_merged_ops_write)) {
1521             st_merged_ops_write = rrdset_find_bytype("services", "merged_io_ops_write");
1522             if(likely(!st_merged_ops_write))
1523                 st_merged_ops_write = rrdset_create("services", "merged_io_ops_write", NULL, "disk", "services.merged_io_ops_write", "Systemd Services Merged Disk Write Operations", "operations/s", CHART_PRIORITY_SYSTEMD_SERVICES + 230, update_every, RRDSET_TYPE_STACKED);
1524         }
1525         else
1526             rrdset_next(st_merged_ops_write);
1527     }
1528
1529     // update the values
1530     struct cgroup *cg;
1531     for(cg = cgroup_root; cg ; cg = cg->next) {
1532         if(unlikely(!cg->available || !cg->enabled || !(cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE)))
1533             continue;
1534
1535         if(likely(do_cpu && cg->cpuacct_stat.updated)) {
1536             if(unlikely(!cg->rd_cpu))
1537                 cg->rd_cpu = rrddim_add(st_cpu, cg->chart_id, cg->chart_title, 100, hz, RRDDIM_INCREMENTAL);
1538
1539             rrddim_set_by_pointer(st_cpu, cg->rd_cpu, cg->cpuacct_stat.user + cg->cpuacct_stat.system);
1540         }
1541
1542         if(likely(do_mem_usage && cg->memory.updated_usage_in_bytes)) {
1543             if(unlikely(!cg->rd_mem_usage))
1544                 cg->rd_mem_usage = rrddim_add(st_mem_usage, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1545
1546             rrddim_set_by_pointer(st_mem_usage, cg->rd_mem_usage, cg->memory.usage_in_bytes - ((cgroup_used_memory_without_cache)?cg->memory.cache:0));
1547         }
1548
1549         if(likely(do_mem_detailed && cg->memory.updated_detailed)) {
1550             if(unlikely(!cg->rd_mem_detailed_rss))
1551                 cg->rd_mem_detailed_rss = rrddim_add(st_mem_detailed_rss, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1552             rrddim_set_by_pointer(st_mem_detailed_rss, cg->rd_mem_detailed_rss, cg->memory.rss + cg->memory.rss_huge);
1553
1554             if(unlikely(!cg->rd_mem_detailed_mapped))
1555                 cg->rd_mem_detailed_mapped = rrddim_add(st_mem_detailed_mapped, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1556             rrddim_set_by_pointer(st_mem_detailed_mapped, cg->rd_mem_detailed_mapped, cg->memory.mapped_file);
1557
1558             if(unlikely(!cg->rd_mem_detailed_cache))
1559                 cg->rd_mem_detailed_cache = rrddim_add(st_mem_detailed_cache, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1560             rrddim_set_by_pointer(st_mem_detailed_cache, cg->rd_mem_detailed_cache, cg->memory.cache);
1561
1562             if(unlikely(!cg->rd_mem_detailed_writeback))
1563                 cg->rd_mem_detailed_writeback = rrddim_add(st_mem_detailed_writeback, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1564             rrddim_set_by_pointer(st_mem_detailed_writeback, cg->rd_mem_detailed_writeback, cg->memory.writeback);
1565
1566             if(unlikely(!cg->rd_mem_detailed_pgfault))
1567                 cg->rd_mem_detailed_pgfault = rrddim_add(st_mem_detailed_pgfault, cg->chart_id, cg->chart_title, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1568             rrddim_set_by_pointer(st_mem_detailed_pgfault, cg->rd_mem_detailed_pgfault, cg->memory.pgfault);
1569
1570             if(unlikely(!cg->rd_mem_detailed_pgmajfault))
1571                 cg->rd_mem_detailed_pgmajfault = rrddim_add(st_mem_detailed_pgmajfault, cg->chart_id, cg->chart_title, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1572             rrddim_set_by_pointer(st_mem_detailed_pgmajfault, cg->rd_mem_detailed_pgmajfault, cg->memory.pgmajfault);
1573
1574             if(unlikely(!cg->rd_mem_detailed_pgpgin))
1575                 cg->rd_mem_detailed_pgpgin = rrddim_add(st_mem_detailed_pgpgin, cg->chart_id, cg->chart_title, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1576             rrddim_set_by_pointer(st_mem_detailed_pgpgin, cg->rd_mem_detailed_pgpgin, cg->memory.pgpgin);
1577
1578             if(unlikely(!cg->rd_mem_detailed_pgpgout))
1579                 cg->rd_mem_detailed_pgpgout = rrddim_add(st_mem_detailed_pgpgout, cg->chart_id, cg->chart_title, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1580             rrddim_set_by_pointer(st_mem_detailed_pgpgout, cg->rd_mem_detailed_pgpgout, cg->memory.pgpgout);
1581         }
1582
1583         if(likely(do_mem_failcnt && cg->memory.updated_failcnt)) {
1584             if(unlikely(!cg->rd_mem_failcnt))
1585                 cg->rd_mem_failcnt = rrddim_add(st_mem_failcnt, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1586
1587             rrddim_set_by_pointer(st_mem_failcnt, cg->rd_mem_failcnt, cg->memory.failcnt);
1588         }
1589
1590         if(likely(do_swap_usage && cg->memory.updated_msw_usage_in_bytes)) {
1591             if(unlikely(!cg->rd_swap_usage))
1592                 cg->rd_swap_usage = rrddim_add(st_swap_usage, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1593
1594             rrddim_set_by_pointer(st_swap_usage, cg->rd_swap_usage, cg->memory.msw_usage_in_bytes);
1595         }
1596
1597         if(likely(do_io && cg->io_service_bytes.updated)) {
1598             if(unlikely(!cg->rd_io_service_bytes_read))
1599                 cg->rd_io_service_bytes_read = rrddim_add(st_io_read, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1600
1601             rrddim_set_by_pointer(st_io_read, cg->rd_io_service_bytes_read, cg->io_service_bytes.Read);
1602
1603             if(unlikely(!cg->rd_io_service_bytes_write))
1604                 cg->rd_io_service_bytes_write = rrddim_add(st_io_write, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1605
1606             rrddim_set_by_pointer(st_io_write, cg->rd_io_service_bytes_write, cg->io_service_bytes.Write);
1607         }
1608
1609         if(likely(do_io_ops && cg->io_serviced.updated)) {
1610             if(unlikely(!cg->rd_io_serviced_read))
1611                 cg->rd_io_serviced_read = rrddim_add(st_io_serviced_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1612
1613             rrddim_set_by_pointer(st_io_serviced_read, cg->rd_io_serviced_read, cg->io_serviced.Read);
1614
1615             if(unlikely(!cg->rd_io_serviced_write))
1616                 cg->rd_io_serviced_write = rrddim_add(st_io_serviced_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1617
1618             rrddim_set_by_pointer(st_io_serviced_write, cg->rd_io_serviced_write, cg->io_serviced.Write);
1619         }
1620
1621         if(likely(do_throttle_io && cg->throttle_io_service_bytes.updated)) {
1622             if(unlikely(!cg->rd_throttle_io_read))
1623                 cg->rd_throttle_io_read = rrddim_add(st_throttle_io_read, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1624
1625             rrddim_set_by_pointer(st_throttle_io_read, cg->rd_throttle_io_read, cg->throttle_io_service_bytes.Read);
1626
1627             if(unlikely(!cg->rd_throttle_io_write))
1628                 cg->rd_throttle_io_write = rrddim_add(st_throttle_io_write, cg->chart_id, cg->chart_title, 1, 1024, RRDDIM_INCREMENTAL);
1629
1630             rrddim_set_by_pointer(st_throttle_io_write, cg->rd_throttle_io_write, cg->throttle_io_service_bytes.Write);
1631         }
1632
1633         if(likely(do_throttle_ops && cg->throttle_io_serviced.updated)) {
1634             if(unlikely(!cg->rd_throttle_io_serviced_read))
1635                 cg->rd_throttle_io_serviced_read = rrddim_add(st_throttle_ops_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1636
1637             rrddim_set_by_pointer(st_throttle_ops_read, cg->rd_throttle_io_serviced_read, cg->throttle_io_serviced.Read);
1638
1639             if(unlikely(!cg->rd_throttle_io_serviced_write))
1640                 cg->rd_throttle_io_serviced_write = rrddim_add(st_throttle_ops_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1641
1642             rrddim_set_by_pointer(st_throttle_ops_write, cg->rd_throttle_io_serviced_write, cg->throttle_io_serviced.Write);
1643         }
1644
1645         if(likely(do_queued_ops && cg->io_queued.updated)) {
1646             if(unlikely(!cg->rd_io_queued_read))
1647                 cg->rd_io_queued_read = rrddim_add(st_queued_ops_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1648
1649             rrddim_set_by_pointer(st_queued_ops_read, cg->rd_io_queued_read, cg->io_queued.Read);
1650
1651             if(unlikely(!cg->rd_io_queued_write))
1652                 cg->rd_io_queued_write = rrddim_add(st_queued_ops_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1653
1654             rrddim_set_by_pointer(st_queued_ops_write, cg->rd_io_queued_write, cg->io_queued.Write);
1655         }
1656
1657         if(likely(do_merged_ops && cg->io_merged.updated)) {
1658             if(unlikely(!cg->rd_io_merged_read))
1659                 cg->rd_io_merged_read = rrddim_add(st_merged_ops_read, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1660
1661             rrddim_set_by_pointer(st_merged_ops_read, cg->rd_io_merged_read, cg->io_merged.Read);
1662
1663             if(unlikely(!cg->rd_io_merged_write))
1664                 cg->rd_io_merged_write = rrddim_add(st_merged_ops_write, cg->chart_id, cg->chart_title, 1, 1, RRDDIM_INCREMENTAL);
1665
1666             rrddim_set_by_pointer(st_merged_ops_write, cg->rd_io_merged_write, cg->io_merged.Write);
1667         }
1668     }
1669
1670     // complete the iteration
1671     if(likely(do_cpu))
1672         rrdset_done(st_cpu);
1673
1674     if(likely(do_mem_usage))
1675         rrdset_done(st_mem_usage);
1676
1677     if(unlikely(do_mem_detailed)) {
1678         rrdset_done(st_mem_detailed_cache);
1679         rrdset_done(st_mem_detailed_rss);
1680         rrdset_done(st_mem_detailed_mapped);
1681         rrdset_done(st_mem_detailed_writeback);
1682         rrdset_done(st_mem_detailed_pgfault);
1683         rrdset_done(st_mem_detailed_pgmajfault);
1684         rrdset_done(st_mem_detailed_pgpgin);
1685         rrdset_done(st_mem_detailed_pgpgout);
1686     }
1687
1688     if(likely(do_mem_failcnt))
1689         rrdset_done(st_mem_failcnt);
1690
1691     if(likely(do_swap_usage))
1692         rrdset_done(st_swap_usage);
1693
1694     if(likely(do_io)) {
1695         rrdset_done(st_io_read);
1696         rrdset_done(st_io_write);
1697     }
1698
1699     if(likely(do_io_ops)) {
1700         rrdset_done(st_io_serviced_read);
1701         rrdset_done(st_io_serviced_write);
1702     }
1703
1704     if(likely(do_throttle_io)) {
1705         rrdset_done(st_throttle_io_read);
1706         rrdset_done(st_throttle_io_write);
1707     }
1708
1709     if(likely(do_throttle_ops)) {
1710         rrdset_done(st_throttle_ops_read);
1711         rrdset_done(st_throttle_ops_write);
1712     }
1713
1714     if(likely(do_queued_ops)) {
1715         rrdset_done(st_queued_ops_read);
1716         rrdset_done(st_queued_ops_write);
1717     }
1718
1719     if(likely(do_merged_ops)) {
1720         rrdset_done(st_merged_ops_read);
1721         rrdset_done(st_merged_ops_write);
1722     }
1723 }
1724
1725 static inline char *cgroup_chart_type(char *buffer, const char *id, size_t len) {
1726     if(buffer[0]) return buffer;
1727
1728     if(id[0] == '\0' || (id[0] == '/' && id[1] == '\0'))
1729         strncpy(buffer, "cgroup_root", len);
1730     else
1731         snprintfz(buffer, len, "cgroup_%s", id);
1732
1733     netdata_fix_chart_id(buffer);
1734     return buffer;
1735 }
1736
1737 void update_cgroup_charts(int update_every) {
1738     debug(D_CGROUP, "updating cgroups charts");
1739
1740     char type[RRD_ID_LENGTH_MAX + 1];
1741     char title[CHART_TITLE_MAX + 1];
1742
1743     int services_do_cpu = 0,
1744             services_do_mem_usage = 0,
1745             services_do_mem_detailed = 0,
1746             services_do_mem_failcnt = 0,
1747             services_do_swap_usage = 0,
1748             services_do_io = 0,
1749             services_do_io_ops = 0,
1750             services_do_throttle_io = 0,
1751             services_do_throttle_ops = 0,
1752             services_do_queued_ops = 0,
1753             services_do_merged_ops = 0;
1754
1755     struct cgroup *cg;
1756     for(cg = cgroup_root; cg ; cg = cg->next) {
1757         if(unlikely(!cg->available || !cg->enabled))
1758             continue;
1759
1760         if(likely(cgroup_enable_systemd_services && cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE)) {
1761             if(cg->cpuacct_stat.updated && cg->cpuacct_stat.enabled == CONFIG_ONDEMAND_YES) services_do_cpu++;
1762
1763             if(cgroup_enable_systemd_services_detailed_memory && cg->memory.updated_detailed && cg->memory.enabled_detailed) services_do_mem_detailed++;
1764             if(cg->memory.updated_usage_in_bytes && cg->memory.enabled_usage_in_bytes == CONFIG_ONDEMAND_YES) services_do_mem_usage++;
1765             if(cg->memory.updated_failcnt && cg->memory.enabled_failcnt == CONFIG_ONDEMAND_YES) services_do_mem_failcnt++;
1766             if(cg->memory.updated_msw_usage_in_bytes && cg->memory.enabled_msw_usage_in_bytes == CONFIG_ONDEMAND_YES) services_do_swap_usage++;
1767
1768             if(cg->io_service_bytes.updated && cg->io_service_bytes.enabled == CONFIG_ONDEMAND_YES) services_do_io++;
1769             if(cg->io_serviced.updated && cg->io_serviced.enabled == CONFIG_ONDEMAND_YES) services_do_io_ops++;
1770             if(cg->throttle_io_service_bytes.updated && cg->throttle_io_service_bytes.enabled == CONFIG_ONDEMAND_YES) services_do_throttle_io++;
1771             if(cg->throttle_io_serviced.updated && cg->throttle_io_serviced.enabled == CONFIG_ONDEMAND_YES) services_do_throttle_ops++;
1772             if(cg->io_queued.updated && cg->io_queued.enabled == CONFIG_ONDEMAND_YES) services_do_queued_ops++;
1773             if(cg->io_merged.updated && cg->io_merged.enabled == CONFIG_ONDEMAND_YES) services_do_merged_ops++;
1774             continue;
1775         }
1776
1777         type[0] = '\0';
1778
1779         if(likely(cg->cpuacct_stat.updated && cg->cpuacct_stat.enabled == CONFIG_ONDEMAND_YES)) {
1780             if(unlikely(!cg->st_cpu)) {
1781                 cg->st_cpu = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "cpu");
1782                 if(likely(!cg->st_cpu)) {
1783                     snprintfz(title, CHART_TITLE_MAX, "CPU Usage (%d%% = %d core%s) for cgroup %s", (processors * 100), processors, (processors > 1) ? "s" : "", cg->chart_title);
1784                     cg->st_cpu = rrdset_create(type, "cpu", NULL, "cpu", "cgroup.cpu", title, "%", CHART_PRIORITY_CONTAINERS, update_every, RRDSET_TYPE_STACKED);
1785                 }
1786                 rrddim_add(cg->st_cpu, "user", NULL, 100, hz, RRDDIM_INCREMENTAL);
1787                 rrddim_add(cg->st_cpu, "system", NULL, 100, hz, RRDDIM_INCREMENTAL);
1788             }
1789             else
1790                 rrdset_next(cg->st_cpu);
1791
1792             rrddim_set(cg->st_cpu, "user", cg->cpuacct_stat.user);
1793             rrddim_set(cg->st_cpu, "system", cg->cpuacct_stat.system);
1794             rrdset_done(cg->st_cpu);
1795         }
1796
1797         if(likely(cg->cpuacct_usage.updated && cg->cpuacct_usage.enabled == CONFIG_ONDEMAND_YES)) {
1798             char id[RRD_ID_LENGTH_MAX + 1];
1799             unsigned int i;
1800
1801             if(unlikely(!cg->st_cpu_per_core)) {
1802                 cg->st_cpu_per_core = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "cpu_per_core");
1803                 if(likely(!cg->st_cpu_per_core)) {
1804                     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);
1805                     cg->st_cpu_per_core = rrdset_create(type, "cpu_per_core", NULL, "cpu", "cgroup.cpu_per_core", title, "%", CHART_PRIORITY_CONTAINERS + 100, update_every, RRDSET_TYPE_STACKED);
1806                 }
1807                 for(i = 0; i < cg->cpuacct_usage.cpus; i++) {
1808                     snprintfz(id, CHART_TITLE_MAX, "cpu%u", i);
1809                     rrddim_add(cg->st_cpu_per_core, id, NULL, 100, 1000000000, RRDDIM_INCREMENTAL);
1810                 }
1811             }
1812             else
1813                 rrdset_next(cg->st_cpu_per_core);
1814
1815             for(i = 0; i < cg->cpuacct_usage.cpus ;i++) {
1816                 snprintfz(id, CHART_TITLE_MAX, "cpu%u", i);
1817                 rrddim_set(cg->st_cpu_per_core, id, cg->cpuacct_usage.cpu_percpu[i]);
1818             }
1819             rrdset_done(cg->st_cpu_per_core);
1820         }
1821
1822         if(likely(cg->memory.updated_detailed && cg->memory.enabled_detailed == CONFIG_ONDEMAND_YES)) {
1823             if(unlikely(!cg->st_mem)) {
1824                 cg->st_mem = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem");
1825                 if(likely(!cg->st_mem)) {
1826                     snprintfz(title, CHART_TITLE_MAX, "Memory Usage for cgroup %s", cg->chart_title);
1827                     cg->st_mem = rrdset_create(type, "mem", NULL, "mem", "cgroup.mem", title, "MB", CHART_PRIORITY_CONTAINERS + 210, update_every, RRDSET_TYPE_STACKED);
1828                 }
1829
1830                 rrddim_add(cg->st_mem, "cache", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1831                 rrddim_add(cg->st_mem, "rss", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1832                 if(cg->memory.detailed_has_swap)
1833                     rrddim_add(cg->st_mem, "swap", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1834                 rrddim_add(cg->st_mem, "rss_huge", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1835                 rrddim_add(cg->st_mem, "mapped_file", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1836             }
1837             else
1838                 rrdset_next(cg->st_mem);
1839
1840             rrddim_set(cg->st_mem, "cache", cg->memory.cache);
1841             rrddim_set(cg->st_mem, "rss", cg->memory.rss);
1842             if(cg->memory.detailed_has_swap)
1843                 rrddim_set(cg->st_mem, "swap", cg->memory.swap);
1844             rrddim_set(cg->st_mem, "rss_huge", cg->memory.rss_huge);
1845             rrddim_set(cg->st_mem, "mapped_file", cg->memory.mapped_file);
1846             rrdset_done(cg->st_mem);
1847
1848             if(unlikely(!cg->st_writeback)) {
1849                 cg->st_writeback = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "writeback");
1850                 if(likely(!cg->st_writeback)) {
1851                     snprintfz(title, CHART_TITLE_MAX, "Writeback Memory for cgroup %s", cg->chart_title);
1852                     cg->st_writeback = rrdset_create(type, "writeback", NULL, "mem", "cgroup.writeback", title, "MB", CHART_PRIORITY_CONTAINERS + 300, update_every, RRDSET_TYPE_AREA);
1853                 }
1854
1855                 if(cg->memory.detailed_has_dirty)
1856                     rrddim_add(cg->st_writeback, "dirty", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1857                 rrddim_add(cg->st_writeback, "writeback", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1858             }
1859             else
1860                 rrdset_next(cg->st_writeback);
1861
1862             if(cg->memory.detailed_has_dirty)
1863                 rrddim_set(cg->st_writeback, "dirty", cg->memory.dirty);
1864             rrddim_set(cg->st_writeback, "writeback", cg->memory.writeback);
1865             rrdset_done(cg->st_writeback);
1866
1867             if(unlikely(!cg->st_mem_activity)) {
1868                 cg->st_mem_activity = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_activity");
1869                 if(likely(!cg->st_mem_activity)) {
1870                     snprintfz(title, CHART_TITLE_MAX, "Memory Activity for cgroup %s", cg->chart_title);
1871                     cg->st_mem_activity = rrdset_create(type, "mem_activity", NULL, "mem", "cgroup.mem_activity", title, "MB/s", CHART_PRIORITY_CONTAINERS + 400, update_every, RRDSET_TYPE_LINE);
1872                 }
1873                 rrddim_add(cg->st_mem_activity, "pgpgin", "in", system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1874                 rrddim_add(cg->st_mem_activity, "pgpgout", "out", -system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1875             }
1876             else
1877                 rrdset_next(cg->st_mem_activity);
1878
1879             rrddim_set(cg->st_mem_activity, "pgpgin", cg->memory.pgpgin);
1880             rrddim_set(cg->st_mem_activity, "pgpgout", cg->memory.pgpgout);
1881             rrdset_done(cg->st_mem_activity);
1882
1883             if(unlikely(!cg->st_pgfaults)) {
1884                 cg->st_pgfaults = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "pgfaults");
1885                 if(likely(!cg->st_pgfaults)) {
1886                     snprintfz(title, CHART_TITLE_MAX, "Memory Page Faults for cgroup %s", cg->chart_title);
1887                     cg->st_pgfaults = rrdset_create(type, "pgfaults", NULL, "mem", "cgroup.pgfaults", title, "MB/s", CHART_PRIORITY_CONTAINERS + 500, update_every, RRDSET_TYPE_LINE);
1888                 }
1889                 rrddim_add(cg->st_pgfaults, "pgfault", NULL, system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1890                 rrddim_add(cg->st_pgfaults, "pgmajfault", "swap", -system_page_size, 1024 * 1024, RRDDIM_INCREMENTAL);
1891             }
1892             else
1893                 rrdset_next(cg->st_pgfaults);
1894
1895             rrddim_set(cg->st_pgfaults, "pgfault", cg->memory.pgfault);
1896             rrddim_set(cg->st_pgfaults, "pgmajfault", cg->memory.pgmajfault);
1897             rrdset_done(cg->st_pgfaults);
1898         }
1899
1900         if(likely(cg->memory.updated_usage_in_bytes && cg->memory.enabled_usage_in_bytes == CONFIG_ONDEMAND_YES)) {
1901             if(unlikely(!cg->st_mem_usage)) {
1902                 cg->st_mem_usage = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_usage");
1903                 if(likely(!cg->st_mem_usage)) {
1904                     snprintfz(title, CHART_TITLE_MAX, "Used Memory %sfor cgroup %s", (cgroup_used_memory_without_cache && cg->memory.updated_detailed)?"without Cache ":"", cg->chart_title);
1905                     cg->st_mem_usage = rrdset_create(type, "mem_usage", NULL, "mem", "cgroup.mem_usage", title, "MB", CHART_PRIORITY_CONTAINERS + 200, update_every, RRDSET_TYPE_STACKED);
1906                 }
1907                 rrddim_add(cg->st_mem_usage, "ram", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1908                 rrddim_add(cg->st_mem_usage, "swap", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1909             }
1910             else
1911                 rrdset_next(cg->st_mem_usage);
1912
1913             rrddim_set(cg->st_mem_usage, "ram", cg->memory.usage_in_bytes - ((cgroup_used_memory_without_cache)?cg->memory.cache:0));
1914             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);
1915             rrdset_done(cg->st_mem_usage);
1916         }
1917
1918         if(likely(cg->memory.updated_failcnt && cg->memory.enabled_failcnt == CONFIG_ONDEMAND_YES)) {
1919             if(unlikely(!cg->st_mem_failcnt)) {
1920                 cg->st_mem_failcnt = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_failcnt");
1921                 if(likely(!cg->st_mem_failcnt)) {
1922                     snprintfz(title, CHART_TITLE_MAX, "Memory Limit Failures for cgroup %s", cg->chart_title);
1923                     cg->st_mem_failcnt = rrdset_create(type, "mem_failcnt", NULL, "mem", "cgroup.mem_failcnt", title, "count", CHART_PRIORITY_CONTAINERS + 250, update_every, RRDSET_TYPE_LINE);
1924                 }
1925                 rrddim_add(cg->st_mem_failcnt, "failures", NULL, 1, 1, RRDDIM_INCREMENTAL);
1926             }
1927             else
1928                 rrdset_next(cg->st_mem_failcnt);
1929
1930             rrddim_set(cg->st_mem_failcnt, "failures", cg->memory.failcnt);
1931             rrdset_done(cg->st_mem_failcnt);
1932         }
1933
1934         if(likely(cg->io_service_bytes.updated && cg->io_service_bytes.enabled == CONFIG_ONDEMAND_YES)) {
1935             if(unlikely(!cg->st_io)) {
1936                 cg->st_io = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "io");
1937                 if(likely(!cg->st_io)) {
1938                     snprintfz(title, CHART_TITLE_MAX, "I/O Bandwidth (all disks) for cgroup %s", cg->chart_title);
1939                     cg->st_io = rrdset_create(type, "io", NULL, "disk", "cgroup.io", title, "KB/s", CHART_PRIORITY_CONTAINERS + 1200, update_every, RRDSET_TYPE_AREA);
1940                 }
1941                 rrddim_add(cg->st_io, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
1942                 rrddim_add(cg->st_io, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
1943             }
1944             else
1945                 rrdset_next(cg->st_io);
1946
1947             rrddim_set(cg->st_io, "read", cg->io_service_bytes.Read);
1948             rrddim_set(cg->st_io, "write", cg->io_service_bytes.Write);
1949             rrdset_done(cg->st_io);
1950         }
1951
1952         if(likely(cg->io_serviced.updated && cg->io_serviced.enabled == CONFIG_ONDEMAND_YES)) {
1953             if(unlikely(!cg->st_serviced_ops)) {
1954                 cg->st_serviced_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "serviced_ops");
1955                 if(likely(!cg->st_serviced_ops)) {
1956                     snprintfz(title, CHART_TITLE_MAX, "Serviced I/O Operations (all disks) for cgroup %s", cg->chart_title);
1957                     cg->st_serviced_ops = rrdset_create(type, "serviced_ops", NULL, "disk", "cgroup.serviced_ops", title, "operations/s", CHART_PRIORITY_CONTAINERS + 1200, update_every, RRDSET_TYPE_LINE);
1958                 }
1959                 rrddim_add(cg->st_serviced_ops, "read", NULL, 1, 1, RRDDIM_INCREMENTAL);
1960                 rrddim_add(cg->st_serviced_ops, "write", NULL, -1, 1, RRDDIM_INCREMENTAL);
1961             }
1962             else
1963                 rrdset_next(cg->st_serviced_ops);
1964
1965             rrddim_set(cg->st_serviced_ops, "read", cg->io_serviced.Read);
1966             rrddim_set(cg->st_serviced_ops, "write", cg->io_serviced.Write);
1967             rrdset_done(cg->st_serviced_ops);
1968         }
1969
1970         if(likely(cg->throttle_io_service_bytes.updated && cg->throttle_io_service_bytes.enabled == CONFIG_ONDEMAND_YES)) {
1971             if(unlikely(!cg->st_throttle_io)) {
1972                 cg->st_throttle_io = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "throttle_io");
1973                 if(likely(!cg->st_throttle_io)) {
1974                     snprintfz(title, CHART_TITLE_MAX, "Throttle I/O Bandwidth (all disks) for cgroup %s", cg->chart_title);
1975                     cg->st_throttle_io = rrdset_create(type, "throttle_io", NULL, "disk", "cgroup.throttle_io", title, "KB/s", CHART_PRIORITY_CONTAINERS + 1200, update_every, RRDSET_TYPE_AREA);
1976                 }
1977                 rrddim_add(cg->st_throttle_io, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
1978                 rrddim_add(cg->st_throttle_io, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
1979             }
1980             else
1981                 rrdset_next(cg->st_throttle_io);
1982
1983             rrddim_set(cg->st_throttle_io, "read", cg->throttle_io_service_bytes.Read);
1984             rrddim_set(cg->st_throttle_io, "write", cg->throttle_io_service_bytes.Write);
1985             rrdset_done(cg->st_throttle_io);
1986         }
1987
1988         if(likely(cg->throttle_io_serviced.updated && cg->throttle_io_serviced.enabled == CONFIG_ONDEMAND_YES)) {
1989             if(unlikely(!cg->st_throttle_serviced_ops)) {
1990                 cg->st_throttle_serviced_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "throttle_serviced_ops");
1991                 if(likely(!cg->st_throttle_serviced_ops)) {
1992                     snprintfz(title, CHART_TITLE_MAX, "Throttle Serviced I/O Operations (all disks) for cgroup %s", cg->chart_title);
1993                     cg->st_throttle_serviced_ops = rrdset_create(type, "throttle_serviced_ops", NULL, "disk", "cgroup.throttle_serviced_ops", title, "operations/s", CHART_PRIORITY_CONTAINERS + 1200, update_every, RRDSET_TYPE_LINE);
1994                 }
1995                 rrddim_add(cg->st_throttle_serviced_ops, "read", NULL, 1, 1, RRDDIM_INCREMENTAL);
1996                 rrddim_add(cg->st_throttle_serviced_ops, "write", NULL, -1, 1, RRDDIM_INCREMENTAL);
1997             }
1998             else
1999                 rrdset_next(cg->st_throttle_serviced_ops);
2000
2001             rrddim_set(cg->st_throttle_serviced_ops, "read", cg->throttle_io_serviced.Read);
2002             rrddim_set(cg->st_throttle_serviced_ops, "write", cg->throttle_io_serviced.Write);
2003             rrdset_done(cg->st_throttle_serviced_ops);
2004         }
2005
2006         if(likely(cg->io_queued.updated && cg->io_queued.enabled == CONFIG_ONDEMAND_YES)) {
2007             if(unlikely(!cg->st_queued_ops)) {
2008                 cg->st_queued_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "queued_ops");
2009                 if(likely(!cg->st_queued_ops)) {
2010                     snprintfz(title, CHART_TITLE_MAX, "Queued I/O Operations (all disks) for cgroup %s", cg->chart_title);
2011                     cg->st_queued_ops = rrdset_create(type, "queued_ops", NULL, "disk", "cgroup.queued_ops", title, "operations", CHART_PRIORITY_CONTAINERS + 2000, update_every, RRDSET_TYPE_LINE);
2012                 }
2013                 rrddim_add(cg->st_queued_ops, "read", NULL, 1, 1, RRDDIM_ABSOLUTE);
2014                 rrddim_add(cg->st_queued_ops, "write", NULL, -1, 1, RRDDIM_ABSOLUTE);
2015             }
2016             else
2017                 rrdset_next(cg->st_queued_ops);
2018
2019             rrddim_set(cg->st_queued_ops, "read", cg->io_queued.Read);
2020             rrddim_set(cg->st_queued_ops, "write", cg->io_queued.Write);
2021             rrdset_done(cg->st_queued_ops);
2022         }
2023
2024         if(likely(cg->io_merged.updated && cg->io_merged.enabled == CONFIG_ONDEMAND_YES)) {
2025             if(unlikely(!cg->st_merged_ops)) {
2026                 cg->st_merged_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "merged_ops");
2027                 if(likely(!cg->st_merged_ops)) {
2028                     snprintfz(title, CHART_TITLE_MAX, "Merged I/O Operations (all disks) for cgroup %s", cg->chart_title);
2029                     cg->st_merged_ops = rrdset_create(type, "merged_ops", NULL, "disk", "cgroup.merged_ops", title, "operations/s", CHART_PRIORITY_CONTAINERS + 2100, update_every, RRDSET_TYPE_LINE);
2030                 }
2031                 rrddim_add(cg->st_merged_ops, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
2032                 rrddim_add(cg->st_merged_ops, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
2033             }
2034             else
2035                 rrdset_next(cg->st_merged_ops);
2036
2037             rrddim_set(cg->st_merged_ops, "read", cg->io_merged.Read);
2038             rrddim_set(cg->st_merged_ops, "write", cg->io_merged.Write);
2039             rrdset_done(cg->st_merged_ops);
2040         }
2041     }
2042
2043     if(likely(cgroup_enable_systemd_services))
2044         update_services_charts(update_every,
2045                 services_do_cpu,
2046                 services_do_mem_usage,
2047                 services_do_mem_detailed,
2048                 services_do_mem_failcnt,
2049                 services_do_swap_usage,
2050                 services_do_io,
2051                 services_do_io_ops,
2052                 services_do_throttle_io,
2053                 services_do_throttle_ops,
2054                 services_do_queued_ops,
2055                 services_do_merged_ops
2056         );
2057
2058     debug(D_CGROUP, "done updating cgroups charts");
2059 }
2060
2061 // ----------------------------------------------------------------------------
2062 // cgroups main
2063
2064 void *cgroups_main(void *ptr) {
2065     struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
2066
2067     info("CGROUP Plugin thread created with task id %d", gettid());
2068
2069     if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
2070         error("Cannot set pthread cancel type to DEFERRED.");
2071
2072     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
2073         error("Cannot set pthread cancel state to ENABLE.");
2074
2075     struct rusage thread;
2076
2077     // when ZERO, attempt to do it
2078     int vdo_cpu_netdata = config_get_boolean("plugin:cgroups", "cgroups plugin resource charts", 1);
2079
2080     read_cgroup_plugin_configuration();
2081
2082     RRDSET *stcpu_thread = NULL;
2083
2084     usec_t step = cgroup_update_every * USEC_PER_SEC;
2085     usec_t find_every = cgroup_check_for_new_every * USEC_PER_SEC, find_next = 0;
2086     for(;;) {
2087         usec_t now = now_monotonic_usec();
2088         usec_t next = now - (now % step) + step;
2089
2090         while(now < next) {
2091             sleep_usec(next - now);
2092             now = now_monotonic_usec();
2093         }
2094
2095         if(unlikely(netdata_exit)) break;
2096
2097         // BEGIN -- the job to be done
2098
2099         if(unlikely(now >= find_next)) {
2100             find_all_cgroups();
2101             find_next = now + find_every;
2102         }
2103
2104         read_all_cgroups(cgroup_root);
2105         update_cgroup_charts(cgroup_update_every);
2106
2107         // END -- the job is done
2108
2109         // --------------------------------------------------------------------
2110
2111         if(vdo_cpu_netdata) {
2112             getrusage(RUSAGE_THREAD, &thread);
2113
2114             if(unlikely(!stcpu_thread)) {
2115                 stcpu_thread = rrdset_find("netdata.plugin_cgroups_cpu");
2116                 if(unlikely(!stcpu_thread))
2117                     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);
2118
2119                 rrddim_add(stcpu_thread, "user",  NULL,  1, 1000, RRDDIM_INCREMENTAL);
2120                 rrddim_add(stcpu_thread, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
2121             }
2122             else
2123                 rrdset_next(stcpu_thread);
2124
2125             rrddim_set(stcpu_thread, "user"  , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
2126             rrddim_set(stcpu_thread, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
2127             rrdset_done(stcpu_thread);
2128         }
2129     }
2130
2131     info("CGROUP thread exiting");
2132
2133     static_thread->enabled = 0;
2134     pthread_exit(NULL);
2135     return NULL;
2136 }