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