10 #include "appconfig.h"
13 #include "plugin_proc.h"
16 #define MAX_INTERRUPTS 256
17 #define MAX_INTERRUPT_CPUS 256
18 #define MAX_INTERRUPT_NAME 50
23 char name[MAX_INTERRUPT_NAME + 1];
24 unsigned long long value[MAX_INTERRUPT_CPUS];
25 unsigned long long total;
28 static struct interrupt *alloc_interrupts(int lines) {
29 static struct interrupt *irrs = NULL;
30 static int alloced = 0;
32 if(lines < alloced) return irrs;
34 irrs = (struct interrupt *)realloc(irrs, lines * sizeof(struct interrupt));
36 fatal("Cannot allocate memory for %d interrupts", lines);
44 int do_proc_softirqs(int update_every, unsigned long long dt) {
45 static procfile *ff = NULL;
46 static int cpus = -1, do_per_core = -1;
48 struct interrupt *irrs = NULL;
52 if(do_per_core == -1) do_per_core = config_get_boolean("plugin:proc:/proc/softirqs", "interrupts per core", 1);
55 char filename[FILENAME_MAX + 1];
56 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/softirqs");
57 ff = procfile_open(config_get("plugin:proc:/proc/softirqs", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
61 ff = procfile_readall(ff);
62 if(!ff) return 0; // we return 0, so that we will retry to open it next time
64 uint32_t lines = procfile_lines(ff), l;
65 uint32_t words = procfile_linewords(ff, 0), w;
68 error("Cannot read /proc/softirqs, zero lines reported.");
72 // find how many CPUs are there
75 for(w = 0; w < words ; w++) {
76 if(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0)
80 if(cpus > MAX_INTERRUPT_CPUS) cpus = MAX_INTERRUPT_CPUS;
84 error("PLUGIN: PROC_SOFTIRQS: Cannot find the number of CPUs in /proc/softirqs");
88 // allocate the size we need;
89 irrs = alloc_interrupts(lines);
92 // loop through all lines
93 for(l = 1; l < lines ;l++) {
94 struct interrupt *irr = &irrs[l];
98 words = procfile_linewords(ff, l);
101 irr->id = procfile_lineword(ff, l, 0);
102 if(!irr->id || !irr->id[0]) continue;
104 int idlen = strlen(irr->id);
105 if(irr->id[idlen - 1] == ':')
106 irr->id[idlen - 1] = '\0';
109 for(c = 0; c < cpus ;c++) {
110 if((c + 1) < (int)words)
111 irr->value[c] = strtoull(procfile_lineword(ff, l, (uint32_t)(c + 1)), NULL, 10);
115 irr->total += irr->value[c];
118 strncpyz(irr->name, irr->id, MAX_INTERRUPT_NAME);
125 // --------------------------------------------------------------------
127 st = rrdset_find_bytype("system", "softirqs");
129 st = rrdset_create("system", "softirqs", NULL, "softirqs", NULL, "System softirqs", "softirqs/s", 950, update_every, RRDSET_TYPE_STACKED);
131 for(l = 0; l < lines ;l++) {
132 if(!irrs[l].used) continue;
133 rrddim_add(st, irrs[l].id, irrs[l].name, 1, 1, RRDDIM_INCREMENTAL);
136 else rrdset_next(st);
138 for(l = 0; l < lines ;l++) {
139 if(!irrs[l].used) continue;
140 rrddim_set(st, irrs[l].id, irrs[l].total);
147 for(c = 0; c < cpus ; c++) {
149 snprintfz(id, 256, "cpu%d_softirqs", c);
151 st = rrdset_find_bytype("cpu", id);
153 // find if everything is zero
154 unsigned long long core_sum = 0 ;
155 for(l = 0; l < lines ;l++) {
156 if(!irrs[l].used) continue;
157 core_sum += irrs[l].value[c];
159 if(core_sum == 0) continue; // try next core
161 char name[256+1], title[256+1];
162 snprintfz(name, 256, "cpu%d_softirqs", c);
163 snprintfz(title, 256, "CPU%d softirqs", c);
164 st = rrdset_create("cpu", id, name, "softirqs", "cpu.softirqs", title, "softirqs/s", 3000 + c, update_every, RRDSET_TYPE_STACKED);
166 for(l = 0; l < lines ;l++) {
167 if(!irrs[l].used) continue;
168 rrddim_add(st, irrs[l].id, irrs[l].name, 1, 1, RRDDIM_INCREMENTAL);
171 else rrdset_next(st);
173 for(l = 0; l < lines ;l++) {
174 if(!irrs[l].used) continue;
175 rrddim_set(st, irrs[l].id, irrs[l].value[c]);