-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathflake.nix
178 lines (152 loc) · 7.02 KB
/
flake.nix
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
{
description = "Zig project flake";
inputs = {
zig2nix.url = "github:Cloudef/zig2nix";
};
outputs = { zig2nix, ... }: let
flake-utils = zig2nix.inputs.flake-utils;
in (flake-utils.lib.eachDefaultSystem (system: let
# Zig flake helper
# Check the flake.nix in zig2nix project for more options:
# <https://github.com/Cloudef/zig2nix/blob/master/flake.nix>
env = zig2nix.outputs.zig-env.${system} {
zig = zig2nix.outputs.packages.${system}.zig-master;
};
in with builtins; with env.lib; with env.pkgs.lib; {
# nix run .
apps.default = env.app [] "zig build example -- \"$@\"";
# nix run .#build
apps.build = env.app [] "zig build \"$@\"";
# nix run .#test
apps.test = with env.pkgs; env.app [wasmtime] ''
set -e
zig version
if [[ "$(uname)" == "Linux" ]]; then
echo "zig build test bug -Daio:posix=disable -Dsanitize=true"
zig build test bug -Daio:posix=disable -Dsanitize=true
echo "zig build test bug -Daio:posix=disable -Dsingle-threaded=true"
zig build test bug -Daio:posix=disable -Dsingle-threaded=true
echo "zig build test bug -Daio:posix=force -Dsanitize=true"
zig build test bug -Daio:posix=force -Dsanitize=true
echo "zig build test bug -Daio:posix=force -Dsingle-threaded=true"
zig build test bug -Daio:posix=force -Dsingle-threaded=true
echo "zig build test:aio test:minilib -Dtarget=wasm32-wasi-none"
zig build -Doptimize=ReleaseSafe test:aio test:minilib -Dtarget=wasm32-wasi-none
elif [[ "$(uname)" == "Darwin" ]]; then
echo "zig build test bug"
zig build test bug
echo "zig build test bug -Dsingle-threaded=true"
zig build test bug -Dsingle-threaded=true
echo "zig build test bug -Dtarget=x86_64-macos"
zig build test bug -Dtarget=x86_64-macos
echo "zig build test bug -Dtarget=x86_64-macos -Dsingle-threaded=true"
zig build test bug -Dtarget=x86_64-macos -Dsingle-threaded=true
else
echo "zig build test bug"
zig build test bug
echo "zig build test bug -Dsingle-threaded=true"
zig build test bug -Dsingle-threaded=true
fi
'';
# nix run .#check
apps.check = env.app [] ''
echo "checking the formatting with zig fmt --check"
zig fmt --check .
'';
# nix run .#bench
apps.bench = with env.pkgs; env.app ([git coreutils gnutar] ++ optionals (stdenv.isLinux) [poop]) ''
set -e
tmpdir="$(mktemp -d)"
trap 'rm -rf "$tmpdir"' EXIT
[[ "$(uname)" == "Linux" ]] && zig build -Daio:posix=disable -p "$tmpdir/new-uring"
zig build -Daio:posix=force -p "$tmpdir/new-posix"
git archive "''${COMMIT:-HEAD~1}" | tar x -C "$tmpdir"
cd "$tmpdir"
[[ "$(uname)" == "Linux" ]] && zig build -Daio:posix=disable -p "$tmpdir/old-uring"
zig build -Daio:posix=force -p "$tmpdir/old-posix"
all=(old-posix/bench/*)
for bench in "''${@:-''${all[@]}}"; do
base="$(basename "$bench")"
[[ -f "new-posix/bench/$base" ]] || continue
if [[ "''${POOP:-0}" == 1 ]]; then
if [[ -f "old-uring/bench/$base" ]]; then
poop "old-uring/bench/$base" "new-uring/bench/$base"
poop "old-posix/bench/$base" "new-posix/bench/$base"
else
poop "new-uring/bench/$base"
poop "new-posix/bench/$base"
fi
else
if [[ "$(uname)" == "Linux" ]]; then
if [[ -f "old-uring/bench/$base" ]]; then
echo "$base: old-uring"
"old-uring/bench/$base"
fi
echo "$base: new-uring"
"new-uring/bench/$base"
fi
if [[ -f "old-posix/bench/$base" ]]; then
echo "$base: old-posix"
"old-posix/bench/$base"
fi
echo "$base: new-posix"
"new-posix/bench/$base"
fi
done
'';
# nix develop
devShells.default = env.mkShell {
nativeBuildInputs = with env.pkgs; [wasmtime wineWowPackages.minimal];
};
# nix run .#readme
apps.readme = env.app [] (builtins.replaceStrings ["`"] ["\\`"] ''
cat <<EOF
# zig-aio
zig-aio provides io_uring like asynchronous API and coroutine powered IO tasks for zig
* [Documentation](https://cloudef.github.io/zig-aio)
---
[](https://opensource.org/licenses/MIT)
Project is tested on zig version $(zig version)
## Support matrix
| OS | AIO | CORO |
|---------|-----------------|-----------------|
| Linux | io_uring, posix | x86_64, aarch64 |
| Windows | iocp | x86_64, aarch64 |
| Darwin | posix | x86_64, aarch64 |
| *BSD | posix | x86_64, aarch64 |
| WASI | posix | ❌ |
* io_uring AIO backend is a light wrapper, where most of the code is error mapping
* WASI may eventually get coro support [Stack Switching Proposal](https://github.com/WebAssembly/stack-switching/blob/main/proposals/stack-switching/Explainer.md)
## Example
```zig
$(cat examples/coro.zig)
```
## Syscall overhead
`strace -c` output from the `examples/coro.zig` without `std.log` output and with `std.heap.FixedBufferAllocator`.
This is using the `io_uring` backend. `posix` backend emulates `io_uring` like interface by using a traditional
readiness event loop, thus it will have larger syscall overhead. Posix backend may still be faster than io_uring
depending on usage.
```
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ------------------
0.00 0.000000 0 2 close
0.00 0.000000 0 4 mmap
0.00 0.000000 0 4 munmap
0.00 0.000000 0 5 rt_sigaction
0.00 0.000000 0 1 bind
0.00 0.000000 0 1 listen
0.00 0.000000 0 2 setsockopt
0.00 0.000000 0 1 execve
0.00 0.000000 0 1 arch_prctl
0.00 0.000000 0 1 gettid
0.00 0.000000 0 2 prlimit64
0.00 0.000000 0 2 io_uring_setup
0.00 0.000000 0 6 io_uring_enter
0.00 0.000000 0 1 io_uring_register
------ ----------- ----------- --------- --------- ------------------
100.00 0.000000 0 33 total
```
EOF
'');
}));
}