Skip to content

Commit

Permalink
Refactor: move the slabs mutex (and all usage of it) into slabs.c
Browse files Browse the repository at this point in the history
  • Loading branch information
Trond Norbye authored and Trond Norbye committed Mar 5, 2009
1 parent 7c23e79 commit d9220d6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 58 deletions.
6 changes: 0 additions & 6 deletions memcached.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,6 @@ char *item_stats_sizes(uint32_t (*add_stats)(char *buf,
const uint32_t vlen, void *cookie), void *c, int *bytes);
void item_unlink(item *it);
void item_update(item *it);
void *slabs_alloc(size_t size, unsigned int id);
void slabs_free(void *ptr, size_t size, unsigned int id);
int slabs_reassign(unsigned char srcid, unsigned char dstid);
char *slabs_stats(uint32_t (*add_stats)(char *buf,
const char *key, const uint16_t klen, const char *val,
const uint32_t vlen, void *cookie), void *c, int *buflen);

void STATS_LOCK(void);
void STATS_UNLOCK(void);
Expand Down
54 changes: 49 additions & 5 deletions slabs.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <pthread.h>

#define POWER_SMALLEST 1
#define POWER_LARGEST 200
Expand Down Expand Up @@ -58,6 +59,11 @@ static void *mem_base = NULL;
static void *mem_current = NULL;
static size_t mem_avail = 0;

/**
* Access to the slab allocator is protected by this lock
*/
static pthread_mutex_t slabs_lock = PTHREAD_MUTEX_INITIALIZER;

/*
* Forward Declarations
*/
Expand Down Expand Up @@ -220,7 +226,7 @@ static int do_slabs_newslab(const unsigned int id) {
}

/*@null@*/
void *do_slabs_alloc(const size_t size, unsigned int id) {
static void *do_slabs_alloc(const size_t size, unsigned int id) {
slabclass_t *p;
void *ret = NULL;

Expand Down Expand Up @@ -272,7 +278,7 @@ void *do_slabs_alloc(const size_t size, unsigned int id) {
return ret;
}

void do_slabs_free(void *ptr, const size_t size, unsigned int id) {
static void do_slabs_free(void *ptr, const size_t size, unsigned int id) {
slabclass_t *p;

assert(((item *)ptr)->slabs_clsid == 0);
Expand Down Expand Up @@ -367,9 +373,12 @@ char *get_stats(const char *stat_type, int nkey,
}

/*@null@*/
char *do_slabs_stats(uint32_t (*add_stats)(char *buf,
const char *key, const uint16_t klen, const char *val,
const uint32_t vlen, void *cookie), void *c, int *buflen) {
static char *do_slabs_stats(uint32_t (*add_stats)(char *buf, const char *key,
const uint16_t klen,
const char *val,
const uint32_t vlen,
void *cookie),
void *c, int *buflen) {
int i, total, linelen;
char *buf = (char *)malloc(power_largest * 200 + 100);
char *bufcurr = buf;
Expand Down Expand Up @@ -533,6 +542,15 @@ int do_slabs_reassign(unsigned char srcid, unsigned char dstid) {
}
return 1;
}

int slabs_reassign(unsigned char srcid, unsigned char dstid) {
int ret;

pthread_mutex_lock(&slabs_lock);
ret = do_slabs_reassign(srcid, dstid);
pthread_mutex_unlock(&slabs_lock);
return ret;
}
#endif

static void *memory_allocate(size_t size) {
Expand Down Expand Up @@ -563,3 +581,29 @@ static void *memory_allocate(size_t size) {

return ret;
}

void *slabs_alloc(size_t size, unsigned int id) {
void *ret;

pthread_mutex_lock(&slabs_lock);
ret = do_slabs_alloc(size, id);
pthread_mutex_unlock(&slabs_lock);
return ret;
}

void slabs_free(void *ptr, size_t size, unsigned int id) {
pthread_mutex_lock(&slabs_lock);
do_slabs_free(ptr, size, id);
pthread_mutex_unlock(&slabs_lock);
}

char *slabs_stats(uint32_t (*add_stats)(char *buf,
const char *key, const uint16_t klen, const char *val,
const uint32_t vlen, void *cookie), void *c, int *buflen) {
char *ret;

pthread_mutex_lock(&slabs_lock);
ret = do_slabs_stats(add_stats, c, buflen);
pthread_mutex_unlock(&slabs_lock);
return ret;
}
11 changes: 7 additions & 4 deletions slabs.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* slabs memory allocation */
#ifndef SLABS_H
#define SLABS_H

/** Init the subsystem. 1st argument is the limit on no. of bytes to allocate,
0 if no limit. 2nd argument is the growth factor; each slab will use a chunk
Expand All @@ -17,10 +19,10 @@ void slabs_init(const size_t limit, const double factor, const bool prealloc);
unsigned int slabs_clsid(const size_t size);

/** Allocate object of given length. 0 on error */ /*@null@*/
void *do_slabs_alloc(const size_t size, unsigned int id);
void *slabs_alloc(const size_t size, unsigned int id);

/** Free previously allocated object */
void do_slabs_free(void *ptr, size_t size, unsigned int id);
void slabs_free(void *ptr, size_t size, unsigned int id);

/** Return a datum for stats in binary protocol */
char *get_stats(const char *stat_type, int nkey,
Expand All @@ -29,13 +31,14 @@ char *get_stats(const char *stat_type, int nkey,
const uint32_t vlen, void *cookie), void *arg, int *buflen);

/** Fill buffer with stats */ /*@null@*/
char *do_slabs_stats(uint32_t (*add_stats)(char *buf,
char *slabs_stats(uint32_t (*add_stats)(char *buf,
const char *key, const uint16_t klen, const char *val,
const uint32_t vlen, void *cookie), void *c, int *buflen);

/* Request some slab be moved between classes
1 = success
0 = fail
-1 = tried. busy. send again shortly. */
int do_slabs_reassign(unsigned char srcid, unsigned char dstid);
int slabs_reassign(unsigned char srcid, unsigned char dstid);

#endif
43 changes: 0 additions & 43 deletions thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ static pthread_mutex_t suffix_lock;
/* Lock for cache operations (item_*, assoc_*) */
pthread_mutex_t cache_lock;

/* Lock for slab allocator operations */
static pthread_mutex_t slabs_lock;

/* Lock for global stats */
static pthread_mutex_t stats_lock;

Expand Down Expand Up @@ -524,45 +521,6 @@ char *item_stats_sizes(uint32_t (*add_stats)(char *buf,
return ret;
}

/******************************* SLAB ALLOCATOR ******************************/

void *slabs_alloc(size_t size, unsigned int id) {
void *ret;

pthread_mutex_lock(&slabs_lock);
ret = do_slabs_alloc(size, id);
pthread_mutex_unlock(&slabs_lock);
return ret;
}

void slabs_free(void *ptr, size_t size, unsigned int id) {
pthread_mutex_lock(&slabs_lock);
do_slabs_free(ptr, size, id);
pthread_mutex_unlock(&slabs_lock);
}

char *slabs_stats(uint32_t (*add_stats)(char *buf,
const char *key, const uint16_t klen, const char *val,
const uint32_t vlen, void *cookie), void *c, int *buflen) {
char *ret;

pthread_mutex_lock(&slabs_lock);
ret = do_slabs_stats(add_stats, c, buflen);
pthread_mutex_unlock(&slabs_lock);
return ret;
}

#ifdef ALLOW_SLABS_REASSIGN
int slabs_reassign(unsigned char srcid, unsigned char dstid) {
int ret;

pthread_mutex_lock(&slabs_lock);
ret = do_slabs_reassign(srcid, dstid);
pthread_mutex_unlock(&slabs_lock);
return ret;
}
#endif

/******************************* GLOBAL STATS ******************************/

void STATS_LOCK() {
Expand Down Expand Up @@ -619,7 +577,6 @@ void thread_init(int nthreads, struct event_base *main_base) {

pthread_mutex_init(&cache_lock, NULL);
pthread_mutex_init(&conn_lock, NULL);
pthread_mutex_init(&slabs_lock, NULL);
pthread_mutex_init(&stats_lock, NULL);

pthread_mutex_init(&init_lock, NULL);
Expand Down

0 comments on commit d9220d6

Please sign in to comment.