TIMECOUNTER(9)
π« 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 value2here is really1 + delta, for some indeterminate value ofdelta. 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 toNULL. 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
bintimestructure 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
- Copyright (c) 2004 Alexander Yurchenko <grange@openbsd.org>
- Copyright (c) 2008 Izumi Tsutsui. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms