Skip to content

Instantly share code, notes, and snippets.

@dberzano
Created November 11, 2015 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dberzano/ca3f2cad389af025d9b9 to your computer and use it in GitHub Desktop.
Save dberzano/ca3f2cad389af025d9b9 to your computer and use it in GitHub Desktop.
CPU efficiency on Linux with Python
#!/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