Debian Codegraph @

src/libsysprof/sysprof-network-usage.c

raw
1 /* sysprof-network-usage.c
2 *
3 * Copyright 2023 Christian Hergert <[email protected]>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21 #include "config.h"
22
23 #include <ctype.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30
31 #include <glib-unix.h>
32 #include <glib/gstdio.h>
33
34 #include "line-reader-private.h"
35
36 #include "sysprof-network-usage.h"
37 #include "sysprof-instrument-private.h"
38 #include "sysprof-recording-private.h"
39
40 struct _SysprofNetworkUsage
41 {
42 SysprofInstrument parent_instance;
43 };
44
45 struct _SysprofNetworkUsageClass
46 {
47 SysprofInstrumentClass parent_class;
48 };
49
50 typedef struct _DeviceUsage
51 {
52 /* Counter IDs */
53 guint rx_bytes_id;
54 guint tx_bytes_id;
55 gchar iface[32];
56 gint64 rx_bytes;
57 gint64 rx_packets;
58 gint64 rx_errors;
59 gint64 rx_dropped;
60 gint64 rx_fifo;
61 gint64 rx_frame;
62 gint64 rx_compressed;
63 gint64 rx_multicast;
64 gint64 tx_bytes;
65 gint64 tx_packets;
66 gint64 tx_errors;
67 gint64 tx_dropped;
68 gint64 tx_fifo;
69 gint64 tx_collisions;
70 gint64 tx_carrier;
71 gint64 tx_compressed;
72 } DeviceUsage;
73
74 G_DEFINE_FINAL_TYPE (SysprofNetworkUsage, sysprof_network_usage, SYSPROF_TYPE_INSTRUMENT)
75
76 typedef struct _Record
77 {
78 SysprofRecording *recording;
79 DexFuture *cancellable;
80 } Record;
81
82 static void
83 record_free (gpointer data)
84 {
85 Record *record = data;
86
87 g_clear_object (&record->recording);
88 dex_clear (&record->cancellable);
89 g_free (record);
90 }
91
92 static DeviceUsage *
93 find_device_by_name (GArray *ar,
94 const char *name)
95 {
96 for (guint i = 0; i < ar->len; i++)
97 {
98 DeviceUsage *dev = &g_array_index (ar, DeviceUsage, i);
99
100 if (g_strcmp0 (name, dev->iface) == 0)
101 return dev;
102 }
103
104 return NULL;
105 }
106
107 static DexFuture *
108 sysprof_network_usage_record_fiber (gpointer user_data)
109 {
110 g_autoptr(GByteArray) buf = g_byte_array_new ();
111 Record *record = user_data;
112 g_autofree SysprofCaptureCounterValue *values = NULL;
113 g_autofree guint *ids = NULL;
114 g_autoptr(GArray) devices = NULL;
115 g_autoptr(GError) error = NULL;
116 SysprofCaptureWriter *writer;
117 SysprofCaptureCounter ctr[2] = {0};
118 g_autofd int stat_fd = -1;
119 LineReader reader;
120 guint combined_rx_id;
121 guint combined_tx_id;
122 gssize n_read;
123 gsize line_len;
124 guint lineno;
125 char *line;
126
127 g_assert (record != NULL);
128 g_assert (SYSPROF_IS_RECORDING (record->recording));
129 g_assert (DEX_IS_FUTURE (record->cancellable));
130
131 g_byte_array_set_size (buf, 4096*2);
132
133 writer = _sysprof_recording_writer (record->recording);
134
135 if (-1 == (stat_fd = open ("/proc/net/dev", O_RDONLY|O_CLOEXEC)))
136 return dex_future_new_for_errno (errno);
137
138 devices = g_array_new (FALSE, FALSE, sizeof (DeviceUsage));
139
140 combined_rx_id = sysprof_capture_writer_request_counter (writer, 1);
141 combined_tx_id = sysprof_capture_writer_request_counter (writer, 1);
142
143 g_strlcpy (ctr[0].category, "Network", sizeof ctr[0].category);
144 g_strlcpy (ctr[0].name, "RX Bytes", sizeof ctr[0].name);
145 g_strlcpy (ctr[0].description, "Combined", sizeof ctr[0].description);
146 ctr[0].id = combined_rx_id;
147 ctr[0].type = SYSPROF_CAPTURE_COUNTER_INT64;
148 ctr[0].value.v64 = 0;
149
150 g_strlcpy (ctr[1].category, "Network", sizeof ctr[1].category);
151 g_strlcpy (ctr[1].name, "TX Bytes", sizeof ctr[1].name);
152 g_strlcpy (ctr[1].description, "Combined", sizeof ctr[1].description);
153 ctr[1].id = combined_tx_id;
154 ctr[1].type = SYSPROF_CAPTURE_COUNTER_INT64;
155 ctr[1].value.v64 = 0;
156
157 sysprof_capture_writer_define_counters (writer,
158 SYSPROF_CAPTURE_CURRENT_TIME,
159 -1,
160 -1,
161 ctr, G_N_ELEMENTS (ctr));
162
163 n_read = dex_await_int64 (dex_aio_read (NULL, stat_fd, buf->data, buf->len, 0), &error);
164 if (n_read <= 0)
165 return dex_future_new_for_errno (errno);
166
167 lineno = 0;
168 line_reader_init (&reader, (char *)buf->data, n_read);
169 while ((line = line_reader_next (&reader, &line_len)))
170 {
171 DeviceUsage dev = {0};
172 g_autofree char *rx = NULL;
173 g_autofree char *tx = NULL;
174 char *ptr = line;
175 char *name;
176
177 line[line_len] = 0;
178
179 if (lineno++ < 2)
180 continue;
181
182 for (; *ptr && g_ascii_isspace (*ptr); ptr++) { /* Do Nothing */ }
183 name = ptr;
184 for (; *ptr && *ptr != ':'; ptr++) { /* Do Nothing */ }
185 *ptr = 0;
186
187 rx = g_strdup_printf ("RX Bytes (%s)", name);
188 tx = g_strdup_printf ("TX Bytes (%s)", name);
189
190 g_strlcpy (ctr[0].category, "Network", sizeof ctr[0].category);
191 g_strlcpy (ctr[0].name, rx, sizeof ctr[0].name);
192 g_strlcpy (ctr[0].description, name, sizeof ctr[0].description);
193 ctr[0].id = sysprof_capture_writer_request_counter (writer, 1);
194 ctr[0].type = SYSPROF_CAPTURE_COUNTER_INT64;
195 ctr[0].value.v64 = 0;
196
197 g_strlcpy (ctr[1].category, "Network", sizeof ctr[1].category);
198 g_strlcpy (ctr[1].name, tx, sizeof ctr[1].name);
199 g_strlcpy (ctr[1].description, name, sizeof ctr[1].description);
200 ctr[1].id = sysprof_capture_writer_request_counter (writer, 1);
201 ctr[1].type = SYSPROF_CAPTURE_COUNTER_INT64;
202 ctr[1].value.v64 = 0;
203
204 sysprof_capture_writer_define_counters (writer,
205 SYSPROF_CAPTURE_CURRENT_TIME,
206 -1,
207 -1,
208 ctr, G_N_ELEMENTS (ctr));
209
210 dev.rx_bytes_id = ctr[0].id;
211 dev.tx_bytes_id = ctr[1].id;
212 g_strlcpy (dev.iface, name, sizeof dev.iface);
213
214 g_array_append_val (devices, dev);
215 }
216
217 values = g_new0 (SysprofCaptureCounterValue, (devices->len*2) + 2);
218 ids = g_new0 (guint, (devices->len*2) + 2);
219 ids[0] = combined_rx_id;
220 ids[1] = combined_tx_id;
221 for (guint i = 0; i < devices->len; i++)
222 {
223 const DeviceUsage *dev = &g_array_index (devices, DeviceUsage, i);
224 ids[(i+1)*2] = dev->rx_bytes_id;
225 ids[(i+1)*2+1] = dev->tx_bytes_id;
226 }
227
228 for (;;)
229 {
230 DeviceUsage *first = &g_array_index (devices, DeviceUsage, 0);
231 gint64 combined_rx = 0;
232 gint64 combined_tx = 0;
233
234 n_read = dex_await_int64 (dex_aio_read (NULL, stat_fd, buf->data, buf->len, 0), &error);
235 if (n_read <= 0)
236 break;
237
238 lineno = 0;
239 line_reader_init (&reader, (char *)buf->data, n_read);
240 while ((line = line_reader_next (&reader, &line_len)))
241 {
242 DeviceUsage *dev;
243 guint index;
244 char *name;
245 char *ptr = line;
246
247 line[line_len] = 0;
248
249 if (lineno++ < 2)
250 continue;
251
252 for (; *ptr && g_ascii_isspace (*ptr); ptr++) { /* Do Nothing */ }
253 name = ptr;
254 for (; *ptr && *ptr != ':'; ptr++) { /* Do Nothing */ }
255 *ptr = 0;
256
257 if (!(dev = find_device_by_name (devices, name)))
258 continue;
259
260 ptr++;
261
262 sscanf (ptr,
263 "%"G_GINT64_FORMAT
264 " %"G_GINT64_FORMAT
265 " %"G_GINT64_FORMAT
266 " %"G_GINT64_FORMAT
267 " %"G_GINT64_FORMAT
268 " %"G_GINT64_FORMAT
269 " %"G_GINT64_FORMAT
270 " %"G_GINT64_FORMAT
271 " %"G_GINT64_FORMAT
272 " %"G_GINT64_FORMAT
273 " %"G_GINT64_FORMAT
274 " %"G_GINT64_FORMAT
275 " %"G_GINT64_FORMAT
276 " %"G_GINT64_FORMAT
277 " %"G_GINT64_FORMAT
278 " %"G_GINT64_FORMAT,
279 &dev->rx_bytes,
280 &dev->rx_packets,
281 &dev->rx_errors,
282 &dev->rx_dropped,
283 &dev->rx_fifo,
284 &dev->rx_frame,
285 &dev->rx_compressed,
286 &dev->rx_multicast,
287 &dev->tx_bytes,
288 &dev->tx_packets,
289 &dev->tx_errors,
290 &dev->tx_dropped,
291 &dev->tx_fifo,
292 &dev->tx_collisions,
293 &dev->tx_carrier,
294 &dev->tx_compressed);
295
296 combined_rx += dev->rx_bytes;
297 combined_tx += dev->tx_bytes;
298
299 index = dev - first;
300
301 values[(index+1)*2].v64 = dev->rx_bytes;
302 values[(index+1)*2+1].v64 = dev->tx_bytes;
303 }
304
305 values[0].v64 = combined_rx;
306 values[1].v64 = combined_tx;
307
308 sysprof_capture_writer_set_counters (writer,
309 SYSPROF_CAPTURE_CURRENT_TIME,
310 -1,
311 -1,
312 ids,
313 values,
314 (devices->len + 1) * 2);
315
316 /* Wait for cancellation or ½ second */
317 dex_await (dex_future_first (dex_ref (record->cancellable),
318 dex_timeout_new_usec (G_USEC_PER_SEC / 2),
319 NULL),
320 NULL);
321 if (dex_future_get_status (record->cancellable) != DEX_FUTURE_STATUS_PENDING)
322 break;
323 }
324
325 return dex_future_new_for_boolean (TRUE);
326 }
327
328 static DexFuture *
329 sysprof_network_usage_record (SysprofInstrument *instrument,
330 SysprofRecording *recording,
331 GCancellable *cancellable)
332 {
333 Record *record;
334
335 g_assert (SYSPROF_IS_NETWORK_USAGE (instrument));
336 g_assert (SYSPROF_IS_RECORDING (recording));
337 g_assert (G_IS_CANCELLABLE (cancellable));
338
339 record = g_new0 (Record, 1);
340 record->recording = g_object_ref (recording);
341 record->cancellable = dex_cancellable_new_from_cancellable (cancellable);
342
343 return dex_scheduler_spawn (NULL, 0,
344 sysprof_network_usage_record_fiber,
345 record,
346 record_free);
347 }
348
349 static void
350 sysprof_network_usage_class_init (SysprofNetworkUsageClass *klass)
351 {
352 SysprofInstrumentClass *instrument_class = SYSPROF_INSTRUMENT_CLASS (klass);
353
354 instrument_class->record = sysprof_network_usage_record;
355 }
356
357 static void
358 sysprof_network_usage_init (SysprofNetworkUsage *self)
359 {
360 }
361
362 SysprofInstrument *
363 sysprof_network_usage_new (void)
364 {
365 return g_object_new (SYSPROF_TYPE_NETWORK_USAGE, NULL);
366 }