5

我用 ruby​​-prof 分析了我的基于事件机器的应用程序,发现以下有趣的东西:

                  5.28 0.00 5.28 0.00 4/4 互斥#同步
90.72% 0.00% 5.28 0.00 5.28 0.00 4 互斥#sleep

我认为 ruby​​-prof 只计算 CPU 滴答声,因此我无法弄清楚为什么互斥锁睡眠可能需要 CPU 时间。我假设它在内核级别上休眠,不计入光纤时间。有任何想法吗?更好的是,我希望 Mutex#sleep 将控制权释放给事件机器,以便它可以做其他事情。

4

5 回答 5

1

如果 ruby​​-prof --mode=cpu 真的只是计算 CPU 时间,那么 Mutex#sleep 就是一个自旋锁:

http://en.wikipedia.org/wiki/Spinlock

这是有道理的,因为您只应该在互斥锁中包装非常短的分配。设置信号警报将是疯狂的开销。

所以你面临的挑战是确定你正在睡觉的互斥锁,以及它们包裹的东西。然后,您将发现可避免的互斥体滥用或不可避免的等待时间。

于 2011-11-06T15:28:27.730 回答
0

There is far too little information here to give an accurate answer, however, I would suggest looking at a few other aspects, such as whether or not ruby-prof is even picking up time spent in the reactor?

Disregarding what percentages ruby-prof gives you, how long does your profile actually run for, and how long is actually spent in the mutex? It's likely this is only showing #defer threads or something similar, and entirely ignoring the C++ runtime in another thread.

于 2011-09-28T17:59:47.483 回答
0

通常,如果一个线程正在做某事而第二个线程正在等待它完成,那么它将被视为睡眠。所以你的睡眠时间可以是(取决于你的线程): - 你的线程正在等待一个事件(空闲) - 你的 theread 正在等待另一个线程完成某些事情(其他线程忙)

于 2011-06-13T10:13:30.493 回答
0

据我所知,如果您被困在等待释放锁的过程中,那么该线程就会陷入睡眠模式。保持 Eventmachine 运行的唯一方法是确保您的锁发生在单独的线程中。

于 2011-04-11T16:15:45.380 回答
0

那 90% 可能意味着您有 90% 的时间都在“睡觉”[?] 一般来说,使用 EM,您不需要睡觉,您只需响应事件,并且大部分 CPU 将显示为 EM 的“运行” “ 方法

于 2011-04-11T20:47:20.180 回答