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
use super::*;
use std::sync::Arc;
use std::sync::Mutex;

pub struct SizeCounter {
    units: Arc<Mutex<EventCounter>>,
    volume: Arc<Mutex<EventCounter>>,
}

/**
 * This file implements a simple counter of events of different sizes
 * (eg object allocations, where total number of objects and total
 * volume of objects would be counted).
 *
 * The counter is trivially composed from two event counters (one for
 * counting the number of events, the other for counting the volume).
 */
impl SizeCounter {
    pub fn new(units: Arc<Mutex<EventCounter>>, volume: Arc<Mutex<EventCounter>>) -> Self {
        SizeCounter { units, volume }
    }

    /**
     * Increment the event counter by provided value
     */
    pub fn inc(&mut self, size: u64) {
        self.units.lock().unwrap().inc();
        self.volume.lock().unwrap().inc_by(size);
    }

    /**
     * Start this counter
     */
    pub fn start(&mut self) {
        self.units.lock().unwrap().start();
        self.volume.lock().unwrap().start();
    }

    /**
     * Stop this counter
     */
    pub fn stop(&mut self) {
        self.units.lock().unwrap().stop();
        self.volume.lock().unwrap().stop();
    }

    /**
     * Print current (mid-phase) units
     */
    pub fn print_current_units(&self) {
        self.units.lock().unwrap().print_current();
    }

    /**
     * Print (mid-phase) volume
     */
    pub fn print_current_volume(&self) {
        self.volume.lock().unwrap().print_current();
    }

    /**
     * Print units
     */
    pub fn print_units(&self) {
        self.units.lock().unwrap().print_total(None);
    }

    /**
     * Print volume
     */
    pub fn print_volume(&self) {
        self.volume.lock().unwrap().print_total(None);
    }
}