2 * Copyright (c) 2003 The Regents of The University of Michigan
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.
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.
36 #include "base/misc.hh"
38 #include "sim/eventq.hh"
39 #include "sim/param.hh"
40 #include "sim/serialize.hh"
41 #include "base/inifile.hh"
42 #include "sim/sim_events.hh"
43 #include "sim/sim_object.hh"
44 #include "base/trace.hh"
48 Serializer
*Serializeable::serializer
= NULL
;
50 Serializeable::Serializeable(const string
&n
)
51 : proxy(this), objName(n
), serialized(false)
54 Serializeable::~Serializeable()
61 serializer
->add_object(this);
67 Serializeable::out() const
69 return serializer
->out();
73 Serializeable::nameOut()
75 out() << "\n[" << name() << "]\n";
79 Serializeable::nameOut(const string
&_name
)
81 out() << "\n[" << _name
<< "]\n";
85 Serializeable::paramOut(const string
&name
, const uint64_t& param
)
87 out() << name
<< "=0x" << hex
<< param
<< dec
<< "\n";
91 Serializeable::childOut(const string
&name
, Serializeable
*child
)
94 if (child
->name() == "")
95 panic("child is unnamed");
97 out() << name
<< "=" << child
->name() << "\n";
101 Serializeable::setName(const string
&name
)
104 panic("Cannot change object name");
109 Serializer::Serializer()
112 Serializer::~Serializer()
116 Serializer::out() const
119 panic("must set output before serializing");
125 Serializer::add_object(Serializeable
*obj
)
127 objects
.push_back(obj
);
131 Serializer::add_objects()
133 mainEventQueue
.mark();
135 SimObject::SimObjectList::iterator i
= SimObject::simObjectList
.begin();
136 SimObject::SimObjectList::iterator end
= SimObject::simObjectList
.end();
145 Serializer::serialize(const string
&f
)
147 if (Serializeable::serializer
!= NULL
)
148 panic("in process of serializing!");
150 Serializeable::serializer
= this;
153 string cpt_file
= file
+ ".cpt";
154 output
= new ofstream(cpt_file
.c_str());
155 time_t t
= time(NULL
);
156 *output
<< "// checkpoint generated: " << ctime(&t
);
161 while (!objects
.empty()) {
162 Serializeable
*serial
= objects
.front();
163 DPRINTF(Serialize
, "Name Children of %s\n", serial
->name());
164 serial
->nameChildren();
166 list
.push_back(serial
);
169 while (!list
.empty()) {
170 list
.front()->serialized
= false;
175 while (!objects
.empty()) {
176 Serializeable
*serial
= objects
.front();
177 DPRINTF(Serialize
, "Name Children of %s\n", serial
->name());
180 list
.push_back(serial
);
183 while (!list
.empty()) {
184 list
.front()->serialized
= false;
188 Serializeable::serializer
= NULL
;
195 class SerializeEvent
: public Event
201 SerializeEvent(EventQueue
*q
, Tick when
, const string
&file
);
204 virtual void process();
205 virtual void serialize();
208 SerializeEvent::SerializeEvent(EventQueue
*q
, Tick when
, const string
&f
)
211 setFlags(AutoDelete
);
215 SerializeEvent::~SerializeEvent()
220 SerializeEvent::process()
223 serial
.serialize(file
);
224 new SimExitEvent("Serialization caused exit");
228 SerializeEvent::serialize()
230 panic("Cannot serialize the SerializeEvent");
233 class SerializeParamContext
: public ParamContext
236 SerializeEvent
*event
;
239 SerializeParamContext(const string
§ion
);
240 ~SerializeParamContext();
244 SerializeParamContext
serialParams("serialize");
246 Param
<Counter
> serialize_cycle(&serialParams
,
248 "cycle to serialize",
251 Param
<string
> serialize_file(&serialParams
,
253 "file to write to", "");
255 SerializeParamContext::SerializeParamContext(const string
§ion
)
256 : ParamContext(section
), event(NULL
)
259 SerializeParamContext::~SerializeParamContext()
264 SerializeParamContext::checkParams()
266 if (!((string
)serialize_file
).empty() && serialize_cycle
> 0)
267 event
= new SerializeEvent(&mainEventQueue
, serialize_cycle
,
272 debug_serialize(const char *file
)
275 serial
.serialize(file
);
276 new SimExitEvent("Serialization caused exit");
282 ////////////////////////////////////////////////////////////////////////
284 // SerializeableClass member definitions
286 ////////////////////////////////////////////////////////////////////////
288 // Map of class names to SerializeableBuilder creation functions.
289 // Need to make this a pointer so we can force initialization on the
290 // first reference; otherwise, some SerializeableClass constructors
291 // may be invoked before the classMap constructor.
292 map
<string
,SerializeableClass::CreateFunc
> *SerializeableClass::classMap
= 0;
294 // SerializeableClass constructor: add mapping to classMap
295 SerializeableClass::SerializeableClass(const string
&className
,
296 CreateFunc createFunc
)
298 if (classMap
== NULL
)
299 classMap
= new map
<string
,SerializeableClass::CreateFunc
>();
301 if ((*classMap
)[className
])
303 cerr
<< "Error: simulation object class " << className
<< " redefined"
308 // add className --> createFunc to class map
309 (*classMap
)[className
] = createFunc
;
316 SerializeableClass::createObject(IniFile
&configDB
,
317 const string
&configClassName
)
319 // find simulation object class name from configuration class
320 // (specified by 'type=' parameter)
321 string simObjClassName
;
323 if (!configDB
.findDefault(configClassName
, "type", simObjClassName
)) {
324 cerr
<< "Configuration class '" << configClassName
<< "' not found."
329 // look up className to get appropriate createFunc
330 if (classMap
->find(simObjClassName
) == classMap
->end()) {
331 cerr
<< "Simulator object class '" << simObjClassName
<< "' not found."
336 CreateFunc createFunc
= (*classMap
)[simObjClassName
];
339 SerializeableBuilder
*objectBuilder
= (*createFunc
)();
341 assert(objectBuilder
!= NULL
);
343 // now create the actual simulation object
344 Serializeable
*object
= objectBuilder
->create();
346 assert(object
!= NULL
);
348 // done with the SerializeableBuilder now
349 delete objectBuilder
;