10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 59515SJonathan.Adams@Sun.COM * Common Development and Distribution License (the "License"). 69515SJonathan.Adams@Sun.COM * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 229515SJonathan.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 * 759515SJonathan.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 839515SJonathan.Adams@Sun.COM * specified, 'nthreads' specifies the maximum number of threads in 840Sstevel@tonic-gate * the task queue. Task execution order for dynamic task queues is 850Sstevel@tonic-gate * not predictable. 860Sstevel@tonic-gate * 870Sstevel@tonic-gate * If this flag is not specified (default case) a 889515SJonathan.Adams@Sun.COM * single-list task queue is created with 'nthreads' threads 899515SJonathan.Adams@Sun.COM * servicing it. Entries in this queue are managed by 909515SJonathan.Adams@Sun.COM * taskq_ent_alloc() and taskq_ent_free() which try to keep the 919515SJonathan.Adams@Sun.COM * task population between 'minalloc' and 'maxalloc', but the 920Sstevel@tonic-gate * latter limit is only advisory for TQ_SLEEP dispatches and the 930Sstevel@tonic-gate * former limit is only advisory for TQ_NOALLOC dispatches. If 940Sstevel@tonic-gate * TASKQ_PREPOPULATE is set in 'flags', the taskq will be 950Sstevel@tonic-gate * prepopulated with 'minalloc' task structures. 960Sstevel@tonic-gate * 970Sstevel@tonic-gate * Since non-DYNAMIC taskqs are queues, tasks are guaranteed to be 980Sstevel@tonic-gate * executed in the order they are scheduled if nthreads == 1. 990Sstevel@tonic-gate * If nthreads > 1, task execution order is not predictable. 1000Sstevel@tonic-gate * 1010Sstevel@tonic-gate * TASKQ_PREPOPULATE: Prepopulate task queue with threads. 1020Sstevel@tonic-gate * Also prepopulate the task queue with 'minalloc' task structures. 1030Sstevel@tonic-gate * 1049515SJonathan.Adams@Sun.COM * TASKQ_THREADS_CPU_PCT: This flag specifies that 'nthreads' should be 1059515SJonathan.Adams@Sun.COM * interpreted as a percentage of the # of online CPUs on the 1069515SJonathan.Adams@Sun.COM * system. The taskq subsystem will automatically adjust the 1079515SJonathan.Adams@Sun.COM * number of threads in the taskq in response to CPU online 1089515SJonathan.Adams@Sun.COM * and offline events, to keep the ratio. nthreads must be in 1099515SJonathan.Adams@Sun.COM * the range [0,100]. 1109515SJonathan.Adams@Sun.COM * 1119515SJonathan.Adams@Sun.COM * The calculation used is: 1129515SJonathan.Adams@Sun.COM * 1139515SJonathan.Adams@Sun.COM * MAX((ncpus_online * percentage)/100, 1) 1149515SJonathan.Adams@Sun.COM * 1159515SJonathan.Adams@Sun.COM * This flag is not supported for DYNAMIC task queues. 1169515SJonathan.Adams@Sun.COM * This flag is not compatible with TASKQ_CPR_SAFE. 1179515SJonathan.Adams@Sun.COM * 1180Sstevel@tonic-gate * TASKQ_CPR_SAFE: This flag specifies that users of the task queue will 1199515SJonathan.Adams@Sun.COM * use their own protocol for handling CPR issues. This flag is not 1209515SJonathan.Adams@Sun.COM * supported for DYNAMIC task queues. This flag is not compatible 1219515SJonathan.Adams@Sun.COM * with TASKQ_THREADS_CPU_PCT. 1220Sstevel@tonic-gate * 1230Sstevel@tonic-gate * The 'pri' field specifies the default priority for the threads that 1240Sstevel@tonic-gate * service all scheduled tasks. 1250Sstevel@tonic-gate * 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 * 2009515SJonathan.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 * +-------------+ 206*10889SJonathan.Adams@Sun.COM * | 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 * 2649515SJonathan.Adams@Sun.COM * Taskq Thread Management ----------------------------------------------------- 2659515SJonathan.Adams@Sun.COM * 2669515SJonathan.Adams@Sun.COM * Taskq's non-dynamic threads are managed with several variables and flags: 2679515SJonathan.Adams@Sun.COM * 2689515SJonathan.Adams@Sun.COM * * tq_nthreads - The number of threads in taskq_thread() for the 2699515SJonathan.Adams@Sun.COM * taskq. 2709515SJonathan.Adams@Sun.COM * 2719515SJonathan.Adams@Sun.COM * * tq_active - The number of threads not waiting on a CV in 2729515SJonathan.Adams@Sun.COM * taskq_thread(); includes newly created threads 2739515SJonathan.Adams@Sun.COM * not yet counted in tq_nthreads. 2749515SJonathan.Adams@Sun.COM * 2759515SJonathan.Adams@Sun.COM * * tq_nthreads_target 2769515SJonathan.Adams@Sun.COM * - The number of threads desired for the taskq. 2779515SJonathan.Adams@Sun.COM * 2789515SJonathan.Adams@Sun.COM * * tq_flags & TASKQ_CHANGING 2799515SJonathan.Adams@Sun.COM * - Indicates that tq_nthreads != tq_nthreads_target. 2809515SJonathan.Adams@Sun.COM * 2819515SJonathan.Adams@Sun.COM * * tq_flags & TASKQ_THREAD_CREATED 2829515SJonathan.Adams@Sun.COM * - Indicates that a thread is being created in the taskq. 2839515SJonathan.Adams@Sun.COM * 2849515SJonathan.Adams@Sun.COM * During creation, tq_nthreads and tq_active are set to 0, and 2859515SJonathan.Adams@Sun.COM * tq_nthreads_target is set to the number of threads desired. The 2869515SJonathan.Adams@Sun.COM * TASKQ_CHANGING flag is set, and taskq_create_thread() is called to 2879515SJonathan.Adams@Sun.COM * create the first thread. taskq_create_thread() increments tq_active, 2889515SJonathan.Adams@Sun.COM * sets TASKQ_THREAD_CREATED, and creates the new thread. 2899515SJonathan.Adams@Sun.COM * 2909515SJonathan.Adams@Sun.COM * Each thread starts in taskq_thread(), clears the TASKQ_THREAD_CREATED 2919515SJonathan.Adams@Sun.COM * flag, and increments tq_nthreads. It stores the new value of 2929515SJonathan.Adams@Sun.COM * tq_nthreads as its "thread_id", and stores its thread pointer in the 2939515SJonathan.Adams@Sun.COM * tq_threadlist at the (thread_id - 1). We keep the thread_id space 2949515SJonathan.Adams@Sun.COM * densely packed by requiring that only the largest thread_id can exit during 2959515SJonathan.Adams@Sun.COM * normal adjustment. The exception is during the destruction of the 2969515SJonathan.Adams@Sun.COM * taskq; once tq_nthreads_target is set to zero, no new threads will be created 2979515SJonathan.Adams@Sun.COM * for the taskq queue, so every thread can exit without any ordering being 2989515SJonathan.Adams@Sun.COM * necessary. 2999515SJonathan.Adams@Sun.COM * 3009515SJonathan.Adams@Sun.COM * Threads will only process work if their thread id is <= tq_nthreads_target. 3019515SJonathan.Adams@Sun.COM * 3029515SJonathan.Adams@Sun.COM * When TASKQ_CHANGING is set, threads will check the current thread target 3039515SJonathan.Adams@Sun.COM * whenever they wake up, and do whatever they can to apply its effects. 3049515SJonathan.Adams@Sun.COM * 3059515SJonathan.Adams@Sun.COM * TASKQ_THREAD_CPU_PCT -------------------------------------------------------- 3069515SJonathan.Adams@Sun.COM * 3079515SJonathan.Adams@Sun.COM * When a taskq is created with TASKQ_THREAD_CPU_PCT, we store their requested 3089515SJonathan.Adams@Sun.COM * percentage in tq_threads_ncpus_pct, start them off with the correct thread 3099515SJonathan.Adams@Sun.COM * target, and add them to the taskq_cpupct_list for later adjustment. 3109515SJonathan.Adams@Sun.COM * 3119515SJonathan.Adams@Sun.COM * We register taskq_cpu_setup() to be called whenever a CPU changes state. It 3129515SJonathan.Adams@Sun.COM * walks the list of TASKQ_THREAD_CPU_PCT taskqs, adjusts their nthread_target 3139515SJonathan.Adams@Sun.COM * if need be, and wakes up all of the threads to process the change. 3149515SJonathan.Adams@Sun.COM * 3159515SJonathan.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 * 3449515SJonathan.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 * 3669515SJonathan.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 * 3879515SJonathan.Adams@Sun.COM * LOCKS and LOCK Hierarchy ---------------------------------------------------- 3880Sstevel@tonic-gate * 3899515SJonathan.Adams@Sun.COM * There are three locks used in task queues: 3900Sstevel@tonic-gate * 3919515SJonathan.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 * 3959515SJonathan.Adams@Sun.COM * 3) The global taskq_cpupct_lock, which protects the list of 3969515SJonathan.Adams@Sun.COM * TASKQ_THREADS_CPU_PCT taskqs. 3979515SJonathan.Adams@Sun.COM * 3989515SJonathan.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 * 4019515SJonathan.Adams@Sun.COM * If both (1) and (3) are needed, tq_lock should be taken *after* 4029515SJonathan.Adams@Sun.COM * taskq_cpupct_lock. 4039515SJonathan.Adams@Sun.COM * 4049515SJonathan.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 * 4139515SJonathan.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 * 4209515SJonathan.Adams@Sun.COM * taskq_minimum_nthreads_max 4219515SJonathan.Adams@Sun.COM * - Minimum size of the thread list for a taskq. 4229515SJonathan.Adams@Sun.COM * Useful for testing different thread pool 4239515SJonathan.Adams@Sun.COM * sizes by overwriting tq_nthreads_target. 4249515SJonathan.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 * 4429515SJonathan.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> 4619515SJonathan.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 /* 4669515SJonathan.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 /* 4749515SJonathan.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 /* 4819515SJonathan.Adams@Sun.COM * Minimum size for tq_nthreads_max; useful for those who want to play around 4829515SJonathan.Adams@Sun.COM * with increasing a taskq's tq_nthreads_target. 4839515SJonathan.Adams@Sun.COM */ 4849515SJonathan.Adams@Sun.COM int taskq_minimum_nthreads_max = 1; 4859515SJonathan.Adams@Sun.COM 4869515SJonathan.Adams@Sun.COM /* Maximum percentage allowed for TASKQ_THREADS_CPU_PCT */ 4879515SJonathan.Adams@Sun.COM #define TASKQ_CPUPCT_MAX_PERCENT 1000 4889515SJonathan.Adams@Sun.COM int taskq_cpupct_max_percent = TASKQ_CPUPCT_MAX_PERCENT; 4899515SJonathan.Adams@Sun.COM 4909515SJonathan.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 6069515SJonathan.Adams@Sun.COM /* 6079515SJonathan.Adams@Sun.COM * State for THREAD_CPU_PCT management 6089515SJonathan.Adams@Sun.COM */ 6099515SJonathan.Adams@Sun.COM typedef struct taskq_cpupct_ent { 6109515SJonathan.Adams@Sun.COM list_node_t tp_link; 6119515SJonathan.Adams@Sun.COM taskq_t *tp_taskq; 6129515SJonathan.Adams@Sun.COM } taskq_cpupct_ent_t; 6139515SJonathan.Adams@Sun.COM 6149515SJonathan.Adams@Sun.COM static kmutex_t taskq_cpupct_lock; 6159515SJonathan.Adams@Sun.COM static list_t taskq_cpupct_list; 6169515SJonathan.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); 7189515SJonathan.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 7339515SJonathan.Adams@Sun.COM ASSERT(tq->tq_nthreads == 0); 7349515SJonathan.Adams@Sun.COM ASSERT(tq->tq_buckets == NULL); 7359515SJonathan.Adams@Sun.COM ASSERT(tq->tq_tcreates == 0); 7369515SJonathan.Adams@Sun.COM ASSERT(tq->tq_tdeaths == 0); 7379515SJonathan.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); 7419515SJonathan.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); 7789515SJonathan.Adams@Sun.COM 7799515SJonathan.Adams@Sun.COM list_create(&taskq_cpupct_list, sizeof (taskq_cpupct_ent_t), 7809515SJonathan.Adams@Sun.COM offsetof(taskq_cpupct_ent_t, tp_link)); 7819515SJonathan.Adams@Sun.COM } 7829515SJonathan.Adams@Sun.COM 7839515SJonathan.Adams@Sun.COM /*ARGSUSED*/ 7849515SJonathan.Adams@Sun.COM static int 7859515SJonathan.Adams@Sun.COM taskq_cpu_setup(cpu_setup_t what, int id, void *arg) 7869515SJonathan.Adams@Sun.COM { 7879515SJonathan.Adams@Sun.COM taskq_cpupct_ent_t *tpp; 7889515SJonathan.Adams@Sun.COM int cpus_online = ncpus_online; 7899515SJonathan.Adams@Sun.COM 7909515SJonathan.Adams@Sun.COM /* offlines are called *before* the cpu is offlined. */ 7919515SJonathan.Adams@Sun.COM if (what == CPU_OFF) 7929515SJonathan.Adams@Sun.COM cpus_online--; 7939515SJonathan.Adams@Sun.COM if (cpus_online < 1) 7949515SJonathan.Adams@Sun.COM cpus_online = 1; 7959515SJonathan.Adams@Sun.COM 7969515SJonathan.Adams@Sun.COM mutex_enter(&taskq_cpupct_lock); 7979515SJonathan.Adams@Sun.COM if (cpus_online == taskq_cpupct_ncpus_online) { 7989515SJonathan.Adams@Sun.COM mutex_exit(&taskq_cpupct_lock); 7999515SJonathan.Adams@Sun.COM return (0); 8009515SJonathan.Adams@Sun.COM } 8019515SJonathan.Adams@Sun.COM 8029515SJonathan.Adams@Sun.COM for (tpp = list_head(&taskq_cpupct_list); tpp != NULL; 8039515SJonathan.Adams@Sun.COM tpp = list_next(&taskq_cpupct_list, tpp)) { 8049515SJonathan.Adams@Sun.COM taskq_t *tq = tpp->tp_taskq; 8059515SJonathan.Adams@Sun.COM int newtarget; 8069515SJonathan.Adams@Sun.COM 8079515SJonathan.Adams@Sun.COM mutex_enter(&tq->tq_lock); 8089515SJonathan.Adams@Sun.COM newtarget = 8099515SJonathan.Adams@Sun.COM TASKQ_THREADS_PCT(cpus_online, tq->tq_threads_ncpus_pct); 8109515SJonathan.Adams@Sun.COM ASSERT3S(newtarget, <=, tq->tq_nthreads_max); 8119515SJonathan.Adams@Sun.COM if (newtarget != tq->tq_nthreads_target) { 8129515SJonathan.Adams@Sun.COM /* The taskq must not be exiting */ 8139515SJonathan.Adams@Sun.COM ASSERT3S(tq->tq_nthreads_target, !=, 0); 8149515SJonathan.Adams@Sun.COM tq->tq_flags |= TASKQ_CHANGING; 8159515SJonathan.Adams@Sun.COM tq->tq_nthreads_target = newtarget; 8169515SJonathan.Adams@Sun.COM cv_broadcast(&tq->tq_dispatch_cv); 8179515SJonathan.Adams@Sun.COM cv_broadcast(&tq->tq_exit_cv); 8189515SJonathan.Adams@Sun.COM } 8199515SJonathan.Adams@Sun.COM mutex_exit(&tq->tq_lock); 8209515SJonathan.Adams@Sun.COM } 8219515SJonathan.Adams@Sun.COM 8229515SJonathan.Adams@Sun.COM taskq_cpupct_ncpus_online = cpus_online; 8239515SJonathan.Adams@Sun.COM mutex_exit(&taskq_cpupct_lock); 8249515SJonathan.Adams@Sun.COM return (0); 8259515SJonathan.Adams@Sun.COM } 8269515SJonathan.Adams@Sun.COM 8279515SJonathan.Adams@Sun.COM void 8289515SJonathan.Adams@Sun.COM taskq_mp_init(void) 8299515SJonathan.Adams@Sun.COM { 8309515SJonathan.Adams@Sun.COM mutex_enter(&cpu_lock); 8319515SJonathan.Adams@Sun.COM register_cpu_setup_func(taskq_cpu_setup, NULL); 8329515SJonathan.Adams@Sun.COM (void) taskq_cpu_setup(CPU_ON, 0, NULL); 8339515SJonathan.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 12269515SJonathan.Adams@Sun.COM static void 12279515SJonathan.Adams@Sun.COM taskq_thread_create(taskq_t *tq) 12289515SJonathan.Adams@Sun.COM { 12299515SJonathan.Adams@Sun.COM kthread_t *t; 12309515SJonathan.Adams@Sun.COM 12319515SJonathan.Adams@Sun.COM ASSERT(MUTEX_HELD(&tq->tq_lock)); 12329515SJonathan.Adams@Sun.COM ASSERT(!(tq->tq_flags & TASKQ_THREAD_CREATED)); 12339515SJonathan.Adams@Sun.COM 12349515SJonathan.Adams@Sun.COM tq->tq_flags |= TASKQ_THREAD_CREATED; 12359515SJonathan.Adams@Sun.COM tq->tq_active++; 12369515SJonathan.Adams@Sun.COM t = thread_create(NULL, 0, taskq_thread, tq, 0, &p0, TS_RUN, 12379515SJonathan.Adams@Sun.COM tq->tq_pri); 12389515SJonathan.Adams@Sun.COM t->t_taskq = tq; 12399515SJonathan.Adams@Sun.COM } 12409515SJonathan.Adams@Sun.COM 1241*10889SJonathan.Adams@Sun.COM /* 1242*10889SJonathan.Adams@Sun.COM * Common "sleep taskq thread" function, which handles CPR stuff, as well 1243*10889SJonathan.Adams@Sun.COM * as giving a nice common point for debuggers to find inactive threads. 1244*10889SJonathan.Adams@Sun.COM */ 1245*10889SJonathan.Adams@Sun.COM static clock_t 1246*10889SJonathan.Adams@Sun.COM taskq_thread_wait(taskq_t *tq, kmutex_t *mx, kcondvar_t *cv, 1247*10889SJonathan.Adams@Sun.COM callb_cpr_t *cprinfo, clock_t timeout) 12489515SJonathan.Adams@Sun.COM { 1249*10889SJonathan.Adams@Sun.COM clock_t ret = 0; 1250*10889SJonathan.Adams@Sun.COM 1251*10889SJonathan.Adams@Sun.COM if (!(tq->tq_flags & TASKQ_CPR_SAFE)) { 12529515SJonathan.Adams@Sun.COM CALLB_CPR_SAFE_BEGIN(cprinfo); 12539515SJonathan.Adams@Sun.COM } 1254*10889SJonathan.Adams@Sun.COM if (timeout < 0) 1255*10889SJonathan.Adams@Sun.COM cv_wait(cv, mx); 1256*10889SJonathan.Adams@Sun.COM else 1257*10889SJonathan.Adams@Sun.COM ret = cv_timedwait(cv, mx, lbolt + timeout); 1258*10889SJonathan.Adams@Sun.COM 1259*10889SJonathan.Adams@Sun.COM if (!(tq->tq_flags & TASKQ_CPR_SAFE)) { 1260*10889SJonathan.Adams@Sun.COM CALLB_CPR_SAFE_END(cprinfo, mx); 1261*10889SJonathan.Adams@Sun.COM } 1262*10889SJonathan.Adams@Sun.COM 1263*10889SJonathan.Adams@Sun.COM return (ret); 12649515SJonathan.Adams@Sun.COM } 12659515SJonathan.Adams@Sun.COM 12660Sstevel@tonic-gate /* 12670Sstevel@tonic-gate * Worker thread for processing task queue. 12680Sstevel@tonic-gate */ 12690Sstevel@tonic-gate static void 12700Sstevel@tonic-gate taskq_thread(void *arg) 12710Sstevel@tonic-gate { 12729515SJonathan.Adams@Sun.COM int thread_id; 12739515SJonathan.Adams@Sun.COM 12740Sstevel@tonic-gate taskq_t *tq = arg; 12750Sstevel@tonic-gate taskq_ent_t *tqe; 12760Sstevel@tonic-gate callb_cpr_t cprinfo; 12770Sstevel@tonic-gate hrtime_t start, end; 12780Sstevel@tonic-gate 12790Sstevel@tonic-gate if (tq->tq_flags & TASKQ_CPR_SAFE) { 12800Sstevel@tonic-gate CALLB_CPR_INIT_SAFE(curthread, tq->tq_name); 12810Sstevel@tonic-gate } else { 12820Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, &tq->tq_lock, callb_generic_cpr, 12830Sstevel@tonic-gate tq->tq_name); 12840Sstevel@tonic-gate } 12850Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 12869515SJonathan.Adams@Sun.COM thread_id = ++tq->tq_nthreads; 12879515SJonathan.Adams@Sun.COM ASSERT(tq->tq_flags & TASKQ_THREAD_CREATED); 12889515SJonathan.Adams@Sun.COM tq->tq_flags &= ~TASKQ_THREAD_CREATED; 12899515SJonathan.Adams@Sun.COM 12909515SJonathan.Adams@Sun.COM VERIFY3S(thread_id, <=, tq->tq_nthreads_max); 12919515SJonathan.Adams@Sun.COM 12929515SJonathan.Adams@Sun.COM if (tq->tq_nthreads_max == 1) 12939515SJonathan.Adams@Sun.COM tq->tq_thread = curthread; 12949515SJonathan.Adams@Sun.COM else 12959515SJonathan.Adams@Sun.COM tq->tq_threadlist[thread_id - 1] = curthread; 12969515SJonathan.Adams@Sun.COM 12979515SJonathan.Adams@Sun.COM for (;;) { 12989515SJonathan.Adams@Sun.COM if (tq->tq_flags & TASKQ_CHANGING) { 12999515SJonathan.Adams@Sun.COM /* we're done; clear the CHANGING flag */ 13009515SJonathan.Adams@Sun.COM if (tq->tq_nthreads == tq->tq_nthreads_target) { 13019515SJonathan.Adams@Sun.COM tq->tq_flags &= ~TASKQ_CHANGING; 13029515SJonathan.Adams@Sun.COM continue; 13039515SJonathan.Adams@Sun.COM } 13049515SJonathan.Adams@Sun.COM /* We're low on threads and none have been created */ 13059515SJonathan.Adams@Sun.COM if (tq->tq_nthreads < tq->tq_nthreads_target && 13069515SJonathan.Adams@Sun.COM !(tq->tq_flags & TASKQ_THREAD_CREATED)) { 13079515SJonathan.Adams@Sun.COM taskq_thread_create(tq); 13089515SJonathan.Adams@Sun.COM continue; 13099515SJonathan.Adams@Sun.COM } 13109515SJonathan.Adams@Sun.COM /* We're no longer needed */ 13119515SJonathan.Adams@Sun.COM if (thread_id > tq->tq_nthreads_target) { 13129515SJonathan.Adams@Sun.COM /* 13139515SJonathan.Adams@Sun.COM * To preserve the one-to-one mapping between 13149515SJonathan.Adams@Sun.COM * thread_id and thread, we must exit from 13159515SJonathan.Adams@Sun.COM * highest thread ID to least. 13169515SJonathan.Adams@Sun.COM * 13179515SJonathan.Adams@Sun.COM * However, if everyone is exiting, the order 13189515SJonathan.Adams@Sun.COM * doesn't matter, so just exit immediately. 13199515SJonathan.Adams@Sun.COM * (this is safe, since you must wait for 13209515SJonathan.Adams@Sun.COM * nthreads to reach 0 after setting 13219515SJonathan.Adams@Sun.COM * tq_nthreads_target to 0) 13229515SJonathan.Adams@Sun.COM */ 13239515SJonathan.Adams@Sun.COM if (thread_id == tq->tq_nthreads || 13249515SJonathan.Adams@Sun.COM tq->tq_nthreads_target == 0) 13259515SJonathan.Adams@Sun.COM break; 13269515SJonathan.Adams@Sun.COM 13279515SJonathan.Adams@Sun.COM /* Wait for higher thread_ids to exit */ 1328*10889SJonathan.Adams@Sun.COM (void) taskq_thread_wait(tq, &tq->tq_lock, 1329*10889SJonathan.Adams@Sun.COM &tq->tq_exit_cv, &cprinfo, -1); 13309515SJonathan.Adams@Sun.COM continue; 13319515SJonathan.Adams@Sun.COM } 13329515SJonathan.Adams@Sun.COM } 13330Sstevel@tonic-gate if ((tqe = tq->tq_task.tqent_next) == &tq->tq_task) { 13340Sstevel@tonic-gate if (--tq->tq_active == 0) 13350Sstevel@tonic-gate cv_broadcast(&tq->tq_wait_cv); 1336*10889SJonathan.Adams@Sun.COM (void) taskq_thread_wait(tq, &tq->tq_lock, 1337*10889SJonathan.Adams@Sun.COM &tq->tq_dispatch_cv, &cprinfo, -1); 13380Sstevel@tonic-gate tq->tq_active++; 13390Sstevel@tonic-gate continue; 13400Sstevel@tonic-gate } 13410Sstevel@tonic-gate tqe->tqent_prev->tqent_next = tqe->tqent_next; 13420Sstevel@tonic-gate tqe->tqent_next->tqent_prev = tqe->tqent_prev; 13430Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 13440Sstevel@tonic-gate 13450Sstevel@tonic-gate rw_enter(&tq->tq_threadlock, RW_READER); 13460Sstevel@tonic-gate start = gethrtime(); 13470Sstevel@tonic-gate DTRACE_PROBE2(taskq__exec__start, taskq_t *, tq, 13480Sstevel@tonic-gate taskq_ent_t *, tqe); 13490Sstevel@tonic-gate tqe->tqent_func(tqe->tqent_arg); 13500Sstevel@tonic-gate DTRACE_PROBE2(taskq__exec__end, taskq_t *, tq, 13510Sstevel@tonic-gate taskq_ent_t *, tqe); 13520Sstevel@tonic-gate end = gethrtime(); 13530Sstevel@tonic-gate rw_exit(&tq->tq_threadlock); 13540Sstevel@tonic-gate 13550Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 13560Sstevel@tonic-gate tq->tq_totaltime += end - start; 13570Sstevel@tonic-gate tq->tq_executed++; 13580Sstevel@tonic-gate 13590Sstevel@tonic-gate taskq_ent_free(tq, tqe); 13600Sstevel@tonic-gate } 13619515SJonathan.Adams@Sun.COM 13629515SJonathan.Adams@Sun.COM if (tq->tq_nthreads_max == 1) 13639515SJonathan.Adams@Sun.COM tq->tq_thread = NULL; 13649515SJonathan.Adams@Sun.COM else 13659515SJonathan.Adams@Sun.COM tq->tq_threadlist[thread_id - 1] = NULL; 13669515SJonathan.Adams@Sun.COM 13679515SJonathan.Adams@Sun.COM ASSERT(tq->tq_nthreads > 0); 13689515SJonathan.Adams@Sun.COM if (--tq->tq_nthreads == 0) 13699515SJonathan.Adams@Sun.COM cv_broadcast(&tq->tq_wait_cv); 13709515SJonathan.Adams@Sun.COM 13719515SJonathan.Adams@Sun.COM /* let the other threads which need to exit know we're done */ 13729515SJonathan.Adams@Sun.COM cv_broadcast(&tq->tq_exit_cv); 13739515SJonathan.Adams@Sun.COM 13749515SJonathan.Adams@Sun.COM /* We're exiting, and therefore no longer active */ 13759515SJonathan.Adams@Sun.COM tq->tq_active--; 13769515SJonathan.Adams@Sun.COM 13770Sstevel@tonic-gate ASSERT(!(tq->tq_flags & TASKQ_CPR_SAFE)); 13780Sstevel@tonic-gate CALLB_CPR_EXIT(&cprinfo); 13790Sstevel@tonic-gate thread_exit(); 13800Sstevel@tonic-gate } 13810Sstevel@tonic-gate 13820Sstevel@tonic-gate /* 13830Sstevel@tonic-gate * Worker per-entry thread for dynamic dispatches. 13840Sstevel@tonic-gate */ 13850Sstevel@tonic-gate static void 13860Sstevel@tonic-gate taskq_d_thread(taskq_ent_t *tqe) 13870Sstevel@tonic-gate { 13880Sstevel@tonic-gate taskq_bucket_t *bucket = tqe->tqent_bucket; 13890Sstevel@tonic-gate taskq_t *tq = bucket->tqbucket_taskq; 13900Sstevel@tonic-gate kmutex_t *lock = &bucket->tqbucket_lock; 13910Sstevel@tonic-gate kcondvar_t *cv = &tqe->tqent_cv; 13920Sstevel@tonic-gate callb_cpr_t cprinfo; 13930Sstevel@tonic-gate clock_t w; 13940Sstevel@tonic-gate 13950Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, lock, callb_generic_cpr, tq->tq_name); 13960Sstevel@tonic-gate 13970Sstevel@tonic-gate mutex_enter(lock); 13980Sstevel@tonic-gate 13990Sstevel@tonic-gate for (;;) { 14000Sstevel@tonic-gate /* 14010Sstevel@tonic-gate * If a task is scheduled (func != NULL), execute it, otherwise 14020Sstevel@tonic-gate * sleep, waiting for a job. 14030Sstevel@tonic-gate */ 14040Sstevel@tonic-gate if (tqe->tqent_func != NULL) { 14050Sstevel@tonic-gate hrtime_t start; 14060Sstevel@tonic-gate hrtime_t end; 14070Sstevel@tonic-gate 14080Sstevel@tonic-gate ASSERT(bucket->tqbucket_nalloc > 0); 14090Sstevel@tonic-gate 14100Sstevel@tonic-gate /* 14110Sstevel@tonic-gate * It is possible to free the entry right away before 14120Sstevel@tonic-gate * actually executing the task so that subsequent 14130Sstevel@tonic-gate * dispatches may immediately reuse it. But this, 14140Sstevel@tonic-gate * effectively, creates a two-length queue in the entry 14150Sstevel@tonic-gate * and may lead to a deadlock if the execution of the 14160Sstevel@tonic-gate * current task depends on the execution of the next 14170Sstevel@tonic-gate * scheduled task. So, we keep the entry busy until the 14180Sstevel@tonic-gate * task is processed. 14190Sstevel@tonic-gate */ 14200Sstevel@tonic-gate 14210Sstevel@tonic-gate mutex_exit(lock); 14220Sstevel@tonic-gate start = gethrtime(); 14230Sstevel@tonic-gate DTRACE_PROBE3(taskq__d__exec__start, taskq_t *, tq, 14240Sstevel@tonic-gate taskq_bucket_t *, bucket, taskq_ent_t *, tqe); 14250Sstevel@tonic-gate tqe->tqent_func(tqe->tqent_arg); 14260Sstevel@tonic-gate DTRACE_PROBE3(taskq__d__exec__end, taskq_t *, tq, 14270Sstevel@tonic-gate taskq_bucket_t *, bucket, taskq_ent_t *, tqe); 14280Sstevel@tonic-gate end = gethrtime(); 14290Sstevel@tonic-gate mutex_enter(lock); 14300Sstevel@tonic-gate bucket->tqbucket_totaltime += end - start; 14310Sstevel@tonic-gate 14320Sstevel@tonic-gate /* 14330Sstevel@tonic-gate * Return the entry to the bucket free list. 14340Sstevel@tonic-gate */ 14350Sstevel@tonic-gate tqe->tqent_func = NULL; 14360Sstevel@tonic-gate TQ_APPEND(bucket->tqbucket_freelist, tqe); 14370Sstevel@tonic-gate bucket->tqbucket_nalloc--; 14380Sstevel@tonic-gate bucket->tqbucket_nfree++; 14390Sstevel@tonic-gate ASSERT(!IS_EMPTY(bucket->tqbucket_freelist)); 14400Sstevel@tonic-gate /* 14410Sstevel@tonic-gate * taskq_wait() waits for nalloc to drop to zero on 14420Sstevel@tonic-gate * tqbucket_cv. 14430Sstevel@tonic-gate */ 14440Sstevel@tonic-gate cv_signal(&bucket->tqbucket_cv); 14450Sstevel@tonic-gate } 14460Sstevel@tonic-gate 14470Sstevel@tonic-gate /* 14480Sstevel@tonic-gate * At this point the entry must be in the bucket free list - 14490Sstevel@tonic-gate * either because it was there initially or because it just 14500Sstevel@tonic-gate * finished executing a task and put itself on the free list. 14510Sstevel@tonic-gate */ 14520Sstevel@tonic-gate ASSERT(bucket->tqbucket_nfree > 0); 14530Sstevel@tonic-gate /* 14540Sstevel@tonic-gate * Go to sleep unless we are closing. 14550Sstevel@tonic-gate * If a thread is sleeping too long, it dies. 14560Sstevel@tonic-gate */ 14570Sstevel@tonic-gate if (! (bucket->tqbucket_flags & TQBUCKET_CLOSE)) { 1458*10889SJonathan.Adams@Sun.COM w = taskq_thread_wait(tq, lock, cv, 1459*10889SJonathan.Adams@Sun.COM &cprinfo, taskq_thread_timeout * hz); 14600Sstevel@tonic-gate } 14610Sstevel@tonic-gate 14620Sstevel@tonic-gate /* 14630Sstevel@tonic-gate * At this point we may be in two different states: 14640Sstevel@tonic-gate * 14650Sstevel@tonic-gate * (1) tqent_func is set which means that a new task is 14660Sstevel@tonic-gate * dispatched and we need to execute it. 14670Sstevel@tonic-gate * 14680Sstevel@tonic-gate * (2) Thread is sleeping for too long or we are closing. In 14690Sstevel@tonic-gate * both cases destroy the thread and the entry. 14700Sstevel@tonic-gate */ 14710Sstevel@tonic-gate 14720Sstevel@tonic-gate /* If func is NULL we should be on the freelist. */ 14730Sstevel@tonic-gate ASSERT((tqe->tqent_func != NULL) || 14740Sstevel@tonic-gate (bucket->tqbucket_nfree > 0)); 14750Sstevel@tonic-gate /* If func is non-NULL we should be allocated */ 14760Sstevel@tonic-gate ASSERT((tqe->tqent_func == NULL) || 14770Sstevel@tonic-gate (bucket->tqbucket_nalloc > 0)); 14780Sstevel@tonic-gate 14790Sstevel@tonic-gate /* Check freelist consistency */ 14800Sstevel@tonic-gate ASSERT((bucket->tqbucket_nfree > 0) || 14810Sstevel@tonic-gate IS_EMPTY(bucket->tqbucket_freelist)); 14820Sstevel@tonic-gate ASSERT((bucket->tqbucket_nfree == 0) || 14830Sstevel@tonic-gate !IS_EMPTY(bucket->tqbucket_freelist)); 14840Sstevel@tonic-gate 14850Sstevel@tonic-gate if ((tqe->tqent_func == NULL) && 14860Sstevel@tonic-gate ((w == -1) || (bucket->tqbucket_flags & TQBUCKET_CLOSE))) { 14870Sstevel@tonic-gate /* 14880Sstevel@tonic-gate * This thread is sleeping for too long or we are 14890Sstevel@tonic-gate * closing - time to die. 14900Sstevel@tonic-gate * Thread creation/destruction happens rarely, 14910Sstevel@tonic-gate * so grabbing the lock is not a big performance issue. 14920Sstevel@tonic-gate * The bucket lock is dropped by CALLB_CPR_EXIT(). 14930Sstevel@tonic-gate */ 14940Sstevel@tonic-gate 14950Sstevel@tonic-gate /* Remove the entry from the free list. */ 14960Sstevel@tonic-gate tqe->tqent_prev->tqent_next = tqe->tqent_next; 14970Sstevel@tonic-gate tqe->tqent_next->tqent_prev = tqe->tqent_prev; 14980Sstevel@tonic-gate ASSERT(bucket->tqbucket_nfree > 0); 14990Sstevel@tonic-gate bucket->tqbucket_nfree--; 15000Sstevel@tonic-gate 15010Sstevel@tonic-gate TQ_STAT(bucket, tqs_tdeaths); 15020Sstevel@tonic-gate cv_signal(&bucket->tqbucket_cv); 15030Sstevel@tonic-gate tqe->tqent_thread = NULL; 15040Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 15050Sstevel@tonic-gate tq->tq_tdeaths++; 15060Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 15070Sstevel@tonic-gate CALLB_CPR_EXIT(&cprinfo); 15080Sstevel@tonic-gate kmem_cache_free(taskq_ent_cache, tqe); 15090Sstevel@tonic-gate thread_exit(); 15100Sstevel@tonic-gate } 15110Sstevel@tonic-gate } 15120Sstevel@tonic-gate } 15130Sstevel@tonic-gate 15140Sstevel@tonic-gate 15150Sstevel@tonic-gate /* 15160Sstevel@tonic-gate * Taskq creation. May sleep for memory. 15170Sstevel@tonic-gate * Always use automatically generated instances to avoid kstat name space 15180Sstevel@tonic-gate * collisions. 15190Sstevel@tonic-gate */ 15200Sstevel@tonic-gate 15210Sstevel@tonic-gate taskq_t * 15220Sstevel@tonic-gate taskq_create(const char *name, int nthreads, pri_t pri, int minalloc, 15230Sstevel@tonic-gate int maxalloc, uint_t flags) 15240Sstevel@tonic-gate { 15250Sstevel@tonic-gate return taskq_create_common(name, 0, nthreads, pri, minalloc, 15260Sstevel@tonic-gate maxalloc, flags | TASKQ_NOINSTANCE); 15270Sstevel@tonic-gate } 15280Sstevel@tonic-gate 15290Sstevel@tonic-gate /* 15300Sstevel@tonic-gate * Create an instance of task queue. It is legal to create task queues with the 15310Sstevel@tonic-gate * same name and different instances. 15320Sstevel@tonic-gate * 15330Sstevel@tonic-gate * taskq_create_instance is used by ddi_taskq_create() where it gets the 15340Sstevel@tonic-gate * instance from ddi_get_instance(). In some cases the instance is not 15350Sstevel@tonic-gate * initialized and is set to -1. This case is handled as if no instance was 15360Sstevel@tonic-gate * passed at all. 15370Sstevel@tonic-gate */ 15380Sstevel@tonic-gate taskq_t * 15390Sstevel@tonic-gate taskq_create_instance(const char *name, int instance, int nthreads, pri_t pri, 15400Sstevel@tonic-gate int minalloc, int maxalloc, uint_t flags) 15410Sstevel@tonic-gate { 15420Sstevel@tonic-gate ASSERT((instance >= 0) || (instance == -1)); 15430Sstevel@tonic-gate 15440Sstevel@tonic-gate if (instance < 0) { 15450Sstevel@tonic-gate flags |= TASKQ_NOINSTANCE; 15460Sstevel@tonic-gate } 15470Sstevel@tonic-gate 15480Sstevel@tonic-gate return (taskq_create_common(name, instance, nthreads, 15490Sstevel@tonic-gate pri, minalloc, maxalloc, flags)); 15500Sstevel@tonic-gate } 15510Sstevel@tonic-gate 15520Sstevel@tonic-gate static taskq_t * 15530Sstevel@tonic-gate taskq_create_common(const char *name, int instance, int nthreads, pri_t pri, 15540Sstevel@tonic-gate int minalloc, int maxalloc, uint_t flags) 15550Sstevel@tonic-gate { 15560Sstevel@tonic-gate taskq_t *tq = kmem_cache_alloc(taskq_cache, KM_SLEEP); 15570Sstevel@tonic-gate uint_t ncpus = ((boot_max_ncpus == -1) ? max_ncpus : boot_max_ncpus); 15580Sstevel@tonic-gate uint_t bsize; /* # of buckets - always power of 2 */ 15599515SJonathan.Adams@Sun.COM int max_nthreads; 15600Sstevel@tonic-gate 15610Sstevel@tonic-gate /* 15629515SJonathan.Adams@Sun.COM * TASKQ_DYNAMIC is incompatible with TASKQ_CPR_SAFE and 15639515SJonathan.Adams@Sun.COM * TASKQ_THREADS_CPU_PCT. 15640Sstevel@tonic-gate */ 15659515SJonathan.Adams@Sun.COM ASSERT(!(flags & TASKQ_DYNAMIC) || 15669515SJonathan.Adams@Sun.COM !(flags & (TASKQ_CPR_SAFE | TASKQ_THREADS_CPU_PCT))); 15679515SJonathan.Adams@Sun.COM /* TASKQ_CPR_SAFE is incompatible with TASKQ_THREADS_CPU_PCT */ 15680Sstevel@tonic-gate 15699515SJonathan.Adams@Sun.COM ASSERT(!(flags & TASKQ_CPR_SAFE) || !(flags & TASKQ_THREADS_CPU_PCT)); 15700Sstevel@tonic-gate 15710Sstevel@tonic-gate bsize = 1 << (highbit(ncpus) - 1); 15720Sstevel@tonic-gate ASSERT(bsize >= 1); 15730Sstevel@tonic-gate bsize = MIN(bsize, taskq_maxbuckets); 15740Sstevel@tonic-gate 15759515SJonathan.Adams@Sun.COM if (flags & TASKQ_DYNAMIC) { 15769515SJonathan.Adams@Sun.COM ASSERT3S(nthreads, >=, 1); 15779515SJonathan.Adams@Sun.COM tq->tq_maxsize = nthreads; 15789515SJonathan.Adams@Sun.COM 15799515SJonathan.Adams@Sun.COM /* For dynamic task queues use just one backup thread */ 15809515SJonathan.Adams@Sun.COM nthreads = max_nthreads = 1; 15819515SJonathan.Adams@Sun.COM 15829515SJonathan.Adams@Sun.COM } else if (!(flags & TASKQ_THREADS_CPU_PCT)) { 15839515SJonathan.Adams@Sun.COM ASSERT3S(nthreads, >=, 1); 15849515SJonathan.Adams@Sun.COM max_nthreads = nthreads; 15859515SJonathan.Adams@Sun.COM } else { 15869515SJonathan.Adams@Sun.COM uint_t pct; 15879515SJonathan.Adams@Sun.COM ASSERT3S(nthreads, >=, 0); 15889515SJonathan.Adams@Sun.COM pct = nthreads; 15890Sstevel@tonic-gate 15909515SJonathan.Adams@Sun.COM if (pct > taskq_cpupct_max_percent) 15919515SJonathan.Adams@Sun.COM pct = taskq_cpupct_max_percent; 15929515SJonathan.Adams@Sun.COM 15939515SJonathan.Adams@Sun.COM tq->tq_threads_ncpus_pct = pct; 15949515SJonathan.Adams@Sun.COM nthreads = TASKQ_THREADS_PCT(ncpus_online, pct); 15959515SJonathan.Adams@Sun.COM max_nthreads = TASKQ_THREADS_PCT(max_ncpus, pct); 15969515SJonathan.Adams@Sun.COM } 15970Sstevel@tonic-gate 15989515SJonathan.Adams@Sun.COM if (max_nthreads < taskq_minimum_nthreads_max) 15999515SJonathan.Adams@Sun.COM max_nthreads = taskq_minimum_nthreads_max; 16009515SJonathan.Adams@Sun.COM 16019515SJonathan.Adams@Sun.COM /* 16029515SJonathan.Adams@Sun.COM * Make sure the name is 0-terminated, and conforms to the rules for 16039515SJonathan.Adams@Sun.COM * C indentifiers 16049515SJonathan.Adams@Sun.COM */ 16050Sstevel@tonic-gate (void) strncpy(tq->tq_name, name, TASKQ_NAMELEN + 1); 16069515SJonathan.Adams@Sun.COM strident_canon(tq->tq_name, TASKQ_NAMELEN + 1); 16070Sstevel@tonic-gate 16089515SJonathan.Adams@Sun.COM tq->tq_flags = flags | TASKQ_CHANGING; 16099515SJonathan.Adams@Sun.COM tq->tq_active = 0; 16100Sstevel@tonic-gate tq->tq_instance = instance; 16119515SJonathan.Adams@Sun.COM tq->tq_nthreads_target = nthreads; 16129515SJonathan.Adams@Sun.COM tq->tq_nthreads_max = max_nthreads; 16130Sstevel@tonic-gate tq->tq_minalloc = minalloc; 16140Sstevel@tonic-gate tq->tq_maxalloc = maxalloc; 16150Sstevel@tonic-gate tq->tq_nbuckets = bsize; 16160Sstevel@tonic-gate tq->tq_pri = pri; 16170Sstevel@tonic-gate 16189515SJonathan.Adams@Sun.COM if (max_nthreads > 1) 16199515SJonathan.Adams@Sun.COM tq->tq_threadlist = kmem_alloc( 16209515SJonathan.Adams@Sun.COM sizeof (kthread_t *) * max_nthreads, KM_SLEEP); 16219515SJonathan.Adams@Sun.COM 16229515SJonathan.Adams@Sun.COM /* Add the taskq to the list of CPU_PCT taskqs */ 16239515SJonathan.Adams@Sun.COM if (flags & TASKQ_THREADS_CPU_PCT) { 16249515SJonathan.Adams@Sun.COM taskq_cpupct_ent_t *tpp = kmem_zalloc(sizeof (*tpp), KM_SLEEP); 16259515SJonathan.Adams@Sun.COM 16269515SJonathan.Adams@Sun.COM list_link_init(&tpp->tp_link); 16279515SJonathan.Adams@Sun.COM tpp->tp_taskq = tq; 16289515SJonathan.Adams@Sun.COM 16299515SJonathan.Adams@Sun.COM mutex_enter(&taskq_cpupct_lock); 16309515SJonathan.Adams@Sun.COM list_insert_tail(&taskq_cpupct_list, tpp); 16319515SJonathan.Adams@Sun.COM /* reset our target, to avoid race conditions */ 16329515SJonathan.Adams@Sun.COM tq->tq_nthreads_target = TASKQ_THREADS_PCT(ncpus_online, 16339515SJonathan.Adams@Sun.COM tq->tq_threads_ncpus_pct); 16349515SJonathan.Adams@Sun.COM mutex_exit(&taskq_cpupct_lock); 16350Sstevel@tonic-gate } 16360Sstevel@tonic-gate 16379515SJonathan.Adams@Sun.COM mutex_enter(&tq->tq_lock); 16389515SJonathan.Adams@Sun.COM if (flags & TASKQ_PREPOPULATE) { 16399515SJonathan.Adams@Sun.COM while (minalloc-- > 0) 16409515SJonathan.Adams@Sun.COM taskq_ent_free(tq, taskq_ent_alloc(tq, TQ_SLEEP)); 16419515SJonathan.Adams@Sun.COM } 16420Sstevel@tonic-gate 16439515SJonathan.Adams@Sun.COM /* create the first thread; if more are needed, it'll create them */ 16449515SJonathan.Adams@Sun.COM taskq_thread_create(tq); 16459515SJonathan.Adams@Sun.COM mutex_exit(&tq->tq_lock); 16460Sstevel@tonic-gate 16470Sstevel@tonic-gate if (flags & TASKQ_DYNAMIC) { 16480Sstevel@tonic-gate taskq_bucket_t *bucket = kmem_zalloc(sizeof (taskq_bucket_t) * 16490Sstevel@tonic-gate bsize, KM_SLEEP); 16500Sstevel@tonic-gate int b_id; 16510Sstevel@tonic-gate 16520Sstevel@tonic-gate tq->tq_buckets = bucket; 16530Sstevel@tonic-gate 16540Sstevel@tonic-gate /* Initialize each bucket */ 16550Sstevel@tonic-gate for (b_id = 0; b_id < bsize; b_id++, bucket++) { 16560Sstevel@tonic-gate mutex_init(&bucket->tqbucket_lock, NULL, MUTEX_DEFAULT, 16570Sstevel@tonic-gate NULL); 16580Sstevel@tonic-gate cv_init(&bucket->tqbucket_cv, NULL, CV_DEFAULT, NULL); 16590Sstevel@tonic-gate bucket->tqbucket_taskq = tq; 16600Sstevel@tonic-gate bucket->tqbucket_freelist.tqent_next = 16610Sstevel@tonic-gate bucket->tqbucket_freelist.tqent_prev = 16620Sstevel@tonic-gate &bucket->tqbucket_freelist; 16630Sstevel@tonic-gate if (flags & TASKQ_PREPOPULATE) 16640Sstevel@tonic-gate taskq_bucket_extend(bucket); 16650Sstevel@tonic-gate } 16660Sstevel@tonic-gate } 16670Sstevel@tonic-gate 16680Sstevel@tonic-gate /* 16690Sstevel@tonic-gate * Install kstats. 16700Sstevel@tonic-gate * We have two cases: 16710Sstevel@tonic-gate * 1) Instance is provided to taskq_create_instance(). In this case it 16720Sstevel@tonic-gate * should be >= 0 and we use it. 16730Sstevel@tonic-gate * 16740Sstevel@tonic-gate * 2) Instance is not provided and is automatically generated 16750Sstevel@tonic-gate */ 16760Sstevel@tonic-gate if (flags & TASKQ_NOINSTANCE) { 16770Sstevel@tonic-gate instance = tq->tq_instance = 16780Sstevel@tonic-gate (int)(uintptr_t)vmem_alloc(taskq_id_arena, 1, VM_SLEEP); 16790Sstevel@tonic-gate } 16800Sstevel@tonic-gate 16810Sstevel@tonic-gate if (flags & TASKQ_DYNAMIC) { 16820Sstevel@tonic-gate if ((tq->tq_kstat = kstat_create("unix", instance, 16839515SJonathan.Adams@Sun.COM tq->tq_name, "taskq_d", KSTAT_TYPE_NAMED, 16849515SJonathan.Adams@Sun.COM sizeof (taskq_d_kstat) / sizeof (kstat_named_t), 16859515SJonathan.Adams@Sun.COM KSTAT_FLAG_VIRTUAL)) != NULL) { 16860Sstevel@tonic-gate tq->tq_kstat->ks_lock = &taskq_d_kstat_lock; 16870Sstevel@tonic-gate tq->tq_kstat->ks_data = &taskq_d_kstat; 16880Sstevel@tonic-gate tq->tq_kstat->ks_update = taskq_d_kstat_update; 16890Sstevel@tonic-gate tq->tq_kstat->ks_private = tq; 16900Sstevel@tonic-gate kstat_install(tq->tq_kstat); 16910Sstevel@tonic-gate } 16920Sstevel@tonic-gate } else { 16930Sstevel@tonic-gate if ((tq->tq_kstat = kstat_create("unix", instance, tq->tq_name, 16949515SJonathan.Adams@Sun.COM "taskq", KSTAT_TYPE_NAMED, 16959515SJonathan.Adams@Sun.COM sizeof (taskq_kstat) / sizeof (kstat_named_t), 16969515SJonathan.Adams@Sun.COM KSTAT_FLAG_VIRTUAL)) != NULL) { 16970Sstevel@tonic-gate tq->tq_kstat->ks_lock = &taskq_kstat_lock; 16980Sstevel@tonic-gate tq->tq_kstat->ks_data = &taskq_kstat; 16990Sstevel@tonic-gate tq->tq_kstat->ks_update = taskq_kstat_update; 17000Sstevel@tonic-gate tq->tq_kstat->ks_private = tq; 17010Sstevel@tonic-gate kstat_install(tq->tq_kstat); 17020Sstevel@tonic-gate } 17030Sstevel@tonic-gate } 17040Sstevel@tonic-gate 17050Sstevel@tonic-gate return (tq); 17060Sstevel@tonic-gate } 17070Sstevel@tonic-gate 17080Sstevel@tonic-gate /* 17090Sstevel@tonic-gate * taskq_destroy(). 17100Sstevel@tonic-gate * 17110Sstevel@tonic-gate * Assumes: by the time taskq_destroy is called no one will use this task queue 17120Sstevel@tonic-gate * in any way and no one will try to dispatch entries in it. 17130Sstevel@tonic-gate */ 17140Sstevel@tonic-gate void 17150Sstevel@tonic-gate taskq_destroy(taskq_t *tq) 17160Sstevel@tonic-gate { 17170Sstevel@tonic-gate taskq_bucket_t *b = tq->tq_buckets; 17180Sstevel@tonic-gate int bid = 0; 17190Sstevel@tonic-gate 17200Sstevel@tonic-gate ASSERT(! (tq->tq_flags & TASKQ_CPR_SAFE)); 17210Sstevel@tonic-gate 17220Sstevel@tonic-gate /* 17230Sstevel@tonic-gate * Destroy kstats. 17240Sstevel@tonic-gate */ 17250Sstevel@tonic-gate if (tq->tq_kstat != NULL) { 17260Sstevel@tonic-gate kstat_delete(tq->tq_kstat); 17270Sstevel@tonic-gate tq->tq_kstat = NULL; 17280Sstevel@tonic-gate } 17290Sstevel@tonic-gate 17300Sstevel@tonic-gate /* 17310Sstevel@tonic-gate * Destroy instance if needed. 17320Sstevel@tonic-gate */ 17330Sstevel@tonic-gate if (tq->tq_flags & TASKQ_NOINSTANCE) { 17340Sstevel@tonic-gate vmem_free(taskq_id_arena, (void *)(uintptr_t)(tq->tq_instance), 17350Sstevel@tonic-gate 1); 17360Sstevel@tonic-gate tq->tq_instance = 0; 17370Sstevel@tonic-gate } 17380Sstevel@tonic-gate 17390Sstevel@tonic-gate /* 17409515SJonathan.Adams@Sun.COM * Unregister from the cpupct list. 17419515SJonathan.Adams@Sun.COM */ 17429515SJonathan.Adams@Sun.COM if (tq->tq_flags & TASKQ_THREADS_CPU_PCT) { 17439515SJonathan.Adams@Sun.COM taskq_cpupct_ent_t *tpp; 17449515SJonathan.Adams@Sun.COM 17459515SJonathan.Adams@Sun.COM mutex_enter(&taskq_cpupct_lock); 17469515SJonathan.Adams@Sun.COM for (tpp = list_head(&taskq_cpupct_list); tpp != NULL; 17479515SJonathan.Adams@Sun.COM tpp = list_next(&taskq_cpupct_list, tpp)) { 17489515SJonathan.Adams@Sun.COM if (tpp->tp_taskq == tq) 17499515SJonathan.Adams@Sun.COM break; 17509515SJonathan.Adams@Sun.COM } 17519515SJonathan.Adams@Sun.COM ASSERT3P(tpp, !=, NULL); 17529515SJonathan.Adams@Sun.COM 17539515SJonathan.Adams@Sun.COM list_remove(&taskq_cpupct_list, tpp); 17549515SJonathan.Adams@Sun.COM mutex_exit(&taskq_cpupct_lock); 17559515SJonathan.Adams@Sun.COM 17569515SJonathan.Adams@Sun.COM kmem_free(tpp, sizeof (*tpp)); 17579515SJonathan.Adams@Sun.COM } 17589515SJonathan.Adams@Sun.COM 17599515SJonathan.Adams@Sun.COM /* 17600Sstevel@tonic-gate * Wait for any pending entries to complete. 17610Sstevel@tonic-gate */ 17620Sstevel@tonic-gate taskq_wait(tq); 17630Sstevel@tonic-gate 17640Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 17650Sstevel@tonic-gate ASSERT((tq->tq_task.tqent_next == &tq->tq_task) && 17660Sstevel@tonic-gate (tq->tq_active == 0)); 17670Sstevel@tonic-gate 17689515SJonathan.Adams@Sun.COM /* notify all the threads that they need to exit */ 17699515SJonathan.Adams@Sun.COM tq->tq_nthreads_target = 0; 17700Sstevel@tonic-gate 17719515SJonathan.Adams@Sun.COM tq->tq_flags |= TASKQ_CHANGING; 17720Sstevel@tonic-gate cv_broadcast(&tq->tq_dispatch_cv); 17739515SJonathan.Adams@Sun.COM cv_broadcast(&tq->tq_exit_cv); 17749515SJonathan.Adams@Sun.COM 17750Sstevel@tonic-gate while (tq->tq_nthreads != 0) 17760Sstevel@tonic-gate cv_wait(&tq->tq_wait_cv, &tq->tq_lock); 17770Sstevel@tonic-gate 17789515SJonathan.Adams@Sun.COM if (tq->tq_nthreads_max != 1) 17799515SJonathan.Adams@Sun.COM kmem_free(tq->tq_threadlist, sizeof (kthread_t *) * 17809515SJonathan.Adams@Sun.COM tq->tq_nthreads_max); 17819515SJonathan.Adams@Sun.COM 17820Sstevel@tonic-gate tq->tq_minalloc = 0; 17830Sstevel@tonic-gate while (tq->tq_nalloc != 0) 17840Sstevel@tonic-gate taskq_ent_free(tq, taskq_ent_alloc(tq, TQ_SLEEP)); 17850Sstevel@tonic-gate 17860Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 17870Sstevel@tonic-gate 17880Sstevel@tonic-gate /* 17890Sstevel@tonic-gate * Mark each bucket as closing and wakeup all sleeping threads. 17900Sstevel@tonic-gate */ 17910Sstevel@tonic-gate for (; (b != NULL) && (bid < tq->tq_nbuckets); b++, bid++) { 17920Sstevel@tonic-gate taskq_ent_t *tqe; 17930Sstevel@tonic-gate 17940Sstevel@tonic-gate mutex_enter(&b->tqbucket_lock); 17950Sstevel@tonic-gate 17960Sstevel@tonic-gate b->tqbucket_flags |= TQBUCKET_CLOSE; 17970Sstevel@tonic-gate /* Wakeup all sleeping threads */ 17980Sstevel@tonic-gate 17990Sstevel@tonic-gate for (tqe = b->tqbucket_freelist.tqent_next; 18000Sstevel@tonic-gate tqe != &b->tqbucket_freelist; tqe = tqe->tqent_next) 18010Sstevel@tonic-gate cv_signal(&tqe->tqent_cv); 18020Sstevel@tonic-gate 18030Sstevel@tonic-gate ASSERT(b->tqbucket_nalloc == 0); 18040Sstevel@tonic-gate 18050Sstevel@tonic-gate /* 18060Sstevel@tonic-gate * At this point we waited for all pending jobs to complete (in 18070Sstevel@tonic-gate * both the task queue and the bucket and no new jobs should 18080Sstevel@tonic-gate * arrive. Wait for all threads to die. 18090Sstevel@tonic-gate */ 18100Sstevel@tonic-gate while (b->tqbucket_nfree > 0) 18110Sstevel@tonic-gate cv_wait(&b->tqbucket_cv, &b->tqbucket_lock); 18120Sstevel@tonic-gate mutex_exit(&b->tqbucket_lock); 18130Sstevel@tonic-gate mutex_destroy(&b->tqbucket_lock); 18140Sstevel@tonic-gate cv_destroy(&b->tqbucket_cv); 18150Sstevel@tonic-gate } 18160Sstevel@tonic-gate 18170Sstevel@tonic-gate if (tq->tq_buckets != NULL) { 18180Sstevel@tonic-gate ASSERT(tq->tq_flags & TASKQ_DYNAMIC); 18190Sstevel@tonic-gate kmem_free(tq->tq_buckets, 18200Sstevel@tonic-gate sizeof (taskq_bucket_t) * tq->tq_nbuckets); 18210Sstevel@tonic-gate 18220Sstevel@tonic-gate /* Cleanup fields before returning tq to the cache */ 18230Sstevel@tonic-gate tq->tq_buckets = NULL; 18240Sstevel@tonic-gate tq->tq_tcreates = 0; 18250Sstevel@tonic-gate tq->tq_tdeaths = 0; 18260Sstevel@tonic-gate } else { 18270Sstevel@tonic-gate ASSERT(!(tq->tq_flags & TASKQ_DYNAMIC)); 18280Sstevel@tonic-gate } 18290Sstevel@tonic-gate 18309515SJonathan.Adams@Sun.COM tq->tq_threads_ncpus_pct = 0; 18310Sstevel@tonic-gate tq->tq_totaltime = 0; 18320Sstevel@tonic-gate tq->tq_tasks = 0; 18330Sstevel@tonic-gate tq->tq_maxtasks = 0; 18340Sstevel@tonic-gate tq->tq_executed = 0; 18350Sstevel@tonic-gate kmem_cache_free(taskq_cache, tq); 18360Sstevel@tonic-gate } 18370Sstevel@tonic-gate 18380Sstevel@tonic-gate /* 18390Sstevel@tonic-gate * Extend a bucket with a new entry on the free list and attach a worker thread 18400Sstevel@tonic-gate * to it. 18410Sstevel@tonic-gate * 18420Sstevel@tonic-gate * Argument: pointer to the bucket. 18430Sstevel@tonic-gate * 18440Sstevel@tonic-gate * This function may quietly fail. It is only used by taskq_dispatch() which 18450Sstevel@tonic-gate * handles such failures properly. 18460Sstevel@tonic-gate */ 18470Sstevel@tonic-gate static void 18480Sstevel@tonic-gate taskq_bucket_extend(void *arg) 18490Sstevel@tonic-gate { 18500Sstevel@tonic-gate taskq_ent_t *tqe; 18510Sstevel@tonic-gate taskq_bucket_t *b = (taskq_bucket_t *)arg; 18520Sstevel@tonic-gate taskq_t *tq = b->tqbucket_taskq; 18530Sstevel@tonic-gate int nthreads; 18540Sstevel@tonic-gate 18550Sstevel@tonic-gate if (! ENOUGH_MEMORY()) { 18560Sstevel@tonic-gate TQ_STAT(b, tqs_nomem); 18570Sstevel@tonic-gate return; 18580Sstevel@tonic-gate } 18590Sstevel@tonic-gate 18600Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 18610Sstevel@tonic-gate 18620Sstevel@tonic-gate /* 18630Sstevel@tonic-gate * Observe global taskq limits on the number of threads. 18640Sstevel@tonic-gate */ 18650Sstevel@tonic-gate if (tq->tq_tcreates++ - tq->tq_tdeaths > tq->tq_maxsize) { 18660Sstevel@tonic-gate tq->tq_tcreates--; 18670Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 18680Sstevel@tonic-gate return; 18690Sstevel@tonic-gate } 18700Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 18710Sstevel@tonic-gate 18720Sstevel@tonic-gate tqe = kmem_cache_alloc(taskq_ent_cache, KM_NOSLEEP); 18730Sstevel@tonic-gate 18740Sstevel@tonic-gate if (tqe == NULL) { 18750Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 18760Sstevel@tonic-gate TQ_STAT(b, tqs_nomem); 18770Sstevel@tonic-gate tq->tq_tcreates--; 18780Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 18790Sstevel@tonic-gate return; 18800Sstevel@tonic-gate } 18810Sstevel@tonic-gate 18820Sstevel@tonic-gate ASSERT(tqe->tqent_thread == NULL); 18830Sstevel@tonic-gate 18840Sstevel@tonic-gate tqe->tqent_bucket = b; 18850Sstevel@tonic-gate 18860Sstevel@tonic-gate /* 18870Sstevel@tonic-gate * Create a thread in a TS_STOPPED state first. If it is successfully 18880Sstevel@tonic-gate * created, place the entry on the free list and start the thread. 18890Sstevel@tonic-gate */ 18900Sstevel@tonic-gate tqe->tqent_thread = thread_create(NULL, 0, taskq_d_thread, tqe, 18910Sstevel@tonic-gate 0, &p0, TS_STOPPED, tq->tq_pri); 18920Sstevel@tonic-gate 18930Sstevel@tonic-gate /* 18940Sstevel@tonic-gate * Once the entry is ready, link it to the the bucket free list. 18950Sstevel@tonic-gate */ 18960Sstevel@tonic-gate mutex_enter(&b->tqbucket_lock); 18970Sstevel@tonic-gate tqe->tqent_func = NULL; 18980Sstevel@tonic-gate TQ_APPEND(b->tqbucket_freelist, tqe); 18990Sstevel@tonic-gate b->tqbucket_nfree++; 19000Sstevel@tonic-gate TQ_STAT(b, tqs_tcreates); 19010Sstevel@tonic-gate 19020Sstevel@tonic-gate #if TASKQ_STATISTIC 19030Sstevel@tonic-gate nthreads = b->tqbucket_stat.tqs_tcreates - 19040Sstevel@tonic-gate b->tqbucket_stat.tqs_tdeaths; 19050Sstevel@tonic-gate b->tqbucket_stat.tqs_maxthreads = MAX(nthreads, 19060Sstevel@tonic-gate b->tqbucket_stat.tqs_maxthreads); 19070Sstevel@tonic-gate #endif 19080Sstevel@tonic-gate 19090Sstevel@tonic-gate mutex_exit(&b->tqbucket_lock); 19100Sstevel@tonic-gate /* 19110Sstevel@tonic-gate * Start the stopped thread. 19120Sstevel@tonic-gate */ 19130Sstevel@tonic-gate thread_lock(tqe->tqent_thread); 19140Sstevel@tonic-gate tqe->tqent_thread->t_taskq = tq; 19150Sstevel@tonic-gate tqe->tqent_thread->t_schedflag |= TS_ALLSTART; 19160Sstevel@tonic-gate setrun_locked(tqe->tqent_thread); 19170Sstevel@tonic-gate thread_unlock(tqe->tqent_thread); 19180Sstevel@tonic-gate } 19190Sstevel@tonic-gate 19200Sstevel@tonic-gate static int 19210Sstevel@tonic-gate taskq_kstat_update(kstat_t *ksp, int rw) 19220Sstevel@tonic-gate { 19230Sstevel@tonic-gate struct taskq_kstat *tqsp = &taskq_kstat; 19240Sstevel@tonic-gate taskq_t *tq = ksp->ks_private; 19250Sstevel@tonic-gate 19260Sstevel@tonic-gate if (rw == KSTAT_WRITE) 19270Sstevel@tonic-gate return (EACCES); 19280Sstevel@tonic-gate 19290Sstevel@tonic-gate tqsp->tq_tasks.value.ui64 = tq->tq_tasks; 19300Sstevel@tonic-gate tqsp->tq_executed.value.ui64 = tq->tq_executed; 19310Sstevel@tonic-gate tqsp->tq_maxtasks.value.ui64 = tq->tq_maxtasks; 19320Sstevel@tonic-gate tqsp->tq_totaltime.value.ui64 = tq->tq_totaltime; 19330Sstevel@tonic-gate tqsp->tq_nactive.value.ui64 = tq->tq_active; 19340Sstevel@tonic-gate tqsp->tq_nalloc.value.ui64 = tq->tq_nalloc; 19350Sstevel@tonic-gate tqsp->tq_pri.value.ui64 = tq->tq_pri; 19360Sstevel@tonic-gate tqsp->tq_nthreads.value.ui64 = tq->tq_nthreads; 19370Sstevel@tonic-gate return (0); 19380Sstevel@tonic-gate } 19390Sstevel@tonic-gate 19400Sstevel@tonic-gate static int 19410Sstevel@tonic-gate taskq_d_kstat_update(kstat_t *ksp, int rw) 19420Sstevel@tonic-gate { 19430Sstevel@tonic-gate struct taskq_d_kstat *tqsp = &taskq_d_kstat; 19440Sstevel@tonic-gate taskq_t *tq = ksp->ks_private; 19450Sstevel@tonic-gate taskq_bucket_t *b = tq->tq_buckets; 19460Sstevel@tonic-gate int bid = 0; 19470Sstevel@tonic-gate 19480Sstevel@tonic-gate if (rw == KSTAT_WRITE) 19490Sstevel@tonic-gate return (EACCES); 19500Sstevel@tonic-gate 19510Sstevel@tonic-gate ASSERT(tq->tq_flags & TASKQ_DYNAMIC); 19520Sstevel@tonic-gate 19530Sstevel@tonic-gate tqsp->tqd_btasks.value.ui64 = tq->tq_tasks; 19540Sstevel@tonic-gate tqsp->tqd_bexecuted.value.ui64 = tq->tq_executed; 19550Sstevel@tonic-gate tqsp->tqd_bmaxtasks.value.ui64 = tq->tq_maxtasks; 19560Sstevel@tonic-gate tqsp->tqd_bnalloc.value.ui64 = tq->tq_nalloc; 19570Sstevel@tonic-gate tqsp->tqd_bnactive.value.ui64 = tq->tq_active; 19580Sstevel@tonic-gate tqsp->tqd_btotaltime.value.ui64 = tq->tq_totaltime; 19590Sstevel@tonic-gate tqsp->tqd_pri.value.ui64 = tq->tq_pri; 19600Sstevel@tonic-gate 19610Sstevel@tonic-gate tqsp->tqd_hits.value.ui64 = 0; 19620Sstevel@tonic-gate tqsp->tqd_misses.value.ui64 = 0; 19630Sstevel@tonic-gate tqsp->tqd_overflows.value.ui64 = 0; 19640Sstevel@tonic-gate tqsp->tqd_tcreates.value.ui64 = 0; 19650Sstevel@tonic-gate tqsp->tqd_tdeaths.value.ui64 = 0; 19660Sstevel@tonic-gate tqsp->tqd_maxthreads.value.ui64 = 0; 19670Sstevel@tonic-gate tqsp->tqd_nomem.value.ui64 = 0; 19680Sstevel@tonic-gate tqsp->tqd_disptcreates.value.ui64 = 0; 19690Sstevel@tonic-gate tqsp->tqd_totaltime.value.ui64 = 0; 19700Sstevel@tonic-gate tqsp->tqd_nalloc.value.ui64 = 0; 19710Sstevel@tonic-gate tqsp->tqd_nfree.value.ui64 = 0; 19720Sstevel@tonic-gate 19730Sstevel@tonic-gate for (; (b != NULL) && (bid < tq->tq_nbuckets); b++, bid++) { 19740Sstevel@tonic-gate tqsp->tqd_hits.value.ui64 += b->tqbucket_stat.tqs_hits; 19750Sstevel@tonic-gate tqsp->tqd_misses.value.ui64 += b->tqbucket_stat.tqs_misses; 19760Sstevel@tonic-gate tqsp->tqd_overflows.value.ui64 += b->tqbucket_stat.tqs_overflow; 19770Sstevel@tonic-gate tqsp->tqd_tcreates.value.ui64 += b->tqbucket_stat.tqs_tcreates; 19780Sstevel@tonic-gate tqsp->tqd_tdeaths.value.ui64 += b->tqbucket_stat.tqs_tdeaths; 19790Sstevel@tonic-gate tqsp->tqd_maxthreads.value.ui64 += 19800Sstevel@tonic-gate b->tqbucket_stat.tqs_maxthreads; 19810Sstevel@tonic-gate tqsp->tqd_nomem.value.ui64 += b->tqbucket_stat.tqs_nomem; 19820Sstevel@tonic-gate tqsp->tqd_disptcreates.value.ui64 += 19830Sstevel@tonic-gate b->tqbucket_stat.tqs_disptcreates; 19840Sstevel@tonic-gate tqsp->tqd_totaltime.value.ui64 += b->tqbucket_totaltime; 19850Sstevel@tonic-gate tqsp->tqd_nalloc.value.ui64 += b->tqbucket_nalloc; 19860Sstevel@tonic-gate tqsp->tqd_nfree.value.ui64 += b->tqbucket_nfree; 19870Sstevel@tonic-gate } 19880Sstevel@tonic-gate return (0); 19890Sstevel@tonic-gate } 1990