TIMECOUNTER(9)

πŸ“¦ version 11.0, in nixpkgs unstable

πŸ’« TIMECOUNTER(9) β€” the kernel dev manual, bestie edition πŸ’«

okay so this is TIMECOUNTER(9) from the Kernel Developer's Manual, and we're about to make a machine-independent binary timescale make sense. yes it's a lot of words. no you're not gonna be lost. let's go ✨


✨ NAME ✨

timecounter, tc_init β€” a machine-independent binary timescale, aka the vibe of time itself πŸ’…


🌸 SYNOPSIS 🌸

#include <sys/timetc.h>

void
tc_init(struct timecounter *tc);

that's it. that's the whole entry point. iconic.


πŸ¦‹ DESCRIPTION πŸ¦‹

okay so the timecounter interface is a machine-independent implementation of a binary timescale β€” and the way it does that is by using literally whatever hardware support it has lying around for tracking time. no cap, it just works with what's there 🫢

a timecounter, at its core, is a binary counter with exactly two properties:

  • it runs at a fixed, known frequency; and
  • it has sufficient bits to not roll over in less than approximately max(2 msec, 2/HZ seconds) β€” and yes the value 2 here is really 1 + delta, for some indeterminate value of delta. not me being like "what's delta tho" 😭 it's indeterminate, bestie, it's fine.

the interface between the hardware that implements a timecounter and the machine-independent code that uses it to keep track of time is a timecounter struct:

struct timecounter {
        timecounter_get_t       *tc_get_timecount;
        timecounter_pps_t       *tc_poll_pps;
        u_int                   tc_counter_mask;
        uint64_t                tc_frequency;
        const char              *tc_name;
        int                     tc_quality;
        void                    *tc_priv;
        struct timecounter      *tc_next;
}

the fields of the timecounter structure are described below. and by "described below" i mean right now. keep reading πŸ’–

  • u_int (*tc_get_timecount)(struct timecounter *) β€” this function reads the counter. it is not required to mask any unimplemented bits out, as long as they are constant. lowkey freeing tbh.
  • void (*tc_poll_pps)(struct timecounter *) β€” this function is optional and can be set to NULL. it will be called whenever the timecounter is rewound, and is intended to check for PPS events. normal hardware does not need it, but timecounters which latch PPS in hardware do. so if that's your fave, that's your fave ✨
  • tc_counter_mask β€” this mask should mask off any unimplemented bits. periodt.
  • tc_frequency β€” frequency of the counter in Hz. numbers, bestie πŸ”₯
  • tc_name β€” name of the timecounter. can be any NUL-terminated string. go off, name your counter whatever.
  • tc_quality β€” used to determine if this timecounter is better than another timecounter β€” higher means better. negative means β€œonly use at explicit request” πŸ™ˆ (so a negative quality is giving "don't call me, i'll call you" energy).
  • tc_priv β€” pointer to the timecounter's private parts. yes that's the actual wording. yes i'm choosing not to comment πŸ’€
  • tc_next β€” for internal use. mind your business, it's internal πŸ‘‘

so to register a new timecounter, the hardware device driver should fill a timecounter structure with appropriate values and call the tc_init() function, giving a pointer to the structure as a tc parameter. that's the whole ritual πŸŽ€


🌈 TIMESTAMP FORMAT 🌈

the timestamp format used in the machine independent timecounter implementation is a bintime structure:

struct bintime {
        time_t  sec;
        uint64_t frac;
}

the sec field records the number of seconds as well as the tv_sec field in the traditional Unix timeval and timespec structures, described in timeval(3). iykyk ⭐

the frac field records fractional seconds represented in a fully 64 bit integer, i.e. it goes all the way from 0 through 0xFFFFFFFFFFFFFFFF per each second. the effective resolution of the frac value depends on a frequency of the machine dependent timecounter source.

the bintime format is a binary number, not a pseudo-decimal number, so it can be used as a simple binary counter without expensive 64 bit arithmetic. ate and left no crumbs πŸ’…


🧚 CODE REFERENCES 🧚

  • the timecounter framework is implemented in the file sys/kern/kern_tc.c.
  • the bintime structure and related functions are defined in the file <sys/time.h>.

πŸ”₯ SEE ALSO πŸ”₯

clock_settime(2), ntp_adjtime(2), settimeofday(2), bintime(9), bintime_add(9), binuptime(9), hz(9), time_second(9)

and also this absolute classic:

  • Poul-Henning Kamp, β€œTimecounters: Efficient and precise timekeeping in SMP kernels”, Proceedings of EuroBSDCon 2002, Amsterdam, http://phk.freebsd.dk/pubs/timecounter.pdf, 15-17 November, 2002. 🫢

πŸŽ€ HISTORY πŸŽ€

the timecounter interface first appeared in FreeBSD, and was ported to NetBSD 4.0 by Frank Kardel and Simon Burge. shoutout to the OGs πŸ‘‘


ok that's the whole page, you understood all of it, i'm so proud of you πŸ˜­πŸ’œ

and that's a wrap, bestie πŸ“ this was TIMECOUNTER(9) β€” Debian, September 18, 2021 β€” go forth and keep time immaculately βœ¨πŸ‘‘

πŸ“¦ where this page comes from
  • nixpkgs unstable: netbsd-source 11.0
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms