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