Instruments User Guide

Analyzing CPU Usage in Your App

Ensuring effective use of all available resources is important when writing code for your app. One of the most important resources is the CPU. Effective use of the CPU allows your app to run faster and more effectively. Even though you will be writing your app for a particular platform, keep in mind that even the same general platform type can have different CPU capabilities. The CPU trace templates provide you with the means to conduct testing under a variety of potential conditions, allowing you to identify how well your app uses multiple cores, how much energy you are using, and other resource measurements.

Looking for Bottlenecks with Performance Monitor Counters

Performance monitor counters (PMCs) are hardware registers that measure events occurring in the processor. They can be used to help find bottlenecks in your app by identifying an excessive amount 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. Even though PMC events can bring these issues to light, it is up to you to match them to your code and decide how they will help you improve your app’s performance.

In Instruments, you track PMC events using the Counters instrument.

To track PMC events in the Counters instrument

Open the Counters trace template.

Choose an app from the target menu in the toolbar.

In the Record Settings inspector, click the plus button (+) in the Events and Formulas table.

Select the event you’d like to count.

Repeat steps 3 and 4 to add more events, if desired.

Click Record and exercise your app.

Click Stop.

Important: The number of PMC events that can be tracked is hardware dependent. Trying to track too many events at one time can cause an error. Experiment with your setup to determine the number of events that can be successfully tracked at one time.

Tip: If you plan on recording the same PMC events frequently, save them in a trace template. Otherwise, they will be lost when you close the document. For information on saving a trace template, see Saving an Instruments Trace Template.

Figure 10-1 The Energy Diagnostics template after it has collected data from an iOS device

With Energy Diagnostics Logging on, your iOS device records energy-related data unobtrusively while the device is used. Because logging is efficient, you can log all day. Logging continues while the iOS device is in sleep mode, but if the device battery runs dry or the iOS Device is powered off, the log data is lost.

A Developer setting appears in the Settings app only after your device is provisioned for development. The setting disappears after the device has been rebooted. Restore the setting by connecting the device to Xcode or Instruments.

After sufficient energy usage events have been logged, you can analyze them by importing the log data from the phone to the Xcode Instruments Energy Diagnostics template. Look for areas of high energy usage and see whether you can reduce energy usage in these areas.

To track energy usage on an iOS device

Go into Settings > Developer and enable developer logging on the iOS device where you want to capture data.

Exercise your app like a user would.

After capturing the data, turn off developer logging.

Minimize the amount of energy your app uses by ensuring that you turn off any device radios that you don’t actively need. You can verify that you have turned off a particular radio by using the Energy Diagnostics trace template. In the track pane, each radio is depicted with red to designate that it is on or with black to designate that it is turned off. In Figure 10-1, the GPS radio track is disabled and therefore its track appears in black.

Examining Thread Usage with the Multicore Trace Template

The Multicore trace template analyzes your multicore performance, including thread state, dispatch queues, and block usage. It consists of the Thread States and the Dispatch instruments.

The Thread States instrument provides you with a graphical representation of each thread’s state at a particular time in the run. Each state is color-coded to help you identify what each thread is doing in conjunction with all of the other threads. Threads that go through multiple state changes are easily identifiable through the changing colors in the track pane. Figure 10-2 shows fifteen threads being tracked.

Figure 10-2 Thread activity displayed by the Multicore trace template

To view thread usage in your app

Create a new trace document using the Multicore template.

Choose your app from the target menu in the toolbar.

Run your app.

Select the Thread States instrument in the Instruments pane.

Choose the threads to examine by selecting the checkbox in the Mark column in the detail pane.

The following thread states are captured in the track pane by the Thread States instrument. Color notations are the default colors, but they can be changed by you.

Unknown / At termination (Gray). Instruments is unable to determine the state of the thread or it is being terminated.

Waiting (Yellow). The thread is waiting for another thread to perform a particular action.

Suspended (Dark Blue). The thread has been put into a suspended state and will not continue until it is specifically told to resume running.

Requested to suspend (Light Blue). The thread has sent out a request to be put into a suspended state.

Running (Green). The thread is running.

On run queue (Black). The thread is in the queue to be run. It will run after a CPU becomes available.

Waiting uninterruptedly (uninterruptibly) (Orange). The thread is waiting for another thread to perform a particular action and can not be interrupted during the wait.

Idling processor (White). The thread is active on a processor, but is not performing any actions.

Along with the Thread States instrument, the Multicore template contains the Dispatch instrument. Use the Dispatch instrument to see when your dispatch queues are executed. You can see how long the dispatched thread lasts and how many blocks are used.

Delving into Core Usage with the Time Profiler Trace Template

The Time Profiler trace template performs low-overhead, time-based sampling of processes running on the system’s CPUs. It consists of the Time Profiler instrument only.

The CPU strategy in the Time Profiler instrument shows how well your app utilizes multiple cores. Selecting the CPU strategy in a trace document configures the track pane to display time on the x-axis and processor cores on the y-axis. Use the CPU strategy usage view to compare core usage over given time periods. Effective core concurrency improves an app’s performance. Areas of heavy usage for a single core while other cores remain quiet can depict areas needing greater optimization.

Exercise your app to execute code, then click the Stop button to capture and analyze data.

Click the CPU strategy button.

Choose whether you want to graphically display the chart by boundary, category, or usage in the lower section of the toolbar.

Look for unbalanced core usage.

Ensure that your app is using multiple cores simultaneously by zooming in on the track pane. One or two threads that jump between cores very quickly can make it look like multiple cores are in use at the same time when in reality, only one core is in use at any one time.