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
use alloc::{string::String, vec::Vec};
use core::{time, time::Duration};
use crate::utils::current_time;
const CLIENT_STATS_TIME_WINDOW_SECS: u64 = 5;
#[derive(Debug, Clone, Default)]
pub struct ClientStats {
pub corpus_size: u64,
pub executions: u64,
pub objective_size: u64,
pub last_window_executions: u64,
pub last_window_time: time::Duration,
pub last_execs_per_sec: f32,
}
impl ClientStats {
pub fn update_executions(&mut self, executions: u64, cur_time: time::Duration) {
let diff = cur_time
.checked_sub(self.last_window_time)
.map_or(0, |d| d.as_secs());
if diff > CLIENT_STATS_TIME_WINDOW_SECS {
let _ = self.execs_per_sec(cur_time);
self.last_window_time = cur_time;
self.last_window_executions = self.executions;
}
self.executions = executions;
}
pub fn update_corpus_size(&mut self, corpus_size: u64) {
self.corpus_size = corpus_size;
}
pub fn update_objective_size(&mut self, objective_size: u64) {
self.objective_size = objective_size;
}
#[allow(clippy::cast_sign_loss, clippy::cast_precision_loss)]
pub fn execs_per_sec(&mut self, cur_time: time::Duration) -> u64 {
if self.executions == 0 {
return 0;
}
let elapsed = cur_time
.checked_sub(self.last_window_time)
.map_or(0, |d| d.as_secs());
if elapsed == 0 {
return self.last_execs_per_sec as u64;
}
let cur_avg = ((self.executions - self.last_window_executions) as f32) / (elapsed as f32);
if self.last_window_executions == 0 {
self.last_execs_per_sec = cur_avg;
return self.last_execs_per_sec as u64;
}
if cur_avg * 5.0 < self.last_execs_per_sec || cur_avg / 5.0 > self.last_execs_per_sec {
self.last_execs_per_sec = cur_avg;
}
self.last_execs_per_sec =
self.last_execs_per_sec * (1.0 - 1.0 / 16.0) + cur_avg * (1.0 / 16.0);
self.last_execs_per_sec as u64
}
}
pub trait Stats {
fn client_stats_mut(&mut self) -> &mut Vec<ClientStats>;
fn client_stats(&self) -> &[ClientStats];
fn start_time(&mut self) -> time::Duration;
fn display(&mut self, event_msg: String);
fn corpus_size(&self) -> u64 {
self.client_stats()
.iter()
.fold(0_u64, |acc, x| acc + x.corpus_size)
}
fn objective_size(&self) -> u64 {
self.client_stats()
.iter()
.fold(0_u64, |acc, x| acc + x.objective_size)
}
#[inline]
fn total_execs(&mut self) -> u64 {
self.client_stats()
.iter()
.fold(0_u64, |acc, x| acc + x.executions)
}
#[inline]
fn execs_per_sec(&mut self) -> u64 {
let cur_time = current_time();
self.client_stats_mut()
.iter_mut()
.fold(0_u64, |acc, x| acc + x.execs_per_sec(cur_time))
}
fn client_stats_mut_for(&mut self, client_id: u32) -> &mut ClientStats {
let client_stat_count = self.client_stats().len();
for _ in client_stat_count..(client_id + 1) as usize {
self.client_stats_mut().push(ClientStats {
last_window_time: current_time(),
..ClientStats::default()
})
}
&mut self.client_stats_mut()[client_id as usize]
}
}
#[derive(Clone, Debug)]
pub struct SimpleStats<F>
where
F: FnMut(String),
{
print_fn: F,
start_time: Duration,
corpus_size: usize,
client_stats: Vec<ClientStats>,
}
impl<F> Stats for SimpleStats<F>
where
F: FnMut(String),
{
fn client_stats_mut(&mut self) -> &mut Vec<ClientStats> {
&mut self.client_stats
}
fn client_stats(&self) -> &[ClientStats] {
&self.client_stats
}
fn start_time(&mut self) -> time::Duration {
self.start_time
}
fn display(&mut self, event_msg: String) {
let fmt = format!(
"[{}] clients: {}, corpus: {}, objectives: {}, executions: {}, exec/sec: {}",
event_msg,
self.client_stats().len(),
self.corpus_size(),
self.objective_size(),
self.total_execs(),
self.execs_per_sec()
);
(self.print_fn)(fmt);
}
}
impl<F> SimpleStats<F>
where
F: FnMut(String),
{
pub fn new(print_fn: F) -> Self {
Self {
print_fn,
start_time: current_time(),
corpus_size: 0,
client_stats: vec![],
}
}
pub fn with_time(print_fn: F, start_time: time::Duration) -> Self {
Self {
print_fn,
start_time,
corpus_size: 0,
client_stats: vec![],
}
}
}