Main Content

View and Compare Code Execution Times

You can run a software-in-the-loop (SIL), processor-in-the-loop (PIL), or XCP-based external mode simulation that produces execution-time metrics for your generated code.

During the simulation, you can use the Simulation Data Inspector to observe streamed execution times. In an XCP-based external mode simulation, you can also visualize task scheduling and related target hardware activity.

At the end of the simulation, you can:

  • View execution-time metrics for a profiled model component.

  • Open a report of execution-time metrics for profiled components. Through report icons, you can view, for example, the profiled code section and the execution-time distribution for each profiled code section.

  • Use the Simulation Data Inspector to plot and compare execution times from various simulations.

Run Simulation That Generates Execution-Time Metrics

In this section, to generate execution-time metrics, you run a SIL simulation. For an example that runs an XCP-based external mode simulation, see Visualize Task Scheduling in XCP External Mode Simulation.

Open the SILTopModel model, which has two subsystems CounterTypeA and CounterTypeB. At the command line, enter:

openExample('ecoder/SILPILVerificationExample', ...
             supportingFile='SILTopModel.slx')

To run a SIL simulation that generates execution-time metrics, on the SIL/PIL tab:

  1. In the Mode section, select SIL/PIL Simulation Only.

  2. In the Prepare section, specify these settings:

    1. System Under TestTop model

    2. SIL/PIL ModeSoftware-in-the-Loop (SIL)

  3. To measure code execution times for the subsystems, in the Prepare section:

    1. Click Settings.

    2. Under Time Profiling:

      • Click the Task Profiling button on. This action selects the Measure task execution time configuration parameter, which provides execution-time metrics for the task generated from the top model SILTopModel.

      • Click the Save Options button until it displays ALL DATA. This action sets the Save options configuration parameter to All data.

      • Click the Functions button until it displays COARSE. This action sets the Measure function execution times configuration parameter to Coarse (referenced models and subsystems only), which provides execution-time metrics for the functions generated from the subsystems CounterTypeA and CounterTypeB.

    3. Under Coverage, click the Coverage Collection button off, which disables code coverage analysis. You collect code coverage data separately from profiling data.

    4. Click the Settings button to open the Configuration Parameters dialog box. On the Data Import/Export pane, clear Single simulation output and then click OK.

  4. To view streamed execution times during the simulation, open the Simulation Data Inspector. In the Results section, click .

  5. In the Run section, click Run SIL/PIL.

In the MATLAB® base workspace, the simulation generates a variable with the default name, executionProfile, which stores the execution profiling data. You can specify an alternative name through the Workspace variable configuration parameter.

Note

If the Data Import/Export > Single simulation output check box is selected, the simulation creates the variable in your specified Simulink.SimulationOutput object.

During the simulation, use the Simulation Data Inspector to observe, for example, the variation of execution times for the step function.

When the simulation is complete, the profiled model components are colored blue and the Code view displays the generated code. To view execution-time metrics for a profiled component, click the component. For example, subsystem CounterTypeB.

For top-model SIL or PIL simulations, the Simulink® Editor background is also colored blue. When you click the background, the display window shows execution-time metrics for top-model tasks.

You can trace from a model component to the profiled code section in the Code view. In the Profiling display window, on a code section row, click the icon . For example, if you click the model background and then click the icon for the SILTopModel_initialize task, you see the measurement probes around the call site in the SIL application. To see the profiling metrics for the component, place your cursor on the call site.

Initialization function call with tooltip showing table of code profiling metrics.

If you click the icon for a function, the call site is highlighted. The Code view displays the average execution time and number of function calls to the left of the code line. In this example, the maximum and average times are the same because the initialization function was called only one time.

Highlighted function call site for CounterTypeA function.

The Profiling display window also has links to:

  • The complete profiling report, which provides execution-time metrics for all profiled code sections.

  • The Simulation Data Inspector, which allows you to plot and compare execution-time measurements for the profiled code section.

  • The execution-time distribution for the profiled code section.

  • Pie charts that show the relative execution times of caller and called functions

If you close the model or display window, you can reopen the colored model and display window with this line command:

annotate(executionProfile)

Code Execution Profiling Report Sections

At the end of the simulation, you can open the report through the Profiling display window or by using this line command:

report(executionProfile)
The report contains these sections:

  1.  Summary

  2.  Feasibility Checks

  3.  Profiled Sections of Code

  4.  Execution Times in Percentages

  5.  CPU Utilization

  6.  Definitions

View Code Execution Profiles for Multiple Model Blocks

You can configure a top-model simulation that runs multiple Model blocks in SIL or PIL mode with code execution profiling enabled. When you run the simulation, it generates separate code execution profiling reports for the referenced models.

  1. Open the top model that contains Model blocks. For example, at the command line, enter:

    openExample('ecoder/SILPILVerificationExample', ...
                 supportingFile='SILModelBlock.slx')

  2. On the SIL/PIL tab, in the Mode section, specify SIL/PIL Simulation Only.

  3. In the Prepare section, specify these settings:

    • System Under Test –– Model blocks in SIL/PIL mode

    • Top Model Mode –– Normal

  4. Model block Counter A is already configured to run in SIL mode. For Model block Counter B, set the block parameter Simulation mode to Software-in-the-loop (SIL).

  5. Both Model blocks reference the model SILCounter. To generate execution times for the Model blocks:

    1. Select CounterA or CounterB.

    2. On the Model Block tab, in the Actions section, click Open as Top Model.

    3. On the SIL/PIL tab, in the Settings gallery, click Functions to COARSE, which sets the Measure function execution times configuration parameter for SILCounter to Coarse (referenced models and subsystems only).

  6. To run a top-model simulation:

    1. Select the SILModelBlock window.

    2. On the SIL/PIL tab, in the Run section, click Run SIL/PIL.

    Since the Single simulation output check box is selected, the simulation creates the variable executionProfile in out, a Simulink.SimulationOutput object.

  7. When the simulation is complete, to show the number of Model blocks that are profiled, in the Command Window, enter:

    NumOfReports=out.executionProfile.getModuleNumber()
    NumOfReports =
         2

  8. To open the report for CounterA, the first Model block, run:

    report(out.executionProfile.getModule(1))

  9. In the Summary section, the report provides a link to the second Model block report. To open the report for CounterB, click Open report or run:

    report(out.executionProfile.getModule(2))

For more information about generating function execution times for model reference hierarchies, see Control Profiling Granularity.

Compare Code Execution-Time Performance Against Baseline

You can create a code execution profiling report that compares the performance of the generated code against the performance of a reference version. For example, suppose you want to use the current execution-time metrics in executionProfile as the baseline for SILTopModel.

  1. In the Workspace browser, rename executionProfile to executionProfileBaseline.

  2. Run another SIL simulation.

  3. To create a report that compares the latest performance against the baseline, in the Command Window, run:

    report(executionProfile,'baseline', executionProfileBaseline)

In the report, the Comparison with Baseline Performance and CPU Utilization sections provide comparisons of execution-time metrics. The differences in execution-time metrics are color-coded:

  • Red — An increase in execution times or CPU usage.

  • Green — A decrease in execution times or CPU usage.

  • Black — No change.

Visualize Task Scheduling

You can use the Simulation Data Inspector to visualize task scheduling and related target hardware activity.

Simulation ModeWhat You Can ObserveHow

SIL or PIL

Task scheduling and the order of function calls

When the SIL or PIL simulation is complete, perform one of these actions:

XCP-based external

Task activity, task diagnostics (run-time only), CPU activity, and CPU utilization

During the XCP external mode simulation, use the Simulation Data Inspector to view displays for task scheduling and related activity.

After the simulation, use the schedule function to regenerate displays (except for task diagnostics).

For more information, see Visualize Task Scheduling in XCP External Mode Simulation.

See Also

| |

Related Examples

More About