The Sparta Modeling Framework
Loading...
Searching...
No Matches
PersistentFastCheckpointer.hpp
1// <PersistentFastCheckpointer> -*- C++ -*-
2
3#pragma once
4
5#include <iostream>
6#include <sstream>
7#include <stack>
8#include <queue>
9#include <string>
10
12#include "sparta/functional/ArchData.hpp"
15#include "sparta/serialization/checkpoint/FastCheckpointer.hpp"
16
17namespace sparta::serialization::checkpoint
18{
28 {
29 public:
30
36 {
37 std::ostream& fs_;
38
39 public:
40 FileWriteAdapter(std::ostream& out) :
41 fs_(out)
42 {;}
43
44 void dump(std::ostream& o) const {
45 o << "<dump not supported on checkpoint file storage adapter>";
46 }
47
48 uint32_t getSize() const {
49 // Return MEMORY size. Assume file is on disk.
50 return sizeof(decltype(*this));
51 }
52
53 void beginLine(ArchData::line_idx_type idx) {
54 fs_ << 'L'; // Line start char
55
56 ArchData::line_idx_type idx_repr = reorder<ArchData::line_idx_type, LE>(idx);
57 fs_.write((char*)&idx_repr, sizeof(ArchData::line_idx_type));
58 }
59
60 void writeLineBytes(const char* data, size_t size) {
61 fs_.write(data, size);
62 }
63
64 void endArchData() {
65 fs_ << "E"; // Indicates end of this checkpoint data
66
67 sparta_assert(fs_.good(),
68 "Ostream error while writing checkpoint data");
69 }
70
71 bool good() const {
72 return fs_.good();
73 }
74 };
75
81 {
82 std::istream& fs_;
83
84 public:
85 FileReadAdapter(std::istream& in) :
86 fs_(in)
87 {;}
88
89 void dump(std::ostream& o) const {
90 o << "<dump not supported on checkpoint file storage adapter>";
91 }
92
93 uint32_t getSize() const {
94 // Return MEMORY size. Assume file is on disk.
95 return sizeof(decltype(*this));
96 }
97
98 void prepareForLoad() {
99 fs_.seekg(0); // Seek to start with get pointer before consuming
100 }
101
102 bool good() const {
103 return fs_.good();
104 }
105
106 ArchData::line_idx_type getNextRestoreLine() {
107 char ctrl;
108 fs_ >> ctrl;
109 sparta_assert(fs_.good(),
110 "Encountered checkpoint data stream error or eof");
111 if(ctrl == 'L'){
112 ArchData::line_idx_type ln_idx = 0;
113 fs_.read((char*)&ln_idx, sizeof(ln_idx)); // Presumed LE encoding
114 return ln_idx;
115 }else if(ctrl == 'E'){
116 return ArchData::INVALID_LINE_IDX; // Done with restore
117 }else{
118 throw SpartaException("Failed to restore a checkpoint because a '")
119 << ctrl << "' control character was found where an 'L' or 'E' was found";
120 }
121 };
122
123 void copyLineBytes(char* buf, uint32_t size) {
124 fs_.read(buf, size);
125 }
126 };
127
131
150 sparta::Scheduler* sched=nullptr) :
151 FastCheckpointer(root, sched),
152 prefix_("chkpt"),
153 suffix_("data")
154 { }
155
161
162 }
163
170 FastCheckpointer::chkpt_id_t save(std::ostream& outf) {
172 save_(outf);
173 return checkpoint_id;
174 }
175
182 FastCheckpointer::chkpt_id_t save(std::string filename) {
184 std::ofstream outf(filename, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc);
185 save_(outf);
186 outf.close();
187 return checkpoint_id;
188 }
189
198 const bool force_snapshot = true;
199 FastCheckpointer::chkpt_id_t checkpoint_id = createCheckpoint(force_snapshot);
200 std::ostringstream chkpt_filename;
201 chkpt_filename << prefix_ << "."
202 << checkpoint_id
203 << "." << suffix_;
204 std::ofstream outf(chkpt_filename.str(), std::ofstream::binary | std::ofstream::trunc);
205 save_(outf);
206 outf.close();
207 return checkpoint_id;
208 }
209
214 void restore(std::istream& in) {
215 auto adatas = getArchDatas();
216 FileReadAdapter fsa(in);
217 for (auto aditr=adatas.begin(); aditr!= adatas.end(); aditr++) {
218 (*aditr)->restoreAll(fsa);
219 }
220 }
221
226 void restore(const std::string& filename) {
227 std::ifstream in(filename);
228 restore(in);
229 in.close();
230 }
231
232 private:
233
238 void save_(std::ostream& outf) {
239 // Throw on write failure
240 outf.exceptions(std::ostream::eofbit | std::ostream::badbit |
241 std::ostream::failbit | std::ostream::goodbit);
242 FileWriteAdapter fsa(outf);
243 auto adatas = getArchDatas();
244 for (auto aditr=adatas.begin(); aditr!= adatas.end(); aditr++) {
245 (*aditr)->saveAll(fsa);
246 }
247 }
248
249 std::string prefix_;
250 std::string suffix_;
251
252 };
253
254} // namespace sparta::serialization::checkpoint
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.
static const line_idx_type INVALID_LINE_IDX
Invalid line index.
Definition ArchData.hpp:112
offset_type line_idx_type
Represents offsets into this ArchData.
Definition ArchData.hpp:59
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.
Node in a composite tree representing a sparta Tree item.
Definition TreeNode.hpp:205
const std::vector< ArchData * > & getArchDatas() const
Returns ArchDatas enumerated by this Checkpointer for iteration when saving or loading checkpoint dat...
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 ...
Checkpoint::chkpt_id_t chkpt_id_t
tick_t Tick type to which checkpoints will refer
Implements quick checkpointing through delta-checkpoint trees which store state-deltas in a compact f...
Implements a Persistent FastCheckpointer, i.e. implements an interface to save checkpoints to disk.
PersistentFastCheckpointer(TreeNode &root, sparta::Scheduler *sched=nullptr)
PersistentFastCheckpointer Constructor.