style: Make stylo thread pool size configurable via pref rather than just env

Remove a variety of code which we don't use to change the pool size. We
can just use the pref as needed in the future.

Differential Revision: https://phabricator.services.mozilla.com/D178575
This commit is contained in:
Emilio Cobos Álvarez 2023-05-23 09:41:14 +00:00 committed by Martin Robinson
parent 3a51e530d2
commit 10f8eb4239

View file

@ -14,9 +14,7 @@ use crate::thread_state;
use gecko_profiler;
use parking_lot::{Mutex, RwLock, RwLockReadGuard};
use rayon;
use std::env;
use std::io;
use std::thread;
use std::{io, thread};
/// Global style data
pub struct GlobalStyleData {
@ -114,50 +112,40 @@ impl StyleThreadPool {
}
}
#[cfg(feature = "servo")]
fn stylo_threads_pref() -> i32 {
pref!(layout.threads)
}
#[cfg(feature = "gecko")]
fn stylo_threads_pref() -> i32 {
static_prefs::pref!("layout.css.stylo-threads")
}
lazy_static! {
/// Global thread pool
pub static ref STYLE_THREAD_POOL: std::sync::Mutex<StyleThreadPool> = {
let stylo_threads = env::var("STYLO_THREADS")
.map(|s| s.parse::<usize>().expect("invalid STYLO_THREADS value"));
let mut num_threads = match stylo_threads {
Ok(num) => num,
#[cfg(feature = "servo")]
_ => {
use servo_config::pref;
// We always set this pref on startup, before layout or script
// have had a chance of accessing (and thus creating) the
// thread-pool.
pref!(layout.threads) as usize
}
#[cfg(feature = "gecko")]
_ => {
// The default heuristic is num_virtual_cores * .75. This gives
// us three threads on a hyper-threaded dual core, and six
// threads on a hyper-threaded quad core. The performance
// benefit of additional threads seems to level off at around
// six, so we cap it there on many-core machines
// (see bug 1431285 comment 14).
use num_cpus;
use std::cmp;
cmp::min(cmp::max(num_cpus::get() * 3 / 4, 1), 6)
}
// We always set this pref on startup, before layout or script have had a chance of
// accessing (and thus creating) the thread-pool.
let threads_pref: i32 = stylo_threads_pref();
let num_threads = if threads_pref >= 0 {
threads_pref as usize
} else {
use num_cpus;
use std::cmp;
// The default heuristic is num_virtual_cores * .75. This gives us three threads on a
// hyper-threaded dual core, and six threads on a hyper-threaded quad core.
//
// The performance benefit of additional threads seems to level off at around six, so
// we cap it there on many-core machines (see bug 1431285 comment 14).
let threads = cmp::min(cmp::max(num_cpus::get() * 3 / 4, 1), 6);
// There's no point in creating a thread pool if there's one thread.
if threads == 1 { 0 } else { threads }
};
// If num_threads is one, there's no point in creating a thread pool, so
// force it to zero.
//
// We allow developers to force a one-thread pool for testing via a
// special environmental variable.
if num_threads == 1 {
let force_pool = env::var("FORCE_STYLO_THREAD_POOL")
.ok().map_or(false, |s| s.parse::<usize>().expect("invalid FORCE_STYLO_THREAD_POOL value") == 1);
if !force_pool {
num_threads = 0;
}
}
let pool = if num_threads < 1 {
None
let (pool, num_threads) = if num_threads < 1 {
(None, None)
} else {
let workers = rayon::ThreadPoolBuilder::new()
.spawn_handler(thread_spawn)
@ -167,15 +155,11 @@ lazy_static! {
.exit_handler(thread_shutdown)
.stack_size(STYLE_THREAD_STACK_SIZE_KB * 1024)
.build();
workers.ok()
(workers.ok(), Some(num_threads))
};
std::sync::Mutex::new(StyleThreadPool {
num_threads: if num_threads > 0 {
Some(num_threads)
} else {
None
},
num_threads,
style_thread_pool: RwLock::new(pool),
})
};