]> arthur.barton.de Git - netdata.git/blob - src/proc_softirqs.c
Merge pull request #1304 from rlefevre/unknown-interrupts
[netdata.git] / src / proc_softirqs.c
1 #include "common.h"
2
3 #define MAX_INTERRUPT_NAME 50
4
5 struct cpu_interrupt {
6     unsigned long long value;
7     RRDDIM *rd;
8 };
9
10 struct interrupt {
11     int used;
12     char *id;
13     char name[MAX_INTERRUPT_NAME + 1];
14     RRDDIM *rd;
15     unsigned long long total;
16     struct cpu_interrupt cpu[];
17 };
18
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)))
22
23 // given a base, get a pointer to each record
24 #define irrindex(base, line, cpus) ((struct interrupt *)&((char *)(base))[line * recordsize(cpus)])
25
26 static inline struct interrupt *get_interrupts_array(int lines, int cpus) {
27     static struct interrupt *irrs = NULL;
28     static int allocated = 0;
29
30     if(unlikely(lines != allocated)) {
31         uint32_t l;
32         int c;
33
34         irrs = (struct interrupt *)reallocz(irrs, lines * recordsize(cpus));
35
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);
39             irr->rd = NULL;
40             irr->name[0] = '\0';
41             for(c = 0; c < cpus ;c++)
42                 irr->cpu[c].rd = NULL;
43         }
44
45         allocated = lines;
46     }
47
48     return irrs;
49 }
50
51 int do_proc_softirqs(int update_every, unsigned long long dt) {
52     (void)dt;
53     static procfile *ff = NULL;
54     static int cpus = -1, do_per_core = -1;
55     struct interrupt *irrs = NULL;
56
57     if(unlikely(do_per_core == -1)) do_per_core = config_get_boolean("plugin:proc:/proc/softirqs", "interrupts per core", 1);
58
59     if(unlikely(!ff)) {
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;
64     }
65
66     ff = procfile_readall(ff);
67     if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time
68
69     uint32_t lines = procfile_lines(ff), l;
70     uint32_t words = procfile_linewords(ff, 0);
71
72     if(unlikely(!lines)) {
73         error("Cannot read /proc/softirqs, zero lines reported.");
74         return 1;
75     }
76
77     // find how many CPUs are there
78     if(unlikely(cpus == -1)) {
79         uint32_t w;
80         cpus = 0;
81         for(w = 0; w < words ; w++) {
82             if(likely(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0))
83                 cpus++;
84         }
85     }
86
87     if(unlikely(!cpus)) {
88         error("PLUGIN: PROC_SOFTIRQS: Cannot find the number of CPUs in /proc/softirqs");
89         return 1;
90     }
91
92     // allocate the size we need;
93     irrs = get_interrupts_array(lines, cpus);
94     irrs[0].used = 0;
95
96     // loop through all lines
97     for(l = 1; l < lines ;l++) {
98         struct interrupt *irr = irrindex(irrs, l, cpus);
99         irr->used = 0;
100         irr->total = 0;
101
102         words = procfile_linewords(ff, l);
103         if(unlikely(!words)) continue;
104
105         irr->id = procfile_lineword(ff, l, 0);
106         if(unlikely(!irr->id || !irr->id[0])) continue;
107
108         int idlen = strlen(irr->id);
109         if(unlikely(irr->id[idlen - 1] == ':'))
110             irr->id[idlen - 1] = '\0';
111
112         int c;
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);
116             else
117                 irr->cpu[c].value = 0;
118
119             irr->total += irr->cpu[c].value;
120         }
121
122         strncpyz(irr->name, irr->id, MAX_INTERRUPT_NAME);
123
124         irr->used = 1;
125     }
126
127     RRDSET *st;
128
129     // --------------------------------------------------------------------
130
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);
134
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);
145             else
146                 rrddim_set_name(st, irr->rd, irr->name);
147
148             // also reset per cpu RRDDIMs to avoid repeating strncmp() in the per core loop
149             if(likely(do_per_core)) {
150                 int c;
151                 for (c = 0; c < cpus ;c++)
152                     irr->cpu[c].rd = NULL;
153             }
154         }
155         rrddim_set_by_pointer(st, irr->rd, irr->total);
156     }
157     rrdset_done(st);
158
159     if(do_per_core) {
160         int c;
161
162         for(c = 0; c < cpus ; c++) {
163             char id[50+1];
164             snprintfz(id, 50, "cpu%d_softirqs", c);
165
166             st = rrdset_find_bytype("cpu", id);
167             if(unlikely(!st)) {
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;
174                 }
175                 if(unlikely(core_sum == 0)) continue; // try next core
176
177                 char title[100+1];
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);
180             }
181             else rrdset_next(st);
182
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);
190                     else
191                         rrddim_set_name(st, irr->cpu[c].rd, irr->name);
192                 }
193                 rrddim_set_by_pointer(st, irr->cpu[c].rd, irr->cpu[c].value);
194             }
195             rrdset_done(st);
196         }
197     }
198
199     return 0;
200 }