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