linux – USER_HZ如何解决jiffy扩展问题?

据我所知,在 Linux 2.6中添加了USER_HZ常量,以解决用户空间中HZ值预期所引起的问题:在以前版本的Linux中,更改HZ值可能会导致用户空间应用程序中的值无意中缩放.

我对USER_HZ常量如何解决这个缩放问题感到困惑.例如,假设用户空间应用程序将jiffies转换为秒:

long MY_HZ = sysconf(_SC_CLK_TCK);

/* num_jiffies acquired from /proc but
 * simplified to 1000 here for clarity */
long num_jiffies = 1000;

long num_seconds = num_jiffies / MY_HZ;

由于用户空间应用程序通过sysconf调用确定HZ值,这是否会阻止扩展问题?

另一方面,如果用户空间应用程序确实将HZ值硬编码到其源中,那么USER_HZ常量如何防止缩放问题 – 用户空间应用程序将使用其硬编码常量而不是系统的USER_HZ,并不能保证硬编码的常量与USER_HZ匹配?

此外,用户空间(例如/ proc)可用的所有时钟滴答值是否已经缩放到USER_HZ?用户空间程序如何知道jiffies中的值是否缩放为HZ或USER_HZ?

解决方法

Linux Kernel Development(或 online version of the 2nd Edition)

In kernels earlier than 2.6,changing the value of HZ resulted in
user-space anomalies. This happened because values were exported to
user-space in units of ticks-per-second. As these interfaces became
permanent,applications grew to rely on a specific value of HZ.
Consequently,changing HZ would scale varIoUs exported values by
some constant—without user-space kNowing. Uptime would read 20 hours
when it was in fact two.

To prevent such problems,the kernel needs to scale all exported
jiffies values. It does this by defining USER_HZ,which is the HZ
value that user-space expects. On x86,because HZ was historically
100,USER_HZ is 100.

导出到用户空间时,每秒刻度始终会缩放到USER_HZ.

相关文章

Linux中的ARP防火墙主要用于防御ARP欺骗攻击,其效果取决于多...
insmod和modprobe加-f参数导致Invalid module format错误 这...
将ArchLinux安装到U盘 几个月前入门Arch的时候上网搜了不少安...
1、安装Apache。 1)执行如下命令,安装Apache服务及其扩展包...
一、先说一下用ansible批量采集机器信息的实现办法: 1、先把...
安装配置 1. 安装vsftpd 检查是否安装了vsftpd # rpm -qa | ...