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 snprintfz(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 strncpyz(irr->name, procfile_lineword(ff, l, words - 1), MAX_INTERRUPT_NAME);
122 int nlen = strlen(irr->name);
123 if(nlen < (MAX_INTERRUPT_NAME-1)) {
124 irr->name[nlen] = '_';
125 strncpyz(&irr->name[nlen + 1], irr->id, MAX_INTERRUPT_NAME - nlen);
129 strncpyz(irr->name, irr->id, MAX_INTERRUPT_NAME);
137 // --------------------------------------------------------------------
139 st = rrdset_find_bytype("system", "interrupts");
141 st = rrdset_create("system", "interrupts", NULL, "interrupts", NULL, "System interrupts", "interrupts/s", 1000, update_every, RRDSET_TYPE_STACKED);
143 for(l = 0; l < lines ;l++) {
144 struct interrupt *irr = irrindex(irrs, l, cpus);
145 if(!irr->used) continue;
146 rrddim_add(st, irr->id, irr->name, 1, 1, RRDDIM_INCREMENTAL);
149 else rrdset_next(st);
151 for(l = 0; l < lines ;l++) {
152 struct interrupt *irr = irrindex(irrs, l, cpus);
153 if(!irr->used) continue;
154 rrddim_set(st, irr->id, irr->total);
161 for(c = 0; c < cpus ; c++) {
163 snprintfz(id, 256, "cpu%d_interrupts", c);
165 st = rrdset_find_bytype("cpu", id);
167 char name[256+1], title[256+1];
168 snprintfz(name, 256, "cpu%d_interrupts", c);
169 snprintfz(title, 256, "CPU%d Interrupts", c);
170 st = rrdset_create("cpu", id, name, "interrupts", "cpu.interrupts", title, "interrupts/s", 2000 + 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]);