This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a /_memusage API on a separate (internal port)
for debugging our memory usage issue #802
- Loading branch information
Showing
7 changed files
with
161 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
"""Produces memory usage information""" | ||
import gc | ||
import objgraph | ||
import os | ||
import resource | ||
import subprocess | ||
import tempfile | ||
import zlib | ||
from StringIO import StringIO | ||
|
||
from autopush.gcdump import Stat | ||
|
||
|
||
def memusage(): | ||
"""Returning a str of memory usage stats""" | ||
# type() -> str | ||
def trap_err(func, *args, **kwargs): | ||
try: | ||
return func(*args, **kwargs) | ||
except Exception as e: | ||
buf.writelines([func.__name__, ': ', repr(e)]) | ||
|
||
buf = StringIO() | ||
rusage = trap_err(resource.getrusage, resource.RUSAGE_SELF) | ||
buf.writelines([repr(rusage), '\n\n']) | ||
trap_err(objgraph.show_most_common_types, limit=0, file=buf) | ||
buf.write('\n\n') | ||
pmap = trap_err(subprocess.check_output, ['pmap', '-X', str(os.getpid())], | ||
stderr=subprocess.STDOUT) | ||
buf.writelines([pmap, '\n\n']) | ||
trap_err(dump_rpy_heap, buf) | ||
return buf.getvalue() | ||
|
||
|
||
def dump_rpy_heap(stream): | ||
"""Write PyPy's gcdump to the specified stream""" | ||
if not hasattr(gc, '_dump_rpy_heap'): | ||
# not PyPy | ||
return | ||
|
||
with tempfile.NamedTemporaryFile('wb') as fp: | ||
gc._dump_rpy_heap(fp.fileno()) | ||
stream.write("{} size: {}\n".format(fp.name, os.stat(fp.name).st_size)) | ||
stat = Stat() | ||
stat.summarize(fp.name, stream=None) | ||
stat.load_typeids(zlib.decompress(gc.get_typeids_z()).split("\n")) | ||
stream.write('\n\n') | ||
stat.print_summary(stream) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters