This is my personal blog. The views expressed on these pages are mine alone and not those of my employer.
Measuring USB with bpftrace
File usb-bw.b
:
#include <linux/usb.h>
interval:s:1 {
printf("--------------------------\n");
print(@total);
print(@sum);
clear(@sum);
clear(@total);
}
kprobe:__usb_hcd_giveback_urb {
$urb = (struct urb*)arg0;
$dev = $urb->dev;
@total = stats((uint64)$urb->actual_length);
@sum[$dev->descriptor.idVendor,
$dev->descriptor.idProduct,
str($dev->product),
str($dev->manufacturer)] = stats((uint64)$urb->actual_length);
}
Example run with a USB stick idling (appears to be probed once every two seconds), and starting and stopping some GNURadio sniffing with an USRP B200 at 10Msps:
$ sudo bpftrace usb-bw.b
Attaching 2 probes...
--------------------------
@total: count 317, average 20, total 6641
@sum[9472, 32, USRP B200, Ettus Research LLC]: count 315, average 20, total 6597
@sum[4871, 357, USB Mass Storage Devie, USBest Technology]: count 2, average 22, total 44
--------------------------
@total: count 6807, average 20, total 136552
@sum[9472, 32, USRP B200, Ettus Research LLC]: count 6807, average 20, total 136552
--------------------------
@total: count 8507, average 20, total 170852
@sum[9472, 32, USRP B200, Ettus Research LLC]: count 8505, average 20, total 170808
@sum[4871, 357, USB Mass Storage Devie, USBest Technology]: count 2, average 22, total 44
--------------------------
@total: count 979, average 20, total 20288
@sum[9472, 32, USRP B200, Ettus Research LLC]: count 979, average 20, total 20288
--------------------------
@total: count 2141, average 7319, total 15670428
@sum[4871, 357, USB Mass Storage Devie, USBest Technology]: count 2, average 22, total 44
@sum[9472, 32, USRP B200, Ettus Research LLC]: count 2140, average 7326, total 15678560
--------------------------
@total: count 5077, average 7891, total 40066648
@sum[9472, 32, USRP B200, Ettus Research LLC]: count 5078, average 7890, total 40066648
--------------------------
@total: count 5080, average 7888, total 40074868
@sum[4871, 357, USB Mass Storage Devie, USBest Technology]: count 2, average 22, total 44
@sum[9472, 32, USRP B200, Ettus Research LLC]: count 5079, average 7890, total 40074824
--------------------------
@total: count 5077, average 7891, total 40066648
@sum[9472, 32, USRP B200, Ettus Research LLC]: count 5077, average 7891, total 40066648
--------------------------
@total: count 2456, average 8009, total 19670524
@sum[4871, 357, USB Mass Storage Devie, USBest Technology]: count 2, average 22, total 44
@sum[9472, 32, USRP B200, Ettus Research LLC]: count 2455, average 8015, total 19678656
--------------------------
--------------------------
@total: count 2, average 22, total 44
@sum[4871, 357, USB Mass Storage Devie, USBest Technology]: count 2, average 22, total 44
It’s not the prettiest, but it’s interesting. Note that the Product and Vendor IDs are in decimal. I couldn’t find a way to convert them to hex in bpftrace.