3 #define MAX_INTERRUPT_NAME 50
6 unsigned long long value;
13 char name[MAX_INTERRUPT_NAME + 1];
15 unsigned long long total;
16 struct cpu_interrupt cpu[];
19 // since each interrupt is variable in size
20 // we use this to calculate its record size
21 #define recordsize(cpus) (sizeof(struct interrupt) + (cpus * sizeof(struct cpu_interrupt)))
23 // given a base, get a pointer to each record
24 #define irrindex(base, line, cpus) ((struct interrupt *)&((char *)(base))[line * recordsize(cpus)])
26 static inline struct interrupt *get_interrupts_array(size_t lines, int cpus) {
27 static struct interrupt *irrs = NULL;
28 static size_t allocated = 0;
30 if(unlikely(lines != allocated)) {
34 irrs = (struct interrupt *)reallocz(irrs, lines * recordsize(cpus));
36 // reset all interrupt RRDDIM pointers as any line could have shifted
37 for(l = 0; l < lines ;l++) {
38 struct interrupt *irr = irrindex(irrs, l, cpus);
41 for(c = 0; c < cpus ;c++)
42 irr->cpu[c].rd = NULL;
51 int do_proc_interrupts(int update_every, usec_t dt) {
53 static procfile *ff = NULL;
54 static int cpus = -1, do_per_core = -1;
55 struct interrupt *irrs = NULL;
57 if(unlikely(do_per_core == -1))
58 do_per_core = config_get_boolean("plugin:proc:/proc/interrupts", "interrupts per core", 1);
61 char filename[FILENAME_MAX + 1];
62 snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/interrupts");
63 ff = procfile_open(config_get("plugin:proc:/proc/interrupts", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
68 ff = procfile_readall(ff);
70 return 0; // we return 0, so that we will retry to open it next time
72 size_t lines = procfile_lines(ff), l;
73 size_t words = procfile_linewords(ff, 0);
75 if(unlikely(!lines)) {
76 error("Cannot read /proc/interrupts, zero lines reported.");
80 // find how many CPUs are there
81 if(unlikely(cpus == -1)) {
84 for(w = 0; w < words ; w++) {
85 if(likely(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0))
91 error("PLUGIN: PROC_INTERRUPTS: Cannot find the number of CPUs in /proc/interrupts");
95 // allocate the size we need;
96 irrs = get_interrupts_array(lines, cpus);
99 // loop through all lines
100 for(l = 1; l < lines ;l++) {
101 struct interrupt *irr = irrindex(irrs, l, cpus);
105 words = procfile_linewords(ff, l);
106 if(unlikely(!words)) continue;
108 irr->id = procfile_lineword(ff, l, 0);
109 if(unlikely(!irr->id || !irr->id[0])) continue;
111 size_t idlen = strlen(irr->id);
112 if(unlikely(idlen && irr->id[idlen - 1] == ':'))
113 irr->id[idlen - 1] = '\0';
116 for(c = 0; c < cpus ;c++) {
117 if(likely((c + 1) < (int)words))
118 irr->cpu[c].value = str2ull(procfile_lineword(ff, l, (uint32_t)(c + 1)));
120 irr->cpu[c].value = 0;
122 irr->total += irr->cpu[c].value;
125 if(unlikely(isdigit(irr->id[0]) && (uint32_t)(cpus + 2) < words)) {
126 strncpyz(irr->name, procfile_lineword(ff, l, words - 1), MAX_INTERRUPT_NAME);
127 size_t nlen = strlen(irr->name);
128 idlen = strlen(irr->id);
129 if(likely(nlen + 1 + idlen <= MAX_INTERRUPT_NAME)) {
130 irr->name[nlen] = '_';
131 strncpyz(&irr->name[nlen + 1], irr->id, MAX_INTERRUPT_NAME - nlen - 1);
134 irr->name[MAX_INTERRUPT_NAME - idlen - 1] = '_';
135 strncpyz(&irr->name[MAX_INTERRUPT_NAME - idlen], irr->id, idlen);
139 strncpyz(irr->name, irr->id, MAX_INTERRUPT_NAME);
147 // --------------------------------------------------------------------
149 st = rrdset_find_bytype("system", "interrupts");
150 if(unlikely(!st)) st = rrdset_create("system", "interrupts", NULL, "interrupts", NULL, "System interrupts", "interrupts/s", 1000, update_every, RRDSET_TYPE_STACKED);
151 else rrdset_next(st);
153 for(l = 0; l < lines ;l++) {
154 struct interrupt *irr = irrindex(irrs, l, cpus);
155 if(unlikely(!irr->used)) continue;
156 // some interrupt may have changed without changing the total number of lines
157 // if the same number of interrupts have been added and removed between two
158 // calls of this function.
159 if(unlikely(!irr->rd || strncmp(irr->rd->name, irr->name, MAX_INTERRUPT_NAME) != 0)) {
160 irr->rd = rrddim_find(st, irr->id);
161 if(unlikely(!irr->rd))
162 irr->rd = rrddim_add(st, irr->id, irr->name, 1, 1, RRDDIM_INCREMENTAL);
164 rrddim_set_name(st, irr->rd, irr->name);
166 // also reset per cpu RRDDIMs to avoid repeating strncmp() in the per core loop
167 if(likely(do_per_core)) {
169 for (c = 0; c < cpus ;c++)
170 irr->cpu[c].rd = NULL;
173 rrddim_set_by_pointer(st, irr->rd, irr->total);
177 if(likely(do_per_core)) {
180 for(c = 0; c < cpus ;c++) {
182 snprintfz(id, 50, "cpu%d_interrupts", c);
184 st = rrdset_find_bytype("cpu", id);
187 snprintfz(title, 100, "CPU%d Interrupts", c);
188 st = rrdset_create("cpu", id, NULL, "interrupts", "cpu.interrupts", title, "interrupts/s", 1100 + c, update_every, RRDSET_TYPE_STACKED);
190 else rrdset_next(st);
192 for(l = 0; l < lines ;l++) {
193 struct interrupt *irr = irrindex(irrs, l, cpus);
194 if(unlikely(!irr->used)) continue;
195 if(unlikely(!irr->cpu[c].rd)) {
196 irr->cpu[c].rd = rrddim_find(st, irr->id);
197 if(unlikely(!irr->cpu[c].rd))
198 irr->cpu[c].rd = rrddim_add(st, irr->id, irr->name, 1, 1, RRDDIM_INCREMENTAL);
200 rrddim_set_name(st, irr->cpu[c].rd, irr->name);
202 rrddim_set_by_pointer(st, irr->cpu[c].rd, irr->cpu[c].value);