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