__init__.py (13681:9e8e1a96c423) __init__.py (13712:e36f980fdc36)
1# Copyright (c) 2017 ARM Limited
2# All rights reserved.
3#
4# The license below extends only to copyright in the software and shall
5# not be construed as granting a license to any other intellectual
6# property including but not limited to intellectual property relating
7# to a hardware implementation of the functionality of the software
8# licensed hereunder. You may use the software subject to the license
9# terms below provided that you ensure that this notice is replicated
10# unmodified and in its entirety in all distributions of the software,
11# modified or unmodified, in source code or in binary form.
12#
13# Copyright (c) 2007 The Regents of The University of Michigan
14# Copyright (c) 2010 The Hewlett-Packard Development Company
15# All rights reserved.
16#
17# Redistribution and use in source and binary forms, with or without
18# modification, are permitted provided that the following conditions are
19# met: redistributions of source code must retain the above copyright
20# notice, this list of conditions and the following disclaimer;
21# redistributions in binary form must reproduce the above copyright
22# notice, this list of conditions and the following disclaimer in the
23# documentation and/or other materials provided with the distribution;
24# neither the name of the copyright holders nor the names of its
25# contributors may be used to endorse or promote products derived from
26# this software without specific prior written permission.
27#
28# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39#
40# Authors: Nathan Binkert
41# Andreas Sandberg
42
43import m5
44
45import _m5.stats
46from m5.objects import Root
47from m5.util import attrdict, fatal
48
49# Stat exports
50from _m5.stats import schedStatEvent as schedEvent
51from _m5.stats import periodicStatDump
52
53outputList = []
54
55def _url_factory(func):
56 """Wrap a plain Python function with URL parsing helpers
57
58 Wrap a plain Python function f(fn, **kwargs) to expect a URL that
59 has been split using urlparse.urlsplit. First positional argument
60 is assumed to be a filename, this is created as the concatenation
61 of the netloc (~hostname) and path in the parsed URL. Keyword
62 arguments are derived from the query values in the URL.
63
64 For example:
65 wrapped_f(urlparse.urlsplit("text://stats.txt?desc=False")) ->
66 f("stats.txt", desc=False)
67
68 """
69
70 from functools import wraps
71
72 @wraps(func)
73 def wrapper(url):
1# Copyright (c) 2017 ARM Limited
2# All rights reserved.
3#
4# The license below extends only to copyright in the software and shall
5# not be construed as granting a license to any other intellectual
6# property including but not limited to intellectual property relating
7# to a hardware implementation of the functionality of the software
8# licensed hereunder. You may use the software subject to the license
9# terms below provided that you ensure that this notice is replicated
10# unmodified and in its entirety in all distributions of the software,
11# modified or unmodified, in source code or in binary form.
12#
13# Copyright (c) 2007 The Regents of The University of Michigan
14# Copyright (c) 2010 The Hewlett-Packard Development Company
15# All rights reserved.
16#
17# Redistribution and use in source and binary forms, with or without
18# modification, are permitted provided that the following conditions are
19# met: redistributions of source code must retain the above copyright
20# notice, this list of conditions and the following disclaimer;
21# redistributions in binary form must reproduce the above copyright
22# notice, this list of conditions and the following disclaimer in the
23# documentation and/or other materials provided with the distribution;
24# neither the name of the copyright holders nor the names of its
25# contributors may be used to endorse or promote products derived from
26# this software without specific prior written permission.
27#
28# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39#
40# Authors: Nathan Binkert
41# Andreas Sandberg
42
43import m5
44
45import _m5.stats
46from m5.objects import Root
47from m5.util import attrdict, fatal
48
49# Stat exports
50from _m5.stats import schedStatEvent as schedEvent
51from _m5.stats import periodicStatDump
52
53outputList = []
54
55def _url_factory(func):
56 """Wrap a plain Python function with URL parsing helpers
57
58 Wrap a plain Python function f(fn, **kwargs) to expect a URL that
59 has been split using urlparse.urlsplit. First positional argument
60 is assumed to be a filename, this is created as the concatenation
61 of the netloc (~hostname) and path in the parsed URL. Keyword
62 arguments are derived from the query values in the URL.
63
64 For example:
65 wrapped_f(urlparse.urlsplit("text://stats.txt?desc=False")) ->
66 f("stats.txt", desc=False)
67
68 """
69
70 from functools import wraps
71
72 @wraps(func)
73 def wrapper(url):
74 from urlparse import parse_qs
74 try:
75 from urllib.parse import parse_qs
76 except ImportError:
77 # Python 2 fallback
78 from urlparse import parse_qs
75 from ast import literal_eval
76
77 qs = parse_qs(url.query, keep_blank_values=True)
78
79 # parse_qs returns a list of values for each parameter. Only
80 # use the last value since kwargs don't allow multiple values
81 # per parameter. Use literal_eval to transform string param
82 # values into proper Python types.
83 def parse_value(key, values):
84 if len(values) == 0 or (len(values) == 1 and not values[0]):
85 fatal("%s: '%s' doesn't have a value." % (url.geturl(), key))
86 elif len(values) > 1:
87 fatal("%s: '%s' has multiple values." % (url.geturl(), key))
88 else:
89 try:
90 return key, literal_eval(values[0])
91 except ValueError:
92 fatal("%s: %s isn't a valid Python literal" \
93 % (url.geturl(), values[0]))
94
95 kwargs = dict([ parse_value(k, v) for k, v in qs.items() ])
96
97 try:
98 return func("%s%s" % (url.netloc, url.path), **kwargs)
99 except TypeError:
100 fatal("Illegal stat visitor parameter specified")
101
102 return wrapper
103
104@_url_factory
105def _textFactory(fn, desc=True):
106 """Output stats in text format.
107
108 Text stat files contain one stat per line with an optional
109 description. The description is enabled by default, but can be
110 disabled by setting the desc parameter to False.
111
112 Example: text://stats.txt?desc=False
113
114 """
115
116 return _m5.stats.initText(fn, desc)
117
118factories = {
119 # Default to the text factory if we're given a naked path
120 "" : _textFactory,
121 "file" : _textFactory,
122 "text" : _textFactory,
123}
124
125def addStatVisitor(url):
126 """Add a stat visitor specified using a URL string
127
128 Stat visitors are specified using URLs on the following format:
129 format://path[?param=value[;param=value]]
130
131 The available formats are listed in the factories list. Factories
132 are called with the path as the first positional parameter and the
133 parameters are keyword arguments. Parameter values must be valid
134 Python literals.
135
136 """
137
79 from ast import literal_eval
80
81 qs = parse_qs(url.query, keep_blank_values=True)
82
83 # parse_qs returns a list of values for each parameter. Only
84 # use the last value since kwargs don't allow multiple values
85 # per parameter. Use literal_eval to transform string param
86 # values into proper Python types.
87 def parse_value(key, values):
88 if len(values) == 0 or (len(values) == 1 and not values[0]):
89 fatal("%s: '%s' doesn't have a value." % (url.geturl(), key))
90 elif len(values) > 1:
91 fatal("%s: '%s' has multiple values." % (url.geturl(), key))
92 else:
93 try:
94 return key, literal_eval(values[0])
95 except ValueError:
96 fatal("%s: %s isn't a valid Python literal" \
97 % (url.geturl(), values[0]))
98
99 kwargs = dict([ parse_value(k, v) for k, v in qs.items() ])
100
101 try:
102 return func("%s%s" % (url.netloc, url.path), **kwargs)
103 except TypeError:
104 fatal("Illegal stat visitor parameter specified")
105
106 return wrapper
107
108@_url_factory
109def _textFactory(fn, desc=True):
110 """Output stats in text format.
111
112 Text stat files contain one stat per line with an optional
113 description. The description is enabled by default, but can be
114 disabled by setting the desc parameter to False.
115
116 Example: text://stats.txt?desc=False
117
118 """
119
120 return _m5.stats.initText(fn, desc)
121
122factories = {
123 # Default to the text factory if we're given a naked path
124 "" : _textFactory,
125 "file" : _textFactory,
126 "text" : _textFactory,
127}
128
129def addStatVisitor(url):
130 """Add a stat visitor specified using a URL string
131
132 Stat visitors are specified using URLs on the following format:
133 format://path[?param=value[;param=value]]
134
135 The available formats are listed in the factories list. Factories
136 are called with the path as the first positional parameter and the
137 parameters are keyword arguments. Parameter values must be valid
138 Python literals.
139
140 """
141
138 from urlparse import urlsplit
142 try:
143 from urllib.parse import urlsplit
144 except ImportError:
145 # Python 2 fallback
146 from urlparse import urlsplit
139
140 parsed = urlsplit(url)
141
142 try:
143 factory = factories[parsed.scheme]
144 except KeyError:
145 fatal("Illegal stat file type specified.")
146
147 outputList.append(factory(parsed))
148
149def initSimStats():
150 _m5.stats.initSimStats()
151 _m5.stats.registerPythonStatsHandlers()
152
153names = []
154stats_dict = {}
155stats_list = []
156def enable():
157 '''Enable the statistics package. Before the statistics package is
158 enabled, all statistics must be created and initialized and once
159 the package is enabled, no more statistics can be created.'''
160
161 global stats_list
162 stats_list = list(_m5.stats.statsList())
163
164 for stat in stats_list:
165 if not stat.check() or not stat.baseCheck():
166 fatal("statistic '%s' (%d) was not properly initialized " \
167 "by a regStats() function\n", stat.name, stat.id)
168
169 if not (stat.flags & flags.display):
170 stat.name = "__Stat%06d" % stat.id
171
172 stats_list.sort(key=lambda s: s.name.split('.'))
173 for stat in stats_list:
174 stats_dict[stat.name] = stat
175 stat.enable()
176
177 _m5.stats.enable();
178
179def prepare():
180 '''Prepare all stats for data access. This must be done before
181 dumping and serialization.'''
182
183 for stat in stats_list:
184 stat.prepare()
185
186lastDump = 0
187def dump():
188 '''Dump all statistics data to the registered outputs'''
189
190 curTick = m5.curTick()
191
192 global lastDump
193 assert lastDump <= curTick
194 if lastDump == curTick:
195 return
196 lastDump = curTick
197
198 _m5.stats.processDumpQueue()
199
200 prepare()
201
202 for output in outputList:
203 if output.valid():
204 output.begin()
205 for stat in stats_list:
206 stat.visit(output)
207 output.end()
208
209def reset():
210 '''Reset all statistics to the base state'''
211
212 # call reset stats on all SimObjects
213 root = Root.getInstance()
214 if root:
215 for obj in root.descendants(): obj.resetStats()
216
217 # call any other registered stats reset callbacks
218 for stat in stats_list:
219 stat.reset()
220
221 _m5.stats.processResetQueue()
222
223flags = attrdict({
224 'none' : 0x0000,
225 'init' : 0x0001,
226 'display' : 0x0002,
227 'total' : 0x0010,
228 'pdf' : 0x0020,
229 'cdf' : 0x0040,
230 'dist' : 0x0080,
231 'nozero' : 0x0100,
232 'nonan' : 0x0200,
233})
147
148 parsed = urlsplit(url)
149
150 try:
151 factory = factories[parsed.scheme]
152 except KeyError:
153 fatal("Illegal stat file type specified.")
154
155 outputList.append(factory(parsed))
156
157def initSimStats():
158 _m5.stats.initSimStats()
159 _m5.stats.registerPythonStatsHandlers()
160
161names = []
162stats_dict = {}
163stats_list = []
164def enable():
165 '''Enable the statistics package. Before the statistics package is
166 enabled, all statistics must be created and initialized and once
167 the package is enabled, no more statistics can be created.'''
168
169 global stats_list
170 stats_list = list(_m5.stats.statsList())
171
172 for stat in stats_list:
173 if not stat.check() or not stat.baseCheck():
174 fatal("statistic '%s' (%d) was not properly initialized " \
175 "by a regStats() function\n", stat.name, stat.id)
176
177 if not (stat.flags & flags.display):
178 stat.name = "__Stat%06d" % stat.id
179
180 stats_list.sort(key=lambda s: s.name.split('.'))
181 for stat in stats_list:
182 stats_dict[stat.name] = stat
183 stat.enable()
184
185 _m5.stats.enable();
186
187def prepare():
188 '''Prepare all stats for data access. This must be done before
189 dumping and serialization.'''
190
191 for stat in stats_list:
192 stat.prepare()
193
194lastDump = 0
195def dump():
196 '''Dump all statistics data to the registered outputs'''
197
198 curTick = m5.curTick()
199
200 global lastDump
201 assert lastDump <= curTick
202 if lastDump == curTick:
203 return
204 lastDump = curTick
205
206 _m5.stats.processDumpQueue()
207
208 prepare()
209
210 for output in outputList:
211 if output.valid():
212 output.begin()
213 for stat in stats_list:
214 stat.visit(output)
215 output.end()
216
217def reset():
218 '''Reset all statistics to the base state'''
219
220 # call reset stats on all SimObjects
221 root = Root.getInstance()
222 if root:
223 for obj in root.descendants(): obj.resetStats()
224
225 # call any other registered stats reset callbacks
226 for stat in stats_list:
227 stat.reset()
228
229 _m5.stats.processResetQueue()
230
231flags = attrdict({
232 'none' : 0x0000,
233 'init' : 0x0001,
234 'display' : 0x0002,
235 'total' : 0x0010,
236 'pdf' : 0x0020,
237 'cdf' : 0x0040,
238 'dist' : 0x0080,
239 'nozero' : 0x0100,
240 'nonan' : 0x0200,
241})