-
Notifications
You must be signed in to change notification settings - Fork 2
/
fs_destroy.rs
338 lines (304 loc) · 9.04 KB
/
fs_destroy.rs
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
use std::{
fs,
io::Write,
os::unix::fs::FileTypeExt,
path::{Path, PathBuf},
process::{Command, Stdio},
time::{Duration, Instant},
};
use assert_cmd::prelude::*;
use bfffs::Result;
use clap::{crate_version, Parser};
use freebsd_libgeom as geom;
use nix::{
mount::{unmount, MntFlags},
sys::signal,
unistd::Pid,
};
use regex::Regex;
use si_scale::helpers::bibytes;
use tempfile::{Builder, TempDir};
use tracing_subscriber::EnvFilter;
pub mod util {
include!("../tests/integration/util.rs");
}
use util::{bfffs, bfffsd, waitfor, Bfffsd};
/// Measure file system destruction speed
#[derive(Parser, Clone, Debug)]
#[clap(version = crate_version!())]
struct Cli {
#[clap(long)]
bench: bool,
#[clap(
short = 'd',
long,
require_value_delimiter(true),
value_delimiter(',')
)]
devices: Vec<String>,
#[clap(long, help = "Drop the cache after fill and before destroy")]
drop_cache: bool,
#[clap(
long,
help = "Time to spend filling file system in seconds",
default_value = "1"
)]
fill_time: f64,
#[clap(long, help = "Record a flamegraph to this file")]
flamegraph: Option<PathBuf>,
/// Daemon options, comma delimited.
#[clap(
short = 'o',
long,
require_value_delimiter(true),
value_delimiter(',')
)]
options: Option<String>,
/// File system propertties, comma delimited.
#[clap(
short = 'p',
long,
require_value_delimiter(true),
value_delimiter(',')
)]
properties: Option<String>,
#[clap(required(false))]
filter: Option<String>,
}
/// Run a function while monitoring the given PID with dtrace. Record its
/// flamegraph to the specified file.
fn dtraceit<F: Fn() -> T, T>(f: F, pid: Pid, fname: &Path) -> T {
let mut dtrace = Command::new("dtrace")
.stdout(Stdio::piped())
.arg("-p")
.arg(format!("{}", pid))
.arg("-n")
.arg("profile-497 /pid == $target && arg1/ { @[ustack()] = count(); }")
.spawn()
.unwrap();
let mut stackcollapse = Command::new("stackcollapse.pl")
.stdin(dtrace.stdout.take().unwrap())
.stdout(Stdio::piped())
.spawn()
.unwrap();
let flamegraph = Command::new("flamegraph.pl")
.stdin(stackcollapse.stdout.take().unwrap())
.stdout(Stdio::piped())
.spawn()
.unwrap();
// Give dtrace time to start up
std::thread::sleep(std::time::Duration::from_secs(1));
let output = f();
signal::kill(pid, signal::Signal::SIGINT).unwrap();
dtrace.wait().unwrap();
let svg = flamegraph.wait_with_output().unwrap().stdout;
fs::File::create(fname).unwrap().write_all(&svg).unwrap();
output
}
struct Harness {
bfffsd: Bfffsd,
pub tempdir: TempDir,
pub mountpoint: PathBuf,
pub sockpath: PathBuf,
}
impl Drop for Harness {
fn drop(&mut self) {
let _ignore_errors = unmount(&self.mountpoint, MntFlags::empty());
}
}
fn setup(mut devices: Vec<String>, options: Option<String>) -> Harness {
let tempdir = Builder::new()
.prefix(concat!("bench.fs_destroy", "."))
.tempdir()
.unwrap();
let sockpath = tempdir.path().join("bfffsd.sock");
if devices.is_empty() {
let len = 1 << 40; // 1 TiB
let filename = tempdir.path().join("vdev");
let file = fs::File::create(&filename).unwrap();
file.set_len(len).unwrap();
devices.push(filename.to_str().unwrap().to_owned());
}
let mountpoint = tempdir.path().join("mnt");
bfffs()
.arg("pool")
.arg("create")
.arg("-p")
.arg(format!("mountpoint={}", mountpoint.display()))
.arg("testpool")
.args(devices.clone())
.assert()
.success();
let mut bfffsd_cmd = bfffsd();
bfffsd_cmd.arg("--sock").arg(sockpath.to_str().unwrap());
if let Some(opts) = options {
bfffsd_cmd.arg("-o").arg(opts);
}
let bfffsd = bfffsd_cmd
.arg("testpool")
.args(devices)
.spawn()
.unwrap()
.into();
// We must wait for bfffsd to be ready to receive commands
waitfor(Duration::from_secs(5), || {
fs::metadata(&sockpath)
.map(|md| md.file_type().is_socket())
.unwrap_or(false)
})
.expect("Timeout waiting for bfffsd to listen");
Harness {
bfffsd,
sockpath,
mountpoint,
tempdir,
}
}
/// Create a file system, fill it with files, and time how long it takes
/// to destroy it.
fn fs_destroy(
devices: Vec<String>,
fill_time: f64,
options: Option<String>,
properties: Option<String>,
flamegraph: Option<PathBuf>,
drop_cache: bool,
) -> Result<()> {
const BSIZE: usize = 1 << 17;
let sdevs = devices
.iter()
.filter_map(|fulldev| fulldev.strip_prefix("/dev/"))
.collect::<Vec<_>>();
let use_libgeom = !sdevs.is_empty();
let geom_regex =
Regex::new(&format!("^({})$", &sdevs[..].join("|"))).unwrap();
let harness = setup(devices, options);
let mp = harness.tempdir.path().join("mnt").join("testfs");
fs::create_dir_all(&mp).unwrap();
let mut bfffs_cmd = bfffs();
bfffs_cmd
.arg("--sock")
.arg(harness.sockpath.to_str().unwrap())
.arg("fs")
.arg("create");
if let Some(props) = properties {
bfffs_cmd.arg("-o").arg(props);
}
bfffs_cmd.arg("testpool/testfs").assert().success();
bfffs()
.arg("--sock")
.arg(harness.sockpath.to_str().unwrap())
.arg("fs")
.arg("mount")
.arg("testpool/testfs")
.assert()
.success();
let mut fsize = 0;
{
let zbuf = vec![0u8; BSIZE];
let mut zfile = fs::File::create(mp.join("zfile")).unwrap();
let duration = Duration::from_secs_f64(fill_time);
let start = Instant::now();
loop {
zfile.write_all(&zbuf).unwrap();
fsize += zbuf.len();
let elapsed = start.elapsed();
if elapsed >= duration {
break;
}
}
zfile.sync_all().unwrap();
}
bfffs()
.arg("--sock")
.arg(harness.sockpath.to_str().unwrap())
.arg("fs")
.arg("unmount")
.arg("-f")
.arg("testpool/testfs")
.assert()
.success();
if drop_cache {
bfffs()
.arg("--sock")
.arg(harness.sockpath.to_str().unwrap())
.arg("debug")
.arg("drop-cache")
.assert()
.success();
}
let doit = || {
let start = Instant::now();
bfffs()
.arg("--sock")
.arg(harness.sockpath.to_str().unwrap())
.arg("fs")
.arg("destroy")
.arg("testpool/testfs")
.assert()
.success();
start.elapsed()
};
let mut before = geom::Snapshot::new().unwrap();
let elapsed = if let Some(fname) = flamegraph {
let pid = Pid::from_raw(harness.bfffsd.id() as libc::pid_t);
dtraceit(doit, pid, &fname)
} else {
doit()
};
let mut after = geom::Snapshot::new().unwrap();
println!(
"fs_destroy: {}/s in {} s for {}",
bibytes(fsize as f64 / elapsed.as_secs_f64()),
elapsed.as_secs_f64(),
bibytes(fsize as f64)
);
let etime = f64::from(after.timestamp() - before.timestamp());
let mut tree = geom::Tree::new().unwrap();
if use_libgeom {
println!("name ops kb/t ms/t reads writes %b");
for (afterstat, beforestat) in after.iter_pair(Some(&mut before)) {
let stats = geom::Statistics::compute(afterstat, beforestat, etime);
if let Some(gident) = tree.lookup(afterstat.id()) {
if !gident.is_provider() {
continue;
}
if let Ok(ident) = gident.name() {
let ident = ident.to_string_lossy();
if !geom_regex.is_match(&ident) {
continue;
}
println!(
"{:8} {:>7.0} {:>7.0} {:>7.0} {:>7.0} {:>7.0} {:>3.0}",
ident,
stats.total_transfers(),
stats.kb_per_transfer_read(),
stats.ms_per_transaction_read(),
stats.total_transfers_read(),
stats.total_transfers_write(),
stats.busy_pct()
);
}
}
}
}
Ok(())
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
tracing_subscriber::fmt()
.pretty()
.with_env_filter(EnvFilter::from_default_env())
.init();
let cli: Cli = Cli::parse();
// Run individual benchmarks in serial
fs_destroy(
cli.devices,
cli.fill_time,
cli.options,
cli.properties,
cli.flamegraph,
cli.drop_cache,
)?;
Ok(())
}