Index: third_party/psutil/examples/iotop.py
|
diff --git a/third_party/psutil/examples/iotop.py b/third_party/psutil/examples/iotop.py
|
index 2307c75aa7ed2d20cab343b1d7dc4804da4b9fb4..222a135b331ba3f88c9e90d43a0463305d03b86c 100644
|
--- a/third_party/psutil/examples/iotop.py
|
+++ b/third_party/psutil/examples/iotop.py
|
@@ -1,6 +1,6 @@
|
#!/usr/bin/env python
|
#
|
-# $Id: iotop.py 1143 2011-10-05 19:11:59Z g.rodola $
|
+# $Id: iotop.py 1213 2011-10-29 03:30:41Z 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
|
@@ -10,18 +10,52 @@
|
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.
|
+It works on Linux only (FreeBSD and OSX are missing support for IO
|
+counters).
|
+It doesn't work on Windows as curses module is required.
|
|
Author: Giampaolo Rodola' <g.rodola@gmail.com>
|
"""
|
|
+import os
|
+import sys
|
+import psutil
|
+if not hasattr(psutil.Process, 'get_io_counters') or os.name != 'posix':
|
+ sys.exit('platform not supported')
|
import time
|
import curses
|
import atexit
|
|
-import psutil
|
+
|
+# --- curses stuff
|
+def tear_down():
|
+ win.keypad(0)
|
+ curses.nocbreak()
|
+ curses.echo()
|
+ curses.endwin()
|
|
win = curses.initscr()
|
+atexit.register(tear_down)
|
+curses.endwin()
|
+lineno = 0
|
+
|
+def print_line(line, highlight=False):
|
+ """A thin wrapper around curses's addstr()."""
|
+ global lineno
|
+ try:
|
+ if highlight:
|
+ line += " " * (win.getmaxyx()[1] - len(line))
|
+ win.addstr(lineno, 0, line, curses.A_REVERSE)
|
+ else:
|
+ win.addstr(lineno, 0, line, 0)
|
+ except curses.error:
|
+ lineno = 0
|
+ win.refresh()
|
+ raise
|
+ else:
|
+ lineno += 1
|
+# --- /curses stuff
|
+
|
|
def bytes2human(n):
|
"""
|
@@ -87,50 +121,41 @@ def poll(interval):
|
|
return (processes, disks_read_per_sec, disks_write_per_sec)
|
|
-def run(win):
|
+
|
+def refresh_window(procs, disks_read, disks_write):
|
"""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
|
+ win.erase()
|
|
-def main():
|
- def tear_down():
|
- win.keypad(0)
|
- curses.nocbreak()
|
- curses.echo()
|
- curses.endwin()
|
+ disks_tot = "Total DISK READ: %s | Total DISK WRITE: %s" \
|
+ % (bytes2human(disks_read), bytes2human(disks_write))
|
+ print_line(disks_tot)
|
|
- atexit.register(tear_down)
|
+ header = templ % ("PID", "USER", "DISK READ", "DISK WRITE", "COMMAND")
|
+ print_line(header, highlight=True)
|
+
|
+ for p in procs:
|
+ line = templ % (p.pid,
|
+ p._username[:7],
|
+ bytes2human(p._read_per_sec),
|
+ bytes2human(p._write_per_sec),
|
+ p._cmdline)
|
+ try:
|
+ print_line(line)
|
+ except curses.error:
|
+ break
|
+ win.refresh()
|
+
|
+def main():
|
try:
|
- run(win)
|
+ interval = 0
|
+ while 1:
|
+ args = poll(interval)
|
+ refresh_window(*args)
|
+ interval = 1
|
except (KeyboardInterrupt, SystemExit):
|
- pass
|
+ print
|
|
if __name__ == '__main__':
|
main()
|
|