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