Profile Code Execution Speed

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.

Use the Profile Hook Function Interface

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:

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

    FunctionInput ArgumentsOutput TypeDescription
    ProfilerHeadersvoidArray of header file namesReturn an array of the header file names to be included in the generated code.
    ProfilerTypedefsvoidtypedefsGenerate code statements for profiler type definitions.
    ProfilerGlobalDatasystemGlobal data for the specified systemGenerate code statements that declare global data.
    ProfilerExternDataDeclssystemextern declarations for the specified systemGenerate code statements that create global extern declarations.
    ProfilerSystemDeclssystem, functionTypeDeclarations for the specified system for the specified functionTypeGenerate code for required variable declarations within the scope of an atomic subsystem Output, Update, OutputUpdate, or Derivatives function.
    ProfilerSystemStartsystem, functionTypeProfiler start commands for the specified system and functionTypeGenerate code that starts the profiler within the scope of an atomic subsystem Output, Update, OutputUpdate, or Derivatives function.
    ProfilerSystemFinishsystem, functionTypeProfiler end commands for the specified system and functionTypeGenerate code that stops the profiler within the scope of the Output, Update, OutputUpdate, or Derivatives functions of an atomic subsystem.
    ProfilerSystemTerminatesystemProfiler termination code for the specified systemGenerate code that terminates profiling (and possibly reports results) for an atomic subsystem.
  2. In your target.tlc file, define the following global variables.

    Define...To Be...

    ProfileGenCode

    TLC_TRUE or 1 to turn on profiling (TLC_FALSE or 0 to turn off profiling)

    ProfilerTLC

    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"')
  3. 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.

  4. Build the model. The build process embeds the profiling code in the hook function locations in the generated code for the model.

  5. Run the generated executable file. In the MATLAB Command Window, enter !model-name. 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.

For details on programming a .tlc file and defining TLC configuration variables, see Target Language Compiler.

Profile Hook Function Interface Limitation

The TLC hook function interface for profiling code execution speed does not support the S-function system target file (rtwsfcn.tlc).

Related Topics