The Sparta Modeling Framework
Loading...
Searching...
No Matches
Checkpoint.hpp
1// <Checkpoint> -*- C++ -*-
2
3#pragma once
4
5#include <iostream>
6#include <sstream>
7
8#include "sparta/functional/ArchData.hpp"
12
14
15
16namespace sparta::serialization::checkpoint
17{
18 class FastCheckpointer;
19
29 {
30 public:
31
35
38
40 typedef uint64_t chkpt_id_t;
41
44
48 static const chkpt_id_t MIN_CHECKPOINT = 0;
49
55
56
60
62 Checkpoint() = delete;
63
65 Checkpoint(const Checkpoint&) = delete;
66
68 const Checkpoint& operator=(const Checkpoint&) = delete;
69
70 protected:
71
76 tick_t tick,
77 Checkpoint* prev) :
78 tick_(tick),
79 chkpt_id_(id),
80 prev_(prev)
81 { }
82
83 public:
84
85
92 virtual ~Checkpoint() {
93 if(getPrev()){
94 getPrev()->removeNext(this);
95 }
96
97 // Reconnect deltas from this checkpoint to the prev (even if nullptr)
98 for(auto& d : getNexts()){
99 d->setPrev(getPrev());
100 if(getPrev()){
101 getPrev()->addNext(d);
102 }
103 }
104
105 }
106
109
113 virtual std::string stringize() const {
114 std::stringstream ss;
115 ss << "<Checkpoint id=" << chkpt_id_ << " at t=" << tick_;
116 ss << ' ' << getTotalMemoryUse()/1000.0f << "kB (" << getContentMemoryUse()/1000.0f << "kB Data)";
117 ss << '>';
118 return ss.str();
119 }
120
126 virtual void dumpData(std::ostream& o) const = 0;
127
132 virtual uint64_t getTotalMemoryUse() const noexcept = 0;
133
138 virtual uint64_t getContentMemoryUse() const noexcept = 0;
139
143
148 virtual void load(const std::vector<ArchData*>& dats) = 0;
149
153 tick_t getTick() const noexcept { return tick_; }
154
160 chkpt_id_t getID() const noexcept { return chkpt_id_; }
161
166 virtual std::string getDeletedRepr() const {
167 return "*";
168 }
169
174 Checkpoint* getPrev() const noexcept {
175 return prev_;
176 }
177
184 void setPrev(Checkpoint* prev) noexcept {
185 prev_ = prev;
186 }
187
197 void addNext(Checkpoint* next) {
198 if(!next){
199 throw CheckpointError("Cannot specify nullptr in addNext");
200 }
201 if(next->getPrev() != this){
202 throw CheckpointError("Attempting to add a next checkpoint whose previous checkpoint pointer is not 'this'");
203 }
204 if(next->getTick() < getTick()){
205 throw CheckpointError("Attempting to add a next checkpoint whose tick number (")
206 << next->getTick() << " is less than this checkpoint's tick: " << getTick();
207 }
208 if(std::find(nexts_.begin(), nexts_.end(), next) != nexts_.end()){
209 throw CheckpointError("Next argument already present in this checkpoint's nexts_ list. Cannot re-add");
210 }
211 nexts_.push_back(next);
212 }
213
222 void removeNext(Checkpoint* next) {
223 if(!next){
224 throw CheckpointError("Cannot specify nullptr in removeNext");
225 }
226 if(next->getPrev() != this){
227 throw CheckpointError("Attempting to remove a next checkpoint whose previous pointer is not 'this'");
228 }
229 auto itr = std::find(nexts_.begin(), nexts_.end(), next);
230 if(itr == nexts_.end()){
231 throw CheckpointError("Next argument was not present in this checkpoint's nexts_ list. Cannot remove");
232 }
233 nexts_.erase(itr);
234 }
235
243 const std::vector<Checkpoint*>& getNexts() const noexcept { return nexts_; }
244
247
248 protected:
249
254 chkpt_id_ = id;
255 }
256
257 private:
258
259 const tick_t tick_;
260 chkpt_id_t chkpt_id_;
261
267 std::vector<Checkpoint*> nexts_;
268
269 Checkpoint* prev_;
270 };
271
272} // namespace sparta::serialization::checkpoint
273
274
276inline std::ostream& operator<<(std::ostream& o, const sparta::serialization::checkpoint::Checkpoint& dcp){
277 o << dcp.stringize();
278 return o;
279}
280
282inline std::ostream& operator<<(std::ostream& o, const sparta::serialization::checkpoint::Checkpoint* dcp){
283 if(dcp == 0){
284 o << "null";
285 }else{
286 o << dcp->stringize();
287 }
288 return o;
289}
290
292#define SPARTA_CHECKPOINT_BODY \
293 namespace sparta{ namespace serialization { namespace checkpoint { \
294 const Checkpoint::chkpt_id_t Checkpoint::MIN_CHECKPOINT; \
295 const Checkpoint::chkpt_id_t Checkpoint::UNIDENTIFIED_CHECKPOINT; \
296 }}}
File that contains checkpoint exception types.
A simple time-based, event precedence based scheduler.
Set of macros for Sparta assertions. Caught by the framework.
Exception class for all of Sparta.
Contains a set of contiguous line of architectural data which can be referred to by any architected o...
Definition ArchData.hpp:39
uint64_t Tick
Typedef for our unit of time.
Indicates that there was an issue operating on checkpoints within the SPARTA framework.
Single checkpoint object interface with a tick number and an ID unique to the owning Checkpointer ins...
Checkpoint()=delete
Not default constructable.
Checkpoint * getPrev() const noexcept
Returns the previous checkpoint. If this checkpoint is a snapshot, it has no previous checkpoint.
virtual std::string getDeletedRepr() const
Gets the representation of this deleted checkpoint as part of a checkpoint chain (if that checkpointe...
virtual std::string stringize() const
Returns a string describing this object.
Checkpoint(const Checkpoint &)=delete
Not copy constructable.
const std::vector< Checkpoint * > & getNexts() const noexcept
Returns next checkpoint following *this. May be an empty vector if there are no later checkpoints fol...
chkpt_id_t getID() const noexcept
Returns the ID of this checkpoint.
tick_t getTick() const noexcept
Returns the tick number at which this checkpoint was taken.
sparta::Scheduler::Tick tick_t
tick_t Tick type to which checkpoints will refer
virtual void load(const std::vector< ArchData * > &dats)=0
Attempts to restore this checkpoint state to the simulation state (ArchData) objects given to this Ch...
static const chkpt_id_t MIN_CHECKPOINT
Indicates the smallest valid checkpoint id.
void addNext(Checkpoint *next)
Adds another next checkpoint following *this.
void setID_(chkpt_id_t id)
Sets the checkpoint ID.
const Checkpoint & operator=(const Checkpoint &)=delete
Non-assignable.
void setPrev(Checkpoint *prev) noexcept
Sets the previous checkpoint of this checkpoint to prev.
virtual uint64_t getTotalMemoryUse() const noexcept=0
Returns memory usage by this checkpoint including any framework data structures.
Checkpoint(chkpt_id_t id, tick_t tick, Checkpoint *prev)
virtual void dumpData(std::ostream &o) const =0
Writes all checkpoint raw data to an ostream.
uint64_t chkpt_id_t
tick_t Tick type to which checkpoints will refer
static const chkpt_id_t UNIDENTIFIED_CHECKPOINT
Indicates unidentified checkpoint (could mean 'invalid' or 'any') depending on context.
void removeNext(Checkpoint *next)
Removes a checkpoint following *this because it was deleted.
virtual uint64_t getContentMemoryUse() const noexcept=0
Returns memory usage by this checkpoint solely for the checkpointed content.
std::ostream & operator<<(std::ostream &o, const SimulationInfo &info)
ostream insertion operator for SimulationInfo