Skip to content

Commit 1135365

Browse files
author
Bartosz Gasiorzewski
committed
Pass the value of --include-children to TimeStamper
This fixes an issue with FUNC lines reporting lower memory usage than MEM lines.
1 parent 1a853e5 commit 1135365

2 files changed

Lines changed: 20 additions & 8 deletions

File tree

memory_profiler.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,37 +470,42 @@ def _find_script(script_name):
470470
class _TimeStamperCM(object):
471471
"""Time-stamping context manager."""
472472

473-
def __init__(self, timestamps, filename, backend, timestamper=None, func=None):
473+
def __init__(self, timestamps, filename, backend, timestamper=None, func=None,
474+
include_children=False):
474475
self.timestamps = timestamps
475476
self.filename = filename
476477
self.backend = backend
477478
self.ts = timestamper
478479
self.func = func
480+
self.include_children = include_children
479481

480482
def __enter__(self):
481483
if self.ts is not None:
482484
self.ts.current_stack_level += 1
483485
self.ts.stack[self.func].append(self.ts.current_stack_level)
484486

485487
self.timestamps.append(
486-
_get_memory(os.getpid(), self.backend, timestamps=True, filename=self.filename))
488+
_get_memory(os.getpid(), self.backend, timestamps=True,
489+
include_children=self.include_children, filename=self.filename))
487490

488491
def __exit__(self, *args):
489492
if self.ts is not None:
490493
self.ts.current_stack_level -= 1
491494

492495
self.timestamps.append(
493-
_get_memory(os.getpid(), self.backend, timestamps=True, filename=self.filename))
496+
_get_memory(os.getpid(), self.backend, timestamps=True,
497+
include_children=self.include_children, filename=self.filename))
494498

495499

496500
class TimeStamper:
497501
""" A profiler that just records start and end execution times for
498502
any decorated function.
499503
"""
500504

501-
def __init__(self, backend):
505+
def __init__(self, backend, include_children=False):
502506
self.functions = {}
503507
self.backend = backend
508+
self.include_children = include_children
504509
self.current_stack_level = -1
505510
self.stack = {}
506511

@@ -561,14 +566,16 @@ def f(*args, **kwds):
561566
except TypeError:
562567
filename = '<unknown>'
563568
timestamps = [
564-
_get_memory(os.getpid(), self.backend, timestamps=True, filename=filename)]
569+
_get_memory(os.getpid(), self.backend, timestamps=True,
570+
include_children=self.include_children, filename=filename)]
565571
self.functions[func].append(timestamps)
566572
try:
567573
with self.call_on_stack(func, *args, **kwds) as result:
568574
return result
569575
finally:
570576
# end time
571577
timestamps.append(_get_memory(os.getpid(), self.backend, timestamps=True,
578+
include_children=self.include_children,
572579
filename=filename))
573580

574581
return f
@@ -1248,6 +1255,9 @@ def flush(self):
12481255
action='store_true',
12491256
help='''print timestamp instead of memory measurement for
12501257
decorated functions''')
1258+
parser.add_argument('--include-children', dest='include_children',
1259+
default=False, action='store_true',
1260+
help='also include memory used by child processes')
12511261
parser.add_argument('--backend', dest='backend', type=str, action='store',
12521262
choices=['tracemalloc', 'psutil', 'posix'], default='psutil',
12531263
help='backend using for getting memory info '
@@ -1264,7 +1274,7 @@ def flush(self):
12641274
script_args = args.program[1:]
12651275
_backend = choose_backend(args.backend)
12661276
if args.timestamp:
1267-
prof = TimeStamper(_backend)
1277+
prof = TimeStamper(_backend, include_children=args.include_children)
12681278
else:
12691279
prof = LineProfiler(max_mem=args.max_mem, backend=_backend)
12701280

mprof.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,10 @@ def run_action():
230230
if not program[0].startswith("python"):
231231
program.insert(0, sys.executable)
232232
cmd_line = get_cmd_line(program)
233-
program[1:1] = ("-m", "memory_profiler", "--timestamp",
234-
"-o", mprofile_output)
233+
extra_args = ["-m", "memory_profiler", "--timestamp", "-o", mprofile_output]
234+
if args.include_children:
235+
extra_args.append("--include-children")
236+
program[1:1] = extra_args
235237
p = subprocess.Popen(program)
236238
else:
237239
cmd_line = get_cmd_line(program)

0 commit comments

Comments
 (0)