This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathvmemcache_repl.c
416 lines (343 loc) · 9.13 KB
/
vmemcache_repl.c
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2018-2019, Intel Corporation */
/*
* vmemcache_repl.c -- replacement policies for vmemcache
*/
#include <stddef.h>
#include "vmemcache.h"
#include "vmemcache_repl.h"
#include "util.h"
#include "out.h"
#include "sys/queue.h"
#include "sys_util.h"
#include "ringbuf.h"
#define LEN_RING_BUF (1 << 12)
struct repl_p_entry {
TAILQ_ENTRY(repl_p_entry) node;
void *data;
struct repl_p_entry **ptr_entry; /* pointer to be zeroed when evicted */
};
struct repl_p_head {
os_mutex_t lock;
TAILQ_HEAD(head, repl_p_entry) first;
struct ringbuf *ringbuf;
};
/* forward declarations of replacement policy operations */
static int
repl_p_none_new(struct repl_p_head **head);
static void
repl_p_none_delete(struct repl_p_head *head);
static struct repl_p_entry *
repl_p_none_insert(struct repl_p_head *head, void *element,
struct repl_p_entry **ptr_entry);
static void
repl_p_none_use(struct repl_p_head *head, struct repl_p_entry **ptr_entry);
static void *
repl_p_none_evict(struct repl_p_head *head, struct repl_p_entry **ptr_entry);
static int
repl_p_lru_new(struct repl_p_head **head);
static void
repl_p_lru_delete(struct repl_p_head *head);
static struct repl_p_entry *
repl_p_lru_insert(struct repl_p_head *head, void *element,
struct repl_p_entry **ptr_entry);
static void
repl_p_lru_use(struct repl_p_head *head, struct repl_p_entry **ptr_entry);
static void *
repl_p_lru_evict(struct repl_p_head *head, struct repl_p_entry **ptr_entry);
/* replacement policy operations */
static const struct repl_p_ops repl_p_ops[VMEMCACHE_REPLACEMENT_NUM] = {
{
.repl_p_new = repl_p_none_new,
.repl_p_delete = repl_p_none_delete,
.repl_p_insert = repl_p_none_insert,
.repl_p_use = repl_p_none_use,
.repl_p_evict = repl_p_none_evict,
.dram_per_entry = 0,
},
{
.repl_p_new = repl_p_lru_new,
.repl_p_delete = repl_p_lru_delete,
.repl_p_insert = repl_p_lru_insert,
.repl_p_use = repl_p_lru_use,
.repl_p_evict = repl_p_lru_evict,
.dram_per_entry = sizeof(struct repl_p_entry),
}
};
/*
* repl_p_init -- allocate and initialize the replacement policy structure
*/
struct repl_p *
repl_p_init(enum vmemcache_repl_p rp)
{
struct repl_p *repl_p = Malloc(sizeof(struct repl_p));
if (repl_p == NULL)
return NULL;
repl_p->ops = &repl_p_ops[rp];
if (repl_p->ops->repl_p_new(&repl_p->head)) {
Free(repl_p);
return NULL;
}
return repl_p;
}
/*
* repl_p_destroy -- destroy the replacement policy structure
*/
void
repl_p_destroy(struct repl_p *repl_p)
{
ASSERTne(repl_p, NULL);
repl_p->ops->repl_p_delete(repl_p->head);
Free(repl_p);
}
/*
* repl_p_none_new -- (internal) create a new "none" replacement policy
*/
static int
repl_p_none_new(struct repl_p_head **head)
{
*head = NULL;
return 0;
}
/*
* repl_p_none_delete -- (internal) destroy the "none" replacement policy
*/
static void
repl_p_none_delete(struct repl_p_head *head)
{
}
/*
* repl_p_none_insert -- (internal) insert a new element
*/
static struct repl_p_entry *
repl_p_none_insert(struct repl_p_head *head, void *element,
struct repl_p_entry **ptr_entry)
{
vmemcache_entry_acquire(element);
return NULL;
}
/*
* repl_p_none_use -- (internal) use the element
*/
static void
repl_p_none_use(struct repl_p_head *head, struct repl_p_entry **ptr_entry)
{
}
/*
* repl_p_none_evict -- (internal) evict the element
*/
static void *
repl_p_none_evict(struct repl_p_head *head, struct repl_p_entry **ptr_entry)
{
return ptr_entry;
}
/*
* repl_p_lru_new -- (internal) create a new LRU replacement policy
*/
static int
repl_p_lru_new(struct repl_p_head **head)
{
struct repl_p_head *h = Zalloc(sizeof(struct repl_p_head));
if (h == NULL)
return -1;
util_mutex_init(&h->lock);
TAILQ_INIT(&h->first);
h->ringbuf = ringbuf_new(LEN_RING_BUF);
*head = h;
return 0;
}
/*
* dequeue_all -- (internal) dequeue all repl_p entries,
* it MUST be run under a lock
*/
static void
dequeue_all(struct repl_p_head *head)
{
struct repl_p_entry *e;
int counter = 0;
do {
e = ringbuf_trydequeue_s(head->ringbuf,
sizeof(struct repl_p_entry));
if (e == NULL)
break;
TAILQ_MOVE_TO_TAIL(&head->first, e, node);
/* unlock the entry, so that it can be used again */
util_atomic_store_explicit64(e->ptr_entry, e,
memory_order_relaxed);
/*
* We are limiting the number of iterations,
* so that this loop ends for sure, because other thread
* can insert new elements to the ring buffer in the same time.
*/
} while (++counter < LEN_RING_BUF);
}
/*
* repl_p_lru_delete -- (internal) destroy the LRU replacement policy
*/
static void
repl_p_lru_delete(struct repl_p_head *head)
{
dequeue_all(head);
ringbuf_delete(head->ringbuf);
while (!TAILQ_EMPTY(&head->first)) {
struct repl_p_entry *entry = TAILQ_FIRST(&head->first);
TAILQ_REMOVE(&head->first, entry, node);
Free(entry);
}
util_mutex_destroy(&head->lock);
Free(head);
}
/*
* repl_p_lru_insert -- (internal) insert a new element
*/
static struct repl_p_entry *
repl_p_lru_insert(struct repl_p_head *head, void *element,
struct repl_p_entry **ptr_entry)
{
struct repl_p_entry *entry = Zalloc(sizeof(struct repl_p_entry));
if (entry == NULL)
return NULL;
entry->data = element;
ASSERTne(ptr_entry, NULL);
entry->ptr_entry = ptr_entry;
/*
* 'util_bool_compare_and_swap64' must always succeed here,
* because this entry with ptr_entry=NULL has been considered as busy
* so it has never been used so far. This is the first time we set
* the 'entry->ptr_entry' to 'entry'.
*/
int rv = util_bool_compare_and_swap64(entry->ptr_entry, NULL, entry);
if (rv == 0) {
FATAL(
"repl_p_lru_insert(): failed to initialize pointer to the LRU list");
}
util_mutex_lock(&head->lock);
vmemcache_entry_acquire(element);
TAILQ_INSERT_TAIL(&head->first, entry, node);
util_mutex_unlock(&head->lock);
return entry;
}
/*
* repl_p_lru_use -- (internal) use the element
*/
static void
repl_p_lru_use(struct repl_p_head *head, struct repl_p_entry **ptr_entry)
{
struct repl_p_entry *entry;
ASSERTne(ptr_entry, NULL);
entry = *ptr_entry;
if (entry == NULL)
return;
/*
* Try to lock the entry by setting 'ptr_entry' to NULL
* and enqueue it to the ring buffer,
* so that it cannot be used nor evicted.
*/
if (!util_bool_compare_and_swap64(ptr_entry, entry, NULL))
return;
/*
* This the "in the middle of being used" state.
* In this state - after bool_compare_and_swap()
* and before ringbuf_tryenqueue() - the entry cannot be evicted.
*/
while (ringbuf_tryenqueue(head->ringbuf, entry) != 0) {
util_mutex_lock(&head->lock);
dequeue_all(head);
util_mutex_unlock(&head->lock);
}
}
/*
* repl_p_lru_evict -- (internal) evict the element
*/
static void *
repl_p_lru_evict(struct repl_p_head *head, struct repl_p_entry **ptr_entry)
{
struct repl_p_entry *entry;
void *data = NULL;
int is_LRU = (ptr_entry == NULL);
util_mutex_lock(&head->lock);
if (TAILQ_EMPTY(&head->first)) {
errno = ESRCH;
ERR("LRU queue is empty");
goto exit_unlock;
}
if (is_LRU) {
entry = TAILQ_FIRST(&head->first);
ptr_entry = entry->ptr_entry;
} else {
entry = *ptr_entry;
}
/*
* Try to lock the entry by setting 'ptr_entry' to NULL,
* so that it cannot be used nor evicted in other threads.
*/
if (entry != NULL && util_bool_compare_and_swap64(ptr_entry,
entry, NULL))
goto evict_found_entry;
/*
* The first try failed. The entry could have been locked and enqueued
* in the ring buffer, so let's flush the ring buffer and try again.
*/
dequeue_all(head);
/*
* If the entry was assigned as the LRU entry, let's assign it again,
* because the LRU entry most likely has been changed in dequeue_all().
*/
if (is_LRU) {
entry = TAILQ_FIRST(&head->first);
ptr_entry = entry->ptr_entry;
} else {
entry = *ptr_entry;
}
/* try to lock the entry the second time */
if (entry != NULL && util_bool_compare_and_swap64(ptr_entry,
entry, NULL))
goto evict_found_entry;
/* the second try failed */
if (!is_LRU) {
/* the given entry is busy, give up */
errno = EAGAIN;
ERR("entry is busy and cannot be evicted");
goto exit_unlock;
}
if (entry == NULL) {
/* no entries in the LRU queue, give up */
errno = ESRCH;
ERR("LRU queue is empty");
goto exit_unlock;
}
/* try to lock the next entries (repl_p_lru_evict can hardly fail) */
do {
entry = TAILQ_NEXT(entry, node);
if (entry == NULL)
break;
ptr_entry = entry->ptr_entry;
} while (!util_bool_compare_and_swap64(ptr_entry, entry, NULL));
if (entry != NULL)
goto evict_found_entry;
/*
* All entries in the LRU queue are locked.
* The last chance is to try to dequeue an entry.
*/
entry = ringbuf_trydequeue_s(head->ringbuf,
sizeof(struct repl_p_entry));
if (entry == NULL) {
/*
* Cannot find any entry to evict.
* It means that all entries are heavily used
* and they have to be "in the middle of being used" state now
* (see repl_p_lru_use()).
* There is nothing we can do but fail.
*/
errno = ESRCH;
ERR("no entry eligible for eviction found");
goto exit_unlock;
}
evict_found_entry:
TAILQ_REMOVE(&head->first, entry, node);
data = entry->data;
Free(entry);
exit_unlock:
util_mutex_unlock(&head->lock);
return data;
}