15#include "sparta/functional/ArchData.hpp"
18#include "sparta/simulation/TreeNodePrivateAttorney.hpp"
19#include "sparta/serialization/checkpoint/Checkpoint.hpp"
21namespace sparta::serialization::checkpoint
90 roots_.push_back(&root);
124 const std::vector<TreeNode*> &
getRoots() const noexcept {
return roots_; }
151 return total_chkpts_created_;
192 exc <<
"<no scheduler>";
194 exc <<
" because a head already exists in this checkpointer";
197 for (
auto root : roots_) {
198 if(root->isFinalized() ==
false){
199 CheckpointError exc(
"Cannot create a checkpoint until the tree is finalized. Attempting to checkpoint from node ");
200 exc << root->getLocation() <<
" at tick ";
204 exc <<
"<no scheduler>";
210 enumerateArchDatas_();
214 sparta_assert(head_ !=
nullptr,
"A call to createHead_ must create a head and invoke setHead_ or throw an exception");
216 total_chkpts_created_++;
252 total_chkpts_created_++;
397 return head_->
getID();
426 return current_->
getID();
468 std::stringstream ss;
469 ss <<
"<Checkpointer on ";
470 for (
size_t i = 0; i < roots_.size(); ++i) {
526 std::deque<uint32_t> c;
547 std::deque<uint32_t>& continues) {
549 static const std::string SEP_STR =
"-> ";
550 static const std::string CONT_SEP_STR =
"`> ";
551 assert(SEP_STR.size() == CONT_SEP_STR.size());
555 std::remove_reference<
decltype(continues)>::type::const_iterator next_cont = continues.begin();
556 for(; i < indent; ++i){
557 if(next_cont != continues.end() && i == *next_cont){
559 while(next_cont != continues.end() && *next_cont == i){
568 std::stringstream ss;
571 if(next_cont != continues.end() && *next_cont == indent && indent != pos){
578 if(current_ && current_->
getID() == chkpt){
582 dumpCheckpointNode_(chkpt, ss);
585 if(current_ && current_->
getID() == chkpt){
590 i += ss.str().size();
593 if(nexts.size() > 0){
594 if(nexts.size() > 1){
595 continues.push_back(i);
597 dumpBranch(o, nexts.front(), i, i, continues);
598 decltype(nexts)::const_iterator itr = nexts.begin() + 1;
599 if(itr == nexts.end()){
602 while(itr < nexts.end()){
603 if(itr+1 == nexts.end()){
604 continues.pop_back();
641 virtual void dumpCheckpointNode_(
const chkpt_id_t id, std::ostream& o) {
675 sparta_assert(head !=
nullptr,
"head argument in setHead_ cannot be nullptr");
676 sparta_assert(head_ ==
nullptr,
"Cannot setHead_ again on a Checkpointer once heas is already set");
696 "Can never setCurrent_ to nullptr except. A null current is a valid state at initialization only")
714 void enumerateArchDatas_() {
715 sparta_assert(adatas_.size() == 0,
"FastCheckpointer already has a vector of ArchDatas. Cannot re-enumerate");
722 std::map<ArchData*,TreeNode*> adatas_helper;
726 recursAddArchData_(root, adatas_helper);
737 void recursAddArchData_(
TreeNode* n, std::map<ArchData*,TreeNode*>& adatas_helper) {
742 auto itr = adatas_helper.find(ad);
743 if(itr != adatas_helper.end()){
745 << ad <<
" in the checkpointer: " <<
stringize() <<
" . First reference found throgh "
746 << itr->second->getLocation() <<
" and second found through " << n->
getLocation()
747 <<
" . An ArchData should be findable throug exactly 1 TreeNode";
749 adatas_.push_back(ad);
750 adatas_helper[ad] = n;
754 recursAddArchData_(child, adatas_helper);
758 std::vector<TreeNode*> roots_;
764 CheckpointBase* head_ =
nullptr;
770 std::vector<ArchData*> adatas_;
775 CheckpointBase* current_ =
nullptr;
781 uint64_t total_chkpts_created_ = 0;
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.
std::vector< ArchData * > getAssociatedArchDatas()
Retrieves all ArchDatas associated with this TreeNode so that children can use it to allocate their d...
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.
static const TreeNode::ChildrenVector & getAllChildren(const TreeNode &node)
Node in a composite tree representing a sparta Tree item.
std::string getLocation() const override final
Single checkpoint object interface with a tick number and an ID unique to the owning Checkpointer ins...
uint64_t chkpt_id_t
tick_t Checkpoint ID type to which checkpoints will refer
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.
sparta::Scheduler::Tick tick_t
tick_t Tick type to which checkpoints will refer
Indicates that there was an issue operating on checkpoints within the SPARTA framework.
Checkpointer interface. Defines an ID-based checkpointing API for tree of related checkpoints which c...
virtual void traceValue(std::ostream &o, chkpt_id_t id, const ArchData *container, uint32_t offset, uint32_t size)=0
Debugging utility which dumps values in some bytes across a chain of checkpoints. The intent is to sh...
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
void createHead()
Creates a head without taking an identified checkpoint. Cannot already have a head.
virtual uint64_t getTotalMemoryUse() const noexcept=0
Computes and returns the memory usage by this checkpointer at this moment including any framework ove...
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.
virtual std::vector< chkpt_id_t > getCheckpoints()=0
Gets all known checkpoint IDs available on any timeline sorted by tick (or equivalently checkpoint ID...
Scheduler *const sched_
Scheduler whose tick count will be set and read. Cannnot be updated after first checkpoint without ba...
virtual void dumpList(std::ostream &o)=0
Dumps this checkpointer's flat list of checkpoints to an ostream with a newline following each checkp...
CheckpointBase * getHead_() noexcept
Non-const variant of getHead_.
Checkpointer(const std::vector< TreeNode * > &roots, sparta::Scheduler *sched=nullptr)
Checkpointer Constructor.
const Scheduler * getScheduler() const noexcept
Returns the sheduler associated with this checkpointer.
chkpt_id_t createCheckpoint(bool force_snapshot=false)
Creates a checkpoint at the given scheduler's current tick with a new checkpoint ID some point after ...
Checkpointer(TreeNode &root, sparta::Scheduler *sched=nullptr)
Checkpointer Constructor.
const CheckpointBase * getHead() const noexcept
Returns the head checkpoint which is equivalent to the earliest checkpoint taken.
virtual void loadCheckpoint(chkpt_id_t id)=0
Loads state from a specific checkpoint by ID.
virtual void dumpAnnotatedData(std::ostream &o)=0
Dumps this checkpointer's data to an ostream with annotations between each ArchData and a newline fol...
void setHead_(CheckpointBase *head)
Sets the head checkpointer pointer to head for the first time.
virtual void createHead_()=0
Create a head node.
virtual std::deque< chkpt_id_t > getCheckpointChain(chkpt_id_t id)=0
Debugging utility which gets a deque of checkpoints representing a chain starting at the checkpoint h...
chkpt_id_t getCurrentID() const
Returns the current checkpoint ID. This is mainly a debugging utility as the current ID changes when ...
const CheckpointBase * getHead_() const noexcept
Gets the head checkpoint. Returns nullptr if none created yet.
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...
virtual std::vector< chkpt_id_t > getNextIDs(chkpt_id_t id)=0
Returns IDs of the checkpoints immediately following the given checkpoint.
void dumpBranch(std::ostream &o, const chkpt_id_t chkpt, uint32_t indent, uint32_t pos, std::deque< uint32_t > &continues)
Recursively dumps one branch (and sub-branches) to an ostream with a line for each branch.
virtual chkpt_id_t createCheckpoint_(bool force_snapshot=false)=0
Create a checkpoint.
virtual void dumpData(std::ostream &o)=0
Dumps this checkpointer's data to an ostream with a newline following each checkpoint.
virtual uint32_t getNumCheckpoints() const noexcept=0
Gets the current number of checkpoints having valid IDs which a client of this interface can refer to...
virtual void deleteCheckpoint(chkpt_id_t id)=0
Deletes a checkpoint by ID.
uint64_t getTotalCheckpointsCreated() const noexcept
Returns the total number of checkpoints which have been created by this checkpointer....
virtual bool hasCheckpoint(chkpt_id_t id) noexcept=0
Tests whether this checkpoint manager has a checkpoint with the given id.
virtual std::vector< chkpt_id_t > getCheckpointsAt(tick_t t)=0
Gets all checkpoints taken at tick t on any timeline.
virtual ~Checkpointer()
Destructor.
virtual std::string stringize() const
Returns a string describing this object.
void dumpTree(std::ostream &o)
Dumps this checkpointer's tree to an ostream with a line for each branch. Printout timescale is not r...
chkpt_id_t getHeadID() const noexcept
Returns the checkpoint ID of the head checkpoint (if it exists) which is equivalent to the earliest c...
virtual uint64_t getContentMemoryUse() const noexcept=0
Computes and returns the memory usage by this checkpointer at this moment purely for the checkpoint s...
CheckpointBase * getCurrent_() const noexcept
Gets the current checkpointer pointer. Returns nullptr if there is no current checkpoint object.
void forgetCurrent()
Forgets the current checkpoint and current checkpoint (resetting to the head checkpoint) so that chec...
std::ostream & operator<<(std::ostream &o, const SimulationInfo &info)
ostream insertion operator for SimulationInfo