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