nphysics2d/counters/
mod.rs

1//! Counters for benchmarking various parts of the physics engine.
2
3use std::fmt::{Display, Formatter, Result};
4
5pub use self::ccd_counters::CCDCounters;
6pub use self::collision_detection_counters::CollisionDetectionCounters;
7pub use self::solver_counters::SolverCounters;
8pub use self::stages_counters::StagesCounters;
9pub use self::timer::Timer;
10
11mod ccd_counters;
12mod collision_detection_counters;
13mod solver_counters;
14mod stages_counters;
15mod timer;
16
17/// Aggregation of all the performances counters tracked by nphysics.
18#[derive(Clone, Copy)]
19pub struct Counters {
20    /// Whether thi counter is enabled or not.
21    pub enabled: bool,
22    /// Timer for a whole timestep.
23    pub step_time: Timer,
24    /// Timer used for debugging.
25    pub custom: Timer,
26    /// Counters of every satge of one time step.
27    pub stages: StagesCounters,
28    /// Counters of the collision-detection stage.
29    pub cd: CollisionDetectionCounters,
30    /// Counters of the constraints resolution and force computation stage.
31    pub solver: SolverCounters,
32    /// Counters for the CCD resolution stage.
33    pub ccd: CCDCounters,
34}
35
36impl Counters {
37    /// Create a new set of counters initialized to wero.
38    pub fn new(enabled: bool) -> Self {
39        Counters {
40            enabled,
41            step_time: Timer::new(),
42            custom: Timer::new(),
43            stages: StagesCounters::new(),
44            cd: CollisionDetectionCounters::new(),
45            solver: SolverCounters::new(),
46            ccd: CCDCounters::new(),
47        }
48    }
49
50    /// Enable all the counters.
51    pub fn enable(&mut self) {
52        self.enabled = true;
53    }
54
55    /// Return `true` if the counters are enabled.
56    pub fn enabled(&self) -> bool {
57        self.enabled
58    }
59
60    /// Disable all the counters.
61    pub fn disable(&mut self) {
62        self.enabled = false;
63    }
64
65    /// Notify that the time-step has started.
66    pub fn step_started(&mut self) {
67        if self.enabled {
68            self.step_time.start();
69        }
70    }
71
72    /// Notfy that the time-step has finished.
73    pub fn step_completed(&mut self) {
74        if self.enabled {
75            self.step_time.pause();
76        }
77    }
78
79    /// Total time spent for one  of the physics engine.
80    pub fn step_time(&self) -> f64 {
81        self.step_time.time()
82    }
83
84    /// Notify that the custom operation has started.
85    pub fn custom_started(&mut self) {
86        if self.enabled {
87            self.custom.start();
88        }
89    }
90
91    /// Notfy that the custom operation has finished.
92    pub fn custom_completed(&mut self) {
93        if self.enabled {
94            self.custom.pause();
95        }
96    }
97
98    /// Total time of a custom event.
99    pub fn custom_time(&self) -> f64 {
100        self.custom.time()
101    }
102
103    /// Set the number of constraints generated.
104    pub fn set_nconstraints(&mut self, n: usize) {
105        self.solver.nconstraints = n;
106    }
107
108    /// Set the number of contacts generated.
109    pub fn set_ncontacts(&mut self, n: usize) {
110        self.solver.ncontacts = n;
111    }
112
113    /// Set the number of contact pairs generated.
114    pub fn set_ncontact_pairs(&mut self, n: usize) {
115        self.cd.ncontact_pairs = n;
116    }
117}
118
119macro_rules! measure_method {
120    ($started:ident, $stopped:ident, $time:ident, $info:ident. $timer:ident) => {
121        impl Counters {
122            /// Start this timer.
123            pub fn $started(&mut self) {
124                if self.enabled {
125                    self.$info.$timer.start()
126                }
127            }
128
129            /// Stop this timer.
130            pub fn $stopped(&mut self) {
131                if self.enabled {
132                    self.$info.$timer.pause()
133                }
134            }
135
136            /// Gets the time elapsed for this timer.
137            pub fn $time(&self) -> f64 {
138                if self.enabled {
139                    self.$info.$timer.time()
140                } else {
141                    0.0
142                }
143            }
144        }
145    };
146}
147
148measure_method!(
149    update_started,
150    update_completed,
151    update_time,
152    stages.update_time
153);
154measure_method!(
155    collision_detection_started,
156    collision_detection_completed,
157    collision_detection_time,
158    stages.collision_detection_time
159);
160measure_method!(
161    island_construction_started,
162    island_construction_completed,
163    island_construction_time,
164    stages.island_construction_time
165);
166measure_method!(
167    solver_started,
168    solver_completed,
169    solver_time,
170    stages.solver_time
171);
172measure_method!(ccd_started, ccd_completed, ccd_time, stages.ccd_time);
173
174measure_method!(
175    assembly_started,
176    assembly_completed,
177    assembly_time,
178    solver.assembly_time
179);
180measure_method!(
181    velocity_resolution_started,
182    velocity_resolution_completed,
183    velocity_resolution_time,
184    solver.velocity_resolution_time
185);
186measure_method!(
187    velocity_update_started,
188    velocity_update_completed,
189    velocity_update_time,
190    solver.velocity_update_time
191);
192measure_method!(
193    position_resolution_started,
194    position_resolution_completed,
195    position_resolution_time,
196    solver.position_resolution_time
197);
198measure_method!(
199    broad_phase_started,
200    broad_phase_completed,
201    broad_phase_time,
202    cd.broad_phase_time
203);
204measure_method!(
205    narrow_phase_started,
206    narrow_phase_completed,
207    narrow_phase_time,
208    cd.narrow_phase_time
209);
210
211impl Display for Counters {
212    fn fmt(&self, f: &mut Formatter) -> Result {
213        writeln!(f, "Total timestep time: {}", self.step_time)?;
214        self.stages.fmt(f)?;
215        self.cd.fmt(f)?;
216        self.solver.fmt(f)?;
217        writeln!(f, "Custom timer: {}", self.custom)
218    }
219}