EVENTTIMERS(9)
β¨ EVENTTIMERS(9) β¨
okay bestie, gather round, because we're about to talk about the kernel event timers subsystem and honestly? it's giving main character energy. this is a section 9 page (kernel developer's manual, iykyk π ) so it's the deep lore, not the casual user-facing stuff.
πΈ SYNOPSIS πΈ
first things first, the include, because we always include:
#include <sys/timeet.h>
then, the forward declaration, sitting there looking mysterious:
struct eventtimer;
and now the typedefs, our little function-pointer besties:
typedef int et_start_t(struct eventtimer *et,
sbintime_t first, sbintime_t period);
typedef int et_stop_t(struct eventtimer *et);
typedef void et_event_cb_t(struct eventtimer *et, void *arg);
typedef int et_deregister_cb_t(struct eventtimer *et, void *arg);
and here's the whole struct, the moment you've all been waiting for, no cap:
struct eventtimer {
SLIST_ENTRY(eventtimer) et_all;
char *et_name;
int et_flags;
#define ET_FLAGS_PERIODIC 1
#define ET_FLAGS_ONESHOT 2
#define ET_FLAGS_PERCPU 4
#define ET_FLAGS_C3STOP 8
#define ET_FLAGS_POW2DIV 16
int et_quality;
int et_active;
uint64_t et_frequency;
sbintime_t et_min_period;
sbintime_t et_max_period;
et_start_t *et_start;
et_stop_t *et_stop;
et_event_cb_t *et_event_cb;
et_deregister_cb_t *et_deregister_cb;
void *et_arg;
void *et_priv;
struct sysctl_oid *et_sysctl;
};
now the functions, in their natural habitat:
int
et_register(struct eventtimer *et);
int
et_deregister(struct eventtimer *et);
void
et_change_frequency(struct eventtimer *et, uint64_t newfreq);
ET_LOCK();
ET_UNLOCK();
struct eventtimer *
et_find(const char *name, int check, int want);
int
et_init(struct eventtimer *et, et_event_cb_t *event,
et_deregister_cb_t *deregister, void *arg);
int
et_start(struct eventtimer *et, sbintime_t first, sbintime_t period);
int
et_stop(struct eventtimer *et);
int
et_ban(struct eventtimer *et);
int
et_free(struct eventtimer *et);
π¦ DESCRIPTION π¦
so what's the whole vibe here? Event timers are the things responsible for generating interrupts at a specified time, or periodically, so that different time-based events can actually run. like, someone has to keep the clock honest. π°οΈ
the subsystem is made of three main parts, and yes, three is a magic number:
- Drivers β manage hardware to generate requested time events. hardware girlies, respect.
- Consumers β
sys/kern/kern_clocksource.cuses event timers to supply the kernel withhardclock(),statclock()andprofclock()time events. literally feeding the kernel its little snacks. - Glue code β
sys/sys/timeet.h,sys/kern/kern_et.cprovide APIs for event timer drivers and consumers. holding everyone together, doing the emotional labour π«Ά
π DRIVER API π
the Driver API is built around the eventtimer structure. to register its functionality, a driver allocates that structure and calls et_register(). and bestie, the driver should fill in these fields:
et_nameβ unique name of the event timer, for management purposes. no two timers can share a name, it's not that kind of party.et_flagsβ a set of flags describing timer capabilities:ET_FLAGS_PERIODICβ periodic mode supported.ET_FLAGS_ONESHOTβ one-shot mode supported.ET_FLAGS_PERCPUβ timer is per-CPU.ET_FLAGS_C3STOPβ timer may stop in CPU sleep state. (it naps. it's valid.)ET_FLAGS_POW2DIVβ timer supports only 2^n divisors.
et_qualityβ abstract value to certify whether this timecounter is better than the others. higher value means better. it's a ranking, it's a leaderboard, it's a vibe check β¨et_frequencyβ timer oscillator's base frequency, if applicable and known. consumers use it to predict the set of possible frequencies that could be obtained by dividing it. should be zero if not applicable or unknown. don't lie about your frequency.et_min_period,et_max_periodβ minimal and maximal reliably programmable time periods.et_startβ driver's timer start function pointer.et_stopβ driver's timer stop function pointer.et_privβ driver's private data storage. keep your secrets π
after the event timer functionality is registered, it is controlled via the et_start and et_stop methods. the et_start method is called to start the specified event timer. the last two arguments specify the time when events should be generated:
- the
firstargument specifies the time period before the first event generated. in periodic mode, aNULLvalue specifies that the first period is equal to theperiodargument value. - the
periodargument specifies the time period between following events for the periodic mode. aNULLvalue there specifies the one-shot mode.
at least one of these two arguments should be not NULL. fr fr, pick one.
when event time arrives, the driver should call the et_event_cb callback function, passing et_arg as the second argument. the et_stop method is called to stop the specified event timer. for the per-CPU event timers, et_start and et_stop methods control timers associated with the current CPU.
a driver may deregister its functionality by calling et_deregister().
if the frequency of the clock hardware can change while it is running (for example, during power-saving modes), the driver must call et_change_frequency() on each change. and here's the tea on what it does:
- if the given event timer is the active timer,
et_change_frequency()stops the timer on all CPUs, updateset->frequency, then restarts the timer on all CPUs so that all current events are rescheduled using the new frequency. glow-up complete π - if the given timer is not currently active,
et_change_frequency()simply updateset->frequency. lowkey chill about it.
π CONSUMER API π
et_find() allows a consumer to find an available event timer, optionally matching a specific name and/or capability flags. the consumer may read the returned eventtimer structure, but should not modify it. look, don't touch β
when the wanted event timer is found, et_init() should be called for it, submitting the event and optionally the deregister callback functions, plus the opaque argument arg. that argument will be passed as an argument to the callbacks.
- the event callback function will be called on scheduled time events. it is called from the hardware interrupt context, so no sleep is permitted there. seriously, no sleeping on the job π΄π«
- the deregister callback function may be called to report to the consumer that the event timer functionality is no longer available. on this call, the consumer should stop using the event timer before the return. bye bestie, it was real π₯²
after the timer is found and initialized, it can be controlled via et_start() and et_stop(). the arguments are the same as described in the driver API. per-CPU event timers can be controlled only from specific CPUs.
et_ban() allows a consumer to mark an event timer as broken via clearing both one-shot and periodic capability flags, if it was somehow detected. blocked. cancelled. not on my watch π
et_free() is the opposite of et_init(). it releases the event timer for other consumers to use. sharing is caring π
ET_LOCK() and ET_UNLOCK() macros should be used to manage the mutex(9) lock around et_find(), et_init() and et_free() calls, to serialize access to the list of registered event timers and the pointers returned by et_find(). meanwhile, et_start() and et_stop() calls should be serialized in the consumer's internal way, to avoid concurrent timer hardware access. don't let two hands touch the same hardware at once, it's messy.
π₯ SEE ALSO π₯
π AUTHORS π
- Alexander Motin
<mav@FreeBSD.org>
and that's the whole page, bestie β every last flag, field, callback and caveat, preserved with love and zero detail loss β¨ periodt.
this little transmission came from the Debian-flavoured corner of the internet, dated April 2, 2014, filed under EVENTTIMERS(9) β stay iconic, stay timely, and always close your mutexes π«Άπ
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2011-2013 Alexander Motin <mav@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms