Coming soon in Red Hat Enterprise Linux 9.3 and 8.9—and already in CentOS Stream 9 and 8—we're updating the rust-toolset package to Rust 1.71.1. This comes with many upstream features, like the new OnceLock API and Cargo's new "sparse" protocol, but an additional change we're making to our packaging is to include the profiler runtime with the Rust standard library. This has no impact on the default Rust compilation workflow, but it does enable two new capabilities: source-based code coverage and profile-guided optimization.
In this blog, I'll explain how to use code coverage and profile-guided optimization with Rust code on Red Hat Enterprise Linux.
Setup
If you've already been using rust-toolset on RHEL, then simply upgrading to the new version will include the necessary runtime library. In addition, the llvm package will be needed for a few additional tools to process the instrumented data. On a new system, you can get it all by running:
$ sudo yum install rust-toolset llvm
If your project also includes C or C++ code, you may want the full llvm-toolset
package to include the Clang compiler as well.
Source-based code coverage
Code coverage is usually part of the dev/test cycle, checking that execution (usually of tests) reaches as much of the code as possible. This way, the developer can increase confidence that all parts of the code are operating correctly. The newly enabled code coverage option in Rust instruments precise annotations based on the original code, so all of the branches and regions of code are represented precisely, rather than just line-based annotations of some older coverage tools.
The rustc
option -Cinstrument-coverage
is best applied in a Cargo build by setting the RUSTFLAGS
environment variable during development builds.
$ env RUSTFLAGS="-Cinstrument-coverage" cargo build
You can also enable this while building and running your entire test suite:
$ env RUSTFLAGS="-Cinstrument-coverage" cargo test
When each application runs, it will output a *.profraw
file in the current directory. LLVM tools can then process and report code coverage from that data.
$ llvm-profdata merge -sparse *.profraw -o coverage.profdata $ llvm-cov show -instr-profile=coverage.profdata \ ./target/debug/hello-world 1| 1|fn main() { 2| 1| println!("Hello, world!"); 3| 1|}
Additional options like -show-line-counts-or-regions
can be added to show more detailed region information when the code is more complicated than line counts alone can represent.
For more information, see the Code Coverage section of the rustc book, as well as the similar section from Clang if you're also using C or C++, and the documentation for llvm-profdata and llvm-cov for more options in reporting.
Profile-guided optimization
A lot of compiler optimizations rely on inferring (and sometimes guessing) which code paths are likely to be taken most often so it can generate the fastest outcome for those paths. This especially matters when an unlikely path might place a heavy cost on code generation that the likely path would do better to avoid. Profile-guided optimization (PGO) is a way to avoid guessing and inform the compiler using real workloads running your program.
The process for building with PGO works in two phases: instrumentation with -Cprofile-generate
, and application with -Cprofile-use
. These are options to the rustc
compiler, and in a normal Cargo-driven build they are easiest to apply using the RUSTFLAGS
environment variable.
To instrument your application, set the option with a directory path to store the raw profiling data. It's also good practice to explicitly set the --target
, as that separates the RUSTFLAGS
from build scripts and procedural macros during the build process. Since we're also focusing on optimization, it's a good idea to use a --release build for this.
$ PROFDIR=$(mktemp -d) $ env RUSTFLAGS="-Cprofile-generate=$PROFDIR" \ cargo build --release --target x86_64-unknown-linux-gnu
The instrumented binary will be ./target/x86_64-unknown-linux-gnu/release/
. Run it a few times with representative workloads for your program, and each run will create a raw profile in the PROFDIR
we specified at build time. Then this data can be merged and used in a new build:
$ llvm-profdata merge "$PROFDIR" -o "$PROFDIR/merged.profdata" $ env RUSTFLAGS="-Cprofile-use=$PROFDIR/merged.profdata" \ cargo build --release --target x86_64-unknown-linux-gnu
The new binary will no longer be instrumented, but it will be optimized using the data from your training workloads, and this should be more performant than a non-PGO build.
For more information, see the Profile-guided Optimization section of the rustc book, as well as the similar section from Clang if you're also using C or C++.
Summary
With the inclusion of the profiler runtime in rust-toolset
in RHEL 9.3 and 8.9, Rust is ready to enhance your workflow with code coverage during development and profile-guided optimization for deployment. We hope you find these features useful!
저자 소개
Josh Stone is a part of the Platform Tools Team, where he is responsible for the Rust toolchain.
채널별 검색
오토메이션
기술, 팀, 인프라를 위한 IT 자동화 최신 동향
인공지능
고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트
오픈 하이브리드 클라우드
하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요
보안
환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보
엣지 컴퓨팅
엣지에서의 운영을 단순화하는 플랫폼 업데이트
인프라
세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보
애플리케이션
복잡한 애플리케이션에 대한 솔루션 더 보기
오리지널 쇼
엔터프라이즈 기술 분야의 제작자와 리더가 전하는 흥미로운 스토리
제품
- Red Hat Enterprise Linux
- Red Hat OpenShift Enterprise
- Red Hat Ansible Automation Platform
- 클라우드 서비스
- 모든 제품 보기
툴
체험, 구매 & 영업
커뮤니케이션
Red Hat 소개
Red Hat은 Linux, 클라우드, 컨테이너, 쿠버네티스 등을 포함한 글로벌 엔터프라이즈 오픈소스 솔루션 공급업체입니다. Red Hat은 코어 데이터센터에서 네트워크 엣지에 이르기까지 다양한 플랫폼과 환경에서 기업의 업무 편의성을 높여 주는 강화된 기능의 솔루션을 제공합니다.