]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: ssl: disable shared memory and locks on session cache if nbproc == 1
authorEmeric Brun <ebrun@exceliance.fr>
Mon, 24 Sep 2012 13:48:52 +0000 (15:48 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 2 Oct 2012 06:34:38 +0000 (08:34 +0200)
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.

include/proto/shctx.h
src/cfgparse.c
src/shctx.c

index 000cc057494f39aae826275bcddfea66fc1a1039..862f6a057a817e8164f5557245bc2e6097b307f7 100644 (file)
@@ -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.
index 7f03d64141590bbff5f2a8fda05a0da5b305d1c5..6fbcd5b106dac758e167e1585ad7449b7ea6e000 100644 (file)
@@ -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;
index 48d95418bb932f4f7ca31e4e96a632b4ffda80bb..23992a6f26192101cf0a428bbc12e46574da52ec 100644 (file)
@@ -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));