serialize.cc revision 9983
12SN/A/*
21762SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
39983Sstever@gmail.com * Copyright (c) 2013 Advanced Micro Devices, Inc.
49983Sstever@gmail.com * Copyright (c) 2013 Mark D. Hill and David A. Wood
52SN/A * All rights reserved.
62SN/A *
72SN/A * Redistribution and use in source and binary forms, with or without
82SN/A * modification, are permitted provided that the following conditions are
92SN/A * met: redistributions of source code must retain the above copyright
102SN/A * notice, this list of conditions and the following disclaimer;
112SN/A * redistributions in binary form must reproduce the above copyright
122SN/A * notice, this list of conditions and the following disclaimer in the
132SN/A * documentation and/or other materials provided with the distribution;
142SN/A * neither the name of the copyright holders nor the names of its
152SN/A * contributors may be used to endorse or promote products derived from
162SN/A * this software without specific prior written permission.
172SN/A *
182SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
192SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
202SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
212SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
222SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
232SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
242SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
252SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
262SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
272SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
282SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
292665Ssaidi@eecs.umich.edu *
302760Sbinkertn@umich.edu * Authors: Nathan Binkert
312760Sbinkertn@umich.edu *          Erik Hallnor
322665Ssaidi@eecs.umich.edu *          Steve Reinhardt
332SN/A */
342SN/A
358229Snate@binkert.org#include <sys/stat.h>
362SN/A#include <sys/time.h>
37363SN/A#include <sys/types.h>
382SN/A
398229Snate@binkert.org#include <cerrno>
402SN/A#include <fstream>
412SN/A#include <list>
422SN/A#include <string>
432SN/A#include <vector>
442SN/A
45363SN/A#include "base/inifile.hh"
4656SN/A#include "base/misc.hh"
471388SN/A#include "base/output.hh"
48217SN/A#include "base/str.hh"
49363SN/A#include "base/trace.hh"
5056SN/A#include "sim/eventq.hh"
5156SN/A#include "sim/serialize.hh"
5256SN/A#include "sim/sim_events.hh"
531638SN/A#include "sim/sim_exit.hh"
5456SN/A#include "sim/sim_object.hh"
552SN/A
562356SN/A// For stat reset hack
572356SN/A#include "sim/stat_control.hh"
582356SN/A
592SN/Ausing namespace std;
602SN/A
614000Ssaidi@eecs.umich.eduextern SimObject *resolveSimObject(const string &);
624000Ssaidi@eecs.umich.edu
634762Snate@binkert.org//
644762Snate@binkert.org// The base implementations use to_number for parsing and '<<' for
654762Snate@binkert.org// displaying, suitable for integer types.
664762Snate@binkert.org//
674762Snate@binkert.orgtemplate <class T>
684762Snate@binkert.orgbool
694762Snate@binkert.orgparseParam(const string &s, T &value)
704762Snate@binkert.org{
714762Snate@binkert.org    return to_number(s, value);
724762Snate@binkert.org}
734762Snate@binkert.org
744762Snate@binkert.orgtemplate <class T>
754762Snate@binkert.orgvoid
764762Snate@binkert.orgshowParam(ostream &os, const T &value)
774762Snate@binkert.org{
784762Snate@binkert.org    os << value;
794762Snate@binkert.org}
804762Snate@binkert.org
814762Snate@binkert.org//
824762Snate@binkert.org// Template specializations:
834762Snate@binkert.org// - char (8-bit integer)
844762Snate@binkert.org// - floating-point types
854762Snate@binkert.org// - bool
864762Snate@binkert.org// - string
874762Snate@binkert.org//
884762Snate@binkert.org
894762Snate@binkert.org// Treat 8-bit ints (chars) as ints on output, not as chars
904762Snate@binkert.orgtemplate <>
914762Snate@binkert.orgvoid
927494Ssteve.reinhardt@amd.comshowParam(ostream &os, const char &value)
937494Ssteve.reinhardt@amd.com{
947494Ssteve.reinhardt@amd.com    os << (int)value;
957494Ssteve.reinhardt@amd.com}
967494Ssteve.reinhardt@amd.com
977494Ssteve.reinhardt@amd.com
987494Ssteve.reinhardt@amd.comtemplate <>
997494Ssteve.reinhardt@amd.comvoid
1007490Ssteve.reinhardt@amd.comshowParam(ostream &os, const signed char &value)
1014762Snate@binkert.org{
1024762Snate@binkert.org    os << (int)value;
1034762Snate@binkert.org}
1044762Snate@binkert.org
1054762Snate@binkert.org
1064762Snate@binkert.orgtemplate <>
1074762Snate@binkert.orgvoid
1084762Snate@binkert.orgshowParam(ostream &os, const unsigned char &value)
1094762Snate@binkert.org{
1104762Snate@binkert.org    os << (unsigned int)value;
1114762Snate@binkert.org}
1124762Snate@binkert.org
1134762Snate@binkert.org
1144762Snate@binkert.org// Use sscanf() for FP types as to_number() only handles integers
1154762Snate@binkert.orgtemplate <>
1164762Snate@binkert.orgbool
1174762Snate@binkert.orgparseParam(const string &s, float &value)
1184762Snate@binkert.org{
1194762Snate@binkert.org    return (sscanf(s.c_str(), "%f", &value) == 1);
1204762Snate@binkert.org}
1214762Snate@binkert.org
1224762Snate@binkert.orgtemplate <>
1234762Snate@binkert.orgbool
1244762Snate@binkert.orgparseParam(const string &s, double &value)
1254762Snate@binkert.org{
1264762Snate@binkert.org    return (sscanf(s.c_str(), "%lf", &value) == 1);
1274762Snate@binkert.org}
1284762Snate@binkert.org
1294762Snate@binkert.orgtemplate <>
1304762Snate@binkert.orgbool
1314762Snate@binkert.orgparseParam(const string &s, bool &value)
1324762Snate@binkert.org{
1334762Snate@binkert.org    const string &ls = to_lower(s);
1344762Snate@binkert.org
1354762Snate@binkert.org    if (ls == "true") {
1364762Snate@binkert.org        value = true;
1374762Snate@binkert.org        return true;
1384762Snate@binkert.org    }
1394762Snate@binkert.org
1404762Snate@binkert.org    if (ls == "false") {
1414762Snate@binkert.org        value = false;
1424762Snate@binkert.org        return true;
1434762Snate@binkert.org    }
1444762Snate@binkert.org
1454762Snate@binkert.org    return false;
1464762Snate@binkert.org}
1474762Snate@binkert.org
1484762Snate@binkert.org// Display bools as strings
1494762Snate@binkert.orgtemplate <>
1504762Snate@binkert.orgvoid
1514762Snate@binkert.orgshowParam(ostream &os, const bool &value)
1524762Snate@binkert.org{
1534762Snate@binkert.org    os << (value ? "true" : "false");
1544762Snate@binkert.org}
1554762Snate@binkert.org
1564762Snate@binkert.org
1574762Snate@binkert.org// String requires no processing to speak of
1584762Snate@binkert.orgtemplate <>
1594762Snate@binkert.orgbool
1604762Snate@binkert.orgparseParam(const string &s, string &value)
1614762Snate@binkert.org{
1624762Snate@binkert.org    value = s;
1634762Snate@binkert.org    return true;
1644762Snate@binkert.org}
1654762Snate@binkert.org
1662287SN/Aint Serializable::ckptMaxCount = 0;
1672287SN/Aint Serializable::ckptCount = 0;
1682287SN/Aint Serializable::ckptPrevCount = -1;
1691637SN/A
1702SN/Avoid
171395SN/ASerializable::nameOut(ostream &os)
1722SN/A{
173217SN/A    os << "\n[" << name() << "]\n";
1742SN/A}
1752SN/A
1762SN/Avoid
177395SN/ASerializable::nameOut(ostream &os, const string &_name)
1782SN/A{
179217SN/A    os << "\n[" << _name << "]\n";
1802SN/A}
1812SN/A
182217SN/Atemplate <class T>
1832SN/Avoid
1846225Snate@binkert.orgparamOut(ostream &os, const string &name, const T &param)
1852SN/A{
186217SN/A    os << name << "=";
187217SN/A    showParam(os, param);
188217SN/A    os << "\n";
1892SN/A}
1902SN/A
1914841Ssaidi@eecs.umich.edutemplate <class T>
1924841Ssaidi@eecs.umich.eduvoid
1936225Snate@binkert.orgarrayParamOut(ostream &os, const string &name, const vector<T> &param)
1944841Ssaidi@eecs.umich.edu{
1956227Snate@binkert.org    typename vector<T>::size_type size = param.size();
1964841Ssaidi@eecs.umich.edu    os << name << "=";
1974841Ssaidi@eecs.umich.edu    if (size > 0)
1984841Ssaidi@eecs.umich.edu        showParam(os, param[0]);
1996227Snate@binkert.org    for (typename vector<T>::size_type i = 1; i < size; ++i) {
2004841Ssaidi@eecs.umich.edu        os << " ";
2014841Ssaidi@eecs.umich.edu        showParam(os, param[i]);
2024841Ssaidi@eecs.umich.edu    }
2034841Ssaidi@eecs.umich.edu    os << "\n";
2044841Ssaidi@eecs.umich.edu}
2054841Ssaidi@eecs.umich.edu
2067948SAli.Saidi@ARM.comtemplate <class T>
2077948SAli.Saidi@ARM.comvoid
2087948SAli.Saidi@ARM.comarrayParamOut(ostream &os, const string &name, const list<T> &param)
2097948SAli.Saidi@ARM.com{
2107948SAli.Saidi@ARM.com    typename list<T>::const_iterator it = param.begin();
2117948SAli.Saidi@ARM.com
2127948SAli.Saidi@ARM.com    os << name << "=";
2137948SAli.Saidi@ARM.com    if (param.size() > 0)
2147948SAli.Saidi@ARM.com        showParam(os, *it);
2157948SAli.Saidi@ARM.com    it++;
2167948SAli.Saidi@ARM.com    while (it != param.end()) {
2177948SAli.Saidi@ARM.com        os << " ";
2187948SAli.Saidi@ARM.com        showParam(os, *it);
2197948SAli.Saidi@ARM.com        it++;
2207948SAli.Saidi@ARM.com    }
2217948SAli.Saidi@ARM.com    os << "\n";
2227948SAli.Saidi@ARM.com}
223217SN/A
224217SN/Atemplate <class T>
225217SN/Avoid
2266225Snate@binkert.orgparamIn(Checkpoint *cp, const string &section, const string &name, T &param)
2272SN/A{
2286225Snate@binkert.org    string str;
229237SN/A    if (!cp->find(section, name, str) || !parseParam(str, param)) {
230217SN/A        fatal("Can't unserialize '%s:%s'\n", section, name);
231217SN/A    }
2322SN/A}
2332SN/A
2346820SLisa.Hsu@amd.comtemplate <class T>
2356820SLisa.Hsu@amd.combool
2366820SLisa.Hsu@amd.comoptParamIn(Checkpoint *cp, const string &section, const string &name, T &param)
2376820SLisa.Hsu@amd.com{
2386820SLisa.Hsu@amd.com    string str;
2396820SLisa.Hsu@amd.com    if (!cp->find(section, name, str) || !parseParam(str, param)) {
2406820SLisa.Hsu@amd.com        warn("optional parameter %s:%s not present\n", section, name);
2416820SLisa.Hsu@amd.com        return false;
2426820SLisa.Hsu@amd.com    } else {
2436820SLisa.Hsu@amd.com        return true;
2446820SLisa.Hsu@amd.com    }
2456820SLisa.Hsu@amd.com}
246217SN/A
247217SN/Atemplate <class T>
248217SN/Avoid
2496227Snate@binkert.orgarrayParamOut(ostream &os, const string &name, const T *param, unsigned size)
250217SN/A{
251217SN/A    os << name << "=";
252217SN/A    if (size > 0)
253217SN/A        showParam(os, param[0]);
2546227Snate@binkert.org    for (unsigned i = 1; i < size; ++i) {
255217SN/A        os << " ";
256217SN/A        showParam(os, param[i]);
257217SN/A    }
258217SN/A    os << "\n";
259217SN/A}
260217SN/A
261217SN/A
262217SN/Atemplate <class T>
263217SN/Avoid
2646225Snate@binkert.orgarrayParamIn(Checkpoint *cp, const string &section, const string &name,
2656227Snate@binkert.org             T *param, unsigned size)
266217SN/A{
2676225Snate@binkert.org    string str;
268237SN/A    if (!cp->find(section, name, str)) {
269217SN/A        fatal("Can't unserialize '%s:%s'\n", section, name);
270217SN/A    }
271217SN/A
272217SN/A    // code below stolen from VectorParam<T>::parse().
273217SN/A    // it would be nice to unify these somehow...
274217SN/A
275217SN/A    vector<string> tokens;
276217SN/A
277217SN/A    tokenize(tokens, str, ' ');
278217SN/A
279217SN/A    // Need this if we were doing a vector
280217SN/A    // value.resize(tokens.size());
281217SN/A
282217SN/A    if (tokens.size() != size) {
283217SN/A        fatal("Array size mismatch on %s:%s'\n", section, name);
284217SN/A    }
285217SN/A
2866227Snate@binkert.org    for (vector<string>::size_type i = 0; i < tokens.size(); i++) {
287217SN/A        // need to parse into local variable to handle vector<bool>,
288217SN/A        // for which operator[] returns a special reference class
289217SN/A        // that's not the same as 'bool&', (since it's a packed
290217SN/A        // vector)
2917576SAli.Saidi@ARM.com        T scalar_value = 0;
292217SN/A        if (!parseParam(tokens[i], scalar_value)) {
293217SN/A            string err("could not parse \"");
294217SN/A
295217SN/A            err += str;
296217SN/A            err += "\"";
297217SN/A
298217SN/A            fatal(err);
299217SN/A        }
300217SN/A
301217SN/A        // assign parsed value to vector
302217SN/A        param[i] = scalar_value;
303217SN/A    }
304217SN/A}
305217SN/A
3064841Ssaidi@eecs.umich.edutemplate <class T>
3074841Ssaidi@eecs.umich.eduvoid
3086225Snate@binkert.orgarrayParamIn(Checkpoint *cp, const string &section,
3096225Snate@binkert.org             const string &name, vector<T> &param)
3104841Ssaidi@eecs.umich.edu{
3116225Snate@binkert.org    string str;
3124841Ssaidi@eecs.umich.edu    if (!cp->find(section, name, str)) {
3134841Ssaidi@eecs.umich.edu        fatal("Can't unserialize '%s:%s'\n", section, name);
3144841Ssaidi@eecs.umich.edu    }
3154841Ssaidi@eecs.umich.edu
3164841Ssaidi@eecs.umich.edu    // code below stolen from VectorParam<T>::parse().
3174841Ssaidi@eecs.umich.edu    // it would be nice to unify these somehow...
3184841Ssaidi@eecs.umich.edu
3194841Ssaidi@eecs.umich.edu    vector<string> tokens;
3204841Ssaidi@eecs.umich.edu
3214841Ssaidi@eecs.umich.edu    tokenize(tokens, str, ' ');
3224841Ssaidi@eecs.umich.edu
3234841Ssaidi@eecs.umich.edu    // Need this if we were doing a vector
3244841Ssaidi@eecs.umich.edu    // value.resize(tokens.size());
3254841Ssaidi@eecs.umich.edu
3264841Ssaidi@eecs.umich.edu    param.resize(tokens.size());
3274841Ssaidi@eecs.umich.edu
3286227Snate@binkert.org    for (vector<string>::size_type i = 0; i < tokens.size(); i++) {
3294841Ssaidi@eecs.umich.edu        // need to parse into local variable to handle vector<bool>,
3304841Ssaidi@eecs.umich.edu        // for which operator[] returns a special reference class
3314841Ssaidi@eecs.umich.edu        // that's not the same as 'bool&', (since it's a packed
3324841Ssaidi@eecs.umich.edu        // vector)
3337576SAli.Saidi@ARM.com        T scalar_value = 0;
3344841Ssaidi@eecs.umich.edu        if (!parseParam(tokens[i], scalar_value)) {
3354841Ssaidi@eecs.umich.edu            string err("could not parse \"");
3364841Ssaidi@eecs.umich.edu
3374841Ssaidi@eecs.umich.edu            err += str;
3384841Ssaidi@eecs.umich.edu            err += "\"";
3394841Ssaidi@eecs.umich.edu
3404841Ssaidi@eecs.umich.edu            fatal(err);
3414841Ssaidi@eecs.umich.edu        }
3424841Ssaidi@eecs.umich.edu
3434841Ssaidi@eecs.umich.edu        // assign parsed value to vector
3444841Ssaidi@eecs.umich.edu        param[i] = scalar_value;
3454841Ssaidi@eecs.umich.edu    }
3464841Ssaidi@eecs.umich.edu}
3474841Ssaidi@eecs.umich.edu
3487948SAli.Saidi@ARM.comtemplate <class T>
3497948SAli.Saidi@ARM.comvoid
3507948SAli.Saidi@ARM.comarrayParamIn(Checkpoint *cp, const string &section,
3517948SAli.Saidi@ARM.com             const string &name, list<T> &param)
3527948SAli.Saidi@ARM.com{
3537948SAli.Saidi@ARM.com    string str;
3547948SAli.Saidi@ARM.com    if (!cp->find(section, name, str)) {
3557948SAli.Saidi@ARM.com        fatal("Can't unserialize '%s:%s'\n", section, name);
3567948SAli.Saidi@ARM.com    }
3577948SAli.Saidi@ARM.com    param.clear();
3587948SAli.Saidi@ARM.com
3597948SAli.Saidi@ARM.com    vector<string> tokens;
3607948SAli.Saidi@ARM.com    tokenize(tokens, str, ' ');
3617948SAli.Saidi@ARM.com
3627948SAli.Saidi@ARM.com    for (vector<string>::size_type i = 0; i < tokens.size(); i++) {
3637948SAli.Saidi@ARM.com        T scalar_value = 0;
3647948SAli.Saidi@ARM.com        if (!parseParam(tokens[i], scalar_value)) {
3657948SAli.Saidi@ARM.com            string err("could not parse \"");
3667948SAli.Saidi@ARM.com
3677948SAli.Saidi@ARM.com            err += str;
3687948SAli.Saidi@ARM.com            err += "\"";
3697948SAli.Saidi@ARM.com
3707948SAli.Saidi@ARM.com            fatal(err);
3717948SAli.Saidi@ARM.com        }
3727948SAli.Saidi@ARM.com
3737948SAli.Saidi@ARM.com        // assign parsed value to vector
3747948SAli.Saidi@ARM.com        param.push_back(scalar_value);
3757948SAli.Saidi@ARM.com    }
3767948SAli.Saidi@ARM.com}
3777948SAli.Saidi@ARM.com
3787948SAli.Saidi@ARM.com
379237SN/Avoid
3806225Snate@binkert.orgobjParamIn(Checkpoint *cp, const string &section,
3816225Snate@binkert.org           const string &name, SimObject * &param)
382237SN/A{
383237SN/A    if (!cp->findObj(section, name, param)) {
384237SN/A        fatal("Can't unserialize '%s:%s'\n", section, name);
385237SN/A    }
386237SN/A}
387237SN/A
388237SN/A
3895543Ssaidi@eecs.umich.edu#define INSTANTIATE_PARAM_TEMPLATES(type)                               \
3905543Ssaidi@eecs.umich.edutemplate void                                                           \
3916225Snate@binkert.orgparamOut(ostream &os, const string &name, type const &param);           \
3925543Ssaidi@eecs.umich.edutemplate void                                                           \
3936225Snate@binkert.orgparamIn(Checkpoint *cp, const string &section,                          \
3946225Snate@binkert.org        const string &name, type & param);                              \
3956820SLisa.Hsu@amd.comtemplate bool                                                           \
3966820SLisa.Hsu@amd.comoptParamIn(Checkpoint *cp, const string &section,                       \
3976820SLisa.Hsu@amd.com        const string &name, type & param);                              \
3985543Ssaidi@eecs.umich.edutemplate void                                                           \
3996225Snate@binkert.orgarrayParamOut(ostream &os, const string &name,                          \
4006227Snate@binkert.org              type const *param, unsigned size);                        \
4015543Ssaidi@eecs.umich.edutemplate void                                                           \
4026225Snate@binkert.orgarrayParamIn(Checkpoint *cp, const string &section,                     \
4036227Snate@binkert.org             const string &name, type *param, unsigned size);           \
4045543Ssaidi@eecs.umich.edutemplate void                                                           \
4056225Snate@binkert.orgarrayParamOut(ostream &os, const string &name,                          \
4066225Snate@binkert.org              const vector<type> &param);                               \
4075543Ssaidi@eecs.umich.edutemplate void                                                           \
4086225Snate@binkert.orgarrayParamIn(Checkpoint *cp, const string &section,                     \
4097948SAli.Saidi@ARM.com             const string &name, vector<type> &param);                  \
4107948SAli.Saidi@ARM.comtemplate void                                                           \
4117948SAli.Saidi@ARM.comarrayParamOut(ostream &os, const string &name,                          \
4127948SAli.Saidi@ARM.com              const list<type> &param);                                 \
4137948SAli.Saidi@ARM.comtemplate void                                                           \
4147948SAli.Saidi@ARM.comarrayParamIn(Checkpoint *cp, const string &section,                     \
4157948SAli.Saidi@ARM.com             const string &name, list<type> &param);
416217SN/A
4177494Ssteve.reinhardt@amd.comINSTANTIATE_PARAM_TEMPLATES(char)
4181642SN/AINSTANTIATE_PARAM_TEMPLATES(signed char)
4191642SN/AINSTANTIATE_PARAM_TEMPLATES(unsigned char)
4201642SN/AINSTANTIATE_PARAM_TEMPLATES(signed short)
4211642SN/AINSTANTIATE_PARAM_TEMPLATES(unsigned short)
4221642SN/AINSTANTIATE_PARAM_TEMPLATES(signed int)
4231642SN/AINSTANTIATE_PARAM_TEMPLATES(unsigned int)
4241642SN/AINSTANTIATE_PARAM_TEMPLATES(signed long)
4251642SN/AINSTANTIATE_PARAM_TEMPLATES(unsigned long)
4261642SN/AINSTANTIATE_PARAM_TEMPLATES(signed long long)
4271642SN/AINSTANTIATE_PARAM_TEMPLATES(unsigned long long)
428219SN/AINSTANTIATE_PARAM_TEMPLATES(bool)
4295992Snate@binkert.orgINSTANTIATE_PARAM_TEMPLATES(float)
4305992Snate@binkert.orgINSTANTIATE_PARAM_TEMPLATES(double)
431217SN/AINSTANTIATE_PARAM_TEMPLATES(string)
432217SN/A
433217SN/A
434395SN/A/////////////////////////////
435395SN/A
436395SN/A/// Container for serializing global variables (not associated with
437395SN/A/// any serialized object).
438395SN/Aclass Globals : public Serializable
4392SN/A{
440395SN/A  public:
441512SN/A    const string name() const;
442510SN/A    void serialize(ostream &os);
4438737Skoansin.tan@gmail.com    void unserialize(Checkpoint *cp, const std::string &section);
444395SN/A};
4452SN/A
446395SN/A/// The one and only instance of the Globals class.
447395SN/AGlobals globals;
4482SN/A
449512SN/Aconst string
450395SN/AGlobals::name() const
4512SN/A{
452395SN/A    return "Globals";
4532SN/A}
4542SN/A
4552SN/Avoid
456510SN/AGlobals::serialize(ostream &os)
4572SN/A{
458395SN/A    nameOut(os);
4597864Ssteve.reinhardt@amd.com    paramOut(os, "curTick", curTick());
460395SN/A
4619983Sstever@gmail.com    paramOut(os, "numMainEventQueues", numMainEventQueues);
4629983Sstever@gmail.com
4639983Sstever@gmail.com    for (uint32_t i = 0; i < numMainEventQueues; ++i) {
4649983Sstever@gmail.com        nameOut(os, "MainEventQueue");
4659983Sstever@gmail.com        mainEventQueue[i]->serialize(os);
4669983Sstever@gmail.com    }
4672SN/A}
4682SN/A
4692SN/Avoid
4708737Skoansin.tan@gmail.comGlobals::unserialize(Checkpoint *cp, const std::string &section)
4712SN/A{
4727823Ssteve.reinhardt@amd.com    Tick tick;
4737823Ssteve.reinhardt@amd.com    paramIn(cp, section, "curTick", tick);
4749983Sstever@gmail.com    paramIn(cp, section, "numMainEventQueues", numMainEventQueues);
4752SN/A
4769983Sstever@gmail.com    for (uint32_t i = 0; i < numMainEventQueues; ++i) {
4779983Sstever@gmail.com        mainEventQueue[i]->setCurTick(tick);
4789983Sstever@gmail.com        mainEventQueue[i]->unserialize(cp, "MainEventQueue");
4799983Sstever@gmail.com    }
4802SN/A}
4812SN/A
4825739Snate@binkert.orgSerializable::Serializable()
4835739Snate@binkert.org{
4845739Snate@binkert.org}
4855739Snate@binkert.org
4865739Snate@binkert.orgSerializable::~Serializable()
4875739Snate@binkert.org{
4885739Snate@binkert.org}
4895739Snate@binkert.org
4905739Snate@binkert.orgvoid
4916225Snate@binkert.orgSerializable::serialize(ostream &os)
4925739Snate@binkert.org{
4935739Snate@binkert.org}
4945739Snate@binkert.org
4955739Snate@binkert.orgvoid
4966225Snate@binkert.orgSerializable::unserialize(Checkpoint *cp, const string &section)
4975739Snate@binkert.org{
4985739Snate@binkert.org}
4995739Snate@binkert.org
5002SN/Avoid
5016225Snate@binkert.orgSerializable::serializeAll(const string &cpt_dir)
5022SN/A{
5037491Ssteve.reinhardt@amd.com    string dir = Checkpoint::setDir(cpt_dir);
504363SN/A    if (mkdir(dir.c_str(), 0775) == -1 && errno != EEXIST)
505449SN/A            fatal("couldn't mkdir %s\n", dir);
506363SN/A
507449SN/A    string cpt_file = dir + Checkpoint::baseFilename;
508395SN/A    ofstream outstream(cpt_file.c_str());
5092SN/A    time_t t = time(NULL);
5105581Ssaidi@eecs.umich.edu    if (!outstream.is_open())
5115581Ssaidi@eecs.umich.edu        fatal("Unable to open file %s for writing\n", cpt_file.c_str());
5126818SLisa.Hsu@amd.com    outstream << "## checkpoint generated: " << ctime(&t);
5132SN/A
514395SN/A    globals.serialize(outstream);
515395SN/A    SimObject::serializeAll(outstream);
516395SN/A}
5172SN/A
5182797Sktlim@umich.eduvoid
519395SN/ASerializable::unserializeGlobals(Checkpoint *cp)
520395SN/A{
5218737Skoansin.tan@gmail.com  globals.unserialize(cp, globals.name());
522395SN/A}
5232SN/A
5242SN/Avoid
5256225Snate@binkert.orgdebug_serialize(const string &cpt_dir)
5262SN/A{
5272868Sktlim@umich.edu    Serializable::serializeAll(cpt_dir);
5282SN/A}
5292SN/A
5302SN/A
5312SN/A////////////////////////////////////////////////////////////////////////
5322SN/A//
533395SN/A// SerializableClass member definitions
5342SN/A//
5352SN/A////////////////////////////////////////////////////////////////////////
5362SN/A
537395SN/A// Map of class names to SerializableBuilder creation functions.
5382SN/A// Need to make this a pointer so we can force initialization on the
539395SN/A// first reference; otherwise, some SerializableClass constructors
5402SN/A// may be invoked before the classMap constructor.
5416225Snate@binkert.orgmap<string, SerializableClass::CreateFunc> *SerializableClass::classMap = 0;
5422SN/A
543395SN/A// SerializableClass constructor: add mapping to classMap
544395SN/ASerializableClass::SerializableClass(const string &className,
5456225Snate@binkert.org                                     CreateFunc createFunc)
5462SN/A{
5472SN/A    if (classMap == NULL)
5486225Snate@binkert.org        classMap = new map<string, SerializableClass::CreateFunc>();
5492SN/A
5502SN/A    if ((*classMap)[className])
5516225Snate@binkert.org        fatal("Error: simulation object class %s redefined\n", className);
5522SN/A
5532SN/A    // add className --> createFunc to class map
5542SN/A    (*classMap)[className] = createFunc;
5552SN/A}
5562SN/A
5572SN/A//
5582SN/A//
559395SN/ASerializable *
5606225Snate@binkert.orgSerializableClass::createObject(Checkpoint *cp, const string &section)
5612SN/A{
562237SN/A    string className;
5632SN/A
564237SN/A    if (!cp->find(section, "type", className)) {
565395SN/A        fatal("Serializable::create: no 'type' entry in section '%s'.\n",
566237SN/A              section);
5672SN/A    }
5682SN/A
569237SN/A    CreateFunc createFunc = (*classMap)[className];
570237SN/A
571237SN/A    if (createFunc == NULL) {
572395SN/A        fatal("Serializable::create: no create function for class '%s'.\n",
573237SN/A              className);
5742SN/A    }
5752SN/A
576395SN/A    Serializable *object = createFunc(cp, section);
5772SN/A
5782SN/A    assert(object != NULL);
5792SN/A
5802SN/A    return object;
5812SN/A}
5822SN/A
583237SN/A
584395SN/ASerializable *
5856225Snate@binkert.orgSerializable::create(Checkpoint *cp, const string &section)
586237SN/A{
587395SN/A    Serializable *object = SerializableClass::createObject(cp, section);
588237SN/A    object->unserialize(cp, section);
589237SN/A    return object;
590237SN/A}
591237SN/A
592237SN/A
5937491Ssteve.reinhardt@amd.comconst char *Checkpoint::baseFilename = "m5.cpt";
5947491Ssteve.reinhardt@amd.com
5957491Ssteve.reinhardt@amd.comstring Checkpoint::currentDirectory;
5967491Ssteve.reinhardt@amd.com
5977491Ssteve.reinhardt@amd.comstring
5987491Ssteve.reinhardt@amd.comCheckpoint::setDir(const string &name)
5997491Ssteve.reinhardt@amd.com{
6007823Ssteve.reinhardt@amd.com    // use csprintf to insert curTick() into directory name if it
6017491Ssteve.reinhardt@amd.com    // appears to have a format placeholder in it.
6027491Ssteve.reinhardt@amd.com    currentDirectory = (name.find("%") != string::npos) ?
6037823Ssteve.reinhardt@amd.com        csprintf(name, curTick()) : name;
6047491Ssteve.reinhardt@amd.com    if (currentDirectory[currentDirectory.size() - 1] != '/')
6057491Ssteve.reinhardt@amd.com        currentDirectory += "/";
6067491Ssteve.reinhardt@amd.com    return currentDirectory;
6077491Ssteve.reinhardt@amd.com}
6087491Ssteve.reinhardt@amd.com
6097491Ssteve.reinhardt@amd.comstring
6107491Ssteve.reinhardt@amd.comCheckpoint::dir()
6117491Ssteve.reinhardt@amd.com{
6127491Ssteve.reinhardt@amd.com    return currentDirectory;
6137491Ssteve.reinhardt@amd.com}
6147491Ssteve.reinhardt@amd.com
6157491Ssteve.reinhardt@amd.com
6167491Ssteve.reinhardt@amd.comCheckpoint::Checkpoint(const string &cpt_dir)
6177532Ssteve.reinhardt@amd.com    : db(new IniFile), cptDir(setDir(cpt_dir))
618237SN/A{
6197532Ssteve.reinhardt@amd.com    string filename = cptDir + "/" + Checkpoint::baseFilename;
620237SN/A    if (!db->load(filename)) {
621237SN/A        fatal("Can't load checkpoint file '%s'\n", filename);
622237SN/A    }
623237SN/A}
624237SN/A
6259086Sandreas.hansson@arm.comCheckpoint::~Checkpoint()
6269086Sandreas.hansson@arm.com{
6279086Sandreas.hansson@arm.com    delete db;
6289086Sandreas.hansson@arm.com}
629237SN/A
630237SN/Abool
6316225Snate@binkert.orgCheckpoint::find(const string &section, const string &entry, string &value)
632237SN/A{
633237SN/A    return db->find(section, entry, value);
634237SN/A}
635237SN/A
636237SN/A
637237SN/Abool
6386225Snate@binkert.orgCheckpoint::findObj(const string &section, const string &entry,
6394000Ssaidi@eecs.umich.edu                    SimObject *&value)
640237SN/A{
641237SN/A    string path;
642237SN/A
643237SN/A    if (!db->find(section, entry, path))
644237SN/A        return false;
645237SN/A
6464000Ssaidi@eecs.umich.edu    value = resolveSimObject(path);
6474000Ssaidi@eecs.umich.edu    return true;
648237SN/A}
649304SN/A
650304SN/A
651304SN/Abool
6526225Snate@binkert.orgCheckpoint::sectionExists(const string &section)
653304SN/A{
654304SN/A    return db->sectionExists(section);
655304SN/A}
656