Skip to content

Commit

Permalink
#799: oneshot() BSD implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Aug 3, 2016
1 parent 38ac193 commit 8fea6e3
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 239 deletions.
73 changes: 59 additions & 14 deletions psutil/_psbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from . import _psutil_posix as cext_posix
from ._common import conn_tmap
from ._common import FREEBSD
from ._common import memoize_when_activated
from ._common import NETBSD
from ._common import OPENBSD
from ._common import sockfam_to_enum
Expand Down Expand Up @@ -96,6 +97,27 @@
PAGESIZE = os.sysconf("SC_PAGE_SIZE")
AF_LINK = cext_posix.AF_LINK

kinfo_proc_map = dict(
ppid=0,
status=1,
real_uid=2,
effective_uid=3,
saved_uid=4,
real_gid=5,
effective_gid=6,
saved_gid=7,
ttynr=8,
create_time=9,
ctx_switches_vol=10,
ctx_switches_unvol=11,
read_io_count=12,
write_io_count=13,
user_time=14,
sys_time=15,
ch_user_time=16,
ch_sys_time=17,
)


# =====================================================================
# --- named tuples
Expand Down Expand Up @@ -452,11 +474,16 @@ def __init__(self, pid):
self._name = None
self._ppid = None

@memoize_when_activated
def oneshot(self):
"""Retrieves multiple process info in one shot as a raw tuple."""
return cext.proc_oneshot_info(self.pid)

def oneshot_enter(self):
pass
self.oneshot.cache_activate()

def oneshot_exit(self):
pass
self.oneshot.cache_deactivate()

@wrap_exceptions
def name(self):
Expand Down Expand Up @@ -508,7 +535,7 @@ def cmdline(self):

@wrap_exceptions
def terminal(self):
tty_nr = cext.proc_tty_nr(self.pid)
tty_nr = self.oneshot()[kinfo_proc_map['ttynr']]
tmap = _psposix.get_terminal_map()
try:
return tmap[tty_nr]
Expand All @@ -517,22 +544,33 @@ def terminal(self):

@wrap_exceptions
def ppid(self):
self._ppid = cext.proc_ppid(self.pid)
self._ppid = self.oneshot()[kinfo_proc_map['ppid']]
return self._ppid

@wrap_exceptions
def uids(self):
real, effective, saved = cext.proc_uids(self.pid)
return _common.puids(real, effective, saved)
rawtuple = self.oneshot()
return _common.puids(
rawtuple[kinfo_proc_map['real_uid']],
rawtuple[kinfo_proc_map['effective_uid']],
rawtuple[kinfo_proc_map['saved_uid']])

@wrap_exceptions
def gids(self):
real, effective, saved = cext.proc_gids(self.pid)
return _common.pgids(real, effective, saved)
rawtuple = self.oneshot()
return _common.pgids(
rawtuple[kinfo_proc_map['real_gid']],
rawtuple[kinfo_proc_map['effective_gid']],
rawtuple[kinfo_proc_map['saved_gid']])

@wrap_exceptions
def cpu_times(self):
return _common.pcputimes(*cext.proc_cpu_times(self.pid))
rawtuple = self.oneshot()
return _common.pcputimes(
rawtuple[kinfo_proc_map['user_time']],
rawtuple[kinfo_proc_map['sys_time']],
rawtuple[kinfo_proc_map['ch_user_time']],
rawtuple[kinfo_proc_map['ch_sys_time']])

@wrap_exceptions
def memory_info(self):
Expand All @@ -542,7 +580,7 @@ def memory_info(self):

@wrap_exceptions
def create_time(self):
return cext.proc_create_time(self.pid)
return self.oneshot()[kinfo_proc_map['create_time']]

@wrap_exceptions
def num_threads(self):
Expand All @@ -554,7 +592,10 @@ def num_threads(self):

@wrap_exceptions
def num_ctx_switches(self):
return _common.pctxsw(*cext.proc_num_ctx_switches(self.pid))
rawtuple = self.oneshot()
return _common.pctxsw(
rawtuple[kinfo_proc_map['ctx_switches_vol']],
rawtuple[kinfo_proc_map['ctx_switches_unvol']])

@wrap_exceptions
def threads(self):
Expand Down Expand Up @@ -632,14 +673,18 @@ def nice_set(self, value):

@wrap_exceptions
def status(self):
code = cext.proc_status(self.pid)
code = self.oneshot()[kinfo_proc_map['status']]
# XXX is '?' legit? (we're not supposed to return it anyway)
return PROC_STATUSES.get(code, '?')

@wrap_exceptions
def io_counters(self):
rc, wc, rb, wb = cext.proc_io_counters(self.pid)
return _common.pio(rc, wc, rb, wb)
rawtuple = self.oneshot()
return _common.pio(
rawtuple[kinfo_proc_map['read_io_count']],
rawtuple[kinfo_proc_map['write_io_count']],
-1,
-1)

@wrap_exceptions
def cwd(self):
Expand Down
Loading

0 comments on commit 8fea6e3

Please sign in to comment.