kernel timers and clocks

John Baldwin jhb at freebsd.org
Sat Aug 1 05:32:32 UTC 2015


On Thursday, July 23, 2015 11:52:34 AM deco33000 at yandex.com wrote:
> Hello,
> 
> I am trying to figure out how timers work for the amd64 platform on freebsd 10.
> 
> How the kernel manage the timers? is it using TSC?

The kern.timecounter sysctl tree shows the available timercounters (and
information about each one) as well as which one is currently being used.

Note that timecounters are used to compute the uptime and time-of-day for
get*time and *time as well as gettimeofday() and clock_gettime() in userland.

There is another timer interface used to manage interval timers.  This uses
timer interrupts to terminate sleeps, and things like setitimer(), alarm(),
etc. (also the timeouts for select(2) and poll(2), etc.).  These can be
examined via the kern.eventtimer sysctl tree.

In some cases the same hardware can be used for both purposes, but not
always.  On x86 it is common to use the TSC for the timecounter and the
local APIC timer for the event timer.

> I see lot of functions using getnanotime and friend but I don't get how they work. 
> They just iterate over time and read a volatile value. Where/how is that value kept updated?

Every so often an event timer interrupt triggers the tc_windup() function
in kern_tc.c.  That function updates the timehands structure used by
getnanotime() and friends.

-- 
John Baldwin


More information about the freebsd-amd64 mailing list