Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | #!/usr/bin/env python |
michael@0 | 2 | |
michael@0 | 3 | # Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. |
michael@0 | 4 | # Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | # found in the LICENSE file. |
michael@0 | 6 | |
michael@0 | 7 | """ |
michael@0 | 8 | A clone of iotop (http://guichaz.free.fr/iotop/) showing real time |
michael@0 | 9 | disk I/O statistics. |
michael@0 | 10 | |
michael@0 | 11 | It works on Linux only (FreeBSD and OSX are missing support for IO |
michael@0 | 12 | counters). |
michael@0 | 13 | It doesn't work on Windows as curses module is required. |
michael@0 | 14 | |
michael@0 | 15 | Author: Giampaolo Rodola' <g.rodola@gmail.com> |
michael@0 | 16 | """ |
michael@0 | 17 | |
michael@0 | 18 | import os |
michael@0 | 19 | import sys |
michael@0 | 20 | import psutil |
michael@0 | 21 | if not hasattr(psutil.Process, 'get_io_counters') or os.name != 'posix': |
michael@0 | 22 | sys.exit('platform not supported') |
michael@0 | 23 | import time |
michael@0 | 24 | import curses |
michael@0 | 25 | import atexit |
michael@0 | 26 | |
michael@0 | 27 | |
michael@0 | 28 | # --- curses stuff |
michael@0 | 29 | def tear_down(): |
michael@0 | 30 | win.keypad(0) |
michael@0 | 31 | curses.nocbreak() |
michael@0 | 32 | curses.echo() |
michael@0 | 33 | curses.endwin() |
michael@0 | 34 | |
michael@0 | 35 | win = curses.initscr() |
michael@0 | 36 | atexit.register(tear_down) |
michael@0 | 37 | curses.endwin() |
michael@0 | 38 | lineno = 0 |
michael@0 | 39 | |
michael@0 | 40 | def print_line(line, highlight=False): |
michael@0 | 41 | """A thin wrapper around curses's addstr().""" |
michael@0 | 42 | global lineno |
michael@0 | 43 | try: |
michael@0 | 44 | if highlight: |
michael@0 | 45 | line += " " * (win.getmaxyx()[1] - len(line)) |
michael@0 | 46 | win.addstr(lineno, 0, line, curses.A_REVERSE) |
michael@0 | 47 | else: |
michael@0 | 48 | win.addstr(lineno, 0, line, 0) |
michael@0 | 49 | except curses.error: |
michael@0 | 50 | lineno = 0 |
michael@0 | 51 | win.refresh() |
michael@0 | 52 | raise |
michael@0 | 53 | else: |
michael@0 | 54 | lineno += 1 |
michael@0 | 55 | # --- /curses stuff |
michael@0 | 56 | |
michael@0 | 57 | |
michael@0 | 58 | def bytes2human(n): |
michael@0 | 59 | """ |
michael@0 | 60 | >>> bytes2human(10000) |
michael@0 | 61 | '9.8 K/s' |
michael@0 | 62 | >>> bytes2human(100001221) |
michael@0 | 63 | '95.4 M/s' |
michael@0 | 64 | """ |
michael@0 | 65 | symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y') |
michael@0 | 66 | prefix = {} |
michael@0 | 67 | for i, s in enumerate(symbols): |
michael@0 | 68 | prefix[s] = 1 << (i+1)*10 |
michael@0 | 69 | for s in reversed(symbols): |
michael@0 | 70 | if n >= prefix[s]: |
michael@0 | 71 | value = float(n) / prefix[s] |
michael@0 | 72 | return '%.2f %s/s' % (value, s) |
michael@0 | 73 | return '%.2f B/s' % (n) |
michael@0 | 74 | |
michael@0 | 75 | def poll(interval): |
michael@0 | 76 | """Calculate IO usage by comparing IO statics before and |
michael@0 | 77 | after the interval. |
michael@0 | 78 | Return a tuple including all currently running processes |
michael@0 | 79 | sorted by IO activity and total disks I/O activity. |
michael@0 | 80 | """ |
michael@0 | 81 | # first get a list of all processes and disk io counters |
michael@0 | 82 | procs = [p for p in psutil.process_iter()] |
michael@0 | 83 | for p in procs[:]: |
michael@0 | 84 | try: |
michael@0 | 85 | p._before = p.get_io_counters() |
michael@0 | 86 | except psutil.Error: |
michael@0 | 87 | procs.remove(p) |
michael@0 | 88 | continue |
michael@0 | 89 | disks_before = psutil.disk_io_counters() |
michael@0 | 90 | |
michael@0 | 91 | # sleep some time |
michael@0 | 92 | time.sleep(interval) |
michael@0 | 93 | |
michael@0 | 94 | # then retrieve the same info again |
michael@0 | 95 | for p in procs[:]: |
michael@0 | 96 | try: |
michael@0 | 97 | p._after = p.get_io_counters() |
michael@0 | 98 | p._cmdline = ' '.join(p.cmdline) |
michael@0 | 99 | if not p._cmdline: |
michael@0 | 100 | p._cmdline = p.name |
michael@0 | 101 | p._username = p.username |
michael@0 | 102 | except psutil.NoSuchProcess: |
michael@0 | 103 | procs.remove(p) |
michael@0 | 104 | disks_after = psutil.disk_io_counters() |
michael@0 | 105 | |
michael@0 | 106 | # finally calculate results by comparing data before and |
michael@0 | 107 | # after the interval |
michael@0 | 108 | for p in procs: |
michael@0 | 109 | p._read_per_sec = p._after.read_bytes - p._before.read_bytes |
michael@0 | 110 | p._write_per_sec = p._after.write_bytes - p._before.write_bytes |
michael@0 | 111 | p._total = p._read_per_sec + p._write_per_sec |
michael@0 | 112 | |
michael@0 | 113 | disks_read_per_sec = disks_after.read_bytes - disks_before.read_bytes |
michael@0 | 114 | disks_write_per_sec = disks_after.write_bytes - disks_before.write_bytes |
michael@0 | 115 | |
michael@0 | 116 | # sort processes by total disk IO so that the more intensive |
michael@0 | 117 | # ones get listed first |
michael@0 | 118 | processes = sorted(procs, key=lambda p: p._total, reverse=True) |
michael@0 | 119 | |
michael@0 | 120 | return (processes, disks_read_per_sec, disks_write_per_sec) |
michael@0 | 121 | |
michael@0 | 122 | |
michael@0 | 123 | def refresh_window(procs, disks_read, disks_write): |
michael@0 | 124 | """Print results on screen by using curses.""" |
michael@0 | 125 | curses.endwin() |
michael@0 | 126 | templ = "%-5s %-7s %11s %11s %s" |
michael@0 | 127 | win.erase() |
michael@0 | 128 | |
michael@0 | 129 | disks_tot = "Total DISK READ: %s | Total DISK WRITE: %s" \ |
michael@0 | 130 | % (bytes2human(disks_read), bytes2human(disks_write)) |
michael@0 | 131 | print_line(disks_tot) |
michael@0 | 132 | |
michael@0 | 133 | header = templ % ("PID", "USER", "DISK READ", "DISK WRITE", "COMMAND") |
michael@0 | 134 | print_line(header, highlight=True) |
michael@0 | 135 | |
michael@0 | 136 | for p in procs: |
michael@0 | 137 | line = templ % (p.pid, |
michael@0 | 138 | p._username[:7], |
michael@0 | 139 | bytes2human(p._read_per_sec), |
michael@0 | 140 | bytes2human(p._write_per_sec), |
michael@0 | 141 | p._cmdline) |
michael@0 | 142 | try: |
michael@0 | 143 | print_line(line) |
michael@0 | 144 | except curses.error: |
michael@0 | 145 | break |
michael@0 | 146 | win.refresh() |
michael@0 | 147 | |
michael@0 | 148 | def main(): |
michael@0 | 149 | try: |
michael@0 | 150 | interval = 0 |
michael@0 | 151 | while 1: |
michael@0 | 152 | args = poll(interval) |
michael@0 | 153 | refresh_window(*args) |
michael@0 | 154 | interval = 1 |
michael@0 | 155 | except (KeyboardInterrupt, SystemExit): |
michael@0 | 156 | pass |
michael@0 | 157 | |
michael@0 | 158 | if __name__ == '__main__': |
michael@0 | 159 | main() |