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