Main Content

Code Execution Profiling on PX4 Target in Monitor & Tune Simulation

This example shows how to use the UAV Toolbox Support Package for PX4 Autopilots to profile the real-time execution of the generated code running as an executable on a PX4 target hardware with XCP on Serial Interface.


Sample times you specify in the Simulink models determine the time schedule for running generated code on target hardware. With enough computing power on the hardware, the code runs in real-time according to the specified sample times. With real-time execution profiling, you can check if the generated code meets your real-time performance requirements. This support package supports the code execution profiling on any supported PX4 hardware.

At the end of the Simulink model code profile execution, you can:

  • View a report of code execution times.

  • Access and analyze execution time profiling data.


Required MathWorks Products

  • Simulink®

  • Embedded Coder®

Required Hardware

One of these Supported PX4 Autopilots.


Open the pre-configured px4demo_profiling model.

Configuring the Model

In this example, you will configure a Simulink model and enable profiling.

Note: These steps are not required in the pre-configured model. Perform these steps if you have changed the hardware or not using the pre-configured model.

1. Open the model.

2. Go to Modeling > Model Settings to open the Configuration Parameters dialog box.

3. Open the Hardware Implementation pane, and select the required PX4 hardware from the list in Hardware board parameter.

4. Expand Target hardware resources for that board.

5. Go to External mode tab and choose XCP on Serial as the Communication interface.

6. Go to Code Generation > Verification > Code execution time profiling and select Measure task execution time.

7. Select the required option for Measure function execution times, Workspace Variable and Save Options. For information on different save options, see Save Options.

8. Click Apply and OK.

Initiate Monitor and Tune Action for the Model

On the Hardware tab of the Simulink toolstrip, click Monitor & Tune to monitor signals and tune parameters.

This generates a profiling report with profiling metrics of different tasks/functions that are being profiled. This also contains links to plot the data on SDI, bar charts, pie charts and CPU Utilization which provides further analysis of the data.

All data report

Code Execution Profiling Report Details

This report provides the following details:

1. A detailed summary

2. Information about profiled code sections, which includes time measurements.

3. Average and maximum CPU core utilization.

Note: CPU Utilization in profiling report is computed by dividing the execution time taken by task by the sample time of the periodic task. This CPU utilization does not consider other PX4 modules to give the actual utilization of the processor.

4. Definition of metrics.

The report has these sections:

1. Section: Name of function from which code is generated.

2. Maximum Execution Time in ns: Longest time between start and end of code section.

3. Average Execution Time in ns: Average time between start and end of code section.

4. Maximum Self Time in ns: Longest execution time, excluding time spent in child sections.

5. Average Self Time in ns: Average execution time, excluding time spent in child sections.

6. Calls: Number of calls to the code section.

7. MATLAB icon: Icon that you click to display the profiled code section.

8. SDI icon: Icon that you click to visualize the profiling data over the duration of simulation.

9. Bar chart icon: Icon that you click to display the normalized frequency of the execution times of the task/function.

10. Pie chart icon: Icon that you click to display the time taken by a function and its child functions.

Metrics only report

This report contains only summary metrics of Average/Maximum execution times of each task/function being profiled. Also, it contains the number of times each function/task is called during simulation.

Note: Profiling with All data or Summary data save options and Detailed option selected for Measure function execution times requires high bandwidth. This is because large amount of data must be sent in real-time. If the target does not have enough bandwidth to stream data in real-time, then select Metrics Only save option. Running profiling with this option stores summary profiling data on the target and will only send data to the host at the end of simulation.

Other Things to Try

Profile other Simulink models from the UAV Toolbox Support Package for PX4 Autopilots. Observe the time taken for implementing steps in the Simulink model that helps to improve its efficiency.

Related Topics