-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.h
executable file
·298 lines (249 loc) · 7.05 KB
/
common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#ifndef COMMON_H
#define COMMON_H
#include <array>
#include <atomic>
#include <boost/algorithm/string.hpp>
#include <ctype.h>
#include <cstring>
#include <errno.h>
#include <execinfo.h>
#include <getopt.h>
#include <gflags/gflags.h>
#include <inttypes.h>
#include <map>
#include <math.h>
#include <rte_timer.h>
#include <setjmp.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/queue.h>
#include <tuple>
#include <unistd.h>
#include <unordered_set>
#include <limits.h>
#include "util/numautils.h"
#include "rpc.h"
// for bitmap serialization
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/unordered_map.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/binary_object.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include "logger.h"
#define N_SEQTHREADS 4
#define MAX_PROXIES 32
#define USEC_PER_SEC 1000000
#define PLOT_RECOVERY 0
#define CHECK_FIFO 0
#ifndef DUAL_PHY_PORTS
#define NPHY_PORTS 1
#else
#define NPHY_PORTS 2
#endif
const int INIT_N_BLOCKS = 2;
const int BYTES_PER_BLOCK = 65536 * 2;
const int SEQNUMS_PER_BLOCK = (BYTES_PER_BLOCK * 8);
const int MAX_BLOCKS_PER_MSG = 32;
const int RECOVERY_RPCID = 254;
// Used for indexing the session_num_vecs in the WorkerContext
enum class MachineIdx : uint8_t {
SEQ = 0,
NEXTPX0,
NEXTPX1,
NEXTPX2,
BACKUP_SEQ,
REPLICA_1,
REPLICA_2,
CLIENT,
OTHER, // unused
};
enum class ReqType : uint8_t {
kGetBitmap,
kGetDependencies,
kAssignSeqnumToHole,
kBackupReady,
kRecoveryComplete,
kGetSeqNum,
kHeartbeat,
kInitiateRecovery,
kRequestVote,
kAppendEntries,
kAppendEntriesResponse,
kSendSnapshot,
kDoGarbageCollection,
kExecuteOpA, // Dummy request type
kNoop,
kRecordNoopSeqnum,
};
// typedefs to keep consistent types
typedef int64_t client_reqid_t;
// Base class for per-thread context
class ThreadContext {
public:
erpc::Rpc<erpc::CTransport> *rpc = nullptr;
std::vector<int> session_num_vec;
size_t nconnections = 0;
size_t thread_id;
};
typedef struct dependency {
uint64_t sequence_space;
uint16_t machine_idx;
uint8_t thread_id;
uint8_t proxy_id;
uint64_t batch_size;
uint64_t seqnum;
} __attribute__((__packed__)) dependency_t;
// Recovery data sent from a proxy
typedef struct recovery_payload {
uint64_t sequence_space;
uint64_t base_seqnum;
uint16_t nblocks_total; // How many blocks this proxy will send
uint16_t nblocks_this_resp; // How many blocks in this message
uint16_t head_block; // Index of first block maintained by this proxy
uint16_t tail_block; // Index of last block maintained by this proxy
uint16_t nrequests; // Number of outstanding requests
uint8_t received_seqnums[1];
} __attribute__((packed)) recovery_payload_t;
typedef struct seq_req {
uint64_t batch_size;
uint64_t seqnum;
template<class Archive>
void serialize(Archive &ar, const unsigned int) {
ar & batch_size;
ar & seqnum;
}
} seq_req_t;
// Seq payload
typedef struct sequencer_payload {
uint64_t proxy_id;
uint64_t batch_id; // used for local sanity checks
uint64_t seq_req_id;
bool retx = false;
seq_req_t seq_reqs[1];
} __attribute__((__packed__)) payload_t;
inline uint64_t sequencer_payload_size(uint64_t n) {
return sizeof(payload_t) - sizeof(seq_req_t) + n * sizeof(seq_req_t);
}
inline void print_seqreqs(seq_req_t *seqnums, size_t n) {
std::string line;
for (size_t i = 0; i < n; i++) {
line += std::to_string(seqnums[i].batch_size) + " " +
std::to_string(seqnums[i].seqnum) + " ";
}
line += "\n";
LOG_INFO("%s", line.c_str());
}
void print_payload(payload_t *payload, size_t n) {
LOG_INFO("pid %zu bid %zu srid %zu retx %d\n",
payload->proxy_id,
payload->batch_id,
payload->seq_req_id,
payload->retx);
print_seqreqs(payload->seq_reqs, n);
}
// Client structs
typedef struct client_payload {
ReqType reqtype; // ReqType for this operation
uint16_t client_id; // Passed in at the command line
client_reqid_t client_reqid; // Client-assigned reqid
uint16_t proxy_id;
bool not_leader;
client_reqid_t highest_recvd_reqid; // highest cli reqid successfully received
seq_req_t seq_reqs[1];
} __attribute__((__packed__)) client_payload_t;
inline uint64_t client_payload_size(uint64_t x) {
return sizeof(client_payload_t) - sizeof(seq_req_t) + x * sizeof(seq_req_t);
}
typedef struct offset_payload {
uint64_t offset;
uint64_t sequence_space;
} offset_payload_t;
/* ----------- Utilities ------------- */
// Forward decls
static uint64_t usec_to_cycles(uint64_t hz, uint64_t usec);
static int __attribute__((unused)) check_double_overflow(double, double);
static int debug_print(int, const char *, ...) __attribute__((unused));
static inline void fmt_rt_assert(bool, const char *, ...);
// A basic mempool for preallocated objects of type T. Copied (nearly) verbatim from
// eRPC code (https://github.com/erpc-io/eRPC/blob/master/apps/apps_common.h)
template<class T>
class AppMemPool {
public:
size_t num_to_alloc = 1;
std::vector<T *> backing_ptr_vec;
std::vector<T *> pool;
void extend_pool() {
T *backing_ptr = new T[num_to_alloc];
for (size_t i = 0; i < num_to_alloc; i++) pool.push_back(&backing_ptr[i]);
backing_ptr_vec.push_back(backing_ptr);
num_to_alloc *= 2;
}
T *alloc() {
if (pool.empty()) extend_pool();
T *ret = pool.back();
pool.pop_back();
return ret;
}
void free(T *t) { pool.push_back(t); }
AppMemPool() {}
~AppMemPool() {
// todo there may be memory leaks with serialization?
// for (T *ptr : backing_ptr_vec) delete[] ptr;
}
};
inline static uint64_t usec_to_cycles(uint64_t hz, uint64_t usec) {
return usec * (hz / USEC_PER_SEC);
}
static int check_double_overflow(double before, double after) {
if (before > after) {
return 0;
}
return 1;
}
static inline void fmt_rt_assert(bool condition, const char *fmt, ...) {
if (unlikely(!condition)) {
char buf[1024];
va_list args;
sprintf(buf, "[%s] ", erpc::get_formatted_time().c_str());
va_start(args, fmt);
vsprintf(buf + strlen(buf), fmt, args);
va_end(args);
throw std::runtime_error(buf);
}
}
// todo replace calls with LOG_* calls (some branches do this)
inline static int __attribute__((unused))
debug_print(int flag, const char *format, ...) {
if (unlikely(flag)) {
flockfile(stdout);
va_list args;
printf("[%s] ", erpc::get_formatted_time().c_str());
va_start(args, format);
int ret = vfprintf(stdout, format, args);
va_end(args);
fflush(stdout);
funlockfile(stdout);
return ret;
}
return 0;
}
inline
static std::string get_formatted_time_from_offset(uint32_t offset) {
struct timespec t;
clock_gettime(CLOCK_REALTIME, &t);
char buf[20];
uint32_t seconds = t.tv_sec * 1000000;
uint32_t usec = t.tv_nsec / 1000;
uint32_t offset_time = seconds + usec - offset;
sprintf(buf, "%u", offset_time);
return std::string(buf);
}
#endif