1616import importlib
1717import memory_profiler as mp
1818
19- ALL_ACTIONS = ("run" , "rm" , "clean" , "list" , "plot" , "attach" )
19+ ALL_ACTIONS = ("run" , "rm" , "clean" , "list" , "plot" , "attach" , "peak" )
2020help_msg = """
2121Available commands:
2222
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
3031Type mprof <command> --help for usage help on a specific command.
3132For example, mprof plot --help will list all plotting options.
@@ -73,7 +74,7 @@ def get_profile_filenames(args):
7374 profiles = glob .glob ("mprofile_??????????????.dat" )
7475 profiles .sort ()
7576
76- if args is "all" :
77+ if args == "all" :
7778 filenames = copy .copy (profiles )
7879 else :
7980 filenames = []
@@ -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. \n This 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,55 @@ 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+ for child , values in prof ["children" ].items ():
852+ child_peak = max ([ mem_ts [0 ] for mem_ts in values ])
853+ print (" Child {}\t \t \t {:.3f} MiB" .format (child , child_peak ))
854+
855+
856+ def get_profiles (args ):
857+ profiles = glob .glob ("mprofile_??????????????.dat" )
858+ profiles .sort ()
859+
860+ if len (args .profiles ) == 0 :
861+ if len (profiles ) == 0 :
862+ print ("No input file found. \n This program looks for "
863+ "mprofile_*.dat files, generated by the "
864+ "'mprof run' command." )
865+ sys .exit (- 1 )
866+ print ("Using last profile data." )
867+ filenames = [profiles [- 1 ]]
868+ else :
869+ filenames = []
870+ for prof in args .profiles :
871+ if osp .exists (prof ):
872+ if not prof in filenames :
873+ filenames .append (prof )
874+ else :
875+ try :
876+ n = int (prof )
877+ if not profiles [n ] in filenames :
878+ filenames .append (profiles [n ])
879+ except ValueError :
880+ print ("Input file not found: " + prof )
881+ if not len (filenames ):
882+ print ("No files found from given input." )
883+ sys .exit (- 1 )
884+
885+ return filenames
886+
863887def main ():
864888 # Workaround for optparse limitation: insert -- before first negative
865889 # number found.
@@ -873,7 +897,8 @@ def main():
873897 "list" : list_action ,
874898 "run" : run_action ,
875899 "attach" : attach_action ,
876- "plot" : plot_action }
900+ "plot" : plot_action ,
901+ "peak" : peak_action }
877902 actions [get_action ()]()
878903
879904if __name__ == "__main__" :
0 commit comments