Skip to main content

luaur_vm/functions/
lua_c_allocationrate.rs

1use crate::functions::lua_clock::lua_clock;
2use crate::type_aliases::lua_state::lua_State;
3
4#[no_mangle]
5pub unsafe fn luaC_allocationrate(l: *mut lua_State) -> i64 {
6    let g = (*l).global;
7    let duration_threshold: f64 = 1e-3; // avoid measuring intervals smaller than 1ms
8
9    const GCS_ATOMIC: u8 = 3;
10
11    if (*g).gcstate <= GCS_ATOMIC {
12        let duration = lua_clock() - (*g).gcstats.endtimestamp;
13
14        if duration < duration_threshold {
15            return -1;
16        }
17
18        return (((*g).totalbytes as f64 - (*g).gcstats.endtotalsizebytes as f64) / duration)
19            as i64;
20    }
21
22    // totalbytes is unstable during the sweep, use the rate measured at the end of mark phase
23    let duration = (*g).gcstats.atomicstarttimestamp - (*g).gcstats.endtimestamp;
24
25    if duration < duration_threshold {
26        return -1;
27    }
28
29    return (((*g).gcstats.atomicstarttotalsizebytes as f64 - (*g).gcstats.endtotalsizebytes as f64)
30        / duration) as i64;
31}