serialize.cc revision 2738:5d7a31c7fa29
15081Sgblack@eecs.umich.edu/*
25081Sgblack@eecs.umich.edu * Copyright (c) 2002-2005 The Regents of The University of Michigan
35081Sgblack@eecs.umich.edu * All rights reserved.
47087Snate@binkert.org *
57087Snate@binkert.org * Redistribution and use in source and binary forms, with or without
67087Snate@binkert.org * modification, are permitted provided that the following conditions are
77087Snate@binkert.org * met: redistributions of source code must retain the above copyright
87087Snate@binkert.org * notice, this list of conditions and the following disclaimer;
97087Snate@binkert.org * redistributions in binary form must reproduce the above copyright
107087Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
117087Snate@binkert.org * documentation and/or other materials provided with the distribution;
125081Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
137087Snate@binkert.org * contributors may be used to endorse or promote products derived from
147087Snate@binkert.org * this software without specific prior written permission.
157087Snate@binkert.org *
167087Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
177087Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
187087Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
197087Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
207087Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
215081Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
227087Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
235081Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245081Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
255081Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
265081Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
275081Sgblack@eecs.umich.edu *
285081Sgblack@eecs.umich.edu * Authors: Erik Hallnor
295081Sgblack@eecs.umich.edu *          Steve Reinhardt
305081Sgblack@eecs.umich.edu */
315081Sgblack@eecs.umich.edu
325081Sgblack@eecs.umich.edu#include <sys/time.h>
335081Sgblack@eecs.umich.edu#include <sys/types.h>
345081Sgblack@eecs.umich.edu#include <sys/stat.h>
355081Sgblack@eecs.umich.edu#include <errno.h>
365081Sgblack@eecs.umich.edu
375081Sgblack@eecs.umich.edu#include <fstream>
385081Sgblack@eecs.umich.edu#include <list>
395081Sgblack@eecs.umich.edu#include <string>
405081Sgblack@eecs.umich.edu#include <vector>
415081Sgblack@eecs.umich.edu
425081Sgblack@eecs.umich.edu#include "base/inifile.hh"
435081Sgblack@eecs.umich.edu#include "base/misc.hh"
445081Sgblack@eecs.umich.edu#include "base/output.hh"
455081Sgblack@eecs.umich.edu#include "base/str.hh"
465081Sgblack@eecs.umich.edu#include "base/trace.hh"
475081Sgblack@eecs.umich.edu#include "sim/eventq.hh"
485081Sgblack@eecs.umich.edu#include "sim/param.hh"
495081Sgblack@eecs.umich.edu#include "sim/serialize.hh"
505081Sgblack@eecs.umich.edu#include "sim/sim_events.hh"
515081Sgblack@eecs.umich.edu#include "sim/sim_exit.hh"
525081Sgblack@eecs.umich.edu#include "sim/sim_object.hh"
535081Sgblack@eecs.umich.edu
545081Sgblack@eecs.umich.eduusing namespace std;
55
56int Serializable::ckptMaxCount = 0;
57int Serializable::ckptCount = 0;
58int Serializable::ckptPrevCount = -1;
59
60void
61Serializable::nameOut(ostream &os)
62{
63    os << "\n[" << name() << "]\n";
64}
65
66void
67Serializable::nameOut(ostream &os, const string &_name)
68{
69    os << "\n[" << _name << "]\n";
70}
71
72template <class T>
73void
74paramOut(ostream &os, const std::string &name, const T &param)
75{
76    os << name << "=";
77    showParam(os, param);
78    os << "\n";
79}
80
81
82template <class T>
83void
84paramIn(Checkpoint *cp, const std::string &section,
85        const std::string &name, T &param)
86{
87    std::string str;
88    if (!cp->find(section, name, str) || !parseParam(str, param)) {
89        fatal("Can't unserialize '%s:%s'\n", section, name);
90    }
91}
92
93
94template <class T>
95void
96arrayParamOut(ostream &os, const std::string &name,
97              const T *param, int size)
98{
99    os << name << "=";
100    if (size > 0)
101        showParam(os, param[0]);
102    for (int i = 1; i < size; ++i) {
103        os << " ";
104        showParam(os, param[i]);
105    }
106    os << "\n";
107}
108
109
110template <class T>
111void
112arrayParamIn(Checkpoint *cp, const std::string &section,
113             const std::string &name, T *param, int size)
114{
115    std::string str;
116    if (!cp->find(section, name, str)) {
117        fatal("Can't unserialize '%s:%s'\n", section, name);
118    }
119
120    // code below stolen from VectorParam<T>::parse().
121    // it would be nice to unify these somehow...
122
123    vector<string> tokens;
124
125    tokenize(tokens, str, ' ');
126
127    // Need this if we were doing a vector
128    // value.resize(tokens.size());
129
130    if (tokens.size() != size) {
131        fatal("Array size mismatch on %s:%s'\n", section, name);
132    }
133
134    for (int i = 0; i < tokens.size(); i++) {
135        // need to parse into local variable to handle vector<bool>,
136        // for which operator[] returns a special reference class
137        // that's not the same as 'bool&', (since it's a packed
138        // vector)
139        T scalar_value;
140        if (!parseParam(tokens[i], scalar_value)) {
141            string err("could not parse \"");
142
143            err += str;
144            err += "\"";
145
146            fatal(err);
147        }
148
149        // assign parsed value to vector
150        param[i] = scalar_value;
151    }
152}
153
154
155void
156objParamIn(Checkpoint *cp, const std::string &section,
157           const std::string &name, Serializable * &param)
158{
159    if (!cp->findObj(section, name, param)) {
160        fatal("Can't unserialize '%s:%s'\n", section, name);
161    }
162}
163
164
165#define INSTANTIATE_PARAM_TEMPLATES(type)				\
166template void								\
167paramOut(ostream &os, const std::string &name, type const &param);	\
168template void								\
169paramIn(Checkpoint *cp, const std::string &section,			\
170        const std::string &name, type & param);				\
171template void								\
172arrayParamOut(ostream &os, const std::string &name,			\
173              type const *param, int size);				\
174template void								\
175arrayParamIn(Checkpoint *cp, const std::string &section,		\
176             const std::string &name, type *param, int size);
177
178INSTANTIATE_PARAM_TEMPLATES(signed char)
179INSTANTIATE_PARAM_TEMPLATES(unsigned char)
180INSTANTIATE_PARAM_TEMPLATES(signed short)
181INSTANTIATE_PARAM_TEMPLATES(unsigned short)
182INSTANTIATE_PARAM_TEMPLATES(signed int)
183INSTANTIATE_PARAM_TEMPLATES(unsigned int)
184INSTANTIATE_PARAM_TEMPLATES(signed long)
185INSTANTIATE_PARAM_TEMPLATES(unsigned long)
186INSTANTIATE_PARAM_TEMPLATES(signed long long)
187INSTANTIATE_PARAM_TEMPLATES(unsigned long long)
188INSTANTIATE_PARAM_TEMPLATES(bool)
189INSTANTIATE_PARAM_TEMPLATES(string)
190
191
192/////////////////////////////
193
194/// Container for serializing global variables (not associated with
195/// any serialized object).
196class Globals : public Serializable
197{
198  public:
199    const string name() const;
200    void serialize(ostream &os);
201    void unserialize(Checkpoint *cp);
202};
203
204/// The one and only instance of the Globals class.
205Globals globals;
206
207const string
208Globals::name() const
209{
210    return "Globals";
211}
212
213void
214Globals::serialize(ostream &os)
215{
216    nameOut(os);
217    SERIALIZE_SCALAR(curTick);
218
219    nameOut(os, "MainEventQueue");
220    mainEventQueue.serialize(os);
221}
222
223void
224Globals::unserialize(Checkpoint *cp)
225{
226    const string &section = name();
227    UNSERIALIZE_SCALAR(curTick);
228
229    mainEventQueue.unserialize(cp, "MainEventQueue");
230}
231
232void
233Serializable::serializeAll()
234{
235    string dir = Checkpoint::dir();
236    if (mkdir(dir.c_str(), 0775) == -1 && errno != EEXIST)
237            fatal("couldn't mkdir %s\n", dir);
238
239    string cpt_file = dir + Checkpoint::baseFilename;
240    ofstream outstream(cpt_file.c_str());
241    time_t t = time(NULL);
242    outstream << "// checkpoint generated: " << ctime(&t);
243
244    globals.serialize(outstream);
245    SimObject::serializeAll(outstream);
246
247    assert(Serializable::ckptPrevCount + 1 == Serializable::ckptCount);
248    Serializable::ckptPrevCount++;
249    if (ckptMaxCount && ++ckptCount >= ckptMaxCount)
250        exitSimLoop(curTick + 1, "Maximum number of checkpoints dropped");
251
252}
253
254
255void
256Serializable::unserializeGlobals(Checkpoint *cp)
257{
258    globals.unserialize(cp);
259}
260
261
262class SerializeEvent : public Event
263{
264  protected:
265    Tick repeat;
266
267  public:
268    SerializeEvent(Tick _when, Tick _repeat);
269    virtual void process();
270    virtual void serialize(std::ostream &os)
271    {
272        panic("Cannot serialize the SerializeEvent");
273    }
274
275};
276
277SerializeEvent::SerializeEvent(Tick _when, Tick _repeat)
278    : Event(&mainEventQueue, Serialize_Pri), repeat(_repeat)
279{
280    setFlags(AutoDelete);
281    schedule(_when);
282}
283
284void
285SerializeEvent::process()
286{
287    Serializable::serializeAll();
288    if (repeat)
289        schedule(curTick + repeat);
290}
291
292const char *Checkpoint::baseFilename = "m5.cpt";
293
294static string checkpointDirBase;
295
296string
297Checkpoint::dir()
298{
299    // use csprintf to insert curTick into directory name if it
300    // appears to have a format placeholder in it.
301    return (checkpointDirBase.find("%") != string::npos) ?
302        csprintf(checkpointDirBase, curTick) : checkpointDirBase;
303}
304
305void
306Checkpoint::setup(Tick when, Tick period)
307{
308    new SerializeEvent(when, period);
309}
310
311class SerializeParamContext : public ParamContext
312{
313  private:
314    SerializeEvent *event;
315
316  public:
317    SerializeParamContext(const string &section);
318    ~SerializeParamContext();
319    void checkParams();
320};
321
322SerializeParamContext serialParams("serialize");
323
324Param<string> serialize_dir(&serialParams, "dir",
325                            "dir to stick checkpoint in "
326                            "(sprintf format with cycle #)");
327
328Param<Counter> serialize_cycle(&serialParams,
329                                "cycle",
330                                "cycle to serialize",
331                                0);
332
333Param<Counter> serialize_period(&serialParams,
334                                "period",
335                                "period to repeat serializations",
336                                0);
337
338Param<int> serialize_count(&serialParams, "count",
339                           "maximum number of checkpoints to drop");
340
341SerializeParamContext::SerializeParamContext(const string &section)
342    : ParamContext(section), event(NULL)
343{ }
344
345SerializeParamContext::~SerializeParamContext()
346{
347}
348
349void
350SerializeParamContext::checkParams()
351{
352    checkpointDirBase = simout.resolve(serialize_dir);
353
354    // guarantee that directory ends with a '/'
355    if (checkpointDirBase[checkpointDirBase.size() - 1] != '/')
356        checkpointDirBase += "/";
357
358    if (serialize_cycle > 0)
359        Checkpoint::setup(serialize_cycle, serialize_period);
360
361    Serializable::ckptMaxCount = serialize_count;
362}
363
364void
365debug_serialize()
366{
367    Serializable::serializeAll();
368}
369
370void
371debug_serialize(Tick when)
372{
373    new SerializeEvent(when, 0);
374}
375
376////////////////////////////////////////////////////////////////////////
377//
378// SerializableClass member definitions
379//
380////////////////////////////////////////////////////////////////////////
381
382// Map of class names to SerializableBuilder creation functions.
383// Need to make this a pointer so we can force initialization on the
384// first reference; otherwise, some SerializableClass constructors
385// may be invoked before the classMap constructor.
386map<string,SerializableClass::CreateFunc> *SerializableClass::classMap = 0;
387
388// SerializableClass constructor: add mapping to classMap
389SerializableClass::SerializableClass(const string &className,
390                                       CreateFunc createFunc)
391{
392    if (classMap == NULL)
393        classMap = new map<string,SerializableClass::CreateFunc>();
394
395    if ((*classMap)[className])
396    {
397        cerr << "Error: simulation object class " << className << " redefined"
398             << endl;
399        fatal("");
400    }
401
402    // add className --> createFunc to class map
403    (*classMap)[className] = createFunc;
404}
405
406
407//
408//
409Serializable *
410SerializableClass::createObject(Checkpoint *cp,
411                                 const std::string &section)
412{
413    string className;
414
415    if (!cp->find(section, "type", className)) {
416        fatal("Serializable::create: no 'type' entry in section '%s'.\n",
417              section);
418    }
419
420    CreateFunc createFunc = (*classMap)[className];
421
422    if (createFunc == NULL) {
423        fatal("Serializable::create: no create function for class '%s'.\n",
424              className);
425    }
426
427    Serializable *object = createFunc(cp, section);
428
429    assert(object != NULL);
430
431    return object;
432}
433
434
435Serializable *
436Serializable::create(Checkpoint *cp, const std::string &section)
437{
438    Serializable *object = SerializableClass::createObject(cp, section);
439    object->unserialize(cp, section);
440    return object;
441}
442
443
444Checkpoint::Checkpoint(const std::string &cpt_dir, const std::string &path)
445    : db(new IniFile), basePath(path), cptDir(cpt_dir)
446{
447    string filename = cpt_dir + "/" + Checkpoint::baseFilename;
448    if (!db->load(filename)) {
449        fatal("Can't load checkpoint file '%s'\n", filename);
450    }
451}
452
453
454bool
455Checkpoint::find(const std::string &section, const std::string &entry,
456                 std::string &value)
457{
458    return db->find(section, entry, value);
459}
460
461
462bool
463Checkpoint::findObj(const std::string &section, const std::string &entry,
464                    Serializable *&value)
465{
466    string path;
467
468    if (!db->find(section, entry, path))
469        return false;
470
471    if ((value = objMap[path]) != NULL)
472        return true;
473
474    return false;
475}
476
477
478bool
479Checkpoint::sectionExists(const std::string &section)
480{
481    return db->sectionExists(section);
482}
483