]> arthur.barton.de Git - netdata.git/blob - src/proc_interrupts.c
several code optimizations
[netdata.git] / src / proc_interrupts.c
1 #include "common.h"
2
3 #define MAX_INTERRUPT_NAME 50
4
5 struct interrupt {
6     int used;
7     char *id;
8     char name[MAX_INTERRUPT_NAME + 1];
9     unsigned long long total;
10     unsigned long long value[];
11 };
12
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)))
16
17 // given a base, get a pointer to each record
18 #define irrindex(base, line, cpus) ((struct interrupt *)&((char *)(base))[line * recordsize(cpus)])
19
20 static inline struct interrupt *get_interrupts_array(int lines, int cpus) {
21     static struct interrupt *irrs = NULL;
22     static int allocated = 0;
23
24     if(unlikely(lines > allocated)) {
25         irrs = (struct interrupt *)reallocz(irrs, lines * recordsize(cpus));
26         allocated = lines;
27     }
28
29     return irrs;
30 }
31
32 int do_proc_interrupts(int update_every, unsigned long long dt) {
33     (void)dt;
34
35     static procfile *ff = NULL;
36     static int cpus = -1, do_per_core = -1;
37     struct interrupt *irrs = NULL;
38
39     if(unlikely(do_per_core == -1))
40         do_per_core = config_get_boolean("plugin:proc:/proc/interrupts", "interrupts per core", 1);
41
42     if(unlikely(!ff)) {
43         char filename[FILENAME_MAX + 1];
44         snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/interrupts");
45         ff = procfile_open(config_get("plugin:proc:/proc/interrupts", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
46     }
47     if(unlikely(!ff))
48         return 1;
49
50     ff = procfile_readall(ff);
51     if(unlikely(!ff))
52         return 0; // we return 0, so that we will retry to open it next time
53
54     uint32_t lines = procfile_lines(ff), l;
55     uint32_t words = procfile_linewords(ff, 0), w;
56
57     if(unlikely(!lines)) {
58         error("Cannot read /proc/interrupts, zero lines reported.");
59         return 1;
60     }
61
62     // find how many CPUs are there
63     if(unlikely(cpus == -1)) {
64         cpus = 0;
65         for(w = 0; w < words ; w++) {
66             if(unlikely(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0))
67                 cpus++;
68         }
69     }
70
71     if(unlikely(!cpus)) {
72         error("PLUGIN: PROC_INTERRUPTS: Cannot find the number of CPUs in /proc/interrupts");
73         return 1;
74     }
75
76     // allocate the size we need;
77     irrs = get_interrupts_array(lines, cpus);
78     irrs[0].used = 0;
79
80     // loop through all lines
81     for(l = 1; l < lines ;l++) {
82         struct interrupt *irr = irrindex(irrs, l, cpus);
83         irr->used = 0;
84         irr->total = 0;
85
86         words = procfile_linewords(ff, l);
87         if(unlikely(!words)) continue;
88
89         irr->id = procfile_lineword(ff, l, 0);
90         if(unlikely(!irr->id || !irr->id[0])) continue;
91
92         int idlen = strlen(irr->id);
93         if(unlikely(irr->id[idlen - 1] == ':'))
94             irr->id[idlen - 1] = '\0';
95
96         int c;
97         for(c = 0; c < cpus ;c++) {
98             if(unlikely((c + 1) < (int)words))
99                 irr->value[c] = strtoull(procfile_lineword(ff, l, (uint32_t)(c + 1)), NULL, 10);
100             else
101                 irr->value[c] = 0;
102
103             irr->total += irr->value[c];
104         }
105
106         if(unlikely(isdigit(irr->id[0]) && (uint32_t)(cpus + 2) < words)) {
107             strncpyz(irr->name, procfile_lineword(ff, l, words - 1), MAX_INTERRUPT_NAME);
108             int nlen = strlen(irr->name);
109             if(unlikely(nlen < (MAX_INTERRUPT_NAME-1))) {
110                 irr->name[nlen] = '_';
111                 strncpyz(&irr->name[nlen + 1], irr->id, MAX_INTERRUPT_NAME - nlen);
112             }
113         }
114         else {
115             strncpyz(irr->name, irr->id, MAX_INTERRUPT_NAME);
116         }
117
118         irr->used = 1;
119     }
120
121     RRDSET *st;
122
123     // --------------------------------------------------------------------
124
125     st = rrdset_find_bytype("system", "interrupts");
126     if(unlikely(!st)) {
127         st = rrdset_create("system", "interrupts", NULL, "interrupts", NULL, "System interrupts", "interrupts/s", 1000, update_every, RRDSET_TYPE_STACKED);
128
129         for(l = 0; l < lines ;l++) {
130             struct interrupt *irr = irrindex(irrs, l, cpus);
131             if(unlikely(!irr->used)) continue;
132             rrddim_add(st, irr->id, irr->name, 1, 1, RRDDIM_INCREMENTAL);
133         }
134     }
135     else rrdset_next(st);
136
137     for(l = 0; l < lines ;l++) {
138         struct interrupt *irr = irrindex(irrs, l, cpus);
139         if(unlikely(!irr->used)) continue;
140         rrddim_set(st, irr->id, irr->total);
141     }
142     rrdset_done(st);
143
144     if(likely(do_per_core)) {
145         int c;
146
147         for(c = 0; c < cpus ; c++) {
148             char id[50+1];
149             snprintfz(id, 50, "cpu%d_interrupts", c);
150
151             st = rrdset_find_bytype("cpu", id);
152             if(unlikely(!st)) {
153                 char title[100+1];
154                 snprintfz(title, 100, "CPU%d Interrupts", c);
155                 st = rrdset_create("cpu", id, NULL, "interrupts", "cpu.interrupts", title, "interrupts/s", 1100 + c, update_every, RRDSET_TYPE_STACKED);
156
157                 for(l = 0; l < lines ;l++) {
158                     struct interrupt *irr = irrindex(irrs, l, cpus);
159                     if(unlikely(!irr->used)) continue;
160                     rrddim_add(st, irr->id, irr->name, 1, 1, RRDDIM_INCREMENTAL);
161                 }
162             }
163             else rrdset_next(st);
164
165             for(l = 0; l < lines ;l++) {
166                 struct interrupt *irr = irrindex(irrs, l, cpus);
167                 if(unlikely(!irr->used)) continue;
168                 rrddim_set(st, irr->id, irr->value[c]);
169             }
170             rrdset_done(st);
171         }
172     }
173
174     return 0;
175 }