Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6e662ed

Browse files
committedDec 15, 2020
fix fmt and clippy
Signed-off-by: Yang Keao <keao.yang@yahoo.com>
1 parent 63dc9f2 commit 6e662ed

File tree

4 files changed

+25
-23
lines changed

4 files changed

+25
-23
lines changed
 

‎examples/heap_profiler.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
22

3-
use pprof::AllocRecorder;
43
use pprof::protos::Message;
4+
use pprof::AllocRecorder;
55
use std::alloc::System;
66
use std::fs::File;
77
use std::io::Write;
@@ -40,4 +40,4 @@ fn memory_leak(size: usize) {
4040
memory_leak(size / 2);
4141
memory_leak(size / 2);
4242
}
43-
}
43+
}

‎src/heap_profiler.rs

+14-18
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.
22

33
use std::alloc::{GlobalAlloc, Layout};
4-
use std::sync::atomic::{AtomicBool, Ordering};
54
use std::ops::Deref;
5+
use std::sync::atomic::{AtomicBool, Ordering};
66

77
use backtrace::Frame;
88
use spin::RwLock;
99

10-
use crate::MAX_DEPTH;
11-
use crate::Result;
1210
use crate::profiler::Profiler;
1311
use crate::Error;
1412
use crate::ReportBuilder;
13+
use crate::Result;
14+
use crate::MAX_DEPTH;
1515

1616
lazy_static::lazy_static! {
1717
pub(crate) static ref HEAP_PROFILER: RwLock<Result<Profiler>> = RwLock::new(Profiler::new());
@@ -34,17 +34,17 @@ impl<T: GlobalAlloc> AllocRecorder<T> {
3434
match HEAP_PROFILER.write().as_mut() {
3535
Err(err) => {
3636
log::error!("Error in creating profiler: {}", err);
37-
return Err(Error::CreatingError)
37+
Err(Error::CreatingError)
3838
}
3939
Ok(profiler) => match profiler.start() {
4040
Ok(()) => {
4141
self.start();
42-
42+
4343
Ok(HeapProfilerGuard::<'static, '_, T> {
4444
profiler: &HEAP_PROFILER,
4545
alloc: self,
4646
})
47-
},
47+
}
4848
Err(err) => Err(err),
4949
},
5050
}
@@ -80,7 +80,7 @@ impl<'a, T: GlobalAlloc> Deref for HeapReportBuilder<'a, '_, '_, T> {
8080

8181
pub struct HeapProfilerGuard<'a, 'b, T: GlobalAlloc> {
8282
profiler: &'a RwLock<Result<Profiler>>,
83-
alloc: &'b AllocRecorder<T>
83+
alloc: &'b AllocRecorder<T>,
8484
}
8585

8686
impl<T: GlobalAlloc> HeapProfilerGuard<'_, '_, T> {
@@ -90,7 +90,7 @@ impl<T: GlobalAlloc> HeapProfilerGuard<'_, '_, T> {
9090

9191
HeapReportBuilder {
9292
report_builder: ReportBuilder::new(&self.profiler),
93-
guard: &self
93+
guard: &self,
9494
}
9595
}
9696
}
@@ -114,10 +114,9 @@ unsafe impl<T: GlobalAlloc> GlobalAlloc for AllocRecorder<T> {
114114
if self.profiling.load(Ordering::SeqCst) {
115115
let mut guard = HEAP_PROFILER.write();
116116
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();
119118
let mut index = 0;
120-
119+
121120
backtrace::trace_unsynchronized(|frame| {
122121
if index < MAX_DEPTH {
123122
bt[index] = frame.clone();
@@ -133,19 +132,16 @@ unsafe impl<T: GlobalAlloc> GlobalAlloc for AllocRecorder<T> {
133132
}
134133
}
135134

136-
let ptr = self.inner.alloc(layout);
137-
138-
ptr
135+
self.inner.alloc(layout)
139136
}
140137

141138
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
142139
if self.profiling.load(Ordering::SeqCst) {
143140
let mut guard = HEAP_PROFILER.write();
144141
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();
147143
let mut index = 0;
148-
144+
149145
backtrace::trace_unsynchronized(|frame| {
150146
if index < MAX_DEPTH {
151147
bt[index] = frame.clone();
@@ -163,4 +159,4 @@ unsafe impl<T: GlobalAlloc> GlobalAlloc for AllocRecorder<T> {
163159

164160
self.inner.dealloc(ptr, layout);
165161
}
166-
}
162+
}

‎src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ pub const MAX_THREAD_NAME: usize = 16;
3232
mod collector;
3333
mod error;
3434
mod frames;
35+
mod heap_profiler;
3536
mod profiler;
3637
mod report;
3738
mod timer;
38-
mod heap_profiler;
3939

4040
pub use self::collector::{Collector, StackHashCounter};
4141
pub use self::error::{Error, Result};
4242
pub use self::frames::{Frames, Symbol};
43+
pub use self::heap_profiler::{AllocRecorder, HeapProfilerGuard};
4344
pub use self::profiler::ProfilerGuard;
4445
pub use self::report::{Report, ReportBuilder};
45-
pub use self::heap_profiler::{AllocRecorder, HeapProfilerGuard};
4646

4747
#[cfg(feature = "protobuf")]
4848
pub mod protos {

‎src/profiler.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,13 @@ impl Profiler {
206206
}
207207

208208
// This function has to be AS-safe
209-
pub fn sample(&mut self, backtrace: &[Frame], thread_name: &[u8], thread_id: u64, count: isize) {
209+
pub fn sample(
210+
&mut self,
211+
backtrace: &[Frame],
212+
thread_name: &[u8],
213+
thread_id: u64,
214+
count: isize,
215+
) {
210216
let frames = UnresolvedFrames::new(backtrace, thread_name, thread_id);
211217
self.sample_counter += 1;
212218

0 commit comments

Comments
 (0)
Please sign in to comment.