|
| 1 | +#![allow(clippy::float_arithmetic)] |
| 2 | + |
| 3 | +use std::sync::atomic::{AtomicUsize, Ordering::Relaxed}; |
| 4 | + |
| 5 | +use crate::Lazy; |
| 6 | + |
| 7 | +/// This function is useful for inducing random jitter into our atomic |
| 8 | +/// operations, shaking out more possible interleavings quickly. It gets |
| 9 | +/// fully eliminated by the compiler in non-test code. |
| 10 | +pub fn debug_delay() { |
| 11 | + use std::thread; |
| 12 | + use std::time::Duration; |
| 13 | + |
| 14 | + static GLOBAL_DELAYS: AtomicUsize = AtomicUsize::new(0); |
| 15 | + |
| 16 | + static INTENSITY: Lazy<u32, fn() -> u32> = Lazy::new(|| { |
| 17 | + std::env::var("SLED_LOCK_FREE_DELAY_INTENSITY") |
| 18 | + .unwrap_or_else(|_| "100".into()) |
| 19 | + .parse() |
| 20 | + .expect( |
| 21 | + "SLED_LOCK_FREE_DELAY_INTENSITY must be set to a \ |
| 22 | + non-negative integer (ideally below 1,000,000)", |
| 23 | + ) |
| 24 | + }); |
| 25 | + |
| 26 | + static CRASH_CHANCE: Lazy<u32, fn() -> u32> = Lazy::new(|| { |
| 27 | + std::env::var("SLED_CRASH_CHANCE") |
| 28 | + .unwrap_or_else(|_| "0".into()) |
| 29 | + .parse() |
| 30 | + .expect( |
| 31 | + "SLED_CRASH_CHANCE must be set to a \ |
| 32 | + non-negative integer (ideally below 50,000)", |
| 33 | + ) |
| 34 | + }); |
| 35 | + |
| 36 | + thread_local!( |
| 37 | + static LOCAL_DELAYS: std::cell::RefCell<usize> = std::cell::RefCell::new(0) |
| 38 | + ); |
| 39 | + |
| 40 | + if cfg!(feature = "miri_optimizations") { |
| 41 | + // Each interaction with LOCAL_DELAYS adds more stacked borrows |
| 42 | + // tracking information, and Miri is single-threaded anyway. |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + let global_delays = GLOBAL_DELAYS.fetch_add(1, Relaxed); |
| 47 | + let local_delays = LOCAL_DELAYS.with(|ld| { |
| 48 | + let mut ld = ld.borrow_mut(); |
| 49 | + let old = *ld; |
| 50 | + *ld = std::cmp::max(global_delays + 1, *ld + 1); |
| 51 | + old |
| 52 | + }); |
| 53 | + |
| 54 | + if *CRASH_CHANCE > 0 && random(*CRASH_CHANCE) == 0 { |
| 55 | + std::process::exit(9) |
| 56 | + } |
| 57 | + |
| 58 | + if global_delays == local_delays { |
| 59 | + // no other threads seem to be |
| 60 | + // calling this, so we may as |
| 61 | + // well skip it |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + if random(1000) == 1 { |
| 66 | + let duration = random(*INTENSITY); |
| 67 | + |
| 68 | + #[allow(clippy::cast_possible_truncation)] |
| 69 | + #[allow(clippy::cast_sign_loss)] |
| 70 | + thread::sleep(Duration::from_micros(u64::from(duration))); |
| 71 | + } |
| 72 | + |
| 73 | + if random(2) == 0 { |
| 74 | + thread::yield_now(); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/// Generates a random number in `0..n`. |
| 79 | +fn random(n: u32) -> u32 { |
| 80 | + use std::cell::Cell; |
| 81 | + use std::num::Wrapping; |
| 82 | + |
| 83 | + thread_local! { |
| 84 | + static RNG: Cell<Wrapping<u32>> = Cell::new(Wrapping(1_406_868_647)); |
| 85 | + } |
| 86 | + |
| 87 | + #[allow(clippy::cast_possible_truncation)] |
| 88 | + RNG.try_with(|rng| { |
| 89 | + // This is the 32-bit variant of Xorshift. |
| 90 | + // |
| 91 | + // Source: https://en.wikipedia.org/wiki/Xorshift |
| 92 | + let mut x = rng.get(); |
| 93 | + x ^= x << 13; |
| 94 | + x ^= x >> 17; |
| 95 | + x ^= x << 5; |
| 96 | + rng.set(x); |
| 97 | + |
| 98 | + // This is a fast alternative to `x % n`. |
| 99 | + // |
| 100 | + // Author: Daniel Lemire |
| 101 | + // Source: https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/ |
| 102 | + (u64::from(x.0).wrapping_mul(u64::from(n)) >> 32) as u32 |
| 103 | + }) |
| 104 | + .unwrap_or(0) |
| 105 | +} |
0 commit comments