taskpool: Add taskpool API, switch Stasis to using it.

This change introduces a new API called taskpool. This is a pool
of taskprocessors. It provides the following functionality:

1. Task pushing to a pool of taskprocessors
2. Synchronous tasks
3. Serializers for execution ordering of tasks
4. Growing/shrinking of number of taskprocessors in pool

This functionality already exists through the combination of
threadpool+taskprocessors but through investigating I determined
that this carries substantial overhead for short to medium duration
tasks. The threadpool uses a single queue of work, and for management
of threads it involves additional tasks.

I wrote taskpool to eliminate the extra overhead and management
as much as possible. Instead of a single queue of work each
taskprocessor has its own queue and at push time a selector chooses
the taskprocessor to queue the task to. Each taskprocessor also
has its own thread like normal. This spreads out the tasks immediately
and reduces contention on shared resources.

Using the included efficiency tests the number of tasks that can be
executed per second in a taskpool is 6-12 times more than an equivalent
threadpool+taskprocessor setup.

Stasis has been moved over to using this new API as it is a heavy consumer
of threadpool+taskprocessors and produces a lot of tasks.

UpgradeNote: The threadpool_* options in stasis.conf have now been deprecated
though they continue to be read and used. They have been replaced with taskpool
options that give greater control over the underlying taskpool used for stasis.

DeveloperNote: The taskpool API has been added for common usage of a
pool of taskprocessors. It is suggested to use this API instead of the
threadpool+taskprocessor approach.
This commit is contained in:
Joshua C. Colp
2025-08-06 13:19:20 -03:00
parent f413a22da2
commit 1dc6fecc21
16 changed files with 2691 additions and 175 deletions

View File

@@ -22,8 +22,10 @@
#include "asterisk/serializer.h"
#include "asterisk/taskprocessor.h"
#include "asterisk/threadpool.h"
#include "asterisk/taskpool.h"
#include "asterisk/utils.h"
#include "asterisk/vector.h"
#include "asterisk/serializer_shutdown_group.h"
struct ast_serializer_pool {
/*! Shutdown group to monitor serializers. */
@@ -119,6 +121,52 @@ struct ast_serializer_pool *ast_serializer_pool_create(const char *name,
return pool;
}
struct ast_serializer_pool *ast_serializer_taskpool_create(const char *name,
unsigned int size, struct ast_taskpool *taskpool, int timeout)
{
struct ast_serializer_pool *pool;
char tps_name[AST_TASKPROCESSOR_MAX_NAME + 1];
size_t idx;
ast_assert(size > 0);
pool = ast_malloc(sizeof(*pool) + strlen(name) + 1);
if (!pool) {
return NULL;
}
strcpy(pool->name, name); /* safe */
pool->shutdown_group_timeout = timeout;
pool->shutdown_group = timeout > -1 ? ast_serializer_shutdown_group_alloc() : NULL;
AST_VECTOR_RW_INIT(&pool->serializers, size);
for (idx = 0; idx < size; ++idx) {
struct ast_taskprocessor *tps;
/* Create name with seq number appended. */
ast_taskprocessor_name_append(tps_name, sizeof(tps_name), name);
tps = ast_taskpool_serializer_group(tps_name, taskpool, pool->shutdown_group);
if (!tps) {
ast_serializer_pool_destroy(pool);
ast_log(LOG_ERROR, "Pool create: unable to create named serializer '%s'\n",
tps_name);
return NULL;
}
if (AST_VECTOR_APPEND(&pool->serializers, tps)) {
ast_serializer_pool_destroy(pool);
ast_log(LOG_ERROR, "Pool create: unable to append named serializer '%s'\n",
tps_name);
return NULL;
}
}
return pool;
}
const char *ast_serializer_pool_name(const struct ast_serializer_pool *pool)
{
return pool->name;