17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*3348528fSdm120769 * Common Development and Distribution License, Version 1.0 only 6*3348528fSdm120769 * (the "License"). You may not use this file except in compliance 7*3348528fSdm120769 * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 23*3348528fSdm120769 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <sys/callo.h> 307c478bd9Sstevel@tonic-gate #include <sys/param.h> 317c478bd9Sstevel@tonic-gate #include <sys/types.h> 327c478bd9Sstevel@tonic-gate #include <sys/systm.h> 337c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h> 347c478bd9Sstevel@tonic-gate #include <sys/thread.h> 357c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 367c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 377c478bd9Sstevel@tonic-gate #include <sys/callb.h> 387c478bd9Sstevel@tonic-gate #include <sys/debug.h> 397c478bd9Sstevel@tonic-gate #include <sys/vtrace.h> 407c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 417c478bd9Sstevel@tonic-gate #include <sys/sdt.h> 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate /* 447c478bd9Sstevel@tonic-gate * Callout tables. See timeout(9F) for details. 457c478bd9Sstevel@tonic-gate */ 467c478bd9Sstevel@tonic-gate static int cpr_stop_callout; 477c478bd9Sstevel@tonic-gate static int callout_fanout; 487c478bd9Sstevel@tonic-gate static int ncallout; 497c478bd9Sstevel@tonic-gate static callout_table_t *callout_table[CALLOUT_TABLES]; 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate #define CALLOUT_HASH_INSERT(cthead, cp, cnext, cprev) \ 527c478bd9Sstevel@tonic-gate { \ 537c478bd9Sstevel@tonic-gate callout_t **headpp = &cthead; \ 547c478bd9Sstevel@tonic-gate callout_t *headp = *headpp; \ 557c478bd9Sstevel@tonic-gate cp->cnext = headp; \ 567c478bd9Sstevel@tonic-gate cp->cprev = NULL; \ 577c478bd9Sstevel@tonic-gate if (headp != NULL) \ 587c478bd9Sstevel@tonic-gate headp->cprev = cp; \ 597c478bd9Sstevel@tonic-gate *headpp = cp; \ 607c478bd9Sstevel@tonic-gate } 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate #define CALLOUT_HASH_DELETE(cthead, cp, cnext, cprev) \ 637c478bd9Sstevel@tonic-gate { \ 647c478bd9Sstevel@tonic-gate callout_t *nextp = cp->cnext; \ 657c478bd9Sstevel@tonic-gate callout_t *prevp = cp->cprev; \ 667c478bd9Sstevel@tonic-gate if (nextp != NULL) \ 677c478bd9Sstevel@tonic-gate nextp->cprev = prevp; \ 687c478bd9Sstevel@tonic-gate if (prevp != NULL) \ 697c478bd9Sstevel@tonic-gate prevp->cnext = nextp; \ 707c478bd9Sstevel@tonic-gate else \ 717c478bd9Sstevel@tonic-gate cthead = nextp; \ 727c478bd9Sstevel@tonic-gate } 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate #define CALLOUT_HASH_UPDATE(INSDEL, ct, cp, id, runtime) \ 757c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&ct->ct_lock)); \ 767c478bd9Sstevel@tonic-gate ASSERT(cp->c_xid == id && cp->c_runtime == runtime); \ 777c478bd9Sstevel@tonic-gate CALLOUT_HASH_##INSDEL(ct->ct_idhash[CALLOUT_IDHASH(id)], \ 787c478bd9Sstevel@tonic-gate cp, c_idnext, c_idprev) \ 797c478bd9Sstevel@tonic-gate CALLOUT_HASH_##INSDEL(ct->ct_lbhash[CALLOUT_LBHASH(runtime)], \ 807c478bd9Sstevel@tonic-gate cp, c_lbnext, c_lbprev) 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate /* 837c478bd9Sstevel@tonic-gate * Allocate a callout structure. We try quite hard because we 847c478bd9Sstevel@tonic-gate * can't sleep, and if we can't do the allocation, we're toast. 857c478bd9Sstevel@tonic-gate * Failing all, we try a KM_PANIC allocation. 867c478bd9Sstevel@tonic-gate */ 877c478bd9Sstevel@tonic-gate static callout_t * 887c478bd9Sstevel@tonic-gate callout_alloc(callout_table_t *ct) 897c478bd9Sstevel@tonic-gate { 907c478bd9Sstevel@tonic-gate size_t size = 0; 917c478bd9Sstevel@tonic-gate callout_t *cp = NULL; 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 947c478bd9Sstevel@tonic-gate cp = kmem_alloc_tryhard(sizeof (callout_t), &size, 957c478bd9Sstevel@tonic-gate KM_NOSLEEP | KM_PANIC); 967c478bd9Sstevel@tonic-gate bzero(cp, sizeof (callout_t)); 977c478bd9Sstevel@tonic-gate ncallout++; 987c478bd9Sstevel@tonic-gate mutex_enter(&ct->ct_lock); 997c478bd9Sstevel@tonic-gate return (cp); 1007c478bd9Sstevel@tonic-gate } 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate /* 1037c478bd9Sstevel@tonic-gate * Arrange that func(arg) be called after delta clock ticks. 1047c478bd9Sstevel@tonic-gate */ 1057c478bd9Sstevel@tonic-gate static timeout_id_t 1067c478bd9Sstevel@tonic-gate timeout_common(void (*func)(void *), void *arg, clock_t delta, 1077c478bd9Sstevel@tonic-gate callout_table_t *ct) 1087c478bd9Sstevel@tonic-gate { 1097c478bd9Sstevel@tonic-gate callout_t *cp; 1107c478bd9Sstevel@tonic-gate callout_id_t id; 1117c478bd9Sstevel@tonic-gate clock_t runtime; 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate mutex_enter(&ct->ct_lock); 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate if ((cp = ct->ct_freelist) == NULL) 1167c478bd9Sstevel@tonic-gate cp = callout_alloc(ct); 1177c478bd9Sstevel@tonic-gate else 1187c478bd9Sstevel@tonic-gate ct->ct_freelist = cp->c_idnext; 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate cp->c_func = func; 1217c478bd9Sstevel@tonic-gate cp->c_arg = arg; 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate /* 1247c478bd9Sstevel@tonic-gate * Make sure the callout runs at least 1 tick in the future. 1257c478bd9Sstevel@tonic-gate */ 1267c478bd9Sstevel@tonic-gate if (delta <= 0) 1277c478bd9Sstevel@tonic-gate delta = 1; 1287c478bd9Sstevel@tonic-gate cp->c_runtime = runtime = lbolt + delta; 1297c478bd9Sstevel@tonic-gate 13030392143Sqiao /* 1317c478bd9Sstevel@tonic-gate * Assign an ID to this callout 1327c478bd9Sstevel@tonic-gate */ 1337c478bd9Sstevel@tonic-gate if (delta > CALLOUT_LONGTERM_TICKS) 1347c478bd9Sstevel@tonic-gate ct->ct_long_id = id = (ct->ct_long_id - CALLOUT_COUNTER_LOW) | 1357c478bd9Sstevel@tonic-gate CALLOUT_COUNTER_HIGH; 1367c478bd9Sstevel@tonic-gate else 1377c478bd9Sstevel@tonic-gate ct->ct_short_id = id = (ct->ct_short_id - CALLOUT_COUNTER_LOW) | 1387c478bd9Sstevel@tonic-gate CALLOUT_COUNTER_HIGH; 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate cp->c_xid = id; 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate CALLOUT_HASH_UPDATE(INSERT, ct, cp, id, runtime); 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate TRACE_4(TR_FAC_CALLOUT, TR_TIMEOUT, 1477c478bd9Sstevel@tonic-gate "timeout:%K(%p) in %ld ticks, cp %p", 1487c478bd9Sstevel@tonic-gate func, arg, delta, cp); 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate return ((timeout_id_t)id); 1517c478bd9Sstevel@tonic-gate } 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate timeout_id_t 1547c478bd9Sstevel@tonic-gate timeout(void (*func)(void *), void *arg, clock_t delta) 1557c478bd9Sstevel@tonic-gate { 1567c478bd9Sstevel@tonic-gate return (timeout_common(func, arg, delta, 1577c478bd9Sstevel@tonic-gate callout_table[CALLOUT_TABLE(CALLOUT_NORMAL, CPU->cpu_seqid)])); 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate } 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate timeout_id_t 1627c478bd9Sstevel@tonic-gate realtime_timeout(void (*func)(void *), void *arg, clock_t delta) 1637c478bd9Sstevel@tonic-gate { 1647c478bd9Sstevel@tonic-gate return (timeout_common(func, arg, delta, 1657c478bd9Sstevel@tonic-gate callout_table[CALLOUT_TABLE(CALLOUT_REALTIME, CPU->cpu_seqid)])); 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate clock_t 1697c478bd9Sstevel@tonic-gate untimeout(timeout_id_t id_arg) 1707c478bd9Sstevel@tonic-gate { 1717c478bd9Sstevel@tonic-gate callout_id_t id = (callout_id_t)id_arg; 1727c478bd9Sstevel@tonic-gate callout_table_t *ct; 1737c478bd9Sstevel@tonic-gate callout_t *cp; 1747c478bd9Sstevel@tonic-gate callout_id_t xid; 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate ct = callout_table[id & CALLOUT_TABLE_MASK]; 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate mutex_enter(&ct->ct_lock); 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate for (cp = ct->ct_idhash[CALLOUT_IDHASH(id)]; cp; cp = cp->c_idnext) { 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate if ((xid = cp->c_xid) == id) { 1837c478bd9Sstevel@tonic-gate clock_t runtime = cp->c_runtime; 1847c478bd9Sstevel@tonic-gate clock_t time_left = runtime - lbolt; 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate CALLOUT_HASH_UPDATE(DELETE, ct, cp, id, runtime); 1877c478bd9Sstevel@tonic-gate cp->c_idnext = ct->ct_freelist; 1887c478bd9Sstevel@tonic-gate ct->ct_freelist = cp; 1897c478bd9Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 1907c478bd9Sstevel@tonic-gate TRACE_2(TR_FAC_CALLOUT, TR_UNTIMEOUT, 1917c478bd9Sstevel@tonic-gate "untimeout:ID %lx ticks_left %ld", id, time_left); 1927c478bd9Sstevel@tonic-gate return (time_left < 0 ? 0 : time_left); 1937c478bd9Sstevel@tonic-gate } 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate if (xid != (id | CALLOUT_EXECUTING)) 1967c478bd9Sstevel@tonic-gate continue; 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate /* 1997c478bd9Sstevel@tonic-gate * The callout we want to delete is currently executing. 2007c478bd9Sstevel@tonic-gate * The DDI states that we must wait until the callout 2017c478bd9Sstevel@tonic-gate * completes before returning, so we block on c_done until 2027c478bd9Sstevel@tonic-gate * the callout ID changes (to zero if it's on the freelist, 2037c478bd9Sstevel@tonic-gate * or to a new callout ID if it's in use). This implicitly 2047c478bd9Sstevel@tonic-gate * assumes that callout structures are persistent (they are). 2057c478bd9Sstevel@tonic-gate */ 2067c478bd9Sstevel@tonic-gate if (cp->c_executor == curthread) { 2077c478bd9Sstevel@tonic-gate /* 2087c478bd9Sstevel@tonic-gate * The timeout handler called untimeout() on itself. 2097c478bd9Sstevel@tonic-gate * Stupid, but legal. We can't wait for the timeout 2107c478bd9Sstevel@tonic-gate * to complete without deadlocking, so we just return. 2117c478bd9Sstevel@tonic-gate */ 2127c478bd9Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 2137c478bd9Sstevel@tonic-gate TRACE_1(TR_FAC_CALLOUT, TR_UNTIMEOUT_SELF, 2147c478bd9Sstevel@tonic-gate "untimeout_self:ID %x", id); 2157c478bd9Sstevel@tonic-gate return (-1); 2167c478bd9Sstevel@tonic-gate } 2177c478bd9Sstevel@tonic-gate while (cp->c_xid == xid) 2187c478bd9Sstevel@tonic-gate cv_wait(&cp->c_done, &ct->ct_lock); 2197c478bd9Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 2207c478bd9Sstevel@tonic-gate TRACE_1(TR_FAC_CALLOUT, TR_UNTIMEOUT_EXECUTING, 2217c478bd9Sstevel@tonic-gate "untimeout_executing:ID %lx", id); 2227c478bd9Sstevel@tonic-gate return (-1); 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 2267c478bd9Sstevel@tonic-gate TRACE_1(TR_FAC_CALLOUT, TR_UNTIMEOUT_BOGUS_ID, 2277c478bd9Sstevel@tonic-gate "untimeout_bogus_id:ID %lx", id); 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate /* 2307c478bd9Sstevel@tonic-gate * We didn't find the specified callout ID. This means either 2317c478bd9Sstevel@tonic-gate * (1) the callout already fired, or (2) the caller passed us 2327c478bd9Sstevel@tonic-gate * a bogus value. Perform a sanity check to detect case (2). 2337c478bd9Sstevel@tonic-gate */ 2347c478bd9Sstevel@tonic-gate if (id != 0 && (id & (CALLOUT_COUNTER_HIGH | CALLOUT_EXECUTING)) != 2357c478bd9Sstevel@tonic-gate CALLOUT_COUNTER_HIGH) 2367c478bd9Sstevel@tonic-gate panic("untimeout: impossible timeout id %lx", id); 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate return (-1); 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate /* 2427c478bd9Sstevel@tonic-gate * Do the actual work of executing callouts. This routine is called either 2437c478bd9Sstevel@tonic-gate * by a taskq_thread (normal case), or by softcall (realtime case). 2447c478bd9Sstevel@tonic-gate */ 2457c478bd9Sstevel@tonic-gate static void 2467c478bd9Sstevel@tonic-gate callout_execute(callout_table_t *ct) 2477c478bd9Sstevel@tonic-gate { 2487c478bd9Sstevel@tonic-gate callout_t *cp; 2497c478bd9Sstevel@tonic-gate callout_id_t xid; 2507c478bd9Sstevel@tonic-gate clock_t runtime; 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate mutex_enter(&ct->ct_lock); 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate while (((runtime = ct->ct_runtime) - ct->ct_curtime) <= 0) { 2557c478bd9Sstevel@tonic-gate for (cp = ct->ct_lbhash[CALLOUT_LBHASH(runtime)]; 2567c478bd9Sstevel@tonic-gate cp != NULL; cp = cp->c_lbnext) { 2577c478bd9Sstevel@tonic-gate xid = cp->c_xid; 2587c478bd9Sstevel@tonic-gate if (cp->c_runtime != runtime || 2597c478bd9Sstevel@tonic-gate (xid & CALLOUT_EXECUTING)) 2607c478bd9Sstevel@tonic-gate continue; 2617c478bd9Sstevel@tonic-gate cp->c_executor = curthread; 2627c478bd9Sstevel@tonic-gate cp->c_xid = xid |= CALLOUT_EXECUTING; 2637c478bd9Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 2647c478bd9Sstevel@tonic-gate DTRACE_PROBE1(callout__start, callout_t *, cp); 2657c478bd9Sstevel@tonic-gate (*cp->c_func)(cp->c_arg); 2667c478bd9Sstevel@tonic-gate DTRACE_PROBE1(callout__end, callout_t *, cp); 2677c478bd9Sstevel@tonic-gate mutex_enter(&ct->ct_lock); 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate /* 270*3348528fSdm120769 * Delete callout from hash tables, return to freelist, 271*3348528fSdm120769 * and tell anyone who cares that we're done. 2727c478bd9Sstevel@tonic-gate * Even though we dropped and reacquired ct->ct_lock, 2737c478bd9Sstevel@tonic-gate * it's OK to pick up where we left off because only 2747c478bd9Sstevel@tonic-gate * newly-created timeouts can precede cp on ct_lbhash, 2757c478bd9Sstevel@tonic-gate * and those timeouts cannot be due on this tick. 2767c478bd9Sstevel@tonic-gate */ 2777c478bd9Sstevel@tonic-gate CALLOUT_HASH_UPDATE(DELETE, ct, cp, xid, runtime); 2787c478bd9Sstevel@tonic-gate cp->c_idnext = ct->ct_freelist; 2797c478bd9Sstevel@tonic-gate ct->ct_freelist = cp; 2807c478bd9Sstevel@tonic-gate cp->c_xid = 0; /* Indicate completion for c_done */ 2817c478bd9Sstevel@tonic-gate cv_broadcast(&cp->c_done); 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate /* 2847c478bd9Sstevel@tonic-gate * We have completed all callouts that were scheduled to 2857c478bd9Sstevel@tonic-gate * run at "runtime". If the global run time still matches 2867c478bd9Sstevel@tonic-gate * our local copy, then we advance the global run time; 2877c478bd9Sstevel@tonic-gate * otherwise, another callout thread must have already done so. 2887c478bd9Sstevel@tonic-gate */ 2897c478bd9Sstevel@tonic-gate if (ct->ct_runtime == runtime) 2907c478bd9Sstevel@tonic-gate ct->ct_runtime = runtime + 1; 2917c478bd9Sstevel@tonic-gate } 2927c478bd9Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 2937c478bd9Sstevel@tonic-gate } 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate /* 2967c478bd9Sstevel@tonic-gate * Schedule any callouts that are due on or before this tick. 2977c478bd9Sstevel@tonic-gate */ 2987c478bd9Sstevel@tonic-gate static void 2997c478bd9Sstevel@tonic-gate callout_schedule_1(callout_table_t *ct) 3007c478bd9Sstevel@tonic-gate { 3017c478bd9Sstevel@tonic-gate callout_t *cp; 3027c478bd9Sstevel@tonic-gate clock_t curtime, runtime; 3037c478bd9Sstevel@tonic-gate 3047c478bd9Sstevel@tonic-gate mutex_enter(&ct->ct_lock); 3057c478bd9Sstevel@tonic-gate ct->ct_curtime = curtime = lbolt; 3067c478bd9Sstevel@tonic-gate while (((runtime = ct->ct_runtime) - curtime) <= 0) { 3077c478bd9Sstevel@tonic-gate for (cp = ct->ct_lbhash[CALLOUT_LBHASH(runtime)]; 3087c478bd9Sstevel@tonic-gate cp != NULL; cp = cp->c_lbnext) { 3097c478bd9Sstevel@tonic-gate if (cp->c_runtime != runtime || 3107c478bd9Sstevel@tonic-gate (cp->c_xid & CALLOUT_EXECUTING)) 3117c478bd9Sstevel@tonic-gate continue; 3127c478bd9Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 3137c478bd9Sstevel@tonic-gate if (ct->ct_taskq == NULL) 3147c478bd9Sstevel@tonic-gate softcall((void (*)(void *))callout_execute, ct); 3157c478bd9Sstevel@tonic-gate else 3167c478bd9Sstevel@tonic-gate (void) taskq_dispatch(ct->ct_taskq, 3177c478bd9Sstevel@tonic-gate (task_func_t *)callout_execute, ct, 3187c478bd9Sstevel@tonic-gate KM_NOSLEEP); 3197c478bd9Sstevel@tonic-gate return; 3207c478bd9Sstevel@tonic-gate } 3217c478bd9Sstevel@tonic-gate ct->ct_runtime++; 3227c478bd9Sstevel@tonic-gate } 3237c478bd9Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 3247c478bd9Sstevel@tonic-gate } 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate /* 3277c478bd9Sstevel@tonic-gate * Schedule callouts for all callout tables. Called by clock() on each tick. 3287c478bd9Sstevel@tonic-gate */ 3297c478bd9Sstevel@tonic-gate void 3307c478bd9Sstevel@tonic-gate callout_schedule(void) 3317c478bd9Sstevel@tonic-gate { 3327c478bd9Sstevel@tonic-gate int f, t; 3337c478bd9Sstevel@tonic-gate 3347c478bd9Sstevel@tonic-gate if (cpr_stop_callout) 3357c478bd9Sstevel@tonic-gate return; 3367c478bd9Sstevel@tonic-gate 3377c478bd9Sstevel@tonic-gate for (t = 0; t < CALLOUT_NTYPES; t++) 3387c478bd9Sstevel@tonic-gate for (f = 0; f < callout_fanout; f++) 3397c478bd9Sstevel@tonic-gate callout_schedule_1(callout_table[CALLOUT_TABLE(t, f)]); 3407c478bd9Sstevel@tonic-gate } 3417c478bd9Sstevel@tonic-gate 3427c478bd9Sstevel@tonic-gate /* 3437c478bd9Sstevel@tonic-gate * Callback handler used by CPR to stop and resume callouts. 3447c478bd9Sstevel@tonic-gate */ 3457c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3467c478bd9Sstevel@tonic-gate static boolean_t 3477c478bd9Sstevel@tonic-gate callout_cpr_callb(void *arg, int code) 3487c478bd9Sstevel@tonic-gate { 3497c478bd9Sstevel@tonic-gate cpr_stop_callout = (code == CB_CODE_CPR_CHKPT); 3507c478bd9Sstevel@tonic-gate return (B_TRUE); 3517c478bd9Sstevel@tonic-gate } 3527c478bd9Sstevel@tonic-gate 3537c478bd9Sstevel@tonic-gate /* 3547c478bd9Sstevel@tonic-gate * Initialize all callout tables. Called at boot time just before clkstart(). 3557c478bd9Sstevel@tonic-gate */ 3567c478bd9Sstevel@tonic-gate void 3577c478bd9Sstevel@tonic-gate callout_init(void) 3587c478bd9Sstevel@tonic-gate { 3597c478bd9Sstevel@tonic-gate int f, t; 3607c478bd9Sstevel@tonic-gate int table_id; 3617c478bd9Sstevel@tonic-gate callout_table_t *ct; 3627c478bd9Sstevel@tonic-gate 3637c478bd9Sstevel@tonic-gate callout_fanout = MIN(CALLOUT_FANOUT, max_ncpus); 3647c478bd9Sstevel@tonic-gate 3657c478bd9Sstevel@tonic-gate for (t = 0; t < CALLOUT_NTYPES; t++) { 3667c478bd9Sstevel@tonic-gate for (f = 0; f < CALLOUT_FANOUT; f++) { 3677c478bd9Sstevel@tonic-gate table_id = CALLOUT_TABLE(t, f); 3687c478bd9Sstevel@tonic-gate if (f >= callout_fanout) { 3697c478bd9Sstevel@tonic-gate callout_table[table_id] = 3707c478bd9Sstevel@tonic-gate callout_table[table_id - callout_fanout]; 3717c478bd9Sstevel@tonic-gate continue; 3727c478bd9Sstevel@tonic-gate } 3737c478bd9Sstevel@tonic-gate ct = kmem_zalloc(sizeof (callout_table_t), KM_SLEEP); 3747c478bd9Sstevel@tonic-gate callout_table[table_id] = ct; 3757c478bd9Sstevel@tonic-gate ct->ct_short_id = (callout_id_t)table_id | 3767c478bd9Sstevel@tonic-gate CALLOUT_COUNTER_HIGH; 3777c478bd9Sstevel@tonic-gate ct->ct_long_id = ct->ct_short_id | CALLOUT_LONGTERM; 3787c478bd9Sstevel@tonic-gate ct->ct_curtime = ct->ct_runtime = lbolt; 3797c478bd9Sstevel@tonic-gate if (t == CALLOUT_NORMAL) { 3807c478bd9Sstevel@tonic-gate /* 3817c478bd9Sstevel@tonic-gate * Each callout thread consumes exactly one 3827c478bd9Sstevel@tonic-gate * task structure while active. Therefore, 3837c478bd9Sstevel@tonic-gate * prepopulating with 2 * CALLOUT_THREADS tasks 3847c478bd9Sstevel@tonic-gate * ensures that there's at least one task per 3857c478bd9Sstevel@tonic-gate * thread that's either scheduled or on the 3867c478bd9Sstevel@tonic-gate * freelist. In turn, this guarantees that 3877c478bd9Sstevel@tonic-gate * taskq_dispatch() will always either succeed 3887c478bd9Sstevel@tonic-gate * (because there's a free task structure) or 3897c478bd9Sstevel@tonic-gate * be unnecessary (because "callout_excute(ct)" 3907c478bd9Sstevel@tonic-gate * has already scheduled). 3917c478bd9Sstevel@tonic-gate */ 3927c478bd9Sstevel@tonic-gate ct->ct_taskq = 3937c478bd9Sstevel@tonic-gate taskq_create_instance("callout_taskq", f, 3947c478bd9Sstevel@tonic-gate CALLOUT_THREADS, maxclsyspri, 3957c478bd9Sstevel@tonic-gate 2 * CALLOUT_THREADS, 2 * CALLOUT_THREADS, 3967c478bd9Sstevel@tonic-gate TASKQ_PREPOPULATE | TASKQ_CPR_SAFE); 3977c478bd9Sstevel@tonic-gate } 3987c478bd9Sstevel@tonic-gate } 3997c478bd9Sstevel@tonic-gate } 4007c478bd9Sstevel@tonic-gate (void) callb_add(callout_cpr_callb, 0, CB_CL_CPR_CALLOUT, "callout"); 4017c478bd9Sstevel@tonic-gate } 402