Collect Code Coverage Metrics with Simulink® Coverage™

This example shows how to collect code coverage metrics during a software-in-the-loop (SIL) or processor-in-the-loop (PIL) simulation with Simulink® Coverage™.

You use the code coverage tool and code coverage report to view the recorded code coverage for a SIL simulation.

In this example, you measure model coverage during a simulation in normal mode, repeat the same simulation in SIL mode, and compare the recorded metrics from both simulations.

Compare model coverage and code coverage results by using the hyperlinks in the model coverage and code coverage reports.

For more examples of measuring SIL and PIL simulations, see .

Initial Setup

Open the model.

model = 'rtwdemo_sil_topmodel';
close_system(model,0)
open_system(model)

Remove any existing build folders.

buildFolder = RTW.getBuildDir(model);
if isfolder(buildFolder.BuildDirectory)
    rmdir(buildFolder.BuildDirectory,'s');
end

Configure the model for coverage collection.

set_param(model, 'CovEnable', 'on')
clear covCumulativeData

Set up the input data.

T = 0.1; % sample time
[ticks_to_count, reset, counter_mode, count_enable, ...
    counter_mode_values_run1, counter_mode_values_run2, ...
    count_enable_values_run1, count_enable_values_run2] = ...
    rtwdemo_sil_topmodel_data(T);

Run the First Simulation in Normal Mode

After the simulation completes, the model coverage report opens. To navigate from blocks in the model to the corresponding sections of the coverage report, use the coverage display window.

counter_mode.signals.values = counter_mode_values_run1;
count_enable.signals.values = count_enable_values_run1;
set_param(model, 'SimulationMode', 'normal');

Use the Simulation Data Inspector to view and compare simulation results.

Simulink.sdi.view;
Simulink.sdi.clear;

Run the simulation.

simout_normal_run1 = sim(model, 'ReturnWorkspaceOutputs', 'on');

Capture the results.

Simulink.sdi.createRun('Run 1 (normal mode)', 'namevalue',...
                       {'simout_normal_run1'}, {simout_normal_run1});

Run the Second Simulation in Normal Mode

For the first simulation, the report shows that the model achieved less than 100% MCDC coverage. Run a second simulation with different input signals to increase the level of MCDC coverage to 100%. The model coverage report is configured to show cumulative coverage across both simulation runs.

counter_mode.signals.values = counter_mode_values_run2;
count_enable.signals.values = count_enable_values_run2;
set_param(model, 'SimulationMode', 'normal');

simout_normal_run2 = sim(model, 'ReturnWorkspaceOutputs', 'on');

Simulink.sdi.createRun('Run 2 (normal mode)', 'namevalue',...
                       {'simout_normal_run2'}, {simout_normal_run2});            

Configure the Model to Measure Code Coverage

Before running a SIL simulation, configure the model to collect code coverage metrics.

coverageSettings = get_param(model, 'CodeCoverageSettings');
coverageSettings.CoverageTool = 'Simulink Coverage';
set_param(model, 'CodeCoverageSettings', coverageSettings);

Run the First Simulation in SIL Mode

You can use the same input signals in the SIL simulation that you used during the first simulation run in normal mode.

Run the first simulation in SIL mode.

counter_mode.signals.values = counter_mode_values_run1;
count_enable.signals.values = count_enable_values_run1;
set_param(model, 'SimulationMode', 'software-in-the-loop');
set_param(model, 'CodeExecutionProfiling', 'off');
set_param(model, 'CodeProfilingInstrumentation', 'off');
simout_sil_run1 = sim(model, 'ReturnWorkspaceOutputs', 'on');
### Starting build procedure for model: rtwdemo_sil_topmodel
### Successful completion of build procedure for model: rtwdemo_sil_topmodel
### Preparing to start SIL simulation ...
Building with 'gcc'.
MEX completed successfully.
### Updating code generation report with SIL files ...
### Starting SIL simulation for component: rtwdemo_sil_topmodel
### Stopping SIL simulation for component: rtwdemo_sil_topmodel
Simulink.sdi.createRun('Run 1 (SIL mode)', 'namevalue',...
                       {'simout_sil_run1'}, {simout_sil_run1});

When the simulation completes, view the code coverage results on the model by using coverage highlighting. To see the SIL code coverage summary for a model element, place your curser over the model element.

You can also view the code coverage results in the HTML code coverage report. The summary section shows that all functions have been called, but the SIL simulation run did not achieve full coverage for decision, condition, or MCDC coverage.

To navigate to the corresponding model elements in the block diagram, use the hyperlinks in the code coverage report

Run the Second Simulation in SIL mode

Use the same input signals in the SIL simulation that you used in the second simulation run in normal mode.

counter_mode.signals.values = counter_mode_values_run2;
count_enable.signals.values = count_enable_values_run2;
set_param(model, 'SimulationMode', 'software-in-the-loop');
set_param(model, 'CodeExecutionProfiling', 'off');
set_param(model, 'CodeProfilingInstrumentation', 'off');
simout_sil_run2 = sim(model, 'ReturnWorkspaceOutputs', 'on');
### Starting build procedure for model: rtwdemo_sil_topmodel
### Generated code for 'rtwdemo_sil_topmodel' is up to date because no structural, parameter or code replacement library changes were found.
### Successful completion of build procedure for model: rtwdemo_sil_topmodel
### Preparing to start SIL simulation ...
### Starting SIL simulation for component: rtwdemo_sil_topmodel
### Stopping SIL simulation for component: rtwdemo_sil_topmodel
Simulink.sdi.createRun('Run 2 (SIL mode)', 'namevalue',...
                       {'simout_sil_run2'}, {simout_sil_run2});

The code coverage highlighting shows that the generated code from the model achieved full coverage.

Compare Metrics from the Normal and SIL Simulations

The Simulation Data Inspector opens automatically after each run, which allows you to view and analyze the results. To confirm that the logged signals for the SIL and normal mode runs are identical, review the information in the Compare and Inspect panes.