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(unlikely(lines > allocated)) {
25 irrs = (struct interrupt *)reallocz(irrs, lines * recordsize(cpus));
32 int do_proc_softirqs(int update_every, usec_t dt) {
35 static procfile *ff = NULL;
36 static int cpus = -1, do_per_core = -1;
38 struct interrupt *irrs = NULL;
40 if(unlikely(do_per_core == -1)) do_per_core = config_get_boolean("plugin:proc:/proc/softirqs", "interrupts per core", 1);
43 char filename[FILENAME_MAX + 1];
44 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/softirqs");
45 ff = procfile_open(config_get("plugin:proc:/proc/softirqs", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
46 if(unlikely(!ff)) return 1;
49 ff = procfile_readall(ff);
50 if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time
52 uint32_t lines = procfile_lines(ff), l;
53 uint32_t words = procfile_linewords(ff, 0);
55 if(unlikely(!lines)) {
56 error("Cannot read /proc/softirqs, zero lines reported.");
60 // find how many CPUs are there
61 if(unlikely(cpus == -1)) {
64 for(w = 0; w < words ; w++) {
65 if(unlikely(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);
86 if(unlikely(!words)) continue;
88 irr->id = procfile_lineword(ff, l, 0);
89 if(unlikely(!irr->id || !irr->id[0])) continue;
91 int idlen = strlen(irr->id);
92 if(unlikely(irr->id[idlen - 1] == ':'))
93 irr->id[idlen - 1] = '\0';
96 for(c = 0; c < cpus ;c++) {
97 if(unlikely((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(unlikely(!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(unlikely(!irr->used)) continue;
129 rrddim_set(st, irr->id, irr->total);
136 for(c = 0; c < cpus ; c++) {
138 snprintfz(id, 50, "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(unlikely(!irr->used)) continue;
147 core_sum += irr->value[c];
149 if(unlikely(core_sum == 0)) continue; // try next core
152 snprintfz(title, 100, "CPU%d softirqs", c);
153 st = rrdset_create("cpu", id, NULL, "softirqs", "cpu.softirqs", title, "softirqs/s", 3000 + c, update_every, RRDSET_TYPE_STACKED);
155 for(l = 0; l < lines ;l++) {
156 struct interrupt *irr = irrindex(irrs, l, cpus);
157 if(unlikely(!irr->used)) continue;
158 rrddim_add(st, irr->id, irr->name, 1, 1, RRDDIM_INCREMENTAL);
161 else rrdset_next(st);
163 for(l = 0; l < lines ;l++) {
164 struct interrupt *irr = irrindex(irrs, l, cpus);
165 if(unlikely(!irr->used)) continue;
166 rrddim_set(st, irr->id, irr->value[c]);