Skip to content

Commit b1a4bbf

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 359cb3e commit b1a4bbf

3 files changed

Lines changed: 30 additions & 11 deletions

File tree

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 & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ 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

8888
let num_threads = num_threads.to_string();
89-
let args = vec![
89+
let mut args = vec![
9090
"merge".as_ref(),
9191
"-f".as_ref(),
9292
"-".as_ref(),
@@ -96,6 +96,13 @@ pub fn llvm_profiles_to_lcov(
9696
"--num-threads".as_ref(),
9797
num_threads.as_ref(),
9898
];
99+
let num_threads_str: String;
100+
// Only pass --num-threads if explicitly specified by the user
101+
if let Some(num) = num_threads {
102+
num_threads_str = num.to_string();
103+
args.push("--num-threads".as_ref());
104+
args.push(num_threads_str.as_ref());
105+
}
99106

100107
let stdin_paths: String = profile_paths.iter().fold("".into(), |mut a, x| {
101108
a.push_str(x.to_string_lossy().as_ref());
@@ -111,14 +118,21 @@ pub fn llvm_profiles_to_lcov(
111118
let results = binaries
112119
.into_par_iter()
113120
.filter_map(|binary| {
114-
let args = [
121+
let mut args = vec![
115122
"export".as_ref(),
116123
binary.as_ref(),
117124
"--instr-profile".as_ref(),
118125
profdata_path.as_ref(),
119126
"--format".as_ref(),
120127
"lcov".as_ref(),
121128
];
129+
let num_threads_str: String;
130+
// Only pass --num-threads if explicitly specified by the user
131+
if let Some(num) = num_threads {
132+
num_threads_str = num.to_string();
133+
args.push("--num-threads".as_ref());
134+
args.push(num_threads_str.as_ref());
135+
}
122136

123137
match run(&cov_tool_path, &args) {
124138
Ok(result) => Some(result),
@@ -305,7 +319,7 @@ mod tests {
305319
&[tmp_path.join("default.profraw")],
306320
&PathBuf::from("src"), // There is no binary file in src
307321
tmp_path,
308-
1,
322+
None,
309323
);
310324
assert!(lcovs.is_ok());
311325
let lcovs = lcovs.unwrap();
@@ -322,7 +336,7 @@ mod tests {
322336
&[tmp_path.join("default.profraw")],
323337
&tmp_path.join(binary_path),
324338
tmp_path,
325-
1,
339+
None,
326340
);
327341
assert!(lcovs.is_ok(), "Error: {}", lcovs.unwrap_err());
328342
let lcovs = lcovs.unwrap();
@@ -355,8 +369,12 @@ mod tests {
355369

356370
assert_eq!(status.unwrap().code().unwrap(), 0);
357371

358-
let lcovs =
359-
llvm_profiles_to_lcov(&[profdata_path], &tmp_path.join(binary_path), tmp_path, 1);
372+
let lcovs = llvm_profiles_to_lcov(
373+
&[profdata_path],
374+
&tmp_path.join(binary_path),
375+
tmp_path,
376+
None,
377+
);
360378

361379
assert!(lcovs.is_ok(), "Error: {}", lcovs.unwrap_err());
362380
let lcovs = lcovs.unwrap();
@@ -403,7 +421,7 @@ mod tests {
403421
&[path_with, path_without],
404422
&tmp_path.join(bin_path),
405423
tmp_path,
406-
1,
424+
None,
407425
);
408426

409427
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)