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