cai_yankun
驱动牛犊
驱动牛犊
  • 注册日期2005-03-10
  • 最后登录2005-03-29
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:1837回复:12

求助:关于vc高精度延时的问题?

楼主#
更多 发布于:2005-03-27 22:21
看过了<<用VC可否实现5微秒的延迟(A/D转换时要求的)?>>
这篇文章受益匪浅 我想问几个问题?
――――――――――――――――――――――――――――――
引用:(dazzy)
用rdtsc这条汇编指令读取当前的时钟计数
unsigned int tsc1;
unsigned int tsc2;
#define rdtsc __asm _emit 0x0f __asm _emit 0x31
_asm rdtsc;
_asm mov tsc1,eax;
_asm mov tsc2,edx;
这个精度高,但是条件是你必须先用软件检测出机器时钟(可以读注册表),用一小段程序循环就可得到5微秒的定时精度。
内核定时器号称100ns,实际上达不到

___________________________________________________________
不过我想问一下dazzy的方法中 如果用程序循环的话会不会有问题?
如果用指令进行循环的话 那windows是多任务的 延时程序那个线程不是独占cpu那样结果会不会被延长?
再者如果用循环读计数值的方法那每循环一次都要执行一些指令这些指令的时间是不是也要算进去?
能不能利用中断呢?
我是新手也许问的问题方式不对 也许有些弱智 请大家多多包涵。先行谢过了!
bmyyyud
驱动老牛
驱动老牛
  • 注册日期2002-02-22
  • 最后登录2010-01-21
  • 粉丝0
  • 关注0
  • 积分1000分
  • 威望130点
  • 贡献值0点
  • 好评度106点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2005-03-28 08:23
不过我想问一下dazzy的方法中 如果用程序循环的话会不会有问题?
如果用指令进行循环的话 那windows是多任务的 延时程序那个线程不是独占cpu那样结果会不会被延长?
是的,会被延长
再者如果用循环读计数值的方法那每循环一次都要执行一些指令这些指令的时间是不是也要算进去?
是的
能不能利用中断呢?
可以,但时钟中断也达不到5ms的精度
滚滚长江东逝水 浪花淘尽英雄 是非成败转头空 青山依旧在 几度夕阳红 白发渔樵江渚上 惯看秋月春风 一壶浊酒喜相逢 古今多少事 尽付笑谈中
cai_yankun
驱动牛犊
驱动牛犊
  • 注册日期2005-03-10
  • 最后登录2005-03-29
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2005-03-28 11:25
啊 !!!
响应中断的时间大于5ms?那时间太长了!
那是不是在windows中没有办法做到1微秒甚至10ns的精度了?
------
还望专家指点一下方向
Leonsoft
驱动小牛
驱动小牛
  • 注册日期2003-05-08
  • 最后登录2012-08-11
  • 粉丝1
  • 关注0
  • 积分21分
  • 威望281点
  • 贡献值1点
  • 好评度103点
  • 原创分0分
  • 专家分0分
地板#
发布于:2005-03-28 12:15
KeStallExecutionProcessor
I will do the best with what the God gave me.
cai_yankun
驱动牛犊
驱动牛犊
  • 注册日期2005-03-10
  • 最后登录2005-03-29
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
地下室#
发布于:2005-03-28 14:18
谢谢3楼的大侠 您提到的这个函数精确吗?
bmyyyud
驱动老牛
驱动老牛
  • 注册日期2002-02-22
  • 最后登录2010-01-21
  • 粉丝0
  • 关注0
  • 积分1000分
  • 威望130点
  • 贡献值0点
  • 好评度106点
  • 原创分0分
  • 专家分0分
5楼#
发布于:2005-03-28 15:46
转大师的话:
Copyright ? 1997 Mark Russinovich
Last Updated: Last updated July 9, 1997
Note: The information presented here is the result of my own study. No source code was used.

Introduction
High resolution timers are desirable in a wide variety of different applications. For example, the most common use of such timers in Windows is by multimedia applications that are producing sound or audio that require precise control. MIDI is a perfect example because MIDI sequencers must maintain the pace of MIDI events with 1 millisecond accuracy. This article describes how high resolution timers are implemented in NT and documents NtSetTimerResolution and NtQueryTimerResolution, the NT kernel functions that manipulate and return information about the system clock. Unfortunately, NtSetTimerResolution and NtQueryTimerResolution are not exported by the NT kernel, so they are not available to kernel-mode device drivers.
The Timer API
Windows NT bases all of its timer support off of one system clock interrupt, which by default runs at a 10 millisecond granularity. This is therefore the resolution of standard Windows timers. When a multimedia application uses the timeBeginPeriod mutlimedia API, which is exported by the Windows NT dynamic link library WINMM.DLL, the call is redirected into the Windows NT kernel-mode function NtSetTimerResolution, which is exported by the native Windows NT library NTDLL.DLL.

NtSetTimerResolution and NtQueryTimerResolution are defined as follows. All times are specifified in hundreds of nanoseconds.

NTSTATUS NtSetTimerResolution (

    IN ULONG RequestedResolution,
    IN BOOLEAN Set,
    OUT PULONG ActualResolution


);

Parameters

RequestedResolution

The desired timer resolution. Must be within the legal range of system timer values supported by NT. On standard x86 systems this is 1-10 milliseconds. Values that are within the acceptable range are rounded to the next highest millisecond boundary by the standard x86 HAL. This parameter is ignored if the Set parameter is FALSE.

Set

This is TRUE if a new timer resolution is being requested, and FALSE if the application is indicating it no longer needs a previously implemented resolution.

ActualResolution

The timer resolution in effect after the call is returned in this parameter.

Comments

NtSetTimerResolution returns STATUS_SUCCESS if the resolution requested is within the valid range of timer values. If Set is FALSE, the caller must have made a previous call to NtSetTimerResolution or STATUS_TIMER_RESOLUTION_NOT_SET is returned.

NTSTATUS NtQueryTimerResolution (

    OUT PULONG MinimumResolution,
    OUT PULONG Maximum Resolution,
    OUT PULONG ActualResolution


);

Parameters

MinimumResolution

The minimum timer resolution. On standard x86 systems this is 0x2625A, which is about 10 milliseconds

MaximumResolution

The maximum timer resolution. On standard x86 systems this is 0x2710, which is about 1 millisecond.

ActualResolution

This is the current resolution of the system clock.
Implementation Details
NtSetTimerResolution can be called to set timer resolutions by more than on application. To support a subsequent process setting a timer resolution without violating the resolution assumptions of a previous caller, NtSetTimerResolution never lowers the timer\'s resolution, only raises it. For example, if a process sets the resolution to 5 milliseconds, subequent calls to set the resolution to between 5 and 10 millseconds will return a status code indicating success, but the timer will be left at 5 milliseconds.

NtSetTimerResolution also keeps track of whether a process has set the timer resolution in its process control block, so that when a call is made with Set equal to FALSE it can verify that the caller has previously requested a new resolution. Every time a new resolution is set a global counter is incremented, and every time it is reset the counter is decremented. When the counter becomes 0 on a reset call the timer is changed back to its default rate, otherwise no action is taken. Again, this preserves the timer resolution assumptions of all the applications that have requested high resolution timers by guaranteeing that the resolution will be at least as good as what they specified.

You can use the ClockRes applet from Sysinternals to view the current clock resolution on your system.
滚滚长江东逝水 浪花淘尽英雄 是非成败转头空 青山依旧在 几度夕阳红 白发渔樵江渚上 惯看秋月春风 一壶浊酒喜相逢 古今多少事 尽付笑谈中
bmyyyud
驱动老牛
驱动老牛
  • 注册日期2002-02-22
  • 最后登录2010-01-21
  • 粉丝0
  • 关注0
  • 积分1000分
  • 威望130点
  • 贡献值0点
  • 好评度106点
  • 原创分0分
  • 专家分0分
6楼#
发布于:2005-03-28 15:47
顺便看一下这个帖子
http://www.driverdevelop.com/forum/viewthread.php?tid=87222
滚滚长江东逝水 浪花淘尽英雄 是非成败转头空 青山依旧在 几度夕阳红 白发渔樵江渚上 惯看秋月春风 一壶浊酒喜相逢 古今多少事 尽付笑谈中
cai_yankun
驱动牛犊
驱动牛犊
  • 注册日期2005-03-10
  • 最后登录2005-03-29
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
7楼#
发布于:2005-03-28 18:24
谢谢大侠们,你们的热情让我热泪盈眶了,今天我在网上查到一种读注册表的方法现在已经能读取CPU的频率了,至此我又产生了几个新问题。
(1)这样读出来的频率精确吗?
(2)这个频率就是cpu执行所有软件(包括操作系统和虚拟机)的周期指令的吗?所谓的多线程轮流占用cpu就是在此基础上(即时间片就是这个周期的整数倍)对cpu进行”瓜分“的吗?
(3)如果上面的都是正确的话那么在ddk中或者api中有没有一些函数可以让自己这段延时的程序在未结束之前单独占用cpu呢?(所谓的线程优先级最高是不是单独占用cpu)
(4)如果有这样的函数的话那么是不是可以象以前的汇编程序那样用指令循环延时?这样不是精确度比任何调用其他函数都高吗?
------------------------------
谢谢大侠们,我还有一个很弱的问题望大侠们能帮个忙http://www.driverdevelop.com/forum/html_89463.html?1112003187
bmyyyud
驱动老牛
驱动老牛
  • 注册日期2002-02-22
  • 最后登录2010-01-21
  • 粉丝0
  • 关注0
  • 积分1000分
  • 威望130点
  • 贡献值0点
  • 好评度106点
  • 原创分0分
  • 专家分0分
8楼#
发布于:2005-03-29 09:31
(1)这样读出来的频率精确吗?
频率很高,应该很精确
(2)这个频率就是cpu执行所有软件(包括操作系统和虚拟机)的周期指令的吗?所谓的多线程轮流占用cpu就是在此基础上(即时间片就是这个周期的整数倍)对cpu进行”瓜分“的吗?
这个频率就是cpu执行所有软件(包括操作系统和虚拟机)的指令周期
所谓的多线程轮流占用cpu却是在时钟中断上实现的(条件是其间没有硬件中断)
(3)如果上面的都是正确的话那么在ddk中或者api中有没有一些函数可以让自己这段延时的程序在未结束之前单独占用cpu呢?
使用最高IRQL就可以独占
所谓的线程优先级最高不是单独占用cpu
(4)如果有这样的函数的话那么是不是可以象以前的汇编程序那样用指令循环延时?这样不是精确度比任何调用其他函数都高吗?
这个问题看了上面应该明白了
滚滚长江东逝水 浪花淘尽英雄 是非成败转头空 青山依旧在 几度夕阳红 白发渔樵江渚上 惯看秋月春风 一壶浊酒喜相逢 古今多少事 尽付笑谈中
cai_yankun
驱动牛犊
驱动牛犊
  • 注册日期2005-03-10
  • 最后登录2005-03-29
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
9楼#
发布于:2005-03-29 10:26
哇 bmyyyud 您真是个好人。就凭您的这份热情就让我感动万分,干劲十足。好 我会加倍努力学习的。
a0041059a
驱动小牛
驱动小牛
  • 注册日期2005-05-09
  • 最后登录2016-01-09
  • 粉丝0
  • 关注0
  • 积分46分
  • 威望6点
  • 贡献值0点
  • 好评度4点
  • 原创分0分
  • 专家分0分
10楼#
发布于:2005-05-09 20:03
好人可真多呀
linac
驱动牛犊
驱动牛犊
  • 注册日期2005-04-25
  • 最后登录2005-11-20
  • 粉丝0
  • 关注0
  • 积分16分
  • 威望3点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
11楼#
发布于:2005-05-15 10:49
这个多媒体定时器与系统时钟定时器用的不是一个定时通道?8253?

把多媒体定时器精度改为1ms,系统时钟一个tick还是15ms.
我想在驱动中达到1-2ms的精度,想破脑袋了。
linac
驱动牛犊
驱动牛犊
  • 注册日期2005-04-25
  • 最后登录2005-11-20
  • 粉丝0
  • 关注0
  • 积分16分
  • 威望3点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
12楼#
发布于:2005-05-15 13:51
KeQueryPerformanceCounter 不是也是用rdtsc来读cpu的脉冲计数器吗?  为什么就这一条汇编指令,DDK说开销很大,不建议频繁使用。
游客

返回顶部