]> arthur.barton.de Git - netdata.git/blob - src/proc_interrupts.c
dns_query_time plugin: replace "." with "_" in dimensions
[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(size_t lines, int cpus) {
27     static struct interrupt *irrs = NULL;
28     static size_t allocated = 0;
29
30     if(unlikely(lines != allocated)) {
31         size_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, usec_t 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", netdata_configured_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     size_t lines = procfile_lines(ff), l;
73     size_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         size_t idlen = strlen(irr->id);
112         if(unlikely(idlen && 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 = str2ull(procfile_lineword(ff, l, (uint32_t)(c + 1)));
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             size_t nlen = strlen(irr->name);
128             idlen = strlen(irr->id);
129             if(likely(nlen + 1 + idlen <= MAX_INTERRUPT_NAME)) {
130                 irr->name[nlen] = '_';
131                 strncpyz(&irr->name[nlen + 1], irr->id, MAX_INTERRUPT_NAME - nlen - 1);
132             }
133             else {
134                 irr->name[MAX_INTERRUPT_NAME - idlen - 1] = '_';
135                 strncpyz(&irr->name[MAX_INTERRUPT_NAME - idlen], irr->id, idlen);
136             }
137         }
138         else {
139             strncpyz(irr->name, irr->id, MAX_INTERRUPT_NAME);
140         }
141
142         irr->used = 1;
143     }
144
145     RRDSET *st;
146
147     // --------------------------------------------------------------------
148
149     st = rrdset_find_bytype_localhost("system", "interrupts");
150     if(unlikely(!st)) st = rrdset_create_localhost("system", "interrupts", NULL, "interrupts", NULL, "System interrupts"
151                                                    , "interrupts/s", 1000, update_every, RRDSET_TYPE_STACKED);
152     else rrdset_next(st);
153
154     for(l = 0; l < lines ;l++) {
155         struct interrupt *irr = irrindex(irrs, l, cpus);
156         if(unlikely(!irr->used)) continue;
157         // some interrupt may have changed without changing the total number of lines
158         // if the same number of interrupts have been added and removed between two
159         // calls of this function.
160         if(unlikely(!irr->rd || strncmp(irr->rd->name, irr->name, MAX_INTERRUPT_NAME) != 0)) {
161             irr->rd = rrddim_find(st, irr->id);
162             if(unlikely(!irr->rd))
163                 irr->rd = rrddim_add(st, irr->id, irr->name, 1, 1, RRD_ALGORITHM_INCREMENTAL);
164             else
165                 rrddim_set_name(st, irr->rd, irr->name);
166
167             // also reset per cpu RRDDIMs to avoid repeating strncmp() in the per core loop
168             if(likely(do_per_core)) {
169                 int c;
170                 for (c = 0; c < cpus ;c++)
171                     irr->cpu[c].rd = NULL;
172             }
173         }
174         rrddim_set_by_pointer(st, irr->rd, irr->total);
175     }
176     rrdset_done(st);
177
178     if(likely(do_per_core)) {
179         int c;
180
181         for(c = 0; c < cpus ;c++) {
182             char id[50+1];
183             snprintfz(id, 50, "cpu%d_interrupts", c);
184
185             st = rrdset_find_bytype_localhost("cpu", id);
186             if(unlikely(!st)) {
187                 char title[100+1];
188                 snprintfz(title, 100, "CPU%d Interrupts", c);
189                 st = rrdset_create_localhost("cpu", id, NULL, "interrupts", "cpu.interrupts", title, "interrupts/s",
190                         1100 + c, update_every, RRDSET_TYPE_STACKED);
191             }
192             else rrdset_next(st);
193
194             for(l = 0; l < lines ;l++) {
195                 struct interrupt *irr = irrindex(irrs, l, cpus);
196                 if(unlikely(!irr->used)) continue;
197                 if(unlikely(!irr->cpu[c].rd)) {
198                     irr->cpu[c].rd = rrddim_find(st, irr->id);
199                     if(unlikely(!irr->cpu[c].rd))
200                         irr->cpu[c].rd = rrddim_add(st, irr->id, irr->name, 1, 1, RRD_ALGORITHM_INCREMENTAL);
201                     else
202                         rrddim_set_name(st, irr->cpu[c].rd, irr->name);
203                 }
204                 rrddim_set_by_pointer(st, irr->cpu[c].rd, irr->cpu[c].value);
205             }
206             rrdset_done(st);
207         }
208     }
209
210     return 0;
211 }