2 * Copyright (c) 2003 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>
38 #include "base/inifile.hh"
39 #include "base/misc.hh"
40 #include "base/str.hh"
41 #include "base/trace.hh"
42 #include "sim/config_node.hh"
43 #include "sim/eventq.hh"
44 #include "sim/param.hh"
45 #include "sim/serialize.hh"
46 #include "sim/sim_events.hh"
47 #include "sim/sim_object.hh"
52 Serializable::nameOut(ostream
&os
)
54 os
<< "\n[" << name() << "]\n";
58 Serializable::nameOut(ostream
&os
, const string
&_name
)
60 os
<< "\n[" << _name
<< "]\n";
65 paramOut(ostream
&os
, const std::string
&name
, const T
¶m
)
75 paramIn(Checkpoint
*cp
, const std::string
§ion
,
76 const std::string
&name
, T
¶m
)
79 if (!cp
->find(section
, name
, str
) || !parseParam(str
, param
)) {
80 fatal("Can't unserialize '%s:%s'\n", section
, name
);
87 arrayParamOut(ostream
&os
, const std::string
&name
,
88 const T
*param
, int size
)
92 showParam(os
, param
[0]);
93 for (int i
= 1; i
< size
; ++i
) {
95 showParam(os
, param
[i
]);
103 arrayParamIn(Checkpoint
*cp
, const std::string
§ion
,
104 const std::string
&name
, T
*param
, int size
)
107 if (!cp
->find(section
, name
, str
)) {
108 fatal("Can't unserialize '%s:%s'\n", section
, name
);
111 // code below stolen from VectorParam<T>::parse().
112 // it would be nice to unify these somehow...
114 vector
<string
> tokens
;
116 tokenize(tokens
, str
, ' ');
118 // Need this if we were doing a vector
119 // value.resize(tokens.size());
121 if (tokens
.size() != size
) {
122 fatal("Array size mismatch on %s:%s'\n", section
, name
);
125 for (int i
= 0; i
< tokens
.size(); i
++) {
126 // need to parse into local variable to handle vector<bool>,
127 // for which operator[] returns a special reference class
128 // that's not the same as 'bool&', (since it's a packed
131 if (!parseParam(tokens
[i
], scalar_value
)) {
132 string
err("could not parse \"");
140 // assign parsed value to vector
141 param
[i
] = scalar_value
;
147 objParamIn(Checkpoint
*cp
, const std::string
§ion
,
148 const std::string
&name
, Serializable
* ¶m
)
150 if (!cp
->findObj(section
, name
, param
)) {
151 fatal("Can't unserialize '%s:%s'\n", section
, name
);
156 #define INSTANTIATE_PARAM_TEMPLATES(type) \
158 paramOut(ostream &os, const std::string &name, type const ¶m); \
160 paramIn(Checkpoint *cp, const std::string §ion, \
161 const std::string &name, type & param); \
163 arrayParamOut(ostream &os, const std::string &name, \
164 type const *param, int size); \
166 arrayParamIn(Checkpoint *cp, const std::string §ion, \
167 const std::string &name, type *param, int size);
170 INSTANTIATE_PARAM_TEMPLATES(int8_t)
171 INSTANTIATE_PARAM_TEMPLATES(uint8_t)
172 INSTANTIATE_PARAM_TEMPLATES(int16_t)
173 INSTANTIATE_PARAM_TEMPLATES(uint16_t)
174 INSTANTIATE_PARAM_TEMPLATES(int32_t)
175 INSTANTIATE_PARAM_TEMPLATES(uint32_t)
176 INSTANTIATE_PARAM_TEMPLATES(int64_t)
177 INSTANTIATE_PARAM_TEMPLATES(uint64_t)
178 INSTANTIATE_PARAM_TEMPLATES(bool)
179 INSTANTIATE_PARAM_TEMPLATES(string
)
182 /////////////////////////////
184 /// Container for serializing global variables (not associated with
185 /// any serialized object).
186 class Globals
: public Serializable
189 const string
name() const;
190 void serialize(ostream
&os
);
191 void unserialize(Checkpoint
*cp
);
194 /// The one and only instance of the Globals class.
198 Globals::name() const
204 Globals::serialize(ostream
&os
)
207 SERIALIZE_SCALAR(curTick
);
209 nameOut(os
, "MainEventQueue");
210 mainEventQueue
.serialize(os
);
214 Globals::unserialize(Checkpoint
*cp
)
216 const string
§ion
= name();
217 UNSERIALIZE_SCALAR(curTick
);
219 mainEventQueue
.unserialize(cp
, "MainEventQueue");
223 Serializable::serializeAll()
225 string dir
= Checkpoint::dir();
226 if (mkdir(dir
.c_str(), 0775) == -1 && errno
!= EEXIST
)
227 fatal("couldn't mkdir %s\n", dir
);
229 string cpt_file
= dir
+ Checkpoint::baseFilename
;
230 ofstream
outstream(cpt_file
.c_str());
231 time_t t
= time(NULL
);
232 outstream
<< "// checkpoint generated: " << ctime(&t
);
234 globals
.serialize(outstream
);
235 SimObject::serializeAll(outstream
);
240 Serializable::unserializeGlobals(Checkpoint
*cp
)
242 globals
.unserialize(cp
);
246 class SerializeEvent
: public Event
252 SerializeEvent(Tick _when
, Tick _repeat
);
253 virtual void process();
254 virtual void serialize(std::ostream
&os
)
256 panic("Cannot serialize the SerializeEvent");
261 SerializeEvent::SerializeEvent(Tick _when
, Tick _repeat
)
262 : Event(&mainEventQueue
, Serialize_Pri
), repeat(_repeat
)
264 setFlags(AutoDelete
);
269 SerializeEvent::process()
271 Serializable::serializeAll();
273 schedule(curTick
+ repeat
);
276 const char *Checkpoint::baseFilename
= "m5.cpt";
278 static string checkpointDirBase
;
283 // use csprintf to insert curTick into directory name if it
284 // appears to have a format placeholder in it.
285 return (checkpointDirBase
.find("%") != string::npos
) ?
286 csprintf(checkpointDirBase
, curTick
) : checkpointDirBase
;
290 Checkpoint::setup(Tick when
, Tick period
)
292 new SerializeEvent(when
, period
);
295 class SerializeParamContext
: public ParamContext
298 SerializeEvent
*event
;
301 SerializeParamContext(const string
§ion
);
302 ~SerializeParamContext();
306 SerializeParamContext
serialParams("serialize");
308 Param
<string
> serialize_dir(&serialParams
, "dir",
309 "dir to stick checkpoint in "
310 "(sprintf format with cycle #)");
312 Param
<Counter
> serialize_cycle(&serialParams
,
314 "cycle to serialize",
317 Param
<Counter
> serialize_period(&serialParams
,
319 "period to repeat serializations",
324 SerializeParamContext::SerializeParamContext(const string
§ion
)
325 : ParamContext(section
), event(NULL
)
328 SerializeParamContext::~SerializeParamContext()
333 SerializeParamContext::checkParams()
335 if (serialize_dir
.isValid()) {
336 checkpointDirBase
= serialize_dir
;
338 if (outputDirectory
.empty())
339 checkpointDirBase
= "m5.%012d";
341 checkpointDirBase
= outputDirectory
+ "cpt.%012d";
344 // guarantee that directory ends with a '/'
345 if (checkpointDirBase
[checkpointDirBase
.size() - 1] != '/')
346 checkpointDirBase
+= "/";
348 if (serialize_cycle
> 0)
349 Checkpoint::setup(serialize_cycle
, serialize_period
);
355 Serializable::serializeAll();
359 debug_serialize(Tick when
)
361 new SerializeEvent(when
, 0);
364 ////////////////////////////////////////////////////////////////////////
366 // SerializableClass member definitions
368 ////////////////////////////////////////////////////////////////////////
370 // Map of class names to SerializableBuilder creation functions.
371 // Need to make this a pointer so we can force initialization on the
372 // first reference; otherwise, some SerializableClass constructors
373 // may be invoked before the classMap constructor.
374 map
<string
,SerializableClass::CreateFunc
> *SerializableClass::classMap
= 0;
376 // SerializableClass constructor: add mapping to classMap
377 SerializableClass::SerializableClass(const string
&className
,
378 CreateFunc createFunc
)
380 if (classMap
== NULL
)
381 classMap
= new map
<string
,SerializableClass::CreateFunc
>();
383 if ((*classMap
)[className
])
385 cerr
<< "Error: simulation object class " << className
<< " redefined"
390 // add className --> createFunc to class map
391 (*classMap
)[className
] = createFunc
;
398 SerializableClass::createObject(Checkpoint
*cp
,
399 const std::string
§ion
)
403 if (!cp
->find(section
, "type", className
)) {
404 fatal("Serializable::create: no 'type' entry in section '%s'.\n",
408 CreateFunc createFunc
= (*classMap
)[className
];
410 if (createFunc
== NULL
) {
411 fatal("Serializable::create: no create function for class '%s'.\n",
415 Serializable
*object
= createFunc(cp
, section
);
417 assert(object
!= NULL
);
424 Serializable::create(Checkpoint
*cp
, const std::string
§ion
)
426 Serializable
*object
= SerializableClass::createObject(cp
, section
);
427 object
->unserialize(cp
, section
);
432 Checkpoint::Checkpoint(const std::string
&cpt_dir
, const std::string
&path
,
433 const ConfigNode
*_configNode
)
434 : db(new IniFile
), basePath(path
), configNode(_configNode
)
436 string filename
= cpt_dir
+ "/" + Checkpoint::baseFilename
;
437 if (!db
->load(filename
)) {
438 fatal("Can't load checkpoint file '%s'\n", filename
);
444 Checkpoint::find(const std::string
§ion
, const std::string
&entry
,
447 return db
->find(section
, entry
, value
);
452 Checkpoint::findObj(const std::string
§ion
, const std::string
&entry
,
453 Serializable
*&value
)
457 if (!db
->find(section
, entry
, path
))
460 if ((value
= configNode
->resolveSimObject(path
)) != NULL
)
463 if ((value
= objMap
[path
]) != NULL
)
471 Checkpoint::sectionExists(const std::string
§ion
)
473 return db
->sectionExists(section
);