View and Compare Stack Usage Metrics - MATLAB & Simulink (original) (raw)

To determine the size of stack memory that is required to run generated code, you can configure software-in-the-loop (SIL) or processor-in-the-loop (PIL) simulations that generate stack usage profiles—see Create Stack Usage Profile for Generated Code. The generated profiles enable you to observe the effect of compiler optimization and data input on stack usage.

During a simulation, you can use the Simulation Data Inspector to observe streamed stack usage metrics. At the end of the simulation, you can:

View Stack Usage Metrics for Tasks and Functions

Suppose you run a SIL simulation of this model:

mStackProfilingTopModel

The top model, mStackProfilingTopModel, is a modification of the shipped model SILModelBlock.

Simulink Editor shows blocks in top model.

The referenced model, mStackProfilingRefModel, is a modification of the shipped model SILCounter.

Simulink Editor shows blocks in referenced model.

The If Action Subsystem block contains this subsystem.

Simulink Editor shows subsystem used by If Action Subsystem block.

When the SIL simulation is complete, the code stack profiling report opens. See Code Stack Profiling Report.

To view stack usage metrics in the Code Profile Analyzer:

  1. Click the SIL/PIL tab.
  2. In the Results gallery, under Stack Profiling Results, click Code Profile Analyser.

Or, in the Command Window, enter:

coder.profile.show(stackProfile)

The app displays information in the Stack Memory andSummary panels.

In the Stack Memory panel, the Profiled Tasks view shows stack usage metrics for these generated code sections:

Profiled Tasks view shows stack usage metrics for generated code sections.

For each code section, the table provides this information:

The Profiled Functions view shows the memory demand of each executed function.

Profiled Functions view shows memory demand of executed functions.

For each function, the table provides this information:

To view the stack usage distribution for a task:

  1. In the Profiled Tasks view, click a task row.
  2. In the Results section, click Generate Distribution.

Plot shows stack usage distribution for generated task.

To view the variation of stack usage over the simulation, in the Results section, click Export to SDI.

Simulation Data Inspector display shows stack usage variation over the simulation.

Stack memory usage for mStackProfilingRefModel_step fluctuates between 112 and 64 bytes. The fluctuation occurs because the nested function generated from the If Action Subsystem block is called bymStackProfilingRefModel_step only when the signalprevious_output has an even value.

… if ((int32_T)(uint8_T)((int32_T)rtb_previous_output % 2) == 0) { /* Outputs for IfAction SubSystem: '/If Action Subsystem' incorporates: * ActionPort: '/Action Port' */

/* Outputs for Atomic SubSystem: '<S1>/Subsystem' */
Subsystem(rtb_previous_output, &rtY.Out1);

/* End of Outputs for SubSystem: '<S1>/Subsystem' */
/* End of Outputs for SubSystem: '<Root>/If Action Subsystem' */

} …

To view the call to a function:

  1. In the Profiled Functions view, click the row that contains the function, for example, mStackProfilingRefModel_step.
  2. In the Results section, click Highlight Code. The code generation report shows the call in the generated code.

Code generation report highlights call to function.

To trace the model component from which code is generated, in theResults section, click Open Model.

View Function-Call Stack Memory Usage

At the simulation step where a task takes longest to execute, you can view the memory usage of the function-call stack.

  1. On the toolstrip, in the Analysis section, clickFunction-Call Stack.
  2. From the Task to analyze drop-down list, select a code section. For example, mStackProfilingRefModel_step.
  3. Note the Longest execution at field value, the simulation time at which execution of the code section is the longest.
  4. Use the default value for Specify simulation time. Modify the value only if you want to visualize the function-call stack at a different simulation time.
  5. Click Display.

Panel displays function-call stack and stack usage metrics for functions.

The panel displays:

Compare Stack Usage Against Baseline

You can compare stack usage values from the current simulation against values from a baseline simulation.

  1. On the toolstrip, in the Analysis section, clickComparison.
  2. From the Compare current data against drop-down list, select the workspace variable that contains results from the baseline simulation. You can update the drop-down list by clicking the Refresh list button.
  3. Click the Compare button.

The panel provides these views:

The table cells are colored:

If the workspace variable for the baseline simulation does not contain data for a code section, the panel displays NaN.

Task Comparison and Function Comparison views compare stack usage metrics against baseline values.

Summary Panel (Stack Usage)

The Summary panel provides miscellaneous simulation information.

Summary panel displays miscellaneous simulation information.

Code Stack Profiling Report

You can also view stack usage metrics for the generated code through the HTML report that opens at the end of the simulation.

Code stack profiling report for model.

If you close the report, you can reopen the report. Use one of these actions:

If you have a MATLAB® Report Generator™ license, you can produce a PDF report of the analysis performed. In theResults section, click Create PDF.

In section 1, the report:

For each task, section 2 of the report provides this information:

The report provides more information through clickable icons. The specified workspace variable that contains stack usage data, for example, stackProfile, must be present in the base workspace.

If you click , you can view the stack usage distribution for the simulation.

If you click , you can view the variation of stack usage over the simulation.

For each function, section 3 of the report provides this information:

If you click , you can view the function call in the code generation report.

If you specify a value for Maximum stack size (bytes) (MaxStackSize), the report provides Section 4. This section displays the maximum stack usage for each task with reference to the maximum stack size permitted, which the bar chart displays as a horizontal red line. If theMaxStackSize value is much greater than the stack usage of the tasks, the bar chart does not display the red line. To help your visual analysis of stack usage requirements, you can use the MaxStackUsageAllowed property of the workspace variable to respecify the maximum stack size value. For example:

stackProfile.MaxStackUsageAllowed = 64; report(stackProfile);

Bar chart in report displays maximum stack usage for each task with reference to maximum stack size permitted, a horizontal red line.

See Also

coder.profile.ExecutionStackSet

Topics