forked from pingcap/tidb-engine-ext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjemalloc_utils.rs
111 lines (102 loc) · 3.22 KB
/
jemalloc_utils.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// 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;
// Embedded 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;
}
#[allow(unused_variables)]
#[allow(unused_mut)]
#[allow(unused_unsafe)]
pub fn issue_mallctl_args(
command: &str,
oldptr: *mut ::std::os::raw::c_void,
oldsize: *mut u64,
newptr: *mut ::std::os::raw::c_void,
newsize: u64,
) -> ::std::os::raw::c_int {
unsafe {
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;
// See unprefixed_malloc_on_supported_platforms in tikv-jemalloc-sys.
#[cfg(any(test, feature = "testexport"))]
{
// Test part
#[cfg(feature = "jemalloc")]
{
// See NO_UNPREFIXED_MALLOC
#[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "macos"))]
return _rjem_mallctl(c_ptr, oldptr, oldsize, newptr, newsize);
#[cfg(not(any(
target_os = "android",
target_os = "dragonfly",
target_os = "macos"
)))]
return mallctl(c_ptr, oldptr, oldsize, newptr, newsize);
}
0
}
#[cfg(not(any(test, feature = "testexport")))]
{
// No test part
#[cfg(feature = "external-jemalloc")]
{
// Must linked to tiflash.
return mallctl(c_ptr, oldptr, oldsize, newptr, newsize);
}
#[cfg(not(feature = "external-jemalloc"))]
{
// Happens only with `raftstore-proxy-main`
#[cfg(not(any(
target_os = "android",
target_os = "dragonfly",
target_os = "macos"
)))]
{
return mallctl(c_ptr, oldptr, oldsize, newptr, newsize);
}
0
}
}
}
}
#[allow(unused_variables)]
#[allow(unused_mut)]
#[allow(unused_unsafe)]
fn issue_mallctl(command: &str) -> u64 {
type PtrUnderlying = u64;
let mut ptr: PtrUnderlying = 0;
let mut size = std::mem::size_of::<PtrUnderlying>() as u64;
issue_mallctl_args(
command,
&mut ptr as *mut _ as *mut ::std::os::raw::c_void,
&mut size as *mut u64,
std::ptr::null_mut(),
0,
);
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")
}
pub fn get_allocate() -> u64 {
issue_mallctl("thread.allocated")
}
pub fn get_deallocate() -> u64 {
issue_mallctl("thread.deallocated")
}