Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,16 @@ pub struct Allocator {
ghost_pairs: usize,
ghost_heap: usize,

// these counters track the largest number of allocations. It's used to
// indicate the memory pressure caused by running a CLVM program. They don't
// take "ghost" atoms and pairs into account for this reason.
#[cfg(feature = "counters")]
max_atom_count: usize,
#[cfg(feature = "counters")]
max_pair_count: usize,
#[cfg(feature = "counters")]
max_heap_size: usize,

#[cfg(feature = "allocator-debug")]
// fingerprints are 24 bits
fingerprint: u32,
Expand Down Expand Up @@ -320,6 +330,12 @@ impl Allocator {
ghost_atoms: 2,
ghost_pairs: 0,
ghost_heap: 0,
#[cfg(feature = "counters")]
max_atom_count: 0,
#[cfg(feature = "counters")]
max_pair_count: 0,
#[cfg(feature = "counters")]
max_heap_size: 0,

// fingerprints are 24 bits
#[cfg(feature = "allocator-debug")]
Expand Down Expand Up @@ -439,6 +455,8 @@ impl Allocator {
self.u8_vec.extend_from_slice(v);
let end = self.u8_vec.len() as u32;
self.atom_vec.push(AtomBuf { start, end });
#[cfg(feature = "counters")]
self.update_max_counts();
Ok(self.mk_node(ObjectType::Bytes, idx))
}
}
Expand Down Expand Up @@ -489,6 +507,8 @@ impl Allocator {
return Err(EvalErr::TooManyPairs);
}
self.pair_vec.push(IntPair { first, rest });
#[cfg(feature = "counters")]
self.update_max_counts();
Ok(self.mk_node(ObjectType::Pair, idx))
}

Expand Down Expand Up @@ -560,6 +580,8 @@ impl Allocator {
start: atom.start + start,
end: atom.start + end,
});
#[cfg(feature = "counters")]
self.update_max_counts();
Ok(self.mk_node(ObjectType::Bytes, idx))
}
ObjectType::SmallAtom => {
Expand All @@ -581,6 +603,8 @@ impl Allocator {
start: start as u32,
end: end as u32,
});
#[cfg(feature = "counters")]
self.update_max_counts();
Ok(self.mk_node(ObjectType::Bytes, idx))
}
}
Expand Down Expand Up @@ -674,6 +698,8 @@ impl Allocator {
start: start as u32,
end,
});
#[cfg(feature = "counters")]
self.update_max_counts();
Ok(self.mk_node(ObjectType::Bytes, idx))
}

Expand Down Expand Up @@ -954,6 +980,31 @@ impl Allocator {
pub fn heap_size(&self) -> usize {
self.u8_vec.len()
}

#[cfg(feature = "counters")]
pub fn max_atom_count(&self) -> usize {
self.max_atom_count
}

#[cfg(feature = "counters")]
pub fn max_pair_count(&self) -> usize {
self.max_pair_count
}

#[cfg(feature = "counters")]
pub fn max_heap_size(&self) -> usize {
self.max_heap_size
}

#[cfg(feature = "counters")]
fn update_max_counts(&mut self) {
let atom_count = self.atom_vec.len();
self.max_atom_count = std::cmp::max(self.max_atom_count, atom_count);
let pair_count = self.pair_vec.len();
self.max_pair_count = std::cmp::max(self.max_pair_count, pair_count);
let heap_size = self.u8_vec.len();
self.max_heap_size = std::cmp::max(self.max_heap_size, heap_size);
}
Comment thread
cursor[bot] marked this conversation as resolved.
}

#[cfg(test)]
Expand Down Expand Up @@ -1578,16 +1629,26 @@ mod tests {
let checkpoint = a.checkpoint();

let atom2 = a.new_atom(&[6, 5, 4, 3]).unwrap();
let _pair1 = a.new_pair(atom1, atom2).unwrap();
assert!(a.atom(atom1).as_ref() == [4, 3, 2, 1]);
assert!(a.atom(atom2).as_ref() == [6, 5, 4, 3]);

#[cfg(feature = "counters")]
let prev_counters = (a.max_atom_count(), a.max_pair_count(), a.max_heap_size());

// at this point we have two atoms and a checkpoint from before the second
// atom was created

// now, restoring the checkpoint state will make atom2 disappear

a.restore_checkpoint(&checkpoint);

#[cfg(feature = "counters")]
assert_eq!(
(a.max_atom_count(), a.max_pair_count(), a.max_heap_size()),
prev_counters
);

assert!(a.atom(atom1).as_ref() == [4, 3, 2, 1]);
let atom3 = a.new_atom(&[6, 5, 4, 3]).unwrap();
assert!(a.atom(atom3).as_ref() == [6, 5, 4, 3]);
Expand Down
9 changes: 9 additions & 0 deletions src/run_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ pub struct Counters {
pub pair_count: u32,
pub allocated_pair_count: u32,
pub heap_size: u32,
pub max_atom_count: u32,
pub max_pair_count: u32,
pub max_heap_size: u32,
}

#[cfg(feature = "counters")]
Expand All @@ -61,6 +64,9 @@ impl Counters {
pair_count: 0,
allocated_pair_count: 0,
heap_size: 0,
max_atom_count: 0,
max_pair_count: 0,
max_heap_size: 0,
}
}
}
Expand Down Expand Up @@ -543,6 +549,9 @@ pub fn run_program_with_counters<'a, D: Dialect>(
rpc.counters.pair_count = rpc.allocator.pair_count() as u32;
rpc.counters.allocated_pair_count = rpc.allocator.allocated_pair_count() as u32;
rpc.counters.heap_size = rpc.allocator.heap_size() as u32;
rpc.counters.max_atom_count = rpc.allocator.max_atom_count() as u32;
rpc.counters.max_pair_count = rpc.allocator.max_pair_count() as u32;
rpc.counters.max_heap_size = rpc.allocator.max_heap_size() as u32;
(rpc.counters, ret)
}

Expand Down
Loading