Showing posts with label timers. Show all posts
Showing posts with label timers. Show all posts

Wednesday, January 15, 2014

Timers and Time Management

When do we need absolute time and when do we need relative time ?

Absolute time is used by the kernel to know the exact time of the day whereas relative time is used by kernel when we need schedule some event for the future(after a fixed time in future, say 10 seconds).For absolute time we have an hardware called RTC(real time clock) whereas to manage relative time we have the hardware called system timer.


How does the Kernel synchronize its timing of events?

To help kernel we have our friend System Timer Hardware. The system timer is preprogrammed to work at a certain frequency. In case of ARM and X86 it is 100 HZ. It means that the system timer issues 100 interrupts in one second to the CPU. These interrupts are also called ticks. We can get the value of system up time by knowing the value number of ticks received by the processor till now.The kernel also knows the frequency of system timer, so it also knows the interval in which it is receiving system timer interrupts. This gives the kernel the fair idea of the time and it helps it to sync events.



What are Jiffies?

They are the global variables that hold the number of ticks that have occurred since the system booted. On boot , the system initializes it to zero and is incremented on receipt of each system timer interrupt.Since there are Hz timer interrupt in a second therefore there are Hz jiffies incremented per second of system time.





  • The jiffies variable is declared in <linux/jiffies.h> 
  • extern unsigned long volatile jiffies;
  • Conversion formula--- jiffies=seconds*Hz.

  • How jiffies are stored internally?
    • The jiffies variable has always been an unsigned long, and therefore 32 bits in size on 32-bit architectures and 64-bits on 64-bit architectures.
    • With a tick rate of 100, a 32-bit jiffies variable would overflow in about 497 days.
    • With HZ increased to 1000, however, that overflow now occurs in just 49.7 days! 
    • If jiffies were stored in a 64-bit variable on all architectures, then for any reasonable HZ value the jiffies variable would never overflow in anyone’s lifetime.
    • A second variable is also defined in <linux/jiffies.h>:          extern u64 jiffies_64;
    • jiffies = jiffies_64;
    • jiffies variable becomes the lower 32 bit of jiffies_64 variable
    • by accessing the lower 32 bits of the jiffies_64 variable we get the value of our jiffies.
    • The function get_jiffies_64() can be used to read the full 64-bit value though such a need is rare.
    • On 64-bit architectures, jiffies_64 and jiffies refer to the same thing. Code can either read jiffies or call get_jiffies_64() as both actions have the same effect.
    • For a 32-bit unsigned integer, the maximum value of jiffy count is 2^32 – 1.Thus, a possible 4294967295 timer ticks can occur before the tick count overflows.
    • When the tick count is equal to this maximum and it is incremented, it wraps around to zero.