這裏有兩個概念,內核搶佔與用戶態搶佔。什麼是內核搶佔?就是指程序執行系統調用的時候(也就是執行於內核態的時候)被其餘內核線程搶佔走了。git
除了以上狀況之外,就可能被搶佔了。vim
如何知道當前是否爲搶佔系統vim /boot/config-3.10.0-123.9.3.el7.x86_64
查看相應的選項服務器
2.6內核之後支持內核搶佔。但須要配置:app
PREEMPT_NONE
———CONFIG_PREEMPT
和CONFIG_PREEMPT_VOLUNTARY
都不會設置,表示在內核態既不會被搶佔,調might_resched()
函數也不會主動切換CONFIG_PREEMPT_VOLUNTARY
,might_resched()函數生效,內核態依然不會被搶佔CONFIG_PREEMPT
和CONFIG_PREEMPT_VOLUNTARY
同時生效,在中斷返回內核態時會檢查TIF_NEEDRESCHED
標誌,若是須要調度,則會調schedule,內核態被搶佔CONFIG_PREEMPT_VOLUNTARY
是資源被搶佔,是內核在耗時操做中間,時不時插入一行代碼,主動要求被schedule。ide
CONFIG_PREEMPT
則是被動的搶佔了。函數
The Linux 2.6 configuration option
CONFIG_PREEMPT_VOLUNTARY
introduces checks to the most common causes of long latencies, so that the kernel can voluntarily yield control to a higher priority task waiting to execute. This can be helpful, but while it reduces the occurences of long latencies (hundreds of milliseconds to potentially seconds or more), it does not eliminate them. However unlikeCONFIG_PREEMPT
(discussed below),CONFIG_PREEMPT_VOLUNTARY
has a much lower impact on the overall throughput of the system. (As always, there is a classical tradeoff between throughput — the overall efficiency of the system — and latency. With the faster CPU’s of modern-day systems, it often makes sense to trade off throughput for lower latencies, but server class systems that do not need minimum latency guarantees may very well choose to use eitherCONFIG_PREEMPT_VOLUNTARY
, or to stick with the traditional non-preemptible kernel design.)性能
The 2.6 Linux kernel has an additional configuration option,
CONFIG_PREEMPT
, which causes all kernel code outside of spinlock-protected regions and interrupt handlers to be eligible for non-voluntary preemption by higher priority kernel threads. With this option, worst case latency drops to (around) single digit milliseconds, although some device drivers can have interrupt handlers that will introduce latency much worse than that. If a real-time Linux application requires latencies smaller than single-digit milliseconds, use of theCONFIG_PREEMPT_RT
patch is highly recommended.ui
搶佔用得越狠,那麼對於高優先級的任務,latency就越低。可是搶佔自己也是耗時的,頻繁的上下文切換確定影響系統吞吐量的,所以整體性能會降一點。通常這樣都是划算的,反正如今CPU很強,稍微降一點關係不大。可是若是真的不須要對latency要求那麼苛刻,那麼關掉搶佔,或者只打開自願搶佔(CONFIG_PREEMPT_VOLUNTARY
)也是能夠的。目前通常是嵌入式實時系統打開這個選項。this
服務器版 通常是 CONFIG_PREEMPT_NONE=y
spa
我的版 通常是 CONFIG_PREEMPT_VOLUNTARY=y
#ifdef CONFIG_PREEMPT_VOLUNTARY extern int _cond_resched(void); # define might_resched() _cond_resched() #else # define might_resched() do { } while (0) #endif