52

我们偶然发现了 Quartz Events 的性能问题,更具体地说是 CGEventPost:在 GPU 负载过重时,CGEventPost 可能会阻塞。我们创建了一个小型基准测试应用程序来演示该问题。这个应用程序只是一个创建、发布和发布事件的循环。

您可以在下面看到运行应用程序的结果。第一次运行是在空闲系统上。第二次运行是使用 FurMark(GPU 压力测试),表盘尽可能地向上转动。

  • Inner 是内部循环需要多长时间,基本上只是使用 Quartz Events 创建、发布和发布事件。
  • 外部是我们的程序等待被唤醒的时间(睡眠)。应该接近我们睡觉的时间,但如果系统处于压力之下,它可能会延迟。
  • Post 是事件发布所需的时间。

 

18:58:01.683 EventPerformance[4946:707] Measurements: (outer should be close to 10)
18:58:01.684 EventPerformance[4946:707] inner (ms): 0.04, outer (ms): 11.02, CGEventPost (ms): 0.03
18:58:01.684 EventPerformance[4946:707] inner (ms): 0.04, outer (ms): 11.02, CGEventPost (ms): 0.03
18:58:01.685 EventPerformance[4946:707] inner (ms): 0.07, outer (ms): 10.26, CGEventPost (ms): 0.03
18:58:01.685 EventPerformance[4946:707] inner (ms): 0.06, outer (ms): 10.85, CGEventPost (ms): 0.05
18:58:01.686 EventPerformance[4946:707] inner (ms): 0.07, outer (ms): 10.41, CGEventPost (ms): 0.04
18:58:01.686 EventPerformance[4946:707] inner (ms): 0.04, outer (ms): 10.39, CGEventPost (ms): 0.03
18:58:01.686 EventPerformance[4946:707] inner (ms): 0.05, outer (ms): 11.02, CGEventPost (ms): 0.03
18:58:01.687 EventPerformance[4946:707] inner (ms): 0.03, outer (ms): 10.67, CGEventPost (ms): 0.03
18:58:01.687 EventPerformance[4946:707] inner (ms): 0.08, outer (ms): 10.09, CGEventPost (ms): 0.05
18:58:01.688 EventPerformance[4946:707] Averages: (outer should be close to 10)
18:58:01.688 EventPerformance[4946:707] avg inner (ms): 0.05, avg outer (ms): 10.64, avg post (ms): 0.03

在这里,我们可以看到发布事件平均需要大约 0.03 毫秒。此外,线程似乎在 0.5 毫秒左右被唤醒为时已晚。CGEventPost 中没有峰值。

19:02:02.150 EventPerformance[5241:707] Measurements: (outer should be close to 10)
19:02:02.151 EventPerformance[5241:707] inner (ms): 0.03, outer (ms): 10.23, CGEventPost (ms): 0.02
19:02:02.151 EventPerformance[5241:707] inner (ms): 0.02, outer (ms): 10.54, CGEventPost (ms): 0.02
19:02:02.151 EventPerformance[5241:707] inner (ms): 0.02, outer (ms): 11.01, CGEventPost (ms): 0.01
19:02:02.152 EventPerformance[5241:707] inner (ms): 0.02, outer (ms): 10.74, CGEventPost (ms): 0.01
19:02:02.152 EventPerformance[5241:707] inner (ms): 0.02, outer (ms): 10.20, CGEventPost (ms): 0.01
19:02:02.152 EventPerformance[5241:707] inner (ms): 10.35, outer (ms): 11.01, CGEventPost (ms): 10.35
19:02:02.152 EventPerformance[5241:707] inner (ms): 0.03, outer (ms): 10.02, CGEventPost (ms): 0.02
19:02:02.153 EventPerformance[5241:707] inner (ms): 58.90, outer (ms): 10.11, CGEventPost (ms): 58.90
19:02:02.153 EventPerformance[5241:707] inner (ms): 0.03, outer (ms): 10.12, CGEventPost (ms): 0.02
19:02:02.153 EventPerformance[5241:707] Averages: (outer should be close to 10)
19:02:02.371 EventPerformance[5241:707] avg inner (ms): 7.71, avg outer (ms): 10.44, avg post (ms): 7.71

当系统处于繁重的 GPU 负载下时,发布事件可能需要(峰值)毫秒而不是微秒。在极端 GPU 压力下 (< 1 FPS),此值可能需要几秒钟。CGEventPost有时 似乎在等待 GPU 完成一些工作后再返回。我们的线程仍然正常调度,没有明显的延迟/尖峰(外部)。

任何想法表示赞赏。

4

1 回答 1

5

我猜你正在填满队列(底层马赫端口)......

您可以使用 Instruments 中的“调度”或“系统调用”工具来确认这一点。(创建一个新的空白文档,添加仪器,然后File > Record Options...确保选中“延迟模式”。)这将显示应用程序中的所有线程活动(线程何时阻塞、何时休眠、何时激活以及原因)。

我会首先尝试提高man 3 PTHREAD_SCHEDPARAM线程调用的线程优先级(参见)CGEventPost。如果您的线程在较低优先级的线程上被阻塞,内核应该暂时提升阻塞线程的优先级以避免优先级倒置并帮助您的任务更早完成。

总的来说,我认为你将不得不实现一个2-thread 解决方案,如下所示:

为要发布的事件创建队列。从您的主线程(或事件发布线程)将事件发布到此队列,然后向第二个线程(您创建的事件使用者线程)发出信号以遍历队列并使用CGEventPost.

CGEventPost阻塞时,您的第二个事件发布线程将阻塞,但这不会阻塞任何其他线程。当CGEventPost最终解除阻塞时,它将消耗您的事件消费者线程发布的任何未完成的事件,并且事件消费者线程可以恢复发布事件。

另一种可能性:你能合并事件吗?您可以将某些类型的事件(鼠标移动?)合并成更少的事件。您有时可能仍会遇到队列限制CGEventPost,我认为 2 线程方法可能是您最好的选择。

于 2013-03-19T23:16:34.437 回答