CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/Validation/Performance/scripts/cmsIgProf_Analysis.py

Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 # Import statements. An '''Analogue''' of the include c++ statement,
00004 # but really really not the same!
00005 # In python everything is an object, even if we don't know. 
00006 # os and time will become 2 objects for us.
00007 
00008 import os,sys
00009 import time
00010 
00011 IgProfProfile={'PERF_TICKS':'IgProfPerf',
00012                'MEM_TOTAL':'IgProfMem',
00013                'MEM_LIVE':'IgProfMem',
00014                'MEM_MAX':'IgProfMem'
00015                }
00016 
00017 def execute(command):
00018     print '[IgAnalysis] %s ...' %command
00019     sys.stdout.flush()
00020     exitstate=os.system(command)
00021     return exitstate
00022     
00023 def simple_igprof_analysis(profile_name,AnalysisType,output_type):
00024     
00025     """This function takes as arguments:
00026     -a compressed (.gz) igprof output profile
00027     -the output directory where to store the output
00028     -the type of output wanted
00029     Based on this information it automatically selects the igprof counter
00030     (PERF_TICKS, MEM_TOTAL or MEM_LIVE) to use with igprof-analyse and it
00031     performs the analysis.
00032     If ASCII output_type is given, it keeps only the first seven lines of
00033     the output file (the report) allowing quick access to the totals for
00034     the counters in order to put together summary tables.
00035     If SQLite3 output_type is given, it simply dumps the full reports in
00036     sqlite3 format, ready to be browsed by igprof-navigator GUI (standalone
00037     or cgi-bin).
00038     """
00039     #Use profile input file to determine the name of the output file adding the counter to it:
00040     #outfile=profile_name[:-3]+"___"+AnalysisType+".res"
00041     outfile=profile_name.split(".")[0].replace(IgProfProfile[AnalysisType],AnalysisType)+".res"
00042     #Launch the 1 command:
00043     
00044     #command='igprof-analyse -d -v -g -r %s %s|tee -a \%s'%(AnalysisType,profile_name,outfile)
00045     #Replacing tee: it is polluting the log files...
00046 
00047     #igprof-analyse command depends on the output type desired:
00048     if output_type =="SQLite3":
00049         #Case of dumping the igprof-analyse report in sqlite format to be igprof-navigator browseable
00050         outfile= outfile[:-3]+"sql3" #NEED TO USE sql3 to be compatible with CGI-BIN igprof-navigator
00051         command='igprof-analyse --sqlite -d -v -g -r %s %s |sqlite3 %s'%(AnalysisType,profile_name,outfile)
00052         #Execute the igprof-analyse command
00053         exit=execute(command)
00054     elif output_type =="ASCII":
00055         #We can keep this part of the igprof-analysis, to create the html table with the totals at each dump
00056         command='igprof-analyse -d -v -g -r %s %s > %s'%(AnalysisType,profile_name,outfile)
00057         #Execute the igprof-analyse command
00058         exit=execute(command)
00059         #Let's manipulate the ASCII output to only keep the top 7 lines:
00060         # we open and read the txt ascii file
00061         print "Reading the res file"
00062         txt_file=open(outfile,'r')
00063         txt_file_content=txt_file.readlines()#again:everything is an object
00064         print "res file has %s lines!"%len(txt_file_content)
00065         txt_file.close()
00066         #overwrite the file to only save the first 7 lines:
00067         print "Overwriting the res file, to reduce it to 7 lines"
00068         out_file=open(outfile,'w')
00069         line_num=0
00070         for line in txt_file_content:
00071             out_file.write(line)
00072             line_num+=1
00073             if line_num == 7:
00074                 break
00075         out_file.close()
00076     
00077     return exit
00078     
00079 def diff_igprof_analysis(profile_name,regression_profile_name,AnalysisType):
00080     
00081     """
00082     This function takes as arguments:
00083     -a compressed (.gz) igprof output profile
00084     -a second compressed (.gz) igprof output profile against which a regression analysis is intended to be done
00085     -the output directory where to store the output of the regression analysis
00086     Based on this information it automatically selects the igprof counter (PERF_TICKS, MEM_TOTAL or MEM_LIVE) to use
00087     with igprof-analyse and it performs the regression analysis with SQLite3 output.
00088     """
00089     #Following Giulio's filename convention:
00090     outfile=profile_name.split(".")[0].replace(IgProfProfile[AnalysisType],AnalysisType)+"_diff_"+regression_profile_name.split(".")[0].split("___")[-1]+".sql3"
00091     command='igprof-analyse --sqlite -d -v -g --diff-mode -b %s -r %s %s |sqlite3 %s'%(regression_profile_name, AnalysisType,profile_name,outfile)
00092     exit=execute(command)
00093 
00094     return exit
00095 def library_igprof_analysis(profile_name,AnalysisType):
00096 
00097     """
00098     This function takes as arguments:
00099     -a compressed (.gz) igprof output profile
00100     -the output directory where to store the output of the igprof-analysis merging the results by library
00101     Based on this information it automatically selects the igprof counter (PERF_TICKS, MEM_TOTAL or MEM_LIVE) to use
00102     with igprof-analyse and it performs the igprof-analyse analysis merging the results by library and saving the results
00103     in the usual igprof-navigator browseable SQLite3 format.
00104     """
00105     #Following Giulio's filename convention:
00106     outfile=profile_name.split(".")[0].replace(IgProfProfile[AnalysisType],AnalysisType)+"_merged"+".sql3"
00107     #Regular Expression supplied by Giulio:
00108     command="igprof-analyse --sqlite -d -v -g -r %s -ml -mr 's|.*/(.*)$|\\1|' %s |sqlite3 %s"%(AnalysisType,profile_name,outfile)
00109     exit=execute(command)
00110     
00111     return exit
00112 #-------------------------------------------------------------------------------
00113 
00114 # A procedure used for importing the function above with the import statement
00115 # or to run it if the script is called: power python..
00116 if __name__ == '__main__':
00117     
00118     import optparse
00119     
00120     # Here we define an option parser to handle commandline options..
00121     usage='IgProf_Analysis.py <options>'
00122     parser = optparse.OptionParser(usage)
00123     parser.add_option('-i', '--in_  profile',
00124                       help='The profile to manipulate' ,
00125                       default='',
00126                       dest='profile')
00127                       
00128     parser.add_option('-c', '--counter',
00129                       help='The IgProf counter for the analysis (PERF_TICKS,MEM_TOTAL,MEM_LIVE,MEM_MAX)' ,
00130                       default='UNKNOWN_COUNTER',
00131                       dest='counter')
00132     parser.add_option('-t', '--output_type',
00133                       help='The type of the output file (ASCII or sqlite3)' ,
00134                       default='',
00135                       dest='output_type')
00136     parser.add_option('-r', '--regression',
00137                       help='The IgProf output file to make a regression analysis against' ,
00138                       default='',
00139                       dest='regressionprofile')
00140     parser.add_option('-l','--library',
00141                       help='Perform igprof-analyse merging the results by library',
00142                       action='store_true',
00143                       default=False,
00144                       dest='library') 
00145     (options,args) = parser.parse_args()
00146     
00147     # Now some fault control..If an error is found we raise an exception
00148     if options.profile=='':
00149         raise('Please select a profile, an output dir AND a type of output (ASCII or SQLITE3)!')
00150     
00151     if not os.path.exists(options.profile):
00152         raise ('Input profile not present!')
00153 
00154     AnalysisType=options.counter
00155 
00156     print "Input file is %s and AnalysisType is %s"%(options.profile,AnalysisType)
00157     
00158     #launch the function!
00159     if options.regressionprofile:
00160         diff_igprof_analysis(options.profile,options.regressionprofile,AnalysisType)
00161     elif options.library:
00162         library_igprof_analysis(options.profile,AnalysisType)
00163     else:
00164         simple_igprof_analysis(options.profile,AnalysisType,options.output_type)        
00165