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