__init__.py revision 14209:7efe1c187149
1# Copyright (c) 2017-2019 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        try:
75            from urllib.parse import parse_qs
76        except ImportError:
77            # Python 2 fallback
78            from urlparse import parse_qs
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
122@_url_factory
123def _hdf5Factory(fn, chunking=10, desc=True, formulas=True):
124    """Output stats in HDF5 format.
125
126    The HDF5 file format is a structured binary file format. It has
127    the multiple benefits over traditional text stat files:
128
129      * Efficient storage of time series (multiple stat dumps)
130      * Fast lookup of stats
131      * Plenty of existing tooling (e.g., Python libraries and graphical
132        viewers)
133      * File format can be used to store frame buffers together with
134        normal stats.
135
136    There are some drawbacks compared to the default text format:
137      * Large startup cost (single stat dump larger than text equivalent)
138      * Stat dumps are slower than text
139
140
141    Known limitations:
142      * Distributions and histograms currently unsupported.
143      * No support for forking.
144
145
146    Parameters:
147      * chunking (unsigned): Number of time steps to pre-allocate (default: 10)
148      * desc (bool): Output stat descriptions (default: True)
149      * formulas (bool): Output derived stats (default: True)
150
151    Example:
152      h5://stats.h5?desc=False;chunking=100;formulas=False
153
154    """
155
156    if hasattr(_m5.stats, "initHDF5"):
157        return _m5.stats.initHDF5(fn, chunking, desc, formulas)
158    else:
159        fatal("HDF5 support not enabled at compile time")
160
161factories = {
162    # Default to the text factory if we're given a naked path
163    "" : _textFactory,
164    "file" : _textFactory,
165    "text" : _textFactory,
166    "h5" : _hdf5Factory,
167}
168
169
170def addStatVisitor(url):
171    """Add a stat visitor specified using a URL string
172
173    Stat visitors are specified using URLs on the following format:
174    format://path[?param=value[;param=value]]
175
176    The available formats are listed in the factories list. Factories
177    are called with the path as the first positional parameter and the
178    parameters are keyword arguments. Parameter values must be valid
179    Python literals.
180
181    """
182
183    try:
184        from urllib.parse import urlsplit
185    except ImportError:
186        # Python 2 fallback
187        from urlparse import urlsplit
188
189    parsed = urlsplit(url)
190
191    try:
192        factory = factories[parsed.scheme]
193    except KeyError:
194        fatal("Illegal stat file type specified.")
195
196    outputList.append(factory(parsed))
197
198def initSimStats():
199    _m5.stats.initSimStats()
200    _m5.stats.registerPythonStatsHandlers()
201
202def _visit_groups(visitor, root=None):
203    if root is None:
204        root = Root.getInstance()
205    for group in root.getStatGroups().values():
206        visitor(group)
207        _visit_groups(visitor, root=group)
208
209def _visit_stats(visitor, root=None):
210    def for_each_stat(g):
211        for stat in g.getStats():
212            visitor(g, stat)
213    _visit_groups(for_each_stat, root=root)
214
215def _bindStatHierarchy(root):
216    def _bind_obj(name, obj):
217        if m5.SimObject.isSimObjectVector(obj):
218            for idx, obj in enumerate(obj):
219                _bind_obj("{}{}".format(name, idx), obj)
220        else:
221            root.addStatGroup(name, obj.getCCObject())
222            _bindStatHierarchy(obj)
223
224    for name, obj in root._children.items():
225        _bind_obj(name, obj)
226
227names = []
228stats_dict = {}
229stats_list = []
230def enable():
231    '''Enable the statistics package.  Before the statistics package is
232    enabled, all statistics must be created and initialized and once
233    the package is enabled, no more statistics can be created.'''
234
235    def check_stat(group, stat):
236        if not stat.check() or not stat.baseCheck():
237            fatal("statistic '%s' (%d) was not properly initialized " \
238                  "by a regStats() function\n", stat.name, stat.id)
239
240        if not (stat.flags & flags.display):
241            stat.name = "__Stat%06d" % stat.id
242
243
244    # Legacy stat
245    global stats_list
246    stats_list = list(_m5.stats.statsList())
247
248    for stat in stats_list:
249        check_stat(None, stat)
250
251    stats_list.sort(key=lambda s: s.name.split('.'))
252    for stat in stats_list:
253        stats_dict[stat.name] = stat
254        stat.enable()
255
256
257    # New stats
258    _visit_stats(check_stat)
259    _visit_stats(lambda g, s: s.enable())
260
261    _m5.stats.enable();
262
263def prepare():
264    '''Prepare all stats for data access.  This must be done before
265    dumping and serialization.'''
266
267    # Legacy stats
268    for stat in stats_list:
269        stat.prepare()
270
271    # New stats
272    _visit_stats(lambda g, s: s.prepare())
273
274def _dump_to_visitor(visitor, root=None):
275    # Legacy stats
276    if root is None:
277        for stat in stats_list:
278            stat.visit(visitor)
279
280    # New stats
281    def dump_group(group):
282        for stat in group.getStats():
283            stat.visit(visitor)
284
285        for n, g in group.getStatGroups().items():
286            visitor.beginGroup(n)
287            dump_group(g)
288            visitor.endGroup()
289
290    if root is not None:
291        for p in root.path_list():
292            visitor.beginGroup(p)
293    dump_group(root if root is not None else Root.getInstance())
294    if root is not None:
295        for p in reversed(root.path_list()):
296            visitor.endGroup()
297
298lastDump = 0
299
300def dump(root=None):
301    '''Dump all statistics data to the registered outputs'''
302
303    now = m5.curTick()
304    global lastDump
305    assert lastDump <= now
306    new_dump = lastDump != now
307    lastDump = now
308
309    # Don't allow multiple global stat dumps in the same tick. It's
310    # still possible to dump a multiple sub-trees.
311    if not new_dump and root is None:
312        return
313
314    # Only prepare stats the first time we dump them in the same tick.
315    if new_dump:
316        _m5.stats.processDumpQueue()
317        prepare()
318
319    for output in outputList:
320        if output.valid():
321            output.begin()
322            _dump_to_visitor(output, root=root)
323            output.end()
324
325def reset():
326    '''Reset all statistics to the base state'''
327
328    # call reset stats on all SimObjects
329    root = Root.getInstance()
330    if root:
331        root.resetStats()
332
333    # call any other registered legacy stats reset callbacks
334    for stat in stats_list:
335        stat.reset()
336
337    _m5.stats.processResetQueue()
338
339flags = attrdict({
340    'none'    : 0x0000,
341    'init'    : 0x0001,
342    'display' : 0x0002,
343    'total'   : 0x0010,
344    'pdf'     : 0x0020,
345    'cdf'     : 0x0040,
346    'dist'    : 0x0080,
347    'nozero'  : 0x0100,
348    'nonan'   : 0x0200,
349})
350