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
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//! The backend for the measurements.

use std::sync::{Arc, Mutex, MutexGuard, TryLockResult};
use std::time::{Duration, Instant};

use measurement_tracker::MeasurementTracker;

lazy_static! {
    pub(crate) static ref MEASUREMENT_STACK: Mutex<Vec<MeasurementRef>> =
        Mutex::new(vec![Measurement::new("root".to_string(), 0, None)]);
}

/// Starts a measurement in the current scope. **Don't use this, use
/// the [`perf_measure!`](macro.perf_measure.html) macro.**
pub fn measure<T: Into<String>>(now: Instant, measurement_name: T) -> MeasurementTracker {
    let name = measurement_name.into();
    let mut stack = MEASUREMENT_STACK.lock().unwrap();
    let depth = stack.len();

    let parent = stack.get(depth - 1).unwrap().clone();
    let measurement = Measurement::new(
        name.clone(),
        depth,
        Some(MeasurementRef::from(parent.clone())),
    );

    let mut parent = parent.get_mut();
    if let Some(existing_measurement) = parent.get_child(&name) {
        {
            let mut measurement = existing_measurement.get_mut();
            measurement.measuring_currently = true;
        }
        stack.push(existing_measurement.clone());
    } else {
        stack.push(measurement.clone());
        parent.children.push(measurement);
        parent.children_names.push(name);
    }

    MeasurementTracker {
        start_time: now,
        overhead: Instant::now() - now,
    }
}

impl Drop for MeasurementTracker {
    fn drop(&mut self) {
        let latter_overhead_start = Instant::now();
        let mut stack = MEASUREMENT_STACK.lock().unwrap();
        let latest_measurement = stack.pop().unwrap();
        let mut measurement = latest_measurement.get_mut();
        measurement.measuring_currently = false;
        measurement.overhead += self.overhead;
        measurement.durations.push(Instant::now() - self.start_time);
        measurement.overhead += Instant::now() - latter_overhead_start;
    }
}

/// Resets the measurement data.
///
/// **Warning**: This will wipe all measurements from the memory!
pub fn reset() {
    let stack = MEASUREMENT_STACK.lock().unwrap();
    let root = stack.get(0).unwrap().get_mut();
    let children = root.collect_all_children_arc();

    for i in 0..children.len() {
        let mut child = children[i].get_mut();
        if !child.measuring_currently {
            child.remove_while_locked();
        } else {
            child.clear_durations();
        }
    }
}

/// Returns a `Vec` of all the
/// [`Measurement`](struct.Measurement.html)s taken so far.
///
/// **Warning**: This function is pretty heavy, especially as the
/// amount of samples rises, as it clones every one of them.
pub(crate) fn get_measures() -> Vec<Measurement> {
    let stack = MEASUREMENT_STACK.lock().unwrap();
    let root = stack.get(0).unwrap().get_mut();
    root.collect_all_children()
}

#[derive(Clone, Debug)]
pub(crate) struct MeasurementRef {
    reference: Arc<Mutex<Measurement>>,
}

impl MeasurementRef {
    pub(crate) fn get_mut(&self) -> MutexGuard<Measurement> {
        match self.reference.try_lock() {
            Ok(measurement) => measurement,
            Err(err) => panic!("Failed to lock measurement! {}", err),
        }
    }

    fn try_get_mut(&self) -> TryLockResult<MutexGuard<Measurement>> {
        self.reference.try_lock()
    }
}

impl From<Arc<Mutex<Measurement>>> for MeasurementRef {
    fn from(t: Arc<Mutex<Measurement>>) -> Self {
        MeasurementRef { reference: t }
    }
}

/// Represents a scope's running time.
#[derive(Clone, Debug)]
pub(crate) struct Measurement {
    pub(crate) name: String,
    pub(crate) depth: usize,
    pub(crate) overhead: Duration,
    pub(crate) durations: Vec<Duration>,
    pub(crate) parent: Option<MeasurementRef>,
    children: Vec<MeasurementRef>,
    children_names: Vec<String>,
    measuring_currently: bool,
}

impl Measurement {
    fn new(name: String, depth: usize, parent: Option<MeasurementRef>) -> MeasurementRef {
        MeasurementRef::from(Arc::new(Mutex::new(Measurement {
            name,
            depth,
            overhead: Duration::new(0, 0),
            durations: Vec::new(),
            parent: parent.into(),
            children: Vec::new(),
            children_names: Vec::new(),
            measuring_currently: true,
        })))
    }

    pub(crate) fn get_ancestor(&self, generation: u32) -> Option<MeasurementRef> {
        if generation == 0 {
            if let Some(ref parent) = self.parent {
                Some(MeasurementRef::from(parent.clone()))
            } else {
                None
            }
        } else {
            if let Some(ref parent) = self.parent {
                let parent = parent.get_mut();
                parent.get_ancestor(generation - 1)
            } else {
                None
            }
        }
    }

    pub(crate) fn has_children(&self) -> bool {
        self.children.len() > 0
    }

    /// Is `name` the last child of `self`?
    pub(crate) fn is_last_child_name(&self, name: &str, leaf: bool) -> bool {
        if let Some(last_leaf_name) = self.last_child_name(leaf) {
            last_leaf_name == name
        } else {
            false
        }
    }

    fn last_child_name(&self, leaf: bool) -> Option<String> {
        if leaf && self.children.len() == 0 {
            None
        } else {
            Some(self.children_names[self.children.len() - 1].clone())
        }
    }

    pub(crate) fn collect_all_children(&self) -> Vec<Measurement> {
        let mut collection = Vec::new();
        collection.push(self.clone());
        for child in &self.children {
            collection.append(&mut child.get_mut().collect_all_children());
        }
        collection
    }

    pub(crate) fn get_duration_ns(&self) -> Option<u64> {
        let count = self.durations.len();
        if count == 0 {
            None
        } else {
            let mut total: u64 = 0;
            for duration in &self.durations {
                total += duration.subsec_nanos() as u64;
            }
            if total < self.get_overhead_ns() {
                // This should never happen, but technically it's possible.
                Some(0)
            } else {
                Some(total - self.get_overhead_ns())
            }
        }
    }

    pub(crate) fn get_overhead_ns(&self) -> u64 {
        let mut overhead =
            self.overhead.as_secs() * 1_000_000_000 + self.overhead.subsec_nanos() as u64;
        for child in &self.children {
            if let Ok(child) = child.try_get_mut() {
                overhead += child.get_overhead_ns();
            }
        }
        overhead
    }

    fn collect_all_children_arc(&self) -> Vec<MeasurementRef> {
        let mut collection = Vec::new();
        for child in &self.children {
            collection.push(child.clone());
            let child = child.get_mut();
            collection.append(&mut child.collect_all_children_arc());
        }
        collection
    }

    fn get_child(&mut self, name: &str) -> Option<MeasurementRef> {
        for child in &self.children {
            let mut child_lock = child.get_mut();
            let child_name = child_lock.name.clone();
            if child_name == name {
                return Some(child.clone());
            }
        }
        None
    }

    fn remove_locked_child(&mut self) {
        let children = &mut self.children;
        let mut remove_index = None;
        for i in 0..children.len() {
            if let Err(_) = children[i].try_get_mut() {
                remove_index = Some(i);
                break;
            }
        }
        if let Some(i) = remove_index {
            children.remove(i);
        }
    }

    fn remove_while_locked(&self) {
        if let Some(ref parent) = self.parent {
            let mut parent = parent.get_mut();
            parent.remove_locked_child();
        }
    }

    fn clear_durations(&mut self) {
        self.durations.clear();
        self.overhead = Duration::new(0, 0);
    }
}