Skip to content

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

Benchmark file copy buffer sizes

Meleb77960afe80
diff --git a/src/files/copy_bench.zig b/src/files/copy_bench.zig
index e588bda..1fcba80 100644
--- a/src/files/copy_bench.zig
+++ b/src/files/copy_bench.zig
@@ -15,6 +15,12 @@ const Allocator = std.mem.Allocator;
const benchmark_iterations = 5;
const write_buffer_size = 64 * 1024;
+const buffer_sizes = [_]usize{
+ 16 * 1024,
+ 64 * 1024,
+ 256 * 1024,
+ 1024 * 1024,
+};
const job_timeout_ns: u64 = 10 * 60 * std.time.ns_per_s;
fn sleepOneMs() void {
@@ -281,11 +287,12 @@ fn runCopySample(
profile_name: []const u8,
iteration: usize,
expected_bytes: u64,
+ buffer_size: usize,
) !u64 {
const destination = try std.fmt.allocPrint(
allocator,
- "{s}/destination-{s}-{d}",
- .{ benchmark_root, profile_name, iteration },
+ "{s}/destination-{s}-{d}-{d}",
+ .{ benchmark_root, profile_name, buffer_size, iteration },
);
defer allocator.free(destination);
@@ -334,7 +341,12 @@ fn sortSamples(samples: []u64) void {
}
}
-fn emitRow(profile: Profile, stats: WorkloadStats, samples: []const u64) void {
+fn emitRow(
+ profile: Profile,
+ stats: WorkloadStats,
+ samples: []const u64,
+ buffer_size: usize,
+) void {
const median_ns = samples[samples.len / 2];
const mib_per_second =
@as(f64, @floatFromInt(stats.total_bytes)) *
@@ -353,7 +365,7 @@ fn emitRow(profile: Profile, stats: WorkloadStats, samples: []const u64) void {
stats.files,
stats.directories,
stats.total_bytes,
- ops.copy_buffer_size,
+ buffer_size,
benchmark_iterations,
median_ns,
samples[0],
@@ -380,31 +392,37 @@ fn benchmarkProfile(
var runner = try ops.JobRunner.init(allocator);
defer runner.deinit();
- // Warm source data and filesystem metadata outside measured samples.
- _ = try runCopySample(
- allocator,
- runner,
- source_root,
- benchmark_root,
- profile.name,
- 0,
- stats.total_bytes,
- );
+ for (buffer_sizes) |buffer_size| {
+ runner.copy_buffer_bytes = buffer_size;
- var samples: [benchmark_iterations]u64 = undefined;
- for (&samples, 0..) |*sample, iteration| {
- sample.* = try runCopySample(
+ // Warm source data and filesystem metadata for this buffer size.
+ _ = try runCopySample(
allocator,
runner,
source_root,
benchmark_root,
profile.name,
- iteration,
+ 0,
stats.total_bytes,
+ buffer_size,
);
+
+ var samples: [benchmark_iterations]u64 = undefined;
+ for (&samples, 0..) |*sample, iteration| {
+ sample.* = try runCopySample(
+ allocator,
+ runner,
+ source_root,
+ benchmark_root,
+ profile.name,
+ iteration + 1,
+ stats.total_bytes,
+ buffer_size,
+ );
+ }
+ sortSamples(&samples);
+ emitRow(profile, stats, &samples, buffer_size);
}
- sortSamples(&samples);
- emitRow(profile, stats, &samples);
}
pub fn run(allocator: Allocator) !void {
diff --git a/src/files/ops.zig b/src/files/ops.zig
index 817b737..7ca4700 100644
--- a/src/files/ops.zig
+++ b/src/files/ops.zig
@@ -8,6 +8,7 @@ const Allocator = std.mem.Allocator;
const max_progress_path_bytes = 4096;
pub const copy_buffer_size = 64 * 1024;
+const max_copy_buffer_size = 1024 * 1024;
pub const ConflictAction = enum {
ask,
@@ -548,7 +549,9 @@ fn copyFileContentsTracked(
if (runner) |job| job.beginFileProgress(dst_path, @intCast(stat.st_size));
- var buf: [copy_buffer_size]u8 = undefined;
+ var buf: [max_copy_buffer_size]u8 = undefined;
+ const buffer_len = if (runner) |job| job.copy_buffer_bytes else copy_buffer_size;
+ std.debug.assert(buffer_len > 0 and buffer_len <= buf.len);
while (true) {
if (runner) |job| if (!job.waitIfResumed(cancel)) {
_ = c.unlink(dst_z);
@@ -558,7 +561,7 @@ fn copyFileContentsTracked(
_ = c.unlink(dst_z);
return error.Cancelled;
}
- const n = c.read(src_fd, &buf, buf.len);
+ const n = c.read(src_fd, buf[0..buffer_len].ptr, buffer_len);
if (n == 0) break;
if (n < 0 and std.posix.errno(n) == .INTR) continue;
if (n < 0) {
@@ -783,6 +786,8 @@ fn moveItemTracked(
pub const JobRunner = struct {
allocator: Allocator,
+ /// Per-job copy buffer size. Production defaults to 64 KiB.
+ copy_buffer_bytes: usize = copy_buffer_size,
mutex: c.pthread_mutex_t = undefined,
cond: c.pthread_cond_t = undefined,
thread: ?std.Thread = null,