Skip to content

Commit

Permalink
Be able to view only the Python internal dict
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Nov 25, 2019
1 parent 50f9457 commit 7c1a3bd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
15 changes: 13 additions & 2 deletions c2cwsgiutils/debug/_listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ def _dump_stacks_impl() -> Dict[str, Any]:
}


def _dump_memory_impl(limit: int, analyze_type: Optional[str]) -> Mapping[str, Any]:
def _dump_memory_impl(
limit: int,
analyze_type: Optional[str],
python_internals_map: bool = False,
) -> Mapping[str, Any]:
nb_collected = [gc.collect(generation) for generation in range(3)]
result = {
'nb_collected': nb_collected,
Expand All @@ -41,6 +45,9 @@ def _dump_memory_impl(limit: int, analyze_type: Optional[str]) -> Mapping[str, A
objects=objgraph.get_leaking_objects())
}

if python_internals_map and not analyze_type:
analyze_type = 'builtins.dict'

if analyze_type:
# timeout after one minute, must be set to a bit less that the timeout of the broadcast in _views.py
timeout = time.monotonic() + 60
Expand All @@ -54,12 +61,16 @@ def _dump_memory_impl(limit: int, analyze_type: Optional[str]) -> Mapping[str, A
mod_counts[short] = mod_counts.get(short, 0) + 1
else:
if analyze_type == 'builtins.dict':
python_internal = False
if not len(FILES_FIELDS - set(obj.keys())):
continue
python_internal = True
if \
not len({'scope', 'module', 'locals', 'globals'} - set(obj.keys())) and \
isinstance(obj['globals'], dict) and \
not len(FILES_FIELDS - set(obj['globals'].keys())):
python_internal = True
if python_internal and not python_internals_map \
or not python_internal and python_internals_map:
continue
size = get_size(obj) / 1024
if len(biggest_objects) < limit or size > biggest_objects[0][0]:
Expand Down
9 changes: 6 additions & 3 deletions c2cwsgiutils/debug/_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ def _dump_memory(request: pyramid.request.Request) -> List[Mapping[str, Any]]:
auth.auth_view(request)
limit = int(request.params.get('limit', '30'))
analyze_type = request.params.get('analyze_type')
result = broadcast.broadcast('c2c_dump_memory',
params={'limit': limit, 'analyze_type': analyze_type},
expect_answers=True, timeout=70)
python_internals_map = request.params.get('python_internals_map', '0').lower() in ('', '1', 'true', 'on')
result = broadcast.broadcast(
'c2c_dump_memory',
params={'limit': limit, 'analyze_type': analyze_type, 'python_internals_map': python_internals_map},
expect_answers=True, timeout=70
)
assert result is not None
return result

Expand Down

0 comments on commit 7c1a3bd

Please sign in to comment.