Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

Simulink Coverage を使用したコード カバレッジ メトリクスの収集

この例では、Simulink® Coverage™ を使用したソフトウェアインザループ (SIL) またはプロセッサインザループ (PIL) のシミュレーション中にコード カバレッジ メトリクスを収集する方法について説明します。

コード カバレッジ ツールとコード カバレッジ レポートを使用し、SIL シミュレーションに対して記録されたコード カバレッジを表示します。

Code pane displays code generated from SILTopModel with coverage highlighting and annotations. Cursor is pointed at line 114 to display a tooltip with coverage results. The decision on line 114 shows zero decision outcomes covered.

この例では、ノーマル モードのシミュレーション中にモデル カバレッジを測定し、同じシミュレーションを SIL モードで繰り返して、両方のシミュレーションから記録されたメトリクスを比較します。

モデル カバレッジおよびコード カバレッジ レポート内のハイパーリンクを使用することで、モデル カバレッジおよびコード カバレッジの結果を比較できます。

SIL および PIL のシミュレーションを測定するその他の例については、SIL シミュレーションと PIL シミュレーションを使用した生成コードのテスト (Embedded Coder)を参照してください。

初期設定

モデルを開きます。

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

既存のビルド フォルダーを削除します。

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

カバレッジ収集のモデルを設定します。

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

入力データを設定します。

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);

ノーマル モードで最初のシミュレーションを実行する

シミュレーション完了後、モデル カバレッジ レポートが開きます。モデル内のブロックからカバレッジ レポートの対応する節に移動するには、カバレッジ表示ウィンドウを使用します。

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

シミュレーション データ インスペクターを使用してシミュレーション結果を表示し、比較します。

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

シミュレーションを実行します。

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

モデルを強調表示します。

cvmodelview(simout_normal_run1.covdata);

結果を取得します。

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

ノーマル モードで 2 番目のシミュレーションを実行する

最初のシミュレーションについて、レポートにはモデルが 100% 未満の MCDC カバレッジを達成したことが示されます。異なる入力信号で 2 番目のシミュレーションを実行し、MCDC カバレッジを 100% のレベルに拡大します。モデル カバレッジ レポートは、両方のシミュレーション実行の累積カバレッジを示すよう設定されています。

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');

cvmodelview(simout_normal_run2.covdata);

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

コード カバレッジを測定する対象のモデルの設定

SIL シミュレーションを実行する前に、コード カバレッジ メトリクスを収集するようにモデルを設定します。

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

最初のシミュレーションを SIL モードで実行する

ノーマル モードで最初のシミュレーションの実行時に使用した同じ入力信号を SIL シミュレーションで使用できます。

最初のシミュレーションを SIL モードで実行します。

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: SILTopModel
### Successful completion of build procedure for: SILTopModel

Build Summary

Top model targets built:

Model        Action                        Rebuild Reason                                    
=============================================================================================
SILTopModel  Code generated and compiled.  Code generation information file does not exist.  

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 14.208s
### Preparing to start SIL simulation ...
Building with 'gcc'.
MEX completed successfully.
### Updating code generation report with SIL files ...
### Starting SIL simulation for component: SILTopModel
### Stopping SIL simulation for component: SILTopModel
### Completed code coverage analysis
cvmodelview(simout_sil_run1.covdata);
Simulink.sdi.createRun('Run 1 (SIL mode)', 'namevalue',...
                       {'simout_sil_run1'}, {simout_sil_run1});

シミュレーションが完了したら、カバレッジの強調表示を使用してモデルでコード カバレッジ結果を表示します。モデル要素の SIL コード カバレッジの概要を確認するには、カーソルをモデル要素に合わせます。

SILTopModel.slx simulated in software in the loop mode shows most blocks highlighted red. The cursor is pointed at a Logical Operator block which displays 50% decision coverage, 50% condition coverage, 0% MCDC coverage, and 100% statement coverage.

HTML のコード カバレッジ レポートでコード カバレッジ結果を表示することもできます。[概要] 節には、すべての関数が呼び出されたものの、SIL シミュレーション実行で判定、条件、または MCDC カバレッジのフル カバレッジが達成されなかったことが示されます。

cvhtml('codecovreport',simout_sil_run1.covdata);

The summary section of the code coverage report for SILTopModel.slx displays a row for the generated file SILTopModel.c as well as a child row for each function called by it. Only function and function call coverage metrics are fully satisfied.

ブロック線図で対応するモデル要素に移動するには、コード カバレッジ レポートのハイパーリンクを使用します。

2 番目のシミュレーションを SIL モードで実行する

ノーマル モードで 2 番目のシミュレーションの実行時に使用した同じ入力信号を SIL シミュレーションで使用します。

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: SILTopModel
### Generated code for 'SILTopModel' is up to date because no structural, parameter or code replacement library changes were found.
### Successful completion of build procedure for: SILTopModel

Build Summary

Top model targets built:

Model        Action          Rebuild Reason                           
======================================================================
SILTopModel  Code compiled.  Compilation artifacts were out of date.  

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 3.7025s
### Preparing to start SIL simulation ...
### Starting SIL simulation for component: SILTopModel
### Stopping SIL simulation for component: SILTopModel
### Completed code coverage analysis
Simulink.sdi.createRun('Run 2 (SIL mode)', 'namevalue',...
                       {'simout_sil_run2'}, {simout_sil_run2});

両方の SIL モード シミュレーションからの累積カバレッジを使用してモデルを強調表示し、モデルから生成されたコードがフル カバレッジを達成したことを確認します。

cvmodelview(simout_sil_run1.covdata + simout_sil_run2.covdata);

SILTopModel.slx simulated in software in the loop mode shows most blocks highlighted green. The cursor is pointed at a Logical Operator block which displays SIL: Full coverage.

ノーマル シミュレーションと SIL シミュレーションからのメトリクスを比較する

シミュレーション データ インスペクターは実行するたびに自動的に開くため、結果を表示して解析できます。SIL およびノーマル モードの実行に対してログ記録された信号が同等であることを確認するには、[比較] ペインと [検査] ペインの情報を確認します。