3 #define MAX_INTERRUPT_NAME 50
8 char name[MAX_INTERRUPT_NAME + 1];
9 unsigned long long total;
10 unsigned long long value[];
13 // since each interrupt is variable in size
14 // we use this to calculate its record size
15 #define recordsize(cpus) (sizeof(struct interrupt) + (cpus * sizeof(unsigned long long)))
17 // given a base, get a pointer to each record
18 #define irrindex(base, line, cpus) ((struct interrupt *)&((char *)(base))[line * recordsize(cpus)])
20 static inline struct interrupt *get_interrupts_array(int lines, int cpus) {
21 static struct interrupt *irrs = NULL;
22 static int allocated = 0;
24 if(lines < allocated) return irrs;
26 irrs = (struct interrupt *)realloc(irrs, lines * recordsize(cpus));
28 fatal("Cannot allocate memory for %d interrupts", lines);
36 int do_proc_softirqs(int update_every, unsigned long long dt) {
37 static procfile *ff = NULL;
38 static int cpus = -1, do_per_core = -1;
40 struct interrupt *irrs = NULL;
44 if(do_per_core == -1) do_per_core = config_get_boolean("plugin:proc:/proc/softirqs", "interrupts per core", 1);
47 char filename[FILENAME_MAX + 1];
48 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/softirqs");
49 ff = procfile_open(config_get("plugin:proc:/proc/softirqs", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
53 ff = procfile_readall(ff);
54 if(!ff) return 0; // we return 0, so that we will retry to open it next time
56 uint32_t lines = procfile_lines(ff), l;
57 uint32_t words = procfile_linewords(ff, 0), w;
60 error("Cannot read /proc/softirqs, zero lines reported.");
64 // find how many CPUs are there
67 for(w = 0; w < words ; w++) {
68 if(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0)
74 error("PLUGIN: PROC_SOFTIRQS: Cannot find the number of CPUs in /proc/softirqs");
78 // allocate the size we need;
79 irrs = get_interrupts_array(lines, cpus);
82 // loop through all lines
83 for(l = 1; l < lines ;l++) {
84 struct interrupt *irr = irrindex(irrs, l, cpus);
88 words = procfile_linewords(ff, l);
91 irr->id = procfile_lineword(ff, l, 0);
92 if(!irr->id || !irr->id[0]) continue;
94 int idlen = strlen(irr->id);
95 if(irr->id[idlen - 1] == ':')
96 irr->id[idlen - 1] = '\0';
99 for(c = 0; c < cpus ;c++) {
100 if((c + 1) < (int)words)
101 irr->value[c] = strtoull(procfile_lineword(ff, l, (uint32_t)(c + 1)), NULL, 10);
105 irr->total += irr->value[c];
108 strncpyz(irr->name, irr->id, MAX_INTERRUPT_NAME);
115 // --------------------------------------------------------------------
117 st = rrdset_find_bytype("system", "softirqs");
119 st = rrdset_create("system", "softirqs", NULL, "softirqs", NULL, "System softirqs", "softirqs/s", 950, update_every, RRDSET_TYPE_STACKED);
121 for(l = 0; l < lines ;l++) {
122 struct interrupt *irr = irrindex(irrs, l, cpus);
123 if(!irr->used) continue;
124 rrddim_add(st, irr->id, irr->name, 1, 1, RRDDIM_INCREMENTAL);
127 else rrdset_next(st);
129 for(l = 0; l < lines ;l++) {
130 struct interrupt *irr = irrindex(irrs, l, cpus);
131 if(!irr->used) continue;
132 rrddim_set(st, irr->id, irr->total);
139 for(c = 0; c < cpus ; c++) {
141 snprintfz(id, 256, "cpu%d_softirqs", c);
143 st = rrdset_find_bytype("cpu", id);
145 // find if everything is zero
146 unsigned long long core_sum = 0 ;
147 for(l = 0; l < lines ;l++) {
148 struct interrupt *irr = irrindex(irrs, l, cpus);
149 if(!irr->used) continue;
150 core_sum += irr->value[c];
152 if(core_sum == 0) continue; // try next core
154 char name[256+1], title[256+1];
155 snprintfz(name, 256, "cpu%d_softirqs", c);
156 snprintfz(title, 256, "CPU%d softirqs", c);
157 st = rrdset_create("cpu", id, name, "softirqs", "cpu.softirqs", title, "softirqs/s", 3000 + c, update_every, RRDSET_TYPE_STACKED);
159 for(l = 0; l < lines ;l++) {
160 struct interrupt *irr = irrindex(irrs, l, cpus);
161 if(!irr->used) continue;
162 rrddim_add(st, irr->id, irr->name, 1, 1, RRDDIM_INCREMENTAL);
165 else rrdset_next(st);
167 for(l = 0; l < lines ;l++) {
168 struct interrupt *irr = irrindex(irrs, l, cpus);
169 if(!irr->used) continue;
170 rrddim_set(st, irr->id, irr->value[c]);