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.
28 * Authors: Erik Hallnor
33 #include <sys/types.h>
42 #include "base/inifile.hh"
43 #include "base/misc.hh"
44 #include "base/output.hh"
45 #include "base/str.hh"
46 #include "base/trace.hh"
47 #include "sim/eventq.hh"
48 #include "sim/param.hh"
49 #include "sim/serialize.hh"
50 #include "sim/sim_events.hh"
51 #include "sim/sim_exit.hh"
52 #include "sim/sim_object.hh"
56 int Serializable::ckptMaxCount
= 0;
57 int Serializable::ckptCount
= 0;
58 int Serializable::ckptPrevCount
= -1;
61 Serializable::nameOut(ostream
&os
)
63 os
<< "\n[" << name() << "]\n";
67 Serializable::nameOut(ostream
&os
, const string
&_name
)
69 os
<< "\n[" << _name
<< "]\n";
74 paramOut(ostream
&os
, const std::string
&name
, const T
¶m
)
84 paramIn(Checkpoint
*cp
, const std::string
§ion
,
85 const std::string
&name
, T
¶m
)
88 if (!cp
->find(section
, name
, str
) || !parseParam(str
, param
)) {
89 fatal("Can't unserialize '%s:%s'\n", section
, name
);
96 arrayParamOut(ostream
&os
, const std::string
&name
,
97 const T
*param
, int size
)
101 showParam(os
, param
[0]);
102 for (int i
= 1; i
< size
; ++i
) {
104 showParam(os
, param
[i
]);
112 arrayParamIn(Checkpoint
*cp
, const std::string
§ion
,
113 const std::string
&name
, T
*param
, int size
)
116 if (!cp
->find(section
, name
, str
)) {
117 fatal("Can't unserialize '%s:%s'\n", section
, name
);
120 // code below stolen from VectorParam<T>::parse().
121 // it would be nice to unify these somehow...
123 vector
<string
> tokens
;
125 tokenize(tokens
, str
, ' ');
127 // Need this if we were doing a vector
128 // value.resize(tokens.size());
130 if (tokens
.size() != size
) {
131 fatal("Array size mismatch on %s:%s'\n", section
, name
);
134 for (int i
= 0; i
< tokens
.size(); i
++) {
135 // need to parse into local variable to handle vector<bool>,
136 // for which operator[] returns a special reference class
137 // that's not the same as 'bool&', (since it's a packed
140 if (!parseParam(tokens
[i
], scalar_value
)) {
141 string
err("could not parse \"");
149 // assign parsed value to vector
150 param
[i
] = scalar_value
;
156 objParamIn(Checkpoint
*cp
, const std::string
§ion
,
157 const std::string
&name
, Serializable
* ¶m
)
159 if (!cp
->findObj(section
, name
, param
)) {
160 fatal("Can't unserialize '%s:%s'\n", section
, name
);
165 #define INSTANTIATE_PARAM_TEMPLATES(type) \
167 paramOut(ostream &os, const std::string &name, type const ¶m); \
169 paramIn(Checkpoint *cp, const std::string §ion, \
170 const std::string &name, type & param); \
172 arrayParamOut(ostream &os, const std::string &name, \
173 type const *param, int size); \
175 arrayParamIn(Checkpoint *cp, const std::string §ion, \
176 const std::string &name, type *param, int size);
178 INSTANTIATE_PARAM_TEMPLATES(signed char)
179 INSTANTIATE_PARAM_TEMPLATES(unsigned char)
180 INSTANTIATE_PARAM_TEMPLATES(signed short)
181 INSTANTIATE_PARAM_TEMPLATES(unsigned short)
182 INSTANTIATE_PARAM_TEMPLATES(signed int)
183 INSTANTIATE_PARAM_TEMPLATES(unsigned int)
184 INSTANTIATE_PARAM_TEMPLATES(signed long)
185 INSTANTIATE_PARAM_TEMPLATES(unsigned long)
186 INSTANTIATE_PARAM_TEMPLATES(signed long long)
187 INSTANTIATE_PARAM_TEMPLATES(unsigned long long)
188 INSTANTIATE_PARAM_TEMPLATES(bool)
189 INSTANTIATE_PARAM_TEMPLATES(string
)
192 /////////////////////////////
194 /// Container for serializing global variables (not associated with
195 /// any serialized object).
196 class Globals
: public Serializable
199 const string
name() const;
200 void serialize(ostream
&os
);
201 void unserialize(Checkpoint
*cp
);
204 /// The one and only instance of the Globals class.
208 Globals::name() const
214 Globals::serialize(ostream
&os
)
217 SERIALIZE_SCALAR(curTick
);
219 nameOut(os
, "MainEventQueue");
220 mainEventQueue
.serialize(os
);
224 Globals::unserialize(Checkpoint
*cp
)
226 const string
§ion
= name();
227 UNSERIALIZE_SCALAR(curTick
);
229 mainEventQueue
.unserialize(cp
, "MainEventQueue");
233 Serializable::serializeAll()
235 string dir
= Checkpoint::dir();
236 if (mkdir(dir
.c_str(), 0775) == -1 && errno
!= EEXIST
)
237 fatal("couldn't mkdir %s\n", dir
);
239 string cpt_file
= dir
+ Checkpoint::baseFilename
;
240 ofstream
outstream(cpt_file
.c_str());
241 time_t t
= time(NULL
);
242 outstream
<< "// checkpoint generated: " << ctime(&t
);
244 globals
.serialize(outstream
);
245 SimObject::serializeAll(outstream
);
247 assert(Serializable::ckptPrevCount
+ 1 == Serializable::ckptCount
);
248 Serializable::ckptPrevCount
++;
249 if (ckptMaxCount
&& ++ckptCount
>= ckptMaxCount
)
250 exitSimLoop(curTick
+ 1, "Maximum number of checkpoints dropped");
256 Serializable::unserializeGlobals(Checkpoint
*cp
)
258 globals
.unserialize(cp
);
262 class SerializeEvent
: public Event
268 SerializeEvent(Tick _when
, Tick _repeat
);
269 virtual void process();
270 virtual void serialize(std::ostream
&os
)
272 panic("Cannot serialize the SerializeEvent");
277 SerializeEvent::SerializeEvent(Tick _when
, Tick _repeat
)
278 : Event(&mainEventQueue
, Serialize_Pri
), repeat(_repeat
)
280 setFlags(AutoDelete
);
285 SerializeEvent::process()
287 Serializable::serializeAll();
289 schedule(curTick
+ repeat
);
292 const char *Checkpoint::baseFilename
= "m5.cpt";
294 static string checkpointDirBase
;
299 // use csprintf to insert curTick into directory name if it
300 // appears to have a format placeholder in it.
301 return (checkpointDirBase
.find("%") != string::npos
) ?
302 csprintf(checkpointDirBase
, curTick
) : checkpointDirBase
;
306 Checkpoint::setup(Tick when
, Tick period
)
308 new SerializeEvent(when
, period
);
311 class SerializeParamContext
: public ParamContext
314 SerializeEvent
*event
;
317 SerializeParamContext(const string
§ion
);
318 ~SerializeParamContext();
322 SerializeParamContext
serialParams("serialize");
324 Param
<string
> serialize_dir(&serialParams
, "dir",
325 "dir to stick checkpoint in "
326 "(sprintf format with cycle #)");
328 Param
<Counter
> serialize_cycle(&serialParams
,
330 "cycle to serialize",
333 Param
<Counter
> serialize_period(&serialParams
,
335 "period to repeat serializations",
338 Param
<int> serialize_count(&serialParams
, "count",
339 "maximum number of checkpoints to drop");
341 SerializeParamContext::SerializeParamContext(const string
§ion
)
342 : ParamContext(section
), event(NULL
)
345 SerializeParamContext::~SerializeParamContext()
350 SerializeParamContext::checkParams()
352 checkpointDirBase
= simout
.resolve(serialize_dir
);
354 // guarantee that directory ends with a '/'
355 if (checkpointDirBase
[checkpointDirBase
.size() - 1] != '/')
356 checkpointDirBase
+= "/";
358 if (serialize_cycle
> 0)
359 Checkpoint::setup(serialize_cycle
, serialize_period
);
361 Serializable::ckptMaxCount
= serialize_count
;
367 Serializable::serializeAll();
371 debug_serialize(Tick when
)
373 new SerializeEvent(when
, 0);
376 ////////////////////////////////////////////////////////////////////////
378 // SerializableClass member definitions
380 ////////////////////////////////////////////////////////////////////////
382 // Map of class names to SerializableBuilder creation functions.
383 // Need to make this a pointer so we can force initialization on the
384 // first reference; otherwise, some SerializableClass constructors
385 // may be invoked before the classMap constructor.
386 map
<string
,SerializableClass::CreateFunc
> *SerializableClass::classMap
= 0;
388 // SerializableClass constructor: add mapping to classMap
389 SerializableClass::SerializableClass(const string
&className
,
390 CreateFunc createFunc
)
392 if (classMap
== NULL
)
393 classMap
= new map
<string
,SerializableClass::CreateFunc
>();
395 if ((*classMap
)[className
])
397 cerr
<< "Error: simulation object class " << className
<< " redefined"
402 // add className --> createFunc to class map
403 (*classMap
)[className
] = createFunc
;
410 SerializableClass::createObject(Checkpoint
*cp
,
411 const std::string
§ion
)
415 if (!cp
->find(section
, "type", className
)) {
416 fatal("Serializable::create: no 'type' entry in section '%s'.\n",
420 CreateFunc createFunc
= (*classMap
)[className
];
422 if (createFunc
== NULL
) {
423 fatal("Serializable::create: no create function for class '%s'.\n",
427 Serializable
*object
= createFunc(cp
, section
);
429 assert(object
!= NULL
);
436 Serializable::create(Checkpoint
*cp
, const std::string
§ion
)
438 Serializable
*object
= SerializableClass::createObject(cp
, section
);
439 object
->unserialize(cp
, section
);
444 Checkpoint::Checkpoint(const std::string
&cpt_dir
, const std::string
&path
)
445 : db(new IniFile
), basePath(path
), cptDir(cpt_dir
)
447 string filename
= cpt_dir
+ "/" + Checkpoint::baseFilename
;
448 if (!db
->load(filename
)) {
449 fatal("Can't load checkpoint file '%s'\n", filename
);
455 Checkpoint::find(const std::string
§ion
, const std::string
&entry
,
458 return db
->find(section
, entry
, value
);
463 Checkpoint::findObj(const std::string
§ion
, const std::string
&entry
,
464 Serializable
*&value
)
468 if (!db
->find(section
, entry
, path
))
471 if ((value
= objMap
[path
]) != NULL
)
479 Checkpoint::sectionExists(const std::string
§ion
)
481 return db
->sectionExists(section
);