10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*6422Sqiao * Common Development and Distribution License (the "License"). 6*6422Sqiao * 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*6422Sqiao * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include <sys/callo.h> 290Sstevel@tonic-gate #include <sys/param.h> 300Sstevel@tonic-gate #include <sys/types.h> 310Sstevel@tonic-gate #include <sys/systm.h> 320Sstevel@tonic-gate #include <sys/cpuvar.h> 330Sstevel@tonic-gate #include <sys/thread.h> 340Sstevel@tonic-gate #include <sys/kmem.h> 350Sstevel@tonic-gate #include <sys/cmn_err.h> 360Sstevel@tonic-gate #include <sys/callb.h> 370Sstevel@tonic-gate #include <sys/debug.h> 380Sstevel@tonic-gate #include <sys/vtrace.h> 390Sstevel@tonic-gate #include <sys/sysmacros.h> 400Sstevel@tonic-gate #include <sys/sdt.h> 410Sstevel@tonic-gate 420Sstevel@tonic-gate /* 430Sstevel@tonic-gate * Callout tables. See timeout(9F) for details. 440Sstevel@tonic-gate */ 450Sstevel@tonic-gate static int cpr_stop_callout; 460Sstevel@tonic-gate static int callout_fanout; 470Sstevel@tonic-gate static int ncallout; 480Sstevel@tonic-gate static callout_table_t *callout_table[CALLOUT_TABLES]; 490Sstevel@tonic-gate 500Sstevel@tonic-gate #define CALLOUT_HASH_INSERT(cthead, cp, cnext, cprev) \ 510Sstevel@tonic-gate { \ 520Sstevel@tonic-gate callout_t **headpp = &cthead; \ 530Sstevel@tonic-gate callout_t *headp = *headpp; \ 540Sstevel@tonic-gate cp->cnext = headp; \ 550Sstevel@tonic-gate cp->cprev = NULL; \ 560Sstevel@tonic-gate if (headp != NULL) \ 570Sstevel@tonic-gate headp->cprev = cp; \ 580Sstevel@tonic-gate *headpp = cp; \ 590Sstevel@tonic-gate } 600Sstevel@tonic-gate 610Sstevel@tonic-gate #define CALLOUT_HASH_DELETE(cthead, cp, cnext, cprev) \ 620Sstevel@tonic-gate { \ 630Sstevel@tonic-gate callout_t *nextp = cp->cnext; \ 640Sstevel@tonic-gate callout_t *prevp = cp->cprev; \ 650Sstevel@tonic-gate if (nextp != NULL) \ 660Sstevel@tonic-gate nextp->cprev = prevp; \ 670Sstevel@tonic-gate if (prevp != NULL) \ 680Sstevel@tonic-gate prevp->cnext = nextp; \ 690Sstevel@tonic-gate else \ 700Sstevel@tonic-gate cthead = nextp; \ 710Sstevel@tonic-gate } 720Sstevel@tonic-gate 73*6422Sqiao #define CALLOUT_HASH_UPDATE(INSDEL, ct, cp, id, runtime, runhrtime) \ 740Sstevel@tonic-gate ASSERT(MUTEX_HELD(&ct->ct_lock)); \ 75*6422Sqiao ASSERT(cp->c_xid == id && ((cp->c_runtime == runtime) || \ 76*6422Sqiao (cp->c_runhrtime <= runhrtime))); \ 770Sstevel@tonic-gate CALLOUT_HASH_##INSDEL(ct->ct_idhash[CALLOUT_IDHASH(id)], \ 780Sstevel@tonic-gate cp, c_idnext, c_idprev) \ 790Sstevel@tonic-gate CALLOUT_HASH_##INSDEL(ct->ct_lbhash[CALLOUT_LBHASH(runtime)], \ 800Sstevel@tonic-gate cp, c_lbnext, c_lbprev) 810Sstevel@tonic-gate 820Sstevel@tonic-gate /* 830Sstevel@tonic-gate * Allocate a callout structure. We try quite hard because we 840Sstevel@tonic-gate * can't sleep, and if we can't do the allocation, we're toast. 850Sstevel@tonic-gate * Failing all, we try a KM_PANIC allocation. 860Sstevel@tonic-gate */ 870Sstevel@tonic-gate static callout_t * 880Sstevel@tonic-gate callout_alloc(callout_table_t *ct) 890Sstevel@tonic-gate { 900Sstevel@tonic-gate size_t size = 0; 910Sstevel@tonic-gate callout_t *cp = NULL; 920Sstevel@tonic-gate 930Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 940Sstevel@tonic-gate cp = kmem_alloc_tryhard(sizeof (callout_t), &size, 950Sstevel@tonic-gate KM_NOSLEEP | KM_PANIC); 960Sstevel@tonic-gate bzero(cp, sizeof (callout_t)); 970Sstevel@tonic-gate ncallout++; 980Sstevel@tonic-gate mutex_enter(&ct->ct_lock); 990Sstevel@tonic-gate return (cp); 1000Sstevel@tonic-gate } 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate /* 1030Sstevel@tonic-gate * Arrange that func(arg) be called after delta clock ticks. 1040Sstevel@tonic-gate */ 1050Sstevel@tonic-gate static timeout_id_t 1060Sstevel@tonic-gate timeout_common(void (*func)(void *), void *arg, clock_t delta, 1070Sstevel@tonic-gate callout_table_t *ct) 1080Sstevel@tonic-gate { 1094123Sdm120769 callout_t *cp; 1104123Sdm120769 callout_id_t id; 1114123Sdm120769 clock_t runtime; 112*6422Sqiao timestruc_t start; 113*6422Sqiao int64_t runhrtime; 114*6422Sqiao 115*6422Sqiao gethrestime_lasttick(&start); 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate mutex_enter(&ct->ct_lock); 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate if ((cp = ct->ct_freelist) == NULL) 1200Sstevel@tonic-gate cp = callout_alloc(ct); 1210Sstevel@tonic-gate else 1220Sstevel@tonic-gate ct->ct_freelist = cp->c_idnext; 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate cp->c_func = func; 1250Sstevel@tonic-gate cp->c_arg = arg; 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate /* 1280Sstevel@tonic-gate * Make sure the callout runs at least 1 tick in the future. 1290Sstevel@tonic-gate */ 1300Sstevel@tonic-gate if (delta <= 0) 1310Sstevel@tonic-gate delta = 1; 1320Sstevel@tonic-gate cp->c_runtime = runtime = lbolt + delta; 133*6422Sqiao cp->c_runhrtime = runhrtime = delta + timespectohz64(&start); 1340Sstevel@tonic-gate 1353783Sqiao /* 1360Sstevel@tonic-gate * Assign an ID to this callout 1370Sstevel@tonic-gate */ 1380Sstevel@tonic-gate if (delta > CALLOUT_LONGTERM_TICKS) 1390Sstevel@tonic-gate ct->ct_long_id = id = (ct->ct_long_id - CALLOUT_COUNTER_LOW) | 1400Sstevel@tonic-gate CALLOUT_COUNTER_HIGH; 1410Sstevel@tonic-gate else 1420Sstevel@tonic-gate ct->ct_short_id = id = (ct->ct_short_id - CALLOUT_COUNTER_LOW) | 1430Sstevel@tonic-gate CALLOUT_COUNTER_HIGH; 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate cp->c_xid = id; 1460Sstevel@tonic-gate 147*6422Sqiao CALLOUT_HASH_UPDATE(INSERT, ct, cp, id, runtime, runhrtime); 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate TRACE_4(TR_FAC_CALLOUT, TR_TIMEOUT, 152*6422Sqiao "timeout:%K(%p) in %ld ticks, cp %p", 153*6422Sqiao func, arg, delta, cp); 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate return ((timeout_id_t)id); 1560Sstevel@tonic-gate } 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate timeout_id_t 1590Sstevel@tonic-gate timeout(void (*func)(void *), void *arg, clock_t delta) 1600Sstevel@tonic-gate { 1610Sstevel@tonic-gate return (timeout_common(func, arg, delta, 1620Sstevel@tonic-gate callout_table[CALLOUT_TABLE(CALLOUT_NORMAL, CPU->cpu_seqid)])); 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate } 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate timeout_id_t 1670Sstevel@tonic-gate realtime_timeout(void (*func)(void *), void *arg, clock_t delta) 1680Sstevel@tonic-gate { 1690Sstevel@tonic-gate return (timeout_common(func, arg, delta, 1700Sstevel@tonic-gate callout_table[CALLOUT_TABLE(CALLOUT_REALTIME, CPU->cpu_seqid)])); 1710Sstevel@tonic-gate } 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate clock_t 1740Sstevel@tonic-gate untimeout(timeout_id_t id_arg) 1750Sstevel@tonic-gate { 1760Sstevel@tonic-gate callout_id_t id = (callout_id_t)id_arg; 1770Sstevel@tonic-gate callout_table_t *ct; 1780Sstevel@tonic-gate callout_t *cp; 1790Sstevel@tonic-gate callout_id_t xid; 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate ct = callout_table[id & CALLOUT_TABLE_MASK]; 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate mutex_enter(&ct->ct_lock); 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate for (cp = ct->ct_idhash[CALLOUT_IDHASH(id)]; cp; cp = cp->c_idnext) { 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate if ((xid = cp->c_xid) == id) { 1880Sstevel@tonic-gate clock_t runtime = cp->c_runtime; 189*6422Sqiao int64_t runhrtime = cp->c_runhrtime; 1900Sstevel@tonic-gate clock_t time_left = runtime - lbolt; 1910Sstevel@tonic-gate 192*6422Sqiao CALLOUT_HASH_UPDATE(DELETE, ct, cp, id, 193*6422Sqiao runtime, runhrtime); 194*6422Sqiao 1950Sstevel@tonic-gate cp->c_idnext = ct->ct_freelist; 1960Sstevel@tonic-gate ct->ct_freelist = cp; 1970Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 1980Sstevel@tonic-gate TRACE_2(TR_FAC_CALLOUT, TR_UNTIMEOUT, 1990Sstevel@tonic-gate "untimeout:ID %lx ticks_left %ld", id, time_left); 2000Sstevel@tonic-gate return (time_left < 0 ? 0 : time_left); 2010Sstevel@tonic-gate } 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate if (xid != (id | CALLOUT_EXECUTING)) 2040Sstevel@tonic-gate continue; 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate /* 2070Sstevel@tonic-gate * The callout we want to delete is currently executing. 2080Sstevel@tonic-gate * The DDI states that we must wait until the callout 2090Sstevel@tonic-gate * completes before returning, so we block on c_done until 2100Sstevel@tonic-gate * the callout ID changes (to zero if it's on the freelist, 2110Sstevel@tonic-gate * or to a new callout ID if it's in use). This implicitly 2120Sstevel@tonic-gate * assumes that callout structures are persistent (they are). 2130Sstevel@tonic-gate */ 2140Sstevel@tonic-gate if (cp->c_executor == curthread) { 2150Sstevel@tonic-gate /* 2160Sstevel@tonic-gate * The timeout handler called untimeout() on itself. 2170Sstevel@tonic-gate * Stupid, but legal. We can't wait for the timeout 2180Sstevel@tonic-gate * to complete without deadlocking, so we just return. 2190Sstevel@tonic-gate */ 2200Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 2210Sstevel@tonic-gate TRACE_1(TR_FAC_CALLOUT, TR_UNTIMEOUT_SELF, 2220Sstevel@tonic-gate "untimeout_self:ID %x", id); 2230Sstevel@tonic-gate return (-1); 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate while (cp->c_xid == xid) 2260Sstevel@tonic-gate cv_wait(&cp->c_done, &ct->ct_lock); 2270Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 2280Sstevel@tonic-gate TRACE_1(TR_FAC_CALLOUT, TR_UNTIMEOUT_EXECUTING, 2290Sstevel@tonic-gate "untimeout_executing:ID %lx", id); 2300Sstevel@tonic-gate return (-1); 2310Sstevel@tonic-gate } 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 2340Sstevel@tonic-gate TRACE_1(TR_FAC_CALLOUT, TR_UNTIMEOUT_BOGUS_ID, 2350Sstevel@tonic-gate "untimeout_bogus_id:ID %lx", id); 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate /* 2380Sstevel@tonic-gate * We didn't find the specified callout ID. This means either 2390Sstevel@tonic-gate * (1) the callout already fired, or (2) the caller passed us 2400Sstevel@tonic-gate * a bogus value. Perform a sanity check to detect case (2). 2410Sstevel@tonic-gate */ 2420Sstevel@tonic-gate if (id != 0 && (id & (CALLOUT_COUNTER_HIGH | CALLOUT_EXECUTING)) != 2430Sstevel@tonic-gate CALLOUT_COUNTER_HIGH) 2440Sstevel@tonic-gate panic("untimeout: impossible timeout id %lx", id); 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate return (-1); 2470Sstevel@tonic-gate } 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate /* 2500Sstevel@tonic-gate * Do the actual work of executing callouts. This routine is called either 2510Sstevel@tonic-gate * by a taskq_thread (normal case), or by softcall (realtime case). 2520Sstevel@tonic-gate */ 2530Sstevel@tonic-gate static void 2540Sstevel@tonic-gate callout_execute(callout_table_t *ct) 2550Sstevel@tonic-gate { 2564123Sdm120769 callout_t *cp; 2574123Sdm120769 callout_id_t xid; 2584123Sdm120769 clock_t runtime; 259*6422Sqiao int64_t curhrtime; 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate mutex_enter(&ct->ct_lock); 2620Sstevel@tonic-gate 263*6422Sqiao /* 264*6422Sqiao * Assuming the system time can be set forward and backward 265*6422Sqiao * at any time. If it is set backward, we will measure the 266*6422Sqiao * c_runtime; otherwise, we will compare c_runhrtime with 267*6422Sqiao * ct_curhrtime. 268*6422Sqiao */ 269*6422Sqiao curhrtime = ct->ct_curhrtime; 2700Sstevel@tonic-gate while (((runtime = ct->ct_runtime) - ct->ct_curtime) <= 0) { 2710Sstevel@tonic-gate for (cp = ct->ct_lbhash[CALLOUT_LBHASH(runtime)]; 2720Sstevel@tonic-gate cp != NULL; cp = cp->c_lbnext) { 2730Sstevel@tonic-gate xid = cp->c_xid; 274*6422Sqiao if ((cp->c_runtime != runtime && 275*6422Sqiao cp->c_runhrtime > curhrtime) || 2760Sstevel@tonic-gate (xid & CALLOUT_EXECUTING)) 2770Sstevel@tonic-gate continue; 2780Sstevel@tonic-gate cp->c_executor = curthread; 2790Sstevel@tonic-gate cp->c_xid = xid |= CALLOUT_EXECUTING; 2800Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 2810Sstevel@tonic-gate DTRACE_PROBE1(callout__start, callout_t *, cp); 2820Sstevel@tonic-gate (*cp->c_func)(cp->c_arg); 2830Sstevel@tonic-gate DTRACE_PROBE1(callout__end, callout_t *, cp); 2840Sstevel@tonic-gate mutex_enter(&ct->ct_lock); 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate /* 2874123Sdm120769 * Delete callout from hash tables, return to freelist, 2884123Sdm120769 * and tell anyone who cares that we're done. 2890Sstevel@tonic-gate * Even though we dropped and reacquired ct->ct_lock, 2900Sstevel@tonic-gate * it's OK to pick up where we left off because only 2910Sstevel@tonic-gate * newly-created timeouts can precede cp on ct_lbhash, 2920Sstevel@tonic-gate * and those timeouts cannot be due on this tick. 2930Sstevel@tonic-gate */ 294*6422Sqiao CALLOUT_HASH_UPDATE(DELETE, ct, cp, xid, 295*6422Sqiao runtime, curhrtime); 296*6422Sqiao 2970Sstevel@tonic-gate cp->c_idnext = ct->ct_freelist; 2980Sstevel@tonic-gate ct->ct_freelist = cp; 2990Sstevel@tonic-gate cp->c_xid = 0; /* Indicate completion for c_done */ 3000Sstevel@tonic-gate cv_broadcast(&cp->c_done); 3010Sstevel@tonic-gate } 3020Sstevel@tonic-gate /* 3030Sstevel@tonic-gate * We have completed all callouts that were scheduled to 3040Sstevel@tonic-gate * run at "runtime". If the global run time still matches 3050Sstevel@tonic-gate * our local copy, then we advance the global run time; 3060Sstevel@tonic-gate * otherwise, another callout thread must have already done so. 3070Sstevel@tonic-gate */ 3080Sstevel@tonic-gate if (ct->ct_runtime == runtime) 3090Sstevel@tonic-gate ct->ct_runtime = runtime + 1; 3100Sstevel@tonic-gate } 3110Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 3120Sstevel@tonic-gate } 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate /* 3150Sstevel@tonic-gate * Schedule any callouts that are due on or before this tick. 3160Sstevel@tonic-gate */ 3170Sstevel@tonic-gate static void 3180Sstevel@tonic-gate callout_schedule_1(callout_table_t *ct) 3190Sstevel@tonic-gate { 3204123Sdm120769 callout_t *cp; 3214123Sdm120769 clock_t curtime, runtime; 322*6422Sqiao timestruc_t now; 323*6422Sqiao int64_t curhrtime; 324*6422Sqiao 325*6422Sqiao gethrestime(&now); 326*6422Sqiao curhrtime = timespectohz64(&now); 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate mutex_enter(&ct->ct_lock); 3290Sstevel@tonic-gate ct->ct_curtime = curtime = lbolt; 330*6422Sqiao 331*6422Sqiao /* 332*6422Sqiao * We use both the conditions cp->c_runtime == runtime and 333*6422Sqiao * cp->c_runhrtime <= curhrtime to determine a timeout is 334*6422Sqiao * premature or not. If the system time has been set backwards, 335*6422Sqiao * then cp->c_runtime == runtime will become true first. 336*6422Sqiao * Otherwise, we test cp->c_runhrtime <= curhrtime 337*6422Sqiao */ 338*6422Sqiao ct->ct_curhrtime = curhrtime; 3390Sstevel@tonic-gate while (((runtime = ct->ct_runtime) - curtime) <= 0) { 3400Sstevel@tonic-gate for (cp = ct->ct_lbhash[CALLOUT_LBHASH(runtime)]; 3410Sstevel@tonic-gate cp != NULL; cp = cp->c_lbnext) { 342*6422Sqiao if ((cp->c_runtime != runtime && 343*6422Sqiao cp->c_runhrtime > curhrtime) || 3440Sstevel@tonic-gate (cp->c_xid & CALLOUT_EXECUTING)) 3450Sstevel@tonic-gate continue; 3460Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 3470Sstevel@tonic-gate if (ct->ct_taskq == NULL) 3480Sstevel@tonic-gate softcall((void (*)(void *))callout_execute, ct); 3490Sstevel@tonic-gate else 3500Sstevel@tonic-gate (void) taskq_dispatch(ct->ct_taskq, 3510Sstevel@tonic-gate (task_func_t *)callout_execute, ct, 3520Sstevel@tonic-gate KM_NOSLEEP); 3530Sstevel@tonic-gate return; 3540Sstevel@tonic-gate } 3550Sstevel@tonic-gate ct->ct_runtime++; 3560Sstevel@tonic-gate } 3570Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 3580Sstevel@tonic-gate } 3590Sstevel@tonic-gate 3600Sstevel@tonic-gate /* 3610Sstevel@tonic-gate * Schedule callouts for all callout tables. Called by clock() on each tick. 3620Sstevel@tonic-gate */ 3630Sstevel@tonic-gate void 3640Sstevel@tonic-gate callout_schedule(void) 3650Sstevel@tonic-gate { 3660Sstevel@tonic-gate int f, t; 3670Sstevel@tonic-gate 3680Sstevel@tonic-gate if (cpr_stop_callout) 3690Sstevel@tonic-gate return; 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate for (t = 0; t < CALLOUT_NTYPES; t++) 3720Sstevel@tonic-gate for (f = 0; f < callout_fanout; f++) 3730Sstevel@tonic-gate callout_schedule_1(callout_table[CALLOUT_TABLE(t, f)]); 3740Sstevel@tonic-gate } 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate /* 3770Sstevel@tonic-gate * Callback handler used by CPR to stop and resume callouts. 3780Sstevel@tonic-gate */ 3790Sstevel@tonic-gate /*ARGSUSED*/ 3800Sstevel@tonic-gate static boolean_t 3810Sstevel@tonic-gate callout_cpr_callb(void *arg, int code) 3820Sstevel@tonic-gate { 3830Sstevel@tonic-gate cpr_stop_callout = (code == CB_CODE_CPR_CHKPT); 3840Sstevel@tonic-gate return (B_TRUE); 3850Sstevel@tonic-gate } 3860Sstevel@tonic-gate 3870Sstevel@tonic-gate /* 3880Sstevel@tonic-gate * Initialize all callout tables. Called at boot time just before clkstart(). 3890Sstevel@tonic-gate */ 3900Sstevel@tonic-gate void 3910Sstevel@tonic-gate callout_init(void) 3920Sstevel@tonic-gate { 3930Sstevel@tonic-gate int f, t; 3940Sstevel@tonic-gate int table_id; 3950Sstevel@tonic-gate callout_table_t *ct; 3960Sstevel@tonic-gate 3970Sstevel@tonic-gate callout_fanout = MIN(CALLOUT_FANOUT, max_ncpus); 3980Sstevel@tonic-gate 3990Sstevel@tonic-gate for (t = 0; t < CALLOUT_NTYPES; t++) { 4000Sstevel@tonic-gate for (f = 0; f < CALLOUT_FANOUT; f++) { 4010Sstevel@tonic-gate table_id = CALLOUT_TABLE(t, f); 4020Sstevel@tonic-gate if (f >= callout_fanout) { 4030Sstevel@tonic-gate callout_table[table_id] = 4040Sstevel@tonic-gate callout_table[table_id - callout_fanout]; 4050Sstevel@tonic-gate continue; 4060Sstevel@tonic-gate } 4070Sstevel@tonic-gate ct = kmem_zalloc(sizeof (callout_table_t), KM_SLEEP); 4080Sstevel@tonic-gate callout_table[table_id] = ct; 4090Sstevel@tonic-gate ct->ct_short_id = (callout_id_t)table_id | 4100Sstevel@tonic-gate CALLOUT_COUNTER_HIGH; 4110Sstevel@tonic-gate ct->ct_long_id = ct->ct_short_id | CALLOUT_LONGTERM; 4120Sstevel@tonic-gate ct->ct_curtime = ct->ct_runtime = lbolt; 413*6422Sqiao 414*6422Sqiao /* 415*6422Sqiao * We can not call gethrestime() at this moment 416*6422Sqiao * since the system time has not been validated. 417*6422Sqiao * So Set ct_curhrtime to zero. 418*6422Sqiao */ 419*6422Sqiao ct->ct_curhrtime = 0; 420*6422Sqiao 4210Sstevel@tonic-gate if (t == CALLOUT_NORMAL) { 4220Sstevel@tonic-gate /* 4230Sstevel@tonic-gate * Each callout thread consumes exactly one 4240Sstevel@tonic-gate * task structure while active. Therefore, 4250Sstevel@tonic-gate * prepopulating with 2 * CALLOUT_THREADS tasks 4260Sstevel@tonic-gate * ensures that there's at least one task per 4270Sstevel@tonic-gate * thread that's either scheduled or on the 4280Sstevel@tonic-gate * freelist. In turn, this guarantees that 4290Sstevel@tonic-gate * taskq_dispatch() will always either succeed 4300Sstevel@tonic-gate * (because there's a free task structure) or 4310Sstevel@tonic-gate * be unnecessary (because "callout_excute(ct)" 4320Sstevel@tonic-gate * has already scheduled). 4330Sstevel@tonic-gate */ 4340Sstevel@tonic-gate ct->ct_taskq = 4350Sstevel@tonic-gate taskq_create_instance("callout_taskq", f, 4360Sstevel@tonic-gate CALLOUT_THREADS, maxclsyspri, 4370Sstevel@tonic-gate 2 * CALLOUT_THREADS, 2 * CALLOUT_THREADS, 4380Sstevel@tonic-gate TASKQ_PREPOPULATE | TASKQ_CPR_SAFE); 4390Sstevel@tonic-gate } 4400Sstevel@tonic-gate } 4410Sstevel@tonic-gate } 4420Sstevel@tonic-gate (void) callb_add(callout_cpr_callb, 0, CB_CL_CPR_CALLOUT, "callout"); 4430Sstevel@tonic-gate } 444