#! /usr/bin/env python # ______________________________________________________________________ # # Filename: cpustats # # Created: 20-JAN-2001, Harry Melanson # # ______________________________________________________________________ """Summarize D0 framework job CPU statistics. USAGE: %(__filename__)s [-hv] framework.out OPTIONS: -h, --help -- Print help text and exit. -v -- Verbose print (list all times). """ # ______________________________________________________________________ import sys, os import glob import string # ______________________________________________________________________ __author__ = 'Harry Melanson, melanson@fnal.gov' __version__ = '00.00.00' __filename__ = 'cpustats' __doc__ = __doc__ % vars() # ______________________________________________________________________ def process_command(): """Process the command.""" from scriptutil import getOptions optlist, args = getOptions('v', [], __doc__) # Process command line arguments if len(args) == 0: print "\nERROR: No RECO output file specified." print "\nNAME: ", (os.path.basename(sys.argv[0])) print __doc__ sys.exit(-1) outfile = args[0] if len(glob.glob(outfile)) == 0: print "\nERROR: RECO output file", outfile, "does not exist.\n" sys.exit(-1) elif len(glob.glob(outfile)) != 1: print "\nERROR: More than one file found?\n" sys.exit(-1) VERBOSE = 0 for opt in optlist: if opt.flag == '-v': VERBOSE = 1 # ________________________________________ # # Get CPU times times = extractCpuTimes(outfile) # Print out times per step, for specified level of detail printSummary(times, 2) if VERBOSE: print "\nDetailed Summary\n" printSummary(times, 10) # ______________________________________________________________________ # def extractCpuTimes(outfile): """Return list of CPU times from a framework output file.""" # Process the file file = open(outfile,'r') results = [] found = 0 for line in file.readlines(): # Look for lines from framework with package/algorithm if (string.find(line, '::') != -1 and string.find(line, '->') != -1): items = string.split(line, '::') package = string.strip(items[0]) algo = string.strip(items[1][:-3]) found = 1 # If previous line was package/algo, this line has # the cpu mean time elif found == 1: found = 0 if string.find(line, 'cpu mean time') == -1: print "\nERROR: Missing 'cpu mean time' - format of file bad.\n" sys.exit(-1) items = string.split(line, ':') time = string.atof(string.strip(items[1])) results.append( (string.find(line, 'cpu mean time'), package, algo, time) ) file.close() return results # ______________________________________________________________________ # def printSummary(times, level): """Print summary of results at specified level of detail.""" # Extract total time for item in times: if (item[1] == 'Controller' and item[2] == '/'): totaltime = item[3] if level == 2: format0 = '%-25s %6s' format1 = '%-25s %6.2f' else: format0 = '%-50s %6s' format1 = '%-50s %6.2f' print format0 % ('Stage', 'Time') print format0 % ('-----', '----') for item in times: if item[1] == 'Controller': if item[2] == '/': name = 'TOTAL' else: name = item[2] else: if level == 2: name = item[1] else: name = item[1] + ' (' + item[2] + ')' if item[0] <= level: space = (item[0] - 2) * ' ' if name != 'TOTAL': print format1 % ((space + name), item[3]) print format0 % (' ', '----') print (format1 % ('Total:', totaltime)), 'sec/event' # ______________________________________________________________________ # # Execute command if __name__ == '__main__': process_command() sys.exit(0)