nvitop.host module
Shortcuts for package psutil
.
psutil
is a cross-platform library for retrieving information on running processes and system
utilization (CPU, memory, disks, network, sensors) in Python.
- nvitop.host.PsutilError
alias of
Error
- exception nvitop.host.NoSuchProcess(pid, name=None, msg=None)
Bases:
Error
Exception raised when a process with a certain PID doesn’t or no longer exists.
- exception nvitop.host.ZombieProcess(pid, name=None, ppid=None, msg=None)
Bases:
NoSuchProcess
Exception raised when querying a zombie process. This is raised on macOS, BSD and Solaris only, and not always: depending on the query the OS may be able to succeed anyway. On Linux all zombie processes are querable (hence this is never raised). Windows doesn’t have zombie processes.
- exception nvitop.host.AccessDenied(pid=None, name=None, msg=None)
Bases:
Error
Exception raised when permission to perform an action is denied.
- exception nvitop.host.TimeoutExpired(seconds, pid=None, name=None)
Bases:
Error
Raised on Process.wait(timeout) if timeout expires and process is still alive.
- class nvitop.host.Process(pid=None)[source]
Bases:
object
Represents an OS process with the given PID. If PID is omitted current process PID (os.getpid()) is used. Raise NoSuchProcess if PID does not exist.
Note that most of the methods of this class do not make sure the PID of the process being queried has been reused over time. That means you might end up retrieving an information referring to another process in case the original one this instance refers to is gone in the meantime.
The only exceptions for which process identity is pre-emptively checked and guaranteed are:
parent()
children()
nice() (set)
ionice() (set)
rlimit() (set)
cpu_affinity (set)
suspend()
resume()
send_signal()
terminate()
kill()
- To prevent this problem for all other methods you can:
use is_running() before querying the process
if you’re continuously iterating over a set of Process instances use process_iter() which pre-emptively checks process identity for every yielded instance
- property pid
The process PID.
- oneshot()[source]
Utility context manager which considerably speeds up the retrieval of multiple process information at the same time.
Internally different process info (e.g. name, ppid, uids, gids, …) may be fetched by using the same routine, but only one information is returned and the others are discarded. When using this context manager the internal routine is executed once (in the example below on name()) and the other info are cached.
The cache is cleared when exiting the context manager block. The advice is to use this every time you retrieve more than one information about the process. If you’re lucky, you’ll get a hell of a speedup.
>>> import psutil >>> p = psutil.Process() >>> with p.oneshot(): ... p.name() # collect multiple info ... p.cpu_times() # return cached value ... p.cpu_percent() # return cached value ... p.create_time() # return cached value ... >>>
- as_dict(attrs=None, ad_value=None)[source]
Utility method returning process information as a hashable dictionary. If attrs is specified it must be a list of strings reflecting available Process class’ attribute names (e.g. [‘cpu_times’, ‘name’]) else all public (read only) attributes are assumed. ad_value is the value which gets assigned in case AccessDenied or ZombieProcess exception is raised when retrieving that particular process information.
- parent()[source]
Return the parent process as a Process object pre-emptively checking whether PID has been reused. If no parent is known return None.
- parents()[source]
Return the parents of this process as a list of Process instances. If no parents are known return an empty list.
- is_running()[source]
Return whether this process is running. It also checks if PID has been reused by another process in which case return False.
- exe()[source]
The process executable as an absolute path. May also be an empty string. The return value is cached after first call.
- username()[source]
The name of the user that owns the process. On UNIX this is calculated by using real process uid.
- create_time()[source]
The process creation time as a floating point number expressed in seconds since the epoch. The return value is cached after first call.
- io_counters()[source]
Return process I/O statistics as a (read_count, write_count, read_bytes, write_bytes) namedtuple. Those are the number of read/write calls performed and the amount of bytes read and written by the process.
- ionice(ioclass=None, value=None)[source]
Get or set process I/O niceness (priority).
On Linux ioclass is one of the IOPRIO_CLASS_* constants. value is a number which goes from 0 to 7. The higher the value, the lower the I/O priority of the process.
On Windows only ioclass is used and it can be set to 2 (normal), 1 (low) or 0 (very low).
Available on Linux and Windows > Vista only.
- rlimit(resource, limits=None)[source]
Get or set process resource limits as a (soft, hard) tuple.
resource is one of the RLIMIT_* constants. limits is supposed to be a (soft, hard) tuple.
See “man prlimit” for further info. Available on Linux and FreeBSD only.
- cpu_affinity(cpus=None)[source]
Get or set process CPU affinity. If specified, cpus must be a list of CPUs for which you want to set the affinity (e.g. [0, 1]). If an empty list is passed, all egible CPUs are assumed (and set). (Windows, Linux and BSD only).
- cpu_num()[source]
Return what CPU this process is currently running on. The returned number should be <= psutil.cpu_count() and <= len(psutil.cpu_percent(percpu=True)). It may be used in conjunction with psutil.cpu_percent(percpu=True) to observe the system workload distributed across CPUs.
- environ()[source]
The environment variables of the process as a dict. Note: this might not reflect changes made after the process started.
- num_ctx_switches()[source]
Return the number of voluntary and involuntary context switches performed by this process.
- threads()[source]
Return threads opened by process as a list of (id, user_time, system_time) namedtuples representing thread id and thread CPU times (user/system). On OpenBSD this method requires root access.
- children(recursive=False)[source]
Return the children of this process as a list of Process instances, pre-emptively checking whether PID has been reused. If recursive is True return all the parent descendants.
Example (A == this process):
- A ─┐
│ ├─ B (child) ─┐ │ └─ X (grandchild) ─┐ │ └─ Y (great grandchild) ├─ C (child) └─ D (child)
>>> import psutil >>> p = psutil.Process() >>> p.children() B, C, D >>> p.children(recursive=True) B, X, Y, C, D
Note that in the example above if process X disappears process Y won’t be listed as the reference to process A is lost.
- cpu_percent(interval=None)[source]
Return a float representing the current process CPU utilization as a percentage.
When interval is 0.0 or None (default) compares process times to system CPU times elapsed since last call, returning immediately (non-blocking). That means that the first time this is called it will return a meaningful 0.0 value.
When interval is > 0.0 compares process times to system CPU times elapsed before and after the interval (blocking).
In this case is recommended for accuracy that this function be called with at least 0.1 seconds between calls.
A value > 100.0 can be returned in case of processes running multiple threads on different CPU cores.
The returned value is explicitly NOT split evenly between all available logical CPUs. This means that a busy loop process running on a system with 2 logical CPUs will be reported as having 100% CPU utilization instead of 50%.
Examples
>>> import psutil >>> p = psutil.Process(os.getpid()) >>> # blocking >>> p.cpu_percent(interval=1) 2.0 >>> # non-blocking (percentage since last call) >>> p.cpu_percent(interval=None) 2.9 >>>
- cpu_times()[source]
Return a (user, system, children_user, children_system) namedtuple representing the accumulated process time, in seconds. This is similar to os.times() but per-process. On macOS and Windows children_user and children_system are always set to 0.
- memory_info()[source]
Return a namedtuple with variable fields depending on the platform, representing memory information about the process.
The “portable” fields available on all platforms are rss and vms.
All numbers are expressed in bytes.
- memory_info_ex()[source]
memory_info_ex() is deprecated and will be removed; use memory_info() instead
- memory_full_info()[source]
This method returns the same information as memory_info(), plus, on some platform (Linux, macOS, Windows), also provides additional metrics (USS, PSS and swap). The additional metrics provide a better representation of actual process memory usage.
Namely USS is the memory which is unique to a process and which would be freed if the process was terminated right now.
It does so by passing through the whole process address. As such it usually requires higher user privileges than memory_info() and is considerably slower.
- memory_percent(memtype='rss')[source]
Compare process memory to total physical system memory and calculate process memory utilization as a percentage. memtype argument is a string that dictates what type of process memory you want to compare against (defaults to “rss”). The list of available strings can be obtained like this:
>>> psutil.Process().memory_info()._fields ('rss', 'vms', 'shared', 'text', 'lib', 'data', 'dirty', 'uss', 'pss')
- memory_maps(grouped=True)[source]
Return process’ mapped memory regions as a list of namedtuples whose fields are variable depending on the platform.
If grouped is True the mapped regions with the same ‘path’ are grouped together and the different memory fields are summed.
If grouped is False every mapped region is shown as a single entity and the namedtuple will also include the mapped region’s address space (‘addr’) and permission set (‘perms’).
- open_files()[source]
Return files opened by process as a list of (path, fd) namedtuples including the absolute file name and file descriptor number.
- connections(kind='inet')[source]
Return socket connections opened by process as a list of (fd, family, type, laddr, raddr, status) namedtuples. The kind parameter filters for connections that match the following criteria:
Kind Value
Connections using
inet inet4 inet6 tcp tcp4 tcp6 udp udp4 udp6 unix all
IPv4 and IPv6 IPv4 IPv6 TCP TCP over IPv4 TCP over IPv6 UDP UDP over IPv4 UDP over IPv6 UNIX socket (both UDP and TCP protocols) the sum of all the possible families and protocols
- send_signal(sig)[source]
Send a signal sig to process pre-emptively checking whether PID has been reused (see signal module constants) . On Windows only SIGTERM is valid and is treated as an alias for kill().
- suspend()[source]
Suspend process execution with SIGSTOP pre-emptively checking whether PID has been reused. On Windows this has the effect of suspending all process threads.
- resume()[source]
Resume process execution with SIGCONT pre-emptively checking whether PID has been reused. On Windows this has the effect of resuming all process threads.
- terminate()[source]
Terminate the process with SIGTERM pre-emptively checking whether PID has been reused. On Windows this is an alias for kill().
- kill()[source]
Kill the current process with SIGKILL pre-emptively checking whether PID has been reused.
- wait(timeout=None)[source]
Wait for process to terminate and, if process is a children of os.getpid(), also return its exit code, else None. On Windows there’s no such limitation (exit code is always returned).
If the process is already terminated immediately return None instead of raising NoSuchProcess.
If timeout (in seconds) is specified and process is still alive raise TimeoutExpired.
To wait for multiple Process(es) use psutil.wait_procs().
- class nvitop.host.Popen(*args, **kwargs)[source]
Bases:
Process
Same as subprocess.Popen, but in addition it provides all psutil.Process methods in a single class. For the following methods which are common to both classes, psutil implementation takes precedence:
send_signal()
terminate()
kill()
This is done in order to avoid killing another process in case its PID has been reused, fixing BPO-6973.
>>> import psutil >>> from subprocess import PIPE >>> p = psutil.Popen(["python", "-c", "print 'hi'"], stdout=PIPE) >>> p.name() 'python' >>> p.uids() user(real=1000, effective=1000, saved=1000) >>> p.username() 'giampaolo' >>> p.communicate() ('hi', None) >>> p.terminate() >>> p.wait(timeout=2) 0 >>>
- wait(timeout=None)[source]
Wait for process to terminate and, if process is a children of os.getpid(), also return its exit code, else None. On Windows there’s no such limitation (exit code is always returned).
If the process is already terminated immediately return None instead of raising NoSuchProcess.
If timeout (in seconds) is specified and process is still alive raise TimeoutExpired.
To wait for multiple Process(es) use psutil.wait_procs().
- nvitop.host.pid_exists(pid)[source]
Return True if given PID exists in the current process list. This is faster than doing “pid in psutil.pids()” and should be preferred.
- nvitop.host.process_iter(attrs=None, ad_value=None)[source]
Return a generator yielding a Process instance for all running processes.
Every new Process instance is only created once and then cached into an internal table which is updated every time this is used.
Cached Process instances are checked for identity so that you’re safe in case a PID has been reused by another process, in which case the cached instance is updated.
The sorting order in which processes are yielded is based on their PIDs.
attrs and ad_value have the same meaning as in Process.as_dict(). If attrs is specified as_dict() is called and the resulting dict is stored as a ‘info’ attribute attached to returned Process instance. If attrs is an empty list it will retrieve all process info (slow).
- nvitop.host.wait_procs(procs, timeout=None, callback=None)[source]
Convenience function which waits for a list of processes to terminate.
Return a (gone, alive) tuple indicating which processes are gone and which ones are still alive.
The gone ones will have a new returncode attribute indicating process exit status (may be None).
callback is a function which gets called every time a process terminates (a Process instance is passed as callback argument).
Function will return as soon as all processes terminate or when timeout occurs. Differently from Process.wait() it will not raise TimeoutExpired if timeout occurs.
Typical use case is:
send SIGTERM to a list of processes
give them some time to terminate
send SIGKILL to those ones which are still alive
Example:
>>> def on_terminate(proc): ... print("process {} terminated".format(proc)) ... >>> for p in procs: ... p.terminate() ... >>> gone, alive = wait_procs(procs, timeout=3, callback=on_terminate) >>> for p in alive: ... p.kill()
- nvitop.host.virtual_memory()[source]
Return statistics about system memory usage as a namedtuple including the following fields, expressed in bytes:
total: total physical memory available.
available: the memory that can be given instantly to processes without the system going into swap. This is calculated by summing different memory values depending on the platform and it is supposed to be used to monitor actual memory usage in a cross platform fashion.
percent: the percentage usage calculated as (total - available) / total * 100
- used:
memory used, calculated differently depending on the platform and designed for informational purposes only: macOS: active + wired BSD: active + wired + cached Linux: total - free
free: memory not being used at all (zeroed) that is readily available; note that this doesn’t reflect the actual memory available (use ‘available’ instead)
Platform-specific fields:
active (UNIX): memory currently in use or very recently used, and so it is in RAM.
inactive (UNIX): memory that is marked as not used.
buffers (BSD, Linux): cache for things like file system metadata.
cached (BSD, macOS): cache for various things.
wired (macOS, BSD): memory that is marked to always stay in RAM. It is never moved to disk.
shared (BSD): memory that may be simultaneously accessed by multiple processes.
The sum of ‘used’ and ‘available’ does not necessarily equal total. On Windows ‘available’ and ‘free’ are the same.
- nvitop.host.swap_memory()[source]
Return system swap memory statistics as a namedtuple including the following fields:
total: total swap memory in bytes
used: used swap memory in bytes
free: free swap memory in bytes
percent: the percentage usage
sin: no. of bytes the system has swapped in from disk (cumulative)
sout: no. of bytes the system has swapped out from disk (cumulative)
‘sin’ and ‘sout’ on Windows are meaningless and always set to 0.
- nvitop.host.cpu_times(percpu=False)[source]
Return system-wide CPU times as a namedtuple. Every CPU time represents the seconds the CPU has spent in the given mode. The namedtuple’s fields availability varies depending on the platform:
user
system
idle
nice (UNIX)
iowait (Linux)
irq (Linux, FreeBSD)
softirq (Linux)
steal (Linux >= 2.6.11)
guest (Linux >= 2.6.24)
guest_nice (Linux >= 3.2.0)
When percpu is True return a list of namedtuples for each CPU. First element of the list refers to first CPU, second element to second CPU and so on. The order of the list is consistent across calls.
- nvitop.host.cpu_percent(interval=None, percpu=False)[source]
Return a float representing the current system-wide CPU utilization as a percentage.
When interval is > 0.0 compares system CPU times elapsed before and after the interval (blocking).
When interval is 0.0 or None compares system CPU times elapsed since last call or module import, returning immediately (non blocking). That means the first time this is called it will return a meaningless 0.0 value which you should ignore. In this case is recommended for accuracy that this function be called with at least 0.1 seconds between calls.
When percpu is True returns a list of floats representing the utilization as a percentage for each CPU. First element of the list refers to first CPU, second element to second CPU and so on. The order of the list is consistent across calls.
Examples
>>> # blocking, system-wide >>> psutil.cpu_percent(interval=1) 2.0 >>> >>> # blocking, per-cpu >>> psutil.cpu_percent(interval=1, percpu=True) [2.0, 1.0] >>> >>> # non-blocking (percentage since last call) >>> psutil.cpu_percent(interval=None) 2.9 >>>
- nvitop.host.cpu_times_percent(interval=None, percpu=False)[source]
Same as cpu_percent() but provides utilization percentages for each specific CPU time as is returned by cpu_times(). For instance, on Linux we’ll get:
>>> cpu_times_percent() cpupercent(user=4.8, nice=0.0, system=4.8, idle=90.5, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0) >>>
interval and percpu arguments have the same meaning as in cpu_percent().
- nvitop.host.cpu_count(logical=True)[source]
Return the number of logical CPUs in the system (same as os.cpu_count() in Python 3.4).
If logical is False return the number of physical cores only (e.g. hyper thread CPUs are excluded).
Return None if undetermined.
The return value is cached after first call. If desired cache can be cleared like this:
>>> psutil.cpu_count.cache_clear()
- nvitop.host.net_io_counters(pernic=False, nowrap=True)[source]
Return network I/O statistics as a namedtuple including the following fields:
bytes_sent: number of bytes sent
bytes_recv: number of bytes received
packets_sent: number of packets sent
packets_recv: number of packets received
errin: total number of errors while receiving
errout: total number of errors while sending
dropin: total number of incoming packets which were dropped
- dropout: total number of outgoing packets which were dropped
(always 0 on macOS and BSD)
If pernic is True return the same information for every network interface installed on the system as a dictionary with network interface names as the keys and the namedtuple described above as the values.
If nowrap is True it detects and adjust the numbers which overflow and wrap (restart from 0) and add “old value” to “new value” so that the returned numbers will always be increasing or remain the same, but never decrease. “net_io_counters.cache_clear()” can be used to invalidate the cache.
- nvitop.host.net_connections(kind='inet')[source]
Return system-wide socket connections as a list of (fd, family, type, laddr, raddr, status, pid) namedtuples. In case of limited privileges ‘fd’ and ‘pid’ may be set to -1 and None respectively. The kind parameter filters for connections that fit the following criteria:
Kind Value
Connections using
inet inet4 inet6 tcp tcp4 tcp6 udp udp4 udp6 unix all
IPv4 and IPv6 IPv4 IPv6 TCP TCP over IPv4 TCP over IPv6 UDP UDP over IPv4 UDP over IPv6 UNIX socket (both UDP and TCP protocols) the sum of all the possible families and protocols
On macOS this function requires root privileges.
- nvitop.host.net_if_addrs()[source]
Return the addresses associated to each NIC (network interface card) installed on the system as a dictionary whose keys are the NIC names and value is a list of namedtuples for each address assigned to the NIC. Each namedtuple includes 5 fields:
- family: can be either socket.AF_INET, socket.AF_INET6 or
psutil.AF_LINK, which refers to a MAC address.
address: is the primary address and it is always set.
netmask: and ‘broadcast’ and ‘ptp’ may be None.
- ptp: stands for “point to point” and references the
destination address on a point to point interface (typically a VPN).
broadcast: and ptp are mutually exclusive.
Note: you can have more than one address of the same family associated with each interface.
- nvitop.host.net_if_stats()[source]
Return information about each NIC (network interface card) installed on the system as a dictionary whose keys are the NIC names and value is a namedtuple with the following fields:
isup: whether the interface is up (bool)
- duplex: can be either NIC_DUPLEX_FULL, NIC_DUPLEX_HALF or
NIC_DUPLEX_UNKNOWN
- speed: the NIC speed expressed in mega bits (MB); if it can’t
be determined (e.g. ‘localhost’) it will be set to 0.
mtu: the maximum transmission unit expressed in bytes.
- nvitop.host.disk_io_counters(perdisk=False, nowrap=True)[source]
Return system disk I/O statistics as a namedtuple including the following fields:
read_count: number of reads
write_count: number of writes
read_bytes: number of bytes read
write_bytes: number of bytes written
read_time: time spent reading from disk (in ms)
write_time: time spent writing to disk (in ms)
Platform specific:
busy_time: (Linux, FreeBSD) time spent doing actual I/Os (in ms)
read_merged_count (Linux): number of merged reads
write_merged_count (Linux): number of merged writes
If perdisk is True return the same information for every physical disk installed on the system as a dictionary with partition names as the keys and the namedtuple described above as the values.
If nowrap is True it detects and adjust the numbers which overflow and wrap (restart from 0) and add “old value” to “new value” so that the returned numbers will always be increasing or remain the same, but never decrease. “disk_io_counters.cache_clear()” can be used to invalidate the cache.
On recent Windows versions ‘diskperf -y’ command may need to be executed first otherwise this function won’t find any disk.
- nvitop.host.disk_partitions(all=False)[source]
Return mounted partitions as a list of (device, mountpoint, fstype, opts) namedtuple. ‘opts’ field is a raw string separated by commas indicating mount options which may vary depending on the platform.
If all parameter is False return physical devices only and ignore all others.
- nvitop.host.disk_usage(path)[source]
Return disk usage statistics about the given path as a namedtuple including total, used and free space expressed in bytes plus the percentage usage.
- nvitop.host.users()[source]
Return users currently connected on the system as a list of namedtuples including the following fields.
user: the name of the user
terminal: the tty or pseudo-tty associated with the user, if any.
host: the host name associated with the entry, if any.
started: the creation time as a floating point number expressed in seconds since the epoch.
- nvitop.host.cpu_freq(percpu=False)[source]
Return CPU frequency as a namedtuple including current, min and max frequency expressed in Mhz.
If percpu is True and the system supports per-cpu frequency retrieval (Linux only) a list of frequencies is returned for each CPU. If not a list with one element is returned.
- nvitop.host.getloadavg()
Return average recent system load information.
Return the number of processes in the system run queue averaged over the last 1, 5, and 15 minutes as a tuple of three floats. Raises OSError if the load average was unobtainable.
- nvitop.host.sensors_temperatures(fahrenheit=False)[source]
Return hardware temperatures. Each entry is a namedtuple representing a certain hardware sensor (it may be a CPU, an hard disk or something else, depending on the OS and its configuration). All temperatures are expressed in celsius unless fahrenheit is set to True.
- nvitop.host.sensors_fans()[source]
Return fans speed. Each entry is a namedtuple representing a certain hardware sensor. All speed are expressed in RPM (rounds per minute).
- nvitop.host.sensors_battery()[source]
Return battery information. If no battery is installed returns None.
percent: battery power left as a percentage.
- secsleft: a rough approximation of how many seconds are left
before the battery runs out of power. May be POWER_TIME_UNLIMITED or POWER_TIME_UNLIMITED.
power_plugged: True if the AC power cable is connected.
- nvitop.host.memory_percent() float [source]
The percentage usage of virtual memory, calculated as
(total - available) / total * 100
.
- nvitop.host.swap_percent() float [source]
The percentage usage of virtual memory, calculated as
used / total * 100
.
- nvitop.host.ppid_map()[source]
Obtain a
{pid: ppid, ...}
dict for all running processes in one shot.
- nvitop.host.reverse_ppid_map() dict[int, list[int]] [source]
Obtain a
{ppid: [pid, ...], ...}
dict for all running processes in one shot.
- nvitop.host.WINDOWS_SUBSYSTEM_FOR_LINUX = None
The Linux distribution name of the Windows Subsystem for Linux.