@amritk/nish-aarch64-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/bin/nish
CHANGED
|
Binary file
|
package/docs/AI.md
CHANGED
|
@@ -1601,11 +1601,12 @@ export const main = (): i32 => {
|
|
|
1601
1601
|
};
|
|
1602
1602
|
```
|
|
1603
1603
|
|
|
1604
|
-
- **A parameter is a scalar, a `CPtr`, a borrowed `string`, a
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1604
|
+
- **A parameter is a scalar, a `CPtr`, a borrowed `string`, a buffer with its
|
|
1605
|
+
count or a borrowed `declare class`; a return is a scalar, a `CPtr` or a
|
|
1606
|
+
copied `string`.** The scalars are the integer and float widths, `boolean`
|
|
1607
|
+
and `void`. A buffer is a `u8[]`, `u16[]`, `u32[]`, `i32[]` or `f32[]`,
|
|
1608
|
+
readonly or not. Any other array, a Nish class or an interface is rejected,
|
|
1609
|
+
in either position, and a `declare class` is never a return.
|
|
1609
1610
|
- **A `declare class` is a C struct, lent by pointer.** Its fields are laid out
|
|
1610
1611
|
in declaration order at C's alignment, and `new Name()` zero-fills it. A
|
|
1611
1612
|
field is `i32`, `i64`, `u8`, `u16`, `u32`, `u64`, `f32`, `f64`, `CPtr`,
|
|
@@ -1646,16 +1647,21 @@ export const main = (): i32 => {
|
|
|
1646
1647
|
};
|
|
1647
1648
|
```
|
|
1648
1649
|
|
|
1649
|
-
- **A run of
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1650
|
+
- **A run of buffer parameters is followed by exactly one `u64`**, the
|
|
1651
|
+
count of elements C may touch in each (`memcpy(dst: u8[], src: readonly
|
|
1652
|
+
u8[], n: u64)`, `scale(xs: f32[], n: u64, k: f32)`). The five element types
|
|
1653
|
+
are `u8`, `u16`, `u32`, `i32` and `f32` (C's `uint8_t`, `uint16_t`,
|
|
1654
|
+
`uint32_t`, `int32_t`, `float`); the count is in elements, so 4 for an
|
|
1655
|
+
`f32[]` of 4 is 16 bytes. `readonly` is C's `const`. A run without its count
|
|
1656
|
+
is NL2549, a count that is not `u64` NL2550, any other element (`f64[]`,
|
|
1657
|
+
`i64[]`) NL2551, `u8[] | null` NL2552, and a run that mixes element types
|
|
1658
|
+
(`f32[]` then `readonly u8[]`) NL2824: give each its own count. Declare
|
|
1659
|
+
`fwrite` as `(buf: readonly u8[], n: u64, one: u64, f: CPtr)` and pass `1`
|
|
1660
|
+
for `one`. A call passes the data pointer and first checks the count against
|
|
1661
|
+
every buffer of the run: a larger count is an `ffi-length` panic, and only
|
|
1662
|
+
`toU64(buf.length)` of the buffer itself is proved. C borrows the elements
|
|
1663
|
+
for the call and may write a buffer that is not `readonly`; a spawned task
|
|
1664
|
+
may not make the call.
|
|
1659
1665
|
|
|
1660
1666
|
<!-- nish:err NL2549 -->
|
|
1661
1667
|
```ts
|
|
@@ -3073,8 +3079,7 @@ tag under one key (NL2721) as a different string (NL2720). A `tag` on a class
|
|
|
3073
3079
|
no union holds does nothing, and a JSON call on that class refuses its
|
|
3074
3080
|
enum-member field.
|
|
3075
3081
|
|
|
3076
|
-
<!--
|
|
3077
|
-
<!-- nish:ok --allow-panics -->
|
|
3082
|
+
<!-- nish:ok -->
|
|
3078
3083
|
```ts
|
|
3079
3084
|
import { JsonWriter } from "nish/json";
|
|
3080
3085
|
import { JsonLimits } from "nish/json-tokenizer";
|
|
@@ -3546,7 +3551,13 @@ its fix, and there are five:
|
|
|
3546
3551
|
`--number-mode f64` — has no guard, so index with an `i32` or a `u32`),
|
|
3547
3552
|
`if (d !== 0 && d !== -1)` before `a / d` (a
|
|
3548
3553
|
constant divisor needs nothing), `if (xs.length > 0)` before `xs.pop()`,
|
|
3549
|
-
`if (n >= 0 && n <= 9)` before `n` enters `integer<0, 9
|
|
3554
|
+
`if (n >= 0 && n <= 9)` before `n` enters `integer<0, 9>`, `if (n >= 0)`
|
|
3555
|
+
before `new Array<T>(n)` of an `i32` (a local of `integer<0, N>` needs
|
|
3556
|
+
nothing), `if (start >= 0 && start <= end && end <= bytes.length)` before
|
|
3557
|
+
`s.slice(start, end)`, `xs.slice(start, end)` or
|
|
3558
|
+
`String.fromBytes(bytes, start, end)`, `if (src.length <= dst.length)`
|
|
3559
|
+
before `dst.set(src, 0)`, and `if (n >= 0 && n <= buf.length)` before
|
|
3560
|
+
`writeBytes(buf, 0, n)` or `netRead(fd, buf, 0, n)`.
|
|
3550
3561
|
- **Bound a signed `+ - *`, or say it wraps**: an `i32`/`i64` operation the
|
|
3551
3562
|
checker cannot prove fits is an `overflow` site; bound its operands (a loop
|
|
3552
3563
|
below a length, a guard on the value), compute in `u32`/`u64`, or write
|
package/package.json
CHANGED
package/runtime/nish.h
CHANGED
|
@@ -303,6 +303,7 @@ int nish_f64_format(double v, char out[NISH_F64_MAX]);
|
|
|
303
303
|
end, or -1, with nothing written, when `at < 0` or they do not fit before
|
|
304
304
|
`len`. `buf` may be null only when nothing fits. */
|
|
305
305
|
int32_t nish_json_put_f64(uint8_t *buf, int32_t len, int32_t at, double v);
|
|
306
|
+
int32_t nish_json_put_string(uint8_t *buf, int32_t len, int32_t at, const uint8_t *src, int64_t from, int64_t to);
|
|
306
307
|
nish_str *nish_str_from_i64(int64_t v);
|
|
307
308
|
/* Unsigned decimal (WP15). u8/u16/u32 are zero-extended by the caller, so one
|
|
308
309
|
symbol serves every unsigned width. */
|
package/runtime/runtime-bytes.c
CHANGED
|
@@ -247,14 +247,28 @@ static inline unsigned nish_run_stops_sse2(int cls, __m128i v) {
|
|
|
247
247
|
return (unsigned)_mm_movemask_epi8(_mm_or_si128(stop, v));
|
|
248
248
|
}
|
|
249
249
|
|
|
250
|
+
static inline unsigned nish_plain_stops_sse2(const uint8_t *p) {
|
|
251
|
+
__m128i v = _mm_loadu_si128((const __m128i *)p);
|
|
252
|
+
__m128i stop = _mm_or_si128(_mm_or_si128(_mm_cmpeq_epi8(v, _mm_set1_epi8(0x22)), _mm_cmpeq_epi8(v, _mm_set1_epi8(0x5c))),
|
|
253
|
+
_mm_cmpeq_epi8(_mm_min_epu8(v, _mm_set1_epi8(0x1f)), v));
|
|
254
|
+
return (unsigned)_mm_movemask_epi8(stop);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/* A span of 16 bytes or more ends with one block that overlaps the last one
|
|
258
|
+
rather than a byte at a time: it lies inside the span, and the bytes it
|
|
259
|
+
shares with the blocks already read are masked off, so a stop among them
|
|
260
|
+
is not found twice. */
|
|
250
261
|
static int64_t nish_plain_base(int cls, const uint8_t *p, int64_t i, int64_t to) {
|
|
262
|
+
int64_t from = i;
|
|
251
263
|
for (; to - i >= 16; i += 16) {
|
|
252
|
-
|
|
253
|
-
__m128i stop = _mm_or_si128(_mm_or_si128(_mm_cmpeq_epi8(v, _mm_set1_epi8(0x22)), _mm_cmpeq_epi8(v, _mm_set1_epi8(0x5c))),
|
|
254
|
-
_mm_cmpeq_epi8(_mm_min_epu8(v, _mm_set1_epi8(0x1f)), v));
|
|
255
|
-
unsigned m = (unsigned)_mm_movemask_epi8(stop);
|
|
264
|
+
unsigned m = nish_plain_stops_sse2(p + i);
|
|
256
265
|
if (m != 0) return i + __builtin_ctz(m);
|
|
257
266
|
}
|
|
267
|
+
if (i < to && to - from >= 16) {
|
|
268
|
+
int64_t at = to - 16;
|
|
269
|
+
unsigned m = nish_plain_stops_sse2(p + at) & (~0u << (i - at));
|
|
270
|
+
return m != 0 ? at + __builtin_ctz(m) : to;
|
|
271
|
+
}
|
|
258
272
|
return nish_plain_scalar(cls, p, i, to);
|
|
259
273
|
}
|
|
260
274
|
|
|
@@ -357,13 +371,24 @@ static inline uint64_t nish_run_stops_neon(int cls, uint8x16_t v) {
|
|
|
357
371
|
return vget_lane_u64(vreinterpret_u64_u8(vshrn_n_u16(vreinterpretq_u16_u8(stop), 4)), 0);
|
|
358
372
|
}
|
|
359
373
|
|
|
374
|
+
static inline uint64_t nish_plain_stops_neon(const uint8_t *p) {
|
|
375
|
+
uint8x16_t v = vld1q_u8(p);
|
|
376
|
+
uint8x16_t stop = vorrq_u8(vorrq_u8(vceqq_u8(v, vdupq_n_u8(0x22)), vceqq_u8(v, vdupq_n_u8(0x5c))), vcleq_u8(v, vdupq_n_u8(0x1f)));
|
|
377
|
+
return vget_lane_u64(vreinterpret_u64_u8(vshrn_n_u16(vreinterpretq_u16_u8(stop), 4)), 0);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/* The SSE2 path's overlapping last block, four mask bits a byte. */
|
|
360
381
|
static int64_t nish_plain_base(int cls, const uint8_t *p, int64_t i, int64_t to) {
|
|
382
|
+
int64_t from = i;
|
|
361
383
|
for (; to - i >= 16; i += 16) {
|
|
362
|
-
|
|
363
|
-
uint8x16_t stop = vorrq_u8(vorrq_u8(vceqq_u8(v, vdupq_n_u8(0x22)), vceqq_u8(v, vdupq_n_u8(0x5c))), vcleq_u8(v, vdupq_n_u8(0x1f)));
|
|
364
|
-
uint64_t m = vget_lane_u64(vreinterpret_u64_u8(vshrn_n_u16(vreinterpretq_u16_u8(stop), 4)), 0);
|
|
384
|
+
uint64_t m = nish_plain_stops_neon(p + i);
|
|
365
385
|
if (m != 0) return i + (int64_t)(__builtin_ctzll(m) >> 2);
|
|
366
386
|
}
|
|
387
|
+
if (i < to && to - from >= 16) {
|
|
388
|
+
int64_t at = to - 16;
|
|
389
|
+
uint64_t m = nish_plain_stops_neon(p + at) & (~0ull << (4 * (i - at)));
|
|
390
|
+
return m != 0 ? at + (int64_t)(__builtin_ctzll(m) >> 2) : to;
|
|
391
|
+
}
|
|
367
392
|
return nish_plain_scalar(cls, p, i, to);
|
|
368
393
|
}
|
|
369
394
|
|
|
@@ -483,3 +508,138 @@ int64_t nish_bytes_skip_json_text(const uint8_t *p, int64_t from, int64_t to) {
|
|
|
483
508
|
int64_t nish_bytes_skip_json_plain(const uint8_t *p, int64_t from, int64_t to) {
|
|
484
509
|
return NISH_PLAIN_PATH(NISH_RUN_PLAIN, p, from, to);
|
|
485
510
|
}
|
|
511
|
+
|
|
512
|
+
/* One byte JSON escapes, at `buf[o]`: `\"`, `\\`, the five short escapes, or
|
|
513
|
+
`\u00XX` in lower-case hex, as std/json.ts's `jsonShortEscape` and
|
|
514
|
+
`jsonHexByte` spell them. Answers the new end, or -1 when it does not fit
|
|
515
|
+
before `cap`. */
|
|
516
|
+
static int64_t nish_json_escape_byte(uint8_t *buf, int64_t o, int64_t cap, uint8_t c) {
|
|
517
|
+
uint8_t letter = c == 0x22 || c == 0x5c ? c
|
|
518
|
+
: c == 0x0a ? 'n'
|
|
519
|
+
: c == 0x09 ? 't'
|
|
520
|
+
: c == 0x0d ? 'r'
|
|
521
|
+
: c == 0x08 ? 'b'
|
|
522
|
+
: c == 0x0c ? 'f'
|
|
523
|
+
: 0;
|
|
524
|
+
if (letter) {
|
|
525
|
+
if (cap - o < 2) return -1;
|
|
526
|
+
buf[o] = 0x5c;
|
|
527
|
+
buf[o + 1] = letter;
|
|
528
|
+
return o + 2;
|
|
529
|
+
}
|
|
530
|
+
if (cap - o < 6) return -1;
|
|
531
|
+
static const char hex[] = "0123456789abcdef";
|
|
532
|
+
buf[o] = 0x5c;
|
|
533
|
+
buf[o + 1] = 'u';
|
|
534
|
+
buf[o + 2] = '0';
|
|
535
|
+
buf[o + 3] = '0';
|
|
536
|
+
buf[o + 4] = (uint8_t)hex[c >> 4];
|
|
537
|
+
buf[o + 5] = (uint8_t)hex[c & 15];
|
|
538
|
+
return o + 6;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
#if defined(NISH_SIMD_SSE2)
|
|
542
|
+
/* The stops among the `n` bytes at `p`, 4 <= n <= 16, as bits from 0 for
|
|
543
|
+
p[0]: read as two loads that may overlap, of 4, 8 or 16 bytes, never
|
|
544
|
+
outside the `n`. */
|
|
545
|
+
static inline unsigned nish_json_stops_short(const uint8_t *p, int64_t n) {
|
|
546
|
+
__m128i v;
|
|
547
|
+
unsigned lo, hi;
|
|
548
|
+
if (n >= 8) {
|
|
549
|
+
v = _mm_unpacklo_epi64(_mm_loadl_epi64((const __m128i *)p), _mm_loadl_epi64((const __m128i *)(p + n - 8)));
|
|
550
|
+
unsigned m = (unsigned)_mm_movemask_epi8(_mm_or_si128(
|
|
551
|
+
_mm_or_si128(_mm_cmpeq_epi8(v, _mm_set1_epi8(0x22)), _mm_cmpeq_epi8(v, _mm_set1_epi8(0x5c))),
|
|
552
|
+
_mm_cmpeq_epi8(_mm_min_epu8(v, _mm_set1_epi8(0x1f)), v)));
|
|
553
|
+
lo = m & 0xff;
|
|
554
|
+
hi = (m >> 8) << (n - 8);
|
|
555
|
+
} else {
|
|
556
|
+
uint32_t a, b;
|
|
557
|
+
__builtin_memcpy(&a, p, 4);
|
|
558
|
+
__builtin_memcpy(&b, p + n - 4, 4);
|
|
559
|
+
v = _mm_unpacklo_epi32(_mm_cvtsi32_si128((int)a), _mm_cvtsi32_si128((int)b));
|
|
560
|
+
unsigned m = (unsigned)_mm_movemask_epi8(_mm_or_si128(
|
|
561
|
+
_mm_or_si128(_mm_cmpeq_epi8(v, _mm_set1_epi8(0x22)), _mm_cmpeq_epi8(v, _mm_set1_epi8(0x5c))),
|
|
562
|
+
_mm_cmpeq_epi8(_mm_min_epu8(v, _mm_set1_epi8(0x1f)), v)));
|
|
563
|
+
lo = m & 0xf;
|
|
564
|
+
hi = ((m >> 4) & 0xf) << (n - 4);
|
|
565
|
+
}
|
|
566
|
+
return lo | hi;
|
|
567
|
+
}
|
|
568
|
+
#endif
|
|
569
|
+
|
|
570
|
+
/* The first byte of `p[i, to)` JSON escapes, or `to`: `skipJsonPlain`'s
|
|
571
|
+
answer. With SSE2 every window of 4 bytes or more is read in blocks that
|
|
572
|
+
lie inside it, the last overlapping the one before. */
|
|
573
|
+
static inline int64_t nish_json_plain_end(const uint8_t *p, int64_t i, int64_t to) {
|
|
574
|
+
#if defined(NISH_SIMD_SSE2)
|
|
575
|
+
int64_t n = to - i;
|
|
576
|
+
if (n >= 16) {
|
|
577
|
+
for (; to - i >= 16; i += 16) {
|
|
578
|
+
unsigned m = nish_plain_stops_sse2(p + i);
|
|
579
|
+
if (m != 0) return i + __builtin_ctz(m);
|
|
580
|
+
}
|
|
581
|
+
if (i == to) return to;
|
|
582
|
+
int64_t at = to - 16;
|
|
583
|
+
unsigned m = nish_plain_stops_sse2(p + at) & (~0u << (i - at));
|
|
584
|
+
return m != 0 ? at + __builtin_ctz(m) : to;
|
|
585
|
+
}
|
|
586
|
+
if (n >= 4) {
|
|
587
|
+
unsigned m = nish_json_stops_short(p + i, n);
|
|
588
|
+
return m != 0 ? i + __builtin_ctz(m) : to;
|
|
589
|
+
}
|
|
590
|
+
#endif
|
|
591
|
+
while (i < to && p[i] >= 0x20 && p[i] != 0x22 && p[i] != 0x5c) i++;
|
|
592
|
+
return i;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
/* `n` bytes from `s` to `d`, which do not overlap, in moves of 16, 8 or 4
|
|
596
|
+
bytes that may overlap one another, never outside the `n`. */
|
|
597
|
+
static inline void nish_json_copy(uint8_t *d, const uint8_t *s, int64_t n) {
|
|
598
|
+
if (n >= 16) {
|
|
599
|
+
int64_t k = 0;
|
|
600
|
+
for (; n - k > 16; k += 16) __builtin_memcpy(d + k, s + k, 16);
|
|
601
|
+
__builtin_memcpy(d + n - 16, s + n - 16, 16);
|
|
602
|
+
} else if (n >= 8) {
|
|
603
|
+
uint64_t a, b;
|
|
604
|
+
__builtin_memcpy(&a, s, 8);
|
|
605
|
+
__builtin_memcpy(&b, s + n - 8, 8);
|
|
606
|
+
__builtin_memcpy(d, &a, 8);
|
|
607
|
+
__builtin_memcpy(d + n - 8, &b, 8);
|
|
608
|
+
} else if (n >= 4) {
|
|
609
|
+
uint32_t a, b;
|
|
610
|
+
__builtin_memcpy(&a, s, 4);
|
|
611
|
+
__builtin_memcpy(&b, s + n - 4, 4);
|
|
612
|
+
__builtin_memcpy(d, &a, 4);
|
|
613
|
+
__builtin_memcpy(d + n - 4, &b, 4);
|
|
614
|
+
} else {
|
|
615
|
+
for (int64_t k = 0; k < n; k++) d[k] = s[k];
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/* `jsonPutString`'s walk (std/json.ts; the emitter makes the call,
|
|
620
|
+
src/kernels.ts): `src[from, to)` as a JSON string, its quotes and escapes
|
|
621
|
+
included, written into `buf` from `at`. Answers the new end, or -1 when
|
|
622
|
+
`at` is negative or the text does not fit before `len`, the array's length
|
|
623
|
+
as the Nish code's `toI32` reads it; the bytes from `at` may then have been
|
|
624
|
+
written, and the caller's cursor has not moved. The caller has checked
|
|
625
|
+
`0 <= from <= to <= src's length`. A run of bytes that need no escape is
|
|
626
|
+
found by `nish_json_plain_end` and copied in one piece, so a string with
|
|
627
|
+
nothing to escape is one scan and one copy. */
|
|
628
|
+
int32_t nish_json_put_string(uint8_t *buf, int32_t len, int32_t at, const uint8_t *src, int64_t from, int64_t to) {
|
|
629
|
+
int64_t cap = len, o = at, i = from;
|
|
630
|
+
if (at < 0 || o >= cap) return -1;
|
|
631
|
+
buf[o++] = 0x22;
|
|
632
|
+
while (i < to) {
|
|
633
|
+
int64_t j = nish_json_plain_end(src, i, to);
|
|
634
|
+
if (cap - o < j - i) return -1;
|
|
635
|
+
nish_json_copy(buf + o, src + i, j - i);
|
|
636
|
+
o += j - i;
|
|
637
|
+
if (j == to) break;
|
|
638
|
+
o = nish_json_escape_byte(buf, o, cap, src[j]);
|
|
639
|
+
if (o < 0) return -1;
|
|
640
|
+
i = j + 1;
|
|
641
|
+
}
|
|
642
|
+
if (o >= cap) return -1;
|
|
643
|
+
buf[o++] = 0x22;
|
|
644
|
+
return (int32_t)o;
|
|
645
|
+
}
|
package/runtime/runtime-net.c
CHANGED
|
@@ -76,6 +76,7 @@ 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>
|
|
79
80
|
#include <signal.h>
|
|
80
81
|
#include <stdio.h>
|
|
81
82
|
#include <sys/epoll.h>
|
|
@@ -286,6 +287,7 @@ struct nish_uring_opts {
|
|
|
286
287
|
int32_t cq_entries;
|
|
287
288
|
int32_t buf_count;
|
|
288
289
|
int32_t buf_size;
|
|
290
|
+
int32_t taskrun_worth;
|
|
289
291
|
};
|
|
290
292
|
int32_t nish_uring_probe(void);
|
|
291
293
|
int32_t nish_uring_premake(int32_t n, int32_t slots, int32_t *fds);
|
|
@@ -303,11 +305,11 @@ int64_t nish_uring_queued(const struct nish_uring_set *s, int32_t ring, int32_t
|
|
|
303
305
|
int32_t nish_uring_full(const struct nish_uring_set *s);
|
|
304
306
|
int32_t nish_uring_hold(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t on);
|
|
305
307
|
void nish_uring_destroy(struct nish_uring_set *s);
|
|
306
|
-
/* runtime-uring.c's counters; the dump below prints
|
|
308
|
+
/* runtime-uring.c's counters; the dump below prints seven of them. */
|
|
307
309
|
struct nish_uring_stats {
|
|
308
310
|
int64_t rings, live, accepting, slots, taskrun_reentries, overflow_drains, nobufs_rearms, send_requeues;
|
|
309
311
|
int64_t accept_errors, accept_pauses, bound_violations, max_accepting_rings, direct_waits, sends;
|
|
310
|
-
int64_t pool_new, pool_held, pool_cached;
|
|
312
|
+
int64_t pool_new, pool_held, pool_cached, enters;
|
|
311
313
|
};
|
|
312
314
|
void nish_uring_stats(const struct nish_uring_set *s, struct nish_uring_stats *st);
|
|
313
315
|
|
|
@@ -360,8 +362,37 @@ struct nr_set {
|
|
|
360
362
|
uint32_t fhead, flen, fcap;
|
|
361
363
|
uint32_t *pend; /* wblocked or lingering slots, checked after each pump */
|
|
362
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;
|
|
363
368
|
};
|
|
364
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
|
+
|
|
365
396
|
static struct nr_set *nr_sets[NR_SETS];
|
|
366
397
|
static int32_t nr_nsets;
|
|
367
398
|
/* `netRingState()`: 0 until a listener opens a set (1) or meets a refusal. */
|
|
@@ -539,7 +570,7 @@ static void nr_dispatch(struct nr_set *S, const struct nish_uring_event *e) {
|
|
|
539
570
|
if (c->rlen + n > c->rcap) {
|
|
540
571
|
uint32_t nc = c->rcap ? c->rcap : NR_RECV_FIRST;
|
|
541
572
|
while (nc < c->rlen + n) nc *= 2;
|
|
542
|
-
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);
|
|
543
574
|
if (!p) {
|
|
544
575
|
c->hup = 1;
|
|
545
576
|
c->hup_res = -12;
|
|
@@ -635,9 +666,11 @@ static void nr_stats_print(const struct nr_set *S, const char *why) {
|
|
|
635
666
|
struct nish_uring_stats st;
|
|
636
667
|
nish_uring_stats(S->u, &st);
|
|
637
668
|
dprintf(2,
|
|
638
|
-
"uring-stats %s port=%d sends=%lld pool_new=%lld pool_held=%lld pool_cached=%lld live=%lld slots=%lld
|
|
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",
|
|
639
671
|
why, (int)S->port, (long long)st.sends, (long long)st.pool_new, (long long)st.pool_held,
|
|
640
|
-
(long long)st.pool_cached, (long long)st.live, (long long)st.slots)
|
|
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);
|
|
641
674
|
}
|
|
642
675
|
|
|
643
676
|
static void nr_pump_all(void) {
|
|
@@ -735,6 +768,10 @@ static int32_t nr_close_listener(struct nr_set *S) {
|
|
|
735
768
|
free(S->fifo);
|
|
736
769
|
free(S->pend);
|
|
737
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;
|
|
738
775
|
S->dead = 1;
|
|
739
776
|
return close(S->listener) != 0 ? nish_net_fail() : 0;
|
|
740
777
|
}
|
|
@@ -776,9 +813,13 @@ static int32_t nr_read(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_
|
|
|
776
813
|
/* A drained queue is freed and emptied, so an idle slot holds none
|
|
777
814
|
(NURING-38) and a receive never finds roff at rlen past 0. */
|
|
778
815
|
if (c->roff == c->rlen) {
|
|
779
|
-
|
|
780
|
-
c->rbuf = NULL;
|
|
816
|
+
nr_rbuf_give(S, c);
|
|
781
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);
|
|
782
823
|
}
|
|
783
824
|
return (int32_t)n;
|
|
784
825
|
}
|
|
@@ -837,8 +878,7 @@ static int32_t nr_close(struct nr_set *S, struct nr_conn *c, int32_t ring, int32
|
|
|
837
878
|
nr_unregister(&c->v);
|
|
838
879
|
c->gen++;
|
|
839
880
|
c->roff = c->rlen = 0;
|
|
840
|
-
|
|
841
|
-
c->rbuf = NULL;
|
|
881
|
+
nr_rbuf_give(S, c);
|
|
842
882
|
c->rcap = 0;
|
|
843
883
|
int64_t q = nish_uring_queued(S->u, ring, slot);
|
|
844
884
|
/* A peer that only stopped sending (a FIN) still takes what is queued, so
|
|
@@ -1115,12 +1155,19 @@ static int32_t nr_poll_wait(int32_t loop, int32_t *out, int max, int32_t timeout
|
|
|
1115
1155
|
#endif
|
|
1116
1156
|
|
|
1117
1157
|
#if defined(__linux__)
|
|
1118
|
-
/* A batch's second wait: `n` pairs are in `out`, and it
|
|
1119
|
-
set
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
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. */
|
|
1124
1171
|
static int nr_batch(int32_t loop, int32_t *out, int n, int max, int32_t min, int32_t max_wait_ms) {
|
|
1125
1172
|
struct nr_set *S = NULL;
|
|
1126
1173
|
for (int32_t i = 0; i < nr_nsets; i++) {
|
|
@@ -1138,21 +1185,18 @@ static int nr_batch(int32_t loop, int32_t *out, int n, int max, int32_t min, int
|
|
|
1138
1185
|
int32_t watch = -1;
|
|
1139
1186
|
int direct = nr_direct(loop, &watch) == S;
|
|
1140
1187
|
int64_t end = nr_now_ms() + max_wait_ms;
|
|
1188
|
+
int64_t slice = (max_wait_ms + 7) / 8;
|
|
1189
|
+
S->windows++;
|
|
1141
1190
|
while (n < min && n < max) {
|
|
1142
1191
|
int64_t left = end - nr_now_ms();
|
|
1143
1192
|
if (left <= 0) break;
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
if (k < 0) break;
|
|
1149
|
-
for (int i = 0; i < k; i++) nr_dispatch(S, &ev[i]);
|
|
1150
|
-
} else if (rc < 0) {
|
|
1151
|
-
break;
|
|
1152
|
-
}
|
|
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;
|
|
1153
1197
|
nr_pump(S);
|
|
1154
1198
|
n += nr_collect_from(loop, out + 2 * n, max - n, 1);
|
|
1155
|
-
if (
|
|
1199
|
+
if (woke) break;
|
|
1156
1200
|
}
|
|
1157
1201
|
return n;
|
|
1158
1202
|
}
|