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