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