|
1 | 1 | #include "server.h" |
| 2 | +#include "bio.h" |
| 3 | + |
| 4 | +static int lazyfree_threaded = 1; /* Use a thread to reclaim objects. */ |
2 | 5 |
|
3 | 6 | /* Initialization of the lazy free engine. Must be called only once at server |
4 | 7 | * startup. */ |
@@ -97,7 +100,17 @@ size_t lazyfreeFastStep(void) { |
97 | 100 |
|
98 | 101 | /* Handles slow or fast collection steps. */ |
99 | 102 | size_t lazyfreeStep(int type) { |
100 | | - if (type == LAZYFREE_STEP_FAST) return lazyfreeFastStep(); |
| 103 | + /* Threaded implementaiton: only block for STEP_OOM. */ |
| 104 | + if (lazyfree_threaded) { |
| 105 | + if (type == LAZYFREE_STEP_OOM) |
| 106 | + return bioWaitStepOfType(BIO_LAZY_FREE); |
| 107 | + return 0; |
| 108 | + } |
| 109 | + |
| 110 | + /* Non threaded implementation: free things incrementally avoiding |
| 111 | + * to block. */ |
| 112 | + if (type == LAZYFREE_STEP_FAST || |
| 113 | + type == LAZYFREE_STEP_OOM) return lazyfreeFastStep(); |
101 | 114 |
|
102 | 115 | size_t totalwork = 0; |
103 | 116 | mstime_t end = mstime()+2; |
@@ -130,8 +143,12 @@ int dbAsyncDelete(redisDb *db, robj *key) { |
130 | 143 | /* If releasing the object is too much work, let's put it into the |
131 | 144 | * lazy free list. */ |
132 | 145 | if (free_effort > LAZYFREE_THRESHOLD) { |
133 | | - listAddNodeTail(server.lazyfree_obj,val); |
134 | | - server.lazyfree_elements += free_effort; |
| 146 | + if (lazyfree_threaded) { |
| 147 | + bioCreateBackgroundJob(BIO_LAZY_FREE,val,NULL,NULL); |
| 148 | + } else { |
| 149 | + listAddNodeTail(server.lazyfree_obj,val); |
| 150 | + server.lazyfree_elements += free_effort; |
| 151 | + } |
135 | 152 | dictSetVal(db->dict,de,NULL); |
136 | 153 | } |
137 | 154 | } |
@@ -165,6 +182,9 @@ int lazyfreeCron(struct aeEventLoop *eventLoop, long long id, void *clientData) |
165 | 182 | UNUSED(id); |
166 | 183 | UNUSED(clientData); |
167 | 184 |
|
| 185 | + /* Threaded lazy free does not need a timer, unregister the timer event. */ |
| 186 | + if (lazyfree_threaded) return AE_NOMORE; |
| 187 | + |
168 | 188 | static size_t prev_mem; |
169 | 189 | static int timer_period = 1000; /* Defauls to 1HZ */ |
170 | 190 | static double mem_trend = 0; |
|
0 commit comments