Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
untitaker committed May 24, 2023
1 parent 6ccffed commit ea54af0
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 38 deletions.
26 changes: 14 additions & 12 deletions examples/hello.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Note: Keep in sync with README.md example
use num_enum::{TryFromPrimitive, IntoPrimitive};
use num_enum::{IntoPrimitive, TryFromPrimitive};

#[derive(TryFromPrimitive, IntoPrimitive, Default, Debug)]
#[repr(u32)]
Expand Down Expand Up @@ -33,15 +33,17 @@ fn main() {
process_data();

println!("memory usage stats:");
ALLOCATOR.with_recorder(|recorder| {
recorder.flush(
|usecase, stat| {
println!("{usecase:?}: {stat:?}");
},
|err, count| {
println!("{err:?}: {count}");
}
);
Ok(())
}).ok();
ALLOCATOR
.with_recorder(|recorder| {
recorder.flush(
|usecase, stat| {
println!("{usecase:?}: {stat:?}");
},
|err, count| {
println!("{err:?}: {count}");
},
);
Ok(())
})
.ok();
}
11 changes: 7 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ thread_local! {
static CURRENT_USECASE: RefCell<Option<UseCaseBytes>> = RefCell::new(None);
}


/// A drop-guard for setting and resetting the current usecase.
///
/// Returned by [Alloc::with_usecase].
Expand Down Expand Up @@ -125,7 +124,9 @@ impl<R: Recorder<U>, U: UseCase, A: GlobalAlloc> Alloc<U, R, A> {

fn handle_on_alloc(&self, ptr: usize, layout: Layout) {
self.synchronized(Some(layout.size()), |use_case_bytes| {
let use_case = use_case_bytes.and_then(|x| U::try_from(x).ok()).unwrap_or_default();
let use_case = use_case_bytes
.and_then(|x| U::try_from(x).ok())
.unwrap_or_default();
if self.recorder.on_alloc(use_case, layout.size()) {
TRACKED_POINTERS.insert(ptr, use_case_bytes.unwrap_or_else(|| U::default().into()));
}
Expand All @@ -137,8 +138,10 @@ impl<R: Recorder<U>, U: UseCase, A: GlobalAlloc> Alloc<U, R, A> {
fn handle_on_dealloc(&self, ptr: usize, layout: Layout) {
self.synchronized(Some(layout.size()), |_| {
if let Some((_, use_case_bytes)) = TRACKED_POINTERS.remove(&ptr) {
self.recorder
.on_dealloc(U::try_from(use_case_bytes).unwrap_or_default(), layout.size());
self.recorder.on_dealloc(
U::try_from(use_case_bytes).unwrap_or_default(),
layout.size(),
);
}
Ok(())
})
Expand Down
32 changes: 22 additions & 10 deletions src/recorder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt;
use std::ops::{DerefMut};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::marker::PhantomData;
use std::ops::DerefMut;
use std::sync::atomic::{AtomicUsize, Ordering};

use crate::{Error, Recorder, UseCase, UseCaseBytes};

Expand All @@ -26,7 +26,7 @@ impl<U: UseCase> StatsRecorder<U> {
current_usecase_contention_thread_local: AtomicUsize::new(0),
current_usecase_bad_bytes: AtomicUsize::new(0),
results: OnceCell::new(),
_phantom: PhantomData
_phantom: PhantomData,
}
}

Expand All @@ -37,10 +37,13 @@ impl<U: UseCase> StatsRecorder<U> {
pub fn get(&self, use_case: U) -> Stat {
let results = match self.results.get() {
Some(x) => x,
None => return Stat::default()
None => return Stat::default(),
};

results.get(&use_case.into()).map(|stat| stat.clone()).unwrap_or_default()
results
.get(&use_case.into())
.map(|stat| stat.clone())
.unwrap_or_default()
}

fn get_mut(&self, use_case: U) -> impl DerefMut<Target = Stat> + '_ {
Expand All @@ -56,11 +59,11 @@ impl<U: UseCase> StatsRecorder<U> {
Error::CurrentUsecaseContentionThreadLocal => {
&self.current_usecase_contention_thread_local
}
Error::CurrentUsecaseBadBytes => &self.current_usecase_bad_bytes
Error::CurrentUsecaseBadBytes => &self.current_usecase_bad_bytes,
}
}

/// Check how often an error has occurred
/// Check how often an error has occurred
pub fn get_error(&self, code: Error) -> usize {
self.get_error_atomic(code).load(Ordering::Relaxed)
}
Expand All @@ -76,9 +79,18 @@ impl<U: UseCase> StatsRecorder<U> {
results.clear();
}

error_fn(Error::CurrentUsecaseBadBytes, self.get_error(Error::CurrentUsecaseBadBytes));
error_fn(Error::CurrentUsecaseContentionRefCell, self.get_error(Error::CurrentUsecaseContentionRefCell));
error_fn(Error::CurrentUsecaseContentionThreadLocal, self.get_error(Error::CurrentUsecaseContentionThreadLocal));
error_fn(
Error::CurrentUsecaseBadBytes,
self.get_error(Error::CurrentUsecaseBadBytes),
);
error_fn(
Error::CurrentUsecaseContentionRefCell,
self.get_error(Error::CurrentUsecaseContentionRefCell),
);
error_fn(
Error::CurrentUsecaseContentionThreadLocal,
self.get_error(Error::CurrentUsecaseContentionThreadLocal),
);
}
}

Expand Down
8 changes: 1 addition & 7 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ pub type UseCaseBytes = u32;
///
/// impl UseCase for ApplicationStage {}
/// ```
pub trait UseCase:
Default
+ TryFrom<UseCaseBytes>
+ Into<UseCaseBytes>
+ 'static
{
}
pub trait UseCase: Default + TryFrom<UseCaseBytes> + Into<UseCaseBytes> + 'static {}

/// A recorder is a structure collecting statistics about memory usage. You might also call it a
/// "metrics sink".
Expand Down
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::MutexGuard;
use std::cell::Cell;
use std::marker::PhantomData;
use std::sync::MutexGuard;

// https://stackoverflow.com/a/71945606/1544347
pub type PhantomUnsend = PhantomData<MutexGuard<'static, ()>>;
Expand Down
9 changes: 5 additions & 4 deletions tests/basic.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use num_enum::{IntoPrimitive, TryFromPrimitive};
use pretty_assertions::assert_eq;
use num_enum::{TryFromPrimitive, IntoPrimitive};

use memento::{Alloc, UseCase, Stat, Error};
use memento::{Alloc, Error, Stat, UseCase};

#[derive(TryFromPrimitive, IntoPrimitive, Default, Debug, Ord, Eq, PartialEq, PartialOrd)]
#[repr(u32)]
enum MyUseCase {
#[default] None,
#[default]
None,
JsonPayload,
UserProfile,
ConfigFile,
Expand Down Expand Up @@ -55,7 +56,7 @@ fn basic() {
if count > 0 && err != Error::CurrentUsecaseContentionRefCell {
panic!("unexpected error: {:?}", err);
}
}
},
);
records.sort();
assert_eq!(
Expand Down

0 comments on commit ea54af0

Please sign in to comment.