Activate the Profiler Feature
Sample times specified in a Simulink® model define the schedule for executing generated code on hardware. If the hardware has enough computing power, the code executes as specified.
The profiler feature generates code that measures execution performance of algorithm code. You can use this feature in PIL simulation or in standalone execution. If the code runs on multicore hardware, the profiler feature can also analyze how generated code uses different cores.
The profiling code calculates execution times from the data that is obtained through profiling instructions that are inserted into generated code. These instructions depend on a hardware-specific profiling function to obtain the current time on hardware. If the supported hardware has multiple cores, this profiling function can store the ID of the core on which the code is executing. Your target must provide the profiling function and the code to configure the associated timer, if one is needed.
Note
This is an example work flow for activating the profiler feature of your target. You must create hardware specific functions to successfully complete the profiler feature.
Create and add a new
Profiler
object,prof
, to yourTarget
object,tgt
, by callingaddNewProfiler
with the name of the profiler, for example,'My New Profiler'
.prof = addNewProfiler(tgt,'My New Profiler');
Do not delete the
Profiler
object from the MATLAB® workspace before you save the target.Confirm that the profiler
'My New Profiler'
is added to your target.show(tgt);
TIRM46Lx2 Launchpad Display Name TIRM46Lx2 Launchpad RM46 TI Deployer 1 FreeRTOS 1 MyPIL Serial RM46 SCI External mode Serial My New Profiler 0
Map the
Profiler
object,prof
, to theHardware
object,hw
.map(tgt,hw,prof);
Confirm that the profiler
'My New Profiler'
is used for the hardware'TIRM46Lx2 Launchpad'
.show(tgt);
TIRM46Lx2 Launchpad Display Name TIRM46Lx2 Launchpad RM46 TI Deployer 1 FreeRTOS 1 MyPIL Serial RM46 SCI External mode Serial My New Profiler 1
View properties of the
Profiler
object.prof
prof = Profiler with properties: Name: 'My New Profiler' TimerReadFcn: '' SourceFile: '' IncludeFile: '' TimerDataType: 'uint32' TimerTicksPerSecond: 1.0000e+09 TimerUpcounting: 1 StoreCoreID: 1 PrintData: 1 PrintInstantly: 0 BufferName: 'profilingData' DataLength: 400 GetDataFcn: ''
Specify a function that returns elapsed time on hardware, also known as a profiling function, by setting the
TimerReadFcn
property of theProfiler
object to, for example,'profileReadTimer'
.prof.TimerReadFcn = 'profileReadTimer';
Note
The function takes no arguments and returns the current time as an integer.
Specify a source file that contains the definition of the profiling function by setting the
SourceFile
property of theProfiler
object. For example:prof.SourceFile = '$(ARM_CORTEX_R_ROOT_DIR)/src/profile_timer.c';
Specify a header file that contains the declaration of the profiling function by setting the
IncludeFile
property of theProfiler
object. For example:prof.IncludeFile = '$(ARM_CORTEX_R_ROOT_DIR)/include/profile_timer.h';
Specify the data type of the value returned by the profiling function by setting the
TimerDataType
property of theProfiler
object. For example:prof.TimerDataType = 'uint32';
Specify the resolution, in ticks per second, of the value returned by the profiling function by setting the
TimerTicksPerSecond
property of theProfiler
object. For example:prof.TimerTicksPerSecond = 100000000;
Specify whether the value returned by the profiling function is increasing or decreasing, that is, counting up or counting down, by setting the
TimerUpcounting
property of theProfiler
object. For example, if the profiling function is counting up, setTimerUpcounting
to1
.prof.TimerUpcounting = 1;
If the profiling function is counting down, set this property to 0 instead.
Specify whether the profiling function also collects information on the processor core that executes generated code by setting the
StoreCoreID
property of theProfiler
object. For example, if the profiling function collects information on the processor core that executes generated code, setStoreCoreID
to1
.prof.StoreCoreID = 1;
If your hardware has a single core or if the profiling function does not collect information on the processor core that executes generated code, set
StoreCoreID
to 0.Specify whether the profiling function should print profiling data by setting the
PrintData
property of theProfiler
object. For example:prof.PrintData = 1;
If you do not want to print the profiling data, set
PrintData
to 1. You can print data only on hardware that uses a file system, for example, the hardware running Linux®.Specify whether the profiling data prints instantly while the code is running by setting the
PrintInstantly
property of theProfiler
object to, for example,0
.prof.PrintInstantly = 0;
Note
When you set
PrintInstantly
to0
, the data is printed only when generated code stops executing. The data is printed and saved to the file namedmodelname
.txt, wheremodelname
is the name of the Simulink model.Specify a function that copies the profiling data from the hardware to the MATLAB host computer by setting the
GetDataFcn
property of theProfiler
object.Set the name of the buffer in the generated code that stores the profiling data by setting the
BufferName
property of theProfiler
object. For example:prof.BufferName = 'profilingData';
Set the size of the buffer that stores the profiling data by setting the
DataLength
property of theProfiler
object. For example:prof.DataLength = 400;
Note
Data is collected from the start of the code execution until the buffer fills up, unless you specify that the data prints instantly.
If you are using the profiling function of the reference target, proceed to step 22 in this section. Otherwise, create an empty file that will hold the profiling function. The file name and location must be the same name as specified in the
SourceFile
property, for example$(ARM_CORTEX_R_ROOT_DIR)/src/profile_timer.c
.Add the profiling function template shown below to the file you created.
#include "rtwtypes.h" extern uint32_T profileReadTimer(void); /* _tmwrunningCoreID symbol declared only for standalone execution profiler */ #ifdef MW_STANDALONE_EXECUTION_PROFILER_ON extern unsigned int _tmwrunningCoreID; #endif uint32_T profileReadTimer(void) { uint32_T ret = (uint32_T) counterfcn(); /* _tmwrunningCoreID symbol declared only for standalone execution profiler */ #ifdef MW_STANDALONE_EXECUTION_PROFILER_ON _tmwrunningCoreID = coreidfcn(); #endif return(ret); }
Modify the profiling function template, as necessary, such that:
The function name,
profileReadTimer
, matches theTimerReadFcn
property.Replace
counterfcn
with the function that returns the timer value for the hardware.Replace
coreidfcn
with the function that returns the core id for your hardware.Replace
uint32_T
with the C/C++ equivalent to the data type specified in theTimerDataType
property.
Create a file named
'myprofile_timer.h'
that contains the definition of the function that obtains time in the include folder of your target’s root folder.#define _PROFILER_TIMER_H_ #ifdef __cplusplus extern "C" { #endif extern uint32_T profileReadTimer(void); #ifdef __cplusplus } #endif
Save the information that describes a target to its framework.
saveTarget(tgt);
Test that the profiler works correctly.
testTarget(tgt,'profiler');
Upon completion of the test, a summary result is displayed.
Confirm the Operation of the Profiler Feature in PIL Simulation
Create a blank Simulink model named
test
.In the model, select Modeling > Model Settings.
In the Configuration Parameters dialog box, select
Solver
pane.From the Type list, select
Fixed-step
. From the Solver list, selectauto
.In the Configuration Parameters dialog box, on the Hardware Implementation pane, set Hardware board to the hardware you registered, for example,
'TIRM46Lx2 Launchpad'
.In the Model Configuration Parameters dialog box, select Code Generation > Verification.
Select Advanced parameters > Create block and set it to
PIL
.Enable Code execution time profiling > Measure task execution time and set Measure function execution times to
Coarse (referenced models and subsystems only)
.Set the Save options to
All data
. Verify the name of the Workspace variable: executionProfile. Click OK.Open the Simulink Library Browser and from the Sources library, add the Constant block to your model.
From the Math Operations library, add the Gain block to your model. Connect the Constant and the Gain block.
From the Math Operations library, add the Subtract block to your model.
Connect the Gain block and the first input port of the Subtract block.
Right-click the Gain block and select Build Subsystem from Selection.
Right-click the Subsystem block and select C/C++ Code > Build This Subsystem. Click Build in the dialog box that opens.
A library containing the PIL Subsystem block is created. This block is gateway to the generated code that will run on the hardware.
Copy the PIL Subsystem block to your model and connect it to the Constant and the Subtract block.
From theSinks Library, add the Scope block to your model. Connect the Subtract and the Scope block.
Open the Scope block and run the model.
After the PIL simulation completes, the
executionProfile
variable appears in the MATLAB workspace.Obtain the profiling report and analyze different turnaround and execution times.
report(executionProfile)
Confirm the Operation of the Profiler Feature in Standalone Execution
Create a blank Simulink model named
test
.In the model, select Modeling > Model Settings.
In the Configuration Parameters dialog box, select
Solver
pane.From the Type list, select
Fixed-step
. From the Solver list, selectauto
.In the Configuration Parameters dialog box, on the
Hardware Implementation
, set Hardware board to the hardware you registered, for example,'My ARM Cortex A Board'
.On the Solver pane, enable Treat each discrete rate as a separate task and Stop time to 5.
On the Optimization pane, clear Block reduction.
On the Code Generation > Interface pane, check MAT-file logging.
Enable Code execution time profiling > Measure task execution time and set Measure function execution times to
Coarse (referenced models and subsystems only)
.Set the Save options to
All data
. Click OK.Open the Simulink Library Browser and from the Sources library, add the Constant block to the model. Double-click the Constant block and set the Sample time to
0.01
.Add a copy of this Constant block to the model. In the copy, set the Sample time to
0.02
and the Constant value to2
.From the Sinks library, add two To Workspace blocks to your model.
Connect each Constant block to a To Workspace block.
In your model, click Build Model. After the build completes, the application runs on your hardware for 5 seconds and then stops.
Get the profiling data into MATLAB workspace.
codertarget.profile.getData('test')
Obtain the profiling report and analyze different turnaround and execution times.
report(executionProfile)
Visualize the scheduling of tasks.
schedule(executionProfile)