1df8bae1dSRodney W. Grimes /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 5df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 6df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 7df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 34acc8326dSGarrett Wollman * From: @(#)kern_clock.c 8.5 (Berkeley) 1/21/94 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37677b542eSDavid E. O'Brien #include <sys/cdefs.h> 38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 39677b542eSDavid E. O'Brien 405b999a6bSDavide Italiano #include "opt_callout_profiling.h" 415b999a6bSDavide Italiano #if defined(__arm__) 425b999a6bSDavide Italiano #include "opt_timer.h" 435b999a6bSDavide Italiano #endif 4491dd9aaeSRobert Watson 45df8bae1dSRodney W. Grimes #include <sys/param.h> 46df8bae1dSRodney W. Grimes #include <sys/systm.h> 478d809d50SJeff Roberson #include <sys/bus.h> 4815b7a470SPoul-Henning Kamp #include <sys/callout.h> 49f8ccf82aSAndre Oppermann #include <sys/file.h> 508d809d50SJeff Roberson #include <sys/interrupt.h> 51df8bae1dSRodney W. Grimes #include <sys/kernel.h> 52ff7ec58aSRobert Watson #include <sys/ktr.h> 53f34fa851SJohn Baldwin #include <sys/lock.h> 548d809d50SJeff Roberson #include <sys/malloc.h> 55cb799bfeSJohn Baldwin #include <sys/mutex.h> 5621f9e816SJohn Baldwin #include <sys/proc.h> 5791dd9aaeSRobert Watson #include <sys/sdt.h> 586a0ce57dSAttilio Rao #include <sys/sleepqueue.h> 5922ee8c4fSPoul-Henning Kamp #include <sys/sysctl.h> 608d809d50SJeff Roberson #include <sys/smp.h> 61df8bae1dSRodney W. Grimes 621283e9cdSAttilio Rao #ifdef SMP 631283e9cdSAttilio Rao #include <machine/cpu.h> 641283e9cdSAttilio Rao #endif 651283e9cdSAttilio Rao 665b999a6bSDavide Italiano #ifndef NO_EVENTTIMERS 675b999a6bSDavide Italiano DPCPU_DECLARE(sbintime_t, hardclocktime); 685b999a6bSDavide Italiano #endif 695b999a6bSDavide Italiano 7091dd9aaeSRobert Watson SDT_PROVIDER_DEFINE(callout_execute); 71d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(callout_execute, kernel, , callout__start, 7291dd9aaeSRobert Watson "struct callout *"); 73d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(callout_execute, kernel, , callout__end, 7491dd9aaeSRobert Watson "struct callout *"); 7591dd9aaeSRobert Watson 765b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING 7722ee8c4fSPoul-Henning Kamp static int avg_depth; 7822ee8c4fSPoul-Henning Kamp SYSCTL_INT(_debug, OID_AUTO, to_avg_depth, CTLFLAG_RD, &avg_depth, 0, 7922ee8c4fSPoul-Henning Kamp "Average number of items examined per softclock call. Units = 1/1000"); 8022ee8c4fSPoul-Henning Kamp static int avg_gcalls; 8122ee8c4fSPoul-Henning Kamp SYSCTL_INT(_debug, OID_AUTO, to_avg_gcalls, CTLFLAG_RD, &avg_gcalls, 0, 8222ee8c4fSPoul-Henning Kamp "Average number of Giant callouts made per softclock call. Units = 1/1000"); 8364b9ee20SAttilio Rao static int avg_lockcalls; 8464b9ee20SAttilio Rao SYSCTL_INT(_debug, OID_AUTO, to_avg_lockcalls, CTLFLAG_RD, &avg_lockcalls, 0, 8564b9ee20SAttilio Rao "Average number of lock callouts made per softclock call. Units = 1/1000"); 8622ee8c4fSPoul-Henning Kamp static int avg_mpcalls; 8722ee8c4fSPoul-Henning Kamp SYSCTL_INT(_debug, OID_AUTO, to_avg_mpcalls, CTLFLAG_RD, &avg_mpcalls, 0, 8822ee8c4fSPoul-Henning Kamp "Average number of MP callouts made per softclock call. Units = 1/1000"); 895b999a6bSDavide Italiano static int avg_depth_dir; 905b999a6bSDavide Italiano SYSCTL_INT(_debug, OID_AUTO, to_avg_depth_dir, CTLFLAG_RD, &avg_depth_dir, 0, 915b999a6bSDavide Italiano "Average number of direct callouts examined per callout_process call. " 925b999a6bSDavide Italiano "Units = 1/1000"); 935b999a6bSDavide Italiano static int avg_lockcalls_dir; 945b999a6bSDavide Italiano SYSCTL_INT(_debug, OID_AUTO, to_avg_lockcalls_dir, CTLFLAG_RD, 955b999a6bSDavide Italiano &avg_lockcalls_dir, 0, "Average number of lock direct callouts made per " 965b999a6bSDavide Italiano "callout_process call. Units = 1/1000"); 975b999a6bSDavide Italiano static int avg_mpcalls_dir; 985b999a6bSDavide Italiano SYSCTL_INT(_debug, OID_AUTO, to_avg_mpcalls_dir, CTLFLAG_RD, &avg_mpcalls_dir, 995b999a6bSDavide Italiano 0, "Average number of MP direct callouts made per callout_process call. " 1005b999a6bSDavide Italiano "Units = 1/1000"); 1015b999a6bSDavide Italiano #endif 102f8ccf82aSAndre Oppermann 103f8ccf82aSAndre Oppermann static int ncallout; 104*af3b2549SHans Petter Selasky SYSCTL_INT(_kern, OID_AUTO, ncallout, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &ncallout, 0, 105f8ccf82aSAndre Oppermann "Number of entries in callwheel and size of timeout() preallocation"); 106f8ccf82aSAndre Oppermann 107ac75ee9fSAdrian Chadd static int pin_default_swi = 0; 108ac75ee9fSAdrian Chadd static int pin_pcpu_swi = 0; 109ac75ee9fSAdrian Chadd 110*af3b2549SHans Petter Selasky SYSCTL_INT(_kern, OID_AUTO, pin_default_swi, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &pin_default_swi, 111ac75ee9fSAdrian Chadd 0, "Pin the default (non-per-cpu) swi (shared with PCPU 0 swi)"); 112*af3b2549SHans Petter Selasky SYSCTL_INT(_kern, OID_AUTO, pin_pcpu_swi, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &pin_pcpu_swi, 113ac75ee9fSAdrian Chadd 0, "Pin the per-CPU swis (except PCPU 0, which is also default"); 114ac75ee9fSAdrian Chadd 11515b7a470SPoul-Henning Kamp /* 11615b7a470SPoul-Henning Kamp * TODO: 11715b7a470SPoul-Henning Kamp * allocate more timeout table slots when table overflows. 11815b7a470SPoul-Henning Kamp */ 1193f555c45SDavide Italiano u_int callwheelsize, callwheelmask; 120f23b4c91SGarrett Wollman 12120c510f8SLuigi Rizzo /* 1225b999a6bSDavide Italiano * The callout cpu exec entities represent informations necessary for 1235b999a6bSDavide Italiano * describing the state of callouts currently running on the CPU and the ones 1245b999a6bSDavide Italiano * necessary for migrating callouts to the new callout cpu. In particular, 1255b999a6bSDavide Italiano * the first entry of the array cc_exec_entity holds informations for callout 1265b999a6bSDavide Italiano * running in SWI thread context, while the second one holds informations 1275b999a6bSDavide Italiano * for callout running directly from hardware interrupt context. 1281283e9cdSAttilio Rao * The cached informations are very important for deferring migration when 1291283e9cdSAttilio Rao * the migrating callout is already running. 1301283e9cdSAttilio Rao */ 1315b999a6bSDavide Italiano struct cc_exec { 1325b999a6bSDavide Italiano struct callout *cc_next; 1335b999a6bSDavide Italiano struct callout *cc_curr; 1341283e9cdSAttilio Rao #ifdef SMP 1351283e9cdSAttilio Rao void (*ce_migration_func)(void *); 1361283e9cdSAttilio Rao void *ce_migration_arg; 1371283e9cdSAttilio Rao int ce_migration_cpu; 1385b999a6bSDavide Italiano sbintime_t ce_migration_time; 1393f321a4eSDavide Italiano sbintime_t ce_migration_prec; 1401283e9cdSAttilio Rao #endif 141a4a3ce99SDavide Italiano bool cc_cancel; 142a4a3ce99SDavide Italiano bool cc_waiting; 1431283e9cdSAttilio Rao }; 1441283e9cdSAttilio Rao 1451283e9cdSAttilio Rao /* 14620c510f8SLuigi Rizzo * There is one struct callout_cpu per cpu, holding all relevant 14720c510f8SLuigi Rizzo * state for the callout processing thread on the individual CPU. 14820c510f8SLuigi Rizzo */ 1498d809d50SJeff Roberson struct callout_cpu { 1504ceaf45dSAttilio Rao struct mtx_padalign cc_lock; 1515b999a6bSDavide Italiano struct cc_exec cc_exec_entity[2]; 1528d809d50SJeff Roberson struct callout *cc_callout; 1535b999a6bSDavide Italiano struct callout_list *cc_callwheel; 1545b999a6bSDavide Italiano struct callout_tailq cc_expireq; 1555b999a6bSDavide Italiano struct callout_slist cc_callfree; 1565b999a6bSDavide Italiano sbintime_t cc_firstevent; 1575b999a6bSDavide Italiano sbintime_t cc_lastscan; 1588d809d50SJeff Roberson void *cc_cookie; 1595b999a6bSDavide Italiano u_int cc_bucket; 1608d809d50SJeff Roberson }; 1618d809d50SJeff Roberson 1625b999a6bSDavide Italiano #define cc_exec_curr cc_exec_entity[0].cc_curr 1635b999a6bSDavide Italiano #define cc_exec_next cc_exec_entity[0].cc_next 1645b999a6bSDavide Italiano #define cc_exec_cancel cc_exec_entity[0].cc_cancel 1655b999a6bSDavide Italiano #define cc_exec_waiting cc_exec_entity[0].cc_waiting 1665b999a6bSDavide Italiano #define cc_exec_curr_dir cc_exec_entity[1].cc_curr 1675b999a6bSDavide Italiano #define cc_exec_next_dir cc_exec_entity[1].cc_next 1685b999a6bSDavide Italiano #define cc_exec_cancel_dir cc_exec_entity[1].cc_cancel 1695b999a6bSDavide Italiano #define cc_exec_waiting_dir cc_exec_entity[1].cc_waiting 1705b999a6bSDavide Italiano 1718d809d50SJeff Roberson #ifdef SMP 1725b999a6bSDavide Italiano #define cc_migration_func cc_exec_entity[0].ce_migration_func 1735b999a6bSDavide Italiano #define cc_migration_arg cc_exec_entity[0].ce_migration_arg 1745b999a6bSDavide Italiano #define cc_migration_cpu cc_exec_entity[0].ce_migration_cpu 1755b999a6bSDavide Italiano #define cc_migration_time cc_exec_entity[0].ce_migration_time 1763f321a4eSDavide Italiano #define cc_migration_prec cc_exec_entity[0].ce_migration_prec 1775b999a6bSDavide Italiano #define cc_migration_func_dir cc_exec_entity[1].ce_migration_func 1785b999a6bSDavide Italiano #define cc_migration_arg_dir cc_exec_entity[1].ce_migration_arg 1795b999a6bSDavide Italiano #define cc_migration_cpu_dir cc_exec_entity[1].ce_migration_cpu 1805b999a6bSDavide Italiano #define cc_migration_time_dir cc_exec_entity[1].ce_migration_time 1813f321a4eSDavide Italiano #define cc_migration_prec_dir cc_exec_entity[1].ce_migration_prec 1821283e9cdSAttilio Rao 1838d809d50SJeff Roberson struct callout_cpu cc_cpu[MAXCPU]; 1841283e9cdSAttilio Rao #define CPUBLOCK MAXCPU 1858d809d50SJeff Roberson #define CC_CPU(cpu) (&cc_cpu[(cpu)]) 1868d809d50SJeff Roberson #define CC_SELF() CC_CPU(PCPU_GET(cpuid)) 1878d809d50SJeff Roberson #else 1888d809d50SJeff Roberson struct callout_cpu cc_cpu; 1898d809d50SJeff Roberson #define CC_CPU(cpu) &cc_cpu 1908d809d50SJeff Roberson #define CC_SELF() &cc_cpu 1918d809d50SJeff Roberson #endif 1928d809d50SJeff Roberson #define CC_LOCK(cc) mtx_lock_spin(&(cc)->cc_lock) 1938d809d50SJeff Roberson #define CC_UNLOCK(cc) mtx_unlock_spin(&(cc)->cc_lock) 1941283e9cdSAttilio Rao #define CC_LOCK_ASSERT(cc) mtx_assert(&(cc)->cc_lock, MA_OWNED) 1958d809d50SJeff Roberson 1968d809d50SJeff Roberson static int timeout_cpu; 1975b999a6bSDavide Italiano 19815ae0c9aSAndre Oppermann static void callout_cpu_init(struct callout_cpu *cc); 1995b999a6bSDavide Italiano static void softclock_call_cc(struct callout *c, struct callout_cpu *cc, 2005b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING 2015b999a6bSDavide Italiano int *mpcalls, int *lockcalls, int *gcalls, 2025b999a6bSDavide Italiano #endif 2035b999a6bSDavide Italiano int direct); 2048d809d50SJeff Roberson 205d745c852SEd Schouten static MALLOC_DEFINE(M_CALLOUT, "callout", "Callout datastructures"); 20649a74476SColin Percival 207e9dec2c4SColin Percival /** 2088d809d50SJeff Roberson * Locked by cc_lock: 2095b999a6bSDavide Italiano * cc_curr - If a callout is in progress, it is cc_curr. 2105b999a6bSDavide Italiano * If cc_curr is non-NULL, threads waiting in 211b36f4588SJohn Baldwin * callout_drain() will be woken up as soon as the 2122c1bb207SColin Percival * relevant callout completes. 2135b999a6bSDavide Italiano * cc_cancel - Changing to 1 with both callout_lock and cc_lock held 21498c926b2SIan Dowse * guarantees that the current callout will not run. 21598c926b2SIan Dowse * The softclock() function sets this to 0 before it 21664b9ee20SAttilio Rao * drops callout_lock to acquire c_lock, and it calls 217b36f4588SJohn Baldwin * the handler only if curr_cancelled is still 0 after 2185b999a6bSDavide Italiano * cc_lock is successfully acquired. 2198d809d50SJeff Roberson * cc_waiting - If a thread is waiting in callout_drain(), then 220b36f4588SJohn Baldwin * callout_wait is nonzero. Set only when 2215b999a6bSDavide Italiano * cc_curr is non-NULL. 2222c1bb207SColin Percival */ 223df8bae1dSRodney W. Grimes 224df8bae1dSRodney W. Grimes /* 2255b999a6bSDavide Italiano * Resets the execution entity tied to a specific callout cpu. 2261283e9cdSAttilio Rao */ 2271283e9cdSAttilio Rao static void 2285b999a6bSDavide Italiano cc_cce_cleanup(struct callout_cpu *cc, int direct) 2291283e9cdSAttilio Rao { 2301283e9cdSAttilio Rao 2315b999a6bSDavide Italiano cc->cc_exec_entity[direct].cc_curr = NULL; 2325b999a6bSDavide Italiano cc->cc_exec_entity[direct].cc_next = NULL; 233ac42a172SDavide Italiano cc->cc_exec_entity[direct].cc_cancel = false; 234ac42a172SDavide Italiano cc->cc_exec_entity[direct].cc_waiting = false; 2351283e9cdSAttilio Rao #ifdef SMP 2365b999a6bSDavide Italiano cc->cc_exec_entity[direct].ce_migration_cpu = CPUBLOCK; 2375b999a6bSDavide Italiano cc->cc_exec_entity[direct].ce_migration_time = 0; 2383f321a4eSDavide Italiano cc->cc_exec_entity[direct].ce_migration_prec = 0; 2395b999a6bSDavide Italiano cc->cc_exec_entity[direct].ce_migration_func = NULL; 2405b999a6bSDavide Italiano cc->cc_exec_entity[direct].ce_migration_arg = NULL; 2411283e9cdSAttilio Rao #endif 2421283e9cdSAttilio Rao } 2431283e9cdSAttilio Rao 2441283e9cdSAttilio Rao /* 2451283e9cdSAttilio Rao * Checks if migration is requested by a specific callout cpu. 2461283e9cdSAttilio Rao */ 2471283e9cdSAttilio Rao static int 2485b999a6bSDavide Italiano cc_cce_migrating(struct callout_cpu *cc, int direct) 2491283e9cdSAttilio Rao { 2501283e9cdSAttilio Rao 2511283e9cdSAttilio Rao #ifdef SMP 2525b999a6bSDavide Italiano return (cc->cc_exec_entity[direct].ce_migration_cpu != CPUBLOCK); 2531283e9cdSAttilio Rao #else 2541283e9cdSAttilio Rao return (0); 2551283e9cdSAttilio Rao #endif 2561283e9cdSAttilio Rao } 2571283e9cdSAttilio Rao 2581283e9cdSAttilio Rao /* 25915ae0c9aSAndre Oppermann * Kernel low level callwheel initialization 26015ae0c9aSAndre Oppermann * called on cpu0 during kernel startup. 261219d632cSMatthew Dillon */ 26215ae0c9aSAndre Oppermann static void 26315ae0c9aSAndre Oppermann callout_callwheel_init(void *dummy) 264219d632cSMatthew Dillon { 2658d809d50SJeff Roberson struct callout_cpu *cc; 2668d809d50SJeff Roberson 267f8ccf82aSAndre Oppermann /* 268f8ccf82aSAndre Oppermann * Calculate the size of the callout wheel and the preallocated 269f8ccf82aSAndre Oppermann * timeout() structures. 270a7aea132SAndre Oppermann * XXX: Clip callout to result of previous function of maxusers 271a7aea132SAndre Oppermann * maximum 384. This is still huge, but acceptable. 272f8ccf82aSAndre Oppermann */ 273f8ccf82aSAndre Oppermann ncallout = imin(16 + maxproc + maxfiles, 18508); 274f8ccf82aSAndre Oppermann TUNABLE_INT_FETCH("kern.ncallout", &ncallout); 275f8ccf82aSAndre Oppermann 276219d632cSMatthew Dillon /* 277922314f0SAlfred Perlstein * Calculate callout wheel size, should be next power of two higher 278922314f0SAlfred Perlstein * than 'ncallout'. 279219d632cSMatthew Dillon */ 280922314f0SAlfred Perlstein callwheelsize = 1 << fls(ncallout); 281219d632cSMatthew Dillon callwheelmask = callwheelsize - 1; 282219d632cSMatthew Dillon 28315ae0c9aSAndre Oppermann /* 284ac75ee9fSAdrian Chadd * Fetch whether we're pinning the swi's or not. 285ac75ee9fSAdrian Chadd */ 286ac75ee9fSAdrian Chadd TUNABLE_INT_FETCH("kern.pin_default_swi", &pin_default_swi); 287ac75ee9fSAdrian Chadd TUNABLE_INT_FETCH("kern.pin_pcpu_swi", &pin_pcpu_swi); 288ac75ee9fSAdrian Chadd 289ac75ee9fSAdrian Chadd /* 29015ae0c9aSAndre Oppermann * Only cpu0 handles timeout(9) and receives a preallocation. 29115ae0c9aSAndre Oppermann * 29215ae0c9aSAndre Oppermann * XXX: Once all timeout(9) consumers are converted this can 29315ae0c9aSAndre Oppermann * be removed. 29415ae0c9aSAndre Oppermann */ 29515ae0c9aSAndre Oppermann timeout_cpu = PCPU_GET(cpuid); 29615ae0c9aSAndre Oppermann cc = CC_CPU(timeout_cpu); 29715ae0c9aSAndre Oppermann cc->cc_callout = malloc(ncallout * sizeof(struct callout), 29815ae0c9aSAndre Oppermann M_CALLOUT, M_WAITOK); 29915ae0c9aSAndre Oppermann callout_cpu_init(cc); 300219d632cSMatthew Dillon } 30115ae0c9aSAndre Oppermann SYSINIT(callwheel_init, SI_SUB_CPU, SI_ORDER_ANY, callout_callwheel_init, NULL); 302219d632cSMatthew Dillon 30315ae0c9aSAndre Oppermann /* 30415ae0c9aSAndre Oppermann * Initialize the per-cpu callout structures. 30515ae0c9aSAndre Oppermann */ 3068d809d50SJeff Roberson static void 3078d809d50SJeff Roberson callout_cpu_init(struct callout_cpu *cc) 3088d809d50SJeff Roberson { 3098d809d50SJeff Roberson struct callout *c; 3108d809d50SJeff Roberson int i; 3118d809d50SJeff Roberson 3128d809d50SJeff Roberson mtx_init(&cc->cc_lock, "callout", NULL, MTX_SPIN | MTX_RECURSE); 3138d809d50SJeff Roberson SLIST_INIT(&cc->cc_callfree); 314c5904471SDavide Italiano cc->cc_callwheel = malloc(sizeof(struct callout_list) * callwheelsize, 31515ae0c9aSAndre Oppermann M_CALLOUT, M_WAITOK); 3165b999a6bSDavide Italiano for (i = 0; i < callwheelsize; i++) 3175b999a6bSDavide Italiano LIST_INIT(&cc->cc_callwheel[i]); 3185b999a6bSDavide Italiano TAILQ_INIT(&cc->cc_expireq); 3194bc38a5aSDavide Italiano cc->cc_firstevent = SBT_MAX; 3205b999a6bSDavide Italiano for (i = 0; i < 2; i++) 3215b999a6bSDavide Italiano cc_cce_cleanup(cc, i); 32215ae0c9aSAndre Oppermann if (cc->cc_callout == NULL) /* Only cpu0 handles timeout(9) */ 3238d809d50SJeff Roberson return; 3248d809d50SJeff Roberson for (i = 0; i < ncallout; i++) { 3258d809d50SJeff Roberson c = &cc->cc_callout[i]; 3268d809d50SJeff Roberson callout_init(c, 0); 3278d809d50SJeff Roberson c->c_flags = CALLOUT_LOCAL_ALLOC; 3288d809d50SJeff Roberson SLIST_INSERT_HEAD(&cc->cc_callfree, c, c_links.sle); 3298d809d50SJeff Roberson } 3308d809d50SJeff Roberson } 3318d809d50SJeff Roberson 3321283e9cdSAttilio Rao #ifdef SMP 3331283e9cdSAttilio Rao /* 3341283e9cdSAttilio Rao * Switches the cpu tied to a specific callout. 3351283e9cdSAttilio Rao * The function expects a locked incoming callout cpu and returns with 3361283e9cdSAttilio Rao * locked outcoming callout cpu. 3371283e9cdSAttilio Rao */ 3381283e9cdSAttilio Rao static struct callout_cpu * 3391283e9cdSAttilio Rao callout_cpu_switch(struct callout *c, struct callout_cpu *cc, int new_cpu) 3401283e9cdSAttilio Rao { 3411283e9cdSAttilio Rao struct callout_cpu *new_cc; 3421283e9cdSAttilio Rao 3431283e9cdSAttilio Rao MPASS(c != NULL && cc != NULL); 3441283e9cdSAttilio Rao CC_LOCK_ASSERT(cc); 3451283e9cdSAttilio Rao 346e75baa28SAttilio Rao /* 347e75baa28SAttilio Rao * Avoid interrupts and preemption firing after the callout cpu 348e75baa28SAttilio Rao * is blocked in order to avoid deadlocks as the new thread 349e75baa28SAttilio Rao * may be willing to acquire the callout cpu lock. 350e75baa28SAttilio Rao */ 3511283e9cdSAttilio Rao c->c_cpu = CPUBLOCK; 352e75baa28SAttilio Rao spinlock_enter(); 3531283e9cdSAttilio Rao CC_UNLOCK(cc); 3541283e9cdSAttilio Rao new_cc = CC_CPU(new_cpu); 3551283e9cdSAttilio Rao CC_LOCK(new_cc); 356e75baa28SAttilio Rao spinlock_exit(); 3571283e9cdSAttilio Rao c->c_cpu = new_cpu; 3581283e9cdSAttilio Rao return (new_cc); 3591283e9cdSAttilio Rao } 3601283e9cdSAttilio Rao #endif 3611283e9cdSAttilio Rao 362219d632cSMatthew Dillon /* 3638d809d50SJeff Roberson * Start standard softclock thread. 3648d809d50SJeff Roberson */ 3658d809d50SJeff Roberson static void 3668d809d50SJeff Roberson start_softclock(void *dummy) 3678d809d50SJeff Roberson { 3688d809d50SJeff Roberson struct callout_cpu *cc; 369f44e2a4cSAdrian Chadd char name[MAXCOMLEN]; 3708d809d50SJeff Roberson #ifdef SMP 3718d809d50SJeff Roberson int cpu; 372ac75ee9fSAdrian Chadd struct intr_event *ie; 3738d809d50SJeff Roberson #endif 3748d809d50SJeff Roberson 3758d809d50SJeff Roberson cc = CC_CPU(timeout_cpu); 376f44e2a4cSAdrian Chadd snprintf(name, sizeof(name), "clock (%d)", timeout_cpu); 377f44e2a4cSAdrian Chadd if (swi_add(&clk_intr_event, name, softclock, cc, SWI_CLOCK, 3783350df48SJohn Baldwin INTR_MPSAFE, &cc->cc_cookie)) 3798d809d50SJeff Roberson panic("died while creating standard software ithreads"); 380ac75ee9fSAdrian Chadd if (pin_default_swi && 381ac75ee9fSAdrian Chadd (intr_event_bind(clk_intr_event, timeout_cpu) != 0)) { 382ac75ee9fSAdrian Chadd printf("%s: timeout clock couldn't be pinned to cpu %d\n", 383ac75ee9fSAdrian Chadd __func__, 384ac75ee9fSAdrian Chadd timeout_cpu); 385ac75ee9fSAdrian Chadd } 386ac75ee9fSAdrian Chadd 3878d809d50SJeff Roberson #ifdef SMP 3883aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 3898d809d50SJeff Roberson if (cpu == timeout_cpu) 3908d809d50SJeff Roberson continue; 3918d809d50SJeff Roberson cc = CC_CPU(cpu); 39215ae0c9aSAndre Oppermann cc->cc_callout = NULL; /* Only cpu0 handles timeout(9). */ 39315ae0c9aSAndre Oppermann callout_cpu_init(cc); 394f44e2a4cSAdrian Chadd snprintf(name, sizeof(name), "clock (%d)", cpu); 395ac75ee9fSAdrian Chadd ie = NULL; 396ac75ee9fSAdrian Chadd if (swi_add(&ie, name, softclock, cc, SWI_CLOCK, 3978d809d50SJeff Roberson INTR_MPSAFE, &cc->cc_cookie)) 3988d809d50SJeff Roberson panic("died while creating standard software ithreads"); 399ac75ee9fSAdrian Chadd if (pin_pcpu_swi && (intr_event_bind(ie, cpu) != 0)) { 400ac75ee9fSAdrian Chadd printf("%s: per-cpu clock couldn't be pinned to " 401ac75ee9fSAdrian Chadd "cpu %d\n", 402ac75ee9fSAdrian Chadd __func__, 403ac75ee9fSAdrian Chadd cpu); 404ac75ee9fSAdrian Chadd } 405219d632cSMatthew Dillon } 4068d809d50SJeff Roberson #endif 407219d632cSMatthew Dillon } 4088d809d50SJeff Roberson SYSINIT(start_softclock, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softclock, NULL); 4098d809d50SJeff Roberson 4105b999a6bSDavide Italiano #define CC_HASH_SHIFT 8 4118d809d50SJeff Roberson 4125b999a6bSDavide Italiano static inline u_int 4135b999a6bSDavide Italiano callout_hash(sbintime_t sbt) 4145b999a6bSDavide Italiano { 4155b999a6bSDavide Italiano 4165b999a6bSDavide Italiano return (sbt >> (32 - CC_HASH_SHIFT)); 4175b999a6bSDavide Italiano } 4185b999a6bSDavide Italiano 4195b999a6bSDavide Italiano static inline u_int 4205b999a6bSDavide Italiano callout_get_bucket(sbintime_t sbt) 4215b999a6bSDavide Italiano { 4225b999a6bSDavide Italiano 4235b999a6bSDavide Italiano return (callout_hash(sbt) & callwheelmask); 4245b999a6bSDavide Italiano } 4255b999a6bSDavide Italiano 4265b999a6bSDavide Italiano void 4275b999a6bSDavide Italiano callout_process(sbintime_t now) 4285b999a6bSDavide Italiano { 4295b999a6bSDavide Italiano struct callout *tmp, *tmpn; 4305b999a6bSDavide Italiano struct callout_cpu *cc; 4315b999a6bSDavide Italiano struct callout_list *sc; 4325b999a6bSDavide Italiano sbintime_t first, last, max, tmp_max; 4335b999a6bSDavide Italiano uint32_t lookahead; 4345b999a6bSDavide Italiano u_int firstb, lastb, nowb; 4355b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING 4365b999a6bSDavide Italiano int depth_dir = 0, mpcalls_dir = 0, lockcalls_dir = 0; 4375b999a6bSDavide Italiano #endif 4385b999a6bSDavide Italiano 4398d809d50SJeff Roberson cc = CC_SELF(); 4408d809d50SJeff Roberson mtx_lock_spin_flags(&cc->cc_lock, MTX_QUIET); 4415b999a6bSDavide Italiano 4425b999a6bSDavide Italiano /* Compute the buckets of the last scan and present times. */ 4435b999a6bSDavide Italiano firstb = callout_hash(cc->cc_lastscan); 4445b999a6bSDavide Italiano cc->cc_lastscan = now; 4455b999a6bSDavide Italiano nowb = callout_hash(now); 4465b999a6bSDavide Italiano 4475b999a6bSDavide Italiano /* Compute the last bucket and minimum time of the bucket after it. */ 4485b999a6bSDavide Italiano if (nowb == firstb) 4495b999a6bSDavide Italiano lookahead = (SBT_1S / 16); 4505b999a6bSDavide Italiano else if (nowb - firstb == 1) 4515b999a6bSDavide Italiano lookahead = (SBT_1S / 8); 4525b999a6bSDavide Italiano else 4535b999a6bSDavide Italiano lookahead = (SBT_1S / 2); 4545b999a6bSDavide Italiano first = last = now; 4555b999a6bSDavide Italiano first += (lookahead / 2); 4565b999a6bSDavide Italiano last += lookahead; 4575b999a6bSDavide Italiano last &= (0xffffffffffffffffLLU << (32 - CC_HASH_SHIFT)); 4585b999a6bSDavide Italiano lastb = callout_hash(last) - 1; 4595b999a6bSDavide Italiano max = last; 4605b999a6bSDavide Italiano 4615b999a6bSDavide Italiano /* 4625b999a6bSDavide Italiano * Check if we wrapped around the entire wheel from the last scan. 4635b999a6bSDavide Italiano * In case, we need to scan entirely the wheel for pending callouts. 4645b999a6bSDavide Italiano */ 4655b999a6bSDavide Italiano if (lastb - firstb >= callwheelsize) { 4665b999a6bSDavide Italiano lastb = firstb + callwheelsize - 1; 4675b999a6bSDavide Italiano if (nowb - firstb >= callwheelsize) 4685b999a6bSDavide Italiano nowb = lastb; 4699fc51b0bSJeff Roberson } 4705b999a6bSDavide Italiano 4715b999a6bSDavide Italiano /* Iterate callwheel from firstb to nowb and then up to lastb. */ 4725b999a6bSDavide Italiano do { 4735b999a6bSDavide Italiano sc = &cc->cc_callwheel[firstb & callwheelmask]; 4745b999a6bSDavide Italiano tmp = LIST_FIRST(sc); 4755b999a6bSDavide Italiano while (tmp != NULL) { 4765b999a6bSDavide Italiano /* Run the callout if present time within allowed. */ 4775b999a6bSDavide Italiano if (tmp->c_time <= now) { 4785b999a6bSDavide Italiano /* 4795b999a6bSDavide Italiano * Consumer told us the callout may be run 4805b999a6bSDavide Italiano * directly from hardware interrupt context. 4815b999a6bSDavide Italiano */ 4825b999a6bSDavide Italiano if (tmp->c_flags & CALLOUT_DIRECT) { 4835b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING 4845b999a6bSDavide Italiano ++depth_dir; 4855b999a6bSDavide Italiano #endif 4865b999a6bSDavide Italiano cc->cc_exec_next_dir = 4875b999a6bSDavide Italiano LIST_NEXT(tmp, c_links.le); 4885b999a6bSDavide Italiano cc->cc_bucket = firstb & callwheelmask; 4895b999a6bSDavide Italiano LIST_REMOVE(tmp, c_links.le); 4905b999a6bSDavide Italiano softclock_call_cc(tmp, cc, 4915b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING 4925b999a6bSDavide Italiano &mpcalls_dir, &lockcalls_dir, NULL, 4935b999a6bSDavide Italiano #endif 4945b999a6bSDavide Italiano 1); 4955b999a6bSDavide Italiano tmp = cc->cc_exec_next_dir; 4965b999a6bSDavide Italiano } else { 4975b999a6bSDavide Italiano tmpn = LIST_NEXT(tmp, c_links.le); 4985b999a6bSDavide Italiano LIST_REMOVE(tmp, c_links.le); 4995b999a6bSDavide Italiano TAILQ_INSERT_TAIL(&cc->cc_expireq, 5005b999a6bSDavide Italiano tmp, c_links.tqe); 5015b999a6bSDavide Italiano tmp->c_flags |= CALLOUT_PROCESSED; 5025b999a6bSDavide Italiano tmp = tmpn; 5039fc51b0bSJeff Roberson } 5045b999a6bSDavide Italiano continue; 5055b999a6bSDavide Italiano } 5065b999a6bSDavide Italiano /* Skip events from distant future. */ 5075b999a6bSDavide Italiano if (tmp->c_time >= max) 5085b999a6bSDavide Italiano goto next; 5095b999a6bSDavide Italiano /* 5105b999a6bSDavide Italiano * Event minimal time is bigger than present maximal 5115b999a6bSDavide Italiano * time, so it cannot be aggregated. 5125b999a6bSDavide Italiano */ 5135b999a6bSDavide Italiano if (tmp->c_time > last) { 5145b999a6bSDavide Italiano lastb = nowb; 5155b999a6bSDavide Italiano goto next; 5165b999a6bSDavide Italiano } 5175b999a6bSDavide Italiano /* Update first and last time, respecting this event. */ 5185b999a6bSDavide Italiano if (tmp->c_time < first) 5195b999a6bSDavide Italiano first = tmp->c_time; 5205b999a6bSDavide Italiano tmp_max = tmp->c_time + tmp->c_precision; 5215b999a6bSDavide Italiano if (tmp_max < last) 5225b999a6bSDavide Italiano last = tmp_max; 5235b999a6bSDavide Italiano next: 5245b999a6bSDavide Italiano tmp = LIST_NEXT(tmp, c_links.le); 5255b999a6bSDavide Italiano } 5265b999a6bSDavide Italiano /* Proceed with the next bucket. */ 5275b999a6bSDavide Italiano firstb++; 5285b999a6bSDavide Italiano /* 5295b999a6bSDavide Italiano * Stop if we looked after present time and found 5305b999a6bSDavide Italiano * some event we can't execute at now. 5315b999a6bSDavide Italiano * Stop if we looked far enough into the future. 5325b999a6bSDavide Italiano */ 5335b999a6bSDavide Italiano } while (((int)(firstb - lastb)) <= 0); 5345b999a6bSDavide Italiano cc->cc_firstevent = last; 5355b999a6bSDavide Italiano #ifndef NO_EVENTTIMERS 5365b999a6bSDavide Italiano cpu_new_callout(curcpu, last, first); 5375b999a6bSDavide Italiano #endif 5385b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING 5395b999a6bSDavide Italiano avg_depth_dir += (depth_dir * 1000 - avg_depth_dir) >> 8; 5405b999a6bSDavide Italiano avg_mpcalls_dir += (mpcalls_dir * 1000 - avg_mpcalls_dir) >> 8; 5415b999a6bSDavide Italiano avg_lockcalls_dir += (lockcalls_dir * 1000 - avg_lockcalls_dir) >> 8; 5425b999a6bSDavide Italiano #endif 5438d809d50SJeff Roberson mtx_unlock_spin_flags(&cc->cc_lock, MTX_QUIET); 5448d809d50SJeff Roberson /* 5458d809d50SJeff Roberson * swi_sched acquires the thread lock, so we don't want to call it 5468d809d50SJeff Roberson * with cc_lock held; incorrect locking order. 5478d809d50SJeff Roberson */ 5485b999a6bSDavide Italiano if (!TAILQ_EMPTY(&cc->cc_expireq)) 5498d809d50SJeff Roberson swi_sched(cc->cc_cookie, 0); 5508d809d50SJeff Roberson } 5518d809d50SJeff Roberson 5528d809d50SJeff Roberson static struct callout_cpu * 5538d809d50SJeff Roberson callout_lock(struct callout *c) 5548d809d50SJeff Roberson { 5558d809d50SJeff Roberson struct callout_cpu *cc; 5568d809d50SJeff Roberson int cpu; 5578d809d50SJeff Roberson 5588d809d50SJeff Roberson for (;;) { 5598d809d50SJeff Roberson cpu = c->c_cpu; 5601283e9cdSAttilio Rao #ifdef SMP 5611283e9cdSAttilio Rao if (cpu == CPUBLOCK) { 5621283e9cdSAttilio Rao while (c->c_cpu == CPUBLOCK) 5631283e9cdSAttilio Rao cpu_spinwait(); 5641283e9cdSAttilio Rao continue; 5651283e9cdSAttilio Rao } 5661283e9cdSAttilio Rao #endif 5678d809d50SJeff Roberson cc = CC_CPU(cpu); 5688d809d50SJeff Roberson CC_LOCK(cc); 5698d809d50SJeff Roberson if (cpu == c->c_cpu) 5708d809d50SJeff Roberson break; 5718d809d50SJeff Roberson CC_UNLOCK(cc); 5728d809d50SJeff Roberson } 5738d809d50SJeff Roberson return (cc); 574219d632cSMatthew Dillon } 575219d632cSMatthew Dillon 5761283e9cdSAttilio Rao static void 5775b999a6bSDavide Italiano callout_cc_add(struct callout *c, struct callout_cpu *cc, 5785b999a6bSDavide Italiano sbintime_t sbt, sbintime_t precision, void (*func)(void *), 5795b999a6bSDavide Italiano void *arg, int cpu, int flags) 5801283e9cdSAttilio Rao { 5815b999a6bSDavide Italiano int bucket; 5821283e9cdSAttilio Rao 5831283e9cdSAttilio Rao CC_LOCK_ASSERT(cc); 5845b999a6bSDavide Italiano if (sbt < cc->cc_lastscan) 5855b999a6bSDavide Italiano sbt = cc->cc_lastscan; 5861283e9cdSAttilio Rao c->c_arg = arg; 5871283e9cdSAttilio Rao c->c_flags |= (CALLOUT_ACTIVE | CALLOUT_PENDING); 5885b999a6bSDavide Italiano if (flags & C_DIRECT_EXEC) 5895b999a6bSDavide Italiano c->c_flags |= CALLOUT_DIRECT; 5905b999a6bSDavide Italiano c->c_flags &= ~CALLOUT_PROCESSED; 5911283e9cdSAttilio Rao c->c_func = func; 5925b999a6bSDavide Italiano c->c_time = sbt; 5935b999a6bSDavide Italiano c->c_precision = precision; 5945b999a6bSDavide Italiano bucket = callout_get_bucket(c->c_time); 5955b999a6bSDavide Italiano CTR3(KTR_CALLOUT, "precision set for %p: %d.%08x", 5965b999a6bSDavide Italiano c, (int)(c->c_precision >> 32), 5975b999a6bSDavide Italiano (u_int)(c->c_precision & 0xffffffff)); 5985b999a6bSDavide Italiano LIST_INSERT_HEAD(&cc->cc_callwheel[bucket], c, c_links.le); 5995b999a6bSDavide Italiano if (cc->cc_bucket == bucket) 6005b999a6bSDavide Italiano cc->cc_exec_next_dir = c; 6015b999a6bSDavide Italiano #ifndef NO_EVENTTIMERS 6025b999a6bSDavide Italiano /* 6035b999a6bSDavide Italiano * Inform the eventtimers(4) subsystem there's a new callout 6045b999a6bSDavide Italiano * that has been inserted, but only if really required. 6055b999a6bSDavide Italiano */ 6064bc38a5aSDavide Italiano if (SBT_MAX - c->c_time < c->c_precision) 6074bc38a5aSDavide Italiano c->c_precision = SBT_MAX - c->c_time; 6085b999a6bSDavide Italiano sbt = c->c_time + c->c_precision; 6095b999a6bSDavide Italiano if (sbt < cc->cc_firstevent) { 6105b999a6bSDavide Italiano cc->cc_firstevent = sbt; 6115b999a6bSDavide Italiano cpu_new_callout(cpu, sbt, c->c_time); 6121283e9cdSAttilio Rao } 6135b999a6bSDavide Italiano #endif 6141283e9cdSAttilio Rao } 6151283e9cdSAttilio Rao 6166098e7acSKonstantin Belousov static void 6176098e7acSKonstantin Belousov callout_cc_del(struct callout *c, struct callout_cpu *cc) 6186098e7acSKonstantin Belousov { 6196098e7acSKonstantin Belousov 620eb8a7186SKonstantin Belousov if ((c->c_flags & CALLOUT_LOCAL_ALLOC) == 0) 621eb8a7186SKonstantin Belousov return; 6226098e7acSKonstantin Belousov c->c_func = NULL; 6236098e7acSKonstantin Belousov SLIST_INSERT_HEAD(&cc->cc_callfree, c, c_links.sle); 6246098e7acSKonstantin Belousov } 6256098e7acSKonstantin Belousov 626eb8a7186SKonstantin Belousov static void 6275b999a6bSDavide Italiano softclock_call_cc(struct callout *c, struct callout_cpu *cc, 6285b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING 6295b999a6bSDavide Italiano int *mpcalls, int *lockcalls, int *gcalls, 6305b999a6bSDavide Italiano #endif 6315b999a6bSDavide Italiano int direct) 6326098e7acSKonstantin Belousov { 6331f96759fSDavide Italiano struct rm_priotracker tracker; 6346098e7acSKonstantin Belousov void (*c_func)(void *); 6356098e7acSKonstantin Belousov void *c_arg; 6366098e7acSKonstantin Belousov struct lock_class *class; 6376098e7acSKonstantin Belousov struct lock_object *c_lock; 6381f96759fSDavide Italiano uintptr_t lock_status; 6391f96759fSDavide Italiano int c_flags; 6406098e7acSKonstantin Belousov #ifdef SMP 6416098e7acSKonstantin Belousov struct callout_cpu *new_cc; 6426098e7acSKonstantin Belousov void (*new_func)(void *); 6436098e7acSKonstantin Belousov void *new_arg; 6445b999a6bSDavide Italiano int flags, new_cpu; 6453f321a4eSDavide Italiano sbintime_t new_prec, new_time; 6466098e7acSKonstantin Belousov #endif 6475b999a6bSDavide Italiano #if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING) 64803763781SDavide Italiano sbintime_t sbt1, sbt2; 6496098e7acSKonstantin Belousov struct timespec ts2; 6505b999a6bSDavide Italiano static sbintime_t maxdt = 2 * SBT_1MS; /* 2 msec */ 6516098e7acSKonstantin Belousov static timeout_t *lastfunc; 6526098e7acSKonstantin Belousov #endif 6536098e7acSKonstantin Belousov 654eb8a7186SKonstantin Belousov KASSERT((c->c_flags & (CALLOUT_PENDING | CALLOUT_ACTIVE)) == 655eb8a7186SKonstantin Belousov (CALLOUT_PENDING | CALLOUT_ACTIVE), 656eb8a7186SKonstantin Belousov ("softclock_call_cc: pend|act %p %x", c, c->c_flags)); 6576098e7acSKonstantin Belousov class = (c->c_lock != NULL) ? LOCK_CLASS(c->c_lock) : NULL; 6581f96759fSDavide Italiano lock_status = 0; 6591f96759fSDavide Italiano if (c->c_flags & CALLOUT_SHAREDLOCK) { 6601f96759fSDavide Italiano if (class == &lock_class_rm) 6611f96759fSDavide Italiano lock_status = (uintptr_t)&tracker; 6621f96759fSDavide Italiano else 6631f96759fSDavide Italiano lock_status = 1; 6641f96759fSDavide Italiano } 6656098e7acSKonstantin Belousov c_lock = c->c_lock; 6666098e7acSKonstantin Belousov c_func = c->c_func; 6676098e7acSKonstantin Belousov c_arg = c->c_arg; 6686098e7acSKonstantin Belousov c_flags = c->c_flags; 6696098e7acSKonstantin Belousov if (c->c_flags & CALLOUT_LOCAL_ALLOC) 6706098e7acSKonstantin Belousov c->c_flags = CALLOUT_LOCAL_ALLOC; 6716098e7acSKonstantin Belousov else 6726098e7acSKonstantin Belousov c->c_flags &= ~CALLOUT_PENDING; 6735b999a6bSDavide Italiano cc->cc_exec_entity[direct].cc_curr = c; 674ac42a172SDavide Italiano cc->cc_exec_entity[direct].cc_cancel = false; 6756098e7acSKonstantin Belousov CC_UNLOCK(cc); 6766098e7acSKonstantin Belousov if (c_lock != NULL) { 6771f96759fSDavide Italiano class->lc_lock(c_lock, lock_status); 6786098e7acSKonstantin Belousov /* 6796098e7acSKonstantin Belousov * The callout may have been cancelled 6806098e7acSKonstantin Belousov * while we switched locks. 6816098e7acSKonstantin Belousov */ 6825b999a6bSDavide Italiano if (cc->cc_exec_entity[direct].cc_cancel) { 6836098e7acSKonstantin Belousov class->lc_unlock(c_lock); 6846098e7acSKonstantin Belousov goto skip; 6856098e7acSKonstantin Belousov } 6866098e7acSKonstantin Belousov /* The callout cannot be stopped now. */ 687ac42a172SDavide Italiano cc->cc_exec_entity[direct].cc_cancel = true; 6886098e7acSKonstantin Belousov if (c_lock == &Giant.lock_object) { 6895b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING 6906098e7acSKonstantin Belousov (*gcalls)++; 6915b999a6bSDavide Italiano #endif 6925b999a6bSDavide Italiano CTR3(KTR_CALLOUT, "callout giant %p func %p arg %p", 6936098e7acSKonstantin Belousov c, c_func, c_arg); 6946098e7acSKonstantin Belousov } else { 6955b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING 6966098e7acSKonstantin Belousov (*lockcalls)++; 6975b999a6bSDavide Italiano #endif 6986098e7acSKonstantin Belousov CTR3(KTR_CALLOUT, "callout lock %p func %p arg %p", 6996098e7acSKonstantin Belousov c, c_func, c_arg); 7006098e7acSKonstantin Belousov } 7016098e7acSKonstantin Belousov } else { 7025b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING 7036098e7acSKonstantin Belousov (*mpcalls)++; 7045b999a6bSDavide Italiano #endif 7055b999a6bSDavide Italiano CTR3(KTR_CALLOUT, "callout %p func %p arg %p", 7066098e7acSKonstantin Belousov c, c_func, c_arg); 7076098e7acSKonstantin Belousov } 70803763781SDavide Italiano #if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING) 7095b999a6bSDavide Italiano sbt1 = sbinuptime(); 7106098e7acSKonstantin Belousov #endif 7116098e7acSKonstantin Belousov THREAD_NO_SLEEPING(); 712d9fae5abSAndriy Gapon SDT_PROBE(callout_execute, kernel, , callout__start, c, 0, 0, 0, 0); 7136098e7acSKonstantin Belousov c_func(c_arg); 714d9fae5abSAndriy Gapon SDT_PROBE(callout_execute, kernel, , callout__end, c, 0, 0, 0, 0); 7156098e7acSKonstantin Belousov THREAD_SLEEPING_OK(); 71603763781SDavide Italiano #if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING) 71703763781SDavide Italiano sbt2 = sbinuptime(); 71803763781SDavide Italiano sbt2 -= sbt1; 71903763781SDavide Italiano if (sbt2 > maxdt) { 72003763781SDavide Italiano if (lastfunc != c_func || sbt2 > maxdt * 2) { 72103763781SDavide Italiano ts2 = sbttots(sbt2); 7226098e7acSKonstantin Belousov printf( 7236098e7acSKonstantin Belousov "Expensive timeout(9) function: %p(%p) %jd.%09ld s\n", 7246098e7acSKonstantin Belousov c_func, c_arg, (intmax_t)ts2.tv_sec, ts2.tv_nsec); 7256098e7acSKonstantin Belousov } 72603763781SDavide Italiano maxdt = sbt2; 7276098e7acSKonstantin Belousov lastfunc = c_func; 7286098e7acSKonstantin Belousov } 7296098e7acSKonstantin Belousov #endif 7306098e7acSKonstantin Belousov CTR1(KTR_CALLOUT, "callout %p finished", c); 7316098e7acSKonstantin Belousov if ((c_flags & CALLOUT_RETURNUNLOCKED) == 0) 7326098e7acSKonstantin Belousov class->lc_unlock(c_lock); 7336098e7acSKonstantin Belousov skip: 7346098e7acSKonstantin Belousov CC_LOCK(cc); 7355b999a6bSDavide Italiano KASSERT(cc->cc_exec_entity[direct].cc_curr == c, ("mishandled cc_curr")); 7365b999a6bSDavide Italiano cc->cc_exec_entity[direct].cc_curr = NULL; 7375b999a6bSDavide Italiano if (cc->cc_exec_entity[direct].cc_waiting) { 7386098e7acSKonstantin Belousov /* 7396098e7acSKonstantin Belousov * There is someone waiting for the 7406098e7acSKonstantin Belousov * callout to complete. 7416098e7acSKonstantin Belousov * If the callout was scheduled for 7426098e7acSKonstantin Belousov * migration just cancel it. 7436098e7acSKonstantin Belousov */ 7445b999a6bSDavide Italiano if (cc_cce_migrating(cc, direct)) { 7455b999a6bSDavide Italiano cc_cce_cleanup(cc, direct); 746bdf9120cSAttilio Rao 747bdf9120cSAttilio Rao /* 748bdf9120cSAttilio Rao * It should be assert here that the callout is not 749bdf9120cSAttilio Rao * destroyed but that is not easy. 750bdf9120cSAttilio Rao */ 751eb8a7186SKonstantin Belousov c->c_flags &= ~CALLOUT_DFRMIGRATION; 752eb8a7186SKonstantin Belousov } 753ac42a172SDavide Italiano cc->cc_exec_entity[direct].cc_waiting = false; 7546098e7acSKonstantin Belousov CC_UNLOCK(cc); 7555b999a6bSDavide Italiano wakeup(&cc->cc_exec_entity[direct].cc_waiting); 7566098e7acSKonstantin Belousov CC_LOCK(cc); 7575b999a6bSDavide Italiano } else if (cc_cce_migrating(cc, direct)) { 758bdf9120cSAttilio Rao KASSERT((c_flags & CALLOUT_LOCAL_ALLOC) == 0, 759eb8a7186SKonstantin Belousov ("Migrating legacy callout %p", c)); 7606098e7acSKonstantin Belousov #ifdef SMP 7616098e7acSKonstantin Belousov /* 7626098e7acSKonstantin Belousov * If the callout was scheduled for 7636098e7acSKonstantin Belousov * migration just perform it now. 7646098e7acSKonstantin Belousov */ 7655b999a6bSDavide Italiano new_cpu = cc->cc_exec_entity[direct].ce_migration_cpu; 7665b999a6bSDavide Italiano new_time = cc->cc_exec_entity[direct].ce_migration_time; 7673f321a4eSDavide Italiano new_prec = cc->cc_exec_entity[direct].ce_migration_prec; 7685b999a6bSDavide Italiano new_func = cc->cc_exec_entity[direct].ce_migration_func; 7695b999a6bSDavide Italiano new_arg = cc->cc_exec_entity[direct].ce_migration_arg; 7705b999a6bSDavide Italiano cc_cce_cleanup(cc, direct); 7716098e7acSKonstantin Belousov 7726098e7acSKonstantin Belousov /* 773bdf9120cSAttilio Rao * It should be assert here that the callout is not destroyed 774bdf9120cSAttilio Rao * but that is not easy. 775bdf9120cSAttilio Rao * 776bdf9120cSAttilio Rao * As first thing, handle deferred callout stops. 7776098e7acSKonstantin Belousov */ 7786098e7acSKonstantin Belousov if ((c->c_flags & CALLOUT_DFRMIGRATION) == 0) { 7796098e7acSKonstantin Belousov CTR3(KTR_CALLOUT, 7806098e7acSKonstantin Belousov "deferred cancelled %p func %p arg %p", 7816098e7acSKonstantin Belousov c, new_func, new_arg); 7826098e7acSKonstantin Belousov callout_cc_del(c, cc); 783eb8a7186SKonstantin Belousov return; 7846098e7acSKonstantin Belousov } 7856098e7acSKonstantin Belousov c->c_flags &= ~CALLOUT_DFRMIGRATION; 7866098e7acSKonstantin Belousov 7876098e7acSKonstantin Belousov new_cc = callout_cpu_switch(c, cc, new_cpu); 7885b999a6bSDavide Italiano flags = (direct) ? C_DIRECT_EXEC : 0; 7893f321a4eSDavide Italiano callout_cc_add(c, new_cc, new_time, new_prec, new_func, 7905b999a6bSDavide Italiano new_arg, new_cpu, flags); 7916098e7acSKonstantin Belousov CC_UNLOCK(new_cc); 7926098e7acSKonstantin Belousov CC_LOCK(cc); 7936098e7acSKonstantin Belousov #else 7946098e7acSKonstantin Belousov panic("migration should not happen"); 7956098e7acSKonstantin Belousov #endif 7966098e7acSKonstantin Belousov } 797eb8a7186SKonstantin Belousov /* 798eb8a7186SKonstantin Belousov * If the current callout is locally allocated (from 799eb8a7186SKonstantin Belousov * timeout(9)) then put it on the freelist. 800eb8a7186SKonstantin Belousov * 801eb8a7186SKonstantin Belousov * Note: we need to check the cached copy of c_flags because 802eb8a7186SKonstantin Belousov * if it was not local, then it's not safe to deref the 803eb8a7186SKonstantin Belousov * callout pointer. 804eb8a7186SKonstantin Belousov */ 805eb8a7186SKonstantin Belousov KASSERT((c_flags & CALLOUT_LOCAL_ALLOC) == 0 || 806eb8a7186SKonstantin Belousov c->c_flags == CALLOUT_LOCAL_ALLOC, 807eb8a7186SKonstantin Belousov ("corrupted callout")); 808bdf9120cSAttilio Rao if (c_flags & CALLOUT_LOCAL_ALLOC) 809eb8a7186SKonstantin Belousov callout_cc_del(c, cc); 8106098e7acSKonstantin Belousov } 8116098e7acSKonstantin Belousov 812219d632cSMatthew Dillon /* 813ab36c067SJustin T. Gibbs * The callout mechanism is based on the work of Adam M. Costello and 814ab36c067SJustin T. Gibbs * George Varghese, published in a technical report entitled "Redesigning 815ab36c067SJustin T. Gibbs * the BSD Callout and Timer Facilities" and modified slightly for inclusion 816ab36c067SJustin T. Gibbs * in FreeBSD by Justin T. Gibbs. The original work on the data structures 817024035e8SHiten Pandya * used in this implementation was published by G. Varghese and T. Lauck in 818ab36c067SJustin T. Gibbs * the paper "Hashed and Hierarchical Timing Wheels: Data Structures for 819ab36c067SJustin T. Gibbs * the Efficient Implementation of a Timer Facility" in the Proceedings of 820ab36c067SJustin T. Gibbs * the 11th ACM Annual Symposium on Operating Systems Principles, 821ab36c067SJustin T. Gibbs * Austin, Texas Nov 1987. 822ab36c067SJustin T. Gibbs */ 823a50ec505SPoul-Henning Kamp 824ab36c067SJustin T. Gibbs /* 825df8bae1dSRodney W. Grimes * Software (low priority) clock interrupt. 826df8bae1dSRodney W. Grimes * Run periodic events from timeout queue. 827df8bae1dSRodney W. Grimes */ 828df8bae1dSRodney W. Grimes void 8298d809d50SJeff Roberson softclock(void *arg) 830df8bae1dSRodney W. Grimes { 8318d809d50SJeff Roberson struct callout_cpu *cc; 832b336df68SPoul-Henning Kamp struct callout *c; 8335b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING 8345b999a6bSDavide Italiano int depth = 0, gcalls = 0, lockcalls = 0, mpcalls = 0; 8355b999a6bSDavide Italiano #endif 836df8bae1dSRodney W. Grimes 8378d809d50SJeff Roberson cc = (struct callout_cpu *)arg; 8388d809d50SJeff Roberson CC_LOCK(cc); 8395b999a6bSDavide Italiano while ((c = TAILQ_FIRST(&cc->cc_expireq)) != NULL) { 8405b999a6bSDavide Italiano TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe); 8415b999a6bSDavide Italiano softclock_call_cc(c, cc, 8425b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING 8435b999a6bSDavide Italiano &mpcalls, &lockcalls, &gcalls, 8445b999a6bSDavide Italiano #endif 8455b999a6bSDavide Italiano 0); 8465b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING 8475b999a6bSDavide Italiano ++depth; 8485b999a6bSDavide Italiano #endif 849df8bae1dSRodney W. Grimes } 8505b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING 85122ee8c4fSPoul-Henning Kamp avg_depth += (depth * 1000 - avg_depth) >> 8; 85222ee8c4fSPoul-Henning Kamp avg_mpcalls += (mpcalls * 1000 - avg_mpcalls) >> 8; 85364b9ee20SAttilio Rao avg_lockcalls += (lockcalls * 1000 - avg_lockcalls) >> 8; 85422ee8c4fSPoul-Henning Kamp avg_gcalls += (gcalls * 1000 - avg_gcalls) >> 8; 8555b999a6bSDavide Italiano #endif 8568d809d50SJeff Roberson CC_UNLOCK(cc); 857df8bae1dSRodney W. Grimes } 858df8bae1dSRodney W. Grimes 859df8bae1dSRodney W. Grimes /* 860df8bae1dSRodney W. Grimes * timeout -- 861df8bae1dSRodney W. Grimes * Execute a function after a specified length of time. 862df8bae1dSRodney W. Grimes * 863df8bae1dSRodney W. Grimes * untimeout -- 864df8bae1dSRodney W. Grimes * Cancel previous timeout function call. 865df8bae1dSRodney W. Grimes * 866ab36c067SJustin T. Gibbs * callout_handle_init -- 867ab36c067SJustin T. Gibbs * Initialize a handle so that using it with untimeout is benign. 868ab36c067SJustin T. Gibbs * 869df8bae1dSRodney W. Grimes * See AT&T BCI Driver Reference Manual for specification. This 870ab36c067SJustin T. Gibbs * implementation differs from that one in that although an 871ab36c067SJustin T. Gibbs * identification value is returned from timeout, the original 872ab36c067SJustin T. Gibbs * arguments to timeout as well as the identifier are used to 873ab36c067SJustin T. Gibbs * identify entries for untimeout. 874df8bae1dSRodney W. Grimes */ 875ab36c067SJustin T. Gibbs struct callout_handle 876e392e44cSDavide Italiano timeout(timeout_t *ftn, void *arg, int to_ticks) 877df8bae1dSRodney W. Grimes { 8788d809d50SJeff Roberson struct callout_cpu *cc; 879ab36c067SJustin T. Gibbs struct callout *new; 880ab36c067SJustin T. Gibbs struct callout_handle handle; 881df8bae1dSRodney W. Grimes 8828d809d50SJeff Roberson cc = CC_CPU(timeout_cpu); 8838d809d50SJeff Roberson CC_LOCK(cc); 884df8bae1dSRodney W. Grimes /* Fill in the next free callout structure. */ 8858d809d50SJeff Roberson new = SLIST_FIRST(&cc->cc_callfree); 886ab36c067SJustin T. Gibbs if (new == NULL) 887ab36c067SJustin T. Gibbs /* XXX Attempt to malloc first */ 888df8bae1dSRodney W. Grimes panic("timeout table full"); 8898d809d50SJeff Roberson SLIST_REMOVE_HEAD(&cc->cc_callfree, c_links.sle); 890acc8326dSGarrett Wollman callout_reset(new, to_ticks, ftn, arg); 891ab36c067SJustin T. Gibbs handle.callout = new; 8928d809d50SJeff Roberson CC_UNLOCK(cc); 8938d809d50SJeff Roberson 894ab36c067SJustin T. Gibbs return (handle); 895df8bae1dSRodney W. Grimes } 896df8bae1dSRodney W. Grimes 897df8bae1dSRodney W. Grimes void 898e392e44cSDavide Italiano untimeout(timeout_t *ftn, void *arg, struct callout_handle handle) 899df8bae1dSRodney W. Grimes { 9008d809d50SJeff Roberson struct callout_cpu *cc; 901df8bae1dSRodney W. Grimes 902ab36c067SJustin T. Gibbs /* 903ab36c067SJustin T. Gibbs * Check for a handle that was initialized 904ab36c067SJustin T. Gibbs * by callout_handle_init, but never used 905ab36c067SJustin T. Gibbs * for a real timeout. 906ab36c067SJustin T. Gibbs */ 907ab36c067SJustin T. Gibbs if (handle.callout == NULL) 908ab36c067SJustin T. Gibbs return; 909df8bae1dSRodney W. Grimes 9108d809d50SJeff Roberson cc = callout_lock(handle.callout); 911acc8326dSGarrett Wollman if (handle.callout->c_func == ftn && handle.callout->c_arg == arg) 912acc8326dSGarrett Wollman callout_stop(handle.callout); 9138d809d50SJeff Roberson CC_UNLOCK(cc); 914df8bae1dSRodney W. Grimes } 915df8bae1dSRodney W. Grimes 9163c816944SBruce Evans void 917ab36c067SJustin T. Gibbs callout_handle_init(struct callout_handle *handle) 918ab36c067SJustin T. Gibbs { 919ab36c067SJustin T. Gibbs handle->callout = NULL; 920ab36c067SJustin T. Gibbs } 921ab36c067SJustin T. Gibbs 922acc8326dSGarrett Wollman /* 923acc8326dSGarrett Wollman * New interface; clients allocate their own callout structures. 924acc8326dSGarrett Wollman * 925acc8326dSGarrett Wollman * callout_reset() - establish or change a timeout 926acc8326dSGarrett Wollman * callout_stop() - disestablish a timeout 927acc8326dSGarrett Wollman * callout_init() - initialize a callout structure so that it can 928acc8326dSGarrett Wollman * safely be passed to callout_reset() and callout_stop() 929acc8326dSGarrett Wollman * 9309b8b58e0SJonathan Lemon * <sys/callout.h> defines three convenience macros: 931acc8326dSGarrett Wollman * 93286fd19deSColin Percival * callout_active() - returns truth if callout has not been stopped, 93386fd19deSColin Percival * drained, or deactivated since the last time the callout was 93486fd19deSColin Percival * reset. 9359b8b58e0SJonathan Lemon * callout_pending() - returns truth if callout is still waiting for timeout 9369b8b58e0SJonathan Lemon * callout_deactivate() - marks the callout as having been serviced 937acc8326dSGarrett Wollman */ 938d04304d1SGleb Smirnoff int 9395b999a6bSDavide Italiano callout_reset_sbt_on(struct callout *c, sbintime_t sbt, sbintime_t precision, 9405b999a6bSDavide Italiano void (*ftn)(void *), void *arg, int cpu, int flags) 941acc8326dSGarrett Wollman { 9425b999a6bSDavide Italiano sbintime_t to_sbt, pr; 9438d809d50SJeff Roberson struct callout_cpu *cc; 9445b999a6bSDavide Italiano int cancelled, direct; 945acc8326dSGarrett Wollman 9465b999a6bSDavide Italiano cancelled = 0; 9475b999a6bSDavide Italiano if (flags & C_ABSOLUTE) { 9485b999a6bSDavide Italiano to_sbt = sbt; 9495b999a6bSDavide Italiano } else { 9505b999a6bSDavide Italiano if ((flags & C_HARDCLOCK) && (sbt < tick_sbt)) 9515b999a6bSDavide Italiano sbt = tick_sbt; 9525b999a6bSDavide Italiano if ((flags & C_HARDCLOCK) || 9535b999a6bSDavide Italiano #ifdef NO_EVENTTIMERS 9545b999a6bSDavide Italiano sbt >= sbt_timethreshold) { 9555b999a6bSDavide Italiano to_sbt = getsbinuptime(); 9565b999a6bSDavide Italiano 9575b999a6bSDavide Italiano /* Add safety belt for the case of hz > 1000. */ 9585b999a6bSDavide Italiano to_sbt += tc_tick_sbt - tick_sbt; 9595b999a6bSDavide Italiano #else 9605b999a6bSDavide Italiano sbt >= sbt_tickthreshold) { 9615b999a6bSDavide Italiano /* 9625b999a6bSDavide Italiano * Obtain the time of the last hardclock() call on 9635b999a6bSDavide Italiano * this CPU directly from the kern_clocksource.c. 9645b999a6bSDavide Italiano * This value is per-CPU, but it is equal for all 9655b999a6bSDavide Italiano * active ones. 9665b999a6bSDavide Italiano */ 9675b999a6bSDavide Italiano #ifdef __LP64__ 9685b999a6bSDavide Italiano to_sbt = DPCPU_GET(hardclocktime); 9695b999a6bSDavide Italiano #else 9705b999a6bSDavide Italiano spinlock_enter(); 9715b999a6bSDavide Italiano to_sbt = DPCPU_GET(hardclocktime); 9725b999a6bSDavide Italiano spinlock_exit(); 9735b999a6bSDavide Italiano #endif 9745b999a6bSDavide Italiano #endif 9755b999a6bSDavide Italiano if ((flags & C_HARDCLOCK) == 0) 9765b999a6bSDavide Italiano to_sbt += tick_sbt; 9775b999a6bSDavide Italiano } else 9785b999a6bSDavide Italiano to_sbt = sbinuptime(); 9794bc38a5aSDavide Italiano if (SBT_MAX - to_sbt < sbt) 9804bc38a5aSDavide Italiano to_sbt = SBT_MAX; 9811b0c144fSDavide Italiano else 9825b999a6bSDavide Italiano to_sbt += sbt; 9835b999a6bSDavide Italiano pr = ((C_PRELGET(flags) < 0) ? sbt >> tc_precexp : 9845b999a6bSDavide Italiano sbt >> C_PRELGET(flags)); 9855b999a6bSDavide Italiano if (pr > precision) 9865b999a6bSDavide Italiano precision = pr; 9875b999a6bSDavide Italiano } 9888d809d50SJeff Roberson /* 9898d809d50SJeff Roberson * Don't allow migration of pre-allocated callouts lest they 9908d809d50SJeff Roberson * become unbalanced. 9918d809d50SJeff Roberson */ 9928d809d50SJeff Roberson if (c->c_flags & CALLOUT_LOCAL_ALLOC) 9938d809d50SJeff Roberson cpu = c->c_cpu; 9945b999a6bSDavide Italiano direct = (c->c_flags & CALLOUT_DIRECT) != 0; 9955b999a6bSDavide Italiano KASSERT(!direct || c->c_lock == NULL, 9965b999a6bSDavide Italiano ("%s: direct callout %p has lock", __func__, c)); 9978d809d50SJeff Roberson cc = callout_lock(c); 9985b999a6bSDavide Italiano if (cc->cc_exec_entity[direct].cc_curr == c) { 9992c1bb207SColin Percival /* 10002c1bb207SColin Percival * We're being asked to reschedule a callout which is 100164b9ee20SAttilio Rao * currently in progress. If there is a lock then we 100298c926b2SIan Dowse * can cancel the callout if it has not really started. 100398c926b2SIan Dowse */ 10045b999a6bSDavide Italiano if (c->c_lock != NULL && !cc->cc_exec_entity[direct].cc_cancel) 1005ac42a172SDavide Italiano cancelled = cc->cc_exec_entity[direct].cc_cancel = true; 10065b999a6bSDavide Italiano if (cc->cc_exec_entity[direct].cc_waiting) { 100798c926b2SIan Dowse /* 100898c926b2SIan Dowse * Someone has called callout_drain to kill this 100998c926b2SIan Dowse * callout. Don't reschedule. 10102c1bb207SColin Percival */ 101168a57ebfSGleb Smirnoff CTR4(KTR_CALLOUT, "%s %p func %p arg %p", 101268a57ebfSGleb Smirnoff cancelled ? "cancelled" : "failed to cancel", 101368a57ebfSGleb Smirnoff c, c->c_func, c->c_arg); 10148d809d50SJeff Roberson CC_UNLOCK(cc); 1015d04304d1SGleb Smirnoff return (cancelled); 101649a74476SColin Percival } 101798c926b2SIan Dowse } 10180413bacdSColin Percival if (c->c_flags & CALLOUT_PENDING) { 10195b999a6bSDavide Italiano if ((c->c_flags & CALLOUT_PROCESSED) == 0) { 10205b999a6bSDavide Italiano if (cc->cc_exec_next_dir == c) 10215b999a6bSDavide Italiano cc->cc_exec_next_dir = LIST_NEXT(c, c_links.le); 10225b999a6bSDavide Italiano LIST_REMOVE(c, c_links.le); 10235b999a6bSDavide Italiano } else 10245b999a6bSDavide Italiano TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe); 1025d04304d1SGleb Smirnoff cancelled = 1; 10268d809d50SJeff Roberson c->c_flags &= ~(CALLOUT_ACTIVE | CALLOUT_PENDING); 10278d809d50SJeff Roberson } 10281283e9cdSAttilio Rao 10291283e9cdSAttilio Rao #ifdef SMP 10300413bacdSColin Percival /* 10311283e9cdSAttilio Rao * If the callout must migrate try to perform it immediately. 10321283e9cdSAttilio Rao * If the callout is currently running, just defer the migration 10331283e9cdSAttilio Rao * to a more appropriate moment. 10340413bacdSColin Percival */ 10358d809d50SJeff Roberson if (c->c_cpu != cpu) { 10365b999a6bSDavide Italiano if (cc->cc_exec_entity[direct].cc_curr == c) { 10375b999a6bSDavide Italiano cc->cc_exec_entity[direct].ce_migration_cpu = cpu; 10385b999a6bSDavide Italiano cc->cc_exec_entity[direct].ce_migration_time 10395b999a6bSDavide Italiano = to_sbt; 10403f321a4eSDavide Italiano cc->cc_exec_entity[direct].ce_migration_prec 10413f321a4eSDavide Italiano = precision; 10425b999a6bSDavide Italiano cc->cc_exec_entity[direct].ce_migration_func = ftn; 10435b999a6bSDavide Italiano cc->cc_exec_entity[direct].ce_migration_arg = arg; 104457d07ca9SKonstantin Belousov c->c_flags |= CALLOUT_DFRMIGRATION; 10455b999a6bSDavide Italiano CTR6(KTR_CALLOUT, 10465b999a6bSDavide Italiano "migration of %p func %p arg %p in %d.%08x to %u deferred", 10475b999a6bSDavide Italiano c, c->c_func, c->c_arg, (int)(to_sbt >> 32), 10485b999a6bSDavide Italiano (u_int)(to_sbt & 0xffffffff), cpu); 104908e4ac8aSAttilio Rao CC_UNLOCK(cc); 10501283e9cdSAttilio Rao return (cancelled); 1051a157e425SAlexander Motin } 10521283e9cdSAttilio Rao cc = callout_cpu_switch(c, cc, cpu); 105308e4ac8aSAttilio Rao } 10541283e9cdSAttilio Rao #endif 10551283e9cdSAttilio Rao 10565b999a6bSDavide Italiano callout_cc_add(c, cc, to_sbt, precision, ftn, arg, cpu, flags); 10575b999a6bSDavide Italiano CTR6(KTR_CALLOUT, "%sscheduled %p func %p arg %p in %d.%08x", 10585b999a6bSDavide Italiano cancelled ? "re" : "", c, c->c_func, c->c_arg, (int)(to_sbt >> 32), 10595b999a6bSDavide Italiano (u_int)(to_sbt & 0xffffffff)); 10608d809d50SJeff Roberson CC_UNLOCK(cc); 1061d04304d1SGleb Smirnoff 1062d04304d1SGleb Smirnoff return (cancelled); 1063acc8326dSGarrett Wollman } 1064acc8326dSGarrett Wollman 10656e0186d5SSam Leffler /* 10666e0186d5SSam Leffler * Common idioms that can be optimized in the future. 10676e0186d5SSam Leffler */ 10686e0186d5SSam Leffler int 10696e0186d5SSam Leffler callout_schedule_on(struct callout *c, int to_ticks, int cpu) 10706e0186d5SSam Leffler { 10716e0186d5SSam Leffler return callout_reset_on(c, to_ticks, c->c_func, c->c_arg, cpu); 10726e0186d5SSam Leffler } 10736e0186d5SSam Leffler 10746e0186d5SSam Leffler int 10756e0186d5SSam Leffler callout_schedule(struct callout *c, int to_ticks) 10766e0186d5SSam Leffler { 10776e0186d5SSam Leffler return callout_reset_on(c, to_ticks, c->c_func, c->c_arg, c->c_cpu); 10786e0186d5SSam Leffler } 10796e0186d5SSam Leffler 10802c1bb207SColin Percival int 1081e392e44cSDavide Italiano _callout_stop_safe(struct callout *c, int safe) 10822c1bb207SColin Percival { 10831283e9cdSAttilio Rao struct callout_cpu *cc, *old_cc; 108464b9ee20SAttilio Rao struct lock_class *class; 10855b999a6bSDavide Italiano int direct, sq_locked, use_lock; 108698c926b2SIan Dowse 108764b9ee20SAttilio Rao /* 108864b9ee20SAttilio Rao * Some old subsystems don't hold Giant while running a callout_stop(), 108964b9ee20SAttilio Rao * so just discard this check for the moment. 109064b9ee20SAttilio Rao */ 109164b9ee20SAttilio Rao if (!safe && c->c_lock != NULL) { 109264b9ee20SAttilio Rao if (c->c_lock == &Giant.lock_object) 109364b9ee20SAttilio Rao use_lock = mtx_owned(&Giant); 109464b9ee20SAttilio Rao else { 109564b9ee20SAttilio Rao use_lock = 1; 109664b9ee20SAttilio Rao class = LOCK_CLASS(c->c_lock); 109764b9ee20SAttilio Rao class->lc_assert(c->c_lock, LA_XLOCKED); 109898c926b2SIan Dowse } 109964b9ee20SAttilio Rao } else 110064b9ee20SAttilio Rao use_lock = 0; 11015b999a6bSDavide Italiano direct = (c->c_flags & CALLOUT_DIRECT) != 0; 110267b158d8SJohn Baldwin sq_locked = 0; 11031283e9cdSAttilio Rao old_cc = NULL; 110467b158d8SJohn Baldwin again: 11058d809d50SJeff Roberson cc = callout_lock(c); 11061283e9cdSAttilio Rao 11071283e9cdSAttilio Rao /* 11081283e9cdSAttilio Rao * If the callout was migrating while the callout cpu lock was 11091283e9cdSAttilio Rao * dropped, just drop the sleepqueue lock and check the states 11101283e9cdSAttilio Rao * again. 11111283e9cdSAttilio Rao */ 11121283e9cdSAttilio Rao if (sq_locked != 0 && cc != old_cc) { 11131283e9cdSAttilio Rao #ifdef SMP 11141283e9cdSAttilio Rao CC_UNLOCK(cc); 11155b999a6bSDavide Italiano sleepq_release(&old_cc->cc_exec_entity[direct].cc_waiting); 11161283e9cdSAttilio Rao sq_locked = 0; 11171283e9cdSAttilio Rao old_cc = NULL; 11181283e9cdSAttilio Rao goto again; 11191283e9cdSAttilio Rao #else 11201283e9cdSAttilio Rao panic("migration should not happen"); 11211283e9cdSAttilio Rao #endif 11221283e9cdSAttilio Rao } 11231283e9cdSAttilio Rao 1124acc8326dSGarrett Wollman /* 1125b36f4588SJohn Baldwin * If the callout isn't pending, it's not on the queue, so 1126b36f4588SJohn Baldwin * don't attempt to remove it from the queue. We can try to 1127b36f4588SJohn Baldwin * stop it by other means however. 1128acc8326dSGarrett Wollman */ 1129acc8326dSGarrett Wollman if (!(c->c_flags & CALLOUT_PENDING)) { 11309b8b58e0SJonathan Lemon c->c_flags &= ~CALLOUT_ACTIVE; 1131b36f4588SJohn Baldwin 1132b36f4588SJohn Baldwin /* 1133b36f4588SJohn Baldwin * If it wasn't on the queue and it isn't the current 1134b36f4588SJohn Baldwin * callout, then we can't stop it, so just bail. 1135b36f4588SJohn Baldwin */ 11365b999a6bSDavide Italiano if (cc->cc_exec_entity[direct].cc_curr != c) { 113768a57ebfSGleb Smirnoff CTR3(KTR_CALLOUT, "failed to stop %p func %p arg %p", 113868a57ebfSGleb Smirnoff c, c->c_func, c->c_arg); 11398d809d50SJeff Roberson CC_UNLOCK(cc); 114067b158d8SJohn Baldwin if (sq_locked) 11415b999a6bSDavide Italiano sleepq_release( 11425b999a6bSDavide Italiano &cc->cc_exec_entity[direct].cc_waiting); 114398c926b2SIan Dowse return (0); 114498c926b2SIan Dowse } 1145b36f4588SJohn Baldwin 114698c926b2SIan Dowse if (safe) { 11472c1bb207SColin Percival /* 1148b36f4588SJohn Baldwin * The current callout is running (or just 1149b36f4588SJohn Baldwin * about to run) and blocking is allowed, so 1150b36f4588SJohn Baldwin * just wait for the current invocation to 1151b36f4588SJohn Baldwin * finish. 11522c1bb207SColin Percival */ 11535b999a6bSDavide Italiano while (cc->cc_exec_entity[direct].cc_curr == c) { 11546a0ce57dSAttilio Rao /* 11556a0ce57dSAttilio Rao * Use direct calls to sleepqueue interface 11566a0ce57dSAttilio Rao * instead of cv/msleep in order to avoid 11578d809d50SJeff Roberson * a LOR between cc_lock and sleepqueue 11586a0ce57dSAttilio Rao * chain spinlocks. This piece of code 11596a0ce57dSAttilio Rao * emulates a msleep_spin() call actually. 116067b158d8SJohn Baldwin * 116167b158d8SJohn Baldwin * If we already have the sleepqueue chain 116267b158d8SJohn Baldwin * locked, then we can safely block. If we 116367b158d8SJohn Baldwin * don't already have it locked, however, 11648d809d50SJeff Roberson * we have to drop the cc_lock to lock 116567b158d8SJohn Baldwin * it. This opens several races, so we 116667b158d8SJohn Baldwin * restart at the beginning once we have 116767b158d8SJohn Baldwin * both locks. If nothing has changed, then 116867b158d8SJohn Baldwin * we will end up back here with sq_locked 116967b158d8SJohn Baldwin * set. 11706a0ce57dSAttilio Rao */ 117167b158d8SJohn Baldwin if (!sq_locked) { 11728d809d50SJeff Roberson CC_UNLOCK(cc); 11735b999a6bSDavide Italiano sleepq_lock( 11745b999a6bSDavide Italiano &cc->cc_exec_entity[direct].cc_waiting); 117567b158d8SJohn Baldwin sq_locked = 1; 11761283e9cdSAttilio Rao old_cc = cc; 117767b158d8SJohn Baldwin goto again; 11786a0ce57dSAttilio Rao } 11791283e9cdSAttilio Rao 11801283e9cdSAttilio Rao /* 11811283e9cdSAttilio Rao * Migration could be cancelled here, but 11821283e9cdSAttilio Rao * as long as it is still not sure when it 11831283e9cdSAttilio Rao * will be packed up, just let softclock() 11841283e9cdSAttilio Rao * take care of it. 11851283e9cdSAttilio Rao */ 1186ac42a172SDavide Italiano cc->cc_exec_entity[direct].cc_waiting = true; 11876a0ce57dSAttilio Rao DROP_GIANT(); 11888d809d50SJeff Roberson CC_UNLOCK(cc); 11895b999a6bSDavide Italiano sleepq_add( 11905b999a6bSDavide Italiano &cc->cc_exec_entity[direct].cc_waiting, 11918d809d50SJeff Roberson &cc->cc_lock.lock_object, "codrain", 11926a0ce57dSAttilio Rao SLEEPQ_SLEEP, 0); 11935b999a6bSDavide Italiano sleepq_wait( 11945b999a6bSDavide Italiano &cc->cc_exec_entity[direct].cc_waiting, 11955b999a6bSDavide Italiano 0); 119667b158d8SJohn Baldwin sq_locked = 0; 11971283e9cdSAttilio Rao old_cc = NULL; 11986a0ce57dSAttilio Rao 11996a0ce57dSAttilio Rao /* Reacquire locks previously released. */ 12006a0ce57dSAttilio Rao PICKUP_GIANT(); 12018d809d50SJeff Roberson CC_LOCK(cc); 1202b36f4588SJohn Baldwin } 12035b999a6bSDavide Italiano } else if (use_lock && 12045b999a6bSDavide Italiano !cc->cc_exec_entity[direct].cc_cancel) { 1205b36f4588SJohn Baldwin /* 120664b9ee20SAttilio Rao * The current callout is waiting for its 120764b9ee20SAttilio Rao * lock which we hold. Cancel the callout 1208b36f4588SJohn Baldwin * and return. After our caller drops the 120964b9ee20SAttilio Rao * lock, the callout will be skipped in 1210b36f4588SJohn Baldwin * softclock(). 1211b36f4588SJohn Baldwin */ 1212ac42a172SDavide Italiano cc->cc_exec_entity[direct].cc_cancel = true; 121368a57ebfSGleb Smirnoff CTR3(KTR_CALLOUT, "cancelled %p func %p arg %p", 121468a57ebfSGleb Smirnoff c, c->c_func, c->c_arg); 12155b999a6bSDavide Italiano KASSERT(!cc_cce_migrating(cc, direct), 12161283e9cdSAttilio Rao ("callout wrongly scheduled for migration")); 12178d809d50SJeff Roberson CC_UNLOCK(cc); 121867b158d8SJohn Baldwin KASSERT(!sq_locked, ("sleepqueue chain locked")); 121998c926b2SIan Dowse return (1); 122057d07ca9SKonstantin Belousov } else if ((c->c_flags & CALLOUT_DFRMIGRATION) != 0) { 122157d07ca9SKonstantin Belousov c->c_flags &= ~CALLOUT_DFRMIGRATION; 122257d07ca9SKonstantin Belousov CTR3(KTR_CALLOUT, "postponing stop %p func %p arg %p", 122357d07ca9SKonstantin Belousov c, c->c_func, c->c_arg); 122457d07ca9SKonstantin Belousov CC_UNLOCK(cc); 122557d07ca9SKonstantin Belousov return (1); 1226b36f4588SJohn Baldwin } 122768a57ebfSGleb Smirnoff CTR3(KTR_CALLOUT, "failed to stop %p func %p arg %p", 122868a57ebfSGleb Smirnoff c, c->c_func, c->c_arg); 12298d809d50SJeff Roberson CC_UNLOCK(cc); 123067b158d8SJohn Baldwin KASSERT(!sq_locked, ("sleepqueue chain still locked")); 1231a45982d2SJohn Baldwin return (0); 1232acc8326dSGarrett Wollman } 123367b158d8SJohn Baldwin if (sq_locked) 12345b999a6bSDavide Italiano sleepq_release(&cc->cc_exec_entity[direct].cc_waiting); 123567b158d8SJohn Baldwin 12369b8b58e0SJonathan Lemon c->c_flags &= ~(CALLOUT_ACTIVE | CALLOUT_PENDING); 1237acc8326dSGarrett Wollman 123868a57ebfSGleb Smirnoff CTR3(KTR_CALLOUT, "cancelled %p func %p arg %p", 123968a57ebfSGleb Smirnoff c, c->c_func, c->c_arg); 12405b999a6bSDavide Italiano if ((c->c_flags & CALLOUT_PROCESSED) == 0) { 12415b999a6bSDavide Italiano if (cc->cc_exec_next_dir == c) 12425b999a6bSDavide Italiano cc->cc_exec_next_dir = LIST_NEXT(c, c_links.le); 12435b999a6bSDavide Italiano LIST_REMOVE(c, c_links.le); 12445b999a6bSDavide Italiano } else 12455b999a6bSDavide Italiano TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe); 12466098e7acSKonstantin Belousov callout_cc_del(c, cc); 124768a57ebfSGleb Smirnoff 12488d809d50SJeff Roberson CC_UNLOCK(cc); 1249a45982d2SJohn Baldwin return (1); 1250acc8326dSGarrett Wollman } 1251acc8326dSGarrett Wollman 1252acc8326dSGarrett Wollman void 1253e392e44cSDavide Italiano callout_init(struct callout *c, int mpsafe) 1254acc8326dSGarrett Wollman { 12557347e1c6SGarrett Wollman bzero(c, sizeof *c); 125698c926b2SIan Dowse if (mpsafe) { 125764b9ee20SAttilio Rao c->c_lock = NULL; 125898c926b2SIan Dowse c->c_flags = CALLOUT_RETURNUNLOCKED; 125998c926b2SIan Dowse } else { 126064b9ee20SAttilio Rao c->c_lock = &Giant.lock_object; 126198c926b2SIan Dowse c->c_flags = 0; 126298c926b2SIan Dowse } 12638d809d50SJeff Roberson c->c_cpu = timeout_cpu; 126498c926b2SIan Dowse } 126598c926b2SIan Dowse 126698c926b2SIan Dowse void 1267e392e44cSDavide Italiano _callout_init_lock(struct callout *c, struct lock_object *lock, int flags) 126898c926b2SIan Dowse { 126998c926b2SIan Dowse bzero(c, sizeof *c); 127064b9ee20SAttilio Rao c->c_lock = lock; 127164b9ee20SAttilio Rao KASSERT((flags & ~(CALLOUT_RETURNUNLOCKED | CALLOUT_SHAREDLOCK)) == 0, 127264b9ee20SAttilio Rao ("callout_init_lock: bad flags %d", flags)); 127364b9ee20SAttilio Rao KASSERT(lock != NULL || (flags & CALLOUT_RETURNUNLOCKED) == 0, 127464b9ee20SAttilio Rao ("callout_init_lock: CALLOUT_RETURNUNLOCKED with no lock")); 127513ddf72dSAttilio Rao KASSERT(lock == NULL || !(LOCK_CLASS(lock)->lc_flags & 127613ddf72dSAttilio Rao (LC_SPINLOCK | LC_SLEEPABLE)), ("%s: invalid lock class", 127764b9ee20SAttilio Rao __func__)); 127864b9ee20SAttilio Rao c->c_flags = flags & (CALLOUT_RETURNUNLOCKED | CALLOUT_SHAREDLOCK); 12798d809d50SJeff Roberson c->c_cpu = timeout_cpu; 1280acc8326dSGarrett Wollman } 1281acc8326dSGarrett Wollman 1282e1d6dc65SNate Williams #ifdef APM_FIXUP_CALLTODO 1283e1d6dc65SNate Williams /* 1284e1d6dc65SNate Williams * Adjust the kernel calltodo timeout list. This routine is used after 1285e1d6dc65SNate Williams * an APM resume to recalculate the calltodo timer list values with the 1286e1d6dc65SNate Williams * number of hz's we have been sleeping. The next hardclock() will detect 1287e1d6dc65SNate Williams * that there are fired timers and run softclock() to execute them. 1288e1d6dc65SNate Williams * 1289e1d6dc65SNate Williams * Please note, I have not done an exhaustive analysis of what code this 1290e1d6dc65SNate Williams * might break. I am motivated to have my select()'s and alarm()'s that 1291e1d6dc65SNate Williams * have expired during suspend firing upon resume so that the applications 1292e1d6dc65SNate Williams * which set the timer can do the maintanence the timer was for as close 1293e1d6dc65SNate Williams * as possible to the originally intended time. Testing this code for a 1294e1d6dc65SNate Williams * week showed that resuming from a suspend resulted in 22 to 25 timers 1295e1d6dc65SNate Williams * firing, which seemed independant on whether the suspend was 2 hours or 1296e1d6dc65SNate Williams * 2 days. Your milage may vary. - Ken Key <key@cs.utk.edu> 1297e1d6dc65SNate Williams */ 1298e1d6dc65SNate Williams void 1299e392e44cSDavide Italiano adjust_timeout_calltodo(struct timeval *time_change) 1300e1d6dc65SNate Williams { 1301e1d6dc65SNate Williams register struct callout *p; 1302e1d6dc65SNate Williams unsigned long delta_ticks; 1303e1d6dc65SNate Williams 1304e1d6dc65SNate Williams /* 1305e1d6dc65SNate Williams * How many ticks were we asleep? 1306c8b47828SBruce Evans * (stolen from tvtohz()). 1307e1d6dc65SNate Williams */ 1308e1d6dc65SNate Williams 1309e1d6dc65SNate Williams /* Don't do anything */ 1310e1d6dc65SNate Williams if (time_change->tv_sec < 0) 1311e1d6dc65SNate Williams return; 1312e1d6dc65SNate Williams else if (time_change->tv_sec <= LONG_MAX / 1000000) 1313e1d6dc65SNate Williams delta_ticks = (time_change->tv_sec * 1000000 + 1314e1d6dc65SNate Williams time_change->tv_usec + (tick - 1)) / tick + 1; 1315e1d6dc65SNate Williams else if (time_change->tv_sec <= LONG_MAX / hz) 1316e1d6dc65SNate Williams delta_ticks = time_change->tv_sec * hz + 1317e1d6dc65SNate Williams (time_change->tv_usec + (tick - 1)) / tick + 1; 1318e1d6dc65SNate Williams else 1319e1d6dc65SNate Williams delta_ticks = LONG_MAX; 1320e1d6dc65SNate Williams 1321e1d6dc65SNate Williams if (delta_ticks > INT_MAX) 1322e1d6dc65SNate Williams delta_ticks = INT_MAX; 1323e1d6dc65SNate Williams 1324e1d6dc65SNate Williams /* 1325e1d6dc65SNate Williams * Now rip through the timer calltodo list looking for timers 1326e1d6dc65SNate Williams * to expire. 1327e1d6dc65SNate Williams */ 1328e1d6dc65SNate Williams 1329e1d6dc65SNate Williams /* don't collide with softclock() */ 13308d809d50SJeff Roberson CC_LOCK(cc); 1331e1d6dc65SNate Williams for (p = calltodo.c_next; p != NULL; p = p->c_next) { 1332e1d6dc65SNate Williams p->c_time -= delta_ticks; 1333e1d6dc65SNate Williams 1334e1d6dc65SNate Williams /* Break if the timer had more time on it than delta_ticks */ 1335e1d6dc65SNate Williams if (p->c_time > 0) 1336e1d6dc65SNate Williams break; 1337e1d6dc65SNate Williams 1338e1d6dc65SNate Williams /* take back the ticks the timer didn't use (p->c_time <= 0) */ 1339e1d6dc65SNate Williams delta_ticks = -p->c_time; 1340e1d6dc65SNate Williams } 13418d809d50SJeff Roberson CC_UNLOCK(cc); 1342e1d6dc65SNate Williams 1343e1d6dc65SNate Williams return; 1344e1d6dc65SNate Williams } 1345e1d6dc65SNate Williams #endif /* APM_FIXUP_CALLTODO */ 13465b999a6bSDavide Italiano 13475b999a6bSDavide Italiano static int 13485b999a6bSDavide Italiano flssbt(sbintime_t sbt) 13495b999a6bSDavide Italiano { 13505b999a6bSDavide Italiano 13515b999a6bSDavide Italiano sbt += (uint64_t)sbt >> 1; 13525b999a6bSDavide Italiano if (sizeof(long) >= sizeof(sbintime_t)) 13535b999a6bSDavide Italiano return (flsl(sbt)); 13545b999a6bSDavide Italiano if (sbt >= SBT_1S) 13555b999a6bSDavide Italiano return (flsl(((uint64_t)sbt) >> 32) + 32); 13565b999a6bSDavide Italiano return (flsl(sbt)); 13575b999a6bSDavide Italiano } 13585b999a6bSDavide Italiano 13595b999a6bSDavide Italiano /* 13605b999a6bSDavide Italiano * Dump immediate statistic snapshot of the scheduled callouts. 13615b999a6bSDavide Italiano */ 13625b999a6bSDavide Italiano static int 13635b999a6bSDavide Italiano sysctl_kern_callout_stat(SYSCTL_HANDLER_ARGS) 13645b999a6bSDavide Italiano { 13655b999a6bSDavide Italiano struct callout *tmp; 13665b999a6bSDavide Italiano struct callout_cpu *cc; 13675b999a6bSDavide Italiano struct callout_list *sc; 13685b999a6bSDavide Italiano sbintime_t maxpr, maxt, medpr, medt, now, spr, st, t; 13695b999a6bSDavide Italiano int ct[64], cpr[64], ccpbk[32]; 13705b999a6bSDavide Italiano int error, val, i, count, tcum, pcum, maxc, c, medc; 13715b999a6bSDavide Italiano #ifdef SMP 13725b999a6bSDavide Italiano int cpu; 13735b999a6bSDavide Italiano #endif 13745b999a6bSDavide Italiano 13755b999a6bSDavide Italiano val = 0; 13765b999a6bSDavide Italiano error = sysctl_handle_int(oidp, &val, 0, req); 13775b999a6bSDavide Italiano if (error != 0 || req->newptr == NULL) 13785b999a6bSDavide Italiano return (error); 13795b999a6bSDavide Italiano count = maxc = 0; 13805b999a6bSDavide Italiano st = spr = maxt = maxpr = 0; 13815b999a6bSDavide Italiano bzero(ccpbk, sizeof(ccpbk)); 13825b999a6bSDavide Italiano bzero(ct, sizeof(ct)); 13835b999a6bSDavide Italiano bzero(cpr, sizeof(cpr)); 13845b999a6bSDavide Italiano now = sbinuptime(); 13855b999a6bSDavide Italiano #ifdef SMP 13865b999a6bSDavide Italiano CPU_FOREACH(cpu) { 13875b999a6bSDavide Italiano cc = CC_CPU(cpu); 13885b999a6bSDavide Italiano #else 13895b999a6bSDavide Italiano cc = CC_CPU(timeout_cpu); 13905b999a6bSDavide Italiano #endif 13915b999a6bSDavide Italiano CC_LOCK(cc); 13925b999a6bSDavide Italiano for (i = 0; i < callwheelsize; i++) { 13935b999a6bSDavide Italiano sc = &cc->cc_callwheel[i]; 13945b999a6bSDavide Italiano c = 0; 13955b999a6bSDavide Italiano LIST_FOREACH(tmp, sc, c_links.le) { 13965b999a6bSDavide Italiano c++; 13975b999a6bSDavide Italiano t = tmp->c_time - now; 13985b999a6bSDavide Italiano if (t < 0) 13995b999a6bSDavide Italiano t = 0; 14005b999a6bSDavide Italiano st += t / SBT_1US; 14015b999a6bSDavide Italiano spr += tmp->c_precision / SBT_1US; 14025b999a6bSDavide Italiano if (t > maxt) 14035b999a6bSDavide Italiano maxt = t; 14045b999a6bSDavide Italiano if (tmp->c_precision > maxpr) 14055b999a6bSDavide Italiano maxpr = tmp->c_precision; 14065b999a6bSDavide Italiano ct[flssbt(t)]++; 14075b999a6bSDavide Italiano cpr[flssbt(tmp->c_precision)]++; 14085b999a6bSDavide Italiano } 14095b999a6bSDavide Italiano if (c > maxc) 14105b999a6bSDavide Italiano maxc = c; 14115b999a6bSDavide Italiano ccpbk[fls(c + c / 2)]++; 14125b999a6bSDavide Italiano count += c; 14135b999a6bSDavide Italiano } 14145b999a6bSDavide Italiano CC_UNLOCK(cc); 14155b999a6bSDavide Italiano #ifdef SMP 14165b999a6bSDavide Italiano } 14175b999a6bSDavide Italiano #endif 14185b999a6bSDavide Italiano 14195b999a6bSDavide Italiano for (i = 0, tcum = 0; i < 64 && tcum < count / 2; i++) 14205b999a6bSDavide Italiano tcum += ct[i]; 14215b999a6bSDavide Italiano medt = (i >= 2) ? (((sbintime_t)1) << (i - 2)) : 0; 14225b999a6bSDavide Italiano for (i = 0, pcum = 0; i < 64 && pcum < count / 2; i++) 14235b999a6bSDavide Italiano pcum += cpr[i]; 14245b999a6bSDavide Italiano medpr = (i >= 2) ? (((sbintime_t)1) << (i - 2)) : 0; 14255b999a6bSDavide Italiano for (i = 0, c = 0; i < 32 && c < count / 2; i++) 14265b999a6bSDavide Italiano c += ccpbk[i]; 14275b999a6bSDavide Italiano medc = (i >= 2) ? (1 << (i - 2)) : 0; 14285b999a6bSDavide Italiano 14295b999a6bSDavide Italiano printf("Scheduled callouts statistic snapshot:\n"); 14305b999a6bSDavide Italiano printf(" Callouts: %6d Buckets: %6d*%-3d Bucket size: 0.%06ds\n", 14315b999a6bSDavide Italiano count, callwheelsize, mp_ncpus, 1000000 >> CC_HASH_SHIFT); 14325b999a6bSDavide Italiano printf(" C/Bk: med %5d avg %6d.%06jd max %6d\n", 14335b999a6bSDavide Italiano medc, 14345b999a6bSDavide Italiano count / callwheelsize / mp_ncpus, 14355b999a6bSDavide Italiano (uint64_t)count * 1000000 / callwheelsize / mp_ncpus % 1000000, 14365b999a6bSDavide Italiano maxc); 14375b999a6bSDavide Italiano printf(" Time: med %5jd.%06jds avg %6jd.%06jds max %6jd.%06jds\n", 14385b999a6bSDavide Italiano medt / SBT_1S, (medt & 0xffffffff) * 1000000 >> 32, 14395b999a6bSDavide Italiano (st / count) / 1000000, (st / count) % 1000000, 14405b999a6bSDavide Italiano maxt / SBT_1S, (maxt & 0xffffffff) * 1000000 >> 32); 14415b999a6bSDavide Italiano printf(" Prec: med %5jd.%06jds avg %6jd.%06jds max %6jd.%06jds\n", 14425b999a6bSDavide Italiano medpr / SBT_1S, (medpr & 0xffffffff) * 1000000 >> 32, 14435b999a6bSDavide Italiano (spr / count) / 1000000, (spr / count) % 1000000, 14445b999a6bSDavide Italiano maxpr / SBT_1S, (maxpr & 0xffffffff) * 1000000 >> 32); 14455b999a6bSDavide Italiano printf(" Distribution: \tbuckets\t time\t tcum\t" 14465b999a6bSDavide Italiano " prec\t pcum\n"); 14475b999a6bSDavide Italiano for (i = 0, tcum = pcum = 0; i < 64; i++) { 14485b999a6bSDavide Italiano if (ct[i] == 0 && cpr[i] == 0) 14495b999a6bSDavide Italiano continue; 14505b999a6bSDavide Italiano t = (i != 0) ? (((sbintime_t)1) << (i - 1)) : 0; 14515b999a6bSDavide Italiano tcum += ct[i]; 14525b999a6bSDavide Italiano pcum += cpr[i]; 14535b999a6bSDavide Italiano printf(" %10jd.%06jds\t 2**%d\t%7d\t%7d\t%7d\t%7d\n", 14545b999a6bSDavide Italiano t / SBT_1S, (t & 0xffffffff) * 1000000 >> 32, 14555b999a6bSDavide Italiano i - 1 - (32 - CC_HASH_SHIFT), 14565b999a6bSDavide Italiano ct[i], tcum, cpr[i], pcum); 14575b999a6bSDavide Italiano } 14585b999a6bSDavide Italiano return (error); 14595b999a6bSDavide Italiano } 14605b999a6bSDavide Italiano SYSCTL_PROC(_kern, OID_AUTO, callout_stat, 14615b999a6bSDavide Italiano CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 14625b999a6bSDavide Italiano 0, 0, sysctl_kern_callout_stat, "I", 14635b999a6bSDavide Italiano "Dump immediate statistic snapshot of the scheduled callouts"); 1464