免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2648 | 回复: 7
打印 上一主题 下一主题

[时钟管理] 请教posix定时器的精度问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-03-28 11:09 |只看该作者 |倒序浏览
请问下,如果我在用户层使用posix定时器,这个定时器接口的精度是微妙级别的,
但Linux系统内核的tick一般都是毫秒级别的,这意味这调度是毫秒级别的,那么
理论上用户空间的定时器是达不到微妙级别的吧,请问我的理解对吗?如果想在
用户空间达到微妙级别的精度有什么办法呢?

论坛徽章:
20
程序设计版块每日发帖之星
日期:2015-08-17 06:20:00程序设计版块每日发帖之星
日期:2016-07-16 06:20:00程序设计版块每日发帖之星
日期:2016-07-18 06:20:00每日论坛发贴之星
日期:2016-07-18 06:20:00黑曼巴
日期:2016-12-26 16:00:3215-16赛季CBA联赛之江苏
日期:2017-06-26 11:05:5615-16赛季CBA联赛之上海
日期:2017-07-21 18:12:5015-16赛季CBA联赛之青岛
日期:2017-09-04 17:32:0515-16赛季CBA联赛之吉林
日期:2018-03-26 10:02:16程序设计版块每日发帖之星
日期:2016-07-15 06:20:0015-16赛季CBA联赛之江苏
日期:2016-07-07 18:37:512015亚冠之萨济拖拉机
日期:2015-08-17 12:21:08
2 [报告]
发表于 2016-03-29 11:59 |只看该作者
也不绝对,关键看系统里有哪些clocksource。

比如HPET,https://en.wikipedia.org/wiki/High_Precision_Event_Timer

论坛徽章:
0
3 [报告]
发表于 2016-03-29 21:25 |只看该作者
我的意识是就算底层的时钟源再精准(比如HEPT),但时钟中断后需要通知进程处理,但进程的调度是tick(毫秒)级别的,受限于tick调度,这样不管底层的时钟源再精准,
进程的timer都达不到微妙级别。

论坛徽章:
13
15-16赛季CBA联赛之八一
日期:2016-07-08 21:00:1415-16赛季CBA联赛之同曦
日期:2017-02-15 14:26:1515-16赛季CBA联赛之佛山
日期:2017-02-20 14:19:2615-16赛季CBA联赛之青岛
日期:2017-05-07 16:49:1115-16赛季CBA联赛之广夏
日期:2017-07-30 09:13:1215-16赛季CBA联赛之广东
日期:2018-07-05 22:34:3615-16赛季CBA联赛之江苏
日期:2018-09-03 12:10:2115-16赛季CBA联赛之上海
日期:2018-09-25 03:49:2215-16赛季CBA联赛之广东
日期:2018-09-25 04:09:12
4 [报告]
发表于 2016-03-29 21:52 |只看该作者
本帖最后由 _nosay 于 2016-03-29 21:57 编辑

回复 3# ppcc800330

哦,是说用户态实现的定时器呀。

论坛徽章:
2
天蝎座
日期:2014-07-23 21:17:122015年辞旧岁徽章
日期:2015-03-03 16:54:15
5 [报告]
发表于 2016-03-30 10:22 |只看该作者
usleep和msleep,我记得都是通过内核的 nanosleep 来实现的。

最精确的:直接去读cpu的counter寄存器,跟cpu的时钟频率做换算。

论坛徽章:
20
程序设计版块每日发帖之星
日期:2015-08-17 06:20:00程序设计版块每日发帖之星
日期:2016-07-16 06:20:00程序设计版块每日发帖之星
日期:2016-07-18 06:20:00每日论坛发贴之星
日期:2016-07-18 06:20:00黑曼巴
日期:2016-12-26 16:00:3215-16赛季CBA联赛之江苏
日期:2017-06-26 11:05:5615-16赛季CBA联赛之上海
日期:2017-07-21 18:12:5015-16赛季CBA联赛之青岛
日期:2017-09-04 17:32:0515-16赛季CBA联赛之吉林
日期:2018-03-26 10:02:16程序设计版块每日发帖之星
日期:2016-07-15 06:20:0015-16赛季CBA联赛之江苏
日期:2016-07-07 18:37:512015亚冠之萨济拖拉机
日期:2015-08-17 12:21:08
6 [报告]
发表于 2016-03-30 11:01 |只看该作者
本帖最后由 nswcfd 于 2016-03-30 11:02 编辑

仅供参考。

两台机器上man nanosleep的区别。

版本1,Linux 2.6.9 @ 2004-10-24,注意BUGS部分。
NAME
       nanosleep - pause execution for a specified time

SYNOPSIS
       #define _POSIX_C_SOURCE 199309 #include <time.h>

       int nanosleep(const struct timespec *req, struct timespec *rem);
BUGS
       The current implementation of nanosleep() is based on the normal kernel timer mechanism, which has a resolution of 1/HZ s (see time(7)).  Therefore, nanosleep() pauses always for  at
       least  the specified time, however it can take up to 10 ms longer than specified until the process becomes runnable again. For the same reason, the value returned in case of a deliv-
       ered signal in *rem is usually rounded to the next larger multiple of 1/HZ s.

   Old behaviour
       In order to support applications requiring much more precise pauses (e.g., in order to control some time-critical hardware), nanosleep() would handle pauses of up  to  2 ms  by  busy
       waiting  with  microsecond  precision when called from a process scheduled under a real-time policy like SCHED_FIFO or SCHED_RR.  This special extension was removed in kernel 2.5.39,
       hence is still present in current 2.4 kernels, but not in 2.6 kernels.

       In Linux 2.4, if nanosleep() is stopped by a signal (e.g., SIGTSTP), then the call fails with the error EINTR after the process is resumed by a SIGCONT signal.  If the system call is
       subsequently restarted, then the time that the process spent in the stopped state is not counted against the sleep interval.

CONFORMING TO
       POSIX.1-2001.

SEE ALSO
       sched_setscheduler(2), timer_create(2), sleep(3), usleep(3)

Linux 2.6.9                       2004-10-24                      NANOSLEEP(2)


版本2,2009-01-19,3.22,没有前面的BUG描述(受限于timer精度)。
NAME
       nanosleep - high-resolution sleep

SYNOPSIS
       #include <time.h>

       int nanosleep(const struct timespec *req, struct timespec *rem);

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       nanosleep(): _POSIX_C_SOURCE >= 199309L

CONFORMING TO
       POSIX.1-2001.

NOTES
       If the interval specified in req is not an exact multiple of the granularity underlying clock (see time(7)), then the interval will be rounded up to the next multiple.  Furthermore, after  the  sleep  com-
       pletes, there may still be a delay before the CPU becomes free to once again execute the calling thread.

       The  fact that nanosleep() sleeps for a relative interval can be problematic if the call is repeatedly restarted after being interrupted by signals, since the time between the interruptions and restarts of
       the call will lead to drift in the time when the sleep finally completes.  This problem can be avoided by using clock_nanosleep(2) with an absolute time value.

       POSIX.1 specifies that nanosleep() should measure time against the CLOCK_REALTIME clock.  However, Linux measures the time using the CLOCK_MONOTONIC clock.  This probably does not matter, since the POSIX.1
       specification for clock_settime() says that discontinuous changes in CLOCK_REALTIME should not affect nanosleep():

              Setting  the  value  of  the  CLOCK_REALTIME  clock  via  clock_settime()  shall  have  no effect on threads that are blocked waiting for a relative time service based upon this clock, including the
              nanosleep() function; ...  Consequently, these time services shall expire when the requested relative interval elapses, independently of the new or old value of the clock.

   Old behavior
       In order to support applications requiring much more precise pauses (e.g., in order to control some time-critical hardware), nanosleep() would handle pauses of up to 2 ms by busy waiting  with  microsecond
       precision  when called from a thread scheduled under a real-time policy like SCHED_FIFO or SCHED_RR.  This special extension was removed in kernel 2.5.39, hence is still present in current 2.4 kernels, but
       not in 2.6 kernels.

BUGS
       In Linux 2.4, if nanosleep() is stopped by a signal (e.g., SIGTSTP), then the call fails with the error EINTR after the thread is resumed by a SIGCONT signal.  If the system call is subsequently restarted,
       then the time that the thread spent in the stopped state is not counted against the sleep interval.

SEE ALSO
       clock_nanosleep(2), sched_setscheduler(2), sleep(3), timer_create(2), usleep(3), time(7)

COLOPHON
       This page is part of release 3.22 of the Linux man-pages project.  A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/.

Linux                             2009-01-19                      NANOSLEEP(2)

论坛徽章:
20
程序设计版块每日发帖之星
日期:2015-08-17 06:20:00程序设计版块每日发帖之星
日期:2016-07-16 06:20:00程序设计版块每日发帖之星
日期:2016-07-18 06:20:00每日论坛发贴之星
日期:2016-07-18 06:20:00黑曼巴
日期:2016-12-26 16:00:3215-16赛季CBA联赛之江苏
日期:2017-06-26 11:05:5615-16赛季CBA联赛之上海
日期:2017-07-21 18:12:5015-16赛季CBA联赛之青岛
日期:2017-09-04 17:32:0515-16赛季CBA联赛之吉林
日期:2018-03-26 10:02:16程序设计版块每日发帖之星
日期:2016-07-15 06:20:0015-16赛季CBA联赛之江苏
日期:2016-07-07 18:37:512015亚冠之萨济拖拉机
日期:2015-08-17 12:21:08
7 [报告]
发表于 2016-03-30 11:20 |只看该作者

论坛徽章:
1
水瓶座
日期:2013-10-14 10:55:54
8 [报告]
发表于 2016-04-07 23:37 |只看该作者
用户是可以达到ns级的定时器精度的。而且你说的定时器受限于ms级的tick精度也是不对的。内核开了高精度定时器时,tick的精度和定时器的精度完全没有关系。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP