If Simulink® Coverage™ is installed, you can collect code coverage metrics during a software-in-the-loop (SIL) or processor-in-the-loop (PIL) simulation - see Collect Code Coverage Data (Simulink Coverage). In this example, you can collect code coverage metrics with a third-party tool, i.e., BullseyeCoverage or LDRA Testbed. You must install the third-party tool - see Code Coverage Tool Support.
This screen shot shows a code coverage report obtained by running a SIL simulation with a code coverage tool enabled. The annotations depend on the code coverage tool that you specify.
In this example, you measure model coverage during normal mode simulation and repeat the same simulation in SIL mode to measure code coverage. By using the hyperlinks in the model coverage and code coverage reports, you can compare model coverage and code coverage results.
For SIL and PIL simulation examples, see Test Generated Code with SIL and PIL Simulations.
Make sure the model is newly opened.
model='rtwdemo_sil_topmodel';
close_system(model,0)
open_system(model)
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.
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);
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});
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});
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® Verification and Validation™.
covToolPath = ''; ldraPath = coder.coverage.LDRA.getPath; bullseyePath = coder.coverage.BullseyeCoverage.getPath; coverageSettings = get_param(model,'CodeCoverageSettings'); coverageSettings.TopModelCoverage='on'; if ~isempty(ldraPath) coverageSettings.CoverageTool='LDRA Testbed'; elseif ~isempty(bullseyePath) coverageSettings.CoverageTool='BullseyeCoverage'; else coverageSettings.CoverageTool='None'; end set_param(model,'CodeCoverageSettings',coverageSettings);
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});
### Starting build procedure for: 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: rtwdemo_sil_topmodel Build Summary Top model targets built: Model Action Rebuild Reason ============================================================================== rtwdemo_sil_topmodel Code compiled Compilation artifacts were out of date. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 21.252s ### Preparing to start SIL simulation ... Building with 'Microsoft Visual C++ 2017 (C)'. 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 ### Completed code coverage analysis
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});
### Starting build procedure for: 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: rtwdemo_sil_topmodel Build Summary Top model targets built: Model Action Rebuild Reason ============================================================================== rtwdemo_sil_topmodel Code compiled Compilation artifacts were out of date. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 12.723s ### Preparing to start SIL simulation ... ### Starting SIL simulation for component: rtwdemo_sil_topmodel ### Stopping SIL simulation for component: rtwdemo_sil_topmodel ### Completed code coverage analysis
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.
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.