Skip to content

Commit

Permalink
#615: disable memory_maps on OpenBSD
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Nov 9, 2015
1 parent cc6b10e commit 1b7b42b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 26 deletions.
2 changes: 2 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,8 @@ Process class
...]
>>>

Availability: All platforms except OpenBSD.

.. method:: children(recursive=False)

Return the children of this process as a list of :Class:`Process` objects,
Expand Down
53 changes: 27 additions & 26 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
_TOTAL_PHYMEM = None
_POSIX = os.name == 'posix'
_WINDOWS = os.name == 'nt'
_OPENBSD = sys.platform.startswith("openbsd")
_timer = getattr(time, 'monotonic', time.time)


Expand Down Expand Up @@ -962,32 +963,33 @@ def memory_percent(self):
except ZeroDivisionError:
return 0.0

def memory_maps(self, grouped=True):
"""Return process' mapped memory regions as a list of namedtuples
whose fields are variable depending on the platform.
if not _OPENBSD:
def memory_maps(self, grouped=True):
"""Return process' mapped memory regions as a list of namedtuples
whose fields are variable depending on the platform.
If 'grouped' is True the mapped regions with the same 'path'
are grouped together and the different memory fields are summed.
If 'grouped' is True the mapped regions with the same 'path'
are grouped together and the different memory fields are summed.
If 'grouped' is False every mapped region is shown as a single
entity and the namedtuple will also include the mapped region's
address space ('addr') and permission set ('perms').
"""
it = self._proc.memory_maps()
if grouped:
d = {}
for tupl in it:
path = tupl[2]
nums = tupl[3:]
try:
d[path] = map(lambda x, y: x + y, d[path], nums)
except KeyError:
d[path] = nums
nt = _psplatform.pmmap_grouped
return [nt(path, *d[path]) for path in d] # NOQA
else:
nt = _psplatform.pmmap_ext
return [nt(*x) for x in it]
If 'grouped' is False every mapped region is shown as a single
entity and the namedtuple will also include the mapped region's
address space ('addr') and permission set ('perms').
"""
it = self._proc.memory_maps()
if grouped:
d = {}
for tupl in it:
path = tupl[2]
nums = tupl[3:]
try:
d[path] = map(lambda x, y: x + y, d[path], nums)
except KeyError:
d[path] = nums
nt = _psplatform.pmmap_grouped
return [nt(path, *d[path]) for path in d] # NOQA
else:
nt = _psplatform.pmmap_ext
return [nt(*x) for x in it]

def open_files(self):
"""Return files opened by process as a list of
Expand Down Expand Up @@ -1029,8 +1031,7 @@ def _send_signal(self, sig):
os.kill(self.pid, sig)
except OSError as err:
if err.errno == errno.ESRCH:
if (sys.platform.startswith("openbsd") and \
pid_exists(self.pid)):
if _OPENBSD and pid_exists(self.pid):
# We do this because os.kill() lies in case of
# zombie processes.
raise ZombieProcess(self.pid, self._name, self._ppid)
Expand Down
5 changes: 5 additions & 0 deletions psutil/_psbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ def threads(self):
for thread_id, utime, stime in rawlist:
ntuple = _common.pthread(thread_id, utime, stime)
retlist.append(ntuple)
if OPENBSD:
# On OpenBSD the underlying C function does not raise NSP
# in case the process is gone (and the returned list may
# incomplete).
self.name() # raise NSP if the process disappeared on us
return retlist

@wrap_exceptions
Expand Down
7 changes: 7 additions & 0 deletions test/test_psutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -1661,6 +1661,7 @@ def test_memory_info(self):
# def test_memory_info_ex(self):
# # tested later in fetch all test suite

@unittest.skipIf(OPENBSD, "not available on OpenBSD")
def test_memory_maps(self):
p = psutil.Process()
maps = p.memory_maps()
Expand Down Expand Up @@ -2284,6 +2285,11 @@ def test_halfway_terminated_process(self):
name)
except psutil.NoSuchProcess:
pass
except psutil.AccessDenied:
if OPENBSD and name in ('threads', 'num_threads'):
pass
else:
raise
except NotImplementedError:
pass
else:
Expand Down Expand Up @@ -3096,6 +3102,7 @@ def test_netstat(self):
def test_ifconfig(self):
self.assert_stdout('ifconfig.py')

@unittest.skipIf(OPENBSD, "OpenBSD does not support memory maps")
def test_pmap(self):
self.assert_stdout('pmap.py', args=str(os.getpid()))

Expand Down

0 comments on commit 1b7b42b

Please sign in to comment.