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