Linux Time Management

linux's time keeping architecture is the set of kernel data structure and functions related to the flow of time.

The time keeping architecture does the few things in kernel:

Tick Rate - HZ

The frequency of the system timer (the tick rate) is programmed on system boot based on a static preprocessor define, HZ.

A common values for HZ are 250 and 1000, corresponding to periods of 4ms and 1 ms.

ubuntu use 250 HZ kernel config

then, what happened when HZ is higher ?

Jiffies

jiffies is incremented when every global system timer interrupt.

a jiffies variable usually declared in <linux/jiffies.h>

extern unsigned long volatile jiffies;

unsigned long is 32bits in size of 32bits CPU and 64bits in size of 64bits CPU.

for 32bits CPU

But for 64bits CPU, the jiffies is endless and never overflow.

Prevent overflow from 32bit CPU

A second variable is also defined in <linux/jiffies.h>

extern u64 jiffies_64;

In <kernel source>/arch/<arch>/kernel/vmlinux.lds.S, depends on different CPU arch,

for example:

arm64[1]:

jiffies = jiffies_64
jiffies is not default to set zero !

jiffies is initailzed to 0xfffb6c20 in linux kernel.

How to get jiffies in kernel ?

unsigned long long get_jiffies64(void) {
	unsigned long seq;
	unsigned long long ret;
	do {
		seq = read_seqbegin(&xtime_lock);
		ret = jiffies_64;
	} while (read_seqretry(&xtime_lock, seq));
	return ret;
}

xtime

The xtime stores the current time and date; it is a structure of type timespec having two fields:

The xtime_lock seqlock avoids the race confitions that could occur due to concurrent accesses to the xtime variable.


  1. https://github.com/torvalds/linux/blob/5847c9777c303a792202c609bd761dceb60f4eed/arch/arm64/kernel/vmlinux.lds.S#L75 ↩︎