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