如何知道linux调度程序时间片?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16401294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:52:31  来源:igfitidea点击:

How to know linux scheduler time slice?

clinuxlinux-kernelembedded-linuxprocfs

提问by backlash

I'm looking for the value of the time slice (or quantum) of my Linux kernel.

我正在寻找我的 Linux 内核的时间片(或量子)的价值。

Specific Questions:

具体问题:

  • Is there a /procfile which expose such an information ?
  • (Or) Is it well-defined in the Linux header of my distributions ?
  • (Or) Is there a C function of the Linux API (maybe sysinfo) that expose this value ?
  • 是否有/proc公开此类信息的文件?
  • (或)它是否在我的发行版的 Linux 头文件中明确定义?
  • (或)是否有公开此值的 Linux API(可能是 sysinfo)的 C 函数?

采纳答案by Vilhelm Gray

The quantum allocated for a particular process may vary:

为特定进程分配的量程可能会有所不同

You can tune "slice" by adjusting sched_latency_nsand sched_min_granularity_ns, but note that "slice" is not a fixed quantum. Also note that CFS preemption decisions are based upon instantaneous state. A task may have received a full (variable) "slice" of CPU time, but preemption will be triggered only if a more deserving task is available, so a "slice" is not the "max uninterrupted CPU time" that you may expect it to be.. but it is somewhat similar.

您可以通过调整sched_latency_nssched_min_granularity_ns来调整“slice” ,但请注意,“slice”不是固定量程。另请注意,CFS 抢占决定基于瞬时状态。一个任务可能已经收到了一个完整的(可变的)“切片”CPU 时间,但是只有当有更值得的任务可用时才会触发抢占,所以“切片”不是您可能期望的“最大不间断 CPU 时间”是......但它有点相似。

This is because the Completely Fair Scheduler, the default Linux scheduler, assigns a proportion of the processorto a process rather than a fixed timeslice. That means the timeslice for each process is proportional to the current load and weighted by the process' priority value.

这是因为Completely Fair Scheduler,默认的 Linux 调度器,将处理器的一部分分配给进程而不是固定的时间片。这意味着每个进程的时间片与当前负载成正比,并由进程的优先级值加权

For special-purpose realtime processes which use SCHED_RR, the default timeslice is defined in the Linux kernel as RR_TIMESLICEin include/linux/sched/rt.h.

对于特殊用途的实时进程在使用SCHED_RR,默认的时间片是在Linux内核定义为RR_TIMESLICE在include / linux /排程/ rt.h

/*
 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
 * Timeslices get refilled after they expire.
 */
#define RR_TIMESLICE            (100 * HZ / 1000)

You can use sched_rr_get_interval()to get the SCHED_RR interval for a specific SCHED_RR process.

您可以使用sched_rr_get_interval()获取特定 SCHED_RR 进程的 SCHED_RR 间隔。

回答by Alexey Shmalko

CFS (which is default scheduler for processes) has no fixed timeslice, it is calculated at runtime depending of targeted latency (sysctl_sched_latency) and number of running processes. Timeslice could never be less than minimum granularity (sysctl_sched_min_granularity).

CFS(进程的默认调度程序)没有固定的时间片,它是在运行时根据目标延迟 ( sysctl_sched_latency) 和正在运行的进程数计算的。时间片永远不会小于最小粒度 ( sysctl_sched_min_granularity)。

Timeslice will be always between sysctl_sched_min_granularityand sysctl_sched_latency, which are defaults to 0.75 ms and 6 ms respectively and defined in kernel/sched/fair.c.

时间片将始终在sysctl_sched_min_granularity和之间sysctl_sched_latency,分别默认为 0.75 毫秒和 6 毫秒,并在kernel/sched/fair.c 中定义。

But actual timeslice isn't exported to user-space.

但实际时间片不会导出到用户空间。

回答by mtk

There is some confusion in the accepted answer between SCHED_OTHERprocesses (i.e., those operating under the (default) non-realtime round-robin timesharing policy) and SCHED_RRprocesses.

SCHED_OTHER进程(即,在(默认)非实时循环分时策略下运行的SCHED_RR进程)和进程之间的公认答案存在一些混淆。

The sched_latency_nsand sched_min_granularity_nsfiles (which are intended for debugging purposes, and visible only if the kernel is configured with CONFIG_SCHED_DEBUG) affect the scheduling of SCHED_OTHERprocesses. As noted in Alexey Shmalko's answer, the time slice under CFS is not fixed (and not exported to user space), and will depend on kernel parameters and factors such as the process's nice value.

sched_latency_nssched_min_granularity_ns(其旨在用于调试目的,且仅当内核配置有可见文件CONFIG_SCHED_DEBUG)影响的调度SCHED_OTHER过程。正如 Alexey Shmalko 的回答中所指出的,CFS 下的时间片不是固定的(并且不会导出到用户空间),并且将取决于内核参数和进程的 nice 值等因素。

sched_rr_get_interval()returns a fixed value which is the quantum that a SCHED_RRprocess is guaranteed to get, unless it is preempted or blocks. On traditional Linux, the SCHED_RRquantum is 0.1 seconds. Since Linux 3.9, the limit is adjustablevia the /proc/sys/kernel/sched_rr_timeslice_msfile, where the quantum is expressed as a millisecond value whose default is 100.

sched_rr_get_interval()返回一个固定值,这是一个SCHED_RR进程保证获得的量程,除非它被抢占或阻塞。在传统的 Linux 上,SCHED_RR时间片是 0.1 秒。从 Linux 3.9 开始,可以通过文件调整限制/proc/sys/kernel/sched_rr_timeslice_ms,其中量程表示为毫秒值,默认值为 100。

回答by Shile

I attemped to google this question's same doubt of time slice of SCHED_RRin Linux, but I did not get clear answer both from here and the kernel's source code.

我试图用谷歌搜索这个问题对SCHED_RRLinux 中的时间片的同样疑问,但我没有从这里和内核的源代码中得到明确的答案。

After further checking, I found the key point is RR_TIMESLICEis the default time slice in jiffies, not millisecond! So, the default time slice of SCHED_RRis always 100ms, no matter what HZyou've configured.

经过进一步检查,我发现关键是RR_TIMESLICEjiffies 中的默认时间片,而不是毫秒!因此,SCHED_RR无论HZ您配置了什么,默认时间片始终为 100 毫秒。

Same as the value of /proc/sys/kernel/sched_rr_timeslice_ms, which input value is in milliseconds, but it stores and outputs in jiffies!

与 的值相同/proc/sys/kernel/sched_rr_timeslice_ms,输入值以毫秒为单位,但它以jiffies存储和输出!

So, when your CONFIG_HZ=100is set, you'll find that:

所以,当你CONFIG_HZ=100设置好后,你会发现:

# echo 100 > /proc/sys/kernel/sched_rr_timeslice_ms
# cat /proc/sys/kernel/sched_rr_timeslice_ms
10

It's little bit confused, hope this can help you to understand it!

有点糊涂,希望能帮助你理解!

回答by Dennis

sysctlis used to read and write kernel parameters at runtime. The parameters available are those listed under /proc/sys/. Also Linux 3.9 added a new mechanism for adjusting (and viewing) the SCHED_RRquantum: the /proc/sys/kernel/sched_rr_timeslice_ms file exposes the quantum as a millisecond value, whose default is 100. Writing 0 to this file resets the quantum to the default value. So you might want to try:

sysctl用于在运行时读写内核参数。可用的参数列在 下/proc/sys/。此外,Linux 3.9 添加了一种用于调整(和查看)SCHED_RR量程的新机制:/proc/sys/kernel/sched_rr_timeslice_ms 文件将量程公开为毫秒值,其默认值为 100。向该文件写入 0 会将量程重置为默认值。所以你可能想尝试:

sysctl kernel.sched_rr_timeslice_ms