#coverage #profiling #pgo #llvm-cov #no-std

no-std dev minicov

为no_std和嵌入式程序提供代码覆盖率和支持基于配置文件的优化(PGO)

14个版本

0.3.5 2024年6月25日
0.3.3 2023年11月28日
0.3.2 2023年3月29日
0.3.1 2022年12月21日
0.1.1 2020年4月20日

#54 in 嵌入式开发

Download history 476/week @ 2024-04-26 582/week @ 2024-05-03 606/week @ 2024-05-10 548/week @ 2024-05-17 365/week @ 2024-05-24 196/week @ 2024-05-31 324/week @ 2024-06-07 308/week @ 2024-06-14 636/week @ 2024-06-21 224/week @ 2024-06-28 58/week @ 2024-07-05 258/week @ 2024-07-12 312/week @ 2024-07-19 360/week @ 2024-07-26 601/week @ 2024-08-02 6995/week @ 2024-08-09

8,364每月下载量
用于 778 个包(4直接使用)

Apache-2.0/MIT

120KB
2K SLoC

C 1K SLoC // 0.2% comments Bitbake 802 SLoC // 0.1% comments Rust 186 SLoC // 0.0% comments

minicov

Crates.io Documentation

此包为no_std和嵌入式程序提供了代码覆盖率和配置文件引导优化(PGO)支持。

这是通过修改版本的LLVM分析运行时(通常是compiler-rt的一部分)实现的,其中已经去除了所有对libc的依赖。

支持所有使用LLVM分析运行时的仪器类型

  • 使用-Cinstrument-coverage进行Rust代码覆盖率。
  • 使用-Cprofile-generate进行Rust配置文件引导优化。
  • 使用-fprofile-instr-generate -fcoverage-mapping进行Clang代码覆盖率。
  • 使用-fprofile-instr-generate进行Clang配置文件引导优化。
  • 使用-fprofile-generate进行Clang LLVM IR配置文件引导优化。

注意,要同时配置C和Rust代码,必须使用与rustc使用的LLVM版本相同的Clang。您可以通过环境变量将这些标志传递给使用cc包编译的C代码。

用法

注意:此包需要一个最新的夜间编译器。

  1. 确保以下环境变量已设置
export RUSTFLAGS="-Cinstrument-coverage -Zno-profiler-runtime"

注意,这些标志默认情况下也适用于构建依赖项和进程宏。通过在调用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. minicov包作为依赖项添加到您的程序中
[dependencies]
minicov = "0.3"
  1. 在程序退出之前,请调用 minicov::capture_coverage 函数,并传入一个接收器(例如 Vec<u8>)并使用 .profraw 扩展名将该接收器的内容输出到文件
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();
}

如果你的程序运行在不同的系统上,那么你需要将这个文件传输回你的构建系统。

接收器必须实现 CoverageWriter 特性。如果默认的 alloc 功能被启用,那么为 Vec<u8> 提供了一个实现。

  1. 使用诸如 grcov 或 llvm-cov 等工具生成可读的覆盖率报告
grcov output.profraw -b ./target/debug/my_program -s . -t html -o cov_report

基于配置文件的优化

基于配置文件的优化的步骤类似。唯一不同的是传递给 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

变更日志

许可证

在以下许可证中选择一项:

任选其一。

贡献

除非你明确声明,否则根据 Apache-2.0 许可证定义的,你提交的任何旨在包含在作品中的贡献,将如上所述双许可,没有额外的条款或条件。

无运行时依赖

~0–0.9MB