1
1
// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.
2
2
3
3
use std:: alloc:: { GlobalAlloc , Layout } ;
4
- use std:: sync:: atomic:: { AtomicBool , Ordering } ;
5
4
use std:: ops:: Deref ;
5
+ use std:: sync:: atomic:: { AtomicBool , Ordering } ;
6
6
7
7
use backtrace:: Frame ;
8
8
use spin:: RwLock ;
9
9
10
- use crate :: MAX_DEPTH ;
11
- use crate :: Result ;
12
10
use crate :: profiler:: Profiler ;
13
11
use crate :: Error ;
14
12
use crate :: ReportBuilder ;
13
+ use crate :: Result ;
14
+ use crate :: MAX_DEPTH ;
15
15
16
16
lazy_static:: lazy_static! {
17
17
pub ( crate ) static ref HEAP_PROFILER : RwLock <Result <Profiler >> = RwLock :: new( Profiler :: new( ) ) ;
@@ -34,17 +34,17 @@ impl<T: GlobalAlloc> AllocRecorder<T> {
34
34
match HEAP_PROFILER . write ( ) . as_mut ( ) {
35
35
Err ( err) => {
36
36
log:: error!( "Error in creating profiler: {}" , err) ;
37
- return Err ( Error :: CreatingError )
37
+ Err ( Error :: CreatingError )
38
38
}
39
39
Ok ( profiler) => match profiler. start ( ) {
40
40
Ok ( ( ) ) => {
41
41
self . start ( ) ;
42
-
42
+
43
43
Ok ( HeapProfilerGuard :: < ' static , ' _ , T > {
44
44
profiler : & HEAP_PROFILER ,
45
45
alloc : self ,
46
46
} )
47
- } ,
47
+ }
48
48
Err ( err) => Err ( err) ,
49
49
} ,
50
50
}
@@ -80,7 +80,7 @@ impl<'a, T: GlobalAlloc> Deref for HeapReportBuilder<'a, '_, '_, T> {
80
80
81
81
pub struct HeapProfilerGuard < ' a , ' b , T : GlobalAlloc > {
82
82
profiler : & ' a RwLock < Result < Profiler > > ,
83
- alloc : & ' b AllocRecorder < T >
83
+ alloc : & ' b AllocRecorder < T > ,
84
84
}
85
85
86
86
impl < T : GlobalAlloc > HeapProfilerGuard < ' _ , ' _ , T > {
@@ -90,7 +90,7 @@ impl<T: GlobalAlloc> HeapProfilerGuard<'_, '_, T> {
90
90
91
91
HeapReportBuilder {
92
92
report_builder : ReportBuilder :: new ( & self . profiler ) ,
93
- guard : & self
93
+ guard : & self ,
94
94
}
95
95
}
96
96
}
@@ -114,10 +114,9 @@ unsafe impl<T: GlobalAlloc> GlobalAlloc for AllocRecorder<T> {
114
114
if self . profiling . load ( Ordering :: SeqCst ) {
115
115
let mut guard = HEAP_PROFILER . write ( ) ;
116
116
if let Ok ( profiler) = guard. as_mut ( ) {
117
- let mut bt: [ Frame ; MAX_DEPTH ] =
118
- std:: mem:: MaybeUninit :: uninit ( ) . assume_init ( ) ;
117
+ let mut bt: [ Frame ; MAX_DEPTH ] = std:: mem:: MaybeUninit :: uninit ( ) . assume_init ( ) ;
119
118
let mut index = 0 ;
120
-
119
+
121
120
backtrace:: trace_unsynchronized ( |frame| {
122
121
if index < MAX_DEPTH {
123
122
bt[ index] = frame. clone ( ) ;
@@ -133,19 +132,16 @@ unsafe impl<T: GlobalAlloc> GlobalAlloc for AllocRecorder<T> {
133
132
}
134
133
}
135
134
136
- let ptr = self . inner . alloc ( layout) ;
137
-
138
- ptr
135
+ self . inner . alloc ( layout)
139
136
}
140
137
141
138
unsafe fn dealloc ( & self , ptr : * mut u8 , layout : Layout ) {
142
139
if self . profiling . load ( Ordering :: SeqCst ) {
143
140
let mut guard = HEAP_PROFILER . write ( ) ;
144
141
if let Ok ( profiler) = guard. as_mut ( ) {
145
- let mut bt: [ Frame ; MAX_DEPTH ] =
146
- std:: mem:: MaybeUninit :: uninit ( ) . assume_init ( ) ;
142
+ let mut bt: [ Frame ; MAX_DEPTH ] = std:: mem:: MaybeUninit :: uninit ( ) . assume_init ( ) ;
147
143
let mut index = 0 ;
148
-
144
+
149
145
backtrace:: trace_unsynchronized ( |frame| {
150
146
if index < MAX_DEPTH {
151
147
bt[ index] = frame. clone ( ) ;
@@ -163,4 +159,4 @@ unsafe impl<T: GlobalAlloc> GlobalAlloc for AllocRecorder<T> {
163
159
164
160
self . inner . dealloc ( ptr, layout) ;
165
161
}
166
- }
162
+ }
0 commit comments