2 * Copyright (c) 2002-2004 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.
30 #include <sys/types.h>
39 #include "base/inifile.hh"
40 #include "base/misc.hh"
41 #include "base/output.hh"
42 #include "base/str.hh"
43 #include "base/trace.hh"
44 #include "sim/config_node.hh"
45 #include "sim/eventq.hh"
46 #include "sim/param.hh"
47 #include "sim/serialize.hh"
48 #include "sim/sim_events.hh"
49 #include "sim/sim_object.hh"
54 Serializable::nameOut(ostream
&os
)
56 os
<< "\n[" << name() << "]\n";
60 Serializable::nameOut(ostream
&os
, const string
&_name
)
62 os
<< "\n[" << _name
<< "]\n";
67 paramOut(ostream
&os
, const std::string
&name
, const T
¶m
)
77 paramIn(Checkpoint
*cp
, const std::string
§ion
,
78 const std::string
&name
, T
¶m
)
81 if (!cp
->find(section
, name
, str
) || !parseParam(str
, param
)) {
82 fatal("Can't unserialize '%s:%s'\n", section
, name
);
89 arrayParamOut(ostream
&os
, const std::string
&name
,
90 const T
*param
, int size
)
94 showParam(os
, param
[0]);
95 for (int i
= 1; i
< size
; ++i
) {
97 showParam(os
, param
[i
]);
105 arrayParamIn(Checkpoint
*cp
, const std::string
§ion
,
106 const std::string
&name
, T
*param
, int size
)
109 if (!cp
->find(section
, name
, str
)) {
110 fatal("Can't unserialize '%s:%s'\n", section
, name
);
113 // code below stolen from VectorParam<T>::parse().
114 // it would be nice to unify these somehow...
116 vector
<string
> tokens
;
118 tokenize(tokens
, str
, ' ');
120 // Need this if we were doing a vector
121 // value.resize(tokens.size());
123 if (tokens
.size() != size
) {
124 fatal("Array size mismatch on %s:%s'\n", section
, name
);
127 for (int i
= 0; i
< tokens
.size(); i
++) {
128 // need to parse into local variable to handle vector<bool>,
129 // for which operator[] returns a special reference class
130 // that's not the same as 'bool&', (since it's a packed
133 if (!parseParam(tokens
[i
], scalar_value
)) {
134 string
err("could not parse \"");
142 // assign parsed value to vector
143 param
[i
] = scalar_value
;
149 objParamIn(Checkpoint
*cp
, const std::string
§ion
,
150 const std::string
&name
, Serializable
* ¶m
)
152 if (!cp
->findObj(section
, name
, param
)) {
153 fatal("Can't unserialize '%s:%s'\n", section
, name
);
158 #define INSTANTIATE_PARAM_TEMPLATES(type) \
160 paramOut(ostream &os, const std::string &name, type const ¶m); \
162 paramIn(Checkpoint *cp, const std::string §ion, \
163 const std::string &name, type & param); \
165 arrayParamOut(ostream &os, const std::string &name, \
166 type const *param, int size); \
168 arrayParamIn(Checkpoint *cp, const std::string §ion, \
169 const std::string &name, type *param, int size);
172 INSTANTIATE_PARAM_TEMPLATES(int8_t)
173 INSTANTIATE_PARAM_TEMPLATES(uint8_t)
174 INSTANTIATE_PARAM_TEMPLATES(int16_t)
175 INSTANTIATE_PARAM_TEMPLATES(uint16_t)
176 INSTANTIATE_PARAM_TEMPLATES(int32_t)
177 INSTANTIATE_PARAM_TEMPLATES(uint32_t)
178 INSTANTIATE_PARAM_TEMPLATES(int64_t)
179 INSTANTIATE_PARAM_TEMPLATES(uint64_t)
180 INSTANTIATE_PARAM_TEMPLATES(bool)
181 INSTANTIATE_PARAM_TEMPLATES(string
)
184 /////////////////////////////
186 /// Container for serializing global variables (not associated with
187 /// any serialized object).
188 class Globals
: public Serializable
191 const string
name() const;
192 void serialize(ostream
&os
);
193 void unserialize(Checkpoint
*cp
);
196 /// The one and only instance of the Globals class.
200 Globals::name() const
206 Globals::serialize(ostream
&os
)
209 SERIALIZE_SCALAR(curTick
);
211 nameOut(os
, "MainEventQueue");
212 mainEventQueue
.serialize(os
);
216 Globals::unserialize(Checkpoint
*cp
)
218 const string
§ion
= name();
219 UNSERIALIZE_SCALAR(curTick
);
221 mainEventQueue
.unserialize(cp
, "MainEventQueue");
225 Serializable::serializeAll()
227 string dir
= Checkpoint::dir();
228 if (mkdir(dir
.c_str(), 0775) == -1 && errno
!= EEXIST
)
229 fatal("couldn't mkdir %s\n", dir
);
231 string cpt_file
= dir
+ Checkpoint::baseFilename
;
232 ofstream
outstream(cpt_file
.c_str());
233 time_t t
= time(NULL
);
234 outstream
<< "// checkpoint generated: " << ctime(&t
);
236 globals
.serialize(outstream
);
237 SimObject::serializeAll(outstream
);
242 Serializable::unserializeGlobals(Checkpoint
*cp
)
244 globals
.unserialize(cp
);
248 class SerializeEvent
: public Event
254 SerializeEvent(Tick _when
, Tick _repeat
);
255 virtual void process();
256 virtual void serialize(std::ostream
&os
)
258 panic("Cannot serialize the SerializeEvent");
263 SerializeEvent::SerializeEvent(Tick _when
, Tick _repeat
)
264 : Event(&mainEventQueue
, Serialize_Pri
), repeat(_repeat
)
266 setFlags(AutoDelete
);
271 SerializeEvent::process()
273 Serializable::serializeAll();
275 schedule(curTick
+ repeat
);
278 const char *Checkpoint::baseFilename
= "m5.cpt";
280 static string checkpointDirBase
;
285 // use csprintf to insert curTick into directory name if it
286 // appears to have a format placeholder in it.
287 return (checkpointDirBase
.find("%") != string::npos
) ?
288 csprintf(checkpointDirBase
, curTick
) : checkpointDirBase
;
292 Checkpoint::setup(Tick when
, Tick period
)
294 new SerializeEvent(when
, period
);
297 class SerializeParamContext
: public ParamContext
300 SerializeEvent
*event
;
303 SerializeParamContext(const string
§ion
);
304 ~SerializeParamContext();
308 SerializeParamContext
serialParams("serialize");
310 Param
<string
> serialize_dir(&serialParams
, "dir",
311 "dir to stick checkpoint in "
312 "(sprintf format with cycle #)");
314 Param
<Counter
> serialize_cycle(&serialParams
,
316 "cycle to serialize",
319 Param
<Counter
> serialize_period(&serialParams
,
321 "period to repeat serializations",
326 SerializeParamContext::SerializeParamContext(const string
§ion
)
327 : ParamContext(section
), event(NULL
)
330 SerializeParamContext::~SerializeParamContext()
335 SerializeParamContext::checkParams()
337 checkpointDirBase
= simout
.resolve(serialize_dir
);
339 // guarantee that directory ends with a '/'
340 if (checkpointDirBase
[checkpointDirBase
.size() - 1] != '/')
341 checkpointDirBase
+= "/";
343 if (serialize_cycle
> 0)
344 Checkpoint::setup(serialize_cycle
, serialize_period
);
350 Serializable::serializeAll();
354 debug_serialize(Tick when
)
356 new SerializeEvent(when
, 0);
359 ////////////////////////////////////////////////////////////////////////
361 // SerializableClass member definitions
363 ////////////////////////////////////////////////////////////////////////
365 // Map of class names to SerializableBuilder creation functions.
366 // Need to make this a pointer so we can force initialization on the
367 // first reference; otherwise, some SerializableClass constructors
368 // may be invoked before the classMap constructor.
369 map
<string
,SerializableClass::CreateFunc
> *SerializableClass::classMap
= 0;
371 // SerializableClass constructor: add mapping to classMap
372 SerializableClass::SerializableClass(const string
&className
,
373 CreateFunc createFunc
)
375 if (classMap
== NULL
)
376 classMap
= new map
<string
,SerializableClass::CreateFunc
>();
378 if ((*classMap
)[className
])
380 cerr
<< "Error: simulation object class " << className
<< " redefined"
385 // add className --> createFunc to class map
386 (*classMap
)[className
] = createFunc
;
393 SerializableClass::createObject(Checkpoint
*cp
,
394 const std::string
§ion
)
398 if (!cp
->find(section
, "type", className
)) {
399 fatal("Serializable::create: no 'type' entry in section '%s'.\n",
403 CreateFunc createFunc
= (*classMap
)[className
];
405 if (createFunc
== NULL
) {
406 fatal("Serializable::create: no create function for class '%s'.\n",
410 Serializable
*object
= createFunc(cp
, section
);
412 assert(object
!= NULL
);
419 Serializable::create(Checkpoint
*cp
, const std::string
§ion
)
421 Serializable
*object
= SerializableClass::createObject(cp
, section
);
422 object
->unserialize(cp
, section
);
427 Checkpoint::Checkpoint(const std::string
&cpt_dir
, const std::string
&path
,
428 const ConfigNode
*_configNode
)
429 : db(new IniFile
), basePath(path
), configNode(_configNode
), cptDir(cpt_dir
)
431 string filename
= cpt_dir
+ "/" + Checkpoint::baseFilename
;
432 if (!db
->load(filename
)) {
433 fatal("Can't load checkpoint file '%s'\n", filename
);
439 Checkpoint::find(const std::string
§ion
, const std::string
&entry
,
442 return db
->find(section
, entry
, value
);
447 Checkpoint::findObj(const std::string
§ion
, const std::string
&entry
,
448 Serializable
*&value
)
452 if (!db
->find(section
, entry
, path
))
455 if ((value
= configNode
->resolveSimObject(path
)) != NULL
)
458 if ((value
= objMap
[path
]) != NULL
)
466 Checkpoint::sectionExists(const std::string
§ion
)
468 return db
->sectionExists(section
);