-
Notifications
You must be signed in to change notification settings - Fork 29
/
IndexCache.h
272 lines (217 loc) · 6.09 KB
/
IndexCache.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
#if !defined(_INDEX_CACHE_H_)
#define _INDEX_CACHE_H_
#include "CacheEntry.h"
#include "HugePageAlloc.h"
#include "Timer.h"
#include "third_party/inlineskiplist.h"
#include <atomic>
#include <vector>
extern bool enter_debug;
using CacheSkipList = InlineSkipList<CacheEntryComparator>;
class IndexCache {
public:
IndexCache(int cache_size);
bool add_to_cache(InternalPage *page);
const CacheEntry *search_from_cache(const Key &k, GlobalAddress *addr);
void search_range_from_cache(const Key &from, const Key &to,
std::vector<InternalPage *> &result);
bool add_entry(const Key &from, const Key &to, InternalPage *ptr);
const CacheEntry *find_entry(const Key &k);
const CacheEntry *find_entry(const Key &from, const Key &to);
bool invalidate(const CacheEntry *entry);
const CacheEntry *get_a_random_entry(uint64_t &freq);
void statistics();
void bench();
private:
uint64_t cache_size; // MB;
std::atomic<int64_t> free_page_cnt;
std::atomic<int64_t> skiplist_node_cnt;
int64_t all_page_cnt;
// SkipList
CacheSkipList *skiplist;
CacheEntryComparator cmp;
Allocator alloc;
void evict_one();
};
inline IndexCache::IndexCache(int cache_size) : cache_size(cache_size) {
skiplist = new CacheSkipList(cmp, &alloc, 21);
uint64_t memory_size = define::MB * cache_size;
all_page_cnt = memory_size / sizeof(InternalPage);
free_page_cnt.store(all_page_cnt);
skiplist_node_cnt.store(0);
}
// [from, to)
inline bool IndexCache::add_entry(const Key &from, const Key &to,
InternalPage *ptr) {
// TODO memory leak
auto buf = skiplist->AllocateKey(sizeof(CacheEntry));
auto &e = *(CacheEntry *)buf;
e.from = from;
e.to = to - 1; // !IMPORTANT;
e.ptr = ptr;
return skiplist->InsertConcurrently(buf);
}
inline const CacheEntry *IndexCache::find_entry(const Key &from,
const Key &to) {
CacheSkipList::Iterator iter(skiplist);
CacheEntry e;
e.from = from;
e.to = to - 1;
iter.Seek((char *)&e);
if (iter.Valid()) {
auto val = (const CacheEntry *)iter.key();
// while (val->ptr == nullptr) {
// iter.Next();
// if (!iter.Valid()) {
// return nullptr;
// }
// val = (const CacheEntry *)iter.key();
// }
return val;
} else {
return nullptr;
}
}
inline const CacheEntry *IndexCache::find_entry(const Key &k) {
return find_entry(k, k + 1);
}
inline bool IndexCache::add_to_cache(InternalPage *page) {
auto new_page = (InternalPage *)malloc(kInternalPageSize);
memcpy(new_page, page, kInternalPageSize);
new_page->index_cache_freq = 0;
if (this->add_entry(page->hdr.lowest, page->hdr.highest, new_page)) {
skiplist_node_cnt.fetch_add(1);
auto v = free_page_cnt.fetch_add(-1);
if (v <= 0) {
evict_one();
}
return true;
} else { // conflicted
auto e = this->find_entry(page->hdr.lowest, page->hdr.highest);
if (e && e->from == page->hdr.lowest && e->to == page->hdr.highest - 1) {
auto ptr = e->ptr;
if (ptr == nullptr &&
__sync_bool_compare_and_swap(&(e->ptr), 0ull, new_page)) {
// if (enter_debug) {
// page->verbose_debug();
// }
auto v = free_page_cnt.fetch_add(-1);
if (v <= 0) {
evict_one();
}
return true;
}
}
free(new_page);
return false;
}
}
inline const CacheEntry *IndexCache::search_from_cache(const Key &k,
GlobalAddress *addr) {
auto entry = find_entry(k);
InternalPage *page = entry ? entry->ptr : nullptr;
if (page && entry->from <= k && entry->to >= k) {
// if (enter_debug) {
// page->verbose_debug();
// }
page->index_cache_freq++;
auto cnt = page->hdr.last_index + 1;
if (k < page->records[0].key) {
*addr = page->hdr.leftmost_ptr;
} else {
bool find = false;
for (int i = 1; i < cnt; ++i) {
if (k < page->records[i].key) {
find = true;
*addr = page->records[i - 1].ptr;
break;
}
}
if (!find) {
*addr = page->records[cnt - 1].ptr;
}
}
compiler_barrier();
if (entry->ptr) { // check if it is freed.
// printf("Cache HIt\n");
return entry;
}
}
return nullptr;
}
inline void
IndexCache::search_range_from_cache(const Key &from, const Key &to,
std::vector<InternalPage *> &result) {
CacheSkipList::Iterator iter(skiplist);
result.clear();
CacheEntry e;
e.from = from;
e.to = from;
iter.Seek((char *)&e);
while (iter.Valid()) {
auto val = (const CacheEntry *)iter.key();
if (val->ptr) {
if (val->from > to) {
return;
}
result.push_back(val->ptr);
}
iter.Next();
}
}
inline bool IndexCache::invalidate(const CacheEntry *entry) {
auto ptr = entry->ptr;
if (ptr == nullptr) {
return false;
}
if (__sync_bool_compare_and_swap(&(entry->ptr), ptr, 0)) {
free(ptr);
free_page_cnt.fetch_add(1);
return true;
}
return false;
}
inline const CacheEntry *IndexCache::get_a_random_entry(uint64_t &freq) {
uint32_t seed = asm_rdtsc();
GlobalAddress tmp_addr;
retry:
auto k = rand_r(&seed) % (1000ull * define::MB);
auto e = this->search_from_cache(k, &tmp_addr);
if (!e) {
goto retry;
}
auto ptr = e->ptr;
if (!ptr) {
goto retry;
}
freq = ptr->index_cache_freq;
if (e->ptr != ptr) {
goto retry;
}
return e;
}
inline void IndexCache::evict_one() {
uint64_t freq1, freq2;
auto e1 = get_a_random_entry(freq1);
auto e2 = get_a_random_entry(freq2);
if (freq1 < freq2) {
invalidate(e1);
} else {
invalidate(e2);
}
}
inline void IndexCache::statistics() {
printf("[skiplist node: %ld] [page cache: %ld]\n", skiplist_node_cnt.load(),
all_page_cnt - free_page_cnt.load());
}
inline void IndexCache::bench() {
Timer t;
t.begin();
const int loop = 100000;
for (int i = 0; i < loop; ++i) {
uint64_t r = rand() % (5 * define::MB);
this->find_entry(r);
}
t.end_print(loop);
}
#endif // _INDEX_CACHE_H_