cup.res package


cup.res.linux module

Provie Linux Resource/State Info Query


Return the system boot time expressed in seconds since the epoch.

Returns:return boot time (seconds) since epoch
Parameters:raw – if raw is True, will use Byte as the measure. Automatically use MB/GB otherwise.
Returns:return a dict of disk usage
Returns:get disk info of the system
class cup.res.linux.CPUInfo[source]

Bases: cup.res.linux.CPUInfo

CPUInfo is used for get_cpu_usage function. The following attr will be in the namedtuple: usr, nice, system, idle, iowait, irq, softirq, steal, guest


import cup
# count cpu usage
from cup.res import linux
cpuinfo = linux.get_cpu_usage(intvl_in_sec=60)
print cpuinfo.usr

get cpu usage statistics during a time period (intvl_in_sec), return a namedtuple CPUInfo


get cpu nums

cup.res.linux.get_cpu_core_usage(coreindex, intvl_in_sec=1)[source]
Parameters:index – cpu core index
class cup.res.linux.MemInfo[source]

Bases: cup.res.linux.vmem

get_meminfo will get memory info (a namedtuple returned:
total, available, percent, used, free, active, inactive, buffers, cached)


from cup.res import linux
meminfo = linux.get_meminfo()

get system memory info

class cup.res.linux.SWAPINFO[source]

Bases: cup.res.linux.SwapInfo

get_swapinfo will return a SWAPINFO


get swamp info of the system


get network statistics with a list of namedtuple (bytes_sent, bytes_recv, packets_sent, packets_recv, errin, errout, dropin, dropout)

# return a dict like below:
         235805206817, 235805206817, 315060887, 315060887, 0, 0, 0, 0
         18508976300272, 8079464483699, 32776530804,
         32719666038, 0, 0, 708015, 0
         0, 0, 0, 0, 0, 0, 0, 0

get network interface statistics by a interface (eth0, e,g,)

cup.res.linux.get_net_transmit_speed(str_interface, intvl_in_sec=1)[source]

get network interface write/read speed


from cup.res import linux
print(linux.get_net_transmit_speed('eth1', 5))

Returns a list of PIDs currently running on the system.


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.


1. the origion use a check_running function to check whether PID has been reused by another process in which case yield a new Process instance hint:i did not use check_running function because the container of pid is set

2. the origion use a sorted list(_pmap.items()) + list(dict.fromkeys(new_pids).items() to get pid and proc to make res.proc is only a instance of a pid Process hint(bugs):i did not use fromkeys(new_pids) because i did not get the meanning of using proc

class cup.res.linux.Process(pid)[source]

Bases: object

Process info query (given a pid)


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)

return -1 represent the process does not exists

get_connections(*args, **kwargs)[source]

get network connection info, each item contains a namedtuple (fd family type laddr raddr status)

Parameters:kind – kind=’inet’ by default
Returns:a list of network connection info
get_cpu_times(*args, **kwargs)[source]

get cpu times, return with a namedtuple (utime, stime)

get_cpu_usage(*args, **kwargs)[source]

get cpu usage

get_ext_memory_info(*args, **kwargs)[source]

return namedtuple with FIELDs below:


from cup.res import linux
process = linux.Process(pid)
print process.rss, process.dirty
rss resident set size / RES
vms total program size size VIRT
shared shared pages (from shared mappings) / SHR
text text (‘code’) trs CODE
lib library (unused in Linux 2.6) lrs /
data data + stack drs DATA
dirty dirty pages (unused in Linux 2.6) dt /
get_memory_info(*args, **kwargs)[source]

get memory info, return with a namedtuple ( rss vms shared text lib data dirty)


get memory map (from /proc smaps file)

get_num_ctx_switches(*args, **kwargs)[source]

get process context switch info (from /proc status file), return with a namedtuple (vol, unvol)

get_num_fds(*args, **kwargs)[source]

get opened file descriptor num

get_open_files(*args, **kwargs)[source]

get opened file info

get_process_cmdline(*args, **kwargs)[source]

get cmdline

get_process_create_time(*args, **kwargs)[source]

get process create time

get_process_cwd(*args, **kwargs)[source]

get process current working direcotry


get executable info of the process. If the process is a daemon, use get_process_name instead!

get_process_gids(*args, **kwargs)[source]

get process gid, namedtuple will be returned (with attrs .real .effective .saved)

get_process_io_counters(*args, **kwargs)[source]

get io statistics info of network adapters.

get_process_name(*args, **kwargs)[source]

get process name of the process (for daemon process only)

get_process_nice(*args, **kwargs)[source]

get process nice

get_process_num_threads(*args, **kwargs)[source]

get threads num of this process

get_process_ppid(*args, **kwargs)[source]

get parent process id

get_process_status(*args, **kwargs)[source]

get status of the current process (info from /proc/xxx/status)

get_process_threads(*args, **kwargs)[source]

get threads that is current using, return with a namedtuple ( thread_id, utime, stime)

get_process_uids(*args, **kwargs)[source]

get uid info of the process, will return a namedtuple


return process group id (not pid, not gid either)


alias of mmap


alias of mmap


get linux kernel verions, e.g.(‘2’, ‘6’, ‘32’):

cup.res.mac module

query mac resource module


get kernel info of mac. e.g.(‘16’, ‘7’, ‘0’):


return cpu num

Parameters:raw – measure set to Byte if Raw is True
Returns:a py dict: { ‘totalSpace’: xxx, ‘usedSpace’: xxx, ‘freeSpace’: xxx, ‘unit’: xxx }
Returns:get disk info from the current macOS
Raises:Exception – RuntimeError, if got no disk at all

Module contents

description:resource related module