11 #include "plugin_proc.h"
14 #define MAX_INTERRUPTS 256
15 #define MAX_INTERRUPT_CPUS 256
16 #define MAX_INTERRUPT_NAME 20
22 unsigned long long value[MAX_INTERRUPT_CPUS];
23 unsigned long long total;
26 int do_proc_interrupts(int update_every, unsigned long long dt) {
27 static procfile *ff = NULL;
28 static int cpus = -1, do_per_core = -1;
32 if(do_per_core == -1) do_per_core = config_get_boolean("plugin:proc:/proc/interrupts", "interrupts per core", 0);
34 if(!ff) ff = procfile_open("/proc/interrupts", " \t", PROCFILE_FLAG_DEFAULT);
37 ff = procfile_readall(ff);
38 if(!ff) return 0; // we return 0, so that we will retry to open it next time
40 uint32_t lines = procfile_lines(ff), l;
41 uint32_t words = procfile_linewords(ff, 0), w;
43 // find how many CPUs are there
46 for(w = 0; w < words ; w++) {
47 if(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0)
51 if(cpus > MAX_INTERRUPT_CPUS) cpus = MAX_INTERRUPT_CPUS;
55 error("PLUGIN: PROC_INTERRUPTS: Cannot find the number of CPUs in /proc/interrupts");
59 // allocate the size we need;
60 struct interrupt irrs[lines];
63 // loop through all lines
64 for(l = 1; l < lines ;l++) {
65 struct interrupt *irr = &irrs[l];
69 words = procfile_linewords(ff, l);
72 irr->id = procfile_lineword(ff, l, 0);
73 if(!irr->id || !irr->id[0]) continue;
75 if(irr->id[strlen(irr->id) - 1] == ':')
76 irr->id[strlen(irr->id) - 1] = '\0';
79 for(c = 0; c < cpus ;c++) {
80 if((c + 1) < (int)words)
81 irr->value[c] = strtoull(procfile_lineword(ff, l, (uint32_t)(c + 1)), NULL, 10);
85 irr->total += irr->value[c];
88 if(isdigit(irr->id[0]) && (uint32_t)(cpus + 2) < words) {
89 irr->name = procfile_lineword(ff, l, words - 1);
99 // --------------------------------------------------------------------
101 st = rrdset_find_bytype("system", "interrupts");
103 st = rrdset_create("system", "interrupts", NULL, "system", "System interrupts", "interrupts/s", 1000, update_every, RRDSET_TYPE_STACKED);
105 for(l = 0; l < lines ;l++) {
106 if(!irrs[l].used) continue;
107 rrddim_add(st, irrs[l].id, irrs[l].name, 1, update_every, RRDDIM_INCREMENTAL);
110 else rrdset_next(st);
112 for(l = 0; l < lines ;l++) {
113 if(!irrs[l].used) continue;
114 rrddim_set(st, irrs[l].id, irrs[l].total);
121 for(c = 0; c < cpus ; c++) {
123 snprintf(id, 256, "cpu%d_interrupts", c);
125 st = rrdset_find_bytype("cpu", id);
127 char name[256], title[256];
128 snprintf(name, 256, "cpu%d_interrupts", c);
129 snprintf(title, 256, "CPU%d Interrupts", c);
130 st = rrdset_create("cpu", id, name, "cpu", title, "interrupts/s", 2000 + c, update_every, RRDSET_TYPE_STACKED);
132 for(l = 0; l < lines ;l++) {
133 if(!irrs[l].used) continue;
134 rrddim_add(st, irrs[l].id, irrs[l].name, 1, update_every, RRDDIM_INCREMENTAL);
137 else rrdset_next(st);
139 for(l = 0; l < lines ;l++) {
140 if(!irrs[l].used) continue;
141 rrddim_set(st, irrs[l].id, irrs[l].value[c]);