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 your CPU. Effective use of CPUs 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 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 identify 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.
Click the Inspector button in the Counters instrument.
Click the plus button (+).
Click the event to change to another event.
The PMC events list is populated with an initial set of common events. You can add events specific to your app through the Window menu.
Select Window > Manage PM Events.
Click the state corresponding to the desired event and select Visible or Favorite.
If you plan on recording the same PMC events frequently, save them in a template. Otherwise, they will be lost when you close the document. For information on saving a trace template, see “Saving an Instruments Trace Template.”
Tracking a Single Event
The Event Profiler instrument tracks performance monitor interrupt (PMI) events, but in this case, only one event is tracked and you can get more detail about it. You set the size of each sample set collected by the Event Profiler instrument. Event Profiler then provides you with information about how active the tracked PMI event was during the sample. Use the track pane slider to identify those samples showing a lot of activity. After you have identified high activity areas, use the Detail pane to get more information on each particular example.
Choose Window > Manage PM Events.
Click the state corresponding to that event and select Visible or Favorite.
In the Threshold field, enter the number of times an event must occur before a sample is taken.
Choose the PMI event you want to track by selecting it from the Event Name pop-up menu control. If the required event is not listed, make sure that you have enabled it. See “To add new PMC events” to create a new event. Figure 10-1 shows the Event Profiler tracking a single PMC event.
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.
The Developer Setting appears only after the 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.
Turn on developer logging in 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 radios that you don’t actively need. You can verify if you have turned off a particular radio using the Energy Diagnostics trace template. Each radio is depicted with red in the track pane to designate that it is on and black to designate that it is turned off.
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 threads 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 four threads being tracked.
Select the Multicore trace template.
Run your app.
Choose the threads to examine by selecting the checkbox in the Mark column in the Detail pane.
The following actions are captured in the track pane. Color notations are the default color for each action, 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. The CPU strategy Usage view facilitates comparison of 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 instrument.
Choose your app from the Choose Target pop-up menu.
Click Record, exercise your app, and then click Stop to capture data.
Click the CPU strategy button.
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.