]> arthur.barton.de Git - netdata.git/blob - src/proc_net_softnet_stat.c
properly initialize softnet_stat
[netdata.git] / src / proc_net_softnet_stat.c
1 #include "common.h"
2
3 static inline char *softnet_column_name(uint32_t column) {
4     static char buf[4] = "c00";
5     char *s;
6
7     switch(column) {
8         case 0: s = "total"; break;
9         case 1: s = "dropped"; break;
10         case 2: s = "squeezed"; break;
11         case 8: s = "collisions"; break;
12         default: {
13             uint32_t c = column + 1;
14             buf[1] = '0' + ( c / 10);   c = c % 10;
15             buf[2] = '0' + c;
16             s = buf;
17             break;
18         }
19     }
20
21     return s;
22 }
23
24 int do_proc_net_softnet_stat(int update_every, unsigned long long dt) {
25     (void)dt;
26
27     static procfile *ff = NULL;
28     static int do_per_core = -1;
29     static uint32_t allocated_lines = 0, allocated_columns = 0, *data = NULL;
30
31     if(do_per_core == -1) do_per_core = config_get_boolean("plugin:proc:/proc/net/softnet_stat", "softnet_stat per core", 1);
32
33     if(!ff) {
34         char filename[FILENAME_MAX + 1];
35         snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/net/softnet_stat");
36         ff = procfile_open(config_get("plugin:proc:/proc/net/softnet_stat", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
37     }
38     if(!ff) return 1;
39
40     ff = procfile_readall(ff);
41     if(!ff) return 0; // we return 0, so that we will retry to open it next time
42
43     uint32_t lines = procfile_lines(ff), l;
44     uint32_t words = procfile_linewords(ff, 0), w;
45
46     if(!lines || !words) {
47         error("Cannot read /proc/net/softnet_stat, %u lines and %u columns reported.", lines, words);
48         return 1;
49     }
50
51     if(lines > 200) lines = 200;
52     if(words > 50) words = 50;
53     
54     if(unlikely(!data || lines > allocated_lines || words > allocated_columns)) {
55         freez(data);
56         allocated_lines = lines;
57         allocated_columns = words;
58         data = mallocz((allocated_lines + 1) * allocated_columns * sizeof(uint32_t));
59     }
60     
61     // initialize to zero
62     bzero(data, (allocated_lines + 1) * allocated_columns * sizeof(uint32_t));
63
64     // parse the values
65     for(l = 0; l < lines ;l++) {
66         words = procfile_linewords(ff, l);
67         if(!words) continue;
68
69         if(words > allocated_columns) words = allocated_columns;
70
71         for(w = 0; w < words ; w++) {
72             uint32_t t = strtoul(procfile_lineword(ff, l, w), NULL, 16);
73             data[w] += t;
74             data[((l + 1) * allocated_columns) + w] = t;
75         }
76     }
77
78     if(data[(lines * allocated_columns)] == 0)
79         lines--;
80
81     RRDSET *st;
82
83     // --------------------------------------------------------------------
84
85     st = rrdset_find_bytype("system", "softnet_stat");
86     if(!st) {
87         st = rrdset_create("system", "softnet_stat", NULL, "softnet_stat", NULL, "System softnet_stat", "events/s", 955, update_every, RRDSET_TYPE_LINE);
88         for(w = 0; w < allocated_columns ;w++)
89             rrddim_add(st, softnet_column_name(w), NULL, 1, 1, RRDDIM_INCREMENTAL);
90     }
91     else rrdset_next(st);
92
93     for(w = 0; w < allocated_columns ;w++)
94         rrddim_set(st, softnet_column_name(w), data[w]);
95
96     rrdset_done(st);
97
98     if(do_per_core) {
99         for(l = 0; l < lines ;l++) {
100             char id[50+1];
101             snprintfz(id, 50, "cpu%d_softnet_stat", l);
102
103             st = rrdset_find_bytype("cpu", id);
104             if(!st) {
105                 char title[100+1];
106                 snprintfz(title, 100, "CPU%d softnet_stat", l);
107
108                 st = rrdset_create("cpu", id, NULL, "softnet_stat", NULL, title, "events/s", 4101 + l, update_every, RRDSET_TYPE_LINE);
109                 for(w = 0; w < allocated_columns ;w++)
110                     rrddim_add(st, softnet_column_name(w), NULL, 1, 1, RRDDIM_INCREMENTAL);
111             }
112             else rrdset_next(st);
113
114             for(w = 0; w < allocated_columns ;w++)
115                 rrddim_set(st, softnet_column_name(w), data[((l + 1) * allocated_columns) + w]);
116
117             rrdset_done(st);
118         }
119     }
120
121     return 0;
122 }