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, unsigned long long 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), w;
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)) {
63 for(w = 0; w < words ; w++) {
64 if(unlikely(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0))
70 error("PLUGIN: PROC_SOFTIRQS: Cannot find the number of CPUs in /proc/softirqs");
74 // allocate the size we need;
75 irrs = get_interrupts_array(lines, cpus);
78 // loop through all lines
79 for(l = 1; l < lines ;l++) {
80 struct interrupt *irr = irrindex(irrs, l, cpus);
84 words = procfile_linewords(ff, l);
85 if(unlikely(!words)) continue;
87 irr->id = procfile_lineword(ff, l, 0);
88 if(unlikely(!irr->id || !irr->id[0])) continue;
90 int idlen = strlen(irr->id);
91 if(unlikely(irr->id[idlen - 1] == ':'))
92 irr->id[idlen - 1] = '\0';
95 for(c = 0; c < cpus ;c++) {
96 if(unlikely((c + 1) < (int)words))
97 irr->value[c] = strtoull(procfile_lineword(ff, l, (uint32_t)(c + 1)), NULL, 10);
101 irr->total += irr->value[c];
104 strncpyz(irr->name, irr->id, MAX_INTERRUPT_NAME);
111 // --------------------------------------------------------------------
113 st = rrdset_find_bytype("system", "softirqs");
115 st = rrdset_create("system", "softirqs", NULL, "softirqs", NULL, "System softirqs", "softirqs/s", 950, update_every, RRDSET_TYPE_STACKED);
117 for(l = 0; l < lines ;l++) {
118 struct interrupt *irr = irrindex(irrs, l, cpus);
119 if(unlikely(!irr->used)) continue;
120 rrddim_add(st, irr->id, irr->name, 1, 1, RRDDIM_INCREMENTAL);
123 else rrdset_next(st);
125 for(l = 0; l < lines ;l++) {
126 struct interrupt *irr = irrindex(irrs, l, cpus);
127 if(unlikely(!irr->used)) continue;
128 rrddim_set(st, irr->id, irr->total);
135 for(c = 0; c < cpus ; c++) {
137 snprintfz(id, 50, "cpu%d_softirqs", c);
139 st = rrdset_find_bytype("cpu", id);
141 // find if everything is zero
142 unsigned long long core_sum = 0 ;
143 for(l = 0; l < lines ;l++) {
144 struct interrupt *irr = irrindex(irrs, l, cpus);
145 if(unlikely(!irr->used)) continue;
146 core_sum += irr->value[c];
148 if(unlikely(core_sum == 0)) continue; // try next core
151 snprintfz(title, 100, "CPU%d softirqs", c);
152 st = rrdset_create("cpu", id, NULL, "softirqs", "cpu.softirqs", title, "softirqs/s", 3000 + c, update_every, RRDSET_TYPE_STACKED);
154 for(l = 0; l < lines ;l++) {
155 struct interrupt *irr = irrindex(irrs, l, cpus);
156 if(unlikely(!irr->used)) continue;
157 rrddim_add(st, irr->id, irr->name, 1, 1, RRDDIM_INCREMENTAL);
160 else rrdset_next(st);
162 for(l = 0; l < lines ;l++) {
163 struct interrupt *irr = irrindex(irrs, l, cpus);
164 if(unlikely(!irr->used)) continue;
165 rrddim_set(st, irr->id, irr->value[c]);