c4ef124bb5723490a644a56bcacfe4cc71fa78e2
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::ckptMaxCount
= 0;
55 int Serializable::ckptCount
= 0;
56 int Serializable::ckptPrevCount
= -1;
59 Serializable::nameOut(ostream
&os
)
61 os
<< "\n[" << name() << "]\n";
65 Serializable::nameOut(ostream
&os
, const string
&_name
)
67 os
<< "\n[" << _name
<< "]\n";
72 paramOut(ostream
&os
, const std::string
&name
, const T
¶m
)
82 paramIn(Checkpoint
*cp
, const std::string
§ion
,
83 const std::string
&name
, T
¶m
)
86 if (!cp
->find(section
, name
, str
) || !parseParam(str
, param
)) {
87 fatal("Can't unserialize '%s:%s'\n", section
, name
);
94 arrayParamOut(ostream
&os
, const std::string
&name
,
95 const T
*param
, int size
)
99 showParam(os
, param
[0]);
100 for (int i
= 1; i
< size
; ++i
) {
102 showParam(os
, param
[i
]);
110 arrayParamIn(Checkpoint
*cp
, const std::string
§ion
,
111 const std::string
&name
, T
*param
, int size
)
114 if (!cp
->find(section
, name
, str
)) {
115 fatal("Can't unserialize '%s:%s'\n", section
, name
);
118 // code below stolen from VectorParam<T>::parse().
119 // it would be nice to unify these somehow...
121 vector
<string
> tokens
;
123 tokenize(tokens
, str
, ' ');
125 // Need this if we were doing a vector
126 // value.resize(tokens.size());
128 if (tokens
.size() != size
) {
129 fatal("Array size mismatch on %s:%s'\n", section
, name
);
132 for (int i
= 0; i
< tokens
.size(); i
++) {
133 // need to parse into local variable to handle vector<bool>,
134 // for which operator[] returns a special reference class
135 // that's not the same as 'bool&', (since it's a packed
138 if (!parseParam(tokens
[i
], scalar_value
)) {
139 string
err("could not parse \"");
147 // assign parsed value to vector
148 param
[i
] = scalar_value
;
154 objParamIn(Checkpoint
*cp
, const std::string
§ion
,
155 const std::string
&name
, Serializable
* ¶m
)
157 if (!cp
->findObj(section
, name
, param
)) {
158 fatal("Can't unserialize '%s:%s'\n", section
, name
);
163 #define INSTANTIATE_PARAM_TEMPLATES(type) \
165 paramOut(ostream &os, const std::string &name, type const ¶m); \
167 paramIn(Checkpoint *cp, const std::string §ion, \
168 const std::string &name, type & param); \
170 arrayParamOut(ostream &os, const std::string &name, \
171 type const *param, int size); \
173 arrayParamIn(Checkpoint *cp, const std::string §ion, \
174 const std::string &name, type *param, int size);
176 INSTANTIATE_PARAM_TEMPLATES(signed char)
177 INSTANTIATE_PARAM_TEMPLATES(unsigned char)
178 INSTANTIATE_PARAM_TEMPLATES(signed short)
179 INSTANTIATE_PARAM_TEMPLATES(unsigned short)
180 INSTANTIATE_PARAM_TEMPLATES(signed int)
181 INSTANTIATE_PARAM_TEMPLATES(unsigned int)
182 INSTANTIATE_PARAM_TEMPLATES(signed long)
183 INSTANTIATE_PARAM_TEMPLATES(unsigned long)
184 INSTANTIATE_PARAM_TEMPLATES(signed long long)
185 INSTANTIATE_PARAM_TEMPLATES(unsigned long long)
186 INSTANTIATE_PARAM_TEMPLATES(bool)
187 INSTANTIATE_PARAM_TEMPLATES(string
)
190 /////////////////////////////
192 /// Container for serializing global variables (not associated with
193 /// any serialized object).
194 class Globals
: public Serializable
197 const string
name() const;
198 void serialize(ostream
&os
);
199 void unserialize(Checkpoint
*cp
);
202 /// The one and only instance of the Globals class.
206 Globals::name() const
212 Globals::serialize(ostream
&os
)
215 SERIALIZE_SCALAR(curTick
);
217 nameOut(os
, "MainEventQueue");
218 mainEventQueue
.serialize(os
);
222 Globals::unserialize(Checkpoint
*cp
)
224 const string
§ion
= name();
225 UNSERIALIZE_SCALAR(curTick
);
227 mainEventQueue
.unserialize(cp
, "MainEventQueue");
231 Serializable::serializeAll()
233 string dir
= Checkpoint::dir();
234 if (mkdir(dir
.c_str(), 0775) == -1 && errno
!= EEXIST
)
235 fatal("couldn't mkdir %s\n", dir
);
237 string cpt_file
= dir
+ Checkpoint::baseFilename
;
238 ofstream
outstream(cpt_file
.c_str());
239 time_t t
= time(NULL
);
240 outstream
<< "// checkpoint generated: " << ctime(&t
);
242 globals
.serialize(outstream
);
243 SimObject::serializeAll(outstream
);
245 assert(Serializable::ckptPrevCount
+ 1 == Serializable::ckptCount
);
246 Serializable::ckptPrevCount
++;
247 if (ckptMaxCount
&& ++ckptCount
>= ckptMaxCount
)
248 SimExit(curTick
+ 1, "Maximum number of checkpoints dropped");
254 Serializable::unserializeGlobals(Checkpoint
*cp
)
256 globals
.unserialize(cp
);
260 class SerializeEvent
: public Event
266 SerializeEvent(Tick _when
, Tick _repeat
);
267 virtual void process();
268 virtual void serialize(std::ostream
&os
)
270 panic("Cannot serialize the SerializeEvent");
275 SerializeEvent::SerializeEvent(Tick _when
, Tick _repeat
)
276 : Event(&mainEventQueue
, Serialize_Pri
), repeat(_repeat
)
278 setFlags(AutoDelete
);
283 SerializeEvent::process()
285 Serializable::serializeAll();
287 schedule(curTick
+ repeat
);
290 const char *Checkpoint::baseFilename
= "m5.cpt";
292 static string checkpointDirBase
;
297 // use csprintf to insert curTick into directory name if it
298 // appears to have a format placeholder in it.
299 return (checkpointDirBase
.find("%") != string::npos
) ?
300 csprintf(checkpointDirBase
, curTick
) : checkpointDirBase
;
304 Checkpoint::setup(Tick when
, Tick period
)
306 new SerializeEvent(when
, period
);
309 class SerializeParamContext
: public ParamContext
312 SerializeEvent
*event
;
315 SerializeParamContext(const string
§ion
);
316 ~SerializeParamContext();
320 SerializeParamContext
serialParams("serialize");
322 Param
<string
> serialize_dir(&serialParams
, "dir",
323 "dir to stick checkpoint in "
324 "(sprintf format with cycle #)");
326 Param
<Counter
> serialize_cycle(&serialParams
,
328 "cycle to serialize",
331 Param
<Counter
> serialize_period(&serialParams
,
333 "period to repeat serializations",
336 Param
<int> serialize_count(&serialParams
, "count",
337 "maximum number of checkpoints to drop");
339 SerializeParamContext::SerializeParamContext(const string
§ion
)
340 : ParamContext(section
), event(NULL
)
343 SerializeParamContext::~SerializeParamContext()
348 SerializeParamContext::checkParams()
350 checkpointDirBase
= simout
.resolve(serialize_dir
);
352 // guarantee that directory ends with a '/'
353 if (checkpointDirBase
[checkpointDirBase
.size() - 1] != '/')
354 checkpointDirBase
+= "/";
356 if (serialize_cycle
> 0)
357 Checkpoint::setup(serialize_cycle
, serialize_period
);
359 Serializable::ckptMaxCount
= serialize_count
;
365 Serializable::serializeAll();
369 debug_serialize(Tick when
)
371 new SerializeEvent(when
, 0);
374 ////////////////////////////////////////////////////////////////////////
376 // SerializableClass member definitions
378 ////////////////////////////////////////////////////////////////////////
380 // Map of class names to SerializableBuilder creation functions.
381 // Need to make this a pointer so we can force initialization on the
382 // first reference; otherwise, some SerializableClass constructors
383 // may be invoked before the classMap constructor.
384 map
<string
,SerializableClass::CreateFunc
> *SerializableClass::classMap
= 0;
386 // SerializableClass constructor: add mapping to classMap
387 SerializableClass::SerializableClass(const string
&className
,
388 CreateFunc createFunc
)
390 if (classMap
== NULL
)
391 classMap
= new map
<string
,SerializableClass::CreateFunc
>();
393 if ((*classMap
)[className
])
395 cerr
<< "Error: simulation object class " << className
<< " redefined"
400 // add className --> createFunc to class map
401 (*classMap
)[className
] = createFunc
;
408 SerializableClass::createObject(Checkpoint
*cp
,
409 const std::string
§ion
)
413 if (!cp
->find(section
, "type", className
)) {
414 fatal("Serializable::create: no 'type' entry in section '%s'.\n",
418 CreateFunc createFunc
= (*classMap
)[className
];
420 if (createFunc
== NULL
) {
421 fatal("Serializable::create: no create function for class '%s'.\n",
425 Serializable
*object
= createFunc(cp
, section
);
427 assert(object
!= NULL
);
434 Serializable::create(Checkpoint
*cp
, const std::string
§ion
)
436 Serializable
*object
= SerializableClass::createObject(cp
, section
);
437 object
->unserialize(cp
, section
);
442 Checkpoint::Checkpoint(const std::string
&cpt_dir
, const std::string
&path
,
443 const ConfigNode
*_configNode
)
444 : db(new IniFile
), basePath(path
), configNode(_configNode
), cptDir(cpt_dir
)
446 string filename
= cpt_dir
+ "/" + Checkpoint::baseFilename
;
447 if (!db
->load(filename
)) {
448 fatal("Can't load checkpoint file '%s'\n", filename
);
454 Checkpoint::find(const std::string
§ion
, const std::string
&entry
,
457 return db
->find(section
, entry
, value
);
462 Checkpoint::findObj(const std::string
§ion
, const std::string
&entry
,
463 Serializable
*&value
)
467 if (!db
->find(section
, entry
, path
))
470 if ((value
= configNode
->resolveSimObject(path
)) != NULL
)
473 if ((value
= objMap
[path
]) != NULL
)
481 Checkpoint::sectionExists(const std::string
§ion
)
483 return db
->sectionExists(section
);