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 /* 22*11474SJonathan.Adams@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate /* 270Sstevel@tonic-gate * Kernel task queues: general-purpose asynchronous task scheduling. 280Sstevel@tonic-gate * 290Sstevel@tonic-gate * A common problem in kernel programming is the need to schedule tasks 300Sstevel@tonic-gate * to be performed later, by another thread. There are several reasons 310Sstevel@tonic-gate * you may want or need to do this: 320Sstevel@tonic-gate * 330Sstevel@tonic-gate * (1) The task isn't time-critical, but your current code path is. 340Sstevel@tonic-gate * 350Sstevel@tonic-gate * (2) The task may require grabbing locks that you already hold. 360Sstevel@tonic-gate * 370Sstevel@tonic-gate * (3) The task may need to block (e.g. to wait for memory), but you 380Sstevel@tonic-gate * cannot block in your current context. 390Sstevel@tonic-gate * 400Sstevel@tonic-gate * (4) Your code path can't complete because of some condition, but you can't 410Sstevel@tonic-gate * sleep or fail, so you queue the task for later execution when condition 420Sstevel@tonic-gate * disappears. 430Sstevel@tonic-gate * 440Sstevel@tonic-gate * (5) You just want a simple way to launch multiple tasks in parallel. 450Sstevel@tonic-gate * 460Sstevel@tonic-gate * Task queues provide such a facility. In its simplest form (used when 470Sstevel@tonic-gate * performance is not a critical consideration) a task queue consists of a 480Sstevel@tonic-gate * single list of tasks, together with one or more threads to service the 490Sstevel@tonic-gate * list. There are some cases when this simple queue is not sufficient: 500Sstevel@tonic-gate * 510Sstevel@tonic-gate * (1) The task queues are very hot and there is a need to avoid data and lock 520Sstevel@tonic-gate * contention over global resources. 530Sstevel@tonic-gate * 540Sstevel@tonic-gate * (2) Some tasks may depend on other tasks to complete, so they can't be put in 550Sstevel@tonic-gate * the same list managed by the same thread. 560Sstevel@tonic-gate * 570Sstevel@tonic-gate * (3) Some tasks may block for a long time, and this should not block other 5811173SJonathan.Adams@Sun.COM * tasks in the queue. 590Sstevel@tonic-gate * 600Sstevel@tonic-gate * To provide useful service in such cases we define a "dynamic task queue" 610Sstevel@tonic-gate * which has an individual thread for each of the tasks. These threads are 620Sstevel@tonic-gate * dynamically created as they are needed and destroyed when they are not in 630Sstevel@tonic-gate * use. The API for managing task pools is the same as for managing task queues 640Sstevel@tonic-gate * with the exception of a taskq creation flag TASKQ_DYNAMIC which tells that 650Sstevel@tonic-gate * dynamic task pool behavior is desired. 660Sstevel@tonic-gate * 670Sstevel@tonic-gate * Dynamic task queues may also place tasks in the normal queue (called "backing 680Sstevel@tonic-gate * queue") when task pool runs out of resources. Users of task queues may 690Sstevel@tonic-gate * disallow such queued scheduling by specifying TQ_NOQUEUE in the dispatch 700Sstevel@tonic-gate * flags. 710Sstevel@tonic-gate * 720Sstevel@tonic-gate * The backing task queue is also used for scheduling internal tasks needed for 730Sstevel@tonic-gate * dynamic task queue maintenance. 740Sstevel@tonic-gate * 759515SJonathan.Adams@Sun.COM * INTERFACES ================================================================== 760Sstevel@tonic-gate * 7711173SJonathan.Adams@Sun.COM * taskq_t *taskq_create(name, nthreads, pri, minalloc, maxall, flags); 780Sstevel@tonic-gate * 790Sstevel@tonic-gate * Create a taskq with specified properties. 800Sstevel@tonic-gate * Possible 'flags': 810Sstevel@tonic-gate * 820Sstevel@tonic-gate * TASKQ_DYNAMIC: Create task pool for task management. If this flag is 839515SJonathan.Adams@Sun.COM * specified, 'nthreads' specifies the maximum number of threads in 840Sstevel@tonic-gate * the task queue. Task execution order for dynamic task queues is 850Sstevel@tonic-gate * not predictable. 860Sstevel@tonic-gate * 870Sstevel@tonic-gate * If this flag is not specified (default case) a 889515SJonathan.Adams@Sun.COM * single-list task queue is created with 'nthreads' threads 899515SJonathan.Adams@Sun.COM * servicing it. Entries in this queue are managed by 909515SJonathan.Adams@Sun.COM * taskq_ent_alloc() and taskq_ent_free() which try to keep the 919515SJonathan.Adams@Sun.COM * task population between 'minalloc' and 'maxalloc', but the 920Sstevel@tonic-gate * latter limit is only advisory for TQ_SLEEP dispatches and the 930Sstevel@tonic-gate * former limit is only advisory for TQ_NOALLOC dispatches. If 940Sstevel@tonic-gate * TASKQ_PREPOPULATE is set in 'flags', the taskq will be 950Sstevel@tonic-gate * prepopulated with 'minalloc' task structures. 960Sstevel@tonic-gate * 970Sstevel@tonic-gate * Since non-DYNAMIC taskqs are queues, tasks are guaranteed to be 980Sstevel@tonic-gate * executed in the order they are scheduled if nthreads == 1. 990Sstevel@tonic-gate * If nthreads > 1, task execution order is not predictable. 1000Sstevel@tonic-gate * 1010Sstevel@tonic-gate * TASKQ_PREPOPULATE: Prepopulate task queue with threads. 1020Sstevel@tonic-gate * Also prepopulate the task queue with 'minalloc' task structures. 1030Sstevel@tonic-gate * 1049515SJonathan.Adams@Sun.COM * TASKQ_THREADS_CPU_PCT: This flag specifies that 'nthreads' should be 1059515SJonathan.Adams@Sun.COM * interpreted as a percentage of the # of online CPUs on the 1069515SJonathan.Adams@Sun.COM * system. The taskq subsystem will automatically adjust the 1079515SJonathan.Adams@Sun.COM * number of threads in the taskq in response to CPU online 1089515SJonathan.Adams@Sun.COM * and offline events, to keep the ratio. nthreads must be in 1099515SJonathan.Adams@Sun.COM * the range [0,100]. 1109515SJonathan.Adams@Sun.COM * 1119515SJonathan.Adams@Sun.COM * The calculation used is: 1129515SJonathan.Adams@Sun.COM * 1139515SJonathan.Adams@Sun.COM * MAX((ncpus_online * percentage)/100, 1) 1149515SJonathan.Adams@Sun.COM * 1159515SJonathan.Adams@Sun.COM * This flag is not supported for DYNAMIC task queues. 1169515SJonathan.Adams@Sun.COM * This flag is not compatible with TASKQ_CPR_SAFE. 1179515SJonathan.Adams@Sun.COM * 1180Sstevel@tonic-gate * TASKQ_CPR_SAFE: This flag specifies that users of the task queue will 1199515SJonathan.Adams@Sun.COM * use their own protocol for handling CPR issues. This flag is not 1209515SJonathan.Adams@Sun.COM * supported for DYNAMIC task queues. This flag is not compatible 1219515SJonathan.Adams@Sun.COM * with TASKQ_THREADS_CPU_PCT. 1220Sstevel@tonic-gate * 1230Sstevel@tonic-gate * The 'pri' field specifies the default priority for the threads that 1240Sstevel@tonic-gate * service all scheduled tasks. 1250Sstevel@tonic-gate * 12611173SJonathan.Adams@Sun.COM * taskq_t *taskq_create_instance(name, instance, nthreads, pri, minalloc, 12711173SJonathan.Adams@Sun.COM * maxall, flags); 12811173SJonathan.Adams@Sun.COM * 12911173SJonathan.Adams@Sun.COM * Like taskq_create(), but takes an instance number (or -1 to indicate 13011173SJonathan.Adams@Sun.COM * no instance). 13111173SJonathan.Adams@Sun.COM * 13211173SJonathan.Adams@Sun.COM * taskq_t *taskq_create_proc(name, nthreads, pri, minalloc, maxall, proc, 13311173SJonathan.Adams@Sun.COM * flags); 13411173SJonathan.Adams@Sun.COM * 13511173SJonathan.Adams@Sun.COM * Like taskq_create(), but creates the taskq threads in the specified 13611173SJonathan.Adams@Sun.COM * system process. If proc != &p0, this must be called from a thread 13711173SJonathan.Adams@Sun.COM * in that process. 13811173SJonathan.Adams@Sun.COM * 13911173SJonathan.Adams@Sun.COM * taskq_t *taskq_create_sysdc(name, nthreads, minalloc, maxall, proc, 14011173SJonathan.Adams@Sun.COM * dc, flags); 14111173SJonathan.Adams@Sun.COM * 14211173SJonathan.Adams@Sun.COM * Like taskq_create_proc(), but the taskq threads will use the 14311173SJonathan.Adams@Sun.COM * System Duty Cycle (SDC) scheduling class with a duty cycle of dc. 14411173SJonathan.Adams@Sun.COM * 1450Sstevel@tonic-gate * void taskq_destroy(tap): 1460Sstevel@tonic-gate * 1470Sstevel@tonic-gate * Waits for any scheduled tasks to complete, then destroys the taskq. 1480Sstevel@tonic-gate * Caller should guarantee that no new tasks are scheduled in the closing 1490Sstevel@tonic-gate * taskq. 1500Sstevel@tonic-gate * 1510Sstevel@tonic-gate * taskqid_t taskq_dispatch(tq, func, arg, flags): 1520Sstevel@tonic-gate * 1530Sstevel@tonic-gate * Dispatches the task "func(arg)" to taskq. The 'flags' indicates whether 1540Sstevel@tonic-gate * the caller is willing to block for memory. The function returns an 1550Sstevel@tonic-gate * opaque value which is zero iff dispatch fails. If flags is TQ_NOSLEEP 1560Sstevel@tonic-gate * or TQ_NOALLOC and the task can't be dispatched, taskq_dispatch() fails 1570Sstevel@tonic-gate * and returns (taskqid_t)0. 1580Sstevel@tonic-gate * 1590Sstevel@tonic-gate * ASSUMES: func != NULL. 1600Sstevel@tonic-gate * 1610Sstevel@tonic-gate * Possible flags: 1620Sstevel@tonic-gate * TQ_NOSLEEP: Do not wait for resources; may fail. 1630Sstevel@tonic-gate * 1640Sstevel@tonic-gate * TQ_NOALLOC: Do not allocate memory; may fail. May only be used with 1650Sstevel@tonic-gate * non-dynamic task queues. 1660Sstevel@tonic-gate * 1670Sstevel@tonic-gate * TQ_NOQUEUE: Do not enqueue a task if it can't dispatch it due to 1680Sstevel@tonic-gate * lack of available resources and fail. If this flag is not 16911173SJonathan.Adams@Sun.COM * set, and the task pool is exhausted, the task may be scheduled 1700Sstevel@tonic-gate * in the backing queue. This flag may ONLY be used with dynamic 1710Sstevel@tonic-gate * task queues. 1720Sstevel@tonic-gate * 1730Sstevel@tonic-gate * NOTE: This flag should always be used when a task queue is used 1740Sstevel@tonic-gate * for tasks that may depend on each other for completion. 1750Sstevel@tonic-gate * Enqueueing dependent tasks may create deadlocks. 1760Sstevel@tonic-gate * 1770Sstevel@tonic-gate * TQ_SLEEP: May block waiting for resources. May still fail for 17811173SJonathan.Adams@Sun.COM * dynamic task queues if TQ_NOQUEUE is also specified, otherwise 1790Sstevel@tonic-gate * always succeed. 1800Sstevel@tonic-gate * 18111173SJonathan.Adams@Sun.COM * TQ_FRONT: Puts the new task at the front of the queue. Be careful. 18211173SJonathan.Adams@Sun.COM * 1830Sstevel@tonic-gate * NOTE: Dynamic task queues are much more likely to fail in 1840Sstevel@tonic-gate * taskq_dispatch() (especially if TQ_NOQUEUE was specified), so it 1850Sstevel@tonic-gate * is important to have backup strategies handling such failures. 1860Sstevel@tonic-gate * 1870Sstevel@tonic-gate * void taskq_wait(tq): 1880Sstevel@tonic-gate * 1890Sstevel@tonic-gate * Waits for all previously scheduled tasks to complete. 1900Sstevel@tonic-gate * 1910Sstevel@tonic-gate * NOTE: It does not stop any new task dispatches. 1920Sstevel@tonic-gate * Do NOT call taskq_wait() from a task: it will cause deadlock. 1930Sstevel@tonic-gate * 1940Sstevel@tonic-gate * void taskq_suspend(tq) 1950Sstevel@tonic-gate * 1960Sstevel@tonic-gate * Suspend all task execution. Tasks already scheduled for a dynamic task 1970Sstevel@tonic-gate * queue will still be executed, but all new scheduled tasks will be 1980Sstevel@tonic-gate * suspended until taskq_resume() is called. 1990Sstevel@tonic-gate * 2000Sstevel@tonic-gate * int taskq_suspended(tq) 2010Sstevel@tonic-gate * 2020Sstevel@tonic-gate * Returns 1 if taskq is suspended and 0 otherwise. It is intended to 2030Sstevel@tonic-gate * ASSERT that the task queue is suspended. 2040Sstevel@tonic-gate * 2050Sstevel@tonic-gate * void taskq_resume(tq) 2060Sstevel@tonic-gate * 2070Sstevel@tonic-gate * Resume task queue execution. 2080Sstevel@tonic-gate * 2090Sstevel@tonic-gate * int taskq_member(tq, thread) 2100Sstevel@tonic-gate * 2110Sstevel@tonic-gate * Returns 1 if 'thread' belongs to taskq 'tq' and 0 otherwise. The 2120Sstevel@tonic-gate * intended use is to ASSERT that a given function is called in taskq 2130Sstevel@tonic-gate * context only. 2140Sstevel@tonic-gate * 2150Sstevel@tonic-gate * system_taskq 2160Sstevel@tonic-gate * 2170Sstevel@tonic-gate * Global system-wide dynamic task queue for common uses. It may be used by 2180Sstevel@tonic-gate * any subsystem that needs to schedule tasks and does not need to manage 2190Sstevel@tonic-gate * its own task queues. It is initialized quite early during system boot. 2200Sstevel@tonic-gate * 2219515SJonathan.Adams@Sun.COM * IMPLEMENTATION ============================================================== 2220Sstevel@tonic-gate * 2230Sstevel@tonic-gate * This is schematic representation of the task queue structures. 2240Sstevel@tonic-gate * 2250Sstevel@tonic-gate * taskq: 2260Sstevel@tonic-gate * +-------------+ 22710889SJonathan.Adams@Sun.COM * | tq_lock | +---< taskq_ent_free() 2280Sstevel@tonic-gate * +-------------+ | 2290Sstevel@tonic-gate * |... | | tqent: tqent: 2300Sstevel@tonic-gate * +-------------+ | +------------+ +------------+ 2310Sstevel@tonic-gate * | tq_freelist |-->| tqent_next |--> ... ->| tqent_next | 2320Sstevel@tonic-gate * +-------------+ +------------+ +------------+ 2330Sstevel@tonic-gate * |... | | ... | | ... | 2340Sstevel@tonic-gate * +-------------+ +------------+ +------------+ 2350Sstevel@tonic-gate * | tq_task | | 2360Sstevel@tonic-gate * | | +-------------->taskq_ent_alloc() 2370Sstevel@tonic-gate * +--------------------------------------------------------------------------+ 2380Sstevel@tonic-gate * | | | tqent tqent | 2390Sstevel@tonic-gate * | +---------------------+ +--> +------------+ +--> +------------+ | 2400Sstevel@tonic-gate * | | ... | | | func, arg | | | func, arg | | 2410Sstevel@tonic-gate * +>+---------------------+ <---|-+ +------------+ <---|-+ +------------+ | 2420Sstevel@tonic-gate * | tq_taskq.tqent_next | ----+ | | tqent_next | --->+ | | tqent_next |--+ 2430Sstevel@tonic-gate * +---------------------+ | +------------+ ^ | +------------+ 2440Sstevel@tonic-gate * +-| tq_task.tqent_prev | +--| tqent_prev | | +--| tqent_prev | ^ 2450Sstevel@tonic-gate * | +---------------------+ +------------+ | +------------+ | 2460Sstevel@tonic-gate * | |... | | ... | | | ... | | 2470Sstevel@tonic-gate * | +---------------------+ +------------+ | +------------+ | 2480Sstevel@tonic-gate * | ^ | | 2490Sstevel@tonic-gate * | | | | 2500Sstevel@tonic-gate * +--------------------------------------+--------------+ TQ_APPEND() -+ 2510Sstevel@tonic-gate * | | | 2520Sstevel@tonic-gate * |... | taskq_thread()-----+ 2530Sstevel@tonic-gate * +-------------+ 2540Sstevel@tonic-gate * | tq_buckets |--+-------> [ NULL ] (for regular task queues) 2550Sstevel@tonic-gate * +-------------+ | 2560Sstevel@tonic-gate * | DYNAMIC TASK QUEUES: 2570Sstevel@tonic-gate * | 25811173SJonathan.Adams@Sun.COM * +-> taskq_bucket[nCPU] taskq_bucket_dispatch() 2590Sstevel@tonic-gate * +-------------------+ ^ 2600Sstevel@tonic-gate * +--->| tqbucket_lock | | 2610Sstevel@tonic-gate * | +-------------------+ +--------+ +--------+ 2620Sstevel@tonic-gate * | | tqbucket_freelist |-->| tqent |-->...| tqent | ^ 2630Sstevel@tonic-gate * | +-------------------+<--+--------+<--...+--------+ | 2640Sstevel@tonic-gate * | | ... | | thread | | thread | | 2650Sstevel@tonic-gate * | +-------------------+ +--------+ +--------+ | 2660Sstevel@tonic-gate * | +-------------------+ | 2670Sstevel@tonic-gate * taskq_dispatch()--+--->| tqbucket_lock | TQ_APPEND()------+ 2680Sstevel@tonic-gate * TQ_HASH() | +-------------------+ +--------+ +--------+ 2690Sstevel@tonic-gate * | | tqbucket_freelist |-->| tqent |-->...| tqent | 2700Sstevel@tonic-gate * | +-------------------+<--+--------+<--...+--------+ 2710Sstevel@tonic-gate * | | ... | | thread | | thread | 2720Sstevel@tonic-gate * | +-------------------+ +--------+ +--------+ 27311173SJonathan.Adams@Sun.COM * +---> ... 2740Sstevel@tonic-gate * 2750Sstevel@tonic-gate * 2760Sstevel@tonic-gate * Task queues use tq_task field to link new entry in the queue. The queue is a 2770Sstevel@tonic-gate * circular doubly-linked list. Entries are put in the end of the list with 2780Sstevel@tonic-gate * TQ_APPEND() and processed from the front of the list by taskq_thread() in 2790Sstevel@tonic-gate * FIFO order. Task queue entries are cached in the free list managed by 2800Sstevel@tonic-gate * taskq_ent_alloc() and taskq_ent_free() functions. 2810Sstevel@tonic-gate * 2820Sstevel@tonic-gate * All threads used by task queues mark t_taskq field of the thread to 2830Sstevel@tonic-gate * point to the task queue. 2840Sstevel@tonic-gate * 2859515SJonathan.Adams@Sun.COM * Taskq Thread Management ----------------------------------------------------- 2869515SJonathan.Adams@Sun.COM * 2879515SJonathan.Adams@Sun.COM * Taskq's non-dynamic threads are managed with several variables and flags: 2889515SJonathan.Adams@Sun.COM * 2899515SJonathan.Adams@Sun.COM * * tq_nthreads - The number of threads in taskq_thread() for the 2909515SJonathan.Adams@Sun.COM * taskq. 2919515SJonathan.Adams@Sun.COM * 2929515SJonathan.Adams@Sun.COM * * tq_active - The number of threads not waiting on a CV in 2939515SJonathan.Adams@Sun.COM * taskq_thread(); includes newly created threads 2949515SJonathan.Adams@Sun.COM * not yet counted in tq_nthreads. 2959515SJonathan.Adams@Sun.COM * 2969515SJonathan.Adams@Sun.COM * * tq_nthreads_target 2979515SJonathan.Adams@Sun.COM * - The number of threads desired for the taskq. 2989515SJonathan.Adams@Sun.COM * 2999515SJonathan.Adams@Sun.COM * * tq_flags & TASKQ_CHANGING 3009515SJonathan.Adams@Sun.COM * - Indicates that tq_nthreads != tq_nthreads_target. 3019515SJonathan.Adams@Sun.COM * 3029515SJonathan.Adams@Sun.COM * * tq_flags & TASKQ_THREAD_CREATED 3039515SJonathan.Adams@Sun.COM * - Indicates that a thread is being created in the taskq. 3049515SJonathan.Adams@Sun.COM * 3059515SJonathan.Adams@Sun.COM * During creation, tq_nthreads and tq_active are set to 0, and 3069515SJonathan.Adams@Sun.COM * tq_nthreads_target is set to the number of threads desired. The 30711173SJonathan.Adams@Sun.COM * TASKQ_CHANGING flag is set, and taskq_thread_create() is called to 30811173SJonathan.Adams@Sun.COM * create the first thread. taskq_thread_create() increments tq_active, 3099515SJonathan.Adams@Sun.COM * sets TASKQ_THREAD_CREATED, and creates the new thread. 3109515SJonathan.Adams@Sun.COM * 3119515SJonathan.Adams@Sun.COM * Each thread starts in taskq_thread(), clears the TASKQ_THREAD_CREATED 3129515SJonathan.Adams@Sun.COM * flag, and increments tq_nthreads. It stores the new value of 3139515SJonathan.Adams@Sun.COM * tq_nthreads as its "thread_id", and stores its thread pointer in the 3149515SJonathan.Adams@Sun.COM * tq_threadlist at the (thread_id - 1). We keep the thread_id space 3159515SJonathan.Adams@Sun.COM * densely packed by requiring that only the largest thread_id can exit during 3169515SJonathan.Adams@Sun.COM * normal adjustment. The exception is during the destruction of the 3179515SJonathan.Adams@Sun.COM * taskq; once tq_nthreads_target is set to zero, no new threads will be created 3189515SJonathan.Adams@Sun.COM * for the taskq queue, so every thread can exit without any ordering being 3199515SJonathan.Adams@Sun.COM * necessary. 3209515SJonathan.Adams@Sun.COM * 3219515SJonathan.Adams@Sun.COM * Threads will only process work if their thread id is <= tq_nthreads_target. 3229515SJonathan.Adams@Sun.COM * 3239515SJonathan.Adams@Sun.COM * When TASKQ_CHANGING is set, threads will check the current thread target 3249515SJonathan.Adams@Sun.COM * whenever they wake up, and do whatever they can to apply its effects. 3259515SJonathan.Adams@Sun.COM * 3269515SJonathan.Adams@Sun.COM * TASKQ_THREAD_CPU_PCT -------------------------------------------------------- 3279515SJonathan.Adams@Sun.COM * 3289515SJonathan.Adams@Sun.COM * When a taskq is created with TASKQ_THREAD_CPU_PCT, we store their requested 3299515SJonathan.Adams@Sun.COM * percentage in tq_threads_ncpus_pct, start them off with the correct thread 3309515SJonathan.Adams@Sun.COM * target, and add them to the taskq_cpupct_list for later adjustment. 3319515SJonathan.Adams@Sun.COM * 3329515SJonathan.Adams@Sun.COM * We register taskq_cpu_setup() to be called whenever a CPU changes state. It 3339515SJonathan.Adams@Sun.COM * walks the list of TASKQ_THREAD_CPU_PCT taskqs, adjusts their nthread_target 3349515SJonathan.Adams@Sun.COM * if need be, and wakes up all of the threads to process the change. 3359515SJonathan.Adams@Sun.COM * 3369515SJonathan.Adams@Sun.COM * Dynamic Task Queues Implementation ------------------------------------------ 3370Sstevel@tonic-gate * 3380Sstevel@tonic-gate * For a dynamic task queues there is a 1-to-1 mapping between a thread and 3390Sstevel@tonic-gate * taskq_ent_structure. Each entry is serviced by its own thread and each thread 3400Sstevel@tonic-gate * is controlled by a single entry. 3410Sstevel@tonic-gate * 3420Sstevel@tonic-gate * Entries are distributed over a set of buckets. To avoid using modulo 3430Sstevel@tonic-gate * arithmetics the number of buckets is 2^n and is determined as the nearest 3440Sstevel@tonic-gate * power of two roundown of the number of CPUs in the system. Tunable 3450Sstevel@tonic-gate * variable 'taskq_maxbuckets' limits the maximum number of buckets. Each entry 3460Sstevel@tonic-gate * is attached to a bucket for its lifetime and can't migrate to other buckets. 3470Sstevel@tonic-gate * 3480Sstevel@tonic-gate * Entries that have scheduled tasks are not placed in any list. The dispatch 3490Sstevel@tonic-gate * function sets their "func" and "arg" fields and signals the corresponding 3500Sstevel@tonic-gate * thread to execute the task. Once the thread executes the task it clears the 3510Sstevel@tonic-gate * "func" field and places an entry on the bucket cache of free entries pointed 3520Sstevel@tonic-gate * by "tqbucket_freelist" field. ALL entries on the free list should have "func" 3530Sstevel@tonic-gate * field equal to NULL. The free list is a circular doubly-linked list identical 3540Sstevel@tonic-gate * in structure to the tq_task list above, but entries are taken from it in LIFO 3550Sstevel@tonic-gate * order - the last freed entry is the first to be allocated. The 3560Sstevel@tonic-gate * taskq_bucket_dispatch() function gets the most recently used entry from the 3570Sstevel@tonic-gate * free list, sets its "func" and "arg" fields and signals a worker thread. 3580Sstevel@tonic-gate * 3590Sstevel@tonic-gate * After executing each task a per-entry thread taskq_d_thread() places its 3600Sstevel@tonic-gate * entry on the bucket free list and goes to a timed sleep. If it wakes up 3610Sstevel@tonic-gate * without getting new task it removes the entry from the free list and destroys 3620Sstevel@tonic-gate * itself. The thread sleep time is controlled by a tunable variable 3630Sstevel@tonic-gate * `taskq_thread_timeout'. 3640Sstevel@tonic-gate * 3659515SJonathan.Adams@Sun.COM * There are various statistics kept in the bucket which allows for later 3660Sstevel@tonic-gate * analysis of taskq usage patterns. Also, a global copy of taskq creation and 3670Sstevel@tonic-gate * death statistics is kept in the global taskq data structure. Since thread 3680Sstevel@tonic-gate * creation and death happen rarely, updating such global data does not present 3690Sstevel@tonic-gate * a performance problem. 3700Sstevel@tonic-gate * 3710Sstevel@tonic-gate * NOTE: Threads are not bound to any CPU and there is absolutely no association 3720Sstevel@tonic-gate * between the bucket and actual thread CPU, so buckets are used only to 3730Sstevel@tonic-gate * split resources and reduce resource contention. Having threads attached 3740Sstevel@tonic-gate * to the CPU denoted by a bucket may reduce number of times the job 3750Sstevel@tonic-gate * switches between CPUs. 3760Sstevel@tonic-gate * 3770Sstevel@tonic-gate * Current algorithm creates a thread whenever a bucket has no free 3780Sstevel@tonic-gate * entries. It would be nice to know how many threads are in the running 3790Sstevel@tonic-gate * state and don't create threads if all CPUs are busy with existing 3800Sstevel@tonic-gate * tasks, but it is unclear how such strategy can be implemented. 3810Sstevel@tonic-gate * 3820Sstevel@tonic-gate * Currently buckets are created statically as an array attached to task 3830Sstevel@tonic-gate * queue. On some system with nCPUs < max_ncpus it may waste system 3840Sstevel@tonic-gate * memory. One solution may be allocation of buckets when they are first 3850Sstevel@tonic-gate * touched, but it is not clear how useful it is. 3860Sstevel@tonic-gate * 3879515SJonathan.Adams@Sun.COM * SUSPEND/RESUME implementation ----------------------------------------------- 3880Sstevel@tonic-gate * 3890Sstevel@tonic-gate * Before executing a task taskq_thread() (executing non-dynamic task 3900Sstevel@tonic-gate * queues) obtains taskq's thread lock as a reader. The taskq_suspend() 3910Sstevel@tonic-gate * function gets the same lock as a writer blocking all non-dynamic task 3920Sstevel@tonic-gate * execution. The taskq_resume() function releases the lock allowing 3930Sstevel@tonic-gate * taskq_thread to continue execution. 3940Sstevel@tonic-gate * 3950Sstevel@tonic-gate * For dynamic task queues, each bucket is marked as TQBUCKET_SUSPEND by 3960Sstevel@tonic-gate * taskq_suspend() function. After that taskq_bucket_dispatch() always 3970Sstevel@tonic-gate * fails, so that taskq_dispatch() will either enqueue tasks for a 3980Sstevel@tonic-gate * suspended backing queue or fail if TQ_NOQUEUE is specified in dispatch 3990Sstevel@tonic-gate * flags. 4000Sstevel@tonic-gate * 4010Sstevel@tonic-gate * NOTE: taskq_suspend() does not immediately block any tasks already 4020Sstevel@tonic-gate * scheduled for dynamic task queues. It only suspends new tasks 4030Sstevel@tonic-gate * scheduled after taskq_suspend() was called. 4040Sstevel@tonic-gate * 4050Sstevel@tonic-gate * taskq_member() function works by comparing a thread t_taskq pointer with 4060Sstevel@tonic-gate * the passed thread pointer. 4070Sstevel@tonic-gate * 4089515SJonathan.Adams@Sun.COM * LOCKS and LOCK Hierarchy ---------------------------------------------------- 4090Sstevel@tonic-gate * 4109515SJonathan.Adams@Sun.COM * There are three locks used in task queues: 4110Sstevel@tonic-gate * 4129515SJonathan.Adams@Sun.COM * 1) The taskq_t's tq_lock, protecting global task queue state. 4130Sstevel@tonic-gate * 4140Sstevel@tonic-gate * 2) Each per-CPU bucket has a lock for bucket management. 4150Sstevel@tonic-gate * 4169515SJonathan.Adams@Sun.COM * 3) The global taskq_cpupct_lock, which protects the list of 4179515SJonathan.Adams@Sun.COM * TASKQ_THREADS_CPU_PCT taskqs. 4189515SJonathan.Adams@Sun.COM * 4199515SJonathan.Adams@Sun.COM * If both (1) and (2) are needed, tq_lock should be taken *after* the bucket 4200Sstevel@tonic-gate * lock. 4210Sstevel@tonic-gate * 4229515SJonathan.Adams@Sun.COM * If both (1) and (3) are needed, tq_lock should be taken *after* 4239515SJonathan.Adams@Sun.COM * taskq_cpupct_lock. 4249515SJonathan.Adams@Sun.COM * 4259515SJonathan.Adams@Sun.COM * DEBUG FACILITIES ------------------------------------------------------------ 4260Sstevel@tonic-gate * 4270Sstevel@tonic-gate * For DEBUG kernels it is possible to induce random failures to 4280Sstevel@tonic-gate * taskq_dispatch() function when it is given TQ_NOSLEEP argument. The value of 4290Sstevel@tonic-gate * taskq_dmtbf and taskq_smtbf tunables control the mean time between induced 4300Sstevel@tonic-gate * failures for dynamic and static task queues respectively. 4310Sstevel@tonic-gate * 4320Sstevel@tonic-gate * Setting TASKQ_STATISTIC to 0 will disable per-bucket statistics. 4330Sstevel@tonic-gate * 4349515SJonathan.Adams@Sun.COM * TUNABLES -------------------------------------------------------------------- 4350Sstevel@tonic-gate * 4360Sstevel@tonic-gate * system_taskq_size - Size of the global system_taskq. 4370Sstevel@tonic-gate * This value is multiplied by nCPUs to determine 4380Sstevel@tonic-gate * actual size. 4390Sstevel@tonic-gate * Default value: 64 4400Sstevel@tonic-gate * 4419515SJonathan.Adams@Sun.COM * taskq_minimum_nthreads_max 4429515SJonathan.Adams@Sun.COM * - Minimum size of the thread list for a taskq. 4439515SJonathan.Adams@Sun.COM * Useful for testing different thread pool 4449515SJonathan.Adams@Sun.COM * sizes by overwriting tq_nthreads_target. 4459515SJonathan.Adams@Sun.COM * 4460Sstevel@tonic-gate * taskq_thread_timeout - Maximum idle time for taskq_d_thread() 4470Sstevel@tonic-gate * Default value: 5 minutes 4480Sstevel@tonic-gate * 4490Sstevel@tonic-gate * taskq_maxbuckets - Maximum number of buckets in any task queue 4500Sstevel@tonic-gate * Default value: 128 4510Sstevel@tonic-gate * 4520Sstevel@tonic-gate * taskq_search_depth - Maximum # of buckets searched for a free entry 4530Sstevel@tonic-gate * Default value: 4 4540Sstevel@tonic-gate * 4550Sstevel@tonic-gate * taskq_dmtbf - Mean time between induced dispatch failures 4560Sstevel@tonic-gate * for dynamic task queues. 4570Sstevel@tonic-gate * Default value: UINT_MAX (no induced failures) 4580Sstevel@tonic-gate * 4590Sstevel@tonic-gate * taskq_smtbf - Mean time between induced dispatch failures 4600Sstevel@tonic-gate * for static task queues. 4610Sstevel@tonic-gate * Default value: UINT_MAX (no induced failures) 4620Sstevel@tonic-gate * 4639515SJonathan.Adams@Sun.COM * CONDITIONAL compilation ----------------------------------------------------- 4640Sstevel@tonic-gate * 4650Sstevel@tonic-gate * TASKQ_STATISTIC - If set will enable bucket statistic (default). 4660Sstevel@tonic-gate * 4670Sstevel@tonic-gate */ 4680Sstevel@tonic-gate 4690Sstevel@tonic-gate #include <sys/taskq_impl.h> 4700Sstevel@tonic-gate #include <sys/thread.h> 4710Sstevel@tonic-gate #include <sys/proc.h> 4720Sstevel@tonic-gate #include <sys/kmem.h> 4730Sstevel@tonic-gate #include <sys/vmem.h> 4740Sstevel@tonic-gate #include <sys/callb.h> 47511173SJonathan.Adams@Sun.COM #include <sys/class.h> 4760Sstevel@tonic-gate #include <sys/systm.h> 4770Sstevel@tonic-gate #include <sys/cmn_err.h> 4780Sstevel@tonic-gate #include <sys/debug.h> 4790Sstevel@tonic-gate #include <sys/vmsystm.h> /* For throttlefree */ 4800Sstevel@tonic-gate #include <sys/sysmacros.h> 4810Sstevel@tonic-gate #include <sys/cpuvar.h> 48211173SJonathan.Adams@Sun.COM #include <sys/cpupart.h> 4830Sstevel@tonic-gate #include <sys/sdt.h> 48411173SJonathan.Adams@Sun.COM #include <sys/sysdc.h> 4859515SJonathan.Adams@Sun.COM #include <sys/note.h> 4860Sstevel@tonic-gate 4870Sstevel@tonic-gate static kmem_cache_t *taskq_ent_cache, *taskq_cache; 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate /* 4909515SJonathan.Adams@Sun.COM * Pseudo instance numbers for taskqs without explicitly provided instance. 4910Sstevel@tonic-gate */ 4920Sstevel@tonic-gate static vmem_t *taskq_id_arena; 4930Sstevel@tonic-gate 4940Sstevel@tonic-gate /* Global system task queue for common use */ 4950Sstevel@tonic-gate taskq_t *system_taskq; 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate /* 4989515SJonathan.Adams@Sun.COM * Maximum number of entries in global system taskq is 49911173SJonathan.Adams@Sun.COM * system_taskq_size * max_ncpus 5000Sstevel@tonic-gate */ 5010Sstevel@tonic-gate #define SYSTEM_TASKQ_SIZE 64 5020Sstevel@tonic-gate int system_taskq_size = SYSTEM_TASKQ_SIZE; 5030Sstevel@tonic-gate 5040Sstevel@tonic-gate /* 5059515SJonathan.Adams@Sun.COM * Minimum size for tq_nthreads_max; useful for those who want to play around 5069515SJonathan.Adams@Sun.COM * with increasing a taskq's tq_nthreads_target. 5079515SJonathan.Adams@Sun.COM */ 5089515SJonathan.Adams@Sun.COM int taskq_minimum_nthreads_max = 1; 5099515SJonathan.Adams@Sun.COM 51011173SJonathan.Adams@Sun.COM /* 51111173SJonathan.Adams@Sun.COM * We want to ensure that when taskq_create() returns, there is at least 51211173SJonathan.Adams@Sun.COM * one thread ready to handle requests. To guarantee this, we have to wait 51311173SJonathan.Adams@Sun.COM * for the second thread, since the first one cannot process requests until 51411173SJonathan.Adams@Sun.COM * the second thread has been created. 51511173SJonathan.Adams@Sun.COM */ 51611173SJonathan.Adams@Sun.COM #define TASKQ_CREATE_ACTIVE_THREADS 2 51711173SJonathan.Adams@Sun.COM 5189515SJonathan.Adams@Sun.COM /* Maximum percentage allowed for TASKQ_THREADS_CPU_PCT */ 5199515SJonathan.Adams@Sun.COM #define TASKQ_CPUPCT_MAX_PERCENT 1000 5209515SJonathan.Adams@Sun.COM int taskq_cpupct_max_percent = TASKQ_CPUPCT_MAX_PERCENT; 5219515SJonathan.Adams@Sun.COM 5229515SJonathan.Adams@Sun.COM /* 5230Sstevel@tonic-gate * Dynamic task queue threads that don't get any work within 5240Sstevel@tonic-gate * taskq_thread_timeout destroy themselves 5250Sstevel@tonic-gate */ 5260Sstevel@tonic-gate #define TASKQ_THREAD_TIMEOUT (60 * 5) 5270Sstevel@tonic-gate int taskq_thread_timeout = TASKQ_THREAD_TIMEOUT; 5280Sstevel@tonic-gate 5290Sstevel@tonic-gate #define TASKQ_MAXBUCKETS 128 5300Sstevel@tonic-gate int taskq_maxbuckets = TASKQ_MAXBUCKETS; 5310Sstevel@tonic-gate 5320Sstevel@tonic-gate /* 5330Sstevel@tonic-gate * When a bucket has no available entries another buckets are tried. 5340Sstevel@tonic-gate * taskq_search_depth parameter limits the amount of buckets that we search 5350Sstevel@tonic-gate * before failing. This is mostly useful in systems with many CPUs where we may 5360Sstevel@tonic-gate * spend too much time scanning busy buckets. 5370Sstevel@tonic-gate */ 5380Sstevel@tonic-gate #define TASKQ_SEARCH_DEPTH 4 5390Sstevel@tonic-gate int taskq_search_depth = TASKQ_SEARCH_DEPTH; 5400Sstevel@tonic-gate 5410Sstevel@tonic-gate /* 5420Sstevel@tonic-gate * Hashing function: mix various bits of x. May be pretty much anything. 5430Sstevel@tonic-gate */ 5440Sstevel@tonic-gate #define TQ_HASH(x) ((x) ^ ((x) >> 11) ^ ((x) >> 17) ^ ((x) ^ 27)) 5450Sstevel@tonic-gate 5460Sstevel@tonic-gate /* 5470Sstevel@tonic-gate * We do not create any new threads when the system is low on memory and start 5480Sstevel@tonic-gate * throttling memory allocations. The following macro tries to estimate such 5490Sstevel@tonic-gate * condition. 5500Sstevel@tonic-gate */ 5510Sstevel@tonic-gate #define ENOUGH_MEMORY() (freemem > throttlefree) 5520Sstevel@tonic-gate 5530Sstevel@tonic-gate /* 5540Sstevel@tonic-gate * Static functions. 5550Sstevel@tonic-gate */ 5560Sstevel@tonic-gate static taskq_t *taskq_create_common(const char *, int, int, pri_t, int, 55711173SJonathan.Adams@Sun.COM int, proc_t *, uint_t, uint_t); 5580Sstevel@tonic-gate static void taskq_thread(void *); 5590Sstevel@tonic-gate static void taskq_d_thread(taskq_ent_t *); 5600Sstevel@tonic-gate static void taskq_bucket_extend(void *); 5610Sstevel@tonic-gate static int taskq_constructor(void *, void *, int); 5620Sstevel@tonic-gate static void taskq_destructor(void *, void *); 5630Sstevel@tonic-gate static int taskq_ent_constructor(void *, void *, int); 5640Sstevel@tonic-gate static void taskq_ent_destructor(void *, void *); 5650Sstevel@tonic-gate static taskq_ent_t *taskq_ent_alloc(taskq_t *, int); 5660Sstevel@tonic-gate static void taskq_ent_free(taskq_t *, taskq_ent_t *); 5670Sstevel@tonic-gate static taskq_ent_t *taskq_bucket_dispatch(taskq_bucket_t *, task_func_t, 5680Sstevel@tonic-gate void *); 5690Sstevel@tonic-gate 5700Sstevel@tonic-gate /* 5710Sstevel@tonic-gate * Task queues kstats. 5720Sstevel@tonic-gate */ 5730Sstevel@tonic-gate struct taskq_kstat { 57411173SJonathan.Adams@Sun.COM kstat_named_t tq_pid; 5750Sstevel@tonic-gate kstat_named_t tq_tasks; 5760Sstevel@tonic-gate kstat_named_t tq_executed; 5770Sstevel@tonic-gate kstat_named_t tq_maxtasks; 5780Sstevel@tonic-gate kstat_named_t tq_totaltime; 5790Sstevel@tonic-gate kstat_named_t tq_nalloc; 5800Sstevel@tonic-gate kstat_named_t tq_nactive; 5810Sstevel@tonic-gate kstat_named_t tq_pri; 5820Sstevel@tonic-gate kstat_named_t tq_nthreads; 5830Sstevel@tonic-gate } taskq_kstat = { 58411173SJonathan.Adams@Sun.COM { "pid", KSTAT_DATA_UINT64 }, 5850Sstevel@tonic-gate { "tasks", KSTAT_DATA_UINT64 }, 5860Sstevel@tonic-gate { "executed", KSTAT_DATA_UINT64 }, 5870Sstevel@tonic-gate { "maxtasks", KSTAT_DATA_UINT64 }, 5880Sstevel@tonic-gate { "totaltime", KSTAT_DATA_UINT64 }, 5890Sstevel@tonic-gate { "nactive", KSTAT_DATA_UINT64 }, 5900Sstevel@tonic-gate { "nalloc", KSTAT_DATA_UINT64 }, 5910Sstevel@tonic-gate { "priority", KSTAT_DATA_UINT64 }, 5920Sstevel@tonic-gate { "threads", KSTAT_DATA_UINT64 }, 5930Sstevel@tonic-gate }; 5940Sstevel@tonic-gate 5950Sstevel@tonic-gate struct taskq_d_kstat { 5960Sstevel@tonic-gate kstat_named_t tqd_pri; 5970Sstevel@tonic-gate kstat_named_t tqd_btasks; 5980Sstevel@tonic-gate kstat_named_t tqd_bexecuted; 5990Sstevel@tonic-gate kstat_named_t tqd_bmaxtasks; 6000Sstevel@tonic-gate kstat_named_t tqd_bnalloc; 6010Sstevel@tonic-gate kstat_named_t tqd_bnactive; 6020Sstevel@tonic-gate kstat_named_t tqd_btotaltime; 6030Sstevel@tonic-gate kstat_named_t tqd_hits; 6040Sstevel@tonic-gate kstat_named_t tqd_misses; 6050Sstevel@tonic-gate kstat_named_t tqd_overflows; 6060Sstevel@tonic-gate kstat_named_t tqd_tcreates; 6070Sstevel@tonic-gate kstat_named_t tqd_tdeaths; 6080Sstevel@tonic-gate kstat_named_t tqd_maxthreads; 6090Sstevel@tonic-gate kstat_named_t tqd_nomem; 6100Sstevel@tonic-gate kstat_named_t tqd_disptcreates; 6110Sstevel@tonic-gate kstat_named_t tqd_totaltime; 6120Sstevel@tonic-gate kstat_named_t tqd_nalloc; 6130Sstevel@tonic-gate kstat_named_t tqd_nfree; 6140Sstevel@tonic-gate } taskq_d_kstat = { 6150Sstevel@tonic-gate { "priority", KSTAT_DATA_UINT64 }, 6160Sstevel@tonic-gate { "btasks", KSTAT_DATA_UINT64 }, 6170Sstevel@tonic-gate { "bexecuted", KSTAT_DATA_UINT64 }, 6180Sstevel@tonic-gate { "bmaxtasks", KSTAT_DATA_UINT64 }, 6190Sstevel@tonic-gate { "bnalloc", KSTAT_DATA_UINT64 }, 6200Sstevel@tonic-gate { "bnactive", KSTAT_DATA_UINT64 }, 6210Sstevel@tonic-gate { "btotaltime", KSTAT_DATA_UINT64 }, 6220Sstevel@tonic-gate { "hits", KSTAT_DATA_UINT64 }, 6230Sstevel@tonic-gate { "misses", KSTAT_DATA_UINT64 }, 6240Sstevel@tonic-gate { "overflows", KSTAT_DATA_UINT64 }, 6250Sstevel@tonic-gate { "tcreates", KSTAT_DATA_UINT64 }, 6260Sstevel@tonic-gate { "tdeaths", KSTAT_DATA_UINT64 }, 6270Sstevel@tonic-gate { "maxthreads", KSTAT_DATA_UINT64 }, 6280Sstevel@tonic-gate { "nomem", KSTAT_DATA_UINT64 }, 6290Sstevel@tonic-gate { "disptcreates", KSTAT_DATA_UINT64 }, 6300Sstevel@tonic-gate { "totaltime", KSTAT_DATA_UINT64 }, 6310Sstevel@tonic-gate { "nalloc", KSTAT_DATA_UINT64 }, 6320Sstevel@tonic-gate { "nfree", KSTAT_DATA_UINT64 }, 6330Sstevel@tonic-gate }; 6340Sstevel@tonic-gate 6350Sstevel@tonic-gate static kmutex_t taskq_kstat_lock; 6360Sstevel@tonic-gate static kmutex_t taskq_d_kstat_lock; 6370Sstevel@tonic-gate static int taskq_kstat_update(kstat_t *, int); 6380Sstevel@tonic-gate static int taskq_d_kstat_update(kstat_t *, int); 6390Sstevel@tonic-gate 6409515SJonathan.Adams@Sun.COM /* 64111173SJonathan.Adams@Sun.COM * List of all TASKQ_THREADS_CPU_PCT taskqs. 6429515SJonathan.Adams@Sun.COM */ 64311173SJonathan.Adams@Sun.COM static list_t taskq_cpupct_list; /* protected by cpu_lock */ 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate /* 6460Sstevel@tonic-gate * Collect per-bucket statistic when TASKQ_STATISTIC is defined. 6470Sstevel@tonic-gate */ 6480Sstevel@tonic-gate #define TASKQ_STATISTIC 1 6490Sstevel@tonic-gate 6500Sstevel@tonic-gate #if TASKQ_STATISTIC 6510Sstevel@tonic-gate #define TQ_STAT(b, x) b->tqbucket_stat.x++ 6520Sstevel@tonic-gate #else 6530Sstevel@tonic-gate #define TQ_STAT(b, x) 6540Sstevel@tonic-gate #endif 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate /* 6570Sstevel@tonic-gate * Random fault injection. 6580Sstevel@tonic-gate */ 6590Sstevel@tonic-gate uint_t taskq_random; 6600Sstevel@tonic-gate uint_t taskq_dmtbf = UINT_MAX; /* mean time between injected failures */ 6610Sstevel@tonic-gate uint_t taskq_smtbf = UINT_MAX; /* mean time between injected failures */ 6620Sstevel@tonic-gate 6630Sstevel@tonic-gate /* 6640Sstevel@tonic-gate * TQ_NOSLEEP dispatches on dynamic task queues are always allowed to fail. 6650Sstevel@tonic-gate * 6660Sstevel@tonic-gate * TQ_NOSLEEP dispatches on static task queues can't arbitrarily fail because 6670Sstevel@tonic-gate * they could prepopulate the cache and make sure that they do not use more 6680Sstevel@tonic-gate * then minalloc entries. So, fault injection in this case insures that 6690Sstevel@tonic-gate * either TASKQ_PREPOPULATE is not set or there are more entries allocated 6700Sstevel@tonic-gate * than is specified by minalloc. TQ_NOALLOC dispatches are always allowed 6710Sstevel@tonic-gate * to fail, but for simplicity we treat them identically to TQ_NOSLEEP 6720Sstevel@tonic-gate * dispatches. 6730Sstevel@tonic-gate */ 6740Sstevel@tonic-gate #ifdef DEBUG 6750Sstevel@tonic-gate #define TASKQ_D_RANDOM_DISPATCH_FAILURE(tq, flag) \ 6760Sstevel@tonic-gate taskq_random = (taskq_random * 2416 + 374441) % 1771875;\ 6770Sstevel@tonic-gate if ((flag & TQ_NOSLEEP) && \ 6780Sstevel@tonic-gate taskq_random < 1771875 / taskq_dmtbf) { \ 6790Sstevel@tonic-gate return (NULL); \ 6800Sstevel@tonic-gate } 6810Sstevel@tonic-gate 6820Sstevel@tonic-gate #define TASKQ_S_RANDOM_DISPATCH_FAILURE(tq, flag) \ 6830Sstevel@tonic-gate taskq_random = (taskq_random * 2416 + 374441) % 1771875;\ 6840Sstevel@tonic-gate if ((flag & (TQ_NOSLEEP | TQ_NOALLOC)) && \ 6850Sstevel@tonic-gate (!(tq->tq_flags & TASKQ_PREPOPULATE) || \ 6860Sstevel@tonic-gate (tq->tq_nalloc > tq->tq_minalloc)) && \ 6870Sstevel@tonic-gate (taskq_random < (1771875 / taskq_smtbf))) { \ 6880Sstevel@tonic-gate mutex_exit(&tq->tq_lock); \ 6890Sstevel@tonic-gate return (NULL); \ 6900Sstevel@tonic-gate } 6910Sstevel@tonic-gate #else 6920Sstevel@tonic-gate #define TASKQ_S_RANDOM_DISPATCH_FAILURE(tq, flag) 6930Sstevel@tonic-gate #define TASKQ_D_RANDOM_DISPATCH_FAILURE(tq, flag) 6940Sstevel@tonic-gate #endif 6950Sstevel@tonic-gate 6960Sstevel@tonic-gate #define IS_EMPTY(l) (((l).tqent_prev == (l).tqent_next) && \ 6970Sstevel@tonic-gate ((l).tqent_prev == &(l))) 6980Sstevel@tonic-gate 6990Sstevel@tonic-gate /* 7000Sstevel@tonic-gate * Append `tqe' in the end of the doubly-linked list denoted by l. 7010Sstevel@tonic-gate */ 7020Sstevel@tonic-gate #define TQ_APPEND(l, tqe) { \ 7030Sstevel@tonic-gate tqe->tqent_next = &l; \ 7040Sstevel@tonic-gate tqe->tqent_prev = l.tqent_prev; \ 7050Sstevel@tonic-gate tqe->tqent_next->tqent_prev = tqe; \ 7060Sstevel@tonic-gate tqe->tqent_prev->tqent_next = tqe; \ 7070Sstevel@tonic-gate } 70811173SJonathan.Adams@Sun.COM /* 70911173SJonathan.Adams@Sun.COM * Prepend 'tqe' to the beginning of l 71011173SJonathan.Adams@Sun.COM */ 71111173SJonathan.Adams@Sun.COM #define TQ_PREPEND(l, tqe) { \ 71211173SJonathan.Adams@Sun.COM tqe->tqent_next = l.tqent_next; \ 71311173SJonathan.Adams@Sun.COM tqe->tqent_prev = &l; \ 71411173SJonathan.Adams@Sun.COM tqe->tqent_next->tqent_prev = tqe; \ 71511173SJonathan.Adams@Sun.COM tqe->tqent_prev->tqent_next = tqe; \ 71611173SJonathan.Adams@Sun.COM } 7170Sstevel@tonic-gate 7180Sstevel@tonic-gate /* 7190Sstevel@tonic-gate * Schedule a task specified by func and arg into the task queue entry tqe. 7200Sstevel@tonic-gate */ 72111173SJonathan.Adams@Sun.COM #define TQ_DO_ENQUEUE(tq, tqe, func, arg, front) { \ 72211173SJonathan.Adams@Sun.COM ASSERT(MUTEX_HELD(&tq->tq_lock)); \ 72311173SJonathan.Adams@Sun.COM _NOTE(CONSTCOND) \ 72411173SJonathan.Adams@Sun.COM if (front) { \ 72511173SJonathan.Adams@Sun.COM TQ_PREPEND(tq->tq_task, tqe); \ 72611173SJonathan.Adams@Sun.COM } else { \ 72711173SJonathan.Adams@Sun.COM TQ_APPEND(tq->tq_task, tqe); \ 72811173SJonathan.Adams@Sun.COM } \ 72911173SJonathan.Adams@Sun.COM tqe->tqent_func = (func); \ 73011173SJonathan.Adams@Sun.COM tqe->tqent_arg = (arg); \ 73111173SJonathan.Adams@Sun.COM tq->tq_tasks++; \ 73211173SJonathan.Adams@Sun.COM if (tq->tq_tasks - tq->tq_executed > tq->tq_maxtasks) \ 7330Sstevel@tonic-gate tq->tq_maxtasks = tq->tq_tasks - tq->tq_executed; \ 73411173SJonathan.Adams@Sun.COM cv_signal(&tq->tq_dispatch_cv); \ 7350Sstevel@tonic-gate DTRACE_PROBE2(taskq__enqueue, taskq_t *, tq, taskq_ent_t *, tqe); \ 7360Sstevel@tonic-gate } 7370Sstevel@tonic-gate 73811173SJonathan.Adams@Sun.COM #define TQ_ENQUEUE(tq, tqe, func, arg) \ 73911173SJonathan.Adams@Sun.COM TQ_DO_ENQUEUE(tq, tqe, func, arg, 0) 74011173SJonathan.Adams@Sun.COM 74111173SJonathan.Adams@Sun.COM #define TQ_ENQUEUE_FRONT(tq, tqe, func, arg) \ 74211173SJonathan.Adams@Sun.COM TQ_DO_ENQUEUE(tq, tqe, func, arg, 1) 74311173SJonathan.Adams@Sun.COM 7440Sstevel@tonic-gate /* 7450Sstevel@tonic-gate * Do-nothing task which may be used to prepopulate thread caches. 7460Sstevel@tonic-gate */ 7470Sstevel@tonic-gate /*ARGSUSED*/ 7480Sstevel@tonic-gate void 7490Sstevel@tonic-gate nulltask(void *unused) 7500Sstevel@tonic-gate { 7510Sstevel@tonic-gate } 7520Sstevel@tonic-gate 7530Sstevel@tonic-gate /*ARGSUSED*/ 7540Sstevel@tonic-gate static int 7550Sstevel@tonic-gate taskq_constructor(void *buf, void *cdrarg, int kmflags) 7560Sstevel@tonic-gate { 7570Sstevel@tonic-gate taskq_t *tq = buf; 7580Sstevel@tonic-gate 7590Sstevel@tonic-gate bzero(tq, sizeof (taskq_t)); 7600Sstevel@tonic-gate 7610Sstevel@tonic-gate mutex_init(&tq->tq_lock, NULL, MUTEX_DEFAULT, NULL); 7620Sstevel@tonic-gate rw_init(&tq->tq_threadlock, NULL, RW_DEFAULT, NULL); 7630Sstevel@tonic-gate cv_init(&tq->tq_dispatch_cv, NULL, CV_DEFAULT, NULL); 7649515SJonathan.Adams@Sun.COM cv_init(&tq->tq_exit_cv, NULL, CV_DEFAULT, NULL); 7650Sstevel@tonic-gate cv_init(&tq->tq_wait_cv, NULL, CV_DEFAULT, NULL); 7660Sstevel@tonic-gate 7670Sstevel@tonic-gate tq->tq_task.tqent_next = &tq->tq_task; 7680Sstevel@tonic-gate tq->tq_task.tqent_prev = &tq->tq_task; 7690Sstevel@tonic-gate 7700Sstevel@tonic-gate return (0); 7710Sstevel@tonic-gate } 7720Sstevel@tonic-gate 7730Sstevel@tonic-gate /*ARGSUSED*/ 7740Sstevel@tonic-gate static void 7750Sstevel@tonic-gate taskq_destructor(void *buf, void *cdrarg) 7760Sstevel@tonic-gate { 7770Sstevel@tonic-gate taskq_t *tq = buf; 7780Sstevel@tonic-gate 7799515SJonathan.Adams@Sun.COM ASSERT(tq->tq_nthreads == 0); 7809515SJonathan.Adams@Sun.COM ASSERT(tq->tq_buckets == NULL); 7819515SJonathan.Adams@Sun.COM ASSERT(tq->tq_tcreates == 0); 7829515SJonathan.Adams@Sun.COM ASSERT(tq->tq_tdeaths == 0); 7839515SJonathan.Adams@Sun.COM 7840Sstevel@tonic-gate mutex_destroy(&tq->tq_lock); 7850Sstevel@tonic-gate rw_destroy(&tq->tq_threadlock); 7860Sstevel@tonic-gate cv_destroy(&tq->tq_dispatch_cv); 7879515SJonathan.Adams@Sun.COM cv_destroy(&tq->tq_exit_cv); 7880Sstevel@tonic-gate cv_destroy(&tq->tq_wait_cv); 7890Sstevel@tonic-gate } 7900Sstevel@tonic-gate 7910Sstevel@tonic-gate /*ARGSUSED*/ 7920Sstevel@tonic-gate static int 7930Sstevel@tonic-gate taskq_ent_constructor(void *buf, void *cdrarg, int kmflags) 7940Sstevel@tonic-gate { 7950Sstevel@tonic-gate taskq_ent_t *tqe = buf; 7960Sstevel@tonic-gate 7970Sstevel@tonic-gate tqe->tqent_thread = NULL; 7980Sstevel@tonic-gate cv_init(&tqe->tqent_cv, NULL, CV_DEFAULT, NULL); 7990Sstevel@tonic-gate 8000Sstevel@tonic-gate return (0); 8010Sstevel@tonic-gate } 8020Sstevel@tonic-gate 8030Sstevel@tonic-gate /*ARGSUSED*/ 8040Sstevel@tonic-gate static void 8050Sstevel@tonic-gate taskq_ent_destructor(void *buf, void *cdrarg) 8060Sstevel@tonic-gate { 8070Sstevel@tonic-gate taskq_ent_t *tqe = buf; 8080Sstevel@tonic-gate 8090Sstevel@tonic-gate ASSERT(tqe->tqent_thread == NULL); 8100Sstevel@tonic-gate cv_destroy(&tqe->tqent_cv); 8110Sstevel@tonic-gate } 8120Sstevel@tonic-gate 8130Sstevel@tonic-gate void 8140Sstevel@tonic-gate taskq_init(void) 8150Sstevel@tonic-gate { 8160Sstevel@tonic-gate taskq_ent_cache = kmem_cache_create("taskq_ent_cache", 8170Sstevel@tonic-gate sizeof (taskq_ent_t), 0, taskq_ent_constructor, 8180Sstevel@tonic-gate taskq_ent_destructor, NULL, NULL, NULL, 0); 8190Sstevel@tonic-gate taskq_cache = kmem_cache_create("taskq_cache", sizeof (taskq_t), 8200Sstevel@tonic-gate 0, taskq_constructor, taskq_destructor, NULL, NULL, NULL, 0); 8210Sstevel@tonic-gate taskq_id_arena = vmem_create("taskq_id_arena", 8220Sstevel@tonic-gate (void *)1, INT32_MAX, 1, NULL, NULL, NULL, 0, 8230Sstevel@tonic-gate VM_SLEEP | VMC_IDENTIFIER); 8249515SJonathan.Adams@Sun.COM 82511173SJonathan.Adams@Sun.COM list_create(&taskq_cpupct_list, sizeof (taskq_t), 82611173SJonathan.Adams@Sun.COM offsetof(taskq_t, tq_cpupct_link)); 82711173SJonathan.Adams@Sun.COM } 82811173SJonathan.Adams@Sun.COM 82911173SJonathan.Adams@Sun.COM static void 83011173SJonathan.Adams@Sun.COM taskq_update_nthreads(taskq_t *tq, uint_t ncpus) 83111173SJonathan.Adams@Sun.COM { 83211173SJonathan.Adams@Sun.COM uint_t newtarget = TASKQ_THREADS_PCT(ncpus, tq->tq_threads_ncpus_pct); 83311173SJonathan.Adams@Sun.COM 83411173SJonathan.Adams@Sun.COM ASSERT(MUTEX_HELD(&cpu_lock)); 83511173SJonathan.Adams@Sun.COM ASSERT(MUTEX_HELD(&tq->tq_lock)); 83611173SJonathan.Adams@Sun.COM 83711173SJonathan.Adams@Sun.COM /* We must be going from non-zero to non-zero; no exiting. */ 83811173SJonathan.Adams@Sun.COM ASSERT3U(tq->tq_nthreads_target, !=, 0); 83911173SJonathan.Adams@Sun.COM ASSERT3U(newtarget, !=, 0); 84011173SJonathan.Adams@Sun.COM 84111173SJonathan.Adams@Sun.COM ASSERT3U(newtarget, <=, tq->tq_nthreads_max); 84211173SJonathan.Adams@Sun.COM if (newtarget != tq->tq_nthreads_target) { 84311173SJonathan.Adams@Sun.COM tq->tq_flags |= TASKQ_CHANGING; 84411173SJonathan.Adams@Sun.COM tq->tq_nthreads_target = newtarget; 84511173SJonathan.Adams@Sun.COM cv_broadcast(&tq->tq_dispatch_cv); 84611173SJonathan.Adams@Sun.COM cv_broadcast(&tq->tq_exit_cv); 84711173SJonathan.Adams@Sun.COM } 84811173SJonathan.Adams@Sun.COM } 84911173SJonathan.Adams@Sun.COM 85011173SJonathan.Adams@Sun.COM /* called during task queue creation */ 85111173SJonathan.Adams@Sun.COM static void 85211173SJonathan.Adams@Sun.COM taskq_cpupct_install(taskq_t *tq, cpupart_t *cpup) 85311173SJonathan.Adams@Sun.COM { 85411173SJonathan.Adams@Sun.COM ASSERT(tq->tq_flags & TASKQ_THREADS_CPU_PCT); 85511173SJonathan.Adams@Sun.COM 85611173SJonathan.Adams@Sun.COM mutex_enter(&cpu_lock); 85711173SJonathan.Adams@Sun.COM mutex_enter(&tq->tq_lock); 85811173SJonathan.Adams@Sun.COM tq->tq_cpupart = cpup->cp_id; 85911173SJonathan.Adams@Sun.COM taskq_update_nthreads(tq, cpup->cp_ncpus); 86011173SJonathan.Adams@Sun.COM mutex_exit(&tq->tq_lock); 86111173SJonathan.Adams@Sun.COM 86211173SJonathan.Adams@Sun.COM list_insert_tail(&taskq_cpupct_list, tq); 86311173SJonathan.Adams@Sun.COM mutex_exit(&cpu_lock); 86411173SJonathan.Adams@Sun.COM } 86511173SJonathan.Adams@Sun.COM 86611173SJonathan.Adams@Sun.COM static void 86711173SJonathan.Adams@Sun.COM taskq_cpupct_remove(taskq_t *tq) 86811173SJonathan.Adams@Sun.COM { 86911173SJonathan.Adams@Sun.COM ASSERT(tq->tq_flags & TASKQ_THREADS_CPU_PCT); 87011173SJonathan.Adams@Sun.COM 87111173SJonathan.Adams@Sun.COM mutex_enter(&cpu_lock); 87211173SJonathan.Adams@Sun.COM list_remove(&taskq_cpupct_list, tq); 87311173SJonathan.Adams@Sun.COM mutex_exit(&cpu_lock); 8749515SJonathan.Adams@Sun.COM } 8759515SJonathan.Adams@Sun.COM 8769515SJonathan.Adams@Sun.COM /*ARGSUSED*/ 8779515SJonathan.Adams@Sun.COM static int 8789515SJonathan.Adams@Sun.COM taskq_cpu_setup(cpu_setup_t what, int id, void *arg) 8799515SJonathan.Adams@Sun.COM { 88011173SJonathan.Adams@Sun.COM taskq_t *tq; 88111173SJonathan.Adams@Sun.COM cpupart_t *cp = cpu[id]->cpu_part; 88211173SJonathan.Adams@Sun.COM uint_t ncpus = cp->cp_ncpus; 88311173SJonathan.Adams@Sun.COM 88411173SJonathan.Adams@Sun.COM ASSERT(MUTEX_HELD(&cpu_lock)); 88511173SJonathan.Adams@Sun.COM ASSERT(ncpus > 0); 8869515SJonathan.Adams@Sun.COM 88711173SJonathan.Adams@Sun.COM switch (what) { 88811173SJonathan.Adams@Sun.COM case CPU_OFF: 88911173SJonathan.Adams@Sun.COM case CPU_CPUPART_OUT: 89011173SJonathan.Adams@Sun.COM /* offlines are called *before* the cpu is offlined. */ 89111173SJonathan.Adams@Sun.COM if (ncpus > 1) 89211173SJonathan.Adams@Sun.COM ncpus--; 89311173SJonathan.Adams@Sun.COM break; 8949515SJonathan.Adams@Sun.COM 89511173SJonathan.Adams@Sun.COM case CPU_ON: 89611173SJonathan.Adams@Sun.COM case CPU_CPUPART_IN: 89711173SJonathan.Adams@Sun.COM break; 89811173SJonathan.Adams@Sun.COM 89911173SJonathan.Adams@Sun.COM default: 90011173SJonathan.Adams@Sun.COM return (0); /* doesn't affect cpu count */ 9019515SJonathan.Adams@Sun.COM } 9029515SJonathan.Adams@Sun.COM 90311173SJonathan.Adams@Sun.COM for (tq = list_head(&taskq_cpupct_list); tq != NULL; 90411173SJonathan.Adams@Sun.COM tq = list_next(&taskq_cpupct_list, tq)) { 9059515SJonathan.Adams@Sun.COM 9069515SJonathan.Adams@Sun.COM mutex_enter(&tq->tq_lock); 90711173SJonathan.Adams@Sun.COM /* 90811173SJonathan.Adams@Sun.COM * If the taskq is part of the cpuset which is changing, 90911173SJonathan.Adams@Sun.COM * update its nthreads_target. 91011173SJonathan.Adams@Sun.COM */ 91111173SJonathan.Adams@Sun.COM if (tq->tq_cpupart == cp->cp_id) { 91211173SJonathan.Adams@Sun.COM taskq_update_nthreads(tq, ncpus); 9139515SJonathan.Adams@Sun.COM } 9149515SJonathan.Adams@Sun.COM mutex_exit(&tq->tq_lock); 9159515SJonathan.Adams@Sun.COM } 9169515SJonathan.Adams@Sun.COM return (0); 9179515SJonathan.Adams@Sun.COM } 9189515SJonathan.Adams@Sun.COM 9199515SJonathan.Adams@Sun.COM void 9209515SJonathan.Adams@Sun.COM taskq_mp_init(void) 9219515SJonathan.Adams@Sun.COM { 9229515SJonathan.Adams@Sun.COM mutex_enter(&cpu_lock); 9239515SJonathan.Adams@Sun.COM register_cpu_setup_func(taskq_cpu_setup, NULL); 92411173SJonathan.Adams@Sun.COM /* 92511173SJonathan.Adams@Sun.COM * Make sure we're up to date. At this point in boot, there is only 92611173SJonathan.Adams@Sun.COM * one processor set, so we only have to update the current CPU. 92711173SJonathan.Adams@Sun.COM */ 92811173SJonathan.Adams@Sun.COM (void) taskq_cpu_setup(CPU_ON, CPU->cpu_id, NULL); 9299515SJonathan.Adams@Sun.COM mutex_exit(&cpu_lock); 9300Sstevel@tonic-gate } 9310Sstevel@tonic-gate 9320Sstevel@tonic-gate /* 9330Sstevel@tonic-gate * Create global system dynamic task queue. 9340Sstevel@tonic-gate */ 9350Sstevel@tonic-gate void 9360Sstevel@tonic-gate system_taskq_init(void) 9370Sstevel@tonic-gate { 9380Sstevel@tonic-gate system_taskq = taskq_create_common("system_taskq", 0, 93911173SJonathan.Adams@Sun.COM system_taskq_size * max_ncpus, minclsyspri, 4, 512, &p0, 0, 9400Sstevel@tonic-gate TASKQ_DYNAMIC | TASKQ_PREPOPULATE); 9410Sstevel@tonic-gate } 9420Sstevel@tonic-gate 9430Sstevel@tonic-gate /* 9440Sstevel@tonic-gate * taskq_ent_alloc() 9450Sstevel@tonic-gate * 9460Sstevel@tonic-gate * Allocates a new taskq_ent_t structure either from the free list or from the 9470Sstevel@tonic-gate * cache. Returns NULL if it can't be allocated. 9480Sstevel@tonic-gate * 9490Sstevel@tonic-gate * Assumes: tq->tq_lock is held. 9500Sstevel@tonic-gate */ 9510Sstevel@tonic-gate static taskq_ent_t * 9520Sstevel@tonic-gate taskq_ent_alloc(taskq_t *tq, int flags) 9530Sstevel@tonic-gate { 9540Sstevel@tonic-gate int kmflags = (flags & TQ_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP; 9550Sstevel@tonic-gate 9560Sstevel@tonic-gate taskq_ent_t *tqe; 9570Sstevel@tonic-gate 9580Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tq->tq_lock)); 9590Sstevel@tonic-gate 9600Sstevel@tonic-gate /* 9610Sstevel@tonic-gate * TQ_NOALLOC allocations are allowed to use the freelist, even if 9620Sstevel@tonic-gate * we are below tq_minalloc. 9630Sstevel@tonic-gate */ 9640Sstevel@tonic-gate if ((tqe = tq->tq_freelist) != NULL && 9650Sstevel@tonic-gate ((flags & TQ_NOALLOC) || tq->tq_nalloc >= tq->tq_minalloc)) { 9660Sstevel@tonic-gate tq->tq_freelist = tqe->tqent_next; 9670Sstevel@tonic-gate } else { 9680Sstevel@tonic-gate if (flags & TQ_NOALLOC) 9690Sstevel@tonic-gate return (NULL); 9700Sstevel@tonic-gate 9710Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 9720Sstevel@tonic-gate if (tq->tq_nalloc >= tq->tq_maxalloc) { 9730Sstevel@tonic-gate if (kmflags & KM_NOSLEEP) { 9740Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 9750Sstevel@tonic-gate return (NULL); 9760Sstevel@tonic-gate } 9770Sstevel@tonic-gate /* 9780Sstevel@tonic-gate * We don't want to exceed tq_maxalloc, but we can't 9790Sstevel@tonic-gate * wait for other tasks to complete (and thus free up 9800Sstevel@tonic-gate * task structures) without risking deadlock with 9810Sstevel@tonic-gate * the caller. So, we just delay for one second 9820Sstevel@tonic-gate * to throttle the allocation rate. 9830Sstevel@tonic-gate */ 9840Sstevel@tonic-gate delay(hz); 9850Sstevel@tonic-gate } 9860Sstevel@tonic-gate tqe = kmem_cache_alloc(taskq_ent_cache, kmflags); 9870Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 9880Sstevel@tonic-gate if (tqe != NULL) 9890Sstevel@tonic-gate tq->tq_nalloc++; 9900Sstevel@tonic-gate } 9910Sstevel@tonic-gate return (tqe); 9920Sstevel@tonic-gate } 9930Sstevel@tonic-gate 9940Sstevel@tonic-gate /* 9950Sstevel@tonic-gate * taskq_ent_free() 9960Sstevel@tonic-gate * 9970Sstevel@tonic-gate * Free taskq_ent_t structure by either putting it on the free list or freeing 9980Sstevel@tonic-gate * it to the cache. 9990Sstevel@tonic-gate * 10000Sstevel@tonic-gate * Assumes: tq->tq_lock is held. 10010Sstevel@tonic-gate */ 10020Sstevel@tonic-gate static void 10030Sstevel@tonic-gate taskq_ent_free(taskq_t *tq, taskq_ent_t *tqe) 10040Sstevel@tonic-gate { 10050Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tq->tq_lock)); 10060Sstevel@tonic-gate 10070Sstevel@tonic-gate if (tq->tq_nalloc <= tq->tq_minalloc) { 10080Sstevel@tonic-gate tqe->tqent_next = tq->tq_freelist; 10090Sstevel@tonic-gate tq->tq_freelist = tqe; 10100Sstevel@tonic-gate } else { 10110Sstevel@tonic-gate tq->tq_nalloc--; 10120Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 10130Sstevel@tonic-gate kmem_cache_free(taskq_ent_cache, tqe); 10140Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 10150Sstevel@tonic-gate } 10160Sstevel@tonic-gate } 10170Sstevel@tonic-gate 10180Sstevel@tonic-gate /* 10190Sstevel@tonic-gate * Dispatch a task "func(arg)" to a free entry of bucket b. 10200Sstevel@tonic-gate * 10210Sstevel@tonic-gate * Assumes: no bucket locks is held. 10220Sstevel@tonic-gate * 10230Sstevel@tonic-gate * Returns: a pointer to an entry if dispatch was successful. 10240Sstevel@tonic-gate * NULL if there are no free entries or if the bucket is suspended. 10250Sstevel@tonic-gate */ 10260Sstevel@tonic-gate static taskq_ent_t * 10270Sstevel@tonic-gate taskq_bucket_dispatch(taskq_bucket_t *b, task_func_t func, void *arg) 10280Sstevel@tonic-gate { 10290Sstevel@tonic-gate taskq_ent_t *tqe; 10300Sstevel@tonic-gate 10310Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&b->tqbucket_lock)); 10320Sstevel@tonic-gate ASSERT(func != NULL); 10330Sstevel@tonic-gate 10340Sstevel@tonic-gate mutex_enter(&b->tqbucket_lock); 10350Sstevel@tonic-gate 10360Sstevel@tonic-gate ASSERT(b->tqbucket_nfree != 0 || IS_EMPTY(b->tqbucket_freelist)); 10370Sstevel@tonic-gate ASSERT(b->tqbucket_nfree == 0 || !IS_EMPTY(b->tqbucket_freelist)); 10380Sstevel@tonic-gate 10390Sstevel@tonic-gate /* 10400Sstevel@tonic-gate * Get en entry from the freelist if there is one. 10410Sstevel@tonic-gate * Schedule task into the entry. 10420Sstevel@tonic-gate */ 10430Sstevel@tonic-gate if ((b->tqbucket_nfree != 0) && 10440Sstevel@tonic-gate !(b->tqbucket_flags & TQBUCKET_SUSPEND)) { 10450Sstevel@tonic-gate tqe = b->tqbucket_freelist.tqent_prev; 10460Sstevel@tonic-gate 10470Sstevel@tonic-gate ASSERT(tqe != &b->tqbucket_freelist); 10480Sstevel@tonic-gate ASSERT(tqe->tqent_thread != NULL); 10490Sstevel@tonic-gate 10500Sstevel@tonic-gate tqe->tqent_prev->tqent_next = tqe->tqent_next; 10510Sstevel@tonic-gate tqe->tqent_next->tqent_prev = tqe->tqent_prev; 10520Sstevel@tonic-gate b->tqbucket_nalloc++; 10530Sstevel@tonic-gate b->tqbucket_nfree--; 10540Sstevel@tonic-gate tqe->tqent_func = func; 10550Sstevel@tonic-gate tqe->tqent_arg = arg; 10560Sstevel@tonic-gate TQ_STAT(b, tqs_hits); 10570Sstevel@tonic-gate cv_signal(&tqe->tqent_cv); 10580Sstevel@tonic-gate DTRACE_PROBE2(taskq__d__enqueue, taskq_bucket_t *, b, 10590Sstevel@tonic-gate taskq_ent_t *, tqe); 10600Sstevel@tonic-gate } else { 10610Sstevel@tonic-gate tqe = NULL; 10620Sstevel@tonic-gate TQ_STAT(b, tqs_misses); 10630Sstevel@tonic-gate } 10640Sstevel@tonic-gate mutex_exit(&b->tqbucket_lock); 10650Sstevel@tonic-gate return (tqe); 10660Sstevel@tonic-gate } 10670Sstevel@tonic-gate 10680Sstevel@tonic-gate /* 10690Sstevel@tonic-gate * Dispatch a task. 10700Sstevel@tonic-gate * 10710Sstevel@tonic-gate * Assumes: func != NULL 10720Sstevel@tonic-gate * 10730Sstevel@tonic-gate * Returns: NULL if dispatch failed. 10740Sstevel@tonic-gate * non-NULL if task dispatched successfully. 10750Sstevel@tonic-gate * Actual return value is the pointer to taskq entry that was used to 10760Sstevel@tonic-gate * dispatch a task. This is useful for debugging. 10770Sstevel@tonic-gate */ 10780Sstevel@tonic-gate /* ARGSUSED */ 10790Sstevel@tonic-gate taskqid_t 10800Sstevel@tonic-gate taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags) 10810Sstevel@tonic-gate { 10820Sstevel@tonic-gate taskq_bucket_t *bucket = NULL; /* Which bucket needs extension */ 10830Sstevel@tonic-gate taskq_ent_t *tqe = NULL; 10840Sstevel@tonic-gate taskq_ent_t *tqe1; 10850Sstevel@tonic-gate uint_t bsize; 10860Sstevel@tonic-gate 10870Sstevel@tonic-gate ASSERT(tq != NULL); 10880Sstevel@tonic-gate ASSERT(func != NULL); 10890Sstevel@tonic-gate 10900Sstevel@tonic-gate if (!(tq->tq_flags & TASKQ_DYNAMIC)) { 10910Sstevel@tonic-gate /* 10920Sstevel@tonic-gate * TQ_NOQUEUE flag can't be used with non-dynamic task queues. 10930Sstevel@tonic-gate */ 10940Sstevel@tonic-gate ASSERT(! (flags & TQ_NOQUEUE)); 10950Sstevel@tonic-gate /* 10960Sstevel@tonic-gate * Enqueue the task to the underlying queue. 10970Sstevel@tonic-gate */ 10980Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 10990Sstevel@tonic-gate 11000Sstevel@tonic-gate TASKQ_S_RANDOM_DISPATCH_FAILURE(tq, flags); 11010Sstevel@tonic-gate 11020Sstevel@tonic-gate if ((tqe = taskq_ent_alloc(tq, flags)) == NULL) { 11030Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 11040Sstevel@tonic-gate return (NULL); 11050Sstevel@tonic-gate } 110611173SJonathan.Adams@Sun.COM if (flags & TQ_FRONT) { 110711173SJonathan.Adams@Sun.COM TQ_ENQUEUE_FRONT(tq, tqe, func, arg); 110811173SJonathan.Adams@Sun.COM } else { 110911173SJonathan.Adams@Sun.COM TQ_ENQUEUE(tq, tqe, func, arg); 111011173SJonathan.Adams@Sun.COM } 11110Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 11120Sstevel@tonic-gate return ((taskqid_t)tqe); 11130Sstevel@tonic-gate } 11140Sstevel@tonic-gate 11150Sstevel@tonic-gate /* 11160Sstevel@tonic-gate * Dynamic taskq dispatching. 11170Sstevel@tonic-gate */ 111811173SJonathan.Adams@Sun.COM ASSERT(!(flags & (TQ_NOALLOC | TQ_FRONT))); 11190Sstevel@tonic-gate TASKQ_D_RANDOM_DISPATCH_FAILURE(tq, flags); 11200Sstevel@tonic-gate 11210Sstevel@tonic-gate bsize = tq->tq_nbuckets; 11220Sstevel@tonic-gate 11230Sstevel@tonic-gate if (bsize == 1) { 11240Sstevel@tonic-gate /* 11250Sstevel@tonic-gate * In a single-CPU case there is only one bucket, so get 11260Sstevel@tonic-gate * entry directly from there. 11270Sstevel@tonic-gate */ 11280Sstevel@tonic-gate if ((tqe = taskq_bucket_dispatch(tq->tq_buckets, func, arg)) 11290Sstevel@tonic-gate != NULL) 11300Sstevel@tonic-gate return ((taskqid_t)tqe); /* Fastpath */ 11310Sstevel@tonic-gate bucket = tq->tq_buckets; 11320Sstevel@tonic-gate } else { 11330Sstevel@tonic-gate int loopcount; 11340Sstevel@tonic-gate taskq_bucket_t *b; 11350Sstevel@tonic-gate uintptr_t h = ((uintptr_t)CPU + (uintptr_t)arg) >> 3; 11360Sstevel@tonic-gate 11370Sstevel@tonic-gate h = TQ_HASH(h); 11380Sstevel@tonic-gate 11390Sstevel@tonic-gate /* 11400Sstevel@tonic-gate * The 'bucket' points to the original bucket that we hit. If we 11410Sstevel@tonic-gate * can't allocate from it, we search other buckets, but only 11420Sstevel@tonic-gate * extend this one. 11430Sstevel@tonic-gate */ 11440Sstevel@tonic-gate b = &tq->tq_buckets[h & (bsize - 1)]; 11450Sstevel@tonic-gate ASSERT(b->tqbucket_taskq == tq); /* Sanity check */ 11460Sstevel@tonic-gate 11470Sstevel@tonic-gate /* 11480Sstevel@tonic-gate * Do a quick check before grabbing the lock. If the bucket does 11490Sstevel@tonic-gate * not have free entries now, chances are very small that it 11500Sstevel@tonic-gate * will after we take the lock, so we just skip it. 11510Sstevel@tonic-gate */ 11520Sstevel@tonic-gate if (b->tqbucket_nfree != 0) { 11530Sstevel@tonic-gate if ((tqe = taskq_bucket_dispatch(b, func, arg)) != NULL) 11540Sstevel@tonic-gate return ((taskqid_t)tqe); /* Fastpath */ 11550Sstevel@tonic-gate } else { 11560Sstevel@tonic-gate TQ_STAT(b, tqs_misses); 11570Sstevel@tonic-gate } 11580Sstevel@tonic-gate 11590Sstevel@tonic-gate bucket = b; 11600Sstevel@tonic-gate loopcount = MIN(taskq_search_depth, bsize); 11610Sstevel@tonic-gate /* 11620Sstevel@tonic-gate * If bucket dispatch failed, search loopcount number of buckets 11630Sstevel@tonic-gate * before we give up and fail. 11640Sstevel@tonic-gate */ 11650Sstevel@tonic-gate do { 11660Sstevel@tonic-gate b = &tq->tq_buckets[++h & (bsize - 1)]; 11670Sstevel@tonic-gate ASSERT(b->tqbucket_taskq == tq); /* Sanity check */ 11680Sstevel@tonic-gate loopcount--; 11690Sstevel@tonic-gate 11700Sstevel@tonic-gate if (b->tqbucket_nfree != 0) { 11710Sstevel@tonic-gate tqe = taskq_bucket_dispatch(b, func, arg); 11720Sstevel@tonic-gate } else { 11730Sstevel@tonic-gate TQ_STAT(b, tqs_misses); 11740Sstevel@tonic-gate } 11750Sstevel@tonic-gate } while ((tqe == NULL) && (loopcount > 0)); 11760Sstevel@tonic-gate } 11770Sstevel@tonic-gate 11780Sstevel@tonic-gate /* 11790Sstevel@tonic-gate * At this point we either scheduled a task and (tqe != NULL) or failed 11800Sstevel@tonic-gate * (tqe == NULL). Try to recover from fails. 11810Sstevel@tonic-gate */ 11820Sstevel@tonic-gate 11830Sstevel@tonic-gate /* 11840Sstevel@tonic-gate * For KM_SLEEP dispatches, try to extend the bucket and retry dispatch. 11850Sstevel@tonic-gate */ 11860Sstevel@tonic-gate if ((tqe == NULL) && !(flags & TQ_NOSLEEP)) { 11870Sstevel@tonic-gate /* 11880Sstevel@tonic-gate * taskq_bucket_extend() may fail to do anything, but this is 11890Sstevel@tonic-gate * fine - we deal with it later. If the bucket was successfully 11900Sstevel@tonic-gate * extended, there is a good chance that taskq_bucket_dispatch() 11910Sstevel@tonic-gate * will get this new entry, unless someone is racing with us and 11920Sstevel@tonic-gate * stealing the new entry from under our nose. 11930Sstevel@tonic-gate * taskq_bucket_extend() may sleep. 11940Sstevel@tonic-gate */ 11950Sstevel@tonic-gate taskq_bucket_extend(bucket); 11960Sstevel@tonic-gate TQ_STAT(bucket, tqs_disptcreates); 11970Sstevel@tonic-gate if ((tqe = taskq_bucket_dispatch(bucket, func, arg)) != NULL) 11980Sstevel@tonic-gate return ((taskqid_t)tqe); 11990Sstevel@tonic-gate } 12000Sstevel@tonic-gate 12010Sstevel@tonic-gate ASSERT(bucket != NULL); 12020Sstevel@tonic-gate /* 12030Sstevel@tonic-gate * Since there are not enough free entries in the bucket, extend it 12040Sstevel@tonic-gate * in the background using backing queue. 12050Sstevel@tonic-gate */ 12060Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 12070Sstevel@tonic-gate if ((tqe1 = taskq_ent_alloc(tq, TQ_NOSLEEP)) != NULL) { 120811173SJonathan.Adams@Sun.COM TQ_ENQUEUE(tq, tqe1, taskq_bucket_extend, bucket); 12090Sstevel@tonic-gate } else { 12100Sstevel@tonic-gate TQ_STAT(bucket, tqs_nomem); 12110Sstevel@tonic-gate } 12120Sstevel@tonic-gate 12130Sstevel@tonic-gate /* 12140Sstevel@tonic-gate * Dispatch failed and we can't find an entry to schedule a task. 12150Sstevel@tonic-gate * Revert to the backing queue unless TQ_NOQUEUE was asked. 12160Sstevel@tonic-gate */ 12170Sstevel@tonic-gate if ((tqe == NULL) && !(flags & TQ_NOQUEUE)) { 12180Sstevel@tonic-gate if ((tqe = taskq_ent_alloc(tq, flags)) != NULL) { 12190Sstevel@tonic-gate TQ_ENQUEUE(tq, tqe, func, arg); 12200Sstevel@tonic-gate } else { 12210Sstevel@tonic-gate TQ_STAT(bucket, tqs_nomem); 12220Sstevel@tonic-gate } 12230Sstevel@tonic-gate } 12240Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 12250Sstevel@tonic-gate 12260Sstevel@tonic-gate return ((taskqid_t)tqe); 12270Sstevel@tonic-gate } 12280Sstevel@tonic-gate 12290Sstevel@tonic-gate /* 12300Sstevel@tonic-gate * Wait for all pending tasks to complete. 12310Sstevel@tonic-gate * Calling taskq_wait from a task will cause deadlock. 12320Sstevel@tonic-gate */ 12330Sstevel@tonic-gate void 12340Sstevel@tonic-gate taskq_wait(taskq_t *tq) 12350Sstevel@tonic-gate { 12360Sstevel@tonic-gate ASSERT(tq != curthread->t_taskq); 12370Sstevel@tonic-gate 12380Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 12390Sstevel@tonic-gate while (tq->tq_task.tqent_next != &tq->tq_task || tq->tq_active != 0) 12400Sstevel@tonic-gate cv_wait(&tq->tq_wait_cv, &tq->tq_lock); 12410Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 12420Sstevel@tonic-gate 12430Sstevel@tonic-gate if (tq->tq_flags & TASKQ_DYNAMIC) { 12440Sstevel@tonic-gate taskq_bucket_t *b = tq->tq_buckets; 12450Sstevel@tonic-gate int bid = 0; 12460Sstevel@tonic-gate for (; (b != NULL) && (bid < tq->tq_nbuckets); b++, bid++) { 12470Sstevel@tonic-gate mutex_enter(&b->tqbucket_lock); 12480Sstevel@tonic-gate while (b->tqbucket_nalloc > 0) 12490Sstevel@tonic-gate cv_wait(&b->tqbucket_cv, &b->tqbucket_lock); 12500Sstevel@tonic-gate mutex_exit(&b->tqbucket_lock); 12510Sstevel@tonic-gate } 12520Sstevel@tonic-gate } 12530Sstevel@tonic-gate } 12540Sstevel@tonic-gate 12550Sstevel@tonic-gate /* 12560Sstevel@tonic-gate * Suspend execution of tasks. 12570Sstevel@tonic-gate * 12580Sstevel@tonic-gate * Tasks in the queue part will be suspended immediately upon return from this 12590Sstevel@tonic-gate * function. Pending tasks in the dynamic part will continue to execute, but all 12600Sstevel@tonic-gate * new tasks will be suspended. 12610Sstevel@tonic-gate */ 12620Sstevel@tonic-gate void 12630Sstevel@tonic-gate taskq_suspend(taskq_t *tq) 12640Sstevel@tonic-gate { 12650Sstevel@tonic-gate rw_enter(&tq->tq_threadlock, RW_WRITER); 12660Sstevel@tonic-gate 12670Sstevel@tonic-gate if (tq->tq_flags & TASKQ_DYNAMIC) { 12680Sstevel@tonic-gate taskq_bucket_t *b = tq->tq_buckets; 12690Sstevel@tonic-gate int bid = 0; 12700Sstevel@tonic-gate for (; (b != NULL) && (bid < tq->tq_nbuckets); b++, bid++) { 12710Sstevel@tonic-gate mutex_enter(&b->tqbucket_lock); 12720Sstevel@tonic-gate b->tqbucket_flags |= TQBUCKET_SUSPEND; 12730Sstevel@tonic-gate mutex_exit(&b->tqbucket_lock); 12740Sstevel@tonic-gate } 12750Sstevel@tonic-gate } 12760Sstevel@tonic-gate /* 12770Sstevel@tonic-gate * Mark task queue as being suspended. Needed for taskq_suspended(). 12780Sstevel@tonic-gate */ 12790Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 12800Sstevel@tonic-gate ASSERT(!(tq->tq_flags & TASKQ_SUSPENDED)); 12810Sstevel@tonic-gate tq->tq_flags |= TASKQ_SUSPENDED; 12820Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 12830Sstevel@tonic-gate } 12840Sstevel@tonic-gate 12850Sstevel@tonic-gate /* 12860Sstevel@tonic-gate * returns: 1 if tq is suspended, 0 otherwise. 12870Sstevel@tonic-gate */ 12880Sstevel@tonic-gate int 12890Sstevel@tonic-gate taskq_suspended(taskq_t *tq) 12900Sstevel@tonic-gate { 12910Sstevel@tonic-gate return ((tq->tq_flags & TASKQ_SUSPENDED) != 0); 12920Sstevel@tonic-gate } 12930Sstevel@tonic-gate 12940Sstevel@tonic-gate /* 12950Sstevel@tonic-gate * Resume taskq execution. 12960Sstevel@tonic-gate */ 12970Sstevel@tonic-gate void 12980Sstevel@tonic-gate taskq_resume(taskq_t *tq) 12990Sstevel@tonic-gate { 13000Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&tq->tq_threadlock)); 13010Sstevel@tonic-gate 13020Sstevel@tonic-gate if (tq->tq_flags & TASKQ_DYNAMIC) { 13030Sstevel@tonic-gate taskq_bucket_t *b = tq->tq_buckets; 13040Sstevel@tonic-gate int bid = 0; 13050Sstevel@tonic-gate for (; (b != NULL) && (bid < tq->tq_nbuckets); b++, bid++) { 13060Sstevel@tonic-gate mutex_enter(&b->tqbucket_lock); 13070Sstevel@tonic-gate b->tqbucket_flags &= ~TQBUCKET_SUSPEND; 13080Sstevel@tonic-gate mutex_exit(&b->tqbucket_lock); 13090Sstevel@tonic-gate } 13100Sstevel@tonic-gate } 13110Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 13120Sstevel@tonic-gate ASSERT(tq->tq_flags & TASKQ_SUSPENDED); 13130Sstevel@tonic-gate tq->tq_flags &= ~TASKQ_SUSPENDED; 13140Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 13150Sstevel@tonic-gate 13160Sstevel@tonic-gate rw_exit(&tq->tq_threadlock); 13170Sstevel@tonic-gate } 13180Sstevel@tonic-gate 13190Sstevel@tonic-gate int 13200Sstevel@tonic-gate taskq_member(taskq_t *tq, kthread_t *thread) 13210Sstevel@tonic-gate { 13220Sstevel@tonic-gate return (thread->t_taskq == tq); 13230Sstevel@tonic-gate } 13240Sstevel@tonic-gate 132511173SJonathan.Adams@Sun.COM /* 132611173SJonathan.Adams@Sun.COM * Creates a thread in the taskq. We only allow one outstanding create at 132711173SJonathan.Adams@Sun.COM * a time. We drop and reacquire the tq_lock in order to avoid blocking other 132811173SJonathan.Adams@Sun.COM * taskq activity while thread_create() or lwp_kernel_create() run. 132911173SJonathan.Adams@Sun.COM * 133011173SJonathan.Adams@Sun.COM * The first time we're called, we do some additional setup, and do not 133111173SJonathan.Adams@Sun.COM * return until there are enough threads to start servicing requests. 133211173SJonathan.Adams@Sun.COM */ 13339515SJonathan.Adams@Sun.COM static void 13349515SJonathan.Adams@Sun.COM taskq_thread_create(taskq_t *tq) 13359515SJonathan.Adams@Sun.COM { 133611173SJonathan.Adams@Sun.COM kthread_t *t; 133711173SJonathan.Adams@Sun.COM const boolean_t first = (tq->tq_nthreads == 0); 13389515SJonathan.Adams@Sun.COM 13399515SJonathan.Adams@Sun.COM ASSERT(MUTEX_HELD(&tq->tq_lock)); 134011173SJonathan.Adams@Sun.COM ASSERT(tq->tq_flags & TASKQ_CHANGING); 134111173SJonathan.Adams@Sun.COM ASSERT(tq->tq_nthreads < tq->tq_nthreads_target); 13429515SJonathan.Adams@Sun.COM ASSERT(!(tq->tq_flags & TASKQ_THREAD_CREATED)); 13439515SJonathan.Adams@Sun.COM 134411173SJonathan.Adams@Sun.COM 13459515SJonathan.Adams@Sun.COM tq->tq_flags |= TASKQ_THREAD_CREATED; 13469515SJonathan.Adams@Sun.COM tq->tq_active++; 134711173SJonathan.Adams@Sun.COM mutex_exit(&tq->tq_lock); 134811173SJonathan.Adams@Sun.COM 134911173SJonathan.Adams@Sun.COM if (tq->tq_proc != &p0) { 135011173SJonathan.Adams@Sun.COM t = lwp_kernel_create(tq->tq_proc, taskq_thread, tq, TS_RUN, 135111173SJonathan.Adams@Sun.COM tq->tq_pri); 135211173SJonathan.Adams@Sun.COM } else { 135311173SJonathan.Adams@Sun.COM t = thread_create(NULL, 0, taskq_thread, tq, 0, &p0, TS_RUN, 135411173SJonathan.Adams@Sun.COM tq->tq_pri); 135511173SJonathan.Adams@Sun.COM } 135611173SJonathan.Adams@Sun.COM 135711173SJonathan.Adams@Sun.COM if (!first) { 135811173SJonathan.Adams@Sun.COM mutex_enter(&tq->tq_lock); 135911173SJonathan.Adams@Sun.COM return; 136011173SJonathan.Adams@Sun.COM } 136111173SJonathan.Adams@Sun.COM 136211173SJonathan.Adams@Sun.COM /* 136311173SJonathan.Adams@Sun.COM * We know the thread cannot go away, since tq cannot be 136411173SJonathan.Adams@Sun.COM * destroyed until creation has completed. We can therefore 136511173SJonathan.Adams@Sun.COM * safely dereference t. 136611173SJonathan.Adams@Sun.COM */ 136711173SJonathan.Adams@Sun.COM if (tq->tq_flags & TASKQ_THREADS_CPU_PCT) { 136811173SJonathan.Adams@Sun.COM taskq_cpupct_install(tq, t->t_cpupart); 136911173SJonathan.Adams@Sun.COM } 137011173SJonathan.Adams@Sun.COM mutex_enter(&tq->tq_lock); 137111173SJonathan.Adams@Sun.COM 137211173SJonathan.Adams@Sun.COM /* Wait until we can service requests. */ 137311173SJonathan.Adams@Sun.COM while (tq->tq_nthreads != tq->tq_nthreads_target && 137411173SJonathan.Adams@Sun.COM tq->tq_nthreads < TASKQ_CREATE_ACTIVE_THREADS) { 137511173SJonathan.Adams@Sun.COM cv_wait(&tq->tq_wait_cv, &tq->tq_lock); 137611173SJonathan.Adams@Sun.COM } 13779515SJonathan.Adams@Sun.COM } 13789515SJonathan.Adams@Sun.COM 137910889SJonathan.Adams@Sun.COM /* 138010889SJonathan.Adams@Sun.COM * Common "sleep taskq thread" function, which handles CPR stuff, as well 138110889SJonathan.Adams@Sun.COM * as giving a nice common point for debuggers to find inactive threads. 138210889SJonathan.Adams@Sun.COM */ 138310889SJonathan.Adams@Sun.COM static clock_t 138410889SJonathan.Adams@Sun.COM taskq_thread_wait(taskq_t *tq, kmutex_t *mx, kcondvar_t *cv, 138510889SJonathan.Adams@Sun.COM callb_cpr_t *cprinfo, clock_t timeout) 13869515SJonathan.Adams@Sun.COM { 138710889SJonathan.Adams@Sun.COM clock_t ret = 0; 138810889SJonathan.Adams@Sun.COM 138910889SJonathan.Adams@Sun.COM if (!(tq->tq_flags & TASKQ_CPR_SAFE)) { 13909515SJonathan.Adams@Sun.COM CALLB_CPR_SAFE_BEGIN(cprinfo); 13919515SJonathan.Adams@Sun.COM } 139210889SJonathan.Adams@Sun.COM if (timeout < 0) 139310889SJonathan.Adams@Sun.COM cv_wait(cv, mx); 139410889SJonathan.Adams@Sun.COM else 139511066Srafael.vanoni@sun.com ret = cv_reltimedwait(cv, mx, timeout, TR_CLOCK_TICK); 139610889SJonathan.Adams@Sun.COM 139710889SJonathan.Adams@Sun.COM if (!(tq->tq_flags & TASKQ_CPR_SAFE)) { 139810889SJonathan.Adams@Sun.COM CALLB_CPR_SAFE_END(cprinfo, mx); 139910889SJonathan.Adams@Sun.COM } 140010889SJonathan.Adams@Sun.COM 140110889SJonathan.Adams@Sun.COM return (ret); 14029515SJonathan.Adams@Sun.COM } 14039515SJonathan.Adams@Sun.COM 14040Sstevel@tonic-gate /* 14050Sstevel@tonic-gate * Worker thread for processing task queue. 14060Sstevel@tonic-gate */ 14070Sstevel@tonic-gate static void 14080Sstevel@tonic-gate taskq_thread(void *arg) 14090Sstevel@tonic-gate { 14109515SJonathan.Adams@Sun.COM int thread_id; 14119515SJonathan.Adams@Sun.COM 14120Sstevel@tonic-gate taskq_t *tq = arg; 14130Sstevel@tonic-gate taskq_ent_t *tqe; 14140Sstevel@tonic-gate callb_cpr_t cprinfo; 14150Sstevel@tonic-gate hrtime_t start, end; 14160Sstevel@tonic-gate 141711173SJonathan.Adams@Sun.COM curthread->t_taskq = tq; /* mark ourselves for taskq_member() */ 141811173SJonathan.Adams@Sun.COM 141911173SJonathan.Adams@Sun.COM if (curproc != &p0 && (tq->tq_flags & TASKQ_DUTY_CYCLE)) { 142011173SJonathan.Adams@Sun.COM sysdc_thread_enter(curthread, tq->tq_DC, 142111173SJonathan.Adams@Sun.COM (tq->tq_flags & TASKQ_DC_BATCH) ? SYSDC_THREAD_BATCH : 0); 142211173SJonathan.Adams@Sun.COM } 142311173SJonathan.Adams@Sun.COM 14240Sstevel@tonic-gate if (tq->tq_flags & TASKQ_CPR_SAFE) { 14250Sstevel@tonic-gate CALLB_CPR_INIT_SAFE(curthread, tq->tq_name); 14260Sstevel@tonic-gate } else { 14270Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, &tq->tq_lock, callb_generic_cpr, 14280Sstevel@tonic-gate tq->tq_name); 14290Sstevel@tonic-gate } 14300Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 14319515SJonathan.Adams@Sun.COM thread_id = ++tq->tq_nthreads; 14329515SJonathan.Adams@Sun.COM ASSERT(tq->tq_flags & TASKQ_THREAD_CREATED); 143311173SJonathan.Adams@Sun.COM ASSERT(tq->tq_flags & TASKQ_CHANGING); 14349515SJonathan.Adams@Sun.COM tq->tq_flags &= ~TASKQ_THREAD_CREATED; 14359515SJonathan.Adams@Sun.COM 14369515SJonathan.Adams@Sun.COM VERIFY3S(thread_id, <=, tq->tq_nthreads_max); 14379515SJonathan.Adams@Sun.COM 14389515SJonathan.Adams@Sun.COM if (tq->tq_nthreads_max == 1) 14399515SJonathan.Adams@Sun.COM tq->tq_thread = curthread; 14409515SJonathan.Adams@Sun.COM else 14419515SJonathan.Adams@Sun.COM tq->tq_threadlist[thread_id - 1] = curthread; 14429515SJonathan.Adams@Sun.COM 144311173SJonathan.Adams@Sun.COM /* Allow taskq_create_common()'s taskq_thread_create() to return. */ 144411173SJonathan.Adams@Sun.COM if (tq->tq_nthreads == TASKQ_CREATE_ACTIVE_THREADS) 144511173SJonathan.Adams@Sun.COM cv_broadcast(&tq->tq_wait_cv); 144611173SJonathan.Adams@Sun.COM 14479515SJonathan.Adams@Sun.COM for (;;) { 14489515SJonathan.Adams@Sun.COM if (tq->tq_flags & TASKQ_CHANGING) { 144911173SJonathan.Adams@Sun.COM /* See if we're no longer needed */ 14509515SJonathan.Adams@Sun.COM if (thread_id > tq->tq_nthreads_target) { 14519515SJonathan.Adams@Sun.COM /* 14529515SJonathan.Adams@Sun.COM * To preserve the one-to-one mapping between 14539515SJonathan.Adams@Sun.COM * thread_id and thread, we must exit from 14549515SJonathan.Adams@Sun.COM * highest thread ID to least. 14559515SJonathan.Adams@Sun.COM * 14569515SJonathan.Adams@Sun.COM * However, if everyone is exiting, the order 14579515SJonathan.Adams@Sun.COM * doesn't matter, so just exit immediately. 14589515SJonathan.Adams@Sun.COM * (this is safe, since you must wait for 14599515SJonathan.Adams@Sun.COM * nthreads to reach 0 after setting 14609515SJonathan.Adams@Sun.COM * tq_nthreads_target to 0) 14619515SJonathan.Adams@Sun.COM */ 14629515SJonathan.Adams@Sun.COM if (thread_id == tq->tq_nthreads || 14639515SJonathan.Adams@Sun.COM tq->tq_nthreads_target == 0) 14649515SJonathan.Adams@Sun.COM break; 14659515SJonathan.Adams@Sun.COM 14669515SJonathan.Adams@Sun.COM /* Wait for higher thread_ids to exit */ 146710889SJonathan.Adams@Sun.COM (void) taskq_thread_wait(tq, &tq->tq_lock, 146810889SJonathan.Adams@Sun.COM &tq->tq_exit_cv, &cprinfo, -1); 14699515SJonathan.Adams@Sun.COM continue; 14709515SJonathan.Adams@Sun.COM } 147111173SJonathan.Adams@Sun.COM 147211173SJonathan.Adams@Sun.COM /* 147311173SJonathan.Adams@Sun.COM * If no thread is starting taskq_thread(), we can 147411173SJonathan.Adams@Sun.COM * do some bookkeeping. 147511173SJonathan.Adams@Sun.COM */ 147611173SJonathan.Adams@Sun.COM if (!(tq->tq_flags & TASKQ_THREAD_CREATED)) { 147711173SJonathan.Adams@Sun.COM /* Check if we've reached our target */ 147811173SJonathan.Adams@Sun.COM if (tq->tq_nthreads == tq->tq_nthreads_target) { 147911173SJonathan.Adams@Sun.COM tq->tq_flags &= ~TASKQ_CHANGING; 148011173SJonathan.Adams@Sun.COM cv_broadcast(&tq->tq_wait_cv); 148111173SJonathan.Adams@Sun.COM } 148211173SJonathan.Adams@Sun.COM /* Check if we need to create a thread */ 148311173SJonathan.Adams@Sun.COM if (tq->tq_nthreads < tq->tq_nthreads_target) { 148411173SJonathan.Adams@Sun.COM taskq_thread_create(tq); 148511173SJonathan.Adams@Sun.COM continue; /* tq_lock was dropped */ 148611173SJonathan.Adams@Sun.COM } 148711173SJonathan.Adams@Sun.COM } 14889515SJonathan.Adams@Sun.COM } 14890Sstevel@tonic-gate if ((tqe = tq->tq_task.tqent_next) == &tq->tq_task) { 14900Sstevel@tonic-gate if (--tq->tq_active == 0) 14910Sstevel@tonic-gate cv_broadcast(&tq->tq_wait_cv); 149210889SJonathan.Adams@Sun.COM (void) taskq_thread_wait(tq, &tq->tq_lock, 149310889SJonathan.Adams@Sun.COM &tq->tq_dispatch_cv, &cprinfo, -1); 14940Sstevel@tonic-gate tq->tq_active++; 14950Sstevel@tonic-gate continue; 14960Sstevel@tonic-gate } 149711173SJonathan.Adams@Sun.COM 14980Sstevel@tonic-gate tqe->tqent_prev->tqent_next = tqe->tqent_next; 14990Sstevel@tonic-gate tqe->tqent_next->tqent_prev = tqe->tqent_prev; 15000Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 15010Sstevel@tonic-gate 15020Sstevel@tonic-gate rw_enter(&tq->tq_threadlock, RW_READER); 15030Sstevel@tonic-gate start = gethrtime(); 15040Sstevel@tonic-gate DTRACE_PROBE2(taskq__exec__start, taskq_t *, tq, 15050Sstevel@tonic-gate taskq_ent_t *, tqe); 15060Sstevel@tonic-gate tqe->tqent_func(tqe->tqent_arg); 15070Sstevel@tonic-gate DTRACE_PROBE2(taskq__exec__end, taskq_t *, tq, 15080Sstevel@tonic-gate taskq_ent_t *, tqe); 15090Sstevel@tonic-gate end = gethrtime(); 15100Sstevel@tonic-gate rw_exit(&tq->tq_threadlock); 15110Sstevel@tonic-gate 15120Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 15130Sstevel@tonic-gate tq->tq_totaltime += end - start; 15140Sstevel@tonic-gate tq->tq_executed++; 15150Sstevel@tonic-gate 15160Sstevel@tonic-gate taskq_ent_free(tq, tqe); 15170Sstevel@tonic-gate } 15189515SJonathan.Adams@Sun.COM 15199515SJonathan.Adams@Sun.COM if (tq->tq_nthreads_max == 1) 15209515SJonathan.Adams@Sun.COM tq->tq_thread = NULL; 15219515SJonathan.Adams@Sun.COM else 15229515SJonathan.Adams@Sun.COM tq->tq_threadlist[thread_id - 1] = NULL; 15239515SJonathan.Adams@Sun.COM 15249515SJonathan.Adams@Sun.COM /* We're exiting, and therefore no longer active */ 152511173SJonathan.Adams@Sun.COM ASSERT(tq->tq_active > 0); 15269515SJonathan.Adams@Sun.COM tq->tq_active--; 15279515SJonathan.Adams@Sun.COM 152811173SJonathan.Adams@Sun.COM ASSERT(tq->tq_nthreads > 0); 152911173SJonathan.Adams@Sun.COM tq->tq_nthreads--; 153011173SJonathan.Adams@Sun.COM 153111173SJonathan.Adams@Sun.COM /* Wake up anyone waiting for us to exit */ 153211173SJonathan.Adams@Sun.COM cv_broadcast(&tq->tq_exit_cv); 153311173SJonathan.Adams@Sun.COM if (tq->tq_nthreads == tq->tq_nthreads_target) { 153411173SJonathan.Adams@Sun.COM if (!(tq->tq_flags & TASKQ_THREAD_CREATED)) 153511173SJonathan.Adams@Sun.COM tq->tq_flags &= ~TASKQ_CHANGING; 153611173SJonathan.Adams@Sun.COM 153711173SJonathan.Adams@Sun.COM cv_broadcast(&tq->tq_wait_cv); 153811173SJonathan.Adams@Sun.COM } 153911173SJonathan.Adams@Sun.COM 15400Sstevel@tonic-gate ASSERT(!(tq->tq_flags & TASKQ_CPR_SAFE)); 154111173SJonathan.Adams@Sun.COM CALLB_CPR_EXIT(&cprinfo); /* drops tq->tq_lock */ 154211173SJonathan.Adams@Sun.COM if (curthread->t_lwp != NULL) { 154311173SJonathan.Adams@Sun.COM mutex_enter(&curproc->p_lock); 154411173SJonathan.Adams@Sun.COM lwp_exit(); 154511173SJonathan.Adams@Sun.COM } else { 154611173SJonathan.Adams@Sun.COM thread_exit(); 154711173SJonathan.Adams@Sun.COM } 15480Sstevel@tonic-gate } 15490Sstevel@tonic-gate 15500Sstevel@tonic-gate /* 15510Sstevel@tonic-gate * Worker per-entry thread for dynamic dispatches. 15520Sstevel@tonic-gate */ 15530Sstevel@tonic-gate static void 15540Sstevel@tonic-gate taskq_d_thread(taskq_ent_t *tqe) 15550Sstevel@tonic-gate { 15560Sstevel@tonic-gate taskq_bucket_t *bucket = tqe->tqent_bucket; 15570Sstevel@tonic-gate taskq_t *tq = bucket->tqbucket_taskq; 15580Sstevel@tonic-gate kmutex_t *lock = &bucket->tqbucket_lock; 15590Sstevel@tonic-gate kcondvar_t *cv = &tqe->tqent_cv; 15600Sstevel@tonic-gate callb_cpr_t cprinfo; 15610Sstevel@tonic-gate clock_t w; 15620Sstevel@tonic-gate 15630Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, lock, callb_generic_cpr, tq->tq_name); 15640Sstevel@tonic-gate 15650Sstevel@tonic-gate mutex_enter(lock); 15660Sstevel@tonic-gate 15670Sstevel@tonic-gate for (;;) { 15680Sstevel@tonic-gate /* 15690Sstevel@tonic-gate * If a task is scheduled (func != NULL), execute it, otherwise 15700Sstevel@tonic-gate * sleep, waiting for a job. 15710Sstevel@tonic-gate */ 15720Sstevel@tonic-gate if (tqe->tqent_func != NULL) { 15730Sstevel@tonic-gate hrtime_t start; 15740Sstevel@tonic-gate hrtime_t end; 15750Sstevel@tonic-gate 15760Sstevel@tonic-gate ASSERT(bucket->tqbucket_nalloc > 0); 15770Sstevel@tonic-gate 15780Sstevel@tonic-gate /* 15790Sstevel@tonic-gate * It is possible to free the entry right away before 15800Sstevel@tonic-gate * actually executing the task so that subsequent 15810Sstevel@tonic-gate * dispatches may immediately reuse it. But this, 15820Sstevel@tonic-gate * effectively, creates a two-length queue in the entry 15830Sstevel@tonic-gate * and may lead to a deadlock if the execution of the 15840Sstevel@tonic-gate * current task depends on the execution of the next 15850Sstevel@tonic-gate * scheduled task. So, we keep the entry busy until the 15860Sstevel@tonic-gate * task is processed. 15870Sstevel@tonic-gate */ 15880Sstevel@tonic-gate 15890Sstevel@tonic-gate mutex_exit(lock); 15900Sstevel@tonic-gate start = gethrtime(); 15910Sstevel@tonic-gate DTRACE_PROBE3(taskq__d__exec__start, taskq_t *, tq, 15920Sstevel@tonic-gate taskq_bucket_t *, bucket, taskq_ent_t *, tqe); 15930Sstevel@tonic-gate tqe->tqent_func(tqe->tqent_arg); 15940Sstevel@tonic-gate DTRACE_PROBE3(taskq__d__exec__end, taskq_t *, tq, 15950Sstevel@tonic-gate taskq_bucket_t *, bucket, taskq_ent_t *, tqe); 15960Sstevel@tonic-gate end = gethrtime(); 15970Sstevel@tonic-gate mutex_enter(lock); 15980Sstevel@tonic-gate bucket->tqbucket_totaltime += end - start; 15990Sstevel@tonic-gate 16000Sstevel@tonic-gate /* 16010Sstevel@tonic-gate * Return the entry to the bucket free list. 16020Sstevel@tonic-gate */ 16030Sstevel@tonic-gate tqe->tqent_func = NULL; 16040Sstevel@tonic-gate TQ_APPEND(bucket->tqbucket_freelist, tqe); 16050Sstevel@tonic-gate bucket->tqbucket_nalloc--; 16060Sstevel@tonic-gate bucket->tqbucket_nfree++; 16070Sstevel@tonic-gate ASSERT(!IS_EMPTY(bucket->tqbucket_freelist)); 16080Sstevel@tonic-gate /* 16090Sstevel@tonic-gate * taskq_wait() waits for nalloc to drop to zero on 16100Sstevel@tonic-gate * tqbucket_cv. 16110Sstevel@tonic-gate */ 16120Sstevel@tonic-gate cv_signal(&bucket->tqbucket_cv); 16130Sstevel@tonic-gate } 16140Sstevel@tonic-gate 16150Sstevel@tonic-gate /* 16160Sstevel@tonic-gate * At this point the entry must be in the bucket free list - 16170Sstevel@tonic-gate * either because it was there initially or because it just 16180Sstevel@tonic-gate * finished executing a task and put itself on the free list. 16190Sstevel@tonic-gate */ 16200Sstevel@tonic-gate ASSERT(bucket->tqbucket_nfree > 0); 16210Sstevel@tonic-gate /* 16220Sstevel@tonic-gate * Go to sleep unless we are closing. 16230Sstevel@tonic-gate * If a thread is sleeping too long, it dies. 16240Sstevel@tonic-gate */ 16250Sstevel@tonic-gate if (! (bucket->tqbucket_flags & TQBUCKET_CLOSE)) { 162610889SJonathan.Adams@Sun.COM w = taskq_thread_wait(tq, lock, cv, 162710889SJonathan.Adams@Sun.COM &cprinfo, taskq_thread_timeout * hz); 16280Sstevel@tonic-gate } 16290Sstevel@tonic-gate 16300Sstevel@tonic-gate /* 16310Sstevel@tonic-gate * At this point we may be in two different states: 16320Sstevel@tonic-gate * 16330Sstevel@tonic-gate * (1) tqent_func is set which means that a new task is 16340Sstevel@tonic-gate * dispatched and we need to execute it. 16350Sstevel@tonic-gate * 16360Sstevel@tonic-gate * (2) Thread is sleeping for too long or we are closing. In 16370Sstevel@tonic-gate * both cases destroy the thread and the entry. 16380Sstevel@tonic-gate */ 16390Sstevel@tonic-gate 16400Sstevel@tonic-gate /* If func is NULL we should be on the freelist. */ 16410Sstevel@tonic-gate ASSERT((tqe->tqent_func != NULL) || 16420Sstevel@tonic-gate (bucket->tqbucket_nfree > 0)); 16430Sstevel@tonic-gate /* If func is non-NULL we should be allocated */ 16440Sstevel@tonic-gate ASSERT((tqe->tqent_func == NULL) || 16450Sstevel@tonic-gate (bucket->tqbucket_nalloc > 0)); 16460Sstevel@tonic-gate 16470Sstevel@tonic-gate /* Check freelist consistency */ 16480Sstevel@tonic-gate ASSERT((bucket->tqbucket_nfree > 0) || 16490Sstevel@tonic-gate IS_EMPTY(bucket->tqbucket_freelist)); 16500Sstevel@tonic-gate ASSERT((bucket->tqbucket_nfree == 0) || 16510Sstevel@tonic-gate !IS_EMPTY(bucket->tqbucket_freelist)); 16520Sstevel@tonic-gate 16530Sstevel@tonic-gate if ((tqe->tqent_func == NULL) && 16540Sstevel@tonic-gate ((w == -1) || (bucket->tqbucket_flags & TQBUCKET_CLOSE))) { 16550Sstevel@tonic-gate /* 16560Sstevel@tonic-gate * This thread is sleeping for too long or we are 16570Sstevel@tonic-gate * closing - time to die. 16580Sstevel@tonic-gate * Thread creation/destruction happens rarely, 16590Sstevel@tonic-gate * so grabbing the lock is not a big performance issue. 16600Sstevel@tonic-gate * The bucket lock is dropped by CALLB_CPR_EXIT(). 16610Sstevel@tonic-gate */ 16620Sstevel@tonic-gate 16630Sstevel@tonic-gate /* Remove the entry from the free list. */ 16640Sstevel@tonic-gate tqe->tqent_prev->tqent_next = tqe->tqent_next; 16650Sstevel@tonic-gate tqe->tqent_next->tqent_prev = tqe->tqent_prev; 16660Sstevel@tonic-gate ASSERT(bucket->tqbucket_nfree > 0); 16670Sstevel@tonic-gate bucket->tqbucket_nfree--; 16680Sstevel@tonic-gate 16690Sstevel@tonic-gate TQ_STAT(bucket, tqs_tdeaths); 16700Sstevel@tonic-gate cv_signal(&bucket->tqbucket_cv); 16710Sstevel@tonic-gate tqe->tqent_thread = NULL; 16720Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 16730Sstevel@tonic-gate tq->tq_tdeaths++; 16740Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 16750Sstevel@tonic-gate CALLB_CPR_EXIT(&cprinfo); 16760Sstevel@tonic-gate kmem_cache_free(taskq_ent_cache, tqe); 16770Sstevel@tonic-gate thread_exit(); 16780Sstevel@tonic-gate } 16790Sstevel@tonic-gate } 16800Sstevel@tonic-gate } 16810Sstevel@tonic-gate 16820Sstevel@tonic-gate 16830Sstevel@tonic-gate /* 16840Sstevel@tonic-gate * Taskq creation. May sleep for memory. 16850Sstevel@tonic-gate * Always use automatically generated instances to avoid kstat name space 16860Sstevel@tonic-gate * collisions. 16870Sstevel@tonic-gate */ 16880Sstevel@tonic-gate 16890Sstevel@tonic-gate taskq_t * 16900Sstevel@tonic-gate taskq_create(const char *name, int nthreads, pri_t pri, int minalloc, 16910Sstevel@tonic-gate int maxalloc, uint_t flags) 16920Sstevel@tonic-gate { 169311173SJonathan.Adams@Sun.COM ASSERT((flags & ~TASKQ_INTERFACE_FLAGS) == 0); 169411173SJonathan.Adams@Sun.COM 169511173SJonathan.Adams@Sun.COM return (taskq_create_common(name, 0, nthreads, pri, minalloc, 169611173SJonathan.Adams@Sun.COM maxalloc, &p0, 0, flags | TASKQ_NOINSTANCE)); 16970Sstevel@tonic-gate } 16980Sstevel@tonic-gate 16990Sstevel@tonic-gate /* 17000Sstevel@tonic-gate * Create an instance of task queue. It is legal to create task queues with the 17010Sstevel@tonic-gate * same name and different instances. 17020Sstevel@tonic-gate * 17030Sstevel@tonic-gate * taskq_create_instance is used by ddi_taskq_create() where it gets the 17040Sstevel@tonic-gate * instance from ddi_get_instance(). In some cases the instance is not 17050Sstevel@tonic-gate * initialized and is set to -1. This case is handled as if no instance was 17060Sstevel@tonic-gate * passed at all. 17070Sstevel@tonic-gate */ 17080Sstevel@tonic-gate taskq_t * 17090Sstevel@tonic-gate taskq_create_instance(const char *name, int instance, int nthreads, pri_t pri, 17100Sstevel@tonic-gate int minalloc, int maxalloc, uint_t flags) 17110Sstevel@tonic-gate { 171211173SJonathan.Adams@Sun.COM ASSERT((flags & ~TASKQ_INTERFACE_FLAGS) == 0); 17130Sstevel@tonic-gate ASSERT((instance >= 0) || (instance == -1)); 17140Sstevel@tonic-gate 17150Sstevel@tonic-gate if (instance < 0) { 17160Sstevel@tonic-gate flags |= TASKQ_NOINSTANCE; 17170Sstevel@tonic-gate } 17180Sstevel@tonic-gate 17190Sstevel@tonic-gate return (taskq_create_common(name, instance, nthreads, 172011173SJonathan.Adams@Sun.COM pri, minalloc, maxalloc, &p0, 0, flags)); 172111173SJonathan.Adams@Sun.COM } 172211173SJonathan.Adams@Sun.COM 172311173SJonathan.Adams@Sun.COM taskq_t * 172411173SJonathan.Adams@Sun.COM taskq_create_proc(const char *name, int nthreads, pri_t pri, int minalloc, 172511173SJonathan.Adams@Sun.COM int maxalloc, proc_t *proc, uint_t flags) 172611173SJonathan.Adams@Sun.COM { 172711173SJonathan.Adams@Sun.COM ASSERT((flags & ~TASKQ_INTERFACE_FLAGS) == 0); 172811173SJonathan.Adams@Sun.COM ASSERT(proc->p_flag & SSYS); 172911173SJonathan.Adams@Sun.COM 173011173SJonathan.Adams@Sun.COM return (taskq_create_common(name, 0, nthreads, pri, minalloc, 173111173SJonathan.Adams@Sun.COM maxalloc, proc, 0, flags | TASKQ_NOINSTANCE)); 17320Sstevel@tonic-gate } 17330Sstevel@tonic-gate 173411173SJonathan.Adams@Sun.COM taskq_t * 173511173SJonathan.Adams@Sun.COM taskq_create_sysdc(const char *name, int nthreads, int minalloc, 173611173SJonathan.Adams@Sun.COM int maxalloc, proc_t *proc, uint_t dc, uint_t flags) 173711173SJonathan.Adams@Sun.COM { 173811173SJonathan.Adams@Sun.COM ASSERT((flags & ~TASKQ_INTERFACE_FLAGS) == 0); 173911173SJonathan.Adams@Sun.COM ASSERT(proc->p_flag & SSYS); 174011173SJonathan.Adams@Sun.COM 174111173SJonathan.Adams@Sun.COM return (taskq_create_common(name, 0, nthreads, minclsyspri, minalloc, 174211173SJonathan.Adams@Sun.COM maxalloc, proc, dc, flags | TASKQ_NOINSTANCE | TASKQ_DUTY_CYCLE)); 174311173SJonathan.Adams@Sun.COM } 174411173SJonathan.Adams@Sun.COM 17450Sstevel@tonic-gate static taskq_t * 17460Sstevel@tonic-gate taskq_create_common(const char *name, int instance, int nthreads, pri_t pri, 174711173SJonathan.Adams@Sun.COM int minalloc, int maxalloc, proc_t *proc, uint_t dc, uint_t flags) 17480Sstevel@tonic-gate { 17490Sstevel@tonic-gate taskq_t *tq = kmem_cache_alloc(taskq_cache, KM_SLEEP); 17500Sstevel@tonic-gate uint_t ncpus = ((boot_max_ncpus == -1) ? max_ncpus : boot_max_ncpus); 17510Sstevel@tonic-gate uint_t bsize; /* # of buckets - always power of 2 */ 17529515SJonathan.Adams@Sun.COM int max_nthreads; 17530Sstevel@tonic-gate 17540Sstevel@tonic-gate /* 175511173SJonathan.Adams@Sun.COM * TASKQ_DYNAMIC, TASKQ_CPR_SAFE and TASKQ_THREADS_CPU_PCT are all 175611173SJonathan.Adams@Sun.COM * mutually incompatible. 17570Sstevel@tonic-gate */ 175811173SJonathan.Adams@Sun.COM IMPLY((flags & TASKQ_DYNAMIC), !(flags & TASKQ_CPR_SAFE)); 175911173SJonathan.Adams@Sun.COM IMPLY((flags & TASKQ_DYNAMIC), !(flags & TASKQ_THREADS_CPU_PCT)); 176011173SJonathan.Adams@Sun.COM IMPLY((flags & TASKQ_CPR_SAFE), !(flags & TASKQ_THREADS_CPU_PCT)); 17610Sstevel@tonic-gate 176211173SJonathan.Adams@Sun.COM /* Cannot have DUTY_CYCLE without a non-p0 kernel process */ 176311173SJonathan.Adams@Sun.COM IMPLY((flags & TASKQ_DUTY_CYCLE), proc != &p0); 176411173SJonathan.Adams@Sun.COM 176511173SJonathan.Adams@Sun.COM /* Cannot have DC_BATCH without DUTY_CYCLE */ 176611173SJonathan.Adams@Sun.COM ASSERT((flags & (TASKQ_DUTY_CYCLE|TASKQ_DC_BATCH)) != TASKQ_DC_BATCH); 176711173SJonathan.Adams@Sun.COM 176811173SJonathan.Adams@Sun.COM ASSERT(proc != NULL); 17690Sstevel@tonic-gate 17700Sstevel@tonic-gate bsize = 1 << (highbit(ncpus) - 1); 17710Sstevel@tonic-gate ASSERT(bsize >= 1); 17720Sstevel@tonic-gate bsize = MIN(bsize, taskq_maxbuckets); 17730Sstevel@tonic-gate 17749515SJonathan.Adams@Sun.COM if (flags & TASKQ_DYNAMIC) { 17759515SJonathan.Adams@Sun.COM ASSERT3S(nthreads, >=, 1); 17769515SJonathan.Adams@Sun.COM tq->tq_maxsize = nthreads; 17779515SJonathan.Adams@Sun.COM 17789515SJonathan.Adams@Sun.COM /* For dynamic task queues use just one backup thread */ 17799515SJonathan.Adams@Sun.COM nthreads = max_nthreads = 1; 17809515SJonathan.Adams@Sun.COM 178111173SJonathan.Adams@Sun.COM } else if (flags & TASKQ_THREADS_CPU_PCT) { 17829515SJonathan.Adams@Sun.COM uint_t pct; 17839515SJonathan.Adams@Sun.COM ASSERT3S(nthreads, >=, 0); 17849515SJonathan.Adams@Sun.COM pct = nthreads; 17850Sstevel@tonic-gate 17869515SJonathan.Adams@Sun.COM if (pct > taskq_cpupct_max_percent) 17879515SJonathan.Adams@Sun.COM pct = taskq_cpupct_max_percent; 17889515SJonathan.Adams@Sun.COM 178911173SJonathan.Adams@Sun.COM /* 179011173SJonathan.Adams@Sun.COM * If you're using THREADS_CPU_PCT, the process for the 179111173SJonathan.Adams@Sun.COM * taskq threads must be curproc. This allows any pset 179211173SJonathan.Adams@Sun.COM * binding to be inherited correctly. If proc is &p0, 179311173SJonathan.Adams@Sun.COM * we won't be creating LWPs, so new threads will be assigned 179411173SJonathan.Adams@Sun.COM * to the default processor set. 179511173SJonathan.Adams@Sun.COM */ 179611173SJonathan.Adams@Sun.COM ASSERT(curproc == proc || proc == &p0); 17979515SJonathan.Adams@Sun.COM tq->tq_threads_ncpus_pct = pct; 179811173SJonathan.Adams@Sun.COM nthreads = 1; /* corrected in taskq_thread_create() */ 17999515SJonathan.Adams@Sun.COM max_nthreads = TASKQ_THREADS_PCT(max_ncpus, pct); 180011173SJonathan.Adams@Sun.COM 180111173SJonathan.Adams@Sun.COM } else { 180211173SJonathan.Adams@Sun.COM ASSERT3S(nthreads, >=, 1); 180311173SJonathan.Adams@Sun.COM max_nthreads = nthreads; 18049515SJonathan.Adams@Sun.COM } 18050Sstevel@tonic-gate 18069515SJonathan.Adams@Sun.COM if (max_nthreads < taskq_minimum_nthreads_max) 18079515SJonathan.Adams@Sun.COM max_nthreads = taskq_minimum_nthreads_max; 18089515SJonathan.Adams@Sun.COM 18099515SJonathan.Adams@Sun.COM /* 18109515SJonathan.Adams@Sun.COM * Make sure the name is 0-terminated, and conforms to the rules for 18119515SJonathan.Adams@Sun.COM * C indentifiers 18129515SJonathan.Adams@Sun.COM */ 18130Sstevel@tonic-gate (void) strncpy(tq->tq_name, name, TASKQ_NAMELEN + 1); 18149515SJonathan.Adams@Sun.COM strident_canon(tq->tq_name, TASKQ_NAMELEN + 1); 18150Sstevel@tonic-gate 18169515SJonathan.Adams@Sun.COM tq->tq_flags = flags | TASKQ_CHANGING; 18179515SJonathan.Adams@Sun.COM tq->tq_active = 0; 18180Sstevel@tonic-gate tq->tq_instance = instance; 18199515SJonathan.Adams@Sun.COM tq->tq_nthreads_target = nthreads; 18209515SJonathan.Adams@Sun.COM tq->tq_nthreads_max = max_nthreads; 18210Sstevel@tonic-gate tq->tq_minalloc = minalloc; 18220Sstevel@tonic-gate tq->tq_maxalloc = maxalloc; 18230Sstevel@tonic-gate tq->tq_nbuckets = bsize; 182411173SJonathan.Adams@Sun.COM tq->tq_proc = proc; 18250Sstevel@tonic-gate tq->tq_pri = pri; 182611173SJonathan.Adams@Sun.COM tq->tq_DC = dc; 182711173SJonathan.Adams@Sun.COM list_link_init(&tq->tq_cpupct_link); 18280Sstevel@tonic-gate 18299515SJonathan.Adams@Sun.COM if (max_nthreads > 1) 18309515SJonathan.Adams@Sun.COM tq->tq_threadlist = kmem_alloc( 18319515SJonathan.Adams@Sun.COM sizeof (kthread_t *) * max_nthreads, KM_SLEEP); 18329515SJonathan.Adams@Sun.COM 18339515SJonathan.Adams@Sun.COM mutex_enter(&tq->tq_lock); 18349515SJonathan.Adams@Sun.COM if (flags & TASKQ_PREPOPULATE) { 18359515SJonathan.Adams@Sun.COM while (minalloc-- > 0) 18369515SJonathan.Adams@Sun.COM taskq_ent_free(tq, taskq_ent_alloc(tq, TQ_SLEEP)); 18379515SJonathan.Adams@Sun.COM } 18380Sstevel@tonic-gate 183911173SJonathan.Adams@Sun.COM /* 184011173SJonathan.Adams@Sun.COM * Create the first thread, which will create any other threads 184111173SJonathan.Adams@Sun.COM * necessary. taskq_thread_create will not return until we have 184211173SJonathan.Adams@Sun.COM * enough threads to be able to process requests. 184311173SJonathan.Adams@Sun.COM */ 18449515SJonathan.Adams@Sun.COM taskq_thread_create(tq); 18459515SJonathan.Adams@Sun.COM mutex_exit(&tq->tq_lock); 18460Sstevel@tonic-gate 18470Sstevel@tonic-gate if (flags & TASKQ_DYNAMIC) { 18480Sstevel@tonic-gate taskq_bucket_t *bucket = kmem_zalloc(sizeof (taskq_bucket_t) * 18490Sstevel@tonic-gate bsize, KM_SLEEP); 18500Sstevel@tonic-gate int b_id; 18510Sstevel@tonic-gate 18520Sstevel@tonic-gate tq->tq_buckets = bucket; 18530Sstevel@tonic-gate 18540Sstevel@tonic-gate /* Initialize each bucket */ 18550Sstevel@tonic-gate for (b_id = 0; b_id < bsize; b_id++, bucket++) { 18560Sstevel@tonic-gate mutex_init(&bucket->tqbucket_lock, NULL, MUTEX_DEFAULT, 18570Sstevel@tonic-gate NULL); 18580Sstevel@tonic-gate cv_init(&bucket->tqbucket_cv, NULL, CV_DEFAULT, NULL); 18590Sstevel@tonic-gate bucket->tqbucket_taskq = tq; 18600Sstevel@tonic-gate bucket->tqbucket_freelist.tqent_next = 18610Sstevel@tonic-gate bucket->tqbucket_freelist.tqent_prev = 18620Sstevel@tonic-gate &bucket->tqbucket_freelist; 18630Sstevel@tonic-gate if (flags & TASKQ_PREPOPULATE) 18640Sstevel@tonic-gate taskq_bucket_extend(bucket); 18650Sstevel@tonic-gate } 18660Sstevel@tonic-gate } 18670Sstevel@tonic-gate 18680Sstevel@tonic-gate /* 18690Sstevel@tonic-gate * Install kstats. 18700Sstevel@tonic-gate * We have two cases: 18710Sstevel@tonic-gate * 1) Instance is provided to taskq_create_instance(). In this case it 187211173SJonathan.Adams@Sun.COM * should be >= 0 and we use it. 18730Sstevel@tonic-gate * 18740Sstevel@tonic-gate * 2) Instance is not provided and is automatically generated 18750Sstevel@tonic-gate */ 18760Sstevel@tonic-gate if (flags & TASKQ_NOINSTANCE) { 18770Sstevel@tonic-gate instance = tq->tq_instance = 18780Sstevel@tonic-gate (int)(uintptr_t)vmem_alloc(taskq_id_arena, 1, VM_SLEEP); 18790Sstevel@tonic-gate } 18800Sstevel@tonic-gate 18810Sstevel@tonic-gate if (flags & TASKQ_DYNAMIC) { 18820Sstevel@tonic-gate if ((tq->tq_kstat = kstat_create("unix", instance, 18839515SJonathan.Adams@Sun.COM tq->tq_name, "taskq_d", KSTAT_TYPE_NAMED, 18849515SJonathan.Adams@Sun.COM sizeof (taskq_d_kstat) / sizeof (kstat_named_t), 18859515SJonathan.Adams@Sun.COM KSTAT_FLAG_VIRTUAL)) != NULL) { 18860Sstevel@tonic-gate tq->tq_kstat->ks_lock = &taskq_d_kstat_lock; 18870Sstevel@tonic-gate tq->tq_kstat->ks_data = &taskq_d_kstat; 18880Sstevel@tonic-gate tq->tq_kstat->ks_update = taskq_d_kstat_update; 18890Sstevel@tonic-gate tq->tq_kstat->ks_private = tq; 18900Sstevel@tonic-gate kstat_install(tq->tq_kstat); 18910Sstevel@tonic-gate } 18920Sstevel@tonic-gate } else { 18930Sstevel@tonic-gate if ((tq->tq_kstat = kstat_create("unix", instance, tq->tq_name, 18949515SJonathan.Adams@Sun.COM "taskq", KSTAT_TYPE_NAMED, 18959515SJonathan.Adams@Sun.COM sizeof (taskq_kstat) / sizeof (kstat_named_t), 18969515SJonathan.Adams@Sun.COM KSTAT_FLAG_VIRTUAL)) != NULL) { 18970Sstevel@tonic-gate tq->tq_kstat->ks_lock = &taskq_kstat_lock; 18980Sstevel@tonic-gate tq->tq_kstat->ks_data = &taskq_kstat; 18990Sstevel@tonic-gate tq->tq_kstat->ks_update = taskq_kstat_update; 19000Sstevel@tonic-gate tq->tq_kstat->ks_private = tq; 19010Sstevel@tonic-gate kstat_install(tq->tq_kstat); 19020Sstevel@tonic-gate } 19030Sstevel@tonic-gate } 19040Sstevel@tonic-gate 19050Sstevel@tonic-gate return (tq); 19060Sstevel@tonic-gate } 19070Sstevel@tonic-gate 19080Sstevel@tonic-gate /* 19090Sstevel@tonic-gate * taskq_destroy(). 19100Sstevel@tonic-gate * 19110Sstevel@tonic-gate * Assumes: by the time taskq_destroy is called no one will use this task queue 19120Sstevel@tonic-gate * in any way and no one will try to dispatch entries in it. 19130Sstevel@tonic-gate */ 19140Sstevel@tonic-gate void 19150Sstevel@tonic-gate taskq_destroy(taskq_t *tq) 19160Sstevel@tonic-gate { 19170Sstevel@tonic-gate taskq_bucket_t *b = tq->tq_buckets; 19180Sstevel@tonic-gate int bid = 0; 19190Sstevel@tonic-gate 19200Sstevel@tonic-gate ASSERT(! (tq->tq_flags & TASKQ_CPR_SAFE)); 19210Sstevel@tonic-gate 19220Sstevel@tonic-gate /* 19230Sstevel@tonic-gate * Destroy kstats. 19240Sstevel@tonic-gate */ 19250Sstevel@tonic-gate if (tq->tq_kstat != NULL) { 19260Sstevel@tonic-gate kstat_delete(tq->tq_kstat); 19270Sstevel@tonic-gate tq->tq_kstat = NULL; 19280Sstevel@tonic-gate } 19290Sstevel@tonic-gate 19300Sstevel@tonic-gate /* 19310Sstevel@tonic-gate * Destroy instance if needed. 19320Sstevel@tonic-gate */ 19330Sstevel@tonic-gate if (tq->tq_flags & TASKQ_NOINSTANCE) { 19340Sstevel@tonic-gate vmem_free(taskq_id_arena, (void *)(uintptr_t)(tq->tq_instance), 19350Sstevel@tonic-gate 1); 19360Sstevel@tonic-gate tq->tq_instance = 0; 19370Sstevel@tonic-gate } 19380Sstevel@tonic-gate 19390Sstevel@tonic-gate /* 19409515SJonathan.Adams@Sun.COM * Unregister from the cpupct list. 19419515SJonathan.Adams@Sun.COM */ 19429515SJonathan.Adams@Sun.COM if (tq->tq_flags & TASKQ_THREADS_CPU_PCT) { 194311173SJonathan.Adams@Sun.COM taskq_cpupct_remove(tq); 19449515SJonathan.Adams@Sun.COM } 19459515SJonathan.Adams@Sun.COM 19469515SJonathan.Adams@Sun.COM /* 19470Sstevel@tonic-gate * Wait for any pending entries to complete. 19480Sstevel@tonic-gate */ 19490Sstevel@tonic-gate taskq_wait(tq); 19500Sstevel@tonic-gate 19510Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 19520Sstevel@tonic-gate ASSERT((tq->tq_task.tqent_next == &tq->tq_task) && 19530Sstevel@tonic-gate (tq->tq_active == 0)); 19540Sstevel@tonic-gate 19559515SJonathan.Adams@Sun.COM /* notify all the threads that they need to exit */ 19569515SJonathan.Adams@Sun.COM tq->tq_nthreads_target = 0; 19570Sstevel@tonic-gate 19589515SJonathan.Adams@Sun.COM tq->tq_flags |= TASKQ_CHANGING; 19590Sstevel@tonic-gate cv_broadcast(&tq->tq_dispatch_cv); 19609515SJonathan.Adams@Sun.COM cv_broadcast(&tq->tq_exit_cv); 19619515SJonathan.Adams@Sun.COM 19620Sstevel@tonic-gate while (tq->tq_nthreads != 0) 19630Sstevel@tonic-gate cv_wait(&tq->tq_wait_cv, &tq->tq_lock); 19640Sstevel@tonic-gate 19659515SJonathan.Adams@Sun.COM if (tq->tq_nthreads_max != 1) 19669515SJonathan.Adams@Sun.COM kmem_free(tq->tq_threadlist, sizeof (kthread_t *) * 19679515SJonathan.Adams@Sun.COM tq->tq_nthreads_max); 19689515SJonathan.Adams@Sun.COM 19690Sstevel@tonic-gate tq->tq_minalloc = 0; 19700Sstevel@tonic-gate while (tq->tq_nalloc != 0) 19710Sstevel@tonic-gate taskq_ent_free(tq, taskq_ent_alloc(tq, TQ_SLEEP)); 19720Sstevel@tonic-gate 19730Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 19740Sstevel@tonic-gate 19750Sstevel@tonic-gate /* 19760Sstevel@tonic-gate * Mark each bucket as closing and wakeup all sleeping threads. 19770Sstevel@tonic-gate */ 19780Sstevel@tonic-gate for (; (b != NULL) && (bid < tq->tq_nbuckets); b++, bid++) { 19790Sstevel@tonic-gate taskq_ent_t *tqe; 19800Sstevel@tonic-gate 19810Sstevel@tonic-gate mutex_enter(&b->tqbucket_lock); 19820Sstevel@tonic-gate 19830Sstevel@tonic-gate b->tqbucket_flags |= TQBUCKET_CLOSE; 19840Sstevel@tonic-gate /* Wakeup all sleeping threads */ 19850Sstevel@tonic-gate 19860Sstevel@tonic-gate for (tqe = b->tqbucket_freelist.tqent_next; 19870Sstevel@tonic-gate tqe != &b->tqbucket_freelist; tqe = tqe->tqent_next) 19880Sstevel@tonic-gate cv_signal(&tqe->tqent_cv); 19890Sstevel@tonic-gate 19900Sstevel@tonic-gate ASSERT(b->tqbucket_nalloc == 0); 19910Sstevel@tonic-gate 19920Sstevel@tonic-gate /* 19930Sstevel@tonic-gate * At this point we waited for all pending jobs to complete (in 19940Sstevel@tonic-gate * both the task queue and the bucket and no new jobs should 19950Sstevel@tonic-gate * arrive. Wait for all threads to die. 19960Sstevel@tonic-gate */ 19970Sstevel@tonic-gate while (b->tqbucket_nfree > 0) 19980Sstevel@tonic-gate cv_wait(&b->tqbucket_cv, &b->tqbucket_lock); 19990Sstevel@tonic-gate mutex_exit(&b->tqbucket_lock); 20000Sstevel@tonic-gate mutex_destroy(&b->tqbucket_lock); 20010Sstevel@tonic-gate cv_destroy(&b->tqbucket_cv); 20020Sstevel@tonic-gate } 20030Sstevel@tonic-gate 20040Sstevel@tonic-gate if (tq->tq_buckets != NULL) { 20050Sstevel@tonic-gate ASSERT(tq->tq_flags & TASKQ_DYNAMIC); 20060Sstevel@tonic-gate kmem_free(tq->tq_buckets, 20070Sstevel@tonic-gate sizeof (taskq_bucket_t) * tq->tq_nbuckets); 20080Sstevel@tonic-gate 20090Sstevel@tonic-gate /* Cleanup fields before returning tq to the cache */ 20100Sstevel@tonic-gate tq->tq_buckets = NULL; 20110Sstevel@tonic-gate tq->tq_tcreates = 0; 20120Sstevel@tonic-gate tq->tq_tdeaths = 0; 20130Sstevel@tonic-gate } else { 20140Sstevel@tonic-gate ASSERT(!(tq->tq_flags & TASKQ_DYNAMIC)); 20150Sstevel@tonic-gate } 20160Sstevel@tonic-gate 20179515SJonathan.Adams@Sun.COM tq->tq_threads_ncpus_pct = 0; 20180Sstevel@tonic-gate tq->tq_totaltime = 0; 20190Sstevel@tonic-gate tq->tq_tasks = 0; 20200Sstevel@tonic-gate tq->tq_maxtasks = 0; 20210Sstevel@tonic-gate tq->tq_executed = 0; 20220Sstevel@tonic-gate kmem_cache_free(taskq_cache, tq); 20230Sstevel@tonic-gate } 20240Sstevel@tonic-gate 20250Sstevel@tonic-gate /* 20260Sstevel@tonic-gate * Extend a bucket with a new entry on the free list and attach a worker thread 20270Sstevel@tonic-gate * to it. 20280Sstevel@tonic-gate * 20290Sstevel@tonic-gate * Argument: pointer to the bucket. 20300Sstevel@tonic-gate * 20310Sstevel@tonic-gate * This function may quietly fail. It is only used by taskq_dispatch() which 20320Sstevel@tonic-gate * handles such failures properly. 20330Sstevel@tonic-gate */ 20340Sstevel@tonic-gate static void 20350Sstevel@tonic-gate taskq_bucket_extend(void *arg) 20360Sstevel@tonic-gate { 20370Sstevel@tonic-gate taskq_ent_t *tqe; 20380Sstevel@tonic-gate taskq_bucket_t *b = (taskq_bucket_t *)arg; 20390Sstevel@tonic-gate taskq_t *tq = b->tqbucket_taskq; 20400Sstevel@tonic-gate int nthreads; 20410Sstevel@tonic-gate 20420Sstevel@tonic-gate if (! ENOUGH_MEMORY()) { 20430Sstevel@tonic-gate TQ_STAT(b, tqs_nomem); 20440Sstevel@tonic-gate return; 20450Sstevel@tonic-gate } 20460Sstevel@tonic-gate 20470Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 20480Sstevel@tonic-gate 20490Sstevel@tonic-gate /* 20500Sstevel@tonic-gate * Observe global taskq limits on the number of threads. 20510Sstevel@tonic-gate */ 20520Sstevel@tonic-gate if (tq->tq_tcreates++ - tq->tq_tdeaths > tq->tq_maxsize) { 20530Sstevel@tonic-gate tq->tq_tcreates--; 20540Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 20550Sstevel@tonic-gate return; 20560Sstevel@tonic-gate } 20570Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 20580Sstevel@tonic-gate 20590Sstevel@tonic-gate tqe = kmem_cache_alloc(taskq_ent_cache, KM_NOSLEEP); 20600Sstevel@tonic-gate 20610Sstevel@tonic-gate if (tqe == NULL) { 20620Sstevel@tonic-gate mutex_enter(&tq->tq_lock); 20630Sstevel@tonic-gate TQ_STAT(b, tqs_nomem); 20640Sstevel@tonic-gate tq->tq_tcreates--; 20650Sstevel@tonic-gate mutex_exit(&tq->tq_lock); 20660Sstevel@tonic-gate return; 20670Sstevel@tonic-gate } 20680Sstevel@tonic-gate 20690Sstevel@tonic-gate ASSERT(tqe->tqent_thread == NULL); 20700Sstevel@tonic-gate 20710Sstevel@tonic-gate tqe->tqent_bucket = b; 20720Sstevel@tonic-gate 20730Sstevel@tonic-gate /* 20740Sstevel@tonic-gate * Create a thread in a TS_STOPPED state first. If it is successfully 20750Sstevel@tonic-gate * created, place the entry on the free list and start the thread. 20760Sstevel@tonic-gate */ 20770Sstevel@tonic-gate tqe->tqent_thread = thread_create(NULL, 0, taskq_d_thread, tqe, 20780Sstevel@tonic-gate 0, &p0, TS_STOPPED, tq->tq_pri); 20790Sstevel@tonic-gate 20800Sstevel@tonic-gate /* 20810Sstevel@tonic-gate * Once the entry is ready, link it to the the bucket free list. 20820Sstevel@tonic-gate */ 20830Sstevel@tonic-gate mutex_enter(&b->tqbucket_lock); 20840Sstevel@tonic-gate tqe->tqent_func = NULL; 20850Sstevel@tonic-gate TQ_APPEND(b->tqbucket_freelist, tqe); 20860Sstevel@tonic-gate b->tqbucket_nfree++; 20870Sstevel@tonic-gate TQ_STAT(b, tqs_tcreates); 20880Sstevel@tonic-gate 20890Sstevel@tonic-gate #if TASKQ_STATISTIC 20900Sstevel@tonic-gate nthreads = b->tqbucket_stat.tqs_tcreates - 20910Sstevel@tonic-gate b->tqbucket_stat.tqs_tdeaths; 20920Sstevel@tonic-gate b->tqbucket_stat.tqs_maxthreads = MAX(nthreads, 20930Sstevel@tonic-gate b->tqbucket_stat.tqs_maxthreads); 20940Sstevel@tonic-gate #endif 20950Sstevel@tonic-gate 20960Sstevel@tonic-gate mutex_exit(&b->tqbucket_lock); 20970Sstevel@tonic-gate /* 20980Sstevel@tonic-gate * Start the stopped thread. 20990Sstevel@tonic-gate */ 21000Sstevel@tonic-gate thread_lock(tqe->tqent_thread); 21010Sstevel@tonic-gate tqe->tqent_thread->t_taskq = tq; 21020Sstevel@tonic-gate tqe->tqent_thread->t_schedflag |= TS_ALLSTART; 21030Sstevel@tonic-gate setrun_locked(tqe->tqent_thread); 21040Sstevel@tonic-gate thread_unlock(tqe->tqent_thread); 21050Sstevel@tonic-gate } 21060Sstevel@tonic-gate 21070Sstevel@tonic-gate static int 21080Sstevel@tonic-gate taskq_kstat_update(kstat_t *ksp, int rw) 21090Sstevel@tonic-gate { 21100Sstevel@tonic-gate struct taskq_kstat *tqsp = &taskq_kstat; 21110Sstevel@tonic-gate taskq_t *tq = ksp->ks_private; 21120Sstevel@tonic-gate 21130Sstevel@tonic-gate if (rw == KSTAT_WRITE) 21140Sstevel@tonic-gate return (EACCES); 21150Sstevel@tonic-gate 211611173SJonathan.Adams@Sun.COM tqsp->tq_pid.value.ui64 = tq->tq_proc->p_pid; 21170Sstevel@tonic-gate tqsp->tq_tasks.value.ui64 = tq->tq_tasks; 21180Sstevel@tonic-gate tqsp->tq_executed.value.ui64 = tq->tq_executed; 21190Sstevel@tonic-gate tqsp->tq_maxtasks.value.ui64 = tq->tq_maxtasks; 21200Sstevel@tonic-gate tqsp->tq_totaltime.value.ui64 = tq->tq_totaltime; 21210Sstevel@tonic-gate tqsp->tq_nactive.value.ui64 = tq->tq_active; 21220Sstevel@tonic-gate tqsp->tq_nalloc.value.ui64 = tq->tq_nalloc; 21230Sstevel@tonic-gate tqsp->tq_pri.value.ui64 = tq->tq_pri; 21240Sstevel@tonic-gate tqsp->tq_nthreads.value.ui64 = tq->tq_nthreads; 21250Sstevel@tonic-gate return (0); 21260Sstevel@tonic-gate } 21270Sstevel@tonic-gate 21280Sstevel@tonic-gate static int 21290Sstevel@tonic-gate taskq_d_kstat_update(kstat_t *ksp, int rw) 21300Sstevel@tonic-gate { 21310Sstevel@tonic-gate struct taskq_d_kstat *tqsp = &taskq_d_kstat; 21320Sstevel@tonic-gate taskq_t *tq = ksp->ks_private; 21330Sstevel@tonic-gate taskq_bucket_t *b = tq->tq_buckets; 21340Sstevel@tonic-gate int bid = 0; 21350Sstevel@tonic-gate 21360Sstevel@tonic-gate if (rw == KSTAT_WRITE) 21370Sstevel@tonic-gate return (EACCES); 21380Sstevel@tonic-gate 21390Sstevel@tonic-gate ASSERT(tq->tq_flags & TASKQ_DYNAMIC); 21400Sstevel@tonic-gate 21410Sstevel@tonic-gate tqsp->tqd_btasks.value.ui64 = tq->tq_tasks; 21420Sstevel@tonic-gate tqsp->tqd_bexecuted.value.ui64 = tq->tq_executed; 21430Sstevel@tonic-gate tqsp->tqd_bmaxtasks.value.ui64 = tq->tq_maxtasks; 21440Sstevel@tonic-gate tqsp->tqd_bnalloc.value.ui64 = tq->tq_nalloc; 21450Sstevel@tonic-gate tqsp->tqd_bnactive.value.ui64 = tq->tq_active; 21460Sstevel@tonic-gate tqsp->tqd_btotaltime.value.ui64 = tq->tq_totaltime; 21470Sstevel@tonic-gate tqsp->tqd_pri.value.ui64 = tq->tq_pri; 21480Sstevel@tonic-gate 21490Sstevel@tonic-gate tqsp->tqd_hits.value.ui64 = 0; 21500Sstevel@tonic-gate tqsp->tqd_misses.value.ui64 = 0; 21510Sstevel@tonic-gate tqsp->tqd_overflows.value.ui64 = 0; 21520Sstevel@tonic-gate tqsp->tqd_tcreates.value.ui64 = 0; 21530Sstevel@tonic-gate tqsp->tqd_tdeaths.value.ui64 = 0; 21540Sstevel@tonic-gate tqsp->tqd_maxthreads.value.ui64 = 0; 21550Sstevel@tonic-gate tqsp->tqd_nomem.value.ui64 = 0; 21560Sstevel@tonic-gate tqsp->tqd_disptcreates.value.ui64 = 0; 21570Sstevel@tonic-gate tqsp->tqd_totaltime.value.ui64 = 0; 21580Sstevel@tonic-gate tqsp->tqd_nalloc.value.ui64 = 0; 21590Sstevel@tonic-gate tqsp->tqd_nfree.value.ui64 = 0; 21600Sstevel@tonic-gate 21610Sstevel@tonic-gate for (; (b != NULL) && (bid < tq->tq_nbuckets); b++, bid++) { 21620Sstevel@tonic-gate tqsp->tqd_hits.value.ui64 += b->tqbucket_stat.tqs_hits; 21630Sstevel@tonic-gate tqsp->tqd_misses.value.ui64 += b->tqbucket_stat.tqs_misses; 21640Sstevel@tonic-gate tqsp->tqd_overflows.value.ui64 += b->tqbucket_stat.tqs_overflow; 21650Sstevel@tonic-gate tqsp->tqd_tcreates.value.ui64 += b->tqbucket_stat.tqs_tcreates; 21660Sstevel@tonic-gate tqsp->tqd_tdeaths.value.ui64 += b->tqbucket_stat.tqs_tdeaths; 21670Sstevel@tonic-gate tqsp->tqd_maxthreads.value.ui64 += 21680Sstevel@tonic-gate b->tqbucket_stat.tqs_maxthreads; 21690Sstevel@tonic-gate tqsp->tqd_nomem.value.ui64 += b->tqbucket_stat.tqs_nomem; 21700Sstevel@tonic-gate tqsp->tqd_disptcreates.value.ui64 += 21710Sstevel@tonic-gate b->tqbucket_stat.tqs_disptcreates; 21720Sstevel@tonic-gate tqsp->tqd_totaltime.value.ui64 += b->tqbucket_totaltime; 21730Sstevel@tonic-gate tqsp->tqd_nalloc.value.ui64 += b->tqbucket_nalloc; 21740Sstevel@tonic-gate tqsp->tqd_nfree.value.ui64 += b->tqbucket_nfree; 21750Sstevel@tonic-gate } 21760Sstevel@tonic-gate return (0); 21770Sstevel@tonic-gate } 2178