profile.py
来自「M5,一个功能强大的多处理器系统模拟器.很多针对处理器架构,性能的研究都使用它作」· Python 代码 · 共 504 行 · 第 1/2 页
PY
504 行
for root,dirs,files in os.walk(directory): for name in files: match = label_ex.match(name) if not match: continue filename = joinpath(root, name) prefix = os.path.commonprefix([root, directory]) dirname = root[len(prefix)+1:] data = self.datatype(filename, self.categorize) self.setdata(dirname, match.group(1), data) def setdata(self, run, cpu, data): if run not in self.data: self.data[run] = {} if cpu in self.data[run]: raise AttributeError, \ 'data already stored for run %s and cpu %s' % (run, cpu) self.data[run][cpu] = data def getdata(self, run, cpu): try: return self.data[run][cpu] except KeyError: print run, cpu return None def alldata(self): for run,cpus in self.data.iteritems(): for cpu,data in cpus.iteritems(): yield run,cpu,data def get(self, job, stat, system=None): if system is None and hasattr('system', job): system = job.system if system is None: raise AttributeError, 'The job must have a system set' cpu = '%s.run%d' % (system, self.cpu) data = self.getdata(str(job), cpu) if not data: return None values = [] for category in self.categories: val = float(data.get(category, 0.0)) if val < 0.0: raise ValueError, 'value is %f' % val values.append(val) total = sum(values) return [ v / total * 100.0 for v in values ] def dump(self): for run,cpu,data in self.alldata(): print 'run %s, cpu %s' % (run, cpu) data.dump() print def write_dot(self, threshold, jobfile=None, jobs=None): import pydot if jobs is None: jobs = [ job for job in jobfile.jobs() ] for job in jobs: cpu = '%s.run%d' % (job.system, self.cpu) symbols = self.getdata(job.name, cpu) if not symbols: continue dot = pydot.Dot() symbols.tree.dot(dot, threshold=threshold) dot.write(symbols.filename[:-3] + 'dot') def write_txt(self, jobfile=None, jobs=None, limit=None): if jobs is None: jobs = [ job for job in jobfile.jobs() ] for job in jobs: cpu = '%s.run%d' % (job.system, self.cpu) symbols = self.getdata(job.name, cpu) if not symbols: continue output = file(symbols.filename[:-3] + 'txt', 'w') symbols.display(output, limit) def display(self, jobfile=None, jobs=None, limit=None): if jobs is None: jobs = [ job for job in jobfile.jobs() ] maxsymlen = 0 thejobs = [] for job in jobs: cpu = '%s.run%d' % (job.system, self.cpu) symbols = self.getdata(job.name, cpu) if symbols: thejobs.append(job) maxsymlen = max(maxsymlen, symbols.maxsymlen) for job in thejobs: cpu = '%s.run%d' % (job.system, self.cpu) symbols = self.getdata(job.name, cpu) print job.name symbols.display(limit=limit, maxsymlen=maxsymlen) printfrom categories import func_categorize, pc_categorizeclass PCProfile(Profile): def __init__(self, categorize=pc_categorize): super(PCProfile, self).__init__(PCData, categorize)class FuncProfile(Profile): def __init__(self, categorize=func_categorize): super(FuncProfile, self).__init__(FuncData, categorize)def usage(exitcode = None): print '''\Usage: %s [-bc] [-g <dir>] [-j <jobfile>] [-n <num>] -c groups symbols into categories -b dumps data for bar charts -d generate dot output -g <d> draw graphs and send output to <d> -j <jobfile> specify a different jobfile (default is Test.py) -n <n> selects number of top symbols to print (default 5)''' % sys.argv[0] if exitcode is not None: sys.exit(exitcode)if __name__ == '__main__': import getopt, re, sys from os.path import expanduser from output import StatOutput # default option values numsyms = 10 graph = None cpus = [ 0 ] categorize = False showidle = True funcdata = True jobfilename = 'Test.py' dodot = False dotfile = None textout = False threshold = 0.01 inputfile = None try: opts, args = getopt.getopt(sys.argv[1:], 'C:cdD:f:g:ij:n:pT:t') except getopt.GetoptError: usage(2) for o,a in opts: if o == '-C': cpus = [ int(x) for x in a.split(',') ] elif o == '-c': categorize = True elif o == '-D': dotfile = a elif o == '-d': dodot = True elif o == '-f': inputfile = expanduser(a) elif o == '-g': graph = a elif o == '-i': showidle = False elif o == '-j': jobfilename = a elif o == '-n': numsyms = int(a) elif o == '-p': funcdata = False elif o == '-T': threshold = float(a) elif o == '-t': textout = True if args: print "'%s'" % args, len(args) usage(1) if inputfile: catfunc = None if categorize: catfunc = func_categorize data = FuncData(inputfile, categorize=catfunc) if dodot: import pydot dot = pydot.Dot() data.tree.dot(dot, threshold=threshold) #dot.orientation = 'landscape' #dot.ranksep='equally' #dot.rank='samerank' dot.write(dotfile, format='png') else: data.display(limit=numsyms) else: from jobfile import JobFile jobfile = JobFile(jobfilename) if funcdata: profile = FuncProfile() else: profile = PCProfile() if not categorize: profile.categorize = None profile.inputdir(jobfile.rootdir) if graph: for cpu in cpus: profile.cpu = cpu if funcdata: name = 'funcstacks%d' % cpu else: name = 'stacks%d' % cpu output = StatOutput(jobfile, info=profile) output.xlabel = 'System Configuration' output.ylabel = '% CPU utilization' output.stat = name output.graph(name, graph) if dodot: for cpu in cpus: profile.cpu = cpu profile.write_dot(jobfile=jobfile, threshold=threshold) if textout: for cpu in cpus: profile.cpu = cpu profile.write_txt(jobfile=jobfile) if not graph and not textout and not dodot: for cpu in cpus: if not categorize: profile.categorize = None profile.cpu = cpu profile.display(jobfile=jobfile, limit=numsyms)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?