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_interrupts(int update_every, unsigned long long dt) {
50 static procfile *ff = NULL;
51 static int cpus = -1, do_per_core = -1;
52 struct interrupt *irrs = NULL;
56 if(do_per_core == -1) do_per_core = config_get_boolean("plugin:proc:/proc/interrupts", "interrupts per core", 1);
59 char filename[FILENAME_MAX + 1];
60 snprintf(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/interrupts");
61 ff = procfile_open(config_get("plugin:proc:/proc/interrupts", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
65 ff = procfile_readall(ff);
66 if(!ff) return 0; // we return 0, so that we will retry to open it next time
68 uint32_t lines = procfile_lines(ff), l;
69 uint32_t words = procfile_linewords(ff, 0), w;
72 error("Cannot read /proc/interrupts, zero lines reported.");
76 // find how many CPUs are there
79 for(w = 0; w < words ; w++) {
80 if(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0)
86 error("PLUGIN: PROC_INTERRUPTS: Cannot find the number of CPUs in /proc/interrupts");
90 // allocate the size we need;
91 irrs = get_interrupts_array(lines, cpus);
94 // loop through all lines
95 for(l = 1; l < lines ;l++) {
96 struct interrupt *irr = irrindex(irrs, l, cpus);
100 words = procfile_linewords(ff, l);
103 irr->id = procfile_lineword(ff, l, 0);
104 if(!irr->id || !irr->id[0]) continue;
106 int idlen = strlen(irr->id);
107 if(irr->id[idlen - 1] == ':')
108 irr->id[idlen - 1] = '\0';
111 for(c = 0; c < cpus ;c++) {
112 if((c + 1) < (int)words)
113 irr->value[c] = strtoull(procfile_lineword(ff, l, (uint32_t)(c + 1)), NULL, 10);
117 irr->total += irr->value[c];
120 if(isdigit(irr->id[0]) && (uint32_t)(cpus + 2) < words) {
121 strncpy(irr->name, procfile_lineword(ff, l, words - 1), MAX_INTERRUPT_NAME);
122 irr->name[MAX_INTERRUPT_NAME] = '\0';
123 int nlen = strlen(irr->name);
124 if(nlen < (MAX_INTERRUPT_NAME-1)) {
125 irr->name[nlen] = '_';
126 strncpy(&irr->name[nlen + 1], irr->id, MAX_INTERRUPT_NAME - nlen);
127 irr->name[MAX_INTERRUPT_NAME] = '\0';
131 strncpy(irr->name, irr->id, MAX_INTERRUPT_NAME);
132 irr->name[MAX_INTERRUPT_NAME] = '\0';
140 // --------------------------------------------------------------------
142 st = rrdset_find_bytype("system", "interrupts");
144 st = rrdset_create("system", "interrupts", NULL, "interrupts", NULL, "System interrupts", "interrupts/s", 1000, update_every, RRDSET_TYPE_STACKED);
146 for(l = 0; l < lines ;l++) {
147 struct interrupt *irr = irrindex(irrs, l, cpus);
148 if(!irr->used) continue;
149 rrddim_add(st, irr->id, irr->name, 1, 1, RRDDIM_INCREMENTAL);
152 else rrdset_next(st);
154 for(l = 0; l < lines ;l++) {
155 struct interrupt *irr = irrindex(irrs, l, cpus);
156 if(!irr->used) continue;
157 rrddim_set(st, irr->id, irr->total);
164 for(c = 0; c < cpus ; c++) {
166 snprintf(id, 256, "cpu%d_interrupts", c);
168 st = rrdset_find_bytype("cpu", id);
170 char name[256], title[256];
171 snprintf(name, 256, "cpu%d_interrupts", c);
172 snprintf(title, 256, "CPU%d Interrupts", c);
173 st = rrdset_create("cpu", id, name, "interrupts", "cpu.interrupts", title, "interrupts/s", 2000 + c, update_every, RRDSET_TYPE_STACKED);
175 for(l = 0; l < lines ;l++) {
176 struct interrupt *irr = irrindex(irrs, l, cpus);
177 if(!irr->used) continue;
178 rrddim_add(st, irr->id, irr->name, 1, 1, RRDDIM_INCREMENTAL);
181 else rrdset_next(st);
183 for(l = 0; l < lines ;l++) {
184 struct interrupt *irr = irrindex(irrs, l, cpus);
185 if(!irr->used) continue;
186 rrddim_set(st, irr->id, irr->value[c]);