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