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