From d50c57297aef29b4287b5dedbb3b971e370845b9 Mon Sep 17 00:00:00 2001 From: dormando Date: Sun, 2 Oct 2011 01:21:52 -0700 Subject: Use spinlocks for main cache lock Partly by Ripduman Sohan Appears to significantly help prevent performance dropoff from additional threads, but only when the locks are frequently contested and are short. --- assoc.c | 4 ++-- items.c | 2 +- memcached.h | 7 +++++++ thread.c | 26 +++++++++++++------------- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/assoc.c b/assoc.c index 9500b00..59cbb0f 100644 --- a/assoc.c +++ b/assoc.c @@ -205,7 +205,7 @@ static void *assoc_maintenance_thread(void *arg) { /* Lock the cache, and bulk move multiple buckets to the new * hash table. */ - pthread_mutex_lock(&cache_lock); + mutex_lock(&cache_lock); for (ii = 0; ii < hash_bulk_move && expanding; ++ii) { item *it, *next; @@ -264,7 +264,7 @@ int start_assoc_maintenance_thread() { } void stop_assoc_maintenance_thread() { - pthread_mutex_lock(&cache_lock); + mutex_lock(&cache_lock); do_run_maintenance_thread = 0; pthread_cond_signal(&maintenance_cond); pthread_mutex_unlock(&cache_lock); diff --git a/items.c b/items.c index 50d2c92..d655e5e 100644 --- a/items.c +++ b/items.c @@ -42,7 +42,7 @@ static itemstats_t itemstats[LARGEST_ID]; static unsigned int sizes[LARGEST_ID]; void item_stats_reset(void) { - pthread_mutex_lock(&cache_lock); + mutex_lock(&cache_lock); memset(itemstats, 0, sizeof(itemstats)); pthread_mutex_unlock(&cache_lock); } diff --git a/memcached.h b/memcached.h index c75ef46..628f043 100644 --- a/memcached.h +++ b/memcached.h @@ -464,6 +464,13 @@ enum store_item_type do_store_item(item *item, int comm, conn* c); conn *conn_new(const int sfd, const enum conn_states init_state, const int event_flags, const int read_buffer_size, enum network_transport transport, struct event_base *base); extern int daemonize(int nochdir, int noclose); +static inline int mutex_lock(pthread_mutex_t *mutex) +{ + while (pthread_mutex_trylock(mutex)); + return 0; +} + +#define mutex_unlock(x) pthread_mutex_unlock(x) #include "stats.h" #include "slabs.h" diff --git a/thread.c b/thread.c index 8fe797f..ff1e898 100644 --- a/thread.c +++ b/thread.c @@ -328,7 +328,7 @@ int is_listen_thread() { */ item *item_alloc(char *key, size_t nkey, int flags, rel_time_t exptime, int nbytes) { item *it; - pthread_mutex_lock(&cache_lock); + mutex_lock(&cache_lock); it = do_item_alloc(key, nkey, flags, exptime, nbytes); pthread_mutex_unlock(&cache_lock); return it; @@ -340,7 +340,7 @@ item *item_alloc(char *key, size_t nkey, int flags, rel_time_t exptime, int nbyt */ item *item_get(const char *key, const size_t nkey) { item *it; - pthread_mutex_lock(&cache_lock); + mutex_lock(&cache_lock); it = do_item_get(key, nkey); pthread_mutex_unlock(&cache_lock); return it; @@ -348,7 +348,7 @@ item *item_get(const char *key, const size_t nkey) { item *item_touch(const char *key, size_t nkey, uint32_t exptime) { item *it; - pthread_mutex_lock(&cache_lock); + mutex_lock(&cache_lock); it = do_item_touch(key, nkey, exptime); pthread_mutex_unlock(&cache_lock); return it; @@ -360,7 +360,7 @@ item *item_touch(const char *key, size_t nkey, uint32_t exptime) { int item_link(item *item) { int ret; - pthread_mutex_lock(&cache_lock); + mutex_lock(&cache_lock); ret = do_item_link(item); pthread_mutex_unlock(&cache_lock); return ret; @@ -371,7 +371,7 @@ int item_link(item *item) { * needed. */ void item_remove(item *item) { - pthread_mutex_lock(&cache_lock); + mutex_lock(&cache_lock); do_item_remove(item); pthread_mutex_unlock(&cache_lock); } @@ -389,7 +389,7 @@ int item_replace(item *old_it, item *new_it) { * Unlinks an item from the LRU and hashtable. */ void item_unlink(item *item) { - pthread_mutex_lock(&cache_lock); + mutex_lock(&cache_lock); do_item_unlink(item); pthread_mutex_unlock(&cache_lock); } @@ -398,7 +398,7 @@ void item_unlink(item *item) { * Moves an item to the back of the LRU queue. */ void item_update(item *item) { - pthread_mutex_lock(&cache_lock); + mutex_lock(&cache_lock); do_item_update(item); pthread_mutex_unlock(&cache_lock); } @@ -412,7 +412,7 @@ enum delta_result_type add_delta(conn *c, const char *key, uint64_t *cas) { enum delta_result_type ret; - pthread_mutex_lock(&cache_lock); + mutex_lock(&cache_lock); ret = do_add_delta(c, key, nkey, incr, delta, buf, cas); pthread_mutex_unlock(&cache_lock); return ret; @@ -424,7 +424,7 @@ enum delta_result_type add_delta(conn *c, const char *key, enum store_item_type store_item(item *item, int comm, conn* c) { enum store_item_type ret; - pthread_mutex_lock(&cache_lock); + mutex_lock(&cache_lock); ret = do_store_item(item, comm, c); pthread_mutex_unlock(&cache_lock); return ret; @@ -434,7 +434,7 @@ enum store_item_type store_item(item *item, int comm, conn* c) { * Flushes expired items after a flush_all call */ void item_flush_expired() { - pthread_mutex_lock(&cache_lock); + mutex_lock(&cache_lock); do_item_flush_expired(); pthread_mutex_unlock(&cache_lock); } @@ -445,7 +445,7 @@ void item_flush_expired() { char *item_cachedump(unsigned int slabs_clsid, unsigned int limit, unsigned int *bytes) { char *ret; - pthread_mutex_lock(&cache_lock); + mutex_lock(&cache_lock); ret = do_item_cachedump(slabs_clsid, limit, bytes); pthread_mutex_unlock(&cache_lock); return ret; @@ -455,7 +455,7 @@ char *item_cachedump(unsigned int slabs_clsid, unsigned int limit, unsigned int * Dumps statistics about slab classes */ void item_stats(ADD_STAT add_stats, void *c) { - pthread_mutex_lock(&cache_lock); + mutex_lock(&cache_lock); do_item_stats(add_stats, c); pthread_mutex_unlock(&cache_lock); } @@ -464,7 +464,7 @@ void item_stats(ADD_STAT add_stats, void *c) { * Dumps a list of objects of each size in 32-byte increments */ void item_stats_sizes(ADD_STAT add_stats, void *c) { - pthread_mutex_lock(&cache_lock); + mutex_lock(&cache_lock); do_item_stats_sizes(add_stats, c); pthread_mutex_unlock(&cache_lock); } -- cgit v1.2.1