3 #define MAX_INTERRUPT_NAME 50
6 unsigned long long value;
13 char name[MAX_INTERRUPT_NAME + 1];
15 unsigned long long total;
16 struct cpu_interrupt cpu[];
19 // since each interrupt is variable in size
20 // we use this to calculate its record size
21 #define recordsize(cpus) (sizeof(struct interrupt) + (cpus * sizeof(struct cpu_interrupt)))
23 // given a base, get a pointer to each record
24 #define irrindex(base, line, cpus) ((struct interrupt *)&((char *)(base))[line * recordsize(cpus)])
26 static inline struct interrupt *get_interrupts_array(uint32_t lines, int cpus) {
27 static struct interrupt *irrs = NULL;
28 static uint32_t allocated = 0;
30 if(unlikely(lines != allocated)) {
34 irrs = (struct interrupt *)reallocz(irrs, lines * recordsize(cpus));
36 // reset all interrupt RRDDIM pointers as any line could have shifted
37 for(l = 0; l < lines ;l++) {
38 struct interrupt *irr = irrindex(irrs, l, cpus);
41 for(c = 0; c < cpus ;c++)
42 irr->cpu[c].rd = NULL;
51 int do_proc_softirqs(int update_every, usec_t dt) {
53 static procfile *ff = NULL;
54 static int cpus = -1, do_per_core = -1;
55 struct interrupt *irrs = NULL;
57 if(unlikely(do_per_core == -1)) do_per_core = config_get_boolean("plugin:proc:/proc/softirqs", "interrupts per core", 1);
60 char filename[FILENAME_MAX + 1];
61 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/softirqs");
62 ff = procfile_open(config_get("plugin:proc:/proc/softirqs", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
63 if(unlikely(!ff)) return 1;
66 ff = procfile_readall(ff);
67 if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time
69 uint32_t lines = procfile_lines(ff), l;
70 uint32_t words = procfile_linewords(ff, 0);
72 if(unlikely(!lines)) {
73 error("Cannot read /proc/softirqs, zero lines reported.");
77 // find how many CPUs are there
78 if(unlikely(cpus == -1)) {
81 for(w = 0; w < words ; w++) {
82 if(likely(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0))
88 error("PLUGIN: PROC_SOFTIRQS: Cannot find the number of CPUs in /proc/softirqs");
92 // allocate the size we need;
93 irrs = get_interrupts_array(lines, cpus);
96 // loop through all lines
97 for(l = 1; l < lines ;l++) {
98 struct interrupt *irr = irrindex(irrs, l, cpus);
102 words = procfile_linewords(ff, l);
103 if(unlikely(!words)) continue;
105 irr->id = procfile_lineword(ff, l, 0);
106 if(unlikely(!irr->id || !irr->id[0])) continue;
108 int idlen = strlen(irr->id);
109 if(unlikely(irr->id[idlen - 1] == ':'))
110 irr->id[idlen - 1] = '\0';
113 for(c = 0; c < cpus ;c++) {
114 if(likely((c + 1) < (int)words))
115 irr->cpu[c].value = strtoull(procfile_lineword(ff, l, (uint32_t)(c + 1)), NULL, 10);
117 irr->cpu[c].value = 0;
119 irr->total += irr->cpu[c].value;
122 strncpyz(irr->name, irr->id, MAX_INTERRUPT_NAME);
129 // --------------------------------------------------------------------
131 st = rrdset_find_bytype("system", "softirqs");
132 if(unlikely(!st)) st = rrdset_create("system", "softirqs", NULL, "softirqs", NULL, "System softirqs", "softirqs/s", 950, update_every, RRDSET_TYPE_STACKED);
133 else rrdset_next(st);
135 for(l = 0; l < lines ;l++) {
136 struct interrupt *irr = irrindex(irrs, l, cpus);
137 if(unlikely(!irr->used)) continue;
138 // some interrupt may have changed without changing the total number of lines
139 // if the same number of interrupts have been added and removed between two
140 // calls of this function.
141 if(unlikely(!irr->rd || strncmp(irr->name, irr->rd->name, MAX_INTERRUPT_NAME) != 0)) {
142 irr->rd = rrddim_find(st, irr->id);
143 if(unlikely(!irr->rd))
144 irr->rd = rrddim_add(st, irr->id, irr->name, 1, 1, RRDDIM_INCREMENTAL);
146 rrddim_set_name(st, irr->rd, irr->name);
148 // also reset per cpu RRDDIMs to avoid repeating strncmp() in the per core loop
149 if(likely(do_per_core)) {
151 for (c = 0; c < cpus ;c++)
152 irr->cpu[c].rd = NULL;
155 rrddim_set_by_pointer(st, irr->rd, irr->total);
162 for(c = 0; c < cpus ; c++) {
164 snprintfz(id, 50, "cpu%d_softirqs", c);
166 st = rrdset_find_bytype("cpu", id);
168 // find if everything is zero
169 unsigned long long core_sum = 0 ;
170 for(l = 0; l < lines ;l++) {
171 struct interrupt *irr = irrindex(irrs, l, cpus);
172 if(unlikely(!irr->used)) continue;
173 core_sum += irr->cpu[c].value;
175 if(unlikely(core_sum == 0)) continue; // try next core
178 snprintfz(title, 100, "CPU%d softirqs", c);
179 st = rrdset_create("cpu", id, NULL, "softirqs", "cpu.softirqs", title, "softirqs/s", 3000 + c, update_every, RRDSET_TYPE_STACKED);
181 else rrdset_next(st);
183 for(l = 0; l < lines ;l++) {
184 struct interrupt *irr = irrindex(irrs, l, cpus);
185 if(unlikely(!irr->used)) continue;
186 if(unlikely(!irr->cpu[c].rd)) {
187 irr->cpu[c].rd = rrddim_find(st, irr->id);
188 if(unlikely(!irr->cpu[c].rd))
189 irr->cpu[c].rd = rrddim_add(st, irr->id, irr->name, 1, 1, RRDDIM_INCREMENTAL);
191 rrddim_set_name(st, irr->cpu[c].rd, irr->name);
193 rrddim_set_by_pointer(st, irr->cpu[c].rd, irr->cpu[c].value);