2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-31 14:35:26 +00:00

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.
This commit is contained in:
Ondřej Surý
2021-04-09 11:31:19 +02:00
committed by Ondřej Surý
parent ae9edb1861
commit b540722bc3
38 changed files with 844 additions and 1264 deletions

View File

@@ -522,3 +522,16 @@ isc_nm_listenhttp(isc_nm_t *mgr, isc_nmiface_t *iface, int backlog,
isc_result_t
isc_nm_http_endpoint(isc_nmsocket_t *sock, const char *uri, isc_nm_recv_cb_t cb,
void *cbarg, size_t extrahandlesize);
void
isc_nm_task_enqueue(isc_nm_t *mgr, isc_task_t *task, int threadid);
/*%<
* Enqueue the 'task' onto the netmgr ievents queue.
*
* Requires:
* \li 'mgr' is a valid netmgr object
* \li 'task' is a valid task
* \li 'threadid' is either the preferred netmgr tid or -1, in which case
* tid will be picked randomly. The threadid is capped (by modulo) to
* maximum number of 'workers' as specifed in isc_nm_start()
*/

View File

@@ -102,12 +102,11 @@ typedef enum {
isc_result_t
isc_task_create(isc_taskmgr_t *manager, unsigned int quantum,
isc_task_t **taskp);
isc_result_t
isc_task_create_bound(isc_taskmgr_t *manager, unsigned int quantum,
isc_task_t **taskp, int threadid);
/*%<
* Create a task.
* Create a task, optionally bound to a particular threadid.
*
* Notes:
*
@@ -138,6 +137,23 @@ isc_task_create_bound(isc_taskmgr_t *manager, unsigned int quantum,
*\li #ISC_R_SHUTTINGDOWN
*/
isc_result_t
isc_task_run(isc_task_t *task);
/*%<
* Run all the queued events for the 'task', returning
* when the queue is empty or the number of events executed
* exceeds the 'quantum' specified when the task was created.
*
* Requires:
*
*\li 'task' is a valid task.
*
* Returns:
*
*\li #ISC_R_SUCCESS
*\li #ISC_R_QUOTA
*/
void
isc_task_attach(isc_task_t *source, isc_task_t **targetp);
/*%<
@@ -611,20 +627,13 @@ isc_task_privilege(isc_task_t *task);
*****/
isc_result_t
isc_taskmgr_create(isc_mem_t *mctx, unsigned int workers,
unsigned int default_quantum, isc_nm_t *nm,
isc_taskmgr_create(isc_mem_t *mctx, unsigned int default_quantum, isc_nm_t *nm,
isc_taskmgr_t **managerp);
/*%<
* Create a new task manager.
*
* Notes:
*
*\li 'workers' in the number of worker threads to create. In general,
* the value should be close to the number of processors in the system.
* The 'workers' value is advisory only. An attempt will be made to
* create 'workers' threads, but if at least one thread creation
* succeeds, isc_taskmgr_create() may return ISC_R_SUCCESS.
*
*\li If 'default_quantum' is non-zero, then it will be used as the default
* quantum value when tasks are created. If zero, then an implementation
* defined default quantum will be used.
@@ -636,8 +645,6 @@ isc_taskmgr_create(isc_mem_t *mctx, unsigned int workers,
*
*\li 'mctx' is a valid memory context.
*
*\li workers > 0
*
*\li managerp != NULL && *managerp == NULL
*
* Ensures:
@@ -651,34 +658,14 @@ isc_taskmgr_create(isc_mem_t *mctx, unsigned int workers,
*\li #ISC_R_NOMEMORY
*\li #ISC_R_NOTHREADS No threads could be created.
*\li #ISC_R_UNEXPECTED An unexpected error occurred.
*\li #ISC_R_SHUTTINGDOWN The non-threaded, shared, task
*\li #ISC_R_SHUTTINGDOWN The non-threaded, shared, task
* manager shutting down.
*/
void
isc_taskmgr_setprivilegedmode(isc_taskmgr_t *manager);
isc_taskmgrmode_t
isc_taskmgr_mode(isc_taskmgr_t *manager);
/*%<
* Set/get the current operating mode of the task manager. Valid modes are:
*
*\li isc_taskmgrmode_normal
*\li isc_taskmgrmode_privileged
*
* In privileged execution mode, only tasks that have had the "privilege"
* flag set via isc_task_setprivilege() can be executed. When all such
* tasks are complete, the manager automatically returns to normal mode
* and proceeds with running non-privileged ready tasks. This means it is
* necessary to have at least one privileged task waiting on the ready
* queue *before* setting the manager into privileged execution mode,
* which in turn means the task which calls this function should be in
* task-exclusive mode when it does so.
*
* Requires:
*
*\li 'manager' is a valid task manager.
*/
isc_taskmgr_attach(isc_taskmgr_t *, isc_taskmgr_t **);
void
isc_taskmgr_detach(isc_taskmgr_t *);
void
isc_taskmgr_destroy(isc_taskmgr_t **managerp);

View File

@@ -51,7 +51,7 @@ typedef struct isc_taskpool isc_taskpool_t;
isc_result_t
isc_taskpool_create(isc_taskmgr_t *tmgr, isc_mem_t *mctx, unsigned int ntasks,
unsigned int quantum, isc_taskpool_t **poolp);
unsigned int quantum, bool priv, isc_taskpool_t **poolp);
/*%<
* Create a task pool of "ntasks" tasks, each with quantum
* "quantum".
@@ -90,7 +90,7 @@ isc_taskpool_size(isc_taskpool_t *pool);
*/
isc_result_t
isc_taskpool_expand(isc_taskpool_t **sourcep, unsigned int size,
isc_taskpool_expand(isc_taskpool_t **sourcep, unsigned int size, bool priv,
isc_taskpool_t **targetp);
/*%<
@@ -131,19 +131,6 @@ isc_taskpool_destroy(isc_taskpool_t **poolp);
* \li '*poolp' is a valid task pool.
*/
void
isc_taskpool_setprivilege(isc_taskpool_t *pool, bool priv);
/*%<
* Set the privilege flag on all tasks in 'pool' to 'priv'. If 'priv' is
* true, then when the task manager is set into privileged mode, only
* tasks wihin this pool will be able to execute. (Note: It is important
* to turn the pool tasks' privilege back off before the last task finishes
* executing.)
*
* Requires:
* \li 'pool' is a valid task pool.
*/
ISC_LANG_ENDDECLS
#endif /* ISC_TASKPOOL_H */