Merge ktlim@zamp:./local/clean/o3-merge/m5
[gem5.git] / src / sim / serialize.cc
1 /*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 *
28 * Authors: Nathan Binkert
29 * Erik Hallnor
30 * Steve Reinhardt
31 */
32
33 #include <sys/time.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <errno.h>
37
38 #include <fstream>
39 #include <list>
40 #include <string>
41 #include <vector>
42
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"
54
55 // For stat reset hack
56 #include "sim/stat_control.hh"
57
58 using namespace std;
59
60 int Serializable::ckptMaxCount = 0;
61 int Serializable::ckptCount = 0;
62 int Serializable::ckptPrevCount = -1;
63
64 void
65 Serializable::nameOut(ostream &os)
66 {
67 os << "\n[" << name() << "]\n";
68 }
69
70 void
71 Serializable::nameOut(ostream &os, const string &_name)
72 {
73 os << "\n[" << _name << "]\n";
74 }
75
76 template <class T>
77 void
78 paramOut(ostream &os, const std::string &name, const T &param)
79 {
80 os << name << "=";
81 showParam(os, param);
82 os << "\n";
83 }
84
85
86 template <class T>
87 void
88 paramIn(Checkpoint *cp, const std::string &section,
89 const std::string &name, T &param)
90 {
91 std::string str;
92 if (!cp->find(section, name, str) || !parseParam(str, param)) {
93 fatal("Can't unserialize '%s:%s'\n", section, name);
94 }
95 }
96
97
98 template <class T>
99 void
100 arrayParamOut(ostream &os, const std::string &name,
101 const T *param, int size)
102 {
103 os << name << "=";
104 if (size > 0)
105 showParam(os, param[0]);
106 for (int i = 1; i < size; ++i) {
107 os << " ";
108 showParam(os, param[i]);
109 }
110 os << "\n";
111 }
112
113
114 template <class T>
115 void
116 arrayParamIn(Checkpoint *cp, const std::string &section,
117 const std::string &name, T *param, int size)
118 {
119 std::string str;
120 if (!cp->find(section, name, str)) {
121 fatal("Can't unserialize '%s:%s'\n", section, name);
122 }
123
124 // code below stolen from VectorParam<T>::parse().
125 // it would be nice to unify these somehow...
126
127 vector<string> tokens;
128
129 tokenize(tokens, str, ' ');
130
131 // Need this if we were doing a vector
132 // value.resize(tokens.size());
133
134 if (tokens.size() != size) {
135 fatal("Array size mismatch on %s:%s'\n", section, name);
136 }
137
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
142 // vector)
143 T scalar_value;
144 if (!parseParam(tokens[i], scalar_value)) {
145 string err("could not parse \"");
146
147 err += str;
148 err += "\"";
149
150 fatal(err);
151 }
152
153 // assign parsed value to vector
154 param[i] = scalar_value;
155 }
156 }
157
158
159 void
160 objParamIn(Checkpoint *cp, const std::string &section,
161 const std::string &name, Serializable * &param)
162 {
163 if (!cp->findObj(section, name, param)) {
164 fatal("Can't unserialize '%s:%s'\n", section, name);
165 }
166 }
167
168
169 #define INSTANTIATE_PARAM_TEMPLATES(type) \
170 template void \
171 paramOut(ostream &os, const std::string &name, type const &param); \
172 template void \
173 paramIn(Checkpoint *cp, const std::string &section, \
174 const std::string &name, type & param); \
175 template void \
176 arrayParamOut(ostream &os, const std::string &name, \
177 type const *param, int size); \
178 template void \
179 arrayParamIn(Checkpoint *cp, const std::string &section, \
180 const std::string &name, type *param, int size);
181
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)
194
195
196 /////////////////////////////
197
198 /// Container for serializing global variables (not associated with
199 /// any serialized object).
200 class Globals : public Serializable
201 {
202 public:
203 const string name() const;
204 void serialize(ostream &os);
205 void unserialize(Checkpoint *cp);
206 };
207
208 /// The one and only instance of the Globals class.
209 Globals globals;
210
211 const string
212 Globals::name() const
213 {
214 return "Globals";
215 }
216
217 void
218 Globals::serialize(ostream &os)
219 {
220 nameOut(os);
221 SERIALIZE_SCALAR(curTick);
222
223 nameOut(os, "MainEventQueue");
224 mainEventQueue.serialize(os);
225 }
226
227 void
228 Globals::unserialize(Checkpoint *cp)
229 {
230 const string &section = name();
231 UNSERIALIZE_SCALAR(curTick);
232
233 mainEventQueue.unserialize(cp, "MainEventQueue");
234 }
235
236 void
237 Serializable::serializeAll(const std::string &cpt_dir)
238 {
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);
243
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);
248
249 globals.serialize(outstream);
250 SimObject::serializeAll(outstream);
251 }
252
253 void
254 Serializable::unserializeAll(const std::string &cpt_dir)
255 {
256 setCheckpointDir(cpt_dir);
257 string dir = Checkpoint::dir();
258 string cpt_file = dir + Checkpoint::baseFilename;
259 string section = "";
260
261 DPRINTFR(Config, "Loading checkpoint dir '%s'\n",
262 dir);
263 Checkpoint *cp = new Checkpoint(dir, section);
264 unserializeGlobals(cp);
265
266 SimObject::unserializeAll(cp);
267 }
268
269 void
270 Serializable::unserializeGlobals(Checkpoint *cp)
271 {
272 globals.unserialize(cp);
273 }
274
275 const char *Checkpoint::baseFilename = "m5.cpt";
276
277 static string checkpointDirBase;
278
279 void
280 setCheckpointDir(const std::string &name)
281 {
282 checkpointDirBase = name;
283 if (checkpointDirBase[checkpointDirBase.size() - 1] != '/')
284 checkpointDirBase += "/";
285 }
286
287 string
288 Checkpoint::dir()
289 {
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;
294 }
295
296 void
297 debug_serialize(const std::string &cpt_dir)
298 {
299 Serializable::serializeAll(cpt_dir);
300 }
301
302
303 ////////////////////////////////////////////////////////////////////////
304 //
305 // SerializableClass member definitions
306 //
307 ////////////////////////////////////////////////////////////////////////
308
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;
314
315 // SerializableClass constructor: add mapping to classMap
316 SerializableClass::SerializableClass(const string &className,
317 CreateFunc createFunc)
318 {
319 if (classMap == NULL)
320 classMap = new map<string,SerializableClass::CreateFunc>();
321
322 if ((*classMap)[className])
323 {
324 cerr << "Error: simulation object class " << className << " redefined"
325 << endl;
326 fatal("");
327 }
328
329 // add className --> createFunc to class map
330 (*classMap)[className] = createFunc;
331 }
332
333
334 //
335 //
336 Serializable *
337 SerializableClass::createObject(Checkpoint *cp,
338 const std::string &section)
339 {
340 string className;
341
342 if (!cp->find(section, "type", className)) {
343 fatal("Serializable::create: no 'type' entry in section '%s'.\n",
344 section);
345 }
346
347 CreateFunc createFunc = (*classMap)[className];
348
349 if (createFunc == NULL) {
350 fatal("Serializable::create: no create function for class '%s'.\n",
351 className);
352 }
353
354 Serializable *object = createFunc(cp, section);
355
356 assert(object != NULL);
357
358 return object;
359 }
360
361
362 Serializable *
363 Serializable::create(Checkpoint *cp, const std::string &section)
364 {
365 Serializable *object = SerializableClass::createObject(cp, section);
366 object->unserialize(cp, section);
367 return object;
368 }
369
370
371 Checkpoint::Checkpoint(const std::string &cpt_dir, const std::string &path)
372 : db(new IniFile), basePath(path), cptDir(cpt_dir)
373 {
374 string filename = cpt_dir + "/" + Checkpoint::baseFilename;
375 if (!db->load(filename)) {
376 fatal("Can't load checkpoint file '%s'\n", filename);
377 }
378 }
379
380
381 bool
382 Checkpoint::find(const std::string &section, const std::string &entry,
383 std::string &value)
384 {
385 return db->find(section, entry, value);
386 }
387
388
389 bool
390 Checkpoint::findObj(const std::string &section, const std::string &entry,
391 Serializable *&value)
392 {
393 string path;
394
395 if (!db->find(section, entry, path))
396 return false;
397
398 if ((value = objMap[path]) != NULL)
399 return true;
400
401 return false;
402 }
403
404
405 bool
406 Checkpoint::sectionExists(const std::string &section)
407 {
408 return db->sectionExists(section);
409 }
410
411 /** Hacked stat reset event */
412
413 class StatresetParamContext : public ParamContext
414 {
415 public:
416 StatresetParamContext(const string &section);
417 ~StatresetParamContext();
418 void startup();
419 };
420
421 StatresetParamContext statParams("statsreset");
422
423 Param<Tick> reset_cycle(&statParams, "reset_cycle",
424 "Cycle to reset stats on", 0);
425
426 StatresetParamContext::StatresetParamContext(const string &section)
427 : ParamContext(section)
428 { }
429
430 StatresetParamContext::~StatresetParamContext()
431 {
432 }
433
434 void
435 StatresetParamContext::startup()
436 {
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);
441 }
442 }