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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
use anyhow::anyhow;
use bytecode::options::ProverOptions;
use clap::{App, Arg};
use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};
use itertools::Itertools;
use log::LevelFilter;
use move_model::{
model::{FunctionEnv, GlobalEnv, ModuleEnv, VerificationScope},
parse_addresses_from_options, run_model_builder_with_options,
};
use move_prover::{
check_errors, cli::Options, create_and_process_bytecode, generate_boogie, verify_boogie,
};
use std::{
fmt::Debug,
fs::File,
io::{LineWriter, Write},
path::PathBuf,
time::{Duration, Instant},
};
struct Runner {
options: Options,
out: LineWriter<File>,
error_writer: StandardStream,
per_function: bool,
}
pub fn benchmark(args: &[String]) {
let cmd_line_parser = App::new("benchmark")
.version("0.1.0")
.about("Benchmark program for the Move Prover")
.author("The Diem Core Contributors")
.arg(
Arg::with_name("config")
.short("c")
.long("config")
.takes_value(true)
.multiple(true)
.number_of_values(1)
.value_name("CONFIG_PATH")
.help(
"path to a prover toml configuration file. The benchmark output will be \
stored at `CONFIG_PATH.data. This can be repeated so different \
configurations are benchmarked against the same set of input modules.",
),
)
.arg(
Arg::with_name("function")
.short("f")
.long("func")
.help("whether benchmarking should happen per function; default is per module"),
)
.arg(
Arg::with_name("dependencies")
.long("dependency")
.short("d")
.multiple(true)
.number_of_values(1)
.takes_value(true)
.value_name("PATH_TO_DEPENDENCY")
.help(
"path to a Move file, or a directory which will be searched for \
Move files, containing dependencies which will not be verified",
),
)
.arg(
Arg::with_name("sources")
.multiple(true)
.value_name("PATH_TO_SOURCE_FILE")
.min_values(1)
.help("the source files to verify"),
);
let matches = cmd_line_parser.get_matches_from(args);
let get_vec = |s: &str| -> Vec<String> {
match matches.values_of(s) {
Some(vs) => vs.map(|v| v.to_string()).collect(),
_ => vec![],
}
};
let sources = get_vec("sources");
let deps = get_vec("dependencies");
let configs: Vec<Option<String>> = if matches.is_present("config") {
get_vec("config").into_iter().map(Some).collect_vec()
} else {
vec![None]
};
let per_function = matches.is_present("function");
for config_spec in configs {
let (config, out) = if let Some(config_file) = &config_spec {
let extension = if per_function { "fun_data" } else { "mod_data" };
let out = PathBuf::from(config_file)
.with_extension(extension)
.to_string_lossy()
.to_string();
(config_spec, out)
} else {
(None, "benchmark.data".to_string())
};
if let Err(s) = run_benchmark(&out, config.as_ref(), &sources, &deps, per_function) {
println!("ERROR: execution failed: {}", s);
} else {
println!("results stored at `{}`", out);
}
}
}
fn run_benchmark(
out: &str,
config_file_opt: Option<&String>,
modules: &[String],
dep_dirs: &[String],
per_function: bool,
) -> anyhow::Result<()> {
let mut options = if let Some(config_file) = config_file_opt {
Options::create_from_toml_file(config_file)?
} else {
Options::default()
};
let env = run_model_builder_with_options(
modules,
dep_dirs,
options.model_builder.clone(),
parse_addresses_from_options(options.move_named_address_values.clone())?,
)?;
let mut error_writer = StandardStream::stderr(ColorChoice::Auto);
options.backend.hard_timeout_secs = 60;
options.verbosity_level = LevelFilter::Warn;
options.backend.proc_cores = 1;
options.backend.derive_options();
options.setup_logging();
check_errors(&env, &options, &mut error_writer, "unexpected build errors")?;
let config_descr = if let Some(config) = config_file_opt {
config.clone()
} else {
"default".to_string()
};
let mut out = LineWriter::new(File::create(out)?);
writeln!(out, "# config: {}", config_descr)?;
writeln!(out, "# time : {}", chrono::Utc::now())?;
let mut runner = Runner {
options,
out,
error_writer,
per_function,
};
println!(
"Starting benchmarking with config `{}`.\n\
Notice that execution is slow because we enforce single core execution.",
config_descr
);
runner.bench(&env)
}
impl Runner {
fn bench(&mut self, env: &GlobalEnv) -> anyhow::Result<()> {
for module in env.get_modules() {
if module.is_target() {
if self.per_function {
for fun in module.get_functions() {
self.bench_function(fun)?;
}
} else {
self.bench_module(module)?;
}
}
}
Ok(())
}
fn bench_function(&mut self, fun: FunctionEnv<'_>) -> anyhow::Result<()> {
print!("benchmarking function {} ..", fun.get_full_name_str());
std::io::stdout().flush()?;
let env = fun.module_env.env;
self.options.prover.verify_scope = VerificationScope::Only(fun.get_full_name_str());
ProverOptions::set(env, self.options.prover.clone());
let (duration, status) = self.bench_function_or_module(fun.module_env.env)?;
writeln!(
self.out,
"{:<40} {:>12} {:>12}",
fun.get_full_name_str(),
duration.as_millis(),
status
)?;
println!("\x08\x08{:.3}s {}.", duration.as_secs_f64(), status);
Ok(())
}
fn bench_module(&mut self, module: ModuleEnv<'_>) -> anyhow::Result<()> {
print!("benchmarking module {} ..", module.get_full_name_str());
std::io::stdout().flush()?;
self.options.prover.verify_scope =
VerificationScope::OnlyModule(module.get_full_name_str());
ProverOptions::set(module.env, self.options.prover.clone());
let (duration, status) = self.bench_function_or_module(module.env)?;
writeln!(
self.out,
"{:<40} {:>12} {:>12}",
module.get_full_name_str(),
duration.as_millis(),
status
)?;
println!("\x08\x08{:.3}s {}.", duration.as_secs_f64(), status);
Ok(())
}
fn bench_function_or_module(&mut self, env: &GlobalEnv) -> anyhow::Result<(Duration, String)> {
let targets = create_and_process_bytecode(&self.options, env);
check_errors(
env,
&self.options,
&mut self.error_writer,
"unexpected transformation errors",
)?;
let code_writer = generate_boogie(env, &self.options, &targets)?;
check_errors(
env,
&self.options,
&mut self.error_writer,
"unexpected boogie generation errors",
)?;
let now = Instant::now();
verify_boogie(env, &self.options, &targets, code_writer)?;
let status = if env.error_count() > 0 {
if env.has_diag("timeout") {
"timeout"
} else {
"errors"
}
} else {
"ok"
};
env.clear_diag();
Ok((now.elapsed(), status.to_string()))
}
}
#[derive(Clone, Debug)]
pub struct Benchmark {
pub config: String,
pub data: Vec<BenchmarkData>,
}
#[derive(Clone, Debug)]
pub struct BenchmarkData {
pub name: String,
pub duration: usize,
pub status: String,
}
pub fn read_benchmark(data_file: &str) -> anyhow::Result<Benchmark> {
let config = PathBuf::from(data_file)
.with_extension("") .file_name() .ok_or_else(|| anyhow!("invalid data file name"))?
.to_string_lossy()
.to_string();
let content = std::fs::read_to_string(data_file)?;
let mut data = vec![];
for line in content.lines() {
if line.starts_with('#') {
continue;
}
let parts = line.split_whitespace().collect_vec();
if parts.len() != 3 {
return Err(anyhow!("bad data entry"));
}
let name = parts[0].to_string();
let duration = parts[1].parse::<usize>()?;
let status = parts[2].to_string();
data.push(BenchmarkData {
name,
duration,
status,
});
}
Ok(Benchmark { config, data })
}
impl Benchmark {
pub fn sort(&mut self) {
self.data
.sort_by(|d1, d2| d1.duration.cmp(&d2.duration).reverse());
}
pub fn take(&mut self, count: usize) {
self.data.truncate(count)
}
pub fn sum(&self) -> u32 {
self.data
.iter()
.filter_map(|d| {
if d.status == "ok" || d.status == "error" {
Some(d.duration as u32)
} else {
None
}
})
.sum()
}
}
pub fn stats_benchmarks(benchmarks: &[&Benchmark]) -> String {
let baseline = benchmarks[0].sum() as f32 / 1000.0;
let mut res = String::new();
let config_width = benchmarks.iter().map(|b| b.config.len()).max().unwrap();
for benchmark in benchmarks {
let sum = benchmark.sum() as f32 / 1000.0;
let factor = sum / baseline;
res = format!(
"{}\n{:width$}: {:.3}s tot, {:.3} rel",
res,
benchmark.config,
sum,
factor,
width = config_width,
);
}
res
}