nish 0.46.0 → 0.48.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/docs/AI.md +29 -18
- package/package.json +5 -5
- 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.h +1 -0
- package/runtime/nish.mjs +1 -0
- package/runtime/runtime-bytes.c +167 -7
- package/runtime/runtime-net.c +122 -52
- package/runtime/runtime-uring.c +150 -69
- package/runtime/sqlite/nish-sqlite.c +166 -39
- package/scripts/build.sh +88 -3
- package/std/README.md +2 -2
- package/std/base64.ts +28 -30
- package/std/compress/deflate.ts +1371 -704
- 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 +24 -6
- package/std/net/http-server.ts +358 -141
- package/std/net/http1-server.ts +44 -13
- package/std/net/prefork.ts +1 -2
- package/std/net/static.ts +7 -2
- package/std/random.ts +34 -51
- package/std/raster.ts +377 -113
- package/std/regex.ts +46 -15
- package/std/release.ts +19 -13
- package/std/sqlite.ts +380 -16
- package/std/synth.ts +20 -3
- 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
|
|
@@ -139,6 +144,7 @@ struct nish_uring_stats {
|
|
|
139
144
|
int64_t pool_new; /* send buffers the pools ever allocated */
|
|
140
145
|
int64_t pool_held; /* pool buffers that queued bytes hold now */
|
|
141
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 */
|
|
142
148
|
};
|
|
143
149
|
|
|
144
150
|
struct nish_uring_set;
|
|
@@ -368,6 +374,13 @@ struct ur_send {
|
|
|
368
374
|
#define UR_CHUNK_DATA ((uint32_t)(UR_CHUNK - sizeof(struct ur_send)))
|
|
369
375
|
#define UR_SENDPOOL_MAX 32
|
|
370
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
|
+
|
|
371
384
|
struct ur_ring {
|
|
372
385
|
int fd, efd;
|
|
373
386
|
uint32_t *sq_head, *sq_tail, *sq_mask, *sq_array, *sq_flags;
|
|
@@ -377,15 +390,17 @@ struct ur_ring {
|
|
|
377
390
|
void *ring_map;
|
|
378
391
|
size_t ring_len, sqes_len;
|
|
379
392
|
uint32_t sq_entries, sq_local_tail, to_submit;
|
|
393
|
+
int64_t enters; /* io_uring_enter calls, for the stats' `enters` */
|
|
380
394
|
struct ur_buf *br;
|
|
381
395
|
uint8_t *bufs;
|
|
382
396
|
size_t br_len, bufs_len;
|
|
383
397
|
uint16_t br_tail;
|
|
384
398
|
long live, accepting;
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
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;
|
|
389
404
|
uint32_t *parked; /* slots whose recv waits for buffers */
|
|
390
405
|
uint32_t nparked;
|
|
391
406
|
/* IORING_FEAT_EXT_ARG: an enter can carry its own timeout, so a sleeper
|
|
@@ -404,6 +419,7 @@ struct ur_ring {
|
|
|
404
419
|
struct nish_uring_set {
|
|
405
420
|
int listener, ep;
|
|
406
421
|
uint32_t slots, buf_count, buf_size, sq_entries, cq_entries;
|
|
422
|
+
uint32_t taskrun_worth; /* `nish_uring_opts.taskrun_worth`, defaulted */
|
|
407
423
|
int nrings, rings_max, acceptor;
|
|
408
424
|
int accept_stopped;
|
|
409
425
|
/* Accepting waits until `resume_ms` after an accept found memory short; the
|
|
@@ -424,6 +440,9 @@ struct nish_uring_set {
|
|
|
424
440
|
* events it answered and not yet taken, and whether one failed, after
|
|
425
441
|
* which no descriptor is watched again. */
|
|
426
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;
|
|
427
446
|
int32_t poll_revents;
|
|
428
447
|
struct nish_uring_stats stats;
|
|
429
448
|
};
|
|
@@ -448,6 +467,7 @@ static int ur_errno(long rc) { return rc < 0 ? -errno : (int)rc; }
|
|
|
448
467
|
static int ur_setup(uint32_t entries, struct ur_params *p) { return ur_errno(UR_SYS_SETUP(entries, p)); }
|
|
449
468
|
|
|
450
469
|
static int ur_enter(struct ur_ring *r, uint32_t submit, uint32_t wait, uint32_t flags) {
|
|
470
|
+
r->enters++;
|
|
451
471
|
return ur_errno(syscall(__NR_io_uring_enter, r->fd, submit, wait, flags, NULL, 0));
|
|
452
472
|
}
|
|
453
473
|
|
|
@@ -476,6 +496,20 @@ static int ur_setup_flags(uint32_t entries, uint32_t cq, struct ur_params *p) {
|
|
|
476
496
|
return fd;
|
|
477
497
|
}
|
|
478
498
|
|
|
499
|
+
/* Registers the provided-buffer ring `reg` describes. Ubuntu's 6.8.0-136
|
|
500
|
+
* and later kernels invert the check on its reserved words (Launchpad
|
|
501
|
+
* #2162843): they refuse all zeroes with -EINVAL and accept a nonzero
|
|
502
|
+
* `resv[0]`, so a refused registration is tried once more that way. A
|
|
503
|
+
* correct kernel answers the first, and never sees the second. */
|
|
504
|
+
static int ur_register_pbuf(int fd, struct ur_buf_reg *reg) {
|
|
505
|
+
int rc = ur_register(fd, UR_REGISTER_PBUF_RING, reg, 1);
|
|
506
|
+
if (rc == -EINVAL) {
|
|
507
|
+
reg->resv[0] = 1;
|
|
508
|
+
rc = ur_register(fd, UR_REGISTER_PBUF_RING, reg, 1);
|
|
509
|
+
}
|
|
510
|
+
return rc;
|
|
511
|
+
}
|
|
512
|
+
|
|
479
513
|
/* Whether the ring `fd` has every opcode the layer submits: 1, -EOPNOTSUPP
|
|
480
514
|
* when one is missing or the kernel has no probe, or the register's errno. */
|
|
481
515
|
static int32_t ur_ops_ok(int fd) {
|
|
@@ -516,7 +550,7 @@ static int32_t ur_probe_once(void) {
|
|
|
516
550
|
reg.ring_addr = (uint64_t)(uintptr_t)br;
|
|
517
551
|
reg.ring_entries = 8;
|
|
518
552
|
reg.bgid = BGID;
|
|
519
|
-
int e =
|
|
553
|
+
int e = ur_register_pbuf(fd, ®);
|
|
520
554
|
if (e < 0) rc = e == -EINVAL ? -EOPNOTSUPP : e;
|
|
521
555
|
close(fd);
|
|
522
556
|
fd = -1;
|
|
@@ -626,11 +660,11 @@ static void ur_recycle(struct nish_uring_set *s, struct ur_ring *r, uint32_t bid
|
|
|
626
660
|
* answer 0 once the kernel has handed over the reset, and a second hangup
|
|
627
661
|
* would turn the reset into a clean end (NURING-27). */
|
|
628
662
|
static void ur_arm(struct ur_ring *r, uint32_t slot) {
|
|
629
|
-
if (r->
|
|
663
|
+
if (r->sl[slot].st & ST_HUP) return;
|
|
630
664
|
struct ur_sqe *e = ur_sqe(r);
|
|
631
665
|
if (!e) {
|
|
632
|
-
if (!(r->
|
|
633
|
-
r->
|
|
666
|
+
if (!(r->sl[slot].st & ST_PARKED)) {
|
|
667
|
+
r->sl[slot].st |= ST_PARKED;
|
|
634
668
|
r->parked[r->nparked++] = slot;
|
|
635
669
|
}
|
|
636
670
|
return;
|
|
@@ -640,8 +674,8 @@ static void ur_arm(struct ur_ring *r, uint32_t slot) {
|
|
|
640
674
|
e->flags = UR_SQE_FIXED_FILE | UR_SQE_BUFFER_SELECT;
|
|
641
675
|
e->ioprio = UR_RECV_MULTISHOT;
|
|
642
676
|
e->buf_group = BGID;
|
|
643
|
-
e->user_data = ur_tag(UR_RECV, r->
|
|
644
|
-
r->
|
|
677
|
+
e->user_data = ur_tag(UR_RECV, r->sl[slot].st, slot);
|
|
678
|
+
r->sl[slot].st |= ST_ARMED;
|
|
645
679
|
}
|
|
646
680
|
|
|
647
681
|
static void ur_free_ring(struct ur_ring *r) {
|
|
@@ -660,11 +694,8 @@ static void ur_free_ring(struct ur_ring *r) {
|
|
|
660
694
|
if (r->sqes && (void *)r->sqes != MAP_FAILED) munmap(r->sqes, r->sqes_len);
|
|
661
695
|
if (r->br && (void *)r->br != MAP_FAILED) munmap(r->br, r->br_len);
|
|
662
696
|
if (r->bufs && (void *)r->bufs != MAP_FAILED) munmap(r->bufs, r->bufs_len);
|
|
663
|
-
free(r->
|
|
664
|
-
free(r->sq_first);
|
|
665
|
-
free(r->sq_last);
|
|
697
|
+
free(r->sl);
|
|
666
698
|
free(r->parked);
|
|
667
|
-
free(r->qbytes);
|
|
668
699
|
free(r->mlist);
|
|
669
700
|
while (r->pool) {
|
|
670
701
|
struct ur_send *q = r->pool;
|
|
@@ -803,17 +834,14 @@ static int ur_make_ring(const struct ur_sizes *z, int restricted, struct ur_ring
|
|
|
803
834
|
reg.ring_addr = (uint64_t)(uintptr_t)r->br;
|
|
804
835
|
reg.ring_entries = z->buf_count;
|
|
805
836
|
reg.bgid = BGID;
|
|
806
|
-
rc =
|
|
837
|
+
rc = ur_register_pbuf(r->fd, ®);
|
|
807
838
|
if (rc < 0) goto fail;
|
|
808
839
|
|
|
809
840
|
/* Per-slot state: generation and flags, send queues, parked recvs. */
|
|
810
|
-
r->
|
|
811
|
-
r->sq_first = calloc(z->slots, sizeof *r->sq_first);
|
|
812
|
-
r->sq_last = calloc(z->slots, sizeof *r->sq_last);
|
|
841
|
+
r->sl = calloc(z->slots, sizeof *r->sl);
|
|
813
842
|
r->parked = calloc(z->slots, sizeof *r->parked);
|
|
814
|
-
r->qbytes = calloc(z->slots, sizeof *r->qbytes);
|
|
815
843
|
r->mlist = calloc(z->slots, sizeof *r->mlist);
|
|
816
|
-
if (!r->
|
|
844
|
+
if (!r->sl || !r->parked || !r->mlist) {
|
|
817
845
|
rc = -ENOMEM;
|
|
818
846
|
goto fail;
|
|
819
847
|
}
|
|
@@ -1022,7 +1050,7 @@ static void ur_top_up(struct nish_uring_set *s) {
|
|
|
1022
1050
|
/* Submit the slot's first queued send, from where the kernel left off, or
|
|
1023
1051
|
* its shutdown. */
|
|
1024
1052
|
static void ur_send_head(struct ur_ring *r, uint32_t slot, struct nish_uring_set *s) {
|
|
1025
|
-
struct ur_send *q = r->
|
|
1053
|
+
struct ur_send *q = r->sl[slot].first;
|
|
1026
1054
|
if (!q || q->in_flight) return;
|
|
1027
1055
|
struct ur_sqe *e = ur_sqe(r);
|
|
1028
1056
|
if (!e) {
|
|
@@ -1048,8 +1076,8 @@ static void ur_send_head(struct ur_ring *r, uint32_t slot, struct nish_uring_set
|
|
|
1048
1076
|
/* Note that (r, slot) has a record to submit; the ring writes its SEND when
|
|
1049
1077
|
* it next submits. */
|
|
1050
1078
|
static void ur_mark(struct ur_ring *r, uint32_t slot) {
|
|
1051
|
-
if (r->
|
|
1052
|
-
r->
|
|
1079
|
+
if (r->sl[slot].st & ST_MARKED) return;
|
|
1080
|
+
r->sl[slot].st |= ST_MARKED;
|
|
1053
1081
|
r->mlist[r->nmarked++] = slot;
|
|
1054
1082
|
}
|
|
1055
1083
|
|
|
@@ -1061,7 +1089,7 @@ static void ur_send_marked(struct nish_uring_set *s, struct ur_ring *r) {
|
|
|
1061
1089
|
r->nmarked = 0;
|
|
1062
1090
|
for (uint32_t i = 0; i < n; i++) {
|
|
1063
1091
|
uint32_t slot = r->mlist[i];
|
|
1064
|
-
r->
|
|
1092
|
+
r->sl[slot].st &= ~ST_MARKED;
|
|
1065
1093
|
ur_send_head(r, slot, s);
|
|
1066
1094
|
}
|
|
1067
1095
|
}
|
|
@@ -1110,9 +1138,9 @@ static void ur_unlink_all(struct nish_uring_set *s, struct ur_ring *r, struct ur
|
|
|
1110
1138
|
* the all-records list; its completion finds the generation moved on and
|
|
1111
1139
|
* frees it. */
|
|
1112
1140
|
static void ur_drop_sends(struct nish_uring_set *s, struct ur_ring *r, uint32_t slot) {
|
|
1113
|
-
struct ur_send *q = r->
|
|
1114
|
-
r->
|
|
1115
|
-
r->
|
|
1141
|
+
struct ur_send *q = r->sl[slot].first;
|
|
1142
|
+
r->sl[slot].first = r->sl[slot].last = NULL;
|
|
1143
|
+
r->sl[slot].qbytes = 0;
|
|
1116
1144
|
while (q) {
|
|
1117
1145
|
struct ur_send *next = q->next;
|
|
1118
1146
|
q->next = NULL;
|
|
@@ -1148,7 +1176,7 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
|
|
|
1148
1176
|
struct ur_send *q = (struct ur_send *)(uintptr_t)(ud & ~(uint64_t)7);
|
|
1149
1177
|
q->in_flight = 0;
|
|
1150
1178
|
uint32_t slot = q->slot;
|
|
1151
|
-
if ((r->
|
|
1179
|
+
if ((r->sl[slot].st & ST_OPEN) == 0 || ST_GEN(r->sl[slot].st) != q->gen || r->sl[slot].first != q) {
|
|
1152
1180
|
ur_unlink_all(s, r, q);
|
|
1153
1181
|
return 0;
|
|
1154
1182
|
}
|
|
@@ -1157,7 +1185,7 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
|
|
|
1157
1185
|
* already gone says nothing a recv will not, so it is not reported. */
|
|
1158
1186
|
res = 0;
|
|
1159
1187
|
}
|
|
1160
|
-
if (res > 0) r->
|
|
1188
|
+
if (res > 0) r->sl[slot].qbytes -= (uint32_t)res < r->sl[slot].qbytes ? (uint32_t)res : r->sl[slot].qbytes;
|
|
1161
1189
|
if (res > 0 && q->off + (uint32_t)res < q->len) {
|
|
1162
1190
|
q->off += (uint32_t)res;
|
|
1163
1191
|
s->stats.send_requeues++;
|
|
@@ -1169,8 +1197,8 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
|
|
|
1169
1197
|
ur_event(ev, NISH_URING_SEND_FAILED, ri, slot, res, NULL);
|
|
1170
1198
|
return 1;
|
|
1171
1199
|
} else {
|
|
1172
|
-
r->
|
|
1173
|
-
if (!q->next) r->
|
|
1200
|
+
r->sl[slot].first = q->next;
|
|
1201
|
+
if (!q->next) r->sl[slot].last = NULL;
|
|
1174
1202
|
ur_unlink_all(s, r, q);
|
|
1175
1203
|
}
|
|
1176
1204
|
ur_send_head(r, slot, s);
|
|
@@ -1196,7 +1224,7 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
|
|
|
1196
1224
|
if (res >= 0 && (uint32_t)res < s->slots) {
|
|
1197
1225
|
uint32_t at = (uint32_t)res;
|
|
1198
1226
|
s->backoff_ms = 0;
|
|
1199
|
-
r->
|
|
1227
|
+
r->sl[at].st = (r->sl[at].st & ~(ST_ARMED | ST_PARKED | ST_HELD | ST_HUP)) | ST_OPEN;
|
|
1200
1228
|
r->live++;
|
|
1201
1229
|
s->stats.live++;
|
|
1202
1230
|
ur_arm(r, at);
|
|
@@ -1230,7 +1258,7 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
|
|
|
1230
1258
|
return wrote;
|
|
1231
1259
|
}
|
|
1232
1260
|
if (op != UR_RECV) return 0; /* a close or cancel; nothing to report */
|
|
1233
|
-
int stale = slot >= s->slots || (r->
|
|
1261
|
+
int stale = slot >= s->slots || (r->sl[slot].st & ST_OPEN) == 0 || (ST_GEN(r->sl[slot].st) & GEN_MASK) != gen;
|
|
1234
1262
|
int more = (c->flags & UR_CQE_F_MORE) != 0;
|
|
1235
1263
|
uint32_t bid = c->flags >> UR_CQE_BUFFER_SHIFT;
|
|
1236
1264
|
int has_buf = (c->flags & UR_CQE_F_BUFFER) != 0 && bid < s->buf_count;
|
|
@@ -1238,12 +1266,12 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
|
|
|
1238
1266
|
if (has_buf) ur_recycle(s, r, bid);
|
|
1239
1267
|
return 0;
|
|
1240
1268
|
}
|
|
1241
|
-
if (!more) r->
|
|
1269
|
+
if (!more) r->sl[slot].st &= ~ST_ARMED;
|
|
1242
1270
|
if (res > 0 && has_buf) {
|
|
1243
1271
|
s->lent_ring[s->nlent] = (uint32_t)ri;
|
|
1244
1272
|
s->lent_bid[s->nlent] = (uint16_t)bid;
|
|
1245
1273
|
s->nlent++;
|
|
1246
|
-
if (!more && !(r->
|
|
1274
|
+
if (!more && !(r->sl[slot].st & ST_HELD)) ur_arm(r, slot);
|
|
1247
1275
|
ur_event(ev, NISH_URING_DATA, ri, slot, res, r->bufs + (size_t)bid * s->buf_size);
|
|
1248
1276
|
return 1;
|
|
1249
1277
|
}
|
|
@@ -1252,20 +1280,20 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
|
|
|
1252
1280
|
* first). The recv stays down while the slot is held, and one let go before
|
|
1253
1281
|
* the cancel landed is armed again; a held recv out of buffers stays down. */
|
|
1254
1282
|
if (!more && res == -ECANCELED) {
|
|
1255
|
-
if (!(r->
|
|
1283
|
+
if (!(r->sl[slot].st & ST_HELD)) ur_arm(r, slot);
|
|
1256
1284
|
return 0;
|
|
1257
1285
|
}
|
|
1258
|
-
if (!more && res == -ENOBUFS && (r->
|
|
1286
|
+
if (!more && res == -ENOBUFS && (r->sl[slot].st & ST_HELD)) return 0;
|
|
1259
1287
|
if (res == -ENOBUFS) {
|
|
1260
1288
|
s->stats.nobufs_rearms++;
|
|
1261
|
-
if (!more && !(r->
|
|
1262
|
-
r->
|
|
1289
|
+
if (!more && !(r->sl[slot].st & ST_PARKED)) {
|
|
1290
|
+
r->sl[slot].st |= ST_PARKED;
|
|
1263
1291
|
r->parked[r->nparked++] = slot;
|
|
1264
1292
|
}
|
|
1265
1293
|
return 0;
|
|
1266
1294
|
}
|
|
1267
1295
|
if (more) return 0;
|
|
1268
|
-
r->
|
|
1296
|
+
r->sl[slot].st |= ST_HUP;
|
|
1269
1297
|
ur_event(ev, NISH_URING_HUP, ri, slot, res, NULL);
|
|
1270
1298
|
return 1;
|
|
1271
1299
|
}
|
|
@@ -1289,6 +1317,19 @@ static int ur_reap(struct nish_uring_set *s, int ri, struct nish_uring_event *ev
|
|
|
1289
1317
|
return n;
|
|
1290
1318
|
}
|
|
1291
1319
|
|
|
1320
|
+
/* Task-work entries a service makes after the first while they keep paying:
|
|
1321
|
+
* one after which fewer than the set's `taskrun_worth` completions were
|
|
1322
|
+
* reaped ends them (UR_TASKRUN_WORTH unless `nish_uring_opts` says). It
|
|
1323
|
+
* counts completions, not the events they hand the caller: a send's
|
|
1324
|
+
* completion is no event, and counting events stopped a backlog of them
|
|
1325
|
+
* from draining, which held every send's buffer. Under steady load new task
|
|
1326
|
+
* work is always arriving, so entering until the flag is down chased it one
|
|
1327
|
+
* completion at a time (measured: 7.8 entries a request at 48k requests a
|
|
1328
|
+
* second); what is left waits for the next wait, which services a flagged
|
|
1329
|
+
* ring without sleeping (`nish_uring_sleep_ms`). A backlog keeps every entry
|
|
1330
|
+
* full, so it is drained to the end, never left to grow. */
|
|
1331
|
+
#define UR_TASKRUN_WORTH 16
|
|
1332
|
+
|
|
1292
1333
|
/* Reap, submit, and enter again while the kernel says it holds more for this
|
|
1293
1334
|
* ring (behaviour 4): IORING_SQ_TASKRUN for completions DEFER_TASKRUN has not
|
|
1294
1335
|
* run, IORING_SQ_CQ_OVERFLOW for ones the CQ had no room for. A flush that
|
|
@@ -1299,14 +1340,18 @@ static int ur_reap(struct nish_uring_set *s, int ri, struct nish_uring_event *ev
|
|
|
1299
1340
|
* starts again from here. */
|
|
1300
1341
|
static int ur_service(struct nish_uring_set *s, int ri, struct nish_uring_event *ev, int cap) {
|
|
1301
1342
|
struct ur_ring *r = s->rings[ri];
|
|
1302
|
-
int n = 0;
|
|
1343
|
+
int n = 0, entered = 0;
|
|
1344
|
+
uint32_t mark = 0;
|
|
1303
1345
|
ur_send_marked(s, r);
|
|
1304
1346
|
for (int guard = 0; guard < 1 << 16; guard++) {
|
|
1305
1347
|
n += ur_reap(s, ri, ev + n, cap - n);
|
|
1306
1348
|
int rc = ur_flush(r);
|
|
1307
1349
|
if (n >= cap) break;
|
|
1350
|
+
if (entered && *r->cq_head - mark < s->taskrun_worth) break;
|
|
1308
1351
|
uint32_t flags = __atomic_load_n(r->sq_flags, __ATOMIC_ACQUIRE);
|
|
1309
1352
|
if (flags & (UR_SQ_TASKRUN | UR_SQ_CQ_OVERFLOW)) {
|
|
1353
|
+
entered++;
|
|
1354
|
+
mark = *r->cq_head;
|
|
1310
1355
|
if (flags & UR_SQ_TASKRUN) s->stats.taskrun_reentries++;
|
|
1311
1356
|
if (flags & UR_SQ_CQ_OVERFLOW) s->stats.overflow_drains++;
|
|
1312
1357
|
rc = ur_enter(r, 0, 0, UR_ENTER_GETEVENTS);
|
|
@@ -1327,7 +1372,7 @@ int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const str
|
|
|
1327
1372
|
memset(&d, 0, sizeof d);
|
|
1328
1373
|
if (o) d = *o;
|
|
1329
1374
|
if (listener < 0 || d.slots < 0 || d.rings_max < 0 || d.sq_entries < 0 || d.cq_entries < 0 || d.buf_count < 0 ||
|
|
1330
|
-
d.buf_size < 0)
|
|
1375
|
+
d.buf_size < 0 || d.taskrun_worth < 0)
|
|
1331
1376
|
return -EINVAL;
|
|
1332
1377
|
if (d.slots == 0) {
|
|
1333
1378
|
struct rlimit rl;
|
|
@@ -1341,6 +1386,7 @@ int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const str
|
|
|
1341
1386
|
if (d.cq_entries < d.sq_entries) d.cq_entries = d.sq_entries;
|
|
1342
1387
|
if (d.buf_count == 0) d.buf_count = UR_BUF_COUNT_DEFAULT;
|
|
1343
1388
|
if (d.buf_size == 0) d.buf_size = UR_BUF_SIZE_DEFAULT;
|
|
1389
|
+
if (d.taskrun_worth == 0) d.taskrun_worth = UR_TASKRUN_WORTH;
|
|
1344
1390
|
if (d.rings_max > 4096 || d.buf_count > 32768 || (d.buf_count & (d.buf_count - 1)) != 0 || d.buf_size > (1 << 20))
|
|
1345
1391
|
return -EINVAL;
|
|
1346
1392
|
/* A pooled ring was made at its own sizes, before the program could ask. */
|
|
@@ -1360,6 +1406,7 @@ int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const str
|
|
|
1360
1406
|
s->cq_entries = (uint32_t)d.cq_entries;
|
|
1361
1407
|
s->buf_count = (uint32_t)d.buf_count;
|
|
1362
1408
|
s->buf_size = (uint32_t)d.buf_size;
|
|
1409
|
+
s->taskrun_worth = (uint32_t)d.taskrun_worth;
|
|
1363
1410
|
s->rings = calloc((size_t)d.rings_max, sizeof *s->rings);
|
|
1364
1411
|
s->poll_fd = -1;
|
|
1365
1412
|
s->ep = epoll_create1(EPOLL_CLOEXEC);
|
|
@@ -1416,6 +1463,10 @@ static int ur_flagged(const struct ur_ring *r) {
|
|
|
1416
1463
|
* wakes a second. */
|
|
1417
1464
|
#define UR_SLEEP_MAX_MS 10
|
|
1418
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
|
+
|
|
1419
1470
|
/* How long a caller may sleep on `nish_uring_fd` before it calls wait, given
|
|
1420
1471
|
* the longest it wants (`timeout_ms`, -1 for ever). Answers 0 while a wait has
|
|
1421
1472
|
* work it can do now: a ring the kernel flagged, completions not yet reaped,
|
|
@@ -1455,8 +1506,8 @@ static void ur_catch_up(struct nish_uring_set *s) {
|
|
|
1455
1506
|
r->nparked = 0;
|
|
1456
1507
|
for (uint32_t i = 0; i < n; i++) {
|
|
1457
1508
|
uint32_t slot = r->parked[i];
|
|
1458
|
-
r->
|
|
1459
|
-
if ((r->
|
|
1509
|
+
r->sl[slot].st &= ~ST_PARKED;
|
|
1510
|
+
if ((r->sl[slot].st & (ST_OPEN | ST_ARMED | ST_HELD)) == ST_OPEN) ur_arm(r, slot);
|
|
1460
1511
|
}
|
|
1461
1512
|
}
|
|
1462
1513
|
/* Scanning every slot of every ring costs rings * slots, but only after a
|
|
@@ -1466,7 +1517,7 @@ static void ur_catch_up(struct nish_uring_set *s) {
|
|
|
1466
1517
|
s->stalled_sends = 0;
|
|
1467
1518
|
for (int ri = 0; ri < s->nrings; ri++)
|
|
1468
1519
|
for (uint32_t slot = 0; slot < s->slots; slot++)
|
|
1469
|
-
if (s->rings[ri]->
|
|
1520
|
+
if (s->rings[ri]->sl[slot].first) ur_send_head(s->rings[ri], slot, s);
|
|
1470
1521
|
}
|
|
1471
1522
|
}
|
|
1472
1523
|
|
|
@@ -1499,6 +1550,7 @@ again:
|
|
|
1499
1550
|
int n = 0;
|
|
1500
1551
|
for (int ri = 0; ri < s->nrings && n < cap; ri++) n += ur_service(s, ri, ev + n, cap - n);
|
|
1501
1552
|
ur_submit_queued(s);
|
|
1553
|
+
s->events_seen += (uint64_t)n;
|
|
1502
1554
|
if (n > 0) return n;
|
|
1503
1555
|
/* No eventfd is trusted to end the sleep (NURING-8): the bound does, and the
|
|
1504
1556
|
* flagged rings are serviced below whether an eventfd was read or not. */
|
|
@@ -1517,6 +1569,7 @@ again:
|
|
|
1517
1569
|
for (int ri = 0; ri < s->nrings && n < cap; ri++)
|
|
1518
1570
|
if (ur_flagged(s->rings[ri])) n += ur_service(s, ri, ev + n, cap - n);
|
|
1519
1571
|
ur_submit_queued(s);
|
|
1572
|
+
s->events_seen += (uint64_t)n;
|
|
1520
1573
|
if (n == 0 && timeout_ms < 0) goto again;
|
|
1521
1574
|
return n;
|
|
1522
1575
|
}
|
|
@@ -1533,15 +1586,31 @@ again:
|
|
|
1533
1586
|
* with `nish_uring_poll_events` (NURING-45), so a caller that also waits on one
|
|
1534
1587
|
* descriptor, `signalFd()`, needs no epoll set beside the ring.
|
|
1535
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
|
+
*
|
|
1536
1600
|
* Answers 0 after a sleep or when there is work to do now, and the caller
|
|
1537
|
-
* then takes events with `nish_uring_wait(s, 0, ...)`; -EOPNOTSUPP when
|
|
1538
|
-
*
|
|
1539
|
-
* 5.11), or `watch_fd` cannot be
|
|
1540
|
-
*
|
|
1541
|
-
*
|
|
1542
|
-
* 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. */
|
|
1543
1608
|
int32_t nish_uring_wait_direct(struct nish_uring_set *s, int32_t watch_fd, int32_t min_complete, int32_t timeout_ms) {
|
|
1544
|
-
|
|
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
|
+
}
|
|
1545
1614
|
struct ur_ring *r = s->rings[0];
|
|
1546
1615
|
if (!r->ext_arg) return -EOPNOTSUPP;
|
|
1547
1616
|
if (watch_fd >= 0 && (r->restricted || s->poll_broken || (s->poll_armed && s->poll_fd != watch_fd)))
|
|
@@ -1565,8 +1634,15 @@ int32_t nish_uring_wait_direct(struct nish_uring_set *s, int32_t watch_fd, int32
|
|
|
1565
1634
|
* enter submits them and sleeps rather than answering at once for the
|
|
1566
1635
|
* caller's pump to send them. */
|
|
1567
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
|
+
}
|
|
1568
1643
|
int32_t t = nish_uring_sleep_ms(s, timeout_ms);
|
|
1569
1644
|
if (t == 0) return 0;
|
|
1645
|
+
if (multi && (t < 0 || t > UR_DIRECT_BUSY_MS)) t = UR_DIRECT_BUSY_MS;
|
|
1570
1646
|
struct {
|
|
1571
1647
|
int64_t sec, nsec;
|
|
1572
1648
|
} ts = {t / 1000, (int64_t)(t % 1000) * 1000000};
|
|
@@ -1578,6 +1654,7 @@ int32_t nish_uring_wait_direct(struct nish_uring_set *s, int32_t watch_fd, int32
|
|
|
1578
1654
|
uint32_t want = min_complete < 1 ? 1 : (uint32_t)min_complete;
|
|
1579
1655
|
if (want > s->cq_entries) want = s->cq_entries;
|
|
1580
1656
|
s->stats.direct_waits++;
|
|
1657
|
+
r->enters++;
|
|
1581
1658
|
int rc = ur_errno(syscall(__NR_io_uring_enter, r->fd, r->to_submit, want, UR_ENTER_GETEVENTS | UR_ENTER_EXT_ARG,
|
|
1582
1659
|
&arg, sizeof arg));
|
|
1583
1660
|
if (rc > 0) r->to_submit -= (uint32_t)rc > r->to_submit ? r->to_submit : (uint32_t)rc;
|
|
@@ -1598,7 +1675,7 @@ int32_t nish_uring_poll_events(struct nish_uring_set *s, int32_t take) {
|
|
|
1598
1675
|
static struct ur_ring *ur_open_slot(struct nish_uring_set *s, int32_t ring, int32_t slot) {
|
|
1599
1676
|
if (ring < 0 || ring >= s->nrings || slot < 0 || (uint32_t)slot >= s->slots) return NULL;
|
|
1600
1677
|
struct ur_ring *r = s->rings[ring];
|
|
1601
|
-
return (r->
|
|
1678
|
+
return (r->sl[slot].st & ST_OPEN) ? r : NULL;
|
|
1602
1679
|
}
|
|
1603
1680
|
|
|
1604
1681
|
/* Put `q` on the set's list of records and behind the slot's queue, and
|
|
@@ -1609,12 +1686,12 @@ static void ur_enqueue(struct nish_uring_set *s, struct ur_ring *r, uint32_t slo
|
|
|
1609
1686
|
q->next_all = s->all;
|
|
1610
1687
|
if (s->all) s->all->prev_all = q;
|
|
1611
1688
|
s->all = q;
|
|
1612
|
-
if (r->
|
|
1613
|
-
r->
|
|
1689
|
+
if (r->sl[slot].last) {
|
|
1690
|
+
r->sl[slot].last->next = q;
|
|
1614
1691
|
} else {
|
|
1615
|
-
r->
|
|
1692
|
+
r->sl[slot].first = q;
|
|
1616
1693
|
}
|
|
1617
|
-
r->
|
|
1694
|
+
r->sl[slot].last = q;
|
|
1618
1695
|
ur_mark(r, slot);
|
|
1619
1696
|
}
|
|
1620
1697
|
|
|
@@ -1631,7 +1708,7 @@ int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, co
|
|
|
1631
1708
|
if (n < 0) return -EINVAL;
|
|
1632
1709
|
if (n == 0) return 0;
|
|
1633
1710
|
uint32_t left = (uint32_t)n, room = 0;
|
|
1634
|
-
struct ur_send *t = r->
|
|
1711
|
+
struct ur_send *t = r->sl[slot].last;
|
|
1635
1712
|
if (t && !t->in_flight && t->how < 0) {
|
|
1636
1713
|
if (UR_CHUNK_DATA - t->len < left && t->off) {
|
|
1637
1714
|
memmove(t->data, t->data + t->off, t->len - t->off);
|
|
@@ -1669,14 +1746,14 @@ int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, co
|
|
|
1669
1746
|
p += c;
|
|
1670
1747
|
left -= c;
|
|
1671
1748
|
q->slot = (uint32_t)slot;
|
|
1672
|
-
q->gen = ST_GEN(r->
|
|
1749
|
+
q->gen = ST_GEN(r->sl[slot].st);
|
|
1673
1750
|
q->off = 0;
|
|
1674
1751
|
q->len = c;
|
|
1675
1752
|
q->in_flight = 0;
|
|
1676
1753
|
q->how = -1;
|
|
1677
1754
|
ur_enqueue(s, r, (uint32_t)slot, q);
|
|
1678
1755
|
}
|
|
1679
|
-
r->
|
|
1756
|
+
r->sl[slot].qbytes += (uint32_t)n;
|
|
1680
1757
|
ur_mark(r, (uint32_t)slot);
|
|
1681
1758
|
return n;
|
|
1682
1759
|
}
|
|
@@ -1691,7 +1768,7 @@ int32_t nish_uring_shutdown(struct nish_uring_set *s, int32_t ring, int32_t slot
|
|
|
1691
1768
|
struct ur_ring *r = ur_open_slot(s, ring, slot);
|
|
1692
1769
|
if (!r) return -EBADF;
|
|
1693
1770
|
if ((uint32_t)how > 2) return -EINVAL;
|
|
1694
|
-
uint32_t *st = &r->
|
|
1771
|
+
uint32_t *st = &r->sl[slot].st;
|
|
1695
1772
|
if (!(ST_SHUT(how) & ~*st)) return 0;
|
|
1696
1773
|
struct ur_send *q = ur_record(s, r);
|
|
1697
1774
|
if (!q) return -ENOMEM;
|
|
@@ -1718,7 +1795,7 @@ int32_t nish_uring_shutdown(struct nish_uring_set *s, int32_t ring, int32_t slot
|
|
|
1718
1795
|
int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot) {
|
|
1719
1796
|
struct ur_ring *r = ur_open_slot(s, ring, slot);
|
|
1720
1797
|
if (!r) return -EBADF;
|
|
1721
|
-
const struct ur_send *h = r->
|
|
1798
|
+
const struct ur_send *h = r->sl[slot].first;
|
|
1722
1799
|
if (h && h->how >= 0) {
|
|
1723
1800
|
ur_send_head(r, (uint32_t)slot, s);
|
|
1724
1801
|
return -EAGAIN;
|
|
@@ -1731,7 +1808,7 @@ int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot) {
|
|
|
1731
1808
|
if (r->sq_local_tail - head + need > r->sq_entries) return -EAGAIN;
|
|
1732
1809
|
}
|
|
1733
1810
|
ur_send_head(r, (uint32_t)slot, s);
|
|
1734
|
-
uint32_t st = r->
|
|
1811
|
+
uint32_t st = r->sl[slot].st;
|
|
1735
1812
|
struct ur_sqe *e = ur_sqe(r);
|
|
1736
1813
|
e->opcode = UR_OP_ASYNC_CANCEL;
|
|
1737
1814
|
e->fd = slot;
|
|
@@ -1744,7 +1821,7 @@ int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot) {
|
|
|
1744
1821
|
e->flags = UR_SQE_CQE_SKIP_SUCCESS;
|
|
1745
1822
|
e->user_data = ur_tag(UR_CLOSE, st, (uint32_t)slot);
|
|
1746
1823
|
ur_drop_sends(s, r, (uint32_t)slot);
|
|
1747
|
-
r->
|
|
1824
|
+
r->sl[slot].st = (((ST_GEN(st) + 1) & GEN_MASK) << 8) | (st & ST_MARKED);
|
|
1748
1825
|
r->live--;
|
|
1749
1826
|
s->stats.live--;
|
|
1750
1827
|
ur_top_up(s);
|
|
@@ -1757,7 +1834,7 @@ int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot) {
|
|
|
1757
1834
|
int64_t nish_uring_queued(const struct nish_uring_set *s, int32_t ring, int32_t slot) {
|
|
1758
1835
|
if (ring < 0 || ring >= s->nrings || slot < 0 || (uint32_t)slot >= s->slots) return -1;
|
|
1759
1836
|
const struct ur_ring *r = s->rings[ring];
|
|
1760
|
-
return (r->
|
|
1837
|
+
return (r->sl[slot].st & ST_OPEN) ? (int64_t)r->sl[slot].qbytes : -1;
|
|
1761
1838
|
}
|
|
1762
1839
|
|
|
1763
1840
|
/* 1 when no accept is in flight and none can be: every ring's table is full
|
|
@@ -1784,9 +1861,9 @@ int32_t nish_uring_full(const struct nish_uring_set *s) {
|
|
|
1784
1861
|
int32_t nish_uring_hold(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t on) {
|
|
1785
1862
|
struct ur_ring *r = ur_open_slot(s, ring, slot);
|
|
1786
1863
|
if (!r) return -EBADF;
|
|
1787
|
-
uint32_t st = r->
|
|
1864
|
+
uint32_t st = r->sl[slot].st;
|
|
1788
1865
|
if (!on) {
|
|
1789
|
-
r->
|
|
1866
|
+
r->sl[slot].st = st & ~ST_HELD;
|
|
1790
1867
|
if ((st & (ST_ARMED | ST_PARKED)) == 0) ur_arm(r, (uint32_t)slot);
|
|
1791
1868
|
return 0;
|
|
1792
1869
|
}
|
|
@@ -1799,11 +1876,15 @@ int32_t nish_uring_hold(struct nish_uring_set *s, int32_t ring, int32_t slot, in
|
|
|
1799
1876
|
e->flags = UR_SQE_CQE_SKIP_SUCCESS;
|
|
1800
1877
|
e->user_data = ur_tag(UR_CANCEL, st, (uint32_t)slot);
|
|
1801
1878
|
}
|
|
1802
|
-
r->
|
|
1879
|
+
r->sl[slot].st = st | ST_HELD;
|
|
1803
1880
|
return 0;
|
|
1804
1881
|
}
|
|
1805
1882
|
|
|
1806
|
-
void nish_uring_stats(const struct nish_uring_set *s, struct nish_uring_stats *st) {
|
|
1883
|
+
void nish_uring_stats(const struct nish_uring_set *s, struct nish_uring_stats *st) {
|
|
1884
|
+
*st = s->stats;
|
|
1885
|
+
st->enters = 0;
|
|
1886
|
+
for (int i = 0; i < s->nrings; i++) st->enters += s->rings[i]->enters;
|
|
1887
|
+
}
|
|
1807
1888
|
|
|
1808
1889
|
/* Close every ring, which ends whatever the kernel still held, and free the
|
|
1809
1890
|
* sends it held them for. The caller's listener stays open: it is theirs. */
|