base: Fix uninitialized variable in Flag
[gem5.git] / util / stats / print.py
1 # Copyright (c) 2003-2004 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 functools import reduce
28
29 all = False
30 descriptions = False
31
32 class Value:
33 def __init__(self, value, precision, percent = False):
34 self.value = value
35 self.precision = precision
36 self.percent = percent
37 def __str__(self):
38 if isinstance(self.value, str):
39 if self.value.lower() == 'nan':
40 value = 'NaN'
41 if self.value.lower() == 'inf':
42 value = 'Inf'
43 else:
44 if self.precision >= 0:
45 format = "%%.%df" % self.precision
46 elif self.value == 0.0:
47 format = "%.0f"
48 elif self.value % 1.0 == 0.0:
49 format = "%.0f"
50 else:
51 format = "%f"
52 value = self.value
53 if self.percent:
54 value = value * 100.0
55 value = format % value
56
57 if self.percent:
58 value = value + "%"
59
60 return value
61
62 class Print:
63 def __init__(self, **vals):
64 self.__dict__.update(vals)
65
66 def __str__(self):
67 value = Value(self.value, self.precision)
68 pdf = ''
69 cdf = ''
70 if 'pdf' in self.__dict__:
71 pdf = Value(self.pdf, 2, True)
72 if 'cdf' in self.__dict__:
73 cdf = Value(self.cdf, 2, True)
74
75 output = "%-40s %12s %8s %8s" % (self.name, value, pdf, cdf)
76
77 if descriptions and 'desc' in self.__dict__ and self.desc:
78 output = "%s # %s" % (output, self.desc)
79
80 return output
81
82 def doprint(self):
83 if display_all:
84 return True
85 if self.value == 0.0 and (self.flags & flags_nozero):
86 return False
87 if isinstance(self.value, str):
88 if self.value == 'NaN' and (self.flags & flags_nonan):
89 return False
90 return True
91
92 def display(self):
93 if self.doprint():
94 print(self)
95
96 class VectorDisplay:
97 def display(self):
98 p = Print()
99 p.flags = self.flags
100 p.precision = self.precision
101
102 if isinstance(self.value, (list, tuple)):
103 if not len(self.value):
104 return
105
106 mytotal = reduce(lambda x,y: float(x) + float(y), self.value)
107 mycdf = 0.0
108
109 value = self.value
110
111 if display_all:
112 subnames = [ '[%d]' % i for i in range(len(value)) ]
113 else:
114 subnames = [''] * len(value)
115
116 if 'subnames' in self.__dict__:
117 for i,each in enumerate(self.subnames):
118 if len(each) > 0:
119 subnames[i] = '.%s' % each
120
121 subdescs = [self.desc]*len(value)
122 if 'subdescs' in self.__dict__:
123 for i in range(min(len(value), len(self.subdescs))):
124 subdescs[i] = self.subdescs[i]
125
126 for val,sname,sdesc in map(None, value, subnames, subdescs):
127 if mytotal > 0.0:
128 mypdf = float(val) / float(mytotal)
129 mycdf += mypdf
130 if (self.flags & flags_pdf):
131 p.pdf = mypdf
132 p.cdf = mycdf
133
134 if len(sname) == 0:
135 continue
136
137 p.name = self.name + sname
138 p.desc = sdesc
139 p.value = val
140 p.display()
141
142 if (self.flags & flags_total):
143 if ('pdf' in p.__dict__): del p.__dict__['pdf']
144 if ('cdf' in p.__dict__): del p.__dict__['cdf']
145 p.name = self.name + '.total'
146 p.desc = self.desc
147 p.value = mytotal
148 p.display()
149
150 else:
151 p.name = self.name
152 p.desc = self.desc
153 p.value = self.value
154 p.display()
155