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
rtwdemo_sil_topmodel model, which has two subsystems
CounterTypeB. At the command line,
To run a SIL simulation that generates execution-time metrics, on the SIL/PIL tab:
In the Mode section, select SIL/PIL Simulation Only.
In the Prepare section, specify these settings:
System Under Test —
SIL/PIL Mode —
To measure code execution times for the subsystems, in the Prepare section:
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
Click the Save Options button until it displays
ALL DATA. This action sets the Save options configuration parameter to
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
Under Coverage, click the Coverage Collection button off, which disables code coverage analysis. You collect code coverage data separately from profiling data.
Click the Settings button to open the Configuration Parameters dialog box. On the Data Import/Export pane, clear Single simulation output.
To view streamed execution times during the simulation, open the Simulation Data Inspector. In the Results section, click .
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
If you select the Data Import/Export > Single simulation output check box, the simulation creates the variable in your specified
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
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
rtwdemo_sil_topmodel_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 ellipses above the call site.
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.
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:
Code Execution Profiling Report
At the end of the simulation, you can open this report through the Profiling display window or with this line command:
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
A task represented by the step function
step [0.1 0].
Functions generated from the subsystems
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
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');
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,
The pie charts, which display average and maximum execution time distributions, can help you to identify functions that are bottlenecks in code execution.
Return to the summary in the report. The following table describes the information provided in the code section profiles.
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,
|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 Time||Longest time between start and end of code section.|
|Average Execution Time||Average time between start and end of code section.|
|Maximum Self Time||Maximum execution time, excluding time in child sections.|
|Average Self Time||Average execution time, excluding time in child sections.|
|Calls||Number of calls to the code section.|
Icon that you click to see the profiled code section in the Code view of the SIL/PIL app. The code section can be a task or a function.
The specified workspace variable, for
Icon that you click to display the profiled code section in the Command
Window. Equivalent to executing the command
specified workspace variable, for example,
Icon that you click to display measured execution times with Simulation Data Inspector.
The specified workspace variable, for example,
Icon that you click to display the execution-time distribution for the profiled code section.
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
In the Workspace browser, rename
Run another SIL simulation.
To create a report that compares the latest performance against the baseline, in the Command Window, run:
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.
Visualize Task Scheduling
You can use the Simulation Data Inspector to visualize task scheduling and the order of function calls. At the end of the simulation, perform one of these actions: