Log In
Log In
Places
All Projects
Status Monitor
Collapse sidebar
zimbra:collaboration:8.7
zimbra-openldap
threadpool.patch
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File threadpool.patch of Package zimbra-openldap
From f230503b108b1c1fd2dec072b0ad5d6785da4968 Mon Sep 17 00:00:00 2001 From: Howard Chu <hyc@openldap.org> Date: Fri, 16 Aug 2013 18:56:31 -0700 Subject: [PATCH] Tweak mutex acquisition Slightly reduce held time. --- servers/slapd/ctxcsn.c | 8 ++++---- servers/slapd/operation.c | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/ldap_pvt_thread.h b/include/ldap_pvt_thread.h index 52d048a..e57caf8 100644 --- a/include/ldap_pvt_thread.h +++ b/include/ldap_pvt_thread.h @@ -214,6 +214,13 @@ ldap_pvt_thread_pool_init LDAP_P(( int max_pending )); LDAP_F( int ) +ldap_pvt_thread_pool_init_q LDAP_P(( + ldap_pvt_thread_pool_t *pool_out, + int max_threads, + int max_pending, + int num_qs )); + +LDAP_F( int ) ldap_pvt_thread_pool_submit LDAP_P(( ldap_pvt_thread_pool_t *pool, ldap_pvt_thread_start_t *start, --- openldap-2.4.36.2z/servers/slapd/bconfig.c.orig 2013-08-16 11:12:59.000000000 -0700 +++ openldap-2.4.36.2z/servers/slapd/bconfig.c 2013-08-19 15:24:50.780122373 -0700 @@ -194,6 +194,7 @@ CFG_ACL_ADD, CFG_SYNC_SUBENTRY, CFG_LTHREADS, + CFG_THREADQS, CFG_LAST }; @@ -655,6 +656,14 @@ #endif "( OLcfgGlAt:66 NAME 'olcThreads' " "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL }, + { "threadqueues", "count", 2, 2, 0, +#ifdef NO_THREADS + ARG_IGNORED, NULL, +#else + ARG_INT|ARG_MAGIC|CFG_THREADQS, &config_generic, +#endif + "( OLcfgGlAt:95 NAME 'olcThreadQueues' " + "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL }, { "timelimit", "limit", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC, &config_timelimit, "( OLcfgGlAt:67 NAME 'olcTimeLimit' " "SYNTAX OMsDirectoryString )", NULL, NULL }, @@ -816,7 +825,8 @@ "olcSecurity $ olcServerID $ olcSizeLimit $ " "olcSockbufMaxIncoming $ olcSockbufMaxIncomingAuth $ " "olcTCPBuffer $ " - "olcThreads $ olcTimeLimit $ olcTLSCACertificateFile $ " + "olcThreads $ olcThreadQueues $ " + "olcTimeLimit $ olcTLSCACertificateFile $ " "olcTLSCACertificatePath $ olcTLSCertificateFile $ " "olcTLSCertificateKeyFile $ olcTLSCipherSuite $ olcTLSCRLCheck $ " "olcTLSRandFile $ olcTLSVerifyClient $ olcTLSDHParamFile $ " @@ -917,6 +927,9 @@ case CFG_THREADS: c->value_int = connection_pool_max; break; + case CFG_THREADQS: + c->value_int = connection_pool_queues; + break; case CFG_TTHREADS: c->value_int = slap_tool_thread_max; break; @@ -1223,6 +1236,7 @@ /* single-valued attrs, no-ops */ case CFG_CONCUR: case CFG_THREADS: + case CFG_THREADQS: case CFG_TTHREADS: case CFG_LTHREADS: case CFG_RO: @@ -1554,6 +1568,20 @@ connection_pool_max = c->value_int; /* save for reference */ break; + case CFG_THREADQS: + if ( c->value_int < 1 ) { + snprintf( c->cr_msg, sizeof( c->cr_msg ), + "threadqueuess=%d smaller than minimum value 1", + c->value_int ); + Debug(LDAP_DEBUG_ANY, "%s: %s.\n", + c->log, c->cr_msg, 0 ); + return 1; + } + if ( slapMode & SLAP_SERVER_MODE ) + ldap_pvt_thread_pool_queues(&connection_pool, c->value_int); + connection_pool_queues = c->value_int; /* save for reference */ + break; + case CFG_TTHREADS: if ( slapMode & SLAP_TOOL_MODE ) ldap_pvt_thread_pool_maxthreads(&connection_pool, c->value_int); diff --git a/servers/slapd/init.c b/servers/slapd/init.c index f134903..26dacd7 100644 --- a/servers/slapd/init.c +++ b/servers/slapd/init.c @@ -59,7 +59,8 @@ BerVarray default_referral = NULL; * global variables that need mutex protection */ ldap_pvt_thread_pool_t connection_pool; -int connection_pool_max = SLAP_MAX_WORKER_THREADS; +int connection_pool_max = SLAP_MAX_WORKER_THREADS; +int connection_pool_queues = 1; int slap_tool_thread_max = 1; slap_counters_t slap_counters, *slap_counters_list; @@ -135,8 +136,8 @@ slap_init( int mode, const char *name ) slap_name = name; - ldap_pvt_thread_pool_init( &connection_pool, - connection_pool_max, 0); + ldap_pvt_thread_pool_init_q( &connection_pool, + connection_pool_max, 0, connection_pool_queues); slap_counters_init( &slap_counters ); diff --git a/servers/slapd/proto-slap.h b/servers/slapd/proto-slap.h index a6a47f0..31c73da 100644 --- a/servers/slapd/proto-slap.h +++ b/servers/slapd/proto-slap.h @@ -2066,6 +2066,7 @@ LDAP_SLAPD_V (time_t) starttime; LDAP_SLAPD_V (ldap_pvt_thread_pool_t) connection_pool; LDAP_SLAPD_V (int) connection_pool_max; +LDAP_SLAPD_V (int) connection_pool_queues; LDAP_SLAPD_V (int) slap_tool_thread_max; LDAP_SLAPD_V (ldap_pvt_thread_mutex_t) entry2str_mutex; -- 1.7.4.2 --- openldap-2.4.42/libraries/libldap_r/tpool.c.orig 2015-08-14 10:25:28.000000000 -0500 +++ openldap-2.4.42/libraries/libldap_r/tpool.c 2015-08-24 13:21:44.517047932 -0500 @@ -32,6 +32,10 @@ #ifndef LDAP_THREAD_HAVE_TPOOL +#ifndef CACHELINE +#define CACHELINE 64 +#endif + /* Thread-specific key with data and optional free function */ typedef struct ldap_int_tpool_key_s { void *ltk_key; @@ -55,15 +59,14 @@ /* Context: thread ID and thread-specific key/data pairs */ typedef struct ldap_int_thread_userctx_s { + struct ldap_int_thread_poolq_s *ltu_pq; ldap_pvt_thread_t ltu_id; ldap_int_tpool_key_t ltu_key[MAXKEYS]; } ldap_int_thread_userctx_t; /* Simple {thread ID -> context} hash table; key=ctx->ltu_id. - * Protected by ldap_pvt_thread_pool_mutex except during pauses, - * when it is read-only (used by pool_purgekey and pool_context). - * Protected by tpool->ltp_mutex during pauses. + * Protected by ldap_pvt_thread_pool_mutex. */ static struct { ldap_int_thread_userctx_t *ctx; @@ -91,18 +94,21 @@ typedef LDAP_STAILQ_HEAD(tcq, ldap_int_thread_task_s) ldap_int_tpool_plist_t; -struct ldap_int_thread_pool_s { - LDAP_STAILQ_ENTRY(ldap_int_thread_pool_s) ltp_next; +struct ldap_int_thread_poolq_s { + void *ltp_free; - /* protect members below, and protect thread_keys[] during pauses */ + struct ldap_int_thread_pool_s *ltp_pool; + + /* protect members below */ ldap_pvt_thread_mutex_t ltp_mutex; - /* not paused and something to do for pool_<wrapper/pause/destroy>() */ + /* not paused and something to do for pool_<wrapper/pause/destroy>() + * Used for normal pool operation, to synch between submitter and + * worker threads. Not used for pauses. In normal operation multiple + * queues can rendezvous without acquiring the main pool lock. + */ ldap_pvt_thread_cond_t ltp_cond; - /* ltp_active_count <= 1 && ltp_pause */ - ldap_pvt_thread_cond_t ltp_pcond; - /* ltp_pause == 0 ? <p_pending_list : &empty_pending_list, * maintaned to reduce work for pool_wrapper() */ @@ -112,6 +118,41 @@ ldap_int_tpool_plist_t ltp_pending_list; LDAP_SLIST_HEAD(tcl, ldap_int_thread_task_s) ltp_free_list; + /* Max number of threads in this queue */ + int ltp_max_count; + + /* Max pending + paused + idle tasks, negated when ltp_finishing */ + int ltp_max_pending; + + int ltp_pending_count; /* Pending + paused + idle tasks */ + int ltp_active_count; /* Active, not paused/idle tasks */ + int ltp_open_count; /* Number of threads */ + int ltp_starting; /* Currently starting threads */ +}; + +struct ldap_int_thread_pool_s { + LDAP_STAILQ_ENTRY(ldap_int_thread_pool_s) ltp_next; + + struct ldap_int_thread_poolq_s **ltp_wqs; + + /* number of poolqs */ + int ltp_numqs; + + /* protect members below */ + ldap_pvt_thread_mutex_t ltp_mutex; + + /* paused and waiting for resume + * When a pause is in effect all workers switch to waiting on + * this cond instead of their per-queue cond. + */ + ldap_pvt_thread_cond_t ltp_cond; + + /* ltp_active_queues < 1 && ltp_pause */ + ldap_pvt_thread_cond_t ltp_pcond; + + /* number of active queues */ + int ltp_active_queues; + /* The pool is finishing, waiting for its threads to close. * They close when ltp_pending_list is done. pool_submit() * rejects new tasks. ltp_max_pending = -(its old value). @@ -120,34 +161,17 @@ /* Some active task needs to be the sole active task. * Atomic variable so ldap_pvt_thread_pool_pausing() can read it. - * Note: Pauses adjust ltp_<open_count/vary_open_count/work_list>, - * so pool_<submit/wrapper>() mostly can avoid testing ltp_pause. */ volatile sig_atomic_t ltp_pause; - /* Max number of threads in pool, or 0 for default (LDAP_MAXTHR) */ + /* Max number of threads in pool */ int ltp_max_count; + /* Configured max number of threads in pool, 0 for default (LDAP_MAXTHR) */ + int ltp_conf_max_count; + /* Max pending + paused + idle tasks, negated when ltp_finishing */ int ltp_max_pending; - - int ltp_pending_count; /* Pending + paused + idle tasks */ - int ltp_active_count; /* Active, not paused/idle tasks */ - int ltp_open_count; /* Number of threads, negated when ltp_pause */ - int ltp_starting; /* Currenlty starting threads */ - - /* >0 if paused or we may open a thread, <0 if we should close a thread. - * Updated when ltp_<finishing/pause/max_count/open_count> change. - * Maintained to reduce the time ltp_mutex must be locked in - * ldap_pvt_thread_pool_<submit/wrapper>(). - */ - int ltp_vary_open_count; -# define SET_VARY_OPEN_COUNT(pool) \ - ((pool)->ltp_vary_open_count = \ - (pool)->ltp_pause ? 1 : \ - (pool)->ltp_finishing ? -1 : \ - ((pool)->ltp_max_count ? (pool)->ltp_max_count : LDAP_MAXTHR) \ - - (pool)->ltp_open_count) }; static ldap_int_tpool_plist_t empty_pending_list = @@ -191,13 +215,15 @@ /* Create a thread pool */ int -ldap_pvt_thread_pool_init ( +ldap_pvt_thread_pool_init_q ( ldap_pvt_thread_pool_t *tpool, int max_threads, - int max_pending ) + int max_pending, + int numqs ) { ldap_pvt_thread_pool_t pool; - int rc; + struct ldap_int_thread_poolq_s *pq; + int i, rc, rem_thr, rem_pend; /* multiple pools are currently not supported (ITS#4943) */ assert(!ldap_int_has_thread_pool); @@ -213,34 +239,80 @@ if (pool == NULL) return(-1); + pool->ltp_wqs = LDAP_MALLOC(numqs * sizeof(struct ldap_int_thread_poolq_s *)); + if (pool->ltp_wqs == NULL) { + LDAP_FREE(pool); + return(-1); + } + + for (i=0; i<numqs; i++) { + char *ptr = LDAP_CALLOC(1, sizeof(struct ldap_int_thread_poolq_s) + CACHELINE-1); + if (ptr == NULL) { + for (--i; i>=0; i--) + LDAP_FREE(pool->ltp_wqs[i]->ltp_free); + LDAP_FREE(pool->ltp_wqs); + LDAP_FREE(pool); + return(-1); + } + pool->ltp_wqs[i] = (struct ldap_int_thread_poolq_s *)(((size_t)ptr + CACHELINE-1) & ~(CACHELINE-1)); + pool->ltp_wqs[i]->ltp_free = ptr; + } + + pool->ltp_numqs = numqs; + pool->ltp_conf_max_count = max_threads; + if ( !max_threads ) + max_threads = LDAP_MAXTHR; + rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex); if (rc != 0) { -fail1: +fail: + for (i=0; i<numqs; i++) + LDAP_FREE(pool->ltp_wqs[i]->ltp_free); + LDAP_FREE(pool->ltp_wqs); LDAP_FREE(pool); return(rc); } + rc = ldap_pvt_thread_cond_init(&pool->ltp_cond); - if (rc != 0) { -fail2: - ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex); - goto fail1; - } + if (rc != 0) + goto fail; + rc = ldap_pvt_thread_cond_init(&pool->ltp_pcond); - if (rc != 0) { - ldap_pvt_thread_cond_destroy(&pool->ltp_cond); - goto fail2; + if (rc != 0) + goto fail; + + rem_thr = max_threads % numqs; + rem_pend = max_pending % numqs; + for ( i=0; i<numqs; i++ ) { + pq = pool->ltp_wqs[i]; + pq->ltp_pool = pool; + rc = ldap_pvt_thread_mutex_init(&pq->ltp_mutex); + if (rc != 0) + return(rc); + rc = ldap_pvt_thread_cond_init(&pq->ltp_cond); + if (rc != 0) + return(rc); + LDAP_STAILQ_INIT(&pq->ltp_pending_list); + pq->ltp_work_list = &pq->ltp_pending_list; + LDAP_SLIST_INIT(&pq->ltp_free_list); + + pq->ltp_max_count = max_threads / numqs; + if ( rem_thr ) { + pq->ltp_max_count++; + rem_thr--; + } + pq->ltp_max_pending = max_pending / numqs; + if ( rem_pend ) { + pq->ltp_max_pending++; + rem_pend--; + } } ldap_int_has_thread_pool = 1; pool->ltp_max_count = max_threads; - SET_VARY_OPEN_COUNT(pool); pool->ltp_max_pending = max_pending; - LDAP_STAILQ_INIT(&pool->ltp_pending_list); - pool->ltp_work_list = &pool->ltp_pending_list; - LDAP_SLIST_INIT(&pool->ltp_free_list); - ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex); LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next); ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex); @@ -256,6 +328,28 @@ return(0); } +int +ldap_pvt_thread_pool_init ( + ldap_pvt_thread_pool_t *tpool, + int max_threads, + int max_pending ) +{ + return ldap_pvt_thread_pool_init_q( tpool, max_threads, max_pending, 1 ); +} + +static int +ldap_int_poolq_hash( + struct ldap_int_thread_pool_s *pool, + void *arg ) +{ + int i = 0, j; + unsigned char *ptr = (unsigned char *)&arg; + /* dumb hash of arg to choose a queue */ + for (j=0; j<sizeof(arg); j++) + i += *ptr++; + i %= pool->ltp_numqs; + return i; +} /* Submit a task to be performed by the thread pool */ int @@ -264,8 +358,10 @@ ldap_pvt_thread_start_t *start_routine, void *arg ) { struct ldap_int_thread_pool_s *pool; + struct ldap_int_thread_poolq_s *pq; ldap_int_thread_task_t *task; ldap_pvt_thread_t thr; + int i, j; if (tpool == NULL) return(-1); @@ -275,14 +371,28 @@ if (pool == NULL) return(-1); - ldap_pvt_thread_mutex_lock(&pool->ltp_mutex); - - if (pool->ltp_pending_count >= pool->ltp_max_pending) - goto failed; + if ( pool->ltp_numqs > 1 ) + i = ldap_int_poolq_hash( pool, arg ); + else + i = 0; + + j = i; + while(1) { + ldap_pvt_thread_mutex_lock(&pool->ltp_wqs[i]->ltp_mutex); + if (pool->ltp_wqs[i]->ltp_pending_count < pool->ltp_wqs[i]->ltp_max_pending) { + break; + } + ldap_pvt_thread_mutex_unlock(&pool->ltp_wqs[i]->ltp_mutex); + i++; + i %= pool->ltp_numqs; + if ( i == j ) + return -1; + } - task = LDAP_SLIST_FIRST(&pool->ltp_free_list); + pq = pool->ltp_wqs[i]; + task = LDAP_SLIST_FIRST(&pq->ltp_free_list); if (task) { - LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltt_next.l); + LDAP_SLIST_REMOVE_HEAD(&pq->ltp_free_list, ltt_next.l); } else { task = (ldap_int_thread_task_t *) LDAP_MALLOC(sizeof(*task)); if (task == NULL) @@ -292,49 +402,47 @@ task->ltt_start_routine = start_routine; task->ltt_arg = arg; - pool->ltp_pending_count++; - LDAP_STAILQ_INSERT_TAIL(&pool->ltp_pending_list, task, ltt_next.q); + pq->ltp_pending_count++; + LDAP_STAILQ_INSERT_TAIL(&pq->ltp_pending_list, task, ltt_next.q); - /* true if ltp_pause != 0 or we should open (create) a thread */ - if (pool->ltp_vary_open_count > 0 && - pool->ltp_open_count < pool->ltp_active_count+pool->ltp_pending_count) - { - if (pool->ltp_pause) - goto done; + if (pool->ltp_pause) + goto done; - pool->ltp_starting++; - pool->ltp_open_count++; - SET_VARY_OPEN_COUNT(pool); + /* should we open (create) a thread? */ + if (pq->ltp_open_count < pq->ltp_active_count+pq->ltp_pending_count && + pq->ltp_open_count < pq->ltp_max_count) + { + pq->ltp_starting++; + pq->ltp_open_count++; if (0 != ldap_pvt_thread_create( - &thr, 1, ldap_int_thread_pool_wrapper, pool)) + &thr, 1, ldap_int_thread_pool_wrapper, pq)) { /* couldn't create thread. back out of * ltp_open_count and check for even worse things. */ - pool->ltp_starting--; - pool->ltp_open_count--; - SET_VARY_OPEN_COUNT(pool); + pq->ltp_starting--; + pq->ltp_open_count--; - if (pool->ltp_open_count == 0) { + if (pq->ltp_open_count == 0) { /* no open threads at all?!? */ ldap_int_thread_task_t *ptr; /* let pool_destroy know there are no more threads */ - ldap_pvt_thread_cond_signal(&pool->ltp_cond); + ldap_pvt_thread_cond_signal(&pq->ltp_cond); - LDAP_STAILQ_FOREACH(ptr, &pool->ltp_pending_list, ltt_next.q) + LDAP_STAILQ_FOREACH(ptr, &pq->ltp_pending_list, ltt_next.q) if (ptr == task) break; if (ptr == task) { /* no open threads, task not handled, so * back out of ltp_pending_count, free the task, * report the error. */ - pool->ltp_pending_count--; - LDAP_STAILQ_REMOVE(&pool->ltp_pending_list, task, + pq->ltp_pending_count--; + LDAP_STAILQ_REMOVE(&pq->ltp_pending_list, task, ldap_int_thread_task_s, ltt_next.q); - LDAP_SLIST_INSERT_HEAD(&pool->ltp_free_list, task, + LDAP_SLIST_INSERT_HEAD(&pq->ltp_free_list, task, ltt_next.l); goto failed; } @@ -344,14 +452,14 @@ */ } } - ldap_pvt_thread_cond_signal(&pool->ltp_cond); + ldap_pvt_thread_cond_signal(&pq->ltp_cond); done: - ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); + ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex); return(0); failed: - ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); + ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex); return(-1); } @@ -371,7 +479,9 @@ ldap_pvt_thread_start_t *start_routine, void *arg ) { struct ldap_int_thread_pool_s *pool; + struct ldap_int_thread_poolq_s *pq; ldap_int_thread_task_t *task; + int i; if (tpool == NULL) return(-1); @@ -381,8 +491,11 @@ if (pool == NULL) return(-1); - ldap_pvt_thread_mutex_lock(&pool->ltp_mutex); - LDAP_STAILQ_FOREACH(task, &pool->ltp_pending_list, ltt_next.q) + i = ldap_int_poolq_hash( pool, arg ); + pq = pool->ltp_wqs[i]; + + ldap_pvt_thread_mutex_lock(&pq->ltp_mutex); + LDAP_STAILQ_FOREACH(task, &pq->ltp_pending_list, ltt_next.q) if (task->ltt_start_routine == start_routine && task->ltt_arg == arg) { /* Could LDAP_STAILQ_REMOVE the task, but that @@ -392,10 +505,79 @@ task->ltt_arg = NULL; break; } - ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); + ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex); return task != NULL; } +/* Set number of work queues in this pool. Should not be + * more than the number of CPUs. */ +int +ldap_pvt_thread_pool_queues( + ldap_pvt_thread_pool_t *tpool, + int numqs ) +{ + struct ldap_int_thread_pool_s *pool; + struct ldap_int_thread_poolq_s *pq; + int i, rc, rem_thr, rem_pend; + + if (numqs < 1 || tpool == NULL) + return(-1); + + pool = *tpool; + + if (pool == NULL) + return(-1); + + if (numqs < pool->ltp_numqs) { + for (i=numqs; i<pool->ltp_numqs; i++) + pool->ltp_wqs[i]->ltp_max_count = 0; + } else if (numqs > pool->ltp_numqs) { + struct ldap_int_thread_poolq_s **wqs; + wqs = LDAP_REALLOC(pool->ltp_wqs, numqs * sizeof(struct ldap_int_thread_poolq_s *)); + if (wqs == NULL) + return(-1); + pool->ltp_wqs = wqs; + for (i=pool->ltp_numqs; i<numqs; i++) { + char *ptr = LDAP_CALLOC(1, sizeof(struct ldap_int_thread_poolq_s) + CACHELINE-1); + if (ptr == NULL) { + for (; i<numqs; i++) + pool->ltp_wqs[i] = NULL; + return(-1); + } + pq = (struct ldap_int_thread_poolq_s *)(((size_t)ptr + CACHELINE-1) & ~(CACHELINE-1)); + pq->ltp_free = ptr; + pool->ltp_wqs[i] = pq; + pq->ltp_pool = pool; + rc = ldap_pvt_thread_mutex_init(&pq->ltp_mutex); + if (rc != 0) + return(rc); + rc = ldap_pvt_thread_cond_init(&pq->ltp_cond); + if (rc != 0) + return(rc); + LDAP_STAILQ_INIT(&pq->ltp_pending_list); + pq->ltp_work_list = &pq->ltp_pending_list; + LDAP_SLIST_INIT(&pq->ltp_free_list); + } + } + rem_thr = pool->ltp_max_count % numqs; + rem_pend = pool->ltp_max_pending % numqs; + for ( i=0; i<numqs; i++ ) { + pq = pool->ltp_wqs[i]; + pq->ltp_max_count = pool->ltp_max_count / numqs; + if ( rem_thr ) { + pq->ltp_max_count++; + rem_thr--; + } + pq->ltp_max_pending = pool->ltp_max_pending / numqs; + if ( rem_pend ) { + pq->ltp_max_pending++; + rem_pend--; + } + } + pool->ltp_numqs = numqs; + return 0; +} + /* Set max #threads. value <= 0 means max supported #threads (LDAP_MAXTHR) */ int ldap_pvt_thread_pool_maxthreads( @@ -403,6 +585,8 @@ int max_threads ) { struct ldap_int_thread_pool_s *pool; + struct ldap_int_thread_poolq_s *pq; + int remthr, i; if (! (0 <= max_threads && max_threads <= LDAP_MAXTHR)) max_threads = 0; @@ -415,12 +599,22 @@ if (pool == NULL) return(-1); - ldap_pvt_thread_mutex_lock(&pool->ltp_mutex); - + pool->ltp_conf_max_count = max_threads; + if ( !max_threads ) + max_threads = LDAP_MAXTHR; pool->ltp_max_count = max_threads; - SET_VARY_OPEN_COUNT(pool); - ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); + remthr = max_threads % pool->ltp_numqs; + max_threads /= pool->ltp_numqs; + + for (i=0; i<pool->ltp_numqs; i++) { + pq = pool->ltp_wqs[i]; + pq->ltp_max_count = max_threads; + if (remthr) { + pq->ltp_max_count++; + remthr--; + } + } return(0); } @@ -444,10 +638,9 @@ return 0; } - ldap_pvt_thread_mutex_lock(&pool->ltp_mutex); switch ( param ) { case LDAP_PVT_THREAD_POOL_PARAM_MAX: - count = pool->ltp_max_count; + count = pool->ltp_conf_max_count; break; case LDAP_PVT_THREAD_POOL_PARAM_MAX_PENDING: @@ -458,30 +651,45 @@ count = 0; break; - case LDAP_PVT_THREAD_POOL_PARAM_OPEN: - count = pool->ltp_open_count; - if (count < 0) - count = -count; - break; - - case LDAP_PVT_THREAD_POOL_PARAM_STARTING: - count = pool->ltp_starting; - break; - - case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE: - count = pool->ltp_active_count; - break; - case LDAP_PVT_THREAD_POOL_PARAM_PAUSING: + ldap_pvt_thread_mutex_lock(&pool->ltp_mutex); count = (pool->ltp_pause != 0); + ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); break; + case LDAP_PVT_THREAD_POOL_PARAM_OPEN: + case LDAP_PVT_THREAD_POOL_PARAM_STARTING: + case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE: case LDAP_PVT_THREAD_POOL_PARAM_PENDING: - count = pool->ltp_pending_count; - break; - case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD: - count = pool->ltp_pending_count + pool->ltp_active_count; + { + int i; + count = 0; + for (i=0; i<pool->ltp_numqs; i++) { + struct ldap_int_thread_poolq_s *pq = pool->ltp_wqs[i]; + ldap_pvt_thread_mutex_lock(&pq->ltp_mutex); + switch(param) { + case LDAP_PVT_THREAD_POOL_PARAM_OPEN: + count += pq->ltp_open_count; + break; + case LDAP_PVT_THREAD_POOL_PARAM_STARTING: + count += pq->ltp_starting; + break; + case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE: + count += pq->ltp_active_count; + break; + case LDAP_PVT_THREAD_POOL_PARAM_PENDING: + count += pq->ltp_pending_count; + break; + case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD: + count += pq->ltp_pending_count + pq->ltp_active_count; + break; + } + ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex); + } + if (count < 0) + count = -count; + } break; case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE_MAX: @@ -494,16 +702,24 @@ break; case LDAP_PVT_THREAD_POOL_PARAM_STATE: - *((char **)value) = - pool->ltp_pause ? "pausing" : - !pool->ltp_finishing ? "running" : - pool->ltp_pending_count ? "finishing" : "stopping"; + if (pool->ltp_pause) + *((char **)value) = "pausing"; + else if (!pool->ltp_finishing) + *((char **)value) = "running"; + else { + int i; + for (i=0; i<pool->ltp_numqs; i++) + if (pool->ltp_wqs[i]->ltp_pending_count) break; + if (i<pool->ltp_numqs) + *((char **)value) = "finishing"; + else + *((char **)value) = "stopping"; + } break; case LDAP_PVT_THREAD_POOL_PARAM_UNKNOWN: break; } - ldap_pvt_thread_mutex_unlock( &pool->ltp_mutex ); if ( count > -1 ) { *((int *)value) = count; @@ -553,7 +769,9 @@ ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending ) { struct ldap_int_thread_pool_s *pool, *pptr; + struct ldap_int_thread_poolq_s *pq; ldap_int_thread_task_t *task; + int i; if (tpool == NULL) return(-1); @@ -575,34 +793,50 @@ ldap_pvt_thread_mutex_lock(&pool->ltp_mutex); pool->ltp_finishing = 1; - SET_VARY_OPEN_COUNT(pool); if (pool->ltp_max_pending > 0) pool->ltp_max_pending = -pool->ltp_max_pending; - if (!run_pending) { - while ((task = LDAP_STAILQ_FIRST(&pool->ltp_pending_list)) != NULL) { - LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltt_next.q); - LDAP_FREE(task); + ldap_pvt_thread_cond_broadcast(&pool->ltp_cond); + ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); + + for (i=0; i<pool->ltp_numqs; i++) { + pq = pool->ltp_wqs[i]; + ldap_pvt_thread_mutex_lock(&pq->ltp_mutex); + if (pq->ltp_max_pending > 0) + pq->ltp_max_pending = -pq->ltp_max_pending; + if (!run_pending) { + while ((task = LDAP_STAILQ_FIRST(&pq->ltp_pending_list)) != NULL) { + LDAP_STAILQ_REMOVE_HEAD(&pq->ltp_pending_list, ltt_next.q); + LDAP_FREE(task); + } + pq->ltp_pending_count = 0; } - pool->ltp_pending_count = 0; - } - while (pool->ltp_open_count) { - if (!pool->ltp_pause) - ldap_pvt_thread_cond_broadcast(&pool->ltp_cond); - ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex); - } + while (pq->ltp_open_count) { + ldap_pvt_thread_cond_broadcast(&pq->ltp_cond); + ldap_pvt_thread_cond_wait(&pq->ltp_cond, &pq->ltp_mutex); + } - while ((task = LDAP_SLIST_FIRST(&pool->ltp_free_list)) != NULL) - { - LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltt_next.l); - LDAP_FREE(task); + while ((task = LDAP_SLIST_FIRST(&pq->ltp_free_list)) != NULL) + { + LDAP_SLIST_REMOVE_HEAD(&pq->ltp_free_list, ltt_next.l); + LDAP_FREE(task); + } + ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex); + ldap_pvt_thread_cond_destroy(&pq->ltp_cond); + ldap_pvt_thread_mutex_destroy(&pq->ltp_mutex); } - ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); ldap_pvt_thread_cond_destroy(&pool->ltp_pcond); ldap_pvt_thread_cond_destroy(&pool->ltp_cond); ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex); + for (i=0; i<pool->ltp_numqs; i++) { + pq = pool->ltp_wqs[i]; + if (pq->ltp_free) { + LDAP_FREE(pq->ltp_free); + } + } + LDAP_FREE(pool->ltp_wqs); LDAP_FREE(pool); *tpool = NULL; ldap_int_has_thread_pool = 0; @@ -614,11 +848,13 @@ ldap_int_thread_pool_wrapper ( void *xpool ) { - struct ldap_int_thread_pool_s *pool = xpool; + struct ldap_int_thread_poolq_s *pq = xpool; + struct ldap_int_thread_pool_s *pool = pq->ltp_pool; ldap_int_thread_task_t *task; ldap_int_tpool_plist_t *work_list; ldap_int_thread_userctx_t ctx, *kctx; unsigned i, keyslot, hash; + int pool_lock = 0, freeme = 0; assert(pool != NULL); @@ -626,16 +862,19 @@ ctx.ltu_key[i].ltk_key = NULL; } + ctx.ltu_pq = pq; ctx.ltu_id = ldap_pvt_thread_self(); TID_HASH(ctx.ltu_id, hash); ldap_pvt_thread_key_setdata( ldap_tpool_key, &ctx ); - ldap_pvt_thread_mutex_lock(&pool->ltp_mutex); - - /* thread_keys[] is read-only when paused */ - while (pool->ltp_pause) - ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex); + if (pool->ltp_pause) { + ldap_pvt_thread_mutex_lock(&pool->ltp_mutex); + /* thread_keys[] is read-only when paused */ + while (pool->ltp_pause) + ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex); + ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); + } /* find a key slot to give this thread ID and store a * pointer to our keys there; start at the thread ID @@ -648,20 +887,28 @@ thread_keys[keyslot].ctx = &ctx; ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex); - pool->ltp_starting--; - pool->ltp_active_count++; + ldap_pvt_thread_mutex_lock(&pq->ltp_mutex); + pq->ltp_starting--; + pq->ltp_active_count++; for (;;) { - work_list = pool->ltp_work_list; /* help the compiler a bit */ + work_list = pq->ltp_work_list; /* help the compiler a bit */ task = LDAP_STAILQ_FIRST(work_list); if (task == NULL) { /* paused or no pending tasks */ - if (--(pool->ltp_active_count) < 2) { - /* Notify pool_pause it is the sole active thread. */ - ldap_pvt_thread_cond_signal(&pool->ltp_pcond); + if (--(pq->ltp_active_count) < 1) { + if (pool->ltp_pause) { + ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex); + ldap_pvt_thread_mutex_lock(&pool->ltp_mutex); + pool_lock = 1; + if (--(pool->ltp_active_queues) < 1) { + /* Notify pool_pause it is the sole active thread. */ + ldap_pvt_thread_cond_signal(&pool->ltp_pcond); + } + } } do { - if (pool->ltp_vary_open_count < 0) { + if (pool->ltp_finishing || pq->ltp_open_count > pq->ltp_max_count) { /* Not paused, and either finishing or too many * threads running (can happen if ltp_max_count * was reduced). Let this thread die. @@ -680,44 +927,68 @@ * Just use pthread_cond_timedwait() if we want to * check idle time. */ - ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex); + if (pool_lock) { + ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex); + if (!pool->ltp_pause) { + ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); + ldap_pvt_thread_mutex_lock(&pq->ltp_mutex); + pool_lock = 0; + } + } else + ldap_pvt_thread_cond_wait(&pq->ltp_cond, &pq->ltp_mutex); - work_list = pool->ltp_work_list; + work_list = pq->ltp_work_list; task = LDAP_STAILQ_FIRST(work_list); } while (task == NULL); - pool->ltp_active_count++; + if (pool_lock) { + ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); + ldap_pvt_thread_mutex_lock(&pq->ltp_mutex); + pool_lock = 0; + } + pq->ltp_active_count++; } LDAP_STAILQ_REMOVE_HEAD(work_list, ltt_next.q); - pool->ltp_pending_count--; - ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); + pq->ltp_pending_count--; + ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex); task->ltt_start_routine(&ctx, task->ltt_arg); - ldap_pvt_thread_mutex_lock(&pool->ltp_mutex); - LDAP_SLIST_INSERT_HEAD(&pool->ltp_free_list, task, ltt_next.l); + ldap_pvt_thread_mutex_lock(&pq->ltp_mutex); + LDAP_SLIST_INSERT_HEAD(&pq->ltp_free_list, task, ltt_next.l); } done: - assert(!pool->ltp_pause); /* thread_keys writable, ltp_open_count >= 0 */ + ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex); - /* The ltp_mutex lock protects ctx->ltu_key from pool_purgekey() + /* The pool_mutex lock protects ctx->ltu_key from pool_purgekey() * during this call, since it prevents new pauses. */ ldap_pvt_thread_pool_context_reset(&ctx); - ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex); thread_keys[keyslot].ctx = DELETED_THREAD_CTX; ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex); - pool->ltp_open_count--; - SET_VARY_OPEN_COUNT(pool); - /* let pool_destroy know we're all done */ - if (pool->ltp_open_count == 0) - ldap_pvt_thread_cond_signal(&pool->ltp_cond); + pq->ltp_open_count--; + if (pq->ltp_open_count == 0) { + if (pool->ltp_finishing) + /* let pool_destroy know we're all done */ + ldap_pvt_thread_cond_signal(&pq->ltp_cond); + else + freeme = 1; + } - ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); + if (pool_lock) + ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); + else + ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex); + if (freeme) { + ldap_pvt_thread_cond_destroy(&pq->ltp_cond); + ldap_pvt_thread_mutex_destroy(&pq->ltp_mutex); + LDAP_FREE(pq->ltp_free); + pq->ltp_free = NULL; + } ldap_pvt_thread_exit(NULL); return(NULL); } @@ -736,6 +1007,7 @@ handle_pause( ldap_pvt_thread_pool_t *tpool, int pause_type ) { struct ldap_int_thread_pool_s *pool; + struct ldap_int_thread_poolq_s *pq; int ret = 0, pause, max_ltp_pause; if (tpool == NULL) @@ -749,6 +1021,11 @@ if (pause_type == CHECK_PAUSE && !pool->ltp_pause) return(0); + { + ldap_int_thread_userctx_t *ctx = ldap_pvt_thread_pool_context(); + pq = ctx->ltu_pq; + } + /* Let pool_unidle() ignore requests for new pauses */ max_ltp_pause = pause_type==PAUSE_ARG(GO_UNIDLE) ? WANT_PAUSE : NOT_PAUSED; @@ -760,11 +1037,19 @@ pause_type -= pause; if (pause_type & GO_IDLE) { - pool->ltp_pending_count++; - pool->ltp_active_count--; - if (pause && pool->ltp_active_count < 2) { + int do_pool = 0; + ldap_pvt_thread_mutex_lock(&pq->ltp_mutex); + pq->ltp_pending_count++; + pq->ltp_active_count--; + if (pause && pq->ltp_active_count < 1) { + do_pool = 1; + } + ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex); + if (do_pool) { + pool->ltp_active_queues--; + if (pool->ltp_active_queues < 1) /* Tell the task waiting to DO_PAUSE it can proceed */ - ldap_pvt_thread_cond_signal(&pool->ltp_pcond); + ldap_pvt_thread_cond_signal(&pool->ltp_pcond); } } @@ -776,30 +1061,58 @@ ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex); } while (pool->ltp_pause > max_ltp_pause); } - pool->ltp_pending_count--; - pool->ltp_active_count++; + ldap_pvt_thread_mutex_lock(&pq->ltp_mutex); + pq->ltp_pending_count--; + pq->ltp_active_count++; + ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex); } if (pause_type & DO_PAUSE) { + int i, j; /* Tell everyone else to pause or finish, then await that */ ret = 0; assert(!pool->ltp_pause); pool->ltp_pause = WANT_PAUSE; - /* Let ldap_pvt_thread_pool_submit() through to its ltp_pause test, - * and do not finish threads in ldap_pvt_thread_pool_wrapper() */ - pool->ltp_open_count = -pool->ltp_open_count; - SET_VARY_OPEN_COUNT(pool); - /* Hide pending tasks from ldap_pvt_thread_pool_wrapper() */ - pool->ltp_work_list = &empty_pending_list; + pool->ltp_active_queues = 0; + + for (i=0; i<pool->ltp_numqs; i++) + if (pool->ltp_wqs[i] == pq) break; + + ldap_pvt_thread_mutex_lock(&pq->ltp_mutex); + /* temporarily remove ourself from active count */ + pq->ltp_active_count--; + + j=i; + do { + pq = pool->ltp_wqs[j]; + if (j != i) + ldap_pvt_thread_mutex_lock(&pq->ltp_mutex); + + /* Hide pending tasks from ldap_pvt_thread_pool_wrapper() */ + pq->ltp_work_list = &empty_pending_list; + + if (pq->ltp_active_count > 0) + pool->ltp_active_queues++; + + ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex); + if (pool->ltp_numqs > 1) { + j++; + j %= pool->ltp_numqs; + } + } while (j != i); + /* Wait for this task to become the sole active task */ - while (pool->ltp_active_count > 1) { + while (pool->ltp_active_queues > 0) ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex); - } + + /* restore us to active count */ + pool->ltp_wqs[i]->ltp_active_count++; + assert(pool->ltp_pause == WANT_PAUSE); pool->ltp_pause = PAUSED; } - ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); + return(ret); } @@ -845,6 +1158,8 @@ ldap_pvt_thread_pool_t *tpool ) { struct ldap_int_thread_pool_s *pool; + struct ldap_int_thread_poolq_s *pq; + int i; if (tpool == NULL) return(-1); @@ -855,16 +1170,14 @@ return(0); ldap_pvt_thread_mutex_lock(&pool->ltp_mutex); - assert(pool->ltp_pause == PAUSED); pool->ltp_pause = 0; - if (pool->ltp_open_count <= 0) /* true when paused, but be paranoid */ - pool->ltp_open_count = -pool->ltp_open_count; - SET_VARY_OPEN_COUNT(pool); - pool->ltp_work_list = &pool->ltp_pending_list; - + for (i=0; i<pool->ltp_numqs; i++) { + pq = pool->ltp_wqs[i]; + pq->ltp_work_list = &pq->ltp_pending_list; + ldap_pvt_thread_cond_broadcast(&pq->ltp_cond); + } ldap_pvt_thread_cond_broadcast(&pool->ltp_cond); - ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); return(0); } @@ -971,6 +1284,7 @@ assert ( key != NULL ); + ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex); for ( i=0; i<LDAP_MAXTHR; i++ ) { ctx = thread_keys[i].ctx; if ( ctx && ctx != DELETED_THREAD_CTX ) { @@ -985,6 +1299,7 @@ } } } + ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex); } /* --- openldap-2.4.43/servers/slapd/ctxcsn.c.orig 2015-11-30 11:02:00.000000000 -0600 +++ openldap-2.4.43/servers/slapd/ctxcsn.c 2015-11-30 16:04:03.979791274 -0600 @@ -50,12 +50,12 @@ *foundit = 0; } - ldap_pvt_thread_mutex_lock( &be->be_pcl_mutex ); - if ( !BER_BVISEMPTY( &op->o_csn )) { sid = slap_parse_csn_sid( &op->o_csn ); } + ldap_pvt_thread_mutex_lock( &be->be_pcl_mutex ); + LDAP_TAILQ_FOREACH( csne, be->be_pending_csn_list, ce_csn_link ) { if ( csne->ce_opid == op->o_opid && csne->ce_connid == op->o_connid ) { csne->ce_state = SLAP_CSN_COMMIT; @@ -188,14 +188,14 @@ Debug( LDAP_DEBUG_SYNC, "slap_queue_csn: queueing %p %s\n", pending, csn->bv_val, 0 ); - ldap_pvt_thread_mutex_lock( &be->be_pcl_mutex ); - ber_dupbv( &pending->ce_csn, csn ); ber_bvreplace_x( &op->o_csn, &pending->ce_csn, op->o_tmpmemctx ); pending->ce_sid = slap_parse_csn_sid( csn ); pending->ce_connid = op->o_connid; pending->ce_opid = op->o_opid; pending->ce_state = SLAP_CSN_PENDING; + + ldap_pvt_thread_mutex_lock( &be->be_pcl_mutex ); LDAP_TAILQ_INSERT_TAIL( be->be_pending_csn_list, pending, ce_csn_link ); ldap_pvt_thread_mutex_unlock( &be->be_pcl_mutex );
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Contact
Support
@OBShq
The Open Build Service is an
openSUSE project
.
Log In
Places
Places
All Projects
Status Monitor