The Sparta Modeling Framework
Loading...
Searching...
No Matches
CherryPickFastCheckpointer.hpp
1// <CherryPickFastCheckpointer> -*- C++ -*-
2
3#pragma once
4
5#include "sparta/serialization/checkpoint/FastCheckpointer.hpp"
7#include "simdb/apps/App.hpp"
8#include "simdb/utils/ConcurrentQueue.hpp"
9#include <map>
10
11namespace sparta
12{
13 class TreeNode;
14 class Scheduler;
15}
16
17namespace simdb::pipeline
18{
19 class Flusher;
20}
21
22namespace sparta::serialization::checkpoint
23{
24
25class CherryPickFastCheckpointer final : public simdb::App
26{
27public:
28 using checkpoint_type = typename FastCheckpointer::checkpoint_type;
29 using checkpoint_ptr = typename FastCheckpointer::checkpoint_ptr;
30 using checkpoint_ptrs = typename FastCheckpointer::checkpoint_ptrs;
31 using chkpt_id_t = typename Checkpointer::chkpt_id_t;
32 using arch_id_t = uint64_t;
33 using tick_t = typename Checkpointer::tick_t;
34
35 static constexpr auto NAME = "cherry-pick-fast-checkpointer";
36
53 CherryPickFastCheckpointer(simdb::DatabaseManager* db_mgr, const std::vector<TreeNode*> & roots,
54 Scheduler* sched = nullptr);
55
59 static void defineSchema(simdb::Schema& schema);
60
64 void createPipeline(simdb::pipeline::PipelineManager* pipeline_mgr) override;
65
70 return checkpointer_;
71 }
72
87 void commitCurrentBranch(bool force_new_head_chkpt = false);
88
92 void saveCheckpoints(checkpoint_ptrs&& checkpoints);
93
99 size_t getNumCheckpoints() const;
100
104 std::string stringize() const;
105
106 struct ChkptWindow {
107 arch_id_t start_arch_id = UINT64_MAX;
108 arch_id_t end_arch_id = UINT64_MAX;
109 tick_t start_tick = UINT64_MAX;
110 tick_t end_tick = UINT64_MAX;
111 checkpoint_ptrs checkpoints;
112
113 template <typename Archive>
114 void serialize(Archive & ar, const unsigned int /*version*/) {
115 ar & start_arch_id;
116 ar & end_arch_id;
117 ar & start_tick;
118 ar & end_tick;
119
120 if (checkpoints.empty()) {
121 // We are loading a checkpoint window from disk
122 const auto num_chkpts = end_arch_id - start_arch_id + 1;
123 for (size_t i = 0; i < num_chkpts; ++i) {
124 checkpoints.emplace_back(new checkpoint_type);
125 ar & *checkpoints.back();
126 }
127 } else {
128 // We are saving a checkpoint window to disk
129 for (auto& chkpt : checkpoints) {
130 ar & *chkpt;
131 }
132 }
133 }
134 };
135
137 arch_id_t start_arch_id = UINT64_MAX;
138 arch_id_t end_arch_id = UINT64_MAX;
139 tick_t start_tick = UINT64_MAX;
140 tick_t end_tick = UINT64_MAX;
141 size_t num_chkpts = 0;
142 std::vector<char> chkpt_bytes;
143 };
144
145private:
146 FastCheckpointer checkpointer_;
147 simdb::DatabaseManager* db_mgr_ = nullptr;
148 simdb::ConcurrentQueue<ChkptWindow>* pipeline_head_ = nullptr;
149 std::unique_ptr<simdb::pipeline::Flusher> pipeline_flusher_;
150};
151
152} // namespace sparta::serialization::checkpoint
153
154namespace simdb
155{
156
162template <>
163class AppFactory<sparta::serialization::checkpoint::CherryPickFastCheckpointer> : public AppFactoryBase
164{
165public:
167
173 void setArchDataRoots(size_t instance_num, const std::vector<sparta::TreeNode*>& roots)
174 {
175 roots_by_inst_num_[instance_num] = roots;
176 }
177
182 {
183 scheduler_ = &sched;
184 }
185
186 AppT* createApp(DatabaseManager* db_mgr, size_t instance_num = 0) override
187 {
188 auto it = roots_by_inst_num_.find(instance_num);
189 if (it == roots_by_inst_num_.end()) {
191 "No TreeNode (ArchData root) set for DatabaseCheckpointer instance number ")
192 << instance_num << ". Did you forget to call setArchDataRoot()?";
193 }
194
195 const auto & roots = it->second;
196
197 // Make the ctor call that the default AppFactory cannot make.
198 return new AppT(db_mgr, roots, scheduler_);
199 }
200
201 void defineSchema(Schema& schema) const override
202 {
203 AppT::defineSchema(schema);
204 }
205
206private:
207 sparta::Scheduler* scheduler_ = nullptr;
208 std::map<size_t, std::vector<sparta::TreeNode*>> roots_by_inst_num_;
209};
210
211} // namespace simdb
Exception class for all of Sparta.
void setArchDataRoots(size_t instance_num, const std::vector< sparta::TreeNode * > &roots)
Sets the ArchData root(s) for a given instance of the checkpointer.
void setScheduler(sparta::Scheduler &sched)
Sets the Scheduler for all instances of the checkpointer.
A class that lets you schedule events now and in the future.
Used to construct and throw a standard C++ exception. Inherits from std::exception.
Checkpoint::chkpt_id_t chkpt_id_t
tick_t Tick type to which checkpoints will refer
Checkpoint::tick_t tick_t
tick_t Tick type to which checkpoints will refer
static void defineSchema(simdb::Schema &schema)
Define the SimDB schema for this checkpointer.
std::string stringize() const
Returns a string describing this object.
void saveCheckpoints(checkpoint_ptrs &&checkpoints)
Send the committed checkpoints down the pipeline to the database.
void createPipeline(simdb::pipeline::PipelineManager *pipeline_mgr) override
Instantiate the async processing pipeline to save checkpoints to the DB.
CherryPickFastCheckpointer(simdb::DatabaseManager *db_mgr, const std::vector< TreeNode * > &roots, Scheduler *sched=nullptr)
CherryPickFastCheckpointer constructor.
FastCheckpointer & getFastCheckpointer() noexcept
Use the FastCheckpointer to create checkpoints / checkpoint branches.
void commitCurrentBranch(bool force_new_head_chkpt=false)
When satisfied with the outstanding/uncommitted checkpoints, call this method to commit them to the d...
size_t getNumCheckpoints() const
Get the total number of checkpoints sent to the database thus far.
Single delta checkpoint object containing all simulator state which changed since some previous Delta...
Implements quick checkpointing through delta-checkpoint trees which store state-deltas in a compact f...
Macros for handling exponential backoff.