__init__.py revision 11766:7c95caf53250
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
30import m5
31
32from m5 import internal
33from m5.objects import Root
34from m5.util import attrdict, fatal
35
36# Stat exports
37from m5.internal.stats import schedStatEvent as schedEvent
38from m5.internal.stats import periodicStatDump
39
40outputList = []
41def initText(filename, desc=True):
42    output = internal.stats.initText(filename, desc)
43    outputList.append(output)
44
45def initSimStats():
46    internal.stats.initSimStats()
47    internal.stats.registerPythonStatsHandlers()
48
49names = []
50stats_dict = {}
51stats_list = []
52raw_stats_list = []
53def enable():
54    '''Enable the statistics package.  Before the statistics package is
55    enabled, all statistics must be created and initialized and once
56    the package is enabled, no more statistics can be created.'''
57    __dynamic_cast = []
58    for k, v in internal.stats.__dict__.iteritems():
59        if k.startswith('dynamic_'):
60            __dynamic_cast.append(v)
61
62    for stat in internal.stats.statsList():
63        for cast in __dynamic_cast:
64            val = cast(stat)
65            if val is not None:
66                stats_list.append(val)
67                raw_stats_list.append(val)
68                break
69        else:
70            fatal("unknown stat type %s", stat)
71
72    for stat in stats_list:
73        if not stat.check() or not stat.baseCheck():
74            fatal("statistic '%s' (%d) was not properly initialized " \
75                  "by a regStats() function\n", stat.name, stat.id)
76
77        if not (stat.flags & flags.display):
78            stat.name = "__Stat%06d" % stat.id
79
80    def less(stat1, stat2):
81        v1 = stat1.name.split('.')
82        v2 = stat2.name.split('.')
83        return v1 < v2
84
85    stats_list.sort(less)
86    for stat in stats_list:
87        stats_dict[stat.name] = stat
88        stat.enable()
89
90    internal.stats.enable();
91
92def prepare():
93    '''Prepare all stats for data access.  This must be done before
94    dumping and serialization.'''
95
96    for stat in stats_list:
97        stat.prepare()
98
99lastDump = 0
100def dump():
101    '''Dump all statistics data to the registered outputs'''
102
103    curTick = m5.curTick()
104
105    global lastDump
106    assert lastDump <= curTick
107    if lastDump == curTick:
108        return
109    lastDump = curTick
110
111    internal.stats.processDumpQueue()
112
113    prepare()
114
115    for output in outputList:
116        if output.valid():
117            output.begin()
118            for stat in stats_list:
119                output.visit(stat)
120            output.end()
121
122def reset():
123    '''Reset all statistics to the base state'''
124
125    # call reset stats on all SimObjects
126    root = Root.getInstance()
127    if root:
128        for obj in root.descendants(): obj.resetStats()
129
130    # call any other registered stats reset callbacks
131    for stat in stats_list:
132        stat.reset()
133
134    internal.stats.processResetQueue()
135
136flags = attrdict({
137    'none'    : 0x0000,
138    'init'    : 0x0001,
139    'display' : 0x0002,
140    'total'   : 0x0010,
141    'pdf'     : 0x0020,
142    'cdf'     : 0x0040,
143    'dist'    : 0x0080,
144    'nozero'  : 0x0100,
145    'nonan'   : 0x0200,
146})
147