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