@amritk/nish-x86_64-linux 0.47.0 → 0.49.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/nish +0 -0
- package/docs/AI.md +3 -1
- package/package.json +1 -1
- package/runtime/gfx/README.md +55 -0
- package/runtime/gfx/nish-gfx.c +1063 -0
- package/runtime/gfx/nish-gfx.h +221 -0
- package/runtime/gfx/nish-gfx.overrides.json +14 -0
- package/runtime/nish.d.ts +22 -5
- package/runtime/nish.mjs +1 -0
- package/runtime/runtime-net.c +53 -27
- package/runtime/runtime-uring.c +39 -7
- package/runtime/sqlite/README.md +17 -0
- package/runtime/sqlite/nish-sqlite.c +97 -0
- package/scripts/build.sh +88 -3
- package/std/compress/deflate.ts +1371 -704
- package/std/net/handler.ts +24 -6
- package/std/net/http-server.ts +543 -156
- package/std/net/http1-server.ts +29 -2
- package/std/net/http2-cleartext.ts +30 -1
- package/std/raster.ts +35 -21
- package/std/sync/shm.ts +6 -3
- package/std/synth.ts +9 -1
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* nish-gfx.h: the C surface Nish programs draw through.
|
|
3
|
+
*
|
|
4
|
+
* A thin, synchronous shim over wgpu-native (WebGPU) and SDL3. It exists so
|
|
5
|
+
* that `nish c-import` can read it: every parameter is a scalar, an opaque
|
|
6
|
+
* handle or a struct passed by pointer; there are no unions, no structs by
|
|
7
|
+
* value, no callbacks and no variadics. A byte buffer is a `void *` or
|
|
8
|
+
* `const void *` followed by its `size_t` length. WebGPU enum and flag values
|
|
9
|
+
* cross as plain uint32_t, with webgpu.h's numbering. This header does not
|
|
10
|
+
* include webgpu.h or SDL.h; only nish-gfx.c does.
|
|
11
|
+
*
|
|
12
|
+
* Errors never cross as callbacks. Every device-lost and uncaptured-error
|
|
13
|
+
* report is copied into a fixed ring inside the device and drained with
|
|
14
|
+
* nishgfx_error_next(); a report that finds the ring full is counted by
|
|
15
|
+
* nishgfx_errors_dropped(). Failures before a device exists (no adapter, a
|
|
16
|
+
* refused device) are queued on the instance the same way.
|
|
17
|
+
*/
|
|
18
|
+
#ifndef NISH_GFX_H
|
|
19
|
+
#define NISH_GFX_H
|
|
20
|
+
|
|
21
|
+
#include <stddef.h>
|
|
22
|
+
#include <stdint.h>
|
|
23
|
+
|
|
24
|
+
#ifdef __cplusplus
|
|
25
|
+
extern "C" {
|
|
26
|
+
#endif
|
|
27
|
+
|
|
28
|
+
typedef struct NishGfxInstance NishGfxInstance;
|
|
29
|
+
typedef struct NishGfxWindow NishGfxWindow;
|
|
30
|
+
typedef struct NishGfxSurface NishGfxSurface;
|
|
31
|
+
typedef struct NishGfxAdapter NishGfxAdapter;
|
|
32
|
+
typedef struct NishGfxDevice NishGfxDevice;
|
|
33
|
+
typedef struct NishGfxBuffer NishGfxBuffer;
|
|
34
|
+
typedef struct NishGfxShader NishGfxShader;
|
|
35
|
+
typedef struct NishGfxPipelineDesc NishGfxPipelineDesc;
|
|
36
|
+
typedef struct NishGfxPipeline NishGfxPipeline;
|
|
37
|
+
typedef struct NishGfxBindGroupDesc NishGfxBindGroupDesc;
|
|
38
|
+
typedef struct NishGfxBindGroup NishGfxBindGroup;
|
|
39
|
+
typedef struct NishGfxEncoder NishGfxEncoder;
|
|
40
|
+
typedef struct NishGfxPass NishGfxPass;
|
|
41
|
+
typedef struct NishGfxView NishGfxView;
|
|
42
|
+
typedef struct NishGfxOffscreen NishGfxOffscreen;
|
|
43
|
+
|
|
44
|
+
/* Error kinds nishgfx_error_next() answers. */
|
|
45
|
+
#define NISHGFX_ERROR_NONE 0
|
|
46
|
+
#define NISHGFX_ERROR_VALIDATION 1
|
|
47
|
+
#define NISHGFX_ERROR_OUT_OF_MEMORY 2
|
|
48
|
+
#define NISHGFX_ERROR_INTERNAL 3
|
|
49
|
+
#define NISHGFX_ERROR_DEVICE_LOST 4
|
|
50
|
+
|
|
51
|
+
/* The error ring's size, per instance and per device. */
|
|
52
|
+
#define NISHGFX_ERROR_RING 16
|
|
53
|
+
/* The longest message an entry keeps, without its NUL. */
|
|
54
|
+
#define NISHGFX_ERROR_MESSAGE_MAX 255
|
|
55
|
+
|
|
56
|
+
/* Event kinds nishgfx_event_next() answers. */
|
|
57
|
+
#define NISHGFX_EVENT_NONE 0
|
|
58
|
+
#define NISHGFX_EVENT_QUIT 1
|
|
59
|
+
#define NISHGFX_EVENT_KEY_DOWN 2
|
|
60
|
+
#define NISHGFX_EVENT_KEY_UP 3
|
|
61
|
+
#define NISHGFX_EVENT_MOUSE_MOVE 4
|
|
62
|
+
#define NISHGFX_EVENT_MOUSE_DOWN 5
|
|
63
|
+
#define NISHGFX_EVENT_MOUSE_UP 6
|
|
64
|
+
#define NISHGFX_EVENT_WHEEL 7
|
|
65
|
+
#define NISHGFX_EVENT_RESIZED 8
|
|
66
|
+
|
|
67
|
+
/* The event ring's size, per window. */
|
|
68
|
+
#define NISHGFX_EVENT_RING 64
|
|
69
|
+
|
|
70
|
+
/* One input or window event. Fields a kind does not use are zero. */
|
|
71
|
+
typedef struct NishGfxEvent {
|
|
72
|
+
uint32_t kind; /* NISHGFX_EVENT_* */
|
|
73
|
+
uint32_t key; /* SDL keycode, for KEY_DOWN and KEY_UP */
|
|
74
|
+
uint32_t button; /* 1 left, 2 middle, 3 right, for MOUSE_DOWN and MOUSE_UP */
|
|
75
|
+
uint32_t modifiers; /* SDL_Keymod bits, for keys */
|
|
76
|
+
float x, y; /* pointer position in window pixels */
|
|
77
|
+
float dx, dy; /* motion or wheel delta */
|
|
78
|
+
uint32_t width, height; /* new size in pixels, for RESIZED */
|
|
79
|
+
} NishGfxEvent;
|
|
80
|
+
|
|
81
|
+
/* A buffer to create. `usage` is WGPUBufferUsage bits. */
|
|
82
|
+
typedef struct NishGfxBufferDesc {
|
|
83
|
+
uint64_t size;
|
|
84
|
+
uint32_t usage;
|
|
85
|
+
uint32_t mapped_at_creation;
|
|
86
|
+
} NishGfxBufferDesc;
|
|
87
|
+
|
|
88
|
+
/* ---- instance, window, surface ---------------------------------------- */
|
|
89
|
+
|
|
90
|
+
/* A WebGPU instance. Answers null if wgpu-native refuses one. */
|
|
91
|
+
NishGfxInstance *nishgfx_instance_create(void);
|
|
92
|
+
/* Release it last: every surface, adapter and device made from it keeps it,
|
|
93
|
+
* and a device's readback drives events through it. */
|
|
94
|
+
void nishgfx_instance_release(NishGfxInstance *instance);
|
|
95
|
+
/* Drain one queued instance error (adapter or device refusal) into msg,
|
|
96
|
+
* NUL-terminated and cut to cap - 1 bytes; answers its kind, 0 when empty. */
|
|
97
|
+
int32_t nishgfx_instance_error_next(NishGfxInstance *instance, void *msg, size_t cap);
|
|
98
|
+
|
|
99
|
+
/* An SDL3 window of width x height pixels; `title` is NUL-terminated UTF-8.
|
|
100
|
+
* Answers null for a 0 side or one past INT32_MAX, when SDL has no video
|
|
101
|
+
* driver, when memory or SDL's window properties run out, and when SDL
|
|
102
|
+
* refuses the window; SDL_GetError() says why for the SDL failures. */
|
|
103
|
+
NishGfxWindow *nishgfx_window_create(const char *title, uint32_t width, uint32_t height);
|
|
104
|
+
/* Release a surface made on the window first: it reads the window's size. */
|
|
105
|
+
void nishgfx_window_release(NishGfxWindow *window);
|
|
106
|
+
/* Move every pending SDL event into the window's ring; answers how many were
|
|
107
|
+
* queued. Events past the ring's size are dropped and counted. */
|
|
108
|
+
uint32_t nishgfx_events_poll(NishGfxWindow *window);
|
|
109
|
+
/* Take the oldest queued event into *out; answers its kind, 0 when empty. */
|
|
110
|
+
uint32_t nishgfx_event_next(NishGfxWindow *window, NishGfxEvent *out);
|
|
111
|
+
uint32_t nishgfx_events_dropped(NishGfxWindow *window);
|
|
112
|
+
|
|
113
|
+
/* A surface on the window, through Metal, Wayland or X11 as SDL's current
|
|
114
|
+
* video driver says. Answers null (and queues an instance error) otherwise. */
|
|
115
|
+
NishGfxSurface *nishgfx_surface_create(NishGfxInstance *instance, NishGfxWindow *window);
|
|
116
|
+
void nishgfx_surface_release(NishGfxSurface *surface);
|
|
117
|
+
/* Configure for width x height. vsync != 0 is Fifo; otherwise Mailbox, then
|
|
118
|
+
* Immediate, then Fifo, whichever the surface offers first. Answers 0, or -1
|
|
119
|
+
* (and queues a device error) for a 0 side or a surface with no format. */
|
|
120
|
+
int32_t nishgfx_surface_configure(NishGfxSurface *surface, NishGfxAdapter *adapter, NishGfxDevice *device,
|
|
121
|
+
uint32_t width, uint32_t height, uint32_t vsync);
|
|
122
|
+
/* The WGPUTextureFormat configure picked. */
|
|
123
|
+
uint32_t nishgfx_surface_format(NishGfxSurface *surface);
|
|
124
|
+
/* The next frame's view, or null to skip this frame (an outdated, lost or
|
|
125
|
+
* timed-out surface is reconfigured at the window's pixel size, or left until
|
|
126
|
+
* a frame where that size is above 0; any other failure queues a device
|
|
127
|
+
* error). The view lives until nishgfx_surface_present(). */
|
|
128
|
+
NishGfxView *nishgfx_surface_acquire(NishGfxSurface *surface);
|
|
129
|
+
void nishgfx_surface_present(NishGfxSurface *surface);
|
|
130
|
+
|
|
131
|
+
/* ---- adapter, device, errors ------------------------------------------- */
|
|
132
|
+
|
|
133
|
+
/* An adapter, compatible with `surface` when it is not null. power is a
|
|
134
|
+
* WGPUPowerPreference. Answers null and queues an instance error on failure. */
|
|
135
|
+
NishGfxAdapter *nishgfx_adapter_request(NishGfxInstance *instance, NishGfxSurface *surface, uint32_t power);
|
|
136
|
+
void nishgfx_adapter_release(NishGfxAdapter *adapter);
|
|
137
|
+
/* A device with default limits. Answers null and queues an instance error on
|
|
138
|
+
* failure. Its device-lost and uncaptured-error reports go to its ring. */
|
|
139
|
+
NishGfxDevice *nishgfx_device_request(NishGfxInstance *instance, NishGfxAdapter *adapter);
|
|
140
|
+
/* Release every buffer, shader, pipeline, bind group, offscreen target and
|
|
141
|
+
* surface made from the device first: one used after this can report an
|
|
142
|
+
* error into the freed device. */
|
|
143
|
+
void nishgfx_device_release(NishGfxDevice *device);
|
|
144
|
+
/* Drain one queued device error into msg (as nishgfx_instance_error_next). */
|
|
145
|
+
int32_t nishgfx_error_next(NishGfxDevice *device, void *msg, size_t cap);
|
|
146
|
+
/* How many reports found the ring full, since the device was made. */
|
|
147
|
+
uint32_t nishgfx_errors_dropped(NishGfxDevice *device);
|
|
148
|
+
|
|
149
|
+
/* ---- resources --------------------------------------------------------- */
|
|
150
|
+
|
|
151
|
+
/* `label` may be null. Answers null when wgpu-native does. */
|
|
152
|
+
NishGfxBuffer *nishgfx_buffer_create(NishGfxDevice *device, const NishGfxBufferDesc *desc, const char *label);
|
|
153
|
+
void nishgfx_buffer_release(NishGfxBuffer *buffer);
|
|
154
|
+
/* Copy len bytes of data into buffer at offset, on the device's queue. */
|
|
155
|
+
void nishgfx_queue_write(NishGfxDevice *device, NishGfxBuffer *buffer, uint64_t offset,
|
|
156
|
+
const void *data, size_t len);
|
|
157
|
+
/* A shader module from NUL-terminated WGSL. A compile error is queued on the
|
|
158
|
+
* device; the module answered is then invalid but safe to pass on. */
|
|
159
|
+
NishGfxShader *nishgfx_shader_create(NishGfxDevice *device, const char *wgsl);
|
|
160
|
+
void nishgfx_shader_release(NishGfxShader *shader);
|
|
161
|
+
|
|
162
|
+
/* A render pipeline builder: one vertex buffer of `stride` bytes (0 for no
|
|
163
|
+
* vertex buffer), one color target of `color_format`, triangle list. */
|
|
164
|
+
NishGfxPipelineDesc *nishgfx_pipeline_desc_new(NishGfxShader *shader, const char *vs_entry, const char *fs_entry,
|
|
165
|
+
uint32_t color_format, uint64_t stride);
|
|
166
|
+
/* Add a vertex attribute (a WGPUVertexFormat at offset, shader location).
|
|
167
|
+
* Answers 0, or -1 once the builder holds 16. */
|
|
168
|
+
int32_t nishgfx_pipeline_desc_attribute(NishGfxPipelineDesc *desc, uint32_t format, uint64_t offset,
|
|
169
|
+
uint32_t location);
|
|
170
|
+
/* Build the pipeline and free the builder, either way. */
|
|
171
|
+
NishGfxPipeline *nishgfx_pipeline_create(NishGfxDevice *device, NishGfxPipelineDesc *desc);
|
|
172
|
+
void nishgfx_pipeline_release(NishGfxPipeline *pipeline);
|
|
173
|
+
|
|
174
|
+
/* A bind group builder for group `index` of the pipeline's layout. */
|
|
175
|
+
NishGfxBindGroupDesc *nishgfx_bind_group_desc_new(NishGfxPipeline *pipeline, uint32_t index);
|
|
176
|
+
/* Bind size bytes of buffer at offset to `binding`; size 0 binds the rest of
|
|
177
|
+
* the buffer from offset. Answers 0, or -1 once the builder holds 16. */
|
|
178
|
+
int32_t nishgfx_bind_group_desc_buffer(NishGfxBindGroupDesc *desc, uint32_t binding, NishGfxBuffer *buffer,
|
|
179
|
+
uint64_t offset, uint64_t size);
|
|
180
|
+
/* Build the bind group and free the builder, either way. */
|
|
181
|
+
NishGfxBindGroup *nishgfx_bind_group_create(NishGfxDevice *device, NishGfxBindGroupDesc *desc);
|
|
182
|
+
void nishgfx_bind_group_release(NishGfxBindGroup *group);
|
|
183
|
+
|
|
184
|
+
/* ---- encoding ---------------------------------------------------------- */
|
|
185
|
+
|
|
186
|
+
NishGfxEncoder *nishgfx_encoder_create(NishGfxDevice *device);
|
|
187
|
+
/* Finish the encoder, submit it on the device's queue, and free it. */
|
|
188
|
+
void nishgfx_encoder_submit(NishGfxDevice *device, NishGfxEncoder *encoder);
|
|
189
|
+
|
|
190
|
+
/* A render pass on one color view. load != 0 keeps the view's contents;
|
|
191
|
+
* otherwise it is cleared to (r, g, b, a). */
|
|
192
|
+
NishGfxPass *nishgfx_pass_begin(NishGfxEncoder *encoder, NishGfxView *view, uint32_t load,
|
|
193
|
+
double r, double g, double b, double a);
|
|
194
|
+
void nishgfx_pass_set_pipeline(NishGfxPass *pass, NishGfxPipeline *pipeline);
|
|
195
|
+
void nishgfx_pass_set_bind_group(NishGfxPass *pass, uint32_t index, NishGfxBindGroup *group);
|
|
196
|
+
/* size 0 binds the rest of the buffer from offset. */
|
|
197
|
+
void nishgfx_pass_set_vertex_buffer(NishGfxPass *pass, uint32_t slot, NishGfxBuffer *buffer, uint64_t offset,
|
|
198
|
+
uint64_t size);
|
|
199
|
+
void nishgfx_pass_draw(NishGfxPass *pass, uint32_t vertices, uint32_t instances, uint32_t first_vertex,
|
|
200
|
+
uint32_t first_instance);
|
|
201
|
+
/* End the pass and free it. */
|
|
202
|
+
void nishgfx_pass_end(NishGfxPass *pass);
|
|
203
|
+
|
|
204
|
+
/* ---- offscreen --------------------------------------------------------- */
|
|
205
|
+
|
|
206
|
+
/* A width x height RGBA8Unorm render target with readback; needs no window
|
|
207
|
+
* and initialises no SDL video. */
|
|
208
|
+
NishGfxOffscreen *nishgfx_offscreen_create(NishGfxDevice *device, uint32_t width, uint32_t height);
|
|
209
|
+
void nishgfx_offscreen_release(NishGfxOffscreen *offscreen);
|
|
210
|
+
/* The target's view, valid until the offscreen is released. */
|
|
211
|
+
NishGfxView *nishgfx_offscreen_view(NishGfxOffscreen *offscreen);
|
|
212
|
+
/* Copy the target's pixels, width * 4 bytes per row, top row first, into dst.
|
|
213
|
+
* Answers 0; -1 when cap < width * height * 4 (dst untouched); -2 when the
|
|
214
|
+
* map fails (an error is queued on the device). */
|
|
215
|
+
int32_t nishgfx_offscreen_read(NishGfxOffscreen *offscreen, void *dst, size_t cap);
|
|
216
|
+
|
|
217
|
+
#ifdef __cplusplus
|
|
218
|
+
}
|
|
219
|
+
#endif
|
|
220
|
+
|
|
221
|
+
#endif /* NISH_GFX_H */
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"header": "nish-gfx.h",
|
|
3
|
+
"allow": ["nishgfx_*"],
|
|
4
|
+
"functions": {
|
|
5
|
+
"nishgfx_instance_error_next": { "msg": { "buffer": "cap" } },
|
|
6
|
+
"nishgfx_error_next": { "msg": { "buffer": "cap" } },
|
|
7
|
+
"nishgfx_queue_write": { "data": { "buffer": "len" } },
|
|
8
|
+
"nishgfx_offscreen_read": { "dst": { "buffer": "cap" } },
|
|
9
|
+
"nishgfx_buffer_create": { "label": { "nullable": true } },
|
|
10
|
+
"nishgfx_adapter_request": { "surface": { "nullable": true } },
|
|
11
|
+
"nishgfx_pass_set_bind_group": { "group": { "nullable": true } },
|
|
12
|
+
"nishgfx_pass_set_vertex_buffer": { "buffer": { "nullable": true } }
|
|
13
|
+
}
|
|
14
|
+
}
|
package/runtime/nish.d.ts
CHANGED
|
@@ -1674,14 +1674,16 @@ declare function tcpListen(host: string, port: i32, backlog: i32): i32;
|
|
|
1674
1674
|
* Flag 8 (`NET_LISTEN_RING` in `nish:net`) accepts into io_uring slots on
|
|
1675
1675
|
* Linux, so connections are not bound by the descriptor limit; where rings are
|
|
1676
1676
|
* refused the listener serves descriptors and `netRingState` says why. Elsewhere
|
|
1677
|
-
* the flag is accepted and does nothing.
|
|
1677
|
+
* the flag is accepted and does nothing. Flag 16 (`NET_LISTEN_RING_WIDE`), with 8, makes each
|
|
1678
|
+
* ring hold as many slots as the descriptor limit allows, up to 65,536 rather than 16,384: fewer
|
|
1679
|
+
* rings for a busy loop to enter, at about 110 bytes a slot per ring up front.
|
|
1678
1680
|
*
|
|
1679
1681
|
* @param host the numeric address to bind.
|
|
1680
1682
|
* @param port the port, 0 for any.
|
|
1681
1683
|
* @param backlog the kernel's queue of unaccepted connections.
|
|
1682
|
-
* @param flags 1, 2, 4 and
|
|
1684
|
+
* @param flags 1, 2, 4, 8 and 16 as above.
|
|
1683
1685
|
* @returns the listener's descriptor, or a negative errno: -22 for any other bit (a negative
|
|
1684
|
-
* `flags` included) or
|
|
1686
|
+
* `flags` included), 4 without 1 or 16 without 8, before a socket is made; -95 for flag 4 off Linux; -98
|
|
1685
1687
|
* for a port in use; -23 once the process has opened 31 ring listeners, closed or not.
|
|
1686
1688
|
*/
|
|
1687
1689
|
declare function tcpListenFlags(host: string, port: i32, backlog: i32, flags: i32): i32;
|
|
@@ -2277,6 +2279,15 @@ declare const NET_UNIX_SEQPACKET: i32;
|
|
|
2277
2279
|
* descriptors, and `netRingState` says why. Inert on Darwin. Capability: none.
|
|
2278
2280
|
*/
|
|
2279
2281
|
declare const NET_LISTEN_RING: i32;
|
|
2282
|
+
/**
|
|
2283
|
+
* `tcpListenFlags` flag 16, with `NET_LISTEN_RING`: each of the listener's rings holds as many
|
|
2284
|
+
* slots as `RLIMIT_NOFILE` allows, up to 65,536, rather than up to 16,384. A busy loop enters
|
|
2285
|
+
* each ring once a pass, so fewer, wider rings cost it less; each slot costs about 110 bytes a
|
|
2286
|
+
* ring up front, about 7 MB for a ring of 65,536. A ring listener under `--sandbox` takes rings
|
|
2287
|
+
* made before its filter, at the narrow width. Without `NET_LISTEN_RING` it is -22. Inert on
|
|
2288
|
+
* Darwin. Capability: none.
|
|
2289
|
+
*/
|
|
2290
|
+
declare const NET_LISTEN_RING_WIDE: i32;
|
|
2280
2291
|
/**
|
|
2281
2292
|
* Whether listeners opened with `NET_LISTEN_RING` accept into rings: 1 when they do, 0 until a
|
|
2282
2293
|
* listener has found rings or a refusal, or the negative errno the kernel refused rings with (-1
|
|
@@ -3220,13 +3231,17 @@ declare module "nish:net" {
|
|
|
3220
3231
|
* memory serves descriptors itself, and a later listener asks again. Off
|
|
3221
3232
|
* Linux the flag is accepted and does nothing.
|
|
3222
3233
|
*
|
|
3234
|
+
* Flag 16, `NET_LISTEN_RING_WIDE`, with flag 8, makes each ring hold as
|
|
3235
|
+
* many slots as `RLIMIT_NOFILE` allows, up to 65,536 rather than 16,384.
|
|
3236
|
+
*
|
|
3223
3237
|
* @param host A numeric host, as for `tcpListen`.
|
|
3224
3238
|
* @param port The port.
|
|
3225
3239
|
* @param backlog The length of the accept queue.
|
|
3226
3240
|
* @param flags The sum of the flags: `3` is both, `5` and `7` steer, `8`
|
|
3227
|
-
* accepts into rings, and `0` is `tcpListen` exactly.
|
|
3241
|
+
* accepts into rings, `24` into wide ones, and `0` is `tcpListen` exactly.
|
|
3228
3242
|
* @returns The listener's descriptor or the failure: `-22` for any other
|
|
3229
|
-
* bit, a negative `flags`,
|
|
3243
|
+
* bit, a negative `flags`, flag 4 without 1, or flag 16 without 8, before
|
|
3244
|
+
* a socket is made;
|
|
3230
3245
|
* `-95` for flag 4 off Linux; `-23` once the process has opened 31 ring
|
|
3231
3246
|
* listeners, closed or not.
|
|
3232
3247
|
*/
|
|
@@ -3683,6 +3698,8 @@ declare module "nish:net" {
|
|
|
3683
3698
|
export const NET_UNIX_SEQPACKET: i32;
|
|
3684
3699
|
/** `tcpListenFlags` flag 8: accept into io_uring slots instead of descriptors. */
|
|
3685
3700
|
export const NET_LISTEN_RING: i32;
|
|
3701
|
+
/** `tcpListenFlags` flag 16, with flag 8: rings of up to 65,536 slots rather than 16,384. */
|
|
3702
|
+
export const NET_LISTEN_RING_WIDE: i32;
|
|
3686
3703
|
/**
|
|
3687
3704
|
* Whether `NET_LISTEN_RING` listeners accept into rings.
|
|
3688
3705
|
*
|
package/runtime/nish.mjs
CHANGED
|
@@ -185,6 +185,7 @@ provide("NET_UNIX_UNLINK_STALE", 1);
|
|
|
185
185
|
provide("NET_UNIX_STREAM", 1);
|
|
186
186
|
provide("NET_UNIX_SEQPACKET", 2);
|
|
187
187
|
provide("NET_LISTEN_RING", 8);
|
|
188
|
+
provide("NET_LISTEN_RING_WIDE", 16);
|
|
188
189
|
|
|
189
190
|
// `nish:term`. Whether a descriptor is a terminal and how big stdout's is have
|
|
190
191
|
// Node's answers; reading stdin, waiting for it and raw mode throw, each naming
|
package/runtime/runtime-net.c
CHANGED
|
@@ -243,15 +243,27 @@ int32_t nish_net_address(nish_array *out, const nish_str *host, int32_t port) {
|
|
|
243
243
|
|
|
244
244
|
A handle is still an `i32`. Below 2^20 it is a descriptor, which is why
|
|
245
245
|
`nish_net_raise_nofile` stops the soft limit at 2^20 on Linux. At or above
|
|
246
|
-
it, `2^20 + ring * 2^
|
|
247
|
-
|
|
248
|
-
with `G = 2^
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
246
|
+
it, `2^20 + set * 2^26 + ring * 2^B + field` names a slot: each ring
|
|
247
|
+
listener's set has 2^26 handles, split into 2^(26 - B) rings of 2^B, and
|
|
248
|
+
`field` is `slot + slots * (gen % G)`, with `G = 2^B / slots` the
|
|
249
|
+
generations a slot's handles cycle through. B is the least that is at
|
|
250
|
+
least 20 and leaves G at 64 or more: 20 for a ring of up to 16,384 slots,
|
|
251
|
+
so 64 rings (`NR_RINGS`) a set, and 22 for one of 65,536, so 16. A set
|
|
252
|
+
holds about 2^20 connections either way. A close moves the slot's
|
|
253
|
+
generation on, so the handle a program closed answers -9 at once and does
|
|
254
|
+
not reach the next connection in that slot until the slot has been reused
|
|
255
|
+
G times (1,024 at a limit of 1,024; never fewer than 64; NURING-54). At
|
|
256
|
+
most `NR_SETS` ring listeners exist in a process, which keeps every handle
|
|
257
|
+
below 2^31; their ids are not reused, so a closed listener's handles
|
|
258
|
+
answer -9 for good.
|
|
259
|
+
|
|
260
|
+
A ring holds RLIMIT_NOFILE slots, the most io_uring lets one ring's table
|
|
261
|
+
have, up to 16,384 (`NR_SLOTS_MAX`), or up to 65,536 (`NR_SLOTS_WIDE`) for
|
|
262
|
+
a listener opened with `NET_LISTEN_RING_WIDE`. Each ring is a pass's
|
|
263
|
+
io_uring_enter and its eventfd, so fewer, wider rings cost a busy loop
|
|
264
|
+
less (measured: 3.5 points of one CPU at 250,000 connections); each slot
|
|
265
|
+
costs about 110 bytes a ring up front, so a ring of 65,536 is about 7 MB
|
|
266
|
+
where one of 16,384 is about 1.8 MB, which is why it is asked for.
|
|
255
267
|
|
|
256
268
|
`netShutdown` queues an io_uring SHUTDOWN behind the slot's sends, so the
|
|
257
269
|
peer reads every byte written before it and then the FIN (NURING-39);
|
|
@@ -325,8 +337,11 @@ void nish_uring_stats(const struct nish_uring_set *s, struct nish_uring_stats *s
|
|
|
325
337
|
/* A slot's first receive queue, doubled as it fills: small enough for
|
|
326
338
|
glibc's thread cache, and freed once drained (NURING-38). */
|
|
327
339
|
#define NR_RECV_FIRST 1024
|
|
328
|
-
/* The most slots per ring: a table is the kernel's memory, sized up front
|
|
340
|
+
/* The most slots per ring: a table is the kernel's memory, sized up front,
|
|
341
|
+
and so is the set's record of each slot. NET_LISTEN_RING_WIDE asks for
|
|
342
|
+
the larger. */
|
|
329
343
|
#define NR_SLOTS_MAX 16384
|
|
344
|
+
#define NR_SLOTS_WIDE 65536
|
|
330
345
|
#define NR_EVENTS 256
|
|
331
346
|
|
|
332
347
|
/* A loop's record of one virtual source, a slot or a ring listener: the loop
|
|
@@ -356,6 +371,7 @@ struct nr_set {
|
|
|
356
371
|
struct nish_uring_set *u;
|
|
357
372
|
struct nr_vent v; /* the listener's own entry */
|
|
358
373
|
int32_t listener, port, slots, gens, dead, accept_err;
|
|
374
|
+
int32_t rbits; /* B above: log2 of each ring's share of the set's 2^26 handles */
|
|
359
375
|
int32_t accept_once; /* an accept that found memory short, answered by the next tcpAccept */
|
|
360
376
|
struct nr_conn *conns[NR_RINGS];
|
|
361
377
|
uint32_t *fifo; /* accepted (ring << 20 | slot), not yet taken */
|
|
@@ -467,11 +483,12 @@ static int nr_push(uint32_t **a, uint32_t *n, uint32_t *cap, uint32_t x) {
|
|
|
467
483
|
static struct nr_conn *nr_decode(int32_t h, struct nr_set **set, int32_t *ring, int32_t *slot) {
|
|
468
484
|
if (h < NR_BASE) return NULL;
|
|
469
485
|
uint32_t x = (uint32_t)h - NR_BASE;
|
|
470
|
-
uint32_t
|
|
471
|
-
uint32_t si = g / NR_RINGS, ri = g % NR_RINGS;
|
|
486
|
+
uint32_t si = x >> 26;
|
|
472
487
|
if (si >= (uint32_t)nr_nsets) return NULL;
|
|
473
488
|
struct nr_set *S = nr_sets[si];
|
|
474
|
-
|
|
489
|
+
uint32_t y = x & ((1u << 26) - 1);
|
|
490
|
+
uint32_t ri = y >> S->rbits, field = y & ((1u << S->rbits) - 1);
|
|
491
|
+
if (ri >= NR_RINGS || S->dead || !S->conns[ri]) return NULL;
|
|
475
492
|
uint32_t sl = field % (uint32_t)S->slots, gm = field / (uint32_t)S->slots;
|
|
476
493
|
struct nr_conn *c = &S->conns[ri][sl];
|
|
477
494
|
if (gm >= (uint32_t)S->gens || !c->open || c->lingering || c->gen % (uint32_t)S->gens != gm) return NULL;
|
|
@@ -484,7 +501,7 @@ static struct nr_conn *nr_decode(int32_t h, struct nr_set **set, int32_t *ring,
|
|
|
484
501
|
static int32_t nr_encode(const struct nr_set *S, int32_t si, uint32_t ring, uint32_t slot) {
|
|
485
502
|
const struct nr_conn *c = &S->conns[ring][slot];
|
|
486
503
|
uint32_t field = slot + (uint32_t)S->slots * (c->gen % (uint32_t)S->gens);
|
|
487
|
-
return (int32_t)((uint32_t)NR_BASE + ((uint32_t)si
|
|
504
|
+
return (int32_t)((uint32_t)NR_BASE + ((uint32_t)si << 26) + (ring << S->rbits) + field);
|
|
488
505
|
}
|
|
489
506
|
|
|
490
507
|
/* Ends a slot for good: the set closes it and the slot goes back to the
|
|
@@ -701,14 +718,15 @@ static int nr_refused(int32_t r) { return r == -1 || r == -38 || r == -22 || r =
|
|
|
701
718
|
to descriptors (§5.4); a refusal before any set has opened also decides for
|
|
702
719
|
every later listen, and any other failure, such as -24 or -12, only for
|
|
703
720
|
this one (NURING-20, NURING-22). */
|
|
704
|
-
static int32_t nr_slots(
|
|
721
|
+
static int32_t nr_slots(int wide) {
|
|
705
722
|
struct rlimit rl;
|
|
706
723
|
if (getrlimit(RLIMIT_NOFILE, &rl) != 0) return -1;
|
|
707
|
-
int32_t
|
|
724
|
+
int32_t cap = wide ? NR_SLOTS_WIDE : NR_SLOTS_MAX;
|
|
725
|
+
int32_t n = rl.rlim_cur == RLIM_INFINITY || rl.rlim_cur > (rlim_t)cap ? cap : (int32_t)rl.rlim_cur;
|
|
708
726
|
return n < 1 ? 1 : n;
|
|
709
727
|
}
|
|
710
728
|
|
|
711
|
-
static int32_t nr_open(int32_t fd, int32_t port) {
|
|
729
|
+
static int32_t nr_open(int32_t fd, int32_t port, int wide) {
|
|
712
730
|
if (nr_state < 0) return 0;
|
|
713
731
|
/* Under `--sandbox` every set takes the rings made before the filter, so
|
|
714
732
|
any failure of theirs, a refusal or not, decides for good: no ring can be
|
|
@@ -724,9 +742,12 @@ static int32_t nr_open(int32_t fd, int32_t port) {
|
|
|
724
742
|
if (nr_nsets >= NR_SETS) return -23;
|
|
725
743
|
struct nish_uring_opts o;
|
|
726
744
|
memset(&o, 0, sizeof o);
|
|
727
|
-
o.slots = pooled > 0 ? pooled : nr_slots();
|
|
745
|
+
o.slots = pooled > 0 ? pooled : nr_slots(wide);
|
|
728
746
|
if (o.slots < 0) return 0;
|
|
729
|
-
|
|
747
|
+
/* B above: each ring's handles hold 64 generations of every slot. */
|
|
748
|
+
int32_t rbits = 20;
|
|
749
|
+
while (((int64_t)1 << rbits) < (int64_t)o.slots * 64) rbits++;
|
|
750
|
+
o.rings_max = 1 << (26 - rbits);
|
|
730
751
|
struct nr_set *S = calloc(1, sizeof *S);
|
|
731
752
|
if (!S) return 0;
|
|
732
753
|
int32_t r = nish_uring_open(&S->u, fd, &o);
|
|
@@ -742,7 +763,8 @@ static int32_t nr_open(int32_t fd, int32_t port) {
|
|
|
742
763
|
S->listener = fd;
|
|
743
764
|
S->port = port;
|
|
744
765
|
S->slots = o.slots;
|
|
745
|
-
S->
|
|
766
|
+
S->rbits = rbits;
|
|
767
|
+
S->gens = (1 << rbits) / o.slots;
|
|
746
768
|
S->v.loop = -1;
|
|
747
769
|
S->v.is_listener = 1;
|
|
748
770
|
nr_sets[nr_nsets++] = S;
|
|
@@ -1211,11 +1233,13 @@ static int nr_batch(int32_t loop, int32_t *out, int n, int max, int32_t min, int
|
|
|
1211
1233
|
has no io_uring, and answers -95. */
|
|
1212
1234
|
/* WP63 S3: `--sandbox`, before its filter, makes the `n` restricted rings
|
|
1213
1235
|
every ring listener of this process takes, at the slots `nr_open` would
|
|
1214
|
-
give a set
|
|
1215
|
-
|
|
1236
|
+
give a set without `NET_LISTEN_RING_WIDE` (they are made before the
|
|
1237
|
+
program could ask for wider ones, so a sandboxed listener's rings are
|
|
1238
|
+
never wide), and writes each ring's descriptor to `fds` (64 of them).
|
|
1239
|
+
With `n` of 0 it records that no ring may be made. */
|
|
1216
1240
|
int32_t nish_net_ring_premake(int32_t n, int32_t *fds) {
|
|
1217
1241
|
#if defined(__linux__)
|
|
1218
|
-
int32_t slots = nr_slots();
|
|
1242
|
+
int32_t slots = nr_slots(0);
|
|
1219
1243
|
return nish_uring_premake(n, slots < 0 ? 1 : slots, fds);
|
|
1220
1244
|
#else
|
|
1221
1245
|
(void)n;
|
|
@@ -1354,17 +1378,19 @@ int32_t nish_tcp_listen(const nish_str *host, int32_t port, int32_t backlog) {
|
|
|
1354
1378
|
with io_uring, `tcpAccept` hands out slot handles rather than descriptors,
|
|
1355
1379
|
so the listener holds connections past RLIMIT_NOFILE (above). Where rings
|
|
1356
1380
|
are refused the listener is a plain one and `netRingState()` says why;
|
|
1357
|
-
Darwin has none, and the flag does nothing there.
|
|
1358
|
-
|
|
1381
|
+
Darwin has none, and the flag does nothing there. Flag 16, which needs
|
|
1382
|
+
flag 8, makes the listener's rings as wide as RLIMIT_NOFILE allows up to
|
|
1383
|
+
65,536 slots rather than 16,384 (above). Any other bit, 4 without 1, or
|
|
1384
|
+
16 without 8, is -22. */
|
|
1359
1385
|
int32_t nish_tcp_listen_flags(const nish_str *host, int32_t port, int32_t backlog, int32_t flags) {
|
|
1360
|
-
if ((uint32_t)flags >
|
|
1386
|
+
if ((uint32_t)flags > 31 || (flags & 5) == 4 || (flags & 24) == 16) return -22;
|
|
1361
1387
|
#if !defined(__linux__)
|
|
1362
1388
|
if (flags & 4) return -95;
|
|
1363
1389
|
#endif
|
|
1364
1390
|
int32_t fd = nish_net_bound(host, port, SOCK_STREAM, backlog, flags & 7);
|
|
1365
1391
|
#if defined(__linux__)
|
|
1366
1392
|
if (fd >= 0 && (flags & 8)) {
|
|
1367
|
-
int32_t r = nr_open(fd, nish_net_local_port(fd));
|
|
1393
|
+
int32_t r = nr_open(fd, nish_net_local_port(fd), (flags & 16) != 0);
|
|
1368
1394
|
if (r < 0) {
|
|
1369
1395
|
close(fd);
|
|
1370
1396
|
return r;
|
package/runtime/runtime-uring.c
CHANGED
|
@@ -440,6 +440,9 @@ struct nish_uring_set {
|
|
|
440
440
|
* events it answered and not yet taken, and whether one failed, after
|
|
441
441
|
* which no descriptor is watched again. */
|
|
442
442
|
int poll_fd, poll_armed, poll_broken;
|
|
443
|
+
/* Events the set's waits have handed out, and that count when a set of
|
|
444
|
+
* several rings last slept in its first ring (`nish_uring_wait_direct`). */
|
|
445
|
+
uint64_t events_seen, events_at_direct;
|
|
443
446
|
int32_t poll_revents;
|
|
444
447
|
struct nish_uring_stats stats;
|
|
445
448
|
};
|
|
@@ -1460,6 +1463,10 @@ static int ur_flagged(const struct ur_ring *r) {
|
|
|
1460
1463
|
* wakes a second. */
|
|
1461
1464
|
#define UR_SLEEP_MAX_MS 10
|
|
1462
1465
|
|
|
1466
|
+
/* The longest a busy set of several rings sleeps in its first ring, whose
|
|
1467
|
+
* enter the other rings' completions do not end (`nish_uring_wait_direct`). */
|
|
1468
|
+
#define UR_DIRECT_BUSY_MS 1
|
|
1469
|
+
|
|
1463
1470
|
/* How long a caller may sleep on `nish_uring_fd` before it calls wait, given
|
|
1464
1471
|
* the longest it wants (`timeout_ms`, -1 for ever). Answers 0 while a wait has
|
|
1465
1472
|
* work it can do now: a ring the kernel flagged, completions not yet reaped,
|
|
@@ -1543,6 +1550,7 @@ again:
|
|
|
1543
1550
|
int n = 0;
|
|
1544
1551
|
for (int ri = 0; ri < s->nrings && n < cap; ri++) n += ur_service(s, ri, ev + n, cap - n);
|
|
1545
1552
|
ur_submit_queued(s);
|
|
1553
|
+
s->events_seen += (uint64_t)n;
|
|
1546
1554
|
if (n > 0) return n;
|
|
1547
1555
|
/* No eventfd is trusted to end the sleep (NURING-8): the bound does, and the
|
|
1548
1556
|
* flagged rings are serviced below whether an eventfd was read or not. */
|
|
@@ -1561,6 +1569,7 @@ again:
|
|
|
1561
1569
|
for (int ri = 0; ri < s->nrings && n < cap; ri++)
|
|
1562
1570
|
if (ur_flagged(s->rings[ri])) n += ur_service(s, ri, ev + n, cap - n);
|
|
1563
1571
|
ur_submit_queued(s);
|
|
1572
|
+
s->events_seen += (uint64_t)n;
|
|
1564
1573
|
if (n == 0 && timeout_ms < 0) goto again;
|
|
1565
1574
|
return n;
|
|
1566
1575
|
}
|
|
@@ -1577,15 +1586,31 @@ again:
|
|
|
1577
1586
|
* with `nish_uring_poll_events` (NURING-45), so a caller that also waits on one
|
|
1578
1587
|
* descriptor, `signalFd()`, needs no epoll set beside the ring.
|
|
1579
1588
|
*
|
|
1589
|
+
* A set of several rings sleeps in its first ring too, but only while it is
|
|
1590
|
+
* busy: the other rings' completions cannot end that sleep, so it lasts at
|
|
1591
|
+
* most UR_DIRECT_BUSY_MS, and their queued submissions are handed to the
|
|
1592
|
+
* kernel first. It is busy while its waits have handed out events since its
|
|
1593
|
+
* last such sleep; once a sleep is followed by none, the next call answers
|
|
1594
|
+
* -EOPNOTSUPP and the caller sleeps on `nish_uring_fd`, whose eventfds wake
|
|
1595
|
+
* it for any ring, until events come again (NURING-55). Under load that
|
|
1596
|
+
* replaces each pass's epoll_wait and per-ring eventfd reads with the one
|
|
1597
|
+
* enter, which also gathers the first ring's completions for up to the
|
|
1598
|
+
* bound; idle, the set sleeps as long as before.
|
|
1599
|
+
*
|
|
1580
1600
|
* Answers 0 after a sleep or when there is work to do now, and the caller
|
|
1581
|
-
* then takes events with `nish_uring_wait(s, 0, ...)`; -EOPNOTSUPP when
|
|
1582
|
-
*
|
|
1583
|
-
* 5.11), or `watch_fd` cannot be
|
|
1584
|
-
*
|
|
1585
|
-
*
|
|
1586
|
-
* signal ends the sleep with
|
|
1601
|
+
* then takes events with `nish_uring_wait(s, 0, ...)`; -EOPNOTSUPP when a set
|
|
1602
|
+
* of several rings has had no events since its last direct sleep, the
|
|
1603
|
+
* kernel lacks IORING_FEAT_EXT_ARG (before 5.11), or `watch_fd` cannot be
|
|
1604
|
+
* watched (a restricted ring, a poll that failed, or one still armed on
|
|
1605
|
+
* another descriptor), and the caller sleeps on `nish_uring_fd` as before;
|
|
1606
|
+
* or the enter's negative errno. A timeout or a signal ends the sleep with
|
|
1607
|
+
* 0, as epoll_wait's would. */
|
|
1587
1608
|
int32_t nish_uring_wait_direct(struct nish_uring_set *s, int32_t watch_fd, int32_t min_complete, int32_t timeout_ms) {
|
|
1588
|
-
|
|
1609
|
+
int multi = s->nrings > 1;
|
|
1610
|
+
if (multi) {
|
|
1611
|
+
if (s->events_seen == s->events_at_direct) return -EOPNOTSUPP;
|
|
1612
|
+
s->events_at_direct = s->events_seen;
|
|
1613
|
+
}
|
|
1589
1614
|
struct ur_ring *r = s->rings[0];
|
|
1590
1615
|
if (!r->ext_arg) return -EOPNOTSUPP;
|
|
1591
1616
|
if (watch_fd >= 0 && (r->restricted || s->poll_broken || (s->poll_armed && s->poll_fd != watch_fd)))
|
|
@@ -1609,8 +1634,15 @@ int32_t nish_uring_wait_direct(struct nish_uring_set *s, int32_t watch_fd, int32
|
|
|
1609
1634
|
* enter submits them and sleeps rather than answering at once for the
|
|
1610
1635
|
* caller's pump to send them. */
|
|
1611
1636
|
ur_send_marked(s, r);
|
|
1637
|
+
if (multi) {
|
|
1638
|
+
for (int ri = 1; ri < s->nrings; ri++) {
|
|
1639
|
+
ur_send_marked(s, s->rings[ri]);
|
|
1640
|
+
if (s->rings[ri]->to_submit) (void)ur_flush(s->rings[ri]);
|
|
1641
|
+
}
|
|
1642
|
+
}
|
|
1612
1643
|
int32_t t = nish_uring_sleep_ms(s, timeout_ms);
|
|
1613
1644
|
if (t == 0) return 0;
|
|
1645
|
+
if (multi && (t < 0 || t > UR_DIRECT_BUSY_MS)) t = UR_DIRECT_BUSY_MS;
|
|
1614
1646
|
struct {
|
|
1615
1647
|
int64_t sec, nsec;
|
|
1616
1648
|
} ts = {t / 1000, (int64_t)(t % 1000) * 1000000};
|
package/runtime/sqlite/README.md
CHANGED
|
@@ -35,6 +35,23 @@ for the units and the files the cache key fingerprints (`sqliteUnits`,
|
|
|
35
35
|
`sqliteFingerprint`), so a file added here needs no list updated.
|
|
36
36
|
`tests/run.js` ("sqlite-link") checks the two hashes above against the files.
|
|
37
37
|
|
|
38
|
+
## The page cache
|
|
39
|
+
|
|
40
|
+
`nish-sqlite.c` installs SQLite's own page cache, pcache1, through
|
|
41
|
+
`SQLITE_CONFIG_PCACHE2` from a constructor, before anything can start SQLite,
|
|
42
|
+
with its fetch, rekey, truncate and destroy wrapped. pcache1 keeps the largest
|
|
43
|
+
page number it holds, `iMaxKey`, so that the truncate after a commit visits
|
|
44
|
+
only the hash slots past the database's end; once that span is as wide as the
|
|
45
|
+
hash table, the truncate visits every slot. A B-tree split that hands its new
|
|
46
|
+
pages numbers out of order swaps two of them through the lock-byte page's
|
|
47
|
+
number (`balance_nonroot`), and that rekey left `iMaxKey` there: every later
|
|
48
|
+
commit's truncate then walked the whole hash table, which grows with the
|
|
49
|
+
cache. The wrappers record the cache's largest key while a page is parked
|
|
50
|
+
above it and put `iMaxKey` back when the page moves down. Where they cannot
|
|
51
|
+
tell, they leave pcache1's value, which is too large at worst, and too large
|
|
52
|
+
costs only the walk. `tests/runtime/sqlite-pcache-test.c` (the "sqlite-link"
|
|
53
|
+
section) checks it after every commit of random index keys.
|
|
54
|
+
|
|
38
55
|
## Updating
|
|
39
56
|
|
|
40
57
|
1. Download the new `sqlite-amalgamation-<NNNNNNN>.zip` from
|