Skip to content

Commit 8cfb047

Browse files
committed
Let llvm tools auto-detect threads when not explicitly specified
When user doesn't pass --threads flag, avoid forcing --num-threads on llvm-profdata and llvm-cov to allow them to auto-detect optimal thread count. Only pass --num-threads when user explicitly sets a value.
1 parent 87fefc3 commit 8cfb047

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ pub fn consumer(
179179
guess_directory: bool,
180180
binary_path: Option<&Path>,
181181
ignore_parsing_error: bool,
182-
num_threads: usize,
182+
llvm_threads: Option<usize>,
183183
) {
184184
let mut gcov_type = GcovType::Unknown;
185185

@@ -291,7 +291,7 @@ pub fn consumer(
291291
profraw_paths.as_slice(),
292292
binary_path.as_ref().unwrap(),
293293
working_dir,
294-
num_threads,
294+
llvm_threads,
295295
) {
296296
Ok(lcovs) => {
297297
let mut new_results: Vec<(String, CovResult)> = Vec::new();

src/llvm_tools.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,25 @@ pub fn llvm_profiles_to_lcov(
8181
profile_paths: &[PathBuf],
8282
binary_path: &Path,
8383
working_dir: &Path,
84-
num_threads: usize,
84+
num_threads: Option<usize>,
8585
) -> Result<Vec<Vec<u8>>, String> {
8686
let profdata_path = working_dir.join("grcov.profdata");
8787

88-
let num_threads = num_threads.to_string();
89-
let args = vec![
88+
let mut args = vec![
9089
"merge".as_ref(),
9190
"-f".as_ref(),
9291
"-".as_ref(),
9392
"-sparse".as_ref(),
9493
"-o".as_ref(),
9594
profdata_path.as_ref(),
96-
"--num-threads".as_ref(),
97-
num_threads.as_ref(),
9895
];
96+
let num_threads_str: String;
97+
// Only pass --num-threads if explicitly specified by the user
98+
if let Some(num) = num_threads {
99+
num_threads_str = num.to_string();
100+
args.push("--num-threads".as_ref());
101+
args.push(num_threads_str.as_ref());
102+
}
99103

100104
let stdin_paths: String = profile_paths.iter().fold("".into(), |mut a, x| {
101105
a.push_str(x.to_string_lossy().as_ref());
@@ -111,14 +115,21 @@ pub fn llvm_profiles_to_lcov(
111115
let results = binaries
112116
.into_par_iter()
113117
.filter_map(|binary| {
114-
let args = [
118+
let mut args = vec![
115119
"export".as_ref(),
116120
binary.as_ref(),
117121
"--instr-profile".as_ref(),
118122
profdata_path.as_ref(),
119123
"--format".as_ref(),
120124
"lcov".as_ref(),
121125
];
126+
let num_threads_str: String;
127+
// Only pass --num-threads if explicitly specified by the user
128+
if let Some(num) = num_threads {
129+
num_threads_str = num.to_string();
130+
args.push("--num-threads".as_ref());
131+
args.push(num_threads_str.as_ref());
132+
}
122133

123134
match run(&cov_tool_path, &args) {
124135
Ok(result) => Some(result),
@@ -305,7 +316,7 @@ mod tests {
305316
&[tmp_path.join("default.profraw")],
306317
&PathBuf::from("src"), // There is no binary file in src
307318
tmp_path,
308-
1,
319+
None,
309320
);
310321
assert!(lcovs.is_ok());
311322
let lcovs = lcovs.unwrap();
@@ -322,7 +333,7 @@ mod tests {
322333
&[tmp_path.join("default.profraw")],
323334
&tmp_path.join(binary_path),
324335
tmp_path,
325-
1,
336+
None,
326337
);
327338
assert!(lcovs.is_ok(), "Error: {}", lcovs.unwrap_err());
328339
let lcovs = lcovs.unwrap();
@@ -355,8 +366,12 @@ mod tests {
355366

356367
assert_eq!(status.unwrap().code().unwrap(), 0);
357368

358-
let lcovs =
359-
llvm_profiles_to_lcov(&[profdata_path], &tmp_path.join(binary_path), tmp_path, 1);
369+
let lcovs = llvm_profiles_to_lcov(
370+
&[profdata_path],
371+
&tmp_path.join(binary_path),
372+
tmp_path,
373+
None,
374+
);
360375

361376
assert!(lcovs.is_ok(), "Error: {}", lcovs.unwrap_err());
362377
let lcovs = lcovs.unwrap();
@@ -403,7 +418,7 @@ mod tests {
403418
&[path_with, path_without],
404419
&tmp_path.join(bin_path),
405420
tmp_path,
406-
1,
421+
None,
407422
);
408423

409424
assert!(lcovs.is_ok(), "Error: {}", lcovs.unwrap_err());

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ fn main() {
451451
let branch_enabled = opt.branch;
452452
let guess_directory = opt.guess_directory;
453453
let ignore_parsing_error = opt.ignore_parsing_error;
454+
let llvm_threads = opt.threads;
454455

455456
let t = thread::Builder::new()
456457
.name(format!("Consumer {i}"))
@@ -465,7 +466,7 @@ fn main() {
465466
guess_directory,
466467
binary_path.as_deref(),
467468
ignore_parsing_error,
468-
num_threads,
469+
llvm_threads,
469470
);
470471
})
471472
.unwrap();

0 commit comments

Comments
 (0)