The concept of time in a kernel
The kernel calculates and manages time with the help of the hardware. The hardware provides a system timer for the kernel to calculate elapsed time. System
The system timer triggers itself at some frequency, resulting in clock interruption and processing in the kernel clock interrupt handler.
Wall time and system run time are calculated based on the clock interval.
Work performed using the time interrupt cycle:
Update system run time;
Update the actual time;
On the SMP system, the queue is run on each processor in the equalization scheduler;
Check whether the current process is running out of time slices and re-scheduling;
A dynamic timer that runs out of time;
Update the statistical values of resource consumption and processor time;
Two beats rate
The frequency of the system timer;--hz defined by static preprocessing; The system starts to set the hardware according to the HZ value. Different architectures, Hz values are different; Hz variable.
Kernel Time Frequency
#define HZ 1000
Increase the benefits of more frequent beat rate interrupts:
Improve the resolution of time-driven events;
Improve the accuracy of time-driven events;
Kernel timers with higher frequency and accuracy;
System calls poll () and select () rely on overhead to perform higher precision;
More granular system time measurement;
Improve the accuracy of process preemption;
Increase the side effects of the beat rate:
Interrupt frequency increases the burden of the system;
Interrupt handlers consume more processor time;
Frequent interrupt processor cache;
The beat rate Hz value needs to be balanced.
Three jiffies
jiffies: A global variable used to record the total number of beats that have been generated since the system started. At startup, the kernel initializes the variable to 0;
Thereafter each time the clock interrupt handler increments the value of the variable. Number of interrupts per second hz,jiffies Hz in one second. System Run time = jiffie/hz.
Jiffies use: Calculate elapsed time and time management
Jiffies internal representation:
extern U64 jiffies_64;
extern unsigned long volatile jiffies; Bit-length more system-related 32/64
32-bit: overflow after 497 days
64 bit: ...
0.5 seconds after timeout unsigned long timeout = jiffies + hz/2;......//Note jiffies value overflow wrapping with macro time_before instead of straight timeout > jiffiesif (Time_before ( Jiffies,timeout)) { //No timeout}else{ //timeout}
Four hard clocks and timers
Two devices are clocked: the system timer and the real-time clock.
Real-time Clock (RTC): A device used to persist system time, even when the system is turned off, to provide power-holding system timing by a miniature battery on the motherboard.
The system boot kernel initializes the wall time by reading the RTC, and the time is stored in the Xtime variable.
System timers: Kernel timing mechanism, register interrupt handlers, periodically trigger interrupts, respond to interrupt handlers, and perform the following tasks:
L Get Xtime_lock Lock, Access jiffies and update wall time xtime;
L Update real-time clock;
L UPDATE the resource statistics: the current process is time-consuming, the system times and so on;
L execute a dynamic timer that has expired;
L Execute Scheduler_tick ()
Interrupt handler irqreturn_t timer_interrupt (int irq, void *dev) { //ticks has passed long nticks; Xtime_update (nticks); while (nticks--) update_process_times (User_mode (Get_irq_regs ())); return irq_handled;} void Xtime_update (unsigned long ticks) { //seq lock Write_seqlock (&xtime_lock); Do_timer (ticks); Write_sequnlock (&xtime_lock);} void Do_timer (unsigned long ticks) { jiffies_64 + = ticks; Update the wall time-actual time update_wall_time (); Calc_global_load (ticks);} void update_process_times (int user_tick) { struct task_struct *p = current; Calculates the current process execution Time Account_process_tick (P, user_tick); A timer run_local_timers () that triggers a soft interrupt timer_softirq timeout ; Calculation process time slice scheduler_tick ();}
Five timers
Timers: The basis for managing kernel time, delaying or executing some code execution time.
Timer data structure:
struct Timer_list { struct list_head entry; The timing value is based on jiffies unsigned long expires; Timer internal value struct tvec_base *base; Timer handler function void (*function) (unsigned long); Timer processing function parameter unsigned long data; ... };
Timer use:
struct timer_list my_timer; Initialize timer init_timer (&my_timer); ... Activation Timer Add_timer (&my_timer); Delete Timer Del_timer (my_timer); ......
Six-Delay execution
Use the timer and the lower half mechanism to postpone the execution of the task. There are other mechanisms for delaying execution:
Busy waiting:
With the beat, the accuracy is not high
unsigned long delay = jiffies + 2*hz; 2 seconds beats integer times;
while (Time_before (Jiffies,delay))
;
Short delay: The delay time is accurate to milliseconds, subtle, a short wait for an action to complete, shorter than the clock beat, and several loops to achieve the delay effect.
void Udelay (unsigned long usecs)
void Mdelay (unsigned long msecs)
Schedule_timeout () Delay: Causes the performed task to sleep for a specified time, reaching a delay
Signed Long __sched Schedule_timeout (signed long timeout) {struct timer_list timer; unsigned long expire; Switch (timeout) {case MAX_SCHEDULE_TIMEOUT://Indefinite sleep SCHEDULE (); Goto out; Default:if (Timeout < 0) {current->state = task_running; Goto out; }}//timeout time expire = timeout + jiffies; Initializes a timer timer parameter of current task Setup_timer_on_stack (&timer,Process_timeout, (unsigned long) Current); __mod_timer (&timer, expire, false, timer_not_pinned); Schedule (); Del_singleshot_timer_sync (&timer); /* Remove the timer from the object Tracker */Destroy_timer_on_stack (&timer); Timeout = expire-jiffies; Out:return Timeout < 0? 0:timeout;} static void Process_timeout (unsigned long __data) {//Wake-Up Task wake_up_process ((struct task_struct *) __data);}
Notes Linux kernel Learning (eight) timer and time management