Profile-Guided Optimization (PGO)
Overview
Profile-Guided Optimization (PGO) is a compiler optimization technique that uses runtime profiling data to generate more efficient code. The optimization process consists of two phases:
Instrumentation Phase: Build with profiling instrumentation to collect runtime execution data
Optimization Phase: Rebuild using the collected profile data to generate optimized code
PGO can significantly improve performance by:
Optimizing hot code paths based on actual runtime behavior
Improving branch prediction and code layout
Reducing instruction cache misses
Enabling better inlining decisions
Configuration
Kconfig Options
To enable PGO, you need to configure the following options:
Enable code coverage for all modules:
CONFIG_COVERAGE_ALL=y
Select PGO mode instead of source-based coverage:
CONFIG_COVERAGE_PGO=y
Enable the gcov system utility to dump profile data:
CONFIG_SYSTEM_GCOV=y
These options can be set via menuconfig:
$ make menuconfig
Navigate to:
Toolchain Library Support
-> Enable code coverage for all module
Coverage mode (Profile Guided Optimization)
Application Configuration
-> System Libraries and NSH Add-Ons
-> gcov tool
Supported Toolchains
Currently, PGO support is primarily implemented for Clang/LLVM toolchains. GCC support is planned but not yet fully implemented.
Usage Workflow
Phase 1: Collect Profile Data
Configure with PGO instrumentation:
$ make menuconfig # Enable CONFIG_COVERAGE_ALL, CONFIG_COVERAGE_PGO, and CONFIG_SYSTEM_GCOV
Build the system:
For CMake-based builds:
$ cmake -B build -S nuttx/ -DBOARD_CONFIG=<your_board> $ cmake --build build
Run the application and dump profile data:
Execute your application with representative workloads. The instrumented binary will collect profiling data internally. After the workload completes, use the
gcovcommand to dump the profile data:$ # In NuttX shell (nsh) nsh> gcov -d /data/default.profraw # Or specify the output directory nsh> gcov -d /tmp/default.profraw
The profile data will be written to the specified location. If using a filesystem like hostfs, you can directly access it from the host. Otherwise, use file transfer methods (TFTP, serial, etc.) to retrieve it.
Transfer profile data to build directory:
Copy the
default.profrawfile from the target to your build directory:$ # If using hostfs, simply copy from mounted directory $ cp /path/to/hostfs/default.profraw . $ # Or use other file transfer methods $ scp target:/data/default.profraw . $ # or via serial, TFTP, etc.
Verify profile data:
Check that
default.profrawhas been successfully transferred to the build directory:$ ls -lh default.profraw
Phase 2: Build with Profile Data
Keep the profile data:
Ensure
default.profrawfrom Phase 1 is present in the build directory. The file must be transferred from the target device to your host build directory (e.g., via hostfs, serial transfer, or other methods).Rebuild:
Simply rebuild the project. The build system will automatically:
Detect the presence of
default.profrawMerge profile data using
llvm-profdataGenerate
default.profdataReconfigure the build with
-fprofile-use=<path>/default.profdataCompile with optimizations based on profile data
For CMake:
$ llvm-profdata merge *.profraw -o default.profdata $ cmake --build build # CMake will automatically reconfigure and rebuild with profile data
Deploy optimized binary:
The resulting binary is now optimized based on the collected profile data.
Implementation Details
CMake Implementation
For CMake-based builds, the PGO logic is implemented in
nuttx/cmake/nuttx_pgo.cmake. The nuttx_add_pgo_flags() function:
Checks for
${CMAKE_BINARY_DIR}/default.profrawIf found, executes:
llvm-profdata merge -output=default.profdata *.profrawAdds
-fprofile-use=<path>if merge succeedsOtherwise, adds
-fprofile-generatefor data collection
Profile Data Format
`.profraw`: Raw profile data generated during program execution
`.profdata`: Indexed profile data created by
llvm-profdata merge
The .profdata format is used by the compiler for optimization.
Best Practices
Representative Workloads
For best results, run workloads that are representative of actual usage patterns:
Execute all critical code paths
Include typical input data
Run for sufficient duration to capture steady-state behavior
Include edge cases if they’re common in production
Iterative Optimization
You can iterate the PGO process multiple times:
Build with
-fprofile-generateRun and collect new profile data
Rebuild with
-fprofile-useIf needed, collect profile data from the optimized binary
Rebuild again with updated profile data
Profile Data Management
Store profile data (
default.profraw) separately for different configurationsUse version control to track profile data for reproducible builds
Update profile data when workload patterns change significantly
Clean Builds
To start fresh:
$ rm -f default.profraw default.profdata
$ make clean
$ make
Limitations
GCC Support: GCC PGO is planned but not fully implemented
Cross-compilation: Profile data must be collected on target or simulator
Binary Size: Instrumented binaries are larger and slower
Profile Validity: Profile data may become stale as code changes
Examples
Example: CMake Build with Hostfs
# Phase 1: Collect profile data
$ cmake -B build -DBOARD_CONFIG=qemu-armv8a:nsh
$ cmake --build build
$ qemu-system-aarch64 -cpu cortex-a53 -nographic -machine virt,virtualization=on,gic-version=3 -kernel build/nuttx -semihosting
# In NuttX shell:
# nsh> mount -t hostfs -o fs=/path/to/host/build /mnt
# nsh> <run your workload>
# nsh> gcov -d /mnt/default.profraw
# Exit QEMU - profile data is now in build directory
# Phase 2: Optimized build
$ cmake --build build
# Build system detects default.profraw and rebuilds with optimizations
$ # Deploy optimized build/nuttx.bin
See Also
LLVM PGO Documentation: https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization