提交 fb9453f1 authored 作者: Serhij S's avatar Serhij S

prealloc_heap

上级 7d4593e2
...@@ -66,7 +66,7 @@ pub enum Error { ...@@ -66,7 +66,7 @@ pub enum Error {
#[error("timed out")] #[error("timed out")]
Timeout, Timeout,
/// I/O and threading errors /// I/O and threading errors
#[error("I/O error {0}")] #[error("I/O error: {0}")]
IO(String), IO(String),
/// 3rd party API errors /// 3rd party API errors
#[error("API error {0}: {1}")] #[error("API error {0}: {1}")]
...@@ -101,6 +101,9 @@ pub enum Error { ...@@ -101,6 +101,9 @@ pub enum Error {
/// This error never happens and is used as a compiler hint only /// This error never happens and is used as a compiler hint only
#[error("never happens")] #[error("never happens")]
Infallible(#[from] std::convert::Infallible), Infallible(#[from] std::convert::Infallible),
/// All other errors
#[error("operation failed: {0}")]
Failed(String),
} }
macro_rules! impl_error { macro_rules! impl_error {
...@@ -131,6 +134,9 @@ impl Error { ...@@ -131,6 +134,9 @@ impl Error {
pub fn io<S: fmt::Display>(msg: S) -> Self { pub fn io<S: fmt::Display>(msg: S) -> Self {
Error::IO(msg.to_string()) Error::IO(msg.to_string())
} }
pub fn failed<S: fmt::Display>(msg: S) -> Self {
Error::Failed(msg.to_string())
}
} }
/// Data delivery policies, used by [`hub::Hub`], [`pchannel::Receiver`] and [`pdeque::Deque`] /// Data delivery policies, used by [`hub::Hub`], [`pchannel::Receiver`] and [`pdeque::Deque`]
......
...@@ -27,6 +27,42 @@ fn is_realtime() -> bool { ...@@ -27,6 +27,42 @@ fn is_realtime() -> bool {
REALTIME_MODE.load(Ordering::Relaxed) REALTIME_MODE.load(Ordering::Relaxed)
} }
/// The method preallocates a heap memory region with the given size. The method is useful to
/// prevent memory fragmentation and speed up memory allocation. It is highly recommended to call
/// the method at the beginning of the program.
///
/// Does nothing in simulated mode.
///
/// # Panics
///
/// Will panic if the page size is too large (more than usize)
pub fn prealloc_heap(size: usize) -> Result<()> {
if !is_realtime() {
return Ok(());
}
let page_size = unsafe {
if libc::mallopt(libc::M_MMAP_MAX, 0) != 1 {
return Err(Error::failed(
"unable to disable mmap for allocation of large mem regions",
));
}
if libc::mallopt(libc::M_TRIM_THRESHOLD, -1) != 1 {
return Err(Error::failed("unable to disable trimming"));
}
if libc::mlockall(libc::MCL_FUTURE) == -1 {
return Err(Error::failed("unable to lock memory pages"));
};
usize::try_from(libc::sysconf(libc::_SC_PAGESIZE)).expect("Page size too large")
};
let mut heap_mem = vec![0_u8; size];
std::hint::black_box(move || {
for i in (0..size).step_by(page_size) {
heap_mem[i] = 0xff;
}
})();
Ok(())
}
/// A thread builder object, similar to [`thread::Builder`] but with real-time capabilities /// A thread builder object, similar to [`thread::Builder`] but with real-time capabilities
/// ///
/// Warning: works on Linux systems only /// Warning: works on Linux systems only
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论