master
c 152 lines 5.17 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "plugin_proc.h"
4
5 #define PLUGIN_PROC_MODULE_NET_SOFTNET_NAME "/proc/net/softnet_stat"
6
7 static inline char *softnet_column_name(size_t column) {
8 switch(column) {
9 // https://github.com/torvalds/linux/blob/a7fd20d1c476af4563e66865213474a2f9f473a4/net/core/net-procfs.c#L161-L166
10 case 0: return "processed";
11 case 1: return "dropped";
12 case 2: return "squeezed";
13 case 9: return "received_rps";
14 case 10: return "flow_limit_count";
15 default: return NULL;
16 }
17 }
18
19 int do_proc_net_softnet_stat(int update_every, usec_t dt) {
20 (void)dt;
21
22 static procfile *ff = NULL;
23 static int do_per_core = -1;
24 static size_t allocated_lines = 0, allocated_columns = 0;
25 static uint32_t *data = NULL;
26
27 if (unlikely(do_per_core == -1)) {
28 do_per_core =
29 inicfg_get_boolean(&netdata_config, "plugin:proc:/proc/net/softnet_stat", "softnet_stat per core", CONFIG_BOOLEAN_NO);
30 }
31
32 if(unlikely(!ff)) {
33 char filename[FILENAME_MAX + 1];
34 snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/net/softnet_stat");
35 ff = procfile_open(inicfg_get(&netdata_config, "plugin:proc:/proc/net/softnet_stat", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
36 if(unlikely(!ff)) return 1;
37 }
38
39 ff = procfile_readall(ff);
40 if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time
41
42 size_t lines = procfile_lines(ff), l;
43 size_t words = procfile_linewords(ff, 0), w;
44
45 if(unlikely(!lines || !words)) {
46 collector_error("Cannot read /proc/net/softnet_stat, %zu lines and %zu columns reported.", lines, words);
47 return 1;
48 }
49
50 if(unlikely(lines > 200)) lines = 200;
51 if(unlikely(words > 50)) words = 50;
52
53 if(unlikely(!data || lines > allocated_lines || words > allocated_columns)) {
54 freez(data);
55 allocated_lines = lines;
56 allocated_columns = words;
57 data = mallocz((allocated_lines + 1) * allocated_columns * sizeof(uint32_t));
58 }
59
60 // initialize to zero
61 memset(data, 0, (allocated_lines + 1) * allocated_columns * sizeof(uint32_t));
62
63 // parse the values
64 for(l = 0; l < lines ;l++) {
65 words = procfile_linewords(ff, l);
66 if(unlikely(!words)) continue;
67
68 if(unlikely(words > allocated_columns))
69 words = allocated_columns;
70
71 for(w = 0; w < words ; w++) {
72 if(unlikely(softnet_column_name(w))) {
73 uint32_t t = (uint32_t)strtoul(procfile_lineword(ff, l, w), NULL, 16);
74 data[w] += t;
75 data[((l + 1) * allocated_columns) + w] = t;
76 }
77 }
78 }
79
80 if(unlikely(data[(lines * allocated_columns)] == 0))
81 lines--;
82
83 RRDSET *st;
84
85 // --------------------------------------------------------------------
86
87 st = rrdset_find_active_bytype_localhost("system", "softnet_stat");
88 if(unlikely(!st)) {
89 st = rrdset_create_localhost(
90 "system"
91 , "softnet_stat"
92 , NULL
93 , "softnet_stat"
94 , "system.softnet_stat"
95 , "System softnet_stat"
96 , "events/s"
97 , PLUGIN_PROC_NAME
98 , PLUGIN_PROC_MODULE_NET_SOFTNET_NAME
99 , NETDATA_CHART_PRIO_SYSTEM_SOFTNET_STAT
100 , update_every
101 , RRDSET_TYPE_LINE
102 );
103 for(w = 0; w < allocated_columns ;w++)
104 if(unlikely(softnet_column_name(w)))
105 rrddim_add(st, softnet_column_name(w), NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
106 }
107
108 for(w = 0; w < allocated_columns ;w++)
109 if(unlikely(softnet_column_name(w)))
110 rrddim_set(st, softnet_column_name(w), data[w]);
111
112 rrdset_done(st);
113
114 if(do_per_core) {
115 for(l = 0; l < lines ;l++) {
116 char id[50+1];
117 snprintfz(id, sizeof(id) - 1,"cpu%zu_softnet_stat", l);
118
119 st = rrdset_find_active_bytype_localhost("cpu", id);
120 if(unlikely(!st)) {
121 char title[100+1];
122 snprintfz(title, sizeof(title) - 1, "CPU softnet_stat");
123
124 st = rrdset_create_localhost(
125 "cpu"
126 , id
127 , NULL
128 , "softnet_stat"
129 , "cpu.softnet_stat"
130 , title
131 , "events/s"
132 , PLUGIN_PROC_NAME
133 , PLUGIN_PROC_MODULE_NET_SOFTNET_NAME
134 , NETDATA_CHART_PRIO_SOFTNET_PER_CORE + l
135 , update_every
136 , RRDSET_TYPE_LINE
137 );
138 for(w = 0; w < allocated_columns ;w++)
139 if(unlikely(softnet_column_name(w)))
140 rrddim_add(st, softnet_column_name(w), NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
141 }
142
143 for(w = 0; w < allocated_columns ;w++)
144 if(unlikely(softnet_column_name(w)))
145 rrddim_set(st, softnet_column_name(w), data[((l + 1) * allocated_columns) + w]);
146
147 rrdset_done(st);
148 }
149 }
150
151 return 0;
152 }