nice(NI), priority(PR), realtime priority(RT)的算术关系( 三 )

举个例子:如果一个 normal process 的 nice (NI) 是 0,那么他在 kernel 角度的 process priority 就是:((nice) + DEFAULT_PRIO) = (0 + (MAX_RT_PRIO + NICE_WIDTH / 2)) = (0 + 100 + 40 / 2) = 120如果 nice = -20,则 normal_prio = 100。如果 nice = 19,则 normal_prio = 139。2. 如果是 realtime process, 从 kernel 的角度看 p-\u0026gt;prio (PR) 就等于 p-\u0026gt;rt_priority。范围就是 。三、ps/top 等命令的原理1. 对于 ps -el 为什么 nice (NI) 是 0,而 PR (priority) 显示的是 80 而不是 120。可能 ps 中,如果是 normal process 就显示 60 + p-\u0026gt;prio - MAX_RT_PRIO,如果是 realtime process 则只显示 rt。我们可以从代码中验证://////////////////////////////////////////////////////////////////////////////////////// "PRI" is created by "opri", or by "pri" when -c is used.//// Unix98 only specifies that a high "PRI" is low priority.// Sun and SCO add the -c behavior. Sun defines "pri" and "opri".// Linux may use "priority" for historical purposes.//// According to the kernel\u0026#39;s fs/proc/array.c and kernel/sched.c source,// the kernel reports it in /proc via this:// p-\u0026gt;prio - MAX_RT_PRIO// such that "RT tasks are offset by -200. Normal tasks are centered// around 0, value goes from -16 to +15" but who knows if that is// before or after the conversion...//// \u0026lt;linux/sched.h\u0026gt; says:// MAX_RT_PRIO is currently 100. (so we see 0 in /proc)// RT tasks have a p-\u0026gt;prio of 0 to 99. (so we see -100 to -1)// non-RT tasks are from 100 to 139. (so we see 0 to 39)// Lower values have higher priority, as in the UNIX standard.//// In any case, pp-\u0026gt;priority+100 should get us back to what the kernel// has for p-\u0026gt;prio.//// Test results with the "yes" program on a 2.6.x kernel://// # ps -C19,_20 -o pri,opri,intpri,priority,ni,pcpu,pid,comm// PRI PRI PRI PRI NI %CPU PID COMMAND// 0 99 99 39 19 10.6 8686 19// 34 65 65 5 -20 94.7 8687 _20//// Grrr. So the UNIX standard "PRI" must NOT be from "pri".// Either of the others will do. We use "opri" for this.// (and use "pri" when the "-c" option is used)// Probably we should have Linux-specific "pri_for_l" and "pri_for_lc"//// sched_get_priority_min.2 says the Linux static priority is// 1..99 for RT and 0 for other... maybe 100 is kernel-only?//// A nice range would be -99..0 for RT and 1..40 for normal,// which is pp-\u0026gt;priority+1. (3-digit max, positive is normal,// negative or 0 is RT, and meets the standard for PRI)//// legal as UNIX "PRI"// "priority" (was -20..20, now -100..39)static int pr_priority(char *restrict const outbuf, const proc_t *restrict const pp){ /* -20..20 */ return snprintf(outbuf, COLWID, "%ld", pp-\u0026gt;priority);}// legal as UNIX "PRI"// "intpri" and "opri" (was 39..79, now -40..99)static int pr_opri(char *restrict const outbuf, const proc_t *restrict const pp){ /* 39..79 */ return snprintf(outbuf, COLWID, "%ld", 60 + pp-\u0026gt;priority);}// legal as UNIX "PRI"// "pri_foo" -- match up w/ nice values of sleeping processes (-120..19)static int pr_pri_foo(char *restrict const outbuf, const proc_t *restrict const pp){ return snprintf(outbuf, COLWID, "%ld", pp-\u0026gt;priority - 20);}// legal as UNIX "PRI"// "pri_bar" -- makes RT pri show as negative (-99..40)static int pr_pri_bar(char *restrict const outbuf, const proc_t *restrict const pp){ return snprintf(outbuf, COLWID, "%ld", pp-\u0026gt;priority + 1);}// legal as UNIX "PRI"// "pri_baz" -- the kernel\u0026#39;s -\u0026gt;prio value, as of Linux 2.6.8 (1..140)static int pr_pri_baz(char *restrict const outbuf, const proc_t *restrict const pp){ return snprintf(outbuf, COLWID, "%ld", pp-\u0026gt;priority + 100);}// not legal as UNIX "PRI"// "pri" (was 20..60, now 0..139)static int pr_pri(char *restrict const outbuf, const proc_t *restrict const pp){ /* 20..60 */ return snprintf(outbuf, COLWID, "%ld", 39 - pp-\u0026gt;priority);}// not legal as UNIX "PRI"// "pri_api" -- match up w/ RT API (-40..99)static int pr_pri_api(char *restrict const outbuf, const proc_t *restrict const pp){ return snprintf(outbuf, COLWID, "%ld", -1 - pp-\u0026gt;priority);}static int pr_nice(char *restrict const outbuf, const proc_t *restrict const pp){ if(pp-\u0026gt;sched!=0 \u0026amp;\u0026amp; pp-\u0026gt;sched!=-1) return snprintf(outbuf, COLWID, "-"); return snprintf(outbuf, COLWID, "%ld", pp-\u0026gt;nice);}


推荐阅读