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