Skip to content

Commit

Permalink
Bug 1602907 - Add thread priority and affinity code for non-Windows r…
Browse files Browse the repository at this point in the history
…=jrmuizel

pthread priority and affinity for Linux; moved to a helper function

Differential Revision: https://phabricator.services.mozilla.com/D56782

--HG--
extra : moz-landing-system : lando
  • Loading branch information
bpeersmoz committed Dec 12, 2019
1 parent 728c8d3 commit 75bf103
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions gfx/webrender_bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ dwrote = "0.9"
dirs = "1.0"
winapi = "0.3"

[target.'cfg(target_os = "linux")'.dependencies]
libc = "0.2"

[target.'cfg(target_os = "macos")'.dependencies]
core-foundation = "0.6"
core-graphics = "0.17.1"
Expand Down
59 changes: 43 additions & 16 deletions gfx/webrender_bindings/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ use rayon;
use num_cpus;
use euclid::SideOffsets2D;
use nsstring::nsAString;
//linux only//use thread_priority::*;

#[cfg(target_os = "linux")]
use libc::{
pthread_self, pthread_setschedparam, sched_param,
cpu_set_t, CPU_SET, pthread_setaffinity_np
};

#[cfg(target_os = "macos")]
use core_foundation::string::CFString;
Expand Down Expand Up @@ -1066,25 +1071,47 @@ pub unsafe extern "C" fn wr_thread_pool_new(low_priority: bool) -> *mut WrThread

let priority_tag = if low_priority { "LP" } else { "" };

// helper function to make sure that low priority threads really are low priority.
// it also sets the affinity so that WRWorkerX and WRWorkerLPX are both locked to
// the same core X, so one or the other can run, but not both: the total number
// of worker threads that's running should respect num_threads above, even if
// they exist in two separate pools.
#[cfg(target_os = "windows")]
fn set_thread_priority_and_affinity(low_priority:bool, thread_index: usize) {
unsafe {
SetThreadPriority(
GetCurrentThread(),
if low_priority {
-1 /* THREAD_PRIORITY_BELOW_NORMAL */
} else {
0 /* THREAD_PRIORITY_NORMAL */
});
SetThreadAffinityMask(GetCurrentThread(), 1usize << thread_index);
}
}
#[cfg(target_os = "linux")]
fn set_thread_priority_and_affinity(low_priority:bool, thread_index: usize) {
unsafe {
let thread_id = pthread_self();
if low_priority {
let params = sched_param {
sched_priority: 0
};
pthread_setschedparam(thread_id, 3 /* SCHED_BATCH */, &params);
}
let mut cpu_set = mem::zeroed::<cpu_set_t>();
CPU_SET(thread_index, &mut cpu_set);
pthread_setaffinity_np(thread_id, mem::size_of::<cpu_set_t>(), &cpu_set);
}
}
#[cfg(not(any(target_os = "windows", target_os = "linux" )))]
fn set_thread_priority_and_affinity(_low_priority:bool, _thread_index: usize) { }

let worker = rayon::ThreadPoolBuilder::new()
.thread_name(move |idx|{ format!("WRWorker{}#{}", priority_tag, idx) })
.num_threads(num_threads)
.start_handler(move |idx| {
#[cfg(target_os = "windows")]
{
SetThreadPriority(
GetCurrentThread(),
if low_priority {
-1 /* THREAD_PRIORITY_BELOW_NORMAL */
} else {
0 /* THREAD_PRIORITY_NORMAL */
});
SetThreadAffinityMask(GetCurrentThread(), 1usize << idx);
}
/*let thread_id = thread_native_id();
set_thread_priority(thread_id,
if low_priority { ThreadPriority::Min } else { ThreadPriority::Max },
ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Normal));*/
set_thread_priority_and_affinity(low_priority, idx);
wr_register_thread_local_arena();
let name = format!("WRWorker{}#{}",priority_tag, idx);
register_thread_with_profiler(name.clone());
Expand Down
3 changes: 2 additions & 1 deletion gfx/webrender_bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ extern crate log;
extern crate dwrote;
#[cfg(target_os = "windows")]
extern crate winapi;

#[cfg(target_os = "linux")]
extern crate libc;

#[cfg(target_os = "macos")]
extern crate core_foundation;
Expand Down

0 comments on commit 75bf103

Please sign in to comment.