Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
add more stats and backtraces
Browse files Browse the repository at this point in the history
  • Loading branch information
robik75 committed Dec 19, 2023
1 parent 68a20e4 commit d7086ce
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,13 +1092,13 @@ fn gpu_prove_from_trace<
println!("FRI Queries are done {:?}", time.elapsed());
#[cfg(feature = "allocator_stats")]
unsafe {
dbg!(_DEVICE_ALLOCATOR
_DEVICE_ALLOCATOR
.as_ref()
.unwrap()
.stats
.lock()
.unwrap()
.print(false));
.print(true);
}

gpu_proof.public_inputs = public_inputs;
Expand Down
131 changes: 90 additions & 41 deletions src/static_allocator/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,55 +24,77 @@ pub struct StaticDeviceAllocator {
// TODO: Can we use deque
bitmap: Arc<Vec<AtomicBool>>,
#[cfg(feature = "allocator_stats")]
pub stats: Arc<std::sync::Mutex<AllocationStats>>,
pub stats: Arc<std::sync::Mutex<stats::AllocationStats>>,
}

#[cfg(feature = "allocator_stats")]
#[derive(Derivative)]
#[derivative(Clone, Debug, Default)]
pub struct AllocationStats {
pub maximum_block_count: usize,
pub maximum_tail_index: usize,
pub allocations: std::collections::BTreeMap<usize, (usize, String)>,
}

#[cfg(feature = "allocator_stats")]
impl AllocationStats {
pub fn alloc(&mut self, index: usize, size: usize, backtrace: String) {
self.allocations.insert(index, (size, backtrace));
self.maximum_block_count = self.maximum_block_count.max(self.current_block_count());
self.maximum_tail_index = self.maximum_tail_index.max(index + size);
mod stats {
use derivative::Derivative;
use std::collections::BTreeMap;

type Allocations = BTreeMap<usize, (usize, String)>;

#[derive(Derivative)]
#[derivative(Clone, Debug, Default)]
pub struct AllocationStats {
pub allocations: Allocations,
pub maximum_block_count: usize,
pub maximum_tail_index: usize,
pub maximum_block_count_at_maximum_tail_index: usize,
pub allocations_at_maximum_block_count: Allocations,
pub allocations_at_maximum_block_count_at_maximum_tail_index: Allocations,
}

pub fn free(&mut self, index: usize) {
self.allocations.remove(&index);
}
impl AllocationStats {
pub fn alloc(&mut self, index: usize, size: usize, backtrace: String) {
self.allocations.insert(index, (size, backtrace));
let current_block_count = self.current_block_count();
let previous_maximum_block_count = self.maximum_block_count;
self.maximum_block_count = self.maximum_block_count.max(current_block_count);
let is_new_maximum_block_count =
self.maximum_block_count != previous_maximum_block_count;
if is_new_maximum_block_count {
self.allocations_at_maximum_block_count = self.allocations.clone();
}
let current_tail_index = index + size;
self.maximum_tail_index = self.maximum_tail_index.max(current_tail_index);
let is_maximum_tail_index = self.maximum_tail_index == current_tail_index;
if is_maximum_tail_index {
let previous_maximum_block_count_at_maximum_tail_index =
self.maximum_block_count_at_maximum_tail_index;
self.maximum_block_count_at_maximum_tail_index = self
.maximum_block_count_at_maximum_tail_index
.max(current_block_count);
let is_new_maximum_block_count_at_maximum_tail_index = self
.maximum_block_count_at_maximum_tail_index
!= previous_maximum_block_count_at_maximum_tail_index;
if is_new_maximum_block_count_at_maximum_tail_index {
self.allocations_at_maximum_block_count_at_maximum_tail_index =
self.allocations.clone();
}
}
}

pub fn current_block_count(&self) -> usize {
self.allocations.values().map(|&(size, _)| size).sum()
}
pub fn free(&mut self, index: usize) {
self.allocations.remove(&index);
}

pub fn current_tail_index(&self) -> usize {
self.allocations
.last_key_value()
.map_or(0, |(&index, &(size, _))| index + size)
}
pub fn current_block_count(&self) -> usize {
self.allocations.values().map(|&(size, _)| size).sum()
}

#[cfg(feature = "allocator_stats")]
pub fn print(&self, detailed: bool) {
const SEPARATOR: &str = "================================";
let AllocationStats {
maximum_block_count,
maximum_tail_index,
allocations,
} = self;
let current_block_count = self.current_block_count();
let current_tail_index = self.current_tail_index();
println!("current_block_count: {current_block_count}");
println!("current_tail_index: {current_tail_index}");
println!("maximum_block_count: {maximum_block_count}");
println!("maximum_tail_index: {maximum_tail_index}");
if detailed {
pub fn current_tail_index(&self) -> usize {
self.allocations
.last_key_value()
.map_or(0, |(&index, &(size, _))| index + size)
}

fn print_allocations(allocations: &Allocations) {
if allocations.is_empty() {
println!("no allocations");
return;
}
const SEPARATOR: &str = "================================";
println!("{SEPARATOR}");
let mut last_index = 0;
for (index, (length, trace)) in allocations {
Expand All @@ -88,6 +110,32 @@ impl AllocationStats {
println!("{SEPARATOR}");
}
}

pub fn print(&self, detailed: bool) {
let AllocationStats {
allocations,
maximum_block_count,
maximum_tail_index,
maximum_block_count_at_maximum_tail_index,
allocations_at_maximum_block_count,
allocations_at_maximum_block_count_at_maximum_tail_index,
} = self;
let current_block_count = self.current_block_count();
let current_tail_index = self.current_tail_index();
println!("current_block_count: {current_block_count}");
println!("current_tail_index: {current_tail_index}");
println!("maximum_block_count: {maximum_block_count}");
println!("maximum_tail_index: {maximum_tail_index}");
println!("maximum_block_count_at_maximum_tail_index: {maximum_block_count_at_maximum_tail_index}");
if detailed {
println!("current_allocations:");
Self::print_allocations(allocations);
println!("allocations_at_maximum_block_count:");
Self::print_allocations(allocations_at_maximum_block_count);
println!("allocations_at_maximum_block_count_at_maximum_tail_index:");
Self::print_allocations(allocations_at_maximum_block_count_at_maximum_tail_index);
}
}
}
}

Expand Down Expand Up @@ -252,6 +300,7 @@ macro_rules! backtrace {
.split('\n')
.rev()
.skip_while(|&s| !s.contains("shivini"))
.take_while(|&s| !s.contains("backtrace"))
.collect();
x.reverse();
let backtrace: String = x.join("\n");
Expand Down

0 comments on commit d7086ce

Please sign in to comment.