1 /**************************************************************************
3 * Copyright (C) 2016 Steven Toth <stoth@kernellabs.com>
4 * Copyright (C) 2016 Zodiac Inflight Innovations
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
29 #if HAVE_GALLIUM_EXTRA_HUD
31 /* Purpose: Reading network interface RX/TX throughput per second,
32 * displaying on the HUD.
35 #include "hud/hud_private.h"
36 #include "util/list.h"
37 #include "util/os_time.h"
38 #include "os/os_thread.h"
39 #include "util/u_memory.h"
46 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <sys/ioctl.h>
50 #include <linux/wireless.h>
54 struct list_head list
;
60 char throughput_filename
[128];
62 uint64_t last_nic_bytes
;
65 /* TODO: We don't handle dynamic NIC arrival or removal.
66 * Static globals specific to this HUD category.
68 static int gnic_count
= 0;
69 static struct list_head gnic_list
;
70 static mtx_t gnic_mutex
= _MTX_INITIALIZER_NP
;
72 static struct nic_info
*
73 find_nic_by_name(const char *n
, int mode
)
75 list_for_each_entry(struct nic_info
, nic
, &gnic_list
, list
) {
76 if (nic
->mode
!= mode
)
79 if (strcasecmp(nic
->name
, n
) == 0)
86 get_file_value(const char *fname
, uint64_t *value
)
88 FILE *fh
= fopen(fname
, "r");
91 if (fscanf(fh
, "%" PRIu64
"", value
) != 0) {
99 get_nic_bytes(const char *fn
, uint64_t *bytes
)
101 if (get_file_value(fn
, bytes
) < 0)
108 query_wifi_bitrate(const struct nic_info
*nic
, uint64_t *bitrate
)
111 struct iw_statistics stats
;
114 memset(&stats
, 0, sizeof(stats
));
115 memset(&req
, 0, sizeof(req
));
117 strcpy(req
.ifr_name
, nic
->name
);
118 req
.u
.data
.pointer
= &stats
;
119 req
.u
.data
.flags
= 1;
120 req
.u
.data
.length
= sizeof(struct iw_statistics
);
122 /* Any old socket will do, and a datagram socket is pretty cheap */
123 if ((sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0)) == -1) {
124 fprintf(stderr
, "Unable to create socket for %s\n", nic
->name
);
128 if (ioctl(sockfd
, SIOCGIWRATE
, &req
) == -1) {
129 fprintf(stderr
, "Error performing SIOCGIWSTATS on %s\n", nic
->name
);
133 *bitrate
= req
.u
.bitrate
.value
;
139 query_nic_rssi(const struct nic_info
*nic
, uint64_t *leveldBm
)
142 struct iw_statistics stats
;
145 memset(&stats
, 0, sizeof(stats
));
146 memset(&req
, 0, sizeof(req
));
148 strcpy(req
.ifr_name
, nic
->name
);
149 req
.u
.data
.pointer
= &stats
;
150 req
.u
.data
.flags
= 1;
151 req
.u
.data
.length
= sizeof(struct iw_statistics
);
153 if (nic
->mode
!= NIC_RSSI_DBM
)
156 /* Any old socket will do, and a datagram socket is pretty cheap */
157 if ((sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0)) == -1) {
158 fprintf(stderr
, "Unable to create socket for %s\n", nic
->name
);
162 /* Perform the ioctl */
163 if (ioctl(sockfd
, SIOCGIWSTATS
, &req
) == -1) {
164 fprintf(stderr
, "Error performing SIOCGIWSTATS on %s\n", nic
->name
);
168 *leveldBm
= ((char) stats
.qual
.level
* -1);
174 query_nic_load(struct hud_graph
*gr
)
176 /* The framework calls us at a regular but indefined period,
177 * not once per second, compensate the statistics accordingly.
180 struct nic_info
*nic
= gr
->query_data
;
181 uint64_t now
= os_time_get();
183 if (nic
->last_time
) {
184 if (nic
->last_time
+ gr
->pane
->period
<= now
) {
186 case NIC_DIRECTION_RX
:
187 case NIC_DIRECTION_TX
:
190 get_nic_bytes(nic
->throughput_filename
, &bytes
);
192 ((bytes
- nic
->last_nic_bytes
) / 1000000) * 8;
194 float speedMbps
= nic
->speedMbps
;
195 float periodMs
= gr
->pane
->period
/ 1000;
196 float bits
= nic_mbps
;
197 float period_factor
= periodMs
/ 1000;
198 float period_speed
= speedMbps
* period_factor
;
199 float pct
= (bits
/ period_speed
) * 100;
201 /* Scaling bps with a narrow time period into a second,
202 * potentially suffers from routing errors at higher
203 * periods. Eg 104%. Compensate.
207 hud_graph_add_value(gr
, (uint64_t) pct
);
209 nic
->last_nic_bytes
= bytes
;
214 uint64_t leveldBm
= 0;
215 query_nic_rssi(nic
, &leveldBm
);
216 hud_graph_add_value(gr
, leveldBm
);
221 nic
->last_time
= now
;
227 case NIC_DIRECTION_RX
:
228 case NIC_DIRECTION_TX
:
229 get_nic_bytes(nic
->throughput_filename
, &nic
->last_nic_bytes
);
235 nic
->last_time
= now
;
240 * Create and initialize a new object for a specific network interface dev.
241 * \param pane parent context.
242 * \param nic_name logical block device name, EG. eth0.
243 * \param mode query type (NIC_DIRECTION_RX/WR/RSSI) statistics.
246 hud_nic_graph_install(struct hud_pane
*pane
, const char *nic_name
,
249 struct hud_graph
*gr
;
250 struct nic_info
*nic
;
252 int num_nics
= hud_get_num_nics(0);
256 nic
= find_nic_by_name(nic_name
, mode
);
260 gr
= CALLOC_STRUCT(hud_graph
);
265 if (nic
->mode
== NIC_DIRECTION_RX
) {
266 snprintf(gr
->name
, sizeof(gr
->name
), "%s-rx-%"PRId64
"Mbps", nic
->name
,
269 else if (nic
->mode
== NIC_DIRECTION_TX
) {
270 snprintf(gr
->name
, sizeof(gr
->name
), "%s-tx-%"PRId64
"Mbps", nic
->name
,
273 else if (nic
->mode
== NIC_RSSI_DBM
)
274 snprintf(gr
->name
, sizeof(gr
->name
), "%s-rssi", nic
->name
);
278 gr
->query_data
= nic
;
279 gr
->query_new_value
= query_nic_load
;
281 hud_pane_add_graph(pane
, gr
);
282 hud_pane_set_max_value(pane
, 100);
286 is_wireless_nic(const char *dirbase
)
288 struct stat stat_buf
;
290 /* Check if its a wireless card */
292 snprintf(fn
, sizeof(fn
), "%s/wireless", dirbase
);
293 if (stat(fn
, &stat_buf
) == 0)
300 query_nic_bitrate(struct nic_info
*nic
, const char *dirbase
)
302 struct stat stat_buf
;
304 /* Check if its a wireless card */
306 snprintf(fn
, sizeof(fn
), "%s/wireless", dirbase
);
307 if (stat(fn
, &stat_buf
) == 0) {
308 /* we're a wireless nic */
309 query_wifi_bitrate(nic
, &nic
->speedMbps
);
310 nic
->speedMbps
/= 1000000;
313 /* Must be a wired nic */
314 snprintf(fn
, sizeof(fn
), "%s/speed", dirbase
);
315 get_file_value(fn
, &nic
->speedMbps
);
320 * Initialize internal object arrays and display NIC HUD help.
321 * \param displayhelp true if the list of detected devices should be
322 displayed on the console.
323 * \return number of detected network interface devices.
326 hud_get_num_nics(bool displayhelp
)
329 struct stat stat_buf
;
330 struct nic_info
*nic
;
333 /* Return the number if network interfaces. */
334 mtx_lock(&gnic_mutex
);
336 mtx_unlock(&gnic_mutex
);
340 /* Scan /sys/block, for every object type we support, create and
341 * persist an object to represent its different statistics.
343 list_inithead(&gnic_list
);
344 DIR *dir
= opendir("/sys/class/net/");
346 mtx_unlock(&gnic_mutex
);
350 while ((dp
= readdir(dir
)) != NULL
) {
352 /* Avoid 'lo' and '..' and '.' */
353 if (strlen(dp
->d_name
) <= 2)
357 snprintf(basename
, sizeof(basename
), "/sys/class/net/%s", dp
->d_name
);
358 snprintf(name
, sizeof(name
), "%s/statistics/rx_bytes", basename
);
359 if (stat(name
, &stat_buf
) < 0)
362 if (!S_ISREG(stat_buf
.st_mode
))
363 continue; /* Not a regular file */
365 int is_wireless
= is_wireless_nic(basename
);
367 /* Add the RX object */
368 nic
= CALLOC_STRUCT(nic_info
);
369 strcpy(nic
->name
, dp
->d_name
);
370 snprintf(nic
->throughput_filename
, sizeof(nic
->throughput_filename
),
371 "%s/statistics/rx_bytes", basename
);
372 nic
->mode
= NIC_DIRECTION_RX
;
373 nic
->is_wireless
= is_wireless
;
374 query_nic_bitrate(nic
, basename
);
376 list_addtail(&nic
->list
, &gnic_list
);
379 /* Add the TX object */
380 nic
= CALLOC_STRUCT(nic_info
);
381 strcpy(nic
->name
, dp
->d_name
);
382 snprintf(nic
->throughput_filename
,
383 sizeof(nic
->throughput_filename
),
384 "/sys/class/net/%s/statistics/tx_bytes", dp
->d_name
);
385 nic
->mode
= NIC_DIRECTION_TX
;
386 nic
->is_wireless
= is_wireless
;
388 query_nic_bitrate(nic
, basename
);
390 list_addtail(&nic
->list
, &gnic_list
);
393 if (nic
->is_wireless
) {
395 nic
= CALLOC_STRUCT(nic_info
);
396 strcpy(nic
->name
, dp
->d_name
);
397 snprintf(nic
->throughput_filename
,
398 sizeof(nic
->throughput_filename
),
399 "/sys/class/net/%s/statistics/tx_bytes", dp
->d_name
);
400 nic
->mode
= NIC_RSSI_DBM
;
402 query_nic_bitrate(nic
, basename
);
404 list_addtail(&nic
->list
, &gnic_list
);
411 list_for_each_entry(struct nic_info
, nic
, &gnic_list
, list
) {
413 snprintf(line
, sizeof(line
), " nic-%s-%s",
414 nic
->mode
== NIC_DIRECTION_RX
? "rx" :
415 nic
->mode
== NIC_DIRECTION_TX
? "tx" :
416 nic
->mode
== NIC_RSSI_DBM
? "rssi" : "undefined", nic
->name
);
422 mtx_unlock(&gnic_mutex
);
426 #endif /* HAVE_GALLIUM_EXTRA_HUD */