cgroups optimizations for faster systemd services data collection
Costa Tsaousis (ktsaou) committed
Jan 16, 2017 at 10:52 UTC
3c3645c3cc12d74f6326c2a7ba46fb2aa6643c83
1 file changed
+102
-119
src/sys_fs_cgroup.c
+102
-119
@@ -29,7 +29,89 @@ static SIMPLE_PATTERN *systemd_services_cgroups = NULL;
29
30
static char *cgroups_rename_script = PLUGINS_DIR "/cgroup-name.sh";
31
32
+static uint32_t Read_hash = 0;
33
+static uint32_t Write_hash = 0;
34
+static uint32_t Sync_hash = 0;
35
+static uint32_t Async_hash = 0;
36
+static uint32_t Total_hash = 0;
37
+static uint32_t user_hash = 0;
38
+static uint32_t system_hash = 0;
39
+static uint32_t cache_hash = 0;
40
+static uint32_t rss_hash = 0;
41
+static uint32_t rss_huge_hash = 0;
42
+static uint32_t mapped_file_hash = 0;
43
+static uint32_t writeback_hash = 0;
44
+static uint32_t dirty_hash = 0;
45
+static uint32_t swap_hash = 0;
46
+static uint32_t pgpgin_hash = 0;
47
+static uint32_t pgpgout_hash = 0;
48
+static uint32_t pgfault_hash = 0;
49
+static uint32_t pgmajfault_hash = 0;
50
+static uint32_t inactive_anon_hash = 0;
51
+static uint32_t active_anon_hash = 0;
52
+static uint32_t inactive_file_hash = 0;
53
+static uint32_t active_file_hash = 0;
54
+static uint32_t unevictable_hash = 0;
55
+static uint32_t hierarchical_memory_limit_hash = 0;
56
+static uint32_t total_cache_hash = 0;
57
+static uint32_t total_rss_hash = 0;
58
+static uint32_t total_rss_huge_hash = 0;
59
+static uint32_t total_mapped_file_hash = 0;
60
+static uint32_t total_writeback_hash = 0;
61
+static uint32_t total_dirty_hash = 0;
62
+static uint32_t total_swap_hash = 0;
63
+static uint32_t total_pgpgin_hash = 0;
64
+static uint32_t total_pgpgout_hash = 0;
65
+static uint32_t total_pgfault_hash = 0;
66
+static uint32_t total_pgmajfault_hash = 0;
67
+static uint32_t total_inactive_anon_hash = 0;
68
+static uint32_t total_active_anon_hash = 0;
69
+static uint32_t total_inactive_file_hash = 0;
70
+static uint32_t total_active_file_hash = 0;
71
+static uint32_t total_unevictable_hash = 0;
72
+
73
void read_cgroup_plugin_configuration() {
74
+ Read_hash = simple_hash("Read");
75
+ Write_hash = simple_hash("Write");
76
+ Sync_hash = simple_hash("Sync");
77
+ Async_hash = simple_hash("Async");
78
+ Total_hash = simple_hash("Total");
79
+ user_hash = simple_hash("user");
80
+ system_hash = simple_hash("system");
81
+ cache_hash = simple_hash("cache");
82
+ rss_hash = simple_hash("rss");
83
+ rss_huge_hash = simple_hash("rss_huge");
84
+ mapped_file_hash = simple_hash("mapped_file");
85
+ writeback_hash = simple_hash("writeback");
86
+ dirty_hash = simple_hash("dirty");
87
+ swap_hash = simple_hash("swap");
88
+ pgpgin_hash = simple_hash("pgpgin");
89
+ pgpgout_hash = simple_hash("pgpgout");
90
+ pgfault_hash = simple_hash("pgfault");
91
+ pgmajfault_hash = simple_hash("pgmajfault");
92
+ inactive_anon_hash = simple_hash("inactive_anon");
93
+ active_anon_hash = simple_hash("active_anon");
94
+ inactive_file_hash = simple_hash("inactive_file");
95
+ active_file_hash = simple_hash("active_file");
96
+ unevictable_hash = simple_hash("unevictable");
97
+ hierarchical_memory_limit_hash = simple_hash("hierarchical_memory_limit");
98
+ total_cache_hash = simple_hash("total_cache");
99
+ total_rss_hash = simple_hash("total_rss");
100
+ total_rss_huge_hash = simple_hash("total_rss_huge");
101
+ total_mapped_file_hash = simple_hash("total_mapped_file");
102
+ total_writeback_hash = simple_hash("total_writeback");
103
+ total_dirty_hash = simple_hash("total_dirty");
104
+ total_swap_hash = simple_hash("total_swap");
105
+ total_pgpgin_hash = simple_hash("total_pgpgin");
106
+ total_pgpgout_hash = simple_hash("total_pgpgout");
107
+ total_pgfault_hash = simple_hash("total_pgfault");
108
+ total_pgmajfault_hash = simple_hash("total_pgmajfault");
109
+ total_inactive_anon_hash = simple_hash("total_inactive_anon");
110
+ total_active_anon_hash = simple_hash("total_active_anon");
111
+ total_inactive_file_hash = simple_hash("total_inactive_file");
112
+ total_active_file_hash = simple_hash("total_active_file");
113
+ total_unevictable_hash = simple_hash("total_unevictable");
114
+
115
cgroup_update_every = (int)config_get_number("plugin:cgroups", "update every", rrd_update_every);
116
if(cgroup_update_every < rrd_update_every)
117
cgroup_update_every = rrd_update_every;
@@ -314,17 +396,9 @@ struct cgroup {
396
// ----------------------------------------------------------------------------
397
// read values from /sys
398
317
-void cgroup_read_cpuacct_stat(struct cpuacct_stat *cp) {
399
+static inline void cgroup_read_cpuacct_stat(struct cpuacct_stat *cp) {
400
static procfile *ff = NULL;
401
320
- static uint32_t user_hash = 0;
321
- static uint32_t system_hash = 0;
322
-
323
- if(unlikely(user_hash == 0)) {
324
- user_hash = simple_hash("user");
325
- system_hash = simple_hash("system");
326
- }
327
-
402
cp->updated = 0;
403
if(cp->filename) {
404
ff = procfile_reopen(ff, cp->filename, NULL, PROCFILE_FLAG_DEFAULT);
@@ -357,7 +431,7 @@ void cgroup_read_cpuacct_stat(struct cpuacct_stat *cp) {
431
}
432
}
433
360
-void cgroup_read_cpuacct_usage(struct cpuacct_usage *ca) {
434
+static inline void cgroup_read_cpuacct_usage(struct cpuacct_usage *ca) {
435
static procfile *ff = NULL;
436
437
ca->updated = 0;
@@ -398,27 +472,9 @@ void cgroup_read_cpuacct_usage(struct cpuacct_usage *ca) {
472
}
473
}
474
401
-void cgroup_read_blkio(struct blkio *io) {
475
+static inline void cgroup_read_blkio(struct blkio *io) {
476
static procfile *ff = NULL;
477
404
- static uint32_t Read_hash = 0;
405
- static uint32_t Write_hash = 0;
406
-/*
407
- static uint32_t Sync_hash = 0;
408
- static uint32_t Async_hash = 0;
409
- static uint32_t Total_hash = 0;
410
-*/
411
-
412
- if(unlikely(Read_hash == 0)) {
413
- Read_hash = simple_hash("Read");
414
- Write_hash = simple_hash("Write");
415
-/*
416
- Sync_hash = simple_hash("Sync");
417
- Async_hash = simple_hash("Async");
418
- Total_hash = simple_hash("Total");
419
-*/
420
- }
421
-
478
io->updated = 0;
479
if(io->filename) {
480
ff = procfile_reopen(ff, io->filename, NULL, PROCFILE_FLAG_DEFAULT);
@@ -469,82 +525,9 @@ void cgroup_read_blkio(struct blkio *io) {
525
}
526
}
527
472
-void cgroup_read_memory(struct memory *mem) {
528
+static inline void cgroup_read_memory(struct memory *mem) {
529
static procfile *ff = NULL;
530
475
- static uint32_t cache_hash = 0;
476
- static uint32_t rss_hash = 0;
477
- static uint32_t rss_huge_hash = 0;
478
- static uint32_t mapped_file_hash = 0;
479
- static uint32_t writeback_hash = 0;
480
- static uint32_t dirty_hash = 0;
481
- static uint32_t swap_hash = 0;
482
- static uint32_t pgpgin_hash = 0;
483
- static uint32_t pgpgout_hash = 0;
484
- static uint32_t pgfault_hash = 0;
485
- static uint32_t pgmajfault_hash = 0;
486
-/*
487
- static uint32_t inactive_anon_hash = 0;
488
- static uint32_t active_anon_hash = 0;
489
- static uint32_t inactive_file_hash = 0;
490
- static uint32_t active_file_hash = 0;
491
- static uint32_t unevictable_hash = 0;
492
- static uint32_t hierarchical_memory_limit_hash = 0;
493
- static uint32_t total_cache_hash = 0;
494
- static uint32_t total_rss_hash = 0;
495
- static uint32_t total_rss_huge_hash = 0;
496
- static uint32_t total_mapped_file_hash = 0;
497
- static uint32_t total_writeback_hash = 0;
498
- static uint32_t total_dirty_hash = 0;
499
- static uint32_t total_swap_hash = 0;
500
- static uint32_t total_pgpgin_hash = 0;
501
- static uint32_t total_pgpgout_hash = 0;
502
- static uint32_t total_pgfault_hash = 0;
503
- static uint32_t total_pgmajfault_hash = 0;
504
- static uint32_t total_inactive_anon_hash = 0;
505
- static uint32_t total_active_anon_hash = 0;
506
- static uint32_t total_inactive_file_hash = 0;
507
- static uint32_t total_active_file_hash = 0;
508
- static uint32_t total_unevictable_hash = 0;
509
-*/
510
- if(unlikely(cache_hash == 0)) {
511
- cache_hash = simple_hash("cache");
512
- rss_hash = simple_hash("rss");
513
- rss_huge_hash = simple_hash("rss_huge");
514
- mapped_file_hash = simple_hash("mapped_file");
515
- writeback_hash = simple_hash("writeback");
516
- dirty_hash = simple_hash("dirty");
517
- swap_hash = simple_hash("swap");
518
- pgpgin_hash = simple_hash("pgpgin");
519
- pgpgout_hash = simple_hash("pgpgout");
520
- pgfault_hash = simple_hash("pgfault");
521
- pgmajfault_hash = simple_hash("pgmajfault");
522
-/*
523
- inactive_anon_hash = simple_hash("inactive_anon");
524
- active_anon_hash = simple_hash("active_anon");
525
- inactive_file_hash = simple_hash("inactive_file");
526
- active_file_hash = simple_hash("active_file");
527
- unevictable_hash = simple_hash("unevictable");
528
- hierarchical_memory_limit_hash = simple_hash("hierarchical_memory_limit");
529
- total_cache_hash = simple_hash("total_cache");
530
- total_rss_hash = simple_hash("total_rss");
531
- total_rss_huge_hash = simple_hash("total_rss_huge");
532
- total_mapped_file_hash = simple_hash("total_mapped_file");
533
- total_writeback_hash = simple_hash("total_writeback");
534
- total_dirty_hash = simple_hash("total_dirty");
535
- total_swap_hash = simple_hash("total_swap");
536
- total_pgpgin_hash = simple_hash("total_pgpgin");
537
- total_pgpgout_hash = simple_hash("total_pgpgout");
538
- total_pgfault_hash = simple_hash("total_pgfault");
539
- total_pgmajfault_hash = simple_hash("total_pgmajfault");
540
- total_inactive_anon_hash = simple_hash("total_inactive_anon");
541
- total_active_anon_hash = simple_hash("total_active_anon");
542
- total_inactive_file_hash = simple_hash("total_inactive_file");
543
- total_active_file_hash = simple_hash("total_active_file");
544
- total_unevictable_hash = simple_hash("total_unevictable");
545
-*/
546
- }
547
-
531
mem->updated = 0;
532
if(mem->filename) {
533
ff = procfile_reopen(ff, mem->filename, NULL, PROCFILE_FLAG_DEFAULT);
@@ -694,7 +677,7 @@ void cgroup_read_memory(struct memory *mem) {
677
}
678
}
679
697
-void cgroup_read(struct cgroup *cg) {
680
+static inline void cgroup_read(struct cgroup *cg) {
681
debug(D_CGROUP, "reading metrics for cgroups '%s'", cg->id);
682
683
cgroup_read_cpuacct_stat(&cg->cpuacct_stat);
@@ -708,7 +691,7 @@ void cgroup_read(struct cgroup *cg) {
691
cgroup_read_blkio(&cg->io_queued);
692
}
693
711
-void read_all_cgroups(struct cgroup *root) {
694
+static inline void read_all_cgroups(struct cgroup *root) {
695
debug(D_CGROUP, "reading metrics for all cgroups");
696
697
struct cgroup *cg;
@@ -723,7 +706,7 @@ void read_all_cgroups(struct cgroup *root) {
706
707
#define CGROUP_CHARTID_LINE_MAX 1024
708
726
-static char *cgroup_title_strdupz(const char *s) {
709
+static inline char *cgroup_title_strdupz(const char *s) {
710
if(!s || !*s) s = "/";
711
712
if(*s == '/' && s[1] != '\0') s++;
@@ -734,7 +717,7 @@ static char *cgroup_title_strdupz(const char *s) {
717
return r;
718
}
719
737
-static char *cgroup_chart_id_strdupz(const char *s) {
720
+static inline char *cgroup_chart_id_strdupz(const char *s) {
721
if(!s || !*s) s = "/";
722
723
if(*s == '/' && s[1] != '\0') s++;
@@ -745,7 +728,7 @@ static char *cgroup_chart_id_strdupz(const char *s) {
728
return r;
729
}
730
748
-void cgroup_get_chart_name(struct cgroup *cg) {
731
+static inline void cgroup_get_chart_name(struct cgroup *cg) {
732
debug(D_CGROUP, "looking for the name of cgroup '%s' with chart id '%s' and title '%s'", cg->id, cg->chart_id, cg->chart_title);
733
734
pid_t cgroup_pid;
@@ -779,7 +762,7 @@ void cgroup_get_chart_name(struct cgroup *cg) {
762
error("CGROUP: Cannot popen(\"%s\", \"r\").", buffer);
763
}
764
782
-struct cgroup *cgroup_add(const char *id) {
765
+static inline struct cgroup *cgroup_add(const char *id) {
766
if(!id || !*id) id = "/";
767
debug(D_CGROUP, "adding to list, cgroup with id '%s'", id);
768
@@ -900,7 +883,7 @@ struct cgroup *cgroup_add(const char *id) {
883
return cg;
884
}
885
903
-void cgroup_free(struct cgroup *cg) {
886
+static inline void cgroup_free(struct cgroup *cg) {
887
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");
888
889
freez(cg->cpuacct_usage.cpu_percpu);
@@ -932,7 +915,7 @@ void cgroup_free(struct cgroup *cg) {
915
}
916
917
// find if a given cgroup exists
935
-struct cgroup *cgroup_find(const char *id) {
918
+static inline struct cgroup *cgroup_find(const char *id) {
919
debug(D_CGROUP, "searching for cgroup '%s'", id);
920
921
uint32_t hash = simple_hash(id);
@@ -951,7 +934,7 @@ struct cgroup *cgroup_find(const char *id) {
934
// detect running cgroups
935
936
// callback for find_file_in_subdirs()
954
-void found_subdir_in_dir(const char *dir) {
937
+static inline void found_subdir_in_dir(const char *dir) {
938
debug(D_CGROUP, "examining cgroup dir '%s'", dir);
939
940
struct cgroup *cg = cgroup_find(dir);
@@ -976,7 +959,7 @@ void found_subdir_in_dir(const char *dir) {
959
if(cg) cg->available = 1;
960
}
961
979
-int find_dir_in_subdirs(const char *base, const char *this, void (*callback)(const char *)) {
962
+static inline int find_dir_in_subdirs(const char *base, const char *this, void (*callback)(const char *)) {
963
if(!this) this = base;
964
debug(D_CGROUP, "searching for directories in '%s' (base '%s')", this?this:"", base);
965
@@ -1041,7 +1024,7 @@ int find_dir_in_subdirs(const char *base, const char *this, void (*callback)(con
1024
return ret;
1025
}
1026
1044
-void mark_all_cgroups_as_not_available() {
1027
+static inline void mark_all_cgroups_as_not_available() {
1028
debug(D_CGROUP, "marking all cgroups as not available");
1029
1030
struct cgroup *cg;
@@ -1052,7 +1035,7 @@ void mark_all_cgroups_as_not_available() {
1035
}
1036
}
1037
1055
-void cleanup_all_cgroups() {
1038
+static inline void cleanup_all_cgroups() {
1039
struct cgroup *cg = cgroup_root, *last = NULL;
1040
1041
for(; cg ;) {
@@ -1089,7 +1072,7 @@ void cleanup_all_cgroups() {
1072
}
1073
}
1074
1092
-void find_all_cgroups() {
1075
+static inline void find_all_cgroups() {
1076
debug(D_CGROUP, "searching for cgroups");
1077
1078
mark_all_cgroups_as_not_available();
@@ -1147,7 +1130,7 @@ void find_all_cgroups() {
1130
else debug(D_CGROUP, "cpuacct.stat file for cgroup '%s': '%s' does not exist.", cg->id, filename);
1131
}
1132
1150
- if(unlikely(cgroup_enable_cpuacct_usage && !cg->cpuacct_usage.filename)) {
1133
+ if(unlikely(cgroup_enable_cpuacct_usage && !cg->cpuacct_usage.filename && !(cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE))) {
1134
snprintfz(filename, FILENAME_MAX, "%s%s/cpuacct.usage_percpu", cgroup_cpuacct_base, cg->id);
1135
if(stat(filename, &buf) != -1) {
1136
cg->cpuacct_usage.filename = strdupz(filename);
@@ -1157,7 +1140,7 @@ void find_all_cgroups() {
1140
}
1141
1142
if(unlikely(cgroup_enable_memory)) {
1160
- if(unlikely(!cg->memory.filename)) {
1143
+ if(unlikely(!cg->memory.filename) && !(cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE)) {
1144
snprintfz(filename, FILENAME_MAX, "%s%s/memory.stat", cgroup_memory_base, cg->id);
1145
if(stat(filename, &buf) != -1) {
1146
cg->memory.filename = strdupz(filename);
@@ -1386,7 +1369,7 @@ void update_services_charts(int update_every,
1369
rrddim_set_by_pointer(st_cpu, cg->rd_cpu, cg->cpuacct_stat.user + cg->cpuacct_stat.system);
1370
}
1371
1389
- if(do_mem_usage && cg->memory.updated) {
1372
+ if(do_mem_usage && cg->memory.usage_in_bytes_updated) {
1373
if(unlikely(!cg->rd_mem_usage))
1374
cg->rd_mem_usage = rrddim_add(st_mem_usage, cg->chart_id, cg->chart_title, 1, 1024 * 1024, RRDDIM_ABSOLUTE);
1375