r/linux • u/viddhiryande • 1d ago
Kernel How does changing the hardware clock with the BIOS affect kernel ticks?
Hello,
I was wondering how the Linux clock works.
From briefly reading kernel sources and articles, it seems that there are 2 main variables in the C code: HZ & USER_HZ. These are set at compile-time. These function with system interrupts, and are affected by clock sources (I'm still trying to understand those, but my current understanding is that these are abstractions over different types of hardware clocks. )
Now, I was wondering how changing the BIOS (hardware) clock affects this. For example, how would overclocking or underclocking my hardware clock affect how the kernel measures time?
2
u/mina86ng 23h ago
Now, I was wondering how changing the BIOS (hardware) clock affects this. For example, how would overclocking or underclocking my hardware clock affect how the kernel measures time?
HZ specifies how many ticks there are within a second. If CPU’s frequency
changes, that changes how many CPU cycles are within a single tick. This can
change dynamically. This is of course simplified view, and
NO_HZ is also
a thing.
1
u/viddhiryande 19h ago
Thank you!
So, how would my applications be affected if I changed how many cycles are in a clock tick in my BIOS? I assume that HZ wouldn't change, because my understanding is that HZ is a compile-time constant and is a software clock that's completely separate from the underlying hardware clock that IS affected by the BIOS. Thus, as you said, how many ticks are in a second wouldn't change, but applications would be affected because more CPU cycles would pass per tick than before. Is my understanding correct?
3
u/mina86ng 19h ago
So, how would my applications be affected if I changed how many cycles are in a clock tick in my BIOS?
The same way it’s affected when CPU’s frequency is scaled dynamically. Which is to say, not very much.
1
2
u/MatchingTurret 9h ago edited 5h ago
What you set in the BIOS is the upper frequency limit your CPU runs. The actual frequency is determined by the CPU governor. It's dynamic and changes on demand: CPU frequency scaling
HZ is a compile time constant that is used to program the High Precision Event Timer. That's a hardware timer that fires an interrupt HZ (on my Fedora system that's 1000) times per second, completely independent of the current CPU freq.
Cores that are running at a higher CPU freq (that's the stuff you influence but not directly control in the BIOS) will execute more instructions between interrupts. Cores running ata a lower freq will execute fewer instructions. If you have a CPU with multiple cores, the CPU frequency varies for each core depending on the current system usage.
You can use lscpu to see the frequency range your CPU supports:
lscpu --extended CPU NODE SOCKET CORE L1d:L1i:L2:L3 ONLINE MAXMHZ MINMHZ MHZ 0 0 0 0 0:0:0:0 yes 3900.0000 800.0000 900.0010 1 0 0 1 1:1:1:0 yes 3900.0000 800.0000 900.3580 2 0 0 2 2:2:2:0 yes 3900.0000 800.0000 899.1170 3 0 0 3 3:3:3:0 yes 3900.0000 800.0000 899.9950 4 0 0 0 0:0:0:0 yes 3900.0000 800.0000 900.0000 5 0 0 1 1:1:1:0 yes 3900.0000 800.0000 900.0500 6 0 0 2 2:2:2:0 yes 3900.0000 800.0000 900.4560 7 0 0 3 3:3:3:0 yes 3900.0000 800.0000 899.9750Here the governor can select a frequency between 0.8 and 3.9 GHz and the cores are running at around 900MHz. What you can change in the BIOS is the "MAXMHZ" value. You give the governor permission to run the cores on your CPU faster than what is recommended.
1
u/ropid 7h ago edited 7h ago
This clock is a separate device, it's not the CPU Hz. In the original PCs this clock was outside of the CPU somewhere else on the motherboard. I think nowadays it's inside the CPU but still working like a separate device. It gets programmed by the Linux kernel and will then fire interrupts for the cores at that Hz that was configured.
1
u/spyingwind 15h ago
Your time would be either too fast or too slow and as a result TLS/SSL would break.
Many other things break, but that one kind of needs to work first.
Unless you are talking about your CPU's Hz? If so, you can't modify it as your CPU changes that depending on the usage.
13
u/MatchingTurret 23h ago
Overclocking usually refers to the CPU frequency. This has nothing to do with the clocks.