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);
35 char filename[FILENAME_MAX + 1];
36 snprintf(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/interrupts");
37 ff = procfile_open(config_get("plugin:proc:/proc/interrupts", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
41 ff = procfile_readall(ff);
42 if(!ff) return 0; // we return 0, so that we will retry to open it next time
44 uint32_t lines = procfile_lines(ff), l;
45 uint32_t words = procfile_linewords(ff, 0), w;
47 // find how many CPUs are there
50 for(w = 0; w < words ; w++) {
51 if(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0)
55 if(cpus > MAX_INTERRUPT_CPUS) cpus = MAX_INTERRUPT_CPUS;
59 error("PLUGIN: PROC_INTERRUPTS: Cannot find the number of CPUs in /proc/interrupts");
63 // allocate the size we need;
64 struct interrupt irrs[lines];
67 // loop through all lines
68 for(l = 1; l < lines ;l++) {
69 struct interrupt *irr = &irrs[l];
73 words = procfile_linewords(ff, l);
76 irr->id = procfile_lineword(ff, l, 0);
77 if(!irr->id || !irr->id[0]) continue;
79 if(irr->id[strlen(irr->id) - 1] == ':')
80 irr->id[strlen(irr->id) - 1] = '\0';
83 for(c = 0; c < cpus ;c++) {
84 if((c + 1) < (int)words)
85 irr->value[c] = strtoull(procfile_lineword(ff, l, (uint32_t)(c + 1)), NULL, 10);
89 irr->total += irr->value[c];
92 if(isdigit(irr->id[0]) && (uint32_t)(cpus + 2) < words) {
93 irr->name = procfile_lineword(ff, l, words - 1);
103 // --------------------------------------------------------------------
105 st = rrdset_find_bytype("system", "interrupts");
107 st = rrdset_create("system", "interrupts", NULL, "system", "System interrupts", "interrupts/s", 1000, update_every, RRDSET_TYPE_STACKED);
109 for(l = 0; l < lines ;l++) {
110 if(!irrs[l].used) continue;
111 rrddim_add(st, irrs[l].id, irrs[l].name, 1, update_every, RRDDIM_INCREMENTAL);
114 else rrdset_next(st);
116 for(l = 0; l < lines ;l++) {
117 if(!irrs[l].used) continue;
118 rrddim_set(st, irrs[l].id, irrs[l].total);
125 for(c = 0; c < cpus ; c++) {
127 snprintf(id, 256, "cpu%d_interrupts", c);
129 st = rrdset_find_bytype("cpu", id);
131 char name[256], title[256];
132 snprintf(name, 256, "cpu%d_interrupts", c);
133 snprintf(title, 256, "CPU%d Interrupts", c);
134 st = rrdset_create("cpu", id, name, "cpu", title, "interrupts/s", 2000 + c, update_every, RRDSET_TYPE_STACKED);
136 for(l = 0; l < lines ;l++) {
137 if(!irrs[l].used) continue;
138 rrddim_add(st, irrs[l].id, irrs[l].name, 1, update_every, RRDDIM_INCREMENTAL);
141 else rrdset_next(st);
143 for(l = 0; l < lines ;l++) {
144 if(!irrs[l].used) continue;
145 rrddim_set(st, irrs[l].id, irrs[l].value[c]);