]> arthur.barton.de Git - netdata.git/blob - src/proc_softirqs.c
Merge remote-tracking branch 'fredericopissarra/changes' into registry
[netdata.git] / src / proc_softirqs.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <ctype.h>
8
9 #include "common.h"
10 #include "appconfig.h"
11 #include "procfile.h"
12 #include "rrd.h"
13 #include "plugin_proc.h"
14 #include "log.h"
15
16 #define MAX_INTERRUPT_NAME 50
17
18 struct interrupt {
19         int used;
20         char *id;
21         char name[MAX_INTERRUPT_NAME + 1];
22         unsigned long long total;
23         unsigned long long value[];
24 };
25
26 // since each interrupt is variable in size
27 // we use this to calculate its record size
28 #define recordsize(cpus) (sizeof(struct interrupt) + (cpus * sizeof(unsigned long long)))
29
30 // given a base, get a pointer to each record
31 #define irrindex(base, line, cpus) ((struct interrupt *)&((char *)(base))[line * recordsize(cpus)])
32
33 static inline struct interrupt *get_interrupts_array(int lines, int cpus) {
34         static struct interrupt *irrs = NULL;
35         static int allocated = 0;
36
37         if(lines < allocated) return irrs;
38         else {
39                 irrs = (struct interrupt *)realloc(irrs, lines * recordsize(cpus));
40                 if(!irrs)
41                         fatal("Cannot allocate memory for %d interrupts", lines);
42
43                 allocated = lines;
44         }
45
46         return irrs;
47 }
48
49 int do_proc_softirqs(int update_every, unsigned long long dt) {
50         static procfile *ff = NULL;
51         static int cpus = -1, do_per_core = -1;
52
53         struct interrupt *irrs = NULL;
54
55         if(dt) {};
56
57         if(do_per_core == -1) do_per_core = config_get_boolean("plugin:proc:/proc/softirqs", "interrupts per core", 1);
58
59         if(!ff) {
60                 char filename[FILENAME_MAX + 1];
61                 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/softirqs");
62                 ff = procfile_open(config_get("plugin:proc:/proc/softirqs", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
63         }
64         if(!ff) return 1;
65
66         ff = procfile_readall(ff);
67         if(!ff) return 0; // we return 0, so that we will retry to open it next time
68
69         uint32_t lines = procfile_lines(ff), l;
70         uint32_t words = procfile_linewords(ff, 0), w;
71
72         if(!lines) {
73                 error("Cannot read /proc/softirqs, zero lines reported.");
74                 return 1;
75         }
76
77         // find how many CPUs are there
78         if(cpus == -1) {
79                 cpus = 0;
80                 for(w = 0; w < words ; w++) {
81                         if(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0)
82                                 cpus++;
83                 }
84         }
85
86         if(!cpus) {
87                 error("PLUGIN: PROC_SOFTIRQS: Cannot find the number of CPUs in /proc/softirqs");
88                 return 1;
89         }
90
91         // allocate the size we need;
92         irrs = get_interrupts_array(lines, cpus);
93         irrs[0].used = 0;
94
95         // loop through all lines
96         for(l = 1; l < lines ;l++) {
97                 struct interrupt *irr = irrindex(irrs, l, cpus);
98                 irr->used = 0;
99                 irr->total = 0;
100
101                 words = procfile_linewords(ff, l);
102                 if(!words) continue;
103
104                 irr->id = procfile_lineword(ff, l, 0);
105                 if(!irr->id || !irr->id[0]) continue;
106
107                 int idlen = strlen(irr->id);
108                 if(irr->id[idlen - 1] == ':')
109                         irr->id[idlen - 1] = '\0';
110
111                 int c;
112                 for(c = 0; c < cpus ;c++) {
113                         if((c + 1) < (int)words)
114                                 irr->value[c] = strtoull(procfile_lineword(ff, l, (uint32_t)(c + 1)), NULL, 10);
115                         else
116                                 irr->value[c] = 0;
117
118                         irr->total += irr->value[c];
119                 }
120
121                 strncpyz(irr->name, irr->id, MAX_INTERRUPT_NAME);
122
123                 irr->used = 1;
124         }
125
126         RRDSET *st;
127
128         // --------------------------------------------------------------------
129
130         st = rrdset_find_bytype("system", "softirqs");
131         if(!st) {
132                 st = rrdset_create("system", "softirqs", NULL, "softirqs", NULL, "System softirqs", "softirqs/s", 950, update_every, RRDSET_TYPE_STACKED);
133
134                 for(l = 0; l < lines ;l++) {
135                         struct interrupt *irr = irrindex(irrs, l, cpus);
136                         if(!irr->used) continue;
137                         rrddim_add(st, irr->id, irr->name, 1, 1, RRDDIM_INCREMENTAL);
138                 }
139         }
140         else rrdset_next(st);
141
142         for(l = 0; l < lines ;l++) {
143                 struct interrupt *irr = irrindex(irrs, l, cpus);
144                 if(!irr->used) continue;
145                 rrddim_set(st, irr->id, irr->total);
146         }
147         rrdset_done(st);
148
149         if(do_per_core) {
150                 int c;
151
152                 for(c = 0; c < cpus ; c++) {
153                         char id[256+1];
154                         snprintfz(id, 256, "cpu%d_softirqs", c);
155
156                         st = rrdset_find_bytype("cpu", id);
157                         if(!st) {
158                                 // find if everything is zero
159                                 unsigned long long core_sum = 0 ;
160                                 for(l = 0; l < lines ;l++) {
161                                         struct interrupt *irr = irrindex(irrs, l, cpus);
162                                         if(!irr->used) continue;
163                                         core_sum += irr->value[c];
164                                 }
165                                 if(core_sum == 0) continue; // try next core
166
167                                 char name[256+1], title[256+1];
168                                 snprintfz(name, 256, "cpu%d_softirqs", c);
169                                 snprintfz(title, 256, "CPU%d softirqs", c);
170                                 st = rrdset_create("cpu", id, name, "softirqs", "cpu.softirqs", title, "softirqs/s", 3000 + c, update_every, RRDSET_TYPE_STACKED);
171
172                                 for(l = 0; l < lines ;l++) {
173                                         struct interrupt *irr = irrindex(irrs, l, cpus);
174                                         if(!irr->used) continue;
175                                         rrddim_add(st, irr->id, irr->name, 1, 1, RRDDIM_INCREMENTAL);
176                                 }
177                         }
178                         else rrdset_next(st);
179
180                         for(l = 0; l < lines ;l++) {
181                                 struct interrupt *irr = irrindex(irrs, l, cpus);
182                                 if(!irr->used) continue;
183                                 rrddim_set(st, irr->id, irr->value[c]);
184                         }
185                         rrdset_done(st);
186                 }
187         }
188
189         return 0;
190 }