关于Linux的 High-Resolution Timer

本文介绍了如何在Oracle Linux中禁用高分辨率定时器。该功能默认开启,允许内核在纳秒级别处理时间分片。禁用后,内核将在毫秒级别处理时间分片。除非应用程序需要纳秒级别的精度,否则通常不会有负面影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考自:
Oracle Linux: How to Disable High-Resolution Timer (文档 ID 2207818.1)

High-Resolution Timer的作用是在纳秒级别处理内部时间分片。
若是禁用High-Resolution Timer,那么内核是在毫秒级别处理内部时间分片。

禁用掉High-Resolution Timer不会有问题,除非你的应用程序需要在纳秒级别处理时间分片。


如下摘自:Oracle Linux: How to Disable High-Resolution Timer (文档 ID 2207818.1)

High-resolution timer is implemented on UEK[1234] and RHCK for OL[67]. 
Note that RHCK for OL5 does not have this feature. 
By default, highres=on as it is configured in kernel booting procedures.
Linux kernel can handle internal time-slice within nanosecond with enabling high-resolution timer. 
By disabling this feature, kernel will handle the slice within millisecond,
it would not be any issues unless your applications need internal time-slice should be nanosecond order.
 
After rebooting the system, it can be confirmed whether highres is disabled by checking whether "highres=off" is included in /proc/cmdline. 
Note that kernel does not prepare methods/interfaces checking high-resolution timer is enabled/disabled, 
thus it is necessary to confirm it by checking the file.

相关参考文章http://blog.chinaunix.net/uid-23504396-id-3787297.html

High Resolution Timer(HRT)是Linux内核提供的一种高精度计时器,可以提供纳秒级别的计时精度。下面是HRT的使用步骤: 1. 定义一个hrtimer结构体变量,该结构体变量用于表示一个HRT计时器。 2. 使用hrtimer_init()函数来初始化hrtimer结构体变量,设置HRT计时器的回调函数以及计时器的模式(相对或绝对时间)。 3. 使用hrtimer_start()函数来启动HRT计时器,设置计时器的超时时间。 4. 在HRT计时器超时时,内核会自动调用设置的回调函数。 5. 在回调函数中,可以进行需要高精度计时的操作,例如测量函数执行时间、执行周期性任务等。 6. 若需要取消已经启动的HRT计时器,可以使用hrtimer_cancel()函数来取消计时器。 下面是一个简单的HRT计时器示例代码,用于测量函数执行时间: ``` #include <linux/hrtimer.h> #include <linux/ktime.h> static struct hrtimer my_hrtimer; static ktime_t start_time; enum hrtimer_restart my_hrtimer_callback(struct hrtimer *timer) { ktime_t end_time = ktime_get(); printk(KERN_INFO "Function execution time: %lld ns\n", ktime_to_ns(ktime_sub(end_time, start_time))); return HRTIMER_NORESTART; } void my_function() { start_time = ktime_get(); hrtimer_init(&my_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); my_hrtimer.function = my_hrtimer_callback; hrtimer_start(&my_hrtimer, ktime_set(1, 0), HRTIMER_MODE_REL); // do something here hrtimer_cancel(&my_hrtimer); } ``` 在上述代码中,my_function()函数调用时会启动一个HRT计时器,并在计时器超时时自动调用my_hrtimer_callback()函数,该函数会输出函数执行时间。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值