Created
September 2, 2016 12:12
-
-
Save eruffaldi/e84d2a0b1990c258cd22a3c20f5b80a6 to your computer and use it in GitHub Desktop.
USB Bandwidth logging using usbmon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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