-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbackground.c
92 lines (73 loc) · 2.11 KB
/
background.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
#include "background.h"
#include "item.h"
#include "seg.h"
#include "ttlbucket.h"
#include "cc_debug.h"
#include "time/cc_wheel.h"
#include <pthread.h>
#include <sysexits.h>
#include <time.h>
extern volatile bool stop;
extern volatile proc_time_i flush_at;
extern pthread_t bg_tid;
extern struct ttl_bucket ttl_buckets[MAX_N_TTL_BUCKET];
static void
check_seg_expire(void)
{
rstatus_i status;
struct seg *seg;
int32_t seg_id, next_seg_id;
for (int i = 0; i < MAX_N_TTL_BUCKET; i++) {
seg_id = ttl_buckets[i].first_seg_id;
if (seg_id == -1) {
/* no object of this TTL */
continue;
}
seg = &heap.segs[seg_id];
/* curr_sec - 2 to avoid a slow client is still writing to
* the expiring segment */
while (seg->create_at + seg->ttl < time_proc_sec() - 2 ||
seg->create_at < flush_at) {
log_debug("expire seg %"PRId32 ", create at %"PRId32 ", ttl %"PRId32
", flushed at %"PRId32, seg_id, seg->create_at, seg->ttl, flush_at);
next_seg_id = seg->next_seg_id;
status = expire_seg(seg_id);
if (status != CC_OK) {
log_error("error removing expired seg %d", seg_id);
}
if (next_seg_id == -1) {
break;
}
seg_id = next_seg_id;
seg = &heap.segs[seg_id];
}
}
}
static void *
background_main(void *data)
{
#ifdef __APPLE__
pthread_setname_np("segBg");
#else
pthread_setname_np(pthread_self(), "segBg");
#endif
log_info("Segcache background thread started");
while (!stop) {
check_seg_expire();
// do we want to enable background eviction?
// merge_based_eviction();
usleep(200000);
}
log_info("seg background thread stopped");
return NULL;
}
void
start_background_thread(void *arg)
{
int ret = pthread_create(&bg_tid, NULL, background_main, arg);
if (ret != 0) {
log_crit("pthread create failed for background thread: %s",
strerror(ret));
exit(EX_OSERR);
}
}