11#include "sparta/functional/ArchData.hpp"
15#include "sparta/serialization/checkpoint/DeltaCheckpoint.hpp"
18#ifndef DEFAULT_SNAPSHOT_THRESH
19#define DEFAULT_SNAPSHOT_THRESH 20
22namespace sparta::serialization::checkpoint
92 snap_thresh_(DEFAULT_SNAPSHOT_THRESH),
94 num_alive_checkpoints_(0),
95 num_alive_snapshots_(0),
96 num_dead_checkpoints_(0)
115 snap_thresh_(DEFAULT_SNAPSHOT_THRESH),
117 num_alive_checkpoints_(0),
118 num_alive_snapshots_(0),
119 num_dead_checkpoints_(0)
129 for(
auto itr = chkpts_.rbegin(); itr != chkpts_.rend(); ++itr){
163 snap_thresh_ = thresh;
174 for(
auto& cp : chkpts_){
175 mem += cp.second->getTotalMemoryUse();
186 for(
auto& cp : chkpts_){
187 mem += cp.second->getContentMemoryUse();
224 <<
id <<
" because no checkpoint by this ID was found";
231 num_dead_checkpoints_++;
233 num_alive_snapshots_--;
235 num_alive_checkpoints_--;
261 <<
id <<
" because no checkpoint by this ID was found";
293 std::vector<chkpt_id_t> results;
294 for(
auto& p : chkpts_){
298 results.push_back(cp->
getID());
313 std::vector<chkpt_id_t> results;
314 for(
auto& p : chkpts_){
318 results.push_back(cp->
getID());
328 return num_alive_checkpoints_;
335 return num_alive_snapshots_;
350 return num_dead_checkpoints_;
369 std::deque<chkpt_id_t> results;
378 results.push_back(d->
getID());
426 auto it = chkpts_.find(
id);
427 if (it != chkpts_.end()) {
441 return chkpts_.find(
id) != chkpts_.end();
448 std::vector<chkpt_id_t> next_ids;
450 for (
const auto next : chkpt->getNexts()) {
452 if (!dcp->isFlaggedDeleted()) {
453 next_ids.push_back(next->getID());
471 std::stringstream ss;
472 ss <<
"<FastCheckpointer on ";
473 for (
size_t i = 0; i <
getRoots().size(); ++i) {
490 for(
auto& cp : chkpts_){
491 o << cp.second->stringize() << std::endl;
501 for(
auto& cp : chkpts_){
502 cp.second->dumpData(o);
514 for(
auto& cp : chkpts_){
515 o << cp.second->stringize() << std::endl;
516 cp.second->dumpData(o);
526 o <<
"trace: Searching for 0x" << std::hex << offset <<
" (" << std::dec << size
527 <<
" bytes) in ArchData " << (
const void*)container <<
" when loading checkpoint "
528 << std::dec <<
id << std::endl;
530 o <<
"trace: Checkpoint " <<
id <<
" not found" << std::endl;
610 num_dead_checkpoints_--;
613 auto itr = chkpts_.find(
id);
633 const std::vector<Checkpoint*> & nexts = d->
getNexts();
634 for(
const auto & chkpt : nexts)
671 auto itr = chkpts_.find(
id);
672 if (itr != chkpts_.end()) {
682 auto itr = chkpts_.find(
id);
683 if (itr != chkpts_.end()) {
693 static std::string SNAPSHOT_NOTICE =
"(s)";
697 if(cp->isFlaggedDeleted()){
698 o << cp->getDeletedRepr();
703 if(cp->isSnapshot()){
704 o <<
' ' << SNAPSHOT_NOTICE;
713 void createHead_()
override {
720 throw CheckpointError(
"Cannot create head at ")
721 << tick <<
" because a head already exists in this checkpointer";
724 if(root->isFinalized() ==
false){
725 CheckpointError exc(
"Cannot create a checkpoint until the tree is finalized. Attempting to checkpoint from node ");
726 exc << root->getLocation() <<
" at tick ";
730 exc <<
"<no scheduler>";
736 checkpoint_type* dcp =
new checkpoint_type(
getArchDatas(), next_chkpt_id_++, tick,
nullptr,
true);
737 chkpts_[dcp->getID()].reset(dcp);
739 num_alive_checkpoints_++;
740 num_alive_snapshots_++;
744 chkpt_id_t createCheckpoint_(
bool force_snapshot=
false)
override {
746 checkpoint_type* prev;
749 throw CheckpointError(
"Exhausted all ")
751 <<
"This is likely a gross misuse of checkpointing";
765 throw CheckpointError(
"Cannot create a new checkpoint at tick ")
766 << tick <<
" because this tick number is smaller than the tick number of the head checkpoint at: "
767 <<
getHead()->
getTick() <<
". The head checkpoint cannot be reset once created, so it should be done "
768 <<
"at the start of simulation before running. The simulator front-end should do this so this must "
769 <<
"likely be fixed in the simulator.";
774 prev =
static_cast<checkpoint_type*
>(
getHead_());
778 throw CheckpointError(
"Current tick number from sparta scheduler (")
779 << tick <<
" ) is less than the current checkpoint's tick number ("
780 <<
getCurrent_()->
getTick() <<
" To create a checkpoint with an earlier tick number, an "
781 <<
"older checkpoint having a tick number <= the tick number specified here must first be "
787 prev =
static_cast<checkpoint_type*
>(
getCurrent_());
791 checkpoint_type* dcp =
new checkpoint_type(
getArchDatas(),
795 force_snapshot || is_snapshot);
796 chkpts_[dcp->getID()].reset(dcp);
797 num_alive_checkpoints_++;
798 num_alive_snapshots_ += (dcp->isSnapshot() ==
true);
801 if (dcp->isSnapshot()){
819 std::map<chkpt_id_t, std::unique_ptr<Checkpoint>> chkpts_;
825 uint32_t snap_thresh_;
836 uint32_t num_alive_checkpoints_;
844 uint32_t num_alive_snapshots_;
850 uint32_t num_dead_checkpoints_;
Set of macros for Sparta assertions. Caught by the framework.
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
Exception class for all of Sparta.
Basic Node framework in sparta device tree composite pattern.
Contains a set of contiguous line of architectural data which can be referred to by any architected o...
A class that lets you schedule events now and in the future.
Tick getCurrentTick() const noexcept
The current tick the Scheduler is working on or just finished.
void restartAt(Tick t)
Clears the events in the scheduler, sets the current tick to tick and the elapsed ticks to either tic...
Node in a composite tree representing a sparta Tree item.
std::string getLocation() const override final
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.
static const chkpt_id_t UNIDENTIFIED_CHECKPOINT
Indicates unidentified checkpoint (could mean 'invalid' or 'any') depending on context.
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 * getPrev() const noexcept
Returns the previous checkpoint. If this checkpoint is a snapshot, it has no previous checkpoint.
const std::vector< Checkpoint * > & getNexts() const noexcept
Returns next checkpoint following *this. May be an empty vector if there are no later checkpoints fol...
Checkpointer interface. Defines an ID-based checkpointing API for tree of related checkpoints which c...
const std::vector< TreeNode * > & getRoots() const noexcept
Returns the root(s) associated with this checkpointer.
Checkpoint::chkpt_id_t chkpt_id_t
tick_t Tick type to which checkpoints will refer
const std::vector< ArchData * > & getArchDatas() const
Returns ArchDatas enumerated by this Checkpointer for iteration when saving or loading checkpoint dat...
void setCurrent_(CheckpointBase *current)
Sets the current checkpoint pointer.
Scheduler *const sched_
Scheduler whose tick count will be set and read. Cannnot be updated after first checkpoint without ba...
CheckpointBase * getHead_() noexcept
Non-const variant of getHead_.
const CheckpointBase * getHead() const noexcept
Returns the head checkpoint which is equivalent to the earliest checkpoint taken.
void setHead_(CheckpointBase *head)
Sets the head checkpointer pointer to head for the first time.
Checkpoint::tick_t tick_t
tick_t Tick type to which checkpoints will refer
tick_t getCurrentTick() const
Gets the tick number of the current checkpoint (see getCurrentID). This is the tick number of the lat...
CheckpointBase * getCurrent_() const noexcept
Gets the current checkpointer pointer. Returns nullptr if there is no current checkpoint object.
Single delta checkpoint object containing all simulator state which changed since some previous Delta...
chkpt_id_t getDeletedID() const noexcept
Return the ID had by this checkpoint before it was deleted If this checkpoint has not been flagged fo...
bool isSnapshot() const noexcept
Is this checkpoint a snapshot (contains ALL simulator state)
bool isFlaggedDeleted() const noexcept
Indicates whether this checkpoint has been flagged deleted.
virtual void load(const std::vector< ArchData * > &dats) override
Attempts to restore this checkpoint including any previous deltas (dependencies).
bool canDelete() const noexcept
Can this checkpoint be deleted Cannot be deleted if:
void traceValue(std::ostream &o, const std::vector< ArchData * > &dats, const ArchData *container, uint32_t offset, uint32_t size)
Implement trace of a value across the restore chain as described in Checkpointer::traceValue.
void flagDeleted()
Allows this checkpoint to be deleted if it is no longer a previous delta of some other delta (i....
Implements quick checkpointing through delta-checkpoint trees which store state-deltas in a compact f...
std::vector< chkpt_id_t > getNextIDs(chkpt_id_t id) override final
Returns IDs of the checkpoints immediately following the given checkpoint.
const checkpoint_type * findCheckpoint(chkpt_id_t id) noexcept
Finds a checkpoint by its ID.
FastCheckpointer(TreeNode &root, Scheduler *sched=nullptr)
FastCheckpointer Constructor.
void deleteCheckpoint(chkpt_id_t id) override
Deletes a checkpoint by ID.
uint64_t getTotalMemoryUse() const noexcept override
Computes and returns the memory usage by this checkpointer at this moment including any framework ove...
void setSnapshotThreshold(uint32_t thresh) noexcept
Sets the snapshot threshold.
uint32_t getNumCheckpoints() const noexcept override
Gets the current number of checkpoints having valid IDs.
std::string stringize() const override
Returns a string describing this object.
void cleanupChain_(checkpoint_type *d)
Delete given checkpoint and all contiguous previous checkpoints which can be deleted (See checkpoint_...
std::deque< chkpt_id_t > getCheckpointChain(chkpt_id_t id) override
Debugging utility which gets a deque of checkpoints representing a chain starting at the checkpoint h...
checkpoint_type * findCheckpoint_(chkpt_id_t id) noexcept
Attempts to find a checkpoint within this checkpointer by ID.
uint32_t getSnapshotThreshold() const noexcept
Returns the next-shapshot threshold.
checkpoint_type * findLatestCheckpointAtOrBefore(tick_t tick, chkpt_id_t from)
Finds the latest checkpoint at or before the given tick starting at the from checkpoint and working b...
uint64_t getContentMemoryUse() const noexcept override
Computes and returns the memory usage by this checkpointer at this moment purely for the checkpoint s...
std::vector< chkpt_id_t > getCheckpoints() override
Gets all checkpoint IDs available on any timeline sorted by tick (or equivalently checkpoint ID).
void dumpAnnotatedData(std::ostream &o) override
Dumps this checkpointer's data to an ostream with annotations between each ArchData and a newline fol...
bool hasCheckpoint(chkpt_id_t id) noexcept override
Tests whether this checkpoint manager has a checkpoint with the given id.
const checkpoint_type * findCheckpoint_(chkpt_id_t id) const noexcept
const variant of findCheckpoint_
FastCheckpointer(const std::vector< sparta::TreeNode * > &roots, Scheduler *sched=nullptr)
FastCheckpointer Constructor.
uint32_t getNumDeltas() const noexcept
Gets the current number of delta checkpoints with valid IDs.
void loadCheckpoint(chkpt_id_t id) override
Loads state from a specific checkpoint by ID.
std::vector< chkpt_id_t > getCheckpointsAt(tick_t t) override
Gets all checkpoints taken at tick t on any timeline.
uint32_t getNumSnapshots() const noexcept
Gets the current number of snapshots with valid IDs.
uint32_t getNumDeadCheckpoints() const noexcept
Gets the curent number of checkpoints (delta or snapshot) withOUT valid IDs.
bool recursForwardFindAlive_(checkpoint_type *d) const
Look forward to see if any future checkpoints depend on d.
void dumpList(std::ostream &o) override
Dumps this checkpointer's flat list of checkpoints to an ostream with a newline following each checkp...
~FastCheckpointer()
Destructor.
void dumpData(std::ostream &o) override
Dumps this checkpointer's data to an ostream with a newline following each checkpoint.
void dumpCheckpointNode_(const chkpt_id_t id, std::ostream &o) override
Implements Checkpointer::dumpCheckpointNode_.
void traceValue(std::ostream &o, chkpt_id_t id, const ArchData *container, uint32_t offset, uint32_t size) override
Forwards debug/trace info onto checkpoint by ID.