Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 46 additions & 13 deletions src/worker/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
io::prelude::*,
net::{Shutdown, TcpStream},
process::Command,
sync::Arc,
thread, time,
};

Expand All @@ -22,6 +23,7 @@ use llvm::execution_engine::*;
use llvm::target::*;
use llvm_sys::LLVMType;
use llvm_sys::prelude::*;
use std::cell::RefCell;
use std::ffi::{CStr, CString, c_void};
use std::mem;

Expand Down Expand Up @@ -129,6 +131,11 @@ pub unsafe extern "C" fn task(name: *const i8, random: bool) -> u64 {
0
}

thread_local! {
static LAST_RANDOM_PATH: RefCell<CString> =
RefCell::new(CString::new("").unwrap());
}

/// Return a randomly generated path.
///
/// # Safety
Expand All @@ -144,7 +151,11 @@ pub unsafe extern "C" fn random_path(base: *const i8) -> *const i8 {
.map(char::from)
.collect();

CString::new(format!("{base}/{uniq}")).unwrap().into_raw()
LAST_RANDOM_PATH.with(|last| {
let mut last = last.borrow_mut();
*last = CString::new(format!("{base}/{uniq}")).unwrap();
last.as_ptr()
})
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -506,21 +517,43 @@ impl Worker for ScriptWorker {
debug!("Distribution {:?}", d);
let Dist::Exp { rate } = d else { todo!() };

loop {
let worker = self.clone();
thread::spawn(move || {
(worker.jit)();
});

let interval: f64 =
thread_rng().sample(Exp::new(*rate).unwrap());
debug!("Interval {}", interval);
thread::sleep(time::Duration::from_secs_f64(interval));
}
const MAX_CONCURRENT: usize = 16;
let semaphore = Arc::new((
std::sync::Mutex::new(0usize),
std::sync::Condvar::new(),
));
Comment on lines +521 to +524

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[ultra-nit] We might want to put this in a new type and add something like .inc and .dec methods to remove some verbosity in the loop. But honestly, this way is fine as well.


thread::scope(|s| {
loop {
{
let (lock, cvar) = &*semaphore;
let mut count = cvar
.wait_while(lock.lock().unwrap(), |c| {
*c >= MAX_CONCURRENT
})
.unwrap();
*count += 1;
}

let worker = self.clone();
let sem = Arc::clone(&semaphore);
s.spawn(move || {
(worker.jit)();
let (lock, cvar) = &*sem;
*lock.lock().unwrap() -= 1;
cvar.notify_one();
});

let interval: f64 =
thread_rng().sample(Exp::new(*rate).unwrap());
debug!("Interval {}", interval);
thread::sleep(time::Duration::from_secs_f64(interval));
}
});
}
None => {
debug!("Single unit");
(self.jit)()
(self.jit)();
}
};

Expand Down
Loading