Skip to content

Commit

Permalink
Use spinlocks for main cache lock
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dormando committed Nov 10, 2011
1 parent 67b6bb1 commit 45e0e95
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
4 changes: 2 additions & 2 deletions assoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion items.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
7 changes: 7 additions & 0 deletions memcached.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
26 changes: 13 additions & 13 deletions thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -340,15 +340,15 @@ 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;
}

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;
Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down

0 comments on commit 45e0e95

Please sign in to comment.