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/str.hh"
42 #include "base/trace.hh"
43 #include "sim/config_node.hh"
44 #include "sim/eventq.hh"
45 #include "sim/param.hh"
46 #include "sim/serialize.hh"
47 #include "sim/sim_events.hh"
48 #include "sim/sim_object.hh"
53 Serializable::nameOut(ostream
&os
)
55 os
<< "\n[" << name() << "]\n";
59 Serializable::nameOut(ostream
&os
, const string
&_name
)
61 os
<< "\n[" << _name
<< "]\n";
66 paramOut(ostream
&os
, const std::string
&name
, const T
¶m
)
76 paramIn(Checkpoint
*cp
, const std::string
§ion
,
77 const std::string
&name
, T
¶m
)
80 if (!cp
->find(section
, name
, str
) || !parseParam(str
, param
)) {
81 fatal("Can't unserialize '%s:%s'\n", section
, name
);
88 arrayParamOut(ostream
&os
, const std::string
&name
,
89 const T
*param
, int size
)
93 showParam(os
, param
[0]);
94 for (int i
= 1; i
< size
; ++i
) {
96 showParam(os
, param
[i
]);
104 arrayParamIn(Checkpoint
*cp
, const std::string
§ion
,
105 const std::string
&name
, T
*param
, int size
)
108 if (!cp
->find(section
, name
, str
)) {
109 fatal("Can't unserialize '%s:%s'\n", section
, name
);
112 // code below stolen from VectorParam<T>::parse().
113 // it would be nice to unify these somehow...
115 vector
<string
> tokens
;
117 tokenize(tokens
, str
, ' ');
119 // Need this if we were doing a vector
120 // value.resize(tokens.size());
122 if (tokens
.size() != size
) {
123 fatal("Array size mismatch on %s:%s'\n", section
, name
);
126 for (int i
= 0; i
< tokens
.size(); i
++) {
127 // need to parse into local variable to handle vector<bool>,
128 // for which operator[] returns a special reference class
129 // that's not the same as 'bool&', (since it's a packed
132 if (!parseParam(tokens
[i
], scalar_value
)) {
133 string
err("could not parse \"");
141 // assign parsed value to vector
142 param
[i
] = scalar_value
;
148 objParamIn(Checkpoint
*cp
, const std::string
§ion
,
149 const std::string
&name
, Serializable
* ¶m
)
151 if (!cp
->findObj(section
, name
, param
)) {
152 fatal("Can't unserialize '%s:%s'\n", section
, name
);
157 #define INSTANTIATE_PARAM_TEMPLATES(type) \
159 paramOut(ostream &os, const std::string &name, type const ¶m); \
161 paramIn(Checkpoint *cp, const std::string §ion, \
162 const std::string &name, type & param); \
164 arrayParamOut(ostream &os, const std::string &name, \
165 type const *param, int size); \
167 arrayParamIn(Checkpoint *cp, const std::string §ion, \
168 const std::string &name, type *param, int size);
171 INSTANTIATE_PARAM_TEMPLATES(int8_t)
172 INSTANTIATE_PARAM_TEMPLATES(uint8_t)
173 INSTANTIATE_PARAM_TEMPLATES(int16_t)
174 INSTANTIATE_PARAM_TEMPLATES(uint16_t)
175 INSTANTIATE_PARAM_TEMPLATES(int32_t)
176 INSTANTIATE_PARAM_TEMPLATES(uint32_t)
177 INSTANTIATE_PARAM_TEMPLATES(int64_t)
178 INSTANTIATE_PARAM_TEMPLATES(uint64_t)
179 INSTANTIATE_PARAM_TEMPLATES(bool)
180 INSTANTIATE_PARAM_TEMPLATES(string
)
183 /////////////////////////////
185 /// Container for serializing global variables (not associated with
186 /// any serialized object).
187 class Globals
: public Serializable
190 const string
name() const;
191 void serialize(ostream
&os
);
192 void unserialize(Checkpoint
*cp
);
195 /// The one and only instance of the Globals class.
199 Globals::name() const
205 Globals::serialize(ostream
&os
)
208 SERIALIZE_SCALAR(curTick
);
210 nameOut(os
, "MainEventQueue");
211 mainEventQueue
.serialize(os
);
215 Globals::unserialize(Checkpoint
*cp
)
217 const string
§ion
= name();
218 UNSERIALIZE_SCALAR(curTick
);
220 mainEventQueue
.unserialize(cp
, "MainEventQueue");
224 Serializable::serializeAll()
226 string dir
= Checkpoint::dir();
227 if (mkdir(dir
.c_str(), 0775) == -1 && errno
!= EEXIST
)
228 fatal("couldn't mkdir %s\n", dir
);
230 string cpt_file
= dir
+ Checkpoint::baseFilename
;
231 ofstream
outstream(cpt_file
.c_str());
232 time_t t
= time(NULL
);
233 outstream
<< "// checkpoint generated: " << ctime(&t
);
235 globals
.serialize(outstream
);
236 SimObject::serializeAll(outstream
);
241 Serializable::unserializeGlobals(Checkpoint
*cp
)
243 globals
.unserialize(cp
);
247 class SerializeEvent
: public Event
253 SerializeEvent(Tick _when
, Tick _repeat
);
254 virtual void process();
255 virtual void serialize(std::ostream
&os
)
257 panic("Cannot serialize the SerializeEvent");
262 SerializeEvent::SerializeEvent(Tick _when
, Tick _repeat
)
263 : Event(&mainEventQueue
, Serialize_Pri
), repeat(_repeat
)
265 setFlags(AutoDelete
);
270 SerializeEvent::process()
272 Serializable::serializeAll();
274 schedule(curTick
+ repeat
);
277 const char *Checkpoint::baseFilename
= "m5.cpt";
279 static string checkpointDirBase
;
284 // use csprintf to insert curTick into directory name if it
285 // appears to have a format placeholder in it.
286 return (checkpointDirBase
.find("%") != string::npos
) ?
287 csprintf(checkpointDirBase
, curTick
) : checkpointDirBase
;
291 Checkpoint::setup(Tick when
, Tick period
)
293 new SerializeEvent(when
, period
);
296 class SerializeParamContext
: public ParamContext
299 SerializeEvent
*event
;
302 SerializeParamContext(const string
§ion
);
303 ~SerializeParamContext();
307 SerializeParamContext
serialParams("serialize");
309 Param
<string
> serialize_dir(&serialParams
, "dir",
310 "dir to stick checkpoint in "
311 "(sprintf format with cycle #)");
313 Param
<Counter
> serialize_cycle(&serialParams
,
315 "cycle to serialize",
318 Param
<Counter
> serialize_period(&serialParams
,
320 "period to repeat serializations",
325 SerializeParamContext::SerializeParamContext(const string
§ion
)
326 : ParamContext(section
), event(NULL
)
329 SerializeParamContext::~SerializeParamContext()
334 SerializeParamContext::checkParams()
336 if (serialize_dir
.isValid()) {
337 checkpointDirBase
= serialize_dir
;
339 checkpointDirBase
= outputDirectory
+ "cpt.%012d";
342 // guarantee that directory ends with a '/'
343 if (checkpointDirBase
[checkpointDirBase
.size() - 1] != '/')
344 checkpointDirBase
+= "/";
346 if (serialize_cycle
> 0)
347 Checkpoint::setup(serialize_cycle
, serialize_period
);
353 Serializable::serializeAll();
357 debug_serialize(Tick when
)
359 new SerializeEvent(when
, 0);
362 ////////////////////////////////////////////////////////////////////////
364 // SerializableClass member definitions
366 ////////////////////////////////////////////////////////////////////////
368 // Map of class names to SerializableBuilder creation functions.
369 // Need to make this a pointer so we can force initialization on the
370 // first reference; otherwise, some SerializableClass constructors
371 // may be invoked before the classMap constructor.
372 map
<string
,SerializableClass::CreateFunc
> *SerializableClass::classMap
= 0;
374 // SerializableClass constructor: add mapping to classMap
375 SerializableClass::SerializableClass(const string
&className
,
376 CreateFunc createFunc
)
378 if (classMap
== NULL
)
379 classMap
= new map
<string
,SerializableClass::CreateFunc
>();
381 if ((*classMap
)[className
])
383 cerr
<< "Error: simulation object class " << className
<< " redefined"
388 // add className --> createFunc to class map
389 (*classMap
)[className
] = createFunc
;
396 SerializableClass::createObject(Checkpoint
*cp
,
397 const std::string
§ion
)
401 if (!cp
->find(section
, "type", className
)) {
402 fatal("Serializable::create: no 'type' entry in section '%s'.\n",
406 CreateFunc createFunc
= (*classMap
)[className
];
408 if (createFunc
== NULL
) {
409 fatal("Serializable::create: no create function for class '%s'.\n",
413 Serializable
*object
= createFunc(cp
, section
);
415 assert(object
!= NULL
);
422 Serializable::create(Checkpoint
*cp
, const std::string
§ion
)
424 Serializable
*object
= SerializableClass::createObject(cp
, section
);
425 object
->unserialize(cp
, section
);
430 Checkpoint::Checkpoint(const std::string
&cpt_dir
, const std::string
&path
,
431 const ConfigNode
*_configNode
)
432 : db(new IniFile
), basePath(path
), configNode(_configNode
), cptDir(cpt_dir
)
434 string filename
= cpt_dir
+ "/" + Checkpoint::baseFilename
;
435 if (!db
->load(filename
)) {
436 fatal("Can't load checkpoint file '%s'\n", filename
);
442 Checkpoint::find(const std::string
§ion
, const std::string
&entry
,
445 return db
->find(section
, entry
, value
);
450 Checkpoint::findObj(const std::string
§ion
, const std::string
&entry
,
451 Serializable
*&value
)
455 if (!db
->find(section
, entry
, path
))
458 if ((value
= configNode
->resolveSimObject(path
)) != NULL
)
461 if ((value
= objMap
[path
]) != NULL
)
469 Checkpoint::sectionExists(const std::string
§ion
)
471 return db
->sectionExists(section
);