mirror of https://github.com/asterisk/asterisk
This patch optimizes taskprocessor to use a semaphore for signaling, which the OS can do a better job at managing contention and waiting that we can with a mutex and condition. The taskprocessor execution was also slightly optimized to reduce the number of locks taken. The only observable difference in the taskprocessor implementation is that when the final reference to the taskprocessor goes away, it will execute all tasks to completion instead of discarding the unexecuted tasks. For systems where unnamed semaphores are not supported, a really simple semaphore implementation is provided. (Which gives identical performance as the original taskprocessor implementation). The way we ended up implementing Stasis caused the threadpool to be a burden instead of a boost to performance. This was switched to just use taskprocessors directly for subscriptions. Review: https://reviewboard.asterisk.org/r/2881/ git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/12@400178 65c4cc65-6c06-0410-ace0-fbb531ad65f3changes/78/78/1
parent
52d73f05f4
commit
e88afe2022
@ -1,8 +0,0 @@
|
||||
[threadpool]
|
||||
;initial_size = 0 ; Initial size of the threadpool
|
||||
|
||||
;idle_timeout_sec = 20 ; Number of seconds a thread should be idle before dying
|
||||
; ; 0 means threads never time out
|
||||
|
||||
;max_size = 200 ; Maximum number of threads in the threadpool
|
||||
; ; 0 means no limit to the threads in the threadpool
|
@ -0,0 +1,157 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2013, Digium, Inc.
|
||||
*
|
||||
* David M. Lee, II <dlee@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
#ifndef ASTERISK_SEMAPHORE_H
|
||||
#define ASTERISK_SEMAPHORE_H
|
||||
|
||||
/*!
|
||||
* \file Asterisk semaphore API
|
||||
*
|
||||
* This API is a thin wrapper around the POSIX semaphore API (when available),
|
||||
* so see the POSIX documentation for further details.
|
||||
*/
|
||||
|
||||
#ifdef HAS_WORKING_SEMAPHORE
|
||||
/* Working semaphore implementation detected */
|
||||
|
||||
#include <semaphore.h>
|
||||
|
||||
struct ast_sem {
|
||||
sem_t real_sem;
|
||||
};
|
||||
|
||||
#define AST_SEM_VALUE_MAX SEM_VALUE_MAX
|
||||
|
||||
/* These are thin wrappers; might as well inline them */
|
||||
|
||||
static force_inline int ast_sem_init(struct ast_sem *sem, int pshared, unsigned int value)
|
||||
{
|
||||
return sem_init(&sem->real_sem, pshared, value);
|
||||
}
|
||||
|
||||
static force_inline int ast_sem_destroy(struct ast_sem *sem)
|
||||
{
|
||||
return sem_destroy(&sem->real_sem);
|
||||
}
|
||||
|
||||
static force_inline int ast_sem_post(struct ast_sem *sem)
|
||||
{
|
||||
return sem_post(&sem->real_sem);
|
||||
}
|
||||
|
||||
static force_inline int ast_sem_wait(struct ast_sem *sem)
|
||||
{
|
||||
return sem_wait(&sem->real_sem);
|
||||
}
|
||||
|
||||
static force_inline int ast_sem_getvalue(struct ast_sem *sem, int *sval)
|
||||
{
|
||||
return sem_getvalue(&sem->real_sem, sval);
|
||||
}
|
||||
|
||||
#else
|
||||
/* Unnamed semaphores don't work. Rolling our own, I guess... */
|
||||
|
||||
#include "asterisk/lock.h"
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
struct ast_sem {
|
||||
/*! Current count of this semaphore */
|
||||
int count;
|
||||
/*! Number of threads currently waiting for this semaphore */
|
||||
int waiters;
|
||||
/*! Mutual exclusion */
|
||||
ast_mutex_t mutex;
|
||||
/*! Condition for singalling waiters */
|
||||
ast_cond_t cond;
|
||||
};
|
||||
|
||||
#define AST_SEM_VALUE_MAX INT_MAX
|
||||
|
||||
/*!
|
||||
* \brief Initialize a semaphore.
|
||||
*
|
||||
* \param sem Semaphore to initialize.
|
||||
* \param pshared Pass true (nonzero) to share this thread between processes.
|
||||
* Not be supported on all platforms, so be wary!
|
||||
* But leave the parameter, to be compatible with the POSIX ABI
|
||||
* in case we need to add support in the future.
|
||||
* \param value Initial value of the semaphore.
|
||||
*
|
||||
* \return 0 on success.
|
||||
* \return -1 on error, errno set to indicate error.
|
||||
*/
|
||||
int ast_sem_init(struct ast_sem *sem, int pshared, unsigned int value);
|
||||
|
||||
/*!
|
||||
* \brief Destroy a semaphore.
|
||||
*
|
||||
* Destroying a semaphore that other threads are currently blocked on produces
|
||||
* undefined behavior.
|
||||
*
|
||||
* \param sem Semaphore to destroy.
|
||||
*
|
||||
* \return 0 on success.
|
||||
* \return -1 on error, errno set to indicate error.
|
||||
*/
|
||||
int ast_sem_destroy(struct ast_sem *sem);
|
||||
|
||||
/*!
|
||||
* \brief Increments the semaphore, unblocking a waiter if necessary.
|
||||
*
|
||||
* \param sem Semaphore to increment.
|
||||
*
|
||||
* \return 0 on success.
|
||||
* \return -1 on error, errno set to indicate error.
|
||||
*/
|
||||
int ast_sem_post(struct ast_sem *sem);
|
||||
|
||||
/*!
|
||||
* \brief Decrements the semaphore.
|
||||
*
|
||||
* If the semaphore's current value is zero, this function blocks until another
|
||||
* thread posts (ast_sem_post()) to the semaphore (or is interrupted by a signal
|
||||
* handler, which sets errno to EINTR).
|
||||
*
|
||||
* \param sem Semaphore to decrement.
|
||||
*
|
||||
* \return 0 on success.
|
||||
* \return -1 on error, errno set to indicate error.
|
||||
*/
|
||||
int ast_sem_wait(struct ast_sem *sem);
|
||||
|
||||
/*!
|
||||
* \brief Gets the current value of the semaphore.
|
||||
*
|
||||
* If threads are blocked on this semaphore, POSIX allows the return value to be
|
||||
* either 0 or a negative number whose absolute value is the number of threads
|
||||
* blocked. Don't assume that it will give you one or the other; Asterisk has
|
||||
* been ported to just about everything.
|
||||
*
|
||||
* \param sem Semaphore to query.
|
||||
* \param[out] sval Output value.
|
||||
*
|
||||
* \return 0 on success.
|
||||
* \return -1 on error, errno set to indicate error.
|
||||
*/
|
||||
int ast_sem_getvalue(struct ast_sem *sem, int *sval);
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* ASTERISK_SEMAPHORE_H */
|
@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2013, Digium, Inc.
|
||||
*
|
||||
* David M. Lee, II <dlee@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*! \file
|
||||
*
|
||||
* \brief Asterisk semaphore support.
|
||||
*/
|
||||
|
||||
#include "asterisk.h"
|
||||
|
||||
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
||||
|
||||
#include "asterisk/sem.h"
|
||||
#include "asterisk/utils.h"
|
||||
|
||||
#ifndef HAS_WORKING_SEMAPHORE
|
||||
|
||||
/* DIY semaphores! */
|
||||
|
||||
int ast_sem_init(struct ast_sem *sem, int pshared, unsigned int value)
|
||||
{
|
||||
if (pshared) {
|
||||
/* Don't need it... yet */
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Since value is unsigned, this will also catch attempts to init with
|
||||
* a negative value */
|
||||
if (value > AST_SEM_VALUE_MAX) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
sem->count = value;
|
||||
sem->waiters = 0;
|
||||
ast_mutex_init(&sem->mutex);
|
||||
ast_cond_init(&sem->cond, NULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ast_sem_destroy(struct ast_sem *sem)
|
||||
{
|
||||
ast_mutex_destroy(&sem->mutex);
|
||||
ast_cond_destroy(&sem->cond);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ast_sem_post(struct ast_sem *sem)
|
||||
{
|
||||
SCOPED_MUTEX(lock, &sem->mutex);
|
||||
|
||||
ast_assert(sem->count >= 0);
|
||||
|
||||
if (sem->count == AST_SEM_VALUE_MAX) {
|
||||
errno = EOVERFLOW;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Give it up! */
|
||||
++sem->count;
|
||||
|
||||
/* Release a waiter, if needed */
|
||||
if (sem->waiters) {
|
||||
ast_cond_signal(&sem->cond);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ast_sem_wait(struct ast_sem *sem)
|
||||
{
|
||||
SCOPED_MUTEX(lock, &sem->mutex);
|
||||
|
||||
ast_assert(sem->count >= 0);
|
||||
|
||||
/* Wait for a non-zero count */
|
||||
++sem->waiters;
|
||||
while (sem->count == 0) {
|
||||
ast_cond_wait(&sem->cond, &sem->mutex);
|
||||
}
|
||||
--sem->waiters;
|
||||
|
||||
/* Take it! */
|
||||
--sem->count;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ast_sem_getvalue(struct ast_sem *sem, int *sval)
|
||||
{
|
||||
SCOPED_MUTEX(lock, &sem->mutex);
|
||||
|
||||
ast_assert(sem->count >= 0);
|
||||
|
||||
*sval = sem->count;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
@ -1,201 +0,0 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2013, Digium, Inc.
|
||||
*
|
||||
* David M. Lee, II <dlee@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*! \file
|
||||
*
|
||||
* \brief Stasis Message Bus configuration API.
|
||||
*
|
||||
* \author David M. Lee, II <dlee@digium.com>
|
||||
*/
|
||||
|
||||
/*** MODULEINFO
|
||||
<support_level>core</support_level>
|
||||
***/
|
||||
|
||||
#include "asterisk.h"
|
||||
|
||||
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
||||
|
||||
#include "asterisk/config_options.h"
|
||||
#include "asterisk/stasis.h"
|
||||
#include "asterisk/threadpool.h"
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
/*** DOCUMENTATION
|
||||
<configInfo name="stasis" language="en_US">
|
||||
<synopsis>Stasis message bus configuration.</synopsis>
|
||||
<configFile name="stasis.conf">
|
||||
<configObject name="threadpool">
|
||||
<synopsis>Threadpool configuration.</synopsis>
|
||||
<configOption name="initial_size" default="0">
|
||||
<synopsis>Initial number of threads in the message bus threadpool.</synopsis>
|
||||
</configOption>
|
||||
<configOption name="idle_timeout_sec" default="20">
|
||||
<synopsis>Number of seconds for an idle thread to be disposed of.</synopsis>
|
||||
</configOption>
|
||||
<configOption name="max_size" default="200">
|
||||
<synopsis>Maximum number of threads in the threadpool.</synopsis>
|
||||
</configOption>
|
||||
</configObject>
|
||||
</configFile>
|
||||
</configInfo>
|
||||
***/
|
||||
|
||||
/*! \brief Locking container for safe configuration access. */
|
||||
static AO2_GLOBAL_OBJ_STATIC(confs);
|
||||
|
||||
struct stasis_threadpool_conf {
|
||||
int initial_size;
|
||||
int idle_timeout_sec;
|
||||
int max_size;
|
||||
};
|
||||
|
||||
struct stasis_conf {
|
||||
struct stasis_threadpool_conf *threadpool;
|
||||
};
|
||||
|
||||
/*! \brief Mapping of the stasis conf struct's globals to the
|
||||
* threadpool context in the config file. */
|
||||
static struct aco_type threadpool_option = {
|
||||
.type = ACO_GLOBAL,
|
||||
.name = "threadpool",
|
||||
.item_offset = offsetof(struct stasis_conf, threadpool),
|
||||
.category = "^threadpool$",
|
||||
.category_match = ACO_WHITELIST,
|
||||
};
|
||||
|
||||
static struct aco_type *threadpool_options[] = ACO_TYPES(&threadpool_option);
|
||||
|
||||
#define CONF_FILENAME "stasis.conf"
|
||||
|
||||
/*! \brief The conf file that's processed for the module. */
|
||||
static struct aco_file conf_file = {
|
||||
/*! The config file name. */
|
||||
.filename = CONF_FILENAME,
|
||||
/*! The mapping object types to be processed. */
|
||||
.types = ACO_TYPES(&threadpool_option),
|
||||
};
|
||||
|
||||
static void conf_dtor(void *obj)
|
||||
{
|
||||
struct stasis_conf *conf = obj;
|
||||
|
||||
ao2_cleanup(conf->threadpool);
|
||||
conf->threadpool = NULL;
|
||||
}
|
||||
|
||||
static void *conf_alloc(void)
|
||||
{
|
||||
RAII_VAR(struct stasis_conf *, conf, NULL, ao2_cleanup);
|
||||
|
||||
conf = ao2_alloc_options(sizeof(*conf), conf_dtor,
|
||||
AO2_ALLOC_OPT_LOCK_NOLOCK);
|
||||
if (!conf) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
conf->threadpool = ao2_alloc_options(sizeof(*conf->threadpool), NULL,
|
||||
AO2_ALLOC_OPT_LOCK_NOLOCK);
|
||||
if (!conf->threadpool) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
aco_set_defaults(&threadpool_option, "threadpool", conf->threadpool);
|
||||
|
||||
ao2_ref(conf, +1);
|
||||
return conf;
|
||||
}
|
||||
|
||||
CONFIG_INFO_CORE("stasis", cfg_info, confs, conf_alloc,
|
||||
.files = ACO_FILES(&conf_file));
|
||||
|
||||
void stasis_config_get_threadpool_options(
|
||||
struct ast_threadpool_options *threadpool_options)
|
||||
{
|
||||
RAII_VAR(struct stasis_conf *, conf, NULL, ao2_cleanup);
|
||||
|
||||
conf = ao2_global_obj_ref(confs);
|
||||
|
||||
ast_assert(conf && conf->threadpool);
|
||||
|
||||
{
|
||||
struct ast_threadpool_options newopts = {
|
||||
.version = AST_THREADPOOL_OPTIONS_VERSION,
|
||||
.initial_size = conf->threadpool->initial_size,
|
||||
.auto_increment = 1,
|
||||
.idle_timeout = conf->threadpool->idle_timeout_sec,
|
||||
.max_size = conf->threadpool->max_size,
|
||||
};
|
||||
|
||||
*threadpool_options = newopts;
|
||||
}
|
||||
}
|
||||
|
||||
/*! \brief Load (or reload) configuration. */
|
||||
static int process_config(int reload)
|
||||
{
|
||||
RAII_VAR(struct stasis_conf *, conf, conf_alloc(), ao2_cleanup);
|
||||
|
||||
switch (aco_process_config(&cfg_info, reload)) {
|
||||
case ACO_PROCESS_ERROR:
|
||||
if (conf && !reload
|
||||
&& !aco_set_defaults(&threadpool_option, "threadpool", conf->threadpool)) {
|
||||
ast_log(AST_LOG_NOTICE, "Failed to process Stasis configuration; using defaults\n");
|
||||
ao2_global_obj_replace_unref(confs, conf);
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
case ACO_PROCESS_OK:
|
||||
case ACO_PROCESS_UNCHANGED:
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void config_exit(void)
|
||||
{
|
||||
aco_info_destroy(&cfg_info);
|
||||
ao2_global_obj_release(confs);
|
||||
}
|
||||
|
||||
int stasis_config_init(void)
|
||||
{
|
||||
if (aco_info_init(&cfg_info)) {
|
||||
aco_info_destroy(&cfg_info);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ast_register_atexit(config_exit);
|
||||
|
||||
/* threadpool section */
|
||||
aco_option_register(&cfg_info, "initial_size", ACO_EXACT,
|
||||
threadpool_options, "0", OPT_INT_T, PARSE_IN_RANGE,
|
||||
FLDSET(struct stasis_threadpool_conf, initial_size), 0,
|
||||
INT_MAX);
|
||||
aco_option_register(&cfg_info, "idle_timeout_sec", ACO_EXACT,
|
||||
threadpool_options, "20", OPT_INT_T, PARSE_IN_RANGE,
|
||||
FLDSET(struct stasis_threadpool_conf, idle_timeout_sec), 0,
|
||||
INT_MAX);
|
||||
aco_option_register(&cfg_info, "max_size", ACO_EXACT,
|
||||
threadpool_options, "200", OPT_INT_T, PARSE_IN_RANGE,
|
||||
FLDSET(struct stasis_threadpool_conf, max_size), 0, INT_MAX);
|
||||
|
||||
return process_config(0);
|
||||
}
|
Loading…
Reference in new issue