serialize.cc revision 449
1/*
2 * Copyright (c) 2003 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
29#include <sys/time.h>
30#include <sys/types.h>
31#include <sys/stat.h>
32
33#include <fstream>
34#include <list>
35#include <string>
36#include <vector>
37
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"
48
49using namespace std;
50
51void
52Serializable::nameOut(ostream &os)
53{
54    os << "\n[" << name() << "]\n";
55}
56
57void
58Serializable::nameOut(ostream &os, const string &_name)
59{
60    os << "\n[" << _name << "]\n";
61}
62
63template <class T>
64void
65paramOut(ostream &os, const std::string &name, const T& param)
66{
67    os << name << "=";
68    showParam(os, param);
69    os << "\n";
70}
71
72
73template <class T>
74void
75paramIn(Checkpoint *cp, const std::string &section,
76        const std::string &name, T& param)
77{
78    std::string str;
79    if (!cp->find(section, name, str) || !parseParam(str, param)) {
80        fatal("Can't unserialize '%s:%s'\n", section, name);
81    }
82}
83
84
85template <class T>
86void
87arrayParamOut(ostream &os, const std::string &name,
88              const T *param, int size)
89{
90    os << name << "=";
91    if (size > 0)
92        showParam(os, param[0]);
93    for (int i = 1; i < size; ++i) {
94        os << " ";
95        showParam(os, param[i]);
96    }
97    os << "\n";
98}
99
100
101template <class T>
102void
103arrayParamIn(Checkpoint *cp, const std::string &section,
104             const std::string &name, T *param, int size)
105{
106    std::string str;
107    if (!cp->find(section, name, str)) {
108        fatal("Can't unserialize '%s:%s'\n", section, name);
109    }
110
111    // code below stolen from VectorParam<T>::parse().
112    // it would be nice to unify these somehow...
113
114    vector<string> tokens;
115
116    tokenize(tokens, str, ' ');
117
118    // Need this if we were doing a vector
119    // value.resize(tokens.size());
120
121    if (tokens.size() != size) {
122        fatal("Array size mismatch on %s:%s'\n", section, name);
123    }
124
125    for (int i = 0; i < tokens.size(); i++) {
126        // need to parse into local variable to handle vector<bool>,
127        // for which operator[] returns a special reference class
128        // that's not the same as 'bool&', (since it's a packed
129        // vector)
130        T scalar_value;
131        if (!parseParam(tokens[i], scalar_value)) {
132            string err("could not parse \"");
133
134            err += str;
135            err += "\"";
136
137            fatal(err);
138        }
139
140        // assign parsed value to vector
141        param[i] = scalar_value;
142    }
143}
144
145
146void
147objParamIn(Checkpoint *cp, const std::string &section,
148           const std::string &name, Serializable * &param)
149{
150    if (!cp->findObj(section, name, param)) {
151        fatal("Can't unserialize '%s:%s'\n", section, name);
152    }
153}
154
155
156#define INSTANTIATE_PARAM_TEMPLATES(type)				\
157template void								\
158paramOut(ostream &os, const std::string &name, type const &param);	\
159template void								\
160paramIn(Checkpoint *cp, const std::string &section,			\
161        const std::string &name, type & param);				\
162template void								\
163arrayParamOut(ostream &os, const std::string &name,			\
164              type const *param, int size);				\
165template void								\
166arrayParamIn(Checkpoint *cp, const std::string &section,		\
167             const std::string &name, type *param, int size);
168
169
170INSTANTIATE_PARAM_TEMPLATES(int8_t)
171INSTANTIATE_PARAM_TEMPLATES(uint8_t)
172INSTANTIATE_PARAM_TEMPLATES(int16_t)
173INSTANTIATE_PARAM_TEMPLATES(uint16_t)
174INSTANTIATE_PARAM_TEMPLATES(int32_t)
175INSTANTIATE_PARAM_TEMPLATES(uint32_t)
176INSTANTIATE_PARAM_TEMPLATES(int64_t)
177INSTANTIATE_PARAM_TEMPLATES(uint64_t)
178INSTANTIATE_PARAM_TEMPLATES(bool)
179INSTANTIATE_PARAM_TEMPLATES(string)
180
181
182/////////////////////////////
183
184/// Container for serializing global variables (not associated with
185/// any serialized object).
186class Globals : public Serializable
187{
188  public:
189    string name() const;
190    void serialize(ostream& os);
191    void unserialize(Checkpoint *cp);
192};
193
194/// The one and only instance of the Globals class.
195Globals globals;
196
197string
198Globals::name() const
199{
200    return "Globals";
201}
202
203void
204Globals::serialize(ostream& os)
205{
206    nameOut(os);
207    SERIALIZE_SCALAR(curTick);
208
209    nameOut(os, "MainEventQueue");
210    mainEventQueue.serialize(os);
211}
212
213void
214Globals::unserialize(Checkpoint *cp)
215{
216    const string &section = name();
217    UNSERIALIZE_SCALAR(curTick);
218
219    mainEventQueue.unserialize(cp, "MainEventQueue");
220}
221
222void
223Serializable::serializeAll()
224{
225    string dir = Checkpoint::dir();
226    if (mkdir(dir.c_str(), 0775) == -1 && errno != EEXIST)
227            fatal("couldn't mkdir %s\n", dir);
228
229    string cpt_file = dir + Checkpoint::baseFilename;
230    ofstream outstream(cpt_file.c_str());
231    time_t t = time(NULL);
232    outstream << "// checkpoint generated: " << ctime(&t);
233
234    globals.serialize(outstream);
235    SimObject::serializeAll(outstream);
236}
237
238
239void
240Serializable::unserializeGlobals(Checkpoint *cp)
241{
242    globals.unserialize(cp);
243}
244
245
246class SerializeEvent : public Event
247{
248  protected:
249    Tick repeat;
250
251  public:
252    SerializeEvent(Tick _when, Tick _repeat);
253    virtual void process();
254    virtual void serialize(std::ostream &os)
255    {
256        panic("Cannot serialize the SerializeEvent");
257    }
258
259};
260
261SerializeEvent::SerializeEvent(Tick _when, Tick _repeat)
262    : Event(&mainEventQueue, Serialize_Pri), repeat(_repeat)
263{
264    setFlags(AutoDelete);
265    schedule(_when);
266}
267
268void
269SerializeEvent::process()
270{
271    Serializable::serializeAll();
272    if (repeat)
273        schedule(curTick + repeat);
274}
275
276const char *Checkpoint::baseFilename = "m5.cpt";
277
278static string checkpointDirBase;
279
280string
281Checkpoint::dir()
282{
283    // use csprintf to insert curTick into directory name if it
284    // appears to have a format placeholder in it.
285    return (checkpointDirBase.find("%") != string::npos) ?
286        csprintf(checkpointDirBase, curTick) : checkpointDirBase;
287}
288
289void
290Checkpoint::setup(Tick when, Tick period)
291{
292    new SerializeEvent(when, period);
293}
294
295class SerializeParamContext : public ParamContext
296{
297  private:
298    SerializeEvent *event;
299
300  public:
301    SerializeParamContext(const string &section);
302    ~SerializeParamContext();
303    void checkParams();
304};
305
306SerializeParamContext serialParams("serialize");
307
308Param<string> serialize_dir(&serialParams,
309                            "dir",
310                            "dir to stick checkpoint in "
311                            "(sprintf format with cycle #)", "m5.%012d");
312
313Param<Counter> serialize_cycle(&serialParams,
314                                "cycle",
315                                "cycle to serialize",
316                                0);
317
318Param<Counter> serialize_period(&serialParams,
319                                "period",
320                                "period to repeat serializations",
321                                0);
322
323
324
325SerializeParamContext::SerializeParamContext(const string &section)
326    : ParamContext(section), event(NULL)
327{ }
328
329SerializeParamContext::~SerializeParamContext()
330{
331}
332
333void
334SerializeParamContext::checkParams()
335{
336    checkpointDirBase = serialize_dir;
337    // guarantee that directory ends with a '/'
338    if (checkpointDirBase[checkpointDirBase.size() - 1] != '/') {
339        checkpointDirBase += "/";
340    }
341
342    if (serialize_cycle > 0)
343        Checkpoint::setup(serialize_cycle, serialize_period);
344}
345
346void
347debug_serialize()
348{
349    Serializable::serializeAll();
350}
351
352void
353debug_serialize(Tick when)
354{
355    new SerializeEvent(when, 0);
356}
357
358////////////////////////////////////////////////////////////////////////
359//
360// SerializableClass member definitions
361//
362////////////////////////////////////////////////////////////////////////
363
364// Map of class names to SerializableBuilder creation functions.
365// Need to make this a pointer so we can force initialization on the
366// first reference; otherwise, some SerializableClass constructors
367// may be invoked before the classMap constructor.
368map<string,SerializableClass::CreateFunc> *SerializableClass::classMap = 0;
369
370// SerializableClass constructor: add mapping to classMap
371SerializableClass::SerializableClass(const string &className,
372                                       CreateFunc createFunc)
373{
374    if (classMap == NULL)
375        classMap = new map<string,SerializableClass::CreateFunc>();
376
377    if ((*classMap)[className])
378    {
379        cerr << "Error: simulation object class " << className << " redefined"
380             << endl;
381        fatal("");
382    }
383
384    // add className --> createFunc to class map
385    (*classMap)[className] = createFunc;
386}
387
388
389//
390//
391Serializable *
392SerializableClass::createObject(Checkpoint *cp,
393                                 const std::string &section)
394{
395    string className;
396
397    if (!cp->find(section, "type", className)) {
398        fatal("Serializable::create: no 'type' entry in section '%s'.\n",
399              section);
400    }
401
402    CreateFunc createFunc = (*classMap)[className];
403
404    if (createFunc == NULL) {
405        fatal("Serializable::create: no create function for class '%s'.\n",
406              className);
407    }
408
409    Serializable *object = createFunc(cp, section);
410
411    assert(object != NULL);
412
413    return object;
414}
415
416
417Serializable *
418Serializable::create(Checkpoint *cp, const std::string &section)
419{
420    Serializable *object = SerializableClass::createObject(cp, section);
421    object->unserialize(cp, section);
422    return object;
423}
424
425
426Checkpoint::Checkpoint(const std::string &cpt_dir, const std::string &path,
427                       const ConfigNode *_configNode)
428    : db(new IniFile), basePath(path), configNode(_configNode)
429{
430    string filename = cpt_dir + "/" + Checkpoint::baseFilename;
431    if (!db->load(filename)) {
432        fatal("Can't load checkpoint file '%s'\n", filename);
433    }
434}
435
436
437bool
438Checkpoint::find(const std::string &section, const std::string &entry,
439                 std::string &value)
440{
441    return db->find(section, entry, value);
442}
443
444
445bool
446Checkpoint::findObj(const std::string &section, const std::string &entry,
447                    Serializable *&value)
448{
449    string path;
450
451    if (!db->find(section, entry, path))
452        return false;
453
454    if ((value = configNode->resolveSimObject(path)) != NULL)
455        return true;
456
457    if ((value = objMap[path]) != NULL)
458        return true;
459
460    return false;
461}
462
463
464bool
465Checkpoint::sectionExists(const std::string &section)
466{
467    return db->sectionExists(section);
468}
469