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 5*9515SJonathan.Adams@Sun.COM * Common Development and Distribution License (the "License"). 6*9515SJonathan.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 /* 22*9515SJonathan.Adams@Sun.COM * Copyright 2009 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 580Sstevel@tonic-gate * 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 * 75*9515SJonathan.Adams@Sun.COM * INTERFACES ================================================================== 760Sstevel@tonic-gate * 770Sstevel@tonic-gate * taskq_t *taskq_create(name, nthreads, pri_t 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 83*9515SJonathan.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 88*9515SJonathan.Adams@Sun.COM * single-list task queue is created with 'nthreads' threads 89*9515SJonathan.Adams@Sun.COM * servicing it. Entries in this queue are managed by 90*9515SJonathan.Adams@Sun.COM * taskq_ent_alloc() and taskq_ent_free() which try to keep the 91*9515SJonathan.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 * 104*9515SJonathan.Adams@Sun.COM * TASKQ_THREADS_CPU_PCT: This flag specifies that 'nthreads' should be 105*9515SJonathan.Adams@Sun.COM * interpreted as a percentage of the # of online CPUs on the 106*9515SJonathan.Adams@Sun.COM * system. The taskq subsystem will automatically adjust the 107*9515SJonathan.Adams@Sun.COM * number of threads in the taskq in response to CPU online 108*9515SJonathan.Adams@Sun.COM * and offline events, to keep the ratio. nthreads must be in 109*9515SJonathan.Adams@Sun.COM * the range [0,100]. 110*9515SJonathan.Adams@Sun.COM * 111*9515SJonathan.Adams@Sun.COM * The calculation used is: 112*9515SJonathan.Adams@Sun.COM * 113*9515SJonathan.Adams@Sun.COM * MAX((ncpus_online * percentage)/100, 1) 114*9515SJonathan.Adams@Sun.COM * 115*9515SJonathan.Adams@Sun.COM * This flag is not supported for DYNAMIC task queues. 116*9515SJonathan.Adams@Sun.COM * This flag is not compatible with TASKQ_CPR_SAFE. 117*9515SJonathan.Adams@Sun.COM * 1180Sstevel@tonic-gate * TASKQ_CPR_SAFE: This flag specifies that users of the task queue will 119*9515SJonathan.Adams@Sun.COM * use their own protocol for handling CPR issues. This flag is not 120*9515SJonathan.Adams@Sun.COM * supported for DYNAMIC task queues. This flag is not compatible 121*9515SJonathan.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 * 1260Sstevel@tonic-gate * void taskq_destroy(tap): 1270Sstevel@tonic-gate * 1280Sstevel@tonic-gate * Waits for any scheduled tasks to complete, then destroys the taskq. 1290Sstevel@tonic-gate * Caller should guarantee that no new tasks are scheduled in the closing 1300Sstevel@tonic-gate * taskq. 1310Sstevel@tonic-gate * 1320Sstevel@tonic-gate * taskqid_t taskq_dispatch(tq, func, arg, flags): 1330Sstevel@tonic-gate * 1340Sstevel@tonic-gate * Dispatches the task "func(arg)" to taskq. The 'flags' indicates whether 1350Sstevel@tonic-gate * the caller is willing to block for memory. The function returns an 1360Sstevel@tonic-gate * opaque value which is zero iff dispatch fails. If flags is TQ_NOSLEEP 1370Sstevel@tonic-gate * or TQ_NOALLOC and the task can't be dispatched, taskq_dispatch() fails 1380Sstevel@tonic-gate * and returns (taskqid_t)0. 1390Sstevel@tonic-gate * 1400Sstevel@tonic-gate * ASSUMES: func != NULL. 1410Sstevel@tonic-gate * 1420Sstevel@tonic-gate * Possible flags: 1430Sstevel@tonic-gate * TQ_NOSLEEP: Do not wait for resources; may fail. 1440Sstevel@tonic-gate * 1450Sstevel@tonic-gate * TQ_NOALLOC: Do not allocate memory; may fail. May only be used with 1460Sstevel@tonic-gate * non-dynamic task queues. 1470Sstevel@tonic-gate * 1480Sstevel@tonic-gate * TQ_NOQUEUE: Do not enqueue a task if it can't dispatch it due to 1490Sstevel@tonic-gate * lack of available resources and fail. If this flag is not 1500Sstevel@tonic-gate * set, and the task pool is exhausted, the task may be scheduled 1510Sstevel@tonic-gate * in the backing queue. This flag may ONLY be used with dynamic 1520Sstevel@tonic-gate * task queues. 1530Sstevel@tonic-gate * 1540Sstevel@tonic-gate * NOTE: This flag should always be used when a task queue is used 1550Sstevel@tonic-gate * for tasks that may depend on each other for completion. 1560Sstevel@tonic-gate * Enqueueing dependent tasks may create deadlocks. 1570Sstevel@tonic-gate * 1580Sstevel@tonic-gate * TQ_SLEEP: May block waiting for resources. May still fail for 1590Sstevel@tonic-gate * dynamic task queues if TQ_NOQUEUE is also specified, otherwise 1600Sstevel@tonic-gate * always succeed. 1610Sstevel@tonic-gate * 1620Sstevel@tonic-gate * NOTE: Dynamic task queues are much more likely to fail in 1630Sstevel@tonic-gate * taskq_dispatch() (especially if TQ_NOQUEUE was specified), so it 1640Sstevel@tonic-gate * is important to have backup strategies handling such failures. 1650Sstevel@tonic-gate * 1660Sstevel@tonic-gate * void taskq_wait(tq): 1670Sstevel@tonic-gate * 1680Sstevel@tonic-gate * Waits for all previously scheduled tasks to complete. 1690Sstevel@tonic-gate * 1700Sstevel@tonic-gate * NOTE: It does not stop any new task dispatches. 1710Sstevel@tonic-gate * Do NOT call taskq_wait() from a task: it will cause deadlock. 1720Sstevel@tonic-gate * 1730Sstevel@tonic-gate * void taskq_suspend(tq) 1740Sstevel@tonic-gate * 1750Sstevel@tonic-gate * Suspend all task execution. Tasks already scheduled for a dynamic task 1760Sstevel@tonic-gate * queue will still be executed, but all new scheduled tasks will be 1770Sstevel@tonic-gate * suspended until taskq_resume() is called. 1780Sstevel@tonic-gate * 1790Sstevel@tonic-gate * int taskq_suspended(tq) 1800Sstevel@tonic-gate * 1810Sstevel@tonic-gate * Returns 1 if taskq is suspended and 0 otherwise. It is intended to 1820Sstevel@tonic-gate * ASSERT that the task queue is suspended. 1830Sstevel@tonic-gate * 1840Sstevel@tonic-gate * void taskq_resume(tq) 1850Sstevel@tonic-gate * 1860Sstevel@tonic-gate * Resume task queue execution. 1870Sstevel@tonic-gate * 1880Sstevel@tonic-gate * int taskq_member(tq, thread) 1890Sstevel@tonic-gate * 1900Sstevel@tonic-gate * Returns 1 if 'thread' belongs to taskq 'tq' and 0 otherwise. The 1910Sstevel@tonic-gate * intended use is to ASSERT that a given function is called in taskq 1920Sstevel@tonic-gate * context only. 1930Sstevel@tonic-gate * 1940Sstevel@tonic-gate * system_taskq 1950Sstevel@tonic-gate * 1960Sstevel@tonic-gate * Global system-wide dynamic task queue for common uses. It may be used by 1970Sstevel@tonic-gate * any subsystem that needs to schedule tasks and does not need to manage 1980Sstevel@tonic-gate * its own task queues. It is initialized quite early during system boot. 1990Sstevel@tonic-gate * 200*9515SJonathan.Adams@Sun.COM * IMPLEMENTATION ============================================================== 2010Sstevel@tonic-gate * 2020Sstevel@tonic-gate * This is schematic representation of the task queue structures. 2030Sstevel@tonic-gate * 2040Sstevel@tonic-gate * taskq: 2050Sstevel@tonic-gate * +-------------+ 2060Sstevel@tonic-gate * |tq_lock | +---< taskq_ent_free() 2070Sstevel@tonic-gate * +-------------+ | 2080Sstevel@tonic-gate * |... | | tqent: tqent: 2090Sstevel@tonic-gate * +-------------+ | +------------+ +------------+ 2100Sstevel@tonic-gate * | tq_freelist |-->| tqent_next |--> ... ->| tqent_next | 2110Sstevel@tonic-gate * +-------------+ +------------+ +------------+ 2120Sstevel@tonic-gate * |... | | ... | | ... | 2130Sstevel@tonic-gate * +-------------+ +------------+ +------------+ 2140Sstevel@tonic-gate * | tq_task | | 2150Sstevel@tonic-gate * | | +-------------->taskq_ent_alloc() 2160Sstevel@tonic-gate * +--------------------------------------------------------------------------+ 2170Sstevel@tonic-gate * | | | tqent tqent | 2180Sstevel@tonic-gate * | +---------------------+ +--> +------------+ +--> +------------+ | 2190Sstevel@tonic-gate * | | ... | | | func, arg | | | func, arg | | 2200Sstevel@tonic-gate * +>+---------------------+ <---|-+ +------------+ <---|-+ +------------+ | 2210Sstevel@tonic-gate * | tq_taskq.tqent_next | ----+ | | tqent_next | --->+ | | tqent_next |--+ 2220Sstevel@tonic-gate * +---------------------+ | +------------+ ^ | +------------+ 2230Sstevel@tonic-gate * +-| tq_task.tqent_prev | +--| tqent_prev | | +--| tqent_prev | ^ 2240Sstevel@tonic-gate * | +---------------------+ +------------+ | +------------+ | 2250Sstevel@tonic-gate * | |... | | ... | | | ... | | 2260Sstevel@tonic-gate * | +---------------------+ +------------+ | +------------+ | 2270Sstevel@tonic-gate * | ^ | | 2280Sstevel@tonic-gate * | | | | 2290Sstevel@tonic-gate * +--------------------------------------+--------------+ TQ_APPEND() -+ 2300Sstevel@tonic-gate * | | | 2310Sstevel@tonic-gate * |... | taskq_thread()-----+ 2320Sstevel@tonic-gate * +-------------+ 2330Sstevel@tonic-gate * | tq_buckets |--+-------> [ NULL ] (for regular task queues) 2340Sstevel@tonic-gate * +-------------+ | 2350Sstevel@tonic-gate * | DYNAMIC TASK QUEUES: 2360Sstevel@tonic-gate * | 2370Sstevel@tonic-gate * +-> taskq_bucket[nCPU] taskq_bucket_dispatch() 2380Sstevel@tonic-gate * +-------------------+ ^ 2390Sstevel@tonic-gate * +--->| tqbucket_lock | | 2400Sstevel@tonic-gate * | +-------------------+ +--------+ +--------+ 2410Sstevel@tonic-gate * | | tqbucket_freelist |-->| tqent |-->...| tqent | ^ 2420Sstevel@tonic-gate * | +-------------------+<--+--------+<--...+--------+ | 2430Sstevel@tonic-gate * | | ... | | thread | | thread | | 2440Sstevel@tonic-gate * | +-------------------+ +--------+ +--------+ | 2450Sstevel@tonic-gate * | +-------------------+ | 2460Sstevel@tonic-gate * taskq_dispatch()--+--->| tqbucket_lock | TQ_APPEND()------+ 2470Sstevel@tonic-gate * TQ_HASH() | +-------------------+ +--------+ +--------+ 2480Sstevel@tonic-gate * | | tqbucket_freelist |-->| tqent |-->...| tqent | 2490Sstevel@tonic-gate * | +-------------------+<--+--------+<--...+--------+ 2500Sstevel@tonic-gate * | | ... | | thread | | thread | 2510Sstevel@tonic-gate * | +-------------------+ +--------+ +--------+ 2520Sstevel@tonic-gate * +---> ... 2530Sstevel@tonic-gate * 2540Sstevel@tonic-gate * 2550Sstevel@tonic-gate * Task queues use tq_task field to link new entry in the queue. The queue is a 2560Sstevel@tonic-gate * circular doubly-linked list. Entries are put in the end of the list with 2570Sstevel@tonic-gate * TQ_APPEND() and processed from the front of the list by taskq_thread() in 2580Sstevel@tonic-gate * FIFO order. Task queue entries are cached in the free list managed by 2590Sstevel@tonic-gate * taskq_ent_alloc() and taskq_ent_free() functions. 2600Sstevel@tonic-gate * 2610Sstevel@tonic-gate * All threads used by task queues mark t_taskq field of the thread to 2620Sstevel@tonic-gate * point to the task queue. 2630Sstevel@tonic-gate * 264*9515SJonathan.Adams@Sun.COM * Taskq Thread Management ----------------------------------------------------- 265*9515SJonathan.Adams@Sun.COM * 266*9515SJonathan.Adams@Sun.COM * Taskq's non-dynamic threads are managed with several variables and flags: 267*9515SJonathan.Adams@Sun.COM * 268*9515SJonathan.Adams@Sun.COM * * tq_nthreads - The number of threads in taskq_thread() for the 269*9515SJonathan.Adams@Sun.COM * taskq. 270*9515SJonathan.Adams@Sun.COM * 271*9515SJonathan.Adams@Sun.COM * * tq_active - The number of threads not waiting on a CV in 272*9515SJonathan.Adams@Sun.COM * taskq_thread(); includes newly created threads 273*9515SJonathan.Adams@Sun.COM * not yet counted in tq_nthreads. 274*9515SJonathan.Adams@Sun.COM * 275*9515SJonathan.Adams@Sun.COM * * tq_nthreads_target 276*9515SJonathan.Adams@Sun.COM * - The number of threads desired for the taskq. 277*9515SJonathan.Adams@Sun.COM * 278*9515SJonathan.Adams@Sun.COM * * tq_flags & TASKQ_CHANGING 279*9515SJonathan.Adams@Sun.COM * - Indicates that tq_nthreads != tq_nthreads_target. 280*9515SJonathan.Adams@Sun.COM * 281*9515SJonathan.Adams@Sun.COM * * tq_flags & TASKQ_THREAD_CREATED 282*9515SJonathan.Adams@Sun.COM * - Indicates that a thread is being created in the taskq. 283*9515SJonathan.Adams@Sun.COM * 284*9515SJonathan.Adams@Sun.COM * During creation, tq_nthreads and tq_active are set to 0, and 285*9515SJonathan.Adams@Sun.COM * tq_nthreads_target is set to the number of threads desired. The 286*9515SJonathan.Adams@Sun.COM * TASKQ_CHANGING flag is set, and taskq_create_thread() is called to 287*9515SJonathan.Adams@Sun.COM * create the first thread. taskq_create_thread() increments tq_active, 288*9515SJonathan.Adams@Sun.COM * sets TASKQ_THREAD_CREATED, and creates the new thread. 289*9515SJonathan.Adams@Sun.COM * 290*9515SJonathan.Adams@Sun.COM * Each thread starts in taskq_thread(), clears the TASKQ_THREAD_CREATED 291*9515SJonathan.Adams@Sun.COM * flag, and increments tq_nthreads. It stores the new value of 292*9515SJonathan.Adams@Sun.COM * tq_nthreads as its "thread_id", and stores its thread pointer in the 293*9515SJonathan.Adams@Sun.COM * tq_threadlist at the (thread_id - 1). We keep the thread_id space 294*9515SJonathan.Adams@Sun.COM * densely packed by requiring that only the largest thread_id can exit during 295*9515SJonathan.Adams@Sun.COM * normal adjustment. The exception is during the destruction of the 296*9515SJonathan.Adams@Sun.COM * taskq; once tq_nthreads_target is set to zero, no new threads will be created 297*9515SJonathan.Adams@Sun.COM * for the taskq queue, so every thread can exit without any ordering being 298*9515SJonathan.Adams@Sun.COM * necessary. 299*9515SJonathan.Adams@Sun.COM * 300*9515SJonathan.Adams@Sun.COM * Threads will only process work if their thread id is <= tq_nthreads_target. 301*9515SJonathan.Adams@Sun.COM * 302*9515SJonathan.Adams@Sun.COM * When TASKQ_CHANGING is set, threads will check the current thread target 303*9515SJonathan.Adams@Sun.COM * whenever they wake up, and do whatever they can to apply its effects. 304*9515SJonathan.Adams@Sun.COM * 305*9515SJonathan.Adams@Sun.COM * TASKQ_THREAD_CPU_PCT -------------------------------------------------------- 306*9515SJonathan.Adams@Sun.COM * 307*9515SJonathan.Adams@Sun.COM * When a taskq is created with TASKQ_THREAD_CPU_PCT, we store their requested 308*9515SJonathan.Adams@Sun.COM * percentage in tq_threads_ncpus_pct, start them off with the correct thread 309*9515SJonathan.Adams@Sun.COM * target, and add them to the taskq_cpupct_list for later adjustment. 310*9515SJonathan.Adams@Sun.COM * 311*9515SJonathan.Adams@Sun.COM * We register taskq_cpu_setup() to be called whenever a CPU changes state. It 312*9515SJonathan.Adams@Sun.COM * walks the list of TASKQ_THREAD_CPU_PCT taskqs, adjusts their nthread_target 313*9515SJonathan.Adams@Sun.COM * if need be, and wakes up all of the threads to process the change. 314*9515SJonathan.Adams@Sun.COM * 315*9515SJonathan.Adams@Sun.COM * Dynamic Task Queues Implementation ------------------------------------------ 3160Sstevel@tonic-gate * 3170Sstevel@tonic-gate * For a dynamic task queues there is a 1-to-1 mapping between a thread and 3180Sstevel@tonic-gate * taskq_ent_structure. Each entry is serviced by its own thread and each thread 3190Sstevel@tonic-gate * is controlled by a single entry. 3200Sstevel@tonic-gate * 3210Sstevel@tonic-gate * Entries are distributed over a set of buckets. To avoid using modulo 3220Sstevel@tonic-gate * arithmetics the number of buckets is 2^n and is determined as the nearest 3230Sstevel@tonic-gate * power of two roundown of the number of CPUs in the system. Tunable 3240Sstevel@tonic-gate * variable 'taskq_maxbuckets' limits the maximum number of buckets. Each entry 3250Sstevel@tonic-gate * is attached to a bucket for its lifetime and can't migrate to other buckets. 3260Sstevel@tonic-gate * 3270Sstevel@tonic-gate * Entries that have scheduled tasks are not placed in any list. The dispatch 3280Sstevel@tonic-gate * function sets their "func" and "arg" fields and signals the corresponding 3290Sstevel@tonic-gate * thread to execute the task. Once the thread executes the task it clears the 3300Sstevel@tonic-gate * "func" field and places an entry on the bucket cache of free entries pointed 3310Sstevel@tonic-gate * by "tqbucket_freelist" field. ALL entries on the free list should have "func" 3320Sstevel@tonic-gate * field equal to NULL. The free list is a circular doubly-linked list identical 3330Sstevel@tonic-gate * in structure to the tq_task list above, but entries are taken from it in LIFO 3340Sstevel@tonic-gate * order - the last freed entry is the first to be allocated. The 3350Sstevel@tonic-gate * taskq_bucket_dispatch() function gets the most recently used entry from the 3360Sstevel@tonic-gate * free list, sets its "func" and "arg" fields and signals a worker thread. 3370Sstevel@tonic-gate * 3380Sstevel@tonic-gate * After executing each task a per-entry thread taskq_d_thread() places its 3390Sstevel@tonic-gate * entry on the bucket free list and goes to a timed sleep. If it wakes up 3400Sstevel@tonic-gate * without getting new task it removes the entry from the free list and destroys 3410Sstevel@tonic-gate * itself. The thread sleep time is controlled by a tunable variable 3420Sstevel@tonic-gate * `taskq_thread_timeout'. 3430Sstevel@tonic-gate * 344*9515SJonathan.Adams@Sun.COM * There are various statistics kept in the bucket which allows for later 3450Sstevel@tonic-gate * analysis of taskq usage patterns. Also, a global copy of taskq creation and 3460Sstevel@tonic-gate * death statistics is kept in the global taskq data structure. Since thread 3470Sstevel@tonic-gate * creation and death happen rarely, updating such global data does not present 3480Sstevel@tonic-gate * a performance problem. 3490Sstevel@tonic-gate * 3500Sstevel@tonic-gate * NOTE: Threads are not bound to any CPU and there is absolutely no association 3510Sstevel@tonic-gate * between the bucket and actual thread CPU, so buckets are used only to 3520Sstevel@tonic-gate * split resources and reduce resource contention. Having threads attached 3530Sstevel@tonic-gate * to the CPU denoted by a bucket may reduce number of times the job 3540Sstevel@tonic-gate * switches between CPUs. 3550Sstevel@tonic-gate * 3560Sstevel@tonic-gate * Current algorithm creates a thread whenever a bucket has no free 3570Sstevel@tonic-gate * entries. It would be nice to know how many threads are in the running 3580Sstevel@tonic-gate * state and don't create threads if all CPUs are busy with existing 3590Sstevel@tonic-gate * tasks, but it is unclear how such strategy can be implemented. 3600Sstevel@tonic-gate * 3610Sstevel@tonic-gate * Currently buckets are created statically as an array attached to task 3620Sstevel@tonic-gate * queue. On some system with nCPUs < max_ncpus it may waste system 3630Sstevel@tonic-gate * memory. One solution may be allocation of buckets when they are first 3640Sstevel@tonic-gate * touched, but it is not clear how useful it is. 3650Sstevel@tonic-gate * 366*9515SJonathan.Adams@Sun.COM * SUSPEND/RESUME implementation ----------------------------------------------- 3670Sstevel@tonic-gate * 3680Sstevel@tonic-gate * Before executing a task taskq_thread() (executing non-dynamic task 3690Sstevel@tonic-gate * queues) obtains taskq's thread lock as a reader. The taskq_suspend() 3700Sstevel@tonic-gate * function gets the same lock as a writer blocking all non-dynamic task 3710Sstevel@tonic-gate * execution. The taskq_resume() function releases the lock allowing 3720Sstevel@tonic-gate * taskq_thread to continue execution. 3730Sstevel@tonic-gate * 3740Sstevel@tonic-gate * For dynamic task queues, each bucket is marked as TQBUCKET_SUSPEND by 3750Sstevel@tonic-gate * taskq_suspend() function. After that taskq_bucket_dispatch() always 3760Sstevel@tonic-gate * fails, so that taskq_dispatch() will either enqueue tasks for a 3770Sstevel@tonic-gate * suspended backing queue or fail if TQ_NOQUEUE is specified in dispatch 3780Sstevel@tonic-gate * flags. 3790Sstevel@tonic-gate * 3800Sstevel@tonic-gate * NOTE: taskq_suspend() does not immediately block any tasks already 3810Sstevel@tonic-gate * scheduled for dynamic task queues. It only suspends new tasks 3820Sstevel@tonic-gate * scheduled after taskq_suspend() was called. 3830Sstevel@tonic-gate * 3840Sstevel@tonic-gate * taskq_member() function works by comparing a thread t_taskq pointer with 3850Sstevel@tonic-gate * the passed thread pointer. 3860Sstevel@tonic-gate * 387*9515SJonathan.Adams@Sun.COM * LOCKS and LOCK Hierarchy ---------------------------------------------------- 3880Sstevel@tonic-gate * 389*9515SJonathan.Adams@Sun.COM * There are three locks used in task queues: 3900Sstevel@tonic-gate * 391*9515SJonathan.Adams@Sun.COM * 1) The taskq_t's tq_lock, protecting global task queue state. 3920Sstevel@tonic-gate * 3930Sstevel@tonic-gate * 2) Each per-CPU bucket has a lock for bucket management. 3940Sstevel@tonic-gate * 395*9515SJonathan.Adams@Sun.COM * 3) The global taskq_cpupct_lock, which protects the list of 396*9515SJonathan.Adams@Sun.COM * TASKQ_THREADS_CPU_PCT taskqs. 397*9515SJonathan.Adams@Sun.COM * 398*9515SJonathan.Adams@Sun.COM * If both (1) and (2) are needed, tq_lock should be taken *after* the bucket 3990Sstevel@tonic-gate * lock. 4000Sstevel@tonic-gate * 401*9515SJonathan.Adams@Sun.COM * If both (1) and (3) are needed, tq_lock should be taken *after* 402*9515SJonathan.Adams@Sun.COM * taskq_cpupct_lock. 403*9515SJonathan.Adams@Sun.COM * 404*9515SJonathan.Adams@Sun.COM * DEBUG FACILITIES ------------------------------------------------------------ 4050Sstevel@tonic-gate * 4060Sstevel@tonic-gate * For DEBUG kernels it is possible to induce random failures to 4070Sstevel@tonic-gate * taskq_dispatch() function when it is given TQ_NOSLEEP argument. The value of 4080Sstevel@tonic-gate * taskq_dmtbf and taskq_smtbf tunables control the mean time between induced 4090Sstevel@tonic-gate * failures for dynamic and static task queues respectively. 4100Sstevel@tonic-gate * 4110Sstevel@tonic-gate * Setting TASKQ_STATISTIC to 0 will disable per-bucket statistics. 4120Sstevel@tonic-gate * 413*9515SJonathan.Adams@Sun.COM * TUNABLES -------------------------------------------------------------------- 4140Sstevel@tonic-gate * 4150Sstevel@tonic-gate * system_taskq_size - Size of the global system_taskq. 4160Sstevel@tonic-gate * This value is multiplied by nCPUs to determine 4170Sstevel@tonic-gate * actual size. 4180Sstevel@tonic-gate * Default value: 64 4190Sstevel@tonic-gate * 420*9515SJonathan.Adams@Sun.COM * taskq_minimum_nthreads_max 421*9515SJonathan.Adams@Sun.COM * - Minimum size of the thread list for a taskq. 422*9515SJonathan.Adams@Sun.COM * Useful for testing different thread pool 423*9515SJonathan.Adams@Sun.COM * sizes by overwriting tq_nthreads_target. 424*9515SJonathan.Adams@Sun.COM * 4250Sstevel@tonic-gate * taskq_thread_timeout - Maximum idle time for taskq_d_thread() 4260Sstevel@tonic-gate * Default value: 5 minutes 4270Sstevel@tonic-gate * 4280Sstevel@tonic-gate * taskq_maxbuckets - Maximum number of buckets in any task queue 4290Sstevel@tonic-gate * Default value: 128 4300Sstevel@tonic-gate * 4310Sstevel@tonic-gate * taskq_search_depth - Maximum # of buckets searched for a free entry 4320Sstevel@tonic-gate * Default value: 4 4330Sstevel@tonic-gate * 4340Sstevel@tonic-gate * taskq_dmtbf - Mean time between induced dispatch failures 4350Sstevel@tonic-gate * for dynamic task queues. 4360Sstevel@tonic-gate * Default value: UINT_MAX (no induced failures) 4370Sstevel@tonic-gate * 4380Sstevel@tonic-gate * taskq_smtbf - Mean time between induced dispatch failures 4390Sstevel@tonic-gate * for static task queues. 4400Sstevel@tonic-gate * Default value: UINT_MAX (no induced failures) 4410Sstevel@tonic-gate * 442*9515SJonathan.Adams@Sun.COM * CONDITIONAL compilation ----------------------------------------------------- 4430Sstevel@tonic-gate * 4440Sstevel@tonic-gate * TASKQ_STATISTIC - If set will enable bucket statistic (default). 4450Sstevel@tonic-gate * 4460Sstevel@tonic-gate */ 4470Sstevel@tonic-gate 4480Sstevel@tonic-gate #include <sys/taskq_impl.h> 4490Sstevel@tonic-gate #include <sys/thread.h> 4500Sstevel@tonic-gate #include <sys/proc.h> 4510Sstevel@tonic-gate #include <sys/kmem.h> 4520Sstevel@tonic-gate #include <sys/vmem.h> 4530Sstevel@tonic-gate #include <sys/callb.h> 4540Sstevel@tonic-gate #include <sys/systm.h> 4550Sstevel@tonic-gate #include <sys/cmn_err.h> 4560Sstevel@tonic-gate #include <sys/debug.h> 4570Sstevel@tonic-gate #include <sys/vmsystm.h> /* For throttlefree */ 4580Sstevel@tonic-gate #include <sys/sysmacros.h> 4590Sstevel@tonic-gate #include <sys/cpuvar.h> 4600Sstevel@tonic-gate #include <sys/sdt.h> 461*9515SJonathan.Adams@Sun.COM #include <sys/note.h> 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate static kmem_cache_t *taskq_ent_cache, *taskq_cache; 4640Sstevel@tonic-gate 4650Sstevel@tonic-gate /* 466*9515SJonathan.Adams@Sun.COM * Pseudo instance numbers for taskqs without explicitly provided instance. 4670Sstevel@tonic-gate */ 4680Sstevel@tonic-gate static vmem_t *taskq_id_arena; 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate /* Global system task queue for common use */ 4710Sstevel@tonic-gate taskq_t *system_taskq; 4720Sstevel@tonic-gate 4730Sstevel@tonic-gate /* 474*9515SJonathan.Adams@Sun.COM * Maximum number of entries in global system taskq is 4750Sstevel@tonic-gate * system_taskq_size * max_ncpus 4760Sstevel@tonic-gate */ 4770Sstevel@tonic-gate #define SYSTEM_TASKQ_SIZE 64 4780Sstevel@tonic-gate int system_taskq_size = SYSTEM_TASKQ_SIZE; 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate /* 481*9515SJonathan.Adams@Sun.COM * Minimum size for tq_nthreads_max; useful for those who want to play around 482*9515SJonathan.Adams@Sun.COM * with increasing a taskq's tq_nthreads_target. 483*9515SJonathan.Adams@Sun.COM */ 484*9515SJonathan.Adams@Sun.COM int taskq_minimum_nthreads_max = 1; 485*9515SJonathan.Adams@Sun.COM 486*9515SJonathan.Adams@Sun.COM /* Maximum percentage allowed for TASKQ_THREADS_CPU_PCT */ 487*9515SJonathan.Adams@Sun.COM #define TASKQ_CPUPCT_MAX_PERCENT 1000 488*9515SJonathan.Adams@Sun.COM int taskq_cpupct_max_percent = TASKQ_CPUPCT_MAX_PERCENT; 489*9515SJonathan.Adams@Sun.COM 490*9515SJonathan.Adams@Sun.COM /* 4910Sstevel@tonic-gate * Dynamic task queue threads that don't get any work within 4920Sstevel@tonic-gate * taskq_thread_timeout destroy themselves 4930Sstevel@tonic-gate */ 4940Sstevel@tonic-gate #define TASKQ_THREAD_TIMEOUT (60 * 5) 4950Sstevel@tonic-gate int taskq_thread_timeout = TASKQ_THREAD_TIMEOUT; 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate #define TASKQ_MAXBUCKETS 128 4980Sstevel@tonic-gate int taskq_maxbuckets = TASKQ_MAXBUCKETS; 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate /* 5010Sstevel@tonic-gate * When a bucket has no available entries another buckets are tried. 5020Sstevel@tonic-gate * taskq_search_depth parameter limits the amount of buckets that we search 5030Sstevel@tonic-gate * before failing. This is mostly useful in systems with many CPUs where we may 5040Sstevel@tonic-gate * spend too much time scanning busy buckets. 5050Sstevel@tonic-gate */ 5060Sstevel@tonic-gate #define TASKQ_SEARCH_DEPTH 4 5070Sstevel@tonic-gate int taskq_search_depth = TASKQ_SEARCH_DEPTH; 5080Sstevel@tonic-gate 5090Sstevel@tonic-gate /* 5100Sstevel@tonic-gate * Hashing function: mix various bits of x. May be pretty much anything. 5110Sstevel@tonic-gate */ 5120Sstevel@tonic-gate #define TQ_HASH(x) ((x) ^ ((x) >> 11) ^ ((x) >> 17) ^ ((x) ^ 27)) 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate /* 5150Sstevel@tonic-gate * We do not create any new threads when the system is low on memory and start 5160Sstevel@tonic-gate * throttling memory allocations. The following macro tries to estimate such 5170Sstevel@tonic-gate * condition. 5180Sstevel@tonic-gate */ 5190Sstevel@tonic-gate #define ENOUGH_MEMORY() (freemem > throttlefree) 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate /* 5220Sstevel@tonic-gate * Static functions. 5230Sstevel@tonic-gate */ 5240Sstevel@tonic-gate static taskq_t *taskq_create_common(const char *, int, int, pri_t, int, 5250Sstevel@tonic-gate int, uint_t); 5260Sstevel@tonic-gate static void taskq_thread(void *); 5270Sstevel@tonic-gate static void taskq_d_thread(taskq_ent_t *); 5280Sstevel@tonic-gate static void taskq_bucket_extend(void *); 5290Sstevel@tonic-gate static int taskq_constructor(void *, void *, int); 5300Sstevel@tonic-gate static void taskq_destructor(void *, void *); 5310Sstevel@tonic-gate static int taskq_ent_constructor(void *, void *, int); 5320Sstevel@tonic-gate static void taskq_ent_destructor(void *, void *); 5330Sstevel@tonic-gate static taskq_ent_t *taskq_ent_alloc(taskq_t *, int); 5340Sstevel@tonic-gate static void taskq_ent_free(taskq_t *, taskq_ent_t *); 5350Sstevel@tonic-gate static taskq_ent_t *taskq_bucket_dispatch(taskq_bucket_t *, task_func_t, 5360Sstevel@tonic-gate void *); 5370Sstevel@tonic-gate 5380Sstevel@tonic-gate /* 5390Sstevel@tonic-gate * Task queues kstats. 5400Sstevel@tonic-gate */ 5410Sstevel@tonic-gate struct taskq_kstat { 5420Sstevel@tonic-gate kstat_named_t tq_tasks; 5430Sstevel@tonic-gate kstat_named_t tq_executed; 5440Sstevel@tonic-gate kstat_named_t tq_maxtasks; 5450Sstevel@tonic-gate kstat_named_t tq_totaltime; 5460Sstevel@tonic-gate kstat_named_t tq_nalloc; 5470Sstevel@tonic-gate kstat_named_t tq_nactive; 5480Sstevel@tonic-gate kstat_named_t tq_pri; 5490Sstevel@tonic-gate kstat_named_t tq_nthreads; 5500Sstevel@tonic-gate } taskq_kstat = { 5510Sstevel@tonic-gate { "tasks", KSTAT_DATA_UINT64 }, 5520Sstevel@tonic-gate { "executed", KSTAT_DATA_UINT64 }, 5530Sstevel@tonic-gate { "maxtasks", KSTAT_DATA_UINT64 }, 5540Sstevel@tonic-gate { "totaltime", KSTAT_DATA_UINT64 }, 5550Sstevel@tonic-gate { "nactive", KSTAT_DATA_UINT64 }, 5560Sstevel@tonic-gate { "nalloc", KSTAT_DATA_UINT64 }, 5570Sstevel@tonic-gate { "priority", KSTAT_DATA_UINT64 }, 5580Sstevel@tonic-gate { "threads", KSTAT_DATA_UINT64 }, 5590Sstevel@tonic-gate }; 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate struct taskq_d_kstat { 5620Sstevel@tonic-gate kstat_named_t tqd_pri; 5630Sstevel@tonic-gate kstat_named_t tqd_btasks; 5640Sstevel@tonic-gate kstat_named_t tqd_bexecuted; 5650Sstevel@tonic-gate kstat_named_t tqd_bmaxtasks; 5660Sstevel@tonic-gate kstat_named_t tqd_bnalloc; 5670Sstevel@tonic-gate kstat_named_t tqd_bnactive; 5680Sstevel@tonic-gate kstat_named_t tqd_btotaltime; 5690Sstevel@tonic-gate kstat_named_t tqd_hits; 5700Sstevel@tonic-gate kstat_named_t tqd_misses; 5710Sstevel@tonic-gate kstat_named_t tqd_overflows; 5720Sstevel@tonic-gate kstat_named_t tqd_tcreates; 5730Sstevel@tonic-gate kstat_named_t tqd_tdeaths; 5740Sstevel@tonic-gate kstat_named_t tqd_maxthreads; 5750Sstevel@tonic-gate kstat_named_t tqd_nomem; 5760Sstevel@tonic-gate kstat_named_t tqd_disptcreates; 5770Sstevel@tonic-gate kstat_named_t tqd_totaltime; 5780Sstevel@tonic-gate kstat_named_t tqd_nalloc; 5790Sstevel@tonic-gate kstat_named_t tqd_nfree; 5800Sstevel@tonic-gate } taskq_d_kstat = { 5810Sstevel@tonic-gate { "priority", KSTAT_DATA_UINT64 }, 5820Sstevel@tonic-gate { "btasks", KSTAT_DATA_UINT64 }, 5830Sstevel@tonic-gate { "bexecuted", KSTAT_DATA_UINT64 }, 5840Sstevel@tonic-gate { "bmaxtasks", KSTAT_DATA_UINT64 }, 5850Sstevel@tonic-gate { "bnalloc", KSTAT_DATA_UINT64 }, 5860Sstevel@tonic-gate { "bnactive", KSTAT_DATA_UINT64 }, 5870Sstevel@tonic-gate { "btotaltime", KSTAT_DATA_UINT64 }, 5880Sstevel@tonic-gate { "hits", KSTAT_DATA_UINT64 }, 5890Sstevel@tonic-gate { "misses", KSTAT_DATA_UINT64 }, 5900Sstevel@tonic-gate { "overflows", KSTAT_DATA_UINT64 }, 5910Sstevel@tonic-gate { "tcreates", KSTAT_DATA_UINT64 }, 5920Sstevel@tonic-gate { "tdeaths", KSTAT_DATA_UINT64 }, 5930Sstevel@tonic-gate { "maxthreads", KSTAT_DATA_UINT64 }, 5940Sstevel@tonic-gate { "nomem", KSTAT_DATA_UINT64 }, 5950Sstevel@tonic-gate { "disptcreates", KSTAT_DATA_UINT64 }, 5960Sstevel@tonic-gate { "totaltime", KSTAT_DATA_UINT64 }, 5970Sstevel@tonic-gate { "nalloc", KSTAT_DATA_UINT64 }, 5980Sstevel@tonic-gate { "nfree", KSTAT_DATA_UINT64 }, 5990Sstevel@tonic-gate }; 6000Sstevel@tonic-gate 6010Sstevel@tonic-gate static kmutex_t taskq_kstat_lock; 6020Sstevel@tonic-gate static kmutex_t taskq_d_kstat_lock; 6030Sstevel@tonic-gate static int taskq_kstat_update(kstat_t *, int); 6040Sstevel@tonic-gate static int taskq_d_kstat_update(kstat_t *, int); 6050Sstevel@tonic-gate 606*9515SJonathan.Adams@Sun.COM /* 607*9515SJonathan.Adams@Sun.COM * State for THREAD_CPU_PCT management 608*9515SJonathan.Adams@Sun.COM */ 609*9515SJonathan.Adams@Sun.COM typedef struct taskq_cpupct_ent { 610*9515SJonathan.Adams@Sun.COM list_node_t tp_link; 611*9515SJonathan.Adams@Sun.COM taskq_t *tp_taskq; 612*9515SJonathan.Adams@Sun.COM } taskq_cpupct_ent_t; 613*9515SJonathan.Adams@Sun.COM 614*9515SJonathan.Adams@Sun.COM static kmutex_t taskq_cpupct_lock; 615*9515SJonathan.Adams@Sun.COM static list_t taskq_cpupct_list; 616*9515SJonathan.Adams@Sun.COM static int taskq_cpupct_ncpus_online; 6170Sstevel@tonic-gate 6180Sstevel@tonic-gate /* 6190Sstevel@tonic-gate * Collect per-bucket statistic when TASKQ_STATISTIC is defined. 6200Sstevel@tonic-gate */ 6210Sstevel@tonic-gate #define TASKQ_STATISTIC 1 6220Sstevel@tonic-gate 6230Sstevel@tonic-gate #if TASKQ_STATISTIC 6240Sstevel@tonic-gate #define TQ_STAT(b, x) b->tqbucket_stat.x++ 6250Sstevel@tonic-gate #else 6260Sstevel@tonic-gate #define TQ_STAT(b, x) 6270Sstevel@tonic-gate #endif 6280Sstevel@tonic-gate 6290Sstevel@tonic-gate /* 6300Sstevel@tonic-gate * Random fault injection. 6310Sstevel@tonic-gate */ 6320Sstevel@tonic-gate uint_t taskq_random; 6330Sstevel@tonic-gate uint_t taskq_dmtbf = UINT_MAX; /* mean time between injected failures */ 6340Sstevel@tonic-gate uint_t taskq_smtbf = UINT_MAX; /* mean time between injected failures */ 6350Sstevel@tonic-gate 6360Sstevel@tonic-gate /* 6370Sstevel@tonic-gate * TQ_NOSLEEP dispatches on dynamic task queues are always allowed to fail. 6380Sstevel@tonic-gate * 6390Sstevel@tonic-gate * TQ_NOSLEEP dispatches on static task queues can't arbitrarily fail because 6400Sstevel@tonic-gate * they could prepopulate the cache and make sure that they do not use more 6410Sstevel@tonic-gate * then minalloc entries. So, fault injection in this case insures that 6420Sstevel@tonic-gate * either TASKQ_PREPOPULATE is not set or there are more entries allocated 6430Sstevel@tonic-gate * than is specified by minalloc. TQ_NOALLOC dispatches are always allowed 6440Sstevel@tonic-gate * to fail, but for simplicity we treat them identically to TQ_NOSLEEP 6450Sstevel@tonic-gate * dispatches. 6460Sstevel@tonic-gate */ 6470Sstevel@tonic-gate #ifdef DEBUG 6480Sstevel@tonic-gate #define TASKQ_D_RANDOM_DISPATCH_FAILURE(tq, flag) \ 6490Sstevel@tonic-gate taskq_random = (taskq_random * 2416 + 374441) % 1771875;\ 6500Sstevel@tonic-gate if ((flag & TQ_NOSLEEP) && \ 6510Sstevel@tonic-gate taskq_random < 1771875 / taskq_dmtbf) { \ 6520Sstevel@tonic-gate return (NULL); \ 6530Sstevel@tonic-gate } 6540Sstevel@tonic-gate 6550Sstevel@tonic-gate #define TASKQ_S_RANDOM_DISPATCH_FAILURE(tq, flag) \ 6560Sstevel@tonic-gate taskq_random = (taskq_random * 2416 + 374441) % 1771875;\ 6570Sstevel@tonic-gate if ((flag & (TQ_NOSLEEP | TQ_NOALLOC)) && \ 6580Sstevel@tonic-gate (!(tq->tq_flags & TASKQ_PREPOPULATE) || \ 6590Sstevel@tonic-gate (tq->tq_nalloc > tq->tq_minalloc)) && \ 6600Sstevel@tonic-gate (taskq_random < (1771875 / taskq_smtbf))) { \ 6610Sstevel@tonic-gate mutex_exit(&tq->tq_lock); \ 6620Sstevel@tonic-gate return (NULL); \ 6630Sstevel@tonic-gate } 6640Sstevel@tonic-gate #else 6650Sstevel@tonic-gate #define TASKQ_S_RANDOM_DISPATCH_FAILURE(tq, flag) 6660Sstevel@tonic-gate #define TASKQ_D_RANDOM_DISPATCH_FAILURE(tq, flag) 6670Sstevel@tonic-gate #endif 6680Sstevel@tonic-gate 6690Sstevel@tonic-gate #define IS_EMPTY(l) (((l).tqent_prev == (l).tqent_next) && \ 6700Sstevel@tonic-gate ((l).tqent_prev == &(l))) 6710Sstevel@tonic-gate 6720Sstevel@tonic-gate /* 6730Sstevel@tonic-gate * Append `tqe' in the end of the doubly-linked list denoted by l. 6740Sstevel@tonic-gate */ 6750Sstevel@tonic-gate #define TQ_APPEND(l, tqe) { \ 6760Sstevel@tonic-gate tqe->tqent_next = &l; \ 6770Sstevel@tonic-gate tqe->tqent_prev = l.tqent_prev; \ 6780Sstevel@tonic-gate tqe->tqent_next->tqent_prev = tqe; \ 6790Sstevel@tonic-gate tqe->tqent_prev->tqent_next = tqe; \ 6800Sstevel@tonic-gate } 6810Sstevel@tonic-gate 6820Sstevel@tonic-gate /* 6830Sstevel@tonic-gate * Schedule a task specified by func and arg into the task queue entry tqe. 6840Sstevel@tonic-gate */ 6850Sstevel@tonic-gate #define TQ_ENQUEUE(tq, tqe, func, arg) { \ 6860Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tq->tq_lock)); \ 6870Sstevel@tonic-gate TQ_APPEND(tq->tq_task, tqe); \ 6880Sstevel@tonic-gate tqe->tqent_func = (func); \ 6890Sstevel@tonic-gate tqe->tqent_arg = (arg); \ 6900Sstevel@tonic-gate tq->tq_tasks++; \ 6910Sstevel@tonic-gate if (tq->tq_tasks - tq->tq_executed > tq->tq_maxtasks) \ 6920Sstevel@tonic-gate tq->tq_maxtasks = tq->tq_tasks - tq->tq_executed; \ 6930Sstevel@tonic-gate cv_signal(&tq->tq_dispatch_cv); \ 6940Sstevel@tonic-gate DTRACE_PROBE2(taskq__enqueue, taskq_t *, tq, taskq_ent_t *, tqe); \ 6950Sstevel@tonic-gate } 6960Sstevel@tonic-gate 6970Sstevel@tonic-gate /* 6980Sstevel@tonic-gate * Do-nothing task which may be used to prepopulate thread caches. 6990Sstevel@tonic-gate */ 7000Sstevel@tonic-gate /*ARGSUSED*/ 7010Sstevel@tonic-gate void 7020Sstevel@tonic-gate nulltask(void *unused) 7030Sstevel@tonic-gate { 7040Sstevel@tonic-gate } 7050Sstevel@tonic-gate 7060Sstevel@tonic-gate 7070Sstevel@tonic-gate /*ARGSUSED*/ 7080Sstevel@tonic-gate static int 7090Sstevel@tonic-gate taskq_constructor(void *buf, void *cdrarg, int kmflags) 7100Sstevel@tonic-gate { 7110Sstevel@tonic-gate taskq_t *tq = buf; 7120Sstevel@tonic-gate 7130Sstevel@tonic-gate bzero(tq, sizeof (taskq_t)); 7140Sstevel@tonic-gate 7150Sstevel@tonic-gate mutex_init(&tq->tq_lock, NULL, MUTEX_DEFAULT, NULL); 7160Sstevel@tonic-gate rw_init(&tq->tq_threadlock, NULL, RW_DEFAULT, NULL); 7170Sstevel@tonic-gate cv_init(&tq->tq_dispatch_cv, NULL, CV_DEFAULT, NULL); 718*9515SJonathan.Adams@Sun.COM cv_init(&tq->tq_exit_cv, NULL, CV_DEFAULT, NULL); 7190Sstevel@tonic-gate cv_init(&tq->tq_wait_cv, NULL, CV_DEFAULT, NULL); 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate tq->tq_task.tqent_next = &tq->tq_task; 7220Sstevel@tonic-gate tq->tq_task.tqent_prev = &tq->tq_task; 7230Sstevel@tonic-gate 7240Sstevel@tonic-gate return (0); 7250Sstevel@tonic-gate } 7260Sstevel@tonic-gate 7270Sstevel@tonic-gate /*ARGSUSED*/ 7280Sstevel@tonic-gate static void 7290Sstevel@tonic-gate taskq_destructor(void *buf, void *cdrarg) 7300Sstevel@tonic-gate { 7310Sstevel@tonic-gate taskq_t *tq = buf; 7320Sstevel@tonic-gate 733*9515SJonathan.Adams@Sun.COM ASSERT(tq->tq_nthreads == 0); 734*9515SJonathan.Adams@Sun.COM ASSERT(tq->tq_buckets == NULL); 735*9515SJonathan.Adams@Sun.COM ASSERT(tq->tq_tcreates == 0); 736*9515SJonathan.Adams@Sun.COM ASSERT(tq->tq_tdeaths == 0); 737*9515SJonathan.Adams@Sun.COM 7380Sstevel@tonic-gate mutex_destroy(&tq->tq_lock); 7390Sstevel@tonic-gate rw_destroy(&tq->tq_threadlock); 7400Sstevel@tonic-gate cv_destroy(&tq->tq_dispatch_cv); 741*9515SJonathan.Adams@Sun.COM cv_destroy(&tq->tq_exit_cv); 7420Sstevel@tonic-gate cv_destroy(&tq->tq_wait_cv); 7430Sstevel@tonic-gate } 7440Sstevel@tonic-gate 7450Sstevel@tonic-gate /*ARGSUSED*/ 7460Sstevel@tonic-gate static int 7470Sstevel@tonic-gate taskq_ent_constructor(void *buf, void *cdrarg, int kmflags) 7480Sstevel@tonic-gate { 7490Sstevel@tonic-gate taskq_ent_t *tqe = buf; 7500Sstevel@tonic-gate 7510Sstevel@tonic-gate tqe->tqent_thread = NULL; 7520Sstevel@tonic-gate cv_init(&tqe->tqent_cv, NULL, CV_DEFAULT, NULL); 7530Sstevel@tonic-gate 7540Sstevel@tonic-gate return (0); 7550Sstevel@tonic-gate } 7560Sstevel@tonic-gate 7570Sstevel@tonic-gate /*ARGSUSED*/ 7580Sstevel@tonic-gate static void 7590Sstevel@tonic-gate taskq_ent_destructor(void *buf, void *cdrarg) 7600Sstevel@tonic-gate { 7610Sstevel@tonic-gate taskq_ent_t *tqe = buf; 7620Sstevel@tonic-gate 7630Sstevel@tonic-gate ASSERT(tqe->tqent_thread == NULL); 7640Sstevel@tonic-gate cv_destroy(&tqe->tqent_cv); 7650Sstevel@tonic-gate } 7660Sstevel@tonic-gate 7670Sstevel@tonic-gate void 7680Sstevel@tonic-gate taskq_init(void) 7690Sstevel@tonic-gate { 7700Sstevel@tonic-gate taskq_ent_cache = kmem_cache_create("taskq_ent_cache", 7710Sstevel@tonic-gate sizeof (taskq_ent_t), 0, taskq_ent_constructor, 7720Sstevel@tonic-gate taskq_ent_destructor, NULL, NULL, NULL, 0); 7730Sstevel@tonic-gate taskq_cache = kmem_cache_create("taskq_cache", sizeof (taskq_t), 7740Sstevel@tonic-gate 0, taskq_constructor, taskq_destructor, NULL, NULL, NULL, 0); 7750Sstevel@tonic-gate taskq_id_arena = vmem_create("taskq_id_arena", 7760Sstevel@tonic-gate (void *)1, INT32_MAX, 1, NULL, NULL, NULL, 0, 7770Sstevel@tonic-gate VM_SLEEP | VMC_IDENTIFIER); 778*9515SJonathan.Adams@Sun.COM 779*9515SJonathan.Adams@Sun.COM list_create(&taskq_cpupct_list, sizeof (taskq_cpupct_ent_t), 780*9515SJonathan.Adams@Sun.COM offsetof(taskq_cpupct_ent_t, tp_link)); 781*9515SJonathan.Adams@Sun.COM } 782*9515SJonathan.Adams@Sun.COM 783*9515SJonathan.Adams@Sun.COM /*ARGSUSED*/ 784*9515SJonathan.Adams@Sun.COM static int 785*9515SJonathan.Adams@Sun.COM taskq_cpu_setup(cpu_setup_t what, int id, void *arg) 786*9515SJonathan.Adams@Sun.COM { 787*9515SJonathan.Adams@Sun.COM taskq_cpupct_ent_t *tpp; 788*9515SJonathan.Adams@Sun.COM int cpus_online = ncpus_online; 789*9515SJonathan.Adams@Sun.COM 790*9515SJonathan.Adams@Sun.COM /* offlines are called *before* the cpu is offlined. */ 791*9515SJonathan.Adams@Sun.COM if (what == CPU_OFF) 792*9515SJonathan.Adams@Sun.COM cpus_online--; 793*9515SJonathan.Adams@Sun.COM if (cpus_online < 1) 794*9515SJonathan.Adams@Sun.COM cpus_online = 1; 795*9515SJonathan.Adams@Sun.COM 796*9515SJonathan.Adams@Sun.COM mutex_enter(&taskq_cpupct_lock); 797*9515SJonathan.Adams@Sun.COM if (cpus_online == taskq_cpupct_ncpus_online) { 798*9515SJonathan.Adams@Sun.COM mutex_exit(&taskq_cpupct_lock); 799*9515SJonathan.Adams@Sun.COM return (0); 800*9515SJonathan.Adams@Sun.COM } 801*9515SJonathan.Adams@Sun.COM 802*9515SJonathan.Adams@Sun.COM for (tpp = list_head(&taskq_cpupct_list); tpp != NULL; 803*9515SJonathan.Adams@Sun.COM tpp = list_next(&taskq_cpupct_list, tpp)) { 804*9515SJonathan.Adams@Sun.COM taskq_t *tq = tpp->tp_taskq; 805*9515SJonathan.Adams@Sun.COM int newtarget; 806*9515SJonathan.Adams@Sun.COM 807*9515SJonathan.Adams@Sun.COM mutex_enter(&tq->tq_lock); 808*9515SJonathan.Adams@Sun.COM newtarget = 809*9515SJonathan.Adams@Sun.COM TASKQ_THREADS_PCT(cpus_online, tq->tq_threads_ncpus_pct); 810*9515SJonathan.Adams@Sun.COM ASSERT3S(newtarget, <=, tq->tq_nthreads_max); 811*9515SJonathan.Adams@Sun.COM if (newtarget != tq->tq_nthreads_target) { 812*9515SJonathan.Adams@Sun.COM /* The taskq must not be exiting */ 813*9515SJonathan.Adams@Sun.COM ASSERT3S(tq->tq_nthreads_target, !=, 0); 814*9515SJonathan.Adams@Sun.COM tq->tq_flags |= TASKQ_CHANGING; 815*9515SJonathan.Adams@Sun.COM tq->tq_nthreads_target = newtarget; 816*9515SJonathan.Adams@Sun.COM cv_broadcast(&tq->tq_dispatch_cv); 817*9515SJonathan.Adams@Sun.COM cv_broadcast(&tq->tq_exit_cv); 818*9515SJonathan.Adams@Sun.COM } 819*9515SJonathan.Adams@Sun.COM mutex_exit(&tq->tq_lock); 820*9515SJonathan.Adams@Sun.COM } 821*9515SJonathan.Adams@Sun.COM 822*9515SJonathan.Adams@Sun.COM taskq_cpupct_ncpus_online = cpus_online; 823*9515SJonathan.Adams@Sun.COM mutex_exit(&taskq_cpupct_lock); 824*9515SJonathan.Adams@Sun.COM return (0); 825*9515SJonathan.Adams@Sun.COM } 826*9515SJonathan.Adams@Sun.COM 827*9515SJonathan.Adams@Sun.COM void 828*9515SJonathan.Adams@Sun.COM taskq_mp_init(void) 829*9515SJonathan.Adams@Sun.COM { 830*9515SJonathan.Adams@Sun.COM mutex_enter(&cpu_lock); 831*9515SJonathan.Adams@Sun.COM register_cpu_setup_func(taskq_cpu_setup, NULL); 832*9515SJonathan.Adams@Sun.COM (void) taskq_cpu_setup(CPU_ON, 0, NULL); 833*9515SJonathan.Adams@Sun.COM mutex_exit(&cpu_lock); 8340Sstevel@tonic-gate } 8350Sstevel@tonic-gate 8360Sstevel@tonic-gate /* 8370Sstevel@tonic-gate * Create global system dynamic task queue. 8380Sstevel@tonic-gate */ 8390Sstevel@tonic-gate void 8400Sstevel@tonic-gate system_taskq_init(void) 8410Sstevel@tonic-gate { 8420Sstevel@tonic-gate system_taskq = taskq_create_common("system_taskq", 0, 8430Sstevel@tonic-gate system_taskq_size * max_ncpus, minclsyspri, 4, 512, 8440Sstevel@tonic-gate TASKQ_DYNAMIC | TASKQ_PREPOPULATE); 8450Sstevel@tonic-gate } 8460Sstevel@tonic-gate 8470Sstevel@tonic-gate /* 8480Sstevel@tonic-gate * taskq_ent_alloc() 8490Sstevel@tonic-gate * 8500Sstevel@tonic-gate * Allocates a new taskq_ent_t structure either from the free list or from the 8510Sstevel@tonic-gate * cache. Returns NULL if it can't be allocated. 8520Sstevel@tonic-gate * 8530Sstevel@tonic-gate * Assumes: tq->tq_lock is held. 8540Sstevel@tonic-gate */ 8550Sstevel@tonic-gate static taskq_ent_t * 8560Sstevel@tonic-gate taskq_ent_alloc(taskq_t *tq, int flags) 8570Sstevel@tonic-gate { 8580Sstevel@tonic-gate int kmflags = (flags & TQ_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP; 8590Sstevel@tonic-gate 8600Sstevel@tonic-gate taskq_ent_t *tqe; 8610Sstevel@tonic-gate 8620Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tq->tq_lock)); 8630Sstevel@tonic-gate 8640Sstevel@tonic-gate /* 8650Sstevel@tonic-gate * TQ_NOALLOC allocations are allowed to use the freelist, even if 8660Sstevel@tonic-gate * we are below tq_minalloc. 8670Sstevel@tonic-gate */ 8680Sstevel@tonic-gate if ((tqe = tq->tq_freelist) != NULL && 8690Sstevel@tonic-gate ((flags & TQ_NOALLOC) || tq->tq_nalloc >= tq->tq_minalloc)) { 8700Sstevel@tonic-gate tq->tq_freelist = tqe->tqent_next; 8710Sstevel@tonic-gate } else { 8720Sstevel@tonic-gate if (flags & TQ_NOALLOC) 8730Sstevel@tonic-gate return (NULL); 8740Sstevel@tonic-gate 8750Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 8760Sstevel@tonic-gate if (tq->tq_nalloc >= tq->tq_maxalloc) { 8770Sstevel@tonic-gate if (kmflags & KM_NOSLEEP) { 8780Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 8790Sstevel@tonic-gate return (NULL); 8800Sstevel@tonic-gate } 8810Sstevel@tonic-gate /* 8820Sstevel@tonic-gate * We don't want to exceed tq_maxalloc, but we can't 8830Sstevel@tonic-gate * wait for other tasks to complete (and thus free up 8840Sstevel@tonic-gate * task structures) without risking deadlock with 8850Sstevel@tonic-gate * the caller. So, we just delay for one second 8860Sstevel@tonic-gate * to throttle the allocation rate. 8870Sstevel@tonic-gate */ 8880Sstevel@tonic-gate delay(hz); 8890Sstevel@tonic-gate } 8900Sstevel@tonic-gate tqe = kmem_cache_alloc(taskq_ent_cache, kmflags); 8910Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 8920Sstevel@tonic-gate if (tqe != NULL) 8930Sstevel@tonic-gate tq->tq_nalloc++; 8940Sstevel@tonic-gate } 8950Sstevel@tonic-gate return (tqe); 8960Sstevel@tonic-gate } 8970Sstevel@tonic-gate 8980Sstevel@tonic-gate /* 8990Sstevel@tonic-gate * taskq_ent_free() 9000Sstevel@tonic-gate * 9010Sstevel@tonic-gate * Free taskq_ent_t structure by either putting it on the free list or freeing 9020Sstevel@tonic-gate * it to the cache. 9030Sstevel@tonic-gate * 9040Sstevel@tonic-gate * Assumes: tq->tq_lock is held. 9050Sstevel@tonic-gate */ 9060Sstevel@tonic-gate static void 9070Sstevel@tonic-gate taskq_ent_free(taskq_t *tq, taskq_ent_t *tqe) 9080Sstevel@tonic-gate { 9090Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tq->tq_lock)); 9100Sstevel@tonic-gate 9110Sstevel@tonic-gate if (tq->tq_nalloc <= tq->tq_minalloc) { 9120Sstevel@tonic-gate tqe->tqent_next = tq->tq_freelist; 9130Sstevel@tonic-gate tq->tq_freelist = tqe; 9140Sstevel@tonic-gate } else { 9150Sstevel@tonic-gate tq->tq_nalloc--; 9160Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 9170Sstevel@tonic-gate kmem_cache_free(taskq_ent_cache, tqe); 9180Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 9190Sstevel@tonic-gate } 9200Sstevel@tonic-gate } 9210Sstevel@tonic-gate 9220Sstevel@tonic-gate /* 9230Sstevel@tonic-gate * Dispatch a task "func(arg)" to a free entry of bucket b. 9240Sstevel@tonic-gate * 9250Sstevel@tonic-gate * Assumes: no bucket locks is held. 9260Sstevel@tonic-gate * 9270Sstevel@tonic-gate * Returns: a pointer to an entry if dispatch was successful. 9280Sstevel@tonic-gate * NULL if there are no free entries or if the bucket is suspended. 9290Sstevel@tonic-gate */ 9300Sstevel@tonic-gate static taskq_ent_t * 9310Sstevel@tonic-gate taskq_bucket_dispatch(taskq_bucket_t *b, task_func_t func, void *arg) 9320Sstevel@tonic-gate { 9330Sstevel@tonic-gate taskq_ent_t *tqe; 9340Sstevel@tonic-gate 9350Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&b->tqbucket_lock)); 9360Sstevel@tonic-gate ASSERT(func != NULL); 9370Sstevel@tonic-gate 9380Sstevel@tonic-gate mutex_enter(&b->tqbucket_lock); 9390Sstevel@tonic-gate 9400Sstevel@tonic-gate ASSERT(b->tqbucket_nfree != 0 || IS_EMPTY(b->tqbucket_freelist)); 9410Sstevel@tonic-gate ASSERT(b->tqbucket_nfree == 0 || !IS_EMPTY(b->tqbucket_freelist)); 9420Sstevel@tonic-gate 9430Sstevel@tonic-gate /* 9440Sstevel@tonic-gate * Get en entry from the freelist if there is one. 9450Sstevel@tonic-gate * Schedule task into the entry. 9460Sstevel@tonic-gate */ 9470Sstevel@tonic-gate if ((b->tqbucket_nfree != 0) && 9480Sstevel@tonic-gate !(b->tqbucket_flags & TQBUCKET_SUSPEND)) { 9490Sstevel@tonic-gate tqe = b->tqbucket_freelist.tqent_prev; 9500Sstevel@tonic-gate 9510Sstevel@tonic-gate ASSERT(tqe != &b->tqbucket_freelist); 9520Sstevel@tonic-gate ASSERT(tqe->tqent_thread != NULL); 9530Sstevel@tonic-gate 9540Sstevel@tonic-gate tqe->tqent_prev->tqent_next = tqe->tqent_next; 9550Sstevel@tonic-gate tqe->tqent_next->tqent_prev = tqe->tqent_prev; 9560Sstevel@tonic-gate b->tqbucket_nalloc++; 9570Sstevel@tonic-gate b->tqbucket_nfree--; 9580Sstevel@tonic-gate tqe->tqent_func = func; 9590Sstevel@tonic-gate tqe->tqent_arg = arg; 9600Sstevel@tonic-gate TQ_STAT(b, tqs_hits); 9610Sstevel@tonic-gate cv_signal(&tqe->tqent_cv); 9620Sstevel@tonic-gate DTRACE_PROBE2(taskq__d__enqueue, taskq_bucket_t *, b, 9630Sstevel@tonic-gate taskq_ent_t *, tqe); 9640Sstevel@tonic-gate } else { 9650Sstevel@tonic-gate tqe = NULL; 9660Sstevel@tonic-gate TQ_STAT(b, tqs_misses); 9670Sstevel@tonic-gate } 9680Sstevel@tonic-gate mutex_exit(&b->tqbucket_lock); 9690Sstevel@tonic-gate return (tqe); 9700Sstevel@tonic-gate } 9710Sstevel@tonic-gate 9720Sstevel@tonic-gate /* 9730Sstevel@tonic-gate * Dispatch a task. 9740Sstevel@tonic-gate * 9750Sstevel@tonic-gate * Assumes: func != NULL 9760Sstevel@tonic-gate * 9770Sstevel@tonic-gate * Returns: NULL if dispatch failed. 9780Sstevel@tonic-gate * non-NULL if task dispatched successfully. 9790Sstevel@tonic-gate * Actual return value is the pointer to taskq entry that was used to 9800Sstevel@tonic-gate * dispatch a task. This is useful for debugging. 9810Sstevel@tonic-gate */ 9820Sstevel@tonic-gate /* ARGSUSED */ 9830Sstevel@tonic-gate taskqid_t 9840Sstevel@tonic-gate taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags) 9850Sstevel@tonic-gate { 9860Sstevel@tonic-gate taskq_bucket_t *bucket = NULL; /* Which bucket needs extension */ 9870Sstevel@tonic-gate taskq_ent_t *tqe = NULL; 9880Sstevel@tonic-gate taskq_ent_t *tqe1; 9890Sstevel@tonic-gate uint_t bsize; 9900Sstevel@tonic-gate 9910Sstevel@tonic-gate ASSERT(tq != NULL); 9920Sstevel@tonic-gate ASSERT(func != NULL); 9930Sstevel@tonic-gate 9940Sstevel@tonic-gate if (!(tq->tq_flags & TASKQ_DYNAMIC)) { 9950Sstevel@tonic-gate /* 9960Sstevel@tonic-gate * TQ_NOQUEUE flag can't be used with non-dynamic task queues. 9970Sstevel@tonic-gate */ 9980Sstevel@tonic-gate ASSERT(! (flags & TQ_NOQUEUE)); 9990Sstevel@tonic-gate /* 10000Sstevel@tonic-gate * Enqueue the task to the underlying queue. 10010Sstevel@tonic-gate */ 10020Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 10030Sstevel@tonic-gate 10040Sstevel@tonic-gate TASKQ_S_RANDOM_DISPATCH_FAILURE(tq, flags); 10050Sstevel@tonic-gate 10060Sstevel@tonic-gate if ((tqe = taskq_ent_alloc(tq, flags)) == NULL) { 10070Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 10080Sstevel@tonic-gate return (NULL); 10090Sstevel@tonic-gate } 10100Sstevel@tonic-gate TQ_ENQUEUE(tq, tqe, func, arg); 10110Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 10120Sstevel@tonic-gate return ((taskqid_t)tqe); 10130Sstevel@tonic-gate } 10140Sstevel@tonic-gate 10150Sstevel@tonic-gate /* 10160Sstevel@tonic-gate * Dynamic taskq dispatching. 10170Sstevel@tonic-gate */ 10180Sstevel@tonic-gate ASSERT(!(flags & TQ_NOALLOC)); 10190Sstevel@tonic-gate TASKQ_D_RANDOM_DISPATCH_FAILURE(tq, flags); 10200Sstevel@tonic-gate 10210Sstevel@tonic-gate bsize = tq->tq_nbuckets; 10220Sstevel@tonic-gate 10230Sstevel@tonic-gate if (bsize == 1) { 10240Sstevel@tonic-gate /* 10250Sstevel@tonic-gate * In a single-CPU case there is only one bucket, so get 10260Sstevel@tonic-gate * entry directly from there. 10270Sstevel@tonic-gate */ 10280Sstevel@tonic-gate if ((tqe = taskq_bucket_dispatch(tq->tq_buckets, func, arg)) 10290Sstevel@tonic-gate != NULL) 10300Sstevel@tonic-gate return ((taskqid_t)tqe); /* Fastpath */ 10310Sstevel@tonic-gate bucket = tq->tq_buckets; 10320Sstevel@tonic-gate } else { 10330Sstevel@tonic-gate int loopcount; 10340Sstevel@tonic-gate taskq_bucket_t *b; 10350Sstevel@tonic-gate uintptr_t h = ((uintptr_t)CPU + (uintptr_t)arg) >> 3; 10360Sstevel@tonic-gate 10370Sstevel@tonic-gate h = TQ_HASH(h); 10380Sstevel@tonic-gate 10390Sstevel@tonic-gate /* 10400Sstevel@tonic-gate * The 'bucket' points to the original bucket that we hit. If we 10410Sstevel@tonic-gate * can't allocate from it, we search other buckets, but only 10420Sstevel@tonic-gate * extend this one. 10430Sstevel@tonic-gate */ 10440Sstevel@tonic-gate b = &tq->tq_buckets[h & (bsize - 1)]; 10450Sstevel@tonic-gate ASSERT(b->tqbucket_taskq == tq); /* Sanity check */ 10460Sstevel@tonic-gate 10470Sstevel@tonic-gate /* 10480Sstevel@tonic-gate * Do a quick check before grabbing the lock. If the bucket does 10490Sstevel@tonic-gate * not have free entries now, chances are very small that it 10500Sstevel@tonic-gate * will after we take the lock, so we just skip it. 10510Sstevel@tonic-gate */ 10520Sstevel@tonic-gate if (b->tqbucket_nfree != 0) { 10530Sstevel@tonic-gate if ((tqe = taskq_bucket_dispatch(b, func, arg)) != NULL) 10540Sstevel@tonic-gate return ((taskqid_t)tqe); /* Fastpath */ 10550Sstevel@tonic-gate } else { 10560Sstevel@tonic-gate TQ_STAT(b, tqs_misses); 10570Sstevel@tonic-gate } 10580Sstevel@tonic-gate 10590Sstevel@tonic-gate bucket = b; 10600Sstevel@tonic-gate loopcount = MIN(taskq_search_depth, bsize); 10610Sstevel@tonic-gate /* 10620Sstevel@tonic-gate * If bucket dispatch failed, search loopcount number of buckets 10630Sstevel@tonic-gate * before we give up and fail. 10640Sstevel@tonic-gate */ 10650Sstevel@tonic-gate do { 10660Sstevel@tonic-gate b = &tq->tq_buckets[++h & (bsize - 1)]; 10670Sstevel@tonic-gate ASSERT(b->tqbucket_taskq == tq); /* Sanity check */ 10680Sstevel@tonic-gate loopcount--; 10690Sstevel@tonic-gate 10700Sstevel@tonic-gate if (b->tqbucket_nfree != 0) { 10710Sstevel@tonic-gate tqe = taskq_bucket_dispatch(b, func, arg); 10720Sstevel@tonic-gate } else { 10730Sstevel@tonic-gate TQ_STAT(b, tqs_misses); 10740Sstevel@tonic-gate } 10750Sstevel@tonic-gate } while ((tqe == NULL) && (loopcount > 0)); 10760Sstevel@tonic-gate } 10770Sstevel@tonic-gate 10780Sstevel@tonic-gate /* 10790Sstevel@tonic-gate * At this point we either scheduled a task and (tqe != NULL) or failed 10800Sstevel@tonic-gate * (tqe == NULL). Try to recover from fails. 10810Sstevel@tonic-gate */ 10820Sstevel@tonic-gate 10830Sstevel@tonic-gate /* 10840Sstevel@tonic-gate * For KM_SLEEP dispatches, try to extend the bucket and retry dispatch. 10850Sstevel@tonic-gate */ 10860Sstevel@tonic-gate if ((tqe == NULL) && !(flags & TQ_NOSLEEP)) { 10870Sstevel@tonic-gate /* 10880Sstevel@tonic-gate * taskq_bucket_extend() may fail to do anything, but this is 10890Sstevel@tonic-gate * fine - we deal with it later. If the bucket was successfully 10900Sstevel@tonic-gate * extended, there is a good chance that taskq_bucket_dispatch() 10910Sstevel@tonic-gate * will get this new entry, unless someone is racing with us and 10920Sstevel@tonic-gate * stealing the new entry from under our nose. 10930Sstevel@tonic-gate * taskq_bucket_extend() may sleep. 10940Sstevel@tonic-gate */ 10950Sstevel@tonic-gate taskq_bucket_extend(bucket); 10960Sstevel@tonic-gate TQ_STAT(bucket, tqs_disptcreates); 10970Sstevel@tonic-gate if ((tqe = taskq_bucket_dispatch(bucket, func, arg)) != NULL) 10980Sstevel@tonic-gate return ((taskqid_t)tqe); 10990Sstevel@tonic-gate } 11000Sstevel@tonic-gate 11010Sstevel@tonic-gate ASSERT(bucket != NULL); 11020Sstevel@tonic-gate /* 11030Sstevel@tonic-gate * Since there are not enough free entries in the bucket, extend it 11040Sstevel@tonic-gate * in the background using backing queue. 11050Sstevel@tonic-gate */ 11060Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 11070Sstevel@tonic-gate if ((tqe1 = taskq_ent_alloc(tq, TQ_NOSLEEP)) != NULL) { 11080Sstevel@tonic-gate TQ_ENQUEUE(tq, tqe1, taskq_bucket_extend, 11090Sstevel@tonic-gate bucket); 11100Sstevel@tonic-gate } else { 11110Sstevel@tonic-gate TQ_STAT(bucket, tqs_nomem); 11120Sstevel@tonic-gate } 11130Sstevel@tonic-gate 11140Sstevel@tonic-gate /* 11150Sstevel@tonic-gate * Dispatch failed and we can't find an entry to schedule a task. 11160Sstevel@tonic-gate * Revert to the backing queue unless TQ_NOQUEUE was asked. 11170Sstevel@tonic-gate */ 11180Sstevel@tonic-gate if ((tqe == NULL) && !(flags & TQ_NOQUEUE)) { 11190Sstevel@tonic-gate if ((tqe = taskq_ent_alloc(tq, flags)) != NULL) { 11200Sstevel@tonic-gate TQ_ENQUEUE(tq, tqe, func, arg); 11210Sstevel@tonic-gate } else { 11220Sstevel@tonic-gate TQ_STAT(bucket, tqs_nomem); 11230Sstevel@tonic-gate } 11240Sstevel@tonic-gate } 11250Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 11260Sstevel@tonic-gate 11270Sstevel@tonic-gate return ((taskqid_t)tqe); 11280Sstevel@tonic-gate } 11290Sstevel@tonic-gate 11300Sstevel@tonic-gate /* 11310Sstevel@tonic-gate * Wait for all pending tasks to complete. 11320Sstevel@tonic-gate * Calling taskq_wait from a task will cause deadlock. 11330Sstevel@tonic-gate */ 11340Sstevel@tonic-gate void 11350Sstevel@tonic-gate taskq_wait(taskq_t *tq) 11360Sstevel@tonic-gate { 11370Sstevel@tonic-gate ASSERT(tq != curthread->t_taskq); 11380Sstevel@tonic-gate 11390Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 11400Sstevel@tonic-gate while (tq->tq_task.tqent_next != &tq->tq_task || tq->tq_active != 0) 11410Sstevel@tonic-gate cv_wait(&tq->tq_wait_cv, &tq->tq_lock); 11420Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 11430Sstevel@tonic-gate 11440Sstevel@tonic-gate if (tq->tq_flags & TASKQ_DYNAMIC) { 11450Sstevel@tonic-gate taskq_bucket_t *b = tq->tq_buckets; 11460Sstevel@tonic-gate int bid = 0; 11470Sstevel@tonic-gate for (; (b != NULL) && (bid < tq->tq_nbuckets); b++, bid++) { 11480Sstevel@tonic-gate mutex_enter(&b->tqbucket_lock); 11490Sstevel@tonic-gate while (b->tqbucket_nalloc > 0) 11500Sstevel@tonic-gate cv_wait(&b->tqbucket_cv, &b->tqbucket_lock); 11510Sstevel@tonic-gate mutex_exit(&b->tqbucket_lock); 11520Sstevel@tonic-gate } 11530Sstevel@tonic-gate } 11540Sstevel@tonic-gate } 11550Sstevel@tonic-gate 11560Sstevel@tonic-gate /* 11570Sstevel@tonic-gate * Suspend execution of tasks. 11580Sstevel@tonic-gate * 11590Sstevel@tonic-gate * Tasks in the queue part will be suspended immediately upon return from this 11600Sstevel@tonic-gate * function. Pending tasks in the dynamic part will continue to execute, but all 11610Sstevel@tonic-gate * new tasks will be suspended. 11620Sstevel@tonic-gate */ 11630Sstevel@tonic-gate void 11640Sstevel@tonic-gate taskq_suspend(taskq_t *tq) 11650Sstevel@tonic-gate { 11660Sstevel@tonic-gate rw_enter(&tq->tq_threadlock, RW_WRITER); 11670Sstevel@tonic-gate 11680Sstevel@tonic-gate if (tq->tq_flags & TASKQ_DYNAMIC) { 11690Sstevel@tonic-gate taskq_bucket_t *b = tq->tq_buckets; 11700Sstevel@tonic-gate int bid = 0; 11710Sstevel@tonic-gate for (; (b != NULL) && (bid < tq->tq_nbuckets); b++, bid++) { 11720Sstevel@tonic-gate mutex_enter(&b->tqbucket_lock); 11730Sstevel@tonic-gate b->tqbucket_flags |= TQBUCKET_SUSPEND; 11740Sstevel@tonic-gate mutex_exit(&b->tqbucket_lock); 11750Sstevel@tonic-gate } 11760Sstevel@tonic-gate } 11770Sstevel@tonic-gate /* 11780Sstevel@tonic-gate * Mark task queue as being suspended. Needed for taskq_suspended(). 11790Sstevel@tonic-gate */ 11800Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 11810Sstevel@tonic-gate ASSERT(!(tq->tq_flags & TASKQ_SUSPENDED)); 11820Sstevel@tonic-gate tq->tq_flags |= TASKQ_SUSPENDED; 11830Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 11840Sstevel@tonic-gate } 11850Sstevel@tonic-gate 11860Sstevel@tonic-gate /* 11870Sstevel@tonic-gate * returns: 1 if tq is suspended, 0 otherwise. 11880Sstevel@tonic-gate */ 11890Sstevel@tonic-gate int 11900Sstevel@tonic-gate taskq_suspended(taskq_t *tq) 11910Sstevel@tonic-gate { 11920Sstevel@tonic-gate return ((tq->tq_flags & TASKQ_SUSPENDED) != 0); 11930Sstevel@tonic-gate } 11940Sstevel@tonic-gate 11950Sstevel@tonic-gate /* 11960Sstevel@tonic-gate * Resume taskq execution. 11970Sstevel@tonic-gate */ 11980Sstevel@tonic-gate void 11990Sstevel@tonic-gate taskq_resume(taskq_t *tq) 12000Sstevel@tonic-gate { 12010Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&tq->tq_threadlock)); 12020Sstevel@tonic-gate 12030Sstevel@tonic-gate if (tq->tq_flags & TASKQ_DYNAMIC) { 12040Sstevel@tonic-gate taskq_bucket_t *b = tq->tq_buckets; 12050Sstevel@tonic-gate int bid = 0; 12060Sstevel@tonic-gate for (; (b != NULL) && (bid < tq->tq_nbuckets); b++, bid++) { 12070Sstevel@tonic-gate mutex_enter(&b->tqbucket_lock); 12080Sstevel@tonic-gate b->tqbucket_flags &= ~TQBUCKET_SUSPEND; 12090Sstevel@tonic-gate mutex_exit(&b->tqbucket_lock); 12100Sstevel@tonic-gate } 12110Sstevel@tonic-gate } 12120Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 12130Sstevel@tonic-gate ASSERT(tq->tq_flags & TASKQ_SUSPENDED); 12140Sstevel@tonic-gate tq->tq_flags &= ~TASKQ_SUSPENDED; 12150Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 12160Sstevel@tonic-gate 12170Sstevel@tonic-gate rw_exit(&tq->tq_threadlock); 12180Sstevel@tonic-gate } 12190Sstevel@tonic-gate 12200Sstevel@tonic-gate int 12210Sstevel@tonic-gate taskq_member(taskq_t *tq, kthread_t *thread) 12220Sstevel@tonic-gate { 12230Sstevel@tonic-gate return (thread->t_taskq == tq); 12240Sstevel@tonic-gate } 12250Sstevel@tonic-gate 1226*9515SJonathan.Adams@Sun.COM static void 1227*9515SJonathan.Adams@Sun.COM taskq_thread_create(taskq_t *tq) 1228*9515SJonathan.Adams@Sun.COM { 1229*9515SJonathan.Adams@Sun.COM kthread_t *t; 1230*9515SJonathan.Adams@Sun.COM 1231*9515SJonathan.Adams@Sun.COM ASSERT(MUTEX_HELD(&tq->tq_lock)); 1232*9515SJonathan.Adams@Sun.COM ASSERT(!(tq->tq_flags & TASKQ_THREAD_CREATED)); 1233*9515SJonathan.Adams@Sun.COM 1234*9515SJonathan.Adams@Sun.COM tq->tq_flags |= TASKQ_THREAD_CREATED; 1235*9515SJonathan.Adams@Sun.COM tq->tq_active++; 1236*9515SJonathan.Adams@Sun.COM t = thread_create(NULL, 0, taskq_thread, tq, 0, &p0, TS_RUN, 1237*9515SJonathan.Adams@Sun.COM tq->tq_pri); 1238*9515SJonathan.Adams@Sun.COM t->t_taskq = tq; 1239*9515SJonathan.Adams@Sun.COM } 1240*9515SJonathan.Adams@Sun.COM 1241*9515SJonathan.Adams@Sun.COM static void 1242*9515SJonathan.Adams@Sun.COM taskq_thread_wait(taskq_t *tq, kcondvar_t *cv, callb_cpr_t *cprinfo) 1243*9515SJonathan.Adams@Sun.COM { 1244*9515SJonathan.Adams@Sun.COM if (tq->tq_flags & TASKQ_CPR_SAFE) { 1245*9515SJonathan.Adams@Sun.COM cv_wait(cv, &tq->tq_lock); 1246*9515SJonathan.Adams@Sun.COM } else { 1247*9515SJonathan.Adams@Sun.COM CALLB_CPR_SAFE_BEGIN(cprinfo); 1248*9515SJonathan.Adams@Sun.COM cv_wait(cv, &tq->tq_lock); 1249*9515SJonathan.Adams@Sun.COM CALLB_CPR_SAFE_END(cprinfo, &tq->tq_lock); 1250*9515SJonathan.Adams@Sun.COM } 1251*9515SJonathan.Adams@Sun.COM } 1252*9515SJonathan.Adams@Sun.COM 12530Sstevel@tonic-gate /* 12540Sstevel@tonic-gate * Worker thread for processing task queue. 12550Sstevel@tonic-gate */ 12560Sstevel@tonic-gate static void 12570Sstevel@tonic-gate taskq_thread(void *arg) 12580Sstevel@tonic-gate { 1259*9515SJonathan.Adams@Sun.COM int thread_id; 1260*9515SJonathan.Adams@Sun.COM 12610Sstevel@tonic-gate taskq_t *tq = arg; 12620Sstevel@tonic-gate taskq_ent_t *tqe; 12630Sstevel@tonic-gate callb_cpr_t cprinfo; 12640Sstevel@tonic-gate hrtime_t start, end; 12650Sstevel@tonic-gate 12660Sstevel@tonic-gate if (tq->tq_flags & TASKQ_CPR_SAFE) { 12670Sstevel@tonic-gate CALLB_CPR_INIT_SAFE(curthread, tq->tq_name); 12680Sstevel@tonic-gate } else { 12690Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, &tq->tq_lock, callb_generic_cpr, 12700Sstevel@tonic-gate tq->tq_name); 12710Sstevel@tonic-gate } 12720Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 1273*9515SJonathan.Adams@Sun.COM thread_id = ++tq->tq_nthreads; 1274*9515SJonathan.Adams@Sun.COM ASSERT(tq->tq_flags & TASKQ_THREAD_CREATED); 1275*9515SJonathan.Adams@Sun.COM tq->tq_flags &= ~TASKQ_THREAD_CREATED; 1276*9515SJonathan.Adams@Sun.COM 1277*9515SJonathan.Adams@Sun.COM VERIFY3S(thread_id, <=, tq->tq_nthreads_max); 1278*9515SJonathan.Adams@Sun.COM 1279*9515SJonathan.Adams@Sun.COM if (tq->tq_nthreads_max == 1) 1280*9515SJonathan.Adams@Sun.COM tq->tq_thread = curthread; 1281*9515SJonathan.Adams@Sun.COM else 1282*9515SJonathan.Adams@Sun.COM tq->tq_threadlist[thread_id - 1] = curthread; 1283*9515SJonathan.Adams@Sun.COM 1284*9515SJonathan.Adams@Sun.COM for (;;) { 1285*9515SJonathan.Adams@Sun.COM if (tq->tq_flags & TASKQ_CHANGING) { 1286*9515SJonathan.Adams@Sun.COM /* we're done; clear the CHANGING flag */ 1287*9515SJonathan.Adams@Sun.COM if (tq->tq_nthreads == tq->tq_nthreads_target) { 1288*9515SJonathan.Adams@Sun.COM tq->tq_flags &= ~TASKQ_CHANGING; 1289*9515SJonathan.Adams@Sun.COM continue; 1290*9515SJonathan.Adams@Sun.COM } 1291*9515SJonathan.Adams@Sun.COM /* We're low on threads and none have been created */ 1292*9515SJonathan.Adams@Sun.COM if (tq->tq_nthreads < tq->tq_nthreads_target && 1293*9515SJonathan.Adams@Sun.COM !(tq->tq_flags & TASKQ_THREAD_CREATED)) { 1294*9515SJonathan.Adams@Sun.COM taskq_thread_create(tq); 1295*9515SJonathan.Adams@Sun.COM continue; 1296*9515SJonathan.Adams@Sun.COM } 1297*9515SJonathan.Adams@Sun.COM /* We're no longer needed */ 1298*9515SJonathan.Adams@Sun.COM if (thread_id > tq->tq_nthreads_target) { 1299*9515SJonathan.Adams@Sun.COM /* 1300*9515SJonathan.Adams@Sun.COM * To preserve the one-to-one mapping between 1301*9515SJonathan.Adams@Sun.COM * thread_id and thread, we must exit from 1302*9515SJonathan.Adams@Sun.COM * highest thread ID to least. 1303*9515SJonathan.Adams@Sun.COM * 1304*9515SJonathan.Adams@Sun.COM * However, if everyone is exiting, the order 1305*9515SJonathan.Adams@Sun.COM * doesn't matter, so just exit immediately. 1306*9515SJonathan.Adams@Sun.COM * (this is safe, since you must wait for 1307*9515SJonathan.Adams@Sun.COM * nthreads to reach 0 after setting 1308*9515SJonathan.Adams@Sun.COM * tq_nthreads_target to 0) 1309*9515SJonathan.Adams@Sun.COM */ 1310*9515SJonathan.Adams@Sun.COM if (thread_id == tq->tq_nthreads || 1311*9515SJonathan.Adams@Sun.COM tq->tq_nthreads_target == 0) 1312*9515SJonathan.Adams@Sun.COM break; 1313*9515SJonathan.Adams@Sun.COM 1314*9515SJonathan.Adams@Sun.COM /* Wait for higher thread_ids to exit */ 1315*9515SJonathan.Adams@Sun.COM taskq_thread_wait(tq, &tq->tq_exit_cv, 1316*9515SJonathan.Adams@Sun.COM &cprinfo); 1317*9515SJonathan.Adams@Sun.COM continue; 1318*9515SJonathan.Adams@Sun.COM } 1319*9515SJonathan.Adams@Sun.COM } 13200Sstevel@tonic-gate if ((tqe = tq->tq_task.tqent_next) == &tq->tq_task) { 13210Sstevel@tonic-gate if (--tq->tq_active == 0) 13220Sstevel@tonic-gate cv_broadcast(&tq->tq_wait_cv); 1323*9515SJonathan.Adams@Sun.COM taskq_thread_wait(tq, &tq->tq_dispatch_cv, &cprinfo); 13240Sstevel@tonic-gate tq->tq_active++; 13250Sstevel@tonic-gate continue; 13260Sstevel@tonic-gate } 13270Sstevel@tonic-gate tqe->tqent_prev->tqent_next = tqe->tqent_next; 13280Sstevel@tonic-gate tqe->tqent_next->tqent_prev = tqe->tqent_prev; 13290Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 13300Sstevel@tonic-gate 13310Sstevel@tonic-gate rw_enter(&tq->tq_threadlock, RW_READER); 13320Sstevel@tonic-gate start = gethrtime(); 13330Sstevel@tonic-gate DTRACE_PROBE2(taskq__exec__start, taskq_t *, tq, 13340Sstevel@tonic-gate taskq_ent_t *, tqe); 13350Sstevel@tonic-gate tqe->tqent_func(tqe->tqent_arg); 13360Sstevel@tonic-gate DTRACE_PROBE2(taskq__exec__end, taskq_t *, tq, 13370Sstevel@tonic-gate taskq_ent_t *, tqe); 13380Sstevel@tonic-gate end = gethrtime(); 13390Sstevel@tonic-gate rw_exit(&tq->tq_threadlock); 13400Sstevel@tonic-gate 13410Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 13420Sstevel@tonic-gate tq->tq_totaltime += end - start; 13430Sstevel@tonic-gate tq->tq_executed++; 13440Sstevel@tonic-gate 13450Sstevel@tonic-gate taskq_ent_free(tq, tqe); 13460Sstevel@tonic-gate } 1347*9515SJonathan.Adams@Sun.COM 1348*9515SJonathan.Adams@Sun.COM if (tq->tq_nthreads_max == 1) 1349*9515SJonathan.Adams@Sun.COM tq->tq_thread = NULL; 1350*9515SJonathan.Adams@Sun.COM else 1351*9515SJonathan.Adams@Sun.COM tq->tq_threadlist[thread_id - 1] = NULL; 1352*9515SJonathan.Adams@Sun.COM 1353*9515SJonathan.Adams@Sun.COM ASSERT(tq->tq_nthreads > 0); 1354*9515SJonathan.Adams@Sun.COM if (--tq->tq_nthreads == 0) 1355*9515SJonathan.Adams@Sun.COM cv_broadcast(&tq->tq_wait_cv); 1356*9515SJonathan.Adams@Sun.COM 1357*9515SJonathan.Adams@Sun.COM /* let the other threads which need to exit know we're done */ 1358*9515SJonathan.Adams@Sun.COM cv_broadcast(&tq->tq_exit_cv); 1359*9515SJonathan.Adams@Sun.COM 1360*9515SJonathan.Adams@Sun.COM /* We're exiting, and therefore no longer active */ 1361*9515SJonathan.Adams@Sun.COM tq->tq_active--; 1362*9515SJonathan.Adams@Sun.COM 13630Sstevel@tonic-gate ASSERT(!(tq->tq_flags & TASKQ_CPR_SAFE)); 13640Sstevel@tonic-gate CALLB_CPR_EXIT(&cprinfo); 13650Sstevel@tonic-gate thread_exit(); 13660Sstevel@tonic-gate } 13670Sstevel@tonic-gate 13680Sstevel@tonic-gate /* 13690Sstevel@tonic-gate * Worker per-entry thread for dynamic dispatches. 13700Sstevel@tonic-gate */ 13710Sstevel@tonic-gate static void 13720Sstevel@tonic-gate taskq_d_thread(taskq_ent_t *tqe) 13730Sstevel@tonic-gate { 13740Sstevel@tonic-gate taskq_bucket_t *bucket = tqe->tqent_bucket; 13750Sstevel@tonic-gate taskq_t *tq = bucket->tqbucket_taskq; 13760Sstevel@tonic-gate kmutex_t *lock = &bucket->tqbucket_lock; 13770Sstevel@tonic-gate kcondvar_t *cv = &tqe->tqent_cv; 13780Sstevel@tonic-gate callb_cpr_t cprinfo; 13790Sstevel@tonic-gate clock_t w; 13800Sstevel@tonic-gate 13810Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, lock, callb_generic_cpr, tq->tq_name); 13820Sstevel@tonic-gate 13830Sstevel@tonic-gate mutex_enter(lock); 13840Sstevel@tonic-gate 13850Sstevel@tonic-gate for (;;) { 13860Sstevel@tonic-gate /* 13870Sstevel@tonic-gate * If a task is scheduled (func != NULL), execute it, otherwise 13880Sstevel@tonic-gate * sleep, waiting for a job. 13890Sstevel@tonic-gate */ 13900Sstevel@tonic-gate if (tqe->tqent_func != NULL) { 13910Sstevel@tonic-gate hrtime_t start; 13920Sstevel@tonic-gate hrtime_t end; 13930Sstevel@tonic-gate 13940Sstevel@tonic-gate ASSERT(bucket->tqbucket_nalloc > 0); 13950Sstevel@tonic-gate 13960Sstevel@tonic-gate /* 13970Sstevel@tonic-gate * It is possible to free the entry right away before 13980Sstevel@tonic-gate * actually executing the task so that subsequent 13990Sstevel@tonic-gate * dispatches may immediately reuse it. But this, 14000Sstevel@tonic-gate * effectively, creates a two-length queue in the entry 14010Sstevel@tonic-gate * and may lead to a deadlock if the execution of the 14020Sstevel@tonic-gate * current task depends on the execution of the next 14030Sstevel@tonic-gate * scheduled task. So, we keep the entry busy until the 14040Sstevel@tonic-gate * task is processed. 14050Sstevel@tonic-gate */ 14060Sstevel@tonic-gate 14070Sstevel@tonic-gate mutex_exit(lock); 14080Sstevel@tonic-gate start = gethrtime(); 14090Sstevel@tonic-gate DTRACE_PROBE3(taskq__d__exec__start, taskq_t *, tq, 14100Sstevel@tonic-gate taskq_bucket_t *, bucket, taskq_ent_t *, tqe); 14110Sstevel@tonic-gate tqe->tqent_func(tqe->tqent_arg); 14120Sstevel@tonic-gate DTRACE_PROBE3(taskq__d__exec__end, taskq_t *, tq, 14130Sstevel@tonic-gate taskq_bucket_t *, bucket, taskq_ent_t *, tqe); 14140Sstevel@tonic-gate end = gethrtime(); 14150Sstevel@tonic-gate mutex_enter(lock); 14160Sstevel@tonic-gate bucket->tqbucket_totaltime += end - start; 14170Sstevel@tonic-gate 14180Sstevel@tonic-gate /* 14190Sstevel@tonic-gate * Return the entry to the bucket free list. 14200Sstevel@tonic-gate */ 14210Sstevel@tonic-gate tqe->tqent_func = NULL; 14220Sstevel@tonic-gate TQ_APPEND(bucket->tqbucket_freelist, tqe); 14230Sstevel@tonic-gate bucket->tqbucket_nalloc--; 14240Sstevel@tonic-gate bucket->tqbucket_nfree++; 14250Sstevel@tonic-gate ASSERT(!IS_EMPTY(bucket->tqbucket_freelist)); 14260Sstevel@tonic-gate /* 14270Sstevel@tonic-gate * taskq_wait() waits for nalloc to drop to zero on 14280Sstevel@tonic-gate * tqbucket_cv. 14290Sstevel@tonic-gate */ 14300Sstevel@tonic-gate cv_signal(&bucket->tqbucket_cv); 14310Sstevel@tonic-gate } 14320Sstevel@tonic-gate 14330Sstevel@tonic-gate /* 14340Sstevel@tonic-gate * At this point the entry must be in the bucket free list - 14350Sstevel@tonic-gate * either because it was there initially or because it just 14360Sstevel@tonic-gate * finished executing a task and put itself on the free list. 14370Sstevel@tonic-gate */ 14380Sstevel@tonic-gate ASSERT(bucket->tqbucket_nfree > 0); 14390Sstevel@tonic-gate /* 14400Sstevel@tonic-gate * Go to sleep unless we are closing. 14410Sstevel@tonic-gate * If a thread is sleeping too long, it dies. 14420Sstevel@tonic-gate */ 14430Sstevel@tonic-gate if (! (bucket->tqbucket_flags & TQBUCKET_CLOSE)) { 14440Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo); 14450Sstevel@tonic-gate w = cv_timedwait(cv, lock, lbolt + 14460Sstevel@tonic-gate taskq_thread_timeout * hz); 14470Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cprinfo, lock); 14480Sstevel@tonic-gate } 14490Sstevel@tonic-gate 14500Sstevel@tonic-gate /* 14510Sstevel@tonic-gate * At this point we may be in two different states: 14520Sstevel@tonic-gate * 14530Sstevel@tonic-gate * (1) tqent_func is set which means that a new task is 14540Sstevel@tonic-gate * dispatched and we need to execute it. 14550Sstevel@tonic-gate * 14560Sstevel@tonic-gate * (2) Thread is sleeping for too long or we are closing. In 14570Sstevel@tonic-gate * both cases destroy the thread and the entry. 14580Sstevel@tonic-gate */ 14590Sstevel@tonic-gate 14600Sstevel@tonic-gate /* If func is NULL we should be on the freelist. */ 14610Sstevel@tonic-gate ASSERT((tqe->tqent_func != NULL) || 14620Sstevel@tonic-gate (bucket->tqbucket_nfree > 0)); 14630Sstevel@tonic-gate /* If func is non-NULL we should be allocated */ 14640Sstevel@tonic-gate ASSERT((tqe->tqent_func == NULL) || 14650Sstevel@tonic-gate (bucket->tqbucket_nalloc > 0)); 14660Sstevel@tonic-gate 14670Sstevel@tonic-gate /* Check freelist consistency */ 14680Sstevel@tonic-gate ASSERT((bucket->tqbucket_nfree > 0) || 14690Sstevel@tonic-gate IS_EMPTY(bucket->tqbucket_freelist)); 14700Sstevel@tonic-gate ASSERT((bucket->tqbucket_nfree == 0) || 14710Sstevel@tonic-gate !IS_EMPTY(bucket->tqbucket_freelist)); 14720Sstevel@tonic-gate 14730Sstevel@tonic-gate if ((tqe->tqent_func == NULL) && 14740Sstevel@tonic-gate ((w == -1) || (bucket->tqbucket_flags & TQBUCKET_CLOSE))) { 14750Sstevel@tonic-gate /* 14760Sstevel@tonic-gate * This thread is sleeping for too long or we are 14770Sstevel@tonic-gate * closing - time to die. 14780Sstevel@tonic-gate * Thread creation/destruction happens rarely, 14790Sstevel@tonic-gate * so grabbing the lock is not a big performance issue. 14800Sstevel@tonic-gate * The bucket lock is dropped by CALLB_CPR_EXIT(). 14810Sstevel@tonic-gate */ 14820Sstevel@tonic-gate 14830Sstevel@tonic-gate /* Remove the entry from the free list. */ 14840Sstevel@tonic-gate tqe->tqent_prev->tqent_next = tqe->tqent_next; 14850Sstevel@tonic-gate tqe->tqent_next->tqent_prev = tqe->tqent_prev; 14860Sstevel@tonic-gate ASSERT(bucket->tqbucket_nfree > 0); 14870Sstevel@tonic-gate bucket->tqbucket_nfree--; 14880Sstevel@tonic-gate 14890Sstevel@tonic-gate TQ_STAT(bucket, tqs_tdeaths); 14900Sstevel@tonic-gate cv_signal(&bucket->tqbucket_cv); 14910Sstevel@tonic-gate tqe->tqent_thread = NULL; 14920Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 14930Sstevel@tonic-gate tq->tq_tdeaths++; 14940Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 14950Sstevel@tonic-gate CALLB_CPR_EXIT(&cprinfo); 14960Sstevel@tonic-gate kmem_cache_free(taskq_ent_cache, tqe); 14970Sstevel@tonic-gate thread_exit(); 14980Sstevel@tonic-gate } 14990Sstevel@tonic-gate } 15000Sstevel@tonic-gate } 15010Sstevel@tonic-gate 15020Sstevel@tonic-gate 15030Sstevel@tonic-gate /* 15040Sstevel@tonic-gate * Taskq creation. May sleep for memory. 15050Sstevel@tonic-gate * Always use automatically generated instances to avoid kstat name space 15060Sstevel@tonic-gate * collisions. 15070Sstevel@tonic-gate */ 15080Sstevel@tonic-gate 15090Sstevel@tonic-gate taskq_t * 15100Sstevel@tonic-gate taskq_create(const char *name, int nthreads, pri_t pri, int minalloc, 15110Sstevel@tonic-gate int maxalloc, uint_t flags) 15120Sstevel@tonic-gate { 15130Sstevel@tonic-gate return taskq_create_common(name, 0, nthreads, pri, minalloc, 15140Sstevel@tonic-gate maxalloc, flags | TASKQ_NOINSTANCE); 15150Sstevel@tonic-gate } 15160Sstevel@tonic-gate 15170Sstevel@tonic-gate /* 15180Sstevel@tonic-gate * Create an instance of task queue. It is legal to create task queues with the 15190Sstevel@tonic-gate * same name and different instances. 15200Sstevel@tonic-gate * 15210Sstevel@tonic-gate * taskq_create_instance is used by ddi_taskq_create() where it gets the 15220Sstevel@tonic-gate * instance from ddi_get_instance(). In some cases the instance is not 15230Sstevel@tonic-gate * initialized and is set to -1. This case is handled as if no instance was 15240Sstevel@tonic-gate * passed at all. 15250Sstevel@tonic-gate */ 15260Sstevel@tonic-gate taskq_t * 15270Sstevel@tonic-gate taskq_create_instance(const char *name, int instance, int nthreads, pri_t pri, 15280Sstevel@tonic-gate int minalloc, int maxalloc, uint_t flags) 15290Sstevel@tonic-gate { 15300Sstevel@tonic-gate ASSERT((instance >= 0) || (instance == -1)); 15310Sstevel@tonic-gate 15320Sstevel@tonic-gate if (instance < 0) { 15330Sstevel@tonic-gate flags |= TASKQ_NOINSTANCE; 15340Sstevel@tonic-gate } 15350Sstevel@tonic-gate 15360Sstevel@tonic-gate return (taskq_create_common(name, instance, nthreads, 15370Sstevel@tonic-gate pri, minalloc, maxalloc, flags)); 15380Sstevel@tonic-gate } 15390Sstevel@tonic-gate 15400Sstevel@tonic-gate static taskq_t * 15410Sstevel@tonic-gate taskq_create_common(const char *name, int instance, int nthreads, pri_t pri, 15420Sstevel@tonic-gate int minalloc, int maxalloc, uint_t flags) 15430Sstevel@tonic-gate { 15440Sstevel@tonic-gate taskq_t *tq = kmem_cache_alloc(taskq_cache, KM_SLEEP); 15450Sstevel@tonic-gate uint_t ncpus = ((boot_max_ncpus == -1) ? max_ncpus : boot_max_ncpus); 15460Sstevel@tonic-gate uint_t bsize; /* # of buckets - always power of 2 */ 1547*9515SJonathan.Adams@Sun.COM int max_nthreads; 15480Sstevel@tonic-gate 15490Sstevel@tonic-gate /* 1550*9515SJonathan.Adams@Sun.COM * TASKQ_DYNAMIC is incompatible with TASKQ_CPR_SAFE and 1551*9515SJonathan.Adams@Sun.COM * TASKQ_THREADS_CPU_PCT. 15520Sstevel@tonic-gate */ 1553*9515SJonathan.Adams@Sun.COM ASSERT(!(flags & TASKQ_DYNAMIC) || 1554*9515SJonathan.Adams@Sun.COM !(flags & (TASKQ_CPR_SAFE | TASKQ_THREADS_CPU_PCT))); 1555*9515SJonathan.Adams@Sun.COM /* TASKQ_CPR_SAFE is incompatible with TASKQ_THREADS_CPU_PCT */ 15560Sstevel@tonic-gate 1557*9515SJonathan.Adams@Sun.COM ASSERT(!(flags & TASKQ_CPR_SAFE) || !(flags & TASKQ_THREADS_CPU_PCT)); 15580Sstevel@tonic-gate 15590Sstevel@tonic-gate bsize = 1 << (highbit(ncpus) - 1); 15600Sstevel@tonic-gate ASSERT(bsize >= 1); 15610Sstevel@tonic-gate bsize = MIN(bsize, taskq_maxbuckets); 15620Sstevel@tonic-gate 1563*9515SJonathan.Adams@Sun.COM if (flags & TASKQ_DYNAMIC) { 1564*9515SJonathan.Adams@Sun.COM ASSERT3S(nthreads, >=, 1); 1565*9515SJonathan.Adams@Sun.COM tq->tq_maxsize = nthreads; 1566*9515SJonathan.Adams@Sun.COM 1567*9515SJonathan.Adams@Sun.COM /* For dynamic task queues use just one backup thread */ 1568*9515SJonathan.Adams@Sun.COM nthreads = max_nthreads = 1; 1569*9515SJonathan.Adams@Sun.COM 1570*9515SJonathan.Adams@Sun.COM } else if (!(flags & TASKQ_THREADS_CPU_PCT)) { 1571*9515SJonathan.Adams@Sun.COM ASSERT3S(nthreads, >=, 1); 1572*9515SJonathan.Adams@Sun.COM max_nthreads = nthreads; 1573*9515SJonathan.Adams@Sun.COM } else { 1574*9515SJonathan.Adams@Sun.COM uint_t pct; 1575*9515SJonathan.Adams@Sun.COM ASSERT3S(nthreads, >=, 0); 1576*9515SJonathan.Adams@Sun.COM pct = nthreads; 15770Sstevel@tonic-gate 1578*9515SJonathan.Adams@Sun.COM if (pct > taskq_cpupct_max_percent) 1579*9515SJonathan.Adams@Sun.COM pct = taskq_cpupct_max_percent; 1580*9515SJonathan.Adams@Sun.COM 1581*9515SJonathan.Adams@Sun.COM tq->tq_threads_ncpus_pct = pct; 1582*9515SJonathan.Adams@Sun.COM nthreads = TASKQ_THREADS_PCT(ncpus_online, pct); 1583*9515SJonathan.Adams@Sun.COM max_nthreads = TASKQ_THREADS_PCT(max_ncpus, pct); 1584*9515SJonathan.Adams@Sun.COM } 15850Sstevel@tonic-gate 1586*9515SJonathan.Adams@Sun.COM if (max_nthreads < taskq_minimum_nthreads_max) 1587*9515SJonathan.Adams@Sun.COM max_nthreads = taskq_minimum_nthreads_max; 1588*9515SJonathan.Adams@Sun.COM 1589*9515SJonathan.Adams@Sun.COM /* 1590*9515SJonathan.Adams@Sun.COM * Make sure the name is 0-terminated, and conforms to the rules for 1591*9515SJonathan.Adams@Sun.COM * C indentifiers 1592*9515SJonathan.Adams@Sun.COM */ 15930Sstevel@tonic-gate (void) strncpy(tq->tq_name, name, TASKQ_NAMELEN + 1); 1594*9515SJonathan.Adams@Sun.COM strident_canon(tq->tq_name, TASKQ_NAMELEN + 1); 15950Sstevel@tonic-gate 1596*9515SJonathan.Adams@Sun.COM tq->tq_flags = flags | TASKQ_CHANGING; 1597*9515SJonathan.Adams@Sun.COM tq->tq_active = 0; 15980Sstevel@tonic-gate tq->tq_instance = instance; 1599*9515SJonathan.Adams@Sun.COM tq->tq_nthreads_target = nthreads; 1600*9515SJonathan.Adams@Sun.COM tq->tq_nthreads_max = max_nthreads; 16010Sstevel@tonic-gate tq->tq_minalloc = minalloc; 16020Sstevel@tonic-gate tq->tq_maxalloc = maxalloc; 16030Sstevel@tonic-gate tq->tq_nbuckets = bsize; 16040Sstevel@tonic-gate tq->tq_pri = pri; 16050Sstevel@tonic-gate 1606*9515SJonathan.Adams@Sun.COM if (max_nthreads > 1) 1607*9515SJonathan.Adams@Sun.COM tq->tq_threadlist = kmem_alloc( 1608*9515SJonathan.Adams@Sun.COM sizeof (kthread_t *) * max_nthreads, KM_SLEEP); 1609*9515SJonathan.Adams@Sun.COM 1610*9515SJonathan.Adams@Sun.COM /* Add the taskq to the list of CPU_PCT taskqs */ 1611*9515SJonathan.Adams@Sun.COM if (flags & TASKQ_THREADS_CPU_PCT) { 1612*9515SJonathan.Adams@Sun.COM taskq_cpupct_ent_t *tpp = kmem_zalloc(sizeof (*tpp), KM_SLEEP); 1613*9515SJonathan.Adams@Sun.COM 1614*9515SJonathan.Adams@Sun.COM list_link_init(&tpp->tp_link); 1615*9515SJonathan.Adams@Sun.COM tpp->tp_taskq = tq; 1616*9515SJonathan.Adams@Sun.COM 1617*9515SJonathan.Adams@Sun.COM mutex_enter(&taskq_cpupct_lock); 1618*9515SJonathan.Adams@Sun.COM list_insert_tail(&taskq_cpupct_list, tpp); 1619*9515SJonathan.Adams@Sun.COM /* reset our target, to avoid race conditions */ 1620*9515SJonathan.Adams@Sun.COM tq->tq_nthreads_target = TASKQ_THREADS_PCT(ncpus_online, 1621*9515SJonathan.Adams@Sun.COM tq->tq_threads_ncpus_pct); 1622*9515SJonathan.Adams@Sun.COM mutex_exit(&taskq_cpupct_lock); 16230Sstevel@tonic-gate } 16240Sstevel@tonic-gate 1625*9515SJonathan.Adams@Sun.COM mutex_enter(&tq->tq_lock); 1626*9515SJonathan.Adams@Sun.COM if (flags & TASKQ_PREPOPULATE) { 1627*9515SJonathan.Adams@Sun.COM while (minalloc-- > 0) 1628*9515SJonathan.Adams@Sun.COM taskq_ent_free(tq, taskq_ent_alloc(tq, TQ_SLEEP)); 1629*9515SJonathan.Adams@Sun.COM } 16300Sstevel@tonic-gate 1631*9515SJonathan.Adams@Sun.COM /* create the first thread; if more are needed, it'll create them */ 1632*9515SJonathan.Adams@Sun.COM taskq_thread_create(tq); 1633*9515SJonathan.Adams@Sun.COM mutex_exit(&tq->tq_lock); 16340Sstevel@tonic-gate 16350Sstevel@tonic-gate if (flags & TASKQ_DYNAMIC) { 16360Sstevel@tonic-gate taskq_bucket_t *bucket = kmem_zalloc(sizeof (taskq_bucket_t) * 16370Sstevel@tonic-gate bsize, KM_SLEEP); 16380Sstevel@tonic-gate int b_id; 16390Sstevel@tonic-gate 16400Sstevel@tonic-gate tq->tq_buckets = bucket; 16410Sstevel@tonic-gate 16420Sstevel@tonic-gate /* Initialize each bucket */ 16430Sstevel@tonic-gate for (b_id = 0; b_id < bsize; b_id++, bucket++) { 16440Sstevel@tonic-gate mutex_init(&bucket->tqbucket_lock, NULL, MUTEX_DEFAULT, 16450Sstevel@tonic-gate NULL); 16460Sstevel@tonic-gate cv_init(&bucket->tqbucket_cv, NULL, CV_DEFAULT, NULL); 16470Sstevel@tonic-gate bucket->tqbucket_taskq = tq; 16480Sstevel@tonic-gate bucket->tqbucket_freelist.tqent_next = 16490Sstevel@tonic-gate bucket->tqbucket_freelist.tqent_prev = 16500Sstevel@tonic-gate &bucket->tqbucket_freelist; 16510Sstevel@tonic-gate if (flags & TASKQ_PREPOPULATE) 16520Sstevel@tonic-gate taskq_bucket_extend(bucket); 16530Sstevel@tonic-gate } 16540Sstevel@tonic-gate } 16550Sstevel@tonic-gate 16560Sstevel@tonic-gate /* 16570Sstevel@tonic-gate * Install kstats. 16580Sstevel@tonic-gate * We have two cases: 16590Sstevel@tonic-gate * 1) Instance is provided to taskq_create_instance(). In this case it 16600Sstevel@tonic-gate * should be >= 0 and we use it. 16610Sstevel@tonic-gate * 16620Sstevel@tonic-gate * 2) Instance is not provided and is automatically generated 16630Sstevel@tonic-gate */ 16640Sstevel@tonic-gate if (flags & TASKQ_NOINSTANCE) { 16650Sstevel@tonic-gate instance = tq->tq_instance = 16660Sstevel@tonic-gate (int)(uintptr_t)vmem_alloc(taskq_id_arena, 1, VM_SLEEP); 16670Sstevel@tonic-gate } 16680Sstevel@tonic-gate 16690Sstevel@tonic-gate if (flags & TASKQ_DYNAMIC) { 16700Sstevel@tonic-gate if ((tq->tq_kstat = kstat_create("unix", instance, 1671*9515SJonathan.Adams@Sun.COM tq->tq_name, "taskq_d", KSTAT_TYPE_NAMED, 1672*9515SJonathan.Adams@Sun.COM sizeof (taskq_d_kstat) / sizeof (kstat_named_t), 1673*9515SJonathan.Adams@Sun.COM KSTAT_FLAG_VIRTUAL)) != NULL) { 16740Sstevel@tonic-gate tq->tq_kstat->ks_lock = &taskq_d_kstat_lock; 16750Sstevel@tonic-gate tq->tq_kstat->ks_data = &taskq_d_kstat; 16760Sstevel@tonic-gate tq->tq_kstat->ks_update = taskq_d_kstat_update; 16770Sstevel@tonic-gate tq->tq_kstat->ks_private = tq; 16780Sstevel@tonic-gate kstat_install(tq->tq_kstat); 16790Sstevel@tonic-gate } 16800Sstevel@tonic-gate } else { 16810Sstevel@tonic-gate if ((tq->tq_kstat = kstat_create("unix", instance, tq->tq_name, 1682*9515SJonathan.Adams@Sun.COM "taskq", KSTAT_TYPE_NAMED, 1683*9515SJonathan.Adams@Sun.COM sizeof (taskq_kstat) / sizeof (kstat_named_t), 1684*9515SJonathan.Adams@Sun.COM KSTAT_FLAG_VIRTUAL)) != NULL) { 16850Sstevel@tonic-gate tq->tq_kstat->ks_lock = &taskq_kstat_lock; 16860Sstevel@tonic-gate tq->tq_kstat->ks_data = &taskq_kstat; 16870Sstevel@tonic-gate tq->tq_kstat->ks_update = taskq_kstat_update; 16880Sstevel@tonic-gate tq->tq_kstat->ks_private = tq; 16890Sstevel@tonic-gate kstat_install(tq->tq_kstat); 16900Sstevel@tonic-gate } 16910Sstevel@tonic-gate } 16920Sstevel@tonic-gate 16930Sstevel@tonic-gate return (tq); 16940Sstevel@tonic-gate } 16950Sstevel@tonic-gate 16960Sstevel@tonic-gate /* 16970Sstevel@tonic-gate * taskq_destroy(). 16980Sstevel@tonic-gate * 16990Sstevel@tonic-gate * Assumes: by the time taskq_destroy is called no one will use this task queue 17000Sstevel@tonic-gate * in any way and no one will try to dispatch entries in it. 17010Sstevel@tonic-gate */ 17020Sstevel@tonic-gate void 17030Sstevel@tonic-gate taskq_destroy(taskq_t *tq) 17040Sstevel@tonic-gate { 17050Sstevel@tonic-gate taskq_bucket_t *b = tq->tq_buckets; 17060Sstevel@tonic-gate int bid = 0; 17070Sstevel@tonic-gate 17080Sstevel@tonic-gate ASSERT(! (tq->tq_flags & TASKQ_CPR_SAFE)); 17090Sstevel@tonic-gate 17100Sstevel@tonic-gate /* 17110Sstevel@tonic-gate * Destroy kstats. 17120Sstevel@tonic-gate */ 17130Sstevel@tonic-gate if (tq->tq_kstat != NULL) { 17140Sstevel@tonic-gate kstat_delete(tq->tq_kstat); 17150Sstevel@tonic-gate tq->tq_kstat = NULL; 17160Sstevel@tonic-gate } 17170Sstevel@tonic-gate 17180Sstevel@tonic-gate /* 17190Sstevel@tonic-gate * Destroy instance if needed. 17200Sstevel@tonic-gate */ 17210Sstevel@tonic-gate if (tq->tq_flags & TASKQ_NOINSTANCE) { 17220Sstevel@tonic-gate vmem_free(taskq_id_arena, (void *)(uintptr_t)(tq->tq_instance), 17230Sstevel@tonic-gate 1); 17240Sstevel@tonic-gate tq->tq_instance = 0; 17250Sstevel@tonic-gate } 17260Sstevel@tonic-gate 17270Sstevel@tonic-gate /* 1728*9515SJonathan.Adams@Sun.COM * Unregister from the cpupct list. 1729*9515SJonathan.Adams@Sun.COM */ 1730*9515SJonathan.Adams@Sun.COM if (tq->tq_flags & TASKQ_THREADS_CPU_PCT) { 1731*9515SJonathan.Adams@Sun.COM taskq_cpupct_ent_t *tpp; 1732*9515SJonathan.Adams@Sun.COM 1733*9515SJonathan.Adams@Sun.COM mutex_enter(&taskq_cpupct_lock); 1734*9515SJonathan.Adams@Sun.COM for (tpp = list_head(&taskq_cpupct_list); tpp != NULL; 1735*9515SJonathan.Adams@Sun.COM tpp = list_next(&taskq_cpupct_list, tpp)) { 1736*9515SJonathan.Adams@Sun.COM if (tpp->tp_taskq == tq) 1737*9515SJonathan.Adams@Sun.COM break; 1738*9515SJonathan.Adams@Sun.COM } 1739*9515SJonathan.Adams@Sun.COM ASSERT3P(tpp, !=, NULL); 1740*9515SJonathan.Adams@Sun.COM 1741*9515SJonathan.Adams@Sun.COM list_remove(&taskq_cpupct_list, tpp); 1742*9515SJonathan.Adams@Sun.COM mutex_exit(&taskq_cpupct_lock); 1743*9515SJonathan.Adams@Sun.COM 1744*9515SJonathan.Adams@Sun.COM kmem_free(tpp, sizeof (*tpp)); 1745*9515SJonathan.Adams@Sun.COM } 1746*9515SJonathan.Adams@Sun.COM 1747*9515SJonathan.Adams@Sun.COM /* 17480Sstevel@tonic-gate * Wait for any pending entries to complete. 17490Sstevel@tonic-gate */ 17500Sstevel@tonic-gate taskq_wait(tq); 17510Sstevel@tonic-gate 17520Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 17530Sstevel@tonic-gate ASSERT((tq->tq_task.tqent_next == &tq->tq_task) && 17540Sstevel@tonic-gate (tq->tq_active == 0)); 17550Sstevel@tonic-gate 1756*9515SJonathan.Adams@Sun.COM /* notify all the threads that they need to exit */ 1757*9515SJonathan.Adams@Sun.COM tq->tq_nthreads_target = 0; 17580Sstevel@tonic-gate 1759*9515SJonathan.Adams@Sun.COM tq->tq_flags |= TASKQ_CHANGING; 17600Sstevel@tonic-gate cv_broadcast(&tq->tq_dispatch_cv); 1761*9515SJonathan.Adams@Sun.COM cv_broadcast(&tq->tq_exit_cv); 1762*9515SJonathan.Adams@Sun.COM 17630Sstevel@tonic-gate while (tq->tq_nthreads != 0) 17640Sstevel@tonic-gate cv_wait(&tq->tq_wait_cv, &tq->tq_lock); 17650Sstevel@tonic-gate 1766*9515SJonathan.Adams@Sun.COM if (tq->tq_nthreads_max != 1) 1767*9515SJonathan.Adams@Sun.COM kmem_free(tq->tq_threadlist, sizeof (kthread_t *) * 1768*9515SJonathan.Adams@Sun.COM tq->tq_nthreads_max); 1769*9515SJonathan.Adams@Sun.COM 17700Sstevel@tonic-gate tq->tq_minalloc = 0; 17710Sstevel@tonic-gate while (tq->tq_nalloc != 0) 17720Sstevel@tonic-gate taskq_ent_free(tq, taskq_ent_alloc(tq, TQ_SLEEP)); 17730Sstevel@tonic-gate 17740Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 17750Sstevel@tonic-gate 17760Sstevel@tonic-gate /* 17770Sstevel@tonic-gate * Mark each bucket as closing and wakeup all sleeping threads. 17780Sstevel@tonic-gate */ 17790Sstevel@tonic-gate for (; (b != NULL) && (bid < tq->tq_nbuckets); b++, bid++) { 17800Sstevel@tonic-gate taskq_ent_t *tqe; 17810Sstevel@tonic-gate 17820Sstevel@tonic-gate mutex_enter(&b->tqbucket_lock); 17830Sstevel@tonic-gate 17840Sstevel@tonic-gate b->tqbucket_flags |= TQBUCKET_CLOSE; 17850Sstevel@tonic-gate /* Wakeup all sleeping threads */ 17860Sstevel@tonic-gate 17870Sstevel@tonic-gate for (tqe = b->tqbucket_freelist.tqent_next; 17880Sstevel@tonic-gate tqe != &b->tqbucket_freelist; tqe = tqe->tqent_next) 17890Sstevel@tonic-gate cv_signal(&tqe->tqent_cv); 17900Sstevel@tonic-gate 17910Sstevel@tonic-gate ASSERT(b->tqbucket_nalloc == 0); 17920Sstevel@tonic-gate 17930Sstevel@tonic-gate /* 17940Sstevel@tonic-gate * At this point we waited for all pending jobs to complete (in 17950Sstevel@tonic-gate * both the task queue and the bucket and no new jobs should 17960Sstevel@tonic-gate * arrive. Wait for all threads to die. 17970Sstevel@tonic-gate */ 17980Sstevel@tonic-gate while (b->tqbucket_nfree > 0) 17990Sstevel@tonic-gate cv_wait(&b->tqbucket_cv, &b->tqbucket_lock); 18000Sstevel@tonic-gate mutex_exit(&b->tqbucket_lock); 18010Sstevel@tonic-gate mutex_destroy(&b->tqbucket_lock); 18020Sstevel@tonic-gate cv_destroy(&b->tqbucket_cv); 18030Sstevel@tonic-gate } 18040Sstevel@tonic-gate 18050Sstevel@tonic-gate if (tq->tq_buckets != NULL) { 18060Sstevel@tonic-gate ASSERT(tq->tq_flags & TASKQ_DYNAMIC); 18070Sstevel@tonic-gate kmem_free(tq->tq_buckets, 18080Sstevel@tonic-gate sizeof (taskq_bucket_t) * tq->tq_nbuckets); 18090Sstevel@tonic-gate 18100Sstevel@tonic-gate /* Cleanup fields before returning tq to the cache */ 18110Sstevel@tonic-gate tq->tq_buckets = NULL; 18120Sstevel@tonic-gate tq->tq_tcreates = 0; 18130Sstevel@tonic-gate tq->tq_tdeaths = 0; 18140Sstevel@tonic-gate } else { 18150Sstevel@tonic-gate ASSERT(!(tq->tq_flags & TASKQ_DYNAMIC)); 18160Sstevel@tonic-gate } 18170Sstevel@tonic-gate 1818*9515SJonathan.Adams@Sun.COM tq->tq_threads_ncpus_pct = 0; 18190Sstevel@tonic-gate tq->tq_totaltime = 0; 18200Sstevel@tonic-gate tq->tq_tasks = 0; 18210Sstevel@tonic-gate tq->tq_maxtasks = 0; 18220Sstevel@tonic-gate tq->tq_executed = 0; 18230Sstevel@tonic-gate kmem_cache_free(taskq_cache, tq); 18240Sstevel@tonic-gate } 18250Sstevel@tonic-gate 18260Sstevel@tonic-gate /* 18270Sstevel@tonic-gate * Extend a bucket with a new entry on the free list and attach a worker thread 18280Sstevel@tonic-gate * to it. 18290Sstevel@tonic-gate * 18300Sstevel@tonic-gate * Argument: pointer to the bucket. 18310Sstevel@tonic-gate * 18320Sstevel@tonic-gate * This function may quietly fail. It is only used by taskq_dispatch() which 18330Sstevel@tonic-gate * handles such failures properly. 18340Sstevel@tonic-gate */ 18350Sstevel@tonic-gate static void 18360Sstevel@tonic-gate taskq_bucket_extend(void *arg) 18370Sstevel@tonic-gate { 18380Sstevel@tonic-gate taskq_ent_t *tqe; 18390Sstevel@tonic-gate taskq_bucket_t *b = (taskq_bucket_t *)arg; 18400Sstevel@tonic-gate taskq_t *tq = b->tqbucket_taskq; 18410Sstevel@tonic-gate int nthreads; 18420Sstevel@tonic-gate 18430Sstevel@tonic-gate if (! ENOUGH_MEMORY()) { 18440Sstevel@tonic-gate TQ_STAT(b, tqs_nomem); 18450Sstevel@tonic-gate return; 18460Sstevel@tonic-gate } 18470Sstevel@tonic-gate 18480Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 18490Sstevel@tonic-gate 18500Sstevel@tonic-gate /* 18510Sstevel@tonic-gate * Observe global taskq limits on the number of threads. 18520Sstevel@tonic-gate */ 18530Sstevel@tonic-gate if (tq->tq_tcreates++ - tq->tq_tdeaths > tq->tq_maxsize) { 18540Sstevel@tonic-gate tq->tq_tcreates--; 18550Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 18560Sstevel@tonic-gate return; 18570Sstevel@tonic-gate } 18580Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 18590Sstevel@tonic-gate 18600Sstevel@tonic-gate tqe = kmem_cache_alloc(taskq_ent_cache, KM_NOSLEEP); 18610Sstevel@tonic-gate 18620Sstevel@tonic-gate if (tqe == NULL) { 18630Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 18640Sstevel@tonic-gate TQ_STAT(b, tqs_nomem); 18650Sstevel@tonic-gate tq->tq_tcreates--; 18660Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 18670Sstevel@tonic-gate return; 18680Sstevel@tonic-gate } 18690Sstevel@tonic-gate 18700Sstevel@tonic-gate ASSERT(tqe->tqent_thread == NULL); 18710Sstevel@tonic-gate 18720Sstevel@tonic-gate tqe->tqent_bucket = b; 18730Sstevel@tonic-gate 18740Sstevel@tonic-gate /* 18750Sstevel@tonic-gate * Create a thread in a TS_STOPPED state first. If it is successfully 18760Sstevel@tonic-gate * created, place the entry on the free list and start the thread. 18770Sstevel@tonic-gate */ 18780Sstevel@tonic-gate tqe->tqent_thread = thread_create(NULL, 0, taskq_d_thread, tqe, 18790Sstevel@tonic-gate 0, &p0, TS_STOPPED, tq->tq_pri); 18800Sstevel@tonic-gate 18810Sstevel@tonic-gate /* 18820Sstevel@tonic-gate * Once the entry is ready, link it to the the bucket free list. 18830Sstevel@tonic-gate */ 18840Sstevel@tonic-gate mutex_enter(&b->tqbucket_lock); 18850Sstevel@tonic-gate tqe->tqent_func = NULL; 18860Sstevel@tonic-gate TQ_APPEND(b->tqbucket_freelist, tqe); 18870Sstevel@tonic-gate b->tqbucket_nfree++; 18880Sstevel@tonic-gate TQ_STAT(b, tqs_tcreates); 18890Sstevel@tonic-gate 18900Sstevel@tonic-gate #if TASKQ_STATISTIC 18910Sstevel@tonic-gate nthreads = b->tqbucket_stat.tqs_tcreates - 18920Sstevel@tonic-gate b->tqbucket_stat.tqs_tdeaths; 18930Sstevel@tonic-gate b->tqbucket_stat.tqs_maxthreads = MAX(nthreads, 18940Sstevel@tonic-gate b->tqbucket_stat.tqs_maxthreads); 18950Sstevel@tonic-gate #endif 18960Sstevel@tonic-gate 18970Sstevel@tonic-gate mutex_exit(&b->tqbucket_lock); 18980Sstevel@tonic-gate /* 18990Sstevel@tonic-gate * Start the stopped thread. 19000Sstevel@tonic-gate */ 19010Sstevel@tonic-gate thread_lock(tqe->tqent_thread); 19020Sstevel@tonic-gate tqe->tqent_thread->t_taskq = tq; 19030Sstevel@tonic-gate tqe->tqent_thread->t_schedflag |= TS_ALLSTART; 19040Sstevel@tonic-gate setrun_locked(tqe->tqent_thread); 19050Sstevel@tonic-gate thread_unlock(tqe->tqent_thread); 19060Sstevel@tonic-gate } 19070Sstevel@tonic-gate 19080Sstevel@tonic-gate static int 19090Sstevel@tonic-gate taskq_kstat_update(kstat_t *ksp, int rw) 19100Sstevel@tonic-gate { 19110Sstevel@tonic-gate struct taskq_kstat *tqsp = &taskq_kstat; 19120Sstevel@tonic-gate taskq_t *tq = ksp->ks_private; 19130Sstevel@tonic-gate 19140Sstevel@tonic-gate if (rw == KSTAT_WRITE) 19150Sstevel@tonic-gate return (EACCES); 19160Sstevel@tonic-gate 19170Sstevel@tonic-gate tqsp->tq_tasks.value.ui64 = tq->tq_tasks; 19180Sstevel@tonic-gate tqsp->tq_executed.value.ui64 = tq->tq_executed; 19190Sstevel@tonic-gate tqsp->tq_maxtasks.value.ui64 = tq->tq_maxtasks; 19200Sstevel@tonic-gate tqsp->tq_totaltime.value.ui64 = tq->tq_totaltime; 19210Sstevel@tonic-gate tqsp->tq_nactive.value.ui64 = tq->tq_active; 19220Sstevel@tonic-gate tqsp->tq_nalloc.value.ui64 = tq->tq_nalloc; 19230Sstevel@tonic-gate tqsp->tq_pri.value.ui64 = tq->tq_pri; 19240Sstevel@tonic-gate tqsp->tq_nthreads.value.ui64 = tq->tq_nthreads; 19250Sstevel@tonic-gate return (0); 19260Sstevel@tonic-gate } 19270Sstevel@tonic-gate 19280Sstevel@tonic-gate static int 19290Sstevel@tonic-gate taskq_d_kstat_update(kstat_t *ksp, int rw) 19300Sstevel@tonic-gate { 19310Sstevel@tonic-gate struct taskq_d_kstat *tqsp = &taskq_d_kstat; 19320Sstevel@tonic-gate taskq_t *tq = ksp->ks_private; 19330Sstevel@tonic-gate taskq_bucket_t *b = tq->tq_buckets; 19340Sstevel@tonic-gate int bid = 0; 19350Sstevel@tonic-gate 19360Sstevel@tonic-gate if (rw == KSTAT_WRITE) 19370Sstevel@tonic-gate return (EACCES); 19380Sstevel@tonic-gate 19390Sstevel@tonic-gate ASSERT(tq->tq_flags & TASKQ_DYNAMIC); 19400Sstevel@tonic-gate 19410Sstevel@tonic-gate tqsp->tqd_btasks.value.ui64 = tq->tq_tasks; 19420Sstevel@tonic-gate tqsp->tqd_bexecuted.value.ui64 = tq->tq_executed; 19430Sstevel@tonic-gate tqsp->tqd_bmaxtasks.value.ui64 = tq->tq_maxtasks; 19440Sstevel@tonic-gate tqsp->tqd_bnalloc.value.ui64 = tq->tq_nalloc; 19450Sstevel@tonic-gate tqsp->tqd_bnactive.value.ui64 = tq->tq_active; 19460Sstevel@tonic-gate tqsp->tqd_btotaltime.value.ui64 = tq->tq_totaltime; 19470Sstevel@tonic-gate tqsp->tqd_pri.value.ui64 = tq->tq_pri; 19480Sstevel@tonic-gate 19490Sstevel@tonic-gate tqsp->tqd_hits.value.ui64 = 0; 19500Sstevel@tonic-gate tqsp->tqd_misses.value.ui64 = 0; 19510Sstevel@tonic-gate tqsp->tqd_overflows.value.ui64 = 0; 19520Sstevel@tonic-gate tqsp->tqd_tcreates.value.ui64 = 0; 19530Sstevel@tonic-gate tqsp->tqd_tdeaths.value.ui64 = 0; 19540Sstevel@tonic-gate tqsp->tqd_maxthreads.value.ui64 = 0; 19550Sstevel@tonic-gate tqsp->tqd_nomem.value.ui64 = 0; 19560Sstevel@tonic-gate tqsp->tqd_disptcreates.value.ui64 = 0; 19570Sstevel@tonic-gate tqsp->tqd_totaltime.value.ui64 = 0; 19580Sstevel@tonic-gate tqsp->tqd_nalloc.value.ui64 = 0; 19590Sstevel@tonic-gate tqsp->tqd_nfree.value.ui64 = 0; 19600Sstevel@tonic-gate 19610Sstevel@tonic-gate for (; (b != NULL) && (bid < tq->tq_nbuckets); b++, bid++) { 19620Sstevel@tonic-gate tqsp->tqd_hits.value.ui64 += b->tqbucket_stat.tqs_hits; 19630Sstevel@tonic-gate tqsp->tqd_misses.value.ui64 += b->tqbucket_stat.tqs_misses; 19640Sstevel@tonic-gate tqsp->tqd_overflows.value.ui64 += b->tqbucket_stat.tqs_overflow; 19650Sstevel@tonic-gate tqsp->tqd_tcreates.value.ui64 += b->tqbucket_stat.tqs_tcreates; 19660Sstevel@tonic-gate tqsp->tqd_tdeaths.value.ui64 += b->tqbucket_stat.tqs_tdeaths; 19670Sstevel@tonic-gate tqsp->tqd_maxthreads.value.ui64 += 19680Sstevel@tonic-gate b->tqbucket_stat.tqs_maxthreads; 19690Sstevel@tonic-gate tqsp->tqd_nomem.value.ui64 += b->tqbucket_stat.tqs_nomem; 19700Sstevel@tonic-gate tqsp->tqd_disptcreates.value.ui64 += 19710Sstevel@tonic-gate b->tqbucket_stat.tqs_disptcreates; 19720Sstevel@tonic-gate tqsp->tqd_totaltime.value.ui64 += b->tqbucket_totaltime; 19730Sstevel@tonic-gate tqsp->tqd_nalloc.value.ui64 += b->tqbucket_nalloc; 19740Sstevel@tonic-gate tqsp->tqd_nfree.value.ui64 += b->tqbucket_nfree; 19750Sstevel@tonic-gate } 19760Sstevel@tonic-gate return (0); 19770Sstevel@tonic-gate } 1978