From d286fea91fa93c6b5723e25ed89a803036e7c72f Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Fri, 5 Apr 2019 20:08:44 +0200 Subject: [PATCH] reuse ps.py script in psutil.test() --- psutil/__init__.py | 67 ++++++++++++++++++++-------------------------- scripts/ps.py | 24 +++++++++++++++-- 2 files changed, 51 insertions(+), 40 deletions(-) diff --git a/psutil/__init__.py b/psutil/__init__.py index 3726079e5..5d2b8d3c6 100644 --- a/psutil/__init__.py +++ b/psutil/__init__.py @@ -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: @@ -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 diff --git a/scripts/ps.py b/scripts/ps.py index fd2a8ec44..40dcce20a 100755 --- a/scripts/ps.py +++ b/scripts/ps.py @@ -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 @@ -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 \