Linux 将 jiffies 转换为毫秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2731463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting jiffies to milli seconds
提问by EpsilonVector
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.
如何在 Linux 中手动将 jiffies 转换为毫秒,反之亦然?我知道内核 2.6 对此有一个函数,但我正在研究 2.4(作业),尽管我查看了代码,但它使用了很多宏常量,我不知道它们是否在 2.4 中定义。
采纳答案by Eric Seppanen
As a previous answer said, the rate at which jiffies
increments is fixed.
正如之前的答案所说,jiffies
增量的速率是固定的。
The standard way of specifying time for a function that accepts jiffies
is using the constant HZ
.
为接受的函数指定时间的标准方法jiffies
是使用常量HZ
。
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).
那是赫兹的缩写,或每秒滴答数。在定时器刻度设置为 1ms 的系统上,HZ=1000。某些发行版或体系结构可能使用另一个数字(过去通常为 100)。
The standard way of specifying a jiffies
count for a function is using HZ
, like this:
jiffies
为函数指定计数的标准方法是使用HZ
,如下所示:
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 */
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!
然而,最后两个有一些问题,因为在一个具有 10 ms 计时器滴答的系统上,它HZ/100
是 1,并且精度开始受到影响。您可能会在 0.0001 和 1.999 计时器滴答之间的任何地方延迟(本质上是 0-2 毫秒)。如果您尝试HZ/200
在 10ms 滴答系统上使用,整数除法会给您 0 jiffies!
So the rule of thumb is, be very careful using HZ for tiny values (those approaching 1 jiffie).
所以经验法则是,对微小值(接近 1 jiffie 的值)使用 HZ 时要非常小心。
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.
您不应该期望比毫秒精度更好的东西。
回答by indiv
Jiffies are hard-coded in Linux 2.4. Check the definition of HZ
, which is defined in the architecture-specific param.h
. It's often 100 Hz, which is one tick every (1 sec/100 ticks * 1000 ms/sec) 10 ms.
Jiffies 在 Linux 2.4 中是硬编码的。检查 的定义HZ
,该定义在特定于体系结构的 中定义param.h
。它通常是 100 Hz,即每(1 秒/100 滴答声 * 1000 毫秒/秒)10 毫秒一个滴答声。
This holds true for i386, and HZ is defined in include/asm-i386/param.h
.
这适用于 i386,HZ 定义在include/asm-i386/param.h
.
There are functions in include/linux/time.h
called timespec_to_jiffies
and jiffies_to_timespec
where you can convert back and forth between a struct timespec
and jiffies
:
有函数include/linux/time.h
称为timespec_to_jiffies
和jiffies_to_timespec
在那里你可以来回转换之间struct timespec
和jiffies
:
#define MAX_JIFFY_OFFSET ((~0UL >> 1)-1)
static __inline__ unsigned long
timespec_to_jiffies(struct timespec *value)
{
unsigned long sec = value->tv_sec;
long nsec = value->tv_nsec;
if (sec >= (MAX_JIFFY_OFFSET / HZ))
return MAX_JIFFY_OFFSET;
nsec += 1000000000L / HZ - 1;
nsec /= 1000000000L / HZ;
return HZ * sec + nsec;
}
static __inline__ void
jiffies_to_timespec(unsigned long jiffies, struct timespec *value)
{
value->tv_nsec = (jiffies % HZ) * (1000000000L / HZ);
value->tv_sec = jiffies / HZ;
}
Note:I checked this info in version 2.4.22.
注意:我在 2.4.22 版中检查了此信息。
回答by Anthony Giorgio
I found this sample code on kernelnewbies. Make sure you link with -lrt
我在kernelnewbies上找到了这个示例代码。确保您链接到-lrt
#include <unistd.h>
#include <time.h>
#include <stdio.h>
int main()
{
struct timespec res;
double resolution;
printf("UserHZ %ld\n", sysconf(_SC_CLK_TCK));
clock_getres(CLOCK_REALTIME, &res);
resolution = res.tv_sec + (((double)res.tv_nsec)/1.0e9);
printf("SystemHZ %ld\n", (unsigned long)(1/resolution + 0.5));
return 0;
}
回答by P. Cahyna
To obtain the USER_HZ value (see the comments under the accepted answer) using CLI:
要使用 CLI 获取 USER_HZ 值(请参阅已接受答案下的评论):
getconf CLK_TCK