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