15#include "sparta/functional/ArchData.hpp"
18#include "sparta/simulation/TreeNodePrivateAttorney.hpp"
19#include "sparta/serialization/checkpoint/Checkpoint.hpp"
21namespace sparta::serialization::checkpoint
93 total_chkpts_created_(0)
133 mem += cp.second->getTotalMemoryUse();
145 mem += cp.second->getContentMemoryUse();
156 return total_chkpts_created_;
197 exc <<
"<no scheduler>";
199 exc <<
" because a head already exists in this checkpointer";
203 CheckpointError exc(
"Cannot create a checkpoint until the tree is finalized. Attempting to checkpoint from node ");
208 exc <<
"<no scheduler>";
213 enumerateArchDatas_();
217 sparta_assert(head_ !=
nullptr,
"A call to createHead_ must create a head and invoke setHead_ or throw an exception");
219 total_chkpts_created_++;
255 total_chkpts_created_++;
432 return head_->
getID();
461 return current_->
getID();
498 std::stringstream ss;
499 ss <<
"<Checkpointer on " << root_.
getLocation() <<
'>';
510 o << cp.second->stringize() << std::endl;
521 cp.second->dumpData(o);
534 o << cp.second->stringize() << std::endl;
535 cp.second->dumpData(o);
563 std::deque<uint32_t> c;
584 std::deque<uint32_t>& continues)
const {
586 static const std::string SEP_STR =
"-> ";
587 static const std::string CONT_SEP_STR =
"`> ";
588 assert(SEP_STR.size() == CONT_SEP_STR.size());
592 std::remove_reference<
decltype(continues)>::type::const_iterator next_cont = continues.begin();
593 for(; i < indent; ++i){
594 if(next_cont != continues.end() && i == *next_cont){
596 while(next_cont != continues.end() && *next_cont == i){
605 std::stringstream ss;
608 if(next_cont != continues.end() && *next_cont == indent && indent != pos){
615 if(current_ == chkpt){
619 dumpCheckpointNode_(chkpt, ss);
622 if(current_ == chkpt){
627 i += ss.str().size();
630 if(nexts.size() > 0){
631 if(nexts.size() > 1){
632 continues.push_back(i);
634 dumpBranch(o, nexts.front(), i, i, continues);
635 decltype(nexts)::const_iterator itr = nexts.begin() + 1;
636 if(itr == nexts.end()){
639 while(itr < nexts.end()){
640 if(itr+1 == nexts.end()){
641 continues.pop_back();
692 virtual void dumpCheckpointNode_(
const Checkpoint* chkpt, std::ostream& o)
const {
726 sparta_assert(head !=
nullptr,
"head argument in setHead_ cannot be nullptr");
727 sparta_assert(head_ ==
nullptr,
"Cannot setHead_ again on a Checkpointer once heas is already set");
747 "Can never setCurrent_ to nullptr except. A null current is a valid state at initialization only")
759 std::map<chkpt_id_t, std::unique_ptr<Checkpoint>>
chkpts_;
775 void enumerateArchDatas_() {
776 sparta_assert(adatas_.size() == 0,
"FastCheckpointer already has a vector of ArchDatas. Cannot re-enumerate");
783 std::map<ArchData*,TreeNode*> adatas_helper;
786 recursAddArchData_(&root_, adatas_helper);
796 void recursAddArchData_(
TreeNode* n, std::map<ArchData*,TreeNode*>& adatas_helper) {
801 auto itr = adatas_helper.find(ad);
802 if(itr != adatas_helper.end()){
804 << ad <<
" in the tree: " << root_.
stringize() <<
" . First reference found throgh "
805 << itr->second->getLocation() <<
" and second found through " << n->
getLocation()
806 <<
" . An ArchData should be findable throug exactly 1 TreeNode";
808 adatas_.push_back(ad);
809 adatas_helper[ad] = n;
812 for(TreeNode* child : TreeNodePrivateAttorney::getAllChildren(n)){
813 recursAddArchData_(child, adatas_helper);
829 std::vector<ArchData*> adatas_;
834 Checkpoint* current_;
840 uint64_t total_chkpts_created_;
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...
virtual bool isFinalized() const
Is this node (and thus the entire tree above it) "finalized".
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.
Node in a composite tree representing a sparta Tree item.
std::string getLocation() const override final
virtual std::string stringize(bool pretty=false) const
Create a string representation of this node.
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...
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
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.
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...
virtual Checkpoint * findLatestCheckpointAtOrBefore(tick_t tick, chkpt_id_t from)=0
Finds the latest checkpoint at or before the given tick starting at the from checkpoint and working b...
void createHead()
Creates a head without taking an identified checkpoint. Cannot already have a head.
virtual const Checkpoint * findCheckpoint_(chkpt_id_t id) const noexcept=0
const variant of findCheckpoint_
const std::vector< ArchData * > & getArchDatas() const
Returns ArchDatas enumerated by this Checkpointer for iteration when saving or loading checkpoint dat...
virtual Checkpoint * findCheckpoint_(chkpt_id_t id) noexcept=0
Attempts to find a checkpoint within this checkpointer by ID.
const Checkpoint * getHead_() const noexcept
Gets the head checkpoint. Returns nullptr if none created yet.
void dumpTree(std::ostream &o) const
Dumps this checkpointer's tree to an ostream with a line for each branch. Printout timescale is not r...
Scheduler *const sched_
Scheduler whose tick count will be set and read. Cannnot be updated after first checkpoint without ba...
void dumpList(std::ostream &o) const
Dumps this checkpointer's flat list of checkpoints to an ostream with a newline following each checkp...
const Scheduler * getScheduler() const noexcept
Returns the sheduler associated with this checkpointer.
const TreeNode & getRoot() const noexcept
Returns the root 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.
virtual void loadCheckpoint(chkpt_id_t id)=0
Loads state from a specific checkpoint by ID.
void dumpData(std::ostream &o) const
Dumps this checkpointer's data to an ostream with a newline following each checkpoint.
uint64_t getTotalMemoryUse() const noexcept
Computes and returns the memory usage by this checkpointer at this moment including any framework ove...
void dumpAnnotatedData(std::ostream &o) const
Dumps this checkpointer's data to an ostream with annotations between each ArchData and a newline fol...
Checkpoint * getCurrent_() const noexcept
Gets the current checkpointer pointer. Returns nullptr if there is no current checkpoint object.
virtual void createHead_()=0
Create a head node.
virtual std::vector< chkpt_id_t > getCheckpointsAt(tick_t t) const =0
Gets all checkpoints taken at tick t on any timeline.
void dumpBranch(std::ostream &o, const Checkpoint *chkpt, uint32_t indent, uint32_t pos, std::deque< uint32_t > &continues) const
Recursively dumps one branch (and sub-branches) to an ostream with a line for each branch.
Checkpoint * getHead_() noexcept
Non-const variant of getHead_.
virtual bool hasCheckpoint(chkpt_id_t id) const noexcept
Tests whether this checkpoint manager has a checkpoint with the given id.
chkpt_id_t getCurrentID() const
Returns the current checkpoint ID. This is mainly a debugging utility as the current ID changes when ...
virtual std::vector< chkpt_id_t > getCheckpoints() const =0
Gets all known checkpoint IDs available on any timeline sorted by tick (or equivalently checkpoint ID...
tick_t getCurrentTick() const
Gets the tick number of the current checkpoint (see getCurrentID). This is the tick number of the lat...
virtual chkpt_id_t createCheckpoint_(bool force_snapshot=false)=0
Create a 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...
Checkpoint::tick_t tick_t
tick_t Tick type to which checkpoints will refer
virtual void deleteCheckpoint(chkpt_id_t id)=0
Deletes a checkpoint by ID.
std::map< chkpt_id_t, std::unique_ptr< Checkpoint > > chkpts_
All checkpoints sorted by ascending tick number (or equivalently ascending checkpoint ID since both a...
const Checkpoint * getHead() const noexcept
Returns the head checkpoint which is equivalent to the earliest checkpoint taken.
uint64_t getTotalCheckpointsCreated() const noexcept
Returns the total number of checkpoints which have been created by this checkpointer....
Checkpoint::chkpt_id_t chkpt_id_t
tick_t Tick type to which checkpoints will refer
TreeNode & getRoot() noexcept
Non-const variant of getRoot.
virtual ~Checkpointer()
Destructor.
virtual std::string stringize() const
Returns a string describing this object.
void setCurrent_(Checkpoint *current)
Sets the current checkpoint pointer.
chkpt_id_t getHeadID() const noexcept
Returns the checkpoint ID of the head checkpoint (if it exists) which is equivalent to the earliest c...
Checkpoint * findCheckpoint(chkpt_id_t id) noexcept
Finds a checkpoint by its ID.
virtual std::deque< chkpt_id_t > getCheckpointChain(chkpt_id_t id) const =0
Debugging utility which gets a deque of checkpoints representing a chain starting at the checkpoint h...
void forgetCurrent()
Forgets the current checkpoint and current checkpoint (resetting to the head checkpoint) so that chec...
uint64_t getContentMemoryUse() const noexcept
Computes and returns the memory usage by this checkpointer at this moment purely for the checkpoint s...
void setHead_(Checkpoint *head)
Sets the head checkpointer pointer to head for the first time.
std::ostream & operator<<(std::ostream &o, const SimulationInfo &info)
ostream insertion operator for SimulationInfo