@amritk/nish-aarch64-linux 0.47.0 → 0.49.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.
@@ -54,6 +54,103 @@
54
54
 
55
55
  #include "../nish.h"
56
56
 
57
+ /* SQLite's page cache, pcache1, with its largest key kept honest.
58
+ *
59
+ * pcache1 keeps `iMaxKey`, the largest page number it has held since the
60
+ * last truncate, so that a truncate after a commit scans only the hash
61
+ * slots from the database's end to `iMaxKey`; when that span is as wide as
62
+ * the hash table, it scans the whole table. When a B-tree split hands its
63
+ * new pages page numbers out of order, `balance_nonroot` swaps two of them
64
+ * through a free key, the lock-byte page's (262,145 at 4 KiB pages), and
65
+ * the rekey into it raises `iMaxKey` there for good. Every commit after
66
+ * such a split then scans every slot of the hash table, which grows with
67
+ * the cache: an index written at random keys splits often enough that, with
68
+ * a 64 MiB cache, that scan measured 1.5% of a write-heavy server's CPU.
69
+ *
70
+ * sqlite3.c is never edited (README.md), so the fix is here: the same
71
+ * pcache1, installed through SQLITE_CONFIG_PCACHE2 before SQLite starts,
72
+ * with three of its methods wrapped. A rekey that raises the largest key
73
+ * parks a page above every other; until the page leaves that key, every
74
+ * fetch and rekey of the cache records the largest key it brings in. When
75
+ * the parked page moves down again and still holds the largest key, the
76
+ * largest key goes back to the larger of what was recorded and the page's
77
+ * new number. Anything else - a fetch above the parked key, a truncate, the
78
+ * cache destroyed, a second cache parking meanwhile - leaves pcache1's own
79
+ * value standing, which is too large at worst, and too large costs only the
80
+ * scan. One record serves a thread: a split's three rekeys run back to back
81
+ * on one connection. */
82
+ typedef struct nishsqlite_park {
83
+ PCache1 *cache; /* the cache with a parked page, NULL when none */
84
+ unsigned int key; /* the parked page's key */
85
+ unsigned int max; /* the cache's largest key apart from the parked page */
86
+ } nishsqlite_park;
87
+
88
+ static NISH_TLS nishsqlite_park nishsqlite_parked;
89
+
90
+ static sqlite3_pcache_page *nishsqlite_pcache_fetch(sqlite3_pcache *p, unsigned int key, int create) {
91
+ sqlite3_pcache_page *page = pcache1Fetch(p, key, create);
92
+ if (nishsqlite_parked.cache == (PCache1 *)p && page != NULL && key > nishsqlite_parked.max) {
93
+ nishsqlite_parked.max = key;
94
+ }
95
+ return page;
96
+ }
97
+
98
+ static void nishsqlite_pcache_rekey(sqlite3_pcache *p, sqlite3_pcache_page *page, unsigned int from,
99
+ unsigned int to) {
100
+ PCache1 *cache = (PCache1 *)p;
101
+ unsigned int before = cache->iMaxKey;
102
+ pcache1Rekey(p, page, from, to);
103
+ if (to > before) {
104
+ nishsqlite_parked.cache = cache;
105
+ nishsqlite_parked.key = to;
106
+ nishsqlite_parked.max = before;
107
+ } else if (nishsqlite_parked.cache == cache) {
108
+ if (from == nishsqlite_parked.key) {
109
+ if (cache->iMaxKey == from) {
110
+ cache->iMaxKey = nishsqlite_parked.max > to ? nishsqlite_parked.max : to;
111
+ }
112
+ nishsqlite_parked.cache = NULL;
113
+ } else if (to > nishsqlite_parked.max) {
114
+ nishsqlite_parked.max = to;
115
+ }
116
+ }
117
+ }
118
+
119
+ static void nishsqlite_pcache_truncate(sqlite3_pcache *p, unsigned int limit) {
120
+ if (nishsqlite_parked.cache == (PCache1 *)p) {
121
+ nishsqlite_parked.cache = NULL;
122
+ }
123
+ pcache1Truncate(p, limit);
124
+ }
125
+
126
+ static void nishsqlite_pcache_destroy(sqlite3_pcache *p) {
127
+ if (nishsqlite_parked.cache == (PCache1 *)p) {
128
+ nishsqlite_parked.cache = NULL;
129
+ }
130
+ pcache1Destroy(p);
131
+ }
132
+
133
+ /* Before main, so before any thread can start SQLite: sqlite3_config is
134
+ * refused once SQLite is initialised and is not itself thread-safe. */
135
+ __attribute__((constructor)) static void nishsqlite_pcache_install(void) {
136
+ static const sqlite3_pcache_methods2 methods = {
137
+ 1,
138
+ 0,
139
+ pcache1Init,
140
+ pcache1Shutdown,
141
+ pcache1Create,
142
+ pcache1Cachesize,
143
+ pcache1Pagecount,
144
+ nishsqlite_pcache_fetch,
145
+ pcache1Unpin,
146
+ nishsqlite_pcache_rekey,
147
+ nishsqlite_pcache_truncate,
148
+ nishsqlite_pcache_destroy,
149
+ pcache1Shrink,
150
+ };
151
+ sqlite3_config(SQLITE_CONFIG_PCACHE2, &methods);
152
+ }
153
+
57
154
  typedef struct nishsqlite_slot {
58
155
  void *p; /* the connection, statement or cursor; NULL while free */
59
156
  int32_t gen; /* 1..32767, the high bits of the slot's handle */
package/scripts/build.sh CHANGED
@@ -431,6 +431,51 @@ if [ "$async_wanted" = 1 ] && [ -z "$objects_from" ]; then
431
431
  esac
432
432
  done
433
433
  fi
434
+ # The graphics shim (runtime/gfx/) is selected by the IR the same way: a module
435
+ # that declares one of its `nishgfx_*` functions gets runtime/gfx/'s units
436
+ # compiled and linked against SDL3 and wgpu-native (`gfx_objects`). Any other
437
+ # program compiles and links none of it, so its link line and binary stay as
438
+ # they were. It is one rule with `callsGfx` in src/run-cache.ts, which keys a
439
+ # run's binary on runtime/gfx/ and the libraries; change both or neither. The
440
+ # libraries are not in the tree: they are read from $NISH_GFX_PREFIX/{include,lib},
441
+ # or from build/gfx/<platform>/, where scripts/fetch-gfx.sh puts them.
442
+ gfx_wanted=0
443
+ if [ ${#lls[@]} -gt 0 ] && grep -Eq '^declare [^"]*@nishgfx_' "${lls[@]}"; then
444
+ gfx_wanted=1
445
+ fi
446
+ gfx_dir=""
447
+ gfx_prefix=""
448
+ if [ "$gfx_wanted" = 1 ]; then
449
+ case "$profile" in
450
+ debug|speed|size) ;;
451
+ *) echo "error: the module calls runtime/gfx/, which the $profile profile cannot link: SDL3 and wgpu-native are native libraries; use the debug, speed or size profile" >&2
452
+ exit 2 ;;
453
+ esac
454
+ gfx_dir="$(cd "$(dirname "$0")/../runtime/gfx" 2>/dev/null && pwd -P)" || gfx_dir=""
455
+ if [ -z "$gfx_dir" ]; then
456
+ echo "error: the module calls runtime/gfx/, and $(dirname "$0")/../runtime/gfx/ is not there" >&2
457
+ exit 2
458
+ fi
459
+ if [ -n "${NISH_GFX_PREFIX:-}" ]; then
460
+ gfx_prefix=$NISH_GFX_PREFIX
461
+ else
462
+ case "$(uname -s)-$(uname -m)" in
463
+ Linux-x86_64) gfx_platform=linux-x86_64 ;;
464
+ Linux-aarch64|Linux-arm64) gfx_platform=linux-aarch64 ;;
465
+ Darwin-x86_64) gfx_platform=macos-x86_64 ;;
466
+ Darwin-arm64) gfx_platform=macos-aarch64 ;;
467
+ *) echo "error: the module calls runtime/gfx/, and scripts/fetch-gfx.sh pins no SDL3 or wgpu-native for $(uname -s)-$(uname -m); set NISH_GFX_PREFIX to an install of both" >&2
468
+ exit 2 ;;
469
+ esac
470
+ gfx_prefix="$(cd "$(dirname "$0")/.." && pwd -P)/build/gfx/$gfx_platform"
471
+ fi
472
+ for need in include/SDL3/SDL.h include/webgpu/webgpu.h include/webgpu/wgpu.h lib/libSDL3.a lib/libwgpu_native.a; do
473
+ if [ ! -f "$gfx_prefix/$need" ]; then
474
+ echo "error: the module calls runtime/gfx/, and $gfx_prefix/$need is not there: run scripts/fetch-gfx.sh, or set NISH_GFX_PREFIX to an install of SDL3 and wgpu-native" >&2
475
+ exit 2
476
+ fi
477
+ done
478
+ fi
434
479
  if [ "$sqlite_wanted" = 1 ] && [ -z "$sqlite_dir" ]; then
435
480
  echo "error: the module calls SQLite, and $(dirname "$0")/../runtime/sqlite/ is not there" >&2
436
481
  exit 2
@@ -777,6 +822,46 @@ sqlite_objects() {
777
822
  done < <(sqlite_unit_names)
778
823
  }
779
824
 
825
+ # The units of runtime/gfx/, by name, in the order they are linked.
826
+ gfx_unit_names() {
827
+ (cd -- "$gfx_dir" && for unit in *.c; do [ -f "$unit" ] && printf '%s\n' "$unit"; done)
828
+ }
829
+ # Adds the objects of runtime/gfx/ and the libraries they call to this link.
830
+ # Each unit is compiled into the link's own directory, by its bare name from
831
+ # inside runtime/gfx/ as sqlite_units does, with the flags given less -flto:
832
+ # the shim is a thin layer over two native libraries, so there is nothing for
833
+ # LTO to see through. The libraries are static archives; what else each needs
834
+ # is the system's: on Linux, pthreads and dlopen (SDL opens its Wayland and
835
+ # X11 client libraries at run time) and libm; on macOS, the frameworks SDL's
836
+ # Cocoa driver and wgpu-native's Metal backend call. Nothing is added for a
837
+ # module that does not call runtime/gfx/.
838
+ gfx_libs=()
839
+ gfx_objects() {
840
+ local unit flag
841
+ local -a flags
842
+ [ "$gfx_wanted" = 1 ] || return 0
843
+ make_work
844
+ mkdir -p -- "$work/gfx"
845
+ flags=()
846
+ for flag in "$@"; do
847
+ case "$flag" in -flto) ;; *) flags+=("$flag") ;; esac
848
+ done
849
+ while IFS= read -r unit; do
850
+ (cd -- "$gfx_dir" && exec "$CC" "${flags[@]}" -I. -I"$gfx_prefix/include" -c "$unit" -o "$work/gfx/${unit%.c}.o") ||
851
+ { echo "error: runtime/gfx/$unit did not compile" >&2; exit 1; }
852
+ inputs+=("$work/gfx/${unit%.c}.o")
853
+ done < <(gfx_unit_names)
854
+ gfx_libs=(-L"$gfx_prefix/lib" -lSDL3 -lwgpu_native)
855
+ case "$(uname -s)" in
856
+ Darwin)
857
+ for flag in Cocoa CoreFoundation CoreVideo Foundation IOKit Carbon Metal QuartzCore; do
858
+ gfx_libs+=(-framework "$flag")
859
+ done
860
+ gfx_libs+=(-weak_framework UniformTypeIdentifiers) ;;
861
+ *) gfx_libs+=(-pthread -ldl) ;;
862
+ esac
863
+ }
864
+
780
865
  if [ -n "$sqlite_to" ]; then
781
866
  mkdir -p -- "$sqlite_to"
782
867
  sqlite_to=$(cd -- "$sqlite_to" && pwd)
@@ -855,7 +940,7 @@ fi
855
940
  # under `set -u` is a fatal "unbound variable" -- bash 4.4 made it legal, which is
856
941
  # why Linux never noticed. `pgo`, `elf`, `strip_flag` and `libs` are all empty on
857
942
  # ordinary builds, so please do not simplify these back.
858
- case "$profile" in debug|speed|size) sqlite_objects "${compile[@]}" ;; esac
943
+ case "$profile" in debug|speed|size) sqlite_objects "${compile[@]}"; gfx_objects "${compile[@]}" ;; esac
859
944
  # A package's own C sources join the inputs here, after the runtime's halves
860
945
  # and SQLite were decided from the IR alone, so a source can neither stand in
861
946
  # for runtime.c nor be taken for one of its siblings. clang compiles each by
@@ -868,10 +953,10 @@ if [ ${#link_libs[@]} -gt 0 ] || [ ${#link_sources[@]} -gt 0 ]; then
868
953
  fi
869
954
  case "$profile" in
870
955
  debug)
871
- "$CC" "${common[@]}" "${inputs[@]}" ${link_libs[@]+"${link_libs[@]}"} -lm -o "$out" ;;
956
+ "$CC" "${common[@]}" "${inputs[@]}" ${link_libs[@]+"${link_libs[@]}"} ${gfx_libs[@]+"${gfx_libs[@]}"} -lm -o "$out" ;;
872
957
  speed|size)
873
958
  "$CC" "${common[@]}" "${optimise[@]}" ${lto_parts[@]+"${lto_parts[@]}"} \
874
- "${gc[@]}" ${strip_flag[@]+"${strip_flag[@]}"} "${inputs[@]}" ${link_libs[@]+"${link_libs[@]}"} -lm -o "$out" ;;
959
+ "${gc[@]}" ${strip_flag[@]+"${strip_flag[@]}"} "${inputs[@]}" ${link_libs[@]+"${link_libs[@]}"} ${gfx_libs[@]+"${gfx_libs[@]}"} -lm -o "$out" ;;
875
960
  wasm|wasm-host)
876
961
  # A region's chunks come from malloc, and the freestanding wasm runtime
877
962
  # (runtime-wasm.c) has none, so IR that makes or enters one is refused here