]> arthur.barton.de Git - netdata.git/blob - src/sys_fs_cgroup.c
allow alarm templates to match patterns of chart families; fixes #1571
[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_devices = CONFIG_ONDEMAND_ONDEMAND;
10 static int cgroup_enable_blkio = CONFIG_ONDEMAND_ONDEMAND;
11 static int cgroup_enable_systemd_services = CONFIG_ONDEMAND_YES;
12 static int cgroup_enable_new_cgroups_detected_at_runtime = 1;
13 static int cgroup_check_for_new_every = 10;
14 static int cgroup_update_every = 1;
15
16 static char *cgroup_cpuacct_base = NULL;
17 static char *cgroup_blkio_base = NULL;
18 static char *cgroup_memory_base = NULL;
19 static char *cgroup_devices_base = NULL;
20
21 static int cgroup_root_count = 0;
22 static int cgroup_root_max = 500;
23 static int cgroup_max_depth = 0;
24
25 static SIMPLE_PATTERN *disabled_cgroups_patterns = NULL;
26 static SIMPLE_PATTERN *disabled_cgroup_paths = NULL;
27 static SIMPLE_PATTERN *disabled_cgroup_renames = NULL;
28 static SIMPLE_PATTERN *systemd_services_cgroups = NULL;
29
30 static char *cgroups_rename_script = PLUGINS_DIR "/cgroup-name.sh";
31
32 void read_cgroup_plugin_configuration() {
33     cgroup_update_every = (int)config_get_number("plugin:cgroups", "update every", rrd_update_every);
34     if(cgroup_update_every < rrd_update_every)
35         cgroup_update_every = rrd_update_every;
36
37     cgroup_check_for_new_every = (int)config_get_number("plugin:cgroups", "check for new cgroups every", cgroup_check_for_new_every * cgroup_update_every);
38     if(cgroup_check_for_new_every < cgroup_update_every)
39         cgroup_check_for_new_every = cgroup_update_every;
40
41     cgroup_enable_cpuacct_stat = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct stat", cgroup_enable_cpuacct_stat);
42     cgroup_enable_cpuacct_usage = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct usage", cgroup_enable_cpuacct_usage);
43     cgroup_enable_memory = config_get_boolean_ondemand("plugin:cgroups", "enable memory", cgroup_enable_memory);
44     cgroup_enable_blkio = config_get_boolean_ondemand("plugin:cgroups", "enable blkio", cgroup_enable_blkio);
45
46     cgroup_enable_systemd_services = config_get_boolean_ondemand("plugin:cgroups", "enable systemd services", cgroup_enable_systemd_services);
47
48     char filename[FILENAME_MAX + 1], *s;
49     struct mountinfo *mi, *root = mountinfo_read(0);
50
51     mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "cpuacct");
52     if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "cpuacct");
53     if(!mi) {
54         error("Cannot find cgroup cpuacct mountinfo. Assuming default: /sys/fs/cgroup/cpuacct");
55         s = "/sys/fs/cgroup/cpuacct";
56     }
57     else s = mi->mount_point;
58     snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
59     cgroup_cpuacct_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/cpuacct", filename);
60
61     mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "blkio");
62     if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "blkio");
63     if(!mi) {
64         error("Cannot find cgroup blkio mountinfo. Assuming default: /sys/fs/cgroup/blkio");
65         s = "/sys/fs/cgroup/blkio";
66     }
67     else s = mi->mount_point;
68     snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
69     cgroup_blkio_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/blkio", filename);
70
71     mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "memory");
72     if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "memory");
73     if(!mi) {
74         error("Cannot find cgroup memory mountinfo. Assuming default: /sys/fs/cgroup/memory");
75         s = "/sys/fs/cgroup/memory";
76     }
77     else s = mi->mount_point;
78     snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
79     cgroup_memory_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/memory", filename);
80
81     mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "devices");
82     if(!mi) mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "devices");
83     if(!mi) {
84         error("Cannot find cgroup devices mountinfo. Assuming default: /sys/fs/cgroup/devices");
85         s = "/sys/fs/cgroup/devices";
86     }
87     else s = mi->mount_point;
88     snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
89     cgroup_devices_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/devices", filename);
90
91     cgroup_root_max = (int)config_get_number("plugin:cgroups", "max cgroups to allow", cgroup_root_max);
92     cgroup_max_depth = (int)config_get_number("plugin:cgroups", "max cgroups depth to monitor", cgroup_max_depth);
93
94     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);
95
96     disabled_cgroups_patterns = simple_pattern_create(
97             config_get("plugin:cgroups", "disable by default cgroups matching", " *.mount "
98                     " *.partition "
99                     " *.service "
100                     " *.slice "
101                     " *.swap "
102                     " *.user "
103                     " / "
104                     " /docker "
105                     " /init.scope "
106                     " /libvirt "
107                     " /lxc "
108                     " /lxc/*/ns "                          //  #1397
109                     " /machine "
110                     " /machine.slice "
111                     " /qemu "
112                     " /system "
113                     " /system.slice "
114                     " /systemd "
115                     " /user "
116                     " /user.slice "
117             ), SIMPLE_PATTERN_EXACT);
118
119     disabled_cgroup_paths = simple_pattern_create(
120             config_get("plugin:cgroups", "do not search for cgroups in paths matching"
121                        , " *-qemu "                             //  #345
122                             " /init.scope "
123                             " /system "
124                             " /systemd "
125                             " /user "
126                             " /user.slice "
127             ), SIMPLE_PATTERN_EXACT);
128
129     cgroups_rename_script = config_get("plugin:cgroups", "script to get cgroup names", cgroups_rename_script);
130
131     disabled_cgroup_renames = simple_pattern_create(
132             config_get("plugin:cgroups", "do not run script to rename cgroups matching", " / "
133                     " *.mount "
134                     " *.partition "
135                     " *.scope "
136                     " *.service "
137                     " *.slice "
138                     " *.swap "
139                     " *.user "
140             ), SIMPLE_PATTERN_EXACT);
141
142     if(cgroup_enable_systemd_services)
143         systemd_services_cgroups = simple_pattern_create(
144                 config_get("plugin:cgroups", "cgroups to match as systemd services"
145                            , " !/system.slice/*/*.service /system.slice/*.service "
146                 ), SIMPLE_PATTERN_EXACT);
147
148     mountinfo_free(root);
149 }
150
151 // ----------------------------------------------------------------------------
152 // cgroup objects
153
154 struct blkio {
155     int updated;
156
157     char *filename;
158
159     unsigned long long Read;
160     unsigned long long Write;
161 /*
162     unsigned long long Sync;
163     unsigned long long Async;
164     unsigned long long Total;
165 */
166 };
167
168 // https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt
169 struct memory {
170     int updated;
171
172     char *filename;
173
174     int has_dirty_swap;
175
176     unsigned long long cache;
177     unsigned long long rss;
178     unsigned long long rss_huge;
179     unsigned long long mapped_file;
180     unsigned long long writeback;
181     unsigned long long dirty;
182     unsigned long long swap;
183     unsigned long long pgpgin;
184     unsigned long long pgpgout;
185     unsigned long long pgfault;
186     unsigned long long pgmajfault;
187 /*
188     unsigned long long inactive_anon;
189     unsigned long long active_anon;
190     unsigned long long inactive_file;
191     unsigned long long active_file;
192     unsigned long long unevictable;
193     unsigned long long hierarchical_memory_limit;
194     unsigned long long total_cache;
195     unsigned long long total_rss;
196     unsigned long long total_rss_huge;
197     unsigned long long total_mapped_file;
198     unsigned long long total_writeback;
199     unsigned long long total_dirty;
200     unsigned long long total_swap;
201     unsigned long long total_pgpgin;
202     unsigned long long total_pgpgout;
203     unsigned long long total_pgfault;
204     unsigned long long total_pgmajfault;
205     unsigned long long total_inactive_anon;
206     unsigned long long total_active_anon;
207     unsigned long long total_inactive_file;
208     unsigned long long total_active_file;
209     unsigned long long total_unevictable;
210 */
211
212     int usage_in_bytes_updated;
213     char *filename_usage_in_bytes;
214     unsigned long long usage_in_bytes;
215
216     int msw_usage_in_bytes_updated;
217     char *filename_msw_usage_in_bytes;
218     unsigned long long msw_usage_in_bytes;
219
220     int failcnt_updated;
221     char *filename_failcnt;
222     unsigned long long failcnt;
223 };
224
225 // https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt
226 struct cpuacct_stat {
227     int updated;
228
229     char *filename;
230
231     unsigned long long user;
232     unsigned long long system;
233 };
234
235 // https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt
236 struct cpuacct_usage {
237     int updated;
238
239     char *filename;
240
241     unsigned int cpus;
242     unsigned long long *cpu_percpu;
243 };
244
245 #define CGROUP_OPTIONS_DISABLED_DUPLICATE   0x00000001
246 #define CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE 0x00000002
247
248 struct cgroup {
249     uint32_t options;
250
251     char available;      // found in the filesystem
252     char enabled;        // enabled in the config
253
254     char *id;
255     uint32_t hash;
256
257     char *chart_id;
258     uint32_t hash_chart;
259
260     char *chart_title;
261
262     struct cpuacct_stat cpuacct_stat;
263     struct cpuacct_usage cpuacct_usage;
264
265     struct memory memory;
266
267     struct blkio io_service_bytes;              // bytes
268     struct blkio io_serviced;                   // operations
269
270     struct blkio throttle_io_service_bytes;     // bytes
271     struct blkio throttle_io_serviced;          // operations
272
273     struct blkio io_merged;                     // operations
274     struct blkio io_queued;                     // operations
275
276     // per cgroup charts
277     RRDSET *st_cpu;
278     RRDSET *st_cpu_per_core;
279     RRDSET *st_mem;
280     RRDSET *st_writeback;
281     RRDSET *st_mem_activity;
282     RRDSET *st_pgfaults;
283     RRDSET *st_mem_usage;
284     RRDSET *st_mem_failcnt;
285     RRDSET *st_io;
286     RRDSET *st_serviced_ops;
287     RRDSET *st_throttle_io;
288     RRDSET *st_throttle_serviced_ops;
289     RRDSET *st_queued_ops;
290     RRDSET *st_merged_ops;
291
292     // services
293     RRDDIM *rd_cpu;
294     RRDDIM *rd_mem_usage;
295
296     struct cgroup *next;
297
298 } *cgroup_root = NULL;
299
300 // ----------------------------------------------------------------------------
301 // read values from /sys
302
303 void cgroup_read_cpuacct_stat(struct cpuacct_stat *cp) {
304     static procfile *ff = NULL;
305
306     static uint32_t user_hash = 0;
307     static uint32_t system_hash = 0;
308
309     if(unlikely(user_hash == 0)) {
310         user_hash = simple_hash("user");
311         system_hash = simple_hash("system");
312     }
313
314     cp->updated = 0;
315     if(cp->filename) {
316         ff = procfile_reopen(ff, cp->filename, NULL, PROCFILE_FLAG_DEFAULT);
317         if(!ff) return;
318
319         ff = procfile_readall(ff);
320         if(!ff) return;
321
322         unsigned long i, lines = procfile_lines(ff);
323
324         if(lines < 1) {
325             error("File '%s' should have 1+ lines.", cp->filename);
326             return;
327         }
328
329         for(i = 0; i < lines ; i++) {
330             char *s = procfile_lineword(ff, i, 0);
331             uint32_t hash = simple_hash(s);
332
333             if(hash == user_hash && !strcmp(s, "user"))
334                 cp->user = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
335
336             else if(hash == system_hash && !strcmp(s, "system"))
337                 cp->system = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
338         }
339
340         cp->updated = 1;
341
342         // fprintf(stderr, "READ '%s': user: %llu, system: %llu\n", cp->filename, cp->user, cp->system);
343     }
344 }
345
346 void cgroup_read_cpuacct_usage(struct cpuacct_usage *ca) {
347     static procfile *ff = NULL;
348
349     ca->updated = 0;
350     if(ca->filename) {
351         ff = procfile_reopen(ff, ca->filename, NULL, PROCFILE_FLAG_DEFAULT);
352         if(!ff) return;
353
354         ff = procfile_readall(ff);
355         if(!ff) return;
356
357         if(procfile_lines(ff) < 1) {
358             error("File '%s' should have 1+ lines but has %u.", ca->filename, procfile_lines(ff));
359             return;
360         }
361
362         unsigned long i = procfile_linewords(ff, 0);
363         if(i == 0) return;
364
365         // we may have 1 more CPU reported
366         while(i > 0) {
367             char *s = procfile_lineword(ff, 0, i - 1);
368             if(!*s) i--;
369             else break;
370         }
371
372         if(i != ca->cpus) {
373             freez(ca->cpu_percpu);
374             ca->cpu_percpu = mallocz(sizeof(unsigned long long) * i);
375             ca->cpus = (unsigned int)i;
376         }
377
378         for(i = 0; i < ca->cpus ;i++) {
379             ca->cpu_percpu[i] = strtoull(procfile_lineword(ff, 0, i), NULL, 10);
380             // fprintf(stderr, "READ '%s': cpu%d/%d: %llu ('%s')\n", ca->filename, i, ca->cpus, ca->cpu_percpu[i], procfile_lineword(ff, 0, i));
381         }
382
383         ca->updated = 1;
384     }
385 }
386
387 void cgroup_read_blkio(struct blkio *io) {
388     static procfile *ff = NULL;
389
390     static uint32_t Read_hash = 0;
391     static uint32_t Write_hash = 0;
392 /*
393     static uint32_t Sync_hash = 0;
394     static uint32_t Async_hash = 0;
395     static uint32_t Total_hash = 0;
396 */
397
398     if(unlikely(Read_hash == 0)) {
399         Read_hash = simple_hash("Read");
400         Write_hash = simple_hash("Write");
401 /*
402         Sync_hash = simple_hash("Sync");
403         Async_hash = simple_hash("Async");
404         Total_hash = simple_hash("Total");
405 */
406     }
407
408     io->updated = 0;
409     if(io->filename) {
410         ff = procfile_reopen(ff, io->filename, NULL, PROCFILE_FLAG_DEFAULT);
411         if(!ff) return;
412
413         ff = procfile_readall(ff);
414         if(!ff) return;
415
416         unsigned long i, lines = procfile_lines(ff);
417
418         if(lines < 1) {
419             error("File '%s' should have 1+ lines.", io->filename);
420             return;
421         }
422
423         io->Read = 0;
424         io->Write = 0;
425 /*
426         io->Sync = 0;
427         io->Async = 0;
428         io->Total = 0;
429 */
430
431         for(i = 0; i < lines ; i++) {
432             char *s = procfile_lineword(ff, i, 1);
433             uint32_t hash = simple_hash(s);
434
435             if(hash == Read_hash && !strcmp(s, "Read"))
436                 io->Read += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
437
438             else if(hash == Write_hash && !strcmp(s, "Write"))
439                 io->Write += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
440
441 /*
442             else if(hash == Sync_hash && !strcmp(s, "Sync"))
443                 io->Sync += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
444
445             else if(hash == Async_hash && !strcmp(s, "Async"))
446                 io->Async += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
447
448             else if(hash == Total_hash && !strcmp(s, "Total"))
449                 io->Total += strtoull(procfile_lineword(ff, i, 2), NULL, 10);
450 */
451         }
452
453         io->updated = 1;
454         // fprintf(stderr, "READ '%s': Read: %llu, Write: %llu, Sync: %llu, Async: %llu, Total: %llu\n", io->filename, io->Read, io->Write, io->Sync, io->Async, io->Total);
455     }
456 }
457
458 void cgroup_read_memory(struct memory *mem) {
459     static procfile *ff = NULL;
460
461     static uint32_t cache_hash = 0;
462     static uint32_t rss_hash = 0;
463     static uint32_t rss_huge_hash = 0;
464     static uint32_t mapped_file_hash = 0;
465     static uint32_t writeback_hash = 0;
466     static uint32_t dirty_hash = 0;
467     static uint32_t swap_hash = 0;
468     static uint32_t pgpgin_hash = 0;
469     static uint32_t pgpgout_hash = 0;
470     static uint32_t pgfault_hash = 0;
471     static uint32_t pgmajfault_hash = 0;
472 /*
473     static uint32_t inactive_anon_hash = 0;
474     static uint32_t active_anon_hash = 0;
475     static uint32_t inactive_file_hash = 0;
476     static uint32_t active_file_hash = 0;
477     static uint32_t unevictable_hash = 0;
478     static uint32_t hierarchical_memory_limit_hash = 0;
479     static uint32_t total_cache_hash = 0;
480     static uint32_t total_rss_hash = 0;
481     static uint32_t total_rss_huge_hash = 0;
482     static uint32_t total_mapped_file_hash = 0;
483     static uint32_t total_writeback_hash = 0;
484     static uint32_t total_dirty_hash = 0;
485     static uint32_t total_swap_hash = 0;
486     static uint32_t total_pgpgin_hash = 0;
487     static uint32_t total_pgpgout_hash = 0;
488     static uint32_t total_pgfault_hash = 0;
489     static uint32_t total_pgmajfault_hash = 0;
490     static uint32_t total_inactive_anon_hash = 0;
491     static uint32_t total_active_anon_hash = 0;
492     static uint32_t total_inactive_file_hash = 0;
493     static uint32_t total_active_file_hash = 0;
494     static uint32_t total_unevictable_hash = 0;
495 */
496     if(unlikely(cache_hash == 0)) {
497         cache_hash = simple_hash("cache");
498         rss_hash = simple_hash("rss");
499         rss_huge_hash = simple_hash("rss_huge");
500         mapped_file_hash = simple_hash("mapped_file");
501         writeback_hash = simple_hash("writeback");
502         dirty_hash = simple_hash("dirty");
503         swap_hash = simple_hash("swap");
504         pgpgin_hash = simple_hash("pgpgin");
505         pgpgout_hash = simple_hash("pgpgout");
506         pgfault_hash = simple_hash("pgfault");
507         pgmajfault_hash = simple_hash("pgmajfault");
508 /*
509         inactive_anon_hash = simple_hash("inactive_anon");
510         active_anon_hash = simple_hash("active_anon");
511         inactive_file_hash = simple_hash("inactive_file");
512         active_file_hash = simple_hash("active_file");
513         unevictable_hash = simple_hash("unevictable");
514         hierarchical_memory_limit_hash = simple_hash("hierarchical_memory_limit");
515         total_cache_hash = simple_hash("total_cache");
516         total_rss_hash = simple_hash("total_rss");
517         total_rss_huge_hash = simple_hash("total_rss_huge");
518         total_mapped_file_hash = simple_hash("total_mapped_file");
519         total_writeback_hash = simple_hash("total_writeback");
520         total_dirty_hash = simple_hash("total_dirty");
521         total_swap_hash = simple_hash("total_swap");
522         total_pgpgin_hash = simple_hash("total_pgpgin");
523         total_pgpgout_hash = simple_hash("total_pgpgout");
524         total_pgfault_hash = simple_hash("total_pgfault");
525         total_pgmajfault_hash = simple_hash("total_pgmajfault");
526         total_inactive_anon_hash = simple_hash("total_inactive_anon");
527         total_active_anon_hash = simple_hash("total_active_anon");
528         total_inactive_file_hash = simple_hash("total_inactive_file");
529         total_active_file_hash = simple_hash("total_active_file");
530         total_unevictable_hash = simple_hash("total_unevictable");
531 */
532     }
533
534     mem->updated = 0;
535     if(mem->filename) {
536         ff = procfile_reopen(ff, mem->filename, NULL, PROCFILE_FLAG_DEFAULT);
537         if(!ff) return;
538
539         ff = procfile_readall(ff);
540         if(!ff) return;
541
542         unsigned long i, lines = procfile_lines(ff);
543
544         if(lines < 1) {
545             error("File '%s' should have 1+ lines.", mem->filename);
546             return;
547         }
548
549         for(i = 0; i < lines ; i++) {
550             char *s = procfile_lineword(ff, i, 0);
551             uint32_t hash = simple_hash(s);
552
553             if(hash == cache_hash && !strcmp(s, "cache"))
554                 mem->cache = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
555
556             else if(hash == rss_hash && !strcmp(s, "rss"))
557                 mem->rss = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
558
559             else if(hash == rss_huge_hash && !strcmp(s, "rss_huge"))
560                 mem->rss_huge = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
561
562             else if(hash == mapped_file_hash && !strcmp(s, "mapped_file"))
563                 mem->mapped_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
564
565             else if(hash == writeback_hash && !strcmp(s, "writeback"))
566                 mem->writeback = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
567
568             else if(hash == dirty_hash && !strcmp(s, "dirty")) {
569                 mem->dirty = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
570                 mem->has_dirty_swap = 1;
571             }
572
573             else if(hash == swap_hash && !strcmp(s, "swap")) {
574                 mem->swap = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
575                 mem->has_dirty_swap = 1;
576             }
577
578             else if(hash == pgpgin_hash && !strcmp(s, "pgpgin"))
579                 mem->pgpgin = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
580
581             else if(hash == pgpgout_hash && !strcmp(s, "pgpgout"))
582                 mem->pgpgout = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
583
584             else if(hash == pgfault_hash && !strcmp(s, "pgfault"))
585                 mem->pgfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
586
587             else if(hash == pgmajfault_hash && !strcmp(s, "pgmajfault"))
588                 mem->pgmajfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
589
590 /*
591             else if(hash == inactive_anon_hash && !strcmp(s, "inactive_anon"))
592                 mem->inactive_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
593
594             else if(hash == active_anon_hash && !strcmp(s, "active_anon"))
595                 mem->active_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
596
597             else if(hash == inactive_file_hash && !strcmp(s, "inactive_file"))
598                 mem->inactive_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
599
600             else if(hash == active_file_hash && !strcmp(s, "active_file"))
601                 mem->active_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
602
603             else if(hash == unevictable_hash && !strcmp(s, "unevictable"))
604                 mem->unevictable = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
605
606             else if(hash == hierarchical_memory_limit_hash && !strcmp(s, "hierarchical_memory_limit"))
607                 mem->hierarchical_memory_limit = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
608
609             else if(hash == total_cache_hash && !strcmp(s, "total_cache"))
610                 mem->total_cache = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
611
612             else if(hash == total_rss_hash && !strcmp(s, "total_rss"))
613                 mem->total_rss = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
614
615             else if(hash == total_rss_huge_hash && !strcmp(s, "total_rss_huge"))
616                 mem->total_rss_huge = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
617
618             else if(hash == total_mapped_file_hash && !strcmp(s, "total_mapped_file"))
619                 mem->total_mapped_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
620
621             else if(hash == total_writeback_hash && !strcmp(s, "total_writeback"))
622                 mem->total_writeback = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
623
624             else if(hash == total_dirty_hash && !strcmp(s, "total_dirty"))
625                 mem->total_dirty = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
626
627             else if(hash == total_swap_hash && !strcmp(s, "total_swap"))
628                 mem->total_swap = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
629
630             else if(hash == total_pgpgin_hash && !strcmp(s, "total_pgpgin"))
631                 mem->total_pgpgin = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
632
633             else if(hash == total_pgpgout_hash && !strcmp(s, "total_pgpgout"))
634                 mem->total_pgpgout = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
635
636             else if(hash == total_pgfault_hash && !strcmp(s, "total_pgfault"))
637                 mem->total_pgfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
638
639             else if(hash == total_pgmajfault_hash && !strcmp(s, "total_pgmajfault"))
640                 mem->total_pgmajfault = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
641
642             else if(hash == total_inactive_anon_hash && !strcmp(s, "total_inactive_anon"))
643                 mem->total_inactive_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
644
645             else if(hash == total_active_anon_hash && !strcmp(s, "total_active_anon"))
646                 mem->total_active_anon = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
647
648             else if(hash == total_inactive_file_hash && !strcmp(s, "total_inactive_file"))
649                 mem->total_inactive_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
650
651             else if(hash == total_active_file_hash && !strcmp(s, "total_active_file"))
652                 mem->total_active_file = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
653
654             else if(hash == total_unevictable_hash && !strcmp(s, "total_unevictable"))
655                 mem->total_unevictable = strtoull(procfile_lineword(ff, i, 1), NULL, 10);
656 */
657         }
658
659         // 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);
660
661         mem->updated = 1;
662     }
663
664     mem->usage_in_bytes_updated = 0;
665     if(mem->filename_usage_in_bytes) {
666         if(likely(!read_single_number_file(mem->filename_usage_in_bytes, &mem->usage_in_bytes)))
667             mem->usage_in_bytes_updated = 1;
668     }
669
670     mem->msw_usage_in_bytes_updated = 0;
671     if(mem->filename_msw_usage_in_bytes) {
672         if(likely(!read_single_number_file(mem->filename_msw_usage_in_bytes, &mem->msw_usage_in_bytes)))
673             mem->msw_usage_in_bytes_updated = 1;
674     }
675
676     mem->failcnt_updated = 0;
677     if(mem->filename_failcnt) {
678         if(likely(!read_single_number_file(mem->filename_failcnt, &mem->failcnt)))
679             mem->failcnt_updated = 1;
680     }
681 }
682
683 void cgroup_read(struct cgroup *cg) {
684     debug(D_CGROUP, "reading metrics for cgroups '%s'", cg->id);
685
686     cgroup_read_cpuacct_stat(&cg->cpuacct_stat);
687     cgroup_read_cpuacct_usage(&cg->cpuacct_usage);
688     cgroup_read_memory(&cg->memory);
689     cgroup_read_blkio(&cg->io_service_bytes);
690     cgroup_read_blkio(&cg->io_serviced);
691     cgroup_read_blkio(&cg->throttle_io_service_bytes);
692     cgroup_read_blkio(&cg->throttle_io_serviced);
693     cgroup_read_blkio(&cg->io_merged);
694     cgroup_read_blkio(&cg->io_queued);
695 }
696
697 void read_all_cgroups(struct cgroup *root) {
698     debug(D_CGROUP, "reading metrics for all cgroups");
699
700     struct cgroup *cg;
701
702     for(cg = root; cg ; cg = cg->next)
703         if(cg->enabled && cg->available)
704             cgroup_read(cg);
705 }
706
707 // ----------------------------------------------------------------------------
708 // add/remove/find cgroup objects
709
710 #define CGROUP_CHARTID_LINE_MAX 1024
711
712 static char *cgroup_title_strdupz(const char *s) {
713     if(!s || !*s) s = "/";
714
715     if(*s == '/' && s[1] != '\0') s++;
716
717     char *r = strdupz(s);
718     netdata_fix_chart_name(r);
719
720     return r;
721 }
722
723 static char *cgroup_chart_id_strdupz(const char *s) {
724     if(!s || !*s) s = "/";
725
726     if(*s == '/' && s[1] != '\0') s++;
727
728     char *r = strdupz(s);
729     netdata_fix_chart_id(r);
730
731     return r;
732 }
733
734 void cgroup_get_chart_name(struct cgroup *cg) {
735     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);
736
737     pid_t cgroup_pid;
738     char buffer[CGROUP_CHARTID_LINE_MAX + 1];
739
740     snprintfz(buffer, CGROUP_CHARTID_LINE_MAX, "exec %s '%s'", cgroups_rename_script, cg->chart_id);
741
742     debug(D_CGROUP, "executing command '%s' for cgroup '%s'", buffer, cg->id);
743     FILE *fp = mypopen(buffer, &cgroup_pid);
744     if(fp) {
745         // debug(D_CGROUP, "reading from command '%s' for cgroup '%s'", buffer, cg->id);
746         char *s = fgets(buffer, CGROUP_CHARTID_LINE_MAX, fp);
747         // debug(D_CGROUP, "closing command for cgroup '%s'", cg->id);
748         mypclose(fp, cgroup_pid);
749         // debug(D_CGROUP, "closed command for cgroup '%s'", cg->id);
750
751         if(s && *s && *s != '\n') {
752             debug(D_CGROUP, "cgroup '%s' should be renamed to '%s'", cg->id, s);
753
754             trim(s);
755
756             freez(cg->chart_title);
757             cg->chart_title = cgroup_title_strdupz(s);
758
759             freez(cg->chart_id);
760             cg->chart_id = cgroup_chart_id_strdupz(s);
761             cg->hash_chart = simple_hash(cg->chart_id);
762         }
763     }
764     else
765         error("CGROUP: Cannot popen(\"%s\", \"r\").", buffer);
766 }
767
768 struct cgroup *cgroup_add(const char *id) {
769     if(!id || !*id) id = "/";
770     debug(D_CGROUP, "adding to list, cgroup with id '%s'", id);
771
772     if(cgroup_root_count >= cgroup_root_max) {
773         info("Maximum number of cgroups reached (%d). Not adding cgroup '%s'", cgroup_root_count, id);
774         return NULL;
775     }
776
777     int def = simple_pattern_matches(disabled_cgroups_patterns, id)?0:cgroup_enable_new_cgroups_detected_at_runtime;
778     struct cgroup *cg = callocz(1, sizeof(struct cgroup));
779
780     cg->id = strdupz(id);
781     cg->hash = simple_hash(cg->id);
782
783     cg->chart_title = cgroup_title_strdupz(id);
784
785     cg->chart_id = cgroup_chart_id_strdupz(id);
786     cg->hash_chart = simple_hash(cg->chart_id);
787
788     if(!cgroup_root)
789         cgroup_root = cg;
790     else {
791         // append it
792         struct cgroup *e;
793         for(e = cgroup_root; e->next ;e = e->next) ;
794         e->next = cg;
795     }
796
797     cgroup_root_count++;
798
799     // fix the chart_id and title by calling the external script
800     if(!simple_pattern_matches(disabled_cgroup_renames, cg->id) &&
801        !simple_pattern_matches(disabled_cgroup_renames, cg->chart_id)) {
802
803         cgroup_get_chart_name(cg);
804
805         debug(D_CGROUP, "cgroup '%s' renamed to '%s' (title: '%s')", cg->id, cg->chart_id, cg->chart_title);
806     }
807     else
808         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);
809
810     int user_configurable = 1;
811
812     // check if this cgroup should be a systemd service
813     if(cgroup_enable_systemd_services) {
814         if(simple_pattern_matches(systemd_services_cgroups, cg->id) ||
815                 simple_pattern_matches(systemd_services_cgroups, cg->chart_id)) {
816             debug(D_CGROUP, "cgroup '%s' with chart id '%s' (title: '%s') matches systemd services cgroups", cg->id, cg->chart_id, cg->chart_title);
817
818             char buffer[CGROUP_CHARTID_LINE_MAX + 1];
819             cg->options |= CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE;
820
821             strncpy(buffer, cg->id, CGROUP_CHARTID_LINE_MAX);
822             char *s = buffer;
823
824             //freez(cg->chart_id);
825             //cg->chart_id = cgroup_chart_id_strdupz(s);
826             //cg->hash_chart = simple_hash(cg->chart_id);
827
828             // skip to the last slash
829             size_t len = strlen(s);
830             while(len--) if(unlikely(s[len] == '/')) break;
831             if(len) s = &s[len + 1];
832
833             // remove extension
834             len = strlen(s);
835             while(len--) if(unlikely(s[len] == '.')) break;
836             if(len) s[len] = '\0';
837
838             freez(cg->chart_title);
839             cg->chart_title = cgroup_title_strdupz(s);
840
841             cg->enabled = 1;
842             user_configurable = 0;
843
844             debug(D_CGROUP, "cgroup '%s' renamed to '%s' (title: '%s')", cg->id, cg->chart_id, cg->chart_title);
845         }
846         else
847             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);
848     }
849
850     if(user_configurable) {
851         // allow the user to enable/disable this individualy
852         char option[FILENAME_MAX + 1];
853         snprintfz(option, FILENAME_MAX, "enable cgroup %s", cg->chart_title);
854         cg->enabled = (char) config_get_boolean("plugin:cgroups", option, def);
855     }
856
857     // detect duplicate cgroups
858     if(cg->enabled) {
859         struct cgroup *t;
860         for (t = cgroup_root; t; t = t->next) {
861             if (t != cg && t->enabled && t->hash_chart == cg->hash_chart && !strcmp(t->chart_id, cg->chart_id)) {
862                 if (!strncmp(t->chart_id, "/system.slice/", 14) && !strncmp(cg->chart_id, "/init.scope/system.slice/", 25)) {
863                     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'.",
864                           cg->chart_id, t->id, cg->id, t->id);
865                     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'.",
866                           cg->chart_id, t->id, cg->id, t->id);
867                     t->enabled = 0;
868                     t->options |= CGROUP_OPTIONS_DISABLED_DUPLICATE;
869                 }
870                 else {
871                     error("Control group with chart id '%s' already exists with id '%s' and is enabled and available. Disabling cgroup with id '%s'.",
872                           cg->chart_id, t->id, cg->id);
873                     debug(D_CGROUP, "Control group with chart id '%s' already exists with id '%s' and is enabled and available. Disabling cgroup with id '%s'.",
874                           cg->chart_id, t->id, cg->id);
875                     cg->enabled = 0;
876                     cg->options |= CGROUP_OPTIONS_DISABLED_DUPLICATE;
877                 }
878
879                 break;
880             }
881         }
882     }
883
884     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");
885
886     return cg;
887 }
888
889 void cgroup_free(struct cgroup *cg) {
890     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");
891
892     freez(cg->cpuacct_usage.cpu_percpu);
893
894     freez(cg->cpuacct_stat.filename);
895     freez(cg->cpuacct_usage.filename);
896
897     freez(cg->memory.filename);
898     freez(cg->memory.filename_failcnt);
899     freez(cg->memory.filename_usage_in_bytes);
900     freez(cg->memory.filename_msw_usage_in_bytes);
901
902     freez(cg->io_service_bytes.filename);
903     freez(cg->io_serviced.filename);
904
905     freez(cg->throttle_io_service_bytes.filename);
906     freez(cg->throttle_io_serviced.filename);
907
908     freez(cg->io_merged.filename);
909     freez(cg->io_queued.filename);
910
911     freez(cg->id);
912     freez(cg->chart_id);
913     freez(cg->chart_title);
914
915     freez(cg);
916
917     cgroup_root_count--;
918 }
919
920 // find if a given cgroup exists
921 struct cgroup *cgroup_find(const char *id) {
922     debug(D_CGROUP, "searching for cgroup '%s'", id);
923
924     uint32_t hash = simple_hash(id);
925
926     struct cgroup *cg;
927     for(cg = cgroup_root; cg ; cg = cg->next) {
928         if(hash == cg->hash && strcmp(id, cg->id) == 0)
929             break;
930     }
931
932     debug(D_CGROUP, "cgroup '%s' %s in memory", id, (cg)?"found":"not found");
933     return cg;
934 }
935
936 // ----------------------------------------------------------------------------
937 // detect running cgroups
938
939 // callback for find_file_in_subdirs()
940 void found_subdir_in_dir(const char *dir) {
941     debug(D_CGROUP, "examining cgroup dir '%s'", dir);
942
943     struct cgroup *cg = cgroup_find(dir);
944     if(!cg) {
945         if(*dir && cgroup_max_depth > 0) {
946             int depth = 0;
947             const char *s;
948
949             for(s = dir; *s ;s++)
950                 if(unlikely(*s == '/'))
951                     depth++;
952
953             if(depth > cgroup_max_depth) {
954                 info("cgroup '%s' is too deep (%d, while max is %d)", dir, depth, cgroup_max_depth);
955                 return;
956             }
957         }
958         // debug(D_CGROUP, "will add dir '%s' as cgroup", dir);
959         cg = cgroup_add(dir);
960     }
961
962     if(cg) cg->available = 1;
963 }
964
965 int find_dir_in_subdirs(const char *base, const char *this, void (*callback)(const char *)) {
966     if(!this) this = base;
967     debug(D_CGROUP, "searching for directories in '%s' (base '%s')", this?this:"", base);
968
969     size_t dirlen = strlen(this), baselen = strlen(base);
970
971     int ret = -1;
972     int enabled = -1;
973
974     const char *relative_path = &this[baselen];
975     if(!*relative_path) relative_path = "/";
976
977     DIR *dir = opendir(this);
978     if(!dir) {
979         error("Cannot read cgroups directory '%s'", base);
980         return ret;
981     }
982     ret = 1;
983
984     callback(relative_path);
985
986     struct dirent *de = NULL;
987     while((de = readdir(dir))) {
988         if(de->d_type == DT_DIR
989             && (
990                 (de->d_name[0] == '.' && de->d_name[1] == '\0')
991                 || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
992                 ))
993             continue;
994
995         if(de->d_type == DT_DIR) {
996             if(enabled == -1) {
997                 const char *r = relative_path;
998                 if(*r == '\0') r = "/";
999
1000                 // do not decent in directories we are not interested
1001                 int def = 1;
1002                 if(simple_pattern_matches(disabled_cgroup_paths, r))
1003                     def = 0;
1004
1005                 // we check for this option here
1006                 // so that the config will not have settings
1007                 // for leaf directories
1008                 char option[FILENAME_MAX + 1];
1009                 snprintfz(option, FILENAME_MAX, "search for cgroups under %s", r);
1010                 option[FILENAME_MAX] = '\0';
1011                 enabled = config_get_boolean("plugin:cgroups", option, def);
1012             }
1013
1014             if(enabled) {
1015                 char *s = mallocz(dirlen + strlen(de->d_name) + 2);
1016                 strcpy(s, this);
1017                 strcat(s, "/");
1018                 strcat(s, de->d_name);
1019                 int ret2 = find_dir_in_subdirs(base, s, callback);
1020                 if(ret2 > 0) ret += ret2;
1021                 freez(s);
1022             }
1023         }
1024     }
1025
1026     closedir(dir);
1027     return ret;
1028 }
1029
1030 void mark_all_cgroups_as_not_available() {
1031     debug(D_CGROUP, "marking all cgroups as not available");
1032
1033     struct cgroup *cg;
1034
1035     // mark all as not available
1036     for(cg = cgroup_root; cg ; cg = cg->next) {
1037         cg->available = 0;
1038     }
1039 }
1040
1041 void cleanup_all_cgroups() {
1042     struct cgroup *cg = cgroup_root, *last = NULL;
1043
1044     for(; cg ;) {
1045         if(!cg->available) {
1046             // enable the first duplicate cgroup
1047             {
1048                 struct cgroup *t;
1049                 for(t = cgroup_root; t ; t = t->next) {
1050                     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)) {
1051                         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);
1052                         t->enabled = 1;
1053                         t->options &= ~CGROUP_OPTIONS_DISABLED_DUPLICATE;
1054                         break;
1055                     }
1056                 }
1057             }
1058
1059             if(!last)
1060                 cgroup_root = cg->next;
1061             else
1062                 last->next = cg->next;
1063
1064             cgroup_free(cg);
1065
1066             if(!last)
1067                 cg = cgroup_root;
1068             else
1069                 cg = last->next;
1070         }
1071         else {
1072             last = cg;
1073             cg = cg->next;
1074         }
1075     }
1076 }
1077
1078 void find_all_cgroups() {
1079     debug(D_CGROUP, "searching for cgroups");
1080
1081     mark_all_cgroups_as_not_available();
1082
1083     if(cgroup_enable_cpuacct_stat || cgroup_enable_cpuacct_usage) {
1084         if (find_dir_in_subdirs(cgroup_cpuacct_base, NULL, found_subdir_in_dir) == -1) {
1085             cgroup_enable_cpuacct_stat = cgroup_enable_cpuacct_usage = 0;
1086             error("disabled cgroup cpu statistics.");
1087         }
1088     }
1089
1090     if(cgroup_enable_blkio) {
1091         if (find_dir_in_subdirs(cgroup_blkio_base, NULL, found_subdir_in_dir) == -1) {
1092             cgroup_enable_blkio = 0;
1093             error("disabled cgroup blkio statistics.");
1094         }
1095     }
1096
1097     if(cgroup_enable_memory) {
1098         if(find_dir_in_subdirs(cgroup_memory_base, NULL, found_subdir_in_dir) == -1) {
1099             cgroup_enable_memory = 0;
1100             error("disabled cgroup memory statistics.");
1101         }
1102     }
1103
1104     if(cgroup_enable_devices) {
1105         if(find_dir_in_subdirs(cgroup_devices_base, NULL, found_subdir_in_dir) == -1) {
1106             cgroup_enable_devices = 0;
1107             error("disabled cgroup devices statistics.");
1108         }
1109     }
1110
1111     // remove any non-existing cgroups
1112     cleanup_all_cgroups();
1113
1114     struct cgroup *cg;
1115     struct stat buf;
1116     for(cg = cgroup_root; cg ; cg = cg->next) {
1117         // fprintf(stderr, " >>> CGROUP '%s' (%u - %s) with name '%s'\n", cg->id, cg->hash, cg->available?"available":"stopped", cg->name);
1118
1119         if(unlikely(!cg->available))
1120             continue;
1121
1122         debug(D_CGROUP, "checking paths for cgroup '%s'", cg->id);
1123
1124         // check for newly added cgroups
1125         // and update the filenames they read
1126         char filename[FILENAME_MAX + 1];
1127         if(unlikely(cgroup_enable_cpuacct_stat && !cg->cpuacct_stat.filename)) {
1128             snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.stat", cgroup_cpuacct_base, cg->id);
1129             if(stat(filename, &buf) != -1) {
1130                 cg->cpuacct_stat.filename = strdupz(filename);
1131                 debug(D_CGROUP, "cpuacct.stat filename for cgroup '%s': '%s'", cg->id, cg->cpuacct_stat.filename);
1132             }
1133             else debug(D_CGROUP, "cpuacct.stat file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1134         }
1135
1136         if(unlikely(cgroup_enable_cpuacct_usage && !cg->cpuacct_usage.filename)) {
1137             snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.usage_percpu", cgroup_cpuacct_base, cg->id);
1138             if(stat(filename, &buf) != -1) {
1139                 cg->cpuacct_usage.filename = strdupz(filename);
1140                 debug(D_CGROUP, "cpuacct.usage_percpu filename for cgroup '%s': '%s'", cg->id, cg->cpuacct_usage.filename);
1141             }
1142             else debug(D_CGROUP, "cpuacct.usage_percpu file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1143         }
1144
1145         if(unlikely(cgroup_enable_memory)) {
1146             if(unlikely(!cg->memory.filename)) {
1147                 snprintfz(filename, FILENAME_MAX, "%s%s/memory.stat", cgroup_memory_base, cg->id);
1148                 if(stat(filename, &buf) != -1) {
1149                     cg->memory.filename = strdupz(filename);
1150                     debug(D_CGROUP, "memory.stat filename for cgroup '%s': '%s'", cg->id, cg->memory.filename);
1151                 }
1152                 else
1153                     debug(D_CGROUP, "memory.stat file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1154             }
1155             if(unlikely(!cg->memory.filename_usage_in_bytes)) {
1156                 snprintfz(filename, FILENAME_MAX, "%s%s/memory.usage_in_bytes", cgroup_memory_base, cg->id);
1157                 if(stat(filename, &buf) != -1) {
1158                     cg->memory.filename_usage_in_bytes = strdupz(filename);
1159                     debug(D_CGROUP, "memory.usage_in_bytes filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_usage_in_bytes);
1160                 }
1161                 else
1162                     debug(D_CGROUP, "memory.usage_in_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1163             }
1164             if(unlikely(!cg->memory.filename_msw_usage_in_bytes)) {
1165                 snprintfz(filename, FILENAME_MAX, "%s%s/memory.msw_usage_in_bytes", cgroup_memory_base, cg->id);
1166                 if(stat(filename, &buf) != -1) {
1167                     cg->memory.filename_msw_usage_in_bytes = strdupz(filename);
1168                     debug(D_CGROUP, "memory.msw_usage_in_bytes filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_msw_usage_in_bytes);
1169                 }
1170                 else
1171                     debug(D_CGROUP, "memory.msw_usage_in_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1172             }
1173             if(unlikely(!cg->memory.filename_failcnt)) {
1174                 snprintfz(filename, FILENAME_MAX, "%s%s/memory.failcnt", cgroup_memory_base, cg->id);
1175                 if(stat(filename, &buf) != -1) {
1176                     cg->memory.filename_failcnt = strdupz(filename);
1177                     debug(D_CGROUP, "memory.failcnt filename for cgroup '%s': '%s'", cg->id, cg->memory.filename_failcnt);
1178                 }
1179                 else
1180                     debug(D_CGROUP, "memory.failcnt file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1181             }
1182         }
1183
1184         if(unlikely(cgroup_enable_blkio)) {
1185             if(unlikely(!cg->io_service_bytes.filename)) {
1186                 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_service_bytes", cgroup_blkio_base, cg->id);
1187                 if(stat(filename, &buf) != -1) {
1188                     cg->io_service_bytes.filename = strdupz(filename);
1189                     debug(D_CGROUP, "io_service_bytes filename for cgroup '%s': '%s'", cg->id, cg->io_service_bytes.filename);
1190                 }
1191                 else debug(D_CGROUP, "io_service_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1192             }
1193             if(unlikely(!cg->io_serviced.filename)) {
1194                 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_serviced", cgroup_blkio_base, cg->id);
1195                 if(stat(filename, &buf) != -1) {
1196                     cg->io_serviced.filename = strdupz(filename);
1197                     debug(D_CGROUP, "io_serviced filename for cgroup '%s': '%s'", cg->id, cg->io_serviced.filename);
1198                 }
1199                 else debug(D_CGROUP, "io_serviced file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1200             }
1201             if(unlikely(!cg->throttle_io_service_bytes.filename)) {
1202                 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.throttle.io_service_bytes", cgroup_blkio_base, cg->id);
1203                 if(stat(filename, &buf) != -1) {
1204                     cg->throttle_io_service_bytes.filename = strdupz(filename);
1205                     debug(D_CGROUP, "throttle_io_service_bytes filename for cgroup '%s': '%s'", cg->id, cg->throttle_io_service_bytes.filename);
1206                 }
1207                 else debug(D_CGROUP, "throttle_io_service_bytes file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1208             }
1209             if(unlikely(!cg->throttle_io_serviced.filename)) {
1210                 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.throttle.io_serviced", cgroup_blkio_base, cg->id);
1211                 if(stat(filename, &buf) != -1) {
1212                     cg->throttle_io_serviced.filename = strdupz(filename);
1213                     debug(D_CGROUP, "throttle_io_serviced filename for cgroup '%s': '%s'", cg->id, cg->throttle_io_serviced.filename);
1214                 }
1215                 else debug(D_CGROUP, "throttle_io_serviced file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1216             }
1217             if(unlikely(!cg->io_merged.filename)) {
1218                 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_merged", cgroup_blkio_base, cg->id);
1219                 if(stat(filename, &buf) != -1) {
1220                     cg->io_merged.filename = strdupz(filename);
1221                     debug(D_CGROUP, "io_merged filename for cgroup '%s': '%s'", cg->id, cg->io_merged.filename);
1222                 }
1223                 else debug(D_CGROUP, "io_merged file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1224             }
1225             if(unlikely(!cg->io_queued.filename)) {
1226                 snprintfz(filename, FILENAME_MAX, "%s%s/blkio.io_queued", cgroup_blkio_base, cg->id);
1227                 if(stat(filename, &buf) != -1) {
1228                     cg->io_queued.filename = strdupz(filename);
1229                     debug(D_CGROUP, "io_queued filename for cgroup '%s': '%s'", cg->id, cg->io_queued.filename);
1230                 }
1231                 else debug(D_CGROUP, "io_queued file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1232             }
1233         }
1234     }
1235
1236     debug(D_CGROUP, "done searching for cgroups");
1237     return;
1238 }
1239
1240 // ----------------------------------------------------------------------------
1241 // generate charts
1242
1243 #define CHART_TITLE_MAX 300
1244
1245 void update_services_charts(int update_every, int do_cpu, int do_mem_usage) {
1246     static RRDSET *st_cpu = NULL;
1247     static RRDSET *st_mem_usage = NULL;
1248
1249     // create the charts
1250
1251     if(do_cpu) {
1252         if(unlikely(!st_cpu)) {
1253             char title[CHART_TITLE_MAX + 1];
1254
1255             st_cpu = rrdset_find_bytype("services", "cpu");
1256             if(!st_cpu) {
1257                 snprintfz(title, CHART_TITLE_MAX, "Systemd Services CPU utilization (%d%% = %d core%s)", (processors * 100), processors, (processors > 1) ? "s" : "");
1258                 st_cpu = rrdset_create("services", "cpu", NULL, "cpu", "services.cpu", title, "%", 30000, update_every, RRDSET_TYPE_STACKED);
1259             }
1260         }
1261         else rrdset_next(st_cpu);
1262     }
1263
1264     if(do_mem_usage) {
1265         if(unlikely(!st_mem_usage)) {
1266             st_mem_usage = rrdset_find_bytype("services", "mem_usage");
1267             if(!st_mem_usage)
1268                 st_mem_usage = rrdset_create("services", "mem_usage", NULL, "mem", "services.mem_usage", "Systemd Services RAM Usage", "MB", 30001, update_every, RRDSET_TYPE_STACKED);
1269         }
1270         else rrdset_next(st_mem_usage);
1271     }
1272
1273     // update the values
1274     struct cgroup *cg;
1275     for(cg = cgroup_root; cg ; cg = cg->next) {
1276         if(!cg->available || !cg->enabled || !(cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE)) continue;
1277
1278         if(do_cpu && cg->cpuacct_stat.updated) {
1279             if(unlikely(!cg->rd_cpu))
1280                 cg->rd_cpu = rrddim_add(st_cpu, cg->chart_id, cg->chart_title, 100, hz, RRDDIM_INCREMENTAL);
1281
1282             rrddim_set_by_pointer(st_cpu, cg->rd_cpu, cg->cpuacct_stat.user + cg->cpuacct_stat.system);
1283         }
1284
1285         if(do_mem_usage) {
1286             if(unlikely(!cg->rd_mem_usage))
1287                 cg->rd_mem_usage = rrddim_add(st_mem_usage, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1288
1289             rrddim_set_by_pointer(st_mem_usage, cg->rd_mem_usage, cg->memory.usage_in_bytes);
1290         }
1291     }
1292
1293     // complete the iteration
1294     if(do_cpu) rrdset_done(st_cpu);
1295     if(do_mem_usage) rrdset_done(st_mem_usage);
1296 }
1297
1298 static inline char *cgroup_chart_type(char *buffer, const char *id, size_t len) {
1299     if(buffer[0]) return buffer;
1300
1301     if(id[0] == '\0' || (id[0] == '/' && id[1] == '\0'))
1302         strncpy(buffer, "cgroup_root", len);
1303     else
1304         snprintfz(buffer, len, "cgroup_%s", id);
1305
1306     netdata_fix_chart_id(buffer);
1307     return buffer;
1308 }
1309
1310 void update_cgroup_charts(int update_every) {
1311     debug(D_CGROUP, "updating cgroups charts");
1312
1313     char type[RRD_ID_LENGTH_MAX + 1];
1314     char title[CHART_TITLE_MAX + 1];
1315
1316     int services_do_cpu = 0;
1317     int services_do_mem_usage = 0;
1318
1319     struct cgroup *cg;
1320     for(cg = cgroup_root; cg ; cg = cg->next) {
1321         if(!cg->available || !cg->enabled) continue;
1322
1323         if(cgroup_enable_systemd_services && cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE) {
1324             if(cg->cpuacct_stat.updated && (cg->cpuacct_stat.user || cg->cpuacct_stat.system)) services_do_cpu++;
1325             if(cg->memory.usage_in_bytes_updated) services_do_mem_usage++;
1326             continue;
1327         }
1328
1329         type[0] = '\0';
1330
1331         if(cg->cpuacct_stat.updated) {
1332             if(unlikely(!cg->st_cpu)) {
1333                 cg->st_cpu = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "cpu");
1334                 if(!cg->st_cpu) {
1335                     snprintfz(title, CHART_TITLE_MAX, "CPU Usage (%d%% = %d core%s) for cgroup %s", (processors * 100), processors, (processors > 1) ? "s" : "", cg->chart_title);
1336                     cg->st_cpu = rrdset_create(type, "cpu", NULL, "cpu", "cgroup.cpu", title, "%", 40000, update_every, RRDSET_TYPE_STACKED);
1337
1338                     rrddim_add(cg->st_cpu, "user", NULL, 100, hz, RRDDIM_INCREMENTAL);
1339                     rrddim_add(cg->st_cpu, "system", NULL, 100, hz, RRDDIM_INCREMENTAL);
1340                 }
1341             }
1342             else rrdset_next(cg->st_cpu);
1343
1344             rrddim_set(cg->st_cpu, "user", cg->cpuacct_stat.user);
1345             rrddim_set(cg->st_cpu, "system", cg->cpuacct_stat.system);
1346             rrdset_done(cg->st_cpu);
1347         }
1348
1349         if(cg->cpuacct_usage.updated) {
1350             char id[RRD_ID_LENGTH_MAX + 1];
1351             unsigned int i;
1352
1353             if(unlikely(!cg->st_cpu_per_core)) {
1354                 cg->st_cpu_per_core = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "cpu_per_core");
1355                 if(!cg->st_cpu_per_core) {
1356                     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);
1357                     cg->st_cpu_per_core = rrdset_create(type, "cpu_per_core", NULL, "cpu", "cgroup.cpu_per_core", title, "%", 40100, update_every, RRDSET_TYPE_STACKED);
1358
1359                     for(i = 0; i < cg->cpuacct_usage.cpus; i++) {
1360                         snprintfz(id, CHART_TITLE_MAX, "cpu%u", i);
1361                         rrddim_add(cg->st_cpu_per_core, id, NULL, 100, 1000000000, RRDDIM_INCREMENTAL);
1362                     }
1363                 }
1364             }
1365             else rrdset_next(cg->st_cpu_per_core);
1366
1367             for(i = 0; i < cg->cpuacct_usage.cpus ;i++) {
1368                 snprintfz(id, CHART_TITLE_MAX, "cpu%u", i);
1369                 rrddim_set(cg->st_cpu_per_core, id, cg->cpuacct_usage.cpu_percpu[i]);
1370             }
1371             rrdset_done(cg->st_cpu_per_core);
1372         }
1373
1374         if(cg->memory.updated) {
1375             if(cg->memory.cache || cg->memory.rss || cg->memory.rss_huge || cg->memory.mapped_file) {
1376                 if(unlikely(!cg->st_mem)) {
1377                     cg->st_mem = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem");
1378                     if(!cg->st_mem) {
1379                         snprintfz(title, CHART_TITLE_MAX, "Memory Usage for cgroup %s", cg->chart_title);
1380                         cg->st_mem = rrdset_create(type, "mem", NULL, "mem", "cgroup.mem", title, "MB", 40210, update_every, RRDSET_TYPE_STACKED);
1381
1382                         rrddim_add(cg->st_mem, "cache", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1383                         rrddim_add(cg->st_mem, "rss", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1384                         if(cg->memory.has_dirty_swap)
1385                             rrddim_add(cg->st_mem, "swap", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1386                         rrddim_add(cg->st_mem, "rss_huge", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1387                         rrddim_add(cg->st_mem, "mapped_file", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1388                     }
1389                 }
1390                 else rrdset_next(cg->st_mem);
1391
1392                 rrddim_set(cg->st_mem, "cache", cg->memory.cache);
1393                 rrddim_set(cg->st_mem, "rss", cg->memory.rss);
1394                 if(cg->memory.has_dirty_swap)
1395                     rrddim_set(cg->st_mem, "swap", cg->memory.swap);
1396                 rrddim_set(cg->st_mem, "rss_huge", cg->memory.rss_huge);
1397                 rrddim_set(cg->st_mem, "mapped_file", cg->memory.mapped_file);
1398                 rrdset_done(cg->st_mem);
1399             }
1400
1401             if(unlikely(!cg->st_writeback)) {
1402                 cg->st_writeback = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "writeback");
1403                 if(!cg->st_writeback) {
1404                     snprintfz(title, CHART_TITLE_MAX, "Writeback Memory for cgroup %s", cg->chart_title);
1405                     cg->st_writeback = rrdset_create(type, "writeback", NULL, "mem", "cgroup.writeback", title, "MB", 40300, update_every, RRDSET_TYPE_AREA);
1406
1407                     if(cg->memory.has_dirty_swap)
1408                         rrddim_add(cg->st_writeback, "dirty", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1409                     rrddim_add(cg->st_writeback, "writeback", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1410                 }
1411             }
1412             else rrdset_next(cg->st_writeback);
1413
1414             if(cg->memory.has_dirty_swap)
1415                 rrddim_set(cg->st_writeback, "dirty", cg->memory.dirty);
1416             rrddim_set(cg->st_writeback, "writeback", cg->memory.writeback);
1417             rrdset_done(cg->st_writeback);
1418
1419             if(cg->memory.pgpgin || cg->memory.pgpgout) {
1420                 if(unlikely(!cg->st_mem_activity)) {
1421                     cg->st_mem_activity = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_activity");
1422                     if(!cg->st_mem_activity) {
1423                         snprintfz(title, CHART_TITLE_MAX, "Memory Activity for cgroup %s", cg->chart_title);
1424                         cg->st_mem_activity = rrdset_create(type, "mem_activity", NULL, "mem", "cgroup.mem_activity", title, "MB/s", 40400, update_every, RRDSET_TYPE_LINE);
1425
1426                         rrddim_add(cg->st_mem_activity, "pgpgin", "in", sysconf(_SC_PAGESIZE), 1024 * 1024, RRDDIM_INCREMENTAL);
1427                         rrddim_add(cg->st_mem_activity, "pgpgout", "out", -sysconf(_SC_PAGESIZE), 1024 * 1024, RRDDIM_INCREMENTAL);
1428                     }
1429                 }
1430                 else rrdset_next(cg->st_mem_activity);
1431
1432                 rrddim_set(cg->st_mem_activity, "pgpgin", cg->memory.pgpgin);
1433                 rrddim_set(cg->st_mem_activity, "pgpgout", cg->memory.pgpgout);
1434                 rrdset_done(cg->st_mem_activity);
1435             }
1436
1437             if(cg->memory.pgfault || cg->memory.pgmajfault) {
1438                 if(unlikely(!cg->st_pgfaults)) {
1439                     cg->st_pgfaults = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "pgfaults");
1440                     if(!cg->st_pgfaults) {
1441                         snprintfz(title, CHART_TITLE_MAX, "Memory Page Faults for cgroup %s", cg->chart_title);
1442                         cg->st_pgfaults = rrdset_create(type, "pgfaults", NULL, "mem", "cgroup.pgfaults", title, "MB/s", 40500, update_every, RRDSET_TYPE_LINE);
1443
1444                         rrddim_add(cg->st_pgfaults, "pgfault", NULL, sysconf(_SC_PAGESIZE), 1024 * 1024, RRDDIM_INCREMENTAL);
1445                         rrddim_add(cg->st_pgfaults, "pgmajfault", "swap", -sysconf(_SC_PAGESIZE), 1024 * 1024, RRDDIM_INCREMENTAL);
1446                     }
1447                 }
1448                 else rrdset_next(cg->st_pgfaults);
1449
1450                 rrddim_set(cg->st_pgfaults, "pgfault", cg->memory.pgfault);
1451                 rrddim_set(cg->st_pgfaults, "pgmajfault", cg->memory.pgmajfault);
1452                 rrdset_done(cg->st_pgfaults);
1453             }
1454         }
1455
1456         if(cg->memory.usage_in_bytes_updated) {
1457             if(unlikely(!cg->st_mem_usage)) {
1458                 cg->st_mem_usage = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_usage");
1459                 if(!cg->st_mem_usage) {
1460                     snprintfz(title, CHART_TITLE_MAX, "Total Memory for cgroup %s", cg->chart_title);
1461                     cg->st_mem_usage = rrdset_create(type, "mem_usage", NULL, "mem", "cgroup.mem_usage", title, "MB", 40200, update_every, RRDSET_TYPE_STACKED);
1462
1463                     rrddim_add(cg->st_mem_usage, "ram", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1464                     rrddim_add(cg->st_mem_usage, "swap", NULL, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1465                 }
1466             }
1467             else rrdset_next(cg->st_mem_usage);
1468
1469             rrddim_set(cg->st_mem_usage, "ram", cg->memory.usage_in_bytes);
1470             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);
1471             rrdset_done(cg->st_mem_usage);
1472         }
1473
1474         if(cg->memory.failcnt_updated && cg->memory.failcnt) {
1475             if(unlikely(!cg->st_mem_failcnt)) {
1476                 cg->st_mem_failcnt = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "mem_failcnt");
1477                 if(!cg->st_mem_failcnt) {
1478                     snprintfz(title, CHART_TITLE_MAX, "Memory Limit Failures for cgroup %s", cg->chart_title);
1479                     cg->st_mem_failcnt = rrdset_create(type, "mem_failcnt", NULL, "mem", "cgroup.mem_failcnt", title, "MB", 40250, update_every, RRDSET_TYPE_LINE);
1480
1481                     rrddim_add(cg->st_mem_failcnt, "failures", NULL, 1, 1, RRDDIM_INCREMENTAL);
1482                 }
1483             }
1484             else rrdset_next(cg->st_mem_failcnt);
1485
1486             rrddim_set(cg->st_mem_failcnt, "failures", cg->memory.failcnt);
1487             rrdset_done(cg->st_mem_failcnt);
1488         }
1489
1490         if(cg->io_service_bytes.updated && (cg->io_service_bytes.Read || cg->io_service_bytes.Write)) {
1491             if(unlikely(!cg->st_io)) {
1492                 cg->st_io = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "io");
1493                 if(!cg->st_io) {
1494                     snprintfz(title, CHART_TITLE_MAX, "I/O Bandwidth (all disks) for cgroup %s", cg->chart_title);
1495                     cg->st_io = rrdset_create(type, "io", NULL, "disk", "cgroup.io", title, "KB/s", 41200, update_every, RRDSET_TYPE_AREA);
1496
1497                     rrddim_add(cg->st_io, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
1498                     rrddim_add(cg->st_io, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
1499                 }
1500             }
1501             else rrdset_next(cg->st_io);
1502
1503             rrddim_set(cg->st_io, "read", cg->io_service_bytes.Read);
1504             rrddim_set(cg->st_io, "write", cg->io_service_bytes.Write);
1505             rrdset_done(cg->st_io);
1506         }
1507
1508         if(cg->io_serviced.updated && (cg->io_serviced.Read || cg->io_serviced.Write)) {
1509             if(unlikely(!cg->st_serviced_ops)) {
1510                 cg->st_serviced_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "serviced_ops");
1511                 if(!cg->st_serviced_ops) {
1512                     snprintfz(title, CHART_TITLE_MAX, "Serviced I/O Operations (all disks) for cgroup %s"
1513                               , cg->chart_title);
1514                     cg->st_serviced_ops = rrdset_create(type, "serviced_ops", NULL, "disk", "cgroup.serviced_ops", title, "operations/s", 41200, update_every, RRDSET_TYPE_LINE);
1515
1516                     rrddim_add(cg->st_serviced_ops, "read", NULL, 1, 1, RRDDIM_INCREMENTAL);
1517                     rrddim_add(cg->st_serviced_ops, "write", NULL, -1, 1, RRDDIM_INCREMENTAL);
1518                 }
1519             }
1520             else rrdset_next(cg->st_serviced_ops);
1521
1522             rrddim_set(cg->st_serviced_ops, "read", cg->io_serviced.Read);
1523             rrddim_set(cg->st_serviced_ops, "write", cg->io_serviced.Write);
1524             rrdset_done(cg->st_serviced_ops);
1525         }
1526
1527         if(cg->throttle_io_service_bytes.updated && (cg->throttle_io_service_bytes.Read || cg->throttle_io_service_bytes.Write)) {
1528             if(unlikely(!cg->st_throttle_io)) {
1529                 cg->st_throttle_io = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "throttle_io");
1530                 if(!cg->st_throttle_io) {
1531                     snprintfz(title, CHART_TITLE_MAX, "Throttle I/O Bandwidth (all disks) for cgroup %s", cg->chart_title);
1532                     cg->st_throttle_io = rrdset_create(type, "throttle_io", NULL, "disk", "cgroup.throttle_io", title, "KB/s", 41200, update_every, RRDSET_TYPE_AREA);
1533
1534                     rrddim_add(cg->st_throttle_io, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
1535                     rrddim_add(cg->st_throttle_io, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
1536                 }
1537             }
1538             else rrdset_next(cg->st_throttle_io);
1539
1540             rrddim_set(cg->st_throttle_io, "read", cg->throttle_io_service_bytes.Read);
1541             rrddim_set(cg->st_throttle_io, "write", cg->throttle_io_service_bytes.Write);
1542             rrdset_done(cg->st_throttle_io);
1543         }
1544
1545         if(cg->throttle_io_serviced.updated && (cg->throttle_io_serviced.Read || cg->throttle_io_serviced.Write)) {
1546             if(unlikely(!cg->st_throttle_serviced_ops)) {
1547                 cg->st_throttle_serviced_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "throttle_serviced_ops");
1548                 if(!cg->st_throttle_serviced_ops) {
1549                     snprintfz(title, CHART_TITLE_MAX, "Throttle Serviced I/O Operations (all disks) for cgroup %s", cg->chart_title);
1550                     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);
1551
1552                     rrddim_add(cg->st_throttle_serviced_ops, "read", NULL, 1, 1, RRDDIM_INCREMENTAL);
1553                     rrddim_add(cg->st_throttle_serviced_ops, "write", NULL, -1, 1, RRDDIM_INCREMENTAL);
1554                 }
1555             }
1556             else rrdset_next(cg->st_throttle_serviced_ops);
1557
1558             rrddim_set(cg->st_throttle_serviced_ops, "read", cg->throttle_io_serviced.Read);
1559             rrddim_set(cg->st_throttle_serviced_ops, "write", cg->throttle_io_serviced.Write);
1560             rrdset_done(cg->st_throttle_serviced_ops);
1561         }
1562
1563         if(cg->io_queued.updated) {
1564             if(unlikely(!cg->st_queued_ops)) {
1565                 cg->st_queued_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "queued_ops");
1566                 if(!cg->st_queued_ops) {
1567                     snprintfz(title, CHART_TITLE_MAX, "Queued I/O Operations (all disks) for cgroup %s", cg->chart_title);
1568                     cg->st_queued_ops = rrdset_create(type, "queued_ops", NULL, "disk", "cgroup.queued_ops", title, "operations", 42000, update_every, RRDSET_TYPE_LINE);
1569
1570                     rrddim_add(cg->st_queued_ops, "read", NULL, 1, 1, RRDDIM_ABSOLUTE);
1571                     rrddim_add(cg->st_queued_ops, "write", NULL, -1, 1, RRDDIM_ABSOLUTE);
1572                 }
1573             }
1574             else rrdset_next(cg->st_queued_ops);
1575
1576             rrddim_set(cg->st_queued_ops, "read", cg->io_queued.Read);
1577             rrddim_set(cg->st_queued_ops, "write", cg->io_queued.Write);
1578             rrdset_done(cg->st_queued_ops);
1579         }
1580
1581         if(cg->io_merged.updated && (cg->io_merged.Read || cg->io_merged.Write)) {
1582             if(unlikely(!cg->st_merged_ops)) {
1583                 cg->st_merged_ops = rrdset_find_bytype(cgroup_chart_type(type, cg->chart_id, RRD_ID_LENGTH_MAX), "merged_ops");
1584                 if(!cg->st_merged_ops) {
1585                     snprintfz(title, CHART_TITLE_MAX, "Merged I/O Operations (all disks) for cgroup %s", cg->chart_title);
1586                     cg->st_merged_ops = rrdset_create(type, "merged_ops", NULL, "disk", "cgroup.merged_ops", title, "operations/s", 42100, update_every, RRDSET_TYPE_LINE);
1587
1588                     rrddim_add(cg->st_merged_ops, "read", NULL, 1, 1024, RRDDIM_INCREMENTAL);
1589                     rrddim_add(cg->st_merged_ops, "write", NULL, -1, 1024, RRDDIM_INCREMENTAL);
1590                 }
1591             }
1592             else rrdset_next(cg->st_merged_ops);
1593
1594             rrddim_set(cg->st_merged_ops, "read", cg->io_merged.Read);
1595             rrddim_set(cg->st_merged_ops, "write", cg->io_merged.Write);
1596             rrdset_done(cg->st_merged_ops);
1597         }
1598     }
1599
1600     debug(D_CGROUP, "done updating cgroups charts");
1601
1602     if(cgroup_enable_systemd_services && services_do_cpu)
1603         update_services_charts(update_every, services_do_cpu, services_do_mem_usage);
1604 }
1605
1606 // ----------------------------------------------------------------------------
1607 // cgroups main
1608
1609 void *cgroups_main(void *ptr) {
1610     struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
1611
1612     info("CGROUP Plugin thread created with task id %d", gettid());
1613
1614     if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
1615         error("Cannot set pthread cancel type to DEFERRED.");
1616
1617     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
1618         error("Cannot set pthread cancel state to ENABLE.");
1619
1620     struct rusage thread;
1621
1622     // when ZERO, attempt to do it
1623     int vdo_cpu_netdata = config_get_boolean("plugin:cgroups", "cgroups plugin resource charts", 1);
1624
1625     read_cgroup_plugin_configuration();
1626
1627     RRDSET *stcpu_thread = NULL;
1628
1629     usec_t step = cgroup_update_every * USEC_PER_SEC;
1630     usec_t find_every = cgroup_check_for_new_every * USEC_PER_SEC, find_next = 0;
1631     for(;;) {
1632         usec_t now = now_monotonic_usec();
1633         usec_t next = now - (now % step) + step;
1634
1635         while(now < next) {
1636             sleep_usec(next - now);
1637             now = now_monotonic_usec();
1638         }
1639
1640         if(unlikely(netdata_exit)) break;
1641
1642         // BEGIN -- the job to be done
1643
1644         if(unlikely(now >= find_next)) {
1645             find_all_cgroups();
1646             find_next = now + find_every;
1647         }
1648
1649         read_all_cgroups(cgroup_root);
1650         update_cgroup_charts(cgroup_update_every);
1651
1652         // END -- the job is done
1653
1654         // --------------------------------------------------------------------
1655
1656         if(vdo_cpu_netdata) {
1657             getrusage(RUSAGE_THREAD, &thread);
1658
1659             if(!stcpu_thread) stcpu_thread = rrdset_find("netdata.plugin_cgroups_cpu");
1660             if(!stcpu_thread) {
1661                 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);
1662
1663                 rrddim_add(stcpu_thread, "user",  NULL,  1, 1000, RRDDIM_INCREMENTAL);
1664                 rrddim_add(stcpu_thread, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
1665             }
1666             else rrdset_next(stcpu_thread);
1667
1668             rrddim_set(stcpu_thread, "user"  , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
1669             rrddim_set(stcpu_thread, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
1670             rrdset_done(stcpu_thread);
1671         }
1672     }
1673
1674     info("CGROUP thread exiting");
1675
1676     static_thread->enabled = 0;
1677     pthread_exit(NULL);
1678     return NULL;
1679 }