Linux中哪个实时优先级最高 [英] Which real-time priority is the highest priority in Linux

查看:21
本文介绍了Linux中哪个实时优先级最高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Linux 实时进程优先级范围 1 到 99 中,我不清楚哪个是最高优先级,1 还是 99.

了解 Linux 内核"(O'Reilly)的第 7.2.2 节说 1 是最高优先级,考虑到正常进程的静态优先级从 100 到 139,其中 100 是最高优先级,这是有道理的:<块引用>

"每个实时进程都与一个实时优先级相关联,该优先级的值范围为 1(最高优先级)到 99(最低优先级)."

另一方面,sched_setscheduler 手册页 (RHEL 6.1) 声称 99 是最高的:

<块引用>

"在实时策略之一(SCHED_FIFO、SCHED_RR)下调度的进程具有 1(低)到 99(高)范围内的 sched_priority 值."

哪个实时优先级最高?

解决方案

我做了一个实验来确定这一点,如下:

  • 进程 1:RT 优先级 = 40,CPU 关联性 = CPU 0.此进程旋转"10 秒,因此不会让任何低优先级进程在 CPU 0 上运行.

  • process2:RT 优先级 = 39,CPU 关联性 = CPU 0.此进程每 0.5 秒向 stdout 打印一条消息,并在此期间休眠.它打印出每条消息的经过时间.

我正在运行带有 PREEMPT_RT 补丁的 2.6.33 内核.

为了运行实验,我在一个窗口中运行 process2(以 root 身份),然后在另一个窗口中启动 process1(以 root 身份).结果是 process1 似乎抢占了 process2,不允许它运行整整 10 秒.

在第二个实验中,我将 process2 的 RT 优先级更改为 41.在这种情况下,process2 不会被 process1 抢占.

这个实验表明,sched_setscheduler() 中的较大 RT 优先级值具有更高的优先级.这似乎与 Michael Fokarakis 在 sched.h 中指出的相矛盾,但实际上并非如此.在内核源代码中的 sched.c 中,我们有:

静态无效__setscheduler(struct rq *rq, struct task_struct *p, int 策略, int prio){BUG_ON(p->se.on_rq);p->policy = 政策;p->rt_priority = prio;p->normal_prio = normal_prio(p);/* 我们已经持有 p->pi_lock */p->prio = rt_mutex_getprio(p);如果 (rt_prio(p->prio))p->sched_class = &rt_sched_class;别的p->sched_class = &fair_sched_class;set_load_weight(p);}

rt_mutex_getprio(p) 执行以下操作:

返回任务->normal_prio;

虽然 normal_prio() 恰好执行以下操作:

prio = MAX_RT_PRIO-1 - p->rt_priority;/* <====== 注意!*/...返回优先;

换句话说,我们有(我自己的解释):

p->prio = p->normal_prio = MAX_RT_PRIO - 1 - p->rt_priority

哇!这令人困惑!总结一下:

  • 对于 p->prio,较小的值会抢占较大的值.

  • 使用 p->rt_priority,较大的值会抢占较小的值.这是使用 sched_setscheduler() 设置的实时优先级.

In the Linux real-time process priority range 1 to 99, it's unclear to me which is the highest priority, 1 or 99.

Section 7.2.2 of "Understanding the Linux Kernel" (O'Reilly) says 1 is the highest priority, which makes sense considering that normal processes have static priorities from 100 to 139, with 100 being the highest priority:

"Every real-time process is associated with a real-time priority, which is a value ranging from 1 (highest priority) to 99 (lowest priority). "

On the other hand, the sched_setscheduler man page (RHEL 6.1) claims that 99 is the highest:

"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)."

Which is the highest real-time priority?

解决方案

I did an experiment to nail this down, as follows:

  • process1: RT priority = 40, CPU affinity = CPU 0. This process "spins" for 10 seconds so it won't let any lower-priority process run on CPU 0.

  • process2: RT priority = 39, CPU affinity = CPU 0. This process prints a message to stdout every 0.5 second, sleeping in between. It prints out the elapsed time with each message.

I'm running a 2.6.33 kernel with the PREEMPT_RT patch.

To run the experiment, I run process2 in one window (as root) and then start process1 (as root) in another window. The result is process1 appears to preempt process2, not allowing it to run for a full 10 seconds.

In a second experiment, I change process2's RT priority to 41. In this case, process2 is not preempted by process1.

This experiment shows that a larger RT priority value in sched_setscheduler() has a higher priority. This appears to contradict what Michael Foukarakis pointed out from sched.h, but actually it does not. In sched.c in the kernel source, we have:

static void
__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
{
        BUG_ON(p->se.on_rq);

        p->policy = policy;
        p->rt_priority = prio;
        p->normal_prio = normal_prio(p);
        /* we are holding p->pi_lock already */
        p->prio = rt_mutex_getprio(p);
        if (rt_prio(p->prio))
                p->sched_class = &rt_sched_class;
        else
                p->sched_class = &fair_sched_class;
        set_load_weight(p);
}

rt_mutex_getprio(p) does the following:

return task->normal_prio;

While normal_prio() happens to do the following:

prio = MAX_RT_PRIO-1 - p->rt_priority;  /* <===== notice! */
...
return prio;

In other words, we have (my own interpretation):

p->prio = p->normal_prio = MAX_RT_PRIO - 1 - p->rt_priority

Wow! That is confusing! To summarize:

  • With p->prio, a smaller value preempts a larger value.

  • With p->rt_priority, a larger value preempts a smaller value. This is the real-time priority set using sched_setscheduler().

这篇关于Linux中哪个实时优先级最高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆