Skip to content

Commit

Permalink
#799 - oneshot / linux: speedup memory_full_info and memory_maps
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Nov 5, 2016
1 parent 4376fdf commit 5274e8a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ Process class
multiple process information at the same time.
Internally different process info (e.g. :meth:`name`, :meth:`ppid`,
:meth:`uids`, :meth:`create_time`, ...) may be fetched by using the same
routine, but only one information is returned and the others are discarded.
routine, but only one data is returned and the others are discarded.
When using this context manager the internal routine is executed once (in
the example below on :meth:`name()`) and the other info are cached.
The subsequent calls sharing the same internal routine will return the
Expand All @@ -796,8 +796,8 @@ Process class

Here's a list of methods which can take advantage of the speedup depending
on what platform you're on.
In the table below horizontal emtpy rows delimitate what process methods
can be efficiently grouped together internally.
In the table below horizontal emtpy rows indicate what process methods can
be efficiently grouped together internally.
The last column (speedup) shows an approximation of the speedup you can get
if you call all the methods together (best case scenario).

Expand Down Expand Up @@ -832,9 +832,9 @@ Process class
+------------------------------+-------------------------------+------------------------------+------------------------------+--------------------------+
| | | :meth:`uids` | :meth:`username` | :meth:`uids` |
+------------------------------+-------------------------------+------------------------------+------------------------------+--------------------------+
| | | :meth:`username` | | :meth:`username` |
| :meth:`memory_full_info` | | :meth:`username` | | :meth:`username` |
+------------------------------+-------------------------------+------------------------------+------------------------------+--------------------------+
| | | | | |
| :meth:`memory_maps` | | | | |
+------------------------------+-------------------------------+------------------------------+------------------------------+--------------------------+
| *speedup: +2.5x* | *speedup: +1.8x / +6.5x* | *speedup: +1.9x* | *speedup: +2.0x* | *speedup: +1.3x* |
+------------------------------+-------------------------------+------------------------------+------------------------------+--------------------------+
Expand Down
7 changes: 1 addition & 6 deletions psutil/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,24 +298,19 @@ def wrapper(self):
ret = cache[fun] = fun(self)
return ret

def cache_clear():
"""Clear cache."""
cache.clear()

def cache_activate():
"""Activate cache."""
wrapper.cache_activated = True

def cache_deactivate():
"""Deactivate and clear cache."""
wrapper.cache_activated = False
cache_clear()
cache.clear()

cache = {}
wrapper.cache_activated = False
wrapper.cache_activate = cache_activate
wrapper.cache_deactivate = cache_deactivate
wrapper.cache_clear = cache_clear
return wrapper


Expand Down
3 changes: 3 additions & 0 deletions psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,7 @@ def _read_status_file(self):
with open_binary("%s/%s/status" % (self._procfs_path, self.pid)) as f:
return f.read()

@memoize_when_activated
def _read_smaps_file(self):
with open_binary("%s/%s/smaps" % (self._procfs_path, self.pid),
buffering=BIGGER_FILE_BUFFERING) as f:
Expand All @@ -1144,10 +1145,12 @@ def _read_smaps_file(self):
def oneshot_enter(self):
self._parse_stat_file.cache_activate()
self._read_status_file.cache_activate()
self._read_smaps_file.cache_activate()

def oneshot_exit(self):
self._parse_stat_file.cache_deactivate()
self._read_status_file.cache_deactivate()
self._read_smaps_file.cache_deactivate()

@wrap_exceptions
def name(self):
Expand Down
2 changes: 2 additions & 0 deletions scripts/internal/bench_oneshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
names += [
'cpu_times',
'gids',
# 'memory_full_info',
# 'memory_maps',
'name',
'num_ctx_switches',
'num_threads',
Expand Down

0 comments on commit 5274e8a

Please sign in to comment.