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