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 snprintf(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 strncpy(irr->name, irr->id, MAX_INTERRUPT_NAME);
122 irr->name[MAX_INTERRUPT_NAME] = '\0';
129 // --------------------------------------------------------------------
131 st = rrdset_find_bytype("system", "softirqs");
133 st = rrdset_create("system", "softirqs", NULL, "softirqs", NULL, "System softirqs", "softirqs/s", 950, update_every, RRDSET_TYPE_STACKED);
135 for(l = 0; l < lines ;l++) {
136 struct interrupt *irr = irrindex(irrs, l, cpus);
137 if(!irr->used) continue;
138 rrddim_add(st, irr->id, irr->name, 1, 1, RRDDIM_INCREMENTAL);
141 else rrdset_next(st);
143 for(l = 0; l < lines ;l++) {
144 struct interrupt *irr = irrindex(irrs, l, cpus);
145 if(!irr->used) continue;
146 rrddim_set(st, irr->id, irr->total);
153 for(c = 0; c < cpus ; c++) {
155 snprintf(id, 256, "cpu%d_softirqs", c);
157 st = rrdset_find_bytype("cpu", id);
159 // find if everything is zero
160 unsigned long long core_sum = 0 ;
161 for(l = 0; l < lines ;l++) {
162 struct interrupt *irr = irrindex(irrs, l, cpus);
163 if(!irr->used) continue;
164 core_sum += irr->value[c];
166 if(core_sum == 0) continue; // try next core
168 char name[256], title[256];
169 snprintf(name, 256, "cpu%d_softirqs", c);
170 snprintf(title, 256, "CPU%d softirqs", c);
171 st = rrdset_create("cpu", id, name, "softirqs", "cpu.softirqs", title, "softirqs/s", 3000 + c, update_every, RRDSET_TYPE_STACKED);
173 for(l = 0; l < lines ;l++) {
174 struct interrupt *irr = irrindex(irrs, l, cpus);
175 if(!irr->used) continue;
176 rrddim_add(st, irr->id, irr->name, 1, 1, RRDDIM_INCREMENTAL);
179 else rrdset_next(st);
181 for(l = 0; l < lines ;l++) {
182 struct interrupt *irr = irrindex(irrs, l, cpus);
183 if(!irr->used) continue;
184 rrddim_set(st, irr->id, irr->value[c]);