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