Skip to content

Commit 804579c

Browse files
Respond to RFC comments.
1 parent 1008822 commit 804579c

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ fn get_pgo_sample_use_path(config: &ModuleConfig) -> Option<CString> {
425425

426426
fn get_instr_profile_output_path(config: &ModuleConfig) -> Option<CString> {
427427
if config.instrument_coverage {
428-
Some(CString::new(format!("{}", PathBuf::from("default_%m_%p.profraw").display())).unwrap())
428+
Some(CString::new("default_%m_%p.profraw").unwrap())
429429
} else {
430430
None
431431
}

src/doc/rustc/src/instrument-coverage.md

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,24 @@ $ echo "{some: 'thing'}" | target/debug/examples/formatjson5 -
9797
}
9898
```
9999

100-
After running this program, a new file, `default.profraw`, should be in the current working directory. It's often preferable to set a specific file name or path. You can change the output file using the environment variable `LLVM_PROFILE_FILE`:
100+
After running this program, a new file, `default_%m_%p.profraw`, should be in the current working directory. This file takes advantage ofLLVM's support for rewriting special pattern strings to ensure `.profraw` files generated are unique. The following special pattern strings are rewritten as:
101+
102+
- `%p` - The process ID.
103+
- `%h` - The hostname of the machine running the program.
104+
- `%t` - The value of the TMPDIR environment variable.
105+
- `%Nm` - the instrumented binary’s signature: The runtime creates a pool of N raw profiles, used for on-line profile merging. The runtime takes care of selecting a raw profile from the pool, locking it, and updating it before the program exits. `N` must be between `1` and `9`, and defaults to `1` if omitted (with simply `%m`).
106+
- `%c` - Does not add anything to the filename, but enables a mode (on some platforms, including Darwin) in which profile counter updates are continuously synced to a file. This means that if the instrumented program crashes, or is killed by a signal, perfect coverage information can still be recovered.
107+
108+
```shell
109+
$ echo "{some: 'thing'}" | target/debug/examples/formatjson5 -
110+
...
111+
$ ls default_11699812450447639123_0_20944.profraw
112+
default_11699812450447639123_0_20944.profraw
113+
```
114+
115+
In the example above, the value `11699812450447639123_0` in the generated filename is the instrumented binary's signature, which replaced the `%m` pattern and the value `20944` is the process ID of the binary being executed.
116+
117+
You can also set a specific file name or path for the generated `.profraw` files by using the environment variable `LLVM_PROFILE_FILE`:
101118

102119
```shell
103120
$ echo "{some: 'thing'}" \
@@ -107,14 +124,6 @@ $ ls formatjson5.profraw
107124
formatjson5.profraw
108125
```
109126

110-
If `LLVM_PROFILE_FILE` contains a path to a non-existent directory, the missing directory structure will be created. Additionally, the following special pattern strings are rewritten:
111-
112-
- `%p` - The process ID.
113-
- `%h` - The hostname of the machine running the program.
114-
- `%t` - The value of the TMPDIR environment variable.
115-
- `%Nm` - the instrumented binary’s signature: The runtime creates a pool of N raw profiles, used for on-line profile merging. The runtime takes care of selecting a raw profile from the pool, locking it, and updating it before the program exits. `N` must be between `1` and `9`, and defaults to `1` if omitted (with simply `%m`).
116-
- `%c` - Does not add anything to the filename, but enables a mode (on some platforms, including Darwin) in which profile counter updates are continuously synced to a file. This means that if the instrumented program crashes, or is killed by a signal, perfect coverage information can still be recovered.
117-
118127
## Installing LLVM coverage tools
119128

120129
LLVM's supplies two tools—`llvm-profdata` and `llvm-cov`—that process coverage data and generate reports. There are several ways to find and/or install these tools, but note that the coverage mapping data generated by the Rust compiler requires LLVM version 12 or higher, and processing the *raw* data may require exactly the LLVM version used by the compiler. (`llvm-cov --version` typically shows the tool's LLVM version number, and `rustc --verbose --version` shows the version of LLVM used by the Rust compiler.)
@@ -181,11 +190,12 @@ A typical use case for coverage analysis is test coverage. Rust's source-based c
181190

182191
The following example (using the [`json5format`] crate, for demonstration purposes) show how to generate and analyze coverage results for all tests in a crate.
183192

184-
Since `cargo test` both builds and runs the tests, we set both the additional `RUSTFLAGS`, to add the `-C instrument-coverage` flag, and `LLVM_PROFILE_FILE`, to set a custom filename for the raw profiling data generated during the test runs. Since there may be more than one test binary, apply `%m` in the filename pattern. This generates unique names for each test binary. (Otherwise, each executed test binary would overwrite the coverage results from the previous binary.)
193+
Since `cargo test` both builds and runs the tests, we set the additional `RUSTFLAGS`, to add the `-C instrument-coverage` flag. If setting `LLVM_PROFILE_FILE` to specify a custom filename for the raw profiling data generated during the test runs,
194+
apply `%m` in the filename pattern since there may be more than one test binary. This generates unique names for each test binary which is not done by default when setting the `LLVM_PROFILE_FILE` environment variable.
195+
(Otherwise, each executed test binary would overwrite the coverage results from the previous binary.) If not setting `LLVM_PROFILE_FILE`, the `%m` and `%p` filename patterns are added by default.
185196

186197
```shell
187198
$ RUSTFLAGS="-C instrument-coverage" \
188-
LLVM_PROFILE_FILE="json5format-%m.profraw" \
189199
cargo test --tests
190200
```
191201

@@ -210,7 +220,7 @@ test result: ok. 31 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
210220
You should have one or more `.profraw` files now, one for each test binary. Run the `profdata` tool to merge them:
211221

212222
```shell
213-
$ llvm-profdata merge -sparse json5format-*.profraw -o json5format.profdata
223+
$ llvm-profdata merge -sparse default_*.profraw -o json5format.profdata
214224
```
215225

216226
Then run the `cov` tool, with the `profdata` file and all test binaries:
@@ -271,9 +281,8 @@ To include doc tests in the coverage results, drop the `--tests` flag, and apply
271281
```bash
272282
$ RUSTFLAGS="-C instrument-coverage" \
273283
RUSTDOCFLAGS="-C instrument-coverage -Z unstable-options --persist-doctests target/debug/doctestbins" \
274-
LLVM_PROFILE_FILE="json5format-%m.profraw" \
275284
cargo test
276-
$ llvm-profdata merge -sparse json5format-*.profraw -o json5format.profdata
285+
$ llvm-profdata merge -sparse default_*.profraw -o json5format.profdata
277286
```
278287

279288
The `-Z unstable-options --persist-doctests` flag is required, to save the test binaries
@@ -302,8 +311,7 @@ $ llvm-cov report \
302311
> version without doc tests, include:
303312
304313
- The `cargo test ... --no-run` command is updated with the same environment variables
305-
and flags used to _build_ the tests, _including_ the doc tests. (`LLVM_PROFILE_FILE`
306-
is only used when _running_ the tests.)
314+
and flags used to _build_ the tests, _including_ the doc tests.
307315
- The file glob pattern `target/debug/doctestbins/*/rust_out` adds the `rust_out`
308316
binaries generated for doc tests (note, however, that some `rust_out` files may not
309317
be executable binaries).

0 commit comments

Comments
 (0)