出于性能分析目的,可以忽略对 WaitHandle.SignalAndWait 的调用吗?
我刚刚下载了 Red Gate 的 ANTS Performance Profiler 试用版,并且正在调查我团队的一些代码。 我立即注意到 ANTS 报告有一段特定的代码占用了 99% 的 CPU 时间。
我对 ANTS 或一般性能分析完全不熟悉(也就是说,除了使用我确信非常粗鲁和令人皱眉的方法进行自我分析之外,例如 double timeToComplete = (endTime - startTime).TotalSeconds< /code>),所以我仍在摆弄该应用程序并弄清楚它是如何使用的。 但我确实打电话给负责相关代码的开发人员,他的立即反应是“是的,它这么说并不让我感到惊讶;但是该代码调用了 SignalAndWait [感谢 ANTS,我可以亲眼看到],它不使用任何CPU,它只是坐在那里等待做一些事情。” 他建议我忽略该代码并寻找我能找到的其他任何内容。
我的问题:SignalAndWait 是否真的不需要 CPU 开销(如果是这样,这怎么可能?),性能分析器将其视为占用 99% CPU 时间是否合理? 我觉得这特别奇怪,因为如果它是 99%,那就表明我们的应用程序经常闲置,不是吗? 但最近它的表现却变得相当低迷。
就像我说的,对于这个工具,我确实只是一个初学者,而且我对 WaitHandle 类一无所知。 因此,任何可以帮助我了解这里发生的事情的信息将不胜感激。
I just downloaded the trial version of ANTS Performance Profiler from Red Gate and am investigating some of my team's code. Immediately I notice that there's a particular section of code that ANTS is reporting as eating up to 99% CPU time.
I am completely unfamiliar with ANTS or performance profiling in general (that is, aside from self-profiling using what I'm sure are extremely crude and frowned-upon methods such as double timeToComplete = (endTime - startTime).TotalSeconds
), so I'm still fiddling around with the application and figuring out how it's used. But I did call the developer responsible for the code in question and his immediate reaction was "Yeah, that doesn't surprise me that it says that; but that code calls SignalAndWait [which I could see for myself, thanks to ANTS], which doesn't use any CPU, it just sits there waiting for something to do." He advised me to simply ignore that code and look for anything ELSE I could find.
My question: is it true that SignalAndWait requires NO CPU overhead (and if so, how is this possible?), and is it reasonable that a performance profiler would view it as taking up 99% CPU time? I find this particularly curious because, if it's at 99%, that would suggest that our application is often idle, wouldn't it? And yet its performance has become rather sluggish lately.
Like I said, I really am just a beginner when it comes to this tool, and I don't know anything about the WaitHandle class. So ANY information to help me to understand what's going on here would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
WaitHandle 确实会让你的线程进入睡眠状态。
额外的好处是您可以在这些句柄上设置超时,以便它们可以在一段时间后唤醒。
您还可以从另一个线程(例如应用程序退出等)发出 WaitHandle 信号,它们会立即唤醒。
我个人更喜欢具有短超时的 WaitHandle,而不是具有相同超时的 Thread.Sleep,因为当睡眠启动时,它必须返回才能恢复操作,而如果需要,可以立即恢复 WaitHandle 。
A WaitHandle does indeed put your Thread to sleep.
The added bonus is you can set timeouts on these Handles so that they can wake after a period of time.
You can also Signal the WaitHandle from another thread (eg. Application exit etc) and they immediately WakeUp.
I personally prefer a WaitHandle with a Short timeout over a Thread.Sleep with the same timeout, as when a Sleep is started it must return before you can resume operation, whilst a WaitHandle can be resumed immediately if required.
我认为您的代码中可能存在严重的错误。
EventWaitHandle 根据其重置模式有 2 种语义。
当 EventWaitHandle 处于 AutoReset 模式时,所有等待线程都将被阻塞,直到事件发出信号为止,一旦事件发出信号,后续的等待操作将重置其状态,并且调用 wait 的线程将再次阻塞。
但是,如果 EventWaitHandle 处于 ManualReset 模式,它将保持有信号状态,直到您对其手动调用 Reset,这意味着如果 EventWaitHandle 有信号并且线程在紧密循环中调用 wait,则该线程将不会阻塞,直到 Reset在事件上手动调用,因此考虑这个假设场景,
上面的循环将占用大部分 CPU,直到其他线程调用 h1.Reset(),这将使 SignalAndWait 阻塞。
希望这可以帮助。
欲了解更多信息,请查看
http://msdn.microsoft.com/en-us/库/system.threading.eventwaithandle.aspx
I think you might have a serious bug in your code.
EventWaitHandle has 2 semantics depending on its reset mode.
When EventWaitHandle are in AutoReset mode, then all waiting threads are blocked until the event is signaled, once the event is signal, subsequent wait operations on it reset its state and the thread calling wait block again on it.
However, if the EventWaitHandle is in ManualReset Mode, it will remain signaled until you call Reset Manually on it, this means that if an EventWaitHandle is signaled and a thread is calling wait on it in a tight loop, this thread will not block until Reset is called manually on the event, so consider this hyphothetical scenario
the loop above will eat most of your CPU until some other thread calls h1.Reset(), which will make SignalAndWait blocks.
Hope this helps.
For more information, check out
http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle.aspx