sim_object.cc revision 395
1/*
2 * Copyright (c) 2003 The Regents of The University of Michigan
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
29#include <assert.h>
30
31#include "base/callback.hh"
32#include "base/inifile.hh"
33#include "base/misc.hh"
34#include "base/trace.hh"
35#include "sim/configfile.hh"
36#include "sim/host.hh"
37#include "sim/sim_object.hh"
38#include "sim/sim_stats.hh"
39
40using namespace std;
41
42
43////////////////////////////////////////////////////////////////////////
44//
45// SimObject member definitions
46//
47////////////////////////////////////////////////////////////////////////
48
49//
50// static list of all SimObjects, used for initialization etc.
51//
52SimObject::SimObjectList SimObject::simObjectList;
53
54//
55// SimObject constructor: used to maintain static simObjectList
56//
57SimObject::SimObject(const string &_name)
58    : objName(_name)
59{
60    simObjectList.push_back(this);
61}
62
63//
64// no default statistics, so nothing to do in base implementation
65//
66void
67SimObject::regStats()
68{
69}
70
71void
72SimObject::regFormulas()
73{
74}
75
76void
77SimObject::resetStats()
78{
79}
80
81//
82// no default extra output
83//
84void
85SimObject::printExtraOutput(ostream &os)
86{
87}
88
89//
90// static function:
91//   call regStats() on all SimObjects and then regFormulas() on all
92//   SimObjects.
93//
94struct SimObjectResetCB : public Callback
95{
96    virtual void process() { SimObject::resetAllStats(); }
97};
98
99namespace {
100    static SimObjectResetCB StatResetCB;
101}
102
103void
104SimObject::regAllStats()
105{
106    SimObjectList::iterator i;
107    SimObjectList::iterator end = simObjectList.end();
108
109    /**
110     * @todo change cprintfs to DPRINTFs
111     */
112    for (i = simObjectList.begin(); i != end; ++i) {
113#ifdef STAT_DEBUG
114        cprintf("registering stats for %s\n", (*i)->name());
115#endif
116        (*i)->regStats();
117    }
118
119    for (i = simObjectList.begin(); i != end; ++i) {
120#ifdef STAT_DEBUG
121        cprintf("registering formulas for %s\n", (*i)->name());
122#endif
123        (*i)->regFormulas();
124    }
125
126    Statistics::registerResetCallback(&StatResetCB);
127}
128
129//
130// static function: call resetStats() on all SimObjects.
131//
132void
133SimObject::resetAllStats()
134{
135    SimObjectList::iterator i = simObjectList.begin();
136    SimObjectList::iterator end = simObjectList.end();
137
138    for (; i != end; ++i) {
139        SimObject *obj = *i;
140        obj->resetStats();
141    }
142}
143
144//
145// static function: call printExtraOutput() on all SimObjects.
146//
147void
148SimObject::printAllExtraOutput(ostream &os)
149{
150    SimObjectList::iterator i = simObjectList.begin();
151    SimObjectList::iterator end = simObjectList.end();
152
153    for (; i != end; ++i) {
154        SimObject *obj = *i;
155        obj->printExtraOutput(os);
156   }
157}
158
159//
160// static function: serialize all SimObjects.
161//
162void
163SimObject::serializeAll(ostream &os)
164{
165    SimObjectList::iterator i = simObjectList.begin();
166    SimObjectList::iterator end = simObjectList.end();
167
168    for (; i != end; ++i) {
169        SimObject *obj = *i;
170        obj->nameOut(os);
171        obj->serialize(os);
172   }
173}
174