Main Content

View and Compare Code Execution Times

You run a software-in-the-loop (SIL) or processor-in-the-loop (PIL) simulation that produces execution-time metrics for tasks and functions in your generated code. During the simulation, you can use the Simulation Data Inspector to observe streamed execution times. 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 all profiled components. Through report icons, you can view, for example, the profiled code section and the execution-time distribution for each profiled function.

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

Run SIL Simulation That Generates Execution-Time Metrics

Open the rtwdemo_sil_topmodel model, which has two subsystems CounterTypeA and CounterTypeB.

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

      • Click the Save Options button until it displays ALL DATA. This action set 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.

  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. You can specify an alternative name through the Workspace variable configuration parameter.

Note

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

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

The display window also has links to:

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

  • The profiled code section in the code generation report.

  • 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

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.

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

At the end of the simulation, you can open this report through the metrics display window or with this line command:

>> report(executionProfile)

Part 1 provides a summary. Part 2 contains information about profiled code sections. Expand and collapse profiled sections in part 2 by clicking [+] and [–] respectively. This graphic shows fully expanded sections.

The report contains time measurements for:

  • The model initialization function initialize.

  • A task represented by the step function step [0.1 0].

  • Functions generated from the subsystems CounterTypeA and CounterTypeB.

You can go to a profiled code section in the Generated Code view of the Code Generation Report. In the Code Execution Profiling Report, on a code section row, click the icon . For example, if you click the icon for the rtwdemo_sil_topmodel_initialize task, you see the measurement probes around the call site in the SIL application.

If you click the icon for a function, the call site is highlighted.

From the Code Execution Profiling Report, you can trace the model component that produces a set of metrics. For example, in the Section column, if you click the CounterTypeA hyperlink, the Simulink Editor identifies the subsystem.

By default, the report displays time in nanoseconds (10-9 seconds). You can specify the time unit and numeric display format. For example, to display time in microseconds (10-6 seconds), use the following command:

report(executionProfile,...
         'Units', 'Seconds', ...
         'ScaleFactor', '1e-06', ...
         'NumericFormat', '%0.3f')

The report displays time in seconds only if the timer is calibrated, that is, the number of timer ticks per second is known. On a Windows® machine, the software determines this value for a SIL simulation. On a Linux® machine, calibrate the timer manually. For example, if your processor speed is 1 GHz, specify the number of timer ticks per second:

executionProfile.TimerTicksPerSecond = 1e9;

To view execution-time metrics for a task or function in the Command Window, on the corresponding row, click .

>> out.get('executionProfile').Sections(5)
  ExecutionTimeCodeSection with properties:

                            Name: 'CounterTypeA'
                          Number: 5
            ExecutionTimeInTicks: [1×101 uint64]
                 SelfTimeInTicks: [1×101 uint64]
           TurnaroundTimeInTicks: [1×101 uint64]
       TotalExecutionTimeInTicks: 24332
            TotalSelfTimeInTicks: 24332
      TotalTurnaroundTimeInTicks: 24332
     MaximumExecutionTimeInTicks: 502
     MaximumExecutionTimeCallNum: 10
          MaximumSelfTimeInTicks: 502
          MaximumSelfTimeCallNum: 10
    MaximumTurnaroundTimeInTicks: 502
    MaximumTurnaroundTimeCallNum: 10
                        NumCalls: 101
          ExecutionTimeInSeconds: [1×101 double]
                            Time: [101×1 double]

To display measured execution times in the Simulation Data Inspector, click the icon . Use the Simulation Data Inspector to manage and compare plots from various simulations.

To display the execution-time distribution, click the icon .

In this example, to create the histogram, the software uses these commands:

section=executionProfile.Sections(5);
data=section.ExecutionTimeInSeconds;
histogram(data, 30,'Normalization','probability');
The software creates the histogram only if the number of calls is greater than one.

Note

To observe how code sections are invoked over the execution timeline, use the timeline function.

If the generated code for your model contains nested functions, the simulation generates pie charts that show the relative execution times of caller and called functions. To display the pie charts, click the icon for the caller function. For example, step [0.1 0].

The pie charts, which display average and maximum execution time distributions, can help you to identify functions that are bottlenecks in code execution.

The following table describes the information provided in the code section profiles.

ColumnDescription
Section

Name of task, top model, subsystem, or Model block. Click the link to go to the model.

With a task, the sample period and sample offset are listed next to the task name. For example, rtwdemo_sil_topmodel_step [0.1 0] indicates that the sample period is 0.1 seconds and the sample offset is 0.

Maximum Turnaround Time (Simulink Real-Time™ and support packages in real-time mode) Longest time between start and end of code section. Includes preemption time.
Average Turnaround Time (Simulink Real-Time and support packages in real-time mode) Average time between start and end of code section. Includes preemption time.
Maximum Execution TimeLongest time between start and end of code section.
Average Execution TimeAverage time between start and end of code section.
Maximum Self TimeMaximum execution time, excluding time in child sections.
Average Self TimeAverage execution time, excluding time in child sections.
CallsNumber of calls to the code section.

Icon that you click to see the profiled code section in the Generated Code view of the Code Generation Report. The code section can be a task or a function.

The specified workspace variable, for example, executionProfile, must be present in the base workspace.

Icon that you click to display the profiled code section in the Command Window. Equivalent to executing the command executionProfile.Sections(i).

The specified workspace variable, for example, executionProfile, must be present in the base workspace.

Icon that you click to display measured execution times with Simulation Data Inspector.

The specified workspace variable, for example, executionProfile, must be present in the base workspace.

Icon that you click to display the execution-time distribution for the profiled code section.

The specified workspace variable, for example, executionProfile, must be present in the base workspace.

Icon that you click to display pie charts that show the relative execution times of caller and called functions in the generated code.

To help you to decide whether you can run the generated code on your target hardware, part 3 of the code execution profiling report provides CPU workload information:

  • Average and maximum CPU time required by each task during a sample period.

  • Total CPU usage in a sample period.

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

The software generates CPU Utilization only if the number of timer ticks per second is specified.

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

  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 part 3 (Comparison with Baseline Performance) and part 4 (CPU Utilization), the report provides a comparison 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.

See Also

|

Related Examples

More About