计时器中断是否与系统处于内核模式还是用户模式无关? [英] Is timer interrupt independent of whether system is in kernel mode or user mode?

查看:144
本文介绍了计时器中断是否与系统处于内核模式还是用户模式无关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux单处理器系统中,计时器中断是否与系统处于内核模式还是用户模式无关?

In a Linux uni-processor system, is timer interrupt independent of whether system is in kernel mode or user mode?

系统处于内核模式时,定时器中断是否有不同的行为?

Is there any different behavior for the timer interrupt while system is in kernel mode?

推荐答案

简单的答案是,硬件时钟中断服务程序的执行或动态计时器处理程序的调度均不受系统所处模式的影响.在硬件时钟中断之前.原因是时钟计时器中断是立即执行的硬件中断,而不管执行是在内核还是用户上下文中进行(假设计时器中断已启用),以及时钟计时器的中断服务程序中断本身会引发运行动态计时器处理程序的软件中断.

The simple answer is that neither the execution of the hardware clock interrupt service routine, nor the scheduling of the dynamic timer handlers are affected by the mode the system was in before the hardware clock interrupt. The reason is that the the clock timer interrupt is a hardware interrupt that is serviced immediately, regardless of whether the execution is in kernel or user context (assuming that the timer interrupt is enabled that is), and the interrupt service routine for the clock timer interrupt itself raises the software interrupt that runs the dynamic timer handlers.

注意:1)我实际上还没有凭经验证明这一点. 2)这不适用于无滴答内核或高级计时器.

Caveat: 1) I haven't actually proved this empirically. 2) This does not apply to tickless kernels or highres timers.

Linux内核代码使用"timer"一词来表示不同的含义:

The Linux kernel code uses the word "timer" to mean several different things:

  1. 硬件计时器或时钟中断,使内核具有滴答声"
  2. 动态计时器-内核和驱动程序使用的软件计时器
  3. 时间间隔计时器-(用于启动和警报系统的设置程序)用于用户模式进程的软件计时器

硬件时钟或滴答计时器

在使用硬件时钟提供滴答声"的系统上,时钟计时器中断是与体系结构相关的硬件中断.例如,在 arch/中查找"timer_interrupt" powerpc/kernel/head_booke.h ,然后在timer_interrupt的实现/time.c?v=3.0"rel =" noreferrer> arch/powerpc/kernel/time.c .无论当前执行上下文如何,定时器中断发生时都会立即执行此ISR.但是,此硬件中断与其他硬件中断不同,因为它返回时,处理不会返回到先前的上下文.而是输入调度程序.

On systems that use a hardware clock to provide the "tick", the clock timer interrupt is an architecture dependent hardware interrupt. For example, look for "timer_interrupt" in arch/powerpc/kernel/head_booke.h and then see the interrupt service routine (ISR) timer_interrupt implementation in arch/powerpc/kernel/time.c. This ISR executes immediately when the timer interrupt occurs, regardless of current execution context. This hardware interrupt differs from other hardware interrupts though, in that when it returns, processing does not return to the prior context. Instead, the scheduler is entered.

对于设置为每秒产生1000个时钟中断的系统,当服务其他中断时,时钟中断有时会被掩盖.这通常被称为丢失的滴答声"问题.如果不补偿丢失的滴答声,则已加载的系统的时间感可能会变慢.在某些体系结构上,内核通过使用更细粒度的硬件增量计数器来补偿丢失的滴答声,该计数器在每个时钟计时器中断时都会读取并记录其值.通过将当前滴答的增量计数器值与前一个滴答的增量计数器值进行比较,内核可以判断滴答是否丢失.

For a system that is set to produce 1000 clock interrupts per second, there is a chance that clock interrupts will sometimes be masked when other interrupts are being serviced. This is usually called the "lost ticks" problem. Without compensating for lost ticks, a loaded system could have a slowed sense of time. On some architectures the kernel compensates for lost ticks by using a finer grained hardware increment counter, whose value is read and recorded every clock timer interrupt. By comparing the increment counter value of the current tick against the increment counter value of the previous tick, the kernel can tell if a tick has been lost.

软件计时器

已到期的动态计时器的动态计时器处理程序列表(使用linux/timer.h设置的类型)在时钟计时器中断结束之前设置,然后返回.顺序是(大约):

The list of dynamic timer handlers (the type you set with the linux/timer.h) of dynamic timers that have expired is set at the end of the clock timer interrupt, before it returns. The sequence is (approximately):

[arch dependent]:timer_interrupt( )
kernel/time/tick-common.c:tick_handle_periodic( )
kernel/time/tick-common.c:tick_periodic( )
kernel/timer.c:update_process_times( )
kernel/timer.c:run_local_timers( )
kernel/softirq.c:raise_softirq(TIMER_SOFTIRQ)

我已经省略了将timer_interrupt的处理程序设置为tick_handle_periodic的处理程序以及将TIMER_SOFTIRQ的处理程序设置的初始化过程.

I have omitted the initialilzations that set the handler for the timer_interrupt to tick_handle_periodic, and the handler for TIMER_SOFTIRQ.

raise_softirq(TIMER_SOFTIRQ)的调用会生成一个软件中断,该中断将立即得到服务.中断的ISR运行动态计时器队列.计时器处理程序在softirq上下文中运行,并启用了硬件中断. ISR返回时,将调用调度程序.这意味着,如果设置了很多计时器,则运行队列中接下来发生的任何过程都会被延迟.

The call to raise_softirq(TIMER_SOFTIRQ) generates a software interrupt that is serviced immediately. The ISR for the interrupt runs the dynamic timer queue. The timer handlers run in softirq context, with hardware interrupts enabled. When the ISR returns, the scheduler is called. This means that if there are a lot of timers set, whatever process happens to be next in the run queue will be delayed.

如果丢失了滴答声,则可以延迟计时器处理程序的执行,但是,延迟不取决于运行时钟计时器中断之前执行的条件.

If there were lost ticks, then the execution of the timer handlers could be delayed, however, the delay does not depend on the contect of execution prior to running the clock timer interrupt.

有关动态计时器准确性的说明

"...内核无法确保计时器功能将在其到期时间立即启动.它只能确保计时器功能在适当的时间或之后执行,并延迟长达几百毫秒. 了解Linux内核,Bovet和Cesati,第三版,O'reilly.

"...the kernel cannot ensure that timer functions will start right at their expiration times. It can only ensure that they are executed either at the proper time or after with a delay of up to a few hundred milliseconds." Understanding the Linux Kernel, Bovet and Cesati, 3rd edition, O'reilly.

因此,如果您需要更好的计时器精度,则需要使用高分辨率计时器.

So, if you need better timer accuracy, you need to use highres timers.

参考文献:软件中断和实时

这篇关于计时器中断是否与系统处于内核模式还是用户模式无关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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