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 *)reallocz(irrs, lines * recordsize(cpus));
33 int do_proc_softirqs(int update_every, unsigned long long dt) {
34 static procfile *ff = NULL;
35 static int cpus = -1, do_per_core = -1;
37 struct interrupt *irrs = NULL;
41 if(do_per_core == -1) do_per_core = config_get_boolean("plugin:proc:/proc/softirqs", "interrupts per core", 1);
44 char filename[FILENAME_MAX + 1];
45 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/softirqs");
46 ff = procfile_open(config_get("plugin:proc:/proc/softirqs", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
50 ff = procfile_readall(ff);
51 if(!ff) return 0; // we return 0, so that we will retry to open it next time
53 uint32_t lines = procfile_lines(ff), l;
54 uint32_t words = procfile_linewords(ff, 0), w;
57 error("Cannot read /proc/softirqs, zero lines reported.");
61 // find how many CPUs are there
64 for(w = 0; w < words ; w++) {
65 if(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0)
71 error("PLUGIN: PROC_SOFTIRQS: Cannot find the number of CPUs in /proc/softirqs");
75 // allocate the size we need;
76 irrs = get_interrupts_array(lines, cpus);
79 // loop through all lines
80 for(l = 1; l < lines ;l++) {
81 struct interrupt *irr = irrindex(irrs, l, cpus);
85 words = procfile_linewords(ff, l);
88 irr->id = procfile_lineword(ff, l, 0);
89 if(!irr->id || !irr->id[0]) continue;
91 int idlen = strlen(irr->id);
92 if(irr->id[idlen - 1] == ':')
93 irr->id[idlen - 1] = '\0';
96 for(c = 0; c < cpus ;c++) {
97 if((c + 1) < (int)words)
98 irr->value[c] = strtoull(procfile_lineword(ff, l, (uint32_t)(c + 1)), NULL, 10);
102 irr->total += irr->value[c];
105 strncpyz(irr->name, irr->id, MAX_INTERRUPT_NAME);
112 // --------------------------------------------------------------------
114 st = rrdset_find_bytype("system", "softirqs");
116 st = rrdset_create("system", "softirqs", NULL, "softirqs", NULL, "System softirqs", "softirqs/s", 950, update_every, RRDSET_TYPE_STACKED);
118 for(l = 0; l < lines ;l++) {
119 struct interrupt *irr = irrindex(irrs, l, cpus);
120 if(!irr->used) continue;
121 rrddim_add(st, irr->id, irr->name, 1, 1, RRDDIM_INCREMENTAL);
124 else rrdset_next(st);
126 for(l = 0; l < lines ;l++) {
127 struct interrupt *irr = irrindex(irrs, l, cpus);
128 if(!irr->used) continue;
129 rrddim_set(st, irr->id, irr->total);
136 for(c = 0; c < cpus ; c++) {
138 snprintfz(id, 256, "cpu%d_softirqs", c);
140 st = rrdset_find_bytype("cpu", id);
142 // find if everything is zero
143 unsigned long long core_sum = 0 ;
144 for(l = 0; l < lines ;l++) {
145 struct interrupt *irr = irrindex(irrs, l, cpus);
146 if(!irr->used) continue;
147 core_sum += irr->value[c];
149 if(core_sum == 0) continue; // try next core
151 char name[256+1], title[256+1];
152 snprintfz(name, 256, "cpu%d_softirqs", c);
153 snprintfz(title, 256, "CPU%d softirqs", c);
154 st = rrdset_create("cpu", id, name, "softirqs", "cpu.softirqs", title, "softirqs/s", 3000 + c, update_every, RRDSET_TYPE_STACKED);
156 for(l = 0; l < lines ;l++) {
157 struct interrupt *irr = irrindex(irrs, l, cpus);
158 if(!irr->used) continue;
159 rrddim_add(st, irr->id, irr->name, 1, 1, RRDDIM_INCREMENTAL);
162 else rrdset_next(st);
164 for(l = 0; l < lines ;l++) {
165 struct interrupt *irr = irrindex(irrs, l, cpus);
166 if(!irr->used) continue;
167 rrddim_set(st, irr->id, irr->value[c]);