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 int Serializable::ckptMaxCount
= 0;
61 int Serializable::ckptCount
= 0;
62 int Serializable::ckptPrevCount
= -1;
65 Serializable::nameOut(ostream
&os
)
67 os
<< "\n[" << name() << "]\n";
71 Serializable::nameOut(ostream
&os
, const string
&_name
)
73 os
<< "\n[" << _name
<< "]\n";
78 paramOut(ostream
&os
, const std::string
&name
, const T
¶m
)
88 paramIn(Checkpoint
*cp
, const std::string
§ion
,
89 const std::string
&name
, T
¶m
)
92 if (!cp
->find(section
, name
, str
) || !parseParam(str
, param
)) {
93 fatal("Can't unserialize '%s:%s'\n", section
, name
);
100 arrayParamOut(ostream
&os
, const std::string
&name
,
101 const T
*param
, int size
)
105 showParam(os
, param
[0]);
106 for (int i
= 1; i
< size
; ++i
) {
108 showParam(os
, param
[i
]);
116 arrayParamIn(Checkpoint
*cp
, const std::string
§ion
,
117 const std::string
&name
, T
*param
, int size
)
120 if (!cp
->find(section
, name
, str
)) {
121 fatal("Can't unserialize '%s:%s'\n", section
, name
);
124 // code below stolen from VectorParam<T>::parse().
125 // it would be nice to unify these somehow...
127 vector
<string
> tokens
;
129 tokenize(tokens
, str
, ' ');
131 // Need this if we were doing a vector
132 // value.resize(tokens.size());
134 if (tokens
.size() != size
) {
135 fatal("Array size mismatch on %s:%s'\n", section
, name
);
138 for (int i
= 0; i
< tokens
.size(); i
++) {
139 // need to parse into local variable to handle vector<bool>,
140 // for which operator[] returns a special reference class
141 // that's not the same as 'bool&', (since it's a packed
144 if (!parseParam(tokens
[i
], scalar_value
)) {
145 string
err("could not parse \"");
153 // assign parsed value to vector
154 param
[i
] = scalar_value
;
160 objParamIn(Checkpoint
*cp
, const std::string
§ion
,
161 const std::string
&name
, Serializable
* ¶m
)
163 if (!cp
->findObj(section
, name
, param
)) {
164 fatal("Can't unserialize '%s:%s'\n", section
, name
);
169 #define INSTANTIATE_PARAM_TEMPLATES(type) \
171 paramOut(ostream &os, const std::string &name, type const ¶m); \
173 paramIn(Checkpoint *cp, const std::string §ion, \
174 const std::string &name, type & param); \
176 arrayParamOut(ostream &os, const std::string &name, \
177 type const *param, int size); \
179 arrayParamIn(Checkpoint *cp, const std::string §ion, \
180 const std::string &name, type *param, int size);
182 INSTANTIATE_PARAM_TEMPLATES(signed char)
183 INSTANTIATE_PARAM_TEMPLATES(unsigned char)
184 INSTANTIATE_PARAM_TEMPLATES(signed short)
185 INSTANTIATE_PARAM_TEMPLATES(unsigned short)
186 INSTANTIATE_PARAM_TEMPLATES(signed int)
187 INSTANTIATE_PARAM_TEMPLATES(unsigned int)
188 INSTANTIATE_PARAM_TEMPLATES(signed long)
189 INSTANTIATE_PARAM_TEMPLATES(unsigned long)
190 INSTANTIATE_PARAM_TEMPLATES(signed long long)
191 INSTANTIATE_PARAM_TEMPLATES(unsigned long long)
192 INSTANTIATE_PARAM_TEMPLATES(bool)
193 INSTANTIATE_PARAM_TEMPLATES(string
)
196 /////////////////////////////
198 /// Container for serializing global variables (not associated with
199 /// any serialized object).
200 class Globals
: public Serializable
203 const string
name() const;
204 void serialize(ostream
&os
);
205 void unserialize(Checkpoint
*cp
);
208 /// The one and only instance of the Globals class.
212 Globals::name() const
218 Globals::serialize(ostream
&os
)
221 SERIALIZE_SCALAR(curTick
);
223 nameOut(os
, "MainEventQueue");
224 mainEventQueue
.serialize(os
);
228 Globals::unserialize(Checkpoint
*cp
)
230 const string
§ion
= name();
231 UNSERIALIZE_SCALAR(curTick
);
233 mainEventQueue
.unserialize(cp
, "MainEventQueue");
237 Serializable::serializeAll(const std::string
&cpt_dir
)
239 setCheckpointDir(cpt_dir
);
240 string dir
= Checkpoint::dir();
241 if (mkdir(dir
.c_str(), 0775) == -1 && errno
!= EEXIST
)
242 fatal("couldn't mkdir %s\n", dir
);
244 string cpt_file
= dir
+ Checkpoint::baseFilename
;
245 ofstream
outstream(cpt_file
.c_str());
246 time_t t
= time(NULL
);
247 outstream
<< "// checkpoint generated: " << ctime(&t
);
249 globals
.serialize(outstream
);
250 SimObject::serializeAll(outstream
);
254 Serializable::unserializeAll(const std::string
&cpt_dir
)
256 setCheckpointDir(cpt_dir
);
257 string dir
= Checkpoint::dir();
258 string cpt_file
= dir
+ Checkpoint::baseFilename
;
261 DPRINTFR(Config
, "Loading checkpoint dir '%s'\n",
263 Checkpoint
*cp
= new Checkpoint(dir
, section
);
264 unserializeGlobals(cp
);
266 SimObject::unserializeAll(cp
);
270 Serializable::unserializeGlobals(Checkpoint
*cp
)
272 globals
.unserialize(cp
);
275 const char *Checkpoint::baseFilename
= "m5.cpt";
277 static string checkpointDirBase
;
280 setCheckpointDir(const std::string
&name
)
282 checkpointDirBase
= name
;
283 if (checkpointDirBase
[checkpointDirBase
.size() - 1] != '/')
284 checkpointDirBase
+= "/";
290 // use csprintf to insert curTick into directory name if it
291 // appears to have a format placeholder in it.
292 return (checkpointDirBase
.find("%") != string::npos
) ?
293 csprintf(checkpointDirBase
, curTick
) : checkpointDirBase
;
297 debug_serialize(const std::string
&cpt_dir
)
299 Serializable::serializeAll(cpt_dir
);
303 ////////////////////////////////////////////////////////////////////////
305 // SerializableClass member definitions
307 ////////////////////////////////////////////////////////////////////////
309 // Map of class names to SerializableBuilder creation functions.
310 // Need to make this a pointer so we can force initialization on the
311 // first reference; otherwise, some SerializableClass constructors
312 // may be invoked before the classMap constructor.
313 map
<string
,SerializableClass::CreateFunc
> *SerializableClass::classMap
= 0;
315 // SerializableClass constructor: add mapping to classMap
316 SerializableClass::SerializableClass(const string
&className
,
317 CreateFunc createFunc
)
319 if (classMap
== NULL
)
320 classMap
= new map
<string
,SerializableClass::CreateFunc
>();
322 if ((*classMap
)[className
])
324 cerr
<< "Error: simulation object class " << className
<< " redefined"
329 // add className --> createFunc to class map
330 (*classMap
)[className
] = createFunc
;
337 SerializableClass::createObject(Checkpoint
*cp
,
338 const std::string
§ion
)
342 if (!cp
->find(section
, "type", className
)) {
343 fatal("Serializable::create: no 'type' entry in section '%s'.\n",
347 CreateFunc createFunc
= (*classMap
)[className
];
349 if (createFunc
== NULL
) {
350 fatal("Serializable::create: no create function for class '%s'.\n",
354 Serializable
*object
= createFunc(cp
, section
);
356 assert(object
!= NULL
);
363 Serializable::create(Checkpoint
*cp
, const std::string
§ion
)
365 Serializable
*object
= SerializableClass::createObject(cp
, section
);
366 object
->unserialize(cp
, section
);
371 Checkpoint::Checkpoint(const std::string
&cpt_dir
, const std::string
&path
)
372 : db(new IniFile
), basePath(path
), cptDir(cpt_dir
)
374 string filename
= cpt_dir
+ "/" + Checkpoint::baseFilename
;
375 if (!db
->load(filename
)) {
376 fatal("Can't load checkpoint file '%s'\n", filename
);
382 Checkpoint::find(const std::string
§ion
, const std::string
&entry
,
385 return db
->find(section
, entry
, value
);
390 Checkpoint::findObj(const std::string
§ion
, const std::string
&entry
,
391 Serializable
*&value
)
395 if (!db
->find(section
, entry
, path
))
398 if ((value
= objMap
[path
]) != NULL
)
406 Checkpoint::sectionExists(const std::string
§ion
)
408 return db
->sectionExists(section
);
411 /** Hacked stat reset event */
413 class StatresetParamContext
: public ParamContext
416 StatresetParamContext(const string
§ion
);
417 ~StatresetParamContext();
421 StatresetParamContext
statParams("statsreset");
423 Param
<Tick
> reset_cycle(&statParams
, "reset_cycle",
424 "Cycle to reset stats on", 0);
426 StatresetParamContext::StatresetParamContext(const string
§ion
)
427 : ParamContext(section
)
430 StatresetParamContext::~StatresetParamContext()
435 StatresetParamContext::startup()
437 if (reset_cycle
> 0) {
438 Stats::SetupEvent(Stats::Reset
, curTick
+ reset_cycle
, 0);
439 cprintf("Stats reset event scheduled for %lli\n",
440 curTick
+ reset_cycle
);