python/psutil/README

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 ===========
     2 Quick links
     3 ===========
     5 * `Home page <http://code.google.com/p/psutil>`_
     6 * `Download <http://code.google.com/p/psutil/downloads/list>`_
     7 * `Documentation <http://code.google.com/p/psutil/wiki/Documentation>`_
     9 =======
    10 Summary
    11 =======
    13 psutil is a module providing an interface for retrieving information on all
    14 running processes and system utilization (CPU, memory, disks, network, users) in
    15 a portable way by using Python, implementing many functionalities offered by
    16 command line tools such as:  **ps, top, df, kill, free, lsof, free, netstat,
    17 ifconfig, nice, ionice, iostat, iotop, uptime, pidof, tty, who, taskset, pmap**.
    19 It currently supports **Linux**, **Windows**, **OSX**, **FreeBSD**,
    20 **Sun Solaris** both **32-bit** and **64-bit** with Python versions from **2.4**
    21 to **3.3** by using a single code base.
    23 ==============
    24 Example usages
    25 ==============
    27 CPU
    28 ===
    30 >>> import psutil
    31 >>> psutil.cpu_times()
    32 cputimes(user=3961.46, nice=169.729, system=2150.659, idle=16900.540,
    33          iowait=629.509, irq=0.0, softirq=19.422, steal=0.0, guest=0, nice=0.0)
    34 >>>
    35 >>> for x in range(3):
    36 ...     psutil.cpu_percent(interval=1)
    37 ...
    38 4.0
    39 5.9
    40 3.8
    41 >>>
    42 >>> for x in range(3):
    43 ...     psutil.cpu_percent(interval=1, percpu=True)
    44 ...
    45 [4.0, 6.9]
    46 [7.0, 8.5]
    47 [1.2, 9.0]
    48 >>>
    49 >>> for x in range(3):
    50 ...     psutil.cpu_times_percent(interval=1, percpu=False)
    51 ...
    52 cpupercent(user=1.5, nice=0.0, system=0.5, idle=96.5, iowait=1.5, irq=0.0,
    53            softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
    54 cpupercent(user=1.0, nice=0.0, system=0.0, idle=99.0, iowait=0.0, irq=0.0,
    55            softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
    56 cpupercent(user=2.0, nice=0.0, system=0.0, idle=98.0, iowait=0.0, irq=0.0,
    57            softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
    59 Memory
    60 ======
    62 >>> psutil.virtual_memory()
    63 vmem(total=8374149120L, available=2081050624L, percent=75.1, used=8074080256L,
    64      free=300068864L, active=3294920704, inactive=1361616896, buffers=529895424L,
    65      cached=1251086336)
    66 >>> psutil.swap_memory()
    67 swap(total=2097147904L, used=296128512L, free=1801019392L, percent=14.1,
    68      sin=304193536, sout=677842944)
    69 >>>
    72 Disks
    73 =====
    75 >>> psutil.disk_partitions()
    76 [partition(device='/dev/sda1', mountpoint='/', fstype='ext4', opts='rw,nosuid'),
    77  partition(device='/dev/sda2', mountpoint='/home', fstype='ext, opts='rw')]
    78 >>>
    79 >>> psutil.disk_usage('/')
    80 usage(total=21378641920, used=4809781248, free=15482871808, percent=22.5)
    81 >>>
    82 >>> psutil.disk_io_counters()
    83 iostat(read_count=719566, write_count=1082197, read_bytes=18626220032,
    84        write_bytes=24081764352, read_time=5023392, write_time=63199568)
    85 >>>
    88 Network
    89 =======
    91 >>> psutil.net_io_counters(pernic=True)
    92 {'lo': iostat(bytes_sent=799953745, bytes_recv=799953745,
    93               packets_sent=453698, packets_recv=453698),
    94  'eth0': iostat(bytes_sent=734324837, bytes_recv=4163935363,
    95                 packets_sent=3605828, packets_recv=4096685)}
    96 >>>
    98 Other system info
    99 =================
   101 >>> psutil.get_users()
   102 [user(name='giampaolo', terminal='pts/2', host='localhost', started=1340737536.0),
   103  user(name='giampaolo', terminal='pts/3', host='localhost', started=1340737792.0)]
   104 >>>
   105 >>> psutil.get_boot_time()
   106 1365519115.0
   109 Process management
   110 ==================
   112 >>> import psutil
   113 >>> psutil.get_pid_list()
   114 [1, 2, 3, 4, 5, 6, 7, 46, 48, 50, 51, 178, 182, 222, 223, 224,
   115 268, 1215, 1216, 1220, 1221, 1243, 1244, 1301, 1601, 2237, 2355,
   116 2637, 2774, 3932, 4176, 4177, 4185, 4187, 4189, 4225, 4243, 4245,
   117 4263, 4282, 4306, 4311, 4312, 4313, 4314, 4337, 4339, 4357, 4358,
   118 4363, 4383, 4395, 4408, 4433, 4443, 4445, 4446, 5167, 5234, 5235,
   119 5252, 5318, 5424, 5644, 6987, 7054, 7055, 7071]
   120 >>>
   121 >>> p = psutil.Process(7055)
   122 >>> p.name
   123 'python'
   124 >>> p.exe
   125 '/usr/bin/python'
   126 >>> p.getcwd()
   127 '/home/giampaolo'
   128 >>> p.cmdline
   129 ['/usr/bin/python', 'main.py']
   130 >>>
   131 >>> str(p.status)
   132 'running'
   133 >>> p.username
   134 'giampaolo'
   135 >>> p.create_time
   136 1267551141.5019531
   137 >>> p.terminal
   138 '/dev/pts/0'
   139 >>>
   140 >>> p.uids
   141 user(real=1000, effective=1000, saved=1000)
   142 >>> p.gids
   143 group(real=1000, effective=1000, saved=1000)
   144 >>>
   145 >>> p.get_cpu_times()
   146 cputimes(user=1.02, system=0.31)
   147 >>> p.get_cpu_percent(interval=1.0)
   148 12.1
   149 >>> p.get_cpu_affinity()
   150 [0, 1, 2, 3]
   151 >>> p.set_cpu_affinity([0])
   152 >>>
   153 >>> p.get_memory_percent()
   154 0.63423
   155 >>>
   156 >>> p.get_memory_info()
   157 meminfo(rss=7471104, vms=68513792)
   158 >>> p.get_ext_memory_info()
   159 meminfo(rss=9662464, vms=49192960, shared=3612672, text=2564096, lib=0, data=5754880, dirty=0)
   160 >>> p.get_memory_maps()
   161 [mmap(path='/lib/x86_64-linux-gnu/libutil-2.15.so', rss=16384, anonymous=8192, swap=0),
   162  mmap(path='/lib/x86_64-linux-gnu/libc-2.15.so', rss=6384, anonymous=15, swap=0),
   163  mmap(path='/lib/x86_64-linux-gnu/libcrypto.so.1.0.0', rss=34124, anonymous=1245, swap=0),
   164  mmap(path='[heap]', rss=54653, anonymous=8192, swap=0),
   165  mmap(path='[stack]', rss=1542, anonymous=166, swap=0),
   166  ...]
   167 >>>
   168 >>> p.get_io_counters()
   169 io(read_count=478001, write_count=59371, read_bytes=700416, write_bytes=69632)
   170 >>>
   171 >>> p.get_open_files()
   172 [openfile(path='/home/giampaolo/svn/psutil/somefile', fd=3)]
   173 >>>
   174 >>> p.get_connections()
   175 [connection(fd=115, family=2, type=1, laddr=('10.0.0.1', 48776),
   176             raddr=('93.186.135.91', 80), status='ESTABLISHED'),
   177  connection(fd=117, family=2, type=1, laddr=('10.0.0.1', 43761),
   178             raddr=('72.14.234.100', 80), status='CLOSING'),
   179  connection(fd=119, family=2, type=1, laddr=('10.0.0.1', 60759),
   180             raddr=('72.14.234.104', 80), status='ESTABLISHED'),
   181  connection(fd=123, family=2, type=1, laddr=('10.0.0.1', 51314),
   182             raddr=('72.14.234.83', 443), status='SYN_SENT')]
   183 >>>
   184 >>> p.get_num_threads()
   185 4
   186 >>> p.get_num_fds()
   187 8
   188 >>> p.get_threads()
   189 [thread(id=5234, user_time=22.5, system_time=9.2891),
   190  thread(id=5235, user_time=0.0, system_time=0.0),
   191  thread(id=5236, user_time=0.0, system_time=0.0),
   192  thread(id=5237, user_time=0.0707, system_time=1.1)]
   193 >>>
   194 >>> p.get_num_ctx_switches()
   195 amount(voluntary=78, involuntary=19)
   196 >>>
   197 >>> p.get_nice()
   198 0
   199 >>> p.set_nice(10)
   200 >>>
   201 >>> p.suspend()
   202 >>> p.resume()
   203 >>>
   204 >>> p.terminate()
   205 >>> p.wait(timeout=3)
   206 0
   207 >>>
   208 >>> psutil.test()
   209 USER         PID %CPU %MEM     VSZ     RSS TTY        START    TIME  COMMAND
   210 root           1  0.0  0.0   24584    2240 ?          Jun17   00:00  init
   211 root           2  0.0  0.0       0       0 ?          Jun17   00:00  kthreadd
   212 root           3  0.0  0.0       0       0 ?          Jun17   00:05  ksoftirqd/0
   213 ...
   214 giampaolo  31475  0.0  0.0   20760    3024 /dev/pts/0 Jun19   00:00  python2.4
   215 giampaolo  31721  0.0  2.2  773060  181896 ?          00:04   10:30  chrome
   216 root       31763  0.0  0.0       0       0 ?          00:05   00:00  kworker/0:1
   217 >>>

mercurial