nish 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.
@@ -76,6 +76,9 @@ typedef int nish_net_unused;
76
76
  #if defined(__linux__)
77
77
  #include <linux/filter.h>
78
78
  #include <netinet/udp.h>
79
+ #include <poll.h>
80
+ #include <signal.h>
81
+ #include <stdio.h>
79
82
  #include <sys/epoll.h>
80
83
  #include <time.h>
81
84
  /* glibc before 2.29 names neither; the numbers are the kernel's ABI. */
@@ -284,6 +287,7 @@ struct nish_uring_opts {
284
287
  int32_t cq_entries;
285
288
  int32_t buf_count;
286
289
  int32_t buf_size;
290
+ int32_t taskrun_worth;
287
291
  };
288
292
  int32_t nish_uring_probe(void);
289
293
  int32_t nish_uring_premake(int32_t n, int32_t slots, int32_t *fds);
@@ -292,6 +296,8 @@ int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const str
292
296
  int32_t nish_uring_fd(const struct nish_uring_set *s);
293
297
  int32_t nish_uring_sleep_ms(const struct nish_uring_set *s, int32_t timeout_ms);
294
298
  int32_t nish_uring_wait(struct nish_uring_set *s, int32_t timeout_ms, struct nish_uring_event *ev, int32_t cap);
299
+ int32_t nish_uring_wait_direct(struct nish_uring_set *s, int32_t watch_fd, int32_t min_complete, int32_t timeout_ms);
300
+ int32_t nish_uring_poll_events(struct nish_uring_set *s, int32_t take);
295
301
  int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, const uint8_t *p, int32_t n);
296
302
  int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot);
297
303
  int32_t nish_uring_shutdown(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t how);
@@ -299,6 +305,13 @@ int64_t nish_uring_queued(const struct nish_uring_set *s, int32_t ring, int32_t
299
305
  int32_t nish_uring_full(const struct nish_uring_set *s);
300
306
  int32_t nish_uring_hold(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t on);
301
307
  void nish_uring_destroy(struct nish_uring_set *s);
308
+ /* runtime-uring.c's counters; the dump below prints seven of them. */
309
+ struct nish_uring_stats {
310
+ int64_t rings, live, accepting, slots, taskrun_reentries, overflow_drains, nobufs_rearms, send_requeues;
311
+ int64_t accept_errors, accept_pauses, bound_violations, max_accepting_rings, direct_waits, sends;
312
+ int64_t pool_new, pool_held, pool_cached, enters;
313
+ };
314
+ void nish_uring_stats(const struct nish_uring_set *s, struct nish_uring_stats *st);
302
315
 
303
316
  #define NR_BASE (1 << 20)
304
317
  #define NR_RINGS 64
@@ -321,6 +334,7 @@ void nish_uring_destroy(struct nish_uring_set *s);
321
334
  the list of sources that are ready, kept in the order they became so. */
322
335
  struct nr_vent {
323
336
  int32_t loop, events, token;
337
+ uint32_t seen; /* the `nr_epoch` of the batch that last reported it */
324
338
  uint8_t listed, is_listener;
325
339
  struct nr_vent *prev, *next;
326
340
  };
@@ -348,8 +362,37 @@ struct nr_set {
348
362
  uint32_t fhead, flen, fcap;
349
363
  uint32_t *pend; /* wblocked or lingering slots, checked after each pump */
350
364
  uint32_t npend, pcap;
365
+ int64_t windows, window_waits; /* batch windows, and the ring waits inside them, for the stats line */
366
+ uint8_t **rcache; /* first-size receive queues given back, reused last in, first out */
367
+ uint32_t nrcache;
351
368
  };
352
369
 
370
+ /* Receive queues of NR_RECV_FIRST bytes a set keeps for reuse. Nearly every
371
+ request is one receive into a fresh queue that its read drains and frees,
372
+ and a pass holds more of them than malloc's per-thread cache keeps, so
373
+ kept here they come back hot and cost no allocator call. */
374
+ #define NR_RCACHE 4096
375
+
376
+ /* A queue of NR_RECV_FIRST bytes: a kept one, else malloc's. */
377
+ static uint8_t *nr_rbuf_take(struct nr_set *S) {
378
+ if (S->nrcache > 0) return S->rcache[--S->nrcache];
379
+ return malloc(NR_RECV_FIRST);
380
+ }
381
+
382
+ /* `c`'s queue given back: kept when it is first-size and there is room. */
383
+ static void nr_rbuf_give(struct nr_set *S, struct nr_conn *c) {
384
+ if (c->rbuf && c->rcap == NR_RECV_FIRST) {
385
+ if (!S->rcache) S->rcache = malloc(NR_RCACHE * sizeof *S->rcache);
386
+ if (S->rcache && S->nrcache < NR_RCACHE) {
387
+ S->rcache[S->nrcache++] = c->rbuf;
388
+ c->rbuf = NULL;
389
+ return;
390
+ }
391
+ }
392
+ free(c->rbuf);
393
+ c->rbuf = NULL;
394
+ }
395
+
353
396
  static struct nr_set *nr_sets[NR_SETS];
354
397
  static int32_t nr_nsets;
355
398
  /* `netRingState()`: 0 until a listener opens a set (1) or meets a refusal. */
@@ -527,7 +570,7 @@ static void nr_dispatch(struct nr_set *S, const struct nish_uring_event *e) {
527
570
  if (c->rlen + n > c->rcap) {
528
571
  uint32_t nc = c->rcap ? c->rcap : NR_RECV_FIRST;
529
572
  while (nc < c->rlen + n) nc *= 2;
530
- uint8_t *p = realloc(c->rbuf, nc);
573
+ uint8_t *p = c->rcap == 0 && nc == NR_RECV_FIRST ? nr_rbuf_take(S) : realloc(c->rbuf, nc);
531
574
  if (!p) {
532
575
  c->hup = 1;
533
576
  c->hup_res = -12;
@@ -590,9 +633,54 @@ static void nr_pump(struct nr_set *S) {
590
633
  S->npend = keep;
591
634
  }
592
635
 
636
+ /* The stats dump. With NISH_URING_STATS set to a value other than "" or
637
+ "0" when the first ring listener opens, SIGUSR1 asks for one line per
638
+ open set on stderr, printed by the next pump of the loop, and closing a
639
+ ring listener prints its set's line before the set goes. Without it no
640
+ handler is installed and SIGUSR1 keeps its default action. The handler
641
+ stays installed once set; with no set open, a signal prints nothing.
642
+ signalFd's pipe (runtime-host.c) handles only SIGINT and SIGTERM, so it
643
+ never claims SIGUSR1. */
644
+ static int nr_stats_on = -1; /* -1 until the first open reads the knob */
645
+ static volatile sig_atomic_t nr_stats_asked;
646
+
647
+ static void nr_stats_signal(int sig) {
648
+ (void)sig;
649
+ nr_stats_asked = 1;
650
+ }
651
+
652
+ static void nr_stats_setup(void) {
653
+ if (nr_stats_on >= 0) return;
654
+ const char *v = getenv("NISH_URING_STATS");
655
+ nr_stats_on = v && v[0] && !(v[0] == '0' && !v[1]);
656
+ if (!nr_stats_on) return;
657
+ struct sigaction sa;
658
+ memset(&sa, 0, sizeof sa);
659
+ sa.sa_handler = nr_stats_signal;
660
+ sa.sa_flags = SA_RESTART;
661
+ sigemptyset(&sa.sa_mask);
662
+ if (sigaction(SIGUSR1, &sa, NULL) != 0) nr_stats_on = 0;
663
+ }
664
+
665
+ static void nr_stats_print(const struct nr_set *S, const char *why) {
666
+ struct nish_uring_stats st;
667
+ nish_uring_stats(S->u, &st);
668
+ dprintf(2,
669
+ "uring-stats %s port=%d sends=%lld pool_new=%lld pool_held=%lld pool_cached=%lld live=%lld slots=%lld "
670
+ "enters=%lld windows=%lld window_waits=%lld\n",
671
+ why, (int)S->port, (long long)st.sends, (long long)st.pool_new, (long long)st.pool_held,
672
+ (long long)st.pool_cached, (long long)st.live, (long long)st.slots, (long long)st.enters,
673
+ (long long)S->windows, (long long)S->window_waits);
674
+ }
675
+
593
676
  static void nr_pump_all(void) {
594
677
  for (int32_t i = 0; i < nr_nsets; i++)
595
678
  if (!nr_sets[i]->dead) nr_pump(nr_sets[i]);
679
+ if (nr_stats_asked) {
680
+ nr_stats_asked = 0;
681
+ for (int32_t i = 0; i < nr_nsets; i++)
682
+ if (!nr_sets[i]->dead) nr_stats_print(nr_sets[i], "signal");
683
+ }
596
684
  }
597
685
 
598
686
  static struct nr_set *nr_listener(int32_t fd) {
@@ -650,6 +738,7 @@ static int32_t nr_open(int32_t fd, int32_t port) {
650
738
  /* Only an open set decides for rings: a probe that passed before a set
651
739
  that ran short leaves the state at 0 (NURING-26). */
652
740
  nr_state = 1;
741
+ nr_stats_setup();
653
742
  S->listener = fd;
654
743
  S->port = port;
655
744
  S->slots = o.slots;
@@ -663,6 +752,7 @@ static int32_t nr_open(int32_t fd, int32_t port) {
663
752
  /* Closes a ring listener: the set and every slot in it, then the descriptor.
664
753
  The record stays, dead, so its handles answer -9. */
665
754
  static int32_t nr_close_listener(struct nr_set *S) {
755
+ if (nr_stats_on > 0) nr_stats_print(S, "close");
666
756
  nr_unregister(&S->v);
667
757
  for (int ri = 0; ri < NR_RINGS; ri++) {
668
758
  if (!S->conns[ri]) continue;
@@ -678,6 +768,10 @@ static int32_t nr_close_listener(struct nr_set *S) {
678
768
  free(S->fifo);
679
769
  free(S->pend);
680
770
  S->fifo = S->pend = NULL;
771
+ for (uint32_t i = 0; i < S->nrcache; i++) free(S->rcache[i]);
772
+ free(S->rcache);
773
+ S->rcache = NULL;
774
+ S->nrcache = 0;
681
775
  S->dead = 1;
682
776
  return close(S->listener) != 0 ? nish_net_fail() : 0;
683
777
  }
@@ -719,9 +813,13 @@ static int32_t nr_read(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_
719
813
  /* A drained queue is freed and emptied, so an idle slot holds none
720
814
  (NURING-38) and a receive never finds roff at rlen past 0. */
721
815
  if (c->roff == c->rlen) {
722
- free(c->rbuf);
723
- c->rbuf = NULL;
816
+ nr_rbuf_give(S, c);
724
817
  c->rcap = c->roff = c->rlen = 0;
818
+ /* Drained, the slot is not ready, so it leaves the ready list now,
819
+ while its entry is in cache, rather than when the next collect
820
+ walks to it to find that out (one cache miss a request at
821
+ 150,000 connections). */
822
+ if (c->v.listed && !(nr_bits(&c->v) & (c->v.events | 4))) nr_unlist(&c->v);
725
823
  }
726
824
  return (int32_t)n;
727
825
  }
@@ -780,8 +878,7 @@ static int32_t nr_close(struct nr_set *S, struct nr_conn *c, int32_t ring, int32
780
878
  nr_unregister(&c->v);
781
879
  c->gen++;
782
880
  c->roff = c->rlen = 0;
783
- free(c->rbuf);
784
- c->rbuf = NULL;
881
+ nr_rbuf_give(S, c);
785
882
  c->rcap = 0;
786
883
  int64_t q = nish_uring_queued(S->u, ring, slot);
787
884
  /* A peer that only stopped sending (a FIN) still takes what is queued, so
@@ -878,17 +975,23 @@ static int32_t nr_ctl(int32_t loop, int op, struct nr_vent *v, struct nr_set *S,
878
975
  /* Writes the loop's ready virtual sources into `out`, at most `max`, in the
879
976
  order they became ready. One reported and still ready goes to the tail,
880
977
  so a busy source does not starve the rest. */
881
- static int nr_collect(int32_t loop, int32_t *out, int max) {
978
+ static uint32_t nr_epoch;
979
+
980
+ static int nr_collect_from(int32_t loop, int32_t *out, int max, int skip_seen) {
882
981
  int n = 0;
883
982
  struct nr_vent *again = NULL, *again_tail = NULL;
884
983
  for (struct nr_vent *v = nr_head, *next; v && n < max; v = next) {
885
984
  next = v->next;
886
985
  if (v->loop != loop) continue;
986
+ /* A batch's second wait passes over what its first already reported,
987
+ which stays listed for the next call. */
988
+ if (skip_seen && v->seen == nr_epoch) continue;
887
989
  int32_t bits = nr_bits(v) & (v->events | 4);
888
990
  nr_unlist(v);
889
991
  if (bits == 0) continue;
890
992
  out[2 * n] = v->token;
891
993
  out[2 * n + 1] = bits;
994
+ v->seen = nr_epoch;
892
995
  n++;
893
996
  v->next = NULL;
894
997
  if (again_tail) again_tail->next = v; else again = v;
@@ -901,6 +1004,8 @@ static int nr_collect(int32_t loop, int32_t *out, int max) {
901
1004
  return n;
902
1005
  }
903
1006
 
1007
+ static int nr_collect(int32_t loop, int32_t *out, int max) { return nr_collect_from(loop, out, max, 0); }
1008
+
904
1009
  static int64_t nr_now_ms(void) {
905
1010
  struct timespec t;
906
1011
  clock_gettime(CLOCK_MONOTONIC, &t);
@@ -910,6 +1015,72 @@ static int64_t nr_now_ms(void) {
910
1015
  /* Whose turn a full wait with room for one gives: 1 the epoll set's. */
911
1016
  static int nr_turn;
912
1017
 
1018
+ /* The ordinary descriptors each loop watches, as `pollAdd` and `pollModify`
1019
+ left them, so a wait can tell a loop that holds nothing but ring slots and
1020
+ `signalFd()` from one that must sleep in epoll_wait. A close without
1021
+ `pollRemove` may leave an entry behind, which only keeps that loop on
1022
+ epoll; past NR_FDS entries no loop waits on its ring directly. */
1023
+ #define NR_FDS 64
1024
+ static struct {
1025
+ int32_t loop, fd, events;
1026
+ } nr_fds[NR_FDS];
1027
+ static int32_t nr_nfds, nr_fds_spilled;
1028
+
1029
+ static void nr_fd_note(int32_t loop, int op, int32_t fd, int32_t events) {
1030
+ int32_t at = -1;
1031
+ for (int32_t i = 0; i < nr_nfds; i++)
1032
+ if (nr_fds[i].loop == loop && nr_fds[i].fd == fd) at = i;
1033
+ if (op == EPOLL_CTL_DEL) {
1034
+ if (at >= 0) nr_fds[at] = nr_fds[--nr_nfds];
1035
+ return;
1036
+ }
1037
+ if (at < 0) {
1038
+ if (nr_nfds == NR_FDS) {
1039
+ nr_fds_spilled = 1;
1040
+ return;
1041
+ }
1042
+ at = nr_nfds++;
1043
+ nr_fds[at].loop = loop;
1044
+ nr_fds[at].fd = fd;
1045
+ }
1046
+ nr_fds[at].events = events;
1047
+ }
1048
+
1049
+ /* `fd` is closed or new: it watches nothing and is in no loop. */
1050
+ static void nr_fd_forget(int32_t fd) {
1051
+ for (int32_t i = 0; i < nr_nfds;)
1052
+ if (nr_fds[i].loop == fd || nr_fds[i].fd == fd) nr_fds[i] = nr_fds[--nr_nfds];
1053
+ else i++;
1054
+ }
1055
+
1056
+ /* The set a wait on `loop` may sleep on directly, in io_uring_enter rather
1057
+ than epoll_wait, and in `*watch` the one descriptor its ring must watch
1058
+ for it, or -1: there is one live set, no other loop holds a virtual
1059
+ source, and the loop's only ordinary descriptor, if any, is `signalFd()`
1060
+ watched for reading. NULL otherwise, and the wait uses epoll. Once
1061
+ `netClose` or `fileClose` has closed the signal descriptor its number may
1062
+ name a new socket, which the ring's poll, still armed on the old pipe,
1063
+ would never report; `nish_signal_fd_made` answers -1 from then on, so no
1064
+ loop with an ordinary descriptor waits directly again. */
1065
+
1066
+ static struct nr_set *nr_direct(int32_t loop, int32_t *watch) {
1067
+ if (nr_fds_spilled || nr_loops_spilled || nr_nloops != 1 || nr_loops[0] != loop) return NULL;
1068
+ struct nr_set *D = NULL;
1069
+ for (int32_t i = 0; i < nr_nsets; i++) {
1070
+ if (nr_sets[i]->dead) continue;
1071
+ if (D) return NULL;
1072
+ D = nr_sets[i];
1073
+ }
1074
+ *watch = -1;
1075
+ for (int32_t i = 0; i < nr_nfds; i++) {
1076
+ if (nr_fds[i].loop != loop) continue;
1077
+ if (*watch >= 0 || nr_fds[i].fd != nish_signal_fd_made() || nr_fds[i].events != 1)
1078
+ return NULL;
1079
+ *watch = nr_fds[i].fd;
1080
+ }
1081
+ return D;
1082
+ }
1083
+
913
1084
  /* `pollWait` for a loop that may hold virtual sources: pump every set, take
914
1085
  what is ready, and sleep on the loop's epoll set, which holds each set's
915
1086
  descriptor, no longer than the sets allow (NURING-8). A wake by the bound,
@@ -933,8 +1104,24 @@ static int32_t nr_poll_wait(int32_t loop, int32_t *out, int max, int32_t timeout
933
1104
  int room = max - n;
934
1105
  if (room == 0) room = max > 1 ? max / 2 : (nr_turn ^= 1);
935
1106
  struct epoll_event e[NR_EVENTS];
936
- int k = room > 0 ? epoll_wait(loop, e, room, t) : 0;
937
- int woke = 0;
1107
+ int woke = 0, k = -2, watch;
1108
+ /* A loop of ring slots and at most `signalFd()` sleeps on its ring, and
1109
+ reads its epoll set only once the ring's poll says the signal
1110
+ descriptor is ready; that read takes the place of NURING-18's share. */
1111
+ struct nr_set *D = room > 0 ? nr_direct(loop, &watch) : NULL;
1112
+ if (D) {
1113
+ int rc = nish_uring_wait_direct(D->u, watch, 1, t);
1114
+ if (rc != -EOPNOTSUPP) {
1115
+ if (rc < 0) {
1116
+ errno = -rc;
1117
+ return nish_net_fail();
1118
+ }
1119
+ nr_pump(D);
1120
+ woke = 1;
1121
+ k = watch >= 0 && nish_uring_poll_events(D->u, 1) ? epoll_wait(loop, e, room, 0) : 0;
1122
+ }
1123
+ }
1124
+ if (k == -2) k = room > 0 ? epoll_wait(loop, e, room, t) : 0;
938
1125
  /* io_uring's task work interrupts the wait with no signal behind it, so
939
1126
  EINTR is a wake to pump for, not an answer of 0 before the timeout. */
940
1127
  if (k < 0 && errno != EINTR) return nish_net_fail();
@@ -967,6 +1154,55 @@ static int32_t nr_poll_wait(int32_t loop, int32_t *out, int max, int32_t timeout
967
1154
  }
968
1155
  #endif
969
1156
 
1157
+ #if defined(__linux__)
1158
+ /* A batch's second wait: `n` pairs are in `out`, and it sleeps on the one
1159
+ live set, up to `max_wait_ms`, until `min` are ready or `max` fill it. It
1160
+ sleeps in at most eight slices, each an eighth of `max_wait_ms` rounded
1161
+ up, without entering the ring, and after each slice pumps the set once and
1162
+ collects once, so a window costs at most eight pumps however its
1163
+ completions trickle in: a wait on the ring itself would wake, run its
1164
+ deferred task work and pump once per completion, sends' included. Its
1165
+ `min` is checked at the end of a slice, so a window can outlast the moment
1166
+ `min` became ready by up to one slice. It reads no epoll set, so an
1167
+ ordinary descriptor that becomes ready meanwhile waits for the next call,
1168
+ at most `max_wait_ms`; on a direct loop the slice's poll(2) on
1169
+ `signalFd()` ends the batch as soon as it answers, and the next call
1170
+ reports it. */
1171
+ static int nr_batch(int32_t loop, int32_t *out, int n, int max, int32_t min, int32_t max_wait_ms) {
1172
+ struct nr_set *S = NULL;
1173
+ for (int32_t i = 0; i < nr_nsets; i++) {
1174
+ if (nr_sets[i]->dead) continue;
1175
+ if (S) return n; /* a batch waits on one ring set, never on several */
1176
+ S = nr_sets[i];
1177
+ }
1178
+ if (!S) return n;
1179
+ /* A loop no virtual source has joined has nothing the ring can add, so it
1180
+ answers as `pollWait` did rather than sleep out `max_wait_ms`. */
1181
+ int held = nr_loops_spilled;
1182
+ for (int32_t i = 0; i < nr_nloops; i++)
1183
+ if (nr_loops[i] == loop) held = 1;
1184
+ if (!held) return n;
1185
+ int32_t watch = -1;
1186
+ int direct = nr_direct(loop, &watch) == S;
1187
+ int64_t end = nr_now_ms() + max_wait_ms;
1188
+ int64_t slice = (max_wait_ms + 7) / 8;
1189
+ S->windows++;
1190
+ while (n < min && n < max) {
1191
+ int64_t left = end - nr_now_ms();
1192
+ if (left <= 0) break;
1193
+ S->window_waits++;
1194
+ /* poll(2) ignores a negative descriptor, so with no watch this sleeps. */
1195
+ struct pollfd p = {direct ? watch : -1, POLLIN, 0};
1196
+ int woke = poll(&p, 1, (int)(left < slice ? left : slice)) > 0;
1197
+ nr_pump(S);
1198
+ n += nr_collect_from(loop, out + 2 * n, max - n, 1);
1199
+ if (woke) break;
1200
+ }
1201
+ return n;
1202
+ }
1203
+ #endif
1204
+
1205
+
970
1206
  /* `netRingState()`: whether `tcpListenFlags` flag 8 gets rings. 0 before a
971
1207
  listener has asked, 1 when it got them, or the negative errno that refused
972
1208
  them, after which every listener in the process is a descriptor one: -1
@@ -1339,6 +1575,8 @@ int32_t nish_net_close(int32_t fd) {
1339
1575
  struct nr_set *R = nr_nsets > 0 ? nr_listener(fd) : NULL;
1340
1576
  if (R) return nr_close_listener(R);
1341
1577
  if (nr_nsets > 0) nr_forget_loop(fd);
1578
+ nr_fd_forget(fd);
1579
+ nish_signal_fd_closing(fd);
1342
1580
  #endif
1343
1581
  return close(fd) != 0 ? nish_net_fail() : 0;
1344
1582
  }
@@ -1574,6 +1812,7 @@ int32_t nish_poll_create(void) {
1574
1812
  nish_net_raise_nofile();
1575
1813
  #if defined(__linux__)
1576
1814
  int fd = epoll_create1(EPOLL_CLOEXEC);
1815
+ if (fd >= 0) nr_fd_forget(fd);
1577
1816
  #else
1578
1817
  int fd = kqueue();
1579
1818
  if (fd >= 0) fcntl(fd, F_SETFD, FD_CLOEXEC);
@@ -1619,7 +1858,9 @@ static int32_t nish_poll_ctl(int32_t loop, int op, int32_t fd, int32_t events, i
1619
1858
  e.events = ((events & 1) ? EPOLLIN : 0) | ((events & 2) ? EPOLLOUT : 0);
1620
1859
  /* The high word is 0 for a descriptor, and 1 for a ring set's own. */
1621
1860
  e.data.u64 = (uint32_t)token;
1622
- return epoll_ctl(loop, op, fd, &e) != 0 ? nish_net_fail() : 0;
1861
+ if (epoll_ctl(loop, op, fd, &e) != 0) return nish_net_fail();
1862
+ nr_fd_note(loop, op, fd, events);
1863
+ return 0;
1623
1864
  #else
1624
1865
  int r, w;
1625
1866
  if (op == NISH_POLL_ADD) {
@@ -1709,6 +1950,28 @@ int32_t nish_poll_wait(int32_t loop, nish_array *ready, int32_t timeout_ms) {
1709
1950
  #endif
1710
1951
  }
1711
1952
 
1953
+ /* `pollWaitBatch(loop, ready, timeoutMs, minEvents, maxWaitMs)`: wait as
1954
+ `pollWait` does for the first event, then, on a loop with one ring set,
1955
+ up to `maxWaitMs` more until `minEvents` are ready (nr_batch). */
1956
+ int32_t nish_poll_wait_batch(int32_t loop, nish_array *ready, int32_t timeout_ms, int32_t min_events,
1957
+ int32_t max_wait_ms) {
1958
+ #if defined(__linux__)
1959
+ uint64_t room = ready->len / 2;
1960
+ if (nr_nsets > 0 && min_events > 1 && max_wait_ms > 0 && room > 0) {
1961
+ int max = room < NISH_POLL_CHUNK ? (int)room : NISH_POLL_CHUNK;
1962
+ int32_t *out = (int32_t *)ready->data;
1963
+ nr_epoch++;
1964
+ int n = nr_poll_wait(loop, out, max, timeout_ms);
1965
+ if (n <= 0 || n >= min_events || n >= max) return n;
1966
+ return nr_batch(loop, out, n, max, min_events, max_wait_ms);
1967
+ }
1968
+ #else
1969
+ (void)min_events;
1970
+ (void)max_wait_ms;
1971
+ #endif
1972
+ return nish_poll_wait(loop, ready, timeout_ms);
1973
+ }
1974
+
1712
1975
  /* The Unix-domain address `path` names into `s`, its length into `n`, and 1
1713
1976
  or 0 for whether it is abstract; or the failure, before any socket exists.
1714
1977
  An empty path or one with a NUL inside is -22, and so is a bare `@`. A path
@@ -324,9 +324,23 @@ int64_t nish_file_size(int32_t fd) {
324
324
  return fstat(fd, &st) == 0 ? (int64_t)st.st_size : nish_fs_err(errno);
325
325
  }
326
326
 
327
+ #if !defined(__wasi__) && !defined(__wasm__)
328
+ /* Weak, so that runtime.c, runtime-text.c and runtime-os.c still link as a set
329
+ of their own: runtime-host.c defines it, and is linked wherever `signalFd()`
330
+ can have made the pipe this hook watches for. */
331
+ extern void nish_signal_fd_closing(int32_t fd) __attribute__((weak));
332
+ #endif
333
+
327
334
  /* `fileClose(fd)`: never retried on `EINTR`, because Linux has released the
328
335
  number by then and a retry could close a descriptor opened since. */
329
- int32_t nish_file_close(int32_t fd) { return fd < 0 ? -9 : close(fd) == 0 ? 0 : nish_fs_err(errno); }
336
+ int32_t nish_file_close(int32_t fd) {
337
+ if (fd < 0) return -9;
338
+ #if !defined(__wasi__) && !defined(__wasm__)
339
+ /* The signal pipe's number is about to be free for reuse (NURING-49). */
340
+ if (nish_signal_fd_closing) nish_signal_fd_closing(fd);
341
+ #endif
342
+ return close(fd) == 0 ? 0 : nish_fs_err(errno);
343
+ }
330
344
 
331
345
  /* ---- Files and directories with an explicit mode (docs/security/runtime.md,
332
346
  RT-24 to RT-26): `writeFileSyncMode`, `createFileExclusiveSync` and