Create Library for Code Profiling Using Self-Managed Builds
You can perform C/C++ code profiling in Polyspace® Test™ using a self-managed approach, which allows you to use any toolchain already configured for your builds. To facilitate the self-managed builds, Polyspace Test provides precompiled code profiling libraries for certain compilers. If the precompiled libraries are not compatible with your compiler, you can create the libraries from sources included with your installation. This topic shows how to create the code profiling libraries from scratch.
Usage of Code Profiling Library
In a typical code profiling workflow, you instrument your sources using the polyspace-code-profiler -instrument command. Next, you link the instrumented sources with a precompiled library containing definition of the macros used during code instrumentation. This process creates a final executable that enables code profiling.
For example, you can perform these steps using a GCC compiler as follows:
polyspace-code-profiler -instrument -instrum-dir instrums -- gcc -c src.c
gcc src.o <PSPROFILELIB><PSRPOFILELIB> is the version of the library precompiled for use with GCC.
For more information on the paths to precompiled libraries, see Set Up C/C++ Testing and Code Profiling Using Self-Managed Builds.
For an example using a precompiled library, see Calculate C/C++ Code Coverage Using Self-Managed Builds.
In the next sections, you will see how to create this library for your own compiler in cases where the precompiled library is not compatible.
Create Code Profiling Library for Profiling on Host
To create a code profiling library for profiling on host, you need the following files and folders:
Profiling source file
. In the examples below, this file is referred to aspolyspaceroot\polyspace\psprofile\src\psprofile.c<PSPROFILE_SOURCE>.Here,
is the Polyspace installation folder, for example,polyspacerootC:\Program Files\Polyspace\R2026a.Profiling include folder
. In the examples below, this file is referred to aspolyspaceroot\polyspace\psprofile\include<PSPROFILE_INCLUDE>.A configuration header file
psprofile_config.h. You can adapt the example configuration file in the folder.polyspaceroot\polyspace\examples\doc_pstest\profiling_configs\hostIn the examples below, the folder containing this header is referred to as
<PSPROFILE_CONFIG_INCLUDE>. For more information on the macros used in the configuration file, see Configuration Macros for Code Profiling.
To create the library, compile the required files in a single compilation command. To make sure that the configuration file is used, define the macro PST_USER_CONFIG_FILE during compilation. For example, if you are using the GCC compiler, compile the sources and includes as
follows:
gcc -c <PSPROFILE_SOURCE> -I <PSPROFILE_INCLUDE> -I <PSPROFILE_CONFIG_INCLUDE> -D PST_USER_CONFIG_FILE -o psProfileLibYou can then use the library at link time just as you would use the precompiled libraries provided with Polyspace Test. For example, with the GCC compiler, your source code instrumentation and linking steps might look like this:
polyspace-code-profiler -instrument -instrum-dir instrums -- gcc -c src.c
gcc src.o psProfileLib.opolyspace-code-profiler -run to collect coverage data. For a complete example of the workflow, see Calculate C/C++ Code Coverage Using Self-Managed Builds.Create Code Profiling Library for Profiling on Target
To create a code profiling library for profiling on a target, you need the same files and folders as for profiling on host. However, your configuration file is different from before. In addition, for targets without a file system, you must define functions to initialize the target and stream data from the target back to the host.
To create a code profiling library for profiling on target, you need the following files and folders:
Profiling source file
. In the examples below, this file is referred to aspolyspaceroot\polyspace\psprofile\src\psprofile.c<PSPROFILE_SOURCE>.Here,
is the Polyspace installation folder, for example,polyspacerootC:\Program Files\Polyspace\R2026a.Profiling include folder
. In the examples below, this file is referred to aspolyspaceroot\polyspace\psprofile\include<PSPROFILE_INCLUDE>.A configuration file
psprofile_config.h.For targets with a file system, you can adapt the example configuration file in the folder
.polyspaceroot\polyspace\examples\doc_pstest\profiling_configs\hostFor targets without a file system, you can adapt the example configuration file in the folder
.polyspaceroot\polyspace\examples\doc_pstest\profiling_configs\targets_no_filesystem
In the examples below, the folder containing this header is referred to as
<PSPROFILE_CONFIG_INCLUDE>. For more information on the macros used in the configuration file, see Configuration Macros for Code Profiling.A file
psprofile_config.cthat defines the implementation of the target initialization and data sending functions declared inpsprofile_config.h(required only for targets without a file system).For instance, if you define macros
PSPROFILE_SENDING_INIT()andPSPROFILE_SENDING_DATA_ASYNC()as shown above, your filepsprofile_config.ccan contain the definitions of the functions mapped to these macros:In the examples below, this file is referred to asint psprofile_sending_init(void) { // Function body } int psprofile_sending_data_async(const char* ptrData, uint32_T numData) { ...// Function body }<PSPROFILE_CONFIG_SOURCE>.
To create the libraries, compile the profiling source file and the profiling configuration source file in separate compilation commands. To make sure that the configuration file is used, define the macro PST_USER_CONFIG_FILE during compilation of the profiling source file. For example, if you are using the GCC compiler, compile the sources and includes as
follows:
gcc -c <PSPROFILE_SOURCE> -I <PSPROFILE_INCLUDE> -I <PSPROFILE_CONFIG_INCLUDE> -D PST_USER_CONFIG_FILE -o psProfileLib
gcc -c <PSPROFILE_CONFIG_SOURCE> -I <PSPROFILE_CONFIG_INCLUDE> -o psProfileConfigsLibYou can then use the library at link time just as you would use the precompiled libraries provided with Polyspace Test. For example, with the GCC compiler, your source code instrumentation and linking steps might look like this:
polyspace-code-profiler -instrument -instrum-dir instrums -- gcc -c src.c
gcc src.o psProfileLib.o psProfileConfigsLib.opolyspace-code-profiler -run to collect coverage data. For a complete example of the workflow, see Manual Conversion Mode for Code Profiling on Targets.Configuration Macros for Code Profiling
This table provides a list of configuration macros that you can use to adapt code profiling for specific targets.
| Macro | Description | Example |
|---|---|---|
PSPROFILE_HOST_EXECUTION | Specifies whether to build for test execution on host or target. | For execution on host, set this macro to 1: For execution on target, set this macro to 0: |
PSPROFILE_SENDING_TYPE | Specifies whether tests will executed on a target with a file system or not. | For execution on host or targets with a file system, set this macro to For targets without a file system, set this macro to |
PSPROFILE_WORKING_BUFFER_SIZE | Specifies the size of the working buffer. | Set a working buffer size of 512 bytes as follows: |
PSPROFILE_SANITIZER | Specifies whether the build should support code sanitizer profiling. | Enable code sanitizer profiling as follows: |
PSPROFILE_PROFILING_COUNTER_WIDTH | Specifies whether to use a 32-bit or 64-bit counter for code profiling. | Specify a 32-bit counter as follows: |
PSPROFILE_EXECPROF_AVAILABLE | Specifies whether the build should support code execution profiling. | Enable execution profiling as follows: |
PSPROFILE_STACKPROF_AVAILABLE | Specifies whether the build should support code memory use profiling. | Enable memory use profiling as follows: |
PSPROFILE_SENDING_INIT | Specifies an initialization function for initializing the target. | Specify the function PSPROFILE_SUCCESS if you include the header psprofile.h).For an example function implementation, see Manual Conversion Mode for Code Profiling on Targets. |
PSPROFILE_SENDING_DATA_ASYNC | Specifies a streaming function to send data from target to the host. | Specify the function PSPROFILE_SUCCESS if you include the header
psprofile.h).For an example function implementation, see Manual Conversion Mode for Code Profiling on Targets. |