from __future__ import division import sys, os, time def getCpuTimes(): cpu0 = 0 cpu1 = 1 for line in open("/proc/stat"): tokens = line.split() if tokens[0] == "cpu0": cpu0 = int(tokens[1]) + int(tokens[3]) elif tokens[0] == "cpu1": cpu1 = int(tokens[1]) + int(tokens[3]) return cpu0, cpu1 def measureCpuImbalance(workload): baseline = getCpuTimes() workload() final = getCpuTimes() return (final[0] - baseline[0]) / (final[1] - baseline[1]) def myWorkload(): pid = os.spawnl(os.P_NOWAIT, "./priosched") time.sleep(0.5) os.kill(pid, 9) os.wait() while True: print "%.04f" % measureCpuImbalance(myWorkload)