Instantly share code, notes, and snippets.

Embed
What would you like to do?
USB Bandwidth logging using usbmon
# See: https://www.kernel.org/doc/Documentation/usb/usbmon.txt
# See: http://people.redhat.com/zaitcev/linux/OLS05_zaitcev.pdf
#
# Emanuele Ruffaldi @ SSSA 2016
import sys,os
def scan(name,device=0):
hdevices = dict()
try:
udev = int(name)
name = "/sys/kernel/debug/usb/usbmon/%du" % udev
except:
pass
print "device time(s) MB packets"
for x in (name == "-" and sys.stdin or open(name,"r")):
q = x.split(" ",6)
if len(q) < 7:
continue
tag,timeus,event,address,status,size,rest = q
times = int(int(timeus)/1E6)
what,bus,dev,ep = address.split(":")
dev = int(dev)
if device != 0 and dev != device:
continue
if what == "Bi" and event == "S":
w = hdevices.get(dev)
if w is None:
last = times
count = 0
packets = 0
else:
last,count,packets = w
if last != times:
print dev,last,count/1E6,packets
count = 0
packets = 0
last = times
count += int(size)
packets += 1
hdevices[dev] = (last,count,packets)
if __name__ == '__main__':
scan(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment