1*b0486b75Scheloha.\" $OpenBSD: clockintr_bind.9,v 1.1 2024/02/24 16:21:32 cheloha Exp $ 2*b0486b75Scheloha.\" 3*b0486b75Scheloha.\" Copyright (c) 2023-2024 Scott Cheloha <cheloha@openbsd.org> 4*b0486b75Scheloha.\" 5*b0486b75Scheloha.\" Permission to use, copy, modify, and distribute this software for any 6*b0486b75Scheloha.\" purpose with or without fee is hereby granted, provided that the above 7*b0486b75Scheloha.\" copyright notice and this permission notice appear in all copies. 8*b0486b75Scheloha.\" 9*b0486b75Scheloha.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10*b0486b75Scheloha.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11*b0486b75Scheloha.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12*b0486b75Scheloha.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13*b0486b75Scheloha.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14*b0486b75Scheloha.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15*b0486b75Scheloha.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16*b0486b75Scheloha.\" 17*b0486b75Scheloha.Dd $Mdocdate: February 24 2024 $ 18*b0486b75Scheloha.Dt CLOCKINTR_BIND 9 19*b0486b75Scheloha.Os 20*b0486b75Scheloha.Sh NAME 21*b0486b75Scheloha.Nm clockintr_bind , 22*b0486b75Scheloha.Nm clockintr_schedule , 23*b0486b75Scheloha.Nm clockintr_advance , 24*b0486b75Scheloha.Nm clockintr_cancel , 25*b0486b75Scheloha.Nm clockintr_unbind , 26*b0486b75Scheloha.Nm clockintr_stagger , 27*b0486b75Scheloha.Nm clockrequest_advance 28*b0486b75Scheloha.Nd execute a function in a clock interrupt context 29*b0486b75Scheloha.Sh SYNOPSIS 30*b0486b75Scheloha.In sys/clockintr.h 31*b0486b75Scheloha.Ft void 32*b0486b75Scheloha.Fo clockintr_bind 33*b0486b75Scheloha.Fa "struct clockintr *cl" 34*b0486b75Scheloha.Fa "struct cpu_info *cpu" 35*b0486b75Scheloha.Fa "void (*callback)(struct clockrequest *cr, void *cf, void *arg)" 36*b0486b75Scheloha.Fa "void *arg" 37*b0486b75Scheloha.Fc 38*b0486b75Scheloha.Ft void 39*b0486b75Scheloha.Fo clockintr_schedule 40*b0486b75Scheloha.Fa "struct clockintr *cl" 41*b0486b75Scheloha.Fa "uint64_t abs" 42*b0486b75Scheloha.Fc 43*b0486b75Scheloha.Ft uint64_t 44*b0486b75Scheloha.Fo clockintr_advance 45*b0486b75Scheloha.Fa "struct clockintr *cl" 46*b0486b75Scheloha.Fa "uint64_t interval" 47*b0486b75Scheloha.Fc 48*b0486b75Scheloha.Ft void 49*b0486b75Scheloha.Fo clockintr_cancel 50*b0486b75Scheloha.Fa "struct clockintr *cl" 51*b0486b75Scheloha.Fc 52*b0486b75Scheloha.Ft void 53*b0486b75Scheloha.Fo clockintr_unbind 54*b0486b75Scheloha.Fa "struct clockintr *cl" 55*b0486b75Scheloha.Fa "uint32_t flags" 56*b0486b75Scheloha.Fc 57*b0486b75Scheloha.Ft void 58*b0486b75Scheloha.Fo clockintr_stagger 59*b0486b75Scheloha.Fa "struct clockintr *cl" 60*b0486b75Scheloha.Fa "uint64_t interval" 61*b0486b75Scheloha.Fa "uint32_t numer" 62*b0486b75Scheloha.Fa "uint32_t denom" 63*b0486b75Scheloha.Fc 64*b0486b75Scheloha.Ft uint64_t 65*b0486b75Scheloha.Fo clockrequest_advance 66*b0486b75Scheloha.Fa "struct clockrequest *cr" 67*b0486b75Scheloha.Fa "uint64_t interval" 68*b0486b75Scheloha.Fc 69*b0486b75Scheloha.\" .Fn clockrequest_advance_random is intentionally undocumented. 70*b0486b75Scheloha.\" It may be removed in the future. New code should not use it. 71*b0486b75Scheloha.Sh DESCRIPTION 72*b0486b75SchelohaThe clock interrupt subsystem schedules functions for asynchronous execution 73*b0486b75Schelohafrom the clock interrupt context on a particular CPU. 74*b0486b75Scheloha.Pp 75*b0486b75SchelohaClock interrupts are well-suited for timekeeping, 76*b0486b75Schelohascheduling, 77*b0486b75Schelohaand statistical profiling. 78*b0486b75SchelohaApplications with more relaxed latency requirements should use timeouts 79*b0486b75Schelohato schedule asynchronous execution; 80*b0486b75Schelohasee 81*b0486b75Scheloha.Xr timeout_add 9 82*b0486b75Schelohafor details. 83*b0486b75Scheloha.Pp 84*b0486b75SchelohaThe 85*b0486b75Scheloha.Fn clockintr_bind 86*b0486b75Schelohafunction initializes the clock interrupt object 87*b0486b75Scheloha.Fa cl . 88*b0486b75SchelohaWhen 89*b0486b75Scheloha.Fa cl 90*b0486b75Schelohaexpires, 91*b0486b75Schelohaits 92*b0486b75Scheloha.Fa callback 93*b0486b75Schelohafunction is executed from the 94*b0486b75Scheloha.Dv IPL_CLOCK 95*b0486b75Schelohacontext on its host 96*b0486b75Scheloha.Fa cpu 97*b0486b75Schelohawithout any locks or mutexes. 98*b0486b75SchelohaThe callback function must not block. 99*b0486b75SchelohaIts parameters are as follows: 100*b0486b75Scheloha.Bl -tag -width indent 101*b0486b75Scheloha.It Fa cr 102*b0486b75SchelohaA private 103*b0486b75Scheloha.Vt clockrequest 104*b0486b75Schelohaobject. 105*b0486b75SchelohaMay be used to request rescheduling; 106*b0486b75Schelohasee 107*b0486b75Scheloha.Fn clockrequest_advance 108*b0486b75Schelohabelow. 109*b0486b75Scheloha.It Fa cf 110*b0486b75SchelohaThe 111*b0486b75Scheloha.Fa cpu Ns 's 112*b0486b75Schelohacurrent machine-dependent clockframe. 113*b0486b75Scheloha.It Fa arg 114*b0486b75SchelohaThe 115*b0486b75Scheloha.Fa arg 116*b0486b75Schelohagiven to 117*b0486b75Scheloha.Fn clockintr_bind . 118*b0486b75Scheloha.El 119*b0486b75Scheloha.Pp 120*b0486b75SchelohaThe memory pointed to by 121*b0486b75Scheloha.Fa cl 122*b0486b75Schelohamust be zeroed before it is first bound. 123*b0486b75SchelohaIt is an error to use 124*b0486b75Scheloha.Fa cl 125*b0486b75Schelohaas argument to any other function in the 126*b0486b75Scheloha.Vt clockintr 127*b0486b75SchelohaAPI before it is bound. 128*b0486b75SchelohaIt is an error to rebind 129*b0486b75Scheloha.Fa cl 130*b0486b75Schelohawithout first unbinding it; 131*b0486b75Schelohasee 132*b0486b75Scheloha.Fn clockintr_unbind 133*b0486b75Schelohabelow. 134*b0486b75Scheloha.Pp 135*b0486b75SchelohaThe 136*b0486b75Scheloha.Fn clockintr_schedule 137*b0486b75Schelohafunction schedules 138*b0486b75Scheloha.Fa cl 139*b0486b75Schelohato expire at the absolute time 140*b0486b75Scheloha.Fa abs 141*b0486b75Schelohaon the system uptime clock. 142*b0486b75SchelohaThe subsystem will never execute 143*b0486b75Scheloha.Fa cl Ns 's 144*b0486b75Schelohacallback function before this expiration time, 145*b0486b75Schelohathough its execution may be delayed by other activity on the system. 146*b0486b75Scheloha.Pp 147*b0486b75SchelohaThe 148*b0486b75Scheloha.Fn clockintr_advance 149*b0486b75Schelohafunction schedules 150*b0486b75Scheloha.Fa cl 151*b0486b75Schelohato expire at the next terminus of the given 152*b0486b75Scheloha.Fa interval , 153*b0486b75Schelohaa non-zero count of nanoseconds, 154*b0486b75Scheloharelative to 155*b0486b75Scheloha.Fa cl Ns 's 156*b0486b75Schelohacurrent expiration time. 157*b0486b75SchelohaPeriodic clock interrupts should be scheduled with 158*b0486b75Scheloha.Fn clockintr_advance 159*b0486b75Schelohato keep the execution period from drifting. 160*b0486b75Scheloha.Pp 161*b0486b75SchelohaThe 162*b0486b75Scheloha.Fn clockintr_cancel 163*b0486b75Schelohafunction cancels any pending expiration of 164*b0486b75Scheloha.Fa cl . 165*b0486b75Scheloha.Pp 166*b0486b75SchelohaThe 167*b0486b75Scheloha.Fn clockintr_unbind 168*b0486b75Schelohafunction cancels any pending expiration of 169*b0486b75Scheloha.Fa cl 170*b0486b75Schelohaand severs the binding between 171*b0486b75Scheloha.Fa cl 172*b0486b75Schelohaand its host 173*b0486b75Scheloha.Fa cpu . 174*b0486b75SchelohaUpon return, 175*b0486b75Scheloha.Fa cl 176*b0486b75Schelohamay be rebound with 177*b0486b75Scheloha.Fn clockintr_bind . 178*b0486b75SchelohaThe call may be configured with zero or more of the following 179*b0486b75Scheloha.Fa flags : 180*b0486b75Scheloha.Bl -tag -width CL_BARRIER 181*b0486b75Scheloha.It Dv CL_BARRIER 182*b0486b75SchelohaIf 183*b0486b75Scheloha.Fa cl Ns 's 184*b0486b75Schelohacallback function is executing, 185*b0486b75Schelohablock until it returns. 186*b0486b75SchelohaBy default, 187*b0486b75Schelohathe caller does not block. 188*b0486b75SchelohaUseful when 189*b0486b75Scheloha.Fa arg 190*b0486b75Schelohais a shared reference. 191*b0486b75Scheloha.El 192*b0486b75Scheloha.Pp 193*b0486b75SchelohaThe 194*b0486b75Scheloha.Fn clockintr_stagger 195*b0486b75Schelohafunction resets 196*b0486b75Scheloha.Fa cl Ns 's 197*b0486b75Schelohaexpiration time to a fraction of the given 198*b0486b75Scheloha.Fa interval , 199*b0486b75Schelohaa count of nanoseconds. 200*b0486b75SchelohaSpecifically, 201*b0486b75Scheloha.Fa cl Ns 's 202*b0486b75Schelohaexpiration time is reset to 203*b0486b75Scheloha.Pq Fa interval Ms / Fa denom Ms * Fa numer . 204*b0486b75SchelohaPeriodic clock interrupts bound to multiple CPUs may be staggered 205*b0486b75Schelohato reduce the likelihood that their callback functions will execute 206*b0486b75Schelohasimultaneously and compete for a shared resource. 207*b0486b75SchelohaIt is an error if 208*b0486b75Scheloha.Fa numer 209*b0486b75Schelohais greater than or equal to 210*b0486b75Scheloha.Fa denom . 211*b0486b75SchelohaIt is an error if 212*b0486b75Scheloha.Fa cl 213*b0486b75Schelohais already scheduled to expire. 214*b0486b75Scheloha.Pp 215*b0486b75SchelohaThe 216*b0486b75Scheloha.Fn clockrequest_advance 217*b0486b75Schelohafunction is similar to 218*b0486b75Scheloha.Fn clockintr_advance , 219*b0486b75Schelohaexcept that 220*b0486b75Scheloha(a) it may only be called during the execution of a 221*b0486b75Scheloha.Fa callback 222*b0486b75Schelohafunction, 223*b0486b75Scheloha(b) it accepts a 224*b0486b75Scheloha.Vt clockrequest 225*b0486b75Schelohapointer as argument, 226*b0486b75Schelohaand (c) scheduling requests submitted with the interface are not fulfilled 227*b0486b75Schelohauntil the callback function returns. 228*b0486b75SchelohaWhen the callback function returns, 229*b0486b75Schelohascheduling requests are only committed to the underlying clock interrupt 230*b0486b75Schelohaobject if that object was not manipulated during the execution of the 231*b0486b75Schelohacallback function. 232*b0486b75SchelohaOtherwise, 233*b0486b75Schelohaoutstanding requests are discarded. 234*b0486b75Scheloha.Sh CONTEXT 235*b0486b75SchelohaThe 236*b0486b75Scheloha.Fn clockintr_bind 237*b0486b75Schelohafunction may only be called from process context. 238*b0486b75Scheloha.Pp 239*b0486b75SchelohaThe 240*b0486b75Scheloha.Fn clockintr_advance , 241*b0486b75Scheloha.Fn clockintr_cancel , 242*b0486b75Scheloha.Fn clockintr_schedule , 243*b0486b75Schelohaand 244*b0486b75Scheloha.Fn clockintr_stagger 245*b0486b75Schelohafunctions may be called from process context or from interrupt context. 246*b0486b75Scheloha.Pp 247*b0486b75SchelohaThe 248*b0486b75Scheloha.Fn clockintr_unbind 249*b0486b75Schelohafunction may normally be called from process context or from interrupt context. 250*b0486b75SchelohaHowever, 251*b0486b75Schelohaif the 252*b0486b75Scheloha.Dv CL_BARRIER 253*b0486b75Schelohaflag is set in 254*b0486b75Scheloha.Fa flags , 255*b0486b75Schelohathe function may only be called from process context. 256*b0486b75Scheloha.Pp 257*b0486b75SchelohaThe 258*b0486b75Scheloha.Fn clockrequest_advance 259*b0486b75Schelohafunction may only be called during execution of a 260*b0486b75Scheloha.Fa callback 261*b0486b75Schelohafunction. 262*b0486b75Scheloha.Sh RETURN VALUES 263*b0486b75SchelohaThe 264*b0486b75Scheloha.Fn clockintr_advance 265*b0486b75Schelohaand 266*b0486b75Scheloha.Fn clockrequest_advance 267*b0486b75Schelohafunctions return the number of 268*b0486b75Scheloha.Fa interval Ns s 269*b0486b75Schelohathat have elapsed since 270*b0486b75Scheloha.Fa cl 271*b0486b75Schelohawas scheduled to expire, 272*b0486b75Schelohaor zero if 273*b0486b75Scheloha.Fa cl 274*b0486b75Schelohahas not yet expired. 275*b0486b75Scheloha.Sh CODE REFERENCES 276*b0486b75Scheloha.Pa sys/kern/kern_clockintr.c 277*b0486b75Scheloha.Sh SEE ALSO 278*b0486b75Scheloha.Xr microtime 9 , 279*b0486b75Scheloha.Xr spl 9 , 280*b0486b75Scheloha.Xr timeout 9 281*b0486b75Scheloha.Rs 282*b0486b75Scheloha.%A Richard McDougall 283*b0486b75Scheloha.%A Jim Mauro 284*b0486b75Scheloha.%B Solaris Internals: Solaris 10 and OpenSolaris Kernel Architecture 285*b0486b75Scheloha.%I Prentice Hall 286*b0486b75Scheloha.%I Sun Microsystems Press 287*b0486b75Scheloha.%D 2nd Edition, 2007 288*b0486b75Scheloha.%P pp. 912\(en925 289*b0486b75Scheloha.Re 290*b0486b75Scheloha.Sh HISTORY 291*b0486b75SchelohaThe 292*b0486b75Scheloha.Vt clockintr 293*b0486b75Schelohaand 294*b0486b75Scheloha.Vt clockrequest 295*b0486b75SchelohaAPIs first appeared in 296*b0486b75Scheloha.Ox 7.5 . 297