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