@amritk/nish-x86_64-linux 0.45.1 → 0.47.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/INSTALL.md +3 -1
- package/bin/nish +0 -0
- package/docs/AI.md +29 -18
- package/package.json +1 -1
- package/runtime/nish.d.ts +41 -0
- package/runtime/nish.h +11 -0
- package/runtime/nish.mjs +1 -0
- package/runtime/runtime-bytes.c +167 -7
- package/runtime/runtime-host.c +15 -0
- package/runtime/runtime-net.c +272 -9
- package/runtime/runtime-os.c +15 -1
- package/runtime/runtime-uring.c +394 -85
- package/runtime/sqlite/nish-sqlite.c +448 -148
- package/std/README.md +14 -14
- package/std/base64.ts +28 -30
- package/std/crypto/aes.ts +81 -45
- package/std/crypto/bcrypt-batch.ts +23 -4
- package/std/crypto/bcrypt.ts +36 -11
- package/std/crypto/chacha20poly1305.ts +93 -52
- package/std/crypto/x25519.ts +7 -4
- package/std/host/game.ts +52 -20
- package/std/host/ring.ts +115 -31
- package/std/host/text.ts +9 -3
- package/std/json.ts +83 -0
- package/std/math.ts +161 -131
- package/std/net/handler.ts +42 -7
- package/std/net/http1-server.ts +36 -11
- package/std/net/prefork.ts +1 -2
- package/std/net/static.ts +7 -2
- package/std/random.ts +34 -51
- package/std/raster.ts +360 -110
- package/std/regex.ts +46 -15
- package/std/release.ts +19 -13
- package/std/sqlite.ts +551 -16
- package/std/synth.ts +11 -2
- package/std/temporal.ts +740 -363
- package/std/term-io.ts +81 -33
- package/std/term-pixels.ts +287 -112
- package/std/term-probe.ts +7 -3
- package/std/term-rate.ts +142 -66
- package/std/term.ts +782 -406
- package/std/testing.ts +14 -7
package/runtime/runtime-uring.c
CHANGED
|
@@ -115,6 +115,11 @@ struct nish_uring_opts {
|
|
|
115
115
|
int32_t cq_entries; /* default 8192, at least sq_entries */
|
|
116
116
|
int32_t buf_count; /* provided buffers per ring, a power of two; default 1024 */
|
|
117
117
|
int32_t buf_size; /* bytes per buffer; default 2048 */
|
|
118
|
+
/* A wait re-enters a ring the kernel flags with pending task work while
|
|
119
|
+
* each entry reaps at least this many completions; default
|
|
120
|
+
* UR_TASKRUN_WORTH (16). 1 re-enters while any entry reaps anything, the
|
|
121
|
+
* fewest completions left for the next wait at the most enters. */
|
|
122
|
+
int32_t taskrun_worth;
|
|
118
123
|
};
|
|
119
124
|
|
|
120
125
|
/* Counters the tests read. `max_accepting_rings` is the most rings that ever
|
|
@@ -134,6 +139,12 @@ struct nish_uring_stats {
|
|
|
134
139
|
int64_t accept_pauses;
|
|
135
140
|
int64_t bound_violations;
|
|
136
141
|
int64_t max_accepting_rings;
|
|
142
|
+
int64_t direct_waits; /* sleeps in io_uring_enter on the ring itself, not epoll_wait */
|
|
143
|
+
int64_t sends; /* SENDs written to an SQ, each a send's first or a requeue */
|
|
144
|
+
int64_t pool_new; /* send buffers the pools ever allocated */
|
|
145
|
+
int64_t pool_held; /* pool buffers that queued bytes hold now */
|
|
146
|
+
int64_t pool_cached; /* pool buffers on the rings' free lists now */
|
|
147
|
+
int64_t enters; /* io_uring_enter calls on the set's rings, direct waits included */
|
|
137
148
|
};
|
|
138
149
|
|
|
139
150
|
struct nish_uring_set;
|
|
@@ -145,6 +156,8 @@ int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const str
|
|
|
145
156
|
int32_t nish_uring_fd(const struct nish_uring_set *s);
|
|
146
157
|
int32_t nish_uring_sleep_ms(const struct nish_uring_set *s, int32_t timeout_ms);
|
|
147
158
|
int32_t nish_uring_wait(struct nish_uring_set *s, int32_t timeout_ms, struct nish_uring_event *ev, int32_t cap);
|
|
159
|
+
int32_t nish_uring_wait_direct(struct nish_uring_set *s, int32_t watch_fd, int32_t min_complete, int32_t timeout_ms);
|
|
160
|
+
int32_t nish_uring_poll_events(struct nish_uring_set *s, int32_t take);
|
|
148
161
|
int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, const uint8_t *p, int32_t n);
|
|
149
162
|
int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot);
|
|
150
163
|
int64_t nish_uring_queued(const struct nish_uring_set *s, int32_t ring, int32_t slot);
|
|
@@ -155,6 +168,7 @@ void nish_uring_destroy(struct nish_uring_set *s);
|
|
|
155
168
|
|
|
156
169
|
#if defined(__linux__)
|
|
157
170
|
#include <fcntl.h>
|
|
171
|
+
#include <poll.h>
|
|
158
172
|
#include <sys/epoll.h>
|
|
159
173
|
#include <sys/eventfd.h>
|
|
160
174
|
#include <sys/mman.h>
|
|
@@ -187,11 +201,13 @@ void nish_uring_destroy(struct nish_uring_set *s);
|
|
|
187
201
|
#define UR_SETUP_DEFER_TASKRUN (1u << 13)
|
|
188
202
|
#define UR_FEAT_SINGLE_MMAP (1u << 0)
|
|
189
203
|
#define UR_FEAT_NODROP (1u << 1)
|
|
204
|
+
#define UR_FEAT_EXT_ARG (1u << 8)
|
|
190
205
|
#define UR_OFF_SQ_RING 0ull
|
|
191
206
|
#define UR_OFF_SQES 0x10000000ull
|
|
192
207
|
#define UR_SQ_CQ_OVERFLOW (1u << 1)
|
|
193
208
|
#define UR_SQ_TASKRUN (1u << 2)
|
|
194
209
|
#define UR_ENTER_GETEVENTS (1u << 0)
|
|
210
|
+
#define UR_ENTER_EXT_ARG (1u << 3)
|
|
195
211
|
#define UR_SQE_FIXED_FILE (1u << 0)
|
|
196
212
|
#define UR_SQE_BUFFER_SELECT (1u << 5)
|
|
197
213
|
#define UR_SQE_CQE_SKIP_SUCCESS (1u << 6)
|
|
@@ -199,6 +215,7 @@ void nish_uring_destroy(struct nish_uring_set *s);
|
|
|
199
215
|
#define UR_CQE_F_MORE (1u << 1)
|
|
200
216
|
#define UR_CQE_BUFFER_SHIFT 16
|
|
201
217
|
#define UR_OP_NOP 0
|
|
218
|
+
#define UR_OP_POLL_ADD 6
|
|
202
219
|
#define UR_OP_ACCEPT 13
|
|
203
220
|
#define UR_OP_ASYNC_CANCEL 14
|
|
204
221
|
#define UR_OP_CLOSE 19
|
|
@@ -307,7 +324,7 @@ _Static_assert(sizeof(struct ur_restriction) == 16, "io_uring_restriction is 16
|
|
|
307
324
|
/* What a completion's user_data says. A send's is its record's address, which
|
|
308
325
|
* malloc aligns to 8, with UR_SEND in the low three bits; every other op's is
|
|
309
326
|
* the op, the slot's generation and the slot, and none of those ops is 3. */
|
|
310
|
-
enum { UR_ACCEPT = 1, UR_RECV = 2, UR_SEND = 3, UR_CLOSE = 4, UR_CANCEL = 5 };
|
|
327
|
+
enum { UR_ACCEPT = 1, UR_RECV = 2, UR_SEND = 3, UR_CLOSE = 4, UR_CANCEL = 5, UR_POLL = 6 };
|
|
311
328
|
|
|
312
329
|
/* A slot's state word: open, recv armed, recv parked on -ENOBUFS, recv held
|
|
313
330
|
* by its caller (`nish_uring_hold`), hung up (its recv ended for good, so it
|
|
@@ -318,6 +335,8 @@ enum { UR_ACCEPT = 1, UR_RECV = 2, UR_SEND = 3, UR_CLOSE = 4, UR_CANCEL = 5 };
|
|
|
318
335
|
#define ST_PARKED 4u
|
|
319
336
|
#define ST_HELD 8u
|
|
320
337
|
#define ST_HUP 16u
|
|
338
|
+
#define ST_MARKED 32u /* on its ring's `mlist`; a close keeps it (NURING-40) */
|
|
339
|
+
#define ST_SHUT(how) (((uint32_t)(how) + 1) << 6) /* the sides a shutdown queued (NURING-43) */
|
|
321
340
|
#define ST_GEN(st) ((st) >> 8)
|
|
322
341
|
#define GEN_MASK 0xffffffu
|
|
323
342
|
|
|
@@ -339,11 +358,29 @@ enum { UR_ACCEPT = 1, UR_RECV = 2, UR_SEND = 3, UR_CLOSE = 4, UR_CANCEL = 5 };
|
|
|
339
358
|
struct ur_send {
|
|
340
359
|
struct ur_send *next, *prev_all, *next_all;
|
|
341
360
|
uint32_t slot, gen;
|
|
342
|
-
uint32_t off, len;
|
|
361
|
+
uint32_t off, len; /* `data` holds UR_CHUNK_DATA bytes; [off, len) are unsent */
|
|
343
362
|
int in_flight, how;
|
|
344
363
|
uint8_t data[];
|
|
345
364
|
};
|
|
346
365
|
|
|
366
|
+
/* A pool buffer: a record and the bytes it carries. Every record is one,
|
|
367
|
+
* a shutdown's included. 16 KiB holds a response head and a typical body, so
|
|
368
|
+
* a head+body write fits one buffer and leaves as one SEND, and NR_AREA's
|
|
369
|
+
* 256 KiB is 16 of them. Bytes that outgrow it go on in further buffers
|
|
370
|
+
* chained behind it on the slot's queue, each its own SEND: a 64 KiB body
|
|
371
|
+
* behind a head is five (NURING-51). A ring keeps at most UR_SENDPOOL_MAX
|
|
372
|
+
* buffers free, 512 KiB, and frees what comes back past that (NURING-41). */
|
|
373
|
+
#define UR_CHUNK 16384
|
|
374
|
+
#define UR_CHUNK_DATA ((uint32_t)(UR_CHUNK - sizeof(struct ur_send)))
|
|
375
|
+
#define UR_SENDPOOL_MAX 32
|
|
376
|
+
|
|
377
|
+
struct ur_slot {
|
|
378
|
+
uint32_t st; /* its state bits and generation */
|
|
379
|
+
uint32_t qbytes; /* bytes queued and not yet sent */
|
|
380
|
+
struct ur_send *first; /* the head of its send queue */
|
|
381
|
+
struct ur_send *last; /* the tail */
|
|
382
|
+
};
|
|
383
|
+
|
|
347
384
|
struct ur_ring {
|
|
348
385
|
int fd, efd;
|
|
349
386
|
uint32_t *sq_head, *sq_tail, *sq_mask, *sq_array, *sq_flags;
|
|
@@ -353,22 +390,36 @@ struct ur_ring {
|
|
|
353
390
|
void *ring_map;
|
|
354
391
|
size_t ring_len, sqes_len;
|
|
355
392
|
uint32_t sq_entries, sq_local_tail, to_submit;
|
|
393
|
+
int64_t enters; /* io_uring_enter calls, for the stats' `enters` */
|
|
356
394
|
struct ur_buf *br;
|
|
357
395
|
uint8_t *bufs;
|
|
358
396
|
size_t br_len, bufs_len;
|
|
359
397
|
uint16_t br_tail;
|
|
360
398
|
long live, accepting;
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
399
|
+
/* Per slot, in one record: a send and its completion read the slot's
|
|
400
|
+
* state, queue and count together, so they share one cache line rather
|
|
401
|
+
* than each taking one of four arrays (measured: three misses a request
|
|
402
|
+
* at 150,000 connections). */
|
|
403
|
+
struct ur_slot *sl;
|
|
365
404
|
uint32_t *parked; /* slots whose recv waits for buffers */
|
|
366
405
|
uint32_t nparked;
|
|
406
|
+
/* IORING_FEAT_EXT_ARG: an enter can carry its own timeout, so a sleeper
|
|
407
|
+
* can wait on the ring itself (nish_uring_wait_direct). A restricted ring
|
|
408
|
+
* may not take POLL_ADD, so it watches no descriptor for its caller. */
|
|
409
|
+
uint8_t ext_arg, restricted;
|
|
410
|
+
/* Slots whose queue gained a record since the ring last submitted: its
|
|
411
|
+
* SEND is written then, so the writes of one pass leave as one (NURING-40).
|
|
412
|
+
* ST_MARKED keeps a slot on `mlist` at most once, across a close too. */
|
|
413
|
+
uint32_t *mlist;
|
|
414
|
+
uint32_t nmarked;
|
|
415
|
+
struct ur_send *pool; /* free send buffers, linked by `next` */
|
|
416
|
+
uint32_t npool;
|
|
367
417
|
};
|
|
368
418
|
|
|
369
419
|
struct nish_uring_set {
|
|
370
420
|
int listener, ep;
|
|
371
421
|
uint32_t slots, buf_count, buf_size, sq_entries, cq_entries;
|
|
422
|
+
uint32_t taskrun_worth; /* `nish_uring_opts.taskrun_worth`, defaulted */
|
|
372
423
|
int nrings, rings_max, acceptor;
|
|
373
424
|
int accept_stopped;
|
|
374
425
|
/* Accepting waits until `resume_ms` after an accept found memory short; the
|
|
@@ -384,6 +435,12 @@ struct nish_uring_set {
|
|
|
384
435
|
uint32_t lent_ring[EVENTS_MAX];
|
|
385
436
|
uint16_t lent_bid[EVENTS_MAX];
|
|
386
437
|
int nlent;
|
|
438
|
+
/* The one-shot POLL_ADD a direct wait arms on its caller's descriptor
|
|
439
|
+
* (NURING-45): the descriptor, whether the poll is in the kernel, the
|
|
440
|
+
* events it answered and not yet taken, and whether one failed, after
|
|
441
|
+
* which no descriptor is watched again. */
|
|
442
|
+
int poll_fd, poll_armed, poll_broken;
|
|
443
|
+
int32_t poll_revents;
|
|
387
444
|
struct nish_uring_stats stats;
|
|
388
445
|
};
|
|
389
446
|
|
|
@@ -407,6 +464,7 @@ static int ur_errno(long rc) { return rc < 0 ? -errno : (int)rc; }
|
|
|
407
464
|
static int ur_setup(uint32_t entries, struct ur_params *p) { return ur_errno(UR_SYS_SETUP(entries, p)); }
|
|
408
465
|
|
|
409
466
|
static int ur_enter(struct ur_ring *r, uint32_t submit, uint32_t wait, uint32_t flags) {
|
|
467
|
+
r->enters++;
|
|
410
468
|
return ur_errno(syscall(__NR_io_uring_enter, r->fd, submit, wait, flags, NULL, 0));
|
|
411
469
|
}
|
|
412
470
|
|
|
@@ -435,6 +493,20 @@ static int ur_setup_flags(uint32_t entries, uint32_t cq, struct ur_params *p) {
|
|
|
435
493
|
return fd;
|
|
436
494
|
}
|
|
437
495
|
|
|
496
|
+
/* Registers the provided-buffer ring `reg` describes. Ubuntu's 6.8.0-136
|
|
497
|
+
* and later kernels invert the check on its reserved words (Launchpad
|
|
498
|
+
* #2162843): they refuse all zeroes with -EINVAL and accept a nonzero
|
|
499
|
+
* `resv[0]`, so a refused registration is tried once more that way. A
|
|
500
|
+
* correct kernel answers the first, and never sees the second. */
|
|
501
|
+
static int ur_register_pbuf(int fd, struct ur_buf_reg *reg) {
|
|
502
|
+
int rc = ur_register(fd, UR_REGISTER_PBUF_RING, reg, 1);
|
|
503
|
+
if (rc == -EINVAL) {
|
|
504
|
+
reg->resv[0] = 1;
|
|
505
|
+
rc = ur_register(fd, UR_REGISTER_PBUF_RING, reg, 1);
|
|
506
|
+
}
|
|
507
|
+
return rc;
|
|
508
|
+
}
|
|
509
|
+
|
|
438
510
|
/* Whether the ring `fd` has every opcode the layer submits: 1, -EOPNOTSUPP
|
|
439
511
|
* when one is missing or the kernel has no probe, or the register's errno. */
|
|
440
512
|
static int32_t ur_ops_ok(int fd) {
|
|
@@ -475,7 +547,7 @@ static int32_t ur_probe_once(void) {
|
|
|
475
547
|
reg.ring_addr = (uint64_t)(uintptr_t)br;
|
|
476
548
|
reg.ring_entries = 8;
|
|
477
549
|
reg.bgid = BGID;
|
|
478
|
-
int e =
|
|
550
|
+
int e = ur_register_pbuf(fd, ®);
|
|
479
551
|
if (e < 0) rc = e == -EINVAL ? -EOPNOTSUPP : e;
|
|
480
552
|
close(fd);
|
|
481
553
|
fd = -1;
|
|
@@ -585,11 +657,11 @@ static void ur_recycle(struct nish_uring_set *s, struct ur_ring *r, uint32_t bid
|
|
|
585
657
|
* answer 0 once the kernel has handed over the reset, and a second hangup
|
|
586
658
|
* would turn the reset into a clean end (NURING-27). */
|
|
587
659
|
static void ur_arm(struct ur_ring *r, uint32_t slot) {
|
|
588
|
-
if (r->
|
|
660
|
+
if (r->sl[slot].st & ST_HUP) return;
|
|
589
661
|
struct ur_sqe *e = ur_sqe(r);
|
|
590
662
|
if (!e) {
|
|
591
|
-
if (!(r->
|
|
592
|
-
r->
|
|
663
|
+
if (!(r->sl[slot].st & ST_PARKED)) {
|
|
664
|
+
r->sl[slot].st |= ST_PARKED;
|
|
593
665
|
r->parked[r->nparked++] = slot;
|
|
594
666
|
}
|
|
595
667
|
return;
|
|
@@ -599,8 +671,8 @@ static void ur_arm(struct ur_ring *r, uint32_t slot) {
|
|
|
599
671
|
e->flags = UR_SQE_FIXED_FILE | UR_SQE_BUFFER_SELECT;
|
|
600
672
|
e->ioprio = UR_RECV_MULTISHOT;
|
|
601
673
|
e->buf_group = BGID;
|
|
602
|
-
e->user_data = ur_tag(UR_RECV, r->
|
|
603
|
-
r->
|
|
674
|
+
e->user_data = ur_tag(UR_RECV, r->sl[slot].st, slot);
|
|
675
|
+
r->sl[slot].st |= ST_ARMED;
|
|
604
676
|
}
|
|
605
677
|
|
|
606
678
|
static void ur_free_ring(struct ur_ring *r) {
|
|
@@ -619,11 +691,14 @@ static void ur_free_ring(struct ur_ring *r) {
|
|
|
619
691
|
if (r->sqes && (void *)r->sqes != MAP_FAILED) munmap(r->sqes, r->sqes_len);
|
|
620
692
|
if (r->br && (void *)r->br != MAP_FAILED) munmap(r->br, r->br_len);
|
|
621
693
|
if (r->bufs && (void *)r->bufs != MAP_FAILED) munmap(r->bufs, r->bufs_len);
|
|
622
|
-
free(r->
|
|
623
|
-
free(r->sq_first);
|
|
624
|
-
free(r->sq_last);
|
|
694
|
+
free(r->sl);
|
|
625
695
|
free(r->parked);
|
|
626
|
-
free(r->
|
|
696
|
+
free(r->mlist);
|
|
697
|
+
while (r->pool) {
|
|
698
|
+
struct ur_send *q = r->pool;
|
|
699
|
+
r->pool = q->next;
|
|
700
|
+
free(q);
|
|
701
|
+
}
|
|
627
702
|
free(r);
|
|
628
703
|
}
|
|
629
704
|
|
|
@@ -698,6 +773,8 @@ static int ur_make_ring(const struct ur_sizes *z, int restricted, struct ur_ring
|
|
|
698
773
|
rc = -EOPNOTSUPP;
|
|
699
774
|
goto fail;
|
|
700
775
|
}
|
|
776
|
+
r->ext_arg = (p.features & UR_FEAT_EXT_ARG) != 0;
|
|
777
|
+
r->restricted = restricted != 0;
|
|
701
778
|
if (restricted) {
|
|
702
779
|
rc = ur_ops_ok(r->fd);
|
|
703
780
|
if (rc < 0) goto fail;
|
|
@@ -754,16 +831,14 @@ static int ur_make_ring(const struct ur_sizes *z, int restricted, struct ur_ring
|
|
|
754
831
|
reg.ring_addr = (uint64_t)(uintptr_t)r->br;
|
|
755
832
|
reg.ring_entries = z->buf_count;
|
|
756
833
|
reg.bgid = BGID;
|
|
757
|
-
rc =
|
|
834
|
+
rc = ur_register_pbuf(r->fd, ®);
|
|
758
835
|
if (rc < 0) goto fail;
|
|
759
836
|
|
|
760
837
|
/* Per-slot state: generation and flags, send queues, parked recvs. */
|
|
761
|
-
r->
|
|
762
|
-
r->sq_first = calloc(z->slots, sizeof *r->sq_first);
|
|
763
|
-
r->sq_last = calloc(z->slots, sizeof *r->sq_last);
|
|
838
|
+
r->sl = calloc(z->slots, sizeof *r->sl);
|
|
764
839
|
r->parked = calloc(z->slots, sizeof *r->parked);
|
|
765
|
-
r->
|
|
766
|
-
if (!r->
|
|
840
|
+
r->mlist = calloc(z->slots, sizeof *r->mlist);
|
|
841
|
+
if (!r->sl || !r->parked || !r->mlist) {
|
|
767
842
|
rc = -ENOMEM;
|
|
768
843
|
goto fail;
|
|
769
844
|
}
|
|
@@ -972,7 +1047,7 @@ static void ur_top_up(struct nish_uring_set *s) {
|
|
|
972
1047
|
/* Submit the slot's first queued send, from where the kernel left off, or
|
|
973
1048
|
* its shutdown. */
|
|
974
1049
|
static void ur_send_head(struct ur_ring *r, uint32_t slot, struct nish_uring_set *s) {
|
|
975
|
-
struct ur_send *q = r->
|
|
1050
|
+
struct ur_send *q = r->sl[slot].first;
|
|
976
1051
|
if (!q || q->in_flight) return;
|
|
977
1052
|
struct ur_sqe *e = ur_sqe(r);
|
|
978
1053
|
if (!e) {
|
|
@@ -986,6 +1061,7 @@ static void ur_send_head(struct ur_ring *r, uint32_t slot, struct nish_uring_set
|
|
|
986
1061
|
e->len = (uint32_t)q->how;
|
|
987
1062
|
} else {
|
|
988
1063
|
e->opcode = UR_OP_SEND;
|
|
1064
|
+
s->stats.sends++;
|
|
989
1065
|
e->addr = (uint64_t)(uintptr_t)(q->data + q->off);
|
|
990
1066
|
e->len = q->len - q->off;
|
|
991
1067
|
e->op_flags = MSG_NOSIGNAL;
|
|
@@ -994,24 +1070,78 @@ static void ur_send_head(struct ur_ring *r, uint32_t slot, struct nish_uring_set
|
|
|
994
1070
|
q->in_flight = 1;
|
|
995
1071
|
}
|
|
996
1072
|
|
|
997
|
-
|
|
1073
|
+
/* Note that (r, slot) has a record to submit; the ring writes its SEND when
|
|
1074
|
+
* it next submits. */
|
|
1075
|
+
static void ur_mark(struct ur_ring *r, uint32_t slot) {
|
|
1076
|
+
if (r->sl[slot].st & ST_MARKED) return;
|
|
1077
|
+
r->sl[slot].st |= ST_MARKED;
|
|
1078
|
+
r->mlist[r->nmarked++] = slot;
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1081
|
+
/* Write the SEND or SHUTDOWN of every marked slot whose head is not in the
|
|
1082
|
+
* kernel, from the head's whole unsent length. A slot closed since it was
|
|
1083
|
+
* marked has no queue, and is skipped. */
|
|
1084
|
+
static void ur_send_marked(struct nish_uring_set *s, struct ur_ring *r) {
|
|
1085
|
+
uint32_t n = r->nmarked;
|
|
1086
|
+
r->nmarked = 0;
|
|
1087
|
+
for (uint32_t i = 0; i < n; i++) {
|
|
1088
|
+
uint32_t slot = r->mlist[i];
|
|
1089
|
+
r->sl[slot].st &= ~ST_MARKED;
|
|
1090
|
+
ur_send_head(r, slot, s);
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
/* A pool buffer, taken from the ring's free list before a new one is
|
|
1095
|
+
* allocated. NULL when memory is short. */
|
|
1096
|
+
static struct ur_send *ur_record(struct nish_uring_set *s, struct ur_ring *r) {
|
|
1097
|
+
struct ur_send *q;
|
|
1098
|
+
if (r->pool) {
|
|
1099
|
+
q = r->pool;
|
|
1100
|
+
r->pool = q->next;
|
|
1101
|
+
r->npool--;
|
|
1102
|
+
s->stats.pool_cached--;
|
|
1103
|
+
} else {
|
|
1104
|
+
q = malloc(UR_CHUNK);
|
|
1105
|
+
if (!q) return NULL;
|
|
1106
|
+
s->stats.pool_new++;
|
|
1107
|
+
}
|
|
1108
|
+
s->stats.pool_held++;
|
|
1109
|
+
return q;
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
/* Give a record's memory back: to its ring's free list while that holds
|
|
1113
|
+
* fewer than UR_SENDPOOL_MAX, else to the allocator. Only a record the kernel
|
|
1114
|
+
* no longer holds comes here (NURING-42). */
|
|
1115
|
+
static void ur_release(struct nish_uring_set *s, struct ur_ring *r, struct ur_send *q) {
|
|
1116
|
+
s->stats.pool_held--;
|
|
1117
|
+
if (r->npool < UR_SENDPOOL_MAX) {
|
|
1118
|
+
q->next = r->pool;
|
|
1119
|
+
r->pool = q;
|
|
1120
|
+
r->npool++;
|
|
1121
|
+
s->stats.pool_cached++;
|
|
1122
|
+
return;
|
|
1123
|
+
}
|
|
1124
|
+
free(q);
|
|
1125
|
+
}
|
|
1126
|
+
|
|
1127
|
+
static void ur_unlink_all(struct nish_uring_set *s, struct ur_ring *r, struct ur_send *q) {
|
|
998
1128
|
if (q->prev_all) q->prev_all->next_all = q->next_all;
|
|
999
1129
|
else s->all = q->next_all;
|
|
1000
1130
|
if (q->next_all) q->next_all->prev_all = q->prev_all;
|
|
1001
|
-
|
|
1131
|
+
ur_release(s, r, q);
|
|
1002
1132
|
}
|
|
1003
1133
|
|
|
1004
1134
|
/* Drop a slot's queued sends. The one the kernel holds, if any, is left on
|
|
1005
1135
|
* the all-records list; its completion finds the generation moved on and
|
|
1006
1136
|
* frees it. */
|
|
1007
1137
|
static void ur_drop_sends(struct nish_uring_set *s, struct ur_ring *r, uint32_t slot) {
|
|
1008
|
-
struct ur_send *q = r->
|
|
1009
|
-
r->
|
|
1010
|
-
r->
|
|
1138
|
+
struct ur_send *q = r->sl[slot].first;
|
|
1139
|
+
r->sl[slot].first = r->sl[slot].last = NULL;
|
|
1140
|
+
r->sl[slot].qbytes = 0;
|
|
1011
1141
|
while (q) {
|
|
1012
1142
|
struct ur_send *next = q->next;
|
|
1013
1143
|
q->next = NULL;
|
|
1014
|
-
if (!q->in_flight) ur_unlink_all(s, q);
|
|
1144
|
+
if (!q->in_flight) ur_unlink_all(s, r, q);
|
|
1015
1145
|
q = next;
|
|
1016
1146
|
}
|
|
1017
1147
|
}
|
|
@@ -1043,8 +1173,8 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
|
|
|
1043
1173
|
struct ur_send *q = (struct ur_send *)(uintptr_t)(ud & ~(uint64_t)7);
|
|
1044
1174
|
q->in_flight = 0;
|
|
1045
1175
|
uint32_t slot = q->slot;
|
|
1046
|
-
if ((r->
|
|
1047
|
-
ur_unlink_all(s, q);
|
|
1176
|
+
if ((r->sl[slot].st & ST_OPEN) == 0 || ST_GEN(r->sl[slot].st) != q->gen || r->sl[slot].first != q) {
|
|
1177
|
+
ur_unlink_all(s, r, q);
|
|
1048
1178
|
return 0;
|
|
1049
1179
|
}
|
|
1050
1180
|
if (q->how >= 0) {
|
|
@@ -1052,7 +1182,7 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
|
|
|
1052
1182
|
* already gone says nothing a recv will not, so it is not reported. */
|
|
1053
1183
|
res = 0;
|
|
1054
1184
|
}
|
|
1055
|
-
if (res > 0) r->
|
|
1185
|
+
if (res > 0) r->sl[slot].qbytes -= (uint32_t)res < r->sl[slot].qbytes ? (uint32_t)res : r->sl[slot].qbytes;
|
|
1056
1186
|
if (res > 0 && q->off + (uint32_t)res < q->len) {
|
|
1057
1187
|
q->off += (uint32_t)res;
|
|
1058
1188
|
s->stats.send_requeues++;
|
|
@@ -1064,13 +1194,23 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
|
|
|
1064
1194
|
ur_event(ev, NISH_URING_SEND_FAILED, ri, slot, res, NULL);
|
|
1065
1195
|
return 1;
|
|
1066
1196
|
} else {
|
|
1067
|
-
r->
|
|
1068
|
-
if (!q->next) r->
|
|
1069
|
-
ur_unlink_all(s, q);
|
|
1197
|
+
r->sl[slot].first = q->next;
|
|
1198
|
+
if (!q->next) r->sl[slot].last = NULL;
|
|
1199
|
+
ur_unlink_all(s, r, q);
|
|
1070
1200
|
}
|
|
1071
1201
|
ur_send_head(r, slot, s);
|
|
1072
1202
|
return 0;
|
|
1073
1203
|
}
|
|
1204
|
+
if (ud == UR_POLL) {
|
|
1205
|
+
/* The direct wait's poll on its caller's descriptor ended: its events
|
|
1206
|
+
* wait for nish_uring_poll_events. A failure other than a cancel (the
|
|
1207
|
+
* descriptor closed, a kernel that refuses the opcode) stops the watch
|
|
1208
|
+
* for good, so a poll that cannot work is not re-armed every sleep. */
|
|
1209
|
+
s->poll_armed = 0;
|
|
1210
|
+
if (res > 0) s->poll_revents |= res;
|
|
1211
|
+
else if (res < 0 && res != -ECANCELED) s->poll_broken = 1;
|
|
1212
|
+
return 0;
|
|
1213
|
+
}
|
|
1074
1214
|
unsigned op = (unsigned)(ud & 0xff);
|
|
1075
1215
|
uint32_t gen = (uint32_t)(ud >> 8) & GEN_MASK;
|
|
1076
1216
|
uint32_t slot = (uint32_t)(ud >> 32);
|
|
@@ -1081,7 +1221,7 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
|
|
|
1081
1221
|
if (res >= 0 && (uint32_t)res < s->slots) {
|
|
1082
1222
|
uint32_t at = (uint32_t)res;
|
|
1083
1223
|
s->backoff_ms = 0;
|
|
1084
|
-
r->
|
|
1224
|
+
r->sl[at].st = (r->sl[at].st & ~(ST_ARMED | ST_PARKED | ST_HELD | ST_HUP)) | ST_OPEN;
|
|
1085
1225
|
r->live++;
|
|
1086
1226
|
s->stats.live++;
|
|
1087
1227
|
ur_arm(r, at);
|
|
@@ -1115,7 +1255,7 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
|
|
|
1115
1255
|
return wrote;
|
|
1116
1256
|
}
|
|
1117
1257
|
if (op != UR_RECV) return 0; /* a close or cancel; nothing to report */
|
|
1118
|
-
int stale = slot >= s->slots || (r->
|
|
1258
|
+
int stale = slot >= s->slots || (r->sl[slot].st & ST_OPEN) == 0 || (ST_GEN(r->sl[slot].st) & GEN_MASK) != gen;
|
|
1119
1259
|
int more = (c->flags & UR_CQE_F_MORE) != 0;
|
|
1120
1260
|
uint32_t bid = c->flags >> UR_CQE_BUFFER_SHIFT;
|
|
1121
1261
|
int has_buf = (c->flags & UR_CQE_F_BUFFER) != 0 && bid < s->buf_count;
|
|
@@ -1123,12 +1263,12 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
|
|
|
1123
1263
|
if (has_buf) ur_recycle(s, r, bid);
|
|
1124
1264
|
return 0;
|
|
1125
1265
|
}
|
|
1126
|
-
if (!more) r->
|
|
1266
|
+
if (!more) r->sl[slot].st &= ~ST_ARMED;
|
|
1127
1267
|
if (res > 0 && has_buf) {
|
|
1128
1268
|
s->lent_ring[s->nlent] = (uint32_t)ri;
|
|
1129
1269
|
s->lent_bid[s->nlent] = (uint16_t)bid;
|
|
1130
1270
|
s->nlent++;
|
|
1131
|
-
if (!more && !(r->
|
|
1271
|
+
if (!more && !(r->sl[slot].st & ST_HELD)) ur_arm(r, slot);
|
|
1132
1272
|
ur_event(ev, NISH_URING_DATA, ri, slot, res, r->bufs + (size_t)bid * s->buf_size);
|
|
1133
1273
|
return 1;
|
|
1134
1274
|
}
|
|
@@ -1137,20 +1277,20 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
|
|
|
1137
1277
|
* first). The recv stays down while the slot is held, and one let go before
|
|
1138
1278
|
* the cancel landed is armed again; a held recv out of buffers stays down. */
|
|
1139
1279
|
if (!more && res == -ECANCELED) {
|
|
1140
|
-
if (!(r->
|
|
1280
|
+
if (!(r->sl[slot].st & ST_HELD)) ur_arm(r, slot);
|
|
1141
1281
|
return 0;
|
|
1142
1282
|
}
|
|
1143
|
-
if (!more && res == -ENOBUFS && (r->
|
|
1283
|
+
if (!more && res == -ENOBUFS && (r->sl[slot].st & ST_HELD)) return 0;
|
|
1144
1284
|
if (res == -ENOBUFS) {
|
|
1145
1285
|
s->stats.nobufs_rearms++;
|
|
1146
|
-
if (!more && !(r->
|
|
1147
|
-
r->
|
|
1286
|
+
if (!more && !(r->sl[slot].st & ST_PARKED)) {
|
|
1287
|
+
r->sl[slot].st |= ST_PARKED;
|
|
1148
1288
|
r->parked[r->nparked++] = slot;
|
|
1149
1289
|
}
|
|
1150
1290
|
return 0;
|
|
1151
1291
|
}
|
|
1152
1292
|
if (more) return 0;
|
|
1153
|
-
r->
|
|
1293
|
+
r->sl[slot].st |= ST_HUP;
|
|
1154
1294
|
ur_event(ev, NISH_URING_HUP, ri, slot, res, NULL);
|
|
1155
1295
|
return 1;
|
|
1156
1296
|
}
|
|
@@ -1174,6 +1314,19 @@ static int ur_reap(struct nish_uring_set *s, int ri, struct nish_uring_event *ev
|
|
|
1174
1314
|
return n;
|
|
1175
1315
|
}
|
|
1176
1316
|
|
|
1317
|
+
/* Task-work entries a service makes after the first while they keep paying:
|
|
1318
|
+
* one after which fewer than the set's `taskrun_worth` completions were
|
|
1319
|
+
* reaped ends them (UR_TASKRUN_WORTH unless `nish_uring_opts` says). It
|
|
1320
|
+
* counts completions, not the events they hand the caller: a send's
|
|
1321
|
+
* completion is no event, and counting events stopped a backlog of them
|
|
1322
|
+
* from draining, which held every send's buffer. Under steady load new task
|
|
1323
|
+
* work is always arriving, so entering until the flag is down chased it one
|
|
1324
|
+
* completion at a time (measured: 7.8 entries a request at 48k requests a
|
|
1325
|
+
* second); what is left waits for the next wait, which services a flagged
|
|
1326
|
+
* ring without sleeping (`nish_uring_sleep_ms`). A backlog keeps every entry
|
|
1327
|
+
* full, so it is drained to the end, never left to grow. */
|
|
1328
|
+
#define UR_TASKRUN_WORTH 16
|
|
1329
|
+
|
|
1177
1330
|
/* Reap, submit, and enter again while the kernel says it holds more for this
|
|
1178
1331
|
* ring (behaviour 4): IORING_SQ_TASKRUN for completions DEFER_TASKRUN has not
|
|
1179
1332
|
* run, IORING_SQ_CQ_OVERFLOW for ones the CQ had no room for. A flush that
|
|
@@ -1184,13 +1337,18 @@ static int ur_reap(struct nish_uring_set *s, int ri, struct nish_uring_event *ev
|
|
|
1184
1337
|
* starts again from here. */
|
|
1185
1338
|
static int ur_service(struct nish_uring_set *s, int ri, struct nish_uring_event *ev, int cap) {
|
|
1186
1339
|
struct ur_ring *r = s->rings[ri];
|
|
1187
|
-
int n = 0;
|
|
1340
|
+
int n = 0, entered = 0;
|
|
1341
|
+
uint32_t mark = 0;
|
|
1342
|
+
ur_send_marked(s, r);
|
|
1188
1343
|
for (int guard = 0; guard < 1 << 16; guard++) {
|
|
1189
1344
|
n += ur_reap(s, ri, ev + n, cap - n);
|
|
1190
1345
|
int rc = ur_flush(r);
|
|
1191
1346
|
if (n >= cap) break;
|
|
1347
|
+
if (entered && *r->cq_head - mark < s->taskrun_worth) break;
|
|
1192
1348
|
uint32_t flags = __atomic_load_n(r->sq_flags, __ATOMIC_ACQUIRE);
|
|
1193
1349
|
if (flags & (UR_SQ_TASKRUN | UR_SQ_CQ_OVERFLOW)) {
|
|
1350
|
+
entered++;
|
|
1351
|
+
mark = *r->cq_head;
|
|
1194
1352
|
if (flags & UR_SQ_TASKRUN) s->stats.taskrun_reentries++;
|
|
1195
1353
|
if (flags & UR_SQ_CQ_OVERFLOW) s->stats.overflow_drains++;
|
|
1196
1354
|
rc = ur_enter(r, 0, 0, UR_ENTER_GETEVENTS);
|
|
@@ -1211,7 +1369,7 @@ int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const str
|
|
|
1211
1369
|
memset(&d, 0, sizeof d);
|
|
1212
1370
|
if (o) d = *o;
|
|
1213
1371
|
if (listener < 0 || d.slots < 0 || d.rings_max < 0 || d.sq_entries < 0 || d.cq_entries < 0 || d.buf_count < 0 ||
|
|
1214
|
-
d.buf_size < 0)
|
|
1372
|
+
d.buf_size < 0 || d.taskrun_worth < 0)
|
|
1215
1373
|
return -EINVAL;
|
|
1216
1374
|
if (d.slots == 0) {
|
|
1217
1375
|
struct rlimit rl;
|
|
@@ -1225,6 +1383,7 @@ int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const str
|
|
|
1225
1383
|
if (d.cq_entries < d.sq_entries) d.cq_entries = d.sq_entries;
|
|
1226
1384
|
if (d.buf_count == 0) d.buf_count = UR_BUF_COUNT_DEFAULT;
|
|
1227
1385
|
if (d.buf_size == 0) d.buf_size = UR_BUF_SIZE_DEFAULT;
|
|
1386
|
+
if (d.taskrun_worth == 0) d.taskrun_worth = UR_TASKRUN_WORTH;
|
|
1228
1387
|
if (d.rings_max > 4096 || d.buf_count > 32768 || (d.buf_count & (d.buf_count - 1)) != 0 || d.buf_size > (1 << 20))
|
|
1229
1388
|
return -EINVAL;
|
|
1230
1389
|
/* A pooled ring was made at its own sizes, before the program could ask. */
|
|
@@ -1244,7 +1403,9 @@ int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const str
|
|
|
1244
1403
|
s->cq_entries = (uint32_t)d.cq_entries;
|
|
1245
1404
|
s->buf_count = (uint32_t)d.buf_count;
|
|
1246
1405
|
s->buf_size = (uint32_t)d.buf_size;
|
|
1406
|
+
s->taskrun_worth = (uint32_t)d.taskrun_worth;
|
|
1247
1407
|
s->rings = calloc((size_t)d.rings_max, sizeof *s->rings);
|
|
1408
|
+
s->poll_fd = -1;
|
|
1248
1409
|
s->ep = epoll_create1(EPOLL_CLOEXEC);
|
|
1249
1410
|
if (!s->rings || s->ep < 0) {
|
|
1250
1411
|
int rc = s->ep < 0 ? -errno : -ENOMEM;
|
|
@@ -1312,6 +1473,7 @@ int32_t nish_uring_sleep_ms(const struct nish_uring_set *s, int32_t timeout_ms)
|
|
|
1312
1473
|
for (int ri = 0; ri < s->nrings; ri++) {
|
|
1313
1474
|
const struct ur_ring *r = s->rings[ri];
|
|
1314
1475
|
if (ur_flagged(r) || *r->cq_head != __atomic_load_n(r->cq_tail, __ATOMIC_ACQUIRE)) return 0;
|
|
1476
|
+
if (r->nmarked) return 0;
|
|
1315
1477
|
if (r->to_submit) room = 0;
|
|
1316
1478
|
if (r->nparked) parked = 1;
|
|
1317
1479
|
}
|
|
@@ -1337,8 +1499,8 @@ static void ur_catch_up(struct nish_uring_set *s) {
|
|
|
1337
1499
|
r->nparked = 0;
|
|
1338
1500
|
for (uint32_t i = 0; i < n; i++) {
|
|
1339
1501
|
uint32_t slot = r->parked[i];
|
|
1340
|
-
r->
|
|
1341
|
-
if ((r->
|
|
1502
|
+
r->sl[slot].st &= ~ST_PARKED;
|
|
1503
|
+
if ((r->sl[slot].st & (ST_OPEN | ST_ARMED | ST_HELD)) == ST_OPEN) ur_arm(r, slot);
|
|
1342
1504
|
}
|
|
1343
1505
|
}
|
|
1344
1506
|
/* Scanning every slot of every ring costs rings * slots, but only after a
|
|
@@ -1348,7 +1510,7 @@ static void ur_catch_up(struct nish_uring_set *s) {
|
|
|
1348
1510
|
s->stalled_sends = 0;
|
|
1349
1511
|
for (int ri = 0; ri < s->nrings; ri++)
|
|
1350
1512
|
for (uint32_t slot = 0; slot < s->slots; slot++)
|
|
1351
|
-
if (s->rings[ri]->
|
|
1513
|
+
if (s->rings[ri]->sl[slot].first) ur_send_head(s->rings[ri], slot, s);
|
|
1352
1514
|
}
|
|
1353
1515
|
}
|
|
1354
1516
|
|
|
@@ -1359,8 +1521,10 @@ static void ur_catch_up(struct nish_uring_set *s) {
|
|
|
1359
1521
|
* user space while the caller sleeps on `nish_uring_fd`. A refused flush
|
|
1360
1522
|
* leaves them queued for the next wait, as ur_flush says. */
|
|
1361
1523
|
static void ur_submit_queued(struct nish_uring_set *s) {
|
|
1362
|
-
for (int ri = 0; ri < s->nrings; ri++)
|
|
1524
|
+
for (int ri = 0; ri < s->nrings; ri++) {
|
|
1525
|
+
ur_send_marked(s, s->rings[ri]);
|
|
1363
1526
|
if (s->rings[ri]->to_submit) (void)ur_flush(s->rings[ri]);
|
|
1527
|
+
}
|
|
1364
1528
|
}
|
|
1365
1529
|
|
|
1366
1530
|
/* Wait for events and write at most `cap` of them, at most 256. Answers the
|
|
@@ -1401,73 +1565,197 @@ again:
|
|
|
1401
1565
|
return n;
|
|
1402
1566
|
}
|
|
1403
1567
|
|
|
1568
|
+
/* Sleep on the set's one ring itself rather than on `nish_uring_fd`: one
|
|
1569
|
+
* io_uring_enter that submits what is queued and waits for `min_complete`
|
|
1570
|
+
* completions (at least 1, at most the CQ's size) or until a timeout passes,
|
|
1571
|
+
* the timeout `nish_uring_sleep_ms` allows for `timeout_ms`, carried in
|
|
1572
|
+
* IORING_ENTER_EXT_ARG. With DEFER_TASKRUN the enter runs the ring's task
|
|
1573
|
+
* work itself, so no eventfd signal is waited for and none can be lost; the
|
|
1574
|
+
* NURING-8 bound is kept all the same, so a ring the kernel flags without a
|
|
1575
|
+
* completion is still serviced within UR_SLEEP_MAX_MS. A `watch_fd` of 0 or
|
|
1576
|
+
* more is watched by a one-shot POLL_ADD in the ring, armed once and taken
|
|
1577
|
+
* with `nish_uring_poll_events` (NURING-45), so a caller that also waits on one
|
|
1578
|
+
* descriptor, `signalFd()`, needs no epoll set beside the ring.
|
|
1579
|
+
*
|
|
1580
|
+
* 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 the
|
|
1582
|
+
* set has more than one ring, the kernel lacks IORING_FEAT_EXT_ARG (before
|
|
1583
|
+
* 5.11), or `watch_fd` cannot be watched (a restricted ring, a poll that
|
|
1584
|
+
* failed, or one still armed on another descriptor), and the caller sleeps on
|
|
1585
|
+
* `nish_uring_fd` as before; or the enter's negative errno. A timeout or a
|
|
1586
|
+
* signal ends the sleep with 0, as epoll_wait's would. */
|
|
1587
|
+
int32_t nish_uring_wait_direct(struct nish_uring_set *s, int32_t watch_fd, int32_t min_complete, int32_t timeout_ms) {
|
|
1588
|
+
if (s->nrings != 1) return -EOPNOTSUPP;
|
|
1589
|
+
struct ur_ring *r = s->rings[0];
|
|
1590
|
+
if (!r->ext_arg) return -EOPNOTSUPP;
|
|
1591
|
+
if (watch_fd >= 0 && (r->restricted || s->poll_broken || (s->poll_armed && s->poll_fd != watch_fd)))
|
|
1592
|
+
return -EOPNOTSUPP;
|
|
1593
|
+
/* Armed even for a wait that does not sleep, so a loop whose slots are
|
|
1594
|
+
* always ready still hears the descriptor: the caller's pump submits it. */
|
|
1595
|
+
if (watch_fd >= 0 && !s->poll_armed && s->poll_revents == 0) {
|
|
1596
|
+
struct ur_sqe *e = ur_sqe(r);
|
|
1597
|
+
if (!e) return 0; /* a full SQ: the caller pumps, which flushes it */
|
|
1598
|
+
e->opcode = UR_OP_POLL_ADD;
|
|
1599
|
+
e->fd = watch_fd;
|
|
1600
|
+
e->op_flags = POLLIN;
|
|
1601
|
+
e->user_data = UR_POLL;
|
|
1602
|
+
s->poll_armed = 1;
|
|
1603
|
+
s->poll_fd = watch_fd;
|
|
1604
|
+
}
|
|
1605
|
+
/* A poll already answered, by a pump that reaped it before this wait, is
|
|
1606
|
+
* work to do now: sleeping would hold a pending signal for a whole enter. */
|
|
1607
|
+
if (watch_fd >= 0 && s->poll_revents != 0) return 0;
|
|
1608
|
+
/* Coalesced sends wait as marked slots: write their SQEs now, so this
|
|
1609
|
+
* enter submits them and sleeps rather than answering at once for the
|
|
1610
|
+
* caller's pump to send them. */
|
|
1611
|
+
ur_send_marked(s, r);
|
|
1612
|
+
int32_t t = nish_uring_sleep_ms(s, timeout_ms);
|
|
1613
|
+
if (t == 0) return 0;
|
|
1614
|
+
struct {
|
|
1615
|
+
int64_t sec, nsec;
|
|
1616
|
+
} ts = {t / 1000, (int64_t)(t % 1000) * 1000000};
|
|
1617
|
+
struct {
|
|
1618
|
+
uint64_t sigmask;
|
|
1619
|
+
uint32_t sigmask_sz, min_wait_usec;
|
|
1620
|
+
uint64_t ts;
|
|
1621
|
+
} arg = {0, 0, 0, (uint64_t)(uintptr_t)&ts};
|
|
1622
|
+
uint32_t want = min_complete < 1 ? 1 : (uint32_t)min_complete;
|
|
1623
|
+
if (want > s->cq_entries) want = s->cq_entries;
|
|
1624
|
+
s->stats.direct_waits++;
|
|
1625
|
+
r->enters++;
|
|
1626
|
+
int rc = ur_errno(syscall(__NR_io_uring_enter, r->fd, r->to_submit, want, UR_ENTER_GETEVENTS | UR_ENTER_EXT_ARG,
|
|
1627
|
+
&arg, sizeof arg));
|
|
1628
|
+
if (rc > 0) r->to_submit -= (uint32_t)rc > r->to_submit ? r->to_submit : (uint32_t)rc;
|
|
1629
|
+
if (rc >= 0 || rc == -ETIME || rc == -EINTR || rc == -EBUSY || rc == -EAGAIN) return 0;
|
|
1630
|
+
return rc;
|
|
1631
|
+
}
|
|
1632
|
+
|
|
1633
|
+
/* The events the direct wait's poll answered since they were last taken
|
|
1634
|
+
* (POLLIN, POLLHUP and POLLERR as poll(2) gives them), or 0; `take` clears
|
|
1635
|
+
* them, so the next direct wait arms the poll again. They say the descriptor
|
|
1636
|
+
* was ready, not that it still is: the caller asks its own epoll set. */
|
|
1637
|
+
int32_t nish_uring_poll_events(struct nish_uring_set *s, int32_t take) {
|
|
1638
|
+
int32_t v = s->poll_revents;
|
|
1639
|
+
if (take) s->poll_revents = 0;
|
|
1640
|
+
return v;
|
|
1641
|
+
}
|
|
1642
|
+
|
|
1404
1643
|
static struct ur_ring *ur_open_slot(struct nish_uring_set *s, int32_t ring, int32_t slot) {
|
|
1405
1644
|
if (ring < 0 || ring >= s->nrings || slot < 0 || (uint32_t)slot >= s->slots) return NULL;
|
|
1406
1645
|
struct ur_ring *r = s->rings[ring];
|
|
1407
|
-
return (r->
|
|
1646
|
+
return (r->sl[slot].st & ST_OPEN) ? r : NULL;
|
|
1408
1647
|
}
|
|
1409
1648
|
|
|
1410
1649
|
/* Put `q` on the set's list of records and behind the slot's queue, and
|
|
1411
|
-
*
|
|
1650
|
+
* mark the slot, so the ring submits it once it is the head. */
|
|
1412
1651
|
static void ur_enqueue(struct nish_uring_set *s, struct ur_ring *r, uint32_t slot, struct ur_send *q) {
|
|
1413
1652
|
q->next = NULL;
|
|
1414
1653
|
q->prev_all = NULL;
|
|
1415
1654
|
q->next_all = s->all;
|
|
1416
1655
|
if (s->all) s->all->prev_all = q;
|
|
1417
1656
|
s->all = q;
|
|
1418
|
-
if (r->
|
|
1419
|
-
r->
|
|
1657
|
+
if (r->sl[slot].last) {
|
|
1658
|
+
r->sl[slot].last->next = q;
|
|
1420
1659
|
} else {
|
|
1421
|
-
r->
|
|
1660
|
+
r->sl[slot].first = q;
|
|
1422
1661
|
}
|
|
1423
|
-
r->
|
|
1424
|
-
|
|
1662
|
+
r->sl[slot].last = q;
|
|
1663
|
+
ur_mark(r, slot);
|
|
1425
1664
|
}
|
|
1426
1665
|
|
|
1427
1666
|
/* Queue `n` bytes for (ring, slot). They are copied, so the caller's buffer
|
|
1428
|
-
* is free on return; a failure comes back later as a SEND_FAILED event.
|
|
1667
|
+
* is free on return; a failure comes back later as a SEND_FAILED event.
|
|
1668
|
+
* Bytes queued before the ring next submits join the slot's last record
|
|
1669
|
+
* while the kernel does not hold it (NURING-40): in its room, after moving
|
|
1670
|
+
* its unsent bytes to the front if that makes room. What does not fit goes
|
|
1671
|
+
* into pool buffers chained behind it (NURING-51), every one taken before
|
|
1672
|
+
* any byte is copied, so -ENOMEM queues nothing. */
|
|
1429
1673
|
int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, const uint8_t *p, int32_t n) {
|
|
1430
1674
|
struct ur_ring *r = ur_open_slot(s, ring, slot);
|
|
1431
1675
|
if (!r) return -EBADF;
|
|
1432
1676
|
if (n < 0) return -EINVAL;
|
|
1433
1677
|
if (n == 0) return 0;
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1678
|
+
uint32_t left = (uint32_t)n, room = 0;
|
|
1679
|
+
struct ur_send *t = r->sl[slot].last;
|
|
1680
|
+
if (t && !t->in_flight && t->how < 0) {
|
|
1681
|
+
if (UR_CHUNK_DATA - t->len < left && t->off) {
|
|
1682
|
+
memmove(t->data, t->data + t->off, t->len - t->off);
|
|
1683
|
+
t->len -= t->off;
|
|
1684
|
+
t->off = 0;
|
|
1685
|
+
}
|
|
1686
|
+
room = UR_CHUNK_DATA - t->len;
|
|
1687
|
+
}
|
|
1688
|
+
struct ur_send *chain = NULL;
|
|
1689
|
+
for (uint32_t k = left > room ? (left - room + UR_CHUNK_DATA - 1) / UR_CHUNK_DATA : 0; k; k--) {
|
|
1690
|
+
struct ur_send *q = ur_record(s, r);
|
|
1691
|
+
if (!q) {
|
|
1692
|
+
while (chain) {
|
|
1693
|
+
q = chain;
|
|
1694
|
+
chain = q->next;
|
|
1695
|
+
ur_release(s, r, q);
|
|
1696
|
+
}
|
|
1697
|
+
return -ENOMEM;
|
|
1698
|
+
}
|
|
1699
|
+
q->next = chain;
|
|
1700
|
+
chain = q;
|
|
1701
|
+
}
|
|
1702
|
+
if (room) {
|
|
1703
|
+
uint32_t c = left < room ? left : room;
|
|
1704
|
+
memcpy(t->data + t->len, p, c);
|
|
1705
|
+
t->len += c;
|
|
1706
|
+
p += c;
|
|
1707
|
+
left -= c;
|
|
1708
|
+
}
|
|
1709
|
+
while (chain) {
|
|
1710
|
+
struct ur_send *q = chain;
|
|
1711
|
+
uint32_t c = left < UR_CHUNK_DATA ? left : UR_CHUNK_DATA;
|
|
1712
|
+
chain = q->next;
|
|
1713
|
+
memcpy(q->data, p, c);
|
|
1714
|
+
p += c;
|
|
1715
|
+
left -= c;
|
|
1716
|
+
q->slot = (uint32_t)slot;
|
|
1717
|
+
q->gen = ST_GEN(r->sl[slot].st);
|
|
1718
|
+
q->off = 0;
|
|
1719
|
+
q->len = c;
|
|
1720
|
+
q->in_flight = 0;
|
|
1721
|
+
q->how = -1;
|
|
1722
|
+
ur_enqueue(s, r, (uint32_t)slot, q);
|
|
1723
|
+
}
|
|
1724
|
+
r->sl[slot].qbytes += (uint32_t)n;
|
|
1725
|
+
ur_mark(r, (uint32_t)slot);
|
|
1445
1726
|
return n;
|
|
1446
1727
|
}
|
|
1447
1728
|
|
|
1448
1729
|
/* Queue a shutdown of (ring, slot)'s `how` side (0 read, 1 write, 2 both)
|
|
1449
1730
|
* behind the bytes already queued for it, so they reach the peer first. Its
|
|
1450
1731
|
* answer is not reported: a shutdown that fails, as on a peer already gone,
|
|
1451
|
-
* leaves a connection whose next read or write says so.
|
|
1732
|
+
* leaves a connection whose next read or write says so. A side already
|
|
1733
|
+
* queued is not queued again, so a slot holds at most two shutdowns and the
|
|
1734
|
+
* bytes between them at most three partly filled records (NURING-43). */
|
|
1452
1735
|
int32_t nish_uring_shutdown(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t how) {
|
|
1453
1736
|
struct ur_ring *r = ur_open_slot(s, ring, slot);
|
|
1454
1737
|
if (!r) return -EBADF;
|
|
1455
1738
|
if ((uint32_t)how > 2) return -EINVAL;
|
|
1456
|
-
|
|
1739
|
+
uint32_t *st = &r->sl[slot].st;
|
|
1740
|
+
if (!(ST_SHUT(how) & ~*st)) return 0;
|
|
1741
|
+
struct ur_send *q = ur_record(s, r);
|
|
1457
1742
|
if (!q) return -ENOMEM;
|
|
1458
1743
|
q->slot = (uint32_t)slot;
|
|
1459
|
-
q->gen = ST_GEN(
|
|
1744
|
+
q->gen = ST_GEN(*st);
|
|
1460
1745
|
q->off = q->len = 0;
|
|
1461
1746
|
q->in_flight = 0;
|
|
1462
1747
|
q->how = how;
|
|
1463
1748
|
ur_enqueue(s, r, (uint32_t)slot, q);
|
|
1749
|
+
*st |= ST_SHUT(how);
|
|
1464
1750
|
return 0;
|
|
1465
1751
|
}
|
|
1466
1752
|
|
|
1467
1753
|
/* Cancel the slot's recv, close the slot, and move its generation on, so
|
|
1468
1754
|
* every completion still on its way for it is stale. The freed slot goes to
|
|
1469
|
-
* the accepting job.
|
|
1470
|
-
*
|
|
1755
|
+
* the accepting job. A head send the ring has not submitted yet is written
|
|
1756
|
+
* first (NURING-40). -EAGAIN, with the slot still open, when the SQ has no
|
|
1757
|
+
* room for the entries or while any shutdown is at the slot's queue's head,
|
|
1758
|
+
* which it then submits if it has not: a SHUTDOWN runs
|
|
1471
1759
|
* on io-wq, which looks the slot's file up only when it runs, and the cancel
|
|
1472
1760
|
* below cannot match a request that has no file yet, so a slot freed before
|
|
1473
1761
|
* it ran could take a new connection that the shutdown then reaches
|
|
@@ -1475,15 +1763,20 @@ int32_t nish_uring_shutdown(struct nish_uring_set *s, int32_t ring, int32_t slot
|
|
|
1475
1763
|
int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot) {
|
|
1476
1764
|
struct ur_ring *r = ur_open_slot(s, ring, slot);
|
|
1477
1765
|
if (!r) return -EBADF;
|
|
1478
|
-
const struct ur_send *h = r->
|
|
1479
|
-
if (h && h->
|
|
1766
|
+
const struct ur_send *h = r->sl[slot].first;
|
|
1767
|
+
if (h && h->how >= 0) {
|
|
1768
|
+
ur_send_head(r, (uint32_t)slot, s);
|
|
1769
|
+
return -EAGAIN;
|
|
1770
|
+
}
|
|
1771
|
+
uint32_t need = h && !h->in_flight ? 3 : 2;
|
|
1480
1772
|
uint32_t head = __atomic_load_n(r->sq_head, __ATOMIC_ACQUIRE);
|
|
1481
|
-
if (r->sq_local_tail - head +
|
|
1773
|
+
if (r->sq_local_tail - head + need > r->sq_entries) {
|
|
1482
1774
|
(void)ur_flush(r);
|
|
1483
1775
|
head = __atomic_load_n(r->sq_head, __ATOMIC_ACQUIRE);
|
|
1484
|
-
if (r->sq_local_tail - head +
|
|
1776
|
+
if (r->sq_local_tail - head + need > r->sq_entries) return -EAGAIN;
|
|
1485
1777
|
}
|
|
1486
|
-
uint32_t
|
|
1778
|
+
ur_send_head(r, (uint32_t)slot, s);
|
|
1779
|
+
uint32_t st = r->sl[slot].st;
|
|
1487
1780
|
struct ur_sqe *e = ur_sqe(r);
|
|
1488
1781
|
e->opcode = UR_OP_ASYNC_CANCEL;
|
|
1489
1782
|
e->fd = slot;
|
|
@@ -1496,7 +1789,7 @@ int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot) {
|
|
|
1496
1789
|
e->flags = UR_SQE_CQE_SKIP_SUCCESS;
|
|
1497
1790
|
e->user_data = ur_tag(UR_CLOSE, st, (uint32_t)slot);
|
|
1498
1791
|
ur_drop_sends(s, r, (uint32_t)slot);
|
|
1499
|
-
r->
|
|
1792
|
+
r->sl[slot].st = (((ST_GEN(st) + 1) & GEN_MASK) << 8) | (st & ST_MARKED);
|
|
1500
1793
|
r->live--;
|
|
1501
1794
|
s->stats.live--;
|
|
1502
1795
|
ur_top_up(s);
|
|
@@ -1509,7 +1802,7 @@ int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot) {
|
|
|
1509
1802
|
int64_t nish_uring_queued(const struct nish_uring_set *s, int32_t ring, int32_t slot) {
|
|
1510
1803
|
if (ring < 0 || ring >= s->nrings || slot < 0 || (uint32_t)slot >= s->slots) return -1;
|
|
1511
1804
|
const struct ur_ring *r = s->rings[ring];
|
|
1512
|
-
return (r->
|
|
1805
|
+
return (r->sl[slot].st & ST_OPEN) ? (int64_t)r->sl[slot].qbytes : -1;
|
|
1513
1806
|
}
|
|
1514
1807
|
|
|
1515
1808
|
/* 1 when no accept is in flight and none can be: every ring's table is full
|
|
@@ -1536,9 +1829,9 @@ int32_t nish_uring_full(const struct nish_uring_set *s) {
|
|
|
1536
1829
|
int32_t nish_uring_hold(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t on) {
|
|
1537
1830
|
struct ur_ring *r = ur_open_slot(s, ring, slot);
|
|
1538
1831
|
if (!r) return -EBADF;
|
|
1539
|
-
uint32_t st = r->
|
|
1832
|
+
uint32_t st = r->sl[slot].st;
|
|
1540
1833
|
if (!on) {
|
|
1541
|
-
r->
|
|
1834
|
+
r->sl[slot].st = st & ~ST_HELD;
|
|
1542
1835
|
if ((st & (ST_ARMED | ST_PARKED)) == 0) ur_arm(r, (uint32_t)slot);
|
|
1543
1836
|
return 0;
|
|
1544
1837
|
}
|
|
@@ -1551,11 +1844,15 @@ int32_t nish_uring_hold(struct nish_uring_set *s, int32_t ring, int32_t slot, in
|
|
|
1551
1844
|
e->flags = UR_SQE_CQE_SKIP_SUCCESS;
|
|
1552
1845
|
e->user_data = ur_tag(UR_CANCEL, st, (uint32_t)slot);
|
|
1553
1846
|
}
|
|
1554
|
-
r->
|
|
1847
|
+
r->sl[slot].st = st | ST_HELD;
|
|
1555
1848
|
return 0;
|
|
1556
1849
|
}
|
|
1557
1850
|
|
|
1558
|
-
void nish_uring_stats(const struct nish_uring_set *s, struct nish_uring_stats *st) {
|
|
1851
|
+
void nish_uring_stats(const struct nish_uring_set *s, struct nish_uring_stats *st) {
|
|
1852
|
+
*st = s->stats;
|
|
1853
|
+
st->enters = 0;
|
|
1854
|
+
for (int i = 0; i < s->nrings; i++) st->enters += s->rings[i]->enters;
|
|
1855
|
+
}
|
|
1559
1856
|
|
|
1560
1857
|
/* Close every ring, which ends whatever the kernel still held, and free the
|
|
1561
1858
|
* sends it held them for. The caller's listener stays open: it is theirs. */
|
|
@@ -1603,6 +1900,18 @@ int32_t nish_uring_wait(struct nish_uring_set *s, int32_t timeout_ms, struct nis
|
|
|
1603
1900
|
(void)cap;
|
|
1604
1901
|
return -38;
|
|
1605
1902
|
}
|
|
1903
|
+
int32_t nish_uring_wait_direct(struct nish_uring_set *s, int32_t watch_fd, int32_t min_complete, int32_t timeout_ms) {
|
|
1904
|
+
(void)s;
|
|
1905
|
+
(void)watch_fd;
|
|
1906
|
+
(void)min_complete;
|
|
1907
|
+
(void)timeout_ms;
|
|
1908
|
+
return -38;
|
|
1909
|
+
}
|
|
1910
|
+
int32_t nish_uring_poll_events(struct nish_uring_set *s, int32_t take) {
|
|
1911
|
+
(void)s;
|
|
1912
|
+
(void)take;
|
|
1913
|
+
return 0;
|
|
1914
|
+
}
|
|
1606
1915
|
int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, const uint8_t *p, int32_t n) {
|
|
1607
1916
|
(void)s;
|
|
1608
1917
|
(void)ring;
|