Skip to content

Commit 04fc1a7

Browse files
feat: adds mprof peak command
Prints maximum memory usage per dat file.
1 parent 685b5f8 commit 04fc1a7

1 file changed

Lines changed: 50 additions & 29 deletions

File tree

mprof.py

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import importlib
1717
import memory_profiler as mp
1818

19-
ALL_ACTIONS = ("run", "rm", "clean", "list", "plot", "attach")
19+
ALL_ACTIONS = ("run", "rm", "clean", "list", "plot", "attach", "peak")
2020
help_msg = """
2121
Available commands:
2222
@@ -26,6 +26,7 @@
2626
clean clean the current directory from files created by mprof
2727
list display existing profiles, with indices
2828
plot plot memory consumption generated by mprof run
29+
peak print the maximum memory used by an mprof run
2930
3031
Type mprof <command> --help for usage help on a specific command.
3132
For example, mprof plot --help will list all plotting options.
@@ -794,33 +795,7 @@ def xlim_type(value):
794795
sys.exit(1)
795796
pl.ioff()
796797

797-
profiles = glob.glob("mprofile_??????????????.dat")
798-
profiles.sort()
799-
800-
if len(args.profiles) == 0:
801-
if len(profiles) == 0:
802-
print("No input file found. \nThis program looks for "
803-
"mprofile_*.dat files, generated by the "
804-
"'mprof run' command.")
805-
sys.exit(-1)
806-
print("Using last profile data.")
807-
filenames = [profiles[-1]]
808-
else:
809-
filenames = []
810-
for prof in args.profiles:
811-
if osp.exists(prof):
812-
if not prof in filenames:
813-
filenames.append(prof)
814-
else:
815-
try:
816-
n = int(prof)
817-
if not profiles[n] in filenames:
818-
filenames.append(profiles[n])
819-
except ValueError:
820-
print("Input file not found: " + prof)
821-
if not len(filenames):
822-
print("No files found from given input.")
823-
sys.exit(-1)
798+
filenames = get_profiles(args)
824799

825800
fig = pl.figure(figsize=(14, 6), dpi=90)
826801
if not args.flame_mode:
@@ -860,6 +835,51 @@ def xlim_type(value):
860835
else:
861836
pl.show()
862837

838+
def peak_action():
839+
desc = """Prints the peak memory used in data file `file.dat` generated
840+
using `mprof run`. If no .dat file is given, it will take the most recent
841+
such file in the current directory."""
842+
parser = ArgumentParser(usage="mprof peak [options] [file.dat]", description=desc)
843+
parser.add_argument("profiles", nargs="*",
844+
help="profiles made by mprof run")
845+
args = parser.parse_args()
846+
filenames = get_profiles(args)
847+
848+
for filename in filenames:
849+
prof = read_mprofile_file(filename)
850+
print("{}\t{:.3f} MiB".format(prof["filename"], max(prof["mem_usage"])))
851+
852+
def get_profiles(args):
853+
profiles = glob.glob("mprofile_??????????????.dat")
854+
profiles.sort()
855+
856+
if len(args.profiles) == 0:
857+
if len(profiles) == 0:
858+
print("No input file found. \nThis program looks for "
859+
"mprofile_*.dat files, generated by the "
860+
"'mprof run' command.")
861+
sys.exit(-1)
862+
print("Using last profile data.")
863+
filenames = [profiles[-1]]
864+
else:
865+
filenames = []
866+
for prof in args.profiles:
867+
if osp.exists(prof):
868+
if not prof in filenames:
869+
filenames.append(prof)
870+
else:
871+
try:
872+
n = int(prof)
873+
if not profiles[n] in filenames:
874+
filenames.append(profiles[n])
875+
except ValueError:
876+
print("Input file not found: " + prof)
877+
if not len(filenames):
878+
print("No files found from given input.")
879+
sys.exit(-1)
880+
881+
return filenames
882+
863883
def main():
864884
# Workaround for optparse limitation: insert -- before first negative
865885
# number found.
@@ -873,7 +893,8 @@ def main():
873893
"list": list_action,
874894
"run": run_action,
875895
"attach": attach_action,
876-
"plot": plot_action}
896+
"plot": plot_action,
897+
"peak": peak_action}
877898
actions[get_action()]()
878899

879900
if __name__ == "__main__":

0 commit comments

Comments
 (0)