@amritk/nish-x86_64-linux 0.46.0 → 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/bin/nish +0 -0
- package/docs/AI.md +29 -18
- package/package.json +1 -1
- package/runtime/nish.h +1 -0
- package/runtime/runtime-bytes.c +167 -7
- package/runtime/runtime-net.c +69 -25
- package/runtime/runtime-uring.c +111 -62
- package/runtime/sqlite/nish-sqlite.c +166 -39
- package/std/README.md +2 -2
- 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/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 +380 -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
|
|
@@ -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
|
|
@@ -448,6 +464,7 @@ static int ur_errno(long rc) { return rc < 0 ? -errno : (int)rc; }
|
|
|
448
464
|
static int ur_setup(uint32_t entries, struct ur_params *p) { return ur_errno(UR_SYS_SETUP(entries, p)); }
|
|
449
465
|
|
|
450
466
|
static int ur_enter(struct ur_ring *r, uint32_t submit, uint32_t wait, uint32_t flags) {
|
|
467
|
+
r->enters++;
|
|
451
468
|
return ur_errno(syscall(__NR_io_uring_enter, r->fd, submit, wait, flags, NULL, 0));
|
|
452
469
|
}
|
|
453
470
|
|
|
@@ -476,6 +493,20 @@ static int ur_setup_flags(uint32_t entries, uint32_t cq, struct ur_params *p) {
|
|
|
476
493
|
return fd;
|
|
477
494
|
}
|
|
478
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
|
+
|
|
479
510
|
/* Whether the ring `fd` has every opcode the layer submits: 1, -EOPNOTSUPP
|
|
480
511
|
* when one is missing or the kernel has no probe, or the register's errno. */
|
|
481
512
|
static int32_t ur_ops_ok(int fd) {
|
|
@@ -516,7 +547,7 @@ static int32_t ur_probe_once(void) {
|
|
|
516
547
|
reg.ring_addr = (uint64_t)(uintptr_t)br;
|
|
517
548
|
reg.ring_entries = 8;
|
|
518
549
|
reg.bgid = BGID;
|
|
519
|
-
int e =
|
|
550
|
+
int e = ur_register_pbuf(fd, ®);
|
|
520
551
|
if (e < 0) rc = e == -EINVAL ? -EOPNOTSUPP : e;
|
|
521
552
|
close(fd);
|
|
522
553
|
fd = -1;
|
|
@@ -626,11 +657,11 @@ static void ur_recycle(struct nish_uring_set *s, struct ur_ring *r, uint32_t bid
|
|
|
626
657
|
* answer 0 once the kernel has handed over the reset, and a second hangup
|
|
627
658
|
* would turn the reset into a clean end (NURING-27). */
|
|
628
659
|
static void ur_arm(struct ur_ring *r, uint32_t slot) {
|
|
629
|
-
if (r->
|
|
660
|
+
if (r->sl[slot].st & ST_HUP) return;
|
|
630
661
|
struct ur_sqe *e = ur_sqe(r);
|
|
631
662
|
if (!e) {
|
|
632
|
-
if (!(r->
|
|
633
|
-
r->
|
|
663
|
+
if (!(r->sl[slot].st & ST_PARKED)) {
|
|
664
|
+
r->sl[slot].st |= ST_PARKED;
|
|
634
665
|
r->parked[r->nparked++] = slot;
|
|
635
666
|
}
|
|
636
667
|
return;
|
|
@@ -640,8 +671,8 @@ static void ur_arm(struct ur_ring *r, uint32_t slot) {
|
|
|
640
671
|
e->flags = UR_SQE_FIXED_FILE | UR_SQE_BUFFER_SELECT;
|
|
641
672
|
e->ioprio = UR_RECV_MULTISHOT;
|
|
642
673
|
e->buf_group = BGID;
|
|
643
|
-
e->user_data = ur_tag(UR_RECV, r->
|
|
644
|
-
r->
|
|
674
|
+
e->user_data = ur_tag(UR_RECV, r->sl[slot].st, slot);
|
|
675
|
+
r->sl[slot].st |= ST_ARMED;
|
|
645
676
|
}
|
|
646
677
|
|
|
647
678
|
static void ur_free_ring(struct ur_ring *r) {
|
|
@@ -660,11 +691,8 @@ static void ur_free_ring(struct ur_ring *r) {
|
|
|
660
691
|
if (r->sqes && (void *)r->sqes != MAP_FAILED) munmap(r->sqes, r->sqes_len);
|
|
661
692
|
if (r->br && (void *)r->br != MAP_FAILED) munmap(r->br, r->br_len);
|
|
662
693
|
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);
|
|
694
|
+
free(r->sl);
|
|
666
695
|
free(r->parked);
|
|
667
|
-
free(r->qbytes);
|
|
668
696
|
free(r->mlist);
|
|
669
697
|
while (r->pool) {
|
|
670
698
|
struct ur_send *q = r->pool;
|
|
@@ -803,17 +831,14 @@ static int ur_make_ring(const struct ur_sizes *z, int restricted, struct ur_ring
|
|
|
803
831
|
reg.ring_addr = (uint64_t)(uintptr_t)r->br;
|
|
804
832
|
reg.ring_entries = z->buf_count;
|
|
805
833
|
reg.bgid = BGID;
|
|
806
|
-
rc =
|
|
834
|
+
rc = ur_register_pbuf(r->fd, ®);
|
|
807
835
|
if (rc < 0) goto fail;
|
|
808
836
|
|
|
809
837
|
/* 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);
|
|
838
|
+
r->sl = calloc(z->slots, sizeof *r->sl);
|
|
813
839
|
r->parked = calloc(z->slots, sizeof *r->parked);
|
|
814
|
-
r->qbytes = calloc(z->slots, sizeof *r->qbytes);
|
|
815
840
|
r->mlist = calloc(z->slots, sizeof *r->mlist);
|
|
816
|
-
if (!r->
|
|
841
|
+
if (!r->sl || !r->parked || !r->mlist) {
|
|
817
842
|
rc = -ENOMEM;
|
|
818
843
|
goto fail;
|
|
819
844
|
}
|
|
@@ -1022,7 +1047,7 @@ static void ur_top_up(struct nish_uring_set *s) {
|
|
|
1022
1047
|
/* Submit the slot's first queued send, from where the kernel left off, or
|
|
1023
1048
|
* its shutdown. */
|
|
1024
1049
|
static void ur_send_head(struct ur_ring *r, uint32_t slot, struct nish_uring_set *s) {
|
|
1025
|
-
struct ur_send *q = r->
|
|
1050
|
+
struct ur_send *q = r->sl[slot].first;
|
|
1026
1051
|
if (!q || q->in_flight) return;
|
|
1027
1052
|
struct ur_sqe *e = ur_sqe(r);
|
|
1028
1053
|
if (!e) {
|
|
@@ -1048,8 +1073,8 @@ static void ur_send_head(struct ur_ring *r, uint32_t slot, struct nish_uring_set
|
|
|
1048
1073
|
/* Note that (r, slot) has a record to submit; the ring writes its SEND when
|
|
1049
1074
|
* it next submits. */
|
|
1050
1075
|
static void ur_mark(struct ur_ring *r, uint32_t slot) {
|
|
1051
|
-
if (r->
|
|
1052
|
-
r->
|
|
1076
|
+
if (r->sl[slot].st & ST_MARKED) return;
|
|
1077
|
+
r->sl[slot].st |= ST_MARKED;
|
|
1053
1078
|
r->mlist[r->nmarked++] = slot;
|
|
1054
1079
|
}
|
|
1055
1080
|
|
|
@@ -1061,7 +1086,7 @@ static void ur_send_marked(struct nish_uring_set *s, struct ur_ring *r) {
|
|
|
1061
1086
|
r->nmarked = 0;
|
|
1062
1087
|
for (uint32_t i = 0; i < n; i++) {
|
|
1063
1088
|
uint32_t slot = r->mlist[i];
|
|
1064
|
-
r->
|
|
1089
|
+
r->sl[slot].st &= ~ST_MARKED;
|
|
1065
1090
|
ur_send_head(r, slot, s);
|
|
1066
1091
|
}
|
|
1067
1092
|
}
|
|
@@ -1110,9 +1135,9 @@ static void ur_unlink_all(struct nish_uring_set *s, struct ur_ring *r, struct ur
|
|
|
1110
1135
|
* the all-records list; its completion finds the generation moved on and
|
|
1111
1136
|
* frees it. */
|
|
1112
1137
|
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->
|
|
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;
|
|
1116
1141
|
while (q) {
|
|
1117
1142
|
struct ur_send *next = q->next;
|
|
1118
1143
|
q->next = NULL;
|
|
@@ -1148,7 +1173,7 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
|
|
|
1148
1173
|
struct ur_send *q = (struct ur_send *)(uintptr_t)(ud & ~(uint64_t)7);
|
|
1149
1174
|
q->in_flight = 0;
|
|
1150
1175
|
uint32_t slot = q->slot;
|
|
1151
|
-
if ((r->
|
|
1176
|
+
if ((r->sl[slot].st & ST_OPEN) == 0 || ST_GEN(r->sl[slot].st) != q->gen || r->sl[slot].first != q) {
|
|
1152
1177
|
ur_unlink_all(s, r, q);
|
|
1153
1178
|
return 0;
|
|
1154
1179
|
}
|
|
@@ -1157,7 +1182,7 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
|
|
|
1157
1182
|
* already gone says nothing a recv will not, so it is not reported. */
|
|
1158
1183
|
res = 0;
|
|
1159
1184
|
}
|
|
1160
|
-
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;
|
|
1161
1186
|
if (res > 0 && q->off + (uint32_t)res < q->len) {
|
|
1162
1187
|
q->off += (uint32_t)res;
|
|
1163
1188
|
s->stats.send_requeues++;
|
|
@@ -1169,8 +1194,8 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
|
|
|
1169
1194
|
ur_event(ev, NISH_URING_SEND_FAILED, ri, slot, res, NULL);
|
|
1170
1195
|
return 1;
|
|
1171
1196
|
} else {
|
|
1172
|
-
r->
|
|
1173
|
-
if (!q->next) r->
|
|
1197
|
+
r->sl[slot].first = q->next;
|
|
1198
|
+
if (!q->next) r->sl[slot].last = NULL;
|
|
1174
1199
|
ur_unlink_all(s, r, q);
|
|
1175
1200
|
}
|
|
1176
1201
|
ur_send_head(r, slot, s);
|
|
@@ -1196,7 +1221,7 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
|
|
|
1196
1221
|
if (res >= 0 && (uint32_t)res < s->slots) {
|
|
1197
1222
|
uint32_t at = (uint32_t)res;
|
|
1198
1223
|
s->backoff_ms = 0;
|
|
1199
|
-
r->
|
|
1224
|
+
r->sl[at].st = (r->sl[at].st & ~(ST_ARMED | ST_PARKED | ST_HELD | ST_HUP)) | ST_OPEN;
|
|
1200
1225
|
r->live++;
|
|
1201
1226
|
s->stats.live++;
|
|
1202
1227
|
ur_arm(r, at);
|
|
@@ -1230,7 +1255,7 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
|
|
|
1230
1255
|
return wrote;
|
|
1231
1256
|
}
|
|
1232
1257
|
if (op != UR_RECV) return 0; /* a close or cancel; nothing to report */
|
|
1233
|
-
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;
|
|
1234
1259
|
int more = (c->flags & UR_CQE_F_MORE) != 0;
|
|
1235
1260
|
uint32_t bid = c->flags >> UR_CQE_BUFFER_SHIFT;
|
|
1236
1261
|
int has_buf = (c->flags & UR_CQE_F_BUFFER) != 0 && bid < s->buf_count;
|
|
@@ -1238,12 +1263,12 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
|
|
|
1238
1263
|
if (has_buf) ur_recycle(s, r, bid);
|
|
1239
1264
|
return 0;
|
|
1240
1265
|
}
|
|
1241
|
-
if (!more) r->
|
|
1266
|
+
if (!more) r->sl[slot].st &= ~ST_ARMED;
|
|
1242
1267
|
if (res > 0 && has_buf) {
|
|
1243
1268
|
s->lent_ring[s->nlent] = (uint32_t)ri;
|
|
1244
1269
|
s->lent_bid[s->nlent] = (uint16_t)bid;
|
|
1245
1270
|
s->nlent++;
|
|
1246
|
-
if (!more && !(r->
|
|
1271
|
+
if (!more && !(r->sl[slot].st & ST_HELD)) ur_arm(r, slot);
|
|
1247
1272
|
ur_event(ev, NISH_URING_DATA, ri, slot, res, r->bufs + (size_t)bid * s->buf_size);
|
|
1248
1273
|
return 1;
|
|
1249
1274
|
}
|
|
@@ -1252,20 +1277,20 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
|
|
|
1252
1277
|
* first). The recv stays down while the slot is held, and one let go before
|
|
1253
1278
|
* the cancel landed is armed again; a held recv out of buffers stays down. */
|
|
1254
1279
|
if (!more && res == -ECANCELED) {
|
|
1255
|
-
if (!(r->
|
|
1280
|
+
if (!(r->sl[slot].st & ST_HELD)) ur_arm(r, slot);
|
|
1256
1281
|
return 0;
|
|
1257
1282
|
}
|
|
1258
|
-
if (!more && res == -ENOBUFS && (r->
|
|
1283
|
+
if (!more && res == -ENOBUFS && (r->sl[slot].st & ST_HELD)) return 0;
|
|
1259
1284
|
if (res == -ENOBUFS) {
|
|
1260
1285
|
s->stats.nobufs_rearms++;
|
|
1261
|
-
if (!more && !(r->
|
|
1262
|
-
r->
|
|
1286
|
+
if (!more && !(r->sl[slot].st & ST_PARKED)) {
|
|
1287
|
+
r->sl[slot].st |= ST_PARKED;
|
|
1263
1288
|
r->parked[r->nparked++] = slot;
|
|
1264
1289
|
}
|
|
1265
1290
|
return 0;
|
|
1266
1291
|
}
|
|
1267
1292
|
if (more) return 0;
|
|
1268
|
-
r->
|
|
1293
|
+
r->sl[slot].st |= ST_HUP;
|
|
1269
1294
|
ur_event(ev, NISH_URING_HUP, ri, slot, res, NULL);
|
|
1270
1295
|
return 1;
|
|
1271
1296
|
}
|
|
@@ -1289,6 +1314,19 @@ static int ur_reap(struct nish_uring_set *s, int ri, struct nish_uring_event *ev
|
|
|
1289
1314
|
return n;
|
|
1290
1315
|
}
|
|
1291
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
|
+
|
|
1292
1330
|
/* Reap, submit, and enter again while the kernel says it holds more for this
|
|
1293
1331
|
* ring (behaviour 4): IORING_SQ_TASKRUN for completions DEFER_TASKRUN has not
|
|
1294
1332
|
* run, IORING_SQ_CQ_OVERFLOW for ones the CQ had no room for. A flush that
|
|
@@ -1299,14 +1337,18 @@ static int ur_reap(struct nish_uring_set *s, int ri, struct nish_uring_event *ev
|
|
|
1299
1337
|
* starts again from here. */
|
|
1300
1338
|
static int ur_service(struct nish_uring_set *s, int ri, struct nish_uring_event *ev, int cap) {
|
|
1301
1339
|
struct ur_ring *r = s->rings[ri];
|
|
1302
|
-
int n = 0;
|
|
1340
|
+
int n = 0, entered = 0;
|
|
1341
|
+
uint32_t mark = 0;
|
|
1303
1342
|
ur_send_marked(s, r);
|
|
1304
1343
|
for (int guard = 0; guard < 1 << 16; guard++) {
|
|
1305
1344
|
n += ur_reap(s, ri, ev + n, cap - n);
|
|
1306
1345
|
int rc = ur_flush(r);
|
|
1307
1346
|
if (n >= cap) break;
|
|
1347
|
+
if (entered && *r->cq_head - mark < s->taskrun_worth) break;
|
|
1308
1348
|
uint32_t flags = __atomic_load_n(r->sq_flags, __ATOMIC_ACQUIRE);
|
|
1309
1349
|
if (flags & (UR_SQ_TASKRUN | UR_SQ_CQ_OVERFLOW)) {
|
|
1350
|
+
entered++;
|
|
1351
|
+
mark = *r->cq_head;
|
|
1310
1352
|
if (flags & UR_SQ_TASKRUN) s->stats.taskrun_reentries++;
|
|
1311
1353
|
if (flags & UR_SQ_CQ_OVERFLOW) s->stats.overflow_drains++;
|
|
1312
1354
|
rc = ur_enter(r, 0, 0, UR_ENTER_GETEVENTS);
|
|
@@ -1327,7 +1369,7 @@ int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const str
|
|
|
1327
1369
|
memset(&d, 0, sizeof d);
|
|
1328
1370
|
if (o) d = *o;
|
|
1329
1371
|
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)
|
|
1372
|
+
d.buf_size < 0 || d.taskrun_worth < 0)
|
|
1331
1373
|
return -EINVAL;
|
|
1332
1374
|
if (d.slots == 0) {
|
|
1333
1375
|
struct rlimit rl;
|
|
@@ -1341,6 +1383,7 @@ int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const str
|
|
|
1341
1383
|
if (d.cq_entries < d.sq_entries) d.cq_entries = d.sq_entries;
|
|
1342
1384
|
if (d.buf_count == 0) d.buf_count = UR_BUF_COUNT_DEFAULT;
|
|
1343
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;
|
|
1344
1387
|
if (d.rings_max > 4096 || d.buf_count > 32768 || (d.buf_count & (d.buf_count - 1)) != 0 || d.buf_size > (1 << 20))
|
|
1345
1388
|
return -EINVAL;
|
|
1346
1389
|
/* A pooled ring was made at its own sizes, before the program could ask. */
|
|
@@ -1360,6 +1403,7 @@ int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const str
|
|
|
1360
1403
|
s->cq_entries = (uint32_t)d.cq_entries;
|
|
1361
1404
|
s->buf_count = (uint32_t)d.buf_count;
|
|
1362
1405
|
s->buf_size = (uint32_t)d.buf_size;
|
|
1406
|
+
s->taskrun_worth = (uint32_t)d.taskrun_worth;
|
|
1363
1407
|
s->rings = calloc((size_t)d.rings_max, sizeof *s->rings);
|
|
1364
1408
|
s->poll_fd = -1;
|
|
1365
1409
|
s->ep = epoll_create1(EPOLL_CLOEXEC);
|
|
@@ -1455,8 +1499,8 @@ static void ur_catch_up(struct nish_uring_set *s) {
|
|
|
1455
1499
|
r->nparked = 0;
|
|
1456
1500
|
for (uint32_t i = 0; i < n; i++) {
|
|
1457
1501
|
uint32_t slot = r->parked[i];
|
|
1458
|
-
r->
|
|
1459
|
-
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);
|
|
1460
1504
|
}
|
|
1461
1505
|
}
|
|
1462
1506
|
/* Scanning every slot of every ring costs rings * slots, but only after a
|
|
@@ -1466,7 +1510,7 @@ static void ur_catch_up(struct nish_uring_set *s) {
|
|
|
1466
1510
|
s->stalled_sends = 0;
|
|
1467
1511
|
for (int ri = 0; ri < s->nrings; ri++)
|
|
1468
1512
|
for (uint32_t slot = 0; slot < s->slots; slot++)
|
|
1469
|
-
if (s->rings[ri]->
|
|
1513
|
+
if (s->rings[ri]->sl[slot].first) ur_send_head(s->rings[ri], slot, s);
|
|
1470
1514
|
}
|
|
1471
1515
|
}
|
|
1472
1516
|
|
|
@@ -1578,6 +1622,7 @@ int32_t nish_uring_wait_direct(struct nish_uring_set *s, int32_t watch_fd, int32
|
|
|
1578
1622
|
uint32_t want = min_complete < 1 ? 1 : (uint32_t)min_complete;
|
|
1579
1623
|
if (want > s->cq_entries) want = s->cq_entries;
|
|
1580
1624
|
s->stats.direct_waits++;
|
|
1625
|
+
r->enters++;
|
|
1581
1626
|
int rc = ur_errno(syscall(__NR_io_uring_enter, r->fd, r->to_submit, want, UR_ENTER_GETEVENTS | UR_ENTER_EXT_ARG,
|
|
1582
1627
|
&arg, sizeof arg));
|
|
1583
1628
|
if (rc > 0) r->to_submit -= (uint32_t)rc > r->to_submit ? r->to_submit : (uint32_t)rc;
|
|
@@ -1598,7 +1643,7 @@ int32_t nish_uring_poll_events(struct nish_uring_set *s, int32_t take) {
|
|
|
1598
1643
|
static struct ur_ring *ur_open_slot(struct nish_uring_set *s, int32_t ring, int32_t slot) {
|
|
1599
1644
|
if (ring < 0 || ring >= s->nrings || slot < 0 || (uint32_t)slot >= s->slots) return NULL;
|
|
1600
1645
|
struct ur_ring *r = s->rings[ring];
|
|
1601
|
-
return (r->
|
|
1646
|
+
return (r->sl[slot].st & ST_OPEN) ? r : NULL;
|
|
1602
1647
|
}
|
|
1603
1648
|
|
|
1604
1649
|
/* Put `q` on the set's list of records and behind the slot's queue, and
|
|
@@ -1609,12 +1654,12 @@ static void ur_enqueue(struct nish_uring_set *s, struct ur_ring *r, uint32_t slo
|
|
|
1609
1654
|
q->next_all = s->all;
|
|
1610
1655
|
if (s->all) s->all->prev_all = q;
|
|
1611
1656
|
s->all = q;
|
|
1612
|
-
if (r->
|
|
1613
|
-
r->
|
|
1657
|
+
if (r->sl[slot].last) {
|
|
1658
|
+
r->sl[slot].last->next = q;
|
|
1614
1659
|
} else {
|
|
1615
|
-
r->
|
|
1660
|
+
r->sl[slot].first = q;
|
|
1616
1661
|
}
|
|
1617
|
-
r->
|
|
1662
|
+
r->sl[slot].last = q;
|
|
1618
1663
|
ur_mark(r, slot);
|
|
1619
1664
|
}
|
|
1620
1665
|
|
|
@@ -1631,7 +1676,7 @@ int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, co
|
|
|
1631
1676
|
if (n < 0) return -EINVAL;
|
|
1632
1677
|
if (n == 0) return 0;
|
|
1633
1678
|
uint32_t left = (uint32_t)n, room = 0;
|
|
1634
|
-
struct ur_send *t = r->
|
|
1679
|
+
struct ur_send *t = r->sl[slot].last;
|
|
1635
1680
|
if (t && !t->in_flight && t->how < 0) {
|
|
1636
1681
|
if (UR_CHUNK_DATA - t->len < left && t->off) {
|
|
1637
1682
|
memmove(t->data, t->data + t->off, t->len - t->off);
|
|
@@ -1669,14 +1714,14 @@ int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, co
|
|
|
1669
1714
|
p += c;
|
|
1670
1715
|
left -= c;
|
|
1671
1716
|
q->slot = (uint32_t)slot;
|
|
1672
|
-
q->gen = ST_GEN(r->
|
|
1717
|
+
q->gen = ST_GEN(r->sl[slot].st);
|
|
1673
1718
|
q->off = 0;
|
|
1674
1719
|
q->len = c;
|
|
1675
1720
|
q->in_flight = 0;
|
|
1676
1721
|
q->how = -1;
|
|
1677
1722
|
ur_enqueue(s, r, (uint32_t)slot, q);
|
|
1678
1723
|
}
|
|
1679
|
-
r->
|
|
1724
|
+
r->sl[slot].qbytes += (uint32_t)n;
|
|
1680
1725
|
ur_mark(r, (uint32_t)slot);
|
|
1681
1726
|
return n;
|
|
1682
1727
|
}
|
|
@@ -1691,7 +1736,7 @@ int32_t nish_uring_shutdown(struct nish_uring_set *s, int32_t ring, int32_t slot
|
|
|
1691
1736
|
struct ur_ring *r = ur_open_slot(s, ring, slot);
|
|
1692
1737
|
if (!r) return -EBADF;
|
|
1693
1738
|
if ((uint32_t)how > 2) return -EINVAL;
|
|
1694
|
-
uint32_t *st = &r->
|
|
1739
|
+
uint32_t *st = &r->sl[slot].st;
|
|
1695
1740
|
if (!(ST_SHUT(how) & ~*st)) return 0;
|
|
1696
1741
|
struct ur_send *q = ur_record(s, r);
|
|
1697
1742
|
if (!q) return -ENOMEM;
|
|
@@ -1718,7 +1763,7 @@ int32_t nish_uring_shutdown(struct nish_uring_set *s, int32_t ring, int32_t slot
|
|
|
1718
1763
|
int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot) {
|
|
1719
1764
|
struct ur_ring *r = ur_open_slot(s, ring, slot);
|
|
1720
1765
|
if (!r) return -EBADF;
|
|
1721
|
-
const struct ur_send *h = r->
|
|
1766
|
+
const struct ur_send *h = r->sl[slot].first;
|
|
1722
1767
|
if (h && h->how >= 0) {
|
|
1723
1768
|
ur_send_head(r, (uint32_t)slot, s);
|
|
1724
1769
|
return -EAGAIN;
|
|
@@ -1731,7 +1776,7 @@ int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot) {
|
|
|
1731
1776
|
if (r->sq_local_tail - head + need > r->sq_entries) return -EAGAIN;
|
|
1732
1777
|
}
|
|
1733
1778
|
ur_send_head(r, (uint32_t)slot, s);
|
|
1734
|
-
uint32_t st = r->
|
|
1779
|
+
uint32_t st = r->sl[slot].st;
|
|
1735
1780
|
struct ur_sqe *e = ur_sqe(r);
|
|
1736
1781
|
e->opcode = UR_OP_ASYNC_CANCEL;
|
|
1737
1782
|
e->fd = slot;
|
|
@@ -1744,7 +1789,7 @@ int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot) {
|
|
|
1744
1789
|
e->flags = UR_SQE_CQE_SKIP_SUCCESS;
|
|
1745
1790
|
e->user_data = ur_tag(UR_CLOSE, st, (uint32_t)slot);
|
|
1746
1791
|
ur_drop_sends(s, r, (uint32_t)slot);
|
|
1747
|
-
r->
|
|
1792
|
+
r->sl[slot].st = (((ST_GEN(st) + 1) & GEN_MASK) << 8) | (st & ST_MARKED);
|
|
1748
1793
|
r->live--;
|
|
1749
1794
|
s->stats.live--;
|
|
1750
1795
|
ur_top_up(s);
|
|
@@ -1757,7 +1802,7 @@ int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot) {
|
|
|
1757
1802
|
int64_t nish_uring_queued(const struct nish_uring_set *s, int32_t ring, int32_t slot) {
|
|
1758
1803
|
if (ring < 0 || ring >= s->nrings || slot < 0 || (uint32_t)slot >= s->slots) return -1;
|
|
1759
1804
|
const struct ur_ring *r = s->rings[ring];
|
|
1760
|
-
return (r->
|
|
1805
|
+
return (r->sl[slot].st & ST_OPEN) ? (int64_t)r->sl[slot].qbytes : -1;
|
|
1761
1806
|
}
|
|
1762
1807
|
|
|
1763
1808
|
/* 1 when no accept is in flight and none can be: every ring's table is full
|
|
@@ -1784,9 +1829,9 @@ int32_t nish_uring_full(const struct nish_uring_set *s) {
|
|
|
1784
1829
|
int32_t nish_uring_hold(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t on) {
|
|
1785
1830
|
struct ur_ring *r = ur_open_slot(s, ring, slot);
|
|
1786
1831
|
if (!r) return -EBADF;
|
|
1787
|
-
uint32_t st = r->
|
|
1832
|
+
uint32_t st = r->sl[slot].st;
|
|
1788
1833
|
if (!on) {
|
|
1789
|
-
r->
|
|
1834
|
+
r->sl[slot].st = st & ~ST_HELD;
|
|
1790
1835
|
if ((st & (ST_ARMED | ST_PARKED)) == 0) ur_arm(r, (uint32_t)slot);
|
|
1791
1836
|
return 0;
|
|
1792
1837
|
}
|
|
@@ -1799,11 +1844,15 @@ int32_t nish_uring_hold(struct nish_uring_set *s, int32_t ring, int32_t slot, in
|
|
|
1799
1844
|
e->flags = UR_SQE_CQE_SKIP_SUCCESS;
|
|
1800
1845
|
e->user_data = ur_tag(UR_CANCEL, st, (uint32_t)slot);
|
|
1801
1846
|
}
|
|
1802
|
-
r->
|
|
1847
|
+
r->sl[slot].st = st | ST_HELD;
|
|
1803
1848
|
return 0;
|
|
1804
1849
|
}
|
|
1805
1850
|
|
|
1806
|
-
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
|
+
}
|
|
1807
1856
|
|
|
1808
1857
|
/* Close every ring, which ends whatever the kernel still held, and free the
|
|
1809
1858
|
* sends it held them for. The caller's listener stays open: it is theirs. */
|