Skip to content

Commit c90299b

Browse files
committed
Ruby: Move codeql_threads calculation to library
1 parent 7467128 commit c90299b

File tree

6 files changed

+49
-31
lines changed

6 files changed

+49
-31
lines changed

ruby/extractor/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ruby/extractor/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ clap = "3.0"
1414
tracing = "0.1"
1515
tracing-subscriber = { version = "0.3.3", features = ["env-filter"] }
1616
rayon = "1.5.0"
17-
num_cpus = "1.14.0"
1817
regex = "1.7.1"
1918
encoding = "0.2"
2019
lazy_static = "1.4.0"

ruby/extractor/src/bin/extractor.rs

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#[macro_use]
22
extern crate lazy_static;
3-
extern crate num_cpus;
43

54
use clap::arg;
6-
use encoding::{self};
5+
use encoding;
76
use rayon::prelude::*;
87
use std::borrow::Cow;
98
use std::fs;
@@ -13,32 +12,6 @@ use tree_sitter::{Language, Parser, Range};
1312

1413
use codeql_extractor::{diagnostics, extractor, file_paths, node_types, trap};
1514

16-
/**
17-
* Gets the number of threads the extractor should use, by reading the
18-
* CODEQL_THREADS environment variable and using it as described in the
19-
* extractor spec:
20-
*
21-
* "If the number is positive, it indicates the number of threads that should
22-
* be used. If the number is negative or zero, it should be added to the number
23-
* of cores available on the machine to determine how many threads to use
24-
* (minimum of 1). If unspecified, should be considered as set to -1."
25-
*/
26-
fn num_codeql_threads() -> Result<usize, String> {
27-
let threads_str = std::env::var("CODEQL_THREADS").unwrap_or_else(|_| "-1".to_owned());
28-
match threads_str.parse::<i32>() {
29-
Ok(num) if num <= 0 => {
30-
let reduction = -num as usize;
31-
Ok(std::cmp::max(1, num_cpus::get() - reduction))
32-
}
33-
Ok(num) => Ok(num as usize),
34-
35-
Err(_) => Err(format!(
36-
"Unable to parse CODEQL_THREADS value '{}'",
37-
&threads_str
38-
)),
39-
}
40-
}
41-
4215
lazy_static! {
4316
static ref CP_NUMBER: regex::Regex = regex::Regex::new("cp([0-9]+)").unwrap();
4417
}
@@ -67,7 +40,7 @@ fn main() -> std::io::Result<()> {
6740
.init();
6841
let diagnostics = diagnostics::DiagnosticLoggers::new("ruby");
6942
let mut main_thread_logger = diagnostics.logger();
70-
let num_threads = match num_codeql_threads() {
43+
let num_threads = match codeql_extractor::options::num_threads() {
7144
Ok(num) => num,
7245
Err(e) => {
7346
main_thread_logger.write(

shared/extractor/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ lazy_static = "1.4.0"
1515
serde = { version = "1.0", features = ["derive"] }
1616
serde_json = "1.0"
1717
chrono = { version = "0.4.19", features = ["serde"] }
18+
num_cpus = "1.14.0"

shared/extractor/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ pub mod extractor;
33
pub mod file_paths;
44
pub mod generator;
55
pub mod node_types;
6+
pub mod options;
67
pub mod trap;

shared/extractor/src/options.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use num_cpus;
2+
3+
/// Gets the number of threads the extractor should use.
4+
/// This is controlled by the CODEQL_THREADS environment variable.
5+
pub fn num_threads() -> Result<usize, String> {
6+
let threads_str = std::env::var("CODEQL_THREADS").unwrap_or_else(|_| "-1".into());
7+
let num_cpus = num_cpus::get();
8+
parse_codeql_threads(&threads_str, num_cpus)
9+
.ok_or_else(|| format!("Unable to parse CODEQL_THREADS value '{}'", &threads_str))
10+
}
11+
12+
/// Parses the given string to determine the number of threads the extractor
13+
/// should use, as described in the extractor spec:
14+
///
15+
/// "If the number is positive, it indicates the number of threads that should
16+
/// be used. If the number is negative or zero, it should be added to the number
17+
/// of cores available on the machine to determine how many threads to use
18+
/// (minimum of 1). If unspecified, should be considered as set to -1."
19+
///
20+
/// # Examples
21+
///
22+
/// ```
23+
/// use codeql_extractor::options::parse_codeql_threads;
24+
///
25+
/// assert_eq!(parse_codeql_threads("1", 4), Some(1));
26+
/// assert_eq!(parse_codeql_threads("5", 4), Some(5));
27+
/// assert_eq!(parse_codeql_threads("0", 4), Some(4));
28+
/// assert_eq!(parse_codeql_threads("-1", 4), Some(3));
29+
/// assert_eq!(parse_codeql_threads("-3", 4), Some(1));
30+
/// assert_eq!(parse_codeql_threads("-4", 4), Some(1));
31+
/// assert_eq!(parse_codeql_threads("-5", 4), Some(1));
32+
/// assert_eq!(parse_codeql_threads("nope", 4), None);
33+
/// ```
34+
pub fn parse_codeql_threads(threads_str: &str, num_cpus: usize) -> Option<usize> {
35+
match threads_str.parse::<i32>() {
36+
Ok(num) if num <= 0 => {
37+
let reduction = num.abs_diff(0) as usize;
38+
Some(std::cmp::max(1, num_cpus.saturating_sub(reduction)))
39+
}
40+
Ok(num) => Some(num as usize),
41+
42+
Err(_) => None,
43+
}
44+
}

0 commit comments

Comments
 (0)