util: Delete authors lists from files in util.
[gem5.git] / util / stats / output.py
1 # Copyright (c) 2005-2006 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 from chart import ChartOptions
28
29 class StatOutput(ChartOptions):
30 def __init__(self, jobfile, info, stat=None):
31 super(StatOutput, self).__init__()
32 self.jobfile = jobfile
33 self.stat = stat
34 self.invert = False
35 self.info = info
36
37 def display(self, name, printmode = 'G'):
38 import info
39
40 if printmode == 'G':
41 valformat = '%g'
42 elif printmode != 'F' and value > 1e6:
43 valformat = '%0.5e'
44 else:
45 valformat = '%f'
46
47 for job in self.jobfile.jobs():
48 value = self.info.get(job, self.stat)
49 if value is None:
50 return
51
52 if not isinstance(value, list):
53 value = [ value ]
54
55 if self.invert:
56 for i,val in enumerate(value):
57 if val != 0.0:
58 value[i] = 1 / val
59
60 valstring = ', '.join([ valformat % val for val in value ])
61 print '%-50s %s' % (job.name + ':', valstring)
62
63 def graph(self, name, graphdir, proxy=None):
64 from os.path import expanduser, isdir, join as joinpath
65 from barchart import BarChart
66 from matplotlib.numerix import Float, array, zeros
67 import os, re, urllib
68 from jobfile import crossproduct
69
70 confgroups = self.jobfile.groups()
71 ngroups = len(confgroups)
72 skiplist = [ False ] * ngroups
73 groupopts = []
74 baropts = []
75 groups = []
76 for i,group in enumerate(confgroups):
77 if group.flags.graph_group:
78 groupopts.append(group.subopts())
79 skiplist[i] = True
80 elif group.flags.graph_bars:
81 baropts.append(group.subopts())
82 skiplist[i] = True
83 else:
84 groups.append(group)
85
86 has_group = bool(groupopts)
87 if has_group:
88 groupopts = [ group for group in crossproduct(groupopts) ]
89 else:
90 groupopts = [ None ]
91
92 if baropts:
93 baropts = [ bar for bar in crossproduct(baropts) ]
94 else:
95 raise AttributeError, 'No group selected for graph bars'
96
97 directory = expanduser(graphdir)
98 if not isdir(directory):
99 os.mkdir(directory)
100 html = file(joinpath(directory, '%s.html' % name), 'w')
101 print >>html, '<html>'
102 print >>html, '<title>Graphs for %s</title>' % name
103 print >>html, '<body>'
104 html.flush()
105
106 for options in self.jobfile.options(groups):
107 chart = BarChart(self)
108
109 data = [ [ None ] * len(baropts) for i in xrange(len(groupopts)) ]
110 enabled = False
111 stacked = 0
112 for g,gopt in enumerate(groupopts):
113 for b,bopt in enumerate(baropts):
114 if gopt is None:
115 gopt = []
116 job = self.jobfile.job(options + gopt + bopt)
117 if not job:
118 continue
119
120 if proxy:
121 import db
122 proxy.dict['system'] = self.info[job.system]
123 val = self.info.get(job, self.stat)
124 if val is None:
125 print 'stat "%s" for job "%s" not found' % \
126 (self.stat, job)
127
128 if isinstance(val, (list, tuple)):
129 if len(val) == 1:
130 val = val[0]
131 else:
132 stacked = len(val)
133
134 data[g][b] = val
135
136 if stacked == 0:
137 for i in xrange(len(groupopts)):
138 for j in xrange(len(baropts)):
139 if data[i][j] is None:
140 data[i][j] = 0.0
141 else:
142 for i in xrange(len(groupopts)):
143 for j in xrange(len(baropts)):
144 val = data[i][j]
145 if val is None:
146 data[i][j] = [ 0.0 ] * stacked
147 elif len(val) != stacked:
148 raise ValueError, "some stats stacked, some not"
149
150 data = array(data)
151 if data.sum() == 0:
152 continue
153
154 dim = len(data.shape)
155 x = data.shape[0]
156 xkeep = [ i for i in xrange(x) if data[i].sum() != 0 ]
157 y = data.shape[1]
158 ykeep = [ i for i in xrange(y) if data[:,i].sum() != 0 ]
159 data = data.take(xkeep, axis=0)
160 data = data.take(ykeep, axis=1)
161 if not has_group:
162 data = data.take([ 0 ], axis=0)
163 chart.data = data
164
165
166 bopts = [ baropts[i] for i in ykeep ]
167 bdescs = [ ' '.join([o.desc for o in opt]) for opt in bopts]
168
169 if has_group:
170 gopts = [ groupopts[i] for i in xkeep ]
171 gdescs = [ ' '.join([o.desc for o in opt]) for opt in gopts]
172
173 if chart.legend is None:
174 if stacked:
175 try:
176 chart.legend = self.info.rcategories
177 except:
178 chart.legend = [ str(i) for i in xrange(stacked) ]
179 else:
180 chart.legend = bdescs
181
182 if chart.xticks is None:
183 if has_group:
184 chart.xticks = gdescs
185 else:
186 chart.xticks = []
187 chart.graph()
188
189 names = [ opt.name for opt in options ]
190 descs = [ opt.desc for opt in options ]
191
192 if names[0] == 'run':
193 names = names[1:]
194 descs = descs[1:]
195
196 basename = '%s-%s' % (name, ':'.join(names))
197 desc = ' '.join(descs)
198
199 pngname = '%s.png' % basename
200 psname = '%s.eps' % re.sub(':', '-', basename)
201 epsname = '%s.ps' % re.sub(':', '-', basename)
202 chart.savefig(joinpath(directory, pngname))
203 chart.savefig(joinpath(directory, epsname))
204 chart.savefig(joinpath(directory, psname))
205 html_name = urllib.quote(pngname)
206 print >>html, '''%s<br><img src="%s"><br>''' % (desc, html_name)
207 html.flush()
208
209 print >>html, '</body>'
210 print >>html, '</html>'
211 html.close()