cup.res package

description:

resource related module

Submodules

cup.res.linux module

Provie Linux Resource/State Info Query

class cup.res.linux.CPUInfo(usr, nice, system, idle, iowait, irq, softirq, steal, guest)[source]

Bases: 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

I.g.

import cup
# count cpu usage
from cup.res import linux
cpuinfo = linux.get_cpu_usage(intvl_in_sec=60)
print cpuinfo.usr
class cup.res.linux.MemInfo(total, available, percent, used, free, active, inactive, buffers, cached)[source]

Bases: vmem

get_meminfo will get memory info (a namedtuple returned:

total, available, percent, used, free, active, inactive, buffers, cached)

E.g.:

from cup.res import linux
meminfo = linux.get_meminfo()
print(meminfo.total)
print(meminfo.available)
class cup.res.linux.Process(pid)[source]

Bases: object

Process info query (given a pid)

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)
create_time()[source]

return -1 represent the process does not exists

get_connections(kind='inet')[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()[source]

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

get_cpu_usage(interval=0.5)[source]

get cpu usage

get_ext_memory_info()[source]

return namedtuple with FIELDs below:

Example:

from cup.res import linux
process = linux.Process(pid)
print process.rss, process.dirty

FIELD

DESCRIPTION

AKA

TOP

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()[source]

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

get_memory_maps()[source]

get memory map (from /proc smaps file)

get_num_ctx_switches()[source]

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

get_num_fds()[source]

get opened file descriptor num

get_open_files()[source]

get opened file info

get_process_cmdline()[source]

get cmdline

get_process_create_time()[source]

get process create time

get_process_cwd()[source]

get process current working direcotry

get_process_exe()[source]

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

get_process_gids()[source]

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

get_process_io_counters()[source]

get io statistics info of network adapters.

get_process_name()[source]

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

get_process_nice()[source]

get process nice

get_process_num_threads()[source]

get threads num of this process

get_process_ppid()[source]

get parent process id

get_process_status()[source]

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

get_process_threads()[source]

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

get_process_uids()[source]

get uid info of the process, will return a namedtuple

getpgid()[source]

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

nt_mmap_ext

alias of mmap

nt_mmap_grouped

alias of mmap

pid
class cup.res.linux.SWAPINFO(total, free, used, sin, sout)[source]

Bases: SwapInfo

get_swapinfo will return a SWAPINFO

cup.res.linux.boot_time()[source]

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

cup.res.linux.get_boottime_since_epoch()[source]
Returns:

return boot time (seconds) since epoch

cup.res.linux.get_cpu_core_usage(coreindex, intvl_in_sec=1)[source]
Parameters:

index – cpu core index

cup.res.linux.get_cpu_nums()[source]

get cpu nums

cup.res.linux.get_cpu_usage(intvl_in_sec=1)[source]

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

cup.res.linux.get_disk_info()[source]
Returns:

get disk info of the system

cup.res.linux.get_disk_usage_all(raw=False)[source]
Parameters:

raw – if raw is True, will use Byte as the measure. Automatically use MB/GB otherwise.

Returns:

return a dict of disk usage

cup.res.linux.get_kernel_version()[source]

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

cup.res.linux.get_meminfo()[source]

get system memory info

cup.res.linux.get_net_through(str_interface)[source]

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

E.g.

from cup.res import linux
print(linux.get_net_transmit_speed('eth1', 5))
cup.res.linux.get_swapinfo()[source]

get swamp info of the system

cup.res.linux.net_io_counters()[source]

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

Returns:

# return a dict like below:
{
    'lo':
     (
       235805206817, 235805206817, 315060887, 315060887, 0, 0, 0, 0
     ),
     'eth1':
     (
         18508976300272, 8079464483699, 32776530804,
         32719666038, 0, 0, 708015, 0
     ),
     'eth0':
     (
         0, 0, 0, 0, 0, 0, 0, 0
     )
}

cup.res.linux.pids()[source]

Returns a list of PIDs currently running on the system.

cup.res.linux.process_iter()[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.

yuetian:

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

cup.res.mac module

query mac resource module

cup.res.mac.get_cpu_nums()[source]

return cpu num

cup.res.mac.get_disk_info()[source]
Returns:

get disk info from the current macOS

Raises:

Exception – RuntimeError, if got no disk at all

cup.res.mac.get_disk_usage_all(raw=False)[source]
Parameters:

raw – measure set to Byte if Raw is True

Returns:

a py dict: { ‘totalSpace’: xxx, ‘usedSpace’: xxx, ‘freeSpace’: xxx, ‘unit’: xxx }

cup.res.mac.get_kernel_version()[source]

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