Decommit instance memory after a runtime call on Linux (#8998)

* Decommit instance memory after a runtime call on Linux

* Update documentation for the test

* Remove unfinished comment

* Use saturating_sub.

Also update the doc comment.

* Precise RSS tracking in the test

Instead of tracking RSS for the whole process we just look at the particular mapping that is associated with the linear memory of the runtime instance

* Remove unused import

* Fix unused imports

* Fix the unused imports error for good

* Rollback an accidental change to benches

* Fix the test

* Remove now unneeded code
This commit is contained in:
Sergei Shulepov
2021-06-14 22:07:06 +01:00
committed by GitHub
parent 65d3d5d4ab
commit 3df32a5411
10 changed files with 260 additions and 1 deletions
@@ -415,6 +415,43 @@ impl InstanceWrapper {
slice::from_raw_parts_mut(ptr, len)
}
}
/// Returns the pointer to the first byte of the linear memory for this instance.
pub fn base_ptr(&self) -> *const u8 {
self.memory.data_ptr()
}
/// Removes physical backing from the allocated linear memory. This leads to returning the memory
/// back to the system. While the memory is zeroed this is considered as a side-effect and is not
/// relied upon. Thus this function acts as a hint.
pub fn decommit(&self) {
if self.memory.data_size() == 0 {
return;
}
cfg_if::cfg_if! {
if #[cfg(target_os = "linux")] {
use std::sync::Once;
unsafe {
let ptr = self.memory.data_ptr();
let len = self.memory.data_size();
// Linux handles MADV_DONTNEED reliably. The result is that the given area
// is unmapped and will be zeroed on the next pagefault.
if libc::madvise(ptr as _, len, libc::MADV_DONTNEED) != 0 {
static LOGGED: Once = Once::new();
LOGGED.call_once(|| {
log::warn!(
"madvise(MADV_DONTNEED) failed: {}",
std::io::Error::last_os_error(),
);
});
}
}
}
}
}
}
impl runtime_blob::InstanceGlobals for InstanceWrapper {