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
use std::fmt::{Display, Formatter, Result};
pub use self::collision_detection_counters::CollisionDetectionCounters;
pub use self::solver_counters::SolverCounters;
pub use self::stages_counters::StagesCounters;
pub use self::timer::Timer;
mod collision_detection_counters;
mod solver_counters;
mod stages_counters;
mod timer;
pub struct Counters {
enabled: bool,
step_time: Timer,
custom: Timer,
stages: StagesCounters,
cd: CollisionDetectionCounters,
solver: SolverCounters,
}
impl Counters {
pub fn new(enabled: bool) -> Self {
Counters {
enabled,
step_time: Timer::new(),
custom: Timer::new(),
stages: StagesCounters::new(),
cd: CollisionDetectionCounters::new(),
solver: SolverCounters::new(),
}
}
pub fn enable(&mut self) {
self.enabled = true;
}
pub fn enabled(&self) -> bool {
self.enabled
}
pub fn disable(&mut self) {
self.enabled = false;
}
pub fn step_started(&mut self) {
if self.enabled {
self.step_time.start();
}
}
pub fn step_completed(&mut self) {
if self.enabled {
self.step_time.pause();
}
}
pub fn step_time(&self) -> f64 {
self.step_time.time()
}
pub fn custom_started(&mut self) {
if self.enabled {
self.custom.start();
}
}
pub fn custom_completed(&mut self) {
if self.enabled {
self.custom.pause();
}
}
pub fn custom_time(&self) -> f64 {
self.custom.time()
}
pub fn set_nconstraints(&mut self, n: usize) {
self.solver.nconstraints = n;
}
pub fn set_ncontacts(&mut self, n: usize) {
self.solver.ncontacts = n;
}
pub fn set_ncontact_pairs(&mut self, n: usize) {
self.cd.ncontact_pairs = n;
}
}
macro_rules! measure_method {
($started:ident, $stopped:ident, $time:ident, $info:ident. $timer:ident) => {
impl Counters {
pub fn $started(&mut self) {
if self.enabled {
self.$info.$timer.start()
}
}
pub fn $stopped(&mut self) {
if self.enabled {
self.$info.$timer.pause()
}
}
pub fn $time(&self) -> f64 {
if self.enabled {
self.$info.$timer.time()
} else {
0.0
}
}
}
};
}
measure_method!(
update_started,
update_completed,
update_time,
stages.update_time
);
measure_method!(
collision_detection_started,
collision_detection_completed,
collision_detection_time,
stages.collision_detection_time
);
measure_method!(
island_construction_started,
island_construction_completed,
island_construction_time,
stages.island_construction_time
);
measure_method!(
solver_started,
solver_completed,
solver_time,
stages.solver_time
);
measure_method!(
assembly_started,
assembly_completed,
assembly_time,
solver.assembly_time
);
measure_method!(
velocity_resolution_started,
velocity_resolution_completed,
velocity_resolution_time,
solver.velocity_resolution_time
);
measure_method!(
velocity_update_started,
velocity_update_completed,
velocity_update_time,
solver.velocity_update_time
);
measure_method!(
position_resolution_started,
position_resolution_completed,
position_resolution_time,
solver.position_resolution_time
);
measure_method!(
broad_phase_started,
broad_phase_completed,
broad_phase_time,
cd.broad_phase_time
);
measure_method!(
narrow_phase_started,
narrow_phase_completed,
narrow_phase_time,
cd.narrow_phase_time
);
impl Display for Counters {
fn fmt(&self, f: &mut Formatter) -> Result {
writeln!(f, "Total timestep time: {}", self.step_time)?;
self.stages.fmt(f)?;
self.cd.fmt(f)?;
self.solver.fmt(f)?;
writeln!(f, "Custom timer: {}", self.custom)
}
}