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