MATLAB Examples

Real-Time Code Execution Profiling

Use the Embedded Coder™ Support Package for ARM Cortex-R Processors for real-time execution profiling of generated code. The hardware being used in this example is the Texas Instruments™ Hercules RM57Lx LaunchPad.

Contents

Introduction

Sample times specified in a Simulink® model determine the time schedule for running generated code on target hardware. If the target hardware has sufficient computing power, the code runs according to specified sample times in real-time.

You can use real-time execution profiling to check whether generated code meets real-time performance requirements. If your target hardware has multicore architecture, you can also use profiling to analyze how your application uses different cores.

You can use execution profiling results to take actions to enhance the design of your system. For example, if the code easily meets the real-time requirements, consider adding more functionality to your system to exploit available processing power. Conversely, if the code does not meet real-time requirements, consider looking for ways to reduce execution time. For example, you can identify the tasks that require the most time and then investigate whether trade-off between functionality and speed is possible. If your target hardware has multiple cores, you can distribute the execution of the code across available cores.

This example introduces a workflow for real-time code execution profiling by showing you how to:

  • Configure the model for code execution profiling.
  • Generate code and run it on hardware board.
  • Analyze performance through code execution profiling plots and reports.

Prerequisites

Complete the docid:armcortexr_examples.example-arm_cortex_r_gettingstarted example.

  • Connect the Hercules RM57Lx LaunchPad to the host.

Task 1 - Profile Task Execution

This task shows you how to configure the model for profiling the execution of different tasks in your model. It also shows you how to view and analyze the profiling results.

The model has two algorithms. They are both applied to signals with similar frequency spectrum: one sine waveform with a frequency of 760 Hz and another sine waveform with a frequency of 1266 Hz. Both signals contain Gaussian noise. The first algorithm uses a bandpass FIR filter to extract the first component. The second algorithm uses a lowpass and a highpass FIR filter to extract the second component. The first algorithm uses a sample time of 1/3800 and a frame size of 76. The second algorithm uses sample time of 1/19000 and a frame size of 760.

1. Open the model. This model is configured to run on the TI Hercules RM57Lx LaunchPad hardware board, and uses the TI ARM Code Generation Tools | gmake toolchain.

2. On the model toolbar, click Model Configuration Parameters, select Code Generation and then Verification.

3. Select the Measure task execution time check box. This option enables you to profile execution time for each rate in the model. The executionProfile workspace variable holds the profiling data. Click OK.

4. On the toolbar, click Deploy to Hardware to generate and build code for your model. The generated code is cross-compiled into an executable and deployed to the hardware board. The code begins execution as soon as it is deployed. After the executable collects the first 400 instrumentation points, the simulation halts and the logged instrumentation data is transferred to a MATLAB workspace variable.

5. Run the following code in MATLAB to obtain the profiling report for the session you just ran. Analyze the report and the different turnaround and execution times for each task. Close the report when you are done.

   executionProfile.report

6. Run the following code in MATLAB to obtain the profiling report for the session you just ran and analyze the execution timeline of two different tasks. Notice where the faster task preempts the slower one and that the slower task still finishes before it is scheduled to run again. Close the timeline when you are done.

   executionProfile.timeline

7. To re-deploy the model and and collect profiling information again, without having to build the model, run the following code in MATLAB.

   codertarget.profile.getData('arm_cortex_r_profiling')

Task 2 - Profile Function Execution

This task shows you how to configure the model for profiling the execution of different functions in your model. It also shows you how to view and analyze the profiling results.

Function execution profiling is used to give more insight into the behavior of generated code by profiling each function in addition to overall tasks for each sample rate.

1. Open the model.

2. On the model toolbar, click Model Configuration Parameters and select Code Generation and then Verification.

3. Select the Measure task execution time and Measure function execution time check boxes. These options enable you to profile execution time for each rate and each function in generated code. Click OK.

4. On the toolbar, click Deploy to Hardware to generate and build code for your model. As in the previous task, the generated executable is deployed to the hardware board. The code begins execution as soon as it is deployed. After the executable collects the first 400 instrumentation points, the simulation halts and the logged instrumentation data is transferred to a MATLAB workspace variable. After the transfer is complete, execution resumes.

6. Run the following code in MATLAB to obtain the profiling report for the session you just ran. Analyze the report and different turnaround and execution times for each task and function. Close the report when you are done.

   executionProfile.report

7. Execute the following code in MATLAB to obtain the profiling report for the session you just ran and analyze the execution timeline of two different tasks and functions. Notice where the faster task preempts the slower one and where different functions start and end. Close the timeline when you are done.

   executionProfile.timeline

Note

You cannot perform execution profiling on a model configured to run in external mode.

Summary

This example introduced the workflow for real-time execution profiling of generated code. It showed you how to profile execution of both tasks and functions in generated code.