本文包含任务调度官方文档和网络上其他文档:
sched(7) - Linux manual pagehttps://www.man7.org/linux/man-pages/man7/sched.7.html
调度的基本概念: Scheduling policies The scheduler is the kernel component that decides which runnable thread will be executed by the CPU next. Each thread has an associated scheduling policy and a static scheduling priority, sched_priority. The scheduler makes its decisions based on knowledge of the scheduling policy and static priority of all threads on the system.
调度策略可以分为三类:普通调度策略、实时调度策略和限期完成调度策略: For threads scheduled under one of the normal scheduling policies (SCHED_OTHER, SCHED_IDLE, SCHED_BATCH), sched_priority is not used in scheduling decisions (it must be specified as 0).
Processes scheduled under one of the real-time policies (SCHED_FIFO, SCHED_RR) have a sched_priority value in the range 1 (low) to 99 (high). (As the numbers imply, real-time threads always have higher priority than normal threads.) Conceptually, the scheduler maintains a list of runnable threads for each possible sched_priority value. In order to determine which thread runs next, the scheduler looks for the nonempty list with the highest static priority and selects the thread at the head of this list.
6个策略的详细说明:
SCHED_FIFO: First in-first out scheduling SCHED_FIFO can be used only with static priorities higher than 0, which means that when a SCHED_FIFO thread becomes runnable, it will always immediately preempt any currently running SCHED_OTHER, SCHED_BATCH, or SCHED_IDLE thread. SCHED_FIFO is a simple scheduling algorithm without time slicing.
SCHED_RR: Round-robin scheduling SCHED_RR is a simple enhancement of SCHED_FIFO. Everything described above for SCHED_FIFO also applies to SCHED_RR, except that each thread is allowed to run only for a maximum time quantum. If a SCHED_RR thread has been running for a time period equal to or longer than the time quantum, it will be put at the end of the list for its priority. A SCHED_RR thread that has been preempted by a higher priority thread and subsequently resumes execution as a running thread will complete the unexpired portion of its round-robin time quantum. The length of the time quantum can be retrieved using sched_rr_get_interval(2). SCHED_DEADLINE: Sporadic task model deadline scheduling Since version 3.14, Linux provides a deadline scheduling policy (SCHED_DEADLINE). This policy is currently implemented using GEDF (Global Earliest Deadline First) in conjunction with CBS (Constant Bandwidth Server).
SCHED_OTHER: Default Linux time-sharing scheduling SCHED_OTHER can be used at only static priority 0 (i.e., threads under real-time policies always have priority over SCHED_OTHER processes). SCHED_OTHER is the standard Linux time-sharing scheduler that is intended for all threads that do not require the special real-time mechanisms. The thread to run is chosen from the static priority 0 list based on a dynamic priority that is determined only inside this list. The dynamic priority is based on the nice value (see below) and is increased for each time quantum the thread is ready to run, but denied to run by the scheduler. This ensures fair progress among all SCHED_OTHER threads. In the Linux kernel source code, the SCHED_OTHER policy is actually named SCHED_NORMAL.
SCHED_BATCH: Scheduling batch processes (Since Linux 2.6.16.) SCHED_BATCH can be used only at static priority 0. This policy is similar to SCHED_OTHER in that it schedules the thread according to its dynamic priority (based on the nice value). The difference is that this policy will cause the scheduler to always assume that the thread is CPU-intensive. Consequently, the scheduler will apply a small scheduling penalty with respect to wakeup behavior, so that this thread is mildly disfavored in scheduling decisions. This policy is useful for workloads that are noninteractive, but do not want to lower their nice value, and for workloads that want a deterministic scheduling policy without interactivity causing extra preemptions (between the workload's tasks). SCHED_IDLE: Scheduling very low priority jobs (Since Linux 2.6.23.) SCHED_IDLE can be used only at static priority 0; the process nice value has no influence for this policy. This policy is intended for running jobs at extremely low priority (lower even than a +19 nice value with the SCHED_OTHER or SCHED_BATCH policies).
对nice值的说明:
The nice value The nice value is an attribute that can be used to influence the CPU scheduler to favor or disfavor a process in scheduling decisions. It affects the scheduling of SCHED_OTHER and SCHED_BATCH (see below) processes. The nice value can be modified using nice(2), setpriority(2), or sched_setattr(2).
sched_setattr(2) - Linux manual pagehttps://www.man7.org/linux/man-pages/man2/sched_setattr.2.html代码中使用sched_setattr对任务调度参数的设置:
struct sched_attr { u32 size; /* Size of this structure */ u32 sched_policy; /* Policy (SCHED_*) */ u64 sched_flags; /* Flags */ s32 sched_nice; /* Nice value (SCHED_OTHER, SCHED_BATCH) */ u32 sched_priority; /* Static priority (SCHED_FIFO, SCHED_RR) */ /* Remaining fields are for SCHED_DEADLINE */ u64 sched_runtime; u64 sched_deadline; u64 sched_period; };
实时策略:需要注意的是,只有实时策略可以使用sched_priority设置优先级,需要注意的是这里设置的优先级并不等同于kernel内的0~139的优先级,kernel中的优先级与此处的sched_priority有一个映射关系:
kernel priority = 100 - 1 - sched_priority
所以,sched_priority为0时对应kernel中的优先级99。sched_priority为99时对应的kernel中的优先级是0,为最高优先级,这里容易混淆,不知道当时为何要这么设计。
至于sched_priority的取值范围可以参考:
Processes with numerically higher priority values are scheduled before processes with numerically lower priority values. Thus, the value returned by sched_get_priority_max() will be greater than the value returned by sched_get_priority_min(). Linux allows the static priority range 1 to 99 for the SCHED_FIFO and SCHED_RR policies, and the priority 0 for the remaining policies. Scheduling priority ranges for the various policies are not alterable. The range of scheduling priorities may vary on other POSIX systems, thus it is a good idea for portable applications to use a virtual priority range and map it to the interval given by sched_get_priority_max() and sched_get_priority_min() POSIX.1 requires a spread of at least 32 between the maximum and the minimum values for SCHED_FIFO and SCHED_RR.
普通策略:通过NICE值的设定来改变静态优先级,NICE与静态优先级的关系是:
kernel prifoity = 100 + 20 + sched_nice
其中sched_nice的取值范围是-20~+19,通过上边的转换就可以将普通策略的优先级设置为100~139.
sched_nice This field specifies the nice value to be set when specifying sched_policy as SCHED_OTHER or SCHED_BATCH. The nice value is a number in the range -20 (high priority) to +19 (low priority); see sched(7).
对于SCHED_DEADLINE使用后边3个参数(u64 sched_runtime; u64 sched_deadline; u64 sched_period;)进行设定:
A sporadic task is one that has a sequence of jobs, where each job is activated at most once per period. Each job also has a relative deadline, before which it should finish execution, and a computation time, which is the CPU time necessary for executing the job. The moment when a task wakes up because a new job has to be executed is called the arrival time (also referred to as the request time or release time). The start time is the time at which a task starts its execution. The absolute deadline is thus obtained by adding the relative deadline to the arrival time. The following diagram clarifies these terms: arrival/wakeup absolute deadline | start time | | | | v v v -----x--------xooooooooooooooooo--------x--------x--- |<- comp. time ->| |<------- relative deadline ------>| |<-------------- period ------------------->| When setting a SCHED_DEADLINE policy for a thread using sched_setattr(2), one can specify three parameters: Runtime, Deadline, and Period. These parameters do not necessarily correspond to the aforementioned terms: usual practice is to set Runtime to something bigger than the average computation time (or worst-case execution time for hard real-time tasks), Deadline to the relative deadline, and Period to the period of the task. Thus, for SCHED_DEADLINE scheduling, we have: arrival/wakeup absolute deadline | start time | | | | v v v -----x--------xooooooooooooooooo--------x--------x--- |<-- Runtime ------->| |<----------- Deadline ----------->| |<-------------- Period ------------------->| The three deadline-scheduling parameters correspond to the sched_runtime, sched_deadline, and sched_period fields of the sched_attr structure; see sched_setattr(2). These fields express values in nanoseconds. If sched_period is specified as 0, then it is made the same as sched_deadline. The kernel requires that: sched_runtime <= sched_deadline <= sched_period In addition, under the current implementation, all of the parameter values must be at least 1024
其他详细信息可以参考:
一次脑残的记录: Linux 中实时任务调度与优先级_IOT物联网小镇-CSDN博客背景知识:Linux 调度策略关于进程的调度策略,不同的操作系统有不同的整体目标,因此调度算法也就各不相同。这需要根据进程的类型(计算密集型?IO密集型?)、优先级等因素来进行选择。对于 Linux x86 平台来说,一般采用的是 CFS:完全公平调度算法。之所以叫做完全公平,是因为操作系统以每个线程占用 CPU 的比率来进行动态的计算,操作系统希望每一个进程都能够平均的使用 CPU 这个资源,雨露均沾。我们在创建一个线程的时候,默认就是这个调度算法 SCHED_OTHER,默认的优...https://blog.csdn.net/jchen1218/article/details/117948274?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164308004916780274141934%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=164308004916780274141934&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-1-117948274.pc_search_result_cache&utm_term=%E4%B8%80%E6%AC%A1%E8%84%91%E6%AE%8B%E7%9A%84%E8%AE%B0%E5%BD%95%EF%BC%9A+Linux+%E4%B8%AD%E5%AE%9E%E6%97%B6%E4%BB%BB%E5%8A%A1%E8%B0%83%E5%BA%A6%E4%B8%8E%E4%BC%98%E5%85%88%E7%BA%A7&spm=1018.2226.3001.4187CFS Scheduler — The Linux Kernel documentation
https://www.kernel.org/doc/html/latest/scheduler/sched-design-CFS.html进程管理 - 蜗窝科技
http://www.wowotech.net/sort/process_managementLinux进程调度器概述--Linux进程的管理与调度(十五)_OSKernelLAB(gatieme)-CSDN博客_进程调度器日期 内核版本 架构 作者 GitHub CSDN 2016-06-14 Linux-4.6 X86 & arm gatieme LinuxDeviceDrivers Linux进程管理与调度内存中保存了对每个进程的唯一描述, 并通过若干结构与其他进程连接起来.调度器面对的情形就是这样, 其任务是在程序之间共享CPU时间, 创造并行执行的错觉, 该任务分为
https://kernel.blog.csdn.net/article/details/51699889
CFS任务的负载均衡(概述)http://www.wowotech.net/process_management/load_balance.html
不同命令中优先级不同于kernel中的优先级: