Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[HOWTO] Changing The Kernels Timer Frequency
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
mrtelekinesis
n00b
n00b


Joined: 06 Mar 2018
Posts: 7

PostPosted: Fri May 08, 2020 8:07 pm    Post subject: [HOWTO] Changing The Kernels Timer Frequency Reply with quote

[WIP]
Hello all, this is a guide on how to change the linux kernels timer frequency to custom values.

References:
https://en.wikipedia.org/wiki/Hertz
https://lwn.net/Articles/549580/
https://lwn.net/Articles/145973/
https://wiki.osdev.org/HPET
https://wiki.osdev.org/RTC

Kernel Sources References:
kernel/Kconfig.hz
include/linux/jiffies.h

There is 3 Timer Subsystems to be chosen at General Setup>Timers subsystem>Timer tick handling in the make menuconfig

1.CONFIG_HZ_PERIODIC
This option keeps the tick rate running periodically at a constant rate, even when the CPU doesn't need it.
2.CONFIG_NO_HZ_IDLE
This option enables a tickless idle system: timer interrupts will only trigger on an as-needed basis when the system is idle.
3.CONFIG_NO_HZ_FULL
Adaptively try to shutdown the tick whenever possible, even when the cpu is running tasks.


https://github.com/torvalds/linux/blob/master/kernel/Kconfig.hz << this is the file in the kernel tree where you can change the timer frequency.

File Location: KernelDirectory/kernel/Kconfig.hz

The example below is the custom timers I chose to experiment with, you can also add as many as you want.
Each of the values i have chosen is an exact millisecond value with no decimal point numbers.

Code:

# SPDX-License-Identifier: GPL-2.0-only
#
# Timer Interrupt Frequency Configuration
#

choice
   prompt "Timer frequency"
   default HZ_500
   help
    Allows the configuration of the timer frequency.
    NR_CPUS * HZ number of timer interrupts per second on each core.
    Example:
    For 2 cores with a 500Hz selected timer frequency, would be 1000
    ticks per second with 500 ticks occurring on each core.
    Tips:
    The more CPU cores you have, the lower the HZ value
    you want.
    Too many timer interrupts with lots of processors
    can reduce performance.

   config HZ_20
      bool "20 HZ"
   help
    20 ticks per second.
    50ms each tick.

   config HZ_25
      bool "25 HZ"
   help
    25 ticks per second.
    40ms each tick.

   config HZ_40
      bool "40 HZ"
   help
    40 ticks per second.
    25ms each tick.

   config HZ_50
      bool "50 HZ"
   help
    50 ticks per second.
    20ms each tick.

   config HZ_100
      bool "100 HZ"
   help
    100 ticks per second.
    10ms each tick.

   config HZ_125
      bool "125 HZ"
   help
    125 ticks per second.
    8ms each tick.

   config HZ_200
      bool "200 HZ"
   help
    200 ticks per second.
    5ms each tick.
    Try with six core CPUs.

   config HZ_250
      bool "250 HZ"
   help
    250 ticks per second.
    4ms each tick.
    Try with quad core CPUs.

   config HZ_500
      bool "500 HZ"
   help
    500 ticks per second.
    2ms each tick.
    Try with dual core CPUs.

   config HZ_1000
      bool "1000 HZ"
   help
    1000 ticks per second.
    1ms each tick.
    Try with single core CPUs.
endchoice

config HZ
   int
   default 20 if HZ_20
   default 25 if HZ_25
   default 40 if HZ_40
   default 50 if HZ_50
   default 100 if HZ_100
   default 125 if HZ_125
   default 200 if HZ_200
   default 250 if HZ_250
   default 500 if HZ_500
   default 1000 if HZ_1000

config SCHED_HRTICK
   def_bool HIGH_RES_TIMERS


So if you look at this example and look at the github file you can pretty much compare both and see how it is changed.

To select the timer frequency, go to Processor type and features>Timer frequency in make menuconfig.

The higher the timer frequency the lower the latency in milliseconds, but if the CPU cannot keep up with low latency then throughput will be affected, so it is a matter of finding the right timer frequency for your needs.

Increasing the timer frequency will also increase power consumption.

You can also tune HPET timers with a sysctl value, you can place this in a file in /etc/sysctl.d/local.conf
Code:

dev.hpet.max-user-freq = 20

And you can also tune RTC timers with a udev rule, you can place this in a file in /etc/udev/rules.d/10-local.rules
Code:

ACTION=="add|change", SUBSYSTEM=="rtc", KERNEL=="rtc0", ATTR{max_user_freq}="20"


20Hz = 50ms each cycle per second.

To calculate your own values, you do 1000 / X
X is the hertz value you define
I used the command line tool "bc -l" to calculate these values, it is recommended to use a decimal point calculator.

Normally the linux kernel uses TSC as a clocksource, but if the kernel sees that it is unstable it can use other timers like HPET or even ACPI-PM as a clocksource.

The clocksource can also be set by a kernel parameter clocksource= which for x86-32 can take pit, hpet and tsc and x86-64 can take hpet or tsc.

Github Link:
Here is the github link for the Kconfig.hz
https://github.com/mrtelekinesis/Linux-Configuration


Last edited by mrtelekinesis on Wed Jul 29, 2020 5:51 pm; edited 59 times in total
Back to top
View user's profile Send private message
pietinger
Apprentice
Apprentice


Joined: 17 Oct 2006
Posts: 258
Location: Bavaria

PostPosted: Fri May 08, 2020 10:51 pm    Post subject: Re: Changing The Kernel's Timer Frequency To A Custom Value Reply with quote

mrtelekinesis wrote:
The higher the frequency the lower the latency, but if the CPU cannot keep up with low latency then throughput might be affected, so it is a matter of finding the right timer frequency for your CPU, but for responsiveness(like gaming) you might want a value greater than or equal to 852Hz.

I dont think its necessary to optimize the timer frequency when using "tickless idle" (General setup > Timers subsystem -> Timer tick handling (Idle dynticks system (tickless idle))
With this you should not have any problems with 1000 hz. (I use 300 hz since today)
mrtelekinesis wrote:
https://lwn.net/Articles/145973/ << this is a good link on the kernels HZ timer frequency.

This is a very old article. Today the patch described there is a standard kernel feauture.
Back to top
View user's profile Send private message
mrtelekinesis
n00b
n00b


Joined: 06 Mar 2018
Posts: 7

PostPosted: Sat May 09, 2020 12:04 am    Post subject: Re: Changing The Kernel's Timer Frequency To A Custom Value Reply with quote

pietinger wrote:
mrtelekinesis wrote:
The higher the frequency the lower the latency, but if the CPU cannot keep up with low latency then throughput might be affected, so it is a matter of finding the right timer frequency for your CPU, but for responsiveness(like gaming) you might want a value greater than or equal to 852Hz.

I dont think its necessary to optimize the timer frequency when using "tickless idle" (General setup > Timers subsystem -> Timer tick handling (Idle dynticks system (tickless idle))
With this you should not have any problems with 1000 hz. (I use 300 hz since today)
mrtelekinesis wrote:
https://lwn.net/Articles/145973/ << this is a good link on the kernels HZ timer frequency.

This is a very old article. Today the patch described there is a standard kernel feauture.


I have that option set already, not saying that I have any problems, this is just a guide for anybody who wants to tinker with the timer frequency as there is no right Hz value, it really depends on the user, the CPU and the kind of computer work they do.


Last edited by mrtelekinesis on Wed May 13, 2020 12:17 am; edited 1 time in total
Back to top
View user's profile Send private message
Ant P.
Watchman
Watchman


Joined: 18 Apr 2009
Posts: 6619

PostPosted: Sat May 09, 2020 12:20 am    Post subject: Reply with quote

If you want low latency then use MuQSS, which uses the hpet for scheduling (~14MHz).

Hacking random values into the kernel tick rate was debunked years ago as useless except for very specific broken software (proprietary game servers).
Back to top
View user's profile Send private message
mrtelekinesis
n00b
n00b


Joined: 06 Mar 2018
Posts: 7

PostPosted: Sat May 09, 2020 12:49 am    Post subject: Reply with quote

Ant P. wrote:
If you want low latency then use MuQSS, which uses the hpet for scheduling (~14MHz).

Hacking random values into the kernel tick rate was debunked years ago as useless except for very specific broken software (proprietary game servers).


oooh, do you remember the article you read on the kernel tick rate being debunked?, i would like to read that to understand this concept better

I have used MuQSS and also PDS/BMQ, they are really great CPU schedulers, but right now im using CFS and I am using it with SCHED_TUNABLESCALING_NONE in kernel/sched/fair.c , seems good so far compared to SCHED_TUNABLESCALING_LOG on my six core


Last edited by mrtelekinesis on Wed May 13, 2020 12:18 am; edited 1 time in total
Back to top
View user's profile Send private message
Ant P.
Watchman
Watchman


Joined: 18 Apr 2009
Posts: 6619

PostPosted: Sat May 09, 2020 5:46 am    Post subject: Reply with quote

mrtelekinesis wrote:
oooh, do you remember the article you read on the kernel tick rate being debunked?, i would like to read that to understand this concept better

It wasn't an article, it was help text from the -ck patchset back in the RSDL days. It added options up to 100,000hz and then explained the ones above 1000 were mostly useless and only increase CPU load.

If you're having latency problems and it's not visibly limited by CPU usage, it's probably related to cpufreq or C-states.
Back to top
View user's profile Send private message
mrtelekinesis
n00b
n00b


Joined: 06 Mar 2018
Posts: 7

PostPosted: Sat May 09, 2020 12:54 pm    Post subject: Reply with quote

Ant P. wrote:
mrtelekinesis wrote:
oooh, do you remember the article you read on the kernel tick rate being debunked?, i would like to read that to understand this concept better

It wasn't an article, it was help text from the -ck patchset back in the RSDL days. It added options up to 100,000hz and then explained the ones above 1000 were mostly useless and only increase CPU load.

If you're having latency problems and it's not visibly limited by CPU usage, it's probably related to cpufreq or C-states.


Not having any latency issues at all, just trying to get the most out of my computer, that's all :D

I am also using schedutil cpufreq governor, but have been experimenting with ondemand cpufreq governor, i read somewhere that schedutil will be the only cpufreq governor in the future so, im gonna stick with that one
Back to top
View user's profile Send private message
krinn
Watchman
Watchman


Joined: 02 May 2003
Posts: 7427

PostPosted: Sat May 09, 2020 3:14 pm    Post subject: Re: Changing The Kernel's Timer Frequency To A Custom Value Reply with quote

mrtelekinesis wrote:
https://www.relaxmelodies.com/blog/science-behind-solfeggio-frequencies/

That article looks like history channel made :)
Quote:

Solfeggio frequencies refer to specific tones of sound that help with and promote various aspects of body and mind health. These frequencies are reputed to date back to ancient history and said to be the fundamental sounds used in both Western Christianity and Eastern Indian religions
Physician and researcher, Dr. Joseph Puleo, rediscovered Solfeggio frequencies in the 1970s...identify six measurable tones that bring the body back into balance and aid in healing.

yeah i always doubt something knew since age of time gets discovered as late as 1970...
the so-call Doctor guy nobody's knows discovering a major thing
and the pseudo science that claims healing of body thru obscure methods

nothing from him on wikipedia
and this https://rationalwiki.org/wiki/Solfeggio_frequencies
Quote:

Solfeggio frequencies, often promoted as "ancient solfeggio frequencies",[2] are a crank concept in sound healing. They involve grandiose claims about a certain series of sound frequencies (396, 417, 528, 639, 741, 852 Hz) derived from a numerological system of family number groups. The concept hijacks and misapplies terminology (including the name) from the "solfeggio" or solfègeWikipedia's W.svg music-education method, but other than that, has nothing in common with it.


no idea why, but i won't play with my kernel for this
I prefer my method by putting my hands on the kenrel .config to make it heal faster
Back to top
View user's profile Send private message
mrtelekinesis
n00b
n00b


Joined: 06 Mar 2018
Posts: 7

PostPosted: Sun May 10, 2020 11:47 pm    Post subject: Re: Changing The Kernel's Timer Frequency To A Custom Value Reply with quote

krinn wrote:
mrtelekinesis wrote:
https://www.relaxmelodies.com/blog/science-behind-solfeggio-frequencies/

That article looks like history channel made :)
Quote:

Solfeggio frequencies refer to specific tones of sound that help with and promote various aspects of body and mind health. These frequencies are reputed to date back to ancient history and said to be the fundamental sounds used in both Western Christianity and Eastern Indian religions
Physician and researcher, Dr. Joseph Puleo, rediscovered Solfeggio frequencies in the 1970s...identify six measurable tones that bring the body back into balance and aid in healing.

yeah i always doubt something knew since age of time gets discovered as late as 1970...
the so-call Doctor guy nobody's knows discovering a major thing
and the pseudo science that claims healing of body thru obscure methods

nothing from him on wikipedia
and this https://rationalwiki.org/wiki/Solfeggio_frequencies
Quote:

Solfeggio frequencies, often promoted as "ancient solfeggio frequencies",[2] are a crank concept in sound healing. They involve grandiose claims about a certain series of sound frequencies (396, 417, 528, 639, 741, 852 Hz) derived from a numerological system of family number groups. The concept hijacks and misapplies terminology (including the name) from the "solfeggio" or solfègeWikipedia's W.svg music-education method, but other than that, has nothing in common with it.


no idea why, but i won't play with my kernel for this
I prefer my method by putting my hands on the kenrel .config to make it heal faster


:D, yeah i guess so, still is interesting though, but the only way it can possibly work is if the monitors refresh rate were in those frequencies, but who knows, i am trying out 64Hz kernel timer frequency now, stuff is complex but you gotta figure it out for yourself right? :D
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum