这里有两个概念,内核抢占与用户态抢占。什么是内核抢占?就是指程序执行系统调用的时候(也就是执行于内核态的时候)被其余内核线程抢占走了。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