]> arthur.barton.de Git - netdata.git/blob - src/proc_interrupts.c
145d72a20fb917b533b46c570a08ab577b243771
[netdata.git] / src / proc_interrupts.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_interrupts(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))
58         do_per_core = config_get_boolean("plugin:proc:/proc/interrupts", "interrupts per core", 1);
59
60     if(unlikely(!ff)) {
61         char filename[FILENAME_MAX + 1];
62         snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/interrupts");
63         ff = procfile_open(config_get("plugin:proc:/proc/interrupts", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
64     }
65     if(unlikely(!ff))
66         return 1;
67
68     ff = procfile_readall(ff);
69     if(unlikely(!ff))
70         return 0; // we return 0, so that we will retry to open it next time
71
72     uint32_t lines = procfile_lines(ff), l;
73     uint32_t words = procfile_linewords(ff, 0);
74
75     if(unlikely(!lines)) {
76         error("Cannot read /proc/interrupts, zero lines reported.");
77         return 1;
78     }
79
80     // find how many CPUs are there
81     if(unlikely(cpus == -1)) {
82         uint32_t w;
83         cpus = 0;
84         for(w = 0; w < words ; w++) {
85             if(likely(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0))
86                 cpus++;
87         }
88     }
89
90     if(unlikely(!cpus)) {
91         error("PLUGIN: PROC_INTERRUPTS: Cannot find the number of CPUs in /proc/interrupts");
92         return 1;
93     }
94
95     // allocate the size we need;
96     irrs = get_interrupts_array(lines, cpus);
97     irrs[0].used = 0;
98
99     // loop through all lines
100     for(l = 1; l < lines ;l++) {
101         struct interrupt *irr = irrindex(irrs, l, cpus);
102         irr->used = 0;
103         irr->total = 0;
104
105         words = procfile_linewords(ff, l);
106         if(unlikely(!words)) continue;
107
108         irr->id = procfile_lineword(ff, l, 0);
109         if(unlikely(!irr->id || !irr->id[0])) continue;
110
111         int idlen = strlen(irr->id);
112         if(unlikely(irr->id[idlen - 1] == ':'))
113             irr->id[idlen - 1] = '\0';
114
115         int c;
116         for(c = 0; c < cpus ;c++) {
117             if(likely((c + 1) < (int)words))
118                 irr->cpu[c].value = strtoull(procfile_lineword(ff, l, (uint32_t)(c + 1)), NULL, 10);
119             else
120                 irr->cpu[c].value = 0;
121
122             irr->total += irr->cpu[c].value;
123         }
124
125         if(unlikely(isdigit(irr->id[0]) && (uint32_t)(cpus + 2) < words)) {
126             strncpyz(irr->name, procfile_lineword(ff, l, words - 1), MAX_INTERRUPT_NAME);
127             int nlen = strlen(irr->name);
128             if(unlikely(nlen < (MAX_INTERRUPT_NAME-1))) {
129                 irr->name[nlen] = '_';
130                 strncpyz(&irr->name[nlen + 1], irr->id, MAX_INTERRUPT_NAME - nlen);
131             }
132         }
133         else {
134             strncpyz(irr->name, irr->id, MAX_INTERRUPT_NAME);
135         }
136
137         irr->used = 1;
138     }
139
140     RRDSET *st;
141
142     // --------------------------------------------------------------------
143
144     st = rrdset_find_bytype("system", "interrupts");
145     if(unlikely(!st)) st = rrdset_create("system", "interrupts", NULL, "interrupts", NULL, "System interrupts", "interrupts/s", 1000, update_every, RRDSET_TYPE_STACKED);
146     else rrdset_next(st);
147
148     for(l = 0; l < lines ;l++) {
149         struct interrupt *irr = irrindex(irrs, l, cpus);
150         if(unlikely(!irr->used)) continue;
151         // some interrupt may have changed without changing the total number of lines
152         // if the same number of interrupts have been added and removed between two
153         // calls of this function.
154         if(unlikely(!irr->rd || strncmp(irr->rd->name, irr->name, MAX_INTERRUPT_NAME) != 0)) {
155             irr->rd = rrddim_find(st, irr->id);
156             if(unlikely(!irr->rd))
157                 irr->rd = rrddim_add(st, irr->id, irr->name, 1, 1, RRDDIM_INCREMENTAL);
158             else
159                 rrddim_set_name(st, irr->rd, irr->name);
160
161             // also reset per cpu RRDDIMs to avoid repeating strncmp() in the per core loop
162             if(likely(do_per_core)) {
163                 int c;
164                 for (c = 0; c < cpus ;c++)
165                     irr->cpu[c].rd = NULL;
166             }
167         }
168         rrddim_set_by_pointer(st, irr->rd, irr->total);
169     }
170     rrdset_done(st);
171
172     if(likely(do_per_core)) {
173         int c;
174
175         for(c = 0; c < cpus ;c++) {
176             char id[50+1];
177             snprintfz(id, 50, "cpu%d_interrupts", c);
178
179             st = rrdset_find_bytype("cpu", id);
180             if(unlikely(!st)) {
181                 char title[100+1];
182                 snprintfz(title, 100, "CPU%d Interrupts", c);
183                 st = rrdset_create("cpu", id, NULL, "interrupts", "cpu.interrupts", title, "interrupts/s", 1100 + c, update_every, RRDSET_TYPE_STACKED);
184             }
185             else rrdset_next(st);
186
187             for(l = 0; l < lines ;l++) {
188                 struct interrupt *irr = irrindex(irrs, l, cpus);
189                 if(unlikely(!irr->used)) continue;
190                 if(unlikely(!irr->cpu[c].rd)) {
191                     irr->cpu[c].rd = rrddim_find(st, irr->id);
192                     if(unlikely(!irr->cpu[c].rd))
193                         irr->cpu[c].rd = rrddim_add(st, irr->id, irr->name, 1, 1, RRDDIM_INCREMENTAL);
194                     else
195                         rrddim_set_name(st, irr->cpu[c].rd, irr->name);
196                 }
197                 rrddim_set_by_pointer(st, irr->cpu[c].rd, irr->cpu[c].value);
198             }
199             rrdset_done(st);
200         }
201     }
202
203     return 0;
204 }