diff options
author | imasaki@google.com <imasaki@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-07 01:56:56 +0000 |
---|---|---|
committer | imasaki@google.com <imasaki@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-07 01:56:56 +0000 |
commit | 81b941e97497d337f742aa7b499435709e769bc2 (patch) | |
tree | 295240e93e7c6ae02b8dd4fa083c43318f654395 /third_party/psutil/examples | |
parent | c1795d643965e35276dfef1fa390df9bbdb11289 (diff) | |
download | chromium_src-81b941e97497d337f742aa7b499435709e769bc2.zip chromium_src-81b941e97497d337f742aa7b499435709e769bc2.tar.gz chromium_src-81b941e97497d337f742aa7b499435709e769bc2.tar.bz2 |
Update third_party/psutil and fix the licence issue with it.
BUG=98456
TEST=None
Review URL: http://codereview.chromium.org/8159001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@104425 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'third_party/psutil/examples')
-rw-r--r-- | third_party/psutil/examples/disk_usage.py | 43 | ||||
-rw-r--r-- | third_party/psutil/examples/iotop.py | 136 | ||||
-rw-r--r-- | third_party/psutil/examples/killall.py | 33 | ||||
-rw-r--r-- | third_party/psutil/examples/process_detail.py | 122 |
4 files changed, 334 insertions, 0 deletions
diff --git a/third_party/psutil/examples/disk_usage.py b/third_party/psutil/examples/disk_usage.py new file mode 100644 index 0000000..23f2d0c --- /dev/null +++ b/third_party/psutil/examples/disk_usage.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +# +# $Id: disk_usage.py 1143 2011-10-05 19:11:59Z g.rodola $ +# +# Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +""" +List all mounted disk partitions a-la "df -h" command. +""" + +import sys +import psutil + +def convert_bytes(n): + if n == 0: + return "0B" + 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) + + +def main(): + templ = "%-17s %8s %8s %8s %5s%% %9s %s" + print templ % ("Device", "Total", "Used", "Free", "Use ", "Type", "Mount") + for part in psutil.disk_partitions(all=False): + usage = psutil.disk_usage(part.mountpoint) + print templ % (part.device, + convert_bytes(usage.total), + convert_bytes(usage.used), + convert_bytes(usage.free), + int(usage.percent), + part.fstype, + part.mountpoint) + +if __name__ == '__main__': + sys.exit(main()) diff --git a/third_party/psutil/examples/iotop.py b/third_party/psutil/examples/iotop.py new file mode 100644 index 0000000..2307c75 --- /dev/null +++ b/third_party/psutil/examples/iotop.py @@ -0,0 +1,136 @@ +#!/usr/bin/env python +# +# $Id: iotop.py 1143 2011-10-05 19:11:59Z g.rodola $ +# +# Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +""" +A clone of iotop (http://guichaz.free.fr/iotop/) showing real time +disk I/O statistics. + +It works on UNIX only as curses module is not available on Windows. + +Author: Giampaolo Rodola' <g.rodola@gmail.com> +""" + +import time +import curses +import atexit + +import psutil + +win = curses.initscr() + +def bytes2human(n): + """ + >>> bytes2human(10000) + '9.8 K/s' + >>> bytes2human(100001221) + '95.4 M/s' + """ + 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 '%.2f %s/s' % (value, s) + return "0.00 B/s" + +def poll(interval): + """Calculate IO usage by comparing IO statics before and + after the interval. + Return a tuple including all currently running processes + sorted by IO activity and total disks I/O activity. + """ + # first get a list of all processes and disk io counters + procs = [p for p in psutil.process_iter()] + for p in procs[:]: + try: + p._before = p.get_io_counters() + except psutil.Error: + procs.remove(p) + continue + disks_before = psutil.disk_io_counters() + + # sleep some time + time.sleep(interval) + + # then retrieve the same info again + for p in procs[:]: + try: + p._after = p.get_io_counters() + p._cmdline = ' '.join(p.cmdline) + if not p._cmdline: + p._cmdline = p.name + p._username = p.username + except psutil.NoSuchProcess: + procs.remove(p) + disks_after = psutil.disk_io_counters() + + # finally calculate results by comparing data before and + # after the interval + for p in procs: + p._read_per_sec = p._after.read_bytes - p._before.read_bytes + p._write_per_sec = p._after.write_bytes - p._before.write_bytes + p._total = p._read_per_sec + p._write_per_sec + + disks_read_per_sec = disks_after.read_bytes - disks_before.read_bytes + disks_write_per_sec = disks_after.write_bytes - disks_before.write_bytes + + # sort processes by total disk IO so that the more intensive + # ones get listed first + processes = sorted(procs, key=lambda p: p._total, reverse=True) + + return (processes, disks_read_per_sec, disks_write_per_sec) + +def run(win): + """Print results on screen by using curses.""" + curses.endwin() + templ = "%-5s %-7s %11s %11s %s" + interval = 0 + while 1: + procs, disks_read, disks_write = poll(interval) + win.erase() + + disks_tot = "Total DISK READ: %s | Total DISK WRITE: %s" \ + % (bytes2human(disks_read), bytes2human(disks_write)) + win.addstr(0, 0, disks_tot) + + header = templ % ("PID", "USER", "DISK READ", "DISK WRITE", "COMMAND") + header += " " * (win.getmaxyx()[1] - len(header)) + win.addstr(1, 0, header, curses.A_REVERSE) + + lineno = 2 + for p in procs: + line = templ % (p.pid, + p._username[:7], + bytes2human(p._read_per_sec), + bytes2human(p._write_per_sec), + p._cmdline) + try: + win.addstr(lineno, 0, line) + except curses.error: + break + win.refresh() + lineno += 1 + interval = 1 + +def main(): + def tear_down(): + win.keypad(0) + curses.nocbreak() + curses.echo() + curses.endwin() + + atexit.register(tear_down) + try: + run(win) + except (KeyboardInterrupt, SystemExit): + pass + +if __name__ == '__main__': + main() diff --git a/third_party/psutil/examples/killall.py b/third_party/psutil/examples/killall.py new file mode 100644 index 0000000..5381b54 --- /dev/null +++ b/third_party/psutil/examples/killall.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python +# +# $Id: killall.py 1143 2011-10-05 19:11:59Z g.rodola $ +# +# Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +""" +Kill a process by name. +""" + +import os +import sys +import psutil + +def main(): + if len(sys.argv) != 2: + sys.exit('usage: %s name' % __file__) + else: + NAME = sys.argv[1] + + killed = [] + for proc in psutil.process_iter(): + if proc.name == NAME and proc.pid != os.getpid(): + proc.kill() + killed.append(proc.pid) + if not killed: + sys.exit('%s: no process found' % NAME) + else: + sys.exit(0) + +sys.exit(main()) diff --git a/third_party/psutil/examples/process_detail.py b/third_party/psutil/examples/process_detail.py new file mode 100644 index 0000000..4c791d1 --- /dev/null +++ b/third_party/psutil/examples/process_detail.py @@ -0,0 +1,122 @@ +#!/usr/bin/env python +# +# $Id: process_detail.py 1143 2011-10-05 19:11:59Z g.rodola $ +# +# Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +""" +Print detailed information about a process. +""" + +import os +import datetime +import socket +import sys + +import psutil +from psutil._compat import namedtuple + + +def convert_bytes(n): + if n == 0: + return '0B' + 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) + +def print_(a, b): + if sys.stdout.isatty(): + fmt = '\x1b[1;32m%-17s\x1b[0m %s' %(a, b) + else: + fmt = '%-15s %s' %(a, b) + print fmt + +def run(pid): + p = psutil.Process(pid) + if p.parent: + parent = '(%s)' % p.parent.name + else: + parent = '' + started = datetime.datetime.fromtimestamp(p.create_time).strftime('%Y-%M-%d %H:%M') + io = p.get_io_counters() + mem = p.get_memory_info() + mem = '%s%% (resident=%s, virtual=%s) ' %(round(p.get_memory_percent(), 1), + convert_bytes(mem.rss), + convert_bytes(mem.vms)) + cpu_times = p.get_cpu_times() + cpu_percent = p.get_cpu_percent(0) + children = p.get_children() + files = p.get_open_files() + threads = p.get_threads() + connections = p.get_connections() + + print_('pid', p.pid) + print_('name', p.name) + print_('exe', p.exe) + print_('parent', '%s %s' % (p.ppid, parent)) + print_('cmdline', ' '.join(p.cmdline)) + print_('started', started) + print_('user', p.username) + if os.name == 'posix': + print_('uids', 'real=%s, effective=%s, saved=%s' % p.uids) + print_('gids', 'real=%s, effective=%s, saved=%s' % p.gids) + print_('terminal', p.terminal or '') + if hasattr(p, 'getcwd'): + print_('cwd', p.getcwd()) + print_('memory', mem) + print_('cpu', '%s%% (user=%s, system=%s)' % (cpu_percent, + cpu_times.user, + cpu_times.system)) + print_('status', p.status) + print_('niceness', p.nice) + print_('num threads', p.get_num_threads()) + if hasattr(p, 'get_io_counters'): + print_('I/O', 'bytes-read=%s, bytes-written=%s' % \ + (convert_bytes(io.read_bytes), + convert_bytes(io.write_bytes))) + if children: + print_('children', '') + for child in children: + print_('', 'pid=%s name=%s' % (child.pid, child.name)) + + if files: + print_('open files', '') + for file in files: + print_('', 'fd=%s %s ' % (file.fd, file.path)) + + if threads: + print_('running threads', '') + for thread in threads: + print_('', 'id=%s, user-time=%s, sys-time=%s' \ + % (thread.id, thread.user_time, thread.system_time)) + if connections: + print_('open connections', '') + for conn in connections: + type = 'TCP' if conn.type == socket.SOCK_STREAM else 'UDP' + lip, lport = conn.local_address + if not conn.remote_address: + rip, rport = '*', '*' + else: + rip, rport = conn.remote_address + print_('', '%s:%s -> %s:%s type=%s status=%s' \ + % (lip, lport, rip, rport, type, conn.status)) + +def main(argv=None): + if argv is None: + argv = sys.argv + if len(argv) == 1: + sys.exit(run(os.getpid())) + elif len(argv) == 2: + sys.exit(run(int(argv[1]))) + else: + sys.exit('usage: %s [pid]' % __file__) + +if __name__ == '__main__': + sys.exit(main()) |