By profiling the execution speed of generated code, you can help verify that the code meets execution speed requirements.
Profiling can be especially important early in the development cycle for identifying potential architectural issues that can be more expensive to address later in the process. Profiling can also identify bottlenecks and procedural issues that indicate a need for optimization, for example, with an inner loop or inline code.
Note
If you have an Embedded Coder® license, see Code Execution Profiling (Embedded Coder) for an alternative and simpler approach based on software-in-the-loop (SIL) or processor-in-the-loop (PIL) simulations.
You can profile code generated with code generation technology by using a Target Language Compiler (TLC) hook function interface.
To use the profile hook function interface:
For your system target file, create a TLC file that defines the following hook functions. Write the functions so that they specify profiling code. The code generator adds the hook function code to code generated for atomic systems in the model.
Function | Input Arguments | Output Type | Description |
---|---|---|---|
ProfilerHeaders | void | Array of header file names | Return an array of the header file names to be included in the generated code. |
ProfilerTypedefs | void | typedefs | Generate code statements for profiler type definitions. |
ProfilerGlobalData | system | Global data for the specified
system | Generate code statements that declare global data. |
ProfilerExternDataDecls | system | extern declarations for the specified
system | Generate code statements that create global
extern declarations. |
ProfilerSystemDecls | system ,
functionType | Declarations for the specified system
for the specified functionType | Generate code for required variable declarations within
the scope of an atomic subsystem
Output , Update ,
OutputUpdate , or
Derivatives function. |
ProfilerSystemStart | system ,
functionType | Profiler start commands for the specified
system and
functionType | Generate code that starts the profiler within the scope
of an atomic subsystem Output ,
Update ,
OutputUpdate , or
Derivatives function. |
ProfilerSystemFinish | system ,
functionType | Profiler end commands for the specified
system and
functionType | Generate code that stops the profiler within the scope of
the Output ,
Update ,
OutputUpdate , or
Derivatives functions of an atomic
subsystem. |
ProfilerSystemTerminate | system | Profiler termination code for the specified
system | Generate code that terminates profiling (and possibly reports results) for an atomic subsystem. |
In your
file,
define the following global variables.target
.tlc
Define... | To Be... |
---|---|
TLC_TRUE or 1 to turn on profiling
(TLC_FALSE or 0 to turn off
profiling) | |
The name of the TLC file that you created in step 1 |
A quick way to define global variables is to define the parameters with
the -a
option. You can apply this option by using the
set_param
command to set the model configuration
parameter TLCOptions
. For example,
>> set_param(gcs,'TLCOptions', ... '-aProfileGenCode=1 -aProfilerTLC="rtwdemo_profile_hook.tlc"')
Consider setting configuration parameters for generating a code generation report. You can then examine the profiling code in the context of the code generated for the model.
Build the model. The build process embeds the profiling code in the hook function locations in the generated code for the model.
Run the generated executable file. In the MATLAB Command Window, enter
!
. You see
the profiling report you programmed in the profiling TLC file that you
created. For example, a profile report could list the number of calls made
to each system in a model and the number of CPU cycles spent in each
system.model-name
For details on programming a .tlc
file and defining TLC
configuration variables, see Target Language Compiler.
The TLC hook function interface for profiling code execution speed does not
support the S-function system target file (rtwsfcn.tlc
).