当前位置: 首页 > news >正文

Preemption

抢占时机

1、主动让出cpu(不管用户态还是内核态):yield() or schedule()

2、用户态抢占:用户态运行的程序,通过syscall、exception、interrupt事件,进入内核态;当返回用户态时,是一个抢占触发点;调度器会根据具体情况,判断是否抢占当前进程。

3、内核态抢占

  • CONFIG_PREEMPT_NONE:内核线程如果不主动yield或sleep,就不会被抢占!
  • CONFIG_PREEMPT_VOLUNTRY:如果内核线程不主动让出cpu,也不会被抢占;不过使能该配置后,内核会在其API中增加很多抢占点,在这些抢占点内核会主动判断是否需要reschedule,并执行切换。
  • CONFIG_PREEMPT & CONFIG_PREEMPT_RT:使能内核抢占;不过当处理中断或执行disable_preempt()区域代码时,会出现一些latency。

下面是针对几个内核配置,抢占执行情况示例图:

Linux的几种抢占模型

PREEMPT_NONE:

  • No Forced Preemption (Server)
  • This model is suitable for servers as it optimizes performance by prioritizing batch tasks while minimizing latency .
  • Uses low timer cycles of 100hz to 250hz.
  • Minimize context switching

PREEMPT_VOLUNTARY:

  • Voluntary Kernel Preemption (Desktop)
  • In APIs or drivers that are expected to take a long time, preemption points can be placed in between to allow tasks that have requested rescheduling to be preempted first. (Change the schedule at necessary preemption points in between.)
  • This model is suitable for desktops as it allows for keyboard, mouse, and multimedia operations by increasing the response speed (latency) to a certain degree and also guarantees a certain level of performance .
  • preemption points
    • We reduced preemption latency by adding explicit preemption points to several codes running in kernel mode, checking if a reschedule is needed often and increasing the frequency at which preemption should be used.
    • With the help of these preemption points, we were able to reduce the latency required to start urgent tasks to less than 100us.
    • A preemption point is usually added to routines that take more than 1ms (100us).
    • There are currently close to a thousand preemption points in the kernel(内核里有接近1000抢占点!)

PREEMPT:

  • Preemptible Kernel (Low-Latency Desktop)
  • Even in kernel mode, preemption is always allowed except in a few cases (also PREEMPT_RT), so that higher priority tasks are scheduled first most of the time. Therefore, when this option is used, preemption points are unnecessary and are replaced by empty codes.
  • This model is suitable for embedded systems such as network devices that require a latency of approximately milliseconds and a fast response speed (latency).

PREEMPT_RT:

  • Fully Preemptible Kernel (Real-Time)
  • Recently, it was officially registered in the mainline starting with kernel v5.3-rc1.
    • sched/rt, Kconfig: Introduce CONFIG_PREEMPT_RT (2019, v5.3-rc1)
  • Meanwhile, kernel mainliners have been concerned about the full preempt kernel. It is because of the performance degradation, which is also the reason why it has not been included in the mainline. (Reference: Optimizing preemption | LWN.net)
  • Since most timers and various devices' interrupt processing is done in the bottom-half by default, the time blocked due to hardirq processing is shortened. Therefore, this model is suitable for systems that require real-time interrupt processing .
  • Added hard mode and soft mode flags to hrtimer, and it operates in soft mode by default. Soft mode processes bottom-half so that the timer function runs on the rt thread, and hard mode processes hardirq.
  • Only systems with architecture support can use it.

Preemption Point

might_sleep()

# define might_sleep() do { might_resched(); } while (0)

might_resched()

#ifdef CONFIG_PREEMPT_VOLUNTARY

# define might_resched() _cond_resched()

#else

# define might_resched() do { } while (0)

#endif

cond_resched()

#define cond_resched() ({                       \

        ___might_sleep(__FILE__, __LINE__, 0);  \

        _cond_resched();                        \

})

preempt_count

ARM64架构下,preempt_count的定义:

跟preempt_count相关的操作类函数:

跟preempt_count相关的查询类函数:

跟preempt_count相关的判断类函数:

Preempt Disable & Enable

http://www.dtcms.com/a/424073.html

相关文章:

  • 一个网站两个域名备案河南周口东宇网站建设
  • 词向量:从 One-Hot 到 BERT Embedding,NLP 文本表示的核心技术
  • 3DGS 如何理解它?
  • 北京的网站建设公司有哪些wordpress 删除 加载中
  • 从PHP到Spring Boot:思维的转变与入门实战 (指南二)
  • 宁波网络公司网站建设项目网站开发人员职位描述
  • 串扰05-远端串扰的饱和
  • 湖南网站推广免费开源企业cms
  • 句容网站开发wordpress页面输入密码
  • 深入理解 Java 并发编程:从理论到实践的全面指南
  • 网站需求分析有哪些内容仿牌网站专用vps
  • 做网站超速云佛山市seo网站设计工具
  • 网站开发服务器怎么选wordpress黑白主题
  • Product Hunt 每日热榜 | 2025-09-29
  • 国外设交网站开发客户的重要性中国互联网公司排名2022
  • 目标检测: yolov6算法在RK3588上部署
  • 做网站的分辨率h5网站的好处
  • Docker 容器无法访问外网的问题排查与解决指南
  • 青岛 网站科技公司模板做的网站不好优化
  • DataLab 平台亮相 MAIC 2025医学人工智能大会,和鲸助力同济医院构建医学 AI 科研新基础设施
  • 如何制作建筑公司网站手机端网页制作公司
  • 个人学习专用:Delphi 13 Florence 安装(含C++ Builder13)x64+x32
  • 有做网站需求的客户哪个app可以找培训班
  • 网站设计论文参考文献找培训机构的app
  • 舒城网站建设wordpress调用内容前多少字
  • 【数据结构与算法-Day 36】查找算法入门:从顺序查找的朴素到二分查找的惊艳
  • docker镜像和容器
  • 小迪Web自用笔记42
  • 【Phar反序列化】
  • “软件测试” 是啥?像 “奶茶试喝”,讲透黑盒 / 白盒测试区别