Skip to content

Commit 03a9088

Browse files
feat(peak): adds --func optional argument
Filter the peak memory usage by function. This doesn't isolate the contribution of the function by itself, but it screens out noise from setting up the test so an accurate result can be obtained by mprof peak if the test is otherwise well isolated. Example usage: mprof peak --func some.function The function name is the fully qualified python import name. You can also figure it out by grepping the dat file for FUNC entries.
1 parent 0d21b8d commit 03a9088

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

mprof.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,19 +835,47 @@ def xlim_type(value):
835835
else:
836836
pl.show()
837837

838+
def filter_mprofile_mem_usage_by_function(prof, func):
839+
if func is None:
840+
return prof["mem_usage"]
841+
842+
if func not in prof["func_timestamp"]:
843+
raise ValueError(str(func) + " was not found.")
844+
845+
time_ranges = prof["func_timestamp"][func]
846+
filtered_memory = []
847+
848+
# The check here could be improved, but it's done in this
849+
# inefficient way to make sure we don't miss overlapping
850+
# ranges.
851+
for mib, ts in zip(prof["mem_usage"], prof["timestamp"]):
852+
for rng in time_ranges:
853+
if rng[0] <= ts <= rng[1]:
854+
filtered_memory.append(mib)
855+
856+
return filtered_memory
857+
838858
def peak_action():
839859
desc = """Prints the peak memory used in data file `file.dat` generated
840860
using `mprof run`. If no .dat file is given, it will take the most recent
841861
such file in the current directory."""
842862
parser = ArgumentParser(usage="mprof peak [options] [file.dat]", description=desc)
843863
parser.add_argument("profiles", nargs="*",
844864
help="profiles made by mprof run")
865+
parser.add_argument("--func", dest="func", default=None,
866+
help="""Show the peak for this function. Does not support child processes.""")
845867
args = parser.parse_args()
846868
filenames = get_profiles(args)
847869

848870
for filename in filenames:
849871
prof = read_mprofile_file(filename)
850-
print("{}\t{:.3f} MiB".format(prof["filename"], max(prof["mem_usage"])))
872+
try:
873+
mem_usage = filter_mprofile_mem_usage_by_function(prof, args.func)
874+
except ValueError:
875+
print("{}\tNaN MiB".format(prof["filename"]))
876+
continue
877+
878+
print("{}\t{:.3f} MiB".format(prof["filename"], max(mem_usage)))
851879
for child, values in prof["children"].items():
852880
child_peak = max([ mem_ts[0] for mem_ts in values ])
853881
print(" Child {}\t\t\t{:.3f} MiB".format(child, child_peak))

0 commit comments

Comments
 (0)