View and Compare Code Execution Times - MATLAB & Simulink (original) (raw)

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:

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 subsystemsCounterTypeA and CounterTypeB. At the command line, enter:

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

To run a SIL simulation that generates execution-time metrics, on theSIL/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 thePrepare section:
    1. Click the Settings arrow.
    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 displaysALL DATA. This action sets the Save options configuration parameter to All data.
      • Click the Functions button until it displaysCOARSE. This action sets the Measure function execution times configuration parameter toCoarse (referenced models and subsystems only), which provides execution-time metrics for the functions generated from the subsystems CounterTypeA andCounterTypeB.
    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 clickOK.
  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 > check box is selected, the simulation creates the variable in your specifiedSimulink.SimulationOutput object.

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

Simulation Data Inspector display shows variation of execution times for step function.

When the simulation is complete, the profiled model components are colored blue. For top-model SIL or PIL simulations, the Simulink® Editor background is also colored blue. The SIL/PIL tab displays two additional panels:

To view execution-time metrics for top-model tasks, click the blue-shaded background. Or, for a profiled component, click a blue-shaded block. For example, subsystemCounterTypeB.

Blue-shaded model and Profiling details panel that displays execution-time metrics for counter type B.

In addition to execution-time metrics, the Profiling details panel provides links to:

You can trace from a model component to the profiled code section in the Code view. In the Profiling details panel, 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.

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

annotate(executionProfile)

Analyze Task and Function Execution Times

To open the Code Profile Analyzer:

  1. Click the SIL/PIL tab.
  2. In the Results gallery, under Execution Profiling Results, click Code Profile Analyzer.

Or, in the Command Window, enter:

coder.profile.show(executionProfile)

The app displays information in the Task Execution andSummary panels.

View Task Metrics

In the Task Execution panel, the Task Execution Times view shows execution-time metrics for these generated code sections:

Task Execution Times view shows execution-time metrics for initialize and step functions.

For each code section, the table provides this information:

By default, the app displays time in nanoseconds (10-9 seconds). To change the time unit, in the Settings section, from theTime unit drop-down list, select another unit, for example,Microseconds (us).

The table displays time in seconds only if the timer is calibrated, that is, the number of timer ticks per second is known:

The Results section of the toolstrip provides features that you can use in your analysis of tasks. For example, to view the step [0.1 0] call in generated code:

  1. Click the row that contains step [0.1 0].
  2. In the Results section, click Highlight Code. The Simulink Editor displays the call in the Code view.
    Code view shows step function in between instrumentation calls.

View CPU Workload

The CPU Utilization view provides information about the CPU workload, which enables you to determine whether you can run the generated code on your target hardware.

CPU Utilization view shows average and maximum CPU utilization values.

The table provides this information:

The software computes the percentage of CPU time assigned to a task by dividing the task execution time by sample time.

The software generates CPU Utilization metrics only if the number of timer ticks per second is known, that is, a value is specified in the Timer Ticks per Second (MHz) field.

Summary Panel (Execution-Time)

Summary panel shows information about simulation.

The Summary panel provides miscellaneous simulation information:

View Function Metrics

On the toolstrip, in the Analysis section, clickFunction Execution.

The Function Execution Times view provides a function-call tree and execution-time metrics for the tasks and their child functions, that is, the functions generated from subsystems CounterTypeA andCounterTypeB.

Function Execution Times view shows function-call tree and execution-time metrics for tasks and child functions.

The Relative Function Execution Times view provides function execution times as percentages of caller function and total execution times, which can help you to identify performance bottlenecks in generated code.

Relative Function Execution Times view shows function execution times as percentages of caller function and total execution times.

The table contains these metrics.

Column Comparison Performed
Self Time / Caller (%) Function self time compared with the total execution time of the caller function.
Self Time / Task (%) Function self time compared with the total execution time of the task.
Self Time / Simulation (%) Function self time compared with the total execution time of the simulation.
Function / Simulation (%) Function execution time, which includes self time and time spent in child functions, compared with the total execution time of the simulation.

The Results section of the toolstrip provides features that you can use in your analysis of functions.

To trace the model component associated with a generated function and its metrics:

  1. In the function-call tree or a metrics table, click the function. For example,CounterTypeA, which is called by step [0.1 0].
  2. On the toolstrip, in the Results section, clickHighlight Source. The Simulink Editor identifies the subsystem.

Simulink Editor identifies subsystem by making name label of subsystem red.

To view the function call in generated code, in the Results section, click Highlight Code. The Simulink Editor displays the call in the Code view.

Code view highlights call to function.

To display the execution-time distribution for the function, in theResults section, click Generate Distribution.

Execution-time distribution for counter type A function.

CounterTypeA is nested in caller function step [0.1 0]. To view the relative execution times of caller and called functions:

  1. In the function-call tree or a metrics table, click the caller function step [0.1 0].
  2. On the toolstrip, in the Results section, clickFunction Distribution, which generates these pie charts.

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

Use the pie charts to identify functions that are bottlenecks in code execution.

To display measured execution times in the Simulation Data Inspector, in the Results section, clickExport to SDI. The Simulation Data Inspector displays execution times for CounterTypeA. UnderFilter Signals, if you also select thestep [0.1 0] and CounterTypeB check boxes, you can see how execution times for the step and child functions vary over simulation time.

Simulation Data Inspector shows variation of execution times for the step and child functions over simulation time.

The execution time for the step function step [0 0.1] is the sum of its self-time and execution times for the child functions CounterTypeA and CounterTypeB. You can use the Simulation Data Inspector to manage and compare plots from various simulations.

Create Execution-Time Profiling Reports

To view execution-time metrics for the generated code in an HTML report, in theResults section, click Generate Report. For more information, see Code Execution Profiling Report.

If you have a MATLAB Report Generator™ license, you can produce a PDF report of the analysis performed. In theResults section, click Create PDF.

Visualize Function-Call Stack

You can visualize the function call-stack at the simulation step where a task takes longest to execute.

  1. On the toolstrip, in the Analysis section, clickFunction-Call Stack.
  2. From the Task to analyze drop-down list, select a code section.
  3. Note the Longest execution at field value, the simulation time at which execution of the code section is the longest.
  4. Use the default value for Specify simulation time. Modify the value only if you want to visualize the function-call stack at a different simulation time.
  5. Click Display.

The panel displays:

Panel shows function-call stack, function-call tree, and execution-time and self-time values for profiled code sections.

Compare Execution Times Against Baseline

You can compare execution-time metrics from the current simulation against values from a baseline simulation.

  1. On the toolstrip, in the Analysis section, clickComparison. The Comparison panel displays the Workspace variable and three controls.
    Section of Comparison panel that shows workspace variable name and three controls.
  2. From the Select a profile variable drop-down list, select the workspace variable that contains results from the baseline simulation.
  3. Click Compare.

The panel displays:

The table cells are colored:

If the workspace variable for the baseline simulation does not contain data for a code section, the panel displays NaN.

To invert the comparison order, that is, make results from the current simulation the reference, click Invert order.

Section of Comparison panel shows inverted comparison order.

View Code Execution Profiles for Multiple Model Blocks

If you run a top-model simulation that executes multiple Model blocks in SIL or PIL mode with code execution profiling enabled, you can use the Code Profile Analyzer to view execution-time metrics for code generated from the blocks.

  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 TestModel blocks in SIL/PIL mode
    • Top Model ModeNormal
  4. Model block Counter A is already configured to run in SIL mode. For Model block Counter B, specify these parameters:
    • Simulation modeSoftware-in-the-loop (SIL)
    • Code interfaceNormal
  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 theActions 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 inout, a Simulink.SimulationOutput object.
  7. When the simulation is complete, in the Results gallery, underExecution Profiling Results, click Code Profile Analyzer. In the Task Execution panel, theTask Execution Times view shows execution-time metrics for the generated code sections.
    Task Execution Times view shows execution-time metrics for code sections.

For information about generating:

Visualize Task Scheduling

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

Simulation Mode What You Can Observe How
SIL or PIL Task scheduling and the order of function calls When the SIL or PIL simulation is complete, perform one of these actions: In the Results gallery of the Code Profile Analyzer, click Generate Schedule. See Visualize Task Scheduling in SIL Simulation.Run the schedule function. See Visualize Task Scheduling in SIL Simulation.From the SIL/PIL tab, in theResults gallery, click Generate Schedule. See Analyze Results and Export Test Cases.
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

annotate | report | schedule | SIL/PIL Manager | Code Profile Analyzer

Topics