How do kernels keep track of time

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
8infy
Member
Member
Posts: 185
Joined: Sun Apr 05, 2020 1:01 pm

How do kernels keep track of time

Post by 8infy »

I know that there's all sorts of timers to keep track of time, however, since kernels often have to disable interrupts how do they keep the time accurate?
I would think it would get skewed real fast... Would appreciate any information on timekeeping overall. I know that there are time servers for easy time syncing,
but you wouldn't open a new connection to sync time each time a user does now() in their code right? So how do we get around that?
nullplan
Member
Member
Posts: 1791
Joined: Wed Aug 30, 2017 8:24 am

Re: How do kernels keep track of time

Post by nullplan »

There's hardware for that. Specifically, there's the HPET. If someone wants to know the time, you can just read the HPET counter and normalize the value somehow. The counter is 64-bits and will therefore not overflow during the runtime of the system, unless you have a particularly long-lived system (on the order of hundreds of years). And you are told the tick rate in the ACPI table, or you can calibrate using the PIT. The HPET counter will tick ever onwards, without a care in the world for your little interrupt flag. Alternatively you can use the TSC, but that is a little harder since every core can have its own TSC, so you need to read the correct one, or else maintain calibration for all cores separately. Building a monotonic timer out of this that is invariant even under core migration is a bit of a challenge.

For two, even if you do use the old interrupt-counted system tick idea, once the system is running, when looking at the system over time, the instances of disabled interrupts are very small compared to most of the time. I should think that the vast, vast, VAST majority of the time is spent idling, anyway. And if you program your timer, whether PIT or HPET or LAPIC timer, to produce periodic interrupts, it is highly unlikely the kernel would remain with interrupts disabled for an entire timer period. And nothing else will introduce a clock skew.

NTP comes into play when you want to fine tune the mapping of whatever timer you are using to actual real-world time. But that is more of a maintenance thing.
Carpe diem!
Octocontrabass
Member
Member
Posts: 5574
Joined: Mon Mar 25, 2013 7:01 pm

Re: How do kernels keep track of time

Post by Octocontrabass »

8infy wrote:I know that there's all sorts of timers to keep track of time, however, since kernels often have to disable interrupts how do they keep the time accurate? I would think it would get skewed real fast...
Usually you'd program one of those timers to raise a periodic interrupt, so that the timer will continue to count at a regular rate even if individual ticks are occasionally delayed.

Depending on which timer(s) you're using, you might choose to poll the timer for its value whenever you need to see how much time has passed instead of (or in addition to) using an interrupt.
8infy wrote:Would appreciate any information on timekeeping overall.
The wiki might help.
8infy
Member
Member
Posts: 185
Joined: Sun Apr 05, 2020 1:01 pm

Re: How do kernels keep track of time

Post by 8infy »

thanks for the answers :)
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Re: How do kernels keep track of time

Post by Korona »

On systems that have an invariant TSC (and that are also unaffected by various errata :roll:), the TSC seems to be by far the best clock source. Reading the TSC is much faster than reading anything else (slightly faster than the LAPIC timer and considerably faster than reading the HPET/PIT). Anecdotally: in Managarm's tickless scheduler, switching from the LAPIC to the TSC for scheduling purposes gave around 10% overall improvement in a scheduling-heavy userspace microbenchmark (probably also due to the fact TSC reads are not serializing). The TSC can also be read from userspace; this enables implementations of CLOCK_MONOTONIC and CLOCK_REALTIME without entering the kernel. Benchmarks (and other programs, like networking stacks) like to query these clocks a lot so this is an important optimization.
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].
Post Reply