Skip to content

RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process

Add reflink copy strategy and benchmarks

Melc2d8cb229eec
diff --git a/src/files/copy_bench.zig b/src/files/copy_bench.zig
index 6891c8e..52649c2 100644
--- a/src/files/copy_bench.zig
+++ b/src/files/copy_bench.zig
@@ -371,7 +371,7 @@ fn emitRow(
@as(f64, @floatFromInt(median_ns));
std.debug.print(
- "BENCH,A,job_copy_e2e_{s},{s},warm_source_cache,{d},{d},{d},{d},{d},{d},{d},{d},{d:.2},{d:.2},{d},{d},{d},{d},{d},{d},{d},{d},{d},{d},{d},{d},{d},{d},{d},{d},{d}\n",
+ "BENCH,A,job_copy_e2e_{s},{s},warm_source_cache,{d},{d},{d},{d},{d},{d},{d},{d},{d:.2},{d:.2},{d},{d},{d},{d},{d},{d},{d},{d},{d},{d},{d},{d},{d},{d},{d},{d},{d},{d},{d},{d}\n",
.{
strategy.name(),
profile.name,
@@ -385,6 +385,9 @@ fn emitRow(
samples[samples.len - 1],
mib_per_second,
files_per_second,
+ metrics.reflink_calls,
+ metrics.reflink_bytes,
+ metrics.reflink_fallback_files,
metrics.copy_file_range_calls,
metrics.copy_file_range_bytes,
metrics.buffered_fallback_files,
@@ -440,13 +443,22 @@ fn benchmarkProfile(
defer runner.deinit();
runner.profile_enabled = true;
- const strategy_count = if (builtin.os.tag == .linux) 2 else 1;
+ const strategy_count = if (builtin.os.tag == .linux) 3 else 1;
for (0..strategy_count) |strategy_index| {
const strategy: ops.CopyStrategy =
- if (strategy_index == 0) .buffered else .copy_file_range;
+ switch (strategy_index) {
+ 0 => .buffered,
+ 1 => .copy_file_range,
+ else => .reflink,
+ };
runner.copy_strategy = strategy;
for (buffer_sizes) |buffer_size| {
+ // Reflink does not use the copy buffer. Compare it at the
+ // production 64 KiB baseline without repeating identical runs.
+ if (strategy == .reflink and buffer_size != write_buffer_size) {
+ continue;
+ }
runner.copy_buffer_bytes = buffer_size;
// Warm source data and filesystem metadata for this combination.
@@ -509,7 +521,7 @@ pub fn run(allocator: Allocator) !void {
defer ops.deleteRecursive(allocator, benchmark_root) catch {};
std.debug.print(
- "BENCH,phase,operation,profile,cache_mode,files,directories,total_bytes,buffer_bytes,iterations,median_ns,min_ns,max_ns,median_mib_per_second,median_files_per_second,copy_file_range_calls,copy_file_range_bytes,buffered_fallback_files,manifest_scan_ns,destination_resolution_ns,recursive_copy_ns,data_transfer_ns,lstat_calls,fstat_calls,opendir_calls,directory_entries,file_open_calls,directory_create_calls,path_allocations,path_allocation_bytes,manifest_lookup_comparisons,mutex_lock_calls\n",
+ "BENCH,phase,operation,profile,cache_mode,files,directories,total_bytes,buffer_bytes,iterations,median_ns,min_ns,max_ns,median_mib_per_second,median_files_per_second,reflink_calls,reflink_bytes,reflink_fallback_files,copy_file_range_calls,copy_file_range_bytes,buffered_fallback_files,manifest_scan_ns,destination_resolution_ns,recursive_copy_ns,data_transfer_ns,lstat_calls,fstat_calls,opendir_calls,directory_entries,file_open_calls,directory_create_calls,path_allocations,path_allocation_bytes,manifest_lookup_comparisons,mutex_lock_calls\n",
.{},
);
diff --git a/src/files/ops.zig b/src/files/ops.zig
index a89fe94..e877281 100644
--- a/src/files/ops.zig
+++ b/src/files/ops.zig
@@ -14,16 +14,21 @@ const max_copy_buffer_size = 1024 * 1024;
pub const CopyStrategy = enum {
buffered,
copy_file_range,
+ reflink,
pub fn name(self: CopyStrategy) []const u8 {
return switch (self) {
.buffered => "buffered",
.copy_file_range => "copy_file_range",
+ .reflink => "reflink",
};
}
};
pub const CopyMetrics = struct {
+ reflink_calls: u64 = 0,
+ reflink_bytes: u64 = 0,
+ reflink_fallback_files: u64 = 0,
copy_file_range_calls: u64 = 0,
copy_file_range_bytes: u64 = 0,
buffered_fallback_files: u64 = 0,
@@ -667,7 +672,47 @@ fn copyFileContentsTracked(
var use_buffered_copy = true;
if (comptime builtin.os.tag == .linux) {
if (runner) |job| {
- if (job.copy_strategy == .copy_file_range) {
+ if (job.copy_strategy == .reflink) {
+ if (!job.waitIfResumed(cancel) or
+ (cancel != null and cancel.?.load(.acquire)))
+ {
+ _ = c.unlink(dst_z);
+ return error.Cancelled;
+ }
+
+ job.recordReflinkCall();
+ // Linux FICLONE: _IOW(0x94, 9, int).
+ const clone_result = c.ioctl(
+ dst_fd,
+ @as(c_ulong, 0x40049409),
+ src_fd,
+ );
+ if (clone_result == 0) {
+ const file_size: u64 = @intCast(stat.st_size);
+ job.recordReflinkBytes(file_size);
+ job.addFileTransferredBytes(file_size);
+ return;
+ }
+
+ const clone_errno = std.c._errno().*;
+ // Unsupported filesystems and cross-filesystem clones can
+ // still use the normal kernel or buffered copy paths.
+ if (clone_errno == c.EXDEV or
+ clone_errno == c.EINVAL or
+ clone_errno == c.ENOSYS or
+ clone_errno == c.ENOTTY or
+ clone_errno == c.EOPNOTSUPP)
+ {
+ job.recordReflinkFallback();
+ } else {
+ _ = c.unlink(dst_z);
+ return error.CopyFailed;
+ }
+ }
+
+ if (job.copy_strategy == .copy_file_range or
+ job.copy_strategy == .reflink)
+ {
use_buffered_copy = false;
while (true) {
if (!job.waitIfResumed(cancel)) {
@@ -1403,6 +1448,27 @@ pub const JobRunner = struct {
_ = c.pthread_mutex_unlock(&self.mutex);
}
+ fn recordReflinkCall(self: *JobRunner) void {
+ self.profileCount(&self.profile_metrics.mutex_lock_calls, 1);
+ _ = c.pthread_mutex_lock(&self.mutex);
+ self.copy_metrics.reflink_calls +|= 1;
+ _ = c.pthread_mutex_unlock(&self.mutex);
+ }
+
+ fn recordReflinkBytes(self: *JobRunner, bytes: u64) void {
+ self.profileCount(&self.profile_metrics.mutex_lock_calls, 1);
+ _ = c.pthread_mutex_lock(&self.mutex);
+ self.copy_metrics.reflink_bytes +|= bytes;
+ _ = c.pthread_mutex_unlock(&self.mutex);
+ }
+
+ fn recordReflinkFallback(self: *JobRunner) void {
+ self.profileCount(&self.profile_metrics.mutex_lock_calls, 1);
+ _ = c.pthread_mutex_lock(&self.mutex);
+ self.copy_metrics.reflink_fallback_files +|= 1;
+ _ = c.pthread_mutex_unlock(&self.mutex);
+ }
+
fn recordCopyFileRangeBytes(self: *JobRunner, bytes: u64) void {
self.profileCount(&self.profile_metrics.mutex_lock_calls, 1);
_ = c.pthread_mutex_lock(&self.mutex);