Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the kernel timer system and how is it related to the scheduler?

I'm having a hard time understanding this.

  • How does the scheduler know that a certain period of time has passed?
  • Does it use some sort of syscall or interrupt for that?
  • What's the point of using the constant HZ instead of seconds?
  • What does the system timer have to do with the scheduler?
like image 717
Trey Avatar asked Nov 14 '17 20:11

Trey


1 Answers

How does the scheduler know that a certain period of time has passed?

The scheduler consults the system clock.

Does it use some sort of syscall or interrupt for that?

Since the system clock is updated frequently, it suffices for the scheduler to just read its current value. The scheduler is already in kernel mode so there is no syscall interface involved in reading the clock.

Yes, there are timer interrupts that trigger an ISR, an interrupt service routine, which reads hardware registers and advances the current value of the system clock.

What's the point of using the constant HZ instead of seconds?

Once upon a time there was significant cost to invoking the ISR, and on each invocation it performed a certain amount of bookkeeping, such as looking for scheduler quantum expired and firing TCP RTO retransmit timers. The hardware had limited flexibility and could only invoke the ISR at fixed intervals, e.g. every 10ms if HZ is 100. Higher HZ values made it more likely the ISR would run and find there is nothing to do, that no events had occurred since the previous run, in which case the ISR represented overhead, cycles stolen from a foreground user task. Lower HZ values would impact dispatch latency, leading to sluggish network and interactive response times. The HZ tuning tradeoff tended to wind up somewhere near 100 or 1000 for practical hardware systems. APIs that reported system clock time could only do so in units of ticks, where each ISR invocation would advance the clock by one tick. So callers would need to know the value of HZ in order to convert from tick units to S.I. units. Modern systems perform network tasks on a separately scheduled TCP kernel thread, and may support tickless kernels which discard many of these outdated assumptions.

What does the system timer have to do with the scheduler?

The scheduler runs when the system timer fires an interrupt. The nature of a pre-emptive scheduler is it can pause "spinning" usermode code, e.g. while (1) {}, and manipulate the run queue, even on a single-core system.

Additionally, the scheduler runs when a process voluntarily gives up its time slice, e.g. when issuing syscalls or taking page faults.

like image 86
J_H Avatar answered Nov 09 '22 02:11

J_H