Main Content

Collect Code Coverage Metrics with a Third-Party Tool

If you have a Simulink® Coverage™ license, you can collect code coverage metrics during a software-in-the-loop (SIL) or processor-in-the-loop (PIL) simulation –– see Collect Coverage for Code (Simulink Coverage). This example shows how you can collect code coverage metrics with a third-party tool, that is, BullseyeCoverage or LDRA tool suite. You must install the third-party tool –– see Code Coverage Tool Support.

You measure model coverage during a normal mode simulation and then use the third-party tool in a SIL simulation to collect coverage metrics for the generated code. By using the hyperlinks in the model coverage and code coverage reports, you can compare model coverage and code coverage results. For more information about SIL simulations, see Test Generated Code with SIL and PIL Simulations.

Initial Setup

Open the model SILTopModel.

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

Remove existing build folders.

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

Configure generation of model coverage report.

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

Set up the stimulus data by running the function SILTopModelData.

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] = ...
    SILTopModelData(T);

Run a Simulation in Normal Mode

The model is configured to collect model coverage metrics. When a simulation is complete, the model coverage report opens. Use the coverage display window to navigate from blocks in the model to the corresponding sections of the coverage report.

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

Set up Simulation Data Inspector for interactive viewing and comparison of 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 a Second Simulation in Normal Mode

For the first simulation, the report shows that the achieved coverage is less than 100%. Run a second simulation with different input signals that increase the level of MC/DC coverage to 100%. Note that 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, check the availability of third-party tools, and configure the model to collect code coverage metrics. If a third-party tool is not available, the model uses Simulink Coverage.

covToolPath = '';
ldraPath = coder.coverage.LDRA.getPath;
bullseyePath = coder.coverage.BullseyeCoverage.getPath;

coverageSettings = get_param(model,'CodeCoverageSettings');
coverageSettings.TopModelCoverage='on';
if ~isempty(ldraPath)
    coverageSettings.CoverageTool='LDRAcover or LDRA tool suite';
elseif ~isempty(bullseyePath)
    coverageSettings.CoverageTool='BullseyeCoverage';
else
    coverageSettings.CoverageTool='None';
end
set_param(model,'CodeCoverageSettings',coverageSettings);

Run Simulations in SIL Mode

The normal mode simulations produce coverage metrics for the model. With a SIL simulation, you can apply the same input stimulus signals to the generated code and measure code coverage.

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');
Simulink.sdi.createRun('Run 1 (SIL mode)', 'namevalue',...
                       {'simout_sil_run1'}, {simout_sil_run1});

Run the second simulation in SIL 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');
Simulink.sdi.createRun('Run 2 (SIL mode)', 'namevalue',...
                       {'simout_sil_run2'}, {simout_sil_run2});

When a simulation is complete, click the link in the Command Window to open the code coverage report and view the cumulative code coverage results. The link is available only if you have a third-party tool installed.

Use hyperlinks in the code coverage report to go to corresponding locations in the block diagram. Then, by using the coverage display window, you can open corresponding sections of the model coverage report. Compare model coverage and code coverage results.

The Simulation Data Inspector opens automatically, allowing interactive viewing and analysis of the results. Use the Compare and Inspect panes to confirm that the SIL and normal mode logged signals are identical for both runs.

Concluding Remarks

In this example, you:

  • Collected model coverage metrics during a normal mode simulation.

  • Collected code coverage metrics during a SIL simulation.

  • Navigated between the code coverage and model coverage reports.

  • Cross-checked metrics from both reports.

Related Topics