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"
37 #include "base/str.hh"
39 #include "sim/eventq.hh"
40 #include "sim/param.hh"
41 #include "sim/serialize.hh"
42 #include "base/inifile.hh"
43 #include "sim/sim_events.hh"
44 #include "sim/sim_object.hh"
45 #include "base/trace.hh"
46 #include "sim/config_node.hh"
50 Serializer
*Serializeable::serializer
= NULL
;
52 Serializeable::Serializeable()
56 Serializeable::~Serializeable()
63 serializer
->add_object(this);
69 Serializeable::nameOut(ostream
&os
)
71 os
<< "\n[" << name() << "]\n";
75 Serializeable::nameOut(ostream
&os
, const string
&_name
)
77 os
<< "\n[" << _name
<< "]\n";
82 paramOut(ostream
&os
, const std::string
&name
, const T
& param
)
92 paramIn(Checkpoint
*cp
, const std::string
§ion
,
93 const std::string
&name
, T
& param
)
96 if (!cp
->find(section
, name
, str
) || !parseParam(str
, param
)) {
97 fatal("Can't unserialize '%s:%s'\n", section
, name
);
104 arrayParamOut(ostream
&os
, const std::string
&name
,
105 const T
*param
, int size
)
109 showParam(os
, param
[0]);
110 for (int i
= 1; i
< size
; ++i
) {
112 showParam(os
, param
[i
]);
120 arrayParamIn(Checkpoint
*cp
, const std::string
§ion
,
121 const std::string
&name
, T
*param
, int size
)
124 if (!cp
->find(section
, name
, str
)) {
125 fatal("Can't unserialize '%s:%s'\n", section
, name
);
128 // code below stolen from VectorParam<T>::parse().
129 // it would be nice to unify these somehow...
131 vector
<string
> tokens
;
133 tokenize(tokens
, str
, ' ');
135 // Need this if we were doing a vector
136 // value.resize(tokens.size());
138 if (tokens
.size() != size
) {
139 fatal("Array size mismatch on %s:%s'\n", section
, name
);
142 for (int i
= 0; i
< tokens
.size(); i
++) {
143 // need to parse into local variable to handle vector<bool>,
144 // for which operator[] returns a special reference class
145 // that's not the same as 'bool&', (since it's a packed
148 if (!parseParam(tokens
[i
], scalar_value
)) {
149 string
err("could not parse \"");
157 // assign parsed value to vector
158 param
[i
] = scalar_value
;
164 objParamIn(Checkpoint
*cp
, const std::string
§ion
,
165 const std::string
&name
, Serializeable
* ¶m
)
167 if (!cp
->findObj(section
, name
, param
)) {
168 fatal("Can't unserialize '%s:%s'\n", section
, name
);
173 #define INSTANTIATE_PARAM_TEMPLATES(type) \
175 paramOut(ostream &os, const std::string &name, type const ¶m); \
177 paramIn(Checkpoint *cp, const std::string §ion, \
178 const std::string &name, type & param); \
180 arrayParamOut(ostream &os, const std::string &name, \
181 type const *param, int size); \
183 arrayParamIn(Checkpoint *cp, const std::string §ion, \
184 const std::string &name, type *param, int size);
187 INSTANTIATE_PARAM_TEMPLATES(int8_t)
188 INSTANTIATE_PARAM_TEMPLATES(uint8_t)
189 INSTANTIATE_PARAM_TEMPLATES(int16_t)
190 INSTANTIATE_PARAM_TEMPLATES(uint16_t)
191 INSTANTIATE_PARAM_TEMPLATES(int32_t)
192 INSTANTIATE_PARAM_TEMPLATES(uint32_t)
193 INSTANTIATE_PARAM_TEMPLATES(int64_t)
194 INSTANTIATE_PARAM_TEMPLATES(uint64_t)
195 INSTANTIATE_PARAM_TEMPLATES(bool)
196 INSTANTIATE_PARAM_TEMPLATES(string
)
202 Serializeable::childOut(const string
&name
, Serializeable
*child
)
205 if (child
->name() == "")
206 panic("child is unnamed");
208 out() << name
<< "=" << child
->name() << "\n";
212 Serializer::Serializer()
215 Serializer::~Serializer()
219 Serializer::out() const
222 panic("must set output before serializing");
228 Serializer::add_object(Serializeable
*obj
)
230 objects
.push_back(obj
);
234 Serializer::add_objects()
236 mainEventQueue
.mark();
238 SimObject::SimObjectList::iterator i
= SimObject::simObjectList
.begin();
239 SimObject::SimObjectList::iterator end
= SimObject::simObjectList
.end();
248 Serializer::serialize()
250 if (Serializeable::serializer
!= NULL
)
251 panic("in process of serializing!");
253 Serializeable::serializer
= this;
255 file
= CheckpointFile();
256 string cpt_file
= file
+ ".cpt";
257 output
= new ofstream(cpt_file
.c_str());
258 time_t t
= time(NULL
);
259 *output
<< "// checkpoint generated: " << ctime(&t
);
264 while (!objects
.empty()) {
265 Serializeable
*obj
= objects
.front();
266 DPRINTF(Serialize
, "Serializing %s\n", obj
->name());
268 obj
->serialize(out());
273 while (!list
.empty()) {
274 list
.front()->serialized
= false;
278 Serializeable::serializer
= NULL
;
285 class SerializeEvent
: public Event
292 SerializeEvent(Tick _when
, Tick _repeat
);
293 virtual void process();
294 virtual void serialize(std::ostream
&os
)
296 panic("Cannot serialize the SerializeEvent");
301 SerializeEvent::SerializeEvent(Tick _when
, Tick _repeat
)
302 : Event(&mainEventQueue
, 990), repeat(_repeat
)
304 setFlags(AutoDelete
);
309 SerializeEvent::process()
314 schedule(curTick
+ repeat
);
317 string __CheckpointFileBase
;
322 if (__CheckpointFileBase
.empty())
323 return __CheckpointFileBase
;
325 return csprintf("%s.%d", __CheckpointFileBase
, curTick
);
329 SetupCheckpoint(Tick when
, Tick period
)
331 new SerializeEvent(when
, period
);
334 class SerializeParamContext
: public ParamContext
337 SerializeEvent
*event
;
340 SerializeParamContext(const string
§ion
);
341 ~SerializeParamContext();
345 SerializeParamContext
serialParams("serialize");
347 Param
<string
> serialize_file(&serialParams
,
349 "file to write to", "m5");
351 Param
<Counter
> serialize_cycle(&serialParams
,
353 "cycle to serialize",
356 Param
<Counter
> serialize_period(&serialParams
,
358 "period to repeat serializations",
363 SerializeParamContext::SerializeParamContext(const string
§ion
)
364 : ParamContext(section
), event(NULL
)
367 SerializeParamContext::~SerializeParamContext()
372 SerializeParamContext::checkParams()
374 __CheckpointFileBase
= serialize_file
;
375 if (serialize_cycle
> 0)
376 SetupCheckpoint(serialize_cycle
, serialize_period
);
387 debug_serialize(Tick when
)
389 new SerializeEvent(when
, 0);
392 ////////////////////////////////////////////////////////////////////////
394 // SerializeableClass member definitions
396 ////////////////////////////////////////////////////////////////////////
398 // Map of class names to SerializeableBuilder creation functions.
399 // Need to make this a pointer so we can force initialization on the
400 // first reference; otherwise, some SerializeableClass constructors
401 // may be invoked before the classMap constructor.
402 map
<string
,SerializeableClass::CreateFunc
> *SerializeableClass::classMap
= 0;
404 // SerializeableClass constructor: add mapping to classMap
405 SerializeableClass::SerializeableClass(const string
&className
,
406 CreateFunc createFunc
)
408 if (classMap
== NULL
)
409 classMap
= new map
<string
,SerializeableClass::CreateFunc
>();
411 if ((*classMap
)[className
])
413 cerr
<< "Error: simulation object class " << className
<< " redefined"
418 // add className --> createFunc to class map
419 (*classMap
)[className
] = createFunc
;
426 SerializeableClass::createObject(Checkpoint
*cp
,
427 const std::string
§ion
)
431 if (!cp
->find(section
, "type", className
)) {
432 fatal("Serializeable::create: no 'type' entry in section '%s'.\n",
436 CreateFunc createFunc
= (*classMap
)[className
];
438 if (createFunc
== NULL
) {
439 fatal("Serializeable::create: no create function for class '%s'.\n",
443 Serializeable
*object
= createFunc(cp
, section
);
445 assert(object
!= NULL
);
452 Serializeable::create(Checkpoint
*cp
, const std::string
§ion
)
454 Serializeable
*object
= SerializeableClass::createObject(cp
, section
);
455 object
->unserialize(cp
, section
);
460 Checkpoint::Checkpoint(const std::string
&filename
, const std::string
&path
,
461 const ConfigNode
*_configNode
)
462 : db(new IniFile
), basePath(path
), configNode(_configNode
)
464 if (!db
->load(filename
)) {
465 fatal("Can't load checkpoint file '%s'\n", filename
);
468 mainEventQueue
.unserialize(this, "MainEventQueue");
473 Checkpoint::find(const std::string
§ion
, const std::string
&entry
,
476 return db
->find(section
, entry
, value
);
481 Checkpoint::findObj(const std::string
§ion
, const std::string
&entry
,
482 Serializeable
*&value
)
486 if (!db
->find(section
, entry
, path
))
489 if ((value
= configNode
->resolveSimObject(path
)) != NULL
)
492 if ((value
= objMap
[path
]) != NULL
)