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