linux - 计算机系统中的计时机制

标签 linux time operating-system hardware

我已阅读 Measure time in Linux - getrusage vs clock_gettime vs clock vs gettimeofday? 上的此链接它提供了 C 中可用的计时函数的详分割类

但是,我对操作系统/硬件如何维护不同的“时间”概念感到非常困惑。

这是来自 the Linux man pages 的引用,

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 docs表明RTC和系统时钟在启动后是独立的。我的问题是:

  1. 是什么导致了系统时钟递增的中断???
  2. 如果wall time = 使用系统时钟的时间间隔,那么进程时间取决于什么?
  3. 这一切都与 CPU 频率有关吗?或者说这是一个完全正交的计时业务

最佳答案

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

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

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

可能,如果某个进程正在制作一个时间很短的计时器,例如10ms,内核会将定时器中断的频率提高到100Hz

在最近的内核上,您可能需要

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

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

内核可能使用类似 -for PC 的硬件设备笔记本电脑和台式机-片上HPET (或 TSC )比旧的 battery saved RTC 好得多定时器。当然,细节是特定于硬件的。因此,在基于 ARM 的 Linux 系统(例如 Android 智能手机)上,情况有所不同。

关于linux - 计算机系统中的计时机制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25330198/

相关文章:

linux - 使用 sh 命令运行时替换 myscript.sh 文件中的变量值

time - Lua 将格式 "X h X min"转换为 WoW 中的秒

java - 如何在 java 中打开 .txt 的应用程序(无论操作系统如何)?

linux - ubuntu 13.04 和 g++

python - 为什么在 IAM 设置为允许所有时 AWS 拒绝许可?

date - Unix 纪元时间在闰秒时钟上的表现如何?

time - 为什么GMT是计算机时间而不是UTC的标准?

javascript - 解构/破解 NodeJs

c++ - 尝试使用 C++ 中的锁进行捕获

linux - 是否可以写入在同一个 PCIe 插槽上使用不同 PCIe channel 的多个设备?