Skip to content

Commit

Permalink
tikv_alloc/*: Add guard for profiler
Browse files Browse the repository at this point in the history
Impl drop for ProfilerGuard to deactivate_prof, but not use Drop for ProfilerLock.
With this approach once we drop the guard, the lock was released.

Signed-off-by: Yang Keao <keao.yang@yahoo.com>
  • Loading branch information
YangKeao committed May 7, 2019
1 parent c35e081 commit beb9027
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 2 additions & 0 deletions components/tikv_alloc/src/jemalloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ mod profiling {
}

pub fn activate_prof() -> ProfResult<()> {
info!("Start Profiler");
unsafe {
if let Err(e) = jemallocator::mallctl_set(PROF_ACTIVE, true) {
error!("failed to activate profiling: {}", e);
Expand All @@ -103,6 +104,7 @@ mod profiling {
}

pub fn deactivate_prof() -> ProfResult<()> {
info!("Stop Profiler");
unsafe {
if let Err(e) = jemallocator::mallctl_set(PROF_ACTIVE, false) {
error!("failed to deactivate profiling: {}", e);
Expand Down
10 changes: 7 additions & 3 deletions components/tikv_alloc/src/profiler_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ lazy_static! {
static ref PROFILER_MUTEX: Mutex<u32> = Mutex::new(0);
}

pub struct ProfilerGuard(MutexGuard<u32>);

pub struct ProfilerLock(MutexFut<u32>);

impl ProfilerLock {
Expand All @@ -19,7 +21,7 @@ impl ProfilerLock {
}
}

impl Drop for ProfilerLock {
impl Drop for ProfilerGuard {
fn drop(&mut self) {
match deactivate_prof() {
_ => {} // TODO: handle error here
Expand All @@ -28,9 +30,11 @@ impl Drop for ProfilerLock {
}

impl Future for ProfilerLock {
type Item = MutexGuard<u32>;
type Item = ProfilerGuard;
type Error = ();
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.0.poll()
self.0
.poll()
.map(|item| item.map(|guard| ProfilerGuard(guard)))
}
}

0 comments on commit beb9027

Please sign in to comment.