#coverage #embedded-profiling #profiling #pgo #llvm-ir #llvm-cov #code-coverage

no-std dev minicov

Code coverage and profile-guided optimization support for no_std and embedded programs

16 releases

0.3.7 Nov 3, 2024
0.3.5 Jun 25, 2024
0.3.3 Nov 28, 2023
0.3.2 Mar 29, 2023
0.1.1 Apr 20, 2020

#45 in Embedded development

Download history 18436/week @ 2024-08-17 23276/week @ 2024-08-24 25684/week @ 2024-08-31 27184/week @ 2024-09-07 32940/week @ 2024-09-14 37617/week @ 2024-09-21 40062/week @ 2024-09-28 46500/week @ 2024-10-05 45070/week @ 2024-10-12 50299/week @ 2024-10-19 47982/week @ 2024-10-26 56154/week @ 2024-11-02 48730/week @ 2024-11-09 48366/week @ 2024-11-16 54262/week @ 2024-11-23 55010/week @ 2024-11-30

214,378 downloads per month
Used in 876 crates (4 directly)

Apache-2.0/MIT

130KB
2.5K SLoC

C 1.5K SLoC // 0.2% comments Bitbake 807 SLoC // 0.1% comments Rust 186 SLoC // 0.0% comments

minicov

Crates.io Documentation

This crate provides code coverage and profile-guided optimization (PGO) support for no_std and embedded programs.

This is done through a modified version of the LLVM profiling runtime (normally part of compiler-rt) from which all dependencies on libc have been removed.

All types of instrumentation using the LLVM profiling runtime are supported:

  • Rust code coverage with -Cinstrument-coverage.
  • Rust profile-guided optimization with -Cprofile-generate.
  • Clang code coverage with -fprofile-instr-generate -fcoverage-mapping.
  • Clang profile-guided optimization with -fprofile-instr-generate.
  • Clang LLVM IR profile-guided optimization with -fprofile-generate.

Note that to profile both C and Rust code at the same time you must use Clang with the same LLVM version as the LLVM used by rustc. You can pass these flags to C code compiled with the cc crates using environment variables.

Usage

Note: This crate requires a recent nightly compiler.

  1. Ensure that the following environment variables are set up:
export RUSTFLAGS="-Cinstrument-coverage -Zno-profiler-runtime"

Note that these flags also apply to build-dependencies and proc macros by default. This can be worked around by explicitly specifying a target when invoking cargo:

# Applies RUSTFLAGS to everything
cargo build

# Doesn't apply RUSTFLAGS to build dependencies and proc macros
cargo build --target x86_64-unknown-linux-gnu
  1. Add the minicov crate as a dependency to your program:
[dependencies]
minicov = "0.3"
  1. Before your program exits, call minicov::capture_coverage with a sink (such as Vec<u8>) and then dump its contents to a file with the .profraw extension:
fn main() {
    // ...

    let mut coverage = vec![];
    unsafe {
        // Note that this function is not thread-safe! Use a lock if needed.
        minicov::capture_coverage(&mut coverage).unwrap();
    }
    std::fs::write("output.profraw", coverage).unwrap();
}

If your program is running on a different system than your build system then you will need to transfer this file back to your build system.

Sinks must implement the CoverageWriter trait. If the default alloc feature is enabled then an implementation is provided for Vec<u8>.

  1. Use a tool such as grcov or llvm-cov to generate a human-readable coverage report:
grcov output.profraw -b ./target/debug/my_program -s . -t html -o cov_report

Profile-guided optimization

The steps for profile-guided optimization are similar. The only difference is the flags passed in RUSTFLAGS:

# First run to generate profiling information.
export RUSTFLAGS="-Cprofile-generate -Zno-profiler-runtime"
cargo run --target x86_64-unknown-linux-gnu --release

# Post-process the profiling information.
# The rust-profdata tool comes from cargo-binutils.
rust-profdata merge -o output.profdata output.profraw

# Optimized build using PGO. minicov is not needed in this step.
export RUSTFLAGS="-Cprofile-use=output.profdata"
cargo build --target x86_64-unknown-linux-gnu --release

Change log

License

Licensed under either of:

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps

~0–1MB
~11K SLoC