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