From fe585ac24c8bf3f0410c83571edc758c33c1eb5f Mon Sep 17 00:00:00 2001 From: Josh Holmer Date: Sat, 21 May 2022 04:55:43 -0400 Subject: [PATCH] Use std::available_parallelism instead of num_cpus (#633) * Explicitly set minimum rust version * Use std::available_parallelism instead of num_cpus --- Cargo.lock | 1 - Cargo.toml | 1 + av1an-cli/Cargo.toml | 1 + av1an-core/Cargo.toml | 2 +- av1an-core/src/broker.rs | 3 ++- av1an-core/src/lib.rs | 5 ++++- av1an-core/src/settings.rs | 7 ++++++- av1an-core/src/target_quality.rs | 5 ++++- 8 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e6fe211..d332af3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -196,7 +196,6 @@ dependencies = [ "log", "memchr", "nom", - "num_cpus", "once_cell", "parking_lot", "paste", diff --git a/Cargo.toml b/Cargo.toml index 190ba44..d646c97 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ repository = "https://github.com/master-of-zen/Av1an" keywords = ["video"] categories = ["command-line-utilities"] license = "GPL-3.0" +rust-version = "1.59" edition = "2021" [[bin]] diff --git a/av1an-cli/Cargo.toml b/av1an-cli/Cargo.toml index 8bd1cf8..3fd942d 100644 --- a/av1an-cli/Cargo.toml +++ b/av1an-cli/Cargo.toml @@ -9,6 +9,7 @@ repository = "https://github.com/master-of-zen/Av1an" keywords = ["video"] categories = ["command-line-utilities"] license = "GPL-3.0" +rust-version = "1.59" edition = "2021" [dependencies] diff --git a/av1an-core/Cargo.toml b/av1an-core/Cargo.toml index 20e9fac..e5f2c59 100644 --- a/av1an-core/Cargo.toml +++ b/av1an-core/Cargo.toml @@ -1,6 +1,7 @@ [package] name = "av1an-core" version = "0.3.1" +rust-version = "1.59" edition = "2021" authors = ["Zen "] description = """ @@ -20,7 +21,6 @@ atty = "0.2.14" av-format = "0.3.1" av-ivf = "0.2.2" memchr = "2.4.1" -num_cpus = "1.13.0" anyhow = "1.0.42" rand = "0.8.4" serde = { version = "1.0", features = ["derive"] } diff --git a/av1an-core/src/broker.rs b/av1an-core/src/broker.rs index 9f16816..8ec8fbd 100644 --- a/av1an-core/src/broker.rs +++ b/av1an-core/src/broker.rs @@ -6,6 +6,7 @@ use std::process::ExitStatus; use std::sync::atomic::{self, AtomicU64}; use std::sync::mpsc::Sender; use std::sync::Arc; +use std::thread::available_parallelism; use cfg_if::cfg_if; use memchr::memmem; @@ -123,7 +124,7 @@ impl<'a> Broker<'a> { cfg_if! { if #[cfg(any(target_os = "linux", target_os = "windows"))] { if let Some(threads) = set_thread_affinity { - let available_threads = num_cpus::get(); + let available_threads = available_parallelism().expect("Unrecoverable: Failed to get thread count").get(); let requested_threads = threads.saturating_mul(self.project.workers); if requested_threads > available_threads { warn!( diff --git a/av1an-core/src/lib.rs b/av1an-core/src/lib.rs index e64bfc9..5816158 100644 --- a/av1an-core/src/lib.rs +++ b/av1an-core/src/lib.rs @@ -29,6 +29,7 @@ use std::io::Write; use std::path::{Path, PathBuf}; use std::string::ToString; use std::sync::atomic::{AtomicBool, AtomicUsize}; +use std::thread::available_parallelism; use std::time::Instant; use ::ffmpeg::color::TransferCharacteristic; @@ -327,7 +328,9 @@ pub fn determine_workers(encoder: Encoder) -> u64 { let mut system = sysinfo::System::new(); system.refresh_memory(); - let cpu = num_cpus::get() as u64; + let cpu = available_parallelism() + .expect("Unrecoverable: Failed to get thread count") + .get() as u64; // available_memory returns kb, convert to gb let ram_gb = system.available_memory() / 10_u64.pow(6); diff --git a/av1an-core/src/settings.rs b/av1an-core/src/settings.rs index f493d75..9843331 100644 --- a/av1an-core/src/settings.rs +++ b/av1an-core/src/settings.rs @@ -9,6 +9,7 @@ use std::path::{Path, PathBuf}; use std::process::{exit, Command, Stdio}; use std::sync::atomic::{self, AtomicBool, AtomicU64, AtomicUsize}; use std::sync::{mpsc, Arc}; +use std::thread::available_parallelism; use std::{cmp, fs, iter, thread}; use ansi_term::{Color, Style}; @@ -1340,7 +1341,11 @@ properly into a mkv file. Specify mkvmerge as the concatenation method by settin Some(filter) => Some(filter), None => None, }, - self.vmaf_threads.unwrap_or_else(num_cpus::get), + self.vmaf_threads.unwrap_or_else(|| { + available_parallelism() + .expect("Unrecoverable: Failed to get thread count") + .get() + }), ) { error!("VMAF calculation failed with error: {}", e); } diff --git a/av1an-core/src/target_quality.rs b/av1an-core/src/target_quality.rs index 7e1aa15..03bce10 100644 --- a/av1an-core/src/target_quality.rs +++ b/av1an-core/src/target_quality.rs @@ -4,6 +4,7 @@ use std::convert::TryInto; use std::fmt::Error; use std::path::{Path, PathBuf}; use std::process::Stdio; +use std::thread::available_parallelism; use ffmpeg::format::Pixel; use splines::{Interpolation, Key, Spline}; @@ -299,7 +300,9 @@ pub fn vmaf_auto_threads(workers: usize) -> usize { const OVER_PROVISION_FACTOR: f64 = 1.25; // Logical CPUs - let threads = num_cpus::get(); + let threads = available_parallelism() + .expect("Unrecoverable: Failed to get thread count") + .get(); cmp::max( ((threads / workers) as f64 * OVER_PROVISION_FACTOR) as usize,