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