From 52c834d6436ddc73f7d25baa7b96e76fbe1b78c6 Mon Sep 17 00:00:00 2001 From: Yuhao Su Date: Mon, 24 Jul 2023 15:12:16 +0800 Subject: [PATCH] fix test Signed-off-by: Yuhao Su --- jemalloc-ctl/src/macros.rs | 40 +++++++++++++++++++------------------- jemalloc-ctl/src/prof.rs | 14 ++++++++----- jemalloc-ctl/src/raw.rs | 21 +------------------- 3 files changed, 30 insertions(+), 45 deletions(-) diff --git a/jemalloc-ctl/src/macros.rs b/jemalloc-ctl/src/macros.rs index d14385df2..04c2dd13d 100644 --- a/jemalloc-ctl/src/macros.rs +++ b/jemalloc-ctl/src/macros.rs @@ -114,25 +114,25 @@ macro_rules! w { #[test] #[cfg(not(target_arch = "mips64el"))] fn [<$id _write_test>]() { - // /// Help test write - // pub trait WriteTestDefault { - // fn default() -> Self; - // } - // macro_rules! impl_write_test_default { - // ($write_ty:ty, $val:expr) => { - // impl WriteTestDefault for $write_ty { - // fn default() -> $write_ty { - // $val - // } - // } - // }; - // } + /// Help test write + pub trait WriteTestDefault { + fn default() -> Self; + } + macro_rules! impl_write_test_default { + ($write_ty:ty, $val:expr) => { + impl WriteTestDefault for $write_ty { + fn default() -> $write_ty { + $val + } + } + }; + } - // use crate::ffi::CStr; - // impl_write_test_default! {libc::size_t, 0} - // impl_write_test_default! {u64, 0} - // impl_write_test_default! {bool, false} - // impl_write_test_default! {&'static CStr, CStr::from_bytes_with_nul(b"test\0").unwrap()} + use crate::ffi::CStr; + impl_write_test_default! {libc::size_t, 0} + impl_write_test_default! {u64, 0} + impl_write_test_default! {bool, false} + impl_write_test_default! {&'static CStr, CStr::from_bytes_with_nul(b"test\0").unwrap()} match stringify!($id) { "background_thread" | @@ -141,10 +141,10 @@ macro_rules! w { _ => (), } - let _ = $id::write(<$ret_ty as Default>::default()).unwrap(); + let _ = $id::write(<$ret_ty as WriteTestDefault>::default()).unwrap(); let mib = $id::mib().unwrap(); - let _ = mib.write(<$ret_ty as Default>::default()).unwrap(); + let _ = mib.write(<$ret_ty as WriteTestDefault>::default()).unwrap(); #[cfg(feature = "use_std")] println!( diff --git a/jemalloc-ctl/src/prof.rs b/jemalloc-ctl/src/prof.rs index 0fe26db60..c6e210bdb 100644 --- a/jemalloc-ctl/src/prof.rs +++ b/jemalloc-ctl/src/prof.rs @@ -18,8 +18,10 @@ option! { /// # /// # fn main() { /// use tikv_jemalloc_ctl::prof; + /// use std::ffi::CStr; + /// let dump_file_name = CStr::from_bytes_with_nul(b"dump\0").unwrap(); /// let dump = prof::dump::mib().unwrap(); - /// prof.write("prof.heap").unwrap(); + /// dump.write(dump_file_name).unwrap(); /// # } /// ``` mib_docs: /// See [`dump`]. @@ -41,8 +43,10 @@ option! { /// # /// # fn main() { /// use tikv_jemalloc_ctl::prof; + /// use std::ffi::CStr; + /// let dump_file_name = CStr::from_bytes_with_nul(b"my_prefix\0").unwrap(); /// let prefix = prof::prefix::mib().unwrap(); - /// prefix.write("my_prefix").unwrap(); + /// prefix.write(dump_file_name).unwrap(); /// # } /// ``` mib_docs: /// See [`prefix`]. @@ -52,9 +56,9 @@ option! { active[ str: b"prof.active\0", non_str: 2 ] => bool | ops: r, w, u | docs: - /// Control whether sampling is currently active. + /// Control whether sampling is currently active. /// - /// See the `opt.prof_active` option for additional information, + /// See the `opt.prof_active` option for additional information, /// as well as the interrelated `thread.prof.active` mallctl. /// /// # Examples @@ -70,4 +74,4 @@ option! { /// # } /// ``` mib_docs: /// See [`active`]. -} \ No newline at end of file +} diff --git a/jemalloc-ctl/src/raw.rs b/jemalloc-ctl/src/raw.rs index 7174dc38c..8fd60351f 100644 --- a/jemalloc-ctl/src/raw.rs +++ b/jemalloc-ctl/src/raw.rs @@ -146,25 +146,6 @@ pub unsafe fn write(name: &[u8], mut value: T) -> Result<()> { )) } -/// Uses the null-terminated string `name` as the key to the _MALLCTL NAMESPACE_ -/// and writes it string `value` -/// -/// # Safety -/// -/// This function is `unsafe` because it is possible to pass a string that does not -/// match it's len. -pub unsafe fn write_str_unsafe(name: &[u8], mut value: *const c_char, len: usize) -> Result<()> { - validate_name(name); - - cvt(tikv_jemalloc_sys::mallctl( - name as *const _ as *const c_char, - ptr::null_mut(), - ptr::null_mut(), - &mut value as *mut _ as *mut _, - len, - )) -} - /// Uses the MIB `mib` as key to the _MALLCTL NAMESPACE_ and writes its `value` /// returning its previous value. /// @@ -332,7 +313,7 @@ pub fn write_str(name: &[u8], value: &'static [u8]) -> Result<()> { // This is safe because `value` will always point to a null-terminated // string, which makes it safe for all key value types: pointers to // null-terminated strings, pointers, pointer-sized integers, etc. - unsafe { write_str_unsafe(name, value.as_ptr() as *const c_char, value.len()) } + unsafe { write(name, value.as_ptr() as *const c_char) } } /// Uses the null-terminated string `name` as key to the _MALLCTL NAMESPACE_ and