10 #include "plugin_proc.h"
13 #define MAX_INTERRUPTS 256
14 #define MAX_INTERRUPT_CPUS 256
15 #define MAX_INTERRUPT_NAME 20
21 unsigned long long value[MAX_INTERRUPT_CPUS];
22 unsigned long long total;
25 int do_proc_interrupts(int update_every, unsigned long long dt) {
26 static procfile *ff = NULL;
27 static int cpus = -1, do_per_core = -1;
31 if(do_per_core == -1) do_per_core = config_get_boolean("plugin:proc:/proc/interrupts", "interrupts per core", 1);
33 if(!ff) ff = procfile_open("/proc/interrupts", " \t", PROCFILE_FLAG_DEFAULT);
36 ff = procfile_readall(ff);
37 if(!ff) return 0; // we return 0, so that we will retry to open it next time
39 uint32_t lines = procfile_lines(ff), l;
40 uint32_t words = procfile_linewords(ff, 0), w;
42 // find how many CPUs are there
45 for(w = 0; w < words ; w++) {
46 if(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0)
50 if(cpus > MAX_INTERRUPT_CPUS) cpus = MAX_INTERRUPT_CPUS;
54 error("PLUGIN: PROC_INTERRUPTS: Cannot find the number of CPUs in /proc/interrupts");
58 // allocate the size we need;
59 struct interrupt irrs[lines];
62 // loop through all lines
63 for(l = 1; l < lines ;l++) {
64 struct interrupt *irr = &irrs[l];
68 words = procfile_linewords(ff, l);
71 irr->id = procfile_lineword(ff, l, 0);
72 if(!irr->id || !irr->id[0]) continue;
74 if(irr->id[strlen(irr->id) - 1] == ':')
75 irr->id[strlen(irr->id) - 1] = '\0';
78 for(c = 0; c < cpus ;c++) {
79 if((c + 1) < (int)words)
80 irr->value[c] = strtoull(procfile_lineword(ff, l, (uint32_t)(c + 1)), NULL, 10);
84 irr->total += irr->value[c];
87 if(isdigit(irr->id[0]) && (uint32_t)(cpus + 2) < words) {
88 irr->name = procfile_lineword(ff, l, words - 1);
98 // --------------------------------------------------------------------
100 st = rrdset_find_bytype("system", "interrupts");
102 st = rrdset_create("system", "interrupts", NULL, "system", "System interrupts", "interrupts/s", 1000, update_every, RRDSET_TYPE_STACKED);
104 for(l = 0; l < lines ;l++) {
105 if(!irrs[l].used) continue;
106 rrddim_add(st, irrs[l].id, irrs[l].name, 1, update_every, RRDDIM_INCREMENTAL);
109 else rrdset_next(st);
111 for(l = 0; l < lines ;l++) {
112 if(!irrs[l].used) continue;
113 rrddim_set(st, irrs[l].id, irrs[l].total);
120 for(c = 0; c < cpus ; c++) {
122 snprintf(id, 256, "cpu%d_interrupts", c);
124 st = rrdset_find_bytype("cpu", id);
126 char name[256], title[256];
127 snprintf(name, 256, "cpu%d_interrupts", c);
128 snprintf(title, 256, "CPU%d Interrupts", c);
129 st = rrdset_create("cpu", id, name, "cpu", title, "interrupts/s", 2000 + c, 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, update_every, 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].value[c]);