forked from tigerbeetle/tigerbeetle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs_blocking.zig
50 lines (44 loc) · 1.7 KB
/
fs_blocking.zig
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
const std = @import("std");
const assert = std.debug.assert;
/// Using blocking syscalls, write a page, fsync the write, then read the page back in.
/// Rinse and repeat to iterate across a large file.
///
/// This should be the fastest candidate after io_uring, faster than Zig's evented I/O on Linux
/// since it does not context switch to an async I/O thread.
pub fn main() !void {
const size: usize = 256 * 1024 * 1024;
const page: usize = 4096;
const runs: usize = 5;
const path = "file_blocking";
const file = try std.fs.cwd().createFile(path, .{ .read = true, .truncate = true });
defer file.close();
defer std.fs.cwd().deleteFile(path) catch {};
var buffer_w = [_]u8{1} ** page;
var buffer_r = [_]u8{0} ** page;
var run: usize = 0;
while (run < runs) : (run += 1) {
var start = std.time.milliTimestamp();
var pages: usize = 0;
var syscalls: usize = 0;
var offset: usize = 0;
while (offset < size) {
// We don't want to undercount syscalls and therefore we don't use pwriteAll() or
// preadAll() because these might use more than one syscall.
var wrote = try file.pwrite(buffer_w[0..page], offset);
assert(wrote == page);
try std.os.fsync(file.handle);
var read = try file.pread(buffer_r[0..page], offset);
assert(read == page);
pages += 1;
syscalls += 3;
offset += page;
}
std.debug.print("fs blocking: write({})/fsync/read({}) * {} pages = {} syscalls: {}ms\n", .{
page,
page,
pages,
syscalls,
std.time.milliTimestamp() - start,
});
}
}