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