Skip to content

Truly independent web browser

AK+LibJS: Release allocator memory from idle JIT compile threads

Andreas Kling1008da24e188
diff --git a/AK/FFIHelpers.cpp b/AK/FFIHelpers.cpp
index f26f5ac414d..81ec1c34893 100644
--- a/AK/FFIHelpers.cpp
+++ b/AK/FFIHelpers.cpp
@@ -201,6 +201,11 @@ extern "C" void ladybird_dealloc(void* pointer, size_t alignment)
ak_kfree(pointer);
}
+extern "C" void ladybird_release_unused_allocator_memory()
+{
+ ak_kmalloc_collect();
+}
+
extern "C" void* ladybird_realloc(void* pointer, size_t old_size, size_t new_size, size_t alignment)
{
if (alignment <= alignof(max_align_t))
diff --git a/AK/FFIHelpers.h b/AK/FFIHelpers.h
index 5320fc7271d..6bc12557802 100644
--- a/AK/FFIHelpers.h
+++ b/AK/FFIHelpers.h
@@ -27,6 +27,9 @@ void* ladybird_alloc_zeroed(size_t size, size_t alignment);
void* ladybird_realloc(void*, size_t old_size, size_t new_size, size_t alignment);
void ladybird_dealloc(void*, size_t alignment);
+// Gives the memory the calling thread freed back to the system (see ak_kmalloc_collect()).
+void ladybird_release_unused_allocator_memory();
+
// Allocations in HeapPartition::JSObjectStorage, where JS objects keep their property values. ak_kfree frees them.
void* ladybird_js_object_storage_alloc(size_t size);
void* ladybird_js_object_storage_realloc(void*, size_t new_size);
diff --git a/AK/Rust/src/lib.rs b/AK/Rust/src/lib.rs
index 3d4897f50b8..881b7b39bd8 100644
--- a/AK/Rust/src/lib.rs
+++ b/AK/Rust/src/lib.rs
@@ -73,6 +73,7 @@ unsafe extern "C" {
read: *mut usize,
) -> u8;
fn ladybird_encode_base64_to_utf16(data: *const u8, length: usize, url: bool, omit_padding: bool) -> usize;
+ fn ladybird_release_unused_allocator_memory();
fn ladybird_convert_to_decimal_exponential_form(
value: f64,
sign: *mut bool,
@@ -417,6 +418,13 @@ impl Utf16String {
}
}
+/// Gives the memory the calling thread freed back to the system. The allocator otherwise keeps it until the thread
+/// allocates again, so a thread that made large transient allocations calls this once it goes idle.
+pub fn release_unused_allocator_memory() {
+ // SAFETY: Takes no arguments, and any thread may call it.
+ unsafe { ladybird_release_unused_allocator_memory() }
+}
+
pub fn validate_utf16(string: &[u16]) -> bool {
// SAFETY: The slice remains alive for the duration of the call.
unsafe { ladybird_utf16_validate(string.as_ptr(), string.len()) }
diff --git a/Libraries/LibJS/Rust/src/jit/compile_queue.rs b/Libraries/LibJS/Rust/src/jit/compile_queue.rs
index 0518a07ba54..c5378d6e67d 100644
--- a/Libraries/LibJS/Rust/src/jit/compile_queue.rs
+++ b/Libraries/LibJS/Rust/src/jit/compile_queue.rs
@@ -6,8 +6,10 @@
//! The compile threads. Only snapshots go to them and only compile results come back: the compiler never sees the heap.
-use std::sync::mpsc::{Receiver, Sender, channel};
-use std::sync::{Arc, Mutex};
+use std::collections::VecDeque;
+use std::sync::mpsc::{Receiver, channel};
+use std::sync::{Arc, Condvar, Mutex};
+use std::time::Duration;
use libjs_jit::CompileFailure;
use libjs_jit::code::{CompiledCode, Site};
@@ -15,6 +17,9 @@ use libjs_jit::snapshot::Snapshot;
pub type CompileResult = Result<CompiledCode, CompileFailure>;
+/// How long a compile thread waits for another job before it gives the memory its jobs freed back to the system.
+const IDLE_MEMORY_RELEASE_DELAY: Duration = Duration::from_secs(1);
+
/// What the compile threads do: compile a snapshot, or drop what dead code no longer needs.
enum Job {
Compile(u64, Box<Snapshot>),
@@ -23,38 +28,61 @@ enum Job {
Drop(Vec<Site>),
}
+struct Jobs {
+ queue: VecDeque<Job>,
+ /// Set once the queue is dropped, which ends the threads.
+ closed: bool,
+}
+
+struct SharedJobs {
+ jobs: Mutex<Jobs>,
+ job_available: Condvar,
+}
+
/// Threads that compile the snapshots they are sent, each taking the oldest job no other thread took yet. Jobs finish
/// in any order. Dropping the queue lets the threads end once they are done with the jobs they compile; the results of
/// abandoned jobs are dropped.
pub struct CompileQueue {
- jobs: Sender<Job>,
+ jobs: Arc<SharedJobs>,
results: Receiver<(u64, CompileResult)>,
}
impl CompileQueue {
pub fn start(thread_count: usize) -> Self {
- let (jobs, job_receiver) = channel::<Job>();
- let job_receiver = Arc::new(Mutex::new(job_receiver));
+ let jobs = Arc::new(SharedJobs {
+ jobs: Mutex::new(Jobs {
+ queue: VecDeque::new(),
+ closed: false,
+ }),
+ job_available: Condvar::new(),
+ });
let (result_sender, results) = channel();
for index in 0..thread_count.max(1) {
- let job_receiver = Arc::clone(&job_receiver);
+ let jobs = Arc::clone(&jobs);
let result_sender = result_sender.clone();
let spawned = std::thread::Builder::new()
.name("LibJS JIT".to_string())
.spawn(move || {
+ let mut released_memory_since_last_job = true;
loop {
- // NB: The lock is held only while waiting for a job, so the other threads compile meanwhile.
- let job = job_receiver
- .lock()
- .expect("no compile thread panics while waiting")
- .recv();
+ let job = match next_job(&jobs, released_memory_since_last_job) {
+ NextJob::Job(job) => job,
+ NextJob::Idle => {
+ // NB: A compile can allocate a lot for a while, and the allocator keeps the memory a
+ // thread freed until the thread allocates again, which an idle thread may never do.
+ ak::release_unused_allocator_memory();
+ released_memory_since_last_job = true;
+ continue;
+ }
+ NextJob::Closed => return,
+ };
+ released_memory_since_last_job = false;
let (id, snapshot) = match job {
- Ok(Job::Compile(id, snapshot)) => (id, snapshot),
- Ok(Job::Drop(sites)) => {
+ Job::Compile(id, snapshot) => (id, snapshot),
+ Job::Drop(sites) => {
drop(sites);
continue;
}
- Err(_) => return,
};
let result = libjs_jit::compile(&snapshot);
if result_sender.send((id, result)).is_err() {
@@ -73,16 +101,22 @@ impl CompileQueue {
}
pub fn submit(&self, id: u64, snapshot: Box<Snapshot>) {
- self.jobs
- .send(Job::Compile(id, snapshot))
- .expect("the compile threads run while the queue lives");
+ self.enqueue(Job::Compile(id, snapshot));
}
/// Has a compile thread drop the sites of code that died.
pub fn drop_sites(&self, sites: Vec<Site>) {
+ self.enqueue(Job::Drop(sites));
+ }
+
+ fn enqueue(&self, job: Job) {
self.jobs
- .send(Job::Drop(sites))
- .expect("the compile threads run while the queue lives");
+ .jobs
+ .lock()
+ .expect("no compile thread panics while holding the jobs")
+ .queue
+ .push_back(job);
+ self.jobs.job_available.notify_one();
}
/// The results of the jobs that finished since the last call, waiting for one to finish if none has. The queue must
@@ -102,3 +136,61 @@ impl CompileQueue {
self.results.try_iter().collect()
}
}
+
+impl Drop for CompileQueue {
+ fn drop(&mut self) {
+ self.jobs
+ .jobs
+ .lock()
+ .expect("no compile thread panics while holding the jobs")
+ .closed = true;
+ self.jobs.job_available.notify_all();
+ }
+}
+
+enum NextJob {
+ Job(Job),
+ /// No job came within IDLE_MEMORY_RELEASE_DELAY.
+ Idle,
+ /// The queue was dropped.
+ Closed,
+}
+
+/// The next job, waiting for one. A thread that has not released its memory since its last job only waits
+/// IDLE_MEMORY_RELEASE_DELAY.
+fn next_job(jobs: &SharedJobs, released_memory_since_last_job: bool) -> NextJob {
+ let mut guard = jobs
+ .jobs
+ .lock()
+ .expect("no compile thread panics while holding the jobs");
+ loop {
+ if guard.closed {
+ // NB: Sites still waiting to be dropped are dropped here too, by the threads that allocated them. The
+ // snapshots of abandoned compiles go with the queue.
+ while let Some(job) = guard.queue.pop_front() {
+ if let Job::Drop(_) = job {
+ return NextJob::Job(job);
+ }
+ }
+ return NextJob::Closed;
+ }
+ if let Some(job) = guard.queue.pop_front() {
+ return NextJob::Job(job);
+ }
+ if released_memory_since_last_job {
+ guard = jobs
+ .job_available
+ .wait(guard)
+ .expect("no compile thread panics while holding the jobs");
+ continue;
+ }
+ let (next_guard, wait) = jobs
+ .job_available
+ .wait_timeout(guard, IDLE_MEMORY_RELEASE_DELAY)
+ .expect("no compile thread panics while holding the jobs");
+ guard = next_guard;
+ if wait.timed_out() && guard.queue.is_empty() && !guard.closed {
+ return NextJob::Idle;
+ }
+ }
+}