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"
51 Serializer
*Serializeable::serializer
= NULL
;
53 Serializeable::Serializeable()
57 Serializeable::~Serializeable()
64 serializer
->add_object(this);
70 Serializeable::nameOut(ostream
&os
)
72 os
<< "\n[" << name() << "]\n";
76 Serializeable::nameOut(ostream
&os
, const string
&_name
)
78 os
<< "\n[" << _name
<< "]\n";
83 paramOut(ostream
&os
, const std::string
&name
, const T
& param
)
93 paramIn(Checkpoint
*cp
, const std::string
§ion
,
94 const std::string
&name
, T
& param
)
97 if (!cp
->find(section
, name
, str
) || !parseParam(str
, param
)) {
98 fatal("Can't unserialize '%s:%s'\n", section
, name
);
105 arrayParamOut(ostream
&os
, const std::string
&name
,
106 const T
*param
, int size
)
110 showParam(os
, param
[0]);
111 for (int i
= 1; i
< size
; ++i
) {
113 showParam(os
, param
[i
]);
121 arrayParamIn(Checkpoint
*cp
, const std::string
§ion
,
122 const std::string
&name
, T
*param
, int size
)
125 if (!cp
->find(section
, name
, str
)) {
126 fatal("Can't unserialize '%s:%s'\n", section
, name
);
129 // code below stolen from VectorParam<T>::parse().
130 // it would be nice to unify these somehow...
132 vector
<string
> tokens
;
134 tokenize(tokens
, str
, ' ');
136 // Need this if we were doing a vector
137 // value.resize(tokens.size());
139 if (tokens
.size() != size
) {
140 fatal("Array size mismatch on %s:%s'\n", section
, name
);
143 for (int i
= 0; i
< tokens
.size(); i
++) {
144 // need to parse into local variable to handle vector<bool>,
145 // for which operator[] returns a special reference class
146 // that's not the same as 'bool&', (since it's a packed
149 if (!parseParam(tokens
[i
], scalar_value
)) {
150 string
err("could not parse \"");
158 // assign parsed value to vector
159 param
[i
] = scalar_value
;
165 objParamIn(Checkpoint
*cp
, const std::string
§ion
,
166 const std::string
&name
, Serializeable
* ¶m
)
168 if (!cp
->findObj(section
, name
, param
)) {
169 fatal("Can't unserialize '%s:%s'\n", section
, name
);
174 #define INSTANTIATE_PARAM_TEMPLATES(type) \
176 paramOut(ostream &os, const std::string &name, type const ¶m); \
178 paramIn(Checkpoint *cp, const std::string §ion, \
179 const std::string &name, type & param); \
181 arrayParamOut(ostream &os, const std::string &name, \
182 type const *param, int size); \
184 arrayParamIn(Checkpoint *cp, const std::string §ion, \
185 const std::string &name, type *param, int size);
188 INSTANTIATE_PARAM_TEMPLATES(int8_t)
189 INSTANTIATE_PARAM_TEMPLATES(uint8_t)
190 INSTANTIATE_PARAM_TEMPLATES(int16_t)
191 INSTANTIATE_PARAM_TEMPLATES(uint16_t)
192 INSTANTIATE_PARAM_TEMPLATES(int32_t)
193 INSTANTIATE_PARAM_TEMPLATES(uint32_t)
194 INSTANTIATE_PARAM_TEMPLATES(int64_t)
195 INSTANTIATE_PARAM_TEMPLATES(uint64_t)
196 INSTANTIATE_PARAM_TEMPLATES(bool)
197 INSTANTIATE_PARAM_TEMPLATES(string
)
203 Serializeable::childOut(const string
&name
, Serializeable
*child
)
206 if (child
->name() == "")
207 panic("child is unnamed");
209 out() << name
<< "=" << child
->name() << "\n";
213 Serializer::Serializer()
216 Serializer::~Serializer()
220 Serializer::out() const
223 panic("must set output before serializing");
229 Serializer::add_object(Serializeable
*obj
)
231 objects
.push_back(obj
);
235 Serializer::add_objects()
237 mainEventQueue
.mark();
239 SimObject::SimObjectList::iterator i
= SimObject::simObjectList
.begin();
240 SimObject::SimObjectList::iterator end
= SimObject::simObjectList
.end();
249 Serializer::serialize()
251 if (Serializeable::serializer
!= NULL
)
252 panic("in process of serializing!");
254 Serializeable::serializer
= this;
256 string dir
= CheckpointDir();
257 if (mkdir(dir
.c_str(), 0775) == -1 && errno
!= EEXIST
)
258 warn("could mkdir %s\n", dir
);
260 string cpt_file
= dir
+ "m5.cpt";
261 output
= new ofstream(cpt_file
.c_str());
262 time_t t
= time(NULL
);
263 *output
<< "// checkpoint generated: " << ctime(&t
);
268 while (!objects
.empty()) {
269 Serializeable
*obj
= objects
.front();
270 DPRINTF(Serialize
, "Serializing %s\n", obj
->name());
272 obj
->serialize(out());
277 while (!list
.empty()) {
278 list
.front()->serialized
= false;
282 Serializeable::serializer
= NULL
;
288 class SerializeEvent
: public Event
294 SerializeEvent(Tick _when
, Tick _repeat
);
295 virtual void process();
296 virtual void serialize(std::ostream
&os
)
298 panic("Cannot serialize the SerializeEvent");
303 SerializeEvent::SerializeEvent(Tick _when
, Tick _repeat
)
304 : Event(&mainEventQueue
, 990), repeat(_repeat
)
306 setFlags(AutoDelete
);
311 SerializeEvent::process()
316 schedule(curTick
+ repeat
);
319 string __CheckpointDirBase
;
324 if (__CheckpointDirBase
.empty())
325 return __CheckpointDirBase
;
327 return csprintf("%s/m5.%012d/", __CheckpointDirBase
, curTick
);
331 SetupCheckpoint(Tick when
, Tick period
)
333 new SerializeEvent(when
, period
);
336 class SerializeParamContext
: public ParamContext
339 SerializeEvent
*event
;
342 SerializeParamContext(const string
§ion
);
343 ~SerializeParamContext();
347 SerializeParamContext
serialParams("serialize");
349 Param
<string
> serialize_dir(&serialParams
,
351 "dir to stick checkpoint in", ".");
353 Param
<Counter
> serialize_cycle(&serialParams
,
355 "cycle to serialize",
358 Param
<Counter
> serialize_period(&serialParams
,
360 "period to repeat serializations",
365 SerializeParamContext::SerializeParamContext(const string
§ion
)
366 : ParamContext(section
), event(NULL
)
369 SerializeParamContext::~SerializeParamContext()
374 SerializeParamContext::checkParams()
376 __CheckpointDirBase
= serialize_dir
;
377 if (serialize_cycle
> 0)
378 SetupCheckpoint(serialize_cycle
, serialize_period
);
389 debug_serialize(Tick when
)
391 new SerializeEvent(when
, 0);
394 ////////////////////////////////////////////////////////////////////////
396 // SerializeableClass member definitions
398 ////////////////////////////////////////////////////////////////////////
400 // Map of class names to SerializeableBuilder creation functions.
401 // Need to make this a pointer so we can force initialization on the
402 // first reference; otherwise, some SerializeableClass constructors
403 // may be invoked before the classMap constructor.
404 map
<string
,SerializeableClass::CreateFunc
> *SerializeableClass::classMap
= 0;
406 // SerializeableClass constructor: add mapping to classMap
407 SerializeableClass::SerializeableClass(const string
&className
,
408 CreateFunc createFunc
)
410 if (classMap
== NULL
)
411 classMap
= new map
<string
,SerializeableClass::CreateFunc
>();
413 if ((*classMap
)[className
])
415 cerr
<< "Error: simulation object class " << className
<< " redefined"
420 // add className --> createFunc to class map
421 (*classMap
)[className
] = createFunc
;
428 SerializeableClass::createObject(Checkpoint
*cp
,
429 const std::string
§ion
)
433 if (!cp
->find(section
, "type", className
)) {
434 fatal("Serializeable::create: no 'type' entry in section '%s'.\n",
438 CreateFunc createFunc
= (*classMap
)[className
];
440 if (createFunc
== NULL
) {
441 fatal("Serializeable::create: no create function for class '%s'.\n",
445 Serializeable
*object
= createFunc(cp
, section
);
447 assert(object
!= NULL
);
454 Serializeable::create(Checkpoint
*cp
, const std::string
§ion
)
456 Serializeable
*object
= SerializeableClass::createObject(cp
, section
);
457 object
->unserialize(cp
, section
);
462 Checkpoint::Checkpoint(const std::string
&filename
, const std::string
&path
,
463 const ConfigNode
*_configNode
)
464 : db(new IniFile
), basePath(path
), configNode(_configNode
)
466 if (!db
->load(filename
)) {
467 fatal("Can't load checkpoint file '%s'\n", filename
);
470 mainEventQueue
.unserialize(this, "MainEventQueue");
475 Checkpoint::find(const std::string
§ion
, const std::string
&entry
,
478 return db
->find(section
, entry
, value
);
483 Checkpoint::findObj(const std::string
§ion
, const std::string
&entry
,
484 Serializeable
*&value
)
488 if (!db
->find(section
, entry
, path
))
491 if ((value
= configNode
->resolveSimObject(path
)) != NULL
)
494 if ((value
= objMap
[path
]) != NULL
)
502 Checkpoint::sectionExists(const std::string
§ion
)
504 return db
->sectionExists(section
);