Skip to content

Commit 8b684ab

Browse files
authored
Turn the GC_ZEAL_ALLOC_COUNTER env var into a Tunable field (#13050)
Replace the `WASMTIME_GC_ZEAL_ALLOC_COUNTER` environment variable with a `gc_zeal_alloc_counter` field on `Tunables` and a corresponding `Config::gc_zeal_alloc_counter` method.
1 parent 2d443ea commit 8b684ab

5 files changed

Lines changed: 50 additions & 24 deletions

File tree

crates/environ/src/tunables.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::prelude::*;
22
use crate::{IndexType, Limits, Memory, TripleExt};
3+
use core::num::NonZeroU32;
34
use core::{fmt, str::FromStr};
45
use serde_derive::{Deserialize, Serialize};
56
use target_lexicon::{PointerWidth, Triple};
@@ -152,6 +153,13 @@ define_tunables! {
152153
/// Whether recording in RR is enabled or not. This is used primarily
153154
/// to signal checksum computation for compiled artifacts.
154155
pub recording: bool,
156+
157+
/// An allocation counter that triggers GC when it reaches zero.
158+
///
159+
/// Decremented on every allocation and when it hits zero, a GC is
160+
/// forced and the counter is reset. Only effective when
161+
/// `cfg(gc_zeal)` is enabled.
162+
pub gc_zeal_alloc_counter: Option<NonZeroU32>,
155163
}
156164

157165
pub struct ConfigTunables {
@@ -230,6 +238,7 @@ impl Tunables {
230238
debug_guest: false,
231239
concurrency_support: true,
232240
recording: false,
241+
gc_zeal_alloc_counter: None,
233242
}
234243
}
235244

crates/wasmtime/src/config.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::prelude::*;
22
use alloc::sync::Arc;
33
use bitflags::Flags;
44
use core::fmt;
5-
use core::num::NonZeroUsize;
5+
use core::num::{NonZeroU32, NonZeroUsize};
66
use core::str::FromStr;
77
#[cfg(any(feature = "cranelift", feature = "winch"))]
88
use std::path::Path;
@@ -728,6 +728,24 @@ impl Config {
728728
self
729729
}
730730

731+
/// XXX: For internal fuzzing and debugging use only!
732+
#[doc(hidden)]
733+
pub fn gc_zeal_alloc_counter(&mut self, counter: Option<NonZeroU32>) -> Result<&mut Self> {
734+
#[cfg(not(gc_zeal))]
735+
{
736+
let _ = counter;
737+
bail!(
738+
"cannot set `gc_zeal_alloc_counter` because Wasmtime was not built with `cfg(gc_zeal)`"
739+
);
740+
}
741+
742+
#[cfg(gc_zeal)]
743+
{
744+
self.tunables.gc_zeal_alloc_counter = Some(counter);
745+
Ok(self)
746+
}
747+
}
748+
731749
/// Configures the maximum amount of stack space available for
732750
/// executing WebAssembly code.
733751
///

crates/wasmtime/src/engine/serialization.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,9 @@ impl Metadata<'_> {
327327

328328
// Just a debugging aid, doesn't affect functionality at all.
329329
debug_adapter_modules: _,
330+
331+
// This is a runtime GC debugging setting, doesn't affect compilation.
332+
gc_zeal_alloc_counter: _,
330333
} = self.tunables;
331334

332335
Self::check_collector(collector, other.collector)?;

crates/wasmtime/src/runtime/store.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1978,7 +1978,11 @@ impl StoreOpaque {
19781978
.allocator()
19791979
.allocate_gc_heap(engine, &**gc_runtime, mem_alloc_index, mem)?;
19801980

1981-
Ok(GcStore::new(index, heap))
1981+
Ok(GcStore::new(
1982+
index,
1983+
heap,
1984+
engine.tunables().gc_zeal_alloc_counter,
1985+
))
19821986
}
19831987

19841988
#[cfg(not(feature = "gc"))]

crates/wasmtime/src/runtime/vm/gc.rs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,46 +56,38 @@ pub struct GcStore {
5656

5757
/// An allocation counter that triggers GC when it reaches zero.
5858
///
59-
/// Initialized from the `WASMTIME_GC_ZEAL_ALLOC_COUNTER` environment
60-
/// variable. Decremented on every allocation and when it hits zero, a GC is
59+
/// Decremented on every allocation and when it hits zero, a GC is
6160
/// forced and the counter is reset.
62-
#[cfg(all(gc_zeal, feature = "std"))]
61+
#[cfg(gc_zeal)]
6362
gc_zeal_alloc_counter: Option<NonZeroU32>,
6463

6564
/// The initial value to reset the counter to after it triggers.
66-
#[cfg(all(gc_zeal, feature = "std"))]
65+
#[cfg(gc_zeal)]
6766
gc_zeal_alloc_counter_init: Option<NonZeroU32>,
6867
}
6968

7069
impl GcStore {
7170
/// Create a new `GcStore`.
72-
pub fn new(allocation_index: GcHeapAllocationIndex, gc_heap: Box<dyn GcHeap>) -> Self {
71+
pub fn new(
72+
allocation_index: GcHeapAllocationIndex,
73+
gc_heap: Box<dyn GcHeap>,
74+
gc_zeal_alloc_counter: Option<NonZeroU32>,
75+
) -> Self {
7376
let host_data_table = ExternRefHostDataTable::default();
7477
let func_ref_table = FuncRefTable::default();
7578

76-
#[cfg(all(gc_zeal, feature = "std"))]
77-
let gc_zeal_alloc_counter_init =
78-
std::env::var("WASMTIME_GC_ZEAL_ALLOC_COUNTER")
79-
.ok()
80-
.map(|v| {
81-
v.parse::<NonZeroU32>().unwrap_or_else(|_| {
82-
panic!(
83-
"`WASMTIME_GC_ZEAL_ALLOC_COUNTER` must be a non-zero \
84-
`u32` value, got: {v}"
85-
)
86-
})
87-
});
79+
let _ = &gc_zeal_alloc_counter;
8880

8981
Self {
9082
allocation_index,
9183
gc_heap,
9284
host_data_table,
9385
func_ref_table,
9486
last_post_gc_allocated_bytes: None,
95-
#[cfg(all(gc_zeal, feature = "std"))]
96-
gc_zeal_alloc_counter: gc_zeal_alloc_counter_init,
97-
#[cfg(all(gc_zeal, feature = "std"))]
98-
gc_zeal_alloc_counter_init,
87+
#[cfg(gc_zeal)]
88+
gc_zeal_alloc_counter,
89+
#[cfg(gc_zeal)]
90+
gc_zeal_alloc_counter_init: gc_zeal_alloc_counter,
9991
}
10092
}
10193

@@ -279,7 +271,7 @@ impl GcStore {
279271
) -> Result<Result<VMGcRef, u64>> {
280272
// When gc_zeal is enabled with an allocation counter, decrement it and
281273
// force a GC cycle when it reaches zero by returning a fake OOM.
282-
#[cfg(all(gc_zeal, feature = "std"))]
274+
#[cfg(gc_zeal)]
283275
if let Some(counter) = self.gc_zeal_alloc_counter.take() {
284276
match NonZeroU32::new(counter.get() - 1) {
285277
Some(c) => self.gc_zeal_alloc_counter = Some(c),

0 commit comments

Comments
 (0)