衡量CPU的使用Measure CPU Use
有效地使用CPU可以使应用程序更快更有效地运行。尽管您正在为特定平台编写应用程序,但请记住,即使是相同的通用平台也有不同的CPU能力。工具帮助您在各种条件下进行测试,从而使您能够确定应用程序使用多个核心、线程和其他资源有多好。Effective use of the CPU allows your app to run faster and more effectively. Although you are writing an app for a particular platform, keep in mind that even the same general platform has different CPU capabilities. Instruments helps you conduct testing under a variety of conditions, allowing you to identify how well your app uses multiple cores, threads, and other resources.
轨道芯线使用Track Core and Thread Use
时间分析器概要模板使用时间分析器工具来执行在系统CPU上运行的进程的低开销、基于时间的采样。随着时间的推移,更有效地使用多个核心和线程,应用程序的性能会更好。时间分析器工具中的CPU和线程策略显示了一个应用程序如何利用内核和线程。The Time Profiler profiling template uses the Time Profiler instrument to perform low-overhead, time-based sampling of processes running on the system’s CPUs. The more effectively multiple cores and threads are used over time, the better an app’s performance. The CPU and thread strategies in the Time Profiler instrument show how well an app utilizes cores and threads.
查看核心或线程使用情况To view core or thread usage
- 运载工具。Launch Instruments.
- 在出现的分析模板选择对话框中,单击“时间分析器”。In the profiling template selection dialog that appears, click Time Profiler.
- 从目标设备和流程列表中选择您的设备和应用程序。Choose your device and app from the target device and process lists.
- 单击“选择”创建跟踪文档。Click Choose to create a trace document.
- 单击“记录”按钮在工具栏(或按command-r)开始记录。Click the Record button () in the toolbar (or press Command-R) to begin recording.
- 正常使用你的应用程序或系统。Use your app or the system normally.
- 请单击“停止”按钮,或按command-r再次,当完成。Click the Stop button (), or press Command-R again, when complete.
- 单击跟踪文档工具栏中的CPU或线程策略按钮。Click the CPU or threads strategy button in the trace document toolbar.
- 检查收集的数据。Examine the collected data.
使用CPU策略使用视图比较给定时间段的核心使用情况。有效的核心并发性提高了应用程序的性能。寻找不平衡的核心使用。如果单个内核有大量使用的区域,而其他内核保持安静,则可以指示需要更大优化的区域。当放大时,活动可能出现在多个线程上,但是更仔细的检查可能会显示出其他情况。因此,在检查核心用法时,一定要在跟踪窗格中进行缩放。Use the CPU strategy usage view to compare core usage over given time periods. Effective core concurrency improves an app’s performance. Look for unbalanced core usage. If a single core has areas of heavy usage while other cores remain quiet, that can indicate areas that need greater optimization. When zoomed out, activity may appear to be occurring on multiple threads, but a closer inspection may reveal otherwise. Therefore, be sure to zoom in on the track pane when examining core usage.
使用线程策略视图检查应用程序在执行工作时的线程使用情况。主线程上的大量工作会使应用程序的用户界面反应迟钝或缓慢。只要有可能,工作应该从主线上移开。Use the threads strategy view to examine your app’s use of threads when performing work. Large amounts of work on the main thread can make your app’s user interface unresponsive or slow. Whenever possible, work should be moved off the main thread.
寻找性能瓶颈Look for Performance Bottlenecks
性能监视器计数器(PMCS)是硬件寄存器,处理器中的事件发生的措施。它们通过识别特定类型的过度事件来帮助发现应用程序中的瓶颈。例如,大量的条件分支指令可能表示一段逻辑,如果重新排列,可能会降低所需分支的数量。PMC事件使这些问题得以解决,但这取决于您如何将它们与您的代码相匹配,并决定它们将如何帮助您改进应用程序的性能。计数器剖析模板使用计数器工具跟踪PMC事件。Performance monitor counters (PMCs) are hardware registers that measure events occurring in the processor. They help find bottlenecks in your app by identifying an excessive number of events of a particular type. For example, a high number of conditional branch instructions may indicate a section of logic that, if rearranged, might lower the number of branches required. PMC events bring these issues to light, but it is up to you to match them to your code and decide how they will help you improve your app’s performance. The Counters profiling template uses the Counters instrument to track PMC events.
跟踪PMC事件To track PMC events
- 运载工具。Launch Instruments.
- 在出现的剖析模板选择对话框中,单击计数器。In the profiling template selection dialog that appears, click Counters.
- 从目标设备和流程列表中选择您的设备和应用程序。Choose your device and app from the target device and process lists.
- 单击“选择”创建跟踪文档。Click Choose to create a trace document.
- 单击时间轴窗格中的计数器工具。Click the Counters instrument in the timeline pane.
- 记者command-2显示督察窗格记录设置。Press Command-2 to show record settings in the inspector pane.
- 在“检查面板”的“记录设置”区域中,单击事件表和公式表中的Add按钮(+)。In the record settings area of the inspector pane, click the Add button (+) in the Events and Formulas table.
- 选择您想要计数的事件。Select the event you’d like to count.
- 如果需要,重复步骤7和步骤8以添加更多事件。Repeat steps 7 and 8 to add more events, if desired.
- 单击“记录”按钮在工具栏(或按command-r)开始记录。lick the Record button () in the toolbar (or press Command-R) to begin recording.
- 正常使用你的应用程序或系统。Use your app or the system normally.
- 请单击“停止”按钮,或按command-r再次,当完成。Click the Stop button (), or press Command-R again, when complete.
- 检查收集的数据。Examine the collected data.
IMPORTANT
可以跟踪的PMC事件数量取决于硬件。尝试您的设置,以确定您可以跟踪多少事件,而不会造成错误。The number of PMC events that can be tracked is hardware dependent. Experiment with your setup to determine how many events you can track at once without causing an error.
TIP
如果您计划频繁记录相同的PMC事件,请将它们保存在分析模板中。否则,当您关闭文档时,它们就会丢失。有关保存分析模板的信息,请参阅将跟踪文档保存为分析模板。If you plan to record the same PMC events frequently, save them in a profiling template. Otherwise, they are lost when you close the document. For information on saving a profiling template, see Save a Trace Document as a Profiling Template.
测量CPU使用的其他方法Other Ways to Measure CPU Use
仪器提供了许多其他的分析模板,可以用来检查应用程序对CPU的使用情况。Instruments provides a number of other profiling templates that can be used to examine your app’s use of the CPU.
- 与其他应用程序相比,使用活动监视器概要模板来衡量整个CPU的使用情况。Use the Activity Monitor profiling template to gauge overall CPU use in comparison to other apps.
- 使用分派分析模板来查看您的调度队列何时执行。您可以看到分派线程持续多长时间和使用了多少块。Use the Dispatch profiling template to see when your dispatch queues are executed. You can see how long the dispatched thread lasts and how many blocks are used.
- 使用系统跟踪剖析模板查看线程何时调度、活动时间、使用的核心以及更多。Use the System Trace profiling template to see when threads are scheduled, time active, core used, and more.