polyspace-code-profiler
(System Command) Profile your source code for calculating C/C++ code coverage, code execution time, and memory use
Since R2023b
Syntax
Description
The polyspace-code-profiler system command instruments source code for code profiling and runs tests using the instrumented source code to collect code profiling results.
Instrument Source Files
Note
This Polyspace® command is available in the folder . Here, polyspaceroot\polyspace\bin is the Polyspace installation folder, for instance, polyspacerootC:\Program Files\Polyspace\R2026a. To avoid typing the full path to this command, add this location to the PATH environment variable in your operating system.
polyspace-code-profiler -instrument -instrum-dir instruments source files to calculate the code coverage metrics specified by instrumFolder -cov-metric-level covLevel [instrumOptions] -- compileCommandcovLevel.
In this step, Polyspace parses the source code, and then uses the compileCommand to compile the instrumented source files. If your code contains nonstandard code constructs, the parsing step might fail. Review the log to find which code constructs are unsupported. Before continuing, remove the unsupported construct.
polyspace-code-profiler -instrument -instrum-dir instruments a Microsoft®
Visual Studio® project to calculate code coverage metrics specified by instrumFolder -cov-metric-level covLevel -instrument-in-place -msbuild [instrumOptions] -- MSbldCommandcovLevel.
polyspace-code-profiler -instrument -instrum-dir instruments the source files to calculate the execution time up to the level of details specified by instrumFolder -exec-metric-level executionTimeLevel -prof-counter-size counterSize [instrumOptions] -- compileCommandexecutionTimeLevel. You cannot calculate more than one of the
code coverage, execution time, or memory use metrics at a time.
polyspace-code-profiler -instrument -instrum-dir instruments a Microsoft Visual Studio project to calculate the execution time up to the level of details specified by instrumFolder -exec-metric-level executionTimeLevel -prof-counter-size counterSize -msbuild [instrumOptions] -- MSbldCommandexecutionTimeLevel. You cannot
calculate more than one of the code coverage, execution time, or memory use metrics at a time.
polyspace-code-profiler -instrument -instrum-dir instruments the source files to calculate the memory use up to the level of details specified by instrumFolder -stack-metric-level memoryUseLevel -prof-counter-size counterSize [instrumOptions] -- compileCommandmemoryUseLevel. You cannot calculate more than one of the code
coverage, execution time, or memory use metrics at a time.
polyspace-code-profiler -instrument -instrum-dir instruments a Microsoft Visual Studio project to calculate the memory use up to the level of details specified by instrumFolder -stack-metric-level memoryUseLevel -prof-counter-size counterSize -msbuild [instrumOptions] -- MSbldCommandmemoryUseLevel. You cannot calculate more than
one of the code coverage, execution time, or memory use metrics at a time.
polyspace-code-profiler -instrument -instrum-dir instruments the source files compiled by instrumFolder -sanitizer [-sanitizer-selection defectSelection] [-sanitizer-max-error-occurrences maxOccurrences] [instrumOptions]-- compileCommandcompileCommand for code sanitization.
polyspace-code-profiler -instrument -instrum-dir instruments the source files in the Microsoft Visual Studio project built by instrumFolder -sanitizer [-sanitizer-selection defectSelection] [-sanitizer-max-error-occurrences maxOccurrences] -msbuild [instrumOptions] -- MSbldCommandMSbldCommand for code sanitization.
Run Instrumented Test Executable
polyspace-code-profiler -run -instrum-dir runs the test executable instrumFolder -results-dir resultsFolder [runOptions] -- runCommandrunCommand, collects code profiling data, and creates a Polyspace
Test™ profiling results (.psprof) file populated with the collected data. You must create the instrumented text executable before invoking this command. After calling polyspace-code-profiler -instrument, link the instrumented source files and tests with the Polyspace
Test run-time libraries to create the instrumented test executable.
Create Report
polyspace-code-profiler -report -xml -report-dir creates XML reports from code profiling results stored in reportDir [reportOptions] resultsPathresultsPath. The reports are stored in reportDir.
polyspace-code-profiler -report -html -report-dir creates HTML reports from code profiling results stored in the paths you specify in reportDir [reportOptions] resultsPathresultsPath. The reports are stored in reportDir.
polyspace-code-profiler -report -xml -report-dir creates XML reports from code profiling results stored in the paths you specify in reportDir -filter-files filterFile[reportOptions] resultsPathresultsPath. The reports are stored in reportDir.
polyspace-code-profiler -report -html -report-dir creates HTML reports from code profiling results stored in the paths you specify in reportDir -filter-files filterFile[reportOptions] resultsPathresultsPath. The reports are stored in reportDir.
Merge Results and Filter Files
polyspace-code-profiler -merge -results-dir merges the data results stored in mergedResult [mergeOptions] resultsToMergeresultsToMerge and creates a merged data file. This command also merges any filters associated with each set of result. The merged data file and filters are stored in mergedResult. You can generate different results in various
separate runs and then use this command to merge the results into a single file.
polyspace-code-profiler -merge -operation performs mergeOperation -results-dir mergedResult [mergeOptions] resultsToMergemergeOperation on the results and filters stored in resultsToMerge and creates a new files in mergedResult.
Convert Results
polyspace-code-profiler -convert -instrum-dir converts the binary results in instrumFolder [convertOptions] binaryResultsPathbinaryResultsPath into .psprof files. You typically obtain the code profiling results in binary form when running your tests on a target.
Justify Uncovered Dead Code Using Polyspace Code Prover or Polyspace Bug Finder Result
polyspace-code-profiler -justify-uncovered-dead-code -rationale creates a filter file to justify the uncovered code in reasonForDeadCode saResults [-target-filter-file customFilter] codeCoverageResultscodeCoverageResults that are reported as unreachable or dead code in saResults. The generated filter file contains the
reasonForDeadCode as the value for the field rationale.
Import Filters
polyspace-code-profiler -import-filters [-filter-file imports the filter with the same name as filterFile] sourceCovResult [-target-filter-file customFilter] targetCovResult sourceCovResult and creates a filter for the results in targetCovResult. The imported filter has the same name as targetCovResult. If
there are no filter files with the same name as sourceCovResult, then specify a filter file as filterFile.
Version
polyspace-code-profiler -version displays the version of the Polyspace
Test product.
Examples
Input Arguments
Limitations
The command
polyspace-code-profilerdoes not support multithreaded code.When you instrument your code by using the
polyspace-code-profiler -instrumentcommand, Polyspace converts the encoding of your source files toutf-8and then inserts instrumentation macros in the converted source files. If your code relies on the raw binary values of the source files, the change in encoding can result in unexpected result.The instrumentation process might prevent some compiler optimization, such as inlining of functions. The execution time of the instrumented code can be larger than the execution time of the uninstrumented code. The execution times calculated by Polyspace Test represents the upper limit of the execution time of your code.
The instrumentation process might increase the stack memory use of your function. The memory use calculated by Polyspace Test represents the upper limit of the memory use of your code.
Because C++ compilers call destructors before a function exit, the calculated memory use of a function is larger if you compile your code using a C++ compiler.
Polyspace Test cannot calculate the stack memory consumed by the entry-point function. The
selfandmaxvalue of the memory use metric for entry-point functions are reported as0.When calculating the execution time and memory use of C++ code,
polyspace-code-profilerignores these functions:Functions that throw an exception.
Functions that handle an exception thrown elsewhere in the call stack.
When calculating the memory use, Polyspace Test ignores variable length arrays (VLA).
If you use a function that is not instrumented, Polyspace Test does not measure the code coverage, execution time, or memory use of the function. When calculating the memory use of functions that call such uninstrumented functions, the memory use of the uninstrumented functions are ignored.
Tips
If you want to exclude specific portions of your source code from code profiling, use the pragma
tmw code_instrumentation off. When Polyspace Test encounters this pragma, it stops instrumenting the source code until it encounters the pragmatmw code_instrumentation on. For instance, in this code, the call tofoois excluded from code profiling:void bar(){ //... #pragma tmw code_instrumentation off foo(); #pragma tmw code_instrumentation on //.... }Instead of typing the
polyspace-code-profilercommands for instrumentation, run, and reporting directly at the command line, you can enter them in a makefile and executemakeat the command line. For an example makefile, see Calculate Code Profile by Using Makefile.You can also generate such a makefile using generators such as CMake. For instance, when generating a makefile with the
cmakecommand, in addition to your usual CMake flags, define the following variables usingcmakeoptions:CMAKE_C_COMPILER_LAUNCHER: Set this variable to the full path of thepolyspace-code-profilercommand. Add appropriatepolyspace-code-profileroptions, separating the command from options and options from each other, using a;separator.For instance, to construct the command:
You can define these variables at the command line using the following flag:polyspace-code-profiler -instrument -instrum-dir instrumFolder -- gcc files
-DCMAKE_C_COMPILER=gcc -DCMAKE_C_COMPILER_LAUNCHER="polyspace-code-profiler;-instrument;-instrum-dir;instrumFolder;--"
CMAKE_CXX_COMPILER_LAUNCHER: Set this variable to the full path of thepolyspace-code-profilercommand. Add appropriatepolyspace-code-profileroptions, separating the command from options and options from each other using a;separator.For instance, to construct the command:
You can define these variables at the command line using the following flag:polyspace-code-profiler -instrument -instrum-dir instrumFolder -- g++ files
-DCMAKE_CXX_COMPILER=g++ -DCMAKE_CXX_COMPILER_LAUNCHER="polyspace-code-profiler;-instrument;-instrum-dir;instrumFolder;--"
CMAKE_EXE_LINKER_FLAGS: Set this variable to flags required for linking to the precompiled coverage instrumentation library available with Polyspace Test.For instance, to link to the library
libmwpsprofile_cli_runtime, precompiled using MinGW in Windows, you can define the variable as follows:Here,-DCMAKE_EXE_LINKER_FLAGS="-L \"polyspaceroot/polyspace/psprofile/lib/win64/import/mingw64\" -L \"polyspaceroot/polyspace/psprofile/lib/win64\" -l mwpsprofile_cli_runtime"
is the path to the Polyspace installation folder, for instance,polyspacerootC:/Program Files/Polyspace/R2026a.
Algorithms
The code coverage calculation algorithm consists of several steps. The code instrumentation step:
Runs the
compileCommandor theMSbldCommand.Extracts source filenames from the executed processes.
Instruments the source files with Polyspace Test macros to collect code profiling data.
Creates the test executable.
After creating the test executable, in the next step, run the test executable. This step collects the code profiling data from your source code.
Once you generate the code profiling database, create an HTML or XML report from the data base.
For the complete workflow, see:
Version History
Introduced in R2023b