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