From: Emeric Brun Date: Mon, 24 Sep 2012 13:48:52 +0000 (+0200) Subject: MINOR: ssl: disable shared memory and locks on session cache if nbproc == 1 X-Git-Tag: v1.5-dev13~240 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4b3091e54e29829afad1a2af994ff75c39793e59;p=thirdparty%2Fhaproxy.git MINOR: ssl: disable shared memory and locks on session cache if nbproc == 1 We don't needa to lock the memory when there is a single process. This can make a difference on small systems where locking is much more expensive than just a test. --- diff --git a/include/proto/shctx.h b/include/proto/shctx.h index 000cc05749..862f6a057a 100644 --- a/include/proto/shctx.h +++ b/include/proto/shctx.h @@ -56,9 +56,10 @@ void shctx_sess_add(const unsigned char *session, unsigned int session_len, long /* Allocate shared memory context. * size is maximum cached sessions. * if set less or equal to 0, SHCTX_DEFAULT_SIZE is used. + * use_shared_memory is set to 1 to use a mapped share memory * Returns: -1 on alloc failure, size if it performs context alloc, * and 0 if cache is already allocated */ -int shared_context_init(int size); +int shared_context_init(int size, int use_shared_memory); /* Set shared cache callbacks on an ssl context. * Set session cache mode to server and disable openssl internal cache. diff --git a/src/cfgparse.c b/src/cfgparse.c index 7f03d64141..6fbcd5b106 100644 --- a/src/cfgparse.c +++ b/src/cfgparse.c @@ -6536,7 +6536,7 @@ out_uri_auth_compat: continue; } - if (shared_context_init(global.tune.sslcachesize) < 0) { + if (shared_context_init(global.tune.sslcachesize, (global.nbproc > 1) ? 1 : 0) < 0) { Alert("Unable to allocate SSL session cache.\n"); cfgerr++; continue; diff --git a/src/shctx.c b/src/shctx.c index 48d95418bb..23992a6f26 100644 --- a/src/shctx.c +++ b/src/shctx.c @@ -49,11 +49,11 @@ struct shared_context { /* Static shared context */ static struct shared_context *shctx = NULL; +static int use_shared_mem = 0; /* Callbacks */ static void (*shared_session_new_cbk)(unsigned char *session, unsigned int session_len, long cdate); - /* Lock functions */ #ifdef USE_SYSCALL_FUTEX #if defined (__i586__) || defined (__x86_64__) @@ -106,7 +106,7 @@ static inline unsigned char atomic_dec(unsigned int *ptr) #endif -static inline void shared_context_lock(void) +static inline void _shared_context_lock(void) { unsigned int x; @@ -122,7 +122,7 @@ static inline void shared_context_lock(void) } } -static inline void shared_context_unlock(void) +static inline void _shared_context_unlock(void) { if (atomic_dec(&shctx->waiters)) { shctx->waiters = 0; @@ -130,10 +130,15 @@ static inline void shared_context_unlock(void) } } +#define shared_context_lock(v) if (use_shared_mem) _shared_context_lock() + +#define shared_context_unlock(v) if (use_shared_mem) _shared_context_unlock() + #else /* USE_SYSCALL_FUTEX */ -#define shared_context_lock(v) pthread_mutex_lock(&shctx->mutex) -#define shared_context_unlock(v) pthread_mutex_unlock(&shctx->mutex) +#define shared_context_lock(v) if (use_shared_mem) pthread_mutex_lock(&shctx->mutex) + +#define shared_context_unlock(v) if (use_shared_mem) pthread_mutex_unlock(&shctx->mutex) #endif @@ -357,13 +362,14 @@ void shsess_set_new_cbk(void (*func)(unsigned char *, unsigned int, long)) * if set less or equal to 0, SHCTX_DEFAULT_SIZE is used. * Returns: -1 on alloc failure, size if it performs context alloc, * and 0 if cache is already allocated */ -int shared_context_init(int size) +int shared_context_init(int size, int shared) { int i; #ifndef USE_SYSCALL_FUTEX pthread_mutexattr_t attr; #endif /* USE_SYSCALL_FUTEX */ struct shared_session *prev,*cur; + int maptype = MAP_PRIVATE; if (shctx) return 0; @@ -371,8 +377,11 @@ int shared_context_init(int size) if (size<=0) size = SHCTX_DEFAULT_SIZE; + if (shared) + maptype = MAP_SHARED; + shctx = (struct shared_context *)mmap(NULL, sizeof(struct shared_context)+(size*sizeof(struct shared_session)), - PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); + PROT_READ | PROT_WRITE, maptype | MAP_ANON, -1, 0); if (!shctx || shctx == MAP_FAILED) { shctx = NULL; return -1; @@ -385,6 +394,9 @@ int shared_context_init(int size) pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); pthread_mutex_init(&shctx->mutex, &attr); #endif + if (maptype == MAP_SHARED) + use_shared_mem = 1; + memset(&shctx->active.key, 0, sizeof(struct ebmb_node)); memset(&shctx->free.key, 0, sizeof(struct ebmb_node));