Add %p to LLVM_PROFILE_FILE pattern when running tests with coverage #8894
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
The current setting for
LLVM_PROFILE_PATH
, used for code coverage, leads to corrupt profile data when tests are run in parallel or when writing "exit tests" with Swift Testing. This also results in theswift test --enable-code-coverage
command to fail.The
LLVM_PROFILE_PATH
environment variable is used by the runtime to write raw profile files, which are then processed when the test command finishes to produce the coverage results as JSON. The variable supports several pattern variables1, including%Nm
, which is currently set, and is documented to create a pool of files that the runtime will handle synchronisation of. This is fine for parallelism within the process but will not work across different processes. SwiftPM uses multiple invocations of the same binary for parallel testing and users may also fork processes within their tests, which is now a required workflow when using exit tests with Swift Testing, which will fork the process internally. Furthermore, the current setting for this variable uses only%m
(which impliesN=1
), which makes it even more likely that processes will stomp over each other when writing the raw profile data.We can see a discussion of this happening in practice in #8893.
The variable also supports
%p
1, which will expand to produce a per-process path for the raw profile, which is probably what we want here, since Swift PM is combining all the profiles in the configured directory.Modifications
Add %p to LLVM_PROFILE_FILE pattern when running tests with coverage.
Result
.serialized
#8893.Appendix: Demonstrating the merging of per-process profiles
Running with just one test results in one per-process profile and 50% coverage, as expected.
Running the other test also results in one per-process profile and 50% coverage, as expected.
Running both tests results in two per-process profile and 100% coverage, after merge.
Footnotes
https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#running-the-instrumented-program ↩ ↩2