Skip to content

Instantly share code, notes, and snippets.

@hashbrowncipher
Created September 29, 2016 08:21
Show Gist options
  • Save hashbrowncipher/5dbc4258a4e9aedec99578f768bbde1e to your computer and use it in GitHub Desktop.
Save hashbrowncipher/5dbc4258a4e9aedec99578f768bbde1e to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from time import sleep
import sys
from pyroute2 import TaskStats
from pyroute2.netlink import NLM_F_REQUEST
from pyroute2.netlink.taskstats import tcmd
from pyroute2.netlink.taskstats import TASKSTATS_CMD_GET
ts = TaskStats()
ts.bind()
def do_request():
return ts.nlm_request(msg, ts.prid, msg_flags=NLM_F_REQUEST)
def delaytop():
msg = tcmd()
msg['cmd'] = TASKSTATS_CMD_GET
msg['version'] = 1
msg['attrs'].append(['TASKSTATS_CMD_ATTR_TGID', sys.argv[1]])
delays = lambda a: (lambda attrs: (attrs['cpu_delay_total'], attrs['cpu_run_real_total']))(a[0]['attrs'][0][1]['attrs'][1][1])
d = delays(do_request())
while True:
sleep(1)
old_d = d
d = delays(do_request())
ran = d[1] - old_d[1]
waited = d[0] - old_d[0]
if ran > 0:
print "{:b}".format(waited * 1024 / ran)
else:
print 0
def watchexits():
ts.register_mask('0-23')
while True:
s = ts.get()
ss = s[0]['attrs'][0][1]['attrs'][1][1]
print((ss['ac_pid'], ss['ac_ppid'], ss['cpu_delay_total'], ss['cpu_run_real_total'], ss['ac_comm']))
watchexits()
ts.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment