MATLAB Examples

Real-Time Code Execution Profiling

This example shows you how to use Embedded Coder™ Support Package for ARM Cortex-A Processors for real-time execution profiling of generated code.

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 design of your system. For example, if the code easily meets the real-time requirements, you may consider adding more functionality to your system to exploit available processing power. If, on the other hand, the code does not meet real-time requirements, you may look 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 may 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, and generate code.
  • Run generated code on target hardware.
  • Analyze performance through code execution profiling plots and reports.

Prerequisites

We recommend completing Getting Started with Embedded Coder Support Package for ARM Cortex-A Processors example.

  • Start QEMU emulator for ARM Cortex-A by entering the following command in MATLAB®:
pid = codertarget.arm_cortex_a.runQemu()

Task 1 - Profile task execution

This example shows 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 frequency of 760 Hz and another sine waveform with 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 sample time of 1/3800 and frame size of 76. The second algorithm uses sample time of 1/19000 and frame size of 760.

1. Open the model. This model is configured for ARM Cortex-A9 (QEMU) target hardware.

2. Click Model Configuration Parameters on the toolbar, select Code Generation and then Verification.

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

4. Click Deploy to Hardware in the toolbar. This action starts generating and building code for your model. When the code is generated, a QEMU session running the generated code is launched. The code runs for 5 seconds and then it stops.

5. Run the following code in MATLAB to get the profiling data into MATLAB workspace.

   codertarget.profile.getData('arm_cortex_a_profiling')

6. 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

7. 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

Task 2 - Profile function execution

This task shows 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 if you closed it. This model is configured for ARM Cortex-A9 (QEMU) target hardware.

2. Click Model Configuration Parameters on the toolbar, select Code Generation and then Verification.

3. Click both Measure task execution time and Measure function execution time checkboxes. These options enable you to profile execution time for each rate and each function in generated code. Click OK.

4. Click Deploy to Hardware in the toolbar. This action starts generating and building code for your model. When the code is generated, a QEMU session running the generated code is launched. The code runs for 5 seconds and then stops.

5. Run the following code in MATLAB to get the profiling data into MATLAB workspace.

   codertarget.profile.getData('arm_cortex_a_profiling')

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

Task 3 - Profile concurrent task execution

This task shows profiling of generated code for the model that is configured for concurrent task execution. Since the QEMU emulator emulates two cores, you get information on the core usage as well. The model is already configured for concurrent execution.

1. Open the model. This model is configured for ARM Cortex-A9 (QEMU) target hardware.

2. Click Model Configuration Parameters on the toolbar, select Code Generation and then Verification.

3. Click the Measure task execution time checkbox. This option enables you to profile execution time for each rate in generated code. Click OK.

4. Click Deploy to Hardware in the toolbar. This action starts generating and building code for your model. When the code is generated, a QEMU session running the generated code is launched. The code runs for 5 seconds and then stops.

5. Run the following code in MATLAB to get the profiling data into MATLAB workspace.

   codertarget.profile.getData('arm_cortex_a_profiling_concurrent')

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. 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. Notice that the tasks run on different cores, which is indicated by the core number underneath each task instance in the graph. Also, notice that there is no preemption between the tasks. Close the timeline when you are done.

   executionProfile.timeline

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. It also showed you how to get information on core usage for multicore target hardware.