Skip to content

Instantly share code, notes, and snippets.

@eruffaldi
Created September 2, 2016 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save eruffaldi/e84d2a0b1990c258cd22a3c20f5b80a6 to your computer and use it in GitHub Desktop.
Save eruffaldi/e84d2a0b1990c258cd22a3c20f5b80a6 to your computer and use it in GitHub Desktop.
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