diff --git a/bench/dirsync/.gitignore b/bench/dirsync/.gitignore new file mode 100644 index 00000000..f74e27a2 --- /dev/null +++ b/bench/dirsync/.gitignore @@ -0,0 +1,6 @@ +# build-variants.sh / run-matrix.sh defaults +work/ +out/ +# cargo, when built in place +target/ +Cargo.lock diff --git a/bench/dirsync/Cargo.toml b/bench/dirsync/Cargo.toml new file mode 100644 index 00000000..0e4ff989 --- /dev/null +++ b/bench/dirsync/Cargo.toml @@ -0,0 +1,47 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# Standalone crate (not a workspace member). `build-variants.sh` copies this manifest per +# variant with the `object_store` path rewritten to that variant's worktree; this copy +# builds against the enclosing checkout so that `cargo fmt`/`cargo clippy` work here. + +[package] +name = "dirsync-bench" +version = "0.1.0" +edition = "2021" +publish = false + +[workspace] + +[[bin]] +name = "dirsync-bench" +path = "src/main.rs" + +[features] +default = ["ext-trait", "with-fsync"] +# object_store >= 0.13: `put`/`put_multipart` live on `ObjectStoreExt`. +ext-trait = [] +# The rev has `LocalFileSystem::with_fsync` (fork branches, upstream >= 0.14). +with-fsync = [] + +[dependencies] +object_store = { path = "../..", default-features = false, features = ["fs"] } +tokio = { version = "1", features = ["rt-multi-thread", "macros"] } +hdrhistogram = { version = "7", default-features = false } + +[profile.release] +debug = false diff --git a/bench/dirsync/README.md b/bench/dirsync/README.md new file mode 100644 index 00000000..085891c1 --- /dev/null +++ b/bench/dirsync/README.md @@ -0,0 +1,159 @@ + + +# Directory-fsync micro-benchmark for `LocalFileSystem` + +Compares object_store git revs on the write paths of `LocalFileSystem` with `with_fsync(true)`: +how many directory fsyncs and other syscalls each design issues per operation, and what that costs +in throughput and latency on a given file system. Motivating issue: +[apache/arrow-rs-object-store#888](https://github.com/apache/arrow-rs-object-store/issues/888) +(durable directory creation / fsync of parent directories). + +This crate is not a workspace member; the repository's own `cargo build/test/clippy` ignore it. + +## What it measures + +One **run** = one binary (`src/main.rs`) built against one object_store rev, one operation, one +layout, `conc` tokio tasks each looping sequentially through the public `ObjectStore` API. Latency +is measured around each `await`. A run prepares a fresh store root, does a discarded warm-up +(0.7 s), then a timed phase (3 s, or until `--max-ops` in total), appends one CSV row, and removes +the root (untimed). + +Operations (`--op`): `put` (overwrite), `create` (`PutMode::Create`, unique names), `delete` of +pre-populated objects each alone in its leaf directory (with `--cleanup on` every delete also +removes that directory), `copy` (hard link into a sibling directory, rotating over +`--copy-sources` sources per writer), `rename` of pre-populated sources into a sibling directory, +`multipart` (`put_multipart`, `--parts` x `put_part(--size)`, `complete`). + +Layouts (`--state`): `steady` (one pre-created, fsynced chain per writer), `shared` (one chain for +all writers), `cold` (every op creates a fresh chain of `--depth` directories), `spread` +(`{prefix}/n1/pt_wal/{file}`, prefix one of 4096 hashed values per op; 12288 directories +pre-created and `sync`ed, a working set larger than a 4096-entry cache). + +A **cell** is `op depth state size conc max-ops cleanup`; the default list in `run-matrix.sh` is +24 cells (put steady/shared/cold at 1/8/64 writers, depth-6 cold, spread, create, delete, copy, +rename, multipart). A **variant** is a rev built by `build-variants.sh`; a variant may also be run +with `--fsync off` as the no-durability floor without a separate build. + +The optional **strace pass** runs each variant over a fixed op count under +`strace -c -f -e trace=fsync,fdatasync,openat,...` and records the syscall table next to the CSV +row, so counts per op can be compared between designs (counts, not timings). + +## Requirements + +- Linux (the driver reads `/proc/loadavg` and calls `sync`; the directory-handle designs are + Linux-only). +- Rust toolchain of this repository (`rust-toolchain.toml`), git worktrees, bash >= 4, python3. +- For the strace pass: `strace` >= 6.6 (`-c -f` over a multi-threaded tokio runtime; the + reference runs used 6.19). `run-matrix.sh` warns below 6.6. +- Two file systems to compare file systems (for example an xfs and an ext4 mount); a single + directory on `$TMPDIR` is enough for a smoke test. The bench root is **removed** before and + after every run; never point it at a directory with data. +- Disk: the multipart cell writes 800 x 10 MiB per run; the delete cells pre-populate 125k files. + +## Quick start + +```sh +cd bench/dirsync + +# 1. Build one driver binary per rev (any rev resolvable in this repository; fetch first). +# Worktrees, generated manifests and targets go under $WORK (default ./work). +./build-variants.sh base=influxdata/base-object-store-0132 \ + dirfd=influxdata/local-dirfd-cache-0132 + +# 2. Run a subset: one round, two cells, the second variant also as the no-fsync floor. +./run-matrix.sh --fs tmp=/tmp/dirsync-bench --out ./out --rounds 1 \ + --only '^put 3 steady 4096 (8|64) ' base dirfd floor=dirfd:nofsync + +# 3. Aggregate (paired ratios against the first variant; --baseline / --pairs to change). +./aggregate.py both out/ops-tmp.csv +``` + +Full matrix on two file systems with the strace pass, 5 rounds: + +```sh +export BENCH_ROOT_XFS=/mnt/xfs/dirsync-bench BENCH_ROOT_EXT4=/mnt/ext4/dirsync-bench +./run-matrix.sh --out ./out --rounds 5 --strace base dirfd floor=dirfd:nofsync | tee out/run.log +./aggregate.py paired out/ops-xfs.csv out/ops-ext4.csv +``` + +Options worth knowing (`run-matrix.sh --help` lists all): `--rotation cycle` (every variant runs +first in turn, needed for order-sensitive cells), `--sync-between-runs[=S]` (`sync` + pause before +every run), `--cell "..."` (own cell list), `--strace-only`, `--extra "--copy-sources 64"`, +`--dry-run`. + +`build-variants.sh` picks the driver's cargo features from the rev's source: `ext-trait` when +`ObjectStoreExt` exists (object_store >= 0.13), `with-fsync` when `LocalFileSystem::with_fsync` +exists (this fork's 0.12.5 and 0.13.2 branches, upstream >= 0.14). A rev without `with_fsync` +builds but accepts only `--fsync off`. Checked against 0.12.5 and 0.13.2 revs of this fork. + +## Reading the results + +- `ops-.csv`: one row per run with `ops`, `wall_s`, `ops_per_s`, latency percentiles in + microseconds, the 1-minute load average at start and end, and `fsync`. +- `aggregate.py paired`: per cell, for each pair A/B, the ratio of ops/s **within the same round**, + then the median and min-max over rounds, and the per-variant median p50. This is the statistic + to read: it cancels the drift between rounds that a shared host produces. +- `aggregate.py full`: medians over rounds with min-max of ops/s, p50/p99/p999, each variant's + ratio to the baseline's median, plus two lines per file system: the load average range per + round and the per-round normalized throughput (every run divided by its cell+variant median + over rounds). A round whose normalized median is far from 1.0 was disturbed as a whole; a wide + p10-p90 means individual cells were. +- `strace-.txt`: per traced run the CSV row and the `strace -c` table; divide the counts by + `max-ops` for per-op syscalls. The driver's own preparation and teardown are included + (`unlinkat` from `remove_dir_all`, `openat`/`mkdir` from pre-population, `sync`). + +## Pitfalls + +- **Only paired within-round ratios are meaningful.** Absolute numbers depend on other tenants of + the disk; in past runs the same cells moved 2-3x between days while paired ratios held. Read the + medians over rounds with the min-max as the warning; a single round's ratio can be an outlier + when interference changed in the middle of a cell. +- **Position effect in the cell.** Variants run back to back on the same directory; the first + variant of a cell writes into a file system with no dirty data from the previous variant's + teardown. Multipart (two 5 MiB parts, 2 fsyncs per op) at 1 writer gave the first variant about + 2x, and a no-fsync variant leaves ~8 GB of dirty data behind for whoever follows it. The + default rotation (forward in odd rounds, reversed in even rounds) does not give middle + variants a first position; use `--rotation cycle` and `--sync-between-runs` for such cells, + or treat them as not interpretable. +- **Host noise sampling.** The CSV carries the load average at start and end of each run; the + `full` aggregate prints the per-round load range and normalized throughput. Processes outside + the container are not visible from inside; a `sync`-heavy neighbour shows up only as longer + fsyncs. For a long matrix, log `top` or `/proc/loadavg` every minute alongside (`run.log` + timestamps let you align them). +- **Copy with fsync off and ext4's link limit.** `copy` is a hard link, and ext4 allows 65000 + links per inode (`EMLINK`). A no-fsync variant made ~270k copies/s; with one source per + writer that exceeds the limit within one 3 s run at 8 writers. The driver therefore rotates + over `--copy-sources` sources per writer (default 16); raise it (`--extra "--copy-sources 64"`) + for no-fsync copy at 1 writer, or skip that cell. xfs has no comparable limit. +- **`delete` and `rename` need `--max-ops`** (pre-populated sources); delete at 100000 ops + finishes in well under 3 s on fast disks, so its timed phase is shorter than the others. +- **bash `set -u`** in scripts of this kind: do not reference a variable in the same `local` + statement that declares it (`local a=$1 b=$a`); declare one per line. The scripts here follow + that and pass shellcheck. +- `ext4` vs `xfs` is only like-for-like when both sit on comparable devices and fill levels; read + each file system's ratios on their own. + +## Files + +- `src/main.rs`, `Cargo.toml`: the driver (path dependency on `../..` so `cargo fmt`/`clippy` + work here; `build-variants.sh` rewrites that path per variant). +- `build-variants.sh`: worktree + build per `NAME=REV` into `$WORK/{src,build,target}/NAME`. +- `run-matrix.sh`: matrix driver, strace pass; writes `$OUT/ops-.csv`, `strace-.{txt,csv}`. +- `aggregate.py`: `paired` / `full` / `both` markdown tables from the CSVs. diff --git a/bench/dirsync/aggregate.py b/bench/dirsync/aggregate.py new file mode 100644 index 00000000..eca467a2 --- /dev/null +++ b/bench/dirsync/aggregate.py @@ -0,0 +1,184 @@ +#!/usr/bin/env python3 +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""Aggregate run-matrix.sh CSV files (ops-.csv) into markdown tables. + +`paired`: per cell, the ratio of ops/s between two variants measured in the same round, then the +median and min-max of those ratios over rounds. `full`: per cell and variant, median over rounds +of ops/s (with min-max) and of the per-round p50/p99/p999 latencies, plus per-round load and +normalized-throughput lines that show whether a round was disturbed. + +usage: aggregate.py [--baseline V] [--pairs A/B,...] paired|full|both CSV... + --baseline V variant every other variant is paired against (default: the first variant in + the file) + --pairs ... explicit A/B pairs instead of the default; variants are the CSV `variant` labels +""" +import argparse +import csv +import statistics as st +from collections import defaultdict + +OPS = ["put", "create", "delete", "copy", "rename", "multipart"] +STATES = {"steady": 0, "shared": 1, "cold": 2, "spread": 3} + + +def load(path): + """fs -> (cells: key -> variant -> round -> row, variants in order of first appearance).""" + per_fs = {} + with open(path, newline="") as f: + for r in csv.DictReader(f): + cells, variants = per_fs.setdefault(r["fs"], (defaultdict(lambda: defaultdict(dict)), [])) + key = (r["op"], int(r["depth"]), r["state"], int(r["size"]), int(r["conc"])) + cells[key][r["variant"]][int(r["round"])] = r + if r["variant"] not in variants: + variants.append(r["variant"]) + return per_fs + + +def med(rows, col): + return st.median(float(r[col]) for r in rows) + + +def fmt_us(v): + return f"{v / 1000:.2f}" if v >= 1000 else f"{v / 1000:.3f}" + + +def cell_order(k): + return (OPS.index(k[0]) if k[0] in OPS else len(OPS), k[3], STATES.get(k[2], 9), k[1], k[4]) + + +def size_s(size): + return {4096: "4K", 1048576: "1M", 5242880: "5M"}.get(size, str(size)) + + +def paired(by, a, b, col="ops_per_s"): + rounds = sorted(set(by.get(a, {})) & set(by.get(b, {}))) + if not rounds: + return None + ratios = [float(by[a][r][col]) / float(by[b][r][col]) for r in rounds] + return st.median(ratios), min(ratios), max(ratios), len(ratios) + + +def paired_table(fs, cells, variants, pairs): + out = [f"### {fs}: paired ratios of ops/s (per round, same cell; median over rounds, min-max)\n"] + head = ["op", "depth", "state", "size", "conc"] + [f"{a}/{b}" for a, b in pairs] + head += [f"{a} p50 / {b} p50" for a, b in pairs[:1]] + [f"{v} p50 ms" for v in variants] + out.append("| " + " | ".join(head) + " |") + out.append("|" + "---|" * len(head)) + for key in sorted(cells, key=cell_order): + op, depth, state, size, conc = key + by = cells[key] + cols = [] + for a, b in pairs: + p = paired(by, a, b) + cols.append(f"{p[0]:.3f} ({p[1]:.2f}-{p[2]:.2f}) n={p[3]}" if p else "") + for a, b in pairs[:1]: + p50 = paired(by, a, b, "p50_us") + cols.append(f"{p50[0]:.3f}" if p50 else "") + for v in variants: + rows = list(by.get(v, {}).values()) + cols.append(fmt_us(med(rows, "p50_us")) if rows else "") + out.append(f"| {op} | {depth} | {state} | {size_s(size)} | {conc} | " + " | ".join(cols) + " |") + return "\n".join(out) + + +def table(fs, cells, variants, baseline): + out = [f"### {fs}: ops/s (median over rounds, min-max) and latency ms " + "(median over rounds of per-round p50/p99/p999)\n"] + out.append(f"| op | depth | state | size | conc | var | rounds | ops/s med (min-max) | p50 ms | p99 ms | " + f"p999 ms | vs {baseline} ops/s |") + out.append("|---|---|---|---|---|---|---|---|---|---|---|---|") + for key in sorted(cells, key=cell_order): + op, depth, state, size, conc = key + by = cells[key] + base = med(by[baseline].values(), "ops_per_s") if by.get(baseline) else None + for v in variants: + rows = list(by.get(v, {}).values()) + if not rows: + continue + tp = [float(r["ops_per_s"]) for r in rows] + m = st.median(tp) + rb = "1" if v == baseline else (f"{m / base:.3f}" if base else "") + out.append( + f"| {op} | {depth} | {state} | {size_s(size)} | {conc} | {v} | {len(rows)} | " + f"{m:.0f} ({min(tp):.0f}-{max(tp):.0f}) | {fmt_us(med(rows, 'p50_us'))} | " + f"{fmt_us(med(rows, 'p99_us'))} | {fmt_us(med(rows, 'p999_us'))} | {rb} |" + ) + return "\n".join(out) + + +def rows_of(cells): + for by in cells.values(): + for rounds in by.values(): + yield from rounds.values() + + +def loads(cells): + """round -> (min, max) of the 1-minute load average at run start.""" + by_round = defaultdict(list) + for r in rows_of(cells): + try: + by_round[int(r["round"])].append(float(r["load1_start"])) + except ValueError: + pass + return {k: (min(v), max(v)) for k, v in sorted(by_round.items()) if v} + + +def normalized(cells): + """Each run divided by its cell+variant median over rounds; median (p10-p90) per round.""" + norm = defaultdict(list) + for by in cells.values(): + for rounds in by.values(): + m = st.median(float(r["ops_per_s"]) for r in rounds.values()) + for rd, r in rounds.items(): + norm[rd].append(float(r["ops_per_s"]) / m) + return ", ".join( + f"r{rd}: {st.median(v):.3f} ({sorted(v)[len(v) // 10]:.2f}-{sorted(v)[len(v) * 9 // 10]:.2f})" + for rd, v in sorted(norm.items()) + ) + + +def main(): + ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + ap.add_argument("--baseline") + ap.add_argument("--pairs", help="comma-separated A/B pairs") + ap.add_argument("mode", choices=["paired", "full", "both"]) + ap.add_argument("csv", nargs="+") + args = ap.parse_args() + for path in args.csv: + for fs, (cells, variants) in load(path).items(): + baseline = args.baseline or variants[0] + if baseline not in variants: + raise SystemExit(f"{path}: baseline {baseline} not among variants {variants}") + if args.pairs: + pairs = [tuple(p.split("/")) for p in args.pairs.split(",")] + else: + pairs = [(v, baseline) for v in variants if v != baseline] + if args.mode in ("paired", "both"): + print(paired_table(fs, cells, variants, pairs)) + print() + if args.mode in ("full", "both"): + print(table(fs, cells, variants, baseline)) + print() + print(f"{fs}: 1-min load average at run start, per round (min-max over the round's runs):", + ", ".join(f"r{k}: {lo:.1f}-{hi:.1f}" for k, (lo, hi) in loads(cells).items())) + print(f"{fs}: per-round normalized throughput median (p10-p90):", normalized(cells)) + print() + + +if __name__ == "__main__": + main() diff --git a/bench/dirsync/build-variants.sh b/bench/dirsync/build-variants.sh new file mode 100755 index 00000000..76665f0c --- /dev/null +++ b/bench/dirsync/build-variants.sh @@ -0,0 +1,106 @@ +#!/usr/bin/env bash +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# Build the dirsync-bench driver against one object_store git rev per variant. +# +# For every NAME=REV: a detached worktree of this repository at REV under $WORK/src/NAME, a +# generated manifest under $WORK/build/NAME (this directory's Cargo.toml with the object_store +# path rewritten), and a release build into $WORK/target/NAME. Cargo features of the driver are +# chosen from the rev's source: `ext-trait` if src/lib.rs defines `ObjectStoreExt` (>= 0.13), +# `with-fsync` if src/local.rs defines `LocalFileSystem::with_fsync`. +set -euo pipefail + +usage() { + cat <<'EOF' +usage: build-variants.sh [--work DIR] [--repo DIR] [--check] NAME=REV ... + + NAME=REV variant label and any git rev of object_store resolvable in --repo + (branch, tag, SHA, remote-tracking ref such as influxdata/main) + --work DIR working directory for worktrees, manifests and targets + (default: $WORK, else ./work next to this script) + --repo DIR repository to take worktrees from (default: the one containing this script) + --check `cargo check` instead of a release build + +Binaries land at $WORK/target/NAME/release/dirsync-bench; run-matrix.sh looks for them there. +EOF +} + +BENCH_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +WORK=${WORK:-$BENCH_DIR/work} +REPO=${REPO:-} +CARGO_CMD=(build --release) +specs=() + +while [ $# -gt 0 ]; do + case $1 in + --work) WORK=$2; shift 2 ;; + --repo) REPO=$2; shift 2 ;; + --check) CARGO_CMD=(check); shift ;; + -h|--help) usage; exit 0 ;; + -*) echo "unknown option $1" >&2; usage >&2; exit 2 ;; + *) specs+=("$1"); shift ;; + esac +done +if [ ${#specs[@]} -eq 0 ]; then usage >&2; exit 2; fi +if [ -z "$REPO" ]; then REPO=$(git -C "$BENCH_DIR" rev-parse --show-toplevel); fi +mkdir -p "$WORK/src" "$WORK/build" "$WORK/target" +WORK=$(cd "$WORK" && pwd) + +for spec in "${specs[@]}"; do + case $spec in + *=*) ;; + *) echo "expected NAME=REV, got '$spec'" >&2; exit 2 ;; + esac + name=${spec%%=*} + rev=${spec#*=} + if ! sha=$(git -C "$REPO" rev-parse --verify --quiet "$rev^{commit}"); then + echo "cannot resolve '$rev' in $REPO (fetch it first, e.g. git -C $REPO fetch )" >&2 + exit 1 + fi + src=$WORK/src/$name + if [ -e "$src/.git" ]; then + if [ "$(git -C "$src" rev-parse HEAD)" != "$sha" ]; then + git -C "$src" checkout --quiet --detach "$sha" + fi + else + git -C "$REPO" worktree add --quiet --detach "$src" "$sha" + fi + + version=$(sed -n 's/^version = "\(.*\)"/\1/p' "$src/Cargo.toml" | head -n 1) + features=() + if grep -q 'pub trait ObjectStoreExt' "$src/src/lib.rs"; then features+=(ext-trait); fi + if grep -q 'pub fn with_fsync' "$src/src/local.rs"; then features+=(with-fsync); fi + feature_list=$(IFS=,; echo "${features[*]:-}") + + build=$WORK/build/$name + mkdir -p "$build" + # Same manifest as this directory's, pointed at the variant's worktree and at the shared source. + sed -e "s|path = \"\.\./\.\.\"|path = \"$src\"|" \ + -e "s|path = \"src/main\.rs\"|path = \"$BENCH_DIR/src/main.rs\"|" \ + "$BENCH_DIR/Cargo.toml" > "$build/Cargo.toml" + printf 'name=%s\nrev=%s\nsha=%s\nversion=%s\nfeatures=%s\n' \ + "$name" "$rev" "$sha" "$version" "$feature_list" > "$build/variant.txt" + + echo "== $name: $rev @ ${sha:0:12} (object_store $version, features: ${feature_list:-none})" + CARGO_TARGET_DIR=$WORK/target/$name \ + cargo "${CARGO_CMD[@]}" --quiet --manifest-path "$build/Cargo.toml" \ + --no-default-features ${feature_list:+--features "$feature_list"} + if [ "${CARGO_CMD[0]}" = build ]; then + echo " $WORK/target/$name/release/dirsync-bench" + fi +done diff --git a/bench/dirsync/run-matrix.sh b/bench/dirsync/run-matrix.sh new file mode 100755 index 00000000..b82d3ad2 --- /dev/null +++ b/bench/dirsync/run-matrix.sh @@ -0,0 +1,256 @@ +#!/usr/bin/env bash +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# Run the op matrix (and optionally a syscall-count pass) over variants built by +# build-variants.sh, one file system after another, rotating the variant order per round. +set -euo pipefail + +usage() { + cat <<'EOF' +usage: run-matrix.sh [options] VARIANT ... + + VARIANT NAME | LABEL=NAME | LABEL=NAME:nofsync + NAME is a variant built by build-variants.sh; LABEL is the CSV + label (default NAME); :nofsync runs it with --fsync off (the floor) +options + --fs NAME=ROOT file system label and a directory on it; repeatable. Default: one + entry per BENCH_ROOT_ environment variable (label lowercased), + else "tmp=$TMPDIR/dirsync-bench" + --rounds N rounds (default 5) + --rotation reverse|cycle variant order per round: reverse = given order in odd rounds, + reversed in even rounds (default); cycle = rotate left by round-1 so + every variant runs first in turn + --only REGEX keep only cells whose line "op depth state size conc max-ops cleanup" + matches REGEX (grep -E); applies to the strace cells too + --cell "op depth state size conc max-ops cleanup" + replace the default matrix cell list (not the strace cells); repeatable + --secs S --warmup S timed and warm-up phase per run (default 3 and 0.7) + --sync-between-runs[=S] `sync` then sleep S seconds before every run (default 3) + --strace after the matrix, count syscalls per op over a fixed op count + --strace-only only the syscall-count pass + --strace-ops N ops per traced run (default 6400; multipart N/40) + --extra "ARGS" extra flags passed to every dirsync-bench invocation + --out DIR output directory (default: $OUT, else ./out next to this script) + --work DIR build directory of build-variants.sh (default: $WORK, else ./work) + --dry-run print the commands instead of running them + +outputs (per file system label FS) + $OUT/ops-FS.csv one row per run; input for aggregate.py + $OUT/strace-FS.txt per traced run: the CSV row and the syscall table + $OUT/strace-FS.csv CSV rows of the traced runs (counts, not timings) +EOF +} + +BENCH_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +OUT=${OUT:-$BENCH_DIR/out} +WORK=${WORK:-$BENCH_DIR/work} +ROUNDS=5 +ROTATION=reverse +ONLY= +SECS=3 +WARMUP=0.7 +SYNC_PAUSE= # empty = no sync between runs +STRACE=0 +MATRIX=1 +STRACE_OPS=6400 +EXTRA= +DRY=0 +fs_specs=() +cells=() +variants=() + +# op depth state size conc max-ops cleanup +default_cells=( + "put 3 steady 4096 1 0 off" "put 3 steady 4096 8 0 off" "put 3 steady 4096 64 0 off" + "put 3 shared 4096 1 0 off" "put 3 shared 4096 8 0 off" "put 3 shared 4096 64 0 off" + "put 3 cold 4096 1 20000 off" "put 3 cold 4096 8 20000 off" "put 3 cold 4096 64 20000 off" + "put 6 cold 4096 64 20000 off" + "put 3 spread 4096 8 0 off" "put 3 spread 4096 64 0 off" + "create 3 steady 4096 8 0 off" "create 3 steady 4096 64 0 off" + "create 3 cold 4096 8 20000 off" "create 3 cold 4096 64 20000 off" + "delete 3 steady 4096 8 100000 on" "delete 3 steady 4096 64 100000 on" + "copy 3 steady 4096 8 0 off" "copy 3 steady 4096 64 0 off" + "rename 3 steady 4096 8 20000 off" "rename 3 steady 4096 64 20000 off" + "multipart 3 steady 5242880 1 800 off" "multipart 3 steady 5242880 8 800 off" +) +# Syscall-count cells: op depth state size conc cleanup; max-ops from --strace-ops. +strace_cells=( + "put 3 steady 4096 64 off" "put 3 shared 4096 64 off" "put 3 cold 4096 64 off" "put 3 spread 4096 64 off" + "create 3 steady 4096 64 off" "create 3 cold 4096 64 off" + "delete 3 steady 4096 64 on" "copy 3 steady 4096 64 off" "rename 3 steady 4096 64 off" + "multipart 3 steady 5242880 8 off" +) +TRACE="fsync,fdatasync,openat,open,mkdir,mkdirat,rename,renameat,link,linkat,unlink,unlinkat,rmdir,futex,fstat,newfstatat,statx" + +while [ $# -gt 0 ]; do + case $1 in + --fs) fs_specs+=("$2"); shift 2 ;; + --rounds) ROUNDS=$2; shift 2 ;; + --rotation) ROTATION=$2; shift 2 ;; + --only) ONLY=$2; shift 2 ;; + --cell) cells+=("$2"); shift 2 ;; + --secs) SECS=$2; shift 2 ;; + --warmup) WARMUP=$2; shift 2 ;; + --sync-between-runs) SYNC_PAUSE=3; shift ;; + --sync-between-runs=*) SYNC_PAUSE=${1#*=}; shift ;; + --strace) STRACE=1; shift ;; + --strace-only) STRACE=1; MATRIX=0; shift ;; + --strace-ops) STRACE_OPS=$2; shift 2 ;; + --extra) EXTRA=$2; shift 2 ;; + --out) OUT=$2; shift 2 ;; + --work) WORK=$2; shift 2 ;; + --dry-run) DRY=1; shift ;; + -h|--help) usage; exit 0 ;; + -*) echo "unknown option $1" >&2; usage >&2; exit 2 ;; + *) variants+=("$1"); shift ;; + esac +done +if [ ${#variants[@]} -eq 0 ]; then usage >&2; exit 2; fi +case $ROTATION in reverse|cycle) ;; *) echo "--rotation must be reverse or cycle" >&2; exit 2 ;; esac +if [ ${#cells[@]} -eq 0 ]; then cells=("${default_cells[@]}"); fi + +# File systems: --fs, else BENCH_ROOT_* from the environment, else one tmp entry. +if [ ${#fs_specs[@]} -eq 0 ]; then + while IFS='=' read -r var val; do + fs_specs+=("$(echo "${var#BENCH_ROOT_}" | tr '[:upper:]' '[:lower:]')=$val") + done < <(env | grep -E '^BENCH_ROOT_[A-Za-z0-9_]+=' | sort) +fi +if [ ${#fs_specs[@]} -eq 0 ]; then fs_specs=("tmp=${TMPDIR:-/tmp}/dirsync-bench"); fi + +# Variants: label -> binary, label -> fsync. +declare -A BIN FSYNC +labels=() +for spec in "${variants[@]}"; do + fsync=on + case $spec in *:nofsync) fsync=off; spec=${spec%:nofsync} ;; esac + label=${spec%%=*} + name=${spec#*=} + bin=$WORK/target/$name/release/dirsync-bench + if [ ! -x "$bin" ] && [ $DRY -eq 0 ]; then + echo "no binary for variant '$name' at $bin (run build-variants.sh $name=REV)" >&2; exit 1 + fi + BIN[$label]=$bin + FSYNC[$label]=$fsync + labels+=("$label") +done + +filter() { # cell lines on stdin, filtered by --only + if [ -n "$ONLY" ]; then grep -E -- "$ONLY" || true; else cat; fi +} +mapfile -t cells < <(printf '%s\n' "${cells[@]}" | filter) +mapfile -t strace_cells < <(printf '%s\n' "${strace_cells[@]}" | filter) +if [ $MATRIX -eq 1 ] && [ ${#cells[@]} -eq 0 ]; then echo "warning: --only '$ONLY' selects no matrix cell" >&2; fi +if [ $STRACE -eq 1 ] && [ ${#strace_cells[@]} -eq 0 ]; then + echo "warning: --only '$ONLY' selects no strace cell (they run at 64 writers, multipart at 8)" >&2 +fi + +order_for_round() { # prints the variant labels for round $1 + local r=$1 + local n=${#labels[@]} + local i + if [ "$ROTATION" = reverse ] && [ $((r % 2)) -eq 0 ]; then + for ((i = n - 1; i >= 0; i--)); do echo "${labels[$i]}"; done + elif [ "$ROTATION" = cycle ]; then + for ((i = 0; i < n; i++)); do echo "${labels[$(( (i + r - 1) % n ))]}"; done + else + printf '%s\n' "${labels[@]}" + fi +} + +run() { # run or print a command + if [ $DRY -eq 1 ]; then printf '%q ' "$@"; echo; else "$@"; fi +} + +before_run() { + if [ -n "$SYNC_PAUSE" ]; then run sync; run sleep "$SYNC_PAUSE"; fi +} + +load() { cut -d' ' -f1-3 /proc/loadavg 2>/dev/null || echo "?"; } + +# shellcheck disable=SC2086 # $EXTRA is split on purpose +matrix() { # fs root + local fs=$1 + local root=$2 + local csv=$OUT/ops-$fs.csv + local r cell label op depth state size conc max cleanup + for ((r = 1; r <= ROUNDS; r++)); do + local -a order + mapfile -t order < <(order_for_round "$r") + for cell in "${cells[@]}"; do + read -r op depth state size conc max cleanup <<<"$cell" + for label in "${order[@]}"; do + before_run + run "${BIN[$label]}" --root "$root/run" --fs "$fs" --variant "$label" --fsync "${FSYNC[$label]}" \ + --op "$op" --depth "$depth" --state "$state" --size "$size" --conc "$conc" --cleanup "$cleanup" \ + --secs "$SECS" --warmup "$WARMUP" --max-ops "$max" --round "$r" --csv "$csv" $EXTRA \ + || echo "FAILED $fs $label '$cell' round $r" + done + done + echo "$fs round $r done $(date +%T) order [${order[*]}] load $(load)" + done +} + +# shellcheck disable=SC2086 +strace_pass() { # fs root + local fs=$1 + local root=$2 + local sum=$OUT/strace-$fs.txt + local csv=$OUT/strace-$fs.csv + local tmp=$OUT/strace-tmp-$fs.txt + local cell label ops op depth state size conc cleanup + if [ $DRY -eq 0 ]; then : > "$sum"; fi + for cell in "${strace_cells[@]}"; do + read -r op depth state size conc cleanup <<<"$cell" + ops=$STRACE_OPS + if [ "$op" = multipart ]; then ops=$(( STRACE_OPS / 40 )); fi + for label in "${labels[@]}"; do + before_run + if [ $DRY -eq 0 ]; then + echo "### $fs op $op depth $depth $state size $size conc $conc max-ops $ops variant $label fsync ${FSYNC[$label]}" >> "$sum" + fi + run strace -c -f -e "trace=$TRACE" -o "$tmp" "${BIN[$label]}" --root "$root/strace" --fs "$fs" \ + --variant "$label" --fsync "${FSYNC[$label]}" --op "$op" --depth "$depth" --state "$state" \ + --size "$size" --conc "$conc" --cleanup "$cleanup" \ + --secs 1000 --warmup 0 --max-ops "$ops" --round 0 --csv "$csv" $EXTRA \ + || echo "FAILED strace $fs $label '$cell'" + if [ $DRY -eq 0 ]; then + { tail -n 1 "$csv"; cat "$tmp"; echo; } >> "$sum" + fi + done + done + rm -f "$tmp" +} + +if [ $STRACE -eq 1 ] && [ $DRY -eq 0 ]; then + if ! command -v strace >/dev/null; then echo "strace not found" >&2; exit 1; fi + sv=$(strace -V 2>/dev/null | head -n 1 | grep -oE '[0-9]+\.[0-9]+' | head -n 1) + if [ -n "$sv" ] && [ "$(printf '%s\n6.6\n' "$sv" | sort -V | head -n 1)" != "6.6" ]; then + echo "warning: strace $sv is older than 6.6; -c -f counts may be incomplete (see README)" >&2 + fi +fi + +mkdir -p "$OUT" +echo "start $(date +%T) load $(load); variants [${labels[*]}]; ${#cells[@]} cells; $ROUNDS rounds; rotation $ROTATION" +for spec in "${fs_specs[@]}"; do + fs=${spec%%=*} + root=${spec#*=} + if [ $DRY -eq 0 ]; then mkdir -p "$root"; fi + if [ $MATRIX -eq 1 ]; then matrix "$fs" "$root"; fi + if [ $STRACE -eq 1 ]; then strace_pass "$fs" "$root"; echo "$fs strace done $(date +%T)"; fi +done +echo "all done $(date +%T) load $(load)" diff --git a/bench/dirsync/src/main.rs b/bench/dirsync/src/main.rs new file mode 100644 index 00000000..21b29ea0 --- /dev/null +++ b/bench/dirsync/src/main.rs @@ -0,0 +1,536 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! Micro-benchmark driver for `LocalFileSystem` directory-fsync designs. +//! +//! `conc` tokio tasks each run a sequential loop of one operation through the public +//! `ObjectStore` API for `secs` seconds (after a discarded warm-up) or until `max-ops` in total. +//! One run = one CSV row. See `README.md` for the matrix driver and how to read results. +//! +//! Operations (`--op`): +//! - `put`: `put` (Overwrite) of `size` bytes. +//! - `create`: `put_opts` with `PutMode::Create`, unique names. +//! - `delete`: `delete` of pre-populated objects; every object sits alone in its leaf directory, +//! so with automatic cleanup each delete also removes that directory. +//! - `copy` / `rename`: into a different pre-created directory of the same depth; `copy` rotates +//! over `copy-sources` sources per writer (a hard link each; see README on ext4's link limit), +//! `rename` consumes pre-populated sources. +//! - `multipart`: `put_multipart`, `parts` x `put_part(size)`, `complete`. +//! +//! Layouts (`--state`): `steady` (one directory chain per writer, pre-created and fsynced), +//! `shared` (one chain for all writers), `cold` (every put makes a fresh first-level directory, so +//! each put creates `depth` directories), `spread` (`{prefix}/n1/pt_wal/{file}` with the prefix one +//! of 4096 hashed values chosen per op; all 12288 directories pre-created, then `sync`). + +use hdrhistogram::Histogram; +use object_store::local::LocalFileSystem; +use object_store::path::Path; +#[cfg(feature = "ext-trait")] +use object_store::ObjectStoreExt; +use object_store::{ObjectStore, PutMode, PutOptions, PutPayload}; +use std::collections::HashMap; +use std::fs::File; +use std::sync::Arc; +use std::time::{Duration, Instant}; + +const USAGE: &str = "\ +usage: dirsync-bench [--flag value]... + --root DIR store root; REMOVED before and after the run (default: $TMPDIR/dirsync-bench) + --op OP put | create | delete | copy | rename | multipart (default put) + --state S steady | shared | cold | spread (default steady) + --depth N directories below the root (default 3) + --size BYTES object or part size (default 4096) + --parts N parts per multipart upload (default 2) + --conc N concurrent writers (default 1) + --secs S timed phase length (default 3) + --warmup S discarded warm-up length (default 0.7) + --max-ops N stop after N ops in total; 0 = time-bounded (default 0; delete/rename need it) + --fsync on|off LocalFileSystem::with_fsync (default on) + --cleanup on|off LocalFileSystem::with_automatic_cleanup (default off) + --copy-sources K copy rotates over K source files per writer (default 16) + --fs NAME label written to the CSV (default ?) + --variant NAME label written to the CSV (default ?) + --round N label written to the CSV (default 0) + --csv FILE append one row (header if new) to FILE (default stdout) +"; + +struct Args { + root: String, + op: String, + fs: String, + variant: String, + depth: usize, + state: String, + size: usize, + parts: usize, + conc: usize, + secs: f64, + warmup: f64, + max_ops: u64, + fsync: bool, + cleanup: bool, + copy_sources: u64, + round: u32, + csv: String, +} + +const KNOWN: &[&str] = &[ + "root", + "op", + "fs", + "variant", + "depth", + "state", + "size", + "parts", + "conc", + "secs", + "warmup", + "max-ops", + "fsync", + "cleanup", + "copy-sources", + "round", + "csv", +]; + +fn parse() -> Args { + let mut m: HashMap = HashMap::new(); + let mut it = std::env::args().skip(1); + while let Some(k) = it.next() { + if k == "-h" || k == "--help" { + print!("{USAGE}"); + std::process::exit(0); + } + let k = k.trim_start_matches("--").to_string(); + if !KNOWN.contains(&k.as_str()) { + eprintln!("unknown flag --{k}\n{USAGE}"); + std::process::exit(2); + } + let v = it.next().unwrap_or_else(|| { + eprintln!("missing value for --{k}"); + std::process::exit(2); + }); + m.insert(k, v); + } + let g = |k: &str, d: &str| m.get(k).cloned().unwrap_or_else(|| d.to_string()); + let default_root = std::env::temp_dir().join("dirsync-bench"); + let args = Args { + root: g("root", &default_root.to_string_lossy()), + op: g("op", "put"), + fs: g("fs", "?"), + variant: g("variant", "?"), + depth: g("depth", "3").parse().expect("--depth"), + state: g("state", "steady"), + size: g("size", "4096").parse().expect("--size"), + parts: g("parts", "2").parse().expect("--parts"), + conc: g("conc", "1").parse().expect("--conc"), + secs: g("secs", "3").parse().expect("--secs"), + warmup: g("warmup", "0.7").parse().expect("--warmup"), + max_ops: g("max-ops", "0").parse().expect("--max-ops"), + fsync: g("fsync", "on") == "on", + cleanup: g("cleanup", "off") == "on", + copy_sources: g("copy-sources", "16").parse().expect("--copy-sources"), + round: g("round", "0").parse().expect("--round"), + csv: g("csv", "/dev/stdout"), + }; + assert!(args.conc >= 1, "--conc must be >= 1"); + assert!(args.copy_sources >= 1, "--copy-sources must be >= 1"); + assert!( + ["put", "create", "delete", "copy", "rename", "multipart"].contains(&args.op.as_str()), + "unknown --op {}", + args.op + ); + assert!( + ["steady", "shared", "cold", "spread"].contains(&args.state.as_str()), + "unknown --state {}", + args.state + ); + args +} + +fn load1() -> String { + std::fs::read_to_string("/proc/loadavg") + .map(|s| s.split_whitespace().next().unwrap_or("?").to_string()) + .unwrap_or_else(|_| "?".into()) +} + +const PREFIXES: u64 = 4096; + +fn spread_prefix(w: usize, i: u64) -> u64 { + let h = (w as u64) + .wrapping_mul(0x9E37_79B9_7F4A_7C15) + .wrapping_add(i) + .wrapping_mul(0xBF58_476D_1CE4_E5B9); + (h >> 52) % PREFIXES +} + +/// The directory chain (without the file) for writer `w`, op `i`. +fn dir_parts(state: &str, depth: usize, w: usize, i: u64) -> Vec { + let mut parts = Vec::with_capacity(depth); + match state { + "spread" => { + parts.push(format!("{:03x}", spread_prefix(w, i))); + parts.push("n1".to_string()); + parts.push("pt_wal".to_string()); + } + _ if depth >= 1 => { + parts.push(match state { + "cold" => format!("w{w}_{i}"), + "shared" => "s".to_string(), + _ => format!("w{w}"), + }); + for l in 2..=depth { + parts.push(format!("l{l}")); + } + } + _ => {} + } + parts +} + +fn join(mut parts: Vec, file: String) -> Path { + parts.push(file); + Path::from_iter(parts.iter().map(|s| s.as_str())) +} + +/// Object written by `put`/`create`/`multipart` for writer `w`, op `i`. +fn object_path(state: &str, depth: usize, w: usize, i: u64) -> Path { + let file = match state { + "shared" | "spread" => format!("w{w}_{i}.pt"), + _ => format!("{i}.pt"), + }; + join(dir_parts(state, depth, w, i), file) +} + +/// Object deleted by `delete`: alone in its own leaf directory `d{i}` at `depth`. +fn delete_path(depth: usize, w: usize, i: u64) -> Path { + let mut parts = dir_parts("steady", depth.saturating_sub(1), w, i); + parts.push(format!("d{i}")); + join(parts, format!("{i}.pt")) +} + +/// Source and destination for `copy` (source `i % copy_sources` of writer `w`) and `rename` +/// (one source per op). +fn copy_paths(op: &str, depth: usize, w: usize, i: u64, copy_sources: u64) -> (Path, Path) { + let src_dir = dir_parts("steady", depth, w, i); + let mut dst_dir = dir_parts("steady", depth.saturating_sub(1), w, i); + dst_dir.push("dst".to_string()); + let src = match op { + "copy" => format!("src{}.pt", i % copy_sources), + _ => format!("src{i}.pt"), + }; + (join(src_dir, src), join(dst_dir, format!("{i}.pt"))) +} + +fn fsync_dir(p: &std::path::Path) -> std::io::Result<()> { + File::open(p)?.sync_all() +} + +/// `create_dir_all` for `dir` and fsync every directory from it up to `root`. +fn create_chain_synced(root: &std::path::Path, dir: &std::path::Path) { + std::fs::create_dir_all(dir).unwrap(); + let mut d = dir.to_path_buf(); + loop { + fsync_dir(&d).unwrap(); + if d == root || !d.pop() { + break; + } + } +} + +fn sync_all_fs() { + let status = std::process::Command::new("sync").status().expect("sync"); + assert!(status.success()); +} + +struct Phase { + hist: Histogram, + ops: u64, + wall: Duration, +} + +async fn one_op(store: &LocalFileSystem, a: &Args, payload: &PutPayload, w: usize, i: u64) { + match a.op.as_str() { + "put" => { + let path = object_path(&a.state, a.depth, w, i); + store.put(&path, payload.clone()).await.expect("put failed"); + } + "create" => { + let path = object_path(&a.state, a.depth, w, i); + let opts = PutOptions { + mode: PutMode::Create, + ..Default::default() + }; + store + .put_opts(&path, payload.clone(), opts) + .await + .expect("create failed"); + } + "delete" => { + let path = delete_path(a.depth, w, i); + store.delete(&path).await.expect("delete failed"); + } + "copy" => { + let (from, to) = copy_paths("copy", a.depth, w, i, a.copy_sources); + store.copy(&from, &to).await.expect("copy failed"); + } + "rename" => { + let (from, to) = copy_paths("rename", a.depth, w, i, a.copy_sources); + store.rename(&from, &to).await.expect("rename failed"); + } + "multipart" => { + let path = object_path(&a.state, a.depth, w, i); + let mut upload = store + .put_multipart(&path) + .await + .expect("put_multipart failed"); + for _ in 0..a.parts { + upload + .put_part(payload.clone()) + .await + .expect("put_part failed"); + } + upload.complete().await.expect("complete failed"); + } + other => panic!("unknown op {other}"), + } +} + +async fn run_ops( + store: Arc, + a: Arc, + payload: PutPayload, + counters: &mut [u64], + secs: f64, + max_ops: u64, +) -> Phase { + let per_writer_cap = per_writer(max_ops, a.conc); + let start = Instant::now(); + let deadline = start + Duration::from_secs_f64(secs); + let mut handles = Vec::with_capacity(a.conc); + for (w, &start_i) in counters.iter().enumerate() { + let store = Arc::clone(&store); + let payload = payload.clone(); + let a = Arc::clone(&a); + handles.push(tokio::spawn(async move { + let mut hist = Histogram::::new_with_bounds(1, 60_000_000, 3).unwrap(); + let mut i = start_i; + while Instant::now() < deadline && i - start_i < per_writer_cap { + let t = Instant::now(); + one_op(&store, &a, &payload, w, i).await; + hist.record(t.elapsed().as_micros().clamp(1, 60_000_000) as u64) + .unwrap(); + i += 1; + } + (w, i, hist) + })); + } + let mut hist = Histogram::::new_with_bounds(1, 60_000_000, 3).unwrap(); + let mut ops = 0; + for h in handles { + let (w, i, hw) = h.await.unwrap(); + ops += i - counters[w]; + counters[w] = i; + hist.add(&hw).unwrap(); + } + Phase { + hist, + ops, + wall: start.elapsed(), + } +} + +fn per_writer(max_ops: u64, conc: usize) -> u64 { + if max_ops == 0 { + u64::MAX + } else { + (max_ops / conc as u64).max(1) + } +} + +fn warm_cap(a: &Args) -> u64 { + if a.max_ops == 0 { + 0 + } else { + (a.max_ops / 4).max(a.conc as u64) + } +} + +/// Creates directories (and, for consuming ops, objects) before the warm-up. +fn prepare(store: &LocalFileSystem, a: &Args, root: &std::path::Path) { + let fs_path = |p: &Path| store.path_to_filesystem(p).unwrap(); + match a.op.as_str() { + "delete" | "rename" => { + // Enough pre-populated objects for the warm-up and the timed phase. + assert!(a.max_ops > 0, "{} needs --max-ops", a.op); + let n = per_writer(warm_cap(a), a.conc) + per_writer(a.max_ops, a.conc); + for w in 0..a.conc { + for i in 0..n { + let p = match a.op.as_str() { + "delete" => delete_path(a.depth, w, i), + _ => copy_paths("rename", a.depth, w, i, a.copy_sources).0, + }; + let fsp = fs_path(&p); + std::fs::create_dir_all(fsp.parent().unwrap()).unwrap(); + std::fs::write(&fsp, b"x").unwrap(); + } + if a.op == "rename" { + let (_, to) = copy_paths("rename", a.depth, w, 0, a.copy_sources); + std::fs::create_dir_all(fs_path(&to).parent().unwrap()).unwrap(); + } + } + sync_all_fs(); + } + "copy" => { + for w in 0..a.conc { + for s in 0..a.copy_sources { + let (from, to) = copy_paths("copy", a.depth, w, s, a.copy_sources); + let from = fs_path(&from); + std::fs::create_dir_all(from.parent().unwrap()).unwrap(); + std::fs::write(&from, vec![0xA5u8; a.size]).unwrap(); + std::fs::create_dir_all(fs_path(&to).parent().unwrap()).unwrap(); + } + } + sync_all_fs(); + } + _ => match a.state.as_str() { + "spread" => { + for prefix in 0..PREFIXES { + let dir = root.join(format!("{prefix:03x}")).join("n1").join("pt_wal"); + std::fs::create_dir_all(dir).unwrap(); + } + sync_all_fs(); + } + "steady" | "shared" => { + for w in 0..a.conc { + let p = object_path(&a.state, a.depth, w, 0); + let fsp = fs_path(&p); + create_chain_synced(root, fsp.parent().unwrap()); + } + } + _ => {} + }, + } +} + +fn write_row(a: &Args, p: &Phase, load_start: &str, load_end: &str) { + use std::io::Write; + let path = std::path::Path::new(&a.csv); + let new = !path.exists() + || std::fs::metadata(path) + .map(|m| m.len() == 0) + .unwrap_or(true); + let mut f = std::fs::OpenOptions::new() + .create(true) + .append(true) + .open(path) + .unwrap(); + if new && a.csv != "/dev/stdout" { + writeln!(f, "fs,variant,op,depth,state,size,conc,round,ops,wall_s,ops_per_s,p50_us,p90_us,p99_us,p999_us,max_us,mean_us,load1_start,load1_end,fsync").unwrap(); + } + let h = &p.hist; + writeln!( + f, + "{},{},{},{},{},{},{},{},{},{:.3},{:.1},{},{},{},{},{},{:.1},{},{},{}", + a.fs, + a.variant, + a.op, + a.depth, + a.state, + a.size, + a.conc, + a.round, + p.ops, + p.wall.as_secs_f64(), + p.ops as f64 / p.wall.as_secs_f64(), + h.value_at_quantile(0.5), + h.value_at_quantile(0.9), + h.value_at_quantile(0.99), + h.value_at_quantile(0.999), + h.max(), + h.mean(), + load_start, + load_end, + if a.fsync { "on" } else { "off" }, + ) + .unwrap(); +} + +fn build_store(a: &Args, root: &std::path::Path) -> LocalFileSystem { + let mut store = LocalFileSystem::new_with_prefix(root).unwrap(); + #[cfg(feature = "with-fsync")] + if a.fsync { + store = store.with_fsync(true); + } + #[cfg(not(feature = "with-fsync"))] + assert!( + !a.fsync, + "built without the `with-fsync` feature (rev has no LocalFileSystem::with_fsync); pass --fsync off" + ); + if a.cleanup { + store = store.with_automatic_cleanup(true); + } + store +} + +fn main() { + let a = Arc::new(parse()); + let root = std::path::PathBuf::from(&a.root); + assert!( + root.components().count() >= 3, + "refusing to remove --root {}: too close to the file system root", + root.display() + ); + let _ = std::fs::remove_dir_all(&root); + std::fs::create_dir_all(&root).unwrap(); + fsync_dir(&root).unwrap(); + + let rt = tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .unwrap(); + rt.block_on(async { + let store = Arc::new(build_store(&a, &root)); + prepare(&store, &a, &root); + let payload = PutPayload::from(vec![0xA5u8; a.size]); + let mut counters = vec![0u64; a.conc]; + let _warm = run_ops( + Arc::clone(&store), + Arc::clone(&a), + payload.clone(), + &mut counters, + a.warmup, + warm_cap(&a), + ) + .await; + let load_start = load1(); + let p = run_ops( + Arc::clone(&store), + Arc::clone(&a), + payload, + &mut counters, + a.secs, + a.max_ops, + ) + .await; + let load_end = load1(); + write_row(&a, &p, &load_start, &load_end); + }); + drop(rt); + let _ = std::fs::remove_dir_all(&root); +}