Key words: RT, preempt_count, RT patch.
In addition to the CFS scheduler, there are important real-time schedulers, with two RR and FIFO scheduling policies. This chapter is just a simple introduction.
A more detailed introduction to the Linux process Management (9) Real-Time scheduling class analysis, and FIFO and RR comparison experiment.
The Linux community also maintains realtime-related patches to improve the real-time nature of Linux. These patches are introduced in the Linux real-time patches and their analysis.
1. preemption kernel
If the Linux kernel does not support preemption, then the process either proactively requires scheduling, such as schedule () or cond_resched (), or before the system call, exception handling, and interrupt processing complete the return user space .
In support of a callable kernel, if the wake-up action occurs in a system call or exception handling context, the next call to Preempt_enable () is to check whether a preemption schedule is required;
On the eve of the interrupt processing check to see if you want to preempt the current process, note that this is a return to the user space where the interrupt is returned rather than the preemption condition is not supported .
The struct Thread_info member Preempt_count count indicates whether the kernel can be fully preempted and, when Preempt_count is 0 o'clock, indicates that the kernel can be safely preempted, and that preemption is forbidden when it is greater than 0.
Preempt_count is 32bit, low 8 bits for preemption count preempt_active represents a large preemption count, typically used to represent a preemption schedule.
The kernel provides preempt_disable() to turn off preemption, and Preempt_count adds 1. preempt_enable () function open preemption, Preempt_count minus 1 to determine whether it is 0, and check the Thread_info tif_need_resched flag bit, if 0, then use Schedule () to complete scheduling preemption.
#definePreempt_disable () Do{preempt_count_inc (); \---------------------------------------- to the current current_thread_info ()->preempt_ Count plus 1 barrier (); } while(0)#definePreempt_count_inc () Preempt_count_add (1)#definePreempt_count_add (Val) __preempt_count_add (Val)Static__always_inlinevoid__preempt_count_add (intval) { *preempt_count_ptr () + =Val;}
#definePreempt_enable () Do{barrier (); if(Unlikely (Preempt_count_dec_and_test ())) \-----------------prermpt_count minus 1 is 0, and tif_need_resched is set, The schedule () is scheduled for preemption. __preempt_schedule (); } while(0)Static__always_inlineBOOL__preempt_count_dec_and_test (void){ return!--*preempt_count_ptr () &&tif_need_resched ();--------- to the current Preempt_count minus 1 and determine whether it is 0, if 0 is checked tif_need_resched}Static__always_inlineint*preempt_count_ptr (void){ return¤t_thread_info ()Preempt_count;}#defineTif_need_resched () Test_thread_flag (tif_need_resched)-----test whether the tif_need_resched is placed#defineTest_thread_flag (flag) \Test_ti_thread_flag (Current_thread_info (), flag)#define__preempt_schedule () Preempt_schedule ()asmlinkage __visiblevoid__sched Notrace Preempt_schedule (void){ if(Likely (!preemptible ())) ----------------------------------- determine if the current Preempt_count is 0 and the IRQ is not disabled. return; Preempt_schedule_common ();------------------------------------__schedule () scheduling preemption. }
# define Preemptible () (preempt_count () = = 0 &&!irqs_disabled ())
Static void __sched notrace preempt_schedule_common (void) { do { __preempt _count_add (preempt_active); __schedule (); __preempt_count_sub (preempt_active); /* * Check again in case we missed a preemption opportunity * between Schedule and now. */ Barrier (); while (need_resched ());}
2. Real-time Kernel progress
Linux has made a number of advances in improving real-time, specifically as follows:
Key Features |
Kernel version |
Description |
Preemption Suport |
2.5 |
|
PI mutexes |
N/A |
Pi is priority inheritance, the mutex of precedence inheritance |
HR Timer |
2.6.24 |
High-precision Timers |
Preemptive RCU |
2.6.25 |
Can preempt RCU |
IRQ Threads |
2.6.30 |
Middle Wire break Threaded |
Forced IRQ Threads |
2.6.39 |
Forced break in threaded |
Deadline Scheduler |
3.14 |
Deadline Scheduler |
Full Realtime preemption Support |
Rt-patches |
rt.wiki.kernel.org |
3. Kernel Delay Debugging Tool
The kernel provides interfaces and tools that allow us to get a glimpse of scheduling delays. Commonly used has a ftrace scheduler Preemptirqoff, and so on, as well as tools latencytop, Cyclictest and so on.
3.1 Ftrace Preemptirqoff
Preemptirqsoff can track the delay of shutting down interrupts and preventing process preemption code, while logging the maximum length of the shutdown.
3.2 Latencytop
Latencytop logs the kernel stack of the switched process during kernel context switching, and then matches the kernel stack function to determine the cause of the context switch.
It is convenient to determine what delays occur in the system and to see the latency of a process or thread.
Linux Process Management (7) Real-time scheduling