From 90e6d0e1fe45eb1b2435cbbeb8b43e8291952f39 Mon Sep 17 00:00:00 2001 From: Ryan Sepassi Date: Thu, 21 Sep 2023 17:52:30 -0700 Subject: [PATCH 1/7] Updrade xev and test against windows --- .github/workflows/zig.yml | 4 +--- build.zig.zon | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/.github/workflows/zig.yml b/.github/workflows/zig.yml index c9424a4..c97113c 100644 --- a/.github/workflows/zig.yml +++ b/.github/workflows/zig.yml @@ -31,7 +31,5 @@ jobs: version: ${{ matrix.zig-version }} - run: zig env - run: zig build test - # libxev not on windows yet - - if: ${{ matrix.os != 'windows-latest' }} - run: zig build test-aio + - run: zig build test-aio - run: zig build benchmark diff --git a/build.zig.zon b/build.zig.zon index 0214d71..644eeeb 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -3,8 +3,8 @@ .version = "0.0.0", .dependencies = .{ .libxev = .{ - .url = "https://api.github.com/repos/mitchellh/libxev/tarball/f06915", - .hash = "1220622831138efa0105981c34677a254894d33748cc0b2ff9694972ae3ec6408ce1", + .url = "https://api.github.com/repos/rsepassi/libxev/tarball/62196bf", + .hash = "1220f34357168affd9aab1a3fcafcaff093c44beb75ce1d4d4b75490e90729221771", }, }, } From 8d6cc5e070bfed9fdd12f068183dee828aa49330 Mon Sep 17 00:00:00 2001 From: Ryan Sepassi Date: Thu, 21 Sep 2023 18:04:10 -0700 Subject: [PATCH 2/7] windows fixes --- src/asyncio.zig | 2 +- src/test_aio.zig | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/asyncio.zig b/src/asyncio.zig index 68cc7d3..768950d 100644 --- a/src/asyncio.zig +++ b/src/asyncio.zig @@ -463,7 +463,7 @@ pub const AsyncNotification = struct { } const WaitResult = xev.Async.WaitError!void; - pub fn wait(self: Self) !void { + pub fn wait(self: *Self) !void { const Data = XCallback(WaitResult); const loop = getExec(self.exec).loop; diff --git a/src/test_aio.zig b/src/test_aio.zig index f49722a..fad82e5 100644 --- a/src/test_aio.zig +++ b/src/test_aio.zig @@ -300,7 +300,8 @@ fn tcpServer(info: *ServerInfo) !void { try xserver.listen(1); var sock_len = address.getOsSockLen(); - try std.os.getsockname(xserver.fd, &address.any, &sock_len); + const fd = if (xev.backend == .iocp) @as(std.os.windows.ws2_32.SOCKET, @ptrCast(xserver.fd)) else xserver.fd; + try std.os.getsockname(fd, &address.any, &sock_len); info.addr = address; const server = aio.TCP.init(env.exec, xserver); @@ -332,7 +333,8 @@ fn udpServer(info: *ServerInfo) !void { try xserver.bind(address); var sock_len = address.getOsSockLen(); - try std.os.getsockname(xserver.fd, &address.any, &sock_len); + const fd = if (xev.backend == .iocp) @as(std.os.windows.ws2_32.SOCKET, @ptrCast(xserver.fd)) else xserver.fd; + try std.os.getsockname(fd, &address.any, &sock_len); info.addr = address; const server = aio.UDP.init(env.exec, xserver); @@ -360,7 +362,7 @@ const NotifierState = struct { }; fn asyncTest(state: *NotifierState) !void { - const notif = aio.AsyncNotification.init(env.exec, state.x); + var notif = aio.AsyncNotification.init(env.exec, state.x); try notif.wait(); state.notified = true; } From 657aa0cd5e275e09ca50e4ad06a11c19461bd136 Mon Sep 17 00:00:00 2001 From: Ryan Sepassi Date: Thu, 21 Sep 2023 18:12:08 -0700 Subject: [PATCH 3/7] Skip process on Windows --- src/test_aio.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test_aio.zig b/src/test_aio.zig index fad82e5..c2f2cd4 100644 --- a/src/test_aio.zig +++ b/src/test_aio.zig @@ -261,6 +261,7 @@ fn processTest() !void { } test "aio process" { + if (@import("builtin").os.tag == .windows) return; const t = try AioTest.init(); defer t.deinit(); try t.run(processTest); From 9fc61c5d902d135d688512011a5b30bdd03c0566 Mon Sep 17 00:00:00 2001 From: ryan Date: Thu, 28 Sep 2023 14:40:01 -0700 Subject: [PATCH 4/7] Increase stack size --- src/test_aio.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test_aio.zig b/src/test_aio.zig index c2f2cd4..1f12a29 100644 --- a/src/test_aio.zig +++ b/src/test_aio.zig @@ -120,7 +120,7 @@ test "aio concurrent sleep" { const stack = try libcoro.stackAlloc( t.allocator, - 1024 * 8, + null, ); defer t.allocator.free(stack); const before = std.time.milliTimestamp(); From 01753991ac53ebae19482ea7112289b95bff5e8c Mon Sep 17 00:00:00 2001 From: ryan Date: Thu, 28 Sep 2023 14:42:03 -0700 Subject: [PATCH 5/7] try again --- src/test_aio.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test_aio.zig b/src/test_aio.zig index 1f12a29..412b682 100644 --- a/src/test_aio.zig +++ b/src/test_aio.zig @@ -22,7 +22,7 @@ const AioTest = struct { tp.* = xev.ThreadPool.init(.{}); loop.* = try xev.Loop.init(.{ .thread_pool = tp }); exec.* = aio.Executor.init(loop); - const stack_size = 1024 * 128; + const stack_size = 1024 * 512; const num_stacks = 5; const stacks = try allocator.alignedAlloc(u8, libcoro.stack_alignment, num_stacks * stack_size); From 74ff04d0e0c580a39803d3357a2326400ec49950 Mon Sep 17 00:00:00 2001 From: ryan Date: Thu, 28 Sep 2023 16:03:34 -0700 Subject: [PATCH 6/7] and again --- build.zig.zon | 2 +- src/coro.zig | 9 ++++++--- src/test_aio.zig | 11 +++-------- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/build.zig.zon b/build.zig.zon index 644eeeb..3f7551e 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -3,7 +3,7 @@ .version = "0.0.0", .dependencies = .{ .libxev = .{ - .url = "https://api.github.com/repos/rsepassi/libxev/tarball/62196bf", + .url = "https://api.github.com/repos/mitchellh/libxev/tarball/ecbc161", .hash = "1220f34357168affd9aab1a3fcafcaff093c44beb75ce1d4d4b75490e90729221771", }, }, diff --git a/src/coro.zig b/src/coro.zig index 52d5ca0..12eec55 100644 --- a/src/coro.zig +++ b/src/coro.zig @@ -28,7 +28,6 @@ pub const Error = error{ }; pub const StackT = []align(base.stack_alignment) u8; pub const stack_alignment = base.stack_alignment; -pub const default_stack_size = libcoro_options.default_stack_size; pub const Frame = *Coro; @@ -36,6 +35,10 @@ pub const Env = struct { stack_allocator: ?std.mem.Allocator = null, default_stack_size: ?usize = null, executor: ?*Executor = null, + + fn getDefaultStackSize(self: @This()) usize { + return self.default_stack_size orelse libcoro_options.default_stack_size; + } }; threadlocal var env: Env = .{}; pub fn initEnv(e: Env) void { @@ -55,7 +58,7 @@ fn getStack(stack: anytype) !StackInfo { if (T == @TypeOf(null) or (is_optional and stack == null)) { // Use env allocator with default stack size if (env.stack_allocator == null) @panic("No explicit stack passed and no default stack allocator available"); - return .{ .stack = try stackAlloc(env.stack_allocator.?, env.default_stack_size), .owned = true }; + return .{ .stack = try stackAlloc(env.stack_allocator.?, null), .owned = true }; } else if (T == comptime_int or T == usize) { // Use env allocator with provided stack size const stack_size: usize = @intCast(stack); @@ -102,7 +105,7 @@ pub const FrameT = CoroT.fromFunc; // Allocate a stack suitable for coroutine usage. // Caller is responsible for freeing memory. pub fn stackAlloc(allocator: std.mem.Allocator, size: ?usize) !StackT { - return try allocator.alignedAlloc(u8, stack_alignment, size orelse default_stack_size); + return try allocator.alignedAlloc(u8, stack_alignment, size orelse env.getDefaultStackSize()); } // True if within a coroutine, false if at top-level. diff --git a/src/test_aio.zig b/src/test_aio.zig index 412b682..415c6f7 100644 --- a/src/test_aio.zig +++ b/src/test_aio.zig @@ -10,7 +10,6 @@ const AioTest = struct { tp: *xev.ThreadPool, loop: *xev.Loop, exec: *aio.Executor, - stacks: []u8, fn init() !@This() { const allocator = std.testing.allocator; @@ -22,9 +21,7 @@ const AioTest = struct { tp.* = xev.ThreadPool.init(.{}); loop.* = try xev.Loop.init(.{ .thread_pool = tp }); exec.* = aio.Executor.init(loop); - const stack_size = 1024 * 512; - const num_stacks = 5; - const stacks = try allocator.alignedAlloc(u8, libcoro.stack_alignment, num_stacks * stack_size); + const stack_size = 1024 * 32; // Thread-local env env = .{ @@ -43,7 +40,6 @@ const AioTest = struct { .tp = tp, .loop = loop, .exec = exec, - .stacks = stacks, }; } @@ -54,7 +50,6 @@ const AioTest = struct { self.allocator.destroy(self.tp); self.allocator.destroy(self.loop); self.allocator.destroy(self.exec); - self.allocator.free(self.stacks); } fn run(self: @This(), func: anytype) !void { @@ -406,8 +401,8 @@ test "aio concurrent sleep env" { try aio.run(null, sleepTaskEnv, .{}, null); const after = std.time.milliTimestamp(); - try std.testing.expect(after > (before + 17)); - try std.testing.expect(after < (before + 23)); + try std.testing.expect(after > (before + 16)); + try std.testing.expect(after < (before + 24)); } const UsizeChannel = libcoro.Channel(usize, .{ .capacity = 10 }); From 56ceed96235efc9a0729c1c85475fe01af9a4b8a Mon Sep 17 00:00:00 2001 From: ryan Date: Thu, 28 Sep 2023 16:11:42 -0700 Subject: [PATCH 7/7] revert default stack size --- src/test_aio.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test_aio.zig b/src/test_aio.zig index 415c6f7..afc4f73 100644 --- a/src/test_aio.zig +++ b/src/test_aio.zig @@ -21,7 +21,7 @@ const AioTest = struct { tp.* = xev.ThreadPool.init(.{}); loop.* = try xev.Loop.init(.{ .thread_pool = tp }); exec.* = aio.Executor.init(loop); - const stack_size = 1024 * 32; + const stack_size = 1024 * 128; // Thread-local env env = .{