Code Analysis and Tracing
After you generate code, make sure that the code meets your requirements by analyzing the code metrics, interfaces, replacements, and code generation assumptions. The code generation report provides information about these aspects of the generated code. You can assess the code and modify your model to generate code that meets your requirements.
Verify the generated code by navigating between a line of code and its corresponding model elements by using code tracing. You can trace from hyperlinks in the code to model elements and from model elements to code lines. You can also include links from the generated code to the model element requirements.
You can also analyze the generated code to identify the execution paths that produce the longest execution-times by using critical path analysis. The paths that produce the longest execution-times are called critical paths. Use the Code Profile Analyzer to identify the most expensive sections of the identified paths and apply optimization efforts on the expensive sections to meet your performance requirements.
Apps
Code Profile Analyzer | Analyze execution-time and stack usage profiles for generated code (Since R2023a) |
Functions
rtwtrace | Trace a block to generated code in code generation report |
coder.profile.test.analyzePath | Identify critical paths of tasks (Since R2024a) |
coder.profile.test.generateTests | Generate decision coverage tests for execution-time analysis (Since R2024a) |
coder.profile.test.generateCriticalPathTest | Generate tests for critical paths (Since R2024a) |
coder.profile.test.getCriticalPathElements | Get cell array of model elements in critical path (Since R2024a) |
coder.profile.test.hiliteCriticalPath | Highlight critical path in model canvas (Since R2024a) |
Topics
Code Analysis
- Check Code Generation Assumptions
Use standalone workflow to check code generator assumptions. - Analyze Code Replacements in Generated Code
The Code Replacements Report section in the code generation report documents the code replacement library (CRL) functions that were used for code replacements during code generation. - Generate Static Code Metrics Report for Simulink Model
When generating code, generate a report that includes static code metrics, such as, the number of lines of code, size of global variables, and the number of reads and writes. - Static Code Metrics
The code generator performs static analysis of the generated C or C++ code and provides these metrics in the static code metrics report in the code generation report. - Analyze Generated Data Code Interface Report
Use the code interface report to see documentation of the generated code data interfaces, including model entry-point functions and data interfaces. - Analyze Generated Service Code Interface Report
Use the code interface report to see documentation of the generated code service interface, including execution functions and services. - Log Program Execution Results
Log data to a MAT-file during execution of generated code.
Code Tracing
- Verify Generated Code by Using Code Tracing
Code tracing enables navigation between generated code and its corresponding elements in a model. - Trace Simulink Model Elements in Generated Code
Trace Simulink® model elements by using code-to-model and model-to-code traceability in the code generation report. - Trace Stateflow Elements in Generated Code
Trace different Stateflow® elements to corresponding lines in generated code. - Reload Existing Traceability Information
Example of how to reload existing traceability information for a model. - Customize Traceability Reports
Obtain complete mapping between model elements and generated code. - Use Traceability in MATLAB Function Blocks
Trace between source code and generated code in a MATLAB Function block.
Critical Path Analysis
- Analyze Execution Times of Critical Paths
Identify and analyze critical paths in the generated code.