1999-10-21 00:32:15 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
1999-10-21 00:32:15 +00:00
|
|
|
*/
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2000-04-28 04:26:08 +00:00
|
|
|
#include <isc/mem.h>
|
2011-07-06 01:36:32 +00:00
|
|
|
#include <isc/random.h>
|
1999-10-21 00:32:15 +00:00
|
|
|
#include <isc/taskpool.h>
|
2000-04-28 01:12:23 +00:00
|
|
|
#include <isc/util.h>
|
|
|
|
|
1999-10-21 00:32:15 +00:00
|
|
|
/***
|
|
|
|
*** Types.
|
|
|
|
***/
|
|
|
|
|
|
|
|
struct isc_taskpool {
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mem_t *mctx;
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_taskmgr_t *tmgr;
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int ntasks;
|
|
|
|
unsigned int quantum;
|
|
|
|
isc_task_t **tasks;
|
1999-10-21 00:32:15 +00:00
|
|
|
};
|
2011-07-06 01:36:32 +00:00
|
|
|
|
1999-10-21 00:32:15 +00:00
|
|
|
/***
|
|
|
|
*** Functions.
|
|
|
|
***/
|
|
|
|
|
2020-02-08 04:37:54 -08:00
|
|
|
static void
|
2011-07-06 01:36:32 +00:00
|
|
|
alloc_pool(isc_taskmgr_t *tmgr, isc_mem_t *mctx, unsigned int ntasks,
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int quantum, isc_taskpool_t **poolp) {
|
1999-10-21 00:32:15 +00:00
|
|
|
isc_taskpool_t *pool;
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int i;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2001-11-27 01:56:32 +00:00
|
|
|
pool = isc_mem_get(mctx, sizeof(*pool));
|
2013-03-08 14:38:03 +11:00
|
|
|
|
|
|
|
pool->mctx = NULL;
|
|
|
|
isc_mem_attach(mctx, &pool->mctx);
|
1999-10-21 00:32:15 +00:00
|
|
|
pool->ntasks = ntasks;
|
2011-07-06 01:36:32 +00:00
|
|
|
pool->quantum = quantum;
|
|
|
|
pool->tmgr = tmgr;
|
1999-10-21 00:32:15 +00:00
|
|
|
pool->tasks = isc_mem_get(mctx, ntasks * sizeof(isc_task_t *));
|
2020-02-08 04:37:54 -08:00
|
|
|
for (i = 0; i < ntasks; i++) {
|
1999-10-21 00:32:15 +00:00
|
|
|
pool->tasks[i] = NULL;
|
2020-02-08 04:37:54 -08:00
|
|
|
}
|
2011-07-06 01:36:32 +00:00
|
|
|
|
|
|
|
*poolp = pool;
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_taskpool_create(isc_taskmgr_t *tmgr, isc_mem_t *mctx, unsigned int ntasks,
|
Refactor taskmgr to run on top of netmgr
This commit changes the taskmgr to run the individual tasks on the
netmgr internal workers. While an effort has been put into keeping the
taskmgr interface intact, couple of changes have been made:
* The taskmgr has no concept of universal privileged mode - rather the
tasks are either privileged or unprivileged (normal). The privileged
tasks are run as a first thing when the netmgr is unpaused. There
are now four different queues in in the netmgr:
1. priority queue - netievent on the priority queue are run even when
the taskmgr enter exclusive mode and netmgr is paused. This is
needed to properly start listening on the interfaces, free
resources and resume.
2. privileged task queue - only privileged tasks are queued here and
this is the first queue that gets processed when network manager
is unpaused using isc_nm_resume(). All netmgr workers need to
clean the privileged task queue before they all proceed normal
operation. Both task queues are processed when the workers are
finished.
3. task queue - only (traditional) task are scheduled here and this
queue along with privileged task queues are process when the
netmgr workers are finishing. This is needed to process the task
shutdown events.
4. normal queue - this is the queue with netmgr events, e.g. reading,
sending, callbacks and pretty much everything is processed here.
* The isc_taskmgr_create() now requires initialized netmgr (isc_nm_t)
object.
* The isc_nm_destroy() function now waits for indefinite time, but it
will print out the active objects when in tracing mode
(-DNETMGR_TRACE=1 and -DNETMGR_TRACE_VERBOSE=1), the netmgr has been
made a little bit more asynchronous and it might take longer time to
shutdown all the active networking connections.
* Previously, the isc_nm_stoplistening() was a synchronous operation.
This has been changed and the isc_nm_stoplistening() just schedules
the child sockets to stop listening and exits. This was needed to
prevent a deadlock as the the (traditional) tasks are now executed on
the netmgr threads.
* The socket selection logic in isc__nm_udp_send() was flawed, but
fortunatelly, it was broken, so we never hit the problem where we
created uvreq_t on a socket from nmhandle_t, but then a different
socket could be picked up and then we were trying to run the send
callback on a socket that had different threadid than currently
running.
2021-04-09 11:31:19 +02:00
|
|
|
unsigned int quantum, bool priv, isc_taskpool_t **poolp) {
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int i;
|
2011-07-06 01:36:32 +00:00
|
|
|
isc_taskpool_t *pool = NULL;
|
|
|
|
|
|
|
|
INSIST(ntasks > 0);
|
2011-07-07 23:47:50 +00:00
|
|
|
|
2011-07-06 01:36:32 +00:00
|
|
|
/* Allocate the pool structure */
|
2020-02-08 04:37:54 -08:00
|
|
|
alloc_pool(tmgr, mctx, ntasks, quantum, &pool);
|
2011-07-06 01:36:32 +00:00
|
|
|
|
|
|
|
/* Create the tasks */
|
1999-10-21 00:32:15 +00:00
|
|
|
for (i = 0; i < ntasks; i++) {
|
Refactor taskmgr to run on top of netmgr
This commit changes the taskmgr to run the individual tasks on the
netmgr internal workers. While an effort has been put into keeping the
taskmgr interface intact, couple of changes have been made:
* The taskmgr has no concept of universal privileged mode - rather the
tasks are either privileged or unprivileged (normal). The privileged
tasks are run as a first thing when the netmgr is unpaused. There
are now four different queues in in the netmgr:
1. priority queue - netievent on the priority queue are run even when
the taskmgr enter exclusive mode and netmgr is paused. This is
needed to properly start listening on the interfaces, free
resources and resume.
2. privileged task queue - only privileged tasks are queued here and
this is the first queue that gets processed when network manager
is unpaused using isc_nm_resume(). All netmgr workers need to
clean the privileged task queue before they all proceed normal
operation. Both task queues are processed when the workers are
finished.
3. task queue - only (traditional) task are scheduled here and this
queue along with privileged task queues are process when the
netmgr workers are finishing. This is needed to process the task
shutdown events.
4. normal queue - this is the queue with netmgr events, e.g. reading,
sending, callbacks and pretty much everything is processed here.
* The isc_taskmgr_create() now requires initialized netmgr (isc_nm_t)
object.
* The isc_nm_destroy() function now waits for indefinite time, but it
will print out the active objects when in tracing mode
(-DNETMGR_TRACE=1 and -DNETMGR_TRACE_VERBOSE=1), the netmgr has been
made a little bit more asynchronous and it might take longer time to
shutdown all the active networking connections.
* Previously, the isc_nm_stoplistening() was a synchronous operation.
This has been changed and the isc_nm_stoplistening() just schedules
the child sockets to stop listening and exits. This was needed to
prevent a deadlock as the the (traditional) tasks are now executed on
the netmgr threads.
* The socket selection logic in isc__nm_udp_send() was flawed, but
fortunatelly, it was broken, so we never hit the problem where we
created uvreq_t on a socket from nmhandle_t, but then a different
socket could be picked up and then we were trying to run the send
callback on a socket that had different threadid than currently
running.
2021-04-09 11:31:19 +02:00
|
|
|
isc_result_t result = isc_task_create_bound(tmgr, quantum,
|
|
|
|
&pool->tasks[i], i);
|
1999-10-21 00:32:15 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
isc_taskpool_destroy(&pool);
|
|
|
|
return (result);
|
|
|
|
}
|
Refactor taskmgr to run on top of netmgr
This commit changes the taskmgr to run the individual tasks on the
netmgr internal workers. While an effort has been put into keeping the
taskmgr interface intact, couple of changes have been made:
* The taskmgr has no concept of universal privileged mode - rather the
tasks are either privileged or unprivileged (normal). The privileged
tasks are run as a first thing when the netmgr is unpaused. There
are now four different queues in in the netmgr:
1. priority queue - netievent on the priority queue are run even when
the taskmgr enter exclusive mode and netmgr is paused. This is
needed to properly start listening on the interfaces, free
resources and resume.
2. privileged task queue - only privileged tasks are queued here and
this is the first queue that gets processed when network manager
is unpaused using isc_nm_resume(). All netmgr workers need to
clean the privileged task queue before they all proceed normal
operation. Both task queues are processed when the workers are
finished.
3. task queue - only (traditional) task are scheduled here and this
queue along with privileged task queues are process when the
netmgr workers are finishing. This is needed to process the task
shutdown events.
4. normal queue - this is the queue with netmgr events, e.g. reading,
sending, callbacks and pretty much everything is processed here.
* The isc_taskmgr_create() now requires initialized netmgr (isc_nm_t)
object.
* The isc_nm_destroy() function now waits for indefinite time, but it
will print out the active objects when in tracing mode
(-DNETMGR_TRACE=1 and -DNETMGR_TRACE_VERBOSE=1), the netmgr has been
made a little bit more asynchronous and it might take longer time to
shutdown all the active networking connections.
* Previously, the isc_nm_stoplistening() was a synchronous operation.
This has been changed and the isc_nm_stoplistening() just schedules
the child sockets to stop listening and exits. This was needed to
prevent a deadlock as the the (traditional) tasks are now executed on
the netmgr threads.
* The socket selection logic in isc__nm_udp_send() was flawed, but
fortunatelly, it was broken, so we never hit the problem where we
created uvreq_t on a socket from nmhandle_t, but then a different
socket could be picked up and then we were trying to run the send
callback on a socket that had different threadid than currently
running.
2021-04-09 11:31:19 +02:00
|
|
|
isc_task_setprivilege(pool->tasks[i], priv);
|
2007-02-13 02:49:08 +00:00
|
|
|
isc_task_setname(pool->tasks[i], "taskpool", NULL);
|
1999-10-21 00:32:15 +00:00
|
|
|
}
|
2011-07-06 01:36:32 +00:00
|
|
|
|
1999-10-21 00:32:15 +00:00
|
|
|
*poolp = pool;
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2011-07-06 01:36:32 +00:00
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_taskpool_gettask(isc_taskpool_t *pool, isc_task_t **targetp) {
|
2018-05-28 15:22:23 +02:00
|
|
|
isc_task_attach(pool->tasks[isc_random_uniform(pool->ntasks)], targetp);
|
2011-07-06 01:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_taskpool_size(isc_taskpool_t *pool) {
|
2011-07-06 01:36:32 +00:00
|
|
|
REQUIRE(pool != NULL);
|
|
|
|
return (pool->ntasks);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
Refactor taskmgr to run on top of netmgr
This commit changes the taskmgr to run the individual tasks on the
netmgr internal workers. While an effort has been put into keeping the
taskmgr interface intact, couple of changes have been made:
* The taskmgr has no concept of universal privileged mode - rather the
tasks are either privileged or unprivileged (normal). The privileged
tasks are run as a first thing when the netmgr is unpaused. There
are now four different queues in in the netmgr:
1. priority queue - netievent on the priority queue are run even when
the taskmgr enter exclusive mode and netmgr is paused. This is
needed to properly start listening on the interfaces, free
resources and resume.
2. privileged task queue - only privileged tasks are queued here and
this is the first queue that gets processed when network manager
is unpaused using isc_nm_resume(). All netmgr workers need to
clean the privileged task queue before they all proceed normal
operation. Both task queues are processed when the workers are
finished.
3. task queue - only (traditional) task are scheduled here and this
queue along with privileged task queues are process when the
netmgr workers are finishing. This is needed to process the task
shutdown events.
4. normal queue - this is the queue with netmgr events, e.g. reading,
sending, callbacks and pretty much everything is processed here.
* The isc_taskmgr_create() now requires initialized netmgr (isc_nm_t)
object.
* The isc_nm_destroy() function now waits for indefinite time, but it
will print out the active objects when in tracing mode
(-DNETMGR_TRACE=1 and -DNETMGR_TRACE_VERBOSE=1), the netmgr has been
made a little bit more asynchronous and it might take longer time to
shutdown all the active networking connections.
* Previously, the isc_nm_stoplistening() was a synchronous operation.
This has been changed and the isc_nm_stoplistening() just schedules
the child sockets to stop listening and exits. This was needed to
prevent a deadlock as the the (traditional) tasks are now executed on
the netmgr threads.
* The socket selection logic in isc__nm_udp_send() was flawed, but
fortunatelly, it was broken, so we never hit the problem where we
created uvreq_t on a socket from nmhandle_t, but then a different
socket could be picked up and then we were trying to run the send
callback on a socket that had different threadid than currently
running.
2021-04-09 11:31:19 +02:00
|
|
|
isc_taskpool_expand(isc_taskpool_t **sourcep, unsigned int size, bool priv,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_taskpool_t **targetp) {
|
2011-07-06 01:36:32 +00:00
|
|
|
isc_taskpool_t *pool;
|
|
|
|
|
|
|
|
REQUIRE(sourcep != NULL && *sourcep != NULL);
|
|
|
|
REQUIRE(targetp != NULL && *targetp == NULL);
|
|
|
|
|
|
|
|
pool = *sourcep;
|
2020-02-08 04:37:54 -08:00
|
|
|
*sourcep = NULL;
|
2011-07-06 01:36:32 +00:00
|
|
|
if (size > pool->ntasks) {
|
|
|
|
isc_taskpool_t *newpool = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int i;
|
2011-07-06 01:36:32 +00:00
|
|
|
|
|
|
|
/* Allocate a new pool structure */
|
2020-02-12 13:59:18 +01:00
|
|
|
alloc_pool(pool->tmgr, pool->mctx, size, pool->quantum,
|
|
|
|
&newpool);
|
2011-07-06 01:36:32 +00:00
|
|
|
|
|
|
|
/* Copy over the tasks from the old pool */
|
|
|
|
for (i = 0; i < pool->ntasks; i++) {
|
|
|
|
newpool->tasks[i] = pool->tasks[i];
|
|
|
|
pool->tasks[i] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create new tasks */
|
|
|
|
for (i = pool->ntasks; i < size; i++) {
|
Refactor taskmgr to run on top of netmgr
This commit changes the taskmgr to run the individual tasks on the
netmgr internal workers. While an effort has been put into keeping the
taskmgr interface intact, couple of changes have been made:
* The taskmgr has no concept of universal privileged mode - rather the
tasks are either privileged or unprivileged (normal). The privileged
tasks are run as a first thing when the netmgr is unpaused. There
are now four different queues in in the netmgr:
1. priority queue - netievent on the priority queue are run even when
the taskmgr enter exclusive mode and netmgr is paused. This is
needed to properly start listening on the interfaces, free
resources and resume.
2. privileged task queue - only privileged tasks are queued here and
this is the first queue that gets processed when network manager
is unpaused using isc_nm_resume(). All netmgr workers need to
clean the privileged task queue before they all proceed normal
operation. Both task queues are processed when the workers are
finished.
3. task queue - only (traditional) task are scheduled here and this
queue along with privileged task queues are process when the
netmgr workers are finishing. This is needed to process the task
shutdown events.
4. normal queue - this is the queue with netmgr events, e.g. reading,
sending, callbacks and pretty much everything is processed here.
* The isc_taskmgr_create() now requires initialized netmgr (isc_nm_t)
object.
* The isc_nm_destroy() function now waits for indefinite time, but it
will print out the active objects when in tracing mode
(-DNETMGR_TRACE=1 and -DNETMGR_TRACE_VERBOSE=1), the netmgr has been
made a little bit more asynchronous and it might take longer time to
shutdown all the active networking connections.
* Previously, the isc_nm_stoplistening() was a synchronous operation.
This has been changed and the isc_nm_stoplistening() just schedules
the child sockets to stop listening and exits. This was needed to
prevent a deadlock as the the (traditional) tasks are now executed on
the netmgr threads.
* The socket selection logic in isc__nm_udp_send() was flawed, but
fortunatelly, it was broken, so we never hit the problem where we
created uvreq_t on a socket from nmhandle_t, but then a different
socket could be picked up and then we were trying to run the send
callback on a socket that had different threadid than currently
running.
2021-04-09 11:31:19 +02:00
|
|
|
isc_result_t result =
|
|
|
|
isc_task_create_bound(pool->tmgr, pool->quantum,
|
|
|
|
&newpool->tasks[i], i);
|
2011-07-06 01:36:32 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2020-02-08 04:37:54 -08:00
|
|
|
*sourcep = pool;
|
2011-07-06 01:36:32 +00:00
|
|
|
isc_taskpool_destroy(&newpool);
|
|
|
|
return (result);
|
|
|
|
}
|
Refactor taskmgr to run on top of netmgr
This commit changes the taskmgr to run the individual tasks on the
netmgr internal workers. While an effort has been put into keeping the
taskmgr interface intact, couple of changes have been made:
* The taskmgr has no concept of universal privileged mode - rather the
tasks are either privileged or unprivileged (normal). The privileged
tasks are run as a first thing when the netmgr is unpaused. There
are now four different queues in in the netmgr:
1. priority queue - netievent on the priority queue are run even when
the taskmgr enter exclusive mode and netmgr is paused. This is
needed to properly start listening on the interfaces, free
resources and resume.
2. privileged task queue - only privileged tasks are queued here and
this is the first queue that gets processed when network manager
is unpaused using isc_nm_resume(). All netmgr workers need to
clean the privileged task queue before they all proceed normal
operation. Both task queues are processed when the workers are
finished.
3. task queue - only (traditional) task are scheduled here and this
queue along with privileged task queues are process when the
netmgr workers are finishing. This is needed to process the task
shutdown events.
4. normal queue - this is the queue with netmgr events, e.g. reading,
sending, callbacks and pretty much everything is processed here.
* The isc_taskmgr_create() now requires initialized netmgr (isc_nm_t)
object.
* The isc_nm_destroy() function now waits for indefinite time, but it
will print out the active objects when in tracing mode
(-DNETMGR_TRACE=1 and -DNETMGR_TRACE_VERBOSE=1), the netmgr has been
made a little bit more asynchronous and it might take longer time to
shutdown all the active networking connections.
* Previously, the isc_nm_stoplistening() was a synchronous operation.
This has been changed and the isc_nm_stoplistening() just schedules
the child sockets to stop listening and exits. This was needed to
prevent a deadlock as the the (traditional) tasks are now executed on
the netmgr threads.
* The socket selection logic in isc__nm_udp_send() was flawed, but
fortunatelly, it was broken, so we never hit the problem where we
created uvreq_t on a socket from nmhandle_t, but then a different
socket could be picked up and then we were trying to run the send
callback on a socket that had different threadid than currently
running.
2021-04-09 11:31:19 +02:00
|
|
|
isc_task_setprivilege(newpool->tasks[i], priv);
|
2011-07-06 01:36:32 +00:00
|
|
|
isc_task_setname(newpool->tasks[i], "taskpool", NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_taskpool_destroy(&pool);
|
|
|
|
pool = newpool;
|
|
|
|
}
|
|
|
|
|
|
|
|
*targetp = pool;
|
|
|
|
return (ISC_R_SUCCESS);
|
1999-10-21 00:32:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_taskpool_destroy(isc_taskpool_t **poolp) {
|
|
|
|
unsigned int i;
|
1999-10-21 00:32:15 +00:00
|
|
|
isc_taskpool_t *pool = *poolp;
|
2020-02-08 04:37:54 -08:00
|
|
|
*poolp = NULL;
|
1999-10-21 00:32:15 +00:00
|
|
|
for (i = 0; i < pool->ntasks; i++) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (pool->tasks[i] != NULL) {
|
1999-10-21 00:32:15 +00:00
|
|
|
isc_task_detach(&pool->tasks[i]);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-10-21 00:32:15 +00:00
|
|
|
}
|
|
|
|
isc_mem_put(pool->mctx, pool->tasks,
|
|
|
|
pool->ntasks * sizeof(isc_task_t *));
|
2013-03-08 14:38:03 +11:00
|
|
|
isc_mem_putanddetach(&pool->mctx, pool, sizeof(*pool));
|
1999-10-21 00:32:15 +00:00
|
|
|
}
|