From 47a5d9a461a56d2f88647b143a2482d9f9a1e68d Mon Sep 17 00:00:00 2001 From: Yuhao Su Date: Wed, 19 Jul 2023 20:06:59 +0800 Subject: [PATCH 01/10] add dump and prefix Signed-off-by: Yuhao Su --- jemalloc-ctl/src/prof.rs | 47 ++++++++++++++++++++++++++++++++++++++++ jemalloc-sys/jemalloc | 2 +- 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 jemalloc-ctl/src/prof.rs diff --git a/jemalloc-ctl/src/prof.rs b/jemalloc-ctl/src/prof.rs new file mode 100644 index 000000000..a6ccdd610 --- /dev/null +++ b/jemalloc-ctl/src/prof.rs @@ -0,0 +1,47 @@ +//! `jemalloc`'s profiling utils. + +option! { + dump[ str: b"prof.dump\0", str: 2 ] => &'static str | + ops: w | + docs: + /// Dump a memory profile to the specified file, or if NULL is specified, + /// to a file according to the pattern ...m.heap, + /// where is controlled by the opt.prof_prefix and prof.prefix options. + /// + /// # Examples + /// + /// ``` + /// # #[global_allocator] + /// # static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; + /// # + /// # fn main() { + /// use tikv_jemalloc_ctl::prof; + /// let dump = prof::dump::mib().unwrap(); + /// prof.write("prof.heap").unwrap()); + /// # } + /// ``` + mib_docs: /// See [`dump`]. +} + +option! { + prefix[ str: b"prof.prefix\0", str: 2 ] => &'static str | + ops: w | + docs: + /// Set the filename prefix for profile dumps. See opt.prof_prefix for the default setting. + /// + /// This can be useful to differentiate profile dumps such as from forked processes. + /// + /// # Examples + /// + /// ``` + /// # #[global_allocator] + /// # static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; + /// # + /// # fn main() { + /// use tikv_jemalloc_ctl::prof; + /// let prefix = prof::prefix::mib().unwrap(); + /// prefix.write("my_prefix").unwrap()); + /// # } + /// ``` + mib_docs: /// See [`prefix`]. +} \ No newline at end of file diff --git a/jemalloc-sys/jemalloc b/jemalloc-sys/jemalloc index e13ca993e..54eaed1d8 160000 --- a/jemalloc-sys/jemalloc +++ b/jemalloc-sys/jemalloc @@ -1 +1 @@ -Subproject commit e13ca993e8ccb9ba9847cc330696e02839f328f7 +Subproject commit 54eaed1d8b56b1aa528be3bdd1877e59c56fa90c From 374056b63e9604441ca63617ebe062a69d14c5a2 Mon Sep 17 00:00:00 2001 From: Yuhao Su Date: Fri, 21 Jul 2023 16:24:33 +0800 Subject: [PATCH 02/10] add CStr Access Signed-off-by: Yuhao Su --- jemalloc-ctl/src/keys.rs | 57 +++++++++++++++++++++++++++++++++++++- jemalloc-ctl/src/lib.rs | 3 +- jemalloc-ctl/src/macros.rs | 24 ++++++++++++++-- jemalloc-ctl/src/prof.rs | 20 +++++++------ 4 files changed, 91 insertions(+), 13 deletions(-) diff --git a/jemalloc-ctl/src/keys.rs b/jemalloc-ctl/src/keys.rs index c207025b4..fbcf2a76a 100644 --- a/jemalloc-ctl/src/keys.rs +++ b/jemalloc-ctl/src/keys.rs @@ -31,6 +31,8 @@ use crate::error::Result; use crate::std::str; use crate::{fmt, ops, raw}; +use super::ffi::CStr; + /// A `Name` in the _MALLCTL NAMESPACE_. #[repr(transparent)] #[derive(PartialEq, Eq)] @@ -105,7 +107,8 @@ impl Name { | b"opt.thp" | b"opt.prof_prefix" | b"thread.prof.name" - | b"prof.dump" => true, + | b"prof.dump" + | b"prof.prefix" => true, v if v.starts_with(b"arena.") && v.ends_with(b".dss") => true, v if v.starts_with(b"stats.arenas.") && v.ends_with(b".dss") => { true @@ -355,6 +358,58 @@ impl Access<&'static str> for Name { } } +impl Access<&'static CStr> for MibStr { + fn read(&self) -> Result<&'static CStr> { + // this is safe because the only safe way to construct a `MibStr` is by + // validating that the key refers to a byte-string value + let s = unsafe { raw::read_str_mib(self.0.as_ref())? }; + Ok(CStr::from_bytes_with_nul(s).unwrap()) + } + fn write(&self, value: &'static CStr) -> Result<()> { + raw::write_str_mib(self.0.as_ref(), value.to_bytes_with_nul()) + } + fn update(&self, value: &'static CStr) -> Result<&'static CStr> { + // this is safe because the only safe way to construct a `MibStr` is by + // validating that the key refers to a byte-string value + let s = unsafe { + raw::update_str_mib(self.0.as_ref(), value.to_bytes_with_nul())? + }; + Ok(CStr::from_bytes_with_nul(s).unwrap()) + } +} + +impl Access<&'static CStr> for Name { + fn read(&self) -> Result<&'static CStr> { + assert!( + self.value_type_str(), + "the name \"{:?}\" does not refer to a byte string", + self + ); + // this is safe because the key refers to a byte string: + let s = unsafe { raw::read_str(&self.0)? }; + Ok(CStr::from_bytes_with_nul(s).unwrap()) + } + fn write(&self, value: &'static CStr) -> Result<()> { + assert!( + self.value_type_str(), + "the name \"{:?}\" does not refer to a byte string", + self + ); + raw::write_str(&self.0, value.to_bytes_with_nul()) + } + fn update(&self, value: &'static CStr) -> Result<&'static CStr> { + assert!( + self.value_type_str(), + "the name \"{:?}\" does not refer to a byte string", + self + ); + // this is safe because the key refers to a byte string: + let s = + unsafe { raw::update_str(&self.0, value.to_bytes_with_nul())? }; + Ok(CStr::from_bytes_with_nul(s).unwrap()) + } +} + #[cfg(test)] mod tests { use super::{Access, AsName, Mib, MibStr}; diff --git a/jemalloc-ctl/src/lib.rs b/jemalloc-ctl/src/lib.rs index a5aef998a..325abe8ec 100644 --- a/jemalloc-ctl/src/lib.rs +++ b/jemalloc-ctl/src/lib.rs @@ -74,7 +74,7 @@ #[global_allocator] static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; -use crate::std::{fmt, mem, num, ops, ptr, result, slice, str}; +use crate::std::{ffi, fmt, mem, num, ops, ptr, result, slice, str}; #[cfg(not(feature = "use_std"))] use core as std; #[cfg(feature = "use_std")] @@ -88,6 +88,7 @@ pub mod config; mod error; mod keys; pub mod opt; +pub mod prof; pub mod raw; pub mod stats; #[cfg(feature = "use_std")] diff --git a/jemalloc-ctl/src/macros.rs b/jemalloc-ctl/src/macros.rs index ee4bd5ff0..e598ca18c 100644 --- a/jemalloc-ctl/src/macros.rs +++ b/jemalloc-ctl/src/macros.rs @@ -114,6 +114,26 @@ 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 + } + } + }; + } + + 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" | "max_background_threads" @@ -121,10 +141,10 @@ macro_rules! w { _ => (), } - let _ = $id::write($ret_ty::default()).unwrap(); + let _ = $id::write(<$ret_ty as WriteTestDefault>::default()).unwrap(); let mib = $id::mib().unwrap(); - let _ = mib.write($ret_ty::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 a6ccdd610..a64b69c67 100644 --- a/jemalloc-ctl/src/prof.rs +++ b/jemalloc-ctl/src/prof.rs @@ -1,11 +1,13 @@ //! `jemalloc`'s profiling utils. +use crate::ffi::CStr; + option! { - dump[ str: b"prof.dump\0", str: 2 ] => &'static str | + dump[ str: b"prof.dump\0", str: 2 ] => &'static CStr | ops: w | docs: - /// Dump a memory profile to the specified file, or if NULL is specified, - /// to a file according to the pattern ...m.heap, + /// Dump a memory profile to the specified file, or if NULL is specified, + /// to a file according to the pattern ...m.heap, /// where is controlled by the opt.prof_prefix and prof.prefix options. /// /// # Examples @@ -17,18 +19,18 @@ option! { /// # fn main() { /// use tikv_jemalloc_ctl::prof; /// let dump = prof::dump::mib().unwrap(); - /// prof.write("prof.heap").unwrap()); + /// prof.write("prof.heap").unwrap(); /// # } /// ``` mib_docs: /// See [`dump`]. } option! { - prefix[ str: b"prof.prefix\0", str: 2 ] => &'static str | + prefix[ str: b"prof.prefix\0", str: 2 ] => &'static CStr | ops: w | docs: - /// Set the filename prefix for profile dumps. See opt.prof_prefix for the default setting. - /// + /// Set the filename prefix for profile dumps. See opt.prof_prefix for the default setting. + /// /// This can be useful to differentiate profile dumps such as from forked processes. /// /// # Examples @@ -40,8 +42,8 @@ option! { /// # fn main() { /// use tikv_jemalloc_ctl::prof; /// let prefix = prof::prefix::mib().unwrap(); - /// prefix.write("my_prefix").unwrap()); + /// prefix.write("my_prefix").unwrap(); /// # } /// ``` mib_docs: /// See [`prefix`]. -} \ No newline at end of file +} From 51f8d3d09a371ceae7be2b4bdcaf04f02572b5ba Mon Sep 17 00:00:00 2001 From: Yuhao Su Date: Fri, 21 Jul 2023 20:45:43 +0800 Subject: [PATCH 03/10] write_str_unsafe and active Signed-off-by: Yuhao Su --- jemalloc-ctl/src/macros.rs | 42 +++++++++++++++++++------------------- jemalloc-ctl/src/prof.rs | 24 ++++++++++++++++++++++ jemalloc-ctl/src/raw.rs | 21 ++++++++++++++++++- 3 files changed, 65 insertions(+), 22 deletions(-) diff --git a/jemalloc-ctl/src/macros.rs b/jemalloc-ctl/src/macros.rs index e598ca18c..d14385df2 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,17 +141,17 @@ macro_rules! w { _ => (), } - let _ = $id::write(<$ret_ty as WriteTestDefault>::default()).unwrap(); + let _ = $id::write(<$ret_ty as Default>::default()).unwrap(); let mib = $id::mib().unwrap(); - let _ = mib.write(<$ret_ty as WriteTestDefault>::default()).unwrap(); + let _ = mib.write(<$ret_ty as Default>::default()).unwrap(); #[cfg(feature = "use_std")] println!( concat!( stringify!($id), " (write): \"{}\""), - $ret_ty::default() + <$ret_ty as Default>::default() ); } diff --git a/jemalloc-ctl/src/prof.rs b/jemalloc-ctl/src/prof.rs index a64b69c67..0fe26db60 100644 --- a/jemalloc-ctl/src/prof.rs +++ b/jemalloc-ctl/src/prof.rs @@ -47,3 +47,27 @@ option! { /// ``` mib_docs: /// See [`prefix`]. } + +option! { + active[ str: b"prof.active\0", non_str: 2 ] => bool | + ops: r, w, u | + docs: + /// Control whether sampling is currently active. + /// + /// See the `opt.prof_active` option for additional information, + /// as well as the interrelated `thread.prof.active` mallctl. + /// + /// # Examples + /// + /// ``` + /// # #[global_allocator] + /// # static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; + /// # + /// # fn main() { + /// use tikv_jemalloc_ctl::prof; + /// let active = prof::active::mib().unwrap(); + /// active.write(true).unwrap(); + /// # } + /// ``` + 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 8fd60351f..7174dc38c 100644 --- a/jemalloc-ctl/src/raw.rs +++ b/jemalloc-ctl/src/raw.rs @@ -146,6 +146,25 @@ 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. /// @@ -313,7 +332,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(name, value.as_ptr() as *const c_char) } + unsafe { write_str_unsafe(name, value.as_ptr() as *const c_char, value.len()) } } /// Uses the null-terminated string `name` as key to the _MALLCTL NAMESPACE_ and From 4914c5d990c41110e3112f59d1b4a3ff8fd7ddad Mon Sep 17 00:00:00 2001 From: Yuhao Su Date: Mon, 24 Jul 2023 15:12:16 +0800 Subject: [PATCH 04/10] 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 From bb3bf17f280408bdff809562a23b4b8a7223f3cc Mon Sep 17 00:00:00 2001 From: Yuhao Su Date: Thu, 27 Jul 2023 13:51:40 +0800 Subject: [PATCH 05/10] add opt.prof Signed-off-by: Yuhao Su --- jemalloc-ctl/dump | 3 +++ jemalloc-ctl/src/opt.rs | 21 +++++++++++++++++++++ jemalloc-ctl/test | 4 ++++ 3 files changed, 28 insertions(+) create mode 100644 jemalloc-ctl/dump create mode 100644 jemalloc-ctl/test diff --git a/jemalloc-ctl/dump b/jemalloc-ctl/dump new file mode 100644 index 000000000..bad23fc5a --- /dev/null +++ b/jemalloc-ctl/dump @@ -0,0 +1,3 @@ +heap_v2/524288 + t*: 0: 0 [0: 0] + t0: 0: 0 [0: 0] diff --git a/jemalloc-ctl/src/opt.rs b/jemalloc-ctl/src/opt.rs index dc7c17ac6..d0a6ec46a 100644 --- a/jemalloc-ctl/src/opt.rs +++ b/jemalloc-ctl/src/opt.rs @@ -215,3 +215,24 @@ option! { /// ``` mib_docs: /// See [`background_thread`]. } + +option! { + prof[ str: b"opt.prof\0", non_str: 2 ] => bool | + ops: r | + docs: + /// Memory profiling enabled/disabled. If enabled, profile memory allocation activity. + /// + /// # Examples + /// + /// ``` + /// # #[global_allocator] + /// # static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; + /// # + /// # fn main() { + /// use tikv_jemalloc_ctl::opt; + /// let prof = opt::prof::read().unwrap(); + /// println!("Jemalloc profiling enabled: {}", prof); + /// # } + /// ``` + mib_docs: /// See [`prof`]. +} \ No newline at end of file diff --git a/jemalloc-ctl/test b/jemalloc-ctl/test new file mode 100644 index 000000000..de7db5cff --- /dev/null +++ b/jemalloc-ctl/test @@ -0,0 +1,4 @@ +heap_v2/524288 + t*: 0: 0 [0: 0] + t0: 0: 0 [0: 0] + t1: 0: 0 [0: 0] From 4eca9c6122e57e5a806987f3817dda9d9cc3624d Mon Sep 17 00:00:00 2001 From: Yuhao Su Date: Mon, 31 Jul 2023 16:33:45 +0800 Subject: [PATCH 06/10] fix test Signed-off-by: Yuhao Su --- ci/run.sh | 2 +- jemalloc-ctl/src/macros.rs | 26 ++++++++++++++++++++++---- jemalloc-ctl/src/opt.rs | 11 +++++++---- jemalloc-ctl/src/prof.rs | 35 ++++++++++++++++++++++------------- 4 files changed, 52 insertions(+), 22 deletions(-) diff --git a/ci/run.sh b/ci/run.sh index 61182f318..9c752ea50 100755 --- a/ci/run.sh +++ b/ci/run.sh @@ -26,7 +26,7 @@ fi cargo build --target "${TARGET}" cargo test --target "${TARGET}" -cargo test --target "${TARGET}" --features profiling +_RJEM_MALLOC_CONF="prof:true" cargo test --target "${TARGET}" --features profiling cargo test --target "${TARGET}" --features debug cargo test --target "${TARGET}" --features stats cargo test --target "${TARGET}" --features 'debug profiling' diff --git a/jemalloc-ctl/src/macros.rs b/jemalloc-ctl/src/macros.rs index 04c2dd13d..103383329 100644 --- a/jemalloc-ctl/src/macros.rs +++ b/jemalloc-ctl/src/macros.rs @@ -43,7 +43,7 @@ macro_rules! types { /// Read macro_rules! r { - ($id:ident => $ret_ty:ty) => { + ($id:ident[ str: $byte_string:expr ] => $ret_ty:ty) => { paste::paste! { impl $id { /// Reads value using string API. @@ -72,6 +72,12 @@ macro_rules! r { if cfg!(target_os = "macos") => return, _ => (), } + match $byte_string.as_slice() { + b"opt.prof\0" | + b"prof.active\0" + if !cfg!(feature = "profiling") => return, + _ => (), + } let a = $id::read().unwrap(); @@ -92,7 +98,7 @@ macro_rules! r { /// Write macro_rules! w { - ($id:ident => $ret_ty:ty) => { + ($id:ident[ str: $byte_string:expr ] => $ret_ty:ty) => { paste::paste! { impl $id { /// Writes `value` using string API. @@ -140,6 +146,13 @@ macro_rules! w { if cfg!(target_os = "macos") => return, _ => (), } + match $byte_string.as_slice() { + b"prof.dump\0" | + b"prof.active\0" | + b"prof.prefix\0" + if !cfg!(feature = "profiling") => return, + _ => (), + } let _ = $id::write(<$ret_ty as WriteTestDefault>::default()).unwrap(); @@ -161,7 +174,7 @@ macro_rules! w { /// Update macro_rules! u { - ($id:ident => $ret_ty:ty) => { + ($id:ident[ str: $byte_string:expr ] => $ret_ty:ty) => { paste::paste! { impl $id { /// Updates key to `value` returning its old value using string API. @@ -190,6 +203,11 @@ macro_rules! u { if cfg!(target_os = "macos") => return, _ => (), } + match $byte_string.as_slice() { + b"prof.active\0" + if !cfg!(feature = "profiling") => return, + _ => (), + } let a = $id::update($ret_ty::default()).unwrap(); @@ -223,7 +241,7 @@ macro_rules! option { mib_docs: $(#[$doc_mib])* } $( - $ops!($id => $ret_ty); + $ops!($id[ str: $byte_string ] => $ret_ty); )* }; // Non-string option: diff --git a/jemalloc-ctl/src/opt.rs b/jemalloc-ctl/src/opt.rs index d0a6ec46a..ef72e9801 100644 --- a/jemalloc-ctl/src/opt.rs +++ b/jemalloc-ctl/src/opt.rs @@ -229,10 +229,13 @@ option! { /// # static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; /// # /// # fn main() { - /// use tikv_jemalloc_ctl::opt; - /// let prof = opt::prof::read().unwrap(); - /// println!("Jemalloc profiling enabled: {}", prof); + /// #[cfg(feature = "profiling")] + /// { + /// use tikv_jemalloc_ctl::opt; + /// let prof = opt::prof::read().unwrap(); + /// println!("Jemalloc profiling enabled: {}", prof); + /// } /// # } /// ``` mib_docs: /// See [`prof`]. -} \ No newline at end of file +} diff --git a/jemalloc-ctl/src/prof.rs b/jemalloc-ctl/src/prof.rs index c6e210bdb..b14c16c13 100644 --- a/jemalloc-ctl/src/prof.rs +++ b/jemalloc-ctl/src/prof.rs @@ -17,11 +17,14 @@ option! { /// # static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; /// # /// # 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(); - /// dump.write(dump_file_name).unwrap(); + /// #[cfg(feature = "profiling")] + /// { + /// 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(); + /// dump.write(dump_file_name).unwrap(); + /// } /// # } /// ``` mib_docs: /// See [`dump`]. @@ -42,11 +45,14 @@ option! { /// # static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; /// # /// # 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(dump_file_name).unwrap(); + /// #[cfg(feature = "profiling")] + /// { + /// 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(dump_file_name).unwrap(); + /// } /// # } /// ``` mib_docs: /// See [`prefix`]. @@ -68,9 +74,12 @@ option! { /// # static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; /// # /// # fn main() { - /// use tikv_jemalloc_ctl::prof; - /// let active = prof::active::mib().unwrap(); - /// active.write(true).unwrap(); + /// #[cfg(feature = "profiling")] + /// { + /// use tikv_jemalloc_ctl::prof; + /// let active = prof::active::mib().unwrap(); + /// active.write(true).unwrap(); + /// } /// # } /// ``` mib_docs: /// See [`active`]. From 2cbdaf2dc49199797190f11aa270881115105230 Mon Sep 17 00:00:00 2001 From: Yuhao Su Date: Tue, 1 Aug 2023 13:32:30 +0800 Subject: [PATCH 07/10] fmt Signed-off-by: Yuhao Su --- jemalloc-ctl/src/prof.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jemalloc-ctl/src/prof.rs b/jemalloc-ctl/src/prof.rs index b14c16c13..9208735a1 100644 --- a/jemalloc-ctl/src/prof.rs +++ b/jemalloc-ctl/src/prof.rs @@ -79,7 +79,7 @@ option! { /// use tikv_jemalloc_ctl::prof; /// let active = prof::active::mib().unwrap(); /// active.write(true).unwrap(); - /// } + /// } /// # } /// ``` mib_docs: /// See [`active`]. From 8ce8ea3c068aa33adf1ec60d18ce5363ecc493f5 Mon Sep 17 00:00:00 2001 From: TennyZhuang Date: Wed, 7 Feb 2024 17:53:18 +0800 Subject: [PATCH 08/10] sync submodule Signed-off-by: TennyZhuang --- jemalloc-sys/jemalloc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jemalloc-sys/jemalloc b/jemalloc-sys/jemalloc index 54eaed1d8..e13ca993e 160000 --- a/jemalloc-sys/jemalloc +++ b/jemalloc-sys/jemalloc @@ -1 +1 @@ -Subproject commit 54eaed1d8b56b1aa528be3bdd1877e59c56fa90c +Subproject commit e13ca993e8ccb9ba9847cc330696e02839f328f7 From 8c062e8947824e6466d6e7d62cdfa2d1760a3058 Mon Sep 17 00:00:00 2001 From: TennyZhuang Date: Wed, 7 Feb 2024 18:22:44 +0800 Subject: [PATCH 09/10] remove two dump files Signed-off-by: TennyZhuang --- jemalloc-ctl/dump | 3 --- jemalloc-ctl/test | 4 ---- 2 files changed, 7 deletions(-) delete mode 100644 jemalloc-ctl/dump delete mode 100644 jemalloc-ctl/test diff --git a/jemalloc-ctl/dump b/jemalloc-ctl/dump deleted file mode 100644 index bad23fc5a..000000000 --- a/jemalloc-ctl/dump +++ /dev/null @@ -1,3 +0,0 @@ -heap_v2/524288 - t*: 0: 0 [0: 0] - t0: 0: 0 [0: 0] diff --git a/jemalloc-ctl/test b/jemalloc-ctl/test deleted file mode 100644 index de7db5cff..000000000 --- a/jemalloc-ctl/test +++ /dev/null @@ -1,4 +0,0 @@ -heap_v2/524288 - t*: 0: 0 [0: 0] - t0: 0: 0 [0: 0] - t1: 0: 0 [0: 0] From 5084726b0f15686d13e88bc54ce7cff4b4ed6d27 Mon Sep 17 00:00:00 2001 From: TennyZhuang Date: Wed, 7 Feb 2024 18:32:30 +0800 Subject: [PATCH 10/10]