1
1
// Copyright 2016 TiKV Project Authors. Licensed under Apache-2.0.
2
- use std:: pin:: Pin ;
2
+ use std:: { cell :: RefCell , pin:: Pin } ;
3
3
4
4
use kvproto:: { kvrpcpb, metapb, raft_cmdpb} ;
5
5
@@ -34,6 +34,13 @@ pub fn gen_engine_store_server_helper(
34
34
unsafe { & ( * ( engine_store_server_helper as * const EngineStoreServerHelper ) ) }
35
35
}
36
36
37
+ thread_local ! {
38
+ pub static JEMALLOC_REGISTERED : RefCell <bool > = RefCell :: new( false ) ;
39
+ pub static JEMALLOC_TNAME : RefCell <( String , u64 ) > = RefCell :: new( Default :: default ( ) ) ;
40
+ pub static JEMALLOC_ALLOCP : RefCell <* mut u64 > = RefCell :: new( std:: ptr:: null_mut( ) ) ;
41
+ pub static JEMALLOC_DEALLOCP : RefCell <* mut u64 > = RefCell :: new( std:: ptr:: null_mut( ) ) ;
42
+ }
43
+
37
44
/// # Safety
38
45
/// The lifetime of `engine_store_server_helper` is definitely longer than
39
46
/// `ENGINE_STORE_SERVER_HELPER_PTR`.
@@ -49,6 +56,85 @@ pub fn set_server_info_resp(res: &kvproto::diagnosticspb::ServerInfoResponse, pt
49
56
}
50
57
51
58
impl EngineStoreServerHelper {
59
+ pub fn maybe_jemalloc_register_alloc ( & self ) {
60
+ JEMALLOC_REGISTERED . with ( |b| {
61
+ if !* b. borrow ( ) {
62
+ unsafe {
63
+ let ptr_alloc: u64 = crate :: jemalloc_utils:: get_allocatep_on_thread_start ( ) ;
64
+ let ptr_dealloc: u64 = crate :: jemalloc_utils:: get_deallocatep_on_thread_start ( ) ;
65
+ let thread_name = std:: thread:: current ( ) . name ( ) . unwrap_or ( "" ) . to_string ( ) ;
66
+ let thread_id: u64 = std:: thread:: current ( ) . id ( ) . as_u64 ( ) . into ( ) ;
67
+ ( self . fn_report_thread_allocate_info . into_inner ( ) ) (
68
+ self . inner ,
69
+ thread_id,
70
+ BaseBuffView :: from ( thread_name. as_bytes ( ) ) ,
71
+ interfaces_ffi:: ReportThreadAllocateInfoType :: Reset ,
72
+ 0 ,
73
+ ) ;
74
+ ( self . fn_report_thread_allocate_info . into_inner ( ) ) (
75
+ self . inner ,
76
+ thread_id,
77
+ BaseBuffView :: from ( thread_name. as_bytes ( ) ) ,
78
+ interfaces_ffi:: ReportThreadAllocateInfoType :: AllocPtr ,
79
+ ptr_alloc,
80
+ ) ;
81
+ ( self . fn_report_thread_allocate_info . into_inner ( ) ) (
82
+ self . inner ,
83
+ thread_id,
84
+ BaseBuffView :: from ( thread_name. as_bytes ( ) ) ,
85
+ interfaces_ffi:: ReportThreadAllocateInfoType :: DeallocPtr ,
86
+ ptr_dealloc,
87
+ ) ;
88
+
89
+ // Some threads are not everlasting, so we don't want TiFlash to directly access
90
+ // the pointer.
91
+ JEMALLOC_TNAME . with ( |p| {
92
+ * p. borrow_mut ( ) = ( thread_name, thread_id) ;
93
+ } ) ;
94
+ if ptr_alloc != 0 {
95
+ JEMALLOC_ALLOCP . with ( |p| {
96
+ * p. borrow_mut ( ) = ptr_alloc as * mut u64 ;
97
+ } ) ;
98
+ }
99
+ if ptr_dealloc != 0 {
100
+ JEMALLOC_DEALLOCP . with ( |p| {
101
+ * p. borrow_mut ( ) = ptr_dealloc as * mut u64 ;
102
+ } ) ;
103
+ }
104
+ }
105
+ * ( b. borrow_mut ( ) ) = true ;
106
+ }
107
+ } ) ;
108
+ }
109
+
110
+ pub fn directly_report_jemalloc_alloc ( & self ) {
111
+ JEMALLOC_TNAME . with ( |thread_info| unsafe {
112
+ let a = JEMALLOC_ALLOCP . with ( |p| {
113
+ let p = * p. borrow_mut ( ) ;
114
+ if p. is_null ( ) {
115
+ return 0 ;
116
+ }
117
+ * p
118
+ } ) ;
119
+ let d = JEMALLOC_DEALLOCP . with ( |p| {
120
+ let p = * p. borrow_mut ( ) ;
121
+ if p. is_null ( ) {
122
+ return 0 ;
123
+ }
124
+ * p
125
+ } ) ;
126
+ ( self . fn_report_thread_allocate_batch . into_inner ( ) ) (
127
+ self . inner ,
128
+ thread_info. borrow ( ) . 1 ,
129
+ BaseBuffView :: from ( thread_info. borrow ( ) . 0 . as_bytes ( ) ) ,
130
+ interfaces_ffi:: ReportThreadAllocateInfoBatch {
131
+ alloc : a,
132
+ dealloc : d,
133
+ } ,
134
+ ) ;
135
+ } ) ;
136
+ }
137
+
52
138
pub fn gc_raw_cpp_ptr ( & self , ptr : * mut :: std:: os:: raw:: c_void , tp : RawCppPtrType ) {
53
139
debug_assert ! ( self . fn_gc_raw_cpp_ptr. is_some( ) ) ;
54
140
unsafe {
@@ -82,6 +168,7 @@ impl EngineStoreServerHelper {
82
168
83
169
pub fn handle_compute_store_stats ( & self ) -> StoreStats {
84
170
debug_assert ! ( self . fn_handle_compute_store_stats. is_some( ) ) ;
171
+ self . maybe_jemalloc_register_alloc ( ) ;
85
172
unsafe { ( self . fn_handle_compute_store_stats . into_inner ( ) ) ( self . inner ) }
86
173
}
87
174
@@ -91,16 +178,22 @@ impl EngineStoreServerHelper {
91
178
header : RaftCmdHeader ,
92
179
) -> EngineStoreApplyRes {
93
180
debug_assert ! ( self . fn_handle_write_raft_cmd. is_some( ) ) ;
181
+ self . maybe_jemalloc_register_alloc ( ) ;
182
+ self . directly_report_jemalloc_alloc ( ) ;
94
183
unsafe { ( self . fn_handle_write_raft_cmd . into_inner ( ) ) ( self . inner , cmds. gen_view ( ) , header) }
95
184
}
96
185
97
186
pub fn handle_get_engine_store_server_status ( & self ) -> EngineStoreServerStatus {
98
187
debug_assert ! ( self . fn_handle_get_engine_store_server_status. is_some( ) ) ;
188
+ self . maybe_jemalloc_register_alloc ( ) ;
189
+ self . directly_report_jemalloc_alloc ( ) ;
99
190
unsafe { ( self . fn_handle_get_engine_store_server_status . into_inner ( ) ) ( self . inner ) }
100
191
}
101
192
102
193
pub fn handle_set_proxy ( & self , proxy : * const RaftStoreProxyFFIHelper ) {
103
194
debug_assert ! ( self . fn_atomic_update_proxy. is_some( ) ) ;
195
+ self . maybe_jemalloc_register_alloc ( ) ;
196
+ self . directly_report_jemalloc_alloc ( ) ;
104
197
unsafe { ( self . fn_atomic_update_proxy . into_inner ( ) ) ( self . inner , proxy as * mut _ ) }
105
198
}
106
199
@@ -129,7 +222,8 @@ impl EngineStoreServerHelper {
129
222
header : RaftCmdHeader ,
130
223
) -> EngineStoreApplyRes {
131
224
debug_assert ! ( self . fn_handle_admin_raft_cmd. is_some( ) ) ;
132
-
225
+ self . maybe_jemalloc_register_alloc ( ) ;
226
+ self . directly_report_jemalloc_alloc ( ) ;
133
227
unsafe {
134
228
let req = ProtoMsgBaseBuff :: new ( req) ;
135
229
let resp = ProtoMsgBaseBuff :: new ( resp) ;
@@ -158,6 +252,8 @@ impl EngineStoreServerHelper {
158
252
term : u64 ,
159
253
) -> bool {
160
254
debug_assert ! ( self . fn_try_flush_data. is_some( ) ) ;
255
+ self . maybe_jemalloc_register_alloc ( ) ;
256
+ self . directly_report_jemalloc_alloc ( ) ;
161
257
// TODO(proactive flush)
162
258
unsafe {
163
259
( self . fn_try_flush_data . into_inner ( ) ) (
@@ -187,6 +283,8 @@ impl EngineStoreServerHelper {
187
283
) -> RawCppPtr {
188
284
debug_assert ! ( self . fn_pre_handle_snapshot. is_some( ) ) ;
189
285
286
+ self . maybe_jemalloc_register_alloc ( ) ;
287
+ self . directly_report_jemalloc_alloc ( ) ;
190
288
let snaps_view = into_sst_views ( snaps) ;
191
289
unsafe {
192
290
let region = ProtoMsgBaseBuff :: new ( region) ;
@@ -203,13 +301,17 @@ impl EngineStoreServerHelper {
203
301
204
302
pub fn apply_pre_handled_snapshot ( & self , snap : RawCppPtr ) {
205
303
debug_assert ! ( self . fn_apply_pre_handled_snapshot. is_some( ) ) ;
304
+ self . maybe_jemalloc_register_alloc ( ) ;
305
+ self . directly_report_jemalloc_alloc ( ) ;
206
306
unsafe {
207
307
( self . fn_apply_pre_handled_snapshot . into_inner ( ) ) ( self . inner , snap. ptr , snap. type_ )
208
308
}
209
309
}
210
310
211
311
pub fn abort_pre_handle_snapshot ( & self , region_id : u64 , peer_id : u64 ) {
212
312
debug_assert ! ( self . fn_abort_pre_handle_snapshot. is_some( ) ) ;
313
+ self . maybe_jemalloc_register_alloc ( ) ;
314
+ self . directly_report_jemalloc_alloc ( ) ;
213
315
unsafe { ( self . fn_abort_pre_handle_snapshot . into_inner ( ) ) ( self . inner , region_id, peer_id) }
214
316
}
215
317
@@ -277,6 +379,8 @@ impl EngineStoreServerHelper {
277
379
) -> EngineStoreApplyRes {
278
380
debug_assert ! ( self . fn_handle_ingest_sst. is_some( ) ) ;
279
381
382
+ self . maybe_jemalloc_register_alloc ( ) ;
383
+ self . directly_report_jemalloc_alloc ( ) ;
280
384
let snaps_view = into_sst_views ( snaps) ;
281
385
unsafe {
282
386
( self . fn_handle_ingest_sst . into_inner ( ) ) (
@@ -290,6 +394,8 @@ impl EngineStoreServerHelper {
290
394
pub fn handle_destroy ( & self , region_id : u64 ) {
291
395
debug_assert ! ( self . fn_handle_destroy. is_some( ) ) ;
292
396
397
+ self . maybe_jemalloc_register_alloc ( ) ;
398
+ self . directly_report_jemalloc_alloc ( ) ;
293
399
unsafe {
294
400
( self . fn_handle_destroy . into_inner ( ) ) ( self . inner , region_id) ;
295
401
}
0 commit comments