Skip to content

Instantly share code, notes, and snippets.

@willejs
Created October 10, 2013 12:42
Show Gist options
  • Save willejs/6917721 to your computer and use it in GitHub Desktop.
Save willejs/6917721 to your computer and use it in GitHub Desktop.
get cpu percent per core for a pid usage: ./pid.py <pid-no-here>
#!/usr/bin/python
import os
import sys
core = { 0:float(0.0), 1:float(0.0), 2:float(0.0), 3:float(0.0), 4:float(0.0), 5:float(0.0), 6:float(0.0), 7:float(0.0) }
output = []
for i in os.popen('ps -p {} -L -o pid,tid,psr,pcpu'.format(str(sys.argv[1]))):
output.append(i.rstrip('\n').split())
for line in output[1:]:
#print line[2], float(line[3])
core[int(line[2])] = core[int(line[2])] + float(line[3])
for c, v in core.iteritems():
print 'CPU', c, '{}%'.format(round(v, 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment