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.c uses event timers to supply the kernel with hardclock(), statclock() and profclock() time events. literally feeding the kernel its little snacks.
  • Glue code β€” sys/sys/timeet.h, sys/kern/kern_et.c provide 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 first argument specifies the time period before the first event generated. in periodic mode, a NULL value specifies that the first period is equal to the period argument value.
  • the period argument specifies the time period between following events for the periodic mode. a NULL value 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, updates et->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 updates et->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