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