output.py revision 1929:fb189519cb06
1# Copyright (c) 2005 The Regents of The University of Michigan 2# All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions are 6# met: redistributions of source code must retain the above copyright 7# notice, this list of conditions and the following disclaimer; 8# redistributions in binary form must reproduce the above copyright 9# notice, this list of conditions and the following disclaimer in the 10# documentation and/or other materials provided with the distribution; 11# neither the name of the copyright holders nor the names of its 12# contributors may be used to endorse or promote products derived from 13# this software without specific prior written permission. 14# 15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26# 27# Authors: Nathan Binkert 28 29class StatOutput(object): 30 def __init__(self, name, jobfile, info, stat=None, binstats=None): 31 self.name = name 32 self.jobfile = jobfile 33 self.stat = stat 34 self.binstats = None 35 self.label = self.name 36 self.invert = False 37 self.info = info 38 39 def printdata(self, bin = None, printmode = 'G'): 40 import info 41 42 if bin: 43 print '%s %s stats' % (self.name, bin) 44 45 if self.binstats: 46 for stat in self.binstats: 47 stat.bins = bin 48 49 if printmode == 'G': 50 valformat = '%g' 51 elif printmode != 'F' and value > 1e6: 52 valformat = '%0.5e' 53 else: 54 valformat = '%f' 55 56 for job in self.jobfile.jobs(): 57 value = self.info.get(job, self.stat) 58 if value is None: 59 return 60 61 if not isinstance(value, list): 62 value = [ value ] 63 64 if self.invert: 65 for i,val in enumerate(value): 66 if val != 0.0: 67 value[i] = 1 / val 68 69 valstring = ', '.join([ valformat % val for val in value ]) 70 print '%-50s %s' % (job.name + ':', valstring) 71 72 def display(self, binned = False, printmode = 'G'): 73 if binned and self.binstats: 74 self.printdata('kernel', printmode) 75 self.printdata('idle', printmode) 76 self.printdata('user', printmode) 77 self.printdata('interrupt', printmode) 78 79 print '%s total stats' % self.name 80 self.printdata(printmode=printmode) 81 82 def graph(self, graphdir): 83 from os.path import expanduser, isdir, join as joinpath 84 from barchart import BarChart 85 from matplotlib.numerix import Float, array, zeros 86 import os, re 87 88 confgroups = self.jobfile.groups() 89 ngroups = len(confgroups) 90 skiplist = [ False ] * ngroups 91 groupopts = None 92 baropts = None 93 groups = [] 94 for i,group in enumerate(confgroups): 95 if group.flags.graph_group: 96 if groupopts is not None: 97 raise AttributeError, \ 98 'Two groups selected for graph group' 99 groupopts = group.subopts() 100 skiplist[i] = True 101 elif group.flags.graph_bars: 102 if baropts is not None: 103 raise AttributeError, \ 104 'Two groups selected for graph bars' 105 baropts = group.subopts() 106 skiplist[i] = True 107 else: 108 groups.append(group) 109 110 if groupopts is None: 111 raise AttributeError, 'No group selected for graph group' 112 113 if baropts is None: 114 raise AttributeError, 'No group selected for graph bars' 115 116 directory = expanduser(graphdir) 117 if not isdir(directory): 118 os.mkdir(directory) 119 html = file(joinpath(directory, '%s.html' % self.name), 'w') 120 print >>html, '<html>' 121 print >>html, '<title>Graphs for %s</title>' % self.name 122 print >>html, '<body>' 123 124 for options in self.jobfile.options(groups): 125 data = zeros((len(groupopts), len(baropts)), Float) 126 data = [ [ None ] * len(baropts) for i in xrange(len(groupopts)) ] 127 enabled = False 128 stacked = 0 129 for g,gopt in enumerate(groupopts): 130 for b,bopt in enumerate(baropts): 131 job = self.jobfile.job(options + [ gopt, bopt ]) 132 if not job: 133 continue 134 135 val = self.info.get(job, self.stat) 136 if isinstance(val, (list, tuple)): 137 if len(val) == 1: 138 val = val[0] 139 else: 140 stacked = len(val) 141 142 data[g][b] = val 143 144 if stacked == 0: 145 for i in xrange(len(groupopts)): 146 for j in xrange(len(baropts)): 147 if data[i][j] is None: 148 data[i][j] = 0.0 149 else: 150 for i in xrange(len(groupopts)): 151 for j in xrange(len(baropts)): 152 val = data[i][j] 153 if val is None: 154 data[i][j] = [] * stacked 155 elif len(val) != stacked: 156 raise ValueError, "some stats stacked, some not" 157 158 data = array(data) 159 if data.sum() == 0: 160 continue 161 162 bar_descs = [ opt.desc for opt in baropts ] 163 group_descs = [ opt.desc for opt in groupopts ] 164 if stacked: 165 try: 166 legend = self.info.rcategories 167 except: 168 legend = [ str(i) for i in xrange(stacked) ] 169 else: 170 legend = bar_descs 171 172 chart = BarChart(data=data, xlabel='Benchmark', ylabel=self.label, 173 legend=legend, xticks=group_descs) 174 chart.graph() 175 176 names = [ opt.name for opt in options ] 177 descs = [ opt.desc for opt in options ] 178 179 filename = '%s-%s.png' % (self.name, ':'.join(names)) 180 desc = ' '.join(descs) 181 filepath = joinpath(directory, filename) 182 chart.savefig(filepath) 183 filename = re.sub(':', '%3A', filename) 184 print >>html, '''%s<br><img src="%s"><br>''' % (desc, filename) 185 186 print >>html, '</body>' 187 print >>html, '</html>' 188 html.close() 189