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