xref: /onnv-gate/usr/src/uts/common/os/taskq.c (revision 11854:5351ddd19d45)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
59515SJonathan.Adams@Sun.COM  * Common Development and Distribution License (the "License").
69515SJonathan.Adams@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
2211474SJonathan.Adams@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Kernel task queues: general-purpose asynchronous task scheduling.
280Sstevel@tonic-gate  *
290Sstevel@tonic-gate  * A common problem in kernel programming is the need to schedule tasks
300Sstevel@tonic-gate  * to be performed later, by another thread. There are several reasons
310Sstevel@tonic-gate  * you may want or need to do this:
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  * (1) The task isn't time-critical, but your current code path is.
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * (2) The task may require grabbing locks that you already hold.
360Sstevel@tonic-gate  *
370Sstevel@tonic-gate  * (3) The task may need to block (e.g. to wait for memory), but you
380Sstevel@tonic-gate  *     cannot block in your current context.
390Sstevel@tonic-gate  *
400Sstevel@tonic-gate  * (4) Your code path can't complete because of some condition, but you can't
410Sstevel@tonic-gate  *     sleep or fail, so you queue the task for later execution when condition
420Sstevel@tonic-gate  *     disappears.
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  * (5) You just want a simple way to launch multiple tasks in parallel.
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  * Task queues provide such a facility. In its simplest form (used when
470Sstevel@tonic-gate  * performance is not a critical consideration) a task queue consists of a
480Sstevel@tonic-gate  * single list of tasks, together with one or more threads to service the
490Sstevel@tonic-gate  * list. There are some cases when this simple queue is not sufficient:
500Sstevel@tonic-gate  *
510Sstevel@tonic-gate  * (1) The task queues are very hot and there is a need to avoid data and lock
520Sstevel@tonic-gate  *	contention over global resources.
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  * (2) Some tasks may depend on other tasks to complete, so they can't be put in
550Sstevel@tonic-gate  *	the same list managed by the same thread.
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  * (3) Some tasks may block for a long time, and this should not block other
5811173SJonathan.Adams@Sun.COM  *	tasks in the queue.
590Sstevel@tonic-gate  *
600Sstevel@tonic-gate  * To provide useful service in such cases we define a "dynamic task queue"
610Sstevel@tonic-gate  * which has an individual thread for each of the tasks. These threads are
620Sstevel@tonic-gate  * dynamically created as they are needed and destroyed when they are not in
630Sstevel@tonic-gate  * use. The API for managing task pools is the same as for managing task queues
640Sstevel@tonic-gate  * with the exception of a taskq creation flag TASKQ_DYNAMIC which tells that
650Sstevel@tonic-gate  * dynamic task pool behavior is desired.
660Sstevel@tonic-gate  *
670Sstevel@tonic-gate  * Dynamic task queues may also place tasks in the normal queue (called "backing
680Sstevel@tonic-gate  * queue") when task pool runs out of resources. Users of task queues may
690Sstevel@tonic-gate  * disallow such queued scheduling by specifying TQ_NOQUEUE in the dispatch
700Sstevel@tonic-gate  * flags.
710Sstevel@tonic-gate  *
720Sstevel@tonic-gate  * The backing task queue is also used for scheduling internal tasks needed for
730Sstevel@tonic-gate  * dynamic task queue maintenance.
740Sstevel@tonic-gate  *
759515SJonathan.Adams@Sun.COM  * INTERFACES ==================================================================
760Sstevel@tonic-gate  *
7711173SJonathan.Adams@Sun.COM  * taskq_t *taskq_create(name, nthreads, pri, minalloc, maxall, flags);
780Sstevel@tonic-gate  *
790Sstevel@tonic-gate  *	Create a taskq with specified properties.
800Sstevel@tonic-gate  *	Possible 'flags':
810Sstevel@tonic-gate  *
820Sstevel@tonic-gate  *	  TASKQ_DYNAMIC: Create task pool for task management. If this flag is
839515SJonathan.Adams@Sun.COM  *		specified, 'nthreads' specifies the maximum number of threads in
840Sstevel@tonic-gate  *		the task queue. Task execution order for dynamic task queues is
850Sstevel@tonic-gate  *		not predictable.
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  *		If this flag is not specified (default case) a
889515SJonathan.Adams@Sun.COM  *		single-list task queue is created with 'nthreads' threads
899515SJonathan.Adams@Sun.COM  *		servicing it. Entries in this queue are managed by
909515SJonathan.Adams@Sun.COM  *		taskq_ent_alloc() and taskq_ent_free() which try to keep the
919515SJonathan.Adams@Sun.COM  *		task population between 'minalloc' and 'maxalloc', but the
920Sstevel@tonic-gate  *		latter limit is only advisory for TQ_SLEEP dispatches and the
930Sstevel@tonic-gate  *		former limit is only advisory for TQ_NOALLOC dispatches. If
940Sstevel@tonic-gate  *		TASKQ_PREPOPULATE is set in 'flags', the taskq will be
950Sstevel@tonic-gate  *		prepopulated with 'minalloc' task structures.
960Sstevel@tonic-gate  *
970Sstevel@tonic-gate  *		Since non-DYNAMIC taskqs are queues, tasks are guaranteed to be
980Sstevel@tonic-gate  *		executed in the order they are scheduled if nthreads == 1.
990Sstevel@tonic-gate  *		If nthreads > 1, task execution order is not predictable.
1000Sstevel@tonic-gate  *
1010Sstevel@tonic-gate  *	  TASKQ_PREPOPULATE: Prepopulate task queue with threads.
1020Sstevel@tonic-gate  *		Also prepopulate the task queue with 'minalloc' task structures.
1030Sstevel@tonic-gate  *
1049515SJonathan.Adams@Sun.COM  *	  TASKQ_THREADS_CPU_PCT: This flag specifies that 'nthreads' should be
1059515SJonathan.Adams@Sun.COM  *		interpreted as a percentage of the # of online CPUs on the
1069515SJonathan.Adams@Sun.COM  *		system.  The taskq subsystem will automatically adjust the
1079515SJonathan.Adams@Sun.COM  *		number of threads in the taskq in response to CPU online
1089515SJonathan.Adams@Sun.COM  *		and offline events, to keep the ratio.  nthreads must be in
1099515SJonathan.Adams@Sun.COM  *		the range [0,100].
1109515SJonathan.Adams@Sun.COM  *
1119515SJonathan.Adams@Sun.COM  *		The calculation used is:
1129515SJonathan.Adams@Sun.COM  *
1139515SJonathan.Adams@Sun.COM  *			MAX((ncpus_online * percentage)/100, 1)
1149515SJonathan.Adams@Sun.COM  *
1159515SJonathan.Adams@Sun.COM  *		This flag is not supported for DYNAMIC task queues.
1169515SJonathan.Adams@Sun.COM  *		This flag is not compatible with TASKQ_CPR_SAFE.
1179515SJonathan.Adams@Sun.COM  *
1180Sstevel@tonic-gate  *	  TASKQ_CPR_SAFE: This flag specifies that users of the task queue will
1199515SJonathan.Adams@Sun.COM  *		use their own protocol for handling CPR issues. This flag is not
1209515SJonathan.Adams@Sun.COM  *		supported for DYNAMIC task queues.  This flag is not compatible
1219515SJonathan.Adams@Sun.COM  *		with TASKQ_THREADS_CPU_PCT.
1220Sstevel@tonic-gate  *
1230Sstevel@tonic-gate  *	The 'pri' field specifies the default priority for the threads that
1240Sstevel@tonic-gate  *	service all scheduled tasks.
1250Sstevel@tonic-gate  *
12611173SJonathan.Adams@Sun.COM  * taskq_t *taskq_create_instance(name, instance, nthreads, pri, minalloc,
12711173SJonathan.Adams@Sun.COM  *    maxall, flags);
12811173SJonathan.Adams@Sun.COM  *
12911173SJonathan.Adams@Sun.COM  *	Like taskq_create(), but takes an instance number (or -1 to indicate
13011173SJonathan.Adams@Sun.COM  *	no instance).
13111173SJonathan.Adams@Sun.COM  *
13211173SJonathan.Adams@Sun.COM  * taskq_t *taskq_create_proc(name, nthreads, pri, minalloc, maxall, proc,
13311173SJonathan.Adams@Sun.COM  *    flags);
13411173SJonathan.Adams@Sun.COM  *
13511173SJonathan.Adams@Sun.COM  *	Like taskq_create(), but creates the taskq threads in the specified
13611173SJonathan.Adams@Sun.COM  *	system process.  If proc != &p0, this must be called from a thread
13711173SJonathan.Adams@Sun.COM  *	in that process.
13811173SJonathan.Adams@Sun.COM  *
13911173SJonathan.Adams@Sun.COM  * taskq_t *taskq_create_sysdc(name, nthreads, minalloc, maxall, proc,
14011173SJonathan.Adams@Sun.COM  *    dc, flags);
14111173SJonathan.Adams@Sun.COM  *
14211173SJonathan.Adams@Sun.COM  *	Like taskq_create_proc(), but the taskq threads will use the
14311173SJonathan.Adams@Sun.COM  *	System Duty Cycle (SDC) scheduling class with a duty cycle of dc.
14411173SJonathan.Adams@Sun.COM  *
1450Sstevel@tonic-gate  * void taskq_destroy(tap):
1460Sstevel@tonic-gate  *
1470Sstevel@tonic-gate  *	Waits for any scheduled tasks to complete, then destroys the taskq.
1480Sstevel@tonic-gate  *	Caller should guarantee that no new tasks are scheduled in the closing
1490Sstevel@tonic-gate  *	taskq.
1500Sstevel@tonic-gate  *
1510Sstevel@tonic-gate  * taskqid_t taskq_dispatch(tq, func, arg, flags):
1520Sstevel@tonic-gate  *
1530Sstevel@tonic-gate  *	Dispatches the task "func(arg)" to taskq. The 'flags' indicates whether
1540Sstevel@tonic-gate  *	the caller is willing to block for memory.  The function returns an
1550Sstevel@tonic-gate  *	opaque value which is zero iff dispatch fails.  If flags is TQ_NOSLEEP
1560Sstevel@tonic-gate  *	or TQ_NOALLOC and the task can't be dispatched, taskq_dispatch() fails
1570Sstevel@tonic-gate  *	and returns (taskqid_t)0.
1580Sstevel@tonic-gate  *
1590Sstevel@tonic-gate  *	ASSUMES: func != NULL.
1600Sstevel@tonic-gate  *
1610Sstevel@tonic-gate  *	Possible flags:
1620Sstevel@tonic-gate  *	  TQ_NOSLEEP: Do not wait for resources; may fail.
1630Sstevel@tonic-gate  *
1640Sstevel@tonic-gate  *	  TQ_NOALLOC: Do not allocate memory; may fail.  May only be used with
1650Sstevel@tonic-gate  *		non-dynamic task queues.
1660Sstevel@tonic-gate  *
1670Sstevel@tonic-gate  *	  TQ_NOQUEUE: Do not enqueue a task if it can't dispatch it due to
1680Sstevel@tonic-gate  *		lack of available resources and fail. If this flag is not
16911173SJonathan.Adams@Sun.COM  *		set, and the task pool is exhausted, the task may be scheduled
1700Sstevel@tonic-gate  *		in the backing queue. This flag may ONLY be used with dynamic
1710Sstevel@tonic-gate  *		task queues.
1720Sstevel@tonic-gate  *
1730Sstevel@tonic-gate  *		NOTE: This flag should always be used when a task queue is used
1740Sstevel@tonic-gate  *		for tasks that may depend on each other for completion.
1750Sstevel@tonic-gate  *		Enqueueing dependent tasks may create deadlocks.
1760Sstevel@tonic-gate  *
1770Sstevel@tonic-gate  *	  TQ_SLEEP:   May block waiting for resources. May still fail for
17811173SJonathan.Adams@Sun.COM  *		dynamic task queues if TQ_NOQUEUE is also specified, otherwise
1790Sstevel@tonic-gate  *		always succeed.
1800Sstevel@tonic-gate  *
18111173SJonathan.Adams@Sun.COM  *	  TQ_FRONT:   Puts the new task at the front of the queue.  Be careful.
18211173SJonathan.Adams@Sun.COM  *
1830Sstevel@tonic-gate  *	NOTE: Dynamic task queues are much more likely to fail in
1840Sstevel@tonic-gate  *		taskq_dispatch() (especially if TQ_NOQUEUE was specified), so it
1850Sstevel@tonic-gate  *		is important to have backup strategies handling such failures.
1860Sstevel@tonic-gate  *
1870Sstevel@tonic-gate  * void taskq_wait(tq):
1880Sstevel@tonic-gate  *
1890Sstevel@tonic-gate  *	Waits for all previously scheduled tasks to complete.
1900Sstevel@tonic-gate  *
1910Sstevel@tonic-gate  *	NOTE: It does not stop any new task dispatches.
1920Sstevel@tonic-gate  *	      Do NOT call taskq_wait() from a task: it will cause deadlock.
1930Sstevel@tonic-gate  *
1940Sstevel@tonic-gate  * void taskq_suspend(tq)
1950Sstevel@tonic-gate  *
1960Sstevel@tonic-gate  *	Suspend all task execution. Tasks already scheduled for a dynamic task
1970Sstevel@tonic-gate  *	queue will still be executed, but all new scheduled tasks will be
1980Sstevel@tonic-gate  *	suspended until taskq_resume() is called.
1990Sstevel@tonic-gate  *
2000Sstevel@tonic-gate  * int  taskq_suspended(tq)
2010Sstevel@tonic-gate  *
2020Sstevel@tonic-gate  *	Returns 1 if taskq is suspended and 0 otherwise. It is intended to
2030Sstevel@tonic-gate  *	ASSERT that the task queue is suspended.
2040Sstevel@tonic-gate  *
2050Sstevel@tonic-gate  * void taskq_resume(tq)
2060Sstevel@tonic-gate  *
2070Sstevel@tonic-gate  *	Resume task queue execution.
2080Sstevel@tonic-gate  *
2090Sstevel@tonic-gate  * int  taskq_member(tq, thread)
2100Sstevel@tonic-gate  *
2110Sstevel@tonic-gate  *	Returns 1 if 'thread' belongs to taskq 'tq' and 0 otherwise. The
2120Sstevel@tonic-gate  *	intended use is to ASSERT that a given function is called in taskq
2130Sstevel@tonic-gate  *	context only.
2140Sstevel@tonic-gate  *
2150Sstevel@tonic-gate  * system_taskq
2160Sstevel@tonic-gate  *
2170Sstevel@tonic-gate  *	Global system-wide dynamic task queue for common uses. It may be used by
2180Sstevel@tonic-gate  *	any subsystem that needs to schedule tasks and does not need to manage
2190Sstevel@tonic-gate  *	its own task queues. It is initialized quite early during system boot.
2200Sstevel@tonic-gate  *
2219515SJonathan.Adams@Sun.COM  * IMPLEMENTATION ==============================================================
2220Sstevel@tonic-gate  *
2230Sstevel@tonic-gate  * This is schematic representation of the task queue structures.
2240Sstevel@tonic-gate  *
2250Sstevel@tonic-gate  *   taskq:
2260Sstevel@tonic-gate  *   +-------------+
22710889SJonathan.Adams@Sun.COM  *   | tq_lock     | +---< taskq_ent_free()
2280Sstevel@tonic-gate  *   +-------------+ |
2290Sstevel@tonic-gate  *   |...          | | tqent:                  tqent:
2300Sstevel@tonic-gate  *   +-------------+ | +------------+          +------------+
2310Sstevel@tonic-gate  *   | tq_freelist |-->| tqent_next |--> ... ->| tqent_next |
2320Sstevel@tonic-gate  *   +-------------+   +------------+          +------------+
2330Sstevel@tonic-gate  *   |...          |   | ...        |          | ...        |
2340Sstevel@tonic-gate  *   +-------------+   +------------+          +------------+
2350Sstevel@tonic-gate  *   | tq_task     |    |
2360Sstevel@tonic-gate  *   |             |    +-------------->taskq_ent_alloc()
2370Sstevel@tonic-gate  * +--------------------------------------------------------------------------+
2380Sstevel@tonic-gate  * | |                     |            tqent                   tqent         |
2390Sstevel@tonic-gate  * | +---------------------+     +--> +------------+     +--> +------------+  |
2400Sstevel@tonic-gate  * | | ...		   |     |    | func, arg  |     |    | func, arg  |  |
2410Sstevel@tonic-gate  * +>+---------------------+ <---|-+  +------------+ <---|-+  +------------+  |
2420Sstevel@tonic-gate  *   | tq_taskq.tqent_next | ----+ |  | tqent_next | --->+ |  | tqent_next |--+
2430Sstevel@tonic-gate  *   +---------------------+	   |  +------------+     ^ |  +------------+
2440Sstevel@tonic-gate  * +-| tq_task.tqent_prev  |	   +--| tqent_prev |     | +--| tqent_prev |  ^
2450Sstevel@tonic-gate  * | +---------------------+	      +------------+     |    +------------+  |
2460Sstevel@tonic-gate  * | |...		   |	      | ...        |     |    | ...        |  |
2470Sstevel@tonic-gate  * | +---------------------+	      +------------+     |    +------------+  |
2480Sstevel@tonic-gate  * |                                      ^              |                    |
2490Sstevel@tonic-gate  * |                                      |              |                    |
2500Sstevel@tonic-gate  * +--------------------------------------+--------------+       TQ_APPEND() -+
2510Sstevel@tonic-gate  *   |             |                      |
2520Sstevel@tonic-gate  *   |...          |   taskq_thread()-----+
2530Sstevel@tonic-gate  *   +-------------+
2540Sstevel@tonic-gate  *   | tq_buckets  |--+-------> [ NULL ] (for regular task queues)
2550Sstevel@tonic-gate  *   +-------------+  |
2560Sstevel@tonic-gate  *                    |   DYNAMIC TASK QUEUES:
2570Sstevel@tonic-gate  *                    |
25811173SJonathan.Adams@Sun.COM  *                    +-> taskq_bucket[nCPU]		taskq_bucket_dispatch()
2590Sstevel@tonic-gate  *                        +-------------------+                    ^
2600Sstevel@tonic-gate  *                   +--->| tqbucket_lock     |                    |
2610Sstevel@tonic-gate  *                   |    +-------------------+   +--------+      +--------+
2620Sstevel@tonic-gate  *                   |    | tqbucket_freelist |-->| tqent  |-->...| tqent  | ^
2630Sstevel@tonic-gate  *                   |    +-------------------+<--+--------+<--...+--------+ |
2640Sstevel@tonic-gate  *                   |    | ...               |   | thread |      | thread | |
2650Sstevel@tonic-gate  *                   |    +-------------------+   +--------+      +--------+ |
2660Sstevel@tonic-gate  *                   |    +-------------------+                              |
2670Sstevel@tonic-gate  * taskq_dispatch()--+--->| tqbucket_lock     |             TQ_APPEND()------+
2680Sstevel@tonic-gate  *      TQ_HASH()    |    +-------------------+   +--------+      +--------+
2690Sstevel@tonic-gate  *                   |    | tqbucket_freelist |-->| tqent  |-->...| tqent  |
2700Sstevel@tonic-gate  *                   |    +-------------------+<--+--------+<--...+--------+
2710Sstevel@tonic-gate  *                   |    | ...               |   | thread |      | thread |
2720Sstevel@tonic-gate  *                   |    +-------------------+   +--------+      +--------+
27311173SJonathan.Adams@Sun.COM  *		     +--->	...
2740Sstevel@tonic-gate  *
2750Sstevel@tonic-gate  *
2760Sstevel@tonic-gate  * Task queues use tq_task field to link new entry in the queue. The queue is a
2770Sstevel@tonic-gate  * circular doubly-linked list. Entries are put in the end of the list with
2780Sstevel@tonic-gate  * TQ_APPEND() and processed from the front of the list by taskq_thread() in
2790Sstevel@tonic-gate  * FIFO order. Task queue entries are cached in the free list managed by
2800Sstevel@tonic-gate  * taskq_ent_alloc() and taskq_ent_free() functions.
2810Sstevel@tonic-gate  *
2820Sstevel@tonic-gate  *	All threads used by task queues mark t_taskq field of the thread to
2830Sstevel@tonic-gate  *	point to the task queue.
2840Sstevel@tonic-gate  *
2859515SJonathan.Adams@Sun.COM  * Taskq Thread Management -----------------------------------------------------
2869515SJonathan.Adams@Sun.COM  *
2879515SJonathan.Adams@Sun.COM  * Taskq's non-dynamic threads are managed with several variables and flags:
2889515SJonathan.Adams@Sun.COM  *
2899515SJonathan.Adams@Sun.COM  *	* tq_nthreads	- The number of threads in taskq_thread() for the
2909515SJonathan.Adams@Sun.COM  *			  taskq.
2919515SJonathan.Adams@Sun.COM  *
2929515SJonathan.Adams@Sun.COM  *	* tq_active	- The number of threads not waiting on a CV in
2939515SJonathan.Adams@Sun.COM  *			  taskq_thread(); includes newly created threads
2949515SJonathan.Adams@Sun.COM  *			  not yet counted in tq_nthreads.
2959515SJonathan.Adams@Sun.COM  *
2969515SJonathan.Adams@Sun.COM  *	* tq_nthreads_target
2979515SJonathan.Adams@Sun.COM  *			- The number of threads desired for the taskq.
2989515SJonathan.Adams@Sun.COM  *
2999515SJonathan.Adams@Sun.COM  *	* tq_flags & TASKQ_CHANGING
3009515SJonathan.Adams@Sun.COM  *			- Indicates that tq_nthreads != tq_nthreads_target.
3019515SJonathan.Adams@Sun.COM  *
3029515SJonathan.Adams@Sun.COM  *	* tq_flags & TASKQ_THREAD_CREATED
3039515SJonathan.Adams@Sun.COM  *			- Indicates that a thread is being created in the taskq.
3049515SJonathan.Adams@Sun.COM  *
3059515SJonathan.Adams@Sun.COM  * During creation, tq_nthreads and tq_active are set to 0, and
3069515SJonathan.Adams@Sun.COM  * tq_nthreads_target is set to the number of threads desired.  The
30711173SJonathan.Adams@Sun.COM  * TASKQ_CHANGING flag is set, and taskq_thread_create() is called to
30811173SJonathan.Adams@Sun.COM  * create the first thread. taskq_thread_create() increments tq_active,
3099515SJonathan.Adams@Sun.COM  * sets TASKQ_THREAD_CREATED, and creates the new thread.
3109515SJonathan.Adams@Sun.COM  *
3119515SJonathan.Adams@Sun.COM  * Each thread starts in taskq_thread(), clears the TASKQ_THREAD_CREATED
3129515SJonathan.Adams@Sun.COM  * flag, and increments tq_nthreads.  It stores the new value of
3139515SJonathan.Adams@Sun.COM  * tq_nthreads as its "thread_id", and stores its thread pointer in the
3149515SJonathan.Adams@Sun.COM  * tq_threadlist at the (thread_id - 1).  We keep the thread_id space
3159515SJonathan.Adams@Sun.COM  * densely packed by requiring that only the largest thread_id can exit during
3169515SJonathan.Adams@Sun.COM  * normal adjustment.   The exception is during the destruction of the
3179515SJonathan.Adams@Sun.COM  * taskq; once tq_nthreads_target is set to zero, no new threads will be created
3189515SJonathan.Adams@Sun.COM  * for the taskq queue, so every thread can exit without any ordering being
3199515SJonathan.Adams@Sun.COM  * necessary.
3209515SJonathan.Adams@Sun.COM  *
3219515SJonathan.Adams@Sun.COM  * Threads will only process work if their thread id is <= tq_nthreads_target.
3229515SJonathan.Adams@Sun.COM  *
3239515SJonathan.Adams@Sun.COM  * When TASKQ_CHANGING is set, threads will check the current thread target
3249515SJonathan.Adams@Sun.COM  * whenever they wake up, and do whatever they can to apply its effects.
3259515SJonathan.Adams@Sun.COM  *
3269515SJonathan.Adams@Sun.COM  * TASKQ_THREAD_CPU_PCT --------------------------------------------------------
3279515SJonathan.Adams@Sun.COM  *
3289515SJonathan.Adams@Sun.COM  * When a taskq is created with TASKQ_THREAD_CPU_PCT, we store their requested
3299515SJonathan.Adams@Sun.COM  * percentage in tq_threads_ncpus_pct, start them off with the correct thread
3309515SJonathan.Adams@Sun.COM  * target, and add them to the taskq_cpupct_list for later adjustment.
3319515SJonathan.Adams@Sun.COM  *
3329515SJonathan.Adams@Sun.COM  * We register taskq_cpu_setup() to be called whenever a CPU changes state.  It
3339515SJonathan.Adams@Sun.COM  * walks the list of TASKQ_THREAD_CPU_PCT taskqs, adjusts their nthread_target
3349515SJonathan.Adams@Sun.COM  * if need be, and wakes up all of the threads to process the change.
3359515SJonathan.Adams@Sun.COM  *
3369515SJonathan.Adams@Sun.COM  * Dynamic Task Queues Implementation ------------------------------------------
3370Sstevel@tonic-gate  *
3380Sstevel@tonic-gate  * For a dynamic task queues there is a 1-to-1 mapping between a thread and
3390Sstevel@tonic-gate  * taskq_ent_structure. Each entry is serviced by its own thread and each thread
3400Sstevel@tonic-gate  * is controlled by a single entry.
3410Sstevel@tonic-gate  *
3420Sstevel@tonic-gate  * Entries are distributed over a set of buckets. To avoid using modulo
3430Sstevel@tonic-gate  * arithmetics the number of buckets is 2^n and is determined as the nearest
3440Sstevel@tonic-gate  * power of two roundown of the number of CPUs in the system. Tunable
3450Sstevel@tonic-gate  * variable 'taskq_maxbuckets' limits the maximum number of buckets. Each entry
3460Sstevel@tonic-gate  * is attached to a bucket for its lifetime and can't migrate to other buckets.
3470Sstevel@tonic-gate  *
3480Sstevel@tonic-gate  * Entries that have scheduled tasks are not placed in any list. The dispatch
3490Sstevel@tonic-gate  * function sets their "func" and "arg" fields and signals the corresponding
3500Sstevel@tonic-gate  * thread to execute the task. Once the thread executes the task it clears the
3510Sstevel@tonic-gate  * "func" field and places an entry on the bucket cache of free entries pointed
3520Sstevel@tonic-gate  * by "tqbucket_freelist" field. ALL entries on the free list should have "func"
3530Sstevel@tonic-gate  * field equal to NULL. The free list is a circular doubly-linked list identical
3540Sstevel@tonic-gate  * in structure to the tq_task list above, but entries are taken from it in LIFO
3550Sstevel@tonic-gate  * order - the last freed entry is the first to be allocated. The
3560Sstevel@tonic-gate  * taskq_bucket_dispatch() function gets the most recently used entry from the
3570Sstevel@tonic-gate  * free list, sets its "func" and "arg" fields and signals a worker thread.
3580Sstevel@tonic-gate  *
3590Sstevel@tonic-gate  * After executing each task a per-entry thread taskq_d_thread() places its
3600Sstevel@tonic-gate  * entry on the bucket free list and goes to a timed sleep. If it wakes up
3610Sstevel@tonic-gate  * without getting new task it removes the entry from the free list and destroys
3620Sstevel@tonic-gate  * itself. The thread sleep time is controlled by a tunable variable
3630Sstevel@tonic-gate  * `taskq_thread_timeout'.
3640Sstevel@tonic-gate  *
3659515SJonathan.Adams@Sun.COM  * There are various statistics kept in the bucket which allows for later
3660Sstevel@tonic-gate  * analysis of taskq usage patterns. Also, a global copy of taskq creation and
3670Sstevel@tonic-gate  * death statistics is kept in the global taskq data structure. Since thread
3680Sstevel@tonic-gate  * creation and death happen rarely, updating such global data does not present
3690Sstevel@tonic-gate  * a performance problem.
3700Sstevel@tonic-gate  *
3710Sstevel@tonic-gate  * NOTE: Threads are not bound to any CPU and there is absolutely no association
3720Sstevel@tonic-gate  *       between the bucket and actual thread CPU, so buckets are used only to
3730Sstevel@tonic-gate  *	 split resources and reduce resource contention. Having threads attached
3740Sstevel@tonic-gate  *	 to the CPU denoted by a bucket may reduce number of times the job
3750Sstevel@tonic-gate  *	 switches between CPUs.
3760Sstevel@tonic-gate  *
3770Sstevel@tonic-gate  *	 Current algorithm creates a thread whenever a bucket has no free
3780Sstevel@tonic-gate  *	 entries. It would be nice to know how many threads are in the running
3790Sstevel@tonic-gate  *	 state and don't create threads if all CPUs are busy with existing
3800Sstevel@tonic-gate  *	 tasks, but it is unclear how such strategy can be implemented.
3810Sstevel@tonic-gate  *
3820Sstevel@tonic-gate  *	 Currently buckets are created statically as an array attached to task
3830Sstevel@tonic-gate  *	 queue. On some system with nCPUs < max_ncpus it may waste system
3840Sstevel@tonic-gate  *	 memory. One solution may be allocation of buckets when they are first
3850Sstevel@tonic-gate  *	 touched, but it is not clear how useful it is.
3860Sstevel@tonic-gate  *
3879515SJonathan.Adams@Sun.COM  * SUSPEND/RESUME implementation -----------------------------------------------
3880Sstevel@tonic-gate  *
3890Sstevel@tonic-gate  *	Before executing a task taskq_thread() (executing non-dynamic task
3900Sstevel@tonic-gate  *	queues) obtains taskq's thread lock as a reader. The taskq_suspend()
3910Sstevel@tonic-gate  *	function gets the same lock as a writer blocking all non-dynamic task
3920Sstevel@tonic-gate  *	execution. The taskq_resume() function releases the lock allowing
3930Sstevel@tonic-gate  *	taskq_thread to continue execution.
3940Sstevel@tonic-gate  *
3950Sstevel@tonic-gate  *	For dynamic task queues, each bucket is marked as TQBUCKET_SUSPEND by
3960Sstevel@tonic-gate  *	taskq_suspend() function. After that taskq_bucket_dispatch() always
3970Sstevel@tonic-gate  *	fails, so that taskq_dispatch() will either enqueue tasks for a
3980Sstevel@tonic-gate  *	suspended backing queue or fail if TQ_NOQUEUE is specified in dispatch
3990Sstevel@tonic-gate  *	flags.
4000Sstevel@tonic-gate  *
4010Sstevel@tonic-gate  *	NOTE: taskq_suspend() does not immediately block any tasks already
4020Sstevel@tonic-gate  *	      scheduled for dynamic task queues. It only suspends new tasks
4030Sstevel@tonic-gate  *	      scheduled after taskq_suspend() was called.
4040Sstevel@tonic-gate  *
4050Sstevel@tonic-gate  *	taskq_member() function works by comparing a thread t_taskq pointer with
4060Sstevel@tonic-gate  *	the passed thread pointer.
4070Sstevel@tonic-gate  *
4089515SJonathan.Adams@Sun.COM  * LOCKS and LOCK Hierarchy ----------------------------------------------------
4090Sstevel@tonic-gate  *
4109515SJonathan.Adams@Sun.COM  *   There are three locks used in task queues:
4110Sstevel@tonic-gate  *
4129515SJonathan.Adams@Sun.COM  *   1) The taskq_t's tq_lock, protecting global task queue state.
4130Sstevel@tonic-gate  *
4140Sstevel@tonic-gate  *   2) Each per-CPU bucket has a lock for bucket management.
4150Sstevel@tonic-gate  *
4169515SJonathan.Adams@Sun.COM  *   3) The global taskq_cpupct_lock, which protects the list of
4179515SJonathan.Adams@Sun.COM  *      TASKQ_THREADS_CPU_PCT taskqs.
4189515SJonathan.Adams@Sun.COM  *
4199515SJonathan.Adams@Sun.COM  *   If both (1) and (2) are needed, tq_lock should be taken *after* the bucket
4200Sstevel@tonic-gate  *   lock.
4210Sstevel@tonic-gate  *
4229515SJonathan.Adams@Sun.COM  *   If both (1) and (3) are needed, tq_lock should be taken *after*
4239515SJonathan.Adams@Sun.COM  *   taskq_cpupct_lock.
4249515SJonathan.Adams@Sun.COM  *
4259515SJonathan.Adams@Sun.COM  * DEBUG FACILITIES ------------------------------------------------------------
4260Sstevel@tonic-gate  *
4270Sstevel@tonic-gate  * For DEBUG kernels it is possible to induce random failures to
4280Sstevel@tonic-gate  * taskq_dispatch() function when it is given TQ_NOSLEEP argument. The value of
4290Sstevel@tonic-gate  * taskq_dmtbf and taskq_smtbf tunables control the mean time between induced
4300Sstevel@tonic-gate  * failures for dynamic and static task queues respectively.
4310Sstevel@tonic-gate  *
4320Sstevel@tonic-gate  * Setting TASKQ_STATISTIC to 0 will disable per-bucket statistics.
4330Sstevel@tonic-gate  *
4349515SJonathan.Adams@Sun.COM  * TUNABLES --------------------------------------------------------------------
4350Sstevel@tonic-gate  *
4360Sstevel@tonic-gate  *	system_taskq_size	- Size of the global system_taskq.
4370Sstevel@tonic-gate  *				  This value is multiplied by nCPUs to determine
4380Sstevel@tonic-gate  *				  actual size.
4390Sstevel@tonic-gate  *				  Default value: 64
4400Sstevel@tonic-gate  *
4419515SJonathan.Adams@Sun.COM  *	taskq_minimum_nthreads_max
4429515SJonathan.Adams@Sun.COM  *				- Minimum size of the thread list for a taskq.
4439515SJonathan.Adams@Sun.COM  *				  Useful for testing different thread pool
4449515SJonathan.Adams@Sun.COM  *				  sizes by overwriting tq_nthreads_target.
4459515SJonathan.Adams@Sun.COM  *
4460Sstevel@tonic-gate  *	taskq_thread_timeout	- Maximum idle time for taskq_d_thread()
4470Sstevel@tonic-gate  *				  Default value: 5 minutes
4480Sstevel@tonic-gate  *
4490Sstevel@tonic-gate  *	taskq_maxbuckets	- Maximum number of buckets in any task queue
4500Sstevel@tonic-gate  *				  Default value: 128
4510Sstevel@tonic-gate  *
4520Sstevel@tonic-gate  *	taskq_search_depth	- Maximum # of buckets searched for a free entry
4530Sstevel@tonic-gate  *				  Default value: 4
4540Sstevel@tonic-gate  *
4550Sstevel@tonic-gate  *	taskq_dmtbf		- Mean time between induced dispatch failures
4560Sstevel@tonic-gate  *				  for dynamic task queues.
4570Sstevel@tonic-gate  *				  Default value: UINT_MAX (no induced failures)
4580Sstevel@tonic-gate  *
4590Sstevel@tonic-gate  *	taskq_smtbf		- Mean time between induced dispatch failures
4600Sstevel@tonic-gate  *				  for static task queues.
4610Sstevel@tonic-gate  *				  Default value: UINT_MAX (no induced failures)
4620Sstevel@tonic-gate  *
4639515SJonathan.Adams@Sun.COM  * CONDITIONAL compilation -----------------------------------------------------
4640Sstevel@tonic-gate  *
4650Sstevel@tonic-gate  *    TASKQ_STATISTIC	- If set will enable bucket statistic (default).
4660Sstevel@tonic-gate  *
4670Sstevel@tonic-gate  */
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate #include <sys/taskq_impl.h>
4700Sstevel@tonic-gate #include <sys/thread.h>
4710Sstevel@tonic-gate #include <sys/proc.h>
4720Sstevel@tonic-gate #include <sys/kmem.h>
4730Sstevel@tonic-gate #include <sys/vmem.h>
4740Sstevel@tonic-gate #include <sys/callb.h>
47511173SJonathan.Adams@Sun.COM #include <sys/class.h>
4760Sstevel@tonic-gate #include <sys/systm.h>
4770Sstevel@tonic-gate #include <sys/cmn_err.h>
4780Sstevel@tonic-gate #include <sys/debug.h>
4790Sstevel@tonic-gate #include <sys/vmsystm.h>	/* For throttlefree */
4800Sstevel@tonic-gate #include <sys/sysmacros.h>
4810Sstevel@tonic-gate #include <sys/cpuvar.h>
48211173SJonathan.Adams@Sun.COM #include <sys/cpupart.h>
4830Sstevel@tonic-gate #include <sys/sdt.h>
48411173SJonathan.Adams@Sun.COM #include <sys/sysdc.h>
4859515SJonathan.Adams@Sun.COM #include <sys/note.h>
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate static kmem_cache_t *taskq_ent_cache, *taskq_cache;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate /*
4909515SJonathan.Adams@Sun.COM  * Pseudo instance numbers for taskqs without explicitly provided instance.
4910Sstevel@tonic-gate  */
4920Sstevel@tonic-gate static vmem_t *taskq_id_arena;
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate /* Global system task queue for common use */
4950Sstevel@tonic-gate taskq_t	*system_taskq;
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate /*
4989515SJonathan.Adams@Sun.COM  * Maximum number of entries in global system taskq is
49911173SJonathan.Adams@Sun.COM  *	system_taskq_size * max_ncpus
5000Sstevel@tonic-gate  */
5010Sstevel@tonic-gate #define	SYSTEM_TASKQ_SIZE 64
5020Sstevel@tonic-gate int system_taskq_size = SYSTEM_TASKQ_SIZE;
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate /*
5059515SJonathan.Adams@Sun.COM  * Minimum size for tq_nthreads_max; useful for those who want to play around
5069515SJonathan.Adams@Sun.COM  * with increasing a taskq's tq_nthreads_target.
5079515SJonathan.Adams@Sun.COM  */
5089515SJonathan.Adams@Sun.COM int taskq_minimum_nthreads_max = 1;
5099515SJonathan.Adams@Sun.COM 
51011173SJonathan.Adams@Sun.COM /*
51111173SJonathan.Adams@Sun.COM  * We want to ensure that when taskq_create() returns, there is at least
51211173SJonathan.Adams@Sun.COM  * one thread ready to handle requests.  To guarantee this, we have to wait
51311173SJonathan.Adams@Sun.COM  * for the second thread, since the first one cannot process requests until
51411173SJonathan.Adams@Sun.COM  * the second thread has been created.
51511173SJonathan.Adams@Sun.COM  */
51611173SJonathan.Adams@Sun.COM #define	TASKQ_CREATE_ACTIVE_THREADS	2
51711173SJonathan.Adams@Sun.COM 
5189515SJonathan.Adams@Sun.COM /* Maximum percentage allowed for TASKQ_THREADS_CPU_PCT */
5199515SJonathan.Adams@Sun.COM #define	TASKQ_CPUPCT_MAX_PERCENT	1000
5209515SJonathan.Adams@Sun.COM int taskq_cpupct_max_percent = TASKQ_CPUPCT_MAX_PERCENT;
5219515SJonathan.Adams@Sun.COM 
5229515SJonathan.Adams@Sun.COM /*
5230Sstevel@tonic-gate  * Dynamic task queue threads that don't get any work within
5240Sstevel@tonic-gate  * taskq_thread_timeout destroy themselves
5250Sstevel@tonic-gate  */
5260Sstevel@tonic-gate #define	TASKQ_THREAD_TIMEOUT (60 * 5)
5270Sstevel@tonic-gate int taskq_thread_timeout = TASKQ_THREAD_TIMEOUT;
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate #define	TASKQ_MAXBUCKETS 128
5300Sstevel@tonic-gate int taskq_maxbuckets = TASKQ_MAXBUCKETS;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate /*
5330Sstevel@tonic-gate  * When a bucket has no available entries another buckets are tried.
5340Sstevel@tonic-gate  * taskq_search_depth parameter limits the amount of buckets that we search
5350Sstevel@tonic-gate  * before failing. This is mostly useful in systems with many CPUs where we may
5360Sstevel@tonic-gate  * spend too much time scanning busy buckets.
5370Sstevel@tonic-gate  */
5380Sstevel@tonic-gate #define	TASKQ_SEARCH_DEPTH 4
5390Sstevel@tonic-gate int taskq_search_depth = TASKQ_SEARCH_DEPTH;
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate /*
5420Sstevel@tonic-gate  * Hashing function: mix various bits of x. May be pretty much anything.
5430Sstevel@tonic-gate  */
5440Sstevel@tonic-gate #define	TQ_HASH(x) ((x) ^ ((x) >> 11) ^ ((x) >> 17) ^ ((x) ^ 27))
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate /*
5470Sstevel@tonic-gate  * We do not create any new threads when the system is low on memory and start
5480Sstevel@tonic-gate  * throttling memory allocations. The following macro tries to estimate such
5490Sstevel@tonic-gate  * condition.
5500Sstevel@tonic-gate  */
5510Sstevel@tonic-gate #define	ENOUGH_MEMORY() (freemem > throttlefree)
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate /*
5540Sstevel@tonic-gate  * Static functions.
5550Sstevel@tonic-gate  */
5560Sstevel@tonic-gate static taskq_t	*taskq_create_common(const char *, int, int, pri_t, int,
55711173SJonathan.Adams@Sun.COM     int, proc_t *, uint_t, uint_t);
5580Sstevel@tonic-gate static void taskq_thread(void *);
5590Sstevel@tonic-gate static void taskq_d_thread(taskq_ent_t *);
5600Sstevel@tonic-gate static void taskq_bucket_extend(void *);
5610Sstevel@tonic-gate static int  taskq_constructor(void *, void *, int);
5620Sstevel@tonic-gate static void taskq_destructor(void *, void *);
5630Sstevel@tonic-gate static int  taskq_ent_constructor(void *, void *, int);
5640Sstevel@tonic-gate static void taskq_ent_destructor(void *, void *);
5650Sstevel@tonic-gate static taskq_ent_t *taskq_ent_alloc(taskq_t *, int);
5660Sstevel@tonic-gate static void taskq_ent_free(taskq_t *, taskq_ent_t *);
567*11854SChris.Horne@Sun.COM static int taskq_ent_exists(taskq_t *, task_func_t, void *);
5680Sstevel@tonic-gate static taskq_ent_t *taskq_bucket_dispatch(taskq_bucket_t *, task_func_t,
5690Sstevel@tonic-gate     void *);
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate /*
5720Sstevel@tonic-gate  * Task queues kstats.
5730Sstevel@tonic-gate  */
5740Sstevel@tonic-gate struct taskq_kstat {
57511173SJonathan.Adams@Sun.COM 	kstat_named_t	tq_pid;
5760Sstevel@tonic-gate 	kstat_named_t	tq_tasks;
5770Sstevel@tonic-gate 	kstat_named_t	tq_executed;
5780Sstevel@tonic-gate 	kstat_named_t	tq_maxtasks;
5790Sstevel@tonic-gate 	kstat_named_t	tq_totaltime;
5800Sstevel@tonic-gate 	kstat_named_t	tq_nalloc;
5810Sstevel@tonic-gate 	kstat_named_t	tq_nactive;
5820Sstevel@tonic-gate 	kstat_named_t	tq_pri;
5830Sstevel@tonic-gate 	kstat_named_t	tq_nthreads;
5840Sstevel@tonic-gate } taskq_kstat = {
58511173SJonathan.Adams@Sun.COM 	{ "pid",		KSTAT_DATA_UINT64 },
5860Sstevel@tonic-gate 	{ "tasks",		KSTAT_DATA_UINT64 },
5870Sstevel@tonic-gate 	{ "executed",		KSTAT_DATA_UINT64 },
5880Sstevel@tonic-gate 	{ "maxtasks",		KSTAT_DATA_UINT64 },
5890Sstevel@tonic-gate 	{ "totaltime",		KSTAT_DATA_UINT64 },
5900Sstevel@tonic-gate 	{ "nactive",		KSTAT_DATA_UINT64 },
5910Sstevel@tonic-gate 	{ "nalloc",		KSTAT_DATA_UINT64 },
5920Sstevel@tonic-gate 	{ "priority",		KSTAT_DATA_UINT64 },
5930Sstevel@tonic-gate 	{ "threads",		KSTAT_DATA_UINT64 },
5940Sstevel@tonic-gate };
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate struct taskq_d_kstat {
5970Sstevel@tonic-gate 	kstat_named_t	tqd_pri;
5980Sstevel@tonic-gate 	kstat_named_t	tqd_btasks;
5990Sstevel@tonic-gate 	kstat_named_t	tqd_bexecuted;
6000Sstevel@tonic-gate 	kstat_named_t	tqd_bmaxtasks;
6010Sstevel@tonic-gate 	kstat_named_t	tqd_bnalloc;
6020Sstevel@tonic-gate 	kstat_named_t	tqd_bnactive;
6030Sstevel@tonic-gate 	kstat_named_t	tqd_btotaltime;
6040Sstevel@tonic-gate 	kstat_named_t	tqd_hits;
6050Sstevel@tonic-gate 	kstat_named_t	tqd_misses;
6060Sstevel@tonic-gate 	kstat_named_t	tqd_overflows;
6070Sstevel@tonic-gate 	kstat_named_t	tqd_tcreates;
6080Sstevel@tonic-gate 	kstat_named_t	tqd_tdeaths;
6090Sstevel@tonic-gate 	kstat_named_t	tqd_maxthreads;
6100Sstevel@tonic-gate 	kstat_named_t	tqd_nomem;
6110Sstevel@tonic-gate 	kstat_named_t	tqd_disptcreates;
6120Sstevel@tonic-gate 	kstat_named_t	tqd_totaltime;
6130Sstevel@tonic-gate 	kstat_named_t	tqd_nalloc;
6140Sstevel@tonic-gate 	kstat_named_t	tqd_nfree;
6150Sstevel@tonic-gate } taskq_d_kstat = {
6160Sstevel@tonic-gate 	{ "priority",		KSTAT_DATA_UINT64 },
6170Sstevel@tonic-gate 	{ "btasks",		KSTAT_DATA_UINT64 },
6180Sstevel@tonic-gate 	{ "bexecuted",		KSTAT_DATA_UINT64 },
6190Sstevel@tonic-gate 	{ "bmaxtasks",		KSTAT_DATA_UINT64 },
6200Sstevel@tonic-gate 	{ "bnalloc",		KSTAT_DATA_UINT64 },
6210Sstevel@tonic-gate 	{ "bnactive",		KSTAT_DATA_UINT64 },
6220Sstevel@tonic-gate 	{ "btotaltime",		KSTAT_DATA_UINT64 },
6230Sstevel@tonic-gate 	{ "hits",		KSTAT_DATA_UINT64 },
6240Sstevel@tonic-gate 	{ "misses",		KSTAT_DATA_UINT64 },
6250Sstevel@tonic-gate 	{ "overflows",		KSTAT_DATA_UINT64 },
6260Sstevel@tonic-gate 	{ "tcreates",		KSTAT_DATA_UINT64 },
6270Sstevel@tonic-gate 	{ "tdeaths",		KSTAT_DATA_UINT64 },
6280Sstevel@tonic-gate 	{ "maxthreads",		KSTAT_DATA_UINT64 },
6290Sstevel@tonic-gate 	{ "nomem",		KSTAT_DATA_UINT64 },
6300Sstevel@tonic-gate 	{ "disptcreates",	KSTAT_DATA_UINT64 },
6310Sstevel@tonic-gate 	{ "totaltime",		KSTAT_DATA_UINT64 },
6320Sstevel@tonic-gate 	{ "nalloc",		KSTAT_DATA_UINT64 },
6330Sstevel@tonic-gate 	{ "nfree",		KSTAT_DATA_UINT64 },
6340Sstevel@tonic-gate };
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate static kmutex_t taskq_kstat_lock;
6370Sstevel@tonic-gate static kmutex_t taskq_d_kstat_lock;
6380Sstevel@tonic-gate static int taskq_kstat_update(kstat_t *, int);
6390Sstevel@tonic-gate static int taskq_d_kstat_update(kstat_t *, int);
6400Sstevel@tonic-gate 
6419515SJonathan.Adams@Sun.COM /*
64211173SJonathan.Adams@Sun.COM  * List of all TASKQ_THREADS_CPU_PCT taskqs.
6439515SJonathan.Adams@Sun.COM  */
64411173SJonathan.Adams@Sun.COM static list_t taskq_cpupct_list;	/* protected by cpu_lock */
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate /*
6470Sstevel@tonic-gate  * Collect per-bucket statistic when TASKQ_STATISTIC is defined.
6480Sstevel@tonic-gate  */
6490Sstevel@tonic-gate #define	TASKQ_STATISTIC 1
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate #if TASKQ_STATISTIC
6520Sstevel@tonic-gate #define	TQ_STAT(b, x)	b->tqbucket_stat.x++
6530Sstevel@tonic-gate #else
6540Sstevel@tonic-gate #define	TQ_STAT(b, x)
6550Sstevel@tonic-gate #endif
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate /*
6580Sstevel@tonic-gate  * Random fault injection.
6590Sstevel@tonic-gate  */
6600Sstevel@tonic-gate uint_t taskq_random;
6610Sstevel@tonic-gate uint_t taskq_dmtbf = UINT_MAX;    /* mean time between injected failures */
6620Sstevel@tonic-gate uint_t taskq_smtbf = UINT_MAX;    /* mean time between injected failures */
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate /*
6650Sstevel@tonic-gate  * TQ_NOSLEEP dispatches on dynamic task queues are always allowed to fail.
6660Sstevel@tonic-gate  *
6670Sstevel@tonic-gate  * TQ_NOSLEEP dispatches on static task queues can't arbitrarily fail because
6680Sstevel@tonic-gate  * they could prepopulate the cache and make sure that they do not use more
6690Sstevel@tonic-gate  * then minalloc entries.  So, fault injection in this case insures that
6700Sstevel@tonic-gate  * either TASKQ_PREPOPULATE is not set or there are more entries allocated
6710Sstevel@tonic-gate  * than is specified by minalloc.  TQ_NOALLOC dispatches are always allowed
6720Sstevel@tonic-gate  * to fail, but for simplicity we treat them identically to TQ_NOSLEEP
6730Sstevel@tonic-gate  * dispatches.
6740Sstevel@tonic-gate  */
6750Sstevel@tonic-gate #ifdef DEBUG
6760Sstevel@tonic-gate #define	TASKQ_D_RANDOM_DISPATCH_FAILURE(tq, flag)		\
6770Sstevel@tonic-gate 	taskq_random = (taskq_random * 2416 + 374441) % 1771875;\
6780Sstevel@tonic-gate 	if ((flag & TQ_NOSLEEP) &&				\
6790Sstevel@tonic-gate 	    taskq_random < 1771875 / taskq_dmtbf) {		\
6800Sstevel@tonic-gate 		return (NULL);					\
6810Sstevel@tonic-gate 	}
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate #define	TASKQ_S_RANDOM_DISPATCH_FAILURE(tq, flag)		\
6840Sstevel@tonic-gate 	taskq_random = (taskq_random * 2416 + 374441) % 1771875;\
6850Sstevel@tonic-gate 	if ((flag & (TQ_NOSLEEP | TQ_NOALLOC)) &&		\
6860Sstevel@tonic-gate 	    (!(tq->tq_flags & TASKQ_PREPOPULATE) ||		\
6870Sstevel@tonic-gate 	    (tq->tq_nalloc > tq->tq_minalloc)) &&		\
6880Sstevel@tonic-gate 	    (taskq_random < (1771875 / taskq_smtbf))) {		\
6890Sstevel@tonic-gate 		mutex_exit(&tq->tq_lock);			\
6900Sstevel@tonic-gate 		return (NULL);					\
6910Sstevel@tonic-gate 	}
6920Sstevel@tonic-gate #else
6930Sstevel@tonic-gate #define	TASKQ_S_RANDOM_DISPATCH_FAILURE(tq, flag)
6940Sstevel@tonic-gate #define	TASKQ_D_RANDOM_DISPATCH_FAILURE(tq, flag)
6950Sstevel@tonic-gate #endif
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate #define	IS_EMPTY(l) (((l).tqent_prev == (l).tqent_next) &&	\
6980Sstevel@tonic-gate 	((l).tqent_prev == &(l)))
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate /*
7010Sstevel@tonic-gate  * Append `tqe' in the end of the doubly-linked list denoted by l.
7020Sstevel@tonic-gate  */
7030Sstevel@tonic-gate #define	TQ_APPEND(l, tqe) {					\
7040Sstevel@tonic-gate 	tqe->tqent_next = &l;					\
7050Sstevel@tonic-gate 	tqe->tqent_prev = l.tqent_prev;				\
7060Sstevel@tonic-gate 	tqe->tqent_next->tqent_prev = tqe;			\
7070Sstevel@tonic-gate 	tqe->tqent_prev->tqent_next = tqe;			\
7080Sstevel@tonic-gate }
70911173SJonathan.Adams@Sun.COM /*
71011173SJonathan.Adams@Sun.COM  * Prepend 'tqe' to the beginning of l
71111173SJonathan.Adams@Sun.COM  */
71211173SJonathan.Adams@Sun.COM #define	TQ_PREPEND(l, tqe) {					\
71311173SJonathan.Adams@Sun.COM 	tqe->tqent_next = l.tqent_next;				\
71411173SJonathan.Adams@Sun.COM 	tqe->tqent_prev = &l;					\
71511173SJonathan.Adams@Sun.COM 	tqe->tqent_next->tqent_prev = tqe;			\
71611173SJonathan.Adams@Sun.COM 	tqe->tqent_prev->tqent_next = tqe;			\
71711173SJonathan.Adams@Sun.COM }
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate /*
7200Sstevel@tonic-gate  * Schedule a task specified by func and arg into the task queue entry tqe.
7210Sstevel@tonic-gate  */
72211173SJonathan.Adams@Sun.COM #define	TQ_DO_ENQUEUE(tq, tqe, func, arg, front) {			\
72311173SJonathan.Adams@Sun.COM 	ASSERT(MUTEX_HELD(&tq->tq_lock));				\
72411173SJonathan.Adams@Sun.COM 	_NOTE(CONSTCOND)						\
72511173SJonathan.Adams@Sun.COM 	if (front) {							\
72611173SJonathan.Adams@Sun.COM 		TQ_PREPEND(tq->tq_task, tqe);				\
72711173SJonathan.Adams@Sun.COM 	} else {							\
72811173SJonathan.Adams@Sun.COM 		TQ_APPEND(tq->tq_task, tqe);				\
72911173SJonathan.Adams@Sun.COM 	}								\
73011173SJonathan.Adams@Sun.COM 	tqe->tqent_func = (func);					\
73111173SJonathan.Adams@Sun.COM 	tqe->tqent_arg = (arg);						\
73211173SJonathan.Adams@Sun.COM 	tq->tq_tasks++;							\
73311173SJonathan.Adams@Sun.COM 	if (tq->tq_tasks - tq->tq_executed > tq->tq_maxtasks)		\
7340Sstevel@tonic-gate 		tq->tq_maxtasks = tq->tq_tasks - tq->tq_executed;	\
73511173SJonathan.Adams@Sun.COM 	cv_signal(&tq->tq_dispatch_cv);					\
7360Sstevel@tonic-gate 	DTRACE_PROBE2(taskq__enqueue, taskq_t *, tq, taskq_ent_t *, tqe); \
7370Sstevel@tonic-gate }
7380Sstevel@tonic-gate 
73911173SJonathan.Adams@Sun.COM #define	TQ_ENQUEUE(tq, tqe, func, arg)					\
74011173SJonathan.Adams@Sun.COM 	TQ_DO_ENQUEUE(tq, tqe, func, arg, 0)
74111173SJonathan.Adams@Sun.COM 
74211173SJonathan.Adams@Sun.COM #define	TQ_ENQUEUE_FRONT(tq, tqe, func, arg)				\
74311173SJonathan.Adams@Sun.COM 	TQ_DO_ENQUEUE(tq, tqe, func, arg, 1)
74411173SJonathan.Adams@Sun.COM 
7450Sstevel@tonic-gate /*
7460Sstevel@tonic-gate  * Do-nothing task which may be used to prepopulate thread caches.
7470Sstevel@tonic-gate  */
7480Sstevel@tonic-gate /*ARGSUSED*/
7490Sstevel@tonic-gate void
nulltask(void * unused)7500Sstevel@tonic-gate nulltask(void *unused)
7510Sstevel@tonic-gate {
7520Sstevel@tonic-gate }
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate /*ARGSUSED*/
7550Sstevel@tonic-gate static int
taskq_constructor(void * buf,void * cdrarg,int kmflags)7560Sstevel@tonic-gate taskq_constructor(void *buf, void *cdrarg, int kmflags)
7570Sstevel@tonic-gate {
7580Sstevel@tonic-gate 	taskq_t *tq = buf;
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	bzero(tq, sizeof (taskq_t));
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	mutex_init(&tq->tq_lock, NULL, MUTEX_DEFAULT, NULL);
7630Sstevel@tonic-gate 	rw_init(&tq->tq_threadlock, NULL, RW_DEFAULT, NULL);
7640Sstevel@tonic-gate 	cv_init(&tq->tq_dispatch_cv, NULL, CV_DEFAULT, NULL);
7659515SJonathan.Adams@Sun.COM 	cv_init(&tq->tq_exit_cv, NULL, CV_DEFAULT, NULL);
7660Sstevel@tonic-gate 	cv_init(&tq->tq_wait_cv, NULL, CV_DEFAULT, NULL);
767*11854SChris.Horne@Sun.COM 	cv_init(&tq->tq_maxalloc_cv, NULL, CV_DEFAULT, NULL);
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	tq->tq_task.tqent_next = &tq->tq_task;
7700Sstevel@tonic-gate 	tq->tq_task.tqent_prev = &tq->tq_task;
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	return (0);
7730Sstevel@tonic-gate }
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate /*ARGSUSED*/
7760Sstevel@tonic-gate static void
taskq_destructor(void * buf,void * cdrarg)7770Sstevel@tonic-gate taskq_destructor(void *buf, void *cdrarg)
7780Sstevel@tonic-gate {
7790Sstevel@tonic-gate 	taskq_t *tq = buf;
7800Sstevel@tonic-gate 
7819515SJonathan.Adams@Sun.COM 	ASSERT(tq->tq_nthreads == 0);
7829515SJonathan.Adams@Sun.COM 	ASSERT(tq->tq_buckets == NULL);
7839515SJonathan.Adams@Sun.COM 	ASSERT(tq->tq_tcreates == 0);
7849515SJonathan.Adams@Sun.COM 	ASSERT(tq->tq_tdeaths == 0);
7859515SJonathan.Adams@Sun.COM 
7860Sstevel@tonic-gate 	mutex_destroy(&tq->tq_lock);
7870Sstevel@tonic-gate 	rw_destroy(&tq->tq_threadlock);
7880Sstevel@tonic-gate 	cv_destroy(&tq->tq_dispatch_cv);
7899515SJonathan.Adams@Sun.COM 	cv_destroy(&tq->tq_exit_cv);
7900Sstevel@tonic-gate 	cv_destroy(&tq->tq_wait_cv);
791*11854SChris.Horne@Sun.COM 	cv_destroy(&tq->tq_maxalloc_cv);
7920Sstevel@tonic-gate }
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate /*ARGSUSED*/
7950Sstevel@tonic-gate static int
taskq_ent_constructor(void * buf,void * cdrarg,int kmflags)7960Sstevel@tonic-gate taskq_ent_constructor(void *buf, void *cdrarg, int kmflags)
7970Sstevel@tonic-gate {
7980Sstevel@tonic-gate 	taskq_ent_t *tqe = buf;
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	tqe->tqent_thread = NULL;
8010Sstevel@tonic-gate 	cv_init(&tqe->tqent_cv, NULL, CV_DEFAULT, NULL);
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	return (0);
8040Sstevel@tonic-gate }
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate /*ARGSUSED*/
8070Sstevel@tonic-gate static void
taskq_ent_destructor(void * buf,void * cdrarg)8080Sstevel@tonic-gate taskq_ent_destructor(void *buf, void *cdrarg)
8090Sstevel@tonic-gate {
8100Sstevel@tonic-gate 	taskq_ent_t *tqe = buf;
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	ASSERT(tqe->tqent_thread == NULL);
8130Sstevel@tonic-gate 	cv_destroy(&tqe->tqent_cv);
8140Sstevel@tonic-gate }
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate void
taskq_init(void)8170Sstevel@tonic-gate taskq_init(void)
8180Sstevel@tonic-gate {
8190Sstevel@tonic-gate 	taskq_ent_cache = kmem_cache_create("taskq_ent_cache",
8200Sstevel@tonic-gate 	    sizeof (taskq_ent_t), 0, taskq_ent_constructor,
8210Sstevel@tonic-gate 	    taskq_ent_destructor, NULL, NULL, NULL, 0);
8220Sstevel@tonic-gate 	taskq_cache = kmem_cache_create("taskq_cache", sizeof (taskq_t),
8230Sstevel@tonic-gate 	    0, taskq_constructor, taskq_destructor, NULL, NULL, NULL, 0);
8240Sstevel@tonic-gate 	taskq_id_arena = vmem_create("taskq_id_arena",
8250Sstevel@tonic-gate 	    (void *)1, INT32_MAX, 1, NULL, NULL, NULL, 0,
8260Sstevel@tonic-gate 	    VM_SLEEP | VMC_IDENTIFIER);
8279515SJonathan.Adams@Sun.COM 
82811173SJonathan.Adams@Sun.COM 	list_create(&taskq_cpupct_list, sizeof (taskq_t),
82911173SJonathan.Adams@Sun.COM 	    offsetof(taskq_t, tq_cpupct_link));
83011173SJonathan.Adams@Sun.COM }
83111173SJonathan.Adams@Sun.COM 
83211173SJonathan.Adams@Sun.COM static void
taskq_update_nthreads(taskq_t * tq,uint_t ncpus)83311173SJonathan.Adams@Sun.COM taskq_update_nthreads(taskq_t *tq, uint_t ncpus)
83411173SJonathan.Adams@Sun.COM {
83511173SJonathan.Adams@Sun.COM 	uint_t newtarget = TASKQ_THREADS_PCT(ncpus, tq->tq_threads_ncpus_pct);
83611173SJonathan.Adams@Sun.COM 
83711173SJonathan.Adams@Sun.COM 	ASSERT(MUTEX_HELD(&cpu_lock));
83811173SJonathan.Adams@Sun.COM 	ASSERT(MUTEX_HELD(&tq->tq_lock));
83911173SJonathan.Adams@Sun.COM 
84011173SJonathan.Adams@Sun.COM 	/* We must be going from non-zero to non-zero; no exiting. */
84111173SJonathan.Adams@Sun.COM 	ASSERT3U(tq->tq_nthreads_target, !=, 0);
84211173SJonathan.Adams@Sun.COM 	ASSERT3U(newtarget, !=, 0);
84311173SJonathan.Adams@Sun.COM 
84411173SJonathan.Adams@Sun.COM 	ASSERT3U(newtarget, <=, tq->tq_nthreads_max);
84511173SJonathan.Adams@Sun.COM 	if (newtarget != tq->tq_nthreads_target) {
84611173SJonathan.Adams@Sun.COM 		tq->tq_flags |= TASKQ_CHANGING;
84711173SJonathan.Adams@Sun.COM 		tq->tq_nthreads_target = newtarget;
84811173SJonathan.Adams@Sun.COM 		cv_broadcast(&tq->tq_dispatch_cv);
84911173SJonathan.Adams@Sun.COM 		cv_broadcast(&tq->tq_exit_cv);
85011173SJonathan.Adams@Sun.COM 	}
85111173SJonathan.Adams@Sun.COM }
85211173SJonathan.Adams@Sun.COM 
85311173SJonathan.Adams@Sun.COM /* called during task queue creation */
85411173SJonathan.Adams@Sun.COM static void
taskq_cpupct_install(taskq_t * tq,cpupart_t * cpup)85511173SJonathan.Adams@Sun.COM taskq_cpupct_install(taskq_t *tq, cpupart_t *cpup)
85611173SJonathan.Adams@Sun.COM {
85711173SJonathan.Adams@Sun.COM 	ASSERT(tq->tq_flags & TASKQ_THREADS_CPU_PCT);
85811173SJonathan.Adams@Sun.COM 
85911173SJonathan.Adams@Sun.COM 	mutex_enter(&cpu_lock);
86011173SJonathan.Adams@Sun.COM 	mutex_enter(&tq->tq_lock);
86111173SJonathan.Adams@Sun.COM 	tq->tq_cpupart = cpup->cp_id;
86211173SJonathan.Adams@Sun.COM 	taskq_update_nthreads(tq, cpup->cp_ncpus);
86311173SJonathan.Adams@Sun.COM 	mutex_exit(&tq->tq_lock);
86411173SJonathan.Adams@Sun.COM 
86511173SJonathan.Adams@Sun.COM 	list_insert_tail(&taskq_cpupct_list, tq);
86611173SJonathan.Adams@Sun.COM 	mutex_exit(&cpu_lock);
86711173SJonathan.Adams@Sun.COM }
86811173SJonathan.Adams@Sun.COM 
86911173SJonathan.Adams@Sun.COM static void
taskq_cpupct_remove(taskq_t * tq)87011173SJonathan.Adams@Sun.COM taskq_cpupct_remove(taskq_t *tq)
87111173SJonathan.Adams@Sun.COM {
87211173SJonathan.Adams@Sun.COM 	ASSERT(tq->tq_flags & TASKQ_THREADS_CPU_PCT);
87311173SJonathan.Adams@Sun.COM 
87411173SJonathan.Adams@Sun.COM 	mutex_enter(&cpu_lock);
87511173SJonathan.Adams@Sun.COM 	list_remove(&taskq_cpupct_list, tq);
87611173SJonathan.Adams@Sun.COM 	mutex_exit(&cpu_lock);
8779515SJonathan.Adams@Sun.COM }
8789515SJonathan.Adams@Sun.COM 
8799515SJonathan.Adams@Sun.COM /*ARGSUSED*/
8809515SJonathan.Adams@Sun.COM static int
taskq_cpu_setup(cpu_setup_t what,int id,void * arg)8819515SJonathan.Adams@Sun.COM taskq_cpu_setup(cpu_setup_t what, int id, void *arg)
8829515SJonathan.Adams@Sun.COM {
88311173SJonathan.Adams@Sun.COM 	taskq_t *tq;
88411173SJonathan.Adams@Sun.COM 	cpupart_t *cp = cpu[id]->cpu_part;
88511173SJonathan.Adams@Sun.COM 	uint_t ncpus = cp->cp_ncpus;
88611173SJonathan.Adams@Sun.COM 
88711173SJonathan.Adams@Sun.COM 	ASSERT(MUTEX_HELD(&cpu_lock));
88811173SJonathan.Adams@Sun.COM 	ASSERT(ncpus > 0);
8899515SJonathan.Adams@Sun.COM 
89011173SJonathan.Adams@Sun.COM 	switch (what) {
89111173SJonathan.Adams@Sun.COM 	case CPU_OFF:
89211173SJonathan.Adams@Sun.COM 	case CPU_CPUPART_OUT:
89311173SJonathan.Adams@Sun.COM 		/* offlines are called *before* the cpu is offlined. */
89411173SJonathan.Adams@Sun.COM 		if (ncpus > 1)
89511173SJonathan.Adams@Sun.COM 			ncpus--;
89611173SJonathan.Adams@Sun.COM 		break;
8979515SJonathan.Adams@Sun.COM 
89811173SJonathan.Adams@Sun.COM 	case CPU_ON:
89911173SJonathan.Adams@Sun.COM 	case CPU_CPUPART_IN:
90011173SJonathan.Adams@Sun.COM 		break;
90111173SJonathan.Adams@Sun.COM 
90211173SJonathan.Adams@Sun.COM 	default:
90311173SJonathan.Adams@Sun.COM 		return (0);		/* doesn't affect cpu count */
9049515SJonathan.Adams@Sun.COM 	}
9059515SJonathan.Adams@Sun.COM 
90611173SJonathan.Adams@Sun.COM 	for (tq = list_head(&taskq_cpupct_list); tq != NULL;
90711173SJonathan.Adams@Sun.COM 	    tq = list_next(&taskq_cpupct_list, tq)) {
9089515SJonathan.Adams@Sun.COM 
9099515SJonathan.Adams@Sun.COM 		mutex_enter(&tq->tq_lock);
91011173SJonathan.Adams@Sun.COM 		/*
91111173SJonathan.Adams@Sun.COM 		 * If the taskq is part of the cpuset which is changing,
91211173SJonathan.Adams@Sun.COM 		 * update its nthreads_target.
91311173SJonathan.Adams@Sun.COM 		 */
91411173SJonathan.Adams@Sun.COM 		if (tq->tq_cpupart == cp->cp_id) {
91511173SJonathan.Adams@Sun.COM 			taskq_update_nthreads(tq, ncpus);
9169515SJonathan.Adams@Sun.COM 		}
9179515SJonathan.Adams@Sun.COM 		mutex_exit(&tq->tq_lock);
9189515SJonathan.Adams@Sun.COM 	}
9199515SJonathan.Adams@Sun.COM 	return (0);
9209515SJonathan.Adams@Sun.COM }
9219515SJonathan.Adams@Sun.COM 
9229515SJonathan.Adams@Sun.COM void
taskq_mp_init(void)9239515SJonathan.Adams@Sun.COM taskq_mp_init(void)
9249515SJonathan.Adams@Sun.COM {
9259515SJonathan.Adams@Sun.COM 	mutex_enter(&cpu_lock);
9269515SJonathan.Adams@Sun.COM 	register_cpu_setup_func(taskq_cpu_setup, NULL);
92711173SJonathan.Adams@Sun.COM 	/*
92811173SJonathan.Adams@Sun.COM 	 * Make sure we're up to date.  At this point in boot, there is only
92911173SJonathan.Adams@Sun.COM 	 * one processor set, so we only have to update the current CPU.
93011173SJonathan.Adams@Sun.COM 	 */
93111173SJonathan.Adams@Sun.COM 	(void) taskq_cpu_setup(CPU_ON, CPU->cpu_id, NULL);
9329515SJonathan.Adams@Sun.COM 	mutex_exit(&cpu_lock);
9330Sstevel@tonic-gate }
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate /*
9360Sstevel@tonic-gate  * Create global system dynamic task queue.
9370Sstevel@tonic-gate  */
9380Sstevel@tonic-gate void
system_taskq_init(void)9390Sstevel@tonic-gate system_taskq_init(void)
9400Sstevel@tonic-gate {
9410Sstevel@tonic-gate 	system_taskq = taskq_create_common("system_taskq", 0,
94211173SJonathan.Adams@Sun.COM 	    system_taskq_size * max_ncpus, minclsyspri, 4, 512, &p0, 0,
9430Sstevel@tonic-gate 	    TASKQ_DYNAMIC | TASKQ_PREPOPULATE);
9440Sstevel@tonic-gate }
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate /*
9470Sstevel@tonic-gate  * taskq_ent_alloc()
9480Sstevel@tonic-gate  *
9490Sstevel@tonic-gate  * Allocates a new taskq_ent_t structure either from the free list or from the
9500Sstevel@tonic-gate  * cache. Returns NULL if it can't be allocated.
9510Sstevel@tonic-gate  *
9520Sstevel@tonic-gate  * Assumes: tq->tq_lock is held.
9530Sstevel@tonic-gate  */
9540Sstevel@tonic-gate static taskq_ent_t *
taskq_ent_alloc(taskq_t * tq,int flags)9550Sstevel@tonic-gate taskq_ent_alloc(taskq_t *tq, int flags)
9560Sstevel@tonic-gate {
9570Sstevel@tonic-gate 	int kmflags = (flags & TQ_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP;
9580Sstevel@tonic-gate 	taskq_ent_t *tqe;
959*11854SChris.Horne@Sun.COM 	clock_t wait_time;
960*11854SChris.Horne@Sun.COM 	clock_t	wait_rv;
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tq->tq_lock));
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	/*
9650Sstevel@tonic-gate 	 * TQ_NOALLOC allocations are allowed to use the freelist, even if
9660Sstevel@tonic-gate 	 * we are below tq_minalloc.
9670Sstevel@tonic-gate 	 */
968*11854SChris.Horne@Sun.COM again:	if ((tqe = tq->tq_freelist) != NULL &&
9690Sstevel@tonic-gate 	    ((flags & TQ_NOALLOC) || tq->tq_nalloc >= tq->tq_minalloc)) {
9700Sstevel@tonic-gate 		tq->tq_freelist = tqe->tqent_next;
9710Sstevel@tonic-gate 	} else {
9720Sstevel@tonic-gate 		if (flags & TQ_NOALLOC)
9730Sstevel@tonic-gate 			return (NULL);
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 		if (tq->tq_nalloc >= tq->tq_maxalloc) {
976*11854SChris.Horne@Sun.COM 			if (kmflags & KM_NOSLEEP)
9770Sstevel@tonic-gate 				return (NULL);
978*11854SChris.Horne@Sun.COM 
9790Sstevel@tonic-gate 			/*
9800Sstevel@tonic-gate 			 * We don't want to exceed tq_maxalloc, but we can't
9810Sstevel@tonic-gate 			 * wait for other tasks to complete (and thus free up
9820Sstevel@tonic-gate 			 * task structures) without risking deadlock with
9830Sstevel@tonic-gate 			 * the caller.  So, we just delay for one second
984*11854SChris.Horne@Sun.COM 			 * to throttle the allocation rate. If we have tasks
985*11854SChris.Horne@Sun.COM 			 * complete before one second timeout expires then
986*11854SChris.Horne@Sun.COM 			 * taskq_ent_free will signal us and we will
987*11854SChris.Horne@Sun.COM 			 * immediately retry the allocation (reap free).
9880Sstevel@tonic-gate 			 */
989*11854SChris.Horne@Sun.COM 			wait_time = ddi_get_lbolt() + hz;
990*11854SChris.Horne@Sun.COM 			while (tq->tq_freelist == NULL) {
991*11854SChris.Horne@Sun.COM 				tq->tq_maxalloc_wait++;
992*11854SChris.Horne@Sun.COM 				wait_rv = cv_timedwait(&tq->tq_maxalloc_cv,
993*11854SChris.Horne@Sun.COM 				    &tq->tq_lock, wait_time);
994*11854SChris.Horne@Sun.COM 				tq->tq_maxalloc_wait--;
995*11854SChris.Horne@Sun.COM 				if (wait_rv == -1)
996*11854SChris.Horne@Sun.COM 					break;
997*11854SChris.Horne@Sun.COM 			}
998*11854SChris.Horne@Sun.COM 			if (tq->tq_freelist)
999*11854SChris.Horne@Sun.COM 				goto again;		/* reap freelist */
1000*11854SChris.Horne@Sun.COM 
10010Sstevel@tonic-gate 		}
1002*11854SChris.Horne@Sun.COM 		mutex_exit(&tq->tq_lock);
1003*11854SChris.Horne@Sun.COM 
10040Sstevel@tonic-gate 		tqe = kmem_cache_alloc(taskq_ent_cache, kmflags);
1005*11854SChris.Horne@Sun.COM 
10060Sstevel@tonic-gate 		mutex_enter(&tq->tq_lock);
10070Sstevel@tonic-gate 		if (tqe != NULL)
10080Sstevel@tonic-gate 			tq->tq_nalloc++;
10090Sstevel@tonic-gate 	}
10100Sstevel@tonic-gate 	return (tqe);
10110Sstevel@tonic-gate }
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate /*
10140Sstevel@tonic-gate  * taskq_ent_free()
10150Sstevel@tonic-gate  *
10160Sstevel@tonic-gate  * Free taskq_ent_t structure by either putting it on the free list or freeing
10170Sstevel@tonic-gate  * it to the cache.
10180Sstevel@tonic-gate  *
10190Sstevel@tonic-gate  * Assumes: tq->tq_lock is held.
10200Sstevel@tonic-gate  */
10210Sstevel@tonic-gate static void
taskq_ent_free(taskq_t * tq,taskq_ent_t * tqe)10220Sstevel@tonic-gate taskq_ent_free(taskq_t *tq, taskq_ent_t *tqe)
10230Sstevel@tonic-gate {
10240Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tq->tq_lock));
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 	if (tq->tq_nalloc <= tq->tq_minalloc) {
10270Sstevel@tonic-gate 		tqe->tqent_next = tq->tq_freelist;
10280Sstevel@tonic-gate 		tq->tq_freelist = tqe;
10290Sstevel@tonic-gate 	} else {
10300Sstevel@tonic-gate 		tq->tq_nalloc--;
10310Sstevel@tonic-gate 		mutex_exit(&tq->tq_lock);
10320Sstevel@tonic-gate 		kmem_cache_free(taskq_ent_cache, tqe);
10330Sstevel@tonic-gate 		mutex_enter(&tq->tq_lock);
10340Sstevel@tonic-gate 	}
1035*11854SChris.Horne@Sun.COM 
1036*11854SChris.Horne@Sun.COM 	if (tq->tq_maxalloc_wait)
1037*11854SChris.Horne@Sun.COM 		cv_signal(&tq->tq_maxalloc_cv);
1038*11854SChris.Horne@Sun.COM }
1039*11854SChris.Horne@Sun.COM 
1040*11854SChris.Horne@Sun.COM /*
1041*11854SChris.Horne@Sun.COM  * taskq_ent_exists()
1042*11854SChris.Horne@Sun.COM  *
1043*11854SChris.Horne@Sun.COM  * Return 1 if taskq already has entry for calling 'func(arg)'.
1044*11854SChris.Horne@Sun.COM  *
1045*11854SChris.Horne@Sun.COM  * Assumes: tq->tq_lock is held.
1046*11854SChris.Horne@Sun.COM  */
1047*11854SChris.Horne@Sun.COM static int
taskq_ent_exists(taskq_t * tq,task_func_t func,void * arg)1048*11854SChris.Horne@Sun.COM taskq_ent_exists(taskq_t *tq, task_func_t func, void *arg)
1049*11854SChris.Horne@Sun.COM {
1050*11854SChris.Horne@Sun.COM 	taskq_ent_t	*tqe;
1051*11854SChris.Horne@Sun.COM 
1052*11854SChris.Horne@Sun.COM 	ASSERT(MUTEX_HELD(&tq->tq_lock));
1053*11854SChris.Horne@Sun.COM 
1054*11854SChris.Horne@Sun.COM 	for (tqe = tq->tq_task.tqent_next; tqe != &tq->tq_task;
1055*11854SChris.Horne@Sun.COM 	    tqe = tqe->tqent_next)
1056*11854SChris.Horne@Sun.COM 		if ((tqe->tqent_func == func) && (tqe->tqent_arg == arg))
1057*11854SChris.Horne@Sun.COM 			return (1);
1058*11854SChris.Horne@Sun.COM 	return (0);
10590Sstevel@tonic-gate }
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate /*
10620Sstevel@tonic-gate  * Dispatch a task "func(arg)" to a free entry of bucket b.
10630Sstevel@tonic-gate  *
10640Sstevel@tonic-gate  * Assumes: no bucket locks is held.
10650Sstevel@tonic-gate  *
10660Sstevel@tonic-gate  * Returns: a pointer to an entry if dispatch was successful.
10670Sstevel@tonic-gate  *	    NULL if there are no free entries or if the bucket is suspended.
10680Sstevel@tonic-gate  */
10690Sstevel@tonic-gate static taskq_ent_t *
taskq_bucket_dispatch(taskq_bucket_t * b,task_func_t func,void * arg)10700Sstevel@tonic-gate taskq_bucket_dispatch(taskq_bucket_t *b, task_func_t func, void *arg)
10710Sstevel@tonic-gate {
10720Sstevel@tonic-gate 	taskq_ent_t *tqe;
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&b->tqbucket_lock));
10750Sstevel@tonic-gate 	ASSERT(func != NULL);
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	mutex_enter(&b->tqbucket_lock);
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 	ASSERT(b->tqbucket_nfree != 0 || IS_EMPTY(b->tqbucket_freelist));
10800Sstevel@tonic-gate 	ASSERT(b->tqbucket_nfree == 0 || !IS_EMPTY(b->tqbucket_freelist));
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	/*
10830Sstevel@tonic-gate 	 * Get en entry from the freelist if there is one.
10840Sstevel@tonic-gate 	 * Schedule task into the entry.
10850Sstevel@tonic-gate 	 */
10860Sstevel@tonic-gate 	if ((b->tqbucket_nfree != 0) &&
10870Sstevel@tonic-gate 	    !(b->tqbucket_flags & TQBUCKET_SUSPEND)) {
10880Sstevel@tonic-gate 		tqe = b->tqbucket_freelist.tqent_prev;
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 		ASSERT(tqe != &b->tqbucket_freelist);
10910Sstevel@tonic-gate 		ASSERT(tqe->tqent_thread != NULL);
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 		tqe->tqent_prev->tqent_next = tqe->tqent_next;
10940Sstevel@tonic-gate 		tqe->tqent_next->tqent_prev = tqe->tqent_prev;
10950Sstevel@tonic-gate 		b->tqbucket_nalloc++;
10960Sstevel@tonic-gate 		b->tqbucket_nfree--;
10970Sstevel@tonic-gate 		tqe->tqent_func = func;
10980Sstevel@tonic-gate 		tqe->tqent_arg = arg;
10990Sstevel@tonic-gate 		TQ_STAT(b, tqs_hits);
11000Sstevel@tonic-gate 		cv_signal(&tqe->tqent_cv);
11010Sstevel@tonic-gate 		DTRACE_PROBE2(taskq__d__enqueue, taskq_bucket_t *, b,
11020Sstevel@tonic-gate 		    taskq_ent_t *, tqe);
11030Sstevel@tonic-gate 	} else {
11040Sstevel@tonic-gate 		tqe = NULL;
11050Sstevel@tonic-gate 		TQ_STAT(b, tqs_misses);
11060Sstevel@tonic-gate 	}
11070Sstevel@tonic-gate 	mutex_exit(&b->tqbucket_lock);
11080Sstevel@tonic-gate 	return (tqe);
11090Sstevel@tonic-gate }
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate /*
11120Sstevel@tonic-gate  * Dispatch a task.
11130Sstevel@tonic-gate  *
11140Sstevel@tonic-gate  * Assumes: func != NULL
11150Sstevel@tonic-gate  *
11160Sstevel@tonic-gate  * Returns: NULL if dispatch failed.
11170Sstevel@tonic-gate  *	    non-NULL if task dispatched successfully.
11180Sstevel@tonic-gate  *	    Actual return value is the pointer to taskq entry that was used to
11190Sstevel@tonic-gate  *	    dispatch a task. This is useful for debugging.
11200Sstevel@tonic-gate  */
11210Sstevel@tonic-gate /* ARGSUSED */
11220Sstevel@tonic-gate taskqid_t
taskq_dispatch(taskq_t * tq,task_func_t func,void * arg,uint_t flags)11230Sstevel@tonic-gate taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags)
11240Sstevel@tonic-gate {
11250Sstevel@tonic-gate 	taskq_bucket_t *bucket = NULL;	/* Which bucket needs extension */
11260Sstevel@tonic-gate 	taskq_ent_t *tqe = NULL;
11270Sstevel@tonic-gate 	taskq_ent_t *tqe1;
11280Sstevel@tonic-gate 	uint_t bsize;
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 	ASSERT(tq != NULL);
11310Sstevel@tonic-gate 	ASSERT(func != NULL);
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	if (!(tq->tq_flags & TASKQ_DYNAMIC)) {
11340Sstevel@tonic-gate 		/*
11350Sstevel@tonic-gate 		 * TQ_NOQUEUE flag can't be used with non-dynamic task queues.
11360Sstevel@tonic-gate 		 */
11370Sstevel@tonic-gate 		ASSERT(! (flags & TQ_NOQUEUE));
11380Sstevel@tonic-gate 		/*
11390Sstevel@tonic-gate 		 * Enqueue the task to the underlying queue.
11400Sstevel@tonic-gate 		 */
11410Sstevel@tonic-gate 		mutex_enter(&tq->tq_lock);
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 		TASKQ_S_RANDOM_DISPATCH_FAILURE(tq, flags);
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 		if ((tqe = taskq_ent_alloc(tq, flags)) == NULL) {
11460Sstevel@tonic-gate 			mutex_exit(&tq->tq_lock);
11470Sstevel@tonic-gate 			return (NULL);
11480Sstevel@tonic-gate 		}
114911173SJonathan.Adams@Sun.COM 		if (flags & TQ_FRONT) {
115011173SJonathan.Adams@Sun.COM 			TQ_ENQUEUE_FRONT(tq, tqe, func, arg);
115111173SJonathan.Adams@Sun.COM 		} else {
115211173SJonathan.Adams@Sun.COM 			TQ_ENQUEUE(tq, tqe, func, arg);
115311173SJonathan.Adams@Sun.COM 		}
11540Sstevel@tonic-gate 		mutex_exit(&tq->tq_lock);
11550Sstevel@tonic-gate 		return ((taskqid_t)tqe);
11560Sstevel@tonic-gate 	}
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	/*
11590Sstevel@tonic-gate 	 * Dynamic taskq dispatching.
11600Sstevel@tonic-gate 	 */
116111173SJonathan.Adams@Sun.COM 	ASSERT(!(flags & (TQ_NOALLOC | TQ_FRONT)));
11620Sstevel@tonic-gate 	TASKQ_D_RANDOM_DISPATCH_FAILURE(tq, flags);
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 	bsize = tq->tq_nbuckets;
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 	if (bsize == 1) {
11670Sstevel@tonic-gate 		/*
11680Sstevel@tonic-gate 		 * In a single-CPU case there is only one bucket, so get
11690Sstevel@tonic-gate 		 * entry directly from there.
11700Sstevel@tonic-gate 		 */
11710Sstevel@tonic-gate 		if ((tqe = taskq_bucket_dispatch(tq->tq_buckets, func, arg))
11720Sstevel@tonic-gate 		    != NULL)
11730Sstevel@tonic-gate 			return ((taskqid_t)tqe);	/* Fastpath */
11740Sstevel@tonic-gate 		bucket = tq->tq_buckets;
11750Sstevel@tonic-gate 	} else {
11760Sstevel@tonic-gate 		int loopcount;
11770Sstevel@tonic-gate 		taskq_bucket_t *b;
11780Sstevel@tonic-gate 		uintptr_t h = ((uintptr_t)CPU + (uintptr_t)arg) >> 3;
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 		h = TQ_HASH(h);
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 		/*
11830Sstevel@tonic-gate 		 * The 'bucket' points to the original bucket that we hit. If we
11840Sstevel@tonic-gate 		 * can't allocate from it, we search other buckets, but only
11850Sstevel@tonic-gate 		 * extend this one.
11860Sstevel@tonic-gate 		 */
11870Sstevel@tonic-gate 		b = &tq->tq_buckets[h & (bsize - 1)];
11880Sstevel@tonic-gate 		ASSERT(b->tqbucket_taskq == tq);	/* Sanity check */
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 		/*
11910Sstevel@tonic-gate 		 * Do a quick check before grabbing the lock. If the bucket does
11920Sstevel@tonic-gate 		 * not have free entries now, chances are very small that it
11930Sstevel@tonic-gate 		 * will after we take the lock, so we just skip it.
11940Sstevel@tonic-gate 		 */
11950Sstevel@tonic-gate 		if (b->tqbucket_nfree != 0) {
11960Sstevel@tonic-gate 			if ((tqe = taskq_bucket_dispatch(b, func, arg)) != NULL)
11970Sstevel@tonic-gate 				return ((taskqid_t)tqe);	/* Fastpath */
11980Sstevel@tonic-gate 		} else {
11990Sstevel@tonic-gate 			TQ_STAT(b, tqs_misses);
12000Sstevel@tonic-gate 		}
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 		bucket = b;
12030Sstevel@tonic-gate 		loopcount = MIN(taskq_search_depth, bsize);
12040Sstevel@tonic-gate 		/*
12050Sstevel@tonic-gate 		 * If bucket dispatch failed, search loopcount number of buckets
12060Sstevel@tonic-gate 		 * before we give up and fail.
12070Sstevel@tonic-gate 		 */
12080Sstevel@tonic-gate 		do {
12090Sstevel@tonic-gate 			b = &tq->tq_buckets[++h & (bsize - 1)];
12100Sstevel@tonic-gate 			ASSERT(b->tqbucket_taskq == tq);  /* Sanity check */
12110Sstevel@tonic-gate 			loopcount--;
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 			if (b->tqbucket_nfree != 0) {
12140Sstevel@tonic-gate 				tqe = taskq_bucket_dispatch(b, func, arg);
12150Sstevel@tonic-gate 			} else {
12160Sstevel@tonic-gate 				TQ_STAT(b, tqs_misses);
12170Sstevel@tonic-gate 			}
12180Sstevel@tonic-gate 		} while ((tqe == NULL) && (loopcount > 0));
12190Sstevel@tonic-gate 	}
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 	/*
12220Sstevel@tonic-gate 	 * At this point we either scheduled a task and (tqe != NULL) or failed
12230Sstevel@tonic-gate 	 * (tqe == NULL). Try to recover from fails.
12240Sstevel@tonic-gate 	 */
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 	/*
12270Sstevel@tonic-gate 	 * For KM_SLEEP dispatches, try to extend the bucket and retry dispatch.
12280Sstevel@tonic-gate 	 */
12290Sstevel@tonic-gate 	if ((tqe == NULL) && !(flags & TQ_NOSLEEP)) {
12300Sstevel@tonic-gate 		/*
12310Sstevel@tonic-gate 		 * taskq_bucket_extend() may fail to do anything, but this is
12320Sstevel@tonic-gate 		 * fine - we deal with it later. If the bucket was successfully
12330Sstevel@tonic-gate 		 * extended, there is a good chance that taskq_bucket_dispatch()
12340Sstevel@tonic-gate 		 * will get this new entry, unless someone is racing with us and
12350Sstevel@tonic-gate 		 * stealing the new entry from under our nose.
12360Sstevel@tonic-gate 		 * taskq_bucket_extend() may sleep.
12370Sstevel@tonic-gate 		 */
12380Sstevel@tonic-gate 		taskq_bucket_extend(bucket);
12390Sstevel@tonic-gate 		TQ_STAT(bucket, tqs_disptcreates);
12400Sstevel@tonic-gate 		if ((tqe = taskq_bucket_dispatch(bucket, func, arg)) != NULL)
12410Sstevel@tonic-gate 			return ((taskqid_t)tqe);
12420Sstevel@tonic-gate 	}
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 	ASSERT(bucket != NULL);
1245*11854SChris.Horne@Sun.COM 
12460Sstevel@tonic-gate 	/*
1247*11854SChris.Horne@Sun.COM 	 * Since there are not enough free entries in the bucket, add a
1248*11854SChris.Horne@Sun.COM 	 * taskq entry to extend it in the background using backing queue
1249*11854SChris.Horne@Sun.COM 	 * (unless we already have a taskq entry to perform that extension).
12500Sstevel@tonic-gate 	 */
12510Sstevel@tonic-gate 	mutex_enter(&tq->tq_lock);
1252*11854SChris.Horne@Sun.COM 	if (!taskq_ent_exists(tq, taskq_bucket_extend, bucket)) {
1253*11854SChris.Horne@Sun.COM 		if ((tqe1 = taskq_ent_alloc(tq, TQ_NOSLEEP)) != NULL) {
1254*11854SChris.Horne@Sun.COM 			TQ_ENQUEUE_FRONT(tq, tqe1, taskq_bucket_extend, bucket);
1255*11854SChris.Horne@Sun.COM 		} else {
1256*11854SChris.Horne@Sun.COM 			TQ_STAT(bucket, tqs_nomem);
1257*11854SChris.Horne@Sun.COM 		}
12580Sstevel@tonic-gate 	}
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	/*
12610Sstevel@tonic-gate 	 * Dispatch failed and we can't find an entry to schedule a task.
12620Sstevel@tonic-gate 	 * Revert to the backing queue unless TQ_NOQUEUE was asked.
12630Sstevel@tonic-gate 	 */
12640Sstevel@tonic-gate 	if ((tqe == NULL) && !(flags & TQ_NOQUEUE)) {
12650Sstevel@tonic-gate 		if ((tqe = taskq_ent_alloc(tq, flags)) != NULL) {
12660Sstevel@tonic-gate 			TQ_ENQUEUE(tq, tqe, func, arg);
12670Sstevel@tonic-gate 		} else {
12680Sstevel@tonic-gate 			TQ_STAT(bucket, tqs_nomem);
12690Sstevel@tonic-gate 		}
12700Sstevel@tonic-gate 	}
12710Sstevel@tonic-gate 	mutex_exit(&tq->tq_lock);
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	return ((taskqid_t)tqe);
12740Sstevel@tonic-gate }
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate /*
12770Sstevel@tonic-gate  * Wait for all pending tasks to complete.
12780Sstevel@tonic-gate  * Calling taskq_wait from a task will cause deadlock.
12790Sstevel@tonic-gate  */
12800Sstevel@tonic-gate void
taskq_wait(taskq_t * tq)12810Sstevel@tonic-gate taskq_wait(taskq_t *tq)
12820Sstevel@tonic-gate {
12830Sstevel@tonic-gate 	ASSERT(tq != curthread->t_taskq);
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 	mutex_enter(&tq->tq_lock);
12860Sstevel@tonic-gate 	while (tq->tq_task.tqent_next != &tq->tq_task || tq->tq_active != 0)
12870Sstevel@tonic-gate 		cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
12880Sstevel@tonic-gate 	mutex_exit(&tq->tq_lock);
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 	if (tq->tq_flags & TASKQ_DYNAMIC) {
12910Sstevel@tonic-gate 		taskq_bucket_t *b = tq->tq_buckets;
12920Sstevel@tonic-gate 		int bid = 0;
12930Sstevel@tonic-gate 		for (; (b != NULL) && (bid < tq->tq_nbuckets); b++, bid++) {
12940Sstevel@tonic-gate 			mutex_enter(&b->tqbucket_lock);
12950Sstevel@tonic-gate 			while (b->tqbucket_nalloc > 0)
12960Sstevel@tonic-gate 				cv_wait(&b->tqbucket_cv, &b->tqbucket_lock);
12970Sstevel@tonic-gate 			mutex_exit(&b->tqbucket_lock);
12980Sstevel@tonic-gate 		}
12990Sstevel@tonic-gate 	}
13000Sstevel@tonic-gate }
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate /*
13030Sstevel@tonic-gate  * Suspend execution of tasks.
13040Sstevel@tonic-gate  *
13050Sstevel@tonic-gate  * Tasks in the queue part will be suspended immediately upon return from this
13060Sstevel@tonic-gate  * function. Pending tasks in the dynamic part will continue to execute, but all
13070Sstevel@tonic-gate  * new tasks will  be suspended.
13080Sstevel@tonic-gate  */
13090Sstevel@tonic-gate void
taskq_suspend(taskq_t * tq)13100Sstevel@tonic-gate taskq_suspend(taskq_t *tq)
13110Sstevel@tonic-gate {
13120Sstevel@tonic-gate 	rw_enter(&tq->tq_threadlock, RW_WRITER);
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	if (tq->tq_flags & TASKQ_DYNAMIC) {
13150Sstevel@tonic-gate 		taskq_bucket_t *b = tq->tq_buckets;
13160Sstevel@tonic-gate 		int bid = 0;
13170Sstevel@tonic-gate 		for (; (b != NULL) && (bid < tq->tq_nbuckets); b++, bid++) {
13180Sstevel@tonic-gate 			mutex_enter(&b->tqbucket_lock);
13190Sstevel@tonic-gate 			b->tqbucket_flags |= TQBUCKET_SUSPEND;
13200Sstevel@tonic-gate 			mutex_exit(&b->tqbucket_lock);
13210Sstevel@tonic-gate 		}
13220Sstevel@tonic-gate 	}
13230Sstevel@tonic-gate 	/*
13240Sstevel@tonic-gate 	 * Mark task queue as being suspended. Needed for taskq_suspended().
13250Sstevel@tonic-gate 	 */
13260Sstevel@tonic-gate 	mutex_enter(&tq->tq_lock);
13270Sstevel@tonic-gate 	ASSERT(!(tq->tq_flags & TASKQ_SUSPENDED));
13280Sstevel@tonic-gate 	tq->tq_flags |= TASKQ_SUSPENDED;
13290Sstevel@tonic-gate 	mutex_exit(&tq->tq_lock);
13300Sstevel@tonic-gate }
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate /*
13330Sstevel@tonic-gate  * returns: 1 if tq is suspended, 0 otherwise.
13340Sstevel@tonic-gate  */
13350Sstevel@tonic-gate int
taskq_suspended(taskq_t * tq)13360Sstevel@tonic-gate taskq_suspended(taskq_t *tq)
13370Sstevel@tonic-gate {
13380Sstevel@tonic-gate 	return ((tq->tq_flags & TASKQ_SUSPENDED) != 0);
13390Sstevel@tonic-gate }
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate /*
13420Sstevel@tonic-gate  * Resume taskq execution.
13430Sstevel@tonic-gate  */
13440Sstevel@tonic-gate void
taskq_resume(taskq_t * tq)13450Sstevel@tonic-gate taskq_resume(taskq_t *tq)
13460Sstevel@tonic-gate {
13470Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&tq->tq_threadlock));
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 	if (tq->tq_flags & TASKQ_DYNAMIC) {
13500Sstevel@tonic-gate 		taskq_bucket_t *b = tq->tq_buckets;
13510Sstevel@tonic-gate 		int bid = 0;
13520Sstevel@tonic-gate 		for (; (b != NULL) && (bid < tq->tq_nbuckets); b++, bid++) {
13530Sstevel@tonic-gate 			mutex_enter(&b->tqbucket_lock);
13540Sstevel@tonic-gate 			b->tqbucket_flags &= ~TQBUCKET_SUSPEND;
13550Sstevel@tonic-gate 			mutex_exit(&b->tqbucket_lock);
13560Sstevel@tonic-gate 		}
13570Sstevel@tonic-gate 	}
13580Sstevel@tonic-gate 	mutex_enter(&tq->tq_lock);
13590Sstevel@tonic-gate 	ASSERT(tq->tq_flags & TASKQ_SUSPENDED);
13600Sstevel@tonic-gate 	tq->tq_flags &= ~TASKQ_SUSPENDED;
13610Sstevel@tonic-gate 	mutex_exit(&tq->tq_lock);
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 	rw_exit(&tq->tq_threadlock);
13640Sstevel@tonic-gate }
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate int
taskq_member(taskq_t * tq,kthread_t * thread)13670Sstevel@tonic-gate taskq_member(taskq_t *tq, kthread_t *thread)
13680Sstevel@tonic-gate {
13690Sstevel@tonic-gate 	return (thread->t_taskq == tq);
13700Sstevel@tonic-gate }
13710Sstevel@tonic-gate 
137211173SJonathan.Adams@Sun.COM /*
137311173SJonathan.Adams@Sun.COM  * Creates a thread in the taskq.  We only allow one outstanding create at
137411173SJonathan.Adams@Sun.COM  * a time.  We drop and reacquire the tq_lock in order to avoid blocking other
137511173SJonathan.Adams@Sun.COM  * taskq activity while thread_create() or lwp_kernel_create() run.
137611173SJonathan.Adams@Sun.COM  *
137711173SJonathan.Adams@Sun.COM  * The first time we're called, we do some additional setup, and do not
137811173SJonathan.Adams@Sun.COM  * return until there are enough threads to start servicing requests.
137911173SJonathan.Adams@Sun.COM  */
13809515SJonathan.Adams@Sun.COM static void
taskq_thread_create(taskq_t * tq)13819515SJonathan.Adams@Sun.COM taskq_thread_create(taskq_t *tq)
13829515SJonathan.Adams@Sun.COM {
138311173SJonathan.Adams@Sun.COM 	kthread_t	*t;
138411173SJonathan.Adams@Sun.COM 	const boolean_t	first = (tq->tq_nthreads == 0);
13859515SJonathan.Adams@Sun.COM 
13869515SJonathan.Adams@Sun.COM 	ASSERT(MUTEX_HELD(&tq->tq_lock));
138711173SJonathan.Adams@Sun.COM 	ASSERT(tq->tq_flags & TASKQ_CHANGING);
138811173SJonathan.Adams@Sun.COM 	ASSERT(tq->tq_nthreads < tq->tq_nthreads_target);
13899515SJonathan.Adams@Sun.COM 	ASSERT(!(tq->tq_flags & TASKQ_THREAD_CREATED));
13909515SJonathan.Adams@Sun.COM 
139111173SJonathan.Adams@Sun.COM 
13929515SJonathan.Adams@Sun.COM 	tq->tq_flags |= TASKQ_THREAD_CREATED;
13939515SJonathan.Adams@Sun.COM 	tq->tq_active++;
139411173SJonathan.Adams@Sun.COM 	mutex_exit(&tq->tq_lock);
139511173SJonathan.Adams@Sun.COM 
139611173SJonathan.Adams@Sun.COM 	if (tq->tq_proc != &p0) {
139711173SJonathan.Adams@Sun.COM 		t = lwp_kernel_create(tq->tq_proc, taskq_thread, tq, TS_RUN,
139811173SJonathan.Adams@Sun.COM 		    tq->tq_pri);
139911173SJonathan.Adams@Sun.COM 	} else {
140011173SJonathan.Adams@Sun.COM 		t = thread_create(NULL, 0, taskq_thread, tq, 0, &p0, TS_RUN,
140111173SJonathan.Adams@Sun.COM 		    tq->tq_pri);
140211173SJonathan.Adams@Sun.COM 	}
140311173SJonathan.Adams@Sun.COM 
140411173SJonathan.Adams@Sun.COM 	if (!first) {
140511173SJonathan.Adams@Sun.COM 		mutex_enter(&tq->tq_lock);
140611173SJonathan.Adams@Sun.COM 		return;
140711173SJonathan.Adams@Sun.COM 	}
140811173SJonathan.Adams@Sun.COM 
140911173SJonathan.Adams@Sun.COM 	/*
141011173SJonathan.Adams@Sun.COM 	 * We know the thread cannot go away, since tq cannot be
141111173SJonathan.Adams@Sun.COM 	 * destroyed until creation has completed.  We can therefore
141211173SJonathan.Adams@Sun.COM 	 * safely dereference t.
141311173SJonathan.Adams@Sun.COM 	 */
141411173SJonathan.Adams@Sun.COM 	if (tq->tq_flags & TASKQ_THREADS_CPU_PCT) {
141511173SJonathan.Adams@Sun.COM 		taskq_cpupct_install(tq, t->t_cpupart);
141611173SJonathan.Adams@Sun.COM 	}
141711173SJonathan.Adams@Sun.COM 	mutex_enter(&tq->tq_lock);
141811173SJonathan.Adams@Sun.COM 
141911173SJonathan.Adams@Sun.COM 	/* Wait until we can service requests. */
142011173SJonathan.Adams@Sun.COM 	while (tq->tq_nthreads != tq->tq_nthreads_target &&
142111173SJonathan.Adams@Sun.COM 	    tq->tq_nthreads < TASKQ_CREATE_ACTIVE_THREADS) {
142211173SJonathan.Adams@Sun.COM 		cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
142311173SJonathan.Adams@Sun.COM 	}
14249515SJonathan.Adams@Sun.COM }
14259515SJonathan.Adams@Sun.COM 
142610889SJonathan.Adams@Sun.COM /*
142710889SJonathan.Adams@Sun.COM  * Common "sleep taskq thread" function, which handles CPR stuff, as well
142810889SJonathan.Adams@Sun.COM  * as giving a nice common point for debuggers to find inactive threads.
142910889SJonathan.Adams@Sun.COM  */
143010889SJonathan.Adams@Sun.COM static clock_t
taskq_thread_wait(taskq_t * tq,kmutex_t * mx,kcondvar_t * cv,callb_cpr_t * cprinfo,clock_t timeout)143110889SJonathan.Adams@Sun.COM taskq_thread_wait(taskq_t *tq, kmutex_t *mx, kcondvar_t *cv,
143210889SJonathan.Adams@Sun.COM     callb_cpr_t *cprinfo, clock_t timeout)
14339515SJonathan.Adams@Sun.COM {
143410889SJonathan.Adams@Sun.COM 	clock_t ret = 0;
143510889SJonathan.Adams@Sun.COM 
143610889SJonathan.Adams@Sun.COM 	if (!(tq->tq_flags & TASKQ_CPR_SAFE)) {
14379515SJonathan.Adams@Sun.COM 		CALLB_CPR_SAFE_BEGIN(cprinfo);
14389515SJonathan.Adams@Sun.COM 	}
143910889SJonathan.Adams@Sun.COM 	if (timeout < 0)
144010889SJonathan.Adams@Sun.COM 		cv_wait(cv, mx);
144110889SJonathan.Adams@Sun.COM 	else
144211066Srafael.vanoni@sun.com 		ret = cv_reltimedwait(cv, mx, timeout, TR_CLOCK_TICK);
144310889SJonathan.Adams@Sun.COM 
144410889SJonathan.Adams@Sun.COM 	if (!(tq->tq_flags & TASKQ_CPR_SAFE)) {
144510889SJonathan.Adams@Sun.COM 		CALLB_CPR_SAFE_END(cprinfo, mx);
144610889SJonathan.Adams@Sun.COM 	}
144710889SJonathan.Adams@Sun.COM 
144810889SJonathan.Adams@Sun.COM 	return (ret);
14499515SJonathan.Adams@Sun.COM }
14509515SJonathan.Adams@Sun.COM 
14510Sstevel@tonic-gate /*
14520Sstevel@tonic-gate  * Worker thread for processing task queue.
14530Sstevel@tonic-gate  */
14540Sstevel@tonic-gate static void
taskq_thread(void * arg)14550Sstevel@tonic-gate taskq_thread(void *arg)
14560Sstevel@tonic-gate {
14579515SJonathan.Adams@Sun.COM 	int thread_id;
14589515SJonathan.Adams@Sun.COM 
14590Sstevel@tonic-gate 	taskq_t *tq = arg;
14600Sstevel@tonic-gate 	taskq_ent_t *tqe;
14610Sstevel@tonic-gate 	callb_cpr_t cprinfo;
14620Sstevel@tonic-gate 	hrtime_t start, end;
14630Sstevel@tonic-gate 
146411173SJonathan.Adams@Sun.COM 	curthread->t_taskq = tq;	/* mark ourselves for taskq_member() */
146511173SJonathan.Adams@Sun.COM 
146611173SJonathan.Adams@Sun.COM 	if (curproc != &p0 && (tq->tq_flags & TASKQ_DUTY_CYCLE)) {
146711173SJonathan.Adams@Sun.COM 		sysdc_thread_enter(curthread, tq->tq_DC,
146811173SJonathan.Adams@Sun.COM 		    (tq->tq_flags & TASKQ_DC_BATCH) ? SYSDC_THREAD_BATCH : 0);
146911173SJonathan.Adams@Sun.COM 	}
147011173SJonathan.Adams@Sun.COM 
14710Sstevel@tonic-gate 	if (tq->tq_flags & TASKQ_CPR_SAFE) {
14720Sstevel@tonic-gate 		CALLB_CPR_INIT_SAFE(curthread, tq->tq_name);
14730Sstevel@tonic-gate 	} else {
14740Sstevel@tonic-gate 		CALLB_CPR_INIT(&cprinfo, &tq->tq_lock, callb_generic_cpr,
14750Sstevel@tonic-gate 		    tq->tq_name);
14760Sstevel@tonic-gate 	}
14770Sstevel@tonic-gate 	mutex_enter(&tq->tq_lock);
14789515SJonathan.Adams@Sun.COM 	thread_id = ++tq->tq_nthreads;
14799515SJonathan.Adams@Sun.COM 	ASSERT(tq->tq_flags & TASKQ_THREAD_CREATED);
148011173SJonathan.Adams@Sun.COM 	ASSERT(tq->tq_flags & TASKQ_CHANGING);
14819515SJonathan.Adams@Sun.COM 	tq->tq_flags &= ~TASKQ_THREAD_CREATED;
14829515SJonathan.Adams@Sun.COM 
14839515SJonathan.Adams@Sun.COM 	VERIFY3S(thread_id, <=, tq->tq_nthreads_max);
14849515SJonathan.Adams@Sun.COM 
14859515SJonathan.Adams@Sun.COM 	if (tq->tq_nthreads_max == 1)
14869515SJonathan.Adams@Sun.COM 		tq->tq_thread = curthread;
14879515SJonathan.Adams@Sun.COM 	else
14889515SJonathan.Adams@Sun.COM 		tq->tq_threadlist[thread_id - 1] = curthread;
14899515SJonathan.Adams@Sun.COM 
149011173SJonathan.Adams@Sun.COM 	/* Allow taskq_create_common()'s taskq_thread_create() to return. */
149111173SJonathan.Adams@Sun.COM 	if (tq->tq_nthreads == TASKQ_CREATE_ACTIVE_THREADS)
149211173SJonathan.Adams@Sun.COM 		cv_broadcast(&tq->tq_wait_cv);
149311173SJonathan.Adams@Sun.COM 
14949515SJonathan.Adams@Sun.COM 	for (;;) {
14959515SJonathan.Adams@Sun.COM 		if (tq->tq_flags & TASKQ_CHANGING) {
149611173SJonathan.Adams@Sun.COM 			/* See if we're no longer needed */
14979515SJonathan.Adams@Sun.COM 			if (thread_id > tq->tq_nthreads_target) {
14989515SJonathan.Adams@Sun.COM 				/*
14999515SJonathan.Adams@Sun.COM 				 * To preserve the one-to-one mapping between
15009515SJonathan.Adams@Sun.COM 				 * thread_id and thread, we must exit from
15019515SJonathan.Adams@Sun.COM 				 * highest thread ID to least.
15029515SJonathan.Adams@Sun.COM 				 *
15039515SJonathan.Adams@Sun.COM 				 * However, if everyone is exiting, the order
15049515SJonathan.Adams@Sun.COM 				 * doesn't matter, so just exit immediately.
15059515SJonathan.Adams@Sun.COM 				 * (this is safe, since you must wait for
15069515SJonathan.Adams@Sun.COM 				 * nthreads to reach 0 after setting
15079515SJonathan.Adams@Sun.COM 				 * tq_nthreads_target to 0)
15089515SJonathan.Adams@Sun.COM 				 */
15099515SJonathan.Adams@Sun.COM 				if (thread_id == tq->tq_nthreads ||
15109515SJonathan.Adams@Sun.COM 				    tq->tq_nthreads_target == 0)
15119515SJonathan.Adams@Sun.COM 					break;
15129515SJonathan.Adams@Sun.COM 
15139515SJonathan.Adams@Sun.COM 				/* Wait for higher thread_ids to exit */
151410889SJonathan.Adams@Sun.COM 				(void) taskq_thread_wait(tq, &tq->tq_lock,
151510889SJonathan.Adams@Sun.COM 				    &tq->tq_exit_cv, &cprinfo, -1);
15169515SJonathan.Adams@Sun.COM 				continue;
15179515SJonathan.Adams@Sun.COM 			}
151811173SJonathan.Adams@Sun.COM 
151911173SJonathan.Adams@Sun.COM 			/*
152011173SJonathan.Adams@Sun.COM 			 * If no thread is starting taskq_thread(), we can
152111173SJonathan.Adams@Sun.COM 			 * do some bookkeeping.
152211173SJonathan.Adams@Sun.COM 			 */
152311173SJonathan.Adams@Sun.COM 			if (!(tq->tq_flags & TASKQ_THREAD_CREATED)) {
152411173SJonathan.Adams@Sun.COM 				/* Check if we've reached our target */
152511173SJonathan.Adams@Sun.COM 				if (tq->tq_nthreads == tq->tq_nthreads_target) {
152611173SJonathan.Adams@Sun.COM 					tq->tq_flags &= ~TASKQ_CHANGING;
152711173SJonathan.Adams@Sun.COM 					cv_broadcast(&tq->tq_wait_cv);
152811173SJonathan.Adams@Sun.COM 				}
152911173SJonathan.Adams@Sun.COM 				/* Check if we need to create a thread */
153011173SJonathan.Adams@Sun.COM 				if (tq->tq_nthreads < tq->tq_nthreads_target) {
153111173SJonathan.Adams@Sun.COM 					taskq_thread_create(tq);
153211173SJonathan.Adams@Sun.COM 					continue; /* tq_lock was dropped */
153311173SJonathan.Adams@Sun.COM 				}
153411173SJonathan.Adams@Sun.COM 			}
15359515SJonathan.Adams@Sun.COM 		}
15360Sstevel@tonic-gate 		if ((tqe = tq->tq_task.tqent_next) == &tq->tq_task) {
15370Sstevel@tonic-gate 			if (--tq->tq_active == 0)
15380Sstevel@tonic-gate 				cv_broadcast(&tq->tq_wait_cv);
153910889SJonathan.Adams@Sun.COM 			(void) taskq_thread_wait(tq, &tq->tq_lock,
154010889SJonathan.Adams@Sun.COM 			    &tq->tq_dispatch_cv, &cprinfo, -1);
15410Sstevel@tonic-gate 			tq->tq_active++;
15420Sstevel@tonic-gate 			continue;
15430Sstevel@tonic-gate 		}
154411173SJonathan.Adams@Sun.COM 
15450Sstevel@tonic-gate 		tqe->tqent_prev->tqent_next = tqe->tqent_next;
15460Sstevel@tonic-gate 		tqe->tqent_next->tqent_prev = tqe->tqent_prev;
15470Sstevel@tonic-gate 		mutex_exit(&tq->tq_lock);
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 		rw_enter(&tq->tq_threadlock, RW_READER);
15500Sstevel@tonic-gate 		start = gethrtime();
15510Sstevel@tonic-gate 		DTRACE_PROBE2(taskq__exec__start, taskq_t *, tq,
15520Sstevel@tonic-gate 		    taskq_ent_t *, tqe);
15530Sstevel@tonic-gate 		tqe->tqent_func(tqe->tqent_arg);
15540Sstevel@tonic-gate 		DTRACE_PROBE2(taskq__exec__end, taskq_t *, tq,
15550Sstevel@tonic-gate 		    taskq_ent_t *, tqe);
15560Sstevel@tonic-gate 		end = gethrtime();
15570Sstevel@tonic-gate 		rw_exit(&tq->tq_threadlock);
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 		mutex_enter(&tq->tq_lock);
15600Sstevel@tonic-gate 		tq->tq_totaltime += end - start;
15610Sstevel@tonic-gate 		tq->tq_executed++;
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 		taskq_ent_free(tq, tqe);
15640Sstevel@tonic-gate 	}
15659515SJonathan.Adams@Sun.COM 
15669515SJonathan.Adams@Sun.COM 	if (tq->tq_nthreads_max == 1)
15679515SJonathan.Adams@Sun.COM 		tq->tq_thread = NULL;
15689515SJonathan.Adams@Sun.COM 	else
15699515SJonathan.Adams@Sun.COM 		tq->tq_threadlist[thread_id - 1] = NULL;
15709515SJonathan.Adams@Sun.COM 
15719515SJonathan.Adams@Sun.COM 	/* We're exiting, and therefore no longer active */
157211173SJonathan.Adams@Sun.COM 	ASSERT(tq->tq_active > 0);
15739515SJonathan.Adams@Sun.COM 	tq->tq_active--;
15749515SJonathan.Adams@Sun.COM 
157511173SJonathan.Adams@Sun.COM 	ASSERT(tq->tq_nthreads > 0);
157611173SJonathan.Adams@Sun.COM 	tq->tq_nthreads--;
157711173SJonathan.Adams@Sun.COM 
157811173SJonathan.Adams@Sun.COM 	/* Wake up anyone waiting for us to exit */
157911173SJonathan.Adams@Sun.COM 	cv_broadcast(&tq->tq_exit_cv);
158011173SJonathan.Adams@Sun.COM 	if (tq->tq_nthreads == tq->tq_nthreads_target) {
158111173SJonathan.Adams@Sun.COM 		if (!(tq->tq_flags & TASKQ_THREAD_CREATED))
158211173SJonathan.Adams@Sun.COM 			tq->tq_flags &= ~TASKQ_CHANGING;
158311173SJonathan.Adams@Sun.COM 
158411173SJonathan.Adams@Sun.COM 		cv_broadcast(&tq->tq_wait_cv);
158511173SJonathan.Adams@Sun.COM 	}
158611173SJonathan.Adams@Sun.COM 
15870Sstevel@tonic-gate 	ASSERT(!(tq->tq_flags & TASKQ_CPR_SAFE));
158811173SJonathan.Adams@Sun.COM 	CALLB_CPR_EXIT(&cprinfo);		/* drops tq->tq_lock */
158911173SJonathan.Adams@Sun.COM 	if (curthread->t_lwp != NULL) {
159011173SJonathan.Adams@Sun.COM 		mutex_enter(&curproc->p_lock);
159111173SJonathan.Adams@Sun.COM 		lwp_exit();
159211173SJonathan.Adams@Sun.COM 	} else {
159311173SJonathan.Adams@Sun.COM 		thread_exit();
159411173SJonathan.Adams@Sun.COM 	}
15950Sstevel@tonic-gate }
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate /*
15980Sstevel@tonic-gate  * Worker per-entry thread for dynamic dispatches.
15990Sstevel@tonic-gate  */
16000Sstevel@tonic-gate static void
taskq_d_thread(taskq_ent_t * tqe)16010Sstevel@tonic-gate taskq_d_thread(taskq_ent_t *tqe)
16020Sstevel@tonic-gate {
16030Sstevel@tonic-gate 	taskq_bucket_t	*bucket = tqe->tqent_bucket;
16040Sstevel@tonic-gate 	taskq_t		*tq = bucket->tqbucket_taskq;
16050Sstevel@tonic-gate 	kmutex_t	*lock = &bucket->tqbucket_lock;
16060Sstevel@tonic-gate 	kcondvar_t	*cv = &tqe->tqent_cv;
16070Sstevel@tonic-gate 	callb_cpr_t	cprinfo;
16080Sstevel@tonic-gate 	clock_t		w;
16090Sstevel@tonic-gate 
16100Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, lock, callb_generic_cpr, tq->tq_name);
16110Sstevel@tonic-gate 
16120Sstevel@tonic-gate 	mutex_enter(lock);
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 	for (;;) {
16150Sstevel@tonic-gate 		/*
16160Sstevel@tonic-gate 		 * If a task is scheduled (func != NULL), execute it, otherwise
16170Sstevel@tonic-gate 		 * sleep, waiting for a job.
16180Sstevel@tonic-gate 		 */
16190Sstevel@tonic-gate 		if (tqe->tqent_func != NULL) {
16200Sstevel@tonic-gate 			hrtime_t	start;
16210Sstevel@tonic-gate 			hrtime_t	end;
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 			ASSERT(bucket->tqbucket_nalloc > 0);
16240Sstevel@tonic-gate 
16250Sstevel@tonic-gate 			/*
16260Sstevel@tonic-gate 			 * It is possible to free the entry right away before
16270Sstevel@tonic-gate 			 * actually executing the task so that subsequent
16280Sstevel@tonic-gate 			 * dispatches may immediately reuse it. But this,
16290Sstevel@tonic-gate 			 * effectively, creates a two-length queue in the entry
16300Sstevel@tonic-gate 			 * and may lead to a deadlock if the execution of the
16310Sstevel@tonic-gate 			 * current task depends on the execution of the next
16320Sstevel@tonic-gate 			 * scheduled task. So, we keep the entry busy until the
16330Sstevel@tonic-gate 			 * task is processed.
16340Sstevel@tonic-gate 			 */
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate 			mutex_exit(lock);
16370Sstevel@tonic-gate 			start = gethrtime();
16380Sstevel@tonic-gate 			DTRACE_PROBE3(taskq__d__exec__start, taskq_t *, tq,
16390Sstevel@tonic-gate 			    taskq_bucket_t *, bucket, taskq_ent_t *, tqe);
16400Sstevel@tonic-gate 			tqe->tqent_func(tqe->tqent_arg);
16410Sstevel@tonic-gate 			DTRACE_PROBE3(taskq__d__exec__end, taskq_t *, tq,
16420Sstevel@tonic-gate 			    taskq_bucket_t *, bucket, taskq_ent_t *, tqe);
16430Sstevel@tonic-gate 			end = gethrtime();
16440Sstevel@tonic-gate 			mutex_enter(lock);
16450Sstevel@tonic-gate 			bucket->tqbucket_totaltime += end - start;
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate 			/*
16480Sstevel@tonic-gate 			 * Return the entry to the bucket free list.
16490Sstevel@tonic-gate 			 */
16500Sstevel@tonic-gate 			tqe->tqent_func = NULL;
16510Sstevel@tonic-gate 			TQ_APPEND(bucket->tqbucket_freelist, tqe);
16520Sstevel@tonic-gate 			bucket->tqbucket_nalloc--;
16530Sstevel@tonic-gate 			bucket->tqbucket_nfree++;
16540Sstevel@tonic-gate 			ASSERT(!IS_EMPTY(bucket->tqbucket_freelist));
16550Sstevel@tonic-gate 			/*
16560Sstevel@tonic-gate 			 * taskq_wait() waits for nalloc to drop to zero on
16570Sstevel@tonic-gate 			 * tqbucket_cv.
16580Sstevel@tonic-gate 			 */
16590Sstevel@tonic-gate 			cv_signal(&bucket->tqbucket_cv);
16600Sstevel@tonic-gate 		}
16610Sstevel@tonic-gate 
16620Sstevel@tonic-gate 		/*
16630Sstevel@tonic-gate 		 * At this point the entry must be in the bucket free list -
16640Sstevel@tonic-gate 		 * either because it was there initially or because it just
16650Sstevel@tonic-gate 		 * finished executing a task and put itself on the free list.
16660Sstevel@tonic-gate 		 */
16670Sstevel@tonic-gate 		ASSERT(bucket->tqbucket_nfree > 0);
16680Sstevel@tonic-gate 		/*
16690Sstevel@tonic-gate 		 * Go to sleep unless we are closing.
16700Sstevel@tonic-gate 		 * If a thread is sleeping too long, it dies.
16710Sstevel@tonic-gate 		 */
16720Sstevel@tonic-gate 		if (! (bucket->tqbucket_flags & TQBUCKET_CLOSE)) {
167310889SJonathan.Adams@Sun.COM 			w = taskq_thread_wait(tq, lock, cv,
167410889SJonathan.Adams@Sun.COM 			    &cprinfo, taskq_thread_timeout * hz);
16750Sstevel@tonic-gate 		}
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate 		/*
16780Sstevel@tonic-gate 		 * At this point we may be in two different states:
16790Sstevel@tonic-gate 		 *
16800Sstevel@tonic-gate 		 * (1) tqent_func is set which means that a new task is
16810Sstevel@tonic-gate 		 *	dispatched and we need to execute it.
16820Sstevel@tonic-gate 		 *
16830Sstevel@tonic-gate 		 * (2) Thread is sleeping for too long or we are closing. In
16840Sstevel@tonic-gate 		 *	both cases destroy the thread and the entry.
16850Sstevel@tonic-gate 		 */
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 		/* If func is NULL we should be on the freelist. */
16880Sstevel@tonic-gate 		ASSERT((tqe->tqent_func != NULL) ||
16890Sstevel@tonic-gate 		    (bucket->tqbucket_nfree > 0));
16900Sstevel@tonic-gate 		/* If func is non-NULL we should be allocated */
16910Sstevel@tonic-gate 		ASSERT((tqe->tqent_func == NULL) ||
16920Sstevel@tonic-gate 		    (bucket->tqbucket_nalloc > 0));
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate 		/* Check freelist consistency */
16950Sstevel@tonic-gate 		ASSERT((bucket->tqbucket_nfree > 0) ||
16960Sstevel@tonic-gate 		    IS_EMPTY(bucket->tqbucket_freelist));
16970Sstevel@tonic-gate 		ASSERT((bucket->tqbucket_nfree == 0) ||
16980Sstevel@tonic-gate 		    !IS_EMPTY(bucket->tqbucket_freelist));
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 		if ((tqe->tqent_func == NULL) &&
17010Sstevel@tonic-gate 		    ((w == -1) || (bucket->tqbucket_flags & TQBUCKET_CLOSE))) {
17020Sstevel@tonic-gate 			/*
17030Sstevel@tonic-gate 			 * This thread is sleeping for too long or we are
17040Sstevel@tonic-gate 			 * closing - time to die.
17050Sstevel@tonic-gate 			 * Thread creation/destruction happens rarely,
17060Sstevel@tonic-gate 			 * so grabbing the lock is not a big performance issue.
17070Sstevel@tonic-gate 			 * The bucket lock is dropped by CALLB_CPR_EXIT().
17080Sstevel@tonic-gate 			 */
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate 			/* Remove the entry from the free list. */
17110Sstevel@tonic-gate 			tqe->tqent_prev->tqent_next = tqe->tqent_next;
17120Sstevel@tonic-gate 			tqe->tqent_next->tqent_prev = tqe->tqent_prev;
17130Sstevel@tonic-gate 			ASSERT(bucket->tqbucket_nfree > 0);
17140Sstevel@tonic-gate 			bucket->tqbucket_nfree--;
17150Sstevel@tonic-gate 
17160Sstevel@tonic-gate 			TQ_STAT(bucket, tqs_tdeaths);
17170Sstevel@tonic-gate 			cv_signal(&bucket->tqbucket_cv);
17180Sstevel@tonic-gate 			tqe->tqent_thread = NULL;
17190Sstevel@tonic-gate 			mutex_enter(&tq->tq_lock);
17200Sstevel@tonic-gate 			tq->tq_tdeaths++;
17210Sstevel@tonic-gate 			mutex_exit(&tq->tq_lock);
17220Sstevel@tonic-gate 			CALLB_CPR_EXIT(&cprinfo);
17230Sstevel@tonic-gate 			kmem_cache_free(taskq_ent_cache, tqe);
17240Sstevel@tonic-gate 			thread_exit();
17250Sstevel@tonic-gate 		}
17260Sstevel@tonic-gate 	}
17270Sstevel@tonic-gate }
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate /*
17310Sstevel@tonic-gate  * Taskq creation. May sleep for memory.
17320Sstevel@tonic-gate  * Always use automatically generated instances to avoid kstat name space
17330Sstevel@tonic-gate  * collisions.
17340Sstevel@tonic-gate  */
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate taskq_t *
taskq_create(const char * name,int nthreads,pri_t pri,int minalloc,int maxalloc,uint_t flags)17370Sstevel@tonic-gate taskq_create(const char *name, int nthreads, pri_t pri, int minalloc,
17380Sstevel@tonic-gate     int maxalloc, uint_t flags)
17390Sstevel@tonic-gate {
174011173SJonathan.Adams@Sun.COM 	ASSERT((flags & ~TASKQ_INTERFACE_FLAGS) == 0);
174111173SJonathan.Adams@Sun.COM 
174211173SJonathan.Adams@Sun.COM 	return (taskq_create_common(name, 0, nthreads, pri, minalloc,
174311173SJonathan.Adams@Sun.COM 	    maxalloc, &p0, 0, flags | TASKQ_NOINSTANCE));
17440Sstevel@tonic-gate }
17450Sstevel@tonic-gate 
17460Sstevel@tonic-gate /*
17470Sstevel@tonic-gate  * Create an instance of task queue. It is legal to create task queues with the
17480Sstevel@tonic-gate  * same name and different instances.
17490Sstevel@tonic-gate  *
17500Sstevel@tonic-gate  * taskq_create_instance is used by ddi_taskq_create() where it gets the
17510Sstevel@tonic-gate  * instance from ddi_get_instance(). In some cases the instance is not
17520Sstevel@tonic-gate  * initialized and is set to -1. This case is handled as if no instance was
17530Sstevel@tonic-gate  * passed at all.
17540Sstevel@tonic-gate  */
17550Sstevel@tonic-gate taskq_t *
taskq_create_instance(const char * name,int instance,int nthreads,pri_t pri,int minalloc,int maxalloc,uint_t flags)17560Sstevel@tonic-gate taskq_create_instance(const char *name, int instance, int nthreads, pri_t pri,
17570Sstevel@tonic-gate     int minalloc, int maxalloc, uint_t flags)
17580Sstevel@tonic-gate {
175911173SJonathan.Adams@Sun.COM 	ASSERT((flags & ~TASKQ_INTERFACE_FLAGS) == 0);
17600Sstevel@tonic-gate 	ASSERT((instance >= 0) || (instance == -1));
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 	if (instance < 0) {
17630Sstevel@tonic-gate 		flags |= TASKQ_NOINSTANCE;
17640Sstevel@tonic-gate 	}
17650Sstevel@tonic-gate 
17660Sstevel@tonic-gate 	return (taskq_create_common(name, instance, nthreads,
176711173SJonathan.Adams@Sun.COM 	    pri, minalloc, maxalloc, &p0, 0, flags));
176811173SJonathan.Adams@Sun.COM }
176911173SJonathan.Adams@Sun.COM 
177011173SJonathan.Adams@Sun.COM taskq_t *
taskq_create_proc(const char * name,int nthreads,pri_t pri,int minalloc,int maxalloc,proc_t * proc,uint_t flags)177111173SJonathan.Adams@Sun.COM taskq_create_proc(const char *name, int nthreads, pri_t pri, int minalloc,
177211173SJonathan.Adams@Sun.COM     int maxalloc, proc_t *proc, uint_t flags)
177311173SJonathan.Adams@Sun.COM {
177411173SJonathan.Adams@Sun.COM 	ASSERT((flags & ~TASKQ_INTERFACE_FLAGS) == 0);
177511173SJonathan.Adams@Sun.COM 	ASSERT(proc->p_flag & SSYS);
177611173SJonathan.Adams@Sun.COM 
177711173SJonathan.Adams@Sun.COM 	return (taskq_create_common(name, 0, nthreads, pri, minalloc,
177811173SJonathan.Adams@Sun.COM 	    maxalloc, proc, 0, flags | TASKQ_NOINSTANCE));
17790Sstevel@tonic-gate }
17800Sstevel@tonic-gate 
178111173SJonathan.Adams@Sun.COM taskq_t *
taskq_create_sysdc(const char * name,int nthreads,int minalloc,int maxalloc,proc_t * proc,uint_t dc,uint_t flags)178211173SJonathan.Adams@Sun.COM taskq_create_sysdc(const char *name, int nthreads, int minalloc,
178311173SJonathan.Adams@Sun.COM     int maxalloc, proc_t *proc, uint_t dc, uint_t flags)
178411173SJonathan.Adams@Sun.COM {
178511173SJonathan.Adams@Sun.COM 	ASSERT((flags & ~TASKQ_INTERFACE_FLAGS) == 0);
178611173SJonathan.Adams@Sun.COM 	ASSERT(proc->p_flag & SSYS);
178711173SJonathan.Adams@Sun.COM 
178811173SJonathan.Adams@Sun.COM 	return (taskq_create_common(name, 0, nthreads, minclsyspri, minalloc,
178911173SJonathan.Adams@Sun.COM 	    maxalloc, proc, dc, flags | TASKQ_NOINSTANCE | TASKQ_DUTY_CYCLE));
179011173SJonathan.Adams@Sun.COM }
179111173SJonathan.Adams@Sun.COM 
17920Sstevel@tonic-gate static taskq_t *
taskq_create_common(const char * name,int instance,int nthreads,pri_t pri,int minalloc,int maxalloc,proc_t * proc,uint_t dc,uint_t flags)17930Sstevel@tonic-gate taskq_create_common(const char *name, int instance, int nthreads, pri_t pri,
179411173SJonathan.Adams@Sun.COM     int minalloc, int maxalloc, proc_t *proc, uint_t dc, uint_t flags)
17950Sstevel@tonic-gate {
17960Sstevel@tonic-gate 	taskq_t *tq = kmem_cache_alloc(taskq_cache, KM_SLEEP);
17970Sstevel@tonic-gate 	uint_t ncpus = ((boot_max_ncpus == -1) ? max_ncpus : boot_max_ncpus);
17980Sstevel@tonic-gate 	uint_t bsize;	/* # of buckets - always power of 2 */
17999515SJonathan.Adams@Sun.COM 	int max_nthreads;
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate 	/*
180211173SJonathan.Adams@Sun.COM 	 * TASKQ_DYNAMIC, TASKQ_CPR_SAFE and TASKQ_THREADS_CPU_PCT are all
180311173SJonathan.Adams@Sun.COM 	 * mutually incompatible.
18040Sstevel@tonic-gate 	 */
180511173SJonathan.Adams@Sun.COM 	IMPLY((flags & TASKQ_DYNAMIC), !(flags & TASKQ_CPR_SAFE));
180611173SJonathan.Adams@Sun.COM 	IMPLY((flags & TASKQ_DYNAMIC), !(flags & TASKQ_THREADS_CPU_PCT));
180711173SJonathan.Adams@Sun.COM 	IMPLY((flags & TASKQ_CPR_SAFE), !(flags & TASKQ_THREADS_CPU_PCT));
18080Sstevel@tonic-gate 
180911173SJonathan.Adams@Sun.COM 	/* Cannot have DUTY_CYCLE without a non-p0 kernel process */
181011173SJonathan.Adams@Sun.COM 	IMPLY((flags & TASKQ_DUTY_CYCLE), proc != &p0);
181111173SJonathan.Adams@Sun.COM 
181211173SJonathan.Adams@Sun.COM 	/* Cannot have DC_BATCH without DUTY_CYCLE */
181311173SJonathan.Adams@Sun.COM 	ASSERT((flags & (TASKQ_DUTY_CYCLE|TASKQ_DC_BATCH)) != TASKQ_DC_BATCH);
181411173SJonathan.Adams@Sun.COM 
181511173SJonathan.Adams@Sun.COM 	ASSERT(proc != NULL);
18160Sstevel@tonic-gate 
18170Sstevel@tonic-gate 	bsize = 1 << (highbit(ncpus) - 1);
18180Sstevel@tonic-gate 	ASSERT(bsize >= 1);
18190Sstevel@tonic-gate 	bsize = MIN(bsize, taskq_maxbuckets);
18200Sstevel@tonic-gate 
18219515SJonathan.Adams@Sun.COM 	if (flags & TASKQ_DYNAMIC) {
18229515SJonathan.Adams@Sun.COM 		ASSERT3S(nthreads, >=, 1);
18239515SJonathan.Adams@Sun.COM 		tq->tq_maxsize = nthreads;
18249515SJonathan.Adams@Sun.COM 
18259515SJonathan.Adams@Sun.COM 		/* For dynamic task queues use just one backup thread */
18269515SJonathan.Adams@Sun.COM 		nthreads = max_nthreads = 1;
18279515SJonathan.Adams@Sun.COM 
182811173SJonathan.Adams@Sun.COM 	} else if (flags & TASKQ_THREADS_CPU_PCT) {
18299515SJonathan.Adams@Sun.COM 		uint_t pct;
18309515SJonathan.Adams@Sun.COM 		ASSERT3S(nthreads, >=, 0);
18319515SJonathan.Adams@Sun.COM 		pct = nthreads;
18320Sstevel@tonic-gate 
18339515SJonathan.Adams@Sun.COM 		if (pct > taskq_cpupct_max_percent)
18349515SJonathan.Adams@Sun.COM 			pct = taskq_cpupct_max_percent;
18359515SJonathan.Adams@Sun.COM 
183611173SJonathan.Adams@Sun.COM 		/*
183711173SJonathan.Adams@Sun.COM 		 * If you're using THREADS_CPU_PCT, the process for the
183811173SJonathan.Adams@Sun.COM 		 * taskq threads must be curproc.  This allows any pset
183911173SJonathan.Adams@Sun.COM 		 * binding to be inherited correctly.  If proc is &p0,
184011173SJonathan.Adams@Sun.COM 		 * we won't be creating LWPs, so new threads will be assigned
184111173SJonathan.Adams@Sun.COM 		 * to the default processor set.
184211173SJonathan.Adams@Sun.COM 		 */
184311173SJonathan.Adams@Sun.COM 		ASSERT(curproc == proc || proc == &p0);
18449515SJonathan.Adams@Sun.COM 		tq->tq_threads_ncpus_pct = pct;
184511173SJonathan.Adams@Sun.COM 		nthreads = 1;		/* corrected in taskq_thread_create() */
18469515SJonathan.Adams@Sun.COM 		max_nthreads = TASKQ_THREADS_PCT(max_ncpus, pct);
184711173SJonathan.Adams@Sun.COM 
184811173SJonathan.Adams@Sun.COM 	} else {
184911173SJonathan.Adams@Sun.COM 		ASSERT3S(nthreads, >=, 1);
185011173SJonathan.Adams@Sun.COM 		max_nthreads = nthreads;
18519515SJonathan.Adams@Sun.COM 	}
18520Sstevel@tonic-gate 
18539515SJonathan.Adams@Sun.COM 	if (max_nthreads < taskq_minimum_nthreads_max)
18549515SJonathan.Adams@Sun.COM 		max_nthreads = taskq_minimum_nthreads_max;
18559515SJonathan.Adams@Sun.COM 
18569515SJonathan.Adams@Sun.COM 	/*
18579515SJonathan.Adams@Sun.COM 	 * Make sure the name is 0-terminated, and conforms to the rules for
18589515SJonathan.Adams@Sun.COM 	 * C indentifiers
18599515SJonathan.Adams@Sun.COM 	 */
18600Sstevel@tonic-gate 	(void) strncpy(tq->tq_name, name, TASKQ_NAMELEN + 1);
18619515SJonathan.Adams@Sun.COM 	strident_canon(tq->tq_name, TASKQ_NAMELEN + 1);
18620Sstevel@tonic-gate 
18639515SJonathan.Adams@Sun.COM 	tq->tq_flags = flags | TASKQ_CHANGING;
18649515SJonathan.Adams@Sun.COM 	tq->tq_active = 0;
18650Sstevel@tonic-gate 	tq->tq_instance = instance;
18669515SJonathan.Adams@Sun.COM 	tq->tq_nthreads_target = nthreads;
18679515SJonathan.Adams@Sun.COM 	tq->tq_nthreads_max = max_nthreads;
18680Sstevel@tonic-gate 	tq->tq_minalloc = minalloc;
18690Sstevel@tonic-gate 	tq->tq_maxalloc = maxalloc;
18700Sstevel@tonic-gate 	tq->tq_nbuckets = bsize;
187111173SJonathan.Adams@Sun.COM 	tq->tq_proc = proc;
18720Sstevel@tonic-gate 	tq->tq_pri = pri;
187311173SJonathan.Adams@Sun.COM 	tq->tq_DC = dc;
187411173SJonathan.Adams@Sun.COM 	list_link_init(&tq->tq_cpupct_link);
18750Sstevel@tonic-gate 
18769515SJonathan.Adams@Sun.COM 	if (max_nthreads > 1)
18779515SJonathan.Adams@Sun.COM 		tq->tq_threadlist = kmem_alloc(
18789515SJonathan.Adams@Sun.COM 		    sizeof (kthread_t *) * max_nthreads, KM_SLEEP);
18799515SJonathan.Adams@Sun.COM 
18809515SJonathan.Adams@Sun.COM 	mutex_enter(&tq->tq_lock);
18819515SJonathan.Adams@Sun.COM 	if (flags & TASKQ_PREPOPULATE) {
18829515SJonathan.Adams@Sun.COM 		while (minalloc-- > 0)
18839515SJonathan.Adams@Sun.COM 			taskq_ent_free(tq, taskq_ent_alloc(tq, TQ_SLEEP));
18849515SJonathan.Adams@Sun.COM 	}
18850Sstevel@tonic-gate 
188611173SJonathan.Adams@Sun.COM 	/*
188711173SJonathan.Adams@Sun.COM 	 * Create the first thread, which will create any other threads
188811173SJonathan.Adams@Sun.COM 	 * necessary.  taskq_thread_create will not return until we have
188911173SJonathan.Adams@Sun.COM 	 * enough threads to be able to process requests.
189011173SJonathan.Adams@Sun.COM 	 */
18919515SJonathan.Adams@Sun.COM 	taskq_thread_create(tq);
18929515SJonathan.Adams@Sun.COM 	mutex_exit(&tq->tq_lock);
18930Sstevel@tonic-gate 
18940Sstevel@tonic-gate 	if (flags & TASKQ_DYNAMIC) {
18950Sstevel@tonic-gate 		taskq_bucket_t *bucket = kmem_zalloc(sizeof (taskq_bucket_t) *
18960Sstevel@tonic-gate 		    bsize, KM_SLEEP);
18970Sstevel@tonic-gate 		int b_id;
18980Sstevel@tonic-gate 
18990Sstevel@tonic-gate 		tq->tq_buckets = bucket;
19000Sstevel@tonic-gate 
19010Sstevel@tonic-gate 		/* Initialize each bucket */
19020Sstevel@tonic-gate 		for (b_id = 0; b_id < bsize; b_id++, bucket++) {
19030Sstevel@tonic-gate 			mutex_init(&bucket->tqbucket_lock, NULL, MUTEX_DEFAULT,
19040Sstevel@tonic-gate 			    NULL);
19050Sstevel@tonic-gate 			cv_init(&bucket->tqbucket_cv, NULL, CV_DEFAULT, NULL);
19060Sstevel@tonic-gate 			bucket->tqbucket_taskq = tq;
19070Sstevel@tonic-gate 			bucket->tqbucket_freelist.tqent_next =
19080Sstevel@tonic-gate 			    bucket->tqbucket_freelist.tqent_prev =
19090Sstevel@tonic-gate 			    &bucket->tqbucket_freelist;
19100Sstevel@tonic-gate 			if (flags & TASKQ_PREPOPULATE)
19110Sstevel@tonic-gate 				taskq_bucket_extend(bucket);
19120Sstevel@tonic-gate 		}
19130Sstevel@tonic-gate 	}
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate 	/*
19160Sstevel@tonic-gate 	 * Install kstats.
19170Sstevel@tonic-gate 	 * We have two cases:
19180Sstevel@tonic-gate 	 *   1) Instance is provided to taskq_create_instance(). In this case it
191911173SJonathan.Adams@Sun.COM 	 *	should be >= 0 and we use it.
19200Sstevel@tonic-gate 	 *
19210Sstevel@tonic-gate 	 *   2) Instance is not provided and is automatically generated
19220Sstevel@tonic-gate 	 */
19230Sstevel@tonic-gate 	if (flags & TASKQ_NOINSTANCE) {
19240Sstevel@tonic-gate 		instance = tq->tq_instance =
19250Sstevel@tonic-gate 		    (int)(uintptr_t)vmem_alloc(taskq_id_arena, 1, VM_SLEEP);
19260Sstevel@tonic-gate 	}
19270Sstevel@tonic-gate 
19280Sstevel@tonic-gate 	if (flags & TASKQ_DYNAMIC) {
19290Sstevel@tonic-gate 		if ((tq->tq_kstat = kstat_create("unix", instance,
19309515SJonathan.Adams@Sun.COM 		    tq->tq_name, "taskq_d", KSTAT_TYPE_NAMED,
19319515SJonathan.Adams@Sun.COM 		    sizeof (taskq_d_kstat) / sizeof (kstat_named_t),
19329515SJonathan.Adams@Sun.COM 		    KSTAT_FLAG_VIRTUAL)) != NULL) {
19330Sstevel@tonic-gate 			tq->tq_kstat->ks_lock = &taskq_d_kstat_lock;
19340Sstevel@tonic-gate 			tq->tq_kstat->ks_data = &taskq_d_kstat;
19350Sstevel@tonic-gate 			tq->tq_kstat->ks_update = taskq_d_kstat_update;
19360Sstevel@tonic-gate 			tq->tq_kstat->ks_private = tq;
19370Sstevel@tonic-gate 			kstat_install(tq->tq_kstat);
19380Sstevel@tonic-gate 		}
19390Sstevel@tonic-gate 	} else {
19400Sstevel@tonic-gate 		if ((tq->tq_kstat = kstat_create("unix", instance, tq->tq_name,
19419515SJonathan.Adams@Sun.COM 		    "taskq", KSTAT_TYPE_NAMED,
19429515SJonathan.Adams@Sun.COM 		    sizeof (taskq_kstat) / sizeof (kstat_named_t),
19439515SJonathan.Adams@Sun.COM 		    KSTAT_FLAG_VIRTUAL)) != NULL) {
19440Sstevel@tonic-gate 			tq->tq_kstat->ks_lock = &taskq_kstat_lock;
19450Sstevel@tonic-gate 			tq->tq_kstat->ks_data = &taskq_kstat;
19460Sstevel@tonic-gate 			tq->tq_kstat->ks_update = taskq_kstat_update;
19470Sstevel@tonic-gate 			tq->tq_kstat->ks_private = tq;
19480Sstevel@tonic-gate 			kstat_install(tq->tq_kstat);
19490Sstevel@tonic-gate 		}
19500Sstevel@tonic-gate 	}
19510Sstevel@tonic-gate 
19520Sstevel@tonic-gate 	return (tq);
19530Sstevel@tonic-gate }
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate /*
19560Sstevel@tonic-gate  * taskq_destroy().
19570Sstevel@tonic-gate  *
19580Sstevel@tonic-gate  * Assumes: by the time taskq_destroy is called no one will use this task queue
19590Sstevel@tonic-gate  * in any way and no one will try to dispatch entries in it.
19600Sstevel@tonic-gate  */
19610Sstevel@tonic-gate void
taskq_destroy(taskq_t * tq)19620Sstevel@tonic-gate taskq_destroy(taskq_t *tq)
19630Sstevel@tonic-gate {
19640Sstevel@tonic-gate 	taskq_bucket_t *b = tq->tq_buckets;
19650Sstevel@tonic-gate 	int bid = 0;
19660Sstevel@tonic-gate 
19670Sstevel@tonic-gate 	ASSERT(! (tq->tq_flags & TASKQ_CPR_SAFE));
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate 	/*
19700Sstevel@tonic-gate 	 * Destroy kstats.
19710Sstevel@tonic-gate 	 */
19720Sstevel@tonic-gate 	if (tq->tq_kstat != NULL) {
19730Sstevel@tonic-gate 		kstat_delete(tq->tq_kstat);
19740Sstevel@tonic-gate 		tq->tq_kstat = NULL;
19750Sstevel@tonic-gate 	}
19760Sstevel@tonic-gate 
19770Sstevel@tonic-gate 	/*
19780Sstevel@tonic-gate 	 * Destroy instance if needed.
19790Sstevel@tonic-gate 	 */
19800Sstevel@tonic-gate 	if (tq->tq_flags & TASKQ_NOINSTANCE) {
19810Sstevel@tonic-gate 		vmem_free(taskq_id_arena, (void *)(uintptr_t)(tq->tq_instance),
19820Sstevel@tonic-gate 		    1);
19830Sstevel@tonic-gate 		tq->tq_instance = 0;
19840Sstevel@tonic-gate 	}
19850Sstevel@tonic-gate 
19860Sstevel@tonic-gate 	/*
19879515SJonathan.Adams@Sun.COM 	 * Unregister from the cpupct list.
19889515SJonathan.Adams@Sun.COM 	 */
19899515SJonathan.Adams@Sun.COM 	if (tq->tq_flags & TASKQ_THREADS_CPU_PCT) {
199011173SJonathan.Adams@Sun.COM 		taskq_cpupct_remove(tq);
19919515SJonathan.Adams@Sun.COM 	}
19929515SJonathan.Adams@Sun.COM 
19939515SJonathan.Adams@Sun.COM 	/*
19940Sstevel@tonic-gate 	 * Wait for any pending entries to complete.
19950Sstevel@tonic-gate 	 */
19960Sstevel@tonic-gate 	taskq_wait(tq);
19970Sstevel@tonic-gate 
19980Sstevel@tonic-gate 	mutex_enter(&tq->tq_lock);
19990Sstevel@tonic-gate 	ASSERT((tq->tq_task.tqent_next == &tq->tq_task) &&
20000Sstevel@tonic-gate 	    (tq->tq_active == 0));
20010Sstevel@tonic-gate 
20029515SJonathan.Adams@Sun.COM 	/* notify all the threads that they need to exit */
20039515SJonathan.Adams@Sun.COM 	tq->tq_nthreads_target = 0;
20040Sstevel@tonic-gate 
20059515SJonathan.Adams@Sun.COM 	tq->tq_flags |= TASKQ_CHANGING;
20060Sstevel@tonic-gate 	cv_broadcast(&tq->tq_dispatch_cv);
20079515SJonathan.Adams@Sun.COM 	cv_broadcast(&tq->tq_exit_cv);
20089515SJonathan.Adams@Sun.COM 
20090Sstevel@tonic-gate 	while (tq->tq_nthreads != 0)
20100Sstevel@tonic-gate 		cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
20110Sstevel@tonic-gate 
20129515SJonathan.Adams@Sun.COM 	if (tq->tq_nthreads_max != 1)
20139515SJonathan.Adams@Sun.COM 		kmem_free(tq->tq_threadlist, sizeof (kthread_t *) *
20149515SJonathan.Adams@Sun.COM 		    tq->tq_nthreads_max);
20159515SJonathan.Adams@Sun.COM 
20160Sstevel@tonic-gate 	tq->tq_minalloc = 0;
20170Sstevel@tonic-gate 	while (tq->tq_nalloc != 0)
20180Sstevel@tonic-gate 		taskq_ent_free(tq, taskq_ent_alloc(tq, TQ_SLEEP));
20190Sstevel@tonic-gate 
20200Sstevel@tonic-gate 	mutex_exit(&tq->tq_lock);
20210Sstevel@tonic-gate 
20220Sstevel@tonic-gate 	/*
20230Sstevel@tonic-gate 	 * Mark each bucket as closing and wakeup all sleeping threads.
20240Sstevel@tonic-gate 	 */
20250Sstevel@tonic-gate 	for (; (b != NULL) && (bid < tq->tq_nbuckets); b++, bid++) {
20260Sstevel@tonic-gate 		taskq_ent_t *tqe;
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate 		mutex_enter(&b->tqbucket_lock);
20290Sstevel@tonic-gate 
20300Sstevel@tonic-gate 		b->tqbucket_flags |= TQBUCKET_CLOSE;
20310Sstevel@tonic-gate 		/* Wakeup all sleeping threads */
20320Sstevel@tonic-gate 
20330Sstevel@tonic-gate 		for (tqe = b->tqbucket_freelist.tqent_next;
20340Sstevel@tonic-gate 		    tqe != &b->tqbucket_freelist; tqe = tqe->tqent_next)
20350Sstevel@tonic-gate 			cv_signal(&tqe->tqent_cv);
20360Sstevel@tonic-gate 
20370Sstevel@tonic-gate 		ASSERT(b->tqbucket_nalloc == 0);
20380Sstevel@tonic-gate 
20390Sstevel@tonic-gate 		/*
20400Sstevel@tonic-gate 		 * At this point we waited for all pending jobs to complete (in
20410Sstevel@tonic-gate 		 * both the task queue and the bucket and no new jobs should
20420Sstevel@tonic-gate 		 * arrive. Wait for all threads to die.
20430Sstevel@tonic-gate 		 */
20440Sstevel@tonic-gate 		while (b->tqbucket_nfree > 0)
20450Sstevel@tonic-gate 			cv_wait(&b->tqbucket_cv, &b->tqbucket_lock);
20460Sstevel@tonic-gate 		mutex_exit(&b->tqbucket_lock);
20470Sstevel@tonic-gate 		mutex_destroy(&b->tqbucket_lock);
20480Sstevel@tonic-gate 		cv_destroy(&b->tqbucket_cv);
20490Sstevel@tonic-gate 	}
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate 	if (tq->tq_buckets != NULL) {
20520Sstevel@tonic-gate 		ASSERT(tq->tq_flags & TASKQ_DYNAMIC);
20530Sstevel@tonic-gate 		kmem_free(tq->tq_buckets,
20540Sstevel@tonic-gate 		    sizeof (taskq_bucket_t) * tq->tq_nbuckets);
20550Sstevel@tonic-gate 
20560Sstevel@tonic-gate 		/* Cleanup fields before returning tq to the cache */
20570Sstevel@tonic-gate 		tq->tq_buckets = NULL;
20580Sstevel@tonic-gate 		tq->tq_tcreates = 0;
20590Sstevel@tonic-gate 		tq->tq_tdeaths = 0;
20600Sstevel@tonic-gate 	} else {
20610Sstevel@tonic-gate 		ASSERT(!(tq->tq_flags & TASKQ_DYNAMIC));
20620Sstevel@tonic-gate 	}
20630Sstevel@tonic-gate 
20649515SJonathan.Adams@Sun.COM 	tq->tq_threads_ncpus_pct = 0;
20650Sstevel@tonic-gate 	tq->tq_totaltime = 0;
20660Sstevel@tonic-gate 	tq->tq_tasks = 0;
20670Sstevel@tonic-gate 	tq->tq_maxtasks = 0;
20680Sstevel@tonic-gate 	tq->tq_executed = 0;
20690Sstevel@tonic-gate 	kmem_cache_free(taskq_cache, tq);
20700Sstevel@tonic-gate }
20710Sstevel@tonic-gate 
20720Sstevel@tonic-gate /*
20730Sstevel@tonic-gate  * Extend a bucket with a new entry on the free list and attach a worker thread
20740Sstevel@tonic-gate  * to it.
20750Sstevel@tonic-gate  *
20760Sstevel@tonic-gate  * Argument: pointer to the bucket.
20770Sstevel@tonic-gate  *
20780Sstevel@tonic-gate  * This function may quietly fail. It is only used by taskq_dispatch() which
20790Sstevel@tonic-gate  * handles such failures properly.
20800Sstevel@tonic-gate  */
20810Sstevel@tonic-gate static void
taskq_bucket_extend(void * arg)20820Sstevel@tonic-gate taskq_bucket_extend(void *arg)
20830Sstevel@tonic-gate {
20840Sstevel@tonic-gate 	taskq_ent_t *tqe;
20850Sstevel@tonic-gate 	taskq_bucket_t *b = (taskq_bucket_t *)arg;
20860Sstevel@tonic-gate 	taskq_t *tq = b->tqbucket_taskq;
20870Sstevel@tonic-gate 	int nthreads;
20880Sstevel@tonic-gate 
20890Sstevel@tonic-gate 	if (! ENOUGH_MEMORY()) {
20900Sstevel@tonic-gate 		TQ_STAT(b, tqs_nomem);
20910Sstevel@tonic-gate 		return;
20920Sstevel@tonic-gate 	}
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate 	mutex_enter(&tq->tq_lock);
20950Sstevel@tonic-gate 
20960Sstevel@tonic-gate 	/*
20970Sstevel@tonic-gate 	 * Observe global taskq limits on the number of threads.
20980Sstevel@tonic-gate 	 */
20990Sstevel@tonic-gate 	if (tq->tq_tcreates++ - tq->tq_tdeaths > tq->tq_maxsize) {
21000Sstevel@tonic-gate 		tq->tq_tcreates--;
21010Sstevel@tonic-gate 		mutex_exit(&tq->tq_lock);
21020Sstevel@tonic-gate 		return;
21030Sstevel@tonic-gate 	}
21040Sstevel@tonic-gate 	mutex_exit(&tq->tq_lock);
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate 	tqe = kmem_cache_alloc(taskq_ent_cache, KM_NOSLEEP);
21070Sstevel@tonic-gate 
21080Sstevel@tonic-gate 	if (tqe == NULL) {
21090Sstevel@tonic-gate 		mutex_enter(&tq->tq_lock);
21100Sstevel@tonic-gate 		TQ_STAT(b, tqs_nomem);
21110Sstevel@tonic-gate 		tq->tq_tcreates--;
21120Sstevel@tonic-gate 		mutex_exit(&tq->tq_lock);
21130Sstevel@tonic-gate 		return;
21140Sstevel@tonic-gate 	}
21150Sstevel@tonic-gate 
21160Sstevel@tonic-gate 	ASSERT(tqe->tqent_thread == NULL);
21170Sstevel@tonic-gate 
21180Sstevel@tonic-gate 	tqe->tqent_bucket = b;
21190Sstevel@tonic-gate 
21200Sstevel@tonic-gate 	/*
21210Sstevel@tonic-gate 	 * Create a thread in a TS_STOPPED state first. If it is successfully
21220Sstevel@tonic-gate 	 * created, place the entry on the free list and start the thread.
21230Sstevel@tonic-gate 	 */
21240Sstevel@tonic-gate 	tqe->tqent_thread = thread_create(NULL, 0, taskq_d_thread, tqe,
21250Sstevel@tonic-gate 	    0, &p0, TS_STOPPED, tq->tq_pri);
21260Sstevel@tonic-gate 
21270Sstevel@tonic-gate 	/*
21280Sstevel@tonic-gate 	 * Once the entry is ready, link it to the the bucket free list.
21290Sstevel@tonic-gate 	 */
21300Sstevel@tonic-gate 	mutex_enter(&b->tqbucket_lock);
21310Sstevel@tonic-gate 	tqe->tqent_func = NULL;
21320Sstevel@tonic-gate 	TQ_APPEND(b->tqbucket_freelist, tqe);
21330Sstevel@tonic-gate 	b->tqbucket_nfree++;
21340Sstevel@tonic-gate 	TQ_STAT(b, tqs_tcreates);
21350Sstevel@tonic-gate 
21360Sstevel@tonic-gate #if TASKQ_STATISTIC
21370Sstevel@tonic-gate 	nthreads = b->tqbucket_stat.tqs_tcreates -
21380Sstevel@tonic-gate 	    b->tqbucket_stat.tqs_tdeaths;
21390Sstevel@tonic-gate 	b->tqbucket_stat.tqs_maxthreads = MAX(nthreads,
21400Sstevel@tonic-gate 	    b->tqbucket_stat.tqs_maxthreads);
21410Sstevel@tonic-gate #endif
21420Sstevel@tonic-gate 
21430Sstevel@tonic-gate 	mutex_exit(&b->tqbucket_lock);
21440Sstevel@tonic-gate 	/*
21450Sstevel@tonic-gate 	 * Start the stopped thread.
21460Sstevel@tonic-gate 	 */
21470Sstevel@tonic-gate 	thread_lock(tqe->tqent_thread);
21480Sstevel@tonic-gate 	tqe->tqent_thread->t_taskq = tq;
21490Sstevel@tonic-gate 	tqe->tqent_thread->t_schedflag |= TS_ALLSTART;
21500Sstevel@tonic-gate 	setrun_locked(tqe->tqent_thread);
21510Sstevel@tonic-gate 	thread_unlock(tqe->tqent_thread);
21520Sstevel@tonic-gate }
21530Sstevel@tonic-gate 
21540Sstevel@tonic-gate static int
taskq_kstat_update(kstat_t * ksp,int rw)21550Sstevel@tonic-gate taskq_kstat_update(kstat_t *ksp, int rw)
21560Sstevel@tonic-gate {
21570Sstevel@tonic-gate 	struct taskq_kstat *tqsp = &taskq_kstat;
21580Sstevel@tonic-gate 	taskq_t *tq = ksp->ks_private;
21590Sstevel@tonic-gate 
21600Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
21610Sstevel@tonic-gate 		return (EACCES);
21620Sstevel@tonic-gate 
216311173SJonathan.Adams@Sun.COM 	tqsp->tq_pid.value.ui64 = tq->tq_proc->p_pid;
21640Sstevel@tonic-gate 	tqsp->tq_tasks.value.ui64 = tq->tq_tasks;
21650Sstevel@tonic-gate 	tqsp->tq_executed.value.ui64 = tq->tq_executed;
21660Sstevel@tonic-gate 	tqsp->tq_maxtasks.value.ui64 = tq->tq_maxtasks;
21670Sstevel@tonic-gate 	tqsp->tq_totaltime.value.ui64 = tq->tq_totaltime;
21680Sstevel@tonic-gate 	tqsp->tq_nactive.value.ui64 = tq->tq_active;
21690Sstevel@tonic-gate 	tqsp->tq_nalloc.value.ui64 = tq->tq_nalloc;
21700Sstevel@tonic-gate 	tqsp->tq_pri.value.ui64 = tq->tq_pri;
21710Sstevel@tonic-gate 	tqsp->tq_nthreads.value.ui64 = tq->tq_nthreads;
21720Sstevel@tonic-gate 	return (0);
21730Sstevel@tonic-gate }
21740Sstevel@tonic-gate 
21750Sstevel@tonic-gate static int
taskq_d_kstat_update(kstat_t * ksp,int rw)21760Sstevel@tonic-gate taskq_d_kstat_update(kstat_t *ksp, int rw)
21770Sstevel@tonic-gate {
21780Sstevel@tonic-gate 	struct taskq_d_kstat *tqsp = &taskq_d_kstat;
21790Sstevel@tonic-gate 	taskq_t *tq = ksp->ks_private;
21800Sstevel@tonic-gate 	taskq_bucket_t *b = tq->tq_buckets;
21810Sstevel@tonic-gate 	int bid = 0;
21820Sstevel@tonic-gate 
21830Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
21840Sstevel@tonic-gate 		return (EACCES);
21850Sstevel@tonic-gate 
21860Sstevel@tonic-gate 	ASSERT(tq->tq_flags & TASKQ_DYNAMIC);
21870Sstevel@tonic-gate 
21880Sstevel@tonic-gate 	tqsp->tqd_btasks.value.ui64 = tq->tq_tasks;
21890Sstevel@tonic-gate 	tqsp->tqd_bexecuted.value.ui64 = tq->tq_executed;
21900Sstevel@tonic-gate 	tqsp->tqd_bmaxtasks.value.ui64 = tq->tq_maxtasks;
21910Sstevel@tonic-gate 	tqsp->tqd_bnalloc.value.ui64 = tq->tq_nalloc;
21920Sstevel@tonic-gate 	tqsp->tqd_bnactive.value.ui64 = tq->tq_active;
21930Sstevel@tonic-gate 	tqsp->tqd_btotaltime.value.ui64 = tq->tq_totaltime;
21940Sstevel@tonic-gate 	tqsp->tqd_pri.value.ui64 = tq->tq_pri;
21950Sstevel@tonic-gate 
21960Sstevel@tonic-gate 	tqsp->tqd_hits.value.ui64 = 0;
21970Sstevel@tonic-gate 	tqsp->tqd_misses.value.ui64 = 0;
21980Sstevel@tonic-gate 	tqsp->tqd_overflows.value.ui64 = 0;
21990Sstevel@tonic-gate 	tqsp->tqd_tcreates.value.ui64 = 0;
22000Sstevel@tonic-gate 	tqsp->tqd_tdeaths.value.ui64 = 0;
22010Sstevel@tonic-gate 	tqsp->tqd_maxthreads.value.ui64 = 0;
22020Sstevel@tonic-gate 	tqsp->tqd_nomem.value.ui64 = 0;
22030Sstevel@tonic-gate 	tqsp->tqd_disptcreates.value.ui64 = 0;
22040Sstevel@tonic-gate 	tqsp->tqd_totaltime.value.ui64 = 0;
22050Sstevel@tonic-gate 	tqsp->tqd_nalloc.value.ui64 = 0;
22060Sstevel@tonic-gate 	tqsp->tqd_nfree.value.ui64 = 0;
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate 	for (; (b != NULL) && (bid < tq->tq_nbuckets); b++, bid++) {
22090Sstevel@tonic-gate 		tqsp->tqd_hits.value.ui64 += b->tqbucket_stat.tqs_hits;
22100Sstevel@tonic-gate 		tqsp->tqd_misses.value.ui64 += b->tqbucket_stat.tqs_misses;
22110Sstevel@tonic-gate 		tqsp->tqd_overflows.value.ui64 += b->tqbucket_stat.tqs_overflow;
22120Sstevel@tonic-gate 		tqsp->tqd_tcreates.value.ui64 += b->tqbucket_stat.tqs_tcreates;
22130Sstevel@tonic-gate 		tqsp->tqd_tdeaths.value.ui64 += b->tqbucket_stat.tqs_tdeaths;
22140Sstevel@tonic-gate 		tqsp->tqd_maxthreads.value.ui64 +=
22150Sstevel@tonic-gate 		    b->tqbucket_stat.tqs_maxthreads;
22160Sstevel@tonic-gate 		tqsp->tqd_nomem.value.ui64 += b->tqbucket_stat.tqs_nomem;
22170Sstevel@tonic-gate 		tqsp->tqd_disptcreates.value.ui64 +=
22180Sstevel@tonic-gate 		    b->tqbucket_stat.tqs_disptcreates;
22190Sstevel@tonic-gate 		tqsp->tqd_totaltime.value.ui64 += b->tqbucket_totaltime;
22200Sstevel@tonic-gate 		tqsp->tqd_nalloc.value.ui64 += b->tqbucket_nalloc;
22210Sstevel@tonic-gate 		tqsp->tqd_nfree.value.ui64 += b->tqbucket_nfree;
22220Sstevel@tonic-gate 	}
22230Sstevel@tonic-gate 	return (0);
22240Sstevel@tonic-gate }
2225