config: Add a --without-python option to build process
[gem5.git] / src / python / m5 / stats / __init__.py
1 # Copyright (c) 2007 The Regents of The University of Michigan
2 # Copyright (c) 2010 The Hewlett-Packard Development Company
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met: redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer;
9 # redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution;
12 # neither the name of the copyright holders nor the names of its
13 # contributors may be used to endorse or promote products derived from
14 # this software without specific prior written permission.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #
28 # Authors: Nathan Binkert
29
30 import m5
31
32 from m5 import internal
33 from m5.internal.stats import schedStatEvent as schedEvent
34 from m5.objects import Root
35 from m5.util import attrdict, fatal
36
37 outputList = []
38 def initText(filename, desc=True):
39 output = internal.stats.initText(filename, desc)
40 outputList.append(output)
41
42 def initSimStats():
43 internal.stats.initSimStats()
44 internal.stats.registerPythonStatsHandlers()
45
46 names = []
47 stats_dict = {}
48 stats_list = []
49 raw_stats_list = []
50 def enable():
51 '''Enable the statistics package. Before the statistics package is
52 enabled, all statistics must be created and initialized and once
53 the package is enabled, no more statistics can be created.'''
54 __dynamic_cast = []
55 for k, v in internal.stats.__dict__.iteritems():
56 if k.startswith('dynamic_'):
57 __dynamic_cast.append(v)
58
59 for stat in internal.stats.statsList():
60 for cast in __dynamic_cast:
61 val = cast(stat)
62 if val is not None:
63 stats_list.append(val)
64 raw_stats_list.append(val)
65 break
66 else:
67 fatal("unknown stat type %s", stat)
68
69 for stat in stats_list:
70 if not stat.check() or not stat.baseCheck():
71 fatal("statistic '%s' (%d) was not properly initialized " \
72 "by a regStats() function\n", stat.name, stat.id)
73
74 if not (stat.flags & flags.display):
75 stat.name = "__Stat%06d" % stat.id
76
77 def less(stat1, stat2):
78 v1 = stat1.name.split('.')
79 v2 = stat2.name.split('.')
80 return v1 < v2
81
82 stats_list.sort(less)
83 for stat in stats_list:
84 stats_dict[stat.name] = stat
85 stat.enable()
86
87 internal.stats.enable();
88
89 def prepare():
90 '''Prepare all stats for data access. This must be done before
91 dumping and serialization.'''
92
93 for stat in stats_list:
94 stat.prepare()
95
96 lastDump = 0
97 def dump():
98 '''Dump all statistics data to the registered outputs'''
99
100 curTick = m5.curTick()
101
102 global lastDump
103 assert lastDump <= curTick
104 if lastDump == curTick:
105 return
106 lastDump = curTick
107
108 internal.stats.processDumpQueue()
109
110 prepare()
111
112 for output in outputList:
113 if output.valid():
114 output.begin()
115 for stat in stats_list:
116 output.visit(stat)
117 output.end()
118
119 def reset():
120 '''Reset all statistics to the base state'''
121
122 # call reset stats on all SimObjects
123 root = Root.getInstance()
124 if root:
125 for obj in root.descendants(): obj.resetStats()
126
127 # call any other registered stats reset callbacks
128 for stat in stats_list:
129 stat.reset()
130
131 internal.stats.processResetQueue()
132
133 flags = attrdict({
134 'none' : 0x0000,
135 'init' : 0x0001,
136 'display' : 0x0002,
137 'total' : 0x0010,
138 'pdf' : 0x0020,
139 'cdf' : 0x0040,
140 'dist' : 0x0080,
141 'nozero' : 0x0100,
142 'nonan' : 0x0200,
143 })