简体   繁体   English

linux kernel:了解 jiffies diff 是如何计算的

[英]linux kernel: understand how jiffies diff is calculated

I came across the following code in the kernel:我在 kernel 中遇到了以下代码:

/*
 * Have the 32 bit jiffies value wrap 5 minutes after boot
 * so jiffies wrap bugs show up earlier.
 */             
#define INITIAL_JIFFIES ((unsigned long)(unsigned int) (-300*HZ))

static inline u32 cstamp_delta(unsigned long cstamp)
{
        return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
}

where cstamp value is in jiffies .其中cstamp值以jiffies为单位。

This is the code from net/ipv4/devinet.c where IP address per interface is implemented (among other things).这是来自net/ipv4/devinet.c的代码,其中实现了每个接口的 IP 地址(除其他外)。

I see that INITIAL_JIFFIES macro takes the value of 5 minutes (300) and converts it in jiffies ( -300*HZ ), and typecasting ensures correct value wrapping.我看到INITIAL_JIFFIES宏采用 5 分钟 (300) 的值并将其转换为jiffies ( -300*HZ ),并且类型转换确保正确的值包装。

But why is it explicitly set to negative value ( -300*HZ )?但为什么它明确设置为负值( -300*HZ )?

I'm not sure, in what units cstamp_delta() returns?我不确定, cstamp_delta()以什么单位返回?

why is it explicitly set to negative value ( -300*HZ )?为什么它显式设置为负值( -300*HZ )?

It's not a negative value.这不是负值。 The value is casted to unsigned int , so it's positive, and more precisely exactly UINT_MAX - 300*HZ , which means that the final value will be 5 minutes before the maximum unsigned 32bit integer value is reached.该值被转换为unsigned int ,因此它是正数,更准确地说是UINT_MAX - 300*HZ ,这意味着最终值将在达到最大无符号 32 位 integer 值之前 5 分钟。 This is, as the comment states, to detect errors with parts of the code incorrectly handling jiffies as a 32bit value.正如评论所述,这是为了检测部分代码错误地将jiffies处理为 32 位值的错误。

I'm not sure, in what units cstamp_delta() returns?我不确定, cstamp_delta()以什么单位返回?

Well, cstamp - INITIAL_JIFFIES just calculates the total number of ticks from kernel boot time.好吧, cstamp - INITIAL_JIFFIES只是从 kernel 启动时间计算总滴答数。 Dividing this value by HZ returns the total number of seconds from boot time.将此值除以HZ会返回从引导时间开始的总秒数 Since the value is multiplied by 100 first though, the final result is the total number of hundredths of second elapsed from boot time.由于该值首先乘以 100,因此最终结果是从启动时间经过的百分之一秒的总数。

Since u32 is used as return type, the value returned by this function is of course going to wrap around relatively "soon", in about 1 year and 4 months of runtime (2^32/100/60/60/24/356).由于使用 u32 作为返回类型,因此此u32返回的值当然会相对“很快”环绕,在大约 1 年零 4 个月的运行时间(2^32/100/60/60/24/356) .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM