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