-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cache weight allocated size (#161)
* calculate cache module & script allocated size * calculate cache module & script allocated size * delete unused libraries * delete deps * delete storage testing * rollback shared libraries * refactory PR161 (#163) * add debug assert to sure no case to call get_size recursively * ignore cache insertion error to avoid non-deterministic state * fmt * fix lint --------- Co-authored-by: beer-1 <[email protected]> Co-authored-by: beer-1 <[email protected]>
- Loading branch information
1 parent
02d65ec
commit 3b79790
Showing
9 changed files
with
320 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use std::{ | ||
alloc::{GlobalAlloc, Layout, System}, | ||
cell::Cell, | ||
}; | ||
|
||
use move_binary_format::errors::VMResult; | ||
|
||
thread_local! { | ||
static METERING: Cell<bool> = const { Cell::new(false) }; | ||
static SIZE: Cell<usize> = const { Cell::new(0) }; | ||
} | ||
|
||
struct SizeCounterAllocator; | ||
|
||
unsafe impl GlobalAlloc for SizeCounterAllocator { | ||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 { | ||
if METERING.with(|metering| metering.get()) { | ||
SIZE.with(|size| size.set(size.get() + layout.size())); | ||
} | ||
|
||
System.alloc(layout) | ||
} | ||
|
||
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { | ||
System.dealloc(ptr, layout) | ||
} | ||
} | ||
|
||
#[global_allocator] | ||
static GLOBAL: SizeCounterAllocator = SizeCounterAllocator; | ||
|
||
#[inline] | ||
fn start_metering() { | ||
debug_assert!(!METERING.with(|metering| metering.get())); | ||
SIZE.with(|size| size.set(0)); | ||
METERING.with(|metering| metering.set(true)); | ||
} | ||
|
||
#[inline] | ||
fn finish_metering() -> usize { | ||
debug_assert!(METERING.with(|metering| metering.get())); | ||
METERING.with(|metering| metering.set(false)); | ||
SIZE.with(|size| size.get()) | ||
} | ||
|
||
#[inline] | ||
pub(crate) fn get_size<T, O: FnOnce() -> VMResult<T>>(f: O) -> VMResult<(T, usize)> { | ||
start_metering(); | ||
let ret = f()?; | ||
let size = finish_metering(); | ||
|
||
Ok((ret, size + size_of::<T>())) | ||
} | ||
|
||
#[cfg(test)] | ||
mod allocator_test { | ||
use rand::Rng; | ||
use std::thread; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_get_size() { | ||
let num_thread = 100; | ||
for _ in 0..num_thread { | ||
let handle = thread::spawn(|| { | ||
let num_bytes = rand::thread_rng().gen_range(0..5120); // < 5KB | ||
let (_, size) = get_size(|| { | ||
for _ in 0..num_bytes { | ||
// allocate 1 byte | ||
let _ = vec![0u8; 1]; | ||
} | ||
|
||
Ok(()) | ||
}) | ||
.unwrap(); | ||
|
||
assert_eq!(size, num_bytes); | ||
}); | ||
|
||
handle.join().unwrap(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ pub mod module_cache; | |
pub mod module_storage; | ||
pub mod script_cache; | ||
|
||
mod allocator; | ||
pub mod code_scale; |
Oops, something went wrong.