问题描述
如何在 Linux 中手动将 jiffies 转换为毫秒,反之亦然?我知道内核 2.6 对此有一个函数,但我正在研究 2.4(作业),尽管我查看了代码,但它使用了很多宏常量,我不知道它们是否在 2.4 中定义.
How do I manually convert jiffies to milliseconds and vice versa in Linux? I know kernel 2.6 has a function for this, but I'm working on 2.4 (homework) and though I looked at the code it uses lots of macro constants which I have no idea if they're defined in 2.4.
推荐答案
正如之前的回答所说,jiffies
递增的速率是固定的.
As a previous answer said, the rate at which jiffies
increments is fixed.
为接受 jiffies
的函数指定时间的标准方法是使用常量 HZ
.
The standard way of specifying time for a function that accepts jiffies
is using the constant HZ
.
那是赫兹的缩写,即每秒的滴答数.在定时器刻度设置为 1ms 的系统上,HZ=1000.某些发行版或架构可能会使用其他数字(过去通常为 100).
That's the abbreviation for Hertz, or the number of ticks per second. On a system with a timer tick set to 1ms, HZ=1000. Some distributions or architectures may use another number (100 used to be common).
为函数指定 jiffies
计数的标准方法是使用 HZ
,如下所示:
The standard way of specifying a jiffies
count for a function is using HZ
, like this:
schedule_timeout(HZ / 10); /* Timeout after 1/10 second */
在大多数简单的情况下,这可以正常工作.
In most simple cases, this works fine.
2*HZ /* 2 seconds in jiffies */
HZ /* 1 second in jiffies */
foo * HZ /* foo seconds in jiffies */
HZ/10 /* 100 milliseconds in jiffies */
HZ/100 /* 10 milliseconds in jiffies */
bar*HZ/1000 /* bar milliseconds in jiffies */
最后两个有一点问题,但是,在具有 10 ms 计时器滴答的系统上,HZ/100
为 1,精度开始受到影响.您可能会在 0.0001 和 1.999 计时器滴答之间的任何地方延迟(本质上是 0-2 毫秒).如果您尝试在 10ms 滴答系统上使用 HZ/200
,整数除法会给您 0 jiffies!
Those last two have a bit of a problem, however, as on a system with a 10 ms timer tick, HZ/100
is 1, and the precision starts to suffer. You may get a delay anywhere between 0.0001 and 1.999 timer ticks (0-2 ms, essentially). If you tried to use HZ/200
on a 10ms tick system, the integer division gives you 0 jiffies!
所以经验法则是,对微小值(接近 1 jiffie 的值)使用 HZ 时要非常小心.
So the rule of thumb is, be very careful using HZ for tiny values (those approaching 1 jiffie).
要以另一种方式转换,您可以使用:
To convert the other way, you would use:
jiffies / HZ /* jiffies to seconds */
jiffies * 1000 / HZ /* jiffies to milliseconds */
您不应该期望比毫秒精度更好的东西.
You shouldn't expect anything better than millisecond precision.
这篇关于将 jiffies 转换为毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!