Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
feat: make memusage's objgraph/dump_rpy_heap optional
Browse files Browse the repository at this point in the history
Closes #1091
  • Loading branch information
pjenvey authored and jrconlin committed Dec 7, 2017
1 parent ffa7865 commit 44007e4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
15 changes: 9 additions & 6 deletions autopush/memusage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
import tempfile
import zlib
from StringIO import StringIO
from typing import Optional # noqa

from autopush.gcdump import Stat


def memusage():
def memusage(do_dump_rpy_heap=True, do_objgraph=True):
"""Returning a str of memory usage stats"""
# type() -> str
# type: (Optional[bool], Optional[bool]) -> str
def trap_err(func, *args, **kwargs):
try:
return func(*args, **kwargs)
Expand All @@ -25,12 +26,14 @@ def trap_err(func, *args, **kwargs):
rusage = trap_err(resource.getrusage, resource.RUSAGE_SELF)
buf.writelines([repr(rusage), '\n\n'])
trap_err(pmap_extended, buf)
# dump rpython's heap before objgraph potentially pollutes the
# heap with its heavy workload
trap_err(dump_rpy_heap, buf)
if do_dump_rpy_heap:
# dump rpython's heap before objgraph potentially pollutes the
# heap with its heavy workload
trap_err(dump_rpy_heap, buf)
trap_err(get_stats_asmmemmgr, buf)
buf.write('\n\n')
trap_err(objgraph.show_most_common_types, limit=0, file=buf)
if do_objgraph:
trap_err(objgraph.show_most_common_types, limit=0, file=buf)
return buf.getvalue()


Expand Down
17 changes: 16 additions & 1 deletion autopush/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2109,7 +2109,7 @@ def test_memusage(self):
"http://localhost:{}/_memusage".format(port),
)
assert response.code == 200
assert 'rusage' in body
assert 'ru_maxrss=' in body
assert 'Logger' in body
if find_executable('pmap'):
assert 'RSS' in body or 'Rss' in body # pmap -x or -XX/X output
Expand All @@ -2118,6 +2118,21 @@ def test_memusage(self):
assert 'rpy_unicode' in body
assert 'get_stats_asmmemmgr: (' in body

@inlineCallbacks
def test_memusage_options(self):
port = self.ep.conf.memusage_port
url = ("http://localhost:{}/_memusage?objgraph=false&"
"dump_rpy_heap=false").format(port)
response, body = yield _agent('GET', url)
assert response.code == 200
assert 'ru_maxrss=' in body
assert 'Logger' not in body
if find_executable('pmap'):
assert 'RSS' in body or 'Rss' in body # pmap -x or -XX/X output
if hasattr(sys, 'pypy_version_info'): # pragma: nocover
assert 'size: ' not in body
assert 'rpy_unicode' not in body


@inlineCallbacks
def _agent(method, url, contextFactory=None, headers=None, body=None):
Expand Down
13 changes: 10 additions & 3 deletions autopush/web/health.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def get(self):


class MemUsageHandler(BaseWebHandler):
"""Spits out a tarball of some memory stats.
"""Spits out some memory stats.
Should be ran on its own port, not accessible externally.
Expand All @@ -110,11 +110,18 @@ def authenticate_peer_cert(self):
def get(self):
"""HTTP Get
Returns that this node is alive, and the version.
Returns the memory stats.
"""
from autopush.memusage import memusage
d = deferToThread(memusage)

def enabled(name):
return self.get_argument(name, u'true').lower() != u'false'
d = deferToThread(
memusage,
do_dump_rpy_heap=enabled('dump_rpy_heap'),
do_objgraph=enabled('objgraph')
)
d.addCallback(self.write)
d.addCallback(self.finish)
d.addErrback(self._response_err)
Expand Down

0 comments on commit 44007e4

Please sign in to comment.