我知道 jiffies 长度是在内核编译时选择的,默认为 250(4ms)。
来源:man 7 time - 软件时钟、HZ 和 Jiffies
我想知道瞬间会发生什么。utime
或stime
in的值增加的条件是什么/proc/pid/stat
?什么时候发生?
我有一些想法,但我不确定它们是否正确:
- 当进程有时间运行时,jiffy 计数会立即增加。
- Linux 无法知道 1 jiffy 内执行了多少个操作。
- 此外,Linux 无法得知当前进程在 1 jiffy 内使用了多少时间。 (它是否用完了全部 4 毫秒,或者更少?)
- 当前进程在瞬间开始,并且不会稍后开始。
- 当一个进程完成并且从当前 jiffy 开始还有剩余时间时,什么也不会发生(nop)。
- 如果出现更高优先级的进程,不会影响当前的 jiffy。
理解这些会有很大帮助。
答案1
当 jiffy 计数增加时,进程就有时间运行。 jiffies 由计时器中断增加,告诉调度程序重新调度。 jiffy 定义了进程在不重新调度的情况下运行的最长时间段。例如,如果进程调用yield()
或,则立即进行重新调度。sleep()
因此,到下一个可用运行进程的上下文切换不一定发生在 jiffy 边界处。
然而,实际的内核行为是在内核编译时定义的,linux/kernel/Kconfig.preempt:
choice
prompt "Preemption Model"
default PREEMPT_NONE
config PREEMPT_NONE
bool "No Forced Preemption (Server)"
help
This is the traditional Linux preemption model, geared towards
throughput. It will still provide good latencies most of the
time, but there are no guarantees and occasional longer delays
are possible.
Select this option if you are building a kernel for a server or
scientific/computation system, or if you want to maximize the
raw processing power of the kernel, irrespective of scheduling
latencies.
config PREEMPT_VOLUNTARY
bool "Voluntary Kernel Preemption (Desktop)"
help
This option reduces the latency of the kernel by adding more
"explicit preemption points" to the kernel code. These new
preemption points have been selected to reduce the maximum
latency of rescheduling, providing faster application reactions,
at the cost of slightly lower throughput.
This allows reaction to interactive events by allowing a
low priority process to voluntarily preempt itself even if it
is in kernel mode executing a system call. This allows
applications to run more 'smoothly' even when the system is
under load.
Select this if you are building a kernel for a desktop system.
config PREEMPT
bool "Preemptible Kernel (Low-Latency Desktop)"
select PREEMPT_COUNT
select UNINLINE_SPIN_UNLOCK if !ARCH_INLINE_SPIN_UNLOCK
help
This option reduces the latency of the kernel by making
all kernel code (that is not executing in a critical section)
preemptible. This allows reaction to interactive events by
permitting a low priority process to be preempted involuntarily
even if it is in kernel mode executing a system call and would
otherwise not be about to reach a natural preemption point.
This allows applications to run more 'smoothly' even when the
system is under load, at the cost of slightly lower throughput
and a slight runtime overhead to kernel code.
Select this if you are building a kernel for a desktop or
embedded system with latency requirements in the milliseconds
range.
endchoice
是的,Linux 无法判断 1 jiffy 内执行了多少个操作,因为不同的指令执行所需的时间不同,而且指令的流水线会影响单位时间内执行的指令数。