-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmemory_tracking.rs
More file actions
80 lines (63 loc) · 1.94 KB
/
memory_tracking.rs
File metadata and controls
80 lines (63 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use criterion::measurement::{Measurement, ValueFormatter};
use polytune_test_utils::peak_alloc::{MAX_PARTIES, scale_memory};
use crate::ALLOCATOR;
/// Criterion [`Measurement`] to use with [`polytune_test_utils::peak_alloc::PeakAllocator`]
/// and [`polytune_test_utils::peak_alloc::create_instrumented_runtime`].
#[derive(Copy, Clone, Debug)]
pub struct MemoryMeasurement {
party: usize,
}
impl MemoryMeasurement {
pub fn new(party: usize) -> Self {
assert!(party < MAX_PARTIES, "Only {MAX_PARTIES} are supported.");
Self { party }
}
}
impl Measurement for MemoryMeasurement {
type Intermediate = usize;
type Value = usize;
fn start(&self) -> Self::Intermediate {
ALLOCATOR.reset();
ALLOCATOR.enable();
ALLOCATOR.peak(self.party)
}
fn end(&self, i: Self::Intermediate) -> Self::Value {
ALLOCATOR.disable();
ALLOCATOR.peak(self.party) - i
}
fn add(&self, v1: &Self::Value, v2: &Self::Value) -> Self::Value {
v1 + v2
}
fn zero(&self) -> Self::Value {
0
}
fn to_f64(&self, value: &Self::Value) -> f64 {
*value as f64
}
fn formatter(&self) -> &dyn ValueFormatter {
&MemoryFormatter
}
}
pub struct MemoryFormatter;
// Implementation based on `DurationFormatter` in criterion.
impl ValueFormatter for MemoryFormatter {
fn scale_values(&self, typical_value: f64, values: &mut [f64]) -> &'static str {
let (denom, unit) = scale_memory(typical_value);
for val in values.iter_mut() {
*val /= denom;
}
unit
}
fn scale_throughputs(
&self,
_typical_value: f64,
_throughput: &criterion::Throughput,
_values: &mut [f64],
) -> &'static str {
unimplemented!("Throughput makes no sense for peak memory")
}
fn scale_for_machines(&self, _values: &mut [f64]) -> &'static str {
// Don't scale
" B"
}
}