1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "plugin_proc.h"
#define PLUGIN_PROC_MODULE_INTERRUPTS_NAME "/proc/interrupts"
#define CONFIG_SECTION_PLUGIN_PROC_INTERRUPTS "plugin:" PLUGIN_PROC_CONFIG_NAME ":" PLUGIN_PROC_MODULE_INTERRUPTS_NAME
#define MAX_INTERRUPT_NAME 50
struct cpu_interrupt {
unsigned long long value;
RRDDIM *rd;
};
struct interrupt {
int used;
char *id;
char name[MAX_INTERRUPT_NAME + 1];
RRDDIM *rd;
unsigned long long total;
struct cpu_interrupt cpu[];
};
// since each interrupt is variable in size
// we use this to calculate its record size
#define recordsize(cpus) (sizeof(struct interrupt) + ((cpus) * sizeof(struct cpu_interrupt)))
// given a base, get a pointer to each record
#define irrindex(base, line, cpus) ((struct interrupt *)&((char *)(base))[(line) * recordsize(cpus)])
static inline struct interrupt *get_interrupts_array(size_t lines, int cpus) {
static struct interrupt *irrs = NULL;
static size_t allocated = 0;
if(unlikely(lines != allocated)) {
size_t l;
int c;
irrs = (struct interrupt *)reallocz(irrs, lines * recordsize(cpus));
// reset all interrupt RRDDIM pointers as any line could have shifted
for(l = 0; l < lines ;l++) {
struct interrupt *irr = irrindex(irrs, l, cpus);
irr->rd = NULL;
irr->name[0] = '\0';
for(c = 0; c < cpus ;c++)
irr->cpu[c].rd = NULL;
}
allocated = lines;
}
return irrs;
}
int do_proc_interrupts(int update_every, usec_t dt) {
(void)dt;
static procfile *ff = NULL;
static int cpus = -1, do_per_core = CONFIG_BOOLEAN_INVALID;
struct interrupt *irrs = NULL;
if(unlikely(do_per_core == CONFIG_BOOLEAN_INVALID))
do_per_core = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_INTERRUPTS, "interrupts per core", CONFIG_BOOLEAN_NO);
if(unlikely(!ff)) {
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/interrupts");
ff = procfile_open(config_get(CONFIG_SECTION_PLUGIN_PROC_INTERRUPTS, "filename to monitor", filename), " \t:", PROCFILE_FLAG_DEFAULT);
}
if(unlikely(!ff))
return 1;
ff = procfile_readall(ff);
if(unlikely(!ff))
return 0; // we return 0, so that we will retry to open it next time
size_t lines = procfile_lines(ff), l;
size_t words = procfile_linewords(ff, 0);
if(unlikely(!lines)) {
collector_error("Cannot read /proc/interrupts, zero lines reported.");
return 1;
}
// find how many CPUs are there
if(unlikely(cpus == -1)) {
uint32_t w;
cpus = 0;
for(w = 0; w < words ; w++) {
if(likely(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0))
cpus++;
}
}
if(unlikely(!cpus)) {
collector_error("PLUGIN: PROC_INTERRUPTS: Cannot find the number of CPUs in /proc/interrupts");
return 1;
}
// allocate the size we need;
irrs = get_interrupts_array(lines, cpus);
irrs[0].used = 0;
// loop through all lines
for(l = 1; l < lines ;l++) {
struct interrupt *irr = irrindex(irrs, l, cpus);
irr->used = 0;
irr->total = 0;
words = procfile_linewords(ff, l);
if(unlikely(!words)) continue;
irr->id = procfile_lineword(ff, l, 0);
if(unlikely(!irr->id || !irr->id[0])) continue;
size_t idlen = strlen(irr->id);
if(irr->id[idlen - 1] == ':')
irr->id[--idlen] = '\0';
int c;
for(c = 0; c < cpus ;c++) {
if(likely((c + 1) < (int)words))
irr->cpu[c].value = str2ull(procfile_lineword(ff, l, (uint32_t) (c + 1)), NULL);
else
irr->cpu[c].value = 0;
irr->total += irr->cpu[c].value;
}
if(unlikely(isdigit(irr->id[0]) && (uint32_t)(cpus + 2) < words)) {
strncpyz(irr->name, procfile_lineword(ff, l, words - 1), MAX_INTERRUPT_NAME);
size_t nlen = strlen(irr->name);
if(likely(nlen + 1 + idlen <= MAX_INTERRUPT_NAME)) {
irr->name[nlen] = '_';
strncpyz(&irr->name[nlen + 1], irr->id, MAX_INTERRUPT_NAME - nlen - 1);
}
else {
irr->name[MAX_INTERRUPT_NAME - idlen - 1] = '_';
strncpyz(&irr->name[MAX_INTERRUPT_NAME - idlen], irr->id, idlen);
}
}
else {
strncpyz(irr->name, irr->id, MAX_INTERRUPT_NAME);
}
irr->used = 1;
}
static RRDSET *st_system_interrupts = NULL;
if(unlikely(!st_system_interrupts))
st_system_interrupts = rrdset_create_localhost(
"system"
, "interrupts"
, NULL
, "interrupts"
, NULL
, "System interrupts"
, "interrupts/s"
, PLUGIN_PROC_NAME
, PLUGIN_PROC_MODULE_INTERRUPTS_NAME
, NETDATA_CHART_PRIO_SYSTEM_INTERRUPTS
, update_every
, RRDSET_TYPE_STACKED
);
for(l = 0; l < lines ;l++) {
struct interrupt *irr = irrindex(irrs, l, cpus);
if(irr->used && irr->total) {
// some interrupt may have changed without changing the total number of lines
// if the same number of interrupts have been added and removed between two
// calls of this function.
if(unlikely(!irr->rd || strncmp(rrddim_name(irr->rd), irr->name, MAX_INTERRUPT_NAME) != 0)) {
irr->rd = rrddim_add(st_system_interrupts, irr->id, irr->name, 1, 1, RRD_ALGORITHM_INCREMENTAL);
rrddim_reset_name(st_system_interrupts, irr->rd, irr->name);
// also reset per cpu RRDDIMs to avoid repeating strncmp() in the per core loop
if(likely(do_per_core != CONFIG_BOOLEAN_NO)) {
int c;
for(c = 0; c < cpus; c++) irr->cpu[c].rd = NULL;
}
}
rrddim_set_by_pointer(st_system_interrupts, irr->rd, irr->total);
}
}
rrdset_done(st_system_interrupts);
if(likely(do_per_core != CONFIG_BOOLEAN_NO)) {
static RRDSET **core_st = NULL;
static int old_cpus = 0;
if(old_cpus < cpus) {
core_st = reallocz(core_st, sizeof(RRDSET *) * cpus);
memset(&core_st[old_cpus], 0, sizeof(RRDSET *) * (cpus - old_cpus));
old_cpus = cpus;
}
int c;
for(c = 0; c < cpus ;c++) {
if(unlikely(!core_st[c])) {
char id[50+1];
snprintfz(id, sizeof(id) - 1, "cpu%d_interrupts", c);
char title[100+1];
snprintfz(title, sizeof(title) - 1, "CPU Interrupts");
core_st[c] = rrdset_create_localhost(
"cpu"
, id
, NULL
, "interrupts"
, "cpu.interrupts"
, title
, "interrupts/s"
, PLUGIN_PROC_NAME
, PLUGIN_PROC_MODULE_INTERRUPTS_NAME
, NETDATA_CHART_PRIO_INTERRUPTS_PER_CORE + c
, update_every
, RRDSET_TYPE_STACKED
);
char core[50+1];
snprintfz(core, sizeof(core) - 1, "cpu%d", c);
rrdlabels_add(core_st[c]->rrdlabels, "cpu", core, RRDLABEL_SRC_AUTO);
}
for(l = 0; l < lines ;l++) {
struct interrupt *irr = irrindex(irrs, l, cpus);
if(irr->used && (do_per_core == CONFIG_BOOLEAN_YES || irr->cpu[c].value)) {
if(unlikely(!irr->cpu[c].rd)) {
irr->cpu[c].rd = rrddim_add(core_st[c], irr->id, irr->name, 1, 1, RRD_ALGORITHM_INCREMENTAL);
rrddim_reset_name(core_st[c], irr->cpu[c].rd, irr->name);
}
rrddim_set_by_pointer(core_st[c], irr->cpu[c].rd, irr->cpu[c].value);
}
}
rrdset_done(core_st[c]);
}
}
return 0;
}
|