Bug 930808 - Upgrade to psutil 2.1.3; r=glandium
psutil 2.1.3 is replacing psutil 1.0.1. There are numerous bug fixes and feature enhancements in psutil worth obtaining. Source code was obtained from https://pypi.python.org/packages/source/p/psutil/psutil-2.1.3.tar.gz and uncompressed into python/psutil without modification except for the removal of the egg-info directory and the .travis.yml file.
This commit is contained in:
@@ -8,15 +8,39 @@
|
||||
A clone of top / htop.
|
||||
|
||||
Author: Giampaolo Rodola' <g.rodola@gmail.com>
|
||||
|
||||
$ python examples/top.py
|
||||
CPU0 [| ] 4.9%
|
||||
CPU1 [||| ] 7.8%
|
||||
CPU2 [ ] 2.0%
|
||||
CPU3 [||||| ] 13.9%
|
||||
Mem [||||||||||||||||||| ] 49.8% 4920M/9888M
|
||||
Swap [ ] 0.0% 0M/0M
|
||||
Processes: 287 (running=1 sleeping=286)
|
||||
Load average: 0.34 0.54 0.46 Uptime: 3 days, 10:16:37
|
||||
|
||||
PID USER NI VIRT RES CPU% MEM% TIME+ NAME
|
||||
------------------------------------------------------------
|
||||
989 giampaol 0 66M 12M 7.4 0.1 0:00.61 python
|
||||
2083 root 0 506M 159M 6.5 1.6 0:29.26 Xorg
|
||||
4503 giampaol 0 599M 25M 6.5 0.3 3:32.60 gnome-terminal
|
||||
3868 giampaol 0 358M 8M 2.8 0.1 23:12.60 pulseaudio
|
||||
3936 giampaol 0 1G 111M 2.8 1.1 33:41.67 compiz
|
||||
4401 giampaol 0 536M 141M 2.8 1.4 35:42.73 skype
|
||||
4047 giampaol 0 743M 76M 1.8 0.8 42:03.33 unity-panel-service
|
||||
13155 giampaol 0 1G 280M 1.8 2.8 41:57.34 chrome
|
||||
10 root 0 0B 0B 0.9 0.0 4:01.81 rcu_sched
|
||||
339 giampaol 0 1G 113M 0.9 1.1 8:15.73 chrome
|
||||
...
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
if os.name != 'posix':
|
||||
sys.exit('platform not supported')
|
||||
import time
|
||||
import curses
|
||||
import atexit
|
||||
import curses
|
||||
import time
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import psutil
|
||||
@@ -34,6 +58,7 @@ atexit.register(tear_down)
|
||||
curses.endwin()
|
||||
lineno = 0
|
||||
|
||||
|
||||
def print_line(line, highlight=False):
|
||||
"""A thin wrapper around curses's addstr()."""
|
||||
global lineno
|
||||
@@ -62,13 +87,14 @@ def bytes2human(n):
|
||||
symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
|
||||
prefix = {}
|
||||
for i, s in enumerate(symbols):
|
||||
prefix[s] = 1 << (i+1)*10
|
||||
prefix[s] = 1 << (i + 1) * 10
|
||||
for s in reversed(symbols):
|
||||
if n >= prefix[s]:
|
||||
value = int(float(n) / prefix[s])
|
||||
return '%s%s' % (value, s)
|
||||
return "%sB" % n
|
||||
|
||||
|
||||
def poll(interval):
|
||||
# sleep some time
|
||||
time.sleep(interval)
|
||||
@@ -76,32 +102,35 @@ def poll(interval):
|
||||
procs_status = {}
|
||||
for p in psutil.process_iter():
|
||||
try:
|
||||
p.dict = p.as_dict(['username', 'get_nice', 'get_memory_info',
|
||||
'get_memory_percent', 'get_cpu_percent',
|
||||
'get_cpu_times', 'name', 'status'])
|
||||
p.dict = p.as_dict(['username', 'nice', 'memory_info',
|
||||
'memory_percent', 'cpu_percent',
|
||||
'cpu_times', 'name', 'status'])
|
||||
try:
|
||||
procs_status[str(p.dict['status'])] += 1
|
||||
procs_status[p.dict['status']] += 1
|
||||
except KeyError:
|
||||
procs_status[str(p.dict['status'])] = 1
|
||||
procs_status[p.dict['status']] = 1
|
||||
except psutil.NoSuchProcess:
|
||||
pass
|
||||
else:
|
||||
procs.append(p)
|
||||
|
||||
# return processes sorted by CPU percent usage
|
||||
processes = sorted(procs, key=lambda p: p.dict['cpu_percent'], reverse=True)
|
||||
processes = sorted(procs, key=lambda p: p.dict['cpu_percent'],
|
||||
reverse=True)
|
||||
return (processes, procs_status)
|
||||
|
||||
|
||||
def print_header(procs_status, num_procs):
|
||||
"""Print system-related info, above the process list."""
|
||||
|
||||
def get_dashes(perc):
|
||||
dashes = "|" * int((float(perc) / 10 * 4))
|
||||
dashes = "|" * int((float(perc) / 10 * 4))
|
||||
empty_dashes = " " * (40 - len(dashes))
|
||||
return dashes, empty_dashes
|
||||
|
||||
# cpu usage
|
||||
for cpu_num, perc in enumerate(psutil.cpu_percent(interval=0, percpu=True)):
|
||||
percs = psutil.cpu_percent(interval=0, percpu=True)
|
||||
for cpu_num, perc in enumerate(percs):
|
||||
dashes, empty_dashes = get_dashes(perc)
|
||||
print_line(" CPU%-2s [%s%s] %5s%%" % (cpu_num, dashes, empty_dashes,
|
||||
perc))
|
||||
@@ -135,12 +164,13 @@ def print_header(procs_status, num_procs):
|
||||
st.sort(key=lambda x: x[:3] in ('run', 'sle'), reverse=1)
|
||||
print_line(" Processes: %s (%s)" % (num_procs, ' '.join(st)))
|
||||
# load average, uptime
|
||||
uptime = datetime.now() - datetime.fromtimestamp(psutil.BOOT_TIME)
|
||||
uptime = datetime.now() - datetime.fromtimestamp(psutil.boot_time())
|
||||
av1, av2, av3 = os.getloadavg()
|
||||
line = " Load average: %.2f %.2f %.2f Uptime: %s" \
|
||||
% (av1, av2, av3, str(uptime).split('.')[0])
|
||||
% (av1, av2, av3, str(uptime).split('.')[0])
|
||||
print_line(line)
|
||||
|
||||
|
||||
def refresh_window(procs, procs_status):
|
||||
"""Print results on screen by using curses."""
|
||||
curses.endwin()
|
||||
@@ -154,7 +184,7 @@ def refresh_window(procs, procs_status):
|
||||
for p in procs:
|
||||
# TIME+ column shows process CPU cumulative time and it
|
||||
# is expressed as: "mm:ss.ms"
|
||||
if p.dict['cpu_times'] != None:
|
||||
if p.dict['cpu_times'] is not None:
|
||||
ctime = timedelta(seconds=sum(p.dict['cpu_times']))
|
||||
ctime = "%s:%s.%s" % (ctime.seconds // 60 % 60,
|
||||
str((ctime.seconds % 60)).zfill(2),
|
||||
|
||||
Reference in New Issue
Block a user