def yield_taskstats(processes): ts = TaskStats() ts.bind() try: for name, pid in processes: results = do_query(ts, pid) for key in interesting_taskstats_keys: value = results[key] yield 'taskstat_{}{{name="{}", pid="{}"}} {}'.format( key, name, pid, value) finally: ts.close()
class _TestTaskStats(object): def setup(self): self.ts = TaskStats() self.ts.bind() def teardown(self): self.ts.close() def test_get_pid_stat(self): require_user('root') ret = self.ts.get_pid_stat(1)[0] assert ret.get_attr('TASKSTATS_TYPE_AGGR_PID').\ get_attr('TASKSTATS_TYPE_PID') == 1
''' Simple taskstats sample. ''' import os from pyroute2 import TaskStats from pyroute2.common import hexdump pid = os.getpid() ts = TaskStats() # bind is required in the case of generic netlink ts.bind() ret = ts.get_pid_stat(int(pid))[0] # raw hex structure to check alignment print(hexdump(ret.raw)) # parsed structure print(ret) ts.close()