summaryrefslogtreecommitdiff
path: root/psutil/_psosx.py
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2016-10-07 03:03:21 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2016-10-07 03:03:21 +0200
commit7b2a6b370d59f8c731aefb5f3c6e12d18e194ade (patch)
tree013d077db895daf2299802fb2962e5efe903d25a /psutil/_psosx.py
parent8da5114649101fdbd224847dcf7ed008dd9885e7 (diff)
downloadpsutil-7b2a6b370d59f8c731aefb5f3c6e12d18e194ade.tar.gz
#799 / OSX: implement oneshot for kinfo_proc info
Diffstat (limited to 'psutil/_psosx.py')
-rw-r--r--psutil/_psosx.py46
1 files changed, 36 insertions, 10 deletions
diff --git a/psutil/_psosx.py b/psutil/_psosx.py
index 75a75975..a0778d1f 100644
--- a/psutil/_psosx.py
+++ b/psutil/_psosx.py
@@ -15,6 +15,7 @@ from . import _psutil_osx as cext
from . import _psutil_posix as cext_posix
from ._common import conn_tmap
from ._common import isfile_strict
+from ._common import memoize_when_activated
from ._common import parse_environ_block
from ._common import sockfam_to_enum
from ._common import socktype_to_enum
@@ -56,6 +57,19 @@ PROC_STATUSES = {
cext.SZOMB: _common.STATUS_ZOMBIE,
}
+kinfo_proc_map = dict(
+ ppid=0,
+ ruid=1,
+ euid=2,
+ suid=3,
+ rgid=4,
+ egid=5,
+ sgid=6,
+ ttynr=7,
+ ctime=8,
+ status=9,
+)
+
scputimes = namedtuple('scputimes', ['user', 'nice', 'system', 'idle'])
svmem = namedtuple(
@@ -264,11 +278,17 @@ class Process(object):
self._name = None
self._ppid = None
+ @memoize_when_activated
+ def _get_kinfo_proc(self):
+ ret = cext.proc_kinfo_oneshot(self.pid)
+ assert len(ret) == len(kinfo_proc_map)
+ return ret
+
def oneshot_enter(self):
- pass
+ self._get_kinfo_proc.cache_activate()
def oneshot_exit(self):
- pass
+ self._get_kinfo_proc.cache_deactivate()
@wrap_exceptions
def name(self):
@@ -292,7 +312,7 @@ class Process(object):
@wrap_exceptions
def ppid(self):
- self._ppid = cext.proc_ppid(self.pid)
+ self._ppid = self._get_kinfo_proc()[kinfo_proc_map['ppid']]
return self._ppid
@wrap_exceptions
@@ -301,17 +321,23 @@ class Process(object):
@wrap_exceptions
def uids(self):
- real, effective, saved = cext.proc_uids(self.pid)
- return _common.puids(real, effective, saved)
+ rawtuple = self._get_kinfo_proc()
+ return _common.puids(
+ rawtuple[kinfo_proc_map['ruid']],
+ rawtuple[kinfo_proc_map['euid']],
+ rawtuple[kinfo_proc_map['suid']])
@wrap_exceptions
def gids(self):
- real, effective, saved = cext.proc_gids(self.pid)
- return _common.pgids(real, effective, saved)
+ rawtuple = self._get_kinfo_proc()
+ return _common.puids(
+ rawtuple[kinfo_proc_map['rgid']],
+ rawtuple[kinfo_proc_map['egid']],
+ rawtuple[kinfo_proc_map['sgid']])
@wrap_exceptions
def terminal(self):
- tty_nr = cext.proc_tty_nr(self.pid)
+ tty_nr = self._get_kinfo_proc()[kinfo_proc_map['ttynr']]
tmap = _psposix.get_terminal_map()
try:
return tmap[tty_nr]
@@ -337,7 +363,7 @@ class Process(object):
@wrap_exceptions
def create_time(self):
- return cext.proc_create_time(self.pid)
+ return self._get_kinfo_proc()[kinfo_proc_map['ctime']]
@wrap_exceptions
def num_ctx_switches(self):
@@ -399,7 +425,7 @@ class Process(object):
@wrap_exceptions
def status(self):
- code = cext.proc_status(self.pid)
+ code = self._get_kinfo_proc()[kinfo_proc_map['status']]
# XXX is '?' legit? (we're not supposed to return it anyway)
return PROC_STATUSES.get(code, '?')