2.2.5 核心概念 - 优先级
简要介绍任务的优先级,即Priority
struct dl_rq { /* runqueue is an rbtree, ordered by deadline */ struct rb_root_cached root; /* 其他字段此处被删除 */ }
Last updated
/* file: include/linux/sched.h */
struct task_struct {
int prio;
int static_prio;
int normal_prio;
unsigned int rt_priority;#define MAX_NICE 19
#define MIN_NICE -20
#define NICE_WIDTH (MAX_NICE - MIN_NICE + 1)
#define DEFAULT_PRIO (MAX_RT_PRIO + NICE_WIDTH / 2)
/* nice 与静态优先级相互转换的宏 */
#define NICE_TO_PRIO(nice) ((nice) + DEFAULT_PRIO)
#define PRIO_TO_NICE(prio) ((prio)-DEFAULT_PRIO)static inline int __normal_prio(struct task_struct *p) {
return p->static_prio;
}
static inline int normal_prio(struct task_struct *p) {
int prio;
if (task_has_dl_policy(p))
/* MAX_DL_PRIO为0, 因此Deadline的优先级永远为-1 */
prio = MAX_DL_PRIO - 1;
else if (task_has_rt_policy(p))
/* MAX_RT_PRIO为100, 而rt_priority的范围是[1,99]且数字越大对应的优先级越高,下面的算法实现了优先级反转,高优先级将对应小的数字。
*/
prio = MAX_RT_PRIO - 1 - p->rt_priority;
else
/* 对于普通进程,直接返回静态优先级static_prio */
prio = __normal_prio(p);
return prio;
}static int effective_prio(struct task_struct *p) {
p->normal_prio = normal_prio(p);
/* 如果 p.prio 的值小于 100(MAX_RT_PRIO的值), 则返回 1, 否则返回 0 */
if (!rt_prio(p->prio))
return p->normal_prio;
return p->prio;
}