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: Nathan Binkert
34 #include <sys/types.h>
43 #include "base/inifile.hh"
44 #include "base/misc.hh"
45 #include "base/output.hh"
46 #include "base/str.hh"
47 #include "base/trace.hh"
48 #include "sim/eventq.hh"
49 #include "sim/param.hh"
50 #include "sim/serialize.hh"
51 #include "sim/sim_events.hh"
52 #include "sim/sim_exit.hh"
53 #include "sim/sim_object.hh"
55 // For stat reset hack
56 #include "sim/stat_control.hh"
60 extern SimObject
*resolveSimObject(const string
&);
62 int Serializable::ckptMaxCount
= 0;
63 int Serializable::ckptCount
= 0;
64 int Serializable::ckptPrevCount
= -1;
67 Serializable::nameOut(ostream
&os
)
69 os
<< "\n[" << name() << "]\n";
73 Serializable::nameOut(ostream
&os
, const string
&_name
)
75 os
<< "\n[" << _name
<< "]\n";
80 paramOut(ostream
&os
, const std::string
&name
, const T
¶m
)
90 paramIn(Checkpoint
*cp
, const std::string
§ion
,
91 const std::string
&name
, T
¶m
)
94 if (!cp
->find(section
, name
, str
) || !parseParam(str
, param
)) {
95 fatal("Can't unserialize '%s:%s'\n", section
, name
);
102 arrayParamOut(ostream
&os
, const std::string
&name
,
103 const T
*param
, int size
)
107 showParam(os
, param
[0]);
108 for (int i
= 1; i
< size
; ++i
) {
110 showParam(os
, param
[i
]);
118 arrayParamIn(Checkpoint
*cp
, const std::string
§ion
,
119 const std::string
&name
, T
*param
, int size
)
122 if (!cp
->find(section
, name
, str
)) {
123 fatal("Can't unserialize '%s:%s'\n", section
, name
);
126 // code below stolen from VectorParam<T>::parse().
127 // it would be nice to unify these somehow...
129 vector
<string
> tokens
;
131 tokenize(tokens
, str
, ' ');
133 // Need this if we were doing a vector
134 // value.resize(tokens.size());
136 if (tokens
.size() != size
) {
137 fatal("Array size mismatch on %s:%s'\n", section
, name
);
140 for (int i
= 0; i
< tokens
.size(); i
++) {
141 // need to parse into local variable to handle vector<bool>,
142 // for which operator[] returns a special reference class
143 // that's not the same as 'bool&', (since it's a packed
146 if (!parseParam(tokens
[i
], scalar_value
)) {
147 string
err("could not parse \"");
155 // assign parsed value to vector
156 param
[i
] = scalar_value
;
162 objParamIn(Checkpoint
*cp
, const std::string
§ion
,
163 const std::string
&name
, SimObject
* ¶m
)
165 if (!cp
->findObj(section
, name
, param
)) {
166 fatal("Can't unserialize '%s:%s'\n", section
, name
);
171 #define INSTANTIATE_PARAM_TEMPLATES(type) \
173 paramOut(ostream &os, const std::string &name, type const ¶m); \
175 paramIn(Checkpoint *cp, const std::string §ion, \
176 const std::string &name, type & param); \
178 arrayParamOut(ostream &os, const std::string &name, \
179 type const *param, int size); \
181 arrayParamIn(Checkpoint *cp, const std::string §ion, \
182 const std::string &name, type *param, int size);
184 INSTANTIATE_PARAM_TEMPLATES(signed char)
185 INSTANTIATE_PARAM_TEMPLATES(unsigned char)
186 INSTANTIATE_PARAM_TEMPLATES(signed short)
187 INSTANTIATE_PARAM_TEMPLATES(unsigned short)
188 INSTANTIATE_PARAM_TEMPLATES(signed int)
189 INSTANTIATE_PARAM_TEMPLATES(unsigned int)
190 INSTANTIATE_PARAM_TEMPLATES(signed long)
191 INSTANTIATE_PARAM_TEMPLATES(unsigned long)
192 INSTANTIATE_PARAM_TEMPLATES(signed long long)
193 INSTANTIATE_PARAM_TEMPLATES(unsigned long long)
194 INSTANTIATE_PARAM_TEMPLATES(bool)
195 INSTANTIATE_PARAM_TEMPLATES(string
)
198 /////////////////////////////
200 /// Container for serializing global variables (not associated with
201 /// any serialized object).
202 class Globals
: public Serializable
205 const string
name() const;
206 void serialize(ostream
&os
);
207 void unserialize(Checkpoint
*cp
);
210 /// The one and only instance of the Globals class.
214 Globals::name() const
220 Globals::serialize(ostream
&os
)
223 SERIALIZE_SCALAR(curTick
);
225 nameOut(os
, "MainEventQueue");
226 mainEventQueue
.serialize(os
);
230 Globals::unserialize(Checkpoint
*cp
)
232 const string
§ion
= name();
233 UNSERIALIZE_SCALAR(curTick
);
235 mainEventQueue
.unserialize(cp
, "MainEventQueue");
239 Serializable::serializeAll(const std::string
&cpt_dir
)
241 setCheckpointDir(cpt_dir
);
242 string dir
= Checkpoint::dir();
243 if (mkdir(dir
.c_str(), 0775) == -1 && errno
!= EEXIST
)
244 fatal("couldn't mkdir %s\n", dir
);
246 string cpt_file
= dir
+ Checkpoint::baseFilename
;
247 ofstream
outstream(cpt_file
.c_str());
248 time_t t
= time(NULL
);
249 outstream
<< "// checkpoint generated: " << ctime(&t
);
251 globals
.serialize(outstream
);
252 SimObject::serializeAll(outstream
);
256 Serializable::unserializeAll(const std::string
&cpt_dir
)
258 setCheckpointDir(cpt_dir
);
259 string dir
= Checkpoint::dir();
260 string cpt_file
= dir
+ Checkpoint::baseFilename
;
263 DPRINTFR(Config
, "Loading checkpoint dir '%s'\n",
265 Checkpoint
*cp
= new Checkpoint(dir
, section
);
266 unserializeGlobals(cp
);
268 SimObject::unserializeAll(cp
);
272 Serializable::unserializeGlobals(Checkpoint
*cp
)
274 globals
.unserialize(cp
);
277 const char *Checkpoint::baseFilename
= "m5.cpt";
279 static string checkpointDirBase
;
282 setCheckpointDir(const std::string
&name
)
284 checkpointDirBase
= name
;
285 if (checkpointDirBase
[checkpointDirBase
.size() - 1] != '/')
286 checkpointDirBase
+= "/";
292 // use csprintf to insert curTick into directory name if it
293 // appears to have a format placeholder in it.
294 return (checkpointDirBase
.find("%") != string::npos
) ?
295 csprintf(checkpointDirBase
, curTick
) : checkpointDirBase
;
299 debug_serialize(const std::string
&cpt_dir
)
301 Serializable::serializeAll(cpt_dir
);
305 ////////////////////////////////////////////////////////////////////////
307 // SerializableClass member definitions
309 ////////////////////////////////////////////////////////////////////////
311 // Map of class names to SerializableBuilder creation functions.
312 // Need to make this a pointer so we can force initialization on the
313 // first reference; otherwise, some SerializableClass constructors
314 // may be invoked before the classMap constructor.
315 map
<string
,SerializableClass::CreateFunc
> *SerializableClass::classMap
= 0;
317 // SerializableClass constructor: add mapping to classMap
318 SerializableClass::SerializableClass(const string
&className
,
319 CreateFunc createFunc
)
321 if (classMap
== NULL
)
322 classMap
= new map
<string
,SerializableClass::CreateFunc
>();
324 if ((*classMap
)[className
])
326 cerr
<< "Error: simulation object class " << className
<< " redefined"
331 // add className --> createFunc to class map
332 (*classMap
)[className
] = createFunc
;
339 SerializableClass::createObject(Checkpoint
*cp
,
340 const std::string
§ion
)
344 if (!cp
->find(section
, "type", className
)) {
345 fatal("Serializable::create: no 'type' entry in section '%s'.\n",
349 CreateFunc createFunc
= (*classMap
)[className
];
351 if (createFunc
== NULL
) {
352 fatal("Serializable::create: no create function for class '%s'.\n",
356 Serializable
*object
= createFunc(cp
, section
);
358 assert(object
!= NULL
);
365 Serializable::create(Checkpoint
*cp
, const std::string
§ion
)
367 Serializable
*object
= SerializableClass::createObject(cp
, section
);
368 object
->unserialize(cp
, section
);
373 Checkpoint::Checkpoint(const std::string
&cpt_dir
, const std::string
&path
)
374 : db(new IniFile
), basePath(path
), cptDir(cpt_dir
)
376 string filename
= cpt_dir
+ "/" + Checkpoint::baseFilename
;
377 if (!db
->load(filename
)) {
378 fatal("Can't load checkpoint file '%s'\n", filename
);
384 Checkpoint::find(const std::string
§ion
, const std::string
&entry
,
387 return db
->find(section
, entry
, value
);
392 Checkpoint::findObj(const std::string
§ion
, const std::string
&entry
,
397 if (!db
->find(section
, entry
, path
))
400 value
= resolveSimObject(path
);
406 Checkpoint::sectionExists(const std::string
§ion
)
408 return db
->sectionExists(section
);