Introduction to the profiler
Reasons for using the profiler
Function profiling can help you find the functions in your source code where the most time is spent during execution. You should focus on those functions when optimizing your code. A simple method of optimizing a function is to compile it using speed optimization. Alternatively, you can move the data used by the function into more efficient memory. For detailed information about efficient memory usage, see Efficient coding for embedded applications.
Alternatively, you can use filtered profiling, which means that you can exclude, for example, individual functions from being profiled. To profile only a specific part of your code, you can select a time interval—using the Timeline window—for which C-SPY produces profiling information.
Instruction profiling can help you fine-tune your code on a very detailed level, especially for assembler source code. Instruction profiling can also help you to understand where your compiled C/C++ source code spends most of its time, and perhaps give insight into how to rewrite it for better performance.
Briefly about the profiler
Function profiling information is displayed in the Function Profiler window, that is, timing information for the functions in an application. Profiling must be turned on explicitly using a button on the window’s toolbar, and will stay enabled until it is turned off.
Instruction profiling information is displayed in the Disassembly window, that is, the number of times each instruction has been executed.
Profiling sources
The profiler can use different mechanisms, or sources, to collect profiling information. Depending on the available trace source features, one or more of the sources can be used for profiling:
The full instruction trace is analyzed to determine all function calls and returns. When the collected instruction sequence is incomplete or discontinuous, the profiling information is less accurate.
Each instruction in the full instruction trace or each PC Sample is assigned to a corresponding function or code fragment, without regard to function calls or returns. This is most useful when the application does not exhibit normal call/return sequences, such as when you are using an RTOS, or when you are profiling code which does not have full debug information.
Trace data for the E1, E2, and E2 Lite emulators and the J-Link debug probe is very limited. If you are using an E1, E2, E2 Lite or EZ-CUBE2 emulator or a J-Link debug probe, PC sampling provides much better profiling data than any other source.
Power sampling
Some debug probes support sampling of the power consumption of the development board, or components on the board. Each sample is associated with a PC sample and represents the power consumption (actually, the electrical current) for a small time interval preceding the time of the sample. When the profiler is set to use power sampling, additional columns are displayed in the Profiler window. Each power sample is associated with a function or code fragment, just as with regular PC sampling.
Note
This does not imply that all the energy corresponding to a sample can be attributed to that function or code fragment. The time scales of power samples and instruction execution are vastly different—during one power measurement, the CPU has typically executed many thousands of instructions. Power sampling is a statistics tool.
Requirements for using the profiler
The C-SPY simulator supports the profiler; there are no specific requirements.
To use the profiler in your hardware debugger system, you need one of these setups:
A target board with built-in J-Link or a J-Link/J-Link Ultra debug probe and a J-Link RX adapter
An E1, E2, E20, E2 Lite or an EZ-CUBE2 emulator.
This table lists the C-SPY driver profiling support:
C-SPY driver | Trace (calls) | Trace (flat) | Sampling | Power |
|---|---|---|---|---|
C-SPY simulator | Yes | Yes | — | — |
C-SPY E1/E20 | — | Yes | Yes | — |
C-SPY E2 | — | Yes | Yes | Yes |
C-SPY E2 Lite | — | Yes | Yes | — |
C-SPY EZ-CUBE2 | — | Yes | Yes | — |
C-SPY J-Link | — | Yes | Yes | Yes |