TC_INIT(9) Kernel Developer's Manual TC_INIT(9)

tc_inittimecounting subsystem

#include <sys/timetc.h>

tc_init(struct timecounter *tc);

The subsystem implements a uniform interface to timekeeping hardware, measures the passage of time, and implements the kernel's software clocks (see microtime(9) for details).

A hardware clock is suitable for counting time if it meets the following requirements:

  1. It is a binary counter.
  2. It advances at a fixed, known frequency.
  3. Its count is synchronized between all CPUs on the system.
  4. It continues counting when it rolls over.
  5. If hz(9) is less than or equal to one millisecond, the counter does not roll over in less than two milliseconds. If hz(9) exceeds one millisecond, the counter does not roll over in less than (2 / hz) seconds.

Hardware clocks are described with a timecounter structure:

struct timecounter {
	u_int (*tc_get_timecount)(struct timecounter *);
	u_int tc_counter_mask;
	u_int64_t tc_frequency;
	char *tc_name;
	int tc_quality;
	void *tc_priv;
	u_int tc_user;
u_int (*tc_get_timecount)(struct timecounter *)
Reads the hardware clock and returns its count. Any unimplemented bits only need to be masked if they are not constant. If the counter is larger than 32 bits, this function must return a 32-bit subset. The subsystem requires an upward count; downward counts must be inverted before they are returned.
The mask of implemented bits. Used to discard unimplemented bits from ().
The counter's fixed frequency.
The counter's unique name. A NUL-terminated string.
A relative quality metric used to compare counters. Higher values indicate a better counter. A negative value indicates that the counter is non-monotonic or otherwise deficient. The system will only use negative-quality counters if requested.
May point to anything the driver needs during tc_get_timecount().
If non-zero, a unique value identifying the userspace implementation of tc_get_timecount().

To register a timecounter, a device driver initializes the above-described fields of a timecounter structure and calls () with a pointer to that structure as argument.

tc_init() may only be called during autoconf.


amdpm(4), gscpm(4), ichpcib(4), viapm(4), hz(9), microtime(9)

Poul-Henning Kamp, Timecounter: Efficient and precise timekeeping in SMP kernels, The FreeBSD Project,, 2002.

The timecounting subsystem first appeared in FreeBSD 3.0. It was ported to OpenBSD 3.6.

Poul-Henning Kamp

April 2, 2023 OpenBSD 7.5