Skip to content

Commit 2246640

Browse files
authored
store processes by PID
1 parent e430342 commit 2246640

1 file changed

Lines changed: 12 additions & 15 deletions

File tree

memory_profiler.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# .. we'll use this to pass it to the child script ..
44
_CLEAN_GLOBALS = globals().copy()
55

6-
__version__ = '0.59.0'
6+
__version__ = '0.60.0'
77

88
_CMD_USAGE = "python -m memory_profiler script_file.py"
99

@@ -15,15 +15,13 @@
1515
import linecache
1616
import logging
1717
import os
18-
import io
1918
import pdb
2019
import subprocess
2120
import sys
2221
import time
2322
import traceback
2423
import warnings
2524

26-
2725
if sys.platform == "win32":
2826
# any value except signal.CTRL_C_EVENT and signal.CTRL_BREAK_EVENT
2927
# can be used to kill a process unconditionally in Windows
@@ -106,12 +104,12 @@ def _get_child_memory(process, meminfo_attr=None, memory_metric=0):
106104
for child in getattr(process, children_attr)(recursive=True):
107105
if isinstance(memory_metric, str):
108106
meminfo = getattr(child, meminfo_attr)()
109-
yield getattr(meminfo, memory_metric) / _TWO_20
107+
yield child.pid, getattr(meminfo, memory_metric) / _TWO_20
110108
else:
111-
yield getattr(child, meminfo_attr)()[memory_metric] / _TWO_20
109+
yield child.pid, getattr(child, meminfo_attr)()[memory_metric] / _TWO_20
112110
except (psutil.NoSuchProcess, psutil.AccessDenied):
113111
# https://github.com/fabianp/memory_profiler/issues/71
114-
yield 0.0
112+
yield (0, 0.0)
115113

116114

117115
def _get_memory(pid, backend, timestamps=False, include_children=False, filename=None):
@@ -139,7 +137,7 @@ def ps_util_tool():
139137
else 'get_memory_info'
140138
mem = getattr(process, meminfo_attr)()[0] / _TWO_20
141139
if include_children:
142-
mem += sum(_get_child_memory(process, meminfo_attr))
140+
mem += sum([mem for (pid, mem) in _get_child_memory(process, meminfo_attr)])
143141
if timestamps:
144142
return mem, time.time()
145143
else:
@@ -166,7 +164,7 @@ def _ps_util_full_tool(memory_metric):
166164
mem = getattr(meminfo, memory_metric) / _TWO_20
167165

168166
if include_children:
169-
mem += sum(_get_child_memory(process, meminfo_attr, memory_metric))
167+
mem += sum([mem for (pid, mem) in _get_child_memory(process, meminfo_attr)])
170168

171169
if timestamps:
172170
return mem, time.time()
@@ -411,13 +409,13 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
411409

412410
# Write children to the stream file
413411
if multiprocess:
414-
for idx, chldmem in enumerate(_get_child_memory(proc.pid)):
412+
for idx, chldmem in _get_child_memory(proc.pid):
415413
stream.write("CHLD {0} {1:.6f} {2:.4f}\n".format(idx, chldmem, time.time()))
416414
else:
417415
# Create a nested list with the child memory
418416
if multiprocess:
419417
mem_usage = [mem_usage]
420-
for chldmem in _get_child_memory(proc.pid):
418+
for _, chldmem in _get_child_memory(proc.pid):
421419
mem_usage.append(chldmem)
422420

423421
# Append the memory usage to the return value
@@ -455,13 +453,13 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
455453

456454
# Write children to the stream file
457455
if multiprocess:
458-
for idx, chldmem in enumerate(_get_child_memory(proc)):
456+
for idx, chldmem in _get_child_memory(proc):
459457
stream.write("CHLD {0} {1:.6f} {2:.4f}\n".format(idx, chldmem, time.time()))
460458
else:
461459
# Create a nested list with the child memory
462460
if multiprocess:
463461
mem_usage = [mem_usage]
464-
for chldmem in _get_child_memory(proc):
462+
for _, chldmem in _get_child_memory(proc):
465463
mem_usage.append(chldmem)
466464

467465
# Append the memory usage to the return value
@@ -1112,7 +1110,7 @@ def memit(self, line='', cell=None):
11121110
counter += 1
11131111
tmp = memory_usage((_func_exec, (stmt, self.shell.user_ns)),
11141112
timeout=timeout, interval=interval,
1115-
max_usage=True, max_iterations=1,
1113+
max_usage=True,
11161114
include_children=include_children)
11171115
mem_usage.append(tmp)
11181116

@@ -1248,8 +1246,7 @@ def exec_with_profiler(filename, profiler, backend, passed_args=[]):
12481246
try:
12491247
if _backend == 'tracemalloc' and has_tracemalloc:
12501248
tracemalloc.start()
1251-
1252-
with io.open(filename, encoding='utf-8') as f:
1249+
with open(filename, encoding='utf-8') as f:
12531250
exec(compile(f.read(), filename, 'exec'), ns, ns)
12541251
finally:
12551252
if has_tracemalloc and tracemalloc.is_tracing():

0 commit comments

Comments
 (0)