2 * Copyright (c) 2002-2005 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_exit.hh"
50 #include "sim/sim_object.hh"
54 int Serializable::maxCount
= 0;
55 int Serializable::count
= 0;
58 Serializable::nameOut(ostream
&os
)
60 os
<< "\n[" << name() << "]\n";
64 Serializable::nameOut(ostream
&os
, const string
&_name
)
66 os
<< "\n[" << _name
<< "]\n";
71 paramOut(ostream
&os
, const std::string
&name
, const T
¶m
)
81 paramIn(Checkpoint
*cp
, const std::string
§ion
,
82 const std::string
&name
, T
¶m
)
85 if (!cp
->find(section
, name
, str
) || !parseParam(str
, param
)) {
86 fatal("Can't unserialize '%s:%s'\n", section
, name
);
93 arrayParamOut(ostream
&os
, const std::string
&name
,
94 const T
*param
, int size
)
98 showParam(os
, param
[0]);
99 for (int i
= 1; i
< size
; ++i
) {
101 showParam(os
, param
[i
]);
109 arrayParamIn(Checkpoint
*cp
, const std::string
§ion
,
110 const std::string
&name
, T
*param
, int size
)
113 if (!cp
->find(section
, name
, str
)) {
114 fatal("Can't unserialize '%s:%s'\n", section
, name
);
117 // code below stolen from VectorParam<T>::parse().
118 // it would be nice to unify these somehow...
120 vector
<string
> tokens
;
122 tokenize(tokens
, str
, ' ');
124 // Need this if we were doing a vector
125 // value.resize(tokens.size());
127 if (tokens
.size() != size
) {
128 fatal("Array size mismatch on %s:%s'\n", section
, name
);
131 for (int i
= 0; i
< tokens
.size(); i
++) {
132 // need to parse into local variable to handle vector<bool>,
133 // for which operator[] returns a special reference class
134 // that's not the same as 'bool&', (since it's a packed
137 if (!parseParam(tokens
[i
], scalar_value
)) {
138 string
err("could not parse \"");
146 // assign parsed value to vector
147 param
[i
] = scalar_value
;
153 objParamIn(Checkpoint
*cp
, const std::string
§ion
,
154 const std::string
&name
, Serializable
* ¶m
)
156 if (!cp
->findObj(section
, name
, param
)) {
157 fatal("Can't unserialize '%s:%s'\n", section
, name
);
162 #define INSTANTIATE_PARAM_TEMPLATES(type) \
164 paramOut(ostream &os, const std::string &name, type const ¶m); \
166 paramIn(Checkpoint *cp, const std::string §ion, \
167 const std::string &name, type & param); \
169 arrayParamOut(ostream &os, const std::string &name, \
170 type const *param, int size); \
172 arrayParamIn(Checkpoint *cp, const std::string §ion, \
173 const std::string &name, type *param, int size);
175 INSTANTIATE_PARAM_TEMPLATES(signed char)
176 INSTANTIATE_PARAM_TEMPLATES(unsigned char)
177 INSTANTIATE_PARAM_TEMPLATES(signed short)
178 INSTANTIATE_PARAM_TEMPLATES(unsigned short)
179 INSTANTIATE_PARAM_TEMPLATES(signed int)
180 INSTANTIATE_PARAM_TEMPLATES(unsigned int)
181 INSTANTIATE_PARAM_TEMPLATES(signed long)
182 INSTANTIATE_PARAM_TEMPLATES(unsigned long)
183 INSTANTIATE_PARAM_TEMPLATES(signed long long)
184 INSTANTIATE_PARAM_TEMPLATES(unsigned long long)
185 INSTANTIATE_PARAM_TEMPLATES(bool)
186 INSTANTIATE_PARAM_TEMPLATES(string
)
189 /////////////////////////////
191 /// Container for serializing global variables (not associated with
192 /// any serialized object).
193 class Globals
: public Serializable
196 const string
name() const;
197 void serialize(ostream
&os
);
198 void unserialize(Checkpoint
*cp
);
201 /// The one and only instance of the Globals class.
205 Globals::name() const
211 Globals::serialize(ostream
&os
)
214 SERIALIZE_SCALAR(curTick
);
216 nameOut(os
, "MainEventQueue");
217 mainEventQueue
.serialize(os
);
221 Globals::unserialize(Checkpoint
*cp
)
223 const string
§ion
= name();
224 UNSERIALIZE_SCALAR(curTick
);
226 mainEventQueue
.unserialize(cp
, "MainEventQueue");
230 Serializable::serializeAll()
232 string dir
= Checkpoint::dir();
233 if (mkdir(dir
.c_str(), 0775) == -1 && errno
!= EEXIST
)
234 fatal("couldn't mkdir %s\n", dir
);
236 string cpt_file
= dir
+ Checkpoint::baseFilename
;
237 ofstream
outstream(cpt_file
.c_str());
238 time_t t
= time(NULL
);
239 outstream
<< "// checkpoint generated: " << ctime(&t
);
241 globals
.serialize(outstream
);
242 SimObject::serializeAll(outstream
);
244 if (maxCount
&& ++count
>= maxCount
)
245 SimExit(curTick
+ 1, "Maximum number of checkpoints dropped");
250 Serializable::unserializeGlobals(Checkpoint
*cp
)
252 globals
.unserialize(cp
);
256 class SerializeEvent
: public Event
262 SerializeEvent(Tick _when
, Tick _repeat
);
263 virtual void process();
264 virtual void serialize(std::ostream
&os
)
266 panic("Cannot serialize the SerializeEvent");
271 SerializeEvent::SerializeEvent(Tick _when
, Tick _repeat
)
272 : Event(&mainEventQueue
, Serialize_Pri
), repeat(_repeat
)
274 setFlags(AutoDelete
);
279 SerializeEvent::process()
281 Serializable::serializeAll();
283 schedule(curTick
+ repeat
);
286 const char *Checkpoint::baseFilename
= "m5.cpt";
288 static string checkpointDirBase
;
293 // use csprintf to insert curTick into directory name if it
294 // appears to have a format placeholder in it.
295 return (checkpointDirBase
.find("%") != string::npos
) ?
296 csprintf(checkpointDirBase
, curTick
) : checkpointDirBase
;
300 Checkpoint::setup(Tick when
, Tick period
)
302 new SerializeEvent(when
, period
);
305 class SerializeParamContext
: public ParamContext
308 SerializeEvent
*event
;
311 SerializeParamContext(const string
§ion
);
312 ~SerializeParamContext();
316 SerializeParamContext
serialParams("serialize");
318 Param
<string
> serialize_dir(&serialParams
, "dir",
319 "dir to stick checkpoint in "
320 "(sprintf format with cycle #)");
322 Param
<Counter
> serialize_cycle(&serialParams
,
324 "cycle to serialize",
327 Param
<Counter
> serialize_period(&serialParams
,
329 "period to repeat serializations",
332 Param
<int> serialize_count(&serialParams
, "count",
333 "maximum number of checkpoints to drop");
335 SerializeParamContext::SerializeParamContext(const string
§ion
)
336 : ParamContext(section
), event(NULL
)
339 SerializeParamContext::~SerializeParamContext()
344 SerializeParamContext::checkParams()
346 checkpointDirBase
= simout
.resolve(serialize_dir
);
348 // guarantee that directory ends with a '/'
349 if (checkpointDirBase
[checkpointDirBase
.size() - 1] != '/')
350 checkpointDirBase
+= "/";
352 if (serialize_cycle
> 0)
353 Checkpoint::setup(serialize_cycle
, serialize_period
);
355 Serializable::maxCount
= serialize_count
;
361 Serializable::serializeAll();
365 debug_serialize(Tick when
)
367 new SerializeEvent(when
, 0);
370 ////////////////////////////////////////////////////////////////////////
372 // SerializableClass member definitions
374 ////////////////////////////////////////////////////////////////////////
376 // Map of class names to SerializableBuilder creation functions.
377 // Need to make this a pointer so we can force initialization on the
378 // first reference; otherwise, some SerializableClass constructors
379 // may be invoked before the classMap constructor.
380 map
<string
,SerializableClass::CreateFunc
> *SerializableClass::classMap
= 0;
382 // SerializableClass constructor: add mapping to classMap
383 SerializableClass::SerializableClass(const string
&className
,
384 CreateFunc createFunc
)
386 if (classMap
== NULL
)
387 classMap
= new map
<string
,SerializableClass::CreateFunc
>();
389 if ((*classMap
)[className
])
391 cerr
<< "Error: simulation object class " << className
<< " redefined"
396 // add className --> createFunc to class map
397 (*classMap
)[className
] = createFunc
;
404 SerializableClass::createObject(Checkpoint
*cp
,
405 const std::string
§ion
)
409 if (!cp
->find(section
, "type", className
)) {
410 fatal("Serializable::create: no 'type' entry in section '%s'.\n",
414 CreateFunc createFunc
= (*classMap
)[className
];
416 if (createFunc
== NULL
) {
417 fatal("Serializable::create: no create function for class '%s'.\n",
421 Serializable
*object
= createFunc(cp
, section
);
423 assert(object
!= NULL
);
430 Serializable::create(Checkpoint
*cp
, const std::string
§ion
)
432 Serializable
*object
= SerializableClass::createObject(cp
, section
);
433 object
->unserialize(cp
, section
);
438 Checkpoint::Checkpoint(const std::string
&cpt_dir
, const std::string
&path
,
439 const ConfigNode
*_configNode
)
440 : db(new IniFile
), basePath(path
), configNode(_configNode
), cptDir(cpt_dir
)
442 string filename
= cpt_dir
+ "/" + Checkpoint::baseFilename
;
443 if (!db
->load(filename
)) {
444 fatal("Can't load checkpoint file '%s'\n", filename
);
450 Checkpoint::find(const std::string
§ion
, const std::string
&entry
,
453 return db
->find(section
, entry
, value
);
458 Checkpoint::findObj(const std::string
§ion
, const std::string
&entry
,
459 Serializable
*&value
)
463 if (!db
->find(section
, entry
, path
))
466 if ((value
= configNode
->resolveSimObject(path
)) != NULL
)
469 if ((value
= objMap
[path
]) != NULL
)
477 Checkpoint::sectionExists(const std::string
§ion
)
479 return db
->sectionExists(section
);