测量轮廓“睡眠”时间的时间。功能 [英] measuring time of a profiled "Sleep" function

查看:187
本文介绍了测量轮廓“睡眠”时间的时间。功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



几天前我发布了这个问题:
测量时间。

(我希望它打开一个新的线程,我只是找不到老的

(I hope it's ok i'm opening a new thread, I just can't find the old one in the few first pages)

我注意到在我的分析过程中,我调用一个睡眠方法 - 这是问题...
当我调用SuspendThread / ResumeThread - 休眠过程暂停,但在现实 - 时间移动!

我认为休眠只是一种循环,需要时间和停止,开始时间和结束时间足够大。

I noticed that in my profiled process, I call a "Sleep" method - and this is the problem... When I call SuspendThread/ResumeThread - The Sleeping process pauses, but in reality - time moves on!
I figure that "Sleep" is just some kind of a loop that takes the time and stops whenever the difference between the start time and the end time is large enough.

,假设您的配置过程需要睡眠1分钟。

您在睡眠2秒后暂停配置过程,然后去吃

15分钟后回来,并恢复分析过程。

分析过程测量时间,发现超过1分钟后,它停止休眠。

well, suppose that your profiled process needs to sleep for 1 minute.
You suspended the profiled process after sleeping 2 seconds, and went to eat something.
You came back after 15 minutes and resumed the profiled process.
The profiled process measures the time, finds that MORE than 1 minute passed and it stops sleeping.

这就是我如何让进程休眠太少(时间过去,当进程被暂停,睡眠会考虑到 - 但我不能!)。 ..

that's how I made the process sleep too little (time passes on, when the process is suspended, the Sleep takes it into account - but I can't!)...

现在,最后这里是我的问题:

如何避免这个问题?如何正确测量睡眠功能的时间?

And now, finally here is my question:
How can I avoid this problem? How can I measure the times of functions like "Sleep" properly?

感谢:)

推荐答案

sleep 方法几乎从来没有真正执行你所描述的自旋机制 - 没有理由浪费这么多的CPU时间,要做的是在操作系统内设置一个定时器,然后简单地停止运行,直到定时器重新唤醒。如果由于 sleep 调用已经冻结的进程再次冻结,因为您已暂停它,那么它只需暂停两次; 闹钟回调并释放其暂停,但应用程序仍然挂起,因为您暂停它。如果您在睡眠休息之前将其取消暂停,您的暂停/取消暂停只会无效。

sleep methods almost never actually perform the "spin" mechanic you're describing- there's no reason to waste so much CPU time when all it really needs to do is set a timer inside the OS, and then simply stop running until reawakened by the timer. If a process that is already frozen due to a sleep call is frozen again because you suspended it, then it's simply suspended twice; the "alarm clock" calls back and releases its suspension, but the application is still suspended because you suspended it. If you unsuspend it before the sleep wears off, your suspend/unsuspend will have simply had no effect.

这样,线程不可能知道或关心为什么 sleep 与预期不同的时间。它只是不是运行代码,而 sleep ing。它停止运行代码,然后它开始运行代码一段时间后,并检查挂钟将简单地告诉它多长时间,而不是什么导致它被冻结在那段时间。 (可能是低优先级,操作系统非常忙,在 sleep 结束后,只需要很长时间来重新运行它。)

As such, it is simply impossible for a thread to know or care about why a sleep took a different time from what was expected. It simply was not running code while it was sleeping. It stopped running code, and then it started running code again some time later, and checking the wall clock will simply tell it how long it was out, not what caused it to be frozen during that time. (Maybe it was low priority, and the OS was very busy, and it simply took that long to schedule it to run again after the sleep ended.)

你最近可以做的就是编写自己的 sleep 方法,并且仅用于调试目的。 (当不在调试模式时,应该调用系统sleep方法,因此您不必更改函数之外的代码 - 通过 #ifdef DEBUG 使用条件编译/ #endif 。) sleep 应该按照你的建议:计数滴答,直到挂钟等待时间足够长,如果在刻度之间有一个意想不到的大差距,推迟截止日期,因为那个时间没有计数。

The closest you can do is to write your own sleep method and use it only for debugging purposes. (It should probably call into the system sleep method when not in debugging mode, so you don't have to change the code outside the function- use conditional compilation via #ifdef DEBUG/#endif.) That sleep should do exactly what you suggest: count ticks until the "wall clock" says it's waited long enough, and if there's an unexpectedly large gap between ticks, push the deadline out further because that time didn't "count".

所有这一切:

也许你在这里提出了错误的问题。为什么你试图配置 sleep 反正,并使其稳定在手动停止程序?精确的性能分析通常在外部冻结程序时就会消失,它会抛弃程序的几乎所有与时间相关的属性,特别是在CPU内存缓存行为方面。

Maybe you've asked the wrong question here. Why are you trying to profile sleep anyway, and make it stable across manually stopping the program? Accurate profiling generally goes out the window anyway once you're freezing the program externally- it throws off almost all the timing-related properties of the program, especially with regard to CPU memory cache behavior.

这篇关于测量轮廓“睡眠”时间的时间。功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆