| 1 |
use std::{ |
| 2 |
num::NonZeroUsize, |
| 3 |
panic::AssertUnwindSafe, |
| 4 |
ptr::NonNull, |
| 5 |
sync::{ |
| 6 |
atomic::{AtomicUsize, Ordering}, |
| 7 |
mpsc, Mutex, PoisonError, |
| 8 |
}, |
| 9 |
thread::Thread, |
| 10 |
}; |
| 11 |
|
| 12 |
use crate::util::{defer, sync::SyncWrap}; |
| 13 |
|
| 14 |
/// Single shared thread pool for running benchmarks on. |
| 15 |
pub(crate) static BENCH_POOL: ThreadPool = ThreadPool::new(); |
| 16 |
|
| 17 |
/// Reusable threads for broadcasting tasks. |
| 18 |
/// |
| 19 |
/// This thread pool runs only a single task at a time, since only one benchmark |
| 20 |
/// should run at a time. Invoking `broadcast` from two threads will cause one |
| 21 |
/// thread to wait for the other to finish. |
| 22 |
/// |
| 23 |
/// # How It Works |
| 24 |
/// |
| 25 |
/// Upon calling `broadcast`: |
| 26 |
/// |
| 27 |
/// 1. The main thread creates a `Task`, which is a pointer to a `TaskShared` |
| 28 |
/// pinned on the stack. `TaskShared` stores the function to run, along with |
| 29 |
/// other fields for coordinating threads. |
| 30 |
/// |
| 31 |
/// 2. New threads are spawned if the requested amount is not available. Each |
| 32 |
/// receives tasks over an associated channel. |
| 33 |
/// |
| 34 |
/// 3. The main thread sends the `Task` over the channels to the requested |
| 35 |
/// amount of threads. Upon receiving the task, each auxiliary thread will |
| 36 |
/// execute it and then decrement the task's reference count. |
| 37 |
/// |
| 38 |
/// 4. The main thread executes the `Task` like auxiliary threads. It then waits |
| 39 |
/// until the reference count is 0 before returning. |
| 40 |
pub(crate) struct ThreadPool { |
| 41 |
threads: Mutex<Vec<mpsc::SyncSender<Task>>>, |
| 42 |
} |
| 43 |
|
| 44 |
impl ThreadPool { |
| 45 |
const fn new() -> Self { |
| 46 |
Self { threads: Mutex::new(Vec::new()) } |
| 47 |
} |
| 48 |
|
| 49 |
/// Performs the given task and pushes the results into a `vec`. |
| 50 |
#[inline] |
| 51 |
pub fn par_extend<T, F>(&self, vec: &mut Vec<Option<T>>, aux_threads: usize, task: F) |
| 52 |
where |
| 53 |
F: Sync + Fn(usize) -> T, |
| 54 |
T: Sync + Send, |
| 55 |
{ |
| 56 |
unsafe { |
| 57 |
let old_len = vec.len(); |
| 58 |
let additional = aux_threads + 1; |
| 59 |
|
| 60 |
vec.reserve_exact(additional); |
| 61 |
vec.spare_capacity_mut().iter_mut().for_each(|val| { |
| 62 |
val.write(None); |
| 63 |
}); |
| 64 |
vec.set_len(old_len + additional); |
| 65 |
|
| 66 |
let ptr = SyncWrap::new(vec.as_mut_ptr().add(old_len)); |
| 67 |
|
| 68 |
self.broadcast(aux_threads, move |index| { |
| 69 |
ptr.add(index).write(Some(task(index))); |
| 70 |
}); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/// Performs the given task across the current thread and auxiliary worker |
| 75 |
/// threads. |
| 76 |
/// |
| 77 |
/// This function returns once all threads complete the task. |
| 78 |
#[inline] |
| 79 |
pub fn broadcast<F>(&self, aux_threads: usize, task: F) |
| 80 |
where |
| 81 |
F: Sync + Fn(usize), |
| 82 |
{ |
| 83 |
// SAFETY: The `TaskShared` instance is guaranteed to be accessible to |
| 84 |
// all threads until this function returns, because this thread waits |
| 85 |
// until `TaskShared.ref_count` is 0 before continuing. |
| 86 |
unsafe { |
| 87 |
let task = TaskShared::new(aux_threads, task); |
| 88 |
let task = Task { shared: NonNull::from(&task).cast() }; |
| 89 |
|
| 90 |
self.broadcast_task(aux_threads, task); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/// Type-erased monomorphized implementation for `broadcast`. |
| 95 |
unsafe fn broadcast_task(&self, aux_threads: usize, task: Task) { |
| 96 |
// Send task to auxiliary threads. |
| 97 |
if aux_threads > 0 { |
| 98 |
let threads = &mut *self.threads.lock().unwrap_or_else(PoisonError::into_inner); |
| 99 |
|
| 100 |
// Spawn more threads if necessary. |
| 101 |
if let Some(additional) = NonZeroUsize::new(aux_threads.saturating_sub(threads.len())) { |
| 102 |
spawn(additional, threads); |
| 103 |
} |
| 104 |
|
| 105 |
for thread in &threads[..aux_threads] { |
| 106 |
thread.send(task).unwrap(); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
// Run the task on the main thread. |
| 111 |
let main_result = std::panic::catch_unwind(AssertUnwindSafe(|| task.run(0))); |
| 112 |
|
| 113 |
// Wait for other threads to finish writing their results. |
| 114 |
// |
| 115 |
// SAFETY: The acquire memory ordering ensures that all writes performed |
| 116 |
// by the task on other threads will become visible to this thread after |
| 117 |
// returning from `broadcast`. |
| 118 |
while task.shared.as_ref().ref_count.load(Ordering::Acquire) > 0 { |
| 119 |
std::thread::park(); |
| 120 |
} |
| 121 |
|
| 122 |
// Don't drop our result until other threads finish, in case the panic |
| 123 |
// error's drop handler itself also panics. |
| 124 |
drop(main_result); |
| 125 |
} |
| 126 |
|
| 127 |
pub fn drop_threads(&self) { |
| 128 |
*self.threads.lock().unwrap_or_else(PoisonError::into_inner) = Default::default(); |
| 129 |
} |
| 130 |
|
| 131 |
#[cfg(test)] |
| 132 |
fn aux_thread_count(&self) -> usize { |
| 133 |
self.threads.lock().unwrap_or_else(PoisonError::into_inner).len() |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/// Type-erased function and metadata. |
| 138 |
#[derive(Clone, Copy)] |
| 139 |
struct Task { |
| 140 |
shared: NonNull<TaskShared<()>>, |
| 141 |
} |
| 142 |
|
| 143 |
unsafe impl Send for Task {} |
| 144 |
unsafe impl Sync for Task {} |
| 145 |
|
| 146 |
impl Task { |
| 147 |
/// Runs this task on behalf of `thread_id`. |
| 148 |
/// |
| 149 |
/// # Safety |
| 150 |
/// |
| 151 |
/// The caller must ensure: |
| 152 |
/// |
| 153 |
/// - This task has not outlived the `TaskShared` it came from, or else |
| 154 |
/// there will be a use-after-free. |
| 155 |
/// |
| 156 |
/// - `thread_id` is within the number of `broadcast` threads requested, so |
| 157 |
/// that it can be used to index input or output buffers. |
| 158 |
#[inline] |
| 159 |
unsafe fn run(&self, thread_id: usize) { |
| 160 |
let shared_ptr = self.shared.as_ptr(); |
| 161 |
let shared = &*shared_ptr; |
| 162 |
|
| 163 |
(shared.task_fn_ptr)(shared_ptr.cast(), thread_id); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
/// Data stored on the main thread that gets shared with auxiliary threads. |
| 168 |
/// |
| 169 |
/// # Memory Layout |
| 170 |
/// |
| 171 |
/// Since the benchmark may have thrashed the cache, this type's fields are |
| 172 |
/// ordered by usage order. This type is also placed on its own cache line. |
| 173 |
#[repr(C)] |
| 174 |
struct TaskShared<F> { |
| 175 |
/// Once an auxiliary thread sets `ref_count` to 0, it should notify the |
| 176 |
/// main thread to wake up. |
| 177 |
main_thread: Thread, |
| 178 |
|
| 179 |
/// The number of auxiliary threads executing the task. |
| 180 |
/// |
| 181 |
/// Once this is 0, the main thread can read any results the task produced. |
| 182 |
ref_count: AtomicUsize, |
| 183 |
|
| 184 |
/// Performs `*result = Some(task_fn(thread))`. |
| 185 |
task_fn_ptr: unsafe fn(task: *const TaskShared<()>, thread: usize), |
| 186 |
|
| 187 |
/// Stores the closure state of the provided task. |
| 188 |
/// |
| 189 |
/// This must be stored as the last field so that all other fields are in |
| 190 |
/// the same place regardless of this field's type. |
| 191 |
task_fn: F, |
| 192 |
} |
| 193 |
|
| 194 |
impl<F> TaskShared<F> { |
| 195 |
#[inline] |
| 196 |
fn new(aux_threads: usize, task_fn: F) -> Self |
| 197 |
where |
| 198 |
F: Sync + Fn(usize), |
| 199 |
{ |
| 200 |
unsafe fn call<F>(task: *const TaskShared<()>, thread: usize) |
| 201 |
where |
| 202 |
F: Fn(usize), |
| 203 |
{ |
| 204 |
#[inline(never)] |
| 205 |
unsafe fn __codspeed_root_frame__<F>(task: *const TaskShared<()>, thread: usize) |
| 206 |
where |
| 207 |
F: Fn(usize), |
| 208 |
{ |
| 209 |
let task_fn = &(*task.cast::<TaskShared<F>>()).task_fn; |
| 210 |
|
| 211 |
task_fn(thread); |
| 212 |
} |
| 213 |
|
| 214 |
__codspeed_root_frame__::<F>(task, thread); |
| 215 |
} |
| 216 |
|
| 217 |
Self { |
| 218 |
main_thread: std::thread::current(), |
| 219 |
ref_count: AtomicUsize::new(aux_threads), |
| 220 |
task_fn_ptr: call::<F>, |
| 221 |
task_fn, |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
/// Spawns N additional threads and appends their channels to the list. |
| 227 |
/// |
| 228 |
/// Threads are given names in the form of `divan-$INDEX`. |
| 229 |
#[cold] |
| 230 |
fn spawn(additional: NonZeroUsize, threads: &mut Vec<mpsc::SyncSender<Task>>) { |
| 231 |
let next_thread_id = threads.len() + 1; |
| 232 |
|
| 233 |
threads.extend((next_thread_id..(next_thread_id + additional.get())).map(|thread_id| { |
| 234 |
// Create single-task channel. Unless another benchmark is running, the |
| 235 |
// current thread will be immediately unblocked after the auxiliary |
| 236 |
// thread accepts the task. |
| 237 |
// |
| 238 |
// This uses a rendezvous channel (capacity 0) instead of other standard |
| 239 |
// library channels because it reduces memory usage by many kilobytes. |
| 240 |
let (sender, receiver) = mpsc::sync_channel::<Task>(0); |
| 241 |
|
| 242 |
let work = move || { |
| 243 |
// Abort the process if the caught panic error itself panics when |
| 244 |
// dropped. |
| 245 |
let panic_guard = defer(|| std::process::abort()); |
| 246 |
|
| 247 |
while let Ok(task) = receiver.recv() { |
| 248 |
// Run the task on this auxiliary thread. |
| 249 |
// |
| 250 |
// SAFETY: The task is valid until `ref_count == 0`. |
| 251 |
let result = |
| 252 |
std::panic::catch_unwind(AssertUnwindSafe(|| unsafe { task.run(thread_id) })); |
| 253 |
|
| 254 |
// Decrement the `ref_count` count to notify the main thread |
| 255 |
// that we finished our work. |
| 256 |
// |
| 257 |
// SAFETY: This release operation makes writes within the task |
| 258 |
// become visible to the main thread. |
| 259 |
unsafe { |
| 260 |
// Clone the main thread's handle for unparking because the |
| 261 |
// `TaskShared` will be invalidated when `ref_count` is 0. |
| 262 |
let main_thread = task.shared.as_ref().main_thread.clone(); |
| 263 |
|
| 264 |
if task.shared.as_ref().ref_count.fetch_sub(1, Ordering::Release) == 1 { |
| 265 |
main_thread.unpark(); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
// Don't drop our result until after notifying the main thread, |
| 270 |
// in case the panic error's drop handler itself also panics. |
| 271 |
drop(result); |
| 272 |
} |
| 273 |
|
| 274 |
std::mem::forget(panic_guard); |
| 275 |
}; |
| 276 |
|
| 277 |
std::thread::Builder::new() |
| 278 |
.name(format!("divan-{thread_id}")) |
| 279 |
.spawn(work) |
| 280 |
.expect("failed to spawn thread"); |
| 281 |
|
| 282 |
sender |
| 283 |
})); |
| 284 |
} |
| 285 |
|
| 286 |
#[cfg(test)] |
| 287 |
mod tests { |
| 288 |
use super::*; |
| 289 |
|
| 290 |
/// Make every thread write its ID to a buffer and then check that the |
| 291 |
/// buffer contains all IDs. |
| 292 |
#[test] |
| 293 |
fn extend() { |
| 294 |
static TEST_POOL: ThreadPool = ThreadPool::new(); |
| 295 |
|
| 296 |
fn test(aux_threads: usize, final_aux_threads: usize) { |
| 297 |
let total_threads = aux_threads + 1; |
| 298 |
|
| 299 |
let mut results = Vec::new(); |
| 300 |
let expected = (0..total_threads).map(Some).collect::<Vec<_>>(); |
| 301 |
|
| 302 |
TEST_POOL.par_extend(&mut results, aux_threads, |index| index); |
| 303 |
|
| 304 |
assert_eq!(results, expected); |
| 305 |
assert_eq!(TEST_POOL.aux_thread_count(), final_aux_threads); |
| 306 |
} |
| 307 |
|
| 308 |
test(0, 0); |
| 309 |
test(1, 1); |
| 310 |
test(2, 2); |
| 311 |
test(3, 3); |
| 312 |
test(4, 4); |
| 313 |
test(8, 8); |
| 314 |
|
| 315 |
// Decreasing auxiliary threads on later calls should still leave |
| 316 |
// previously spawned threads running. |
| 317 |
test(4, 8); |
| 318 |
test(0, 8); |
| 319 |
|
| 320 |
// Silence Miri about leaking threads. |
| 321 |
TEST_POOL.drop_threads(); |
| 322 |
} |
| 323 |
|
| 324 |
/// Execute a task that takes longer on all other threads than the main |
| 325 |
/// thread. |
| 326 |
#[test] |
| 327 |
fn broadcast_sleep() { |
| 328 |
use std::time::Duration; |
| 329 |
|
| 330 |
static TEST_POOL: ThreadPool = ThreadPool::new(); |
| 331 |
|
| 332 |
TEST_POOL.broadcast(10, |thread_id| { |
| 333 |
if thread_id > 0 { |
| 334 |
std::thread::sleep(Duration::from_millis(10)); |
| 335 |
} |
| 336 |
}); |
| 337 |
|
| 338 |
// Silence Miri about leaking threads. |
| 339 |
TEST_POOL.drop_threads(); |
| 340 |
} |
| 341 |
|
| 342 |
/// Checks that thread ID 0 refers to the main thread. |
| 343 |
#[test] |
| 344 |
fn broadcast_thread_id() { |
| 345 |
static TEST_POOL: ThreadPool = ThreadPool::new(); |
| 346 |
|
| 347 |
let main_thread = std::thread::current().id(); |
| 348 |
|
| 349 |
TEST_POOL.broadcast(10, |thread_id| { |
| 350 |
let is_main = main_thread == std::thread::current().id(); |
| 351 |
assert_eq!(is_main, thread_id == 0); |
| 352 |
}); |
| 353 |
|
| 354 |
// Silence Miri about leaking threads. |
| 355 |
TEST_POOL.drop_threads(); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
#[cfg(feature = "internal_benches")] |
| 360 |
mod benches { |
| 361 |
use super::*; |
| 362 |
|
| 363 |
fn aux_thread_counts() -> impl Iterator<Item = usize> { |
| 364 |
let mut available_parallelism = std::thread::available_parallelism().ok().map(|n| n.get()); |
| 365 |
|
| 366 |
let range = 0..=16; |
| 367 |
|
| 368 |
if let Some(n) = available_parallelism { |
| 369 |
if range.contains(&n) { |
| 370 |
available_parallelism = None; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
range.chain(available_parallelism) |
| 375 |
} |
| 376 |
|
| 377 |
/// Benchmarks repeatedly using `ThreadPool` for the same number of threads |
| 378 |
/// on every run. |
| 379 |
#[crate::bench(crate = crate, args = aux_thread_counts())] |
| 380 |
fn broadcast(bencher: crate::Bencher, aux_threads: usize) { |
| 381 |
let pool = ThreadPool::new(); |
| 382 |
let benched = move || pool.broadcast(aux_threads, crate::black_box_drop); |
| 383 |
|
| 384 |
// Warmup to spawn threads. |
| 385 |
benched(); |
| 386 |
|
| 387 |
bencher.bench(benched); |
| 388 |
} |
| 389 |
|
| 390 |
/// Benchmarks using `ThreadPool` once. |
| 391 |
#[crate::bench(crate = crate, args = aux_thread_counts(), sample_size = 1)] |
| 392 |
fn broadcast_once(bencher: crate::Bencher, aux_threads: usize) { |
| 393 |
bencher |
| 394 |
.with_inputs(ThreadPool::new) |
| 395 |
.bench_refs(|pool| pool.broadcast(aux_threads, crate::black_box_drop)); |
| 396 |
} |
| 397 |
} |