Skip to content

Commit

Permalink
reuse ps.py script in psutil.test()
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Apr 5, 2019
1 parent 83ed63b commit d286fea
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 40 deletions.
67 changes: 29 additions & 38 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2438,36 +2438,16 @@ def win_service_get(name):


def test(): # pragma: no cover
"""List info of all currently running processes emulating ps aux
output.
"""
def bytes2human(n):
"""
>>> bytes2human(10000)
'9.8 K'
>>> bytes2human(100001221)
'95.4 M'
"""
symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
prefix = {}
for i, s in enumerate(symbols):
prefix[s] = 1 << (i + 1) * 10
for s in reversed(symbols):
if n >= prefix[s]:
value = float(n) / prefix[s]
return '%.1f%s' % (value, s)
return '%.1fB' % (n)
from ._common import bytes2human
from ._compat import get_terminal_size

today_day = datetime.date.today()
templ = "%-10s %6s %5s %8s %8s %12s %5s %7s %s"
attrs = ['pid', 'memory_percent', 'name', 'cpu_times', 'create_time',
'memory_info']
if POSIX:
attrs.append('uids')
attrs.append('terminal')
print(templ % ("USER", "PID", "%MEM", "VSZ", "RSS", "TTY", "START", "TIME",
"COMMAND"))
for p in process_iter(attrs=attrs, ad_value=None):
templ = "%-10s %5s %5s %7s %7s %5s %6s %6s %6s %s"
attrs = ['pid', 'memory_percent', 'name', 'cmdline', 'cpu_times',
'create_time', 'memory_info', 'status', 'nice', 'username']
print(templ % ("USER", "PID", "%MEM", "VSZ", "RSS", "NICE",
"STATUS", "START", "TIME", "CMDLINE"))
for p in process_iter(attrs, ad_value=None):
if p.info['create_time']:
ctime = datetime.datetime.fromtimestamp(p.info['create_time'])
if ctime.date() == today_day:
Expand All @@ -2476,35 +2456,46 @@ def bytes2human(n):
ctime = ctime.strftime("%b%d")
else:
ctime = ''
if p.info['cpu_times'] is not None:
if p.info['cpu_times']:
cputime = time.strftime("%M:%S",
time.localtime(sum(p.info['cpu_times'])))
else:
cputime = ''
try:
user = p.username()[:9]
except Error:
user = ''
if WINDOWS and '\\' in user:
user = user.split('\\')[1]

user = p.info['username']
if not user and POSIX:
try:
user = p.uids()[0]
except Error:
pass
if user and WINDOWS and '\\' in user:
user = user.split('\\')[1]
user = user[:9]
vms = bytes2human(p.info['memory_info'].vms) if \
p.info['memory_info'] is not None else ''
rss = bytes2human(p.info['memory_info'].rss) if \
p.info['memory_info'] is not None else ''
memp = round(p.info['memory_percent'], 1) if \
p.info['memory_percent'] is not None else ''
nice = int(p.info['nice']) if p.info['nice'] else ''
if p.info['cmdline']:
cmdline = ' '.join(p.info['cmdline'])
else:
cmdline = p.info['name']
status = p.info['status'][:5] if p.info['status'] else ''

print(templ % (
line = templ % (
user[:10],
p.info['pid'],
memp,
vms,
rss,
p.info.get('terminal', '') or '',
nice,
status,
ctime,
cputime,
p.info['name'].strip() or '?'))
cmdline)
print(line[:get_terminal_size()[0]])


del memoize, memoize_when_activated, division, deprecated_method
Expand Down
24 changes: 22 additions & 2 deletions scripts/ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,28 @@
A clone of 'ps aux'.
$ python scripts/ps.py
...
USER PID %MEM VSZ RSS NICE STATUS START TIME CMDLINE
root 1 0.0 220.9M 6.5M sleep Mar27 09:10 /lib/systemd
root 2 0.0 0.0B 0.0B sleep Mar27 00:00 kthreadd
root 4 0.0 0.0B 0.0B -20 idle Mar27 00:00 kworker/0:0H
root 6 0.0 0.0B 0.0B -20 idle Mar27 00:00 mm_percpu_wq
root 7 0.0 0.0B 0.0B sleep Mar27 00:06 ksoftirqd/0
root 8 0.0 0.0B 0.0B idle Mar27 03:32 rcu_sched
root 9 0.0 0.0B 0.0B idle Mar27 00:00 rcu_bh
root 10 0.0 0.0B 0.0B sleep Mar27 00:00 migration/0
root 11 0.0 0.0B 0.0B sleep Mar27 00:00 watchdog/0
root 12 0.0 0.0B 0.0B sleep Mar27 00:00 cpuhp/0
root 13 0.0 0.0B 0.0B sleep Mar27 00:00 cpuhp/1
root 14 0.0 0.0B 0.0B sleep Mar27 00:01 watchdog/1
root 15 0.0 0.0B 0.0B sleep Mar27 00:00 migration/1
[...]
giampaolo 19704 1.5 1.9G 235.6M sleep 17:39 01:11 firefox
root 20414 0.0 0.0B 0.0B idle Apr04 00:00 kworker/4:2
giampaolo 20952 0.0 10.7M 100.0K sleep Mar28 00:00 sh -c /usr
giampaolo 20953 0.0 269.0M 528.0K sleep Mar28 00:00 /usr/lib/
giampaolo 22150 3.3 2.4G 525.5M sleep Apr02 49:09 /usr/lib/
root 22338 0.0 0.0B 0.0B idle 02:04 00:00 kworker/1:2
giampaolo 24123 0.0 35.0M 7.0M sleep 02:12 00:02 bash
"""

import datetime
Expand Down Expand Up @@ -50,7 +71,6 @@ def main():
if user and psutil.WINDOWS and '\\' in user:
user = user.split('\\')[1]
user = user[:9]

vms = bytes2human(p.info['memory_info'].vms) if \
p.info['memory_info'] is not None else ''
rss = bytes2human(p.info['memory_info'].rss) if \
Expand Down

0 comments on commit d286fea

Please sign in to comment.