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