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