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.

bullet
To track PMC events in the Counters instrument
  1. Open the Counters trace template.

  2. Choose an app from the target menu in the toolbar.

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

    ../Art/instruments_counters_config1_2x.png
  4. Select the event you’d like to count.

    ../Art/instruments_counters_config2_2x.png
  5. Repeat steps 3 and 4 to add more events, if desired.

  6. Click Record and exercise your app.

  7. Click Stop.

../Art/instruments_trace_document_counters_data_2x.png

Saving Energy with the Energy Diagnostics Trace Template

The Energy Diagnostics trace template provides diagnostics regarding energy usage, as well as basic on/off states of major device components. This template consists of the Energy Usage, CPU Activity, Network Activity, Display Brightness, Sleep/Wake, Bluetooth, WiFi, and GPS instruments.

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.

bullet
To track energy usage on an iOS device
  1. Go into Settings > Developer and enable developer logging on the iOS device where you want to capture data.

    ../Art/Logging_Energy_Usage_in_an_iOS_Device_2x.png
  2. Exercise your app like a user would.

  3. 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
bullet
To view thread usage in your app
  1. Create a new trace document using the Multicore template.

  2. Choose your app from the target menu in the toolbar.

  3. Run your app.

  4. Select the Thread States instrument in the Instruments pane.

  5. 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.

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.

The Multicore trace template displays thread interaction throughout your app. However, you are not able to see which cores are being used. To see core usage by your app, see Delving into Core Usage with the Time Profiler Trace Template.

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.

bullet
To view individual core usage
  1. Open the Time Profiler trace template.

  2. Choose your app from the Choose Target pop-up menu in the trace document’s toolbar.

  3. Click the Record button.

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

  5. Click the CPU strategy button.

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

  7. Look for unbalanced core usage.

    ../Art/Monitoring_Multiple_Core_Usage_V6_2x.png

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.