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