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