1
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
3
* Thread management for memcached.
21
#define ITEMS_PER_ALLOC 64
23
/* An item in the connection queue. */
24
typedef struct conn_queue_item CQ_ITEM;
25
struct conn_queue_item {
34
/* A connection queue. */
35
typedef struct conn_queue CQ;
43
/* Lock for connection freelist */
44
static pthread_mutex_t conn_lock;
46
/* Lock for cache operations (item_*, assoc_*) */
47
static pthread_mutex_t cache_lock;
49
/* Lock for slab allocator operations */
50
static pthread_mutex_t slabs_lock;
52
/* Lock for global stats */
53
static pthread_mutex_t stats_lock;
55
/* Free list of CQ_ITEM structs */
56
static CQ_ITEM *cqi_freelist;
57
static pthread_mutex_t cqi_freelist_lock;
60
* Each libevent instance has a wakeup pipe, which other threads
61
* can use to signal that they've put a new connection on its queue.
64
pthread_t thread_id; /* unique ID of this thread */
65
struct event_base *base; /* libevent handle this thread uses */
66
struct event notify_event; /* listen event for notify pipe */
67
int notify_receive_fd; /* receiving end of notify pipe */
68
int notify_send_fd; /* sending end of notify pipe */
69
CQ new_conn_queue; /* queue of new connections to handle */
72
static LIBEVENT_THREAD *threads;
75
* Number of threads that have finished setting themselves up.
77
static int init_count = 0;
78
static pthread_mutex_t init_lock;
79
static pthread_cond_t init_cond;
82
static void thread_libevent_process(int fd, short which, void *arg);
85
* Initializes a connection queue.
87
static void cq_init(CQ *cq) {
88
pthread_mutex_init(&cq->lock, NULL);
89
pthread_cond_init(&cq->cond, NULL);
95
* Waits for work on a connection queue.
97
static CQ_ITEM *cq_pop(CQ *cq) {
100
pthread_mutex_lock(&cq->lock);
101
while (NULL == cq->head)
102
pthread_cond_wait(&cq->cond, &cq->lock);
104
cq->head = item->next;
105
if (NULL == cq->head)
107
pthread_mutex_unlock(&cq->lock);
113
* Looks for an item on a connection queue, but doesn't block if there isn't
116
static CQ_ITEM *cq_peek(CQ *cq) {
119
pthread_mutex_lock(&cq->lock);
122
cq->head = item->next;
123
if (NULL == cq->head)
126
pthread_mutex_unlock(&cq->lock);
132
* Adds an item to a connection queue.
134
static void cq_push(CQ *cq, CQ_ITEM *item) {
137
pthread_mutex_lock(&cq->lock);
138
if (NULL == cq->tail)
141
cq->tail->next = item;
143
pthread_cond_signal(&cq->cond);
144
pthread_mutex_unlock(&cq->lock);
148
* Returns a fresh connection queue item.
150
static CQ_ITEM *cqi_new() {
151
CQ_ITEM *item = NULL;
152
pthread_mutex_lock(&cqi_freelist_lock);
155
cqi_freelist = item->next;
157
pthread_mutex_unlock(&cqi_freelist_lock);
162
/* Allocate a bunch of items at once to reduce fragmentation */
163
item = malloc(sizeof(CQ_ITEM) * ITEMS_PER_ALLOC);
168
* Link together all the new items except the first one
169
* (which we'll return to the caller) for placement on
172
for (i = 2; i < ITEMS_PER_ALLOC; i++)
173
item[i - 1].next = &item[i];
175
pthread_mutex_lock(&cqi_freelist_lock);
176
item[ITEMS_PER_ALLOC - 1].next = cqi_freelist;
177
cqi_freelist = &item[1];
178
pthread_mutex_unlock(&cqi_freelist_lock);
186
* Frees a connection queue item (adds it to the freelist.)
188
static void cqi_free(CQ_ITEM *item) {
189
pthread_mutex_lock(&cqi_freelist_lock);
190
item->next = cqi_freelist;
192
pthread_mutex_unlock(&cqi_freelist_lock);
197
* Creates a worker thread.
199
static void create_worker(void *(*func)(void *), void *arg) {
204
pthread_attr_init(&attr);
206
if (ret = pthread_create(&thread, &attr, func, arg)) {
207
fprintf(stderr, "Can't create thread: %s\n",
215
* Pulls a conn structure from the freelist, if one is available.
217
conn *mt_conn_from_freelist() {
220
pthread_mutex_lock(&conn_lock);
221
c = do_conn_from_freelist();
222
pthread_mutex_unlock(&conn_lock);
229
* Adds a conn structure to the freelist.
231
* Returns 0 on success, 1 if the structure couldn't be added.
233
int mt_conn_add_to_freelist(conn *c) {
236
pthread_mutex_lock(&conn_lock);
237
result = do_conn_add_to_freelist(c);
238
pthread_mutex_unlock(&conn_lock);
243
/****************************** LIBEVENT THREADS *****************************/
246
* Set up a thread's information.
248
static void setup_thread(LIBEVENT_THREAD *me) {
250
me->base = event_init();
252
fprintf(stderr, "Can't allocate event base\n");
257
/* Listen for notifications from other threads */
258
event_set(&me->notify_event, me->notify_receive_fd,
259
EV_READ | EV_PERSIST, thread_libevent_process, me);
260
event_base_set(me->base, &me->notify_event);
262
if (event_add(&me->notify_event, 0) == -1) {
263
fprintf(stderr, "Can't monitor libevent notify pipe\n");
267
cq_init(&me->new_conn_queue);
272
* Worker thread: main event loop
274
static void *worker_libevent(void *arg) {
275
LIBEVENT_THREAD *me = arg;
277
/* Any per-thread setup can happen here; thread_init() will block until
278
* all threads have finished initializing.
281
pthread_mutex_lock(&init_lock);
283
pthread_cond_signal(&init_cond);
284
pthread_mutex_unlock(&init_lock);
286
event_base_loop(me->base, 0);
291
* Processes an incoming "handle a new connection" item. This is called when
292
* input arrives on the libevent wakeup pipe.
294
static void thread_libevent_process(int fd, short which, void *arg) {
295
LIBEVENT_THREAD *me = arg;
299
if (read(fd, buf, 1) != 1)
300
if (settings.verbose > 0)
301
fprintf(stderr, "Can't read from libevent pipe\n");
303
if (item = cq_peek(&me->new_conn_queue)) {
304
conn *c = conn_new(item->sfd, item->init_state, item->event_flags,
305
item->read_buffer_size, item->is_udp, me->base);
308
fprintf(stderr, "Can't listen for events on UDP socket\n");
312
if (settings.verbose > 0) {
313
fprintf(stderr, "Can't listen for events on fd %d\n",
323
/* Which thread we assigned a connection to most recently. */
324
static int last_thread = -1;
327
* Dispatches a new connection to another thread. This is only ever called
328
* from the main thread, either during initialization (for UDP) or because
329
* of an incoming connection.
331
void dispatch_conn_new(int sfd, int init_state, int event_flags,
332
int read_buffer_size, int is_udp) {
333
CQ_ITEM *item = cqi_new();
334
int thread = (last_thread + 1) % settings.num_threads;
336
last_thread = thread;
339
item->init_state = init_state;
340
item->event_flags = event_flags;
341
item->read_buffer_size = read_buffer_size;
342
item->is_udp = is_udp;
344
cq_push(&threads[thread].new_conn_queue, item);
345
if (write(threads[thread].notify_send_fd, "", 1) != 1) {
346
perror("Writing to thread notify pipe");
351
* Returns true if this is the thread that listens for new TCP connections.
353
int mt_is_listen_thread() {
354
return pthread_self() == threads[0].thread_id;
357
/********************************* ITEM ACCESS *******************************/
360
* Walks through the list of deletes that have been deferred because the items
361
* were locked down at the tmie.
363
void mt_run_deferred_deletes() {
364
pthread_mutex_lock(&cache_lock);
365
do_run_deferred_deletes();
366
pthread_mutex_unlock(&cache_lock);
370
* Allocates a new item.
372
item *mt_item_alloc(char *key, size_t nkey, int flags, rel_time_t exptime, int nbytes) {
374
pthread_mutex_lock(&cache_lock);
375
it = do_item_alloc(key, nkey, flags, exptime, nbytes);
376
pthread_mutex_unlock(&cache_lock);
381
* Returns an item if it hasn't been marked as expired or deleted,
382
* lazy-expiring as needed.
384
item *mt_item_get_notedeleted(char *key, size_t nkey, bool *delete_locked) {
386
pthread_mutex_lock(&cache_lock);
387
it = do_item_get_notedeleted(key, nkey, delete_locked);
388
pthread_mutex_unlock(&cache_lock);
393
* Returns an item whether or not it's been marked as expired or deleted.
395
item *mt_item_get_nocheck(char *key, size_t nkey) {
398
pthread_mutex_lock(&cache_lock);
399
it = assoc_find(key, nkey);
401
pthread_mutex_unlock(&cache_lock);
406
* Links an item into the LRU and hashtable.
408
int mt_item_link(item *item) {
411
pthread_mutex_lock(&cache_lock);
412
ret = do_item_link(item);
413
pthread_mutex_unlock(&cache_lock);
418
* Decrements the reference count on an item and adds it to the freelist if
421
void mt_item_remove(item *item) {
422
pthread_mutex_lock(&cache_lock);
423
do_item_remove(item);
424
pthread_mutex_unlock(&cache_lock);
428
* Replaces one item with another in the hashtable.
430
int mt_item_replace(item *old, item *new) {
433
pthread_mutex_lock(&cache_lock);
434
ret = do_item_replace(old, new);
435
pthread_mutex_unlock(&cache_lock);
440
* Unlinks an item from the LRU and hashtable.
442
void mt_item_unlink(item *item) {
443
pthread_mutex_lock(&cache_lock);
444
do_item_unlink(item);
445
pthread_mutex_unlock(&cache_lock);
449
* Moves an item to the back of the LRU queue.
451
void mt_item_update(item *item) {
452
pthread_mutex_lock(&cache_lock);
453
do_item_update(item);
454
pthread_mutex_unlock(&cache_lock);
458
* Adds an item to the deferred-delete list so it can be reaped later.
460
char *mt_defer_delete(item *item, time_t exptime) {
463
pthread_mutex_lock(&cache_lock);
464
ret = do_defer_delete(item, exptime);
465
pthread_mutex_unlock(&cache_lock);
470
* Does arithmetic on a numeric item value.
472
char *mt_add_delta(item *item, int incr, unsigned int delta, char *buf) {
475
pthread_mutex_lock(&cache_lock);
476
ret = do_add_delta(item, incr, delta, buf);
477
pthread_mutex_unlock(&cache_lock);
482
* Stores an item in the cache (high level, obeys set/add/replace semantics)
484
int mt_store_item(item *item, int comm) {
487
pthread_mutex_lock(&cache_lock);
488
ret = do_store_item(item, comm);
489
pthread_mutex_unlock(&cache_lock);
494
* Flushes expired items after a flush_all call
496
void mt_item_flush_expired() {
497
pthread_mutex_lock(&cache_lock);
498
do_item_flush_expired();
499
pthread_mutex_unlock(&cache_lock);
502
/****************************** HASHTABLE MODULE *****************************/
504
void mt_assoc_move_next_bucket() {
505
pthread_mutex_lock(&cache_lock);
506
do_assoc_move_next_bucket();
507
pthread_mutex_unlock(&cache_lock);
510
/******************************* SLAB ALLOCATOR ******************************/
512
void *mt_slabs_alloc(size_t size) {
515
pthread_mutex_lock(&slabs_lock);
516
ret = do_slabs_alloc(size);
517
pthread_mutex_unlock(&slabs_lock);
521
void mt_slabs_free(void *ptr, size_t size) {
522
pthread_mutex_lock(&slabs_lock);
523
do_slabs_free(ptr, size);
524
pthread_mutex_unlock(&slabs_lock);
527
char *mt_slabs_stats(int *buflen) {
530
pthread_mutex_lock(&slabs_lock);
531
ret = do_slabs_stats(buflen);
532
pthread_mutex_unlock(&slabs_lock);
536
#ifdef ALLOW_SLABS_REASSIGN
537
int mt_slabs_reassign(unsigned char srcid, unsigned char dstid) {
540
pthread_mutex_lock(&slabs_lock);
541
ret = do_slabs_reassign(srcid, dstid);
542
pthread_mutex_unlock(&slabs_lock);
547
/******************************* GLOBAL STATS ******************************/
549
void mt_stats_lock() {
550
pthread_mutex_lock(&stats_lock);
553
void mt_stats_unlock() {
554
pthread_mutex_unlock(&stats_lock);
558
* Initializes the thread subsystem, creating various worker threads.
560
* nthreads Number of event handler threads to spawn
561
* main_base Event base for main thread
563
void thread_init(int nthreads, struct event_base *main_base) {
567
pthread_mutex_init(&cache_lock, NULL);
568
pthread_mutex_init(&conn_lock, NULL);
569
pthread_mutex_init(&slabs_lock, NULL);
570
pthread_mutex_init(&stats_lock, NULL);
572
pthread_mutex_init(&init_lock, NULL);
573
pthread_cond_init(&init_cond, NULL);
575
pthread_mutex_init(&cqi_freelist_lock, NULL);
578
threads = malloc(sizeof(LIBEVENT_THREAD) * nthreads);
580
perror("Can't allocate thread descriptors");
584
threads[0].base = main_base;
585
threads[0].thread_id = pthread_self();
587
for (i = 0; i < nthreads; i++) {
590
perror("Can't create notify pipe");
594
threads[i].notify_receive_fd = fds[0];
595
threads[i].notify_send_fd = fds[1];
597
setup_thread(&threads[i]);
600
/* Create threads after we've done all the libevent setup. */
601
for (i = 1; i < nthreads; i++) {
602
create_worker(worker_libevent, &threads[i]);
605
/* Wait for all the threads to set themselves up before returning. */
606
pthread_mutex_lock(&init_lock);
607
init_count++; // main thread
608
while (init_count < nthreads) {
609
pthread_cond_wait(&init_cond, &init_lock);
611
pthread_mutex_unlock(&init_lock);