-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge-commit.cpp
90 lines (72 loc) · 2.06 KB
/
merge-commit.cpp
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
#include "mem.h"
#include "log.h"
#include <assert.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <queue>
#include <vector>
#include <sstream>
using namespace std;
// #define DEBUG
#include "debug.h"
struct q_ent {
tid_t tid;
uint64_t ts;
q_ent(tid_t t, const uint64_t ts) : tid(t), ts(ts) {}
bool operator>(const q_ent &rv) const {
return ts > rv.ts;
}
};
uint64_t *next_ts(struct mapped_log *tslog) {
return (uint64_t *)read_log_entry(tslog, sizeof(uint64_t));
}
static void merge_commit(vector<struct mapped_log> &log, tid_t nthr) {
assert((int)log.size() == nthr);
struct mapped_log commitlog;
if (new_mapped_log_path(LOGDIR"commit", &commitlog) == -1) {
perror("create commit order log");
exit(1);
}
priority_queue<q_ent, vector<q_ent>, greater<q_ent> > ts_q;
for (unsigned int i = 0; i < log.size(); i++) {
if (log[i].fd == -1) continue;
uint64_t *ts = next_ts(&log[i]);
if (ts != NULL) {
ts_q.push(q_ent((tid_t)i, *ts));
//printf("T%u %ld\n", i, *ts);
}
}
while (!ts_q.empty()) {
q_ent e = ts_q.top();
ts_q.pop();
//printf("T%d %ld\n", e.tid, e.ts);
tid_t *p = (tid_t *)next_log_entry(&commitlog, sizeof(tid_t));
*p = e.tid;
// Enqueue next ts.
uint64_t *nts = next_ts(&log[e.tid]);
if (nts != NULL) {
ts_q.push(q_ent(e.tid, *nts));
}
}
unmap_truncate_log(&commitlog);
}
int main(int argc, char const *argv[]) {
if (argc != 2) {
printf("Usage: merge-tsc <nthr>\n");
exit(1);
}
int nthr;
istringstream nthrs(argv[1]);
nthrs >> nthr;
vector<struct mapped_log> log;
struct mapped_log l;
for (int i = 0; i < nthr; ++i) {
// XXX Push the log structure into the vector even if open failed,
// because the index in the vector represents thread id.
open_mapped_log("commit", i, &l);
log.push_back(l);
}
merge_commit(log, (tid_t)nthr);
return 0;
}