MATLAB Examples

Real-Time Code Execution Profiling

This example shows you how to use Embedded Coder™ Support Package for Xilinx® Zynq™-7000 Platform 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 will run according to specified sample times i.e. in real-time fashion.

Real-time execution profiling will help you check whether generated code meets real-time performance requirements. Profiling will also help you analyze how your application uses different cores if your target hardware has multi-core architecture.

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.
  • Execute 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 Xilinx Zynq Platform example.

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 Xilinx Zynq ZC702 evaluation kit target hardware.

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

3. Click Measure task execution time checkbox. This will enable you to profile execution time for each rate in the model. Note that the executionProfile is the name of the workspace variable that holds the profiling data. Click OK.

4. Click Deploy to Hardware in the toolbar. This will start generating and building code for your model.

5. Once the code is generated, a command window will pop-up showing the executable has started running on the Zynq hardware. The code will run for 5 second and then it will stop.

6. Execute the following code in MATLAB to get the profiling data into MATLAB workspace.

   codertarget.profile.getData('zynq_profiling')

7. Execute the following code in MATLAB to obtain the profiling report for the session you just ran and analyze the report. Notice different turnaround and execution times for each task. Close the report once you are done.

   executionProfile.report

8. 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 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 once you are done.

   executionProfile.timeline

Task 2 - Profile function execution

This example 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 Xilinx Zynq ZC702 evaluation kit 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. This will 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 will start generating and building code for your model.

5. Once the code is generated, a command window will pop-up showing the executable has started running on the Zynq hardware. The code will run for 5 second and then it will stop.

6. Execute the following code in MATLAB to get the profiling data into MATLAB workspace.

   codertarget.profile.getData('zynq_profiling')

7. Execute the following code in MATLAB to obtain the profiling report for the session you just ran and analyze the report. Notice different turnaround and execution times for each task and function. Close the report once you are done.

   executionProfile.report

8. 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 once you are done.

   executionProfile.timeline

Task 3 - Profile concurrent task execution

This example shows profiling of generated code for the model that is configured for concurrent task execution. Since the Xilinx Zynq processor has two ARM® Cortex-A9 cores, you will get the information on the core usage as well. The model is already configured for concurrent execution.

1. Open the model. This model is configured for Xilinx Zynq ZC702 evaluation kit 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 will enable you to profile execution time for each rate in generated code. Click OK.

4. Click Deploy to Hardware in the toolbar. This will start generating and building code for your model.

5. Once the code is generated, a command window is launched running the generated code is launched. The code will run for 5 second and then it will stop.

6. Execute the following code in MATLAB to get the profiling data into MATLAB workspace.

   codertarget.profile.getData('zynq_profiling_concurrent')

7. Execute the following code in MATLAB to obtain the profiling report for the session you just ran and analyze the report. Notice different turnaround and execution times for each task. Close the report once you are done.

   executionProfile.report

8. 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 once you are done.

   executionProfile.timeline

Summary

This example introduced the workflow for real-time execution profiling of generated code.