Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Performance

How to find out where irsync spends its time, and how to tell whether a change made it faster. Watch a transfer with telemetry covers irsync’s own timings; this page adds the method and the system tools.

Method

  1. Measure before changing anything. Pick one workload that shows the problem and write down its numbers on the current commit. A guess about where the time goes is a hypothesis, not a finding: the cost is often somewhere no one expects (SQLite re-preparing a statement on every call, for example; see Known costs).
  2. Keep the workload fixed and reproducible. Generate the tree with filegen from a fixed seed, run the daemon and the client on the same machine with release builds, and reset the state the run depends on (the destination tree, and the index and resume state described in Files and state) the same way every time. Use an idle machine: on a busy one, single runs of the same binary vary by up to 2x.
  3. Compare binaries, not commits in your head. Build each version you compare into its own file (cp target/release/irsync /some/dir/irsync-old) and interleave their runs: old, new, old, new. Check that each run did the same work (-v shows files and bytes transferred) and that the trees match afterwards (diff -r), or the faster run may simply have done less. The Overhead benchmark of the telemetry is a worked example: interleaved runs, pooled medians, and CPU time reported beside wall time.
  4. Prefer counts to times. CPU time, context switches and syscall counts vary far less between runs than wall time, and they say what changed. Report wall time too, but over several runs.
  5. Find the cause before fixing it. Go from the coarse to the fine: the -vv summary for the phase, perf stat for CPU against waiting, perf record for the hot code, strace -c for the syscalls. When a profile shows a cost but not its caller, count the calls per call site with temporary code (a counter keyed by #[track_caller]’s location works well), then remove it.
  6. Change one thing, then measure again with the same workload and the same commands. Bisect when a regression’s cause is unclear: build the commits in between with git worktree and run each.
  7. Record the result in the commit message: the workload, the numbers before and after, and what did not change.

Host setup for perf

Linux restricts what an unprivileged perf may see. Two sysctls matter:

SettingValue neededWhy
kernel.perf_event_paranoid1 or lowerLets your user record kernel samples of its own processes. At 2 perf sees user space only; some distributions default higher still, which blocks unprivileged perf altogether.
kernel.kptr_restrict0Shows kernel symbol addresses in /proc/kallsyms. At 1, your user sees all-zero addresses, so perf records kernel samples but cannot name them.

Check the current values first, so you can restore them:

sysctl kernel.perf_event_paranoid kernel.kptr_restrict

One-off, until the next reboot:

sudo sysctl kernel.perf_event_paranoid=1
sudo sysctl kernel.kptr_restrict=0

Persistent, applied at every boot:

sudo tee /etc/sysctl.d/99-perf.conf <<'EOF'
kernel.perf_event_paranoid = 1
kernel.kptr_restrict = 0
EOF
sudo sysctl --system    # applies it now as well

To undo it, delete /etc/sysctl.d/99-perf.conf and reboot, or set the values you noted back with sysctl.

head -2 /proc/kallsyms shows non-zero addresses once kptr_restrict is in effect.

These settings weaken the kernel’s defences slightly. Every local user can then profile its own processes in kernel mode and read kernel addresses, which makes kernel exploits easier. That is fine on a single-user development machine. Do not leave them set on a shared or production host.

Tools

-vv summary. Add -vv to the client. The block at the end of each session shows wall time, busy time per activity (walk, plan, hash, write, waiting on the peer, …), CPU and wire bytes, for the client and the daemon. Start here to see which phase and which side to look at.

Telemetry. --debug-addr serves span timings and flamegraph input over HTTP. These are irsync’s own spans, so they show time per phase rather than per function. See Watch a transfer with telemetry.

perf stat. CPU time and context switches for a whole process:

perf stat -e task-clock,context-switches irsync daemon --config irsync.toml

High CPU points at computation. Many context switches point at threads handing work to each other (with tokio, every spawn_blocking round trip costs futex wake-ups). Wall time much longer than CPU time means the process is waiting: on disk, on the network, or on a lock.

perf record. Where the CPU time goes, with call stacks. Build with frame pointers into a separate target directory, so the normal build stays cached:

RUSTFLAGS="-C force-frame-pointers=yes" CARGO_TARGET_DIR=target/fp \
  cargo build --release -p irsync
perf record -F 999 -g -o perf.data target/fp/release/irsync daemon --config irsync.toml
perf report -i perf.data --no-children

The prebuilt standard library has no frame pointers, so stacks that pass through it can be cut short. perf record --call-graph dwarf avoids that, at the cost of much larger data files. Kernel frames need the host setup above.

strace -c. Syscall counts and errors per syscall:

strace -f -c -o strace.txt irsync daemon --config irsync.toml

The errors column is often the useful part: tens of thousands of failing calls (a mkdir getting EEXIST, a statx getting ENOENT) are work that can usually be skipped. strace slows the traced process by an order of magnitude, so use its counts, not its times.

Syscalls per call site. When strace -c shows a count worth cutting, find which code makes the calls. strace -k prints a stack for every syscall, and syscall-sites.py groups the calls by syscall, result and the innermost irsync frames. Use a small workload (a few hundred files), since every call writes a full stack:

CARGO_PROFILE_RELEASE_DEBUG=line-tables-only CARGO_TARGET_DIR=target/dbg \
  cargo build --release -p irsync
strace -f -k -e trace=statx,fstat,newfstatat -o trace.txt \
  target/dbg/release/irsync daemon --config irsync.toml
# ... run the client against it, then stop the daemon ...
tools/syscall-sites.py trace.txt target/dbg/release/irsync

Each output line is a count, the syscall and where it was called from, innermost frame first (the line numbers depend on the build):

     142  statx ENOENT             part_exists (part_file.rs:353) < open_from (part_file.rs:560) < poll (session/receiver/file_io.rs:461)
     142  statx (fd)               open_locked_part (part_file.rs:435) < open_part_in_dir (part_file.rs:464) < open_sized_part (part_file.rs:1220)

Divide by the number of files to get calls per file, then look for one file paying for the same answer twice: two fstats of the same descriptor, an lstat whose result the next call would report anyway, a call whose outcome is already known. Count again after a change; the same command shows whether each site went away.

Two details the script handles:

  • strace prints a frame’s offset in the file, while addr2line wants a virtual address. The script adds the executable segment’s address minus its offset (readelf -lW), and steps each return address back by one so it lands in the call.
  • With -f, a call interrupted by another thread is printed as <unfinished ...> and later <... resumed>, with its stack after the resumed line. The script attaches it to the right call.

The line tables do not change the code, so counts from this build match the normal release build. -k needs an strace built with stack-trace support (strace -k -e trace=openat true prints stacks if it has it).

bench-link.sh times a push between two machines: irsync against rsync over ssh and tar | nc, on the same tree to the same place. It keeps the runs of a session together, so numbers taken days apart can still be compared. Run it on the client. The server must already be paired (irsync pair myserver), and ssh must reach it.

A session belongs to one environment. Settle the kernel settings and other system settings first, then start a session:

tools/bench-link.sh init ~/bench/usr myserver bench-dest

The destination is on the server; a relative path like bench-dest is under your home directory there. init saves the source, the server and the destination, and takes a snapshot of both machines: the kernel, the network buffer limits (net.core.rmem_max and the like) and the network card with its MTU. Then record the baselines you want, as many runs of each as you like:

tools/bench-link.sh rsync -n 3
tools/bench-link.sh tar_nc -n 3

Now change irsync, its flags or the server’s config, and run it again as often as you need. Give each try a label:

cargo build --release -p irsync
tools/bench-link.sh irsync -n 3 --pair --label "bigger send window"
tools/bench-link.sh irsync -n 3 --label "with --fsync" -- --fsync

--pair installs the new build on the server and restarts its daemon. Without it, irsync stops when the server runs another build. Flags after -- go to irsync. The default label is the commit and the flags.

Every command takes the snapshot again first. When anything in it changed, for example a sysctl on either machine, the command stops and names the change. The baselines no longer fit then, so start a new session with init. The command also stops when the source tree changed.

Each run empties the destination first. It keeps the directory itself, because a deleted export directory breaks irsync’s automatic export (#374). After the run, the script checks that the destination has as many entries and bytes as the source. A run that did not copy everything is kept in the results, but left out of the medians. For each run it records:

  • the wall time;
  • the CPU time used on each machine, from /proc/stat. That is all of the machine’s CPU time, so the kernel’s network work, ssh’s encryption and the irsync daemon all count. Keep both machines quiet while it runs;
  • the bytes the client’s network card sent and received;
  • the UDP packets each machine dropped for lack of buffer space (RcvbufErrors and SndbufErrors in /proc/net/snmp and /proc/net/snmp6).

CPU time, bytes and drops need Linux. On other systems those columns show -.

tools/bench-link.sh show prints the session as Markdown, ready to paste into an issue: the snapshot, then the median of each method and label, and its time divided by the medians of rsync and tar | nc. sessions lists the sessions, and show SESSION prints an older one. Sessions are kept in ~/.local/state/irsync-bench-link (IRSYNC_BENCH_DIR names another place), with each run’s output in the session’s logs directory.

tar_nc listens with nc on port 5201 on the server; init --port N picks another. The port must be open between the two machines.

Measured so far

2026-11-28, San Jose to Europe (143 ms round trip; a copy of /usr, 77k entries, 2.5 GB). Measured by hand before the script existed, in #375:

rmem_max / wmem_maxrsync over sshtar | ncirsyncUDP drops on the server
208 KB (the default)3m03s1m19s8m54sabout 22,600
208 KB, again9m47sabout 22,400
16 MB3m12sabout 11,400

The last two rows differ only in the limit, each with a freshly started daemon. With the default limit the kernel gave irsync’s sockets 208 KB instead of the 7 MiB they ask for, which made irsync about three times slower than rsync. irsync now warns when that happens; see Tune the kernel for long-distance links. The drops that remain at 16 MB are #381.

Known costs

  • SQLite re-preparing a statement. A statement whose plan depends on a bound value (?2 IS NULL OR path > ?2, LIMIT ?3) can be re-prepared on every execution. In perf report this shows up as sqlite3Reprepare. Spell such variants into the SQL text, so each one is prepared and cached once.
  • WAL commits empty other connections’ page caches. Each commit makes every other connection reread the pages it touched. Fewer, larger commits (the index writer batches what arrives within 10 ms, LINGER in index.rs) and memory-mapped reads (PRAGMA mmap_size) keep that cheap. A larger cache_size does not help, because the misses are invalidations, not capacity.
  • Blocking-pool round trips per file. With tens of thousands of small files, the number of spawn_blocking calls per file matters more than what each one does. Doing a file’s related blocking steps in one call saves a thread hand-off, with its futex wake-ups, per step.
  • Syscalls that are expected to fail. Creating a directory that already exists, or checking a path that is not there, costs a syscall per file. Trying the operation that usually succeeds, and recovering only when it fails, removes them.