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