www.gusucode.com > ecoder 案例源码程序 matlab代码 > ecoder/CodeExecutionProfilingWithSILAndPILExample.m
%% Code Execution Profiling Using SIL and PIL Simulations % Produce execution-time metrics for tasks and functions in generated code by % using software-in-the-loop (SIL) or processor-in-the-Loop (PIL) simulations. % Use the code execution report or Simulation Data Inspector to analyze execution % times. % % Copyright 2007-2015 The MathWorks, Inc. %% Top-Model Simulation % Run a SIL or PIL simulation to produce execution time metrics % for the code generated from the top model: % % * You configure the model to load test vectors or stimulus inputs from % the MATLAB(R) workspace. % * You can the top model in SIL or PIL % simulation modes. % % Note that for a PIL simulation, you require a target connectivity % configuration to: % % * Build the target application. % * Download, start, and stop the application on the target. % * Support communication between Simulink and the target. %% Open a simple counter top model. model='rtwdemo_sil_topmodel'; close_system(model,0) open_system(model) %% % Turn off collection and reporting of model coverage data. set_param(gcs, 'RecordCoverage','off') %% % Configure the input stimulus data. [ticks_to_count, reset, counter_mode, count_enable] = ... rtwdemo_sil_topmodel_data(T); %% % Configure logging options in the model. set_param(model, 'LoadExternalInput','on'); set_param(model, 'ExternalInput','ticks_to_count, reset, counter_mode, count_enable'); set_param(model, 'SignalLogging', 'on'); set_param(model, 'SignalLoggingName', 'logsOut'); %% % Enable execution time profiling for tasks and functions in generated code. set_param(model, 'CodeExecutionProfiling','on'); set_param(model, 'CodeProfilingInstrumentation','on'); set_param(model, 'CodeProfilingSaveOptions','AllData'); %% % Switch off code coverage. coverageSettings = get_param(model, 'CodeCoverageSettings'); coverageSettings.CoverageTool='None'; set_param(model, 'CodeCoverageSettings',coverageSettings); %% % Run a normal mode mode simulation. set_param(model,'SimulationMode','normal') [~, ~, yout_normal] = sim(model,10); %% % Run a top-model SIL simulation. set_param(model,'SimulationMode','Software-in-the-Loop (SIL)') [~, ~, yout_sil] = sim(model,10); %% % Unless up-to-date code for this model exists, new code is generated and % compiled. The generated code runs as a separate process on your host % computer. %% % Plot and compare the results of the normal and SIL simulations. Observe % that the results match. fig1 = figure; subplot(3,1,1), plot(yout_normal), title('Counter output for normal simulation') subplot(3,1,2), plot(yout_normal-yout_sil), title('Error') subplot(3,1,3), plot(yout_sil), title('Counter output for SIL simulation'); %% % Clean up. close_system(model,0); if ishandle(fig1), close(fig1), end, clear fig1 simResults = {'yout_sil','yout_normal','model','T',... 'ticks_to_count','reset'}; save([model '_results'],simResults{:}, 'executionProfile'); clear(simResults{:},'simResults') %% Execution Time Measurements % In Example 3, you configured the model to collect execution time % measurements and saved this data as a variable in the base workspace. The % software provides an API to retrieve execution time information from the base % workspace variable. For example, you can use the report method to create a % summary report. report(executionProfile); %% % For more information, see <matlab:helpview(fullfile(docroot,'toolbox','ecoder','helptargets.map'),'code_exe_profile_view_comp_exe_times') View and Compare Code Execution Times>. %% % Clean up execution profiling data. clear executionProfile %% Further Information on Hardware Implementation Settings for SIL Simulation % When you run a SIL simulation, you must configure your hardware implementation % settings (i.e. characteristics such as native word sizes) to allow compilation % for your host computer. The settings can differ from the hardware % implementation settings that you use when building the model % for your production hardware. To avoid the need to change hardware % implementation settings between SIL and PIL simulations, enable portable % word sizes. For more information, see <docid:ecoder_ug.buty32q>.