Skip to content

Commit 0ac5a2f

Browse files
authored
Merge pull request #330 from william-silversmith/master
feat(peak): adds --func optional argument
2 parents d1bd809 + 03a9088 commit 0ac5a2f

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
@@ -838,19 +838,47 @@ def xlim_type(value):
838838
else:
839839
pl.show()
840840

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

851873
for filename in filenames:
852874
prof = read_mprofile_file(filename)
853-
print("{}\t{:.3f} MiB".format(prof["filename"], max(prof["mem_usage"])))
875+
try:
876+
mem_usage = filter_mprofile_mem_usage_by_function(prof, args.func)
877+
except ValueError:
878+
print("{}\tNaN MiB".format(prof["filename"]))
879+
continue
880+
881+
print("{}\t{:.3f} MiB".format(prof["filename"], max(mem_usage)))
854882
for child, values in prof["children"].items():
855883
child_peak = max([ mem_ts[0] for mem_ts in values ])
856884
print(" Child {}\t\t\t{:.3f} MiB".format(child, child_peak))

0 commit comments

Comments
 (0)