cup.res package

Submodules

cup.res.linux module

Provie Linux Resource/State Info Query

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

I.g.

import cup
# count cpu usage
from cup.res import linux
cpuinfo = linux.get_cpu_usage(intvl_in_sec=60)
print cpuinfo.usr
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_cpu_nums()[source]

get cpu nums

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)

E.g.:

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

get system memory info

class cup.res.linux.SWAPINFO[source]

Bases: cup.res.linux.SwapInfo

get_swapinfo will return a SWAPINFO

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)

example

{
    '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.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.

import cup
print cup.res.linux.get_net_transmit_speed('eth1', 5)
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

  1. 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)

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(*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_ext_memory_info(*args, **kwargs)[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(*args, **kwargs)[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(*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_process_exe()[source]

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

nt_mmap_ext

alias of mmap

nt_mmap_grouped

alias of mmap

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

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

cup.res.mac module

query mac resource module

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

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

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

return cpu num

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_disk_info()[source]
Returns:get disk info from the current macOS
Raises:Exception – RuntimeError, if got no disk at all

Module contents

description:resource related module