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