serialize.cc revision 10453
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 614762Snate@binkert.org// 624762Snate@binkert.org// The base implementations use to_number for parsing and '<<' for 634762Snate@binkert.org// displaying, suitable for integer types. 644762Snate@binkert.org// 654762Snate@binkert.orgtemplate <class T> 664762Snate@binkert.orgbool 674762Snate@binkert.orgparseParam(const string &s, T &value) 684762Snate@binkert.org{ 694762Snate@binkert.org return to_number(s, value); 704762Snate@binkert.org} 714762Snate@binkert.org 724762Snate@binkert.orgtemplate <class T> 734762Snate@binkert.orgvoid 744762Snate@binkert.orgshowParam(ostream &os, const T &value) 754762Snate@binkert.org{ 764762Snate@binkert.org os << value; 774762Snate@binkert.org} 784762Snate@binkert.org 794762Snate@binkert.org// 804762Snate@binkert.org// Template specializations: 814762Snate@binkert.org// - char (8-bit integer) 824762Snate@binkert.org// - floating-point types 834762Snate@binkert.org// - bool 844762Snate@binkert.org// - string 854762Snate@binkert.org// 864762Snate@binkert.org 874762Snate@binkert.org// Treat 8-bit ints (chars) as ints on output, not as chars 884762Snate@binkert.orgtemplate <> 894762Snate@binkert.orgvoid 907494Ssteve.reinhardt@amd.comshowParam(ostream &os, const char &value) 917494Ssteve.reinhardt@amd.com{ 927494Ssteve.reinhardt@amd.com os << (int)value; 937494Ssteve.reinhardt@amd.com} 947494Ssteve.reinhardt@amd.com 957494Ssteve.reinhardt@amd.com 967494Ssteve.reinhardt@amd.comtemplate <> 977494Ssteve.reinhardt@amd.comvoid 987490Ssteve.reinhardt@amd.comshowParam(ostream &os, const signed char &value) 994762Snate@binkert.org{ 1004762Snate@binkert.org os << (int)value; 1014762Snate@binkert.org} 1024762Snate@binkert.org 1034762Snate@binkert.org 1044762Snate@binkert.orgtemplate <> 1054762Snate@binkert.orgvoid 1064762Snate@binkert.orgshowParam(ostream &os, const unsigned char &value) 1074762Snate@binkert.org{ 1084762Snate@binkert.org os << (unsigned int)value; 1094762Snate@binkert.org} 1104762Snate@binkert.org 1114762Snate@binkert.org 1124762Snate@binkert.orgtemplate <> 1134762Snate@binkert.orgbool 1144762Snate@binkert.orgparseParam(const string &s, float &value) 1154762Snate@binkert.org{ 11610386Sandreas.hansson@arm.com return to_number(s, value); 1174762Snate@binkert.org} 1184762Snate@binkert.org 1194762Snate@binkert.orgtemplate <> 1204762Snate@binkert.orgbool 1214762Snate@binkert.orgparseParam(const string &s, double &value) 1224762Snate@binkert.org{ 12310386Sandreas.hansson@arm.com return to_number(s, value); 1244762Snate@binkert.org} 1254762Snate@binkert.org 1264762Snate@binkert.orgtemplate <> 1274762Snate@binkert.orgbool 1284762Snate@binkert.orgparseParam(const string &s, bool &value) 1294762Snate@binkert.org{ 13010386Sandreas.hansson@arm.com return to_bool(s, value); 1314762Snate@binkert.org} 1324762Snate@binkert.org 1334762Snate@binkert.org// Display bools as strings 1344762Snate@binkert.orgtemplate <> 1354762Snate@binkert.orgvoid 1364762Snate@binkert.orgshowParam(ostream &os, const bool &value) 1374762Snate@binkert.org{ 1384762Snate@binkert.org os << (value ? "true" : "false"); 1394762Snate@binkert.org} 1404762Snate@binkert.org 1414762Snate@binkert.org 1424762Snate@binkert.org// String requires no processing to speak of 1434762Snate@binkert.orgtemplate <> 1444762Snate@binkert.orgbool 1454762Snate@binkert.orgparseParam(const string &s, string &value) 1464762Snate@binkert.org{ 1474762Snate@binkert.org value = s; 1484762Snate@binkert.org return true; 1494762Snate@binkert.org} 1504762Snate@binkert.org 1512287SN/Aint Serializable::ckptMaxCount = 0; 1522287SN/Aint Serializable::ckptCount = 0; 1532287SN/Aint Serializable::ckptPrevCount = -1; 1541637SN/A 1552SN/Avoid 156395SN/ASerializable::nameOut(ostream &os) 1572SN/A{ 158217SN/A os << "\n[" << name() << "]\n"; 1592SN/A} 1602SN/A 1612SN/Avoid 162395SN/ASerializable::nameOut(ostream &os, const string &_name) 1632SN/A{ 164217SN/A os << "\n[" << _name << "]\n"; 1652SN/A} 1662SN/A 167217SN/Atemplate <class T> 1682SN/Avoid 1696225Snate@binkert.orgparamOut(ostream &os, const string &name, const T ¶m) 1702SN/A{ 171217SN/A os << name << "="; 172217SN/A showParam(os, param); 173217SN/A os << "\n"; 1742SN/A} 1752SN/A 1764841Ssaidi@eecs.umich.edutemplate <class T> 1774841Ssaidi@eecs.umich.eduvoid 1786225Snate@binkert.orgarrayParamOut(ostream &os, const string &name, const vector<T> ¶m) 1794841Ssaidi@eecs.umich.edu{ 1806227Snate@binkert.org typename vector<T>::size_type size = param.size(); 1814841Ssaidi@eecs.umich.edu os << name << "="; 1824841Ssaidi@eecs.umich.edu if (size > 0) 1834841Ssaidi@eecs.umich.edu showParam(os, param[0]); 1846227Snate@binkert.org for (typename vector<T>::size_type i = 1; i < size; ++i) { 1854841Ssaidi@eecs.umich.edu os << " "; 1864841Ssaidi@eecs.umich.edu showParam(os, param[i]); 1874841Ssaidi@eecs.umich.edu } 1884841Ssaidi@eecs.umich.edu os << "\n"; 1894841Ssaidi@eecs.umich.edu} 1904841Ssaidi@eecs.umich.edu 1917948SAli.Saidi@ARM.comtemplate <class T> 1927948SAli.Saidi@ARM.comvoid 1937948SAli.Saidi@ARM.comarrayParamOut(ostream &os, const string &name, const list<T> ¶m) 1947948SAli.Saidi@ARM.com{ 1957948SAli.Saidi@ARM.com typename list<T>::const_iterator it = param.begin(); 1967948SAli.Saidi@ARM.com 1977948SAli.Saidi@ARM.com os << name << "="; 1987948SAli.Saidi@ARM.com if (param.size() > 0) 1997948SAli.Saidi@ARM.com showParam(os, *it); 2007948SAli.Saidi@ARM.com it++; 2017948SAli.Saidi@ARM.com while (it != param.end()) { 2027948SAli.Saidi@ARM.com os << " "; 2037948SAli.Saidi@ARM.com showParam(os, *it); 2047948SAli.Saidi@ARM.com it++; 2057948SAli.Saidi@ARM.com } 2067948SAli.Saidi@ARM.com os << "\n"; 2077948SAli.Saidi@ARM.com} 208217SN/A 209217SN/Atemplate <class T> 210217SN/Avoid 2116225Snate@binkert.orgparamIn(Checkpoint *cp, const string §ion, const string &name, T ¶m) 2122SN/A{ 2136225Snate@binkert.org string str; 214237SN/A if (!cp->find(section, name, str) || !parseParam(str, param)) { 215217SN/A fatal("Can't unserialize '%s:%s'\n", section, name); 216217SN/A } 2172SN/A} 2182SN/A 2196820SLisa.Hsu@amd.comtemplate <class T> 2206820SLisa.Hsu@amd.combool 2216820SLisa.Hsu@amd.comoptParamIn(Checkpoint *cp, const string §ion, const string &name, T ¶m) 2226820SLisa.Hsu@amd.com{ 2236820SLisa.Hsu@amd.com string str; 2246820SLisa.Hsu@amd.com if (!cp->find(section, name, str) || !parseParam(str, param)) { 2256820SLisa.Hsu@amd.com warn("optional parameter %s:%s not present\n", section, name); 2266820SLisa.Hsu@amd.com return false; 2276820SLisa.Hsu@amd.com } else { 2286820SLisa.Hsu@amd.com return true; 2296820SLisa.Hsu@amd.com } 2306820SLisa.Hsu@amd.com} 231217SN/A 232217SN/Atemplate <class T> 233217SN/Avoid 2346227Snate@binkert.orgarrayParamOut(ostream &os, const string &name, const T *param, unsigned size) 235217SN/A{ 236217SN/A os << name << "="; 237217SN/A if (size > 0) 238217SN/A showParam(os, param[0]); 2396227Snate@binkert.org for (unsigned i = 1; i < size; ++i) { 240217SN/A os << " "; 241217SN/A showParam(os, param[i]); 242217SN/A } 243217SN/A os << "\n"; 244217SN/A} 245217SN/A 246217SN/A 247217SN/Atemplate <class T> 248217SN/Avoid 2496225Snate@binkert.orgarrayParamIn(Checkpoint *cp, const string §ion, const string &name, 2506227Snate@binkert.org T *param, unsigned size) 251217SN/A{ 2526225Snate@binkert.org string str; 253237SN/A if (!cp->find(section, name, str)) { 254217SN/A fatal("Can't unserialize '%s:%s'\n", section, name); 255217SN/A } 256217SN/A 257217SN/A // code below stolen from VectorParam<T>::parse(). 258217SN/A // it would be nice to unify these somehow... 259217SN/A 260217SN/A vector<string> tokens; 261217SN/A 262217SN/A tokenize(tokens, str, ' '); 263217SN/A 264217SN/A // Need this if we were doing a vector 265217SN/A // value.resize(tokens.size()); 266217SN/A 267217SN/A if (tokens.size() != size) { 268217SN/A fatal("Array size mismatch on %s:%s'\n", section, name); 269217SN/A } 270217SN/A 2716227Snate@binkert.org for (vector<string>::size_type i = 0; i < tokens.size(); i++) { 272217SN/A // need to parse into local variable to handle vector<bool>, 273217SN/A // for which operator[] returns a special reference class 274217SN/A // that's not the same as 'bool&', (since it's a packed 275217SN/A // vector) 2767576SAli.Saidi@ARM.com T scalar_value = 0; 277217SN/A if (!parseParam(tokens[i], scalar_value)) { 278217SN/A string err("could not parse \""); 279217SN/A 280217SN/A err += str; 281217SN/A err += "\""; 282217SN/A 283217SN/A fatal(err); 284217SN/A } 285217SN/A 286217SN/A // assign parsed value to vector 287217SN/A param[i] = scalar_value; 288217SN/A } 289217SN/A} 290217SN/A 2914841Ssaidi@eecs.umich.edutemplate <class T> 2924841Ssaidi@eecs.umich.eduvoid 2936225Snate@binkert.orgarrayParamIn(Checkpoint *cp, const string §ion, 2946225Snate@binkert.org const string &name, vector<T> ¶m) 2954841Ssaidi@eecs.umich.edu{ 2966225Snate@binkert.org string str; 2974841Ssaidi@eecs.umich.edu if (!cp->find(section, name, str)) { 2984841Ssaidi@eecs.umich.edu fatal("Can't unserialize '%s:%s'\n", section, name); 2994841Ssaidi@eecs.umich.edu } 3004841Ssaidi@eecs.umich.edu 3014841Ssaidi@eecs.umich.edu // code below stolen from VectorParam<T>::parse(). 3024841Ssaidi@eecs.umich.edu // it would be nice to unify these somehow... 3034841Ssaidi@eecs.umich.edu 3044841Ssaidi@eecs.umich.edu vector<string> tokens; 3054841Ssaidi@eecs.umich.edu 3064841Ssaidi@eecs.umich.edu tokenize(tokens, str, ' '); 3074841Ssaidi@eecs.umich.edu 3084841Ssaidi@eecs.umich.edu // Need this if we were doing a vector 3094841Ssaidi@eecs.umich.edu // value.resize(tokens.size()); 3104841Ssaidi@eecs.umich.edu 3114841Ssaidi@eecs.umich.edu param.resize(tokens.size()); 3124841Ssaidi@eecs.umich.edu 3136227Snate@binkert.org for (vector<string>::size_type i = 0; i < tokens.size(); i++) { 3144841Ssaidi@eecs.umich.edu // need to parse into local variable to handle vector<bool>, 3154841Ssaidi@eecs.umich.edu // for which operator[] returns a special reference class 3164841Ssaidi@eecs.umich.edu // that's not the same as 'bool&', (since it's a packed 3174841Ssaidi@eecs.umich.edu // vector) 3187576SAli.Saidi@ARM.com T scalar_value = 0; 3194841Ssaidi@eecs.umich.edu if (!parseParam(tokens[i], scalar_value)) { 3204841Ssaidi@eecs.umich.edu string err("could not parse \""); 3214841Ssaidi@eecs.umich.edu 3224841Ssaidi@eecs.umich.edu err += str; 3234841Ssaidi@eecs.umich.edu err += "\""; 3244841Ssaidi@eecs.umich.edu 3254841Ssaidi@eecs.umich.edu fatal(err); 3264841Ssaidi@eecs.umich.edu } 3274841Ssaidi@eecs.umich.edu 3284841Ssaidi@eecs.umich.edu // assign parsed value to vector 3294841Ssaidi@eecs.umich.edu param[i] = scalar_value; 3304841Ssaidi@eecs.umich.edu } 3314841Ssaidi@eecs.umich.edu} 3324841Ssaidi@eecs.umich.edu 3337948SAli.Saidi@ARM.comtemplate <class T> 3347948SAli.Saidi@ARM.comvoid 3357948SAli.Saidi@ARM.comarrayParamIn(Checkpoint *cp, const string §ion, 3367948SAli.Saidi@ARM.com const string &name, list<T> ¶m) 3377948SAli.Saidi@ARM.com{ 3387948SAli.Saidi@ARM.com string str; 3397948SAli.Saidi@ARM.com if (!cp->find(section, name, str)) { 3407948SAli.Saidi@ARM.com fatal("Can't unserialize '%s:%s'\n", section, name); 3417948SAli.Saidi@ARM.com } 3427948SAli.Saidi@ARM.com param.clear(); 3437948SAli.Saidi@ARM.com 3447948SAli.Saidi@ARM.com vector<string> tokens; 3457948SAli.Saidi@ARM.com tokenize(tokens, str, ' '); 3467948SAli.Saidi@ARM.com 3477948SAli.Saidi@ARM.com for (vector<string>::size_type i = 0; i < tokens.size(); i++) { 3487948SAli.Saidi@ARM.com T scalar_value = 0; 3497948SAli.Saidi@ARM.com if (!parseParam(tokens[i], scalar_value)) { 3507948SAli.Saidi@ARM.com string err("could not parse \""); 3517948SAli.Saidi@ARM.com 3527948SAli.Saidi@ARM.com err += str; 3537948SAli.Saidi@ARM.com err += "\""; 3547948SAli.Saidi@ARM.com 3557948SAli.Saidi@ARM.com fatal(err); 3567948SAli.Saidi@ARM.com } 3577948SAli.Saidi@ARM.com 3587948SAli.Saidi@ARM.com // assign parsed value to vector 3597948SAli.Saidi@ARM.com param.push_back(scalar_value); 3607948SAli.Saidi@ARM.com } 3617948SAli.Saidi@ARM.com} 3627948SAli.Saidi@ARM.com 3637948SAli.Saidi@ARM.com 364237SN/Avoid 3656225Snate@binkert.orgobjParamIn(Checkpoint *cp, const string §ion, 3666225Snate@binkert.org const string &name, SimObject * ¶m) 367237SN/A{ 368237SN/A if (!cp->findObj(section, name, param)) { 369237SN/A fatal("Can't unserialize '%s:%s'\n", section, name); 370237SN/A } 371237SN/A} 372237SN/A 373237SN/A 3745543Ssaidi@eecs.umich.edu#define INSTANTIATE_PARAM_TEMPLATES(type) \ 3755543Ssaidi@eecs.umich.edutemplate void \ 3766225Snate@binkert.orgparamOut(ostream &os, const string &name, type const ¶m); \ 3775543Ssaidi@eecs.umich.edutemplate void \ 3786225Snate@binkert.orgparamIn(Checkpoint *cp, const string §ion, \ 3796225Snate@binkert.org const string &name, type & param); \ 3806820SLisa.Hsu@amd.comtemplate bool \ 3816820SLisa.Hsu@amd.comoptParamIn(Checkpoint *cp, const string §ion, \ 3826820SLisa.Hsu@amd.com const string &name, type & param); \ 3835543Ssaidi@eecs.umich.edutemplate void \ 3846225Snate@binkert.orgarrayParamOut(ostream &os, const string &name, \ 3856227Snate@binkert.org type const *param, unsigned size); \ 3865543Ssaidi@eecs.umich.edutemplate void \ 3876225Snate@binkert.orgarrayParamIn(Checkpoint *cp, const string §ion, \ 3886227Snate@binkert.org const string &name, type *param, unsigned size); \ 3895543Ssaidi@eecs.umich.edutemplate void \ 3906225Snate@binkert.orgarrayParamOut(ostream &os, const string &name, \ 3916225Snate@binkert.org const vector<type> ¶m); \ 3925543Ssaidi@eecs.umich.edutemplate void \ 3936225Snate@binkert.orgarrayParamIn(Checkpoint *cp, const string §ion, \ 3947948SAli.Saidi@ARM.com const string &name, vector<type> ¶m); \ 3957948SAli.Saidi@ARM.comtemplate void \ 3967948SAli.Saidi@ARM.comarrayParamOut(ostream &os, const string &name, \ 3977948SAli.Saidi@ARM.com const list<type> ¶m); \ 3987948SAli.Saidi@ARM.comtemplate void \ 3997948SAli.Saidi@ARM.comarrayParamIn(Checkpoint *cp, const string §ion, \ 4007948SAli.Saidi@ARM.com const string &name, list<type> ¶m); 401217SN/A 4027494Ssteve.reinhardt@amd.comINSTANTIATE_PARAM_TEMPLATES(char) 4031642SN/AINSTANTIATE_PARAM_TEMPLATES(signed char) 4041642SN/AINSTANTIATE_PARAM_TEMPLATES(unsigned char) 4051642SN/AINSTANTIATE_PARAM_TEMPLATES(signed short) 4061642SN/AINSTANTIATE_PARAM_TEMPLATES(unsigned short) 4071642SN/AINSTANTIATE_PARAM_TEMPLATES(signed int) 4081642SN/AINSTANTIATE_PARAM_TEMPLATES(unsigned int) 4091642SN/AINSTANTIATE_PARAM_TEMPLATES(signed long) 4101642SN/AINSTANTIATE_PARAM_TEMPLATES(unsigned long) 4111642SN/AINSTANTIATE_PARAM_TEMPLATES(signed long long) 4121642SN/AINSTANTIATE_PARAM_TEMPLATES(unsigned long long) 413219SN/AINSTANTIATE_PARAM_TEMPLATES(bool) 4145992Snate@binkert.orgINSTANTIATE_PARAM_TEMPLATES(float) 4155992Snate@binkert.orgINSTANTIATE_PARAM_TEMPLATES(double) 416217SN/AINSTANTIATE_PARAM_TEMPLATES(string) 417217SN/A 418217SN/A 419395SN/A///////////////////////////// 420395SN/A 421395SN/A/// Container for serializing global variables (not associated with 422395SN/A/// any serialized object). 423395SN/Aclass Globals : public Serializable 4242SN/A{ 425395SN/A public: 426512SN/A const string name() const; 427510SN/A void serialize(ostream &os); 4288737Skoansin.tan@gmail.com void unserialize(Checkpoint *cp, const std::string §ion); 429395SN/A}; 4302SN/A 431395SN/A/// The one and only instance of the Globals class. 432395SN/AGlobals globals; 4332SN/A 434512SN/Aconst string 435395SN/AGlobals::name() const 4362SN/A{ 437395SN/A return "Globals"; 4382SN/A} 4392SN/A 4402SN/Avoid 441510SN/AGlobals::serialize(ostream &os) 4422SN/A{ 443395SN/A nameOut(os); 4447864Ssteve.reinhardt@amd.com paramOut(os, "curTick", curTick()); 445395SN/A 4469983Sstever@gmail.com paramOut(os, "numMainEventQueues", numMainEventQueues); 4479983Sstever@gmail.com 4489983Sstever@gmail.com for (uint32_t i = 0; i < numMainEventQueues; ++i) { 4499983Sstever@gmail.com nameOut(os, "MainEventQueue"); 4509983Sstever@gmail.com mainEventQueue[i]->serialize(os); 4519983Sstever@gmail.com } 4522SN/A} 4532SN/A 4542SN/Avoid 4558737Skoansin.tan@gmail.comGlobals::unserialize(Checkpoint *cp, const std::string §ion) 4562SN/A{ 4577823Ssteve.reinhardt@amd.com Tick tick; 4587823Ssteve.reinhardt@amd.com paramIn(cp, section, "curTick", tick); 4599983Sstever@gmail.com paramIn(cp, section, "numMainEventQueues", numMainEventQueues); 4602SN/A 4619983Sstever@gmail.com for (uint32_t i = 0; i < numMainEventQueues; ++i) { 4629983Sstever@gmail.com mainEventQueue[i]->setCurTick(tick); 4639983Sstever@gmail.com mainEventQueue[i]->unserialize(cp, "MainEventQueue"); 4649983Sstever@gmail.com } 4652SN/A} 4662SN/A 4675739Snate@binkert.orgSerializable::Serializable() 4685739Snate@binkert.org{ 4695739Snate@binkert.org} 4705739Snate@binkert.org 4715739Snate@binkert.orgSerializable::~Serializable() 4725739Snate@binkert.org{ 4735739Snate@binkert.org} 4745739Snate@binkert.org 4755739Snate@binkert.orgvoid 4766225Snate@binkert.orgSerializable::serialize(ostream &os) 4775739Snate@binkert.org{ 4785739Snate@binkert.org} 4795739Snate@binkert.org 4805739Snate@binkert.orgvoid 4816225Snate@binkert.orgSerializable::unserialize(Checkpoint *cp, const string §ion) 4825739Snate@binkert.org{ 4835739Snate@binkert.org} 4845739Snate@binkert.org 4852SN/Avoid 4866225Snate@binkert.orgSerializable::serializeAll(const string &cpt_dir) 4872SN/A{ 4887491Ssteve.reinhardt@amd.com string dir = Checkpoint::setDir(cpt_dir); 489363SN/A if (mkdir(dir.c_str(), 0775) == -1 && errno != EEXIST) 490449SN/A fatal("couldn't mkdir %s\n", dir); 491363SN/A 492449SN/A string cpt_file = dir + Checkpoint::baseFilename; 493395SN/A ofstream outstream(cpt_file.c_str()); 4942SN/A time_t t = time(NULL); 4955581Ssaidi@eecs.umich.edu if (!outstream.is_open()) 4965581Ssaidi@eecs.umich.edu fatal("Unable to open file %s for writing\n", cpt_file.c_str()); 4976818SLisa.Hsu@amd.com outstream << "## checkpoint generated: " << ctime(&t); 4982SN/A 499395SN/A globals.serialize(outstream); 500395SN/A SimObject::serializeAll(outstream); 501395SN/A} 5022SN/A 5032797Sktlim@umich.eduvoid 504395SN/ASerializable::unserializeGlobals(Checkpoint *cp) 505395SN/A{ 5068737Skoansin.tan@gmail.com globals.unserialize(cp, globals.name()); 507395SN/A} 5082SN/A 5092SN/Avoid 5106225Snate@binkert.orgdebug_serialize(const string &cpt_dir) 5112SN/A{ 5122868Sktlim@umich.edu Serializable::serializeAll(cpt_dir); 5132SN/A} 5142SN/A 5152SN/A 5162SN/A//////////////////////////////////////////////////////////////////////// 5172SN/A// 518395SN/A// SerializableClass member definitions 5192SN/A// 5202SN/A//////////////////////////////////////////////////////////////////////// 5212SN/A 522395SN/A// Map of class names to SerializableBuilder creation functions. 5232SN/A// Need to make this a pointer so we can force initialization on the 524395SN/A// first reference; otherwise, some SerializableClass constructors 5252SN/A// may be invoked before the classMap constructor. 5266225Snate@binkert.orgmap<string, SerializableClass::CreateFunc> *SerializableClass::classMap = 0; 5272SN/A 528395SN/A// SerializableClass constructor: add mapping to classMap 529395SN/ASerializableClass::SerializableClass(const string &className, 5306225Snate@binkert.org CreateFunc createFunc) 5312SN/A{ 5322SN/A if (classMap == NULL) 5336225Snate@binkert.org classMap = new map<string, SerializableClass::CreateFunc>(); 5342SN/A 5352SN/A if ((*classMap)[className]) 5366225Snate@binkert.org fatal("Error: simulation object class %s redefined\n", className); 5372SN/A 5382SN/A // add className --> createFunc to class map 5392SN/A (*classMap)[className] = createFunc; 5402SN/A} 5412SN/A 5422SN/A// 5432SN/A// 544395SN/ASerializable * 5456225Snate@binkert.orgSerializableClass::createObject(Checkpoint *cp, const string §ion) 5462SN/A{ 547237SN/A string className; 5482SN/A 549237SN/A if (!cp->find(section, "type", className)) { 550395SN/A fatal("Serializable::create: no 'type' entry in section '%s'.\n", 551237SN/A section); 5522SN/A } 5532SN/A 554237SN/A CreateFunc createFunc = (*classMap)[className]; 555237SN/A 556237SN/A if (createFunc == NULL) { 557395SN/A fatal("Serializable::create: no create function for class '%s'.\n", 558237SN/A className); 5592SN/A } 5602SN/A 561395SN/A Serializable *object = createFunc(cp, section); 5622SN/A 5632SN/A assert(object != NULL); 5642SN/A 5652SN/A return object; 5662SN/A} 5672SN/A 568237SN/A 569395SN/ASerializable * 5706225Snate@binkert.orgSerializable::create(Checkpoint *cp, const string §ion) 571237SN/A{ 572395SN/A Serializable *object = SerializableClass::createObject(cp, section); 573237SN/A object->unserialize(cp, section); 574237SN/A return object; 575237SN/A} 576237SN/A 577237SN/A 5787491Ssteve.reinhardt@amd.comconst char *Checkpoint::baseFilename = "m5.cpt"; 5797491Ssteve.reinhardt@amd.com 5807491Ssteve.reinhardt@amd.comstring Checkpoint::currentDirectory; 5817491Ssteve.reinhardt@amd.com 5827491Ssteve.reinhardt@amd.comstring 5837491Ssteve.reinhardt@amd.comCheckpoint::setDir(const string &name) 5847491Ssteve.reinhardt@amd.com{ 5857823Ssteve.reinhardt@amd.com // use csprintf to insert curTick() into directory name if it 5867491Ssteve.reinhardt@amd.com // appears to have a format placeholder in it. 5877491Ssteve.reinhardt@amd.com currentDirectory = (name.find("%") != string::npos) ? 5887823Ssteve.reinhardt@amd.com csprintf(name, curTick()) : name; 5897491Ssteve.reinhardt@amd.com if (currentDirectory[currentDirectory.size() - 1] != '/') 5907491Ssteve.reinhardt@amd.com currentDirectory += "/"; 5917491Ssteve.reinhardt@amd.com return currentDirectory; 5927491Ssteve.reinhardt@amd.com} 5937491Ssteve.reinhardt@amd.com 5947491Ssteve.reinhardt@amd.comstring 5957491Ssteve.reinhardt@amd.comCheckpoint::dir() 5967491Ssteve.reinhardt@amd.com{ 5977491Ssteve.reinhardt@amd.com return currentDirectory; 5987491Ssteve.reinhardt@amd.com} 5997491Ssteve.reinhardt@amd.com 6007491Ssteve.reinhardt@amd.com 60110453SAndrew.Bardsley@arm.comCheckpoint::Checkpoint(const string &cpt_dir, SimObjectResolver &resolver) 60210453SAndrew.Bardsley@arm.com : db(new IniFile), objNameResolver(resolver), cptDir(setDir(cpt_dir)) 603237SN/A{ 6047532Ssteve.reinhardt@amd.com string filename = cptDir + "/" + Checkpoint::baseFilename; 605237SN/A if (!db->load(filename)) { 606237SN/A fatal("Can't load checkpoint file '%s'\n", filename); 607237SN/A } 608237SN/A} 609237SN/A 6109086Sandreas.hansson@arm.comCheckpoint::~Checkpoint() 6119086Sandreas.hansson@arm.com{ 6129086Sandreas.hansson@arm.com delete db; 6139086Sandreas.hansson@arm.com} 614237SN/A 615237SN/Abool 6166225Snate@binkert.orgCheckpoint::find(const string §ion, const string &entry, string &value) 617237SN/A{ 618237SN/A return db->find(section, entry, value); 619237SN/A} 620237SN/A 621237SN/A 622237SN/Abool 6236225Snate@binkert.orgCheckpoint::findObj(const string §ion, const string &entry, 6244000Ssaidi@eecs.umich.edu SimObject *&value) 625237SN/A{ 626237SN/A string path; 627237SN/A 628237SN/A if (!db->find(section, entry, path)) 629237SN/A return false; 630237SN/A 63110453SAndrew.Bardsley@arm.com value = objNameResolver.resolveSimObject(path); 6324000Ssaidi@eecs.umich.edu return true; 633237SN/A} 634304SN/A 635304SN/A 636304SN/Abool 6376225Snate@binkert.orgCheckpoint::sectionExists(const string §ion) 638304SN/A{ 639304SN/A return db->sectionExists(section); 640304SN/A} 641