10 #include "appconfig.h"
13 #include "plugin_proc.h"
16 #define MAX_INTERRUPT_NAME 50
21 char name[MAX_INTERRUPT_NAME + 1];
22 unsigned long long total;
23 unsigned long long value[];
26 // since each interrupt is variable in size
27 // we use this to calculate its record size
28 #define recordsize(cpus) (sizeof(struct interrupt) + (cpus * sizeof(unsigned long long)))
30 // given a base, get a pointer to each record
31 #define irrindex(base, line, cpus) ((struct interrupt *)&((char *)(base))[line * recordsize(cpus)])
33 static inline struct interrupt *get_interrupts_array(int lines, int cpus) {
34 static struct interrupt *irrs = NULL;
35 static int allocated = 0;
37 if(lines < allocated) return irrs;
39 irrs = (struct interrupt *)realloc(irrs, lines * recordsize(cpus));
41 fatal("Cannot allocate memory for %d interrupts", lines);
49 int do_proc_softirqs(int update_every, unsigned long long dt) {
50 static procfile *ff = NULL;
51 static int cpus = -1, do_per_core = -1;
53 struct interrupt *irrs = NULL;
57 if(do_per_core == -1) do_per_core = config_get_boolean("plugin:proc:/proc/softirqs", "interrupts per core", 1);
60 char filename[FILENAME_MAX + 1];
61 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/softirqs");
62 ff = procfile_open(config_get("plugin:proc:/proc/softirqs", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
66 ff = procfile_readall(ff);
67 if(!ff) return 0; // we return 0, so that we will retry to open it next time
69 uint32_t lines = procfile_lines(ff), l;
70 uint32_t words = procfile_linewords(ff, 0), w;
73 error("Cannot read /proc/softirqs, zero lines reported.");
77 // find how many CPUs are there
80 for(w = 0; w < words ; w++) {
81 if(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0)
87 error("PLUGIN: PROC_SOFTIRQS: Cannot find the number of CPUs in /proc/softirqs");
91 // allocate the size we need;
92 irrs = get_interrupts_array(lines, cpus);
95 // loop through all lines
96 for(l = 1; l < lines ;l++) {
97 struct interrupt *irr = irrindex(irrs, l, cpus);
101 words = procfile_linewords(ff, l);
104 irr->id = procfile_lineword(ff, l, 0);
105 if(!irr->id || !irr->id[0]) continue;
107 int idlen = strlen(irr->id);
108 if(irr->id[idlen - 1] == ':')
109 irr->id[idlen - 1] = '\0';
112 for(c = 0; c < cpus ;c++) {
113 if((c + 1) < (int)words)
114 irr->value[c] = strtoull(procfile_lineword(ff, l, (uint32_t)(c + 1)), NULL, 10);
118 irr->total += irr->value[c];
121 strncpyz(irr->name, irr->id, MAX_INTERRUPT_NAME);
128 // --------------------------------------------------------------------
130 st = rrdset_find_bytype("system", "softirqs");
132 st = rrdset_create("system", "softirqs", NULL, "softirqs", NULL, "System softirqs", "softirqs/s", 950, update_every, RRDSET_TYPE_STACKED);
134 for(l = 0; l < lines ;l++) {
135 struct interrupt *irr = irrindex(irrs, l, cpus);
136 if(!irr->used) continue;
137 rrddim_add(st, irr->id, irr->name, 1, 1, RRDDIM_INCREMENTAL);
140 else rrdset_next(st);
142 for(l = 0; l < lines ;l++) {
143 struct interrupt *irr = irrindex(irrs, l, cpus);
144 if(!irr->used) continue;
145 rrddim_set(st, irr->id, irr->total);
152 for(c = 0; c < cpus ; c++) {
154 snprintfz(id, 256, "cpu%d_softirqs", c);
156 st = rrdset_find_bytype("cpu", id);
158 // find if everything is zero
159 unsigned long long core_sum = 0 ;
160 for(l = 0; l < lines ;l++) {
161 struct interrupt *irr = irrindex(irrs, l, cpus);
162 if(!irr->used) continue;
163 core_sum += irr->value[c];
165 if(core_sum == 0) continue; // try next core
167 char name[256+1], title[256+1];
168 snprintfz(name, 256, "cpu%d_softirqs", c);
169 snprintfz(title, 256, "CPU%d softirqs", c);
170 st = rrdset_create("cpu", id, name, "softirqs", "cpu.softirqs", title, "softirqs/s", 3000 + c, update_every, RRDSET_TYPE_STACKED);
172 for(l = 0; l < lines ;l++) {
173 struct interrupt *irr = irrindex(irrs, l, cpus);
174 if(!irr->used) continue;
175 rrddim_add(st, irr->id, irr->name, 1, 1, RRDDIM_INCREMENTAL);
178 else rrdset_next(st);
180 for(l = 0; l < lines ;l++) {
181 struct interrupt *irr = irrindex(irrs, l, cpus);
182 if(!irr->used) continue;
183 rrddim_set(st, irr->id, irr->value[c]);