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