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.
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.
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.
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.
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.
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.
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.
Open the Time Profiler trace template.
Choose your app from the Choose Target pop-up menu in the trace document’s toolbar.
Click the Record button.
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.