-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEpoche.cpp
160 lines (138 loc) · 4.58 KB
/
Epoche.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
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
//
// Created by florian on 22.10.15.
//
#ifndef EPOCHE_CPP
#define EPOCHE_CPP
#include <assert.h>
#include <iostream>
#include "Epoche.h"
#include "N.h"
using namespace ART;
inline DeletionList::~DeletionList() {
assert(deletitionListCount == 0 && headDeletionList == nullptr);
LabelDelete *cur = nullptr, *next = freeLabelDeletes;
while (next != nullptr) {
cur = next;
next = cur->next;
delete cur;
}
freeLabelDeletes = nullptr;
}
inline std::size_t DeletionList::size() {
return deletitionListCount;
}
inline void DeletionList::remove(LabelDelete *label, LabelDelete *prev) {
if (prev == nullptr) {
headDeletionList = label->next;
} else {
prev->next = label->next;
}
deletitionListCount -= label->nodesCount;
label->next = freeLabelDeletes;
freeLabelDeletes = label;
deleted += label->nodesCount;
}
inline void DeletionList::add(void *n, uint64_t globalEpoch) {
deletitionListCount++;
LabelDelete *label;
if (headDeletionList != nullptr && headDeletionList->nodesCount < headDeletionList->nodes.size()) {
label = headDeletionList;
} else {
if (freeLabelDeletes != nullptr) {
label = freeLabelDeletes;
freeLabelDeletes = freeLabelDeletes->next;
} else {
label = new LabelDelete();
}
label->nodesCount = 0;
label->next = headDeletionList;
headDeletionList = label;
}
label->nodes[label->nodesCount] = n;
label->nodesCount++;
label->epoche = globalEpoch;
added++;
}
inline LabelDelete *DeletionList::head() {
return headDeletionList;
}
inline void Epoche::enterEpoche(ThreadInfo &epocheInfo) {
unsigned long curEpoche = currentEpoche.load(std::memory_order_relaxed);
epocheInfo.getDeletionList().localEpoche.store(curEpoche, std::memory_order_release);
}
inline void Epoche::markNodeForDeletion(void *n, ThreadInfo &epocheInfo) {
epocheInfo.getDeletionList().add(n, currentEpoche.load());
epocheInfo.getDeletionList().thresholdCounter++;
}
inline void Epoche::exitEpocheAndCleanup(ThreadInfo &epocheInfo) {
DeletionList &deletionList = epocheInfo.getDeletionList();
if ((deletionList.thresholdCounter & (64 - 1)) == 1) {
currentEpoche++;
}
if (deletionList.thresholdCounter > startGCThreshhold) {
if (deletionList.size() == 0) {
deletionList.thresholdCounter = 0;
return;
}
deletionList.localEpoche.store(std::numeric_limits<uint64_t>::max());
uint64_t oldestEpoche = std::numeric_limits<uint64_t>::max();
for (auto &epoche : deletionLists) {
auto e = epoche.localEpoche.load();
if (e < oldestEpoche) {
oldestEpoche = e;
}
}
LabelDelete *cur = deletionList.head(), *next, *prev = nullptr;
while (cur != nullptr) {
next = cur->next;
if (cur->epoche < oldestEpoche) {
for (std::size_t i = 0; i < cur->nodesCount; ++i) {
// operator delete(cur->nodes[i]);
// delete cur->nodes[i];
ART_OLC::N::deleteNode(static_cast<ART_OLC::N*>(cur->nodes[i]));
}
deletionList.remove(cur, prev);
} else {
prev = cur;
}
cur = next;
}
deletionList.thresholdCounter = 0;
}
}
inline Epoche::~Epoche() {
uint64_t oldestEpoche = std::numeric_limits<uint64_t>::max();
for (auto &epoche : deletionLists) {
auto e = epoche.localEpoche.load();
if (e < oldestEpoche) {
oldestEpoche = e;
}
}
for (auto &d : deletionLists) {
LabelDelete *cur = d.head(), *next, *prev = nullptr;
while (cur != nullptr) {
next = cur->next;
assert(cur->epoche < oldestEpoche);
for (std::size_t i = 0; i < cur->nodesCount; ++i) {
// operator delete(cur->nodes[i]);
ART_OLC::N::deleteNode(static_cast<ART_OLC::N*>(cur->nodes[i]));
}
d.remove(cur, prev);
cur = next;
}
}
}
inline void Epoche::showDeleteRatio() {
for (auto &d : deletionLists) {
std::cout << "deleted " << d.deleted << " of " << d.added << std::endl;
}
}
inline ThreadInfo::ThreadInfo(Epoche &epoche)
: epoche(epoche), deletionList(epoche.deletionLists.local()) { }
inline DeletionList &ThreadInfo::getDeletionList() const {
return deletionList;
}
inline Epoche &ThreadInfo::getEpoche() const {
return epoche;
}
#endif //EPOCHE_CPP