From 00e3324e348f6c08a55f0f9fb52167c9c73592d3 Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Tue, 12 Mar 2024 15:53:23 +0800 Subject: [PATCH 01/26] initial Signed-off-by: CalvinNeo --- proxy_components/engine_store_ffi/Cargo.toml | 2 + .../src/core/forward_raft/region.rs | 2 + .../src/mock_cluster/test_utils.rs | 1 - .../mock_store/mock_engine_store_server.rs | 58 +++++++++++++++++++ proxy_components/proxy_ffi/Cargo.toml | 2 + .../src/engine_store_helper_impls.rs | 42 +++++++++++++- proxy_components/proxy_ffi/src/interfaces.rs | 10 +++- proxy_components/proxy_ffi/src/lib.rs | 1 + proxy_components/proxy_server/Cargo.toml | 1 + proxy_components/proxy_server/src/run.rs | 5 ++ proxy_tests/Cargo.toml | 1 + proxy_tests/proxy/shared/mod.rs | 1 + .../ffi/src/RaftStoreProxyFFI/@version | 2 +- .../ffi/src/RaftStoreProxyFFI/ProxyFFI.h | 3 + 14 files changed, 127 insertions(+), 4 deletions(-) diff --git a/proxy_components/engine_store_ffi/Cargo.toml b/proxy_components/engine_store_ffi/Cargo.toml index 21eb18ce61d..2657cbf9b73 100644 --- a/proxy_components/engine_store_ffi/Cargo.toml +++ b/proxy_components/engine_store_ffi/Cargo.toml @@ -32,6 +32,8 @@ openssl-vendored = [ "openssl/vendored" ] +external-jemalloc = ["proxy_ffi/external-jemalloc"] + [dependencies] batch-system = { workspace = true, default-features = false } bitflags = "1.0.1" diff --git a/proxy_components/engine_store_ffi/src/core/forward_raft/region.rs b/proxy_components/engine_store_ffi/src/core/forward_raft/region.rs index bdb356c451f..19afb025e5b 100644 --- a/proxy_components/engine_store_ffi/src/core/forward_raft/region.rs +++ b/proxy_components/engine_store_ffi/src/core/forward_raft/region.rs @@ -13,6 +13,7 @@ impl ProxyForwarder { } pub fn on_region_changed(&self, ob_region: &Region, e: RegionChangeEvent, r: StateRole) { + self.engine_store_server_helper.maybe_jemalloc_register_alloc(); let region_id = ob_region.get_id(); if e == RegionChangeEvent::Destroy { info!( @@ -104,6 +105,7 @@ impl ProxyForwarder { } pub fn on_role_change(&self, ob_region: &Region, r: &RoleChange) { + self.engine_store_server_helper.maybe_jemalloc_register_alloc(); let region_id = ob_region.get_id(); let is_replicated = !r.initialized; let is_fap_enabled = if let Some(b) = self.engine.proxy_ext.config_set.as_ref() { diff --git a/proxy_components/mock-engine-store/src/mock_cluster/test_utils.rs b/proxy_components/mock-engine-store/src/mock_cluster/test_utils.rs index 095aeab1bb3..650d9a96d36 100644 --- a/proxy_components/mock-engine-store/src/mock_cluster/test_utils.rs +++ b/proxy_components/mock-engine-store/src/mock_cluster/test_utils.rs @@ -98,7 +98,6 @@ pub fn maybe_collect_states( } pub fn collect_all_states(cluster_ext: &ClusterExt, region_id: u64) -> HashMap { - maybe_collect_states(cluster_ext, region_id, None); let prev_state = maybe_collect_states(cluster_ext, region_id, None); assert_eq!( prev_state.len(), diff --git a/proxy_components/mock-engine-store/src/mock_store/mock_engine_store_server.rs b/proxy_components/mock-engine-store/src/mock_store/mock_engine_store_server.rs index 68fb195747e..3116d2b0233 100644 --- a/proxy_components/mock-engine-store/src/mock_store/mock_engine_store_server.rs +++ b/proxy_components/mock-engine-store/src/mock_store/mock_engine_store_server.rs @@ -1,6 +1,7 @@ // Copyright 2022 TiKV Project Authors. Licensed under Apache-2.0. use std::{cell::RefCell, pin::Pin, sync::atomic::Ordering}; +use std::sync::Mutex; use engine_store_ffi::TiFlashEngine; @@ -18,6 +19,27 @@ use super::{ }; use crate::mock_cluster; +#[derive(Clone)] +pub struct ThreadInfoJealloc { + pub allocated_ptr: u64, + pub deallocated_ptr: u64, +} + +impl ThreadInfoJealloc { + pub fn allocated(&self) -> u64 { + unsafe { + *(self.allocated_ptr as *const u64) + } + } + pub fn deallocated(&self) -> u64 { + unsafe { + *(self.deallocated_ptr as *const u64) + } + } + pub fn remaining(&self) -> i64 { + self.allocated() as i64 - self.deallocated() as i64 + } +} pub struct EngineStoreServer { pub id: u64, // TODO engines maybe changed into TabletRegistry? @@ -28,6 +50,7 @@ pub struct EngineStoreServer { pub page_storage: MockPageStorage, // (region_id, peer_id) -> MockRegion pub tmp_fap_regions: HashMap>, + pub thread_info_map: Mutex>, } impl EngineStoreServer { @@ -40,6 +63,7 @@ impl EngineStoreServer { region_states: RefCell::new(Default::default()), page_storage: Default::default(), tmp_fap_regions: Default::default(), + thread_info_map: Default::default(), } } @@ -369,6 +393,7 @@ pub fn gen_engine_store_server_helper( fn_query_fap_snapshot_state: Some(ffi_query_fap_snapshot_state), fn_kvstore_region_exists: Some(ffi_kvstore_region_exists), fn_clear_fap_snapshot: Some(ffi_clear_fap_snapshot), + fn_report_thread_allocate_info: Some(ffi_report_thread_allocate_info), ps: PageStorageInterfaces { fn_create_write_batch: Some(ffi_mockps_create_write_batch), fn_wb_put_page: Some(ffi_mockps_wb_put_page), @@ -609,3 +634,36 @@ unsafe extern "C" fn ffi_get_lock_by_key( }, } } + +unsafe extern "C" fn ffi_report_thread_allocate_info( + arg1: *mut interfaces_ffi::EngineStoreServerWrap, + name: interfaces_ffi::BaseBuffView, + t: u64, + value: u64, +) { + let store = into_engine_store_server_wrap(arg1); + let tn = std::str::from_utf8(name.to_slice()).unwrap().to_string(); + match (*store.engine_store_server).thread_info_map.lock().expect("poisoned").entry(tn) { + std::collections::hash_map::Entry::Occupied(mut o) => { + if t == 0 { + o.get_mut().allocated_ptr = value; + } else { + o.get_mut().deallocated_ptr = value; + } + } + std::collections::hash_map::Entry::Vacant(v) => { + if t == 0 { + v.insert(ThreadInfoJealloc { + allocated_ptr: value, + deallocated_ptr: 0 + }); + } else { + v.insert(ThreadInfoJealloc { + allocated_ptr: 0, + deallocated_ptr: value + }); + } + } + + } +} \ No newline at end of file diff --git a/proxy_components/proxy_ffi/Cargo.toml b/proxy_components/proxy_ffi/Cargo.toml index 0b49155e49e..4b75b4ce327 100644 --- a/proxy_components/proxy_ffi/Cargo.toml +++ b/proxy_components/proxy_ffi/Cargo.toml @@ -28,6 +28,8 @@ openssl-vendored = [ "openssl/vendored" ] +external-jemalloc = [] + [dependencies] encryption = { workspace = true, default-features = false } openssl = { workspace = true } # TODO only for feature diff --git a/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs b/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs index 68149af83e0..61683a18a06 100644 --- a/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs +++ b/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs @@ -1,5 +1,6 @@ // Copyright 2016 TiKV Project Authors. Licensed under Apache-2.0. use std::pin::Pin; +use std::cell::RefCell; use kvproto::{kvrpcpb, metapb, raft_cmdpb}; @@ -34,6 +35,10 @@ pub fn gen_engine_store_server_helper( unsafe { &(*(engine_store_server_helper as *const EngineStoreServerHelper)) } } +thread_local! { + pub static JEMALLOC_REGISTERED: RefCell = RefCell::new(false); +} + /// # Safety /// The lifetime of `engine_store_server_helper` is definitely longer than /// `ENGINE_STORE_SERVER_HELPER_PTR`. @@ -49,6 +54,31 @@ pub fn set_server_info_resp(res: &kvproto::diagnosticspb::ServerInfoResponse, pt } impl EngineStoreServerHelper { + pub fn maybe_jemalloc_register_alloc(&self) { + JEMALLOC_REGISTERED.with(|b| { + if !*b.borrow() { + unsafe { + let ptr_alloc: u64 = crate::jemalloc_utils::get_allocatep_on_thread_start(); + let ptr_dealloc: u64 = crate::jemalloc_utils::get_deallocatep_on_thread_start(); + let thread_name = std::thread::current().name().unwrap_or("").to_string(); + (self.fn_report_thread_allocate_info.into_inner())( + self.inner, + BaseBuffView::from(thread_name.as_bytes()), + 0, + ptr_alloc + ); + (self.fn_report_thread_allocate_info.into_inner())( + self.inner, + BaseBuffView::from(thread_name.as_bytes()), + 1, + ptr_dealloc + ); + } + *(b.borrow_mut()) = true; + } + }); + } + pub fn gc_raw_cpp_ptr(&self, ptr: *mut ::std::os::raw::c_void, tp: RawCppPtrType) { debug_assert!(self.fn_gc_raw_cpp_ptr.is_some()); unsafe { @@ -82,6 +112,7 @@ impl EngineStoreServerHelper { pub fn handle_compute_store_stats(&self) -> StoreStats { debug_assert!(self.fn_handle_compute_store_stats.is_some()); + self.maybe_jemalloc_register_alloc(); unsafe { (self.fn_handle_compute_store_stats.into_inner())(self.inner) } } @@ -91,16 +122,19 @@ impl EngineStoreServerHelper { header: RaftCmdHeader, ) -> EngineStoreApplyRes { debug_assert!(self.fn_handle_write_raft_cmd.is_some()); + self.maybe_jemalloc_register_alloc(); unsafe { (self.fn_handle_write_raft_cmd.into_inner())(self.inner, cmds.gen_view(), header) } } pub fn handle_get_engine_store_server_status(&self) -> EngineStoreServerStatus { debug_assert!(self.fn_handle_get_engine_store_server_status.is_some()); + self.maybe_jemalloc_register_alloc(); unsafe { (self.fn_handle_get_engine_store_server_status.into_inner())(self.inner) } } pub fn handle_set_proxy(&self, proxy: *const RaftStoreProxyFFIHelper) { debug_assert!(self.fn_atomic_update_proxy.is_some()); + self.maybe_jemalloc_register_alloc(); unsafe { (self.fn_atomic_update_proxy.into_inner())(self.inner, proxy as *mut _) } } @@ -129,7 +163,7 @@ impl EngineStoreServerHelper { header: RaftCmdHeader, ) -> EngineStoreApplyRes { debug_assert!(self.fn_handle_admin_raft_cmd.is_some()); - + self.maybe_jemalloc_register_alloc(); unsafe { let req = ProtoMsgBaseBuff::new(req); let resp = ProtoMsgBaseBuff::new(resp); @@ -158,6 +192,7 @@ impl EngineStoreServerHelper { term: u64, ) -> bool { debug_assert!(self.fn_try_flush_data.is_some()); + self.maybe_jemalloc_register_alloc(); // TODO(proactive flush) unsafe { (self.fn_try_flush_data.into_inner())( @@ -187,6 +222,7 @@ impl EngineStoreServerHelper { ) -> RawCppPtr { debug_assert!(self.fn_pre_handle_snapshot.is_some()); + self.maybe_jemalloc_register_alloc(); let snaps_view = into_sst_views(snaps); unsafe { let region = ProtoMsgBaseBuff::new(region); @@ -203,6 +239,7 @@ impl EngineStoreServerHelper { pub fn apply_pre_handled_snapshot(&self, snap: RawCppPtr) { debug_assert!(self.fn_apply_pre_handled_snapshot.is_some()); + self.maybe_jemalloc_register_alloc(); unsafe { (self.fn_apply_pre_handled_snapshot.into_inner())(self.inner, snap.ptr, snap.type_) } @@ -210,6 +247,7 @@ impl EngineStoreServerHelper { pub fn abort_pre_handle_snapshot(&self, region_id: u64, peer_id: u64) { debug_assert!(self.fn_abort_pre_handle_snapshot.is_some()); + self.maybe_jemalloc_register_alloc(); unsafe { (self.fn_abort_pre_handle_snapshot.into_inner())(self.inner, region_id, peer_id) } } @@ -277,6 +315,7 @@ impl EngineStoreServerHelper { ) -> EngineStoreApplyRes { debug_assert!(self.fn_handle_ingest_sst.is_some()); + self.maybe_jemalloc_register_alloc(); let snaps_view = into_sst_views(snaps); unsafe { (self.fn_handle_ingest_sst.into_inner())( @@ -290,6 +329,7 @@ impl EngineStoreServerHelper { pub fn handle_destroy(&self, region_id: u64) { debug_assert!(self.fn_handle_destroy.is_some()); + self.maybe_jemalloc_register_alloc(); unsafe { (self.fn_handle_destroy.into_inner())(self.inner, region_id); } diff --git a/proxy_components/proxy_ffi/src/interfaces.rs b/proxy_components/proxy_ffi/src/interfaces.rs index b06d0a5c7ca..7b84fdde853 100644 --- a/proxy_components/proxy_ffi/src/interfaces.rs +++ b/proxy_components/proxy_ffi/src/interfaces.rs @@ -750,6 +750,14 @@ pub mod root { region_id: u64, ) -> bool, >, + pub fn_report_thread_allocate_info: ::std::option::Option< + unsafe extern "C" fn( + arg1: *mut root::DB::EngineStoreServerWrap, + name: root::DB::BaseBuffView, + type_: u64, + value: u64, + ), + >, } extern "C" { pub fn ffi_get_server_info_from_proxy( @@ -758,7 +766,7 @@ pub mod root { arg3: root::DB::RawVoidPtr, ) -> u32; } - pub const RAFT_STORE_PROXY_VERSION: u64 = 8589640407431546086; + pub const RAFT_STORE_PROXY_VERSION: u64 = 10285342393410618515; pub const RAFT_STORE_PROXY_MAGIC_NUMBER: u32 = 324508639; } } diff --git a/proxy_components/proxy_ffi/src/lib.rs b/proxy_components/proxy_ffi/src/lib.rs index 59ed9dbdc8e..a9c02acefb0 100644 --- a/proxy_components/proxy_ffi/src/lib.rs +++ b/proxy_components/proxy_ffi/src/lib.rs @@ -23,6 +23,7 @@ pub mod read_index_helper; // FFI releated with reading from SST/RocksDB files. pub mod snapshot_reader_impls; pub mod utils; +pub mod jemalloc_utils; pub use self::{ basic_ffi_impls::*, domain_impls::*, encryption_impls::*, engine_store_helper_impls::*, diff --git a/proxy_components/proxy_server/Cargo.toml b/proxy_components/proxy_server/Cargo.toml index d843816c169..41b577ee77d 100644 --- a/proxy_components/proxy_server/Cargo.toml +++ b/proxy_components/proxy_server/Cargo.toml @@ -35,6 +35,7 @@ backup-stream-debug = ["backup-stream/backup-stream-debug"] testexport = ["engine_tiflash/testexport", "engine_store_ffi/testexport", "tikv/testexport"] pprof-fp = ["tikv/pprof-fp"] openssl-vendored = ["tikv/openssl-vendored", "openssl/vendored"] +external-jemalloc = ["engine_store_ffi/external-jemalloc"] [dependencies] api_version = { workspace = true } diff --git a/proxy_components/proxy_server/src/run.rs b/proxy_components/proxy_server/src/run.rs index 2aa90faaea9..4a2ea4a271d 100644 --- a/proxy_components/proxy_server/src/run.rs +++ b/proxy_components/proxy_server/src/run.rs @@ -618,6 +618,11 @@ impl TiKvServer { Arc::clone(&security_mgr), ); + #[cfg(feature = "external-jemalloc")] + info!("linked with external jemalloc"); + #[cfg(not(feature = "external-jemalloc"))] + info!("linked without external jemalloc"); + // Initialize and check config info!("using proxy config"; "config" => ?proxy_config); crate::config::address_proxy_config(&mut config, &proxy_config); diff --git a/proxy_tests/Cargo.toml b/proxy_tests/Cargo.toml index 35f0bebe765..dcb366b9706 100644 --- a/proxy_tests/Cargo.toml +++ b/proxy_tests/Cargo.toml @@ -46,6 +46,7 @@ sse = ["tikv/sse"] portable = ["tikv/portable"] openssl-vendored = ["tikv/openssl-vendored"] enable-pagestorage = [] +external-jemalloc = ["proxy_server/external-jemalloc", "engine_store_ffi/external-jemalloc"] [dependencies] api_version = { workspace = true } diff --git a/proxy_tests/proxy/shared/mod.rs b/proxy_tests/proxy/shared/mod.rs index 7a20b9d4483..b094adebf7b 100644 --- a/proxy_tests/proxy/shared/mod.rs +++ b/proxy_tests/proxy/shared/mod.rs @@ -14,3 +14,4 @@ mod server_cluster_test; mod snapshot; mod store; mod write; +mod jemalloc; diff --git a/raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version b/raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version index 91166e0b477..64ac68ec2ee 100644 --- a/raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version +++ b/raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version @@ -1,3 +1,3 @@ #pragma once #include -namespace DB { constexpr uint64_t RAFT_STORE_PROXY_VERSION = 8589640407431546086ull; } \ No newline at end of file +namespace DB { constexpr uint64_t RAFT_STORE_PROXY_VERSION = 10285342393410618515ull; } \ No newline at end of file diff --git a/raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h b/raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h index 17428d52ce3..27dedc79370 100644 --- a/raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h +++ b/raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h @@ -373,6 +373,9 @@ struct EngineStoreServerHelper { uint64_t, uint64_t); void (*fn_clear_fap_snapshot)(EngineStoreServerWrap *, uint64_t region_id); bool (*fn_kvstore_region_exists)(EngineStoreServerWrap *, uint64_t region_id); + void (*fn_report_thread_allocate_info)(EngineStoreServerWrap *, + BaseBuffView name, uint64_t type, + uint64_t value); }; #ifdef __cplusplus From 3554e0629d0867c766ae655f59746d937142114a Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Tue, 12 Mar 2024 16:05:38 +0800 Subject: [PATCH 02/26] tests Signed-off-by: CalvinNeo --- .../src/core/forward_raft/region.rs | 6 +- .../mock_store/mock_engine_store_server.rs | 29 +++---- .../src/engine_store_helper_impls.rs | 12 +-- .../proxy_ffi/src/jemalloc_utils.rs | 70 +++++++++++++++++ proxy_components/proxy_ffi/src/lib.rs | 2 +- proxy_components/proxy_server/src/run.rs | 2 +- proxy_tests/proxy/shared/jemalloc.rs | 78 +++++++++++++++++++ proxy_tests/proxy/shared/mod.rs | 2 +- 8 files changed, 178 insertions(+), 23 deletions(-) create mode 100644 proxy_components/proxy_ffi/src/jemalloc_utils.rs create mode 100644 proxy_tests/proxy/shared/jemalloc.rs diff --git a/proxy_components/engine_store_ffi/src/core/forward_raft/region.rs b/proxy_components/engine_store_ffi/src/core/forward_raft/region.rs index 19afb025e5b..79b12513439 100644 --- a/proxy_components/engine_store_ffi/src/core/forward_raft/region.rs +++ b/proxy_components/engine_store_ffi/src/core/forward_raft/region.rs @@ -13,7 +13,8 @@ impl ProxyForwarder { } pub fn on_region_changed(&self, ob_region: &Region, e: RegionChangeEvent, r: StateRole) { - self.engine_store_server_helper.maybe_jemalloc_register_alloc(); + self.engine_store_server_helper + .maybe_jemalloc_register_alloc(); let region_id = ob_region.get_id(); if e == RegionChangeEvent::Destroy { info!( @@ -105,7 +106,8 @@ impl ProxyForwarder { } pub fn on_role_change(&self, ob_region: &Region, r: &RoleChange) { - self.engine_store_server_helper.maybe_jemalloc_register_alloc(); + self.engine_store_server_helper + .maybe_jemalloc_register_alloc(); let region_id = ob_region.get_id(); let is_replicated = !r.initialized; let is_fap_enabled = if let Some(b) = self.engine.proxy_ext.config_set.as_ref() { diff --git a/proxy_components/mock-engine-store/src/mock_store/mock_engine_store_server.rs b/proxy_components/mock-engine-store/src/mock_store/mock_engine_store_server.rs index 3116d2b0233..e49e88673f9 100644 --- a/proxy_components/mock-engine-store/src/mock_store/mock_engine_store_server.rs +++ b/proxy_components/mock-engine-store/src/mock_store/mock_engine_store_server.rs @@ -1,7 +1,10 @@ // Copyright 2022 TiKV Project Authors. Licensed under Apache-2.0. -use std::{cell::RefCell, pin::Pin, sync::atomic::Ordering}; -use std::sync::Mutex; +use std::{ + cell::RefCell, + pin::Pin, + sync::{atomic::Ordering, Mutex}, +}; use engine_store_ffi::TiFlashEngine; @@ -27,14 +30,10 @@ pub struct ThreadInfoJealloc { impl ThreadInfoJealloc { pub fn allocated(&self) -> u64 { - unsafe { - *(self.allocated_ptr as *const u64) - } + unsafe { *(self.allocated_ptr as *const u64) } } pub fn deallocated(&self) -> u64 { - unsafe { - *(self.deallocated_ptr as *const u64) - } + unsafe { *(self.deallocated_ptr as *const u64) } } pub fn remaining(&self) -> i64 { self.allocated() as i64 - self.deallocated() as i64 @@ -643,7 +642,12 @@ unsafe extern "C" fn ffi_report_thread_allocate_info( ) { let store = into_engine_store_server_wrap(arg1); let tn = std::str::from_utf8(name.to_slice()).unwrap().to_string(); - match (*store.engine_store_server).thread_info_map.lock().expect("poisoned").entry(tn) { + match (*store.engine_store_server) + .thread_info_map + .lock() + .expect("poisoned") + .entry(tn) + { std::collections::hash_map::Entry::Occupied(mut o) => { if t == 0 { o.get_mut().allocated_ptr = value; @@ -655,15 +659,14 @@ unsafe extern "C" fn ffi_report_thread_allocate_info( if t == 0 { v.insert(ThreadInfoJealloc { allocated_ptr: value, - deallocated_ptr: 0 + deallocated_ptr: 0, }); } else { v.insert(ThreadInfoJealloc { allocated_ptr: 0, - deallocated_ptr: value + deallocated_ptr: value, }); } } - } -} \ No newline at end of file +} diff --git a/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs b/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs index 61683a18a06..bf2b96b2ab5 100644 --- a/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs +++ b/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs @@ -1,6 +1,5 @@ // Copyright 2016 TiKV Project Authors. Licensed under Apache-2.0. -use std::pin::Pin; -use std::cell::RefCell; +use std::{cell::RefCell, pin::Pin}; use kvproto::{kvrpcpb, metapb, raft_cmdpb}; @@ -60,18 +59,21 @@ impl EngineStoreServerHelper { unsafe { let ptr_alloc: u64 = crate::jemalloc_utils::get_allocatep_on_thread_start(); let ptr_dealloc: u64 = crate::jemalloc_utils::get_deallocatep_on_thread_start(); - let thread_name = std::thread::current().name().unwrap_or("").to_string(); + let thread_name = std::thread::current() + .name() + .unwrap_or("") + .to_string(); (self.fn_report_thread_allocate_info.into_inner())( self.inner, BaseBuffView::from(thread_name.as_bytes()), 0, - ptr_alloc + ptr_alloc, ); (self.fn_report_thread_allocate_info.into_inner())( self.inner, BaseBuffView::from(thread_name.as_bytes()), 1, - ptr_dealloc + ptr_dealloc, ); } *(b.borrow_mut()) = true; diff --git a/proxy_components/proxy_ffi/src/jemalloc_utils.rs b/proxy_components/proxy_ffi/src/jemalloc_utils.rs new file mode 100644 index 00000000000..c7abe134b13 --- /dev/null +++ b/proxy_components/proxy_ffi/src/jemalloc_utils.rs @@ -0,0 +1,70 @@ +// Copyright 2024 TiKV Project Authors. Licensed under Apache-2.0. + +extern "C" { + // External jemalloc + pub fn mallctl( + name: *const ::std::os::raw::c_char, + oldp: *mut ::std::os::raw::c_void, + oldlenp: *mut u64, + newp: *mut ::std::os::raw::c_void, + newlen: u64, + ) -> ::std::os::raw::c_int; + + // Embeded jemalloc + pub fn _rjem_mallctl( + name: *const ::std::os::raw::c_char, + oldp: *mut ::std::os::raw::c_void, + oldlenp: *mut u64, + newp: *mut ::std::os::raw::c_void, + newlen: u64, + ) -> ::std::os::raw::c_int; +} + +pub fn get_allocatep_on_thread_start() -> u64 { + type PtrUnderlying = u64; + let mut ptr: PtrUnderlying = 0; + let mut size = std::mem::size_of::() as u64; + let c_str = std::ffi::CString::new("thread.allocatedp").unwrap(); + let c_ptr: *const ::std::os::raw::c_char = c_str.as_ptr() as *const ::std::os::raw::c_char; + unsafe { + _rjem_mallctl( + c_ptr, + &mut ptr as *mut _ as *mut ::std::os::raw::c_void, + &mut size as *mut u64, + std::ptr::null_mut(), + 0, + ); + } + return ptr; +} + +pub fn get_deallocatep_on_thread_start() -> u64 { + type PtrUnderlying = u64; + let mut ptr: PtrUnderlying = 0; + let mut size = std::mem::size_of::() as u64; + let c_str = std::ffi::CString::new("thread.deallocatedp").unwrap(); + let c_ptr: *const ::std::os::raw::c_char = c_str.as_ptr() as *const ::std::os::raw::c_char; + unsafe { + #[cfg(any(test, feature = "testexport"))] + _rjem_mallctl( + c_ptr, + &mut ptr as *mut _ as *mut ::std::os::raw::c_void, + &mut size as *mut u64, + std::ptr::null_mut(), + 0, + ); + + #[cfg(not(any(test, feature = "testexport")))] + { + #[cfg(feature = "external-jemalloc")] + mallctl( + c_ptr, + &mut ptr as *mut _ as *mut ::std::os::raw::c_void, + &mut size as *mut u64, + std::ptr::null_mut(), + 0, + ); + } + } + return ptr; +} diff --git a/proxy_components/proxy_ffi/src/lib.rs b/proxy_components/proxy_ffi/src/lib.rs index a9c02acefb0..313f10b7539 100644 --- a/proxy_components/proxy_ffi/src/lib.rs +++ b/proxy_components/proxy_ffi/src/lib.rs @@ -21,9 +21,9 @@ pub mod raftstore_proxy; pub mod raftstore_proxy_helper_impls; pub mod read_index_helper; // FFI releated with reading from SST/RocksDB files. +pub mod jemalloc_utils; pub mod snapshot_reader_impls; pub mod utils; -pub mod jemalloc_utils; pub use self::{ basic_ffi_impls::*, domain_impls::*, encryption_impls::*, engine_store_helper_impls::*, diff --git a/proxy_components/proxy_server/src/run.rs b/proxy_components/proxy_server/src/run.rs index 4a2ea4a271d..d6bc3694f85 100644 --- a/proxy_components/proxy_server/src/run.rs +++ b/proxy_components/proxy_server/src/run.rs @@ -622,7 +622,7 @@ impl TiKvServer { info!("linked with external jemalloc"); #[cfg(not(feature = "external-jemalloc"))] info!("linked without external jemalloc"); - + // Initialize and check config info!("using proxy config"; "config" => ?proxy_config); crate::config::address_proxy_config(&mut config, &proxy_config); diff --git a/proxy_tests/proxy/shared/jemalloc.rs b/proxy_tests/proxy/shared/jemalloc.rs new file mode 100644 index 00000000000..6ff6f0faf7b --- /dev/null +++ b/proxy_tests/proxy/shared/jemalloc.rs @@ -0,0 +1,78 @@ +// Copyright 2024 TiKV Project Authors. Licensed under Apache-2.0. + +use collections::HashMap; +use mock_engine_store::ThreadInfoJealloc; +use more_asserts::assert_gt; +use proxy_ffi::jemalloc_utils::{get_allocatep_on_thread_start, get_deallocatep_on_thread_start}; + +use crate::utils::v1::*; + +#[test] +fn test_alloc_dealloc() { + let dummy: Vec = Vec::with_capacity(100000); + let ptr_alloc = get_allocatep_on_thread_start(); + let actual_alloc: &u64 = unsafe { &*(ptr_alloc as *const u64) }; + let ptr_dealloc = get_deallocatep_on_thread_start(); + let actual_dealloc: &u64 = unsafe { &*(ptr_dealloc as *const u64) }; + assert_gt!(*actual_alloc, 100000 * std::mem::size_of::() as u64); + let dummy2: Vec = Vec::with_capacity(100000); + assert_gt!( + *actual_alloc, + 2 * 100000 * std::mem::size_of::() as u64 + ); + drop(dummy); + assert_gt!(*actual_dealloc, 100000 * std::mem::size_of::() as u64); + drop(dummy2); + assert_gt!( + *actual_dealloc, + 2 * 100000 * std::mem::size_of::() as u64 + ); +} + +fn collect_thread_state( + cluster_ext: &ClusterExt, + store_id: u64, +) -> HashMap { + let mut res: HashMap = Default::default(); + cluster_ext.iter_ffi_helpers(Some(vec![store_id]), &mut |_, ffi: &mut FFIHelperSet| { + res = (*ffi.engine_store_server.thread_info_map.lock().expect("")).clone(); + }); + res +} + +fn gather(m: &HashMap, pattern: &str) -> i64 { + m.iter() + .filter(|(k, _)| k.contains(pattern)) + .fold(0, |acc, e| acc + e.1.remaining()) +} + +#[test] +fn test_ffi() { + let (mut cluster, _pd_client) = new_mock_cluster(0, 1); + + let _ = cluster.run(); + + let prev = collect_thread_state(&cluster.cluster_ext, 1); + let before_raftstore = gather(&prev, "raftstore"); + let before_apply = gather(&prev, "apply"); + + for i in 0..10 { + let k = format!("k{}", i); + let v = format!("v{}", i); + cluster.must_put(k.as_bytes(), v.as_bytes()); + } + + for i in 0..10 { + let k = format!("k{}", i); + let v = format!("v{}", i); + check_key(&cluster, k.as_bytes(), v.as_bytes(), Some(true), None, None); + } + + let after = collect_thread_state(&cluster.cluster_ext, 1); + let after_raftstore = gather(&after, "raftstore"); + let after_apply = gather(&after, "apply"); + assert_gt!(after_raftstore, before_raftstore); + assert_gt!(after_apply, before_apply); + + cluster.shutdown(); +} diff --git a/proxy_tests/proxy/shared/mod.rs b/proxy_tests/proxy/shared/mod.rs index b094adebf7b..3fbdcfe8053 100644 --- a/proxy_tests/proxy/shared/mod.rs +++ b/proxy_tests/proxy/shared/mod.rs @@ -6,6 +6,7 @@ mod engine; mod fast_add_peer; mod ffi; mod ingest; +mod jemalloc; mod mock; mod normal; mod region; @@ -14,4 +15,3 @@ mod server_cluster_test; mod snapshot; mod store; mod write; -mod jemalloc; From a80c17278b7f30f874f121945b4f06d26582cda8 Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Tue, 12 Mar 2024 16:51:55 +0800 Subject: [PATCH 03/26] fix Signed-off-by: CalvinNeo --- .../proxy_ffi/src/jemalloc_utils.rs | 34 +++++++------------ 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/proxy_components/proxy_ffi/src/jemalloc_utils.rs b/proxy_components/proxy_ffi/src/jemalloc_utils.rs index c7abe134b13..ed05a0cb6ba 100644 --- a/proxy_components/proxy_ffi/src/jemalloc_utils.rs +++ b/proxy_components/proxy_ffi/src/jemalloc_utils.rs @@ -10,7 +10,7 @@ extern "C" { newlen: u64, ) -> ::std::os::raw::c_int; - // Embeded jemalloc + // Embedded jemalloc pub fn _rjem_mallctl( name: *const ::std::os::raw::c_char, oldp: *mut ::std::os::raw::c_void, @@ -20,29 +20,11 @@ extern "C" { ) -> ::std::os::raw::c_int; } -pub fn get_allocatep_on_thread_start() -> u64 { +fn issue_mallctl(command: &str) -> u64{ type PtrUnderlying = u64; let mut ptr: PtrUnderlying = 0; let mut size = std::mem::size_of::() as u64; - let c_str = std::ffi::CString::new("thread.allocatedp").unwrap(); - let c_ptr: *const ::std::os::raw::c_char = c_str.as_ptr() as *const ::std::os::raw::c_char; - unsafe { - _rjem_mallctl( - c_ptr, - &mut ptr as *mut _ as *mut ::std::os::raw::c_void, - &mut size as *mut u64, - std::ptr::null_mut(), - 0, - ); - } - return ptr; -} - -pub fn get_deallocatep_on_thread_start() -> u64 { - type PtrUnderlying = u64; - let mut ptr: PtrUnderlying = 0; - let mut size = std::mem::size_of::() as u64; - let c_str = std::ffi::CString::new("thread.deallocatedp").unwrap(); + let c_str = std::ffi::CString::new(command).unwrap(); let c_ptr: *const ::std::os::raw::c_char = c_str.as_ptr() as *const ::std::os::raw::c_char; unsafe { #[cfg(any(test, feature = "testexport"))] @@ -66,5 +48,13 @@ pub fn get_deallocatep_on_thread_start() -> u64 { ); } } - return ptr; + ptr +} + +pub fn get_allocatep_on_thread_start() -> u64 { + issue_mallctl("thread.allocatedp") +} + +pub fn get_deallocatep_on_thread_start() -> u64 { + issue_mallctl("thread.deallocatedp") } From 42b5bc7b77e7a32590d4544e2593ed57040461d8 Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Tue, 12 Mar 2024 16:55:40 +0800 Subject: [PATCH 04/26] fix Signed-off-by: CalvinNeo --- proxy_scripts/ci_check.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/proxy_scripts/ci_check.sh b/proxy_scripts/ci_check.sh index a38316b1afd..1c9fe0859a6 100755 --- a/proxy_scripts/ci_check.sh +++ b/proxy_scripts/ci_check.sh @@ -61,6 +61,7 @@ elif [[ $M == "testnew" ]]; then cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::replica_read -- --test-threads 1 cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::ffi -- --test-threads 1 cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::write --features="proxy_tests/enable-pagestorage" + cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::jemalloc --features="jemalloc" # We don't support snapshot test for PS, since it don't support trait Snapshot. elif [[ $M == "debug" ]]; then # export RUSTC_WRAPPER=~/.cargo/bin/sccache From 9caf719addb848bb748b98dddfbb0db18f2eb1da Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Tue, 12 Mar 2024 17:09:25 +0800 Subject: [PATCH 05/26] fix2 Signed-off-by: CalvinNeo --- proxy_components/proxy_ffi/src/jemalloc_utils.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/proxy_components/proxy_ffi/src/jemalloc_utils.rs b/proxy_components/proxy_ffi/src/jemalloc_utils.rs index ed05a0cb6ba..f6c3dceb444 100644 --- a/proxy_components/proxy_ffi/src/jemalloc_utils.rs +++ b/proxy_components/proxy_ffi/src/jemalloc_utils.rs @@ -20,6 +20,9 @@ extern "C" { ) -> ::std::os::raw::c_int; } +#[allow(unused_variables)] +#[allow(unused_mut)] +#[allow(unused_unsafe)] fn issue_mallctl(command: &str) -> u64{ type PtrUnderlying = u64; let mut ptr: PtrUnderlying = 0; From 567aed7cd0ce8000ffebeef67a8f267dafbd02fa Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Tue, 12 Mar 2024 17:13:37 +0800 Subject: [PATCH 06/26] fix4 Signed-off-by: CalvinNeo --- raftstore-proxy/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/raftstore-proxy/Cargo.toml b/raftstore-proxy/Cargo.toml index 1916b7b6228..4141686abe2 100644 --- a/raftstore-proxy/Cargo.toml +++ b/raftstore-proxy/Cargo.toml @@ -35,6 +35,7 @@ backup-stream-debug = ["proxy_server/backup-stream-debug"] pprof-fp = ["proxy_server/pprof-fp"] openssl-vendored = ["proxy_server/openssl-vendored"] +external-jemalloc = ["proxy_server/external-jemalloc"] [lib] name = "raftstore_proxy" From 7013718bc91e4813d89be8f92b62f8f4352740dd Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Tue, 12 Mar 2024 18:29:13 +0800 Subject: [PATCH 07/26] fmt Signed-off-by: CalvinNeo --- proxy_components/proxy_ffi/src/jemalloc_utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proxy_components/proxy_ffi/src/jemalloc_utils.rs b/proxy_components/proxy_ffi/src/jemalloc_utils.rs index f6c3dceb444..28c668c4c2b 100644 --- a/proxy_components/proxy_ffi/src/jemalloc_utils.rs +++ b/proxy_components/proxy_ffi/src/jemalloc_utils.rs @@ -23,7 +23,7 @@ extern "C" { #[allow(unused_variables)] #[allow(unused_mut)] #[allow(unused_unsafe)] -fn issue_mallctl(command: &str) -> u64{ +fn issue_mallctl(command: &str) -> u64 { type PtrUnderlying = u64; let mut ptr: PtrUnderlying = 0; let mut size = std::mem::size_of::() as u64; From d23225eb9f7750acad1ac7499726638adb007d32 Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Tue, 12 Mar 2024 20:15:36 +0800 Subject: [PATCH 08/26] x Signed-off-by: CalvinNeo --- proxy_scripts/ci_check.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/proxy_scripts/ci_check.sh b/proxy_scripts/ci_check.sh index 1c9fe0859a6..6c00be5c3f6 100755 --- a/proxy_scripts/ci_check.sh +++ b/proxy_scripts/ci_check.sh @@ -1,4 +1,6 @@ set -uxeo pipefail +cat /etc/issue +cat /proc/version if [[ $M == "fmt" ]]; then pwd git rev-parse --show-toplevel @@ -39,6 +41,7 @@ elif [[ $M == "testnew" ]]; then export ENABLE_FEATURES="test-engine-kv-rocksdb test-engine-raft-raft-engine openssl-vendored" cargo check --package proxy_server --features="$ENABLE_FEATURES" # tests based on mock-engine-store, with compat for new proxy + cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::jemalloc --features="jemalloc" cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::write cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::snapshot cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::store @@ -61,7 +64,6 @@ elif [[ $M == "testnew" ]]; then cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::replica_read -- --test-threads 1 cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::ffi -- --test-threads 1 cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::write --features="proxy_tests/enable-pagestorage" - cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::jemalloc --features="jemalloc" # We don't support snapshot test for PS, since it don't support trait Snapshot. elif [[ $M == "debug" ]]; then # export RUSTC_WRAPPER=~/.cargo/bin/sccache From b9d84c0c589782e4a7638b137842e1ab7a3bc0dd Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Tue, 12 Mar 2024 20:28:00 +0800 Subject: [PATCH 09/26] make env Signed-off-by: CalvinNeo --- proxy_scripts/ci_check.sh | 3 +++ proxy_scripts/make_env.sh | 6 ++++++ 2 files changed, 9 insertions(+) create mode 100644 proxy_scripts/make_env.sh diff --git a/proxy_scripts/ci_check.sh b/proxy_scripts/ci_check.sh index 6c00be5c3f6..88aa58cb81e 100755 --- a/proxy_scripts/ci_check.sh +++ b/proxy_scripts/ci_check.sh @@ -1,6 +1,7 @@ set -uxeo pipefail cat /etc/issue cat /proc/version + if [[ $M == "fmt" ]]; then pwd git rev-parse --show-toplevel @@ -36,6 +37,8 @@ elif [[ $M == "testold" ]]; then # cargo test --package tests --test failpoints cases::test_snap cargo test --package tests --test failpoints cases::test_import_service elif [[ $M == "testnew" ]]; then + chmod +x ./proxy_scripts/make_env.sh + ./proxy_scripts/make_env.sh export ENGINE_LABEL_VALUE=tiflash export RUST_BACKTRACE=full export ENABLE_FEATURES="test-engine-kv-rocksdb test-engine-raft-raft-engine openssl-vendored" diff --git a/proxy_scripts/make_env.sh b/proxy_scripts/make_env.sh new file mode 100644 index 00000000000..9884b991db0 --- /dev/null +++ b/proxy_scripts/make_env.sh @@ -0,0 +1,6 @@ +wget https://github.com/jemalloc/jemalloc/releases/download/5.2.1/jemalloc-5.2.1.tar.bz2 +tar -xvf jemalloc-5.2.1.tar.bz2 +cd jemalloc-5.2.1 +./configure +make +make install \ No newline at end of file From f95ca62c82ef7ae6a57a0c420fe88186540d0510 Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Tue, 12 Mar 2024 22:15:54 +0800 Subject: [PATCH 10/26] log Signed-off-by: CalvinNeo --- proxy_scripts/ci_check.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/proxy_scripts/ci_check.sh b/proxy_scripts/ci_check.sh index 88aa58cb81e..12761868331 100755 --- a/proxy_scripts/ci_check.sh +++ b/proxy_scripts/ci_check.sh @@ -1,6 +1,8 @@ set -uxeo pipefail cat /etc/issue cat /proc/version +echo "LD_LIBRARY_PATH=", $LD_LIBRARY_PATH +echo "PATH=", $PATH if [[ $M == "fmt" ]]; then pwd @@ -39,6 +41,7 @@ elif [[ $M == "testold" ]]; then elif [[ $M == "testnew" ]]; then chmod +x ./proxy_scripts/make_env.sh ./proxy_scripts/make_env.sh + export /usr/local/lib/ export ENGINE_LABEL_VALUE=tiflash export RUST_BACKTRACE=full export ENABLE_FEATURES="test-engine-kv-rocksdb test-engine-raft-raft-engine openssl-vendored" @@ -66,7 +69,7 @@ elif [[ $M == "testnew" ]]; then cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::fast_add_peer cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::replica_read -- --test-threads 1 cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::ffi -- --test-threads 1 - cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::write --features="proxy_tests/enable-pagestorage" + LD_LIBRARY_PATH=/usr/local/lib cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::write --features="proxy_tests/enable-pagestorage" # We don't support snapshot test for PS, since it don't support trait Snapshot. elif [[ $M == "debug" ]]; then # export RUSTC_WRAPPER=~/.cargo/bin/sccache @@ -76,3 +79,4 @@ elif [[ $M == "release" ]]; then export ENGINE_LABEL_VALUE=tiflash make release fi + \ No newline at end of file From 25021279347531ac06bdec900abba23b0c56c42b Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Tue, 12 Mar 2024 22:57:13 +0800 Subject: [PATCH 11/26] fix Signed-off-by: CalvinNeo --- proxy_scripts/ci_check.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proxy_scripts/ci_check.sh b/proxy_scripts/ci_check.sh index 12761868331..951cb756d29 100755 --- a/proxy_scripts/ci_check.sh +++ b/proxy_scripts/ci_check.sh @@ -41,7 +41,7 @@ elif [[ $M == "testold" ]]; then elif [[ $M == "testnew" ]]; then chmod +x ./proxy_scripts/make_env.sh ./proxy_scripts/make_env.sh - export /usr/local/lib/ + export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/ export ENGINE_LABEL_VALUE=tiflash export RUST_BACKTRACE=full export ENABLE_FEATURES="test-engine-kv-rocksdb test-engine-raft-raft-engine openssl-vendored" From f8a49d930021c693fec3703bcee72d4a7dcf7f1a Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Wed, 13 Mar 2024 00:18:27 +0800 Subject: [PATCH 12/26] fix Signed-off-by: CalvinNeo --- proxy_scripts/ci_check.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/proxy_scripts/ci_check.sh b/proxy_scripts/ci_check.sh index 951cb756d29..3be3043156e 100755 --- a/proxy_scripts/ci_check.sh +++ b/proxy_scripts/ci_check.sh @@ -47,7 +47,7 @@ elif [[ $M == "testnew" ]]; then export ENABLE_FEATURES="test-engine-kv-rocksdb test-engine-raft-raft-engine openssl-vendored" cargo check --package proxy_server --features="$ENABLE_FEATURES" # tests based on mock-engine-store, with compat for new proxy - cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::jemalloc --features="jemalloc" + LD_LIBRARY_PATH=/usr/local/lib cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::jemalloc --features="jemalloc" cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::write cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::snapshot cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::store @@ -69,7 +69,7 @@ elif [[ $M == "testnew" ]]; then cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::fast_add_peer cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::replica_read -- --test-threads 1 cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::ffi -- --test-threads 1 - LD_LIBRARY_PATH=/usr/local/lib cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::write --features="proxy_tests/enable-pagestorage" + cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::write --features="proxy_tests/enable-pagestorage" # We don't support snapshot test for PS, since it don't support trait Snapshot. elif [[ $M == "debug" ]]; then # export RUSTC_WRAPPER=~/.cargo/bin/sccache From 9ec4e8f4b4ea37f515e3f9d868d5f3aecb84bb2b Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Wed, 13 Mar 2024 01:07:46 +0800 Subject: [PATCH 13/26] another test Signed-off-by: CalvinNeo --- proxy_scripts/ci_check.sh | 53 +++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/proxy_scripts/ci_check.sh b/proxy_scripts/ci_check.sh index 3be3043156e..65debb09860 100755 --- a/proxy_scripts/ci_check.sh +++ b/proxy_scripts/ci_check.sh @@ -13,41 +13,40 @@ if [[ $M == "fmt" ]]; then cargo fmt -- --check elif [[ $M == "testold" ]]; then export ENGINE_LABEL_VALUE=tiflash - export RUST_BACKTRACE=full - export ENABLE_FEATURES="test-engine-kv-rocksdb test-engine-raft-raft-engine" - echo "Start clippy" - chmod +x ./proxy_scripts/clippy.sh - ./proxy_scripts/clippy.sh - echo "Finish clippy" - chmod +x ./proxy_scripts/tikv-code-consistency.sh - ./proxy_scripts/tikv-code-consistency.sh - echo "Finish tikv code consistency" - exit # If we depend TiKV as a Cargo component, the following is not necessary, and can fail. - # TODO we have to let tests support openssl-vendored. - yum install openssl openssl-devel -y - cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_normal - cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_bootstrap - cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_compact_log - cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_early_apply - cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_encryption - # cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_pd_client - cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_pending_peers - cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_transaction - cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_cmd_epoch_checker - # cargo test --package tests --test failpoints cases::test_disk_full - # cargo test --package tests --test failpoints cases::test_merge -- --skip test_node_merge_restart --skip test_node_merge_catch_up_logs_no_need - # cargo test --package tests --test failpoints cases::test_snap - cargo test --package tests --test failpoints cases::test_import_service + # export RUST_BACKTRACE=full + # export ENABLE_FEATURES="test-engine-kv-rocksdb test-engine-raft-raft-engine" + # echo "Start clippy" + # chmod +x ./proxy_scripts/clippy.sh + # ./proxy_scripts/clippy.sh + # echo "Finish clippy" + # chmod +x ./proxy_scripts/tikv-code-consistency.sh + # ./proxy_scripts/tikv-code-consistency.sh + # echo "Finish tikv code consistency" + # exit # If we depend TiKV as a Cargo component, the following is not necessary, and can fail. + # # TODO we have to let tests support openssl-vendored. + # yum install openssl openssl-devel -y + # cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_normal + # cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_bootstrap + # cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_compact_log + # cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_early_apply + # cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_encryption + # # cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_pd_client + # cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_pending_peers + # cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_transaction + # cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_cmd_epoch_checker + # # cargo test --package tests --test failpoints cases::test_disk_full + # # cargo test --package tests --test failpoints cases::test_merge -- --skip test_node_merge_restart --skip test_node_merge_catch_up_logs_no_need + # # cargo test --package tests --test failpoints cases::test_snap + # cargo test --package tests --test failpoints cases::test_import_service elif [[ $M == "testnew" ]]; then chmod +x ./proxy_scripts/make_env.sh ./proxy_scripts/make_env.sh - export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/ export ENGINE_LABEL_VALUE=tiflash export RUST_BACKTRACE=full export ENABLE_FEATURES="test-engine-kv-rocksdb test-engine-raft-raft-engine openssl-vendored" cargo check --package proxy_server --features="$ENABLE_FEATURES" # tests based on mock-engine-store, with compat for new proxy - LD_LIBRARY_PATH=/usr/local/lib cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::jemalloc --features="jemalloc" + LD_LIBRARY_PATH=/usr/local/lib:/usr/local/lib/x86_64-unknown-linux-gnu/ cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::jemalloc --features="jemalloc" cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::write cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::snapshot cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::store From e67a03d983b50a02a6a48824b30993837d246b8e Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Wed, 13 Mar 2024 02:29:33 +0800 Subject: [PATCH 14/26] fix Signed-off-by: CalvinNeo --- .../proxy_ffi/src/jemalloc_utils.rs | 28 ++++++++--- proxy_scripts/ci_check.sh | 50 +++++++++---------- proxy_tests/Cargo.toml | 2 +- 3 files changed, 47 insertions(+), 33 deletions(-) diff --git a/proxy_components/proxy_ffi/src/jemalloc_utils.rs b/proxy_components/proxy_ffi/src/jemalloc_utils.rs index 28c668c4c2b..a2ba6dd7a1f 100644 --- a/proxy_components/proxy_ffi/src/jemalloc_utils.rs +++ b/proxy_components/proxy_ffi/src/jemalloc_utils.rs @@ -30,17 +30,31 @@ fn issue_mallctl(command: &str) -> u64 { let c_str = std::ffi::CString::new(command).unwrap(); let c_ptr: *const ::std::os::raw::c_char = c_str.as_ptr() as *const ::std::os::raw::c_char; unsafe { + // See unprefixed_malloc_on_supported_platforms in tikv-jemalloc-sys. #[cfg(any(test, feature = "testexport"))] - _rjem_mallctl( - c_ptr, - &mut ptr as *mut _ as *mut ::std::os::raw::c_void, - &mut size as *mut u64, - std::ptr::null_mut(), - 0, - ); + { + // See NO_UNPREFIXED_MALLOC + #[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "macos"))] + _rjem_mallctl( + c_ptr, + &mut ptr as *mut _ as *mut ::std::os::raw::c_void, + &mut size as *mut u64, + std::ptr::null_mut(), + 0, + ); + #[cfg(not(any(target_os = "android", target_os = "dragonfly", target_os = "macos")))] + mallctl( + c_ptr, + &mut ptr as *mut _ as *mut ::std::os::raw::c_void, + &mut size as *mut u64, + std::ptr::null_mut(), + 0, + ); + } #[cfg(not(any(test, feature = "testexport")))] { + // Must linked to tiflash. #[cfg(feature = "external-jemalloc")] mallctl( c_ptr, diff --git a/proxy_scripts/ci_check.sh b/proxy_scripts/ci_check.sh index 65debb09860..db25b421b1d 100755 --- a/proxy_scripts/ci_check.sh +++ b/proxy_scripts/ci_check.sh @@ -13,31 +13,31 @@ if [[ $M == "fmt" ]]; then cargo fmt -- --check elif [[ $M == "testold" ]]; then export ENGINE_LABEL_VALUE=tiflash - # export RUST_BACKTRACE=full - # export ENABLE_FEATURES="test-engine-kv-rocksdb test-engine-raft-raft-engine" - # echo "Start clippy" - # chmod +x ./proxy_scripts/clippy.sh - # ./proxy_scripts/clippy.sh - # echo "Finish clippy" - # chmod +x ./proxy_scripts/tikv-code-consistency.sh - # ./proxy_scripts/tikv-code-consistency.sh - # echo "Finish tikv code consistency" - # exit # If we depend TiKV as a Cargo component, the following is not necessary, and can fail. - # # TODO we have to let tests support openssl-vendored. - # yum install openssl openssl-devel -y - # cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_normal - # cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_bootstrap - # cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_compact_log - # cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_early_apply - # cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_encryption - # # cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_pd_client - # cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_pending_peers - # cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_transaction - # cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_cmd_epoch_checker - # # cargo test --package tests --test failpoints cases::test_disk_full - # # cargo test --package tests --test failpoints cases::test_merge -- --skip test_node_merge_restart --skip test_node_merge_catch_up_logs_no_need - # # cargo test --package tests --test failpoints cases::test_snap - # cargo test --package tests --test failpoints cases::test_import_service + export RUST_BACKTRACE=full + export ENABLE_FEATURES="test-engine-kv-rocksdb test-engine-raft-raft-engine" + echo "Start clippy" + chmod +x ./proxy_scripts/clippy.sh + ./proxy_scripts/clippy.sh + echo "Finish clippy" + chmod +x ./proxy_scripts/tikv-code-consistency.sh + ./proxy_scripts/tikv-code-consistency.sh + echo "Finish tikv code consistency" + exit # If we depend TiKV as a Cargo component, the following is not necessary, and can fail. + # TODO we have to let tests support openssl-vendored. + yum install openssl openssl-devel -y + cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_normal + cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_bootstrap + cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_compact_log + cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_early_apply + cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_encryption + # cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_pd_client + cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_pending_peers + cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_transaction + cargo test --features "$ENABLE_FEATURES" --package tests --test failpoints cases::test_cmd_epoch_checker + # cargo test --package tests --test failpoints cases::test_disk_full + # cargo test --package tests --test failpoints cases::test_merge -- --skip test_node_merge_restart --skip test_node_merge_catch_up_logs_no_need + # cargo test --package tests --test failpoints cases::test_snap + cargo test --package tests --test failpoints cases::test_import_service elif [[ $M == "testnew" ]]; then chmod +x ./proxy_scripts/make_env.sh ./proxy_scripts/make_env.sh diff --git a/proxy_tests/Cargo.toml b/proxy_tests/Cargo.toml index dcb366b9706..9646387c0ec 100644 --- a/proxy_tests/Cargo.toml +++ b/proxy_tests/Cargo.toml @@ -38,7 +38,7 @@ test-engine-raft-rocksdb = [ "engine_test/test-engine-raft-rocksdb" ] -jemalloc = ["tikv/jemalloc"] +jemalloc = ["tikv/jemalloc", "proxy_server/jemalloc"] mimalloc = ["tikv/mimalloc"] snmalloc = ["tikv/snmalloc"] mem-profiling = ["tikv/mem-profiling"] From 3a150e589f5fe99630e57f979494cebdfbb227b4 Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Wed, 13 Mar 2024 12:58:46 +0800 Subject: [PATCH 15/26] fix other tests Signed-off-by: CalvinNeo --- proxy_components/engine_store_ffi/Cargo.toml | 1 + proxy_components/proxy_ffi/Cargo.toml | 1 + .../proxy_ffi/src/jemalloc_utils.rs | 41 +++++++++++-------- proxy_components/proxy_server/Cargo.toml | 2 +- proxy_tests/Cargo.toml | 2 +- 5 files changed, 28 insertions(+), 19 deletions(-) diff --git a/proxy_components/engine_store_ffi/Cargo.toml b/proxy_components/engine_store_ffi/Cargo.toml index 2657cbf9b73..c04b4bcd9a4 100644 --- a/proxy_components/engine_store_ffi/Cargo.toml +++ b/proxy_components/engine_store_ffi/Cargo.toml @@ -32,6 +32,7 @@ openssl-vendored = [ "openssl/vendored" ] +jemalloc = ["proxy_ffi/jemalloc"] external-jemalloc = ["proxy_ffi/external-jemalloc"] [dependencies] diff --git a/proxy_components/proxy_ffi/Cargo.toml b/proxy_components/proxy_ffi/Cargo.toml index 4b75b4ce327..f4349a4eceb 100644 --- a/proxy_components/proxy_ffi/Cargo.toml +++ b/proxy_components/proxy_ffi/Cargo.toml @@ -22,6 +22,7 @@ test-engines-rocksdb = [ test-engines-panic = [ "engine_test/test-engines-panic", ] +jemalloc = [] # TODO use encryption/openssl-vendored if later supports openssl-vendored = [ diff --git a/proxy_components/proxy_ffi/src/jemalloc_utils.rs b/proxy_components/proxy_ffi/src/jemalloc_utils.rs index a2ba6dd7a1f..85c3f756526 100644 --- a/proxy_components/proxy_ffi/src/jemalloc_utils.rs +++ b/proxy_components/proxy_ffi/src/jemalloc_utils.rs @@ -33,23 +33,30 @@ fn issue_mallctl(command: &str) -> u64 { // See unprefixed_malloc_on_supported_platforms in tikv-jemalloc-sys. #[cfg(any(test, feature = "testexport"))] { - // See NO_UNPREFIXED_MALLOC - #[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "macos"))] - _rjem_mallctl( - c_ptr, - &mut ptr as *mut _ as *mut ::std::os::raw::c_void, - &mut size as *mut u64, - std::ptr::null_mut(), - 0, - ); - #[cfg(not(any(target_os = "android", target_os = "dragonfly", target_os = "macos")))] - mallctl( - c_ptr, - &mut ptr as *mut _ as *mut ::std::os::raw::c_void, - &mut size as *mut u64, - std::ptr::null_mut(), - 0, - ); + #[cfg(any(feature = "jemalloc"))] + { + // See NO_UNPREFIXED_MALLOC + #[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "macos"))] + _rjem_mallctl( + c_ptr, + &mut ptr as *mut _ as *mut ::std::os::raw::c_void, + &mut size as *mut u64, + std::ptr::null_mut(), + 0, + ); + #[cfg(not(any( + target_os = "android", + target_os = "dragonfly", + target_os = "macos" + )))] + mallctl( + c_ptr, + &mut ptr as *mut _ as *mut ::std::os::raw::c_void, + &mut size as *mut u64, + std::ptr::null_mut(), + 0, + ); + } } #[cfg(not(any(test, feature = "testexport")))] diff --git a/proxy_components/proxy_server/Cargo.toml b/proxy_components/proxy_server/Cargo.toml index 41b577ee77d..4296f870f63 100644 --- a/proxy_components/proxy_server/Cargo.toml +++ b/proxy_components/proxy_server/Cargo.toml @@ -7,7 +7,7 @@ publish = false [features] tcmalloc = ["tikv/tcmalloc"] -jemalloc = ["tikv/jemalloc"] +jemalloc = ["tikv/jemalloc", "engine_store_ffi/jemalloc"] mimalloc = ["tikv/mimalloc"] snmalloc = ["tikv/snmalloc"] portable = ["tikv/portable"] diff --git a/proxy_tests/Cargo.toml b/proxy_tests/Cargo.toml index 9646387c0ec..895832a8809 100644 --- a/proxy_tests/Cargo.toml +++ b/proxy_tests/Cargo.toml @@ -38,7 +38,7 @@ test-engine-raft-rocksdb = [ "engine_test/test-engine-raft-rocksdb" ] -jemalloc = ["tikv/jemalloc", "proxy_server/jemalloc"] +jemalloc = ["tikv/jemalloc", "proxy_server/jemalloc", "proxy_ffi/jemalloc"] mimalloc = ["tikv/mimalloc"] snmalloc = ["tikv/snmalloc"] mem-profiling = ["tikv/mem-profiling"] From c2fb45e29ee8d68f2c5329300c98a6988bacf51c Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Wed, 13 Mar 2024 14:34:32 +0800 Subject: [PATCH 16/26] fix Signed-off-by: CalvinNeo --- proxy_scripts/ci_check.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/proxy_scripts/ci_check.sh b/proxy_scripts/ci_check.sh index db25b421b1d..fb11fd1eafa 100755 --- a/proxy_scripts/ci_check.sh +++ b/proxy_scripts/ci_check.sh @@ -1,7 +1,7 @@ set -uxeo pipefail cat /etc/issue cat /proc/version -echo "LD_LIBRARY_PATH=", $LD_LIBRARY_PATH +echo "LD_LIBRARY_PATH=", ${LD_LIBRARY_PATH:-nil} echo "PATH=", $PATH if [[ $M == "fmt" ]]; then @@ -46,7 +46,7 @@ elif [[ $M == "testnew" ]]; then export ENABLE_FEATURES="test-engine-kv-rocksdb test-engine-raft-raft-engine openssl-vendored" cargo check --package proxy_server --features="$ENABLE_FEATURES" # tests based on mock-engine-store, with compat for new proxy - LD_LIBRARY_PATH=/usr/local/lib:/usr/local/lib/x86_64-unknown-linux-gnu/ cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::jemalloc --features="jemalloc" + LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::jemalloc --features="jemalloc" cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::write cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::snapshot cargo test --package proxy_tests --features="$ENABLE_FEATURES" --test proxy shared::store From 894d379e2291b7f71d419debdc65c703861b6fcc Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Wed, 13 Mar 2024 16:44:19 +0800 Subject: [PATCH 17/26] fix for upgrade ps_engine.raftlog Signed-off-by: CalvinNeo --- proxy_components/proxy_server/src/run.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/proxy_components/proxy_server/src/run.rs b/proxy_components/proxy_server/src/run.rs index d6bc3694f85..f1abd46f7fe 100644 --- a/proxy_components/proxy_server/src/run.rs +++ b/proxy_components/proxy_server/src/run.rs @@ -627,6 +627,18 @@ impl TiKvServer { info!("using proxy config"; "config" => ?proxy_config); crate::config::address_proxy_config(&mut config, &proxy_config); info!("after address config"; "config" => ?config); + + // NOTE: Compat disagg arch upgraded from * to 8.0. + { + let raft_engine_path = config.raft_engine.config().dir + "/ps_engine"; + let path = Path::new(&raft_engine_path); + if path.exists() { + info!("created ps_engine.raftlog for upgraded cluster"); + let new_raft_engine_path = config.raft_engine.config().dir + "/ps_engine.raftlog"; + File::create(new_raft_engine_path).unwrap(); + } + } + let cfg_controller = Self::init_config(config); let config = cfg_controller.get_current(); From 5914312bbc1208057abc260b87bc523d37de68af Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Wed, 13 Mar 2024 18:28:05 +0800 Subject: [PATCH 18/26] c Signed-off-by: CalvinNeo --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index fd5b6fbb061..f2dc2998277 100644 --- a/Makefile +++ b/Makefile @@ -196,6 +196,7 @@ dev: format clippy ifeq ($(PROXY_FRAME_POINTER),1) build: ENABLE_FEATURES += pprof-fp endif +build: ENABLE_FEATURES += external-jemalloc build: PROXY_ENABLE_FEATURES="${ENABLE_FEATURES}" ./proxy_scripts/build.sh From ef6fc3a7208dc83741da34e6641853ba6ca671b4 Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Wed, 13 Mar 2024 23:07:35 +0800 Subject: [PATCH 19/26] fix Signed-off-by: CalvinNeo --- .../src/mock_store/mock_engine_store_server.rs | 6 +++--- .../proxy_ffi/src/engine_store_helper_impls.rs | 10 ++++++++-- proxy_components/proxy_ffi/src/interfaces.rs | 11 +++++++++-- proxy_components/proxy_server/src/run.rs | 2 +- raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version | 2 +- raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h | 9 ++++++++- 6 files changed, 30 insertions(+), 10 deletions(-) diff --git a/proxy_components/mock-engine-store/src/mock_store/mock_engine_store_server.rs b/proxy_components/mock-engine-store/src/mock_store/mock_engine_store_server.rs index e49e88673f9..aa68e99c34b 100644 --- a/proxy_components/mock-engine-store/src/mock_store/mock_engine_store_server.rs +++ b/proxy_components/mock-engine-store/src/mock_store/mock_engine_store_server.rs @@ -637,7 +637,7 @@ unsafe extern "C" fn ffi_get_lock_by_key( unsafe extern "C" fn ffi_report_thread_allocate_info( arg1: *mut interfaces_ffi::EngineStoreServerWrap, name: interfaces_ffi::BaseBuffView, - t: u64, + t: interfaces_ffi::ReportThreadAllocateInfoType, value: u64, ) { let store = into_engine_store_server_wrap(arg1); @@ -649,14 +649,14 @@ unsafe extern "C" fn ffi_report_thread_allocate_info( .entry(tn) { std::collections::hash_map::Entry::Occupied(mut o) => { - if t == 0 { + if t == interfaces_ffi::ReportThreadAllocateInfoType::AllocPtr { o.get_mut().allocated_ptr = value; } else { o.get_mut().deallocated_ptr = value; } } std::collections::hash_map::Entry::Vacant(v) => { - if t == 0 { + if t == interfaces_ffi::ReportThreadAllocateInfoType::AllocPtr { v.insert(ThreadInfoJealloc { allocated_ptr: value, deallocated_ptr: 0, diff --git a/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs b/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs index bf2b96b2ab5..47d7fa10863 100644 --- a/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs +++ b/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs @@ -66,13 +66,19 @@ impl EngineStoreServerHelper { (self.fn_report_thread_allocate_info.into_inner())( self.inner, BaseBuffView::from(thread_name.as_bytes()), - 0, + interfaces_ffi::ReportThreadAllocateInfoType::Reset, ptr_alloc, ); (self.fn_report_thread_allocate_info.into_inner())( self.inner, BaseBuffView::from(thread_name.as_bytes()), - 1, + interfaces_ffi::ReportThreadAllocateInfoType::AllocPtr, + ptr_alloc, + ); + (self.fn_report_thread_allocate_info.into_inner())( + self.inner, + BaseBuffView::from(thread_name.as_bytes()), + interfaces_ffi::ReportThreadAllocateInfoType::DeallocPtr, ptr_dealloc, ); } diff --git a/proxy_components/proxy_ffi/src/interfaces.rs b/proxy_components/proxy_ffi/src/interfaces.rs index 7b84fdde853..ce81fb583cb 100644 --- a/proxy_components/proxy_ffi/src/interfaces.rs +++ b/proxy_components/proxy_ffi/src/interfaces.rs @@ -558,6 +558,13 @@ pub mod root { unsafe extern "C" fn(arg1: *const root::DB::EngineStoreServerWrap), >, } + #[repr(u64)] + #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] + pub enum ReportThreadAllocateInfoType { + Reset = 0, + AllocPtr = 1, + DeallocPtr = 2, + } #[repr(C)] #[derive(Debug)] pub struct EngineStoreServerHelper { @@ -754,7 +761,7 @@ pub mod root { unsafe extern "C" fn( arg1: *mut root::DB::EngineStoreServerWrap, name: root::DB::BaseBuffView, - type_: u64, + type_: root::DB::ReportThreadAllocateInfoType, value: u64, ), >, @@ -766,7 +773,7 @@ pub mod root { arg3: root::DB::RawVoidPtr, ) -> u32; } - pub const RAFT_STORE_PROXY_VERSION: u64 = 10285342393410618515; + pub const RAFT_STORE_PROXY_VERSION: u64 = 13437100439851870476; pub const RAFT_STORE_PROXY_MAGIC_NUMBER: u32 = 324508639; } } diff --git a/proxy_components/proxy_server/src/run.rs b/proxy_components/proxy_server/src/run.rs index f1abd46f7fe..52744618796 100644 --- a/proxy_components/proxy_server/src/run.rs +++ b/proxy_components/proxy_server/src/run.rs @@ -635,7 +635,7 @@ impl TiKvServer { if path.exists() { info!("created ps_engine.raftlog for upgraded cluster"); let new_raft_engine_path = config.raft_engine.config().dir + "/ps_engine.raftlog"; - File::create(new_raft_engine_path).unwrap(); + std::fs::File::create(new_raft_engine_path).unwrap(); } } diff --git a/raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version b/raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version index 64ac68ec2ee..efc42c40334 100644 --- a/raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version +++ b/raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version @@ -1,3 +1,3 @@ #pragma once #include -namespace DB { constexpr uint64_t RAFT_STORE_PROXY_VERSION = 10285342393410618515ull; } \ No newline at end of file +namespace DB { constexpr uint64_t RAFT_STORE_PROXY_VERSION = 13437100439851870476ull; } \ No newline at end of file diff --git a/raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h b/raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h index 27dedc79370..87c51219de7 100644 --- a/raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h +++ b/raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h @@ -311,6 +311,12 @@ struct PageStorageInterfaces { void (*fn_handle_purge_ps)(const EngineStoreServerWrap *); }; +enum class ReportThreadAllocateInfoType : uint64_t { + Reset = 0, + AllocPtr, + DeallocPtr, +}; + struct EngineStoreServerHelper { uint32_t magic_number; // use a very special number to check whether this // struct is legal @@ -374,7 +380,8 @@ struct EngineStoreServerHelper { void (*fn_clear_fap_snapshot)(EngineStoreServerWrap *, uint64_t region_id); bool (*fn_kvstore_region_exists)(EngineStoreServerWrap *, uint64_t region_id); void (*fn_report_thread_allocate_info)(EngineStoreServerWrap *, - BaseBuffView name, uint64_t type, + BaseBuffView name, + ReportThreadAllocateInfoType type, uint64_t value); }; From adc45570663f42745a2a1f9b24389df6e07c596e Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Thu, 14 Mar 2024 01:03:23 +0800 Subject: [PATCH 20/26] z Signed-off-by: CalvinNeo --- .../src/core/forward_raft/region.rs | 4 ++ .../mock_store/mock_engine_store_server.rs | 4 +- .../src/engine_store_helper_impls.rs | 54 +++++++++++++++++++ proxy_components/proxy_ffi/src/interfaces.rs | 4 +- .../proxy_ffi/src/jemalloc_utils.rs | 8 +++ .../ffi/src/RaftStoreProxyFFI/@version | 2 +- .../ffi/src/RaftStoreProxyFFI/ProxyFFI.h | 2 + 7 files changed, 74 insertions(+), 4 deletions(-) diff --git a/proxy_components/engine_store_ffi/src/core/forward_raft/region.rs b/proxy_components/engine_store_ffi/src/core/forward_raft/region.rs index 79b12513439..12c61ddfb66 100644 --- a/proxy_components/engine_store_ffi/src/core/forward_raft/region.rs +++ b/proxy_components/engine_store_ffi/src/core/forward_raft/region.rs @@ -15,6 +15,8 @@ impl ProxyForwarder { pub fn on_region_changed(&self, ob_region: &Region, e: RegionChangeEvent, r: StateRole) { self.engine_store_server_helper .maybe_jemalloc_register_alloc(); + self.engine_store_server_helper + .directly_report_jemalloc_alloc(); let region_id = ob_region.get_id(); if e == RegionChangeEvent::Destroy { info!( @@ -108,6 +110,8 @@ impl ProxyForwarder { pub fn on_role_change(&self, ob_region: &Region, r: &RoleChange) { self.engine_store_server_helper .maybe_jemalloc_register_alloc(); + self.engine_store_server_helper + .directly_report_jemalloc_alloc(); let region_id = ob_region.get_id(); let is_replicated = !r.initialized; let is_fap_enabled = if let Some(b) = self.engine.proxy_ext.config_set.as_ref() { diff --git a/proxy_components/mock-engine-store/src/mock_store/mock_engine_store_server.rs b/proxy_components/mock-engine-store/src/mock_store/mock_engine_store_server.rs index aa68e99c34b..0cd00a26d56 100644 --- a/proxy_components/mock-engine-store/src/mock_store/mock_engine_store_server.rs +++ b/proxy_components/mock-engine-store/src/mock_store/mock_engine_store_server.rs @@ -651,7 +651,7 @@ unsafe extern "C" fn ffi_report_thread_allocate_info( std::collections::hash_map::Entry::Occupied(mut o) => { if t == interfaces_ffi::ReportThreadAllocateInfoType::AllocPtr { o.get_mut().allocated_ptr = value; - } else { + } else if t == interfaces_ffi::ReportThreadAllocateInfoType::DeallocPtr { o.get_mut().deallocated_ptr = value; } } @@ -661,7 +661,7 @@ unsafe extern "C" fn ffi_report_thread_allocate_info( allocated_ptr: value, deallocated_ptr: 0, }); - } else { + } else if t == interfaces_ffi::ReportThreadAllocateInfoType::DeallocPtr { v.insert(ThreadInfoJealloc { allocated_ptr: 0, deallocated_ptr: value, diff --git a/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs b/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs index 47d7fa10863..f6ab4b239c5 100644 --- a/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs +++ b/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs @@ -36,6 +36,9 @@ pub fn gen_engine_store_server_helper( thread_local! { pub static JEMALLOC_REGISTERED: RefCell = RefCell::new(false); + pub static JEMALLOC_TNAME: RefCell = RefCell::new(Default::default()); + pub static JEMALLOC_ALLOCP: RefCell<*mut u64> = RefCell::new(std::ptr::null_mut()); + pub static JEMALLOC_DEALLOCP: RefCell<*mut u64> = RefCell::new(std::ptr::null_mut()); } /// # Safety @@ -81,12 +84,56 @@ impl EngineStoreServerHelper { interfaces_ffi::ReportThreadAllocateInfoType::DeallocPtr, ptr_dealloc, ); + // Some threads are not everlasting, so we don't want TiFlash to directly access + // the pointer. + JEMALLOC_TNAME.with(|p| { + *p.borrow_mut() = thread_name; + }); + if ptr_alloc != 0 { + JEMALLOC_ALLOCP.with(|p| { + *p.borrow_mut() = ptr_alloc as *mut u64; + }); + } + if ptr_dealloc != 0 { + JEMALLOC_DEALLOCP.with(|p| { + *p.borrow_mut() = ptr_dealloc as *mut u64; + }); + } } *(b.borrow_mut()) = true; } }); } + pub fn directly_report_jemalloc_alloc(&self) { + JEMALLOC_TNAME.with(|thread_name| { + JEMALLOC_ALLOCP.with(|p| unsafe { + let p = *p.borrow_mut(); + if p == std::ptr::null_mut() { + return; + } + (self.fn_report_thread_allocate_info.into_inner())( + self.inner, + BaseBuffView::from(thread_name.borrow().as_bytes()), + interfaces_ffi::ReportThreadAllocateInfoType::Alloc, + *p, + ); + }); + JEMALLOC_DEALLOCP.with(|p| unsafe { + let p = *p.borrow_mut(); + if p == std::ptr::null_mut() { + return; + } + (self.fn_report_thread_allocate_info.into_inner())( + self.inner, + BaseBuffView::from(thread_name.borrow().as_bytes()), + interfaces_ffi::ReportThreadAllocateInfoType::Dealloc, + *p, + ); + }); + }); + } + pub fn gc_raw_cpp_ptr(&self, ptr: *mut ::std::os::raw::c_void, tp: RawCppPtrType) { debug_assert!(self.fn_gc_raw_cpp_ptr.is_some()); unsafe { @@ -131,18 +178,21 @@ impl EngineStoreServerHelper { ) -> EngineStoreApplyRes { debug_assert!(self.fn_handle_write_raft_cmd.is_some()); self.maybe_jemalloc_register_alloc(); + self.directly_report_jemalloc_alloc(); unsafe { (self.fn_handle_write_raft_cmd.into_inner())(self.inner, cmds.gen_view(), header) } } pub fn handle_get_engine_store_server_status(&self) -> EngineStoreServerStatus { debug_assert!(self.fn_handle_get_engine_store_server_status.is_some()); self.maybe_jemalloc_register_alloc(); + self.directly_report_jemalloc_alloc(); unsafe { (self.fn_handle_get_engine_store_server_status.into_inner())(self.inner) } } pub fn handle_set_proxy(&self, proxy: *const RaftStoreProxyFFIHelper) { debug_assert!(self.fn_atomic_update_proxy.is_some()); self.maybe_jemalloc_register_alloc(); + self.directly_report_jemalloc_alloc(); unsafe { (self.fn_atomic_update_proxy.into_inner())(self.inner, proxy as *mut _) } } @@ -172,6 +222,7 @@ impl EngineStoreServerHelper { ) -> EngineStoreApplyRes { debug_assert!(self.fn_handle_admin_raft_cmd.is_some()); self.maybe_jemalloc_register_alloc(); + self.directly_report_jemalloc_alloc(); unsafe { let req = ProtoMsgBaseBuff::new(req); let resp = ProtoMsgBaseBuff::new(resp); @@ -201,6 +252,7 @@ impl EngineStoreServerHelper { ) -> bool { debug_assert!(self.fn_try_flush_data.is_some()); self.maybe_jemalloc_register_alloc(); + self.directly_report_jemalloc_alloc(); // TODO(proactive flush) unsafe { (self.fn_try_flush_data.into_inner())( @@ -324,6 +376,7 @@ impl EngineStoreServerHelper { debug_assert!(self.fn_handle_ingest_sst.is_some()); self.maybe_jemalloc_register_alloc(); + self.directly_report_jemalloc_alloc(); let snaps_view = into_sst_views(snaps); unsafe { (self.fn_handle_ingest_sst.into_inner())( @@ -338,6 +391,7 @@ impl EngineStoreServerHelper { debug_assert!(self.fn_handle_destroy.is_some()); self.maybe_jemalloc_register_alloc(); + self.directly_report_jemalloc_alloc(); unsafe { (self.fn_handle_destroy.into_inner())(self.inner, region_id); } diff --git a/proxy_components/proxy_ffi/src/interfaces.rs b/proxy_components/proxy_ffi/src/interfaces.rs index ce81fb583cb..27297048713 100644 --- a/proxy_components/proxy_ffi/src/interfaces.rs +++ b/proxy_components/proxy_ffi/src/interfaces.rs @@ -564,6 +564,8 @@ pub mod root { Reset = 0, AllocPtr = 1, DeallocPtr = 2, + Alloc = 3, + Dealloc = 4, } #[repr(C)] #[derive(Debug)] @@ -773,7 +775,7 @@ pub mod root { arg3: root::DB::RawVoidPtr, ) -> u32; } - pub const RAFT_STORE_PROXY_VERSION: u64 = 13437100439851870476; + pub const RAFT_STORE_PROXY_VERSION: u64 = 1764984388775075973; pub const RAFT_STORE_PROXY_MAGIC_NUMBER: u32 = 324508639; } } diff --git a/proxy_components/proxy_ffi/src/jemalloc_utils.rs b/proxy_components/proxy_ffi/src/jemalloc_utils.rs index 85c3f756526..33875908b0c 100644 --- a/proxy_components/proxy_ffi/src/jemalloc_utils.rs +++ b/proxy_components/proxy_ffi/src/jemalloc_utils.rs @@ -82,3 +82,11 @@ pub fn get_allocatep_on_thread_start() -> u64 { pub fn get_deallocatep_on_thread_start() -> u64 { issue_mallctl("thread.deallocatedp") } + +pub fn get_allocate() -> u64 { + issue_mallctl("thread.allocated") +} + +pub fn get_deallocate() -> u64 { + issue_mallctl("thread.deallocated") +} diff --git a/raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version b/raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version index efc42c40334..dc8002623ec 100644 --- a/raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version +++ b/raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version @@ -1,3 +1,3 @@ #pragma once #include -namespace DB { constexpr uint64_t RAFT_STORE_PROXY_VERSION = 13437100439851870476ull; } \ No newline at end of file +namespace DB { constexpr uint64_t RAFT_STORE_PROXY_VERSION = 1764984388775075973ull; } \ No newline at end of file diff --git a/raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h b/raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h index 87c51219de7..9763b9de75a 100644 --- a/raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h +++ b/raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h @@ -315,6 +315,8 @@ enum class ReportThreadAllocateInfoType : uint64_t { Reset = 0, AllocPtr, DeallocPtr, + Alloc, + Dealloc, }; struct EngineStoreServerHelper { From 8469457adc504d8bbcdde5c57e0c4c0c6cb302e9 Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Thu, 14 Mar 2024 11:40:58 +0800 Subject: [PATCH 21/26] fix test Signed-off-by: CalvinNeo --- .../mock_store/mock_engine_store_server.rs | 8 +++++ .../src/engine_store_helper_impls.rs | 32 +++++++++---------- proxy_components/proxy_ffi/src/interfaces.rs | 17 ++++++++-- .../ffi/src/RaftStoreProxyFFI/@version | 2 +- .../ffi/src/RaftStoreProxyFFI/ProxyFFI.h | 10 ++++-- 5 files changed, 46 insertions(+), 23 deletions(-) diff --git a/proxy_components/mock-engine-store/src/mock_store/mock_engine_store_server.rs b/proxy_components/mock-engine-store/src/mock_store/mock_engine_store_server.rs index 0cd00a26d56..40b94118b82 100644 --- a/proxy_components/mock-engine-store/src/mock_store/mock_engine_store_server.rs +++ b/proxy_components/mock-engine-store/src/mock_store/mock_engine_store_server.rs @@ -393,6 +393,7 @@ pub fn gen_engine_store_server_helper( fn_kvstore_region_exists: Some(ffi_kvstore_region_exists), fn_clear_fap_snapshot: Some(ffi_clear_fap_snapshot), fn_report_thread_allocate_info: Some(ffi_report_thread_allocate_info), + fn_report_thread_allocate_batch: Some(ffi_report_thread_allocate_batch), ps: PageStorageInterfaces { fn_create_write_batch: Some(ffi_mockps_create_write_batch), fn_wb_put_page: Some(ffi_mockps_wb_put_page), @@ -670,3 +671,10 @@ unsafe extern "C" fn ffi_report_thread_allocate_info( } } } + +unsafe extern "C" fn ffi_report_thread_allocate_batch( + _: *mut interfaces_ffi::EngineStoreServerWrap, + _name: interfaces_ffi::BaseBuffView, + _data: interfaces_ffi::ReportThreadAllocateInfoBatch, +) { +} diff --git a/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs b/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs index f6ab4b239c5..2bfe20ece09 100644 --- a/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs +++ b/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs @@ -106,31 +106,29 @@ impl EngineStoreServerHelper { } pub fn directly_report_jemalloc_alloc(&self) { - JEMALLOC_TNAME.with(|thread_name| { - JEMALLOC_ALLOCP.with(|p| unsafe { + JEMALLOC_TNAME.with(|thread_name| unsafe { + let a = JEMALLOC_ALLOCP.with(|p| { let p = *p.borrow_mut(); if p == std::ptr::null_mut() { - return; + return 0; } - (self.fn_report_thread_allocate_info.into_inner())( - self.inner, - BaseBuffView::from(thread_name.borrow().as_bytes()), - interfaces_ffi::ReportThreadAllocateInfoType::Alloc, - *p, - ); + *p }); - JEMALLOC_DEALLOCP.with(|p| unsafe { + let d = JEMALLOC_DEALLOCP.with(|p| { let p = *p.borrow_mut(); if p == std::ptr::null_mut() { - return; + return 0; } - (self.fn_report_thread_allocate_info.into_inner())( - self.inner, - BaseBuffView::from(thread_name.borrow().as_bytes()), - interfaces_ffi::ReportThreadAllocateInfoType::Dealloc, - *p, - ); + *p }); + (self.fn_report_thread_allocate_batch.into_inner())( + self.inner, + BaseBuffView::from(thread_name.borrow().as_bytes()), + interfaces_ffi::ReportThreadAllocateInfoBatch { + alloc: a, + dealloc: d, + }, + ); }); } diff --git a/proxy_components/proxy_ffi/src/interfaces.rs b/proxy_components/proxy_ffi/src/interfaces.rs index 27297048713..000e06223d5 100644 --- a/proxy_components/proxy_ffi/src/interfaces.rs +++ b/proxy_components/proxy_ffi/src/interfaces.rs @@ -564,8 +564,12 @@ pub mod root { Reset = 0, AllocPtr = 1, DeallocPtr = 2, - Alloc = 3, - Dealloc = 4, + } + #[repr(C)] + #[derive(Debug)] + pub struct ReportThreadAllocateInfoBatch { + pub alloc: u64, + pub dealloc: u64, } #[repr(C)] #[derive(Debug)] @@ -767,6 +771,13 @@ pub mod root { value: u64, ), >, + pub fn_report_thread_allocate_batch: ::std::option::Option< + unsafe extern "C" fn( + arg1: *mut root::DB::EngineStoreServerWrap, + name: root::DB::BaseBuffView, + data: root::DB::ReportThreadAllocateInfoBatch, + ), + >, } extern "C" { pub fn ffi_get_server_info_from_proxy( @@ -775,7 +786,7 @@ pub mod root { arg3: root::DB::RawVoidPtr, ) -> u32; } - pub const RAFT_STORE_PROXY_VERSION: u64 = 1764984388775075973; + pub const RAFT_STORE_PROXY_VERSION: u64 = 4690628711022033644; pub const RAFT_STORE_PROXY_MAGIC_NUMBER: u32 = 324508639; } } diff --git a/raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version b/raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version index dc8002623ec..16051a1cf7d 100644 --- a/raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version +++ b/raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version @@ -1,3 +1,3 @@ #pragma once #include -namespace DB { constexpr uint64_t RAFT_STORE_PROXY_VERSION = 1764984388775075973ull; } \ No newline at end of file +namespace DB { constexpr uint64_t RAFT_STORE_PROXY_VERSION = 4690628711022033644ull; } \ No newline at end of file diff --git a/raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h b/raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h index 9763b9de75a..00f42f192da 100644 --- a/raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h +++ b/raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h @@ -315,8 +315,11 @@ enum class ReportThreadAllocateInfoType : uint64_t { Reset = 0, AllocPtr, DeallocPtr, - Alloc, - Dealloc, +}; + +struct ReportThreadAllocateInfoBatch { + uint64_t alloc; + uint64_t dealloc; }; struct EngineStoreServerHelper { @@ -385,6 +388,9 @@ struct EngineStoreServerHelper { BaseBuffView name, ReportThreadAllocateInfoType type, uint64_t value); + void (*fn_report_thread_allocate_batch)(EngineStoreServerWrap *, + BaseBuffView name, + ReportThreadAllocateInfoBatch data); }; #ifdef __cplusplus From 24ce15aebec4cb5626468632b5c2d85bf37b9952 Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Thu, 14 Mar 2024 14:19:11 +0800 Subject: [PATCH 22/26] use cmake Signed-off-by: CalvinNeo --- Makefile | 1 - 1 file changed, 1 deletion(-) diff --git a/Makefile b/Makefile index f2dc2998277..fd5b6fbb061 100644 --- a/Makefile +++ b/Makefile @@ -196,7 +196,6 @@ dev: format clippy ifeq ($(PROXY_FRAME_POINTER),1) build: ENABLE_FEATURES += pprof-fp endif -build: ENABLE_FEATURES += external-jemalloc build: PROXY_ENABLE_FEATURES="${ENABLE_FEATURES}" ./proxy_scripts/build.sh From 4282bead4f7c7cdba3d9f6c43551677ba0710c04 Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Thu, 14 Mar 2024 15:16:20 +0800 Subject: [PATCH 23/26] is_null Signed-off-by: CalvinNeo --- proxy_components/proxy_ffi/src/engine_store_helper_impls.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs b/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs index 2bfe20ece09..601e925041a 100644 --- a/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs +++ b/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs @@ -109,14 +109,14 @@ impl EngineStoreServerHelper { JEMALLOC_TNAME.with(|thread_name| unsafe { let a = JEMALLOC_ALLOCP.with(|p| { let p = *p.borrow_mut(); - if p == std::ptr::null_mut() { + if p.is_null() { return 0; } *p }); let d = JEMALLOC_DEALLOCP.with(|p| { let p = *p.borrow_mut(); - if p == std::ptr::null_mut() { + if p.is_null() { return 0; } *p From 1bca3dbfd4fcf78a6ba4c0d3116a2709d4269293 Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Thu, 14 Mar 2024 15:43:32 +0800 Subject: [PATCH 24/26] add id Signed-off-by: CalvinNeo --- .../mock_store/mock_engine_store_server.rs | 2 + .../src/engine_store_helper_impls.rs | 40 +++++++++++-------- proxy_components/proxy_ffi/src/interfaces.rs | 4 +- proxy_components/proxy_ffi/src/lib.rs | 3 ++ .../ffi/src/RaftStoreProxyFFI/@version | 2 +- .../ffi/src/RaftStoreProxyFFI/ProxyFFI.h | 4 +- 6 files changed, 35 insertions(+), 20 deletions(-) diff --git a/proxy_components/mock-engine-store/src/mock_store/mock_engine_store_server.rs b/proxy_components/mock-engine-store/src/mock_store/mock_engine_store_server.rs index 40b94118b82..969c28af033 100644 --- a/proxy_components/mock-engine-store/src/mock_store/mock_engine_store_server.rs +++ b/proxy_components/mock-engine-store/src/mock_store/mock_engine_store_server.rs @@ -637,6 +637,7 @@ unsafe extern "C" fn ffi_get_lock_by_key( unsafe extern "C" fn ffi_report_thread_allocate_info( arg1: *mut interfaces_ffi::EngineStoreServerWrap, + _: u64, name: interfaces_ffi::BaseBuffView, t: interfaces_ffi::ReportThreadAllocateInfoType, value: u64, @@ -674,6 +675,7 @@ unsafe extern "C" fn ffi_report_thread_allocate_info( unsafe extern "C" fn ffi_report_thread_allocate_batch( _: *mut interfaces_ffi::EngineStoreServerWrap, + _: u64, _name: interfaces_ffi::BaseBuffView, _data: interfaces_ffi::ReportThreadAllocateInfoBatch, ) { diff --git a/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs b/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs index 601e925041a..d2d21a1ae36 100644 --- a/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs +++ b/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs @@ -36,7 +36,7 @@ pub fn gen_engine_store_server_helper( thread_local! { pub static JEMALLOC_REGISTERED: RefCell = RefCell::new(false); - pub static JEMALLOC_TNAME: RefCell = RefCell::new(Default::default()); + pub static JEMALLOC_TNAME: RefCell<(String, u64)> = RefCell::new(Default::default()); pub static JEMALLOC_ALLOCP: RefCell<*mut u64> = RefCell::new(std::ptr::null_mut()); pub static JEMALLOC_DEALLOCP: RefCell<*mut u64> = RefCell::new(std::ptr::null_mut()); } @@ -66,28 +66,32 @@ impl EngineStoreServerHelper { .name() .unwrap_or("") .to_string(); + let thread_id: u64 = std::thread::current().id().as_u64().into(); (self.fn_report_thread_allocate_info.into_inner())( self.inner, + thread_id, BaseBuffView::from(thread_name.as_bytes()), interfaces_ffi::ReportThreadAllocateInfoType::Reset, ptr_alloc, ); - (self.fn_report_thread_allocate_info.into_inner())( - self.inner, - BaseBuffView::from(thread_name.as_bytes()), - interfaces_ffi::ReportThreadAllocateInfoType::AllocPtr, - ptr_alloc, - ); - (self.fn_report_thread_allocate_info.into_inner())( - self.inner, - BaseBuffView::from(thread_name.as_bytes()), - interfaces_ffi::ReportThreadAllocateInfoType::DeallocPtr, - ptr_dealloc, - ); + // Since we don't have lifelong thread to monitor, temperarily disable this. + // (self.fn_report_thread_allocate_info.into_inner())( + // self.inner, + // BaseBuffView::from(thread_name.as_bytes()), + // interfaces_ffi::ReportThreadAllocateInfoType::AllocPtr, + // ptr_alloc, + // ); + // (self.fn_report_thread_allocate_info.into_inner())( + // self.inner, + // BaseBuffView::from(thread_name.as_bytes()), + // interfaces_ffi::ReportThreadAllocateInfoType::DeallocPtr, + // ptr_dealloc, + // ); + // Some threads are not everlasting, so we don't want TiFlash to directly access // the pointer. JEMALLOC_TNAME.with(|p| { - *p.borrow_mut() = thread_name; + *p.borrow_mut() = (thread_name, thread_id); }); if ptr_alloc != 0 { JEMALLOC_ALLOCP.with(|p| { @@ -106,7 +110,7 @@ impl EngineStoreServerHelper { } pub fn directly_report_jemalloc_alloc(&self) { - JEMALLOC_TNAME.with(|thread_name| unsafe { + JEMALLOC_TNAME.with(|thread_info| unsafe { let a = JEMALLOC_ALLOCP.with(|p| { let p = *p.borrow_mut(); if p.is_null() { @@ -123,7 +127,8 @@ impl EngineStoreServerHelper { }); (self.fn_report_thread_allocate_batch.into_inner())( self.inner, - BaseBuffView::from(thread_name.borrow().as_bytes()), + thread_info.borrow().1, + BaseBuffView::from(thread_info.borrow().0.as_bytes()), interfaces_ffi::ReportThreadAllocateInfoBatch { alloc: a, dealloc: d, @@ -281,6 +286,7 @@ impl EngineStoreServerHelper { debug_assert!(self.fn_pre_handle_snapshot.is_some()); self.maybe_jemalloc_register_alloc(); + self.directly_report_jemalloc_alloc(); let snaps_view = into_sst_views(snaps); unsafe { let region = ProtoMsgBaseBuff::new(region); @@ -298,6 +304,7 @@ impl EngineStoreServerHelper { pub fn apply_pre_handled_snapshot(&self, snap: RawCppPtr) { debug_assert!(self.fn_apply_pre_handled_snapshot.is_some()); self.maybe_jemalloc_register_alloc(); + self.directly_report_jemalloc_alloc(); unsafe { (self.fn_apply_pre_handled_snapshot.into_inner())(self.inner, snap.ptr, snap.type_) } @@ -306,6 +313,7 @@ impl EngineStoreServerHelper { pub fn abort_pre_handle_snapshot(&self, region_id: u64, peer_id: u64) { debug_assert!(self.fn_abort_pre_handle_snapshot.is_some()); self.maybe_jemalloc_register_alloc(); + self.directly_report_jemalloc_alloc(); unsafe { (self.fn_abort_pre_handle_snapshot.into_inner())(self.inner, region_id, peer_id) } } diff --git a/proxy_components/proxy_ffi/src/interfaces.rs b/proxy_components/proxy_ffi/src/interfaces.rs index 000e06223d5..bf3aad97b2c 100644 --- a/proxy_components/proxy_ffi/src/interfaces.rs +++ b/proxy_components/proxy_ffi/src/interfaces.rs @@ -766,6 +766,7 @@ pub mod root { pub fn_report_thread_allocate_info: ::std::option::Option< unsafe extern "C" fn( arg1: *mut root::DB::EngineStoreServerWrap, + thread_id: u64, name: root::DB::BaseBuffView, type_: root::DB::ReportThreadAllocateInfoType, value: u64, @@ -774,6 +775,7 @@ pub mod root { pub fn_report_thread_allocate_batch: ::std::option::Option< unsafe extern "C" fn( arg1: *mut root::DB::EngineStoreServerWrap, + thread_id: u64, name: root::DB::BaseBuffView, data: root::DB::ReportThreadAllocateInfoBatch, ), @@ -786,7 +788,7 @@ pub mod root { arg3: root::DB::RawVoidPtr, ) -> u32; } - pub const RAFT_STORE_PROXY_VERSION: u64 = 4690628711022033644; + pub const RAFT_STORE_PROXY_VERSION: u64 = 17598435718842250051; pub const RAFT_STORE_PROXY_MAGIC_NUMBER: u32 = 324508639; } } diff --git a/proxy_components/proxy_ffi/src/lib.rs b/proxy_components/proxy_ffi/src/lib.rs index 313f10b7539..e7f70318581 100644 --- a/proxy_components/proxy_ffi/src/lib.rs +++ b/proxy_components/proxy_ffi/src/lib.rs @@ -1,9 +1,12 @@ // Copyright 2022 TiKV Project Authors. Licensed under Apache-2.0. +#![feature(thread_id_value)] + /// All mods end up with `_impls` impl structs defined in interface. /// Other mods which define and impl structs should not end up with name /// `_impls`. + #[allow(dead_code)] pub mod interfaces; // All ffi impls that not related to raft domain. diff --git a/raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version b/raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version index 16051a1cf7d..fc03df7a8f9 100644 --- a/raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version +++ b/raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version @@ -1,3 +1,3 @@ #pragma once #include -namespace DB { constexpr uint64_t RAFT_STORE_PROXY_VERSION = 4690628711022033644ull; } \ No newline at end of file +namespace DB { constexpr uint64_t RAFT_STORE_PROXY_VERSION = 17598435718842250051ull; } \ No newline at end of file diff --git a/raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h b/raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h index 00f42f192da..0e214b63e0d 100644 --- a/raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h +++ b/raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h @@ -385,11 +385,11 @@ struct EngineStoreServerHelper { void (*fn_clear_fap_snapshot)(EngineStoreServerWrap *, uint64_t region_id); bool (*fn_kvstore_region_exists)(EngineStoreServerWrap *, uint64_t region_id); void (*fn_report_thread_allocate_info)(EngineStoreServerWrap *, - BaseBuffView name, + uint64_t thread_id, BaseBuffView name, ReportThreadAllocateInfoType type, uint64_t value); void (*fn_report_thread_allocate_batch)(EngineStoreServerWrap *, - BaseBuffView name, + uint64_t thread_id, BaseBuffView name, ReportThreadAllocateInfoBatch data); }; From c58aa770b59ae446131fa73e5f0c01dfffc8f5f4 Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Thu, 14 Mar 2024 18:08:18 +0800 Subject: [PATCH 25/26] Remove Signed-off-by: CalvinNeo --- .../proxy_ffi/src/engine_store_helper_impls.rs | 2 +- proxy_components/proxy_ffi/src/interfaces.rs | 7 ++++--- raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version | 2 +- raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h | 1 + 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs b/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs index d2d21a1ae36..37b0bd4f061 100644 --- a/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs +++ b/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs @@ -72,7 +72,7 @@ impl EngineStoreServerHelper { thread_id, BaseBuffView::from(thread_name.as_bytes()), interfaces_ffi::ReportThreadAllocateInfoType::Reset, - ptr_alloc, + 0, ); // Since we don't have lifelong thread to monitor, temperarily disable this. // (self.fn_report_thread_allocate_info.into_inner())( diff --git a/proxy_components/proxy_ffi/src/interfaces.rs b/proxy_components/proxy_ffi/src/interfaces.rs index bf3aad97b2c..ca2e9337c6f 100644 --- a/proxy_components/proxy_ffi/src/interfaces.rs +++ b/proxy_components/proxy_ffi/src/interfaces.rs @@ -562,8 +562,9 @@ pub mod root { #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum ReportThreadAllocateInfoType { Reset = 0, - AllocPtr = 1, - DeallocPtr = 2, + Remove = 1, + AllocPtr = 2, + DeallocPtr = 3, } #[repr(C)] #[derive(Debug)] @@ -788,7 +789,7 @@ pub mod root { arg3: root::DB::RawVoidPtr, ) -> u32; } - pub const RAFT_STORE_PROXY_VERSION: u64 = 17598435718842250051; + pub const RAFT_STORE_PROXY_VERSION: u64 = 9679186549381427051; pub const RAFT_STORE_PROXY_MAGIC_NUMBER: u32 = 324508639; } } diff --git a/raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version b/raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version index fc03df7a8f9..59c3d0bca37 100644 --- a/raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version +++ b/raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version @@ -1,3 +1,3 @@ #pragma once #include -namespace DB { constexpr uint64_t RAFT_STORE_PROXY_VERSION = 17598435718842250051ull; } \ No newline at end of file +namespace DB { constexpr uint64_t RAFT_STORE_PROXY_VERSION = 9679186549381427051ull; } \ No newline at end of file diff --git a/raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h b/raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h index 0e214b63e0d..3dd1feba12a 100644 --- a/raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h +++ b/raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h @@ -313,6 +313,7 @@ struct PageStorageInterfaces { enum class ReportThreadAllocateInfoType : uint64_t { Reset = 0, + Remove, AllocPtr, DeallocPtr, }; From 5f1958ea6840550e2ea6ddb190b95814af692ce7 Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Fri, 15 Mar 2024 12:19:56 +0800 Subject: [PATCH 26/26] z Signed-off-by: CalvinNeo --- .../src/engine_store_helper_impls.rs | 32 +++++++++---------- proxy_components/proxy_ffi/src/lib.rs | 1 - 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs b/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs index 37b0bd4f061..dd96e0a66f4 100644 --- a/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs +++ b/proxy_components/proxy_ffi/src/engine_store_helper_impls.rs @@ -62,10 +62,7 @@ impl EngineStoreServerHelper { unsafe { let ptr_alloc: u64 = crate::jemalloc_utils::get_allocatep_on_thread_start(); let ptr_dealloc: u64 = crate::jemalloc_utils::get_deallocatep_on_thread_start(); - let thread_name = std::thread::current() - .name() - .unwrap_or("") - .to_string(); + let thread_name = std::thread::current().name().unwrap_or("").to_string(); let thread_id: u64 = std::thread::current().id().as_u64().into(); (self.fn_report_thread_allocate_info.into_inner())( self.inner, @@ -74,19 +71,20 @@ impl EngineStoreServerHelper { interfaces_ffi::ReportThreadAllocateInfoType::Reset, 0, ); - // Since we don't have lifelong thread to monitor, temperarily disable this. - // (self.fn_report_thread_allocate_info.into_inner())( - // self.inner, - // BaseBuffView::from(thread_name.as_bytes()), - // interfaces_ffi::ReportThreadAllocateInfoType::AllocPtr, - // ptr_alloc, - // ); - // (self.fn_report_thread_allocate_info.into_inner())( - // self.inner, - // BaseBuffView::from(thread_name.as_bytes()), - // interfaces_ffi::ReportThreadAllocateInfoType::DeallocPtr, - // ptr_dealloc, - // ); + (self.fn_report_thread_allocate_info.into_inner())( + self.inner, + thread_id, + BaseBuffView::from(thread_name.as_bytes()), + interfaces_ffi::ReportThreadAllocateInfoType::AllocPtr, + ptr_alloc, + ); + (self.fn_report_thread_allocate_info.into_inner())( + self.inner, + thread_id, + BaseBuffView::from(thread_name.as_bytes()), + interfaces_ffi::ReportThreadAllocateInfoType::DeallocPtr, + ptr_dealloc, + ); // Some threads are not everlasting, so we don't want TiFlash to directly access // the pointer. diff --git a/proxy_components/proxy_ffi/src/lib.rs b/proxy_components/proxy_ffi/src/lib.rs index e7f70318581..fd61492e800 100644 --- a/proxy_components/proxy_ffi/src/lib.rs +++ b/proxy_components/proxy_ffi/src/lib.rs @@ -6,7 +6,6 @@ /// Other mods which define and impl structs should not end up with name /// `_impls`. - #[allow(dead_code)] pub mod interfaces; // All ffi impls that not related to raft domain.