Created
November 11, 2015 13:43
-
-
Save dberzano/ca3f2cad389af025d9b9 to your computer and use it in GitHub Desktop.
CPU efficiency on Linux with Python
This file contains hidden or 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
#!/usr/bin/env python | |
from time import sleep | |
ncpus = sum([ 1 for x in open("/proc/cpuinfo").read().split("\n") if "bogomips" in x ]) | |
def getup(): | |
tot,idle = open("/proc/uptime").read().split(" ") | |
return float(tot)*ncpus,float(idle) | |
def loop(): | |
tot0,idle0 = getup() | |
while True: | |
sleep(5) | |
tot,idle = getup() | |
dtot = tot-tot0 | |
didle = idle-idle0 | |
eff = ncpus * 100 * (dtot-didle)/dtot | |
eff = eff if eff >= 0 else 0 | |
print "dtot=%.2f didle=%.2f eff=%.2f" % (dtot,didle,eff) | |
tot0 = tot | |
idle0 = idle | |
print "number of cpus: %d" % ncpus | |
loop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment