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