mirror of
https://github.com/fluencelabs/marine.git
synced 2025-06-26 21:21:36 +00:00
Improve arrays and record passing scheme (#76)
This commit is contained in:
@ -11,4 +11,6 @@ path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
fluence = { git = "https://github.com/fluencelabs/rust-sdk", features = ["logger"] }
|
||||
wasm-tracing-allocator = "0.1.0"
|
||||
|
||||
log = "0.4.8"
|
||||
|
@ -33,7 +33,17 @@ pub fn main() {
|
||||
/// You can read or write files from the file system if there is permission to use directories described in `Config.toml`.
|
||||
#[fce]
|
||||
pub fn put(name: String, file_content: Vec<u8>) -> String {
|
||||
log::info!("put called with file name {}", name);
|
||||
log::info!("put called with file name {}\n", name);
|
||||
unsafe {
|
||||
log::info!(
|
||||
"put track: {} {} {} {} {}",
|
||||
*ALLOCS.get_mut(),
|
||||
*DEALLOCS.get_mut(),
|
||||
*REALLOCSA.get_mut(),
|
||||
*REALLOCSD.get_mut(),
|
||||
*ALLOCS.get_mut() - *DEALLOCS.get_mut() - *REALLOCSD.get_mut() + *REALLOCSA.get_mut()
|
||||
);
|
||||
}
|
||||
|
||||
let rpc_tmp_filepath = format!("{}{}", SITES_DIR, name);
|
||||
|
||||
@ -42,14 +52,92 @@ pub fn put(name: String, file_content: Vec<u8>) -> String {
|
||||
return format!("file can't be written: {}", e);
|
||||
}
|
||||
|
||||
log::info!("put ended with OK\n");
|
||||
|
||||
String::from("Ok")
|
||||
}
|
||||
|
||||
#[fce]
|
||||
pub fn get(file_name: String) -> Vec<u8> {
|
||||
log::info!("get called with file name: {}", file_name);
|
||||
log::info!("get called with file name: {}\n", file_name);
|
||||
unsafe {
|
||||
log::info!(
|
||||
"get track: {} {} {} {} {}",
|
||||
*ALLOCS.get_mut(),
|
||||
*DEALLOCS.get_mut(),
|
||||
*REALLOCSA.get_mut(),
|
||||
*REALLOCSD.get_mut(),
|
||||
*ALLOCS.get_mut() - *DEALLOCS.get_mut() - *REALLOCSD.get_mut() + *REALLOCSA.get_mut()
|
||||
);
|
||||
}
|
||||
|
||||
let tmp_filepath = format!("{}{}", SITES_DIR, file_name);
|
||||
|
||||
fs::read(tmp_filepath).unwrap_or_else(|_| b"error while reading file".to_vec())
|
||||
let res = fs::read(tmp_filepath).unwrap_or_else(|_| b"error while reading file".to_vec());
|
||||
|
||||
log::info!("get ended with file name: {}\n", file_name);
|
||||
|
||||
res
|
||||
}
|
||||
|
||||
use std::alloc::{GlobalAlloc, System, Layout};
|
||||
|
||||
#[global_allocator]
|
||||
static GLOBAL_ALLOCATOR: WasmTracingAllocator<System> = WasmTracingAllocator(System);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct WasmTracingAllocator<A>(pub A)
|
||||
where
|
||||
A: GlobalAlloc;
|
||||
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
|
||||
static mut ALLOCS: AtomicUsize = AtomicUsize::new(0);
|
||||
static mut DEALLOCS: AtomicUsize = AtomicUsize::new(0);
|
||||
static mut REALLOCSA: AtomicUsize = AtomicUsize::new(0);
|
||||
static mut REALLOCSD: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
unsafe impl<A> GlobalAlloc for WasmTracingAllocator<A>
|
||||
where
|
||||
A: GlobalAlloc,
|
||||
{
|
||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||
let size = layout.size();
|
||||
let t = *ALLOCS.get_mut();
|
||||
*ALLOCS.get_mut() = t + size;
|
||||
|
||||
let pointer = self.0.alloc(layout);
|
||||
pointer
|
||||
}
|
||||
|
||||
unsafe fn dealloc(&self, pointer: *mut u8, layout: Layout) {
|
||||
let size = layout.size();
|
||||
let t = *DEALLOCS.get_mut();
|
||||
*DEALLOCS.get_mut() = t + size;
|
||||
|
||||
self.0.dealloc(pointer, layout);
|
||||
}
|
||||
|
||||
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
|
||||
let size = layout.size();
|
||||
let t = *ALLOCS.get_mut();
|
||||
*ALLOCS.get_mut() = t + size;
|
||||
|
||||
let pointer = self.0.alloc_zeroed(layout);
|
||||
|
||||
pointer
|
||||
}
|
||||
|
||||
unsafe fn realloc(&self, old_pointer: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
|
||||
let old_size = layout.size();
|
||||
let t = *REALLOCSD.get_mut();
|
||||
*REALLOCSD.get_mut() = t + old_size;
|
||||
|
||||
let t = *REALLOCSA.get_mut();
|
||||
*REALLOCSA.get_mut() = t + layout.size();
|
||||
|
||||
let new_pointer = self.0.realloc(old_pointer, layout, new_size);
|
||||
|
||||
new_pointer
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user