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