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 #ifdef 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"
40 #include "util/u_string.h"
47 #include <sys/types.h>
49 #include <sys/socket.h>
50 #include <sys/ioctl.h>
51 #include <linux/wireless.h>
55 struct list_head list
;
61 char throughput_filename
[128];
63 uint64_t last_nic_bytes
;
66 /* TODO: We don't handle dynamic NIC arrival or removal.
67 * Static globals specific to this HUD category.
69 static int gnic_count
= 0;
70 static struct list_head gnic_list
;
71 static mtx_t gnic_mutex
= _MTX_INITIALIZER_NP
;
73 static struct nic_info
*
74 find_nic_by_name(const char *n
, int mode
)
76 list_for_each_entry(struct nic_info
, nic
, &gnic_list
, list
) {
77 if (nic
->mode
!= mode
)
80 if (strcasecmp(nic
->name
, n
) == 0)
87 get_file_value(const char *fname
, uint64_t *value
)
89 FILE *fh
= fopen(fname
, "r");
92 if (fscanf(fh
, "%" PRIu64
"", value
) != 0) {
100 get_nic_bytes(const char *fn
, uint64_t *bytes
)
102 if (get_file_value(fn
, bytes
) < 0)
109 query_wifi_bitrate(const struct nic_info
*nic
, uint64_t *bitrate
)
112 struct iw_statistics stats
;
115 memset(&stats
, 0, sizeof(stats
));
116 memset(&req
, 0, sizeof(req
));
118 snprintf(req
.ifr_name
, sizeof(req
.ifr_name
), "%s", nic
->name
);
119 req
.u
.data
.pointer
= &stats
;
120 req
.u
.data
.flags
= 1;
121 req
.u
.data
.length
= sizeof(struct iw_statistics
);
123 /* Any old socket will do, and a datagram socket is pretty cheap */
124 if ((sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0)) == -1) {
125 fprintf(stderr
, "Unable to create socket for %s\n", nic
->name
);
129 if (ioctl(sockfd
, SIOCGIWRATE
, &req
) == -1) {
130 fprintf(stderr
, "Error performing SIOCGIWSTATS on %s\n", nic
->name
);
134 *bitrate
= req
.u
.bitrate
.value
;
140 query_nic_rssi(const struct nic_info
*nic
, uint64_t *leveldBm
)
143 struct iw_statistics stats
;
146 memset(&stats
, 0, sizeof(stats
));
147 memset(&req
, 0, sizeof(req
));
149 snprintf(req
.ifr_name
, sizeof(req
.ifr_name
), "%s", nic
->name
);
150 req
.u
.data
.pointer
= &stats
;
151 req
.u
.data
.flags
= 1;
152 req
.u
.data
.length
= sizeof(struct iw_statistics
);
154 if (nic
->mode
!= NIC_RSSI_DBM
)
157 /* Any old socket will do, and a datagram socket is pretty cheap */
158 if ((sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0)) == -1) {
159 fprintf(stderr
, "Unable to create socket for %s\n", nic
->name
);
163 /* Perform the ioctl */
164 if (ioctl(sockfd
, SIOCGIWSTATS
, &req
) == -1) {
165 fprintf(stderr
, "Error performing SIOCGIWSTATS on %s\n", nic
->name
);
169 *leveldBm
= ((char) stats
.qual
.level
* -1);
175 query_nic_load(struct hud_graph
*gr
, struct pipe_context
*pipe
)
177 /* The framework calls us at a regular but indefined period,
178 * not once per second, compensate the statistics accordingly.
181 struct nic_info
*nic
= gr
->query_data
;
182 uint64_t now
= os_time_get();
184 if (nic
->last_time
) {
185 if (nic
->last_time
+ gr
->pane
->period
<= now
) {
187 case NIC_DIRECTION_RX
:
188 case NIC_DIRECTION_TX
:
191 get_nic_bytes(nic
->throughput_filename
, &bytes
);
193 ((bytes
- nic
->last_nic_bytes
) / 1000000) * 8;
195 float speedMbps
= nic
->speedMbps
;
196 float periodMs
= gr
->pane
->period
/ 1000.0;
197 float bits
= nic_mbps
;
198 float period_factor
= periodMs
/ 1000;
199 float period_speed
= speedMbps
* period_factor
;
200 float pct
= (bits
/ period_speed
) * 100;
202 /* Scaling bps with a narrow time period into a second,
203 * potentially suffers from rounding errors at higher
204 * periods. Eg 104%. Compensate.
208 hud_graph_add_value(gr
, (uint64_t) pct
);
210 nic
->last_nic_bytes
= bytes
;
215 uint64_t leveldBm
= 0;
216 query_nic_rssi(nic
, &leveldBm
);
217 hud_graph_add_value(gr
, leveldBm
);
222 nic
->last_time
= now
;
228 case NIC_DIRECTION_RX
:
229 case NIC_DIRECTION_TX
:
230 get_nic_bytes(nic
->throughput_filename
, &nic
->last_nic_bytes
);
236 nic
->last_time
= now
;
241 * Create and initialize a new object for a specific network interface dev.
242 * \param pane parent context.
243 * \param nic_name logical block device name, EG. eth0.
244 * \param mode query type (NIC_DIRECTION_RX/WR/RSSI) statistics.
247 hud_nic_graph_install(struct hud_pane
*pane
, const char *nic_name
,
250 struct hud_graph
*gr
;
251 struct nic_info
*nic
;
253 int num_nics
= hud_get_num_nics(0);
257 nic
= find_nic_by_name(nic_name
, mode
);
261 gr
= CALLOC_STRUCT(hud_graph
);
266 if (nic
->mode
== NIC_DIRECTION_RX
) {
267 snprintf(gr
->name
, sizeof(gr
->name
), "%s-rx-%"PRId64
"Mbps", nic
->name
,
270 else if (nic
->mode
== NIC_DIRECTION_TX
) {
271 snprintf(gr
->name
, sizeof(gr
->name
), "%s-tx-%"PRId64
"Mbps", nic
->name
,
274 else if (nic
->mode
== NIC_RSSI_DBM
)
275 snprintf(gr
->name
, sizeof(gr
->name
), "%s-rssi", nic
->name
);
281 gr
->query_data
= nic
;
282 gr
->query_new_value
= query_nic_load
;
284 hud_pane_add_graph(pane
, gr
);
285 hud_pane_set_max_value(pane
, 100);
289 is_wireless_nic(const char *dirbase
)
291 struct stat stat_buf
;
293 /* Check if its a wireless card */
295 snprintf(fn
, sizeof(fn
), "%s/wireless", dirbase
);
296 if (stat(fn
, &stat_buf
) == 0)
303 query_nic_bitrate(struct nic_info
*nic
, const char *dirbase
)
305 struct stat stat_buf
;
307 /* Check if its a wireless card */
309 snprintf(fn
, sizeof(fn
), "%s/wireless", dirbase
);
310 if (stat(fn
, &stat_buf
) == 0) {
311 /* we're a wireless nic */
312 query_wifi_bitrate(nic
, &nic
->speedMbps
);
313 nic
->speedMbps
/= 1000000;
316 /* Must be a wired nic */
317 snprintf(fn
, sizeof(fn
), "%s/speed", dirbase
);
318 get_file_value(fn
, &nic
->speedMbps
);
323 * Initialize internal object arrays and display NIC HUD help.
324 * \param displayhelp true if the list of detected devices should be
325 displayed on the console.
326 * \return number of detected network interface devices.
329 hud_get_num_nics(bool displayhelp
)
332 struct stat stat_buf
;
333 struct nic_info
*nic
;
336 /* Return the number if network interfaces. */
337 mtx_lock(&gnic_mutex
);
339 mtx_unlock(&gnic_mutex
);
343 /* Scan /sys/block, for every object type we support, create and
344 * persist an object to represent its different statistics.
346 list_inithead(&gnic_list
);
347 DIR *dir
= opendir("/sys/class/net/");
349 mtx_unlock(&gnic_mutex
);
353 while ((dp
= readdir(dir
)) != NULL
) {
355 /* Avoid 'lo' and '..' and '.' */
356 if (strlen(dp
->d_name
) <= 2)
360 snprintf(basename
, sizeof(basename
), "/sys/class/net/%s", dp
->d_name
);
361 snprintf(name
, sizeof(name
), "%s/statistics/rx_bytes", basename
);
362 if (stat(name
, &stat_buf
) < 0)
365 if (!S_ISREG(stat_buf
.st_mode
))
366 continue; /* Not a regular file */
368 int is_wireless
= is_wireless_nic(basename
);
370 /* Add the RX object */
371 nic
= CALLOC_STRUCT(nic_info
);
372 strcpy(nic
->name
, dp
->d_name
);
373 snprintf(nic
->throughput_filename
, sizeof(nic
->throughput_filename
),
374 "%s/statistics/rx_bytes", basename
);
375 nic
->mode
= NIC_DIRECTION_RX
;
376 nic
->is_wireless
= is_wireless
;
377 query_nic_bitrate(nic
, basename
);
379 list_addtail(&nic
->list
, &gnic_list
);
382 /* Add the TX object */
383 nic
= CALLOC_STRUCT(nic_info
);
384 strcpy(nic
->name
, dp
->d_name
);
385 snprintf(nic
->throughput_filename
,
386 sizeof(nic
->throughput_filename
),
387 "/sys/class/net/%s/statistics/tx_bytes", dp
->d_name
);
388 nic
->mode
= NIC_DIRECTION_TX
;
389 nic
->is_wireless
= is_wireless
;
391 query_nic_bitrate(nic
, basename
);
393 list_addtail(&nic
->list
, &gnic_list
);
396 if (nic
->is_wireless
) {
398 nic
= CALLOC_STRUCT(nic_info
);
399 strcpy(nic
->name
, dp
->d_name
);
400 snprintf(nic
->throughput_filename
,
401 sizeof(nic
->throughput_filename
),
402 "/sys/class/net/%s/statistics/tx_bytes", dp
->d_name
);
403 nic
->mode
= NIC_RSSI_DBM
;
405 query_nic_bitrate(nic
, basename
);
407 list_addtail(&nic
->list
, &gnic_list
);
414 list_for_each_entry(struct nic_info
, nic
, &gnic_list
, list
) {
416 snprintf(line
, sizeof(line
), " nic-%s-%s",
417 nic
->mode
== NIC_DIRECTION_RX
? "rx" :
418 nic
->mode
== NIC_DIRECTION_TX
? "tx" :
419 nic
->mode
== NIC_RSSI_DBM
? "rssi" : "undefined", nic
->name
);
425 mtx_unlock(&gnic_mutex
);
429 #endif /* HAVE_GALLIUM_EXTRA_HUD */