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