关于linux:计算机系统中的计时机制

timing mechanisms in computer systems

我已经阅读了关于Linux中测量时间的这个链接 - getrusage vs clock_gettime vs clock vs gettimeofday? 它提供了C中可用的定时功能的大量细分

然而,我很困惑OS /硬件如何维护"时间"的不同概念。

这是Linux手册页的引用,

RTCs should not be confused with the system clock, which is a
software clock maintained by the kernel and used to implement
gettimeofday(2) and time(2), as well as setting timestamps on files,
and so on. The system clock reports seconds and microseconds since a
start point, defined to be the POSIX Epoch: 1970-01-01 00:00:00 +0000
(UTC). (One common implementation counts timer interrupts, once per
"jiffy", at a frequency of 100, 250, or 1000 Hz.) That is, it is
supposed to report wall clock time, which RTCs also do.

A key difference between an RTC and the system clock is that RTCs run
even when the system is in a low power state (including"off"), and
the system clock can't. Until it is initialized, the system clock
can only report time since system boot ... not since the POSIX Epoch.
So at boot time, and after resuming from a system low power state,
the system clock will often be set to the current wall clock time
using an RTC. Systems without an RTC need to set the system clock
using another clock, maybe across the network or by entering that
data manually.

Arch Linux文档表明RTC和系统时钟在启动后是独立的。 我的问题是:

  • 是什么导致中断增加系统时钟???
  • 如果壁时间=使用系统时钟的时间间隔,则处理时间取决于什么?
  • 这些都与CPU频率有关吗? 或者这是一个完全正交的计时业务

  • 在Linux上,从应用程序的角度来看,time(7)手册页给出了很好的解释。

    Linux还提供(linux特定的)timerfd_create(2)和相关的系统调用。

    您不应该关心中断(它们是内核的业务,并且是动态配置的,例如通过应用程序定时器-timer_create(2),poll(2)和许多其他系统调用 - 以及调度程序),但仅涉及应用程序可见时间相关系统调用。

    也许,如果某个过程正在制作一个具有很短周期的定时器,例如10ms,内核会将定时器中断的频率增加到100Hz

    在最近的内核上,你可能想要

    1
    2
    3
    4
    CONFIG_HIGH_RES_TIMERS=y
    CONFIG_TIMERFD=y
    CONFIG_HPET_TIMER=y
    CONFIG_PREEMPT=y

    内核的.config文件中的选项。

    顺便说一下,你可以用10秒间隔做cat /proc/interrupts两次。在我的笔记本电脑上有一个自制的3.16内核 - 主要是空闲进程,但是firefox浏览器和emacs,我每秒钟有25个中断。同时尝试cat /proc/timer_listcat /proc/timer_stats

    另请参见最近(例如3.16)Linux内核树的Documentation / timers /目录。

    内核可能使用硬件设备,如PC笔记本电脑和台式机片上HPET(或TSC),它们比旧电池节省的RTC定时器要好得多。当然,细节是特定于硬件的。因此,在基于ARM的Linux系统(例如您的Android智能手机)上,它是不同的。