@ai-ecoverse/slicc-kernel 1.11.0 → 1.12.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.
@@ -730,6 +730,10 @@ var init_sync_fs_wire = __esm({
730
730
  });
731
731
 
732
732
  // src/realm/sync-sab-wire.ts
733
+ function publishMemory(sab, bytes) {
734
+ if (bytes > 0)
735
+ Atomics.store(new Int32Array(sab, 0, SAB_HEADER_I32), SAB_I_MEMORY, Math.ceil(bytes / 65536));
736
+ }
733
737
  function decodeSabResult(status2, payload) {
734
738
  switch (status2) {
735
739
  case SAB_STATUS_BYTES:
@@ -765,7 +769,7 @@ function sabViews(sab) {
765
769
  window: new Uint8Array(sab, SAB_HEADER_BYTES)
766
770
  };
767
771
  }
768
- var SAB_HEADER_I32, SAB_HEADER_BYTES, SAB_I_STATE, SAB_I_SEQ, SAB_I_STATUS, SAB_I_TOTAL, SAB_I_CHUNK, SAB_I_OFFSET, SAB_I_REQ, SAB_I_PUB, SAB_I_SIGNALS, SAB_I_TIMERS, SAB_STATE_IDLE, SAB_STATE_PENDING, SAB_STATE_READY, SAB_STATUS_BYTES, SAB_STATUS_JSON, SAB_STATUS_VOID, SAB_STATUS_ERRNO, SAB_DEFAULT_WINDOW_BYTES, SAB_MIN_BYTES, SYNC_SAB_REQ_MSG, SYNC_SAB_NEXT_MSG, encoder, decoder;
772
+ var SAB_HEADER_I32, SAB_HEADER_BYTES, SAB_I_STATE, SAB_I_SEQ, SAB_I_STATUS, SAB_I_TOTAL, SAB_I_CHUNK, SAB_I_OFFSET, SAB_I_REQ, SAB_I_PUB, SAB_I_SIGNALS, SAB_I_TIMERS, SAB_I_MEMORY, SAB_STATE_IDLE, SAB_STATE_PENDING, SAB_STATE_READY, SAB_STATUS_BYTES, SAB_STATUS_JSON, SAB_STATUS_VOID, SAB_STATUS_ERRNO, SAB_DEFAULT_WINDOW_BYTES, SAB_MIN_BYTES, SYNC_SAB_REQ_MSG, SYNC_SAB_NEXT_MSG, encoder, decoder;
769
773
  var init_sync_sab_wire = __esm({
770
774
  "src/realm/sync-sab-wire.ts"() {
771
775
  "use strict";
@@ -781,6 +785,7 @@ var init_sync_sab_wire = __esm({
781
785
  SAB_I_PUB = 7;
782
786
  SAB_I_SIGNALS = 8;
783
787
  SAB_I_TIMERS = 9;
788
+ SAB_I_MEMORY = 10;
784
789
  SAB_STATE_IDLE = 0;
785
790
  SAB_STATE_PENDING = 1;
786
791
  SAB_STATE_READY = 2;
@@ -817,6 +822,7 @@ function createSyncSabTransport(sab, port2, deps = {}) {
817
822
  }
818
823
  return {
819
824
  call(req, timeoutMs, label) {
825
+ if (deps.memory) publishMemory(sab, deps.memory());
820
826
  const id = ++seq;
821
827
  const deadline = now() + timeoutMs;
822
828
  let out = null;
@@ -2439,6 +2445,9 @@ function ttyNumber(tty) {
2439
2445
  const vt = tty && /^\/dev\/tty(\d+)$/.exec(tty);
2440
2446
  return vt ? 4 << 8 | Number(vt[1]) : 0;
2441
2447
  }
2448
+ function pages(bytes) {
2449
+ return Math.ceil(bytes / 4096);
2450
+ }
2442
2451
  function ticks(ms) {
2443
2452
  return Math.max(0, Math.floor(ms * HZ / 1e3));
2444
2453
  }
@@ -2466,8 +2475,8 @@ function pidStat(info, boot) {
2466
2475
  1,
2467
2476
  0,
2468
2477
  ticks(info.started - boot),
2469
- 0,
2470
- 0,
2478
+ info.memory,
2479
+ pages(info.memory),
2471
2480
  "18446744073709551615",
2472
2481
  ...Array(12).fill(0),
2473
2482
  17,
@@ -2498,6 +2507,8 @@ function pidStatus(info) {
2498
2507
  `Uid: ${ids}`,
2499
2508
  `Gid: ${ids}`,
2500
2509
  "FDSize: 64",
2510
+ `VmSize: ${Math.ceil(info.memory / KIB)} kB`,
2511
+ `VmRSS: ${Math.ceil(info.memory / KIB)} kB`,
2501
2512
  "Groups: ",
2502
2513
  `NSpid: ${info.pid}`,
2503
2514
  `NSpgid: ${info.pgid}`,
@@ -2517,12 +2528,16 @@ function pidFile(name, info, boot) {
2517
2528
  if (name === "comm") return `${commOf(info)}
2518
2529
  `;
2519
2530
  if (name === "stat") return pidStat(info, boot);
2520
- if (name === "statm") return "0 0 0 0 0 0 0\n";
2531
+ if (name === "statm") {
2532
+ const p = pages(info.memory);
2533
+ return `${p} ${p} 0 0 0 ${p} 0
2534
+ `;
2535
+ }
2521
2536
  return pidStatus(info);
2522
2537
  }
2523
- function memoryInfo(source) {
2538
+ function memoryInfo(source, used = 0) {
2524
2539
  const total = source.deviceMemory ? source.deviceMemory * KIB * KIB * KIB : FALLBACK_MEMORY;
2525
- const free = Math.max(0, total - (source.usedHeap ?? 0));
2540
+ const free = Math.max(0, total - used - (source.usedHeap ?? 0));
2526
2541
  const kb = (bytes) => `${Math.floor(bytes / KIB)} kB`;
2527
2542
  return [
2528
2543
  ["MemTotal", kb(total)],
@@ -2541,7 +2556,12 @@ function systemFile(name, listing, now, memory) {
2541
2556
  const up = Math.max(0, now - listing.boot) / 1e3;
2542
2557
  if (name === "uptime") return `${up.toFixed(2)} ${up.toFixed(2)}
2543
2558
  `;
2544
- if (name === "meminfo") return memoryInfo(memory);
2559
+ if (name === "meminfo") {
2560
+ return memoryInfo(
2561
+ memory,
2562
+ listing.processes.reduce((sum, p) => sum + p.memory, 0)
2563
+ );
2564
+ }
2545
2565
  const live = listing.processes.filter((p) => p.state !== "Z");
2546
2566
  if (name === "loadavg") {
2547
2567
  const last = Math.max(0, ...listing.processes.map((p) => p.pid));
@@ -2892,9 +2912,21 @@ function signalMasks(m) {
2892
2912
  if (!mask || caught === -1) return null;
2893
2913
  return { caught, ignored: mask(1), restart: mask(2) };
2894
2914
  }
2915
+ function reportGrowth(imports, publish) {
2916
+ const env = imports.env;
2917
+ const resize = env?.emscripten_resize_heap;
2918
+ if (!env || typeof resize !== "function") return;
2919
+ env.emscripten_resize_heap = (...args) => {
2920
+ const grown = resize(...args);
2921
+ publish();
2922
+ return grown;
2923
+ };
2924
+ }
2895
2925
  async function runWasmProcess(init, port2, deps = {}) {
2896
2926
  const signals = new SignalGate(
2897
- createSyncSabTransport(init.sab, port2),
2927
+ createSyncSabTransport(init.sab, port2, {
2928
+ memory: () => memory?.buffer.byteLength ?? 0
2929
+ }),
2898
2930
  new Int32Array(init.sab, 0, SAB_HEADER_I32),
2899
2931
  {
2900
2932
  masks: () => signalMasks(module),
@@ -2949,9 +2981,11 @@ async function runWasmProcess(init, port2, deps = {}) {
2949
2981
  sys,
2950
2982
  memory: () => memory
2951
2983
  });
2984
+ reportGrowth(imports, () => publishMemory(init.sab, memory?.buffer.byteLength ?? 0));
2952
2985
  return WebAssembly.instantiate(init.program.module, imports);
2953
2986
  }).then((instance) => {
2954
2987
  memory = wasmMemory(instance, imports);
2988
+ publishMemory(init.sab, memory?.buffer.byteLength ?? 0);
2955
2989
  done(instance, init.program.module);
2956
2990
  }, failed2);
2957
2991
  return {};
@@ -5237,8 +5271,8 @@ function align(n, to) {
5237
5271
  return Math.ceil(n / to) * to;
5238
5272
  }
5239
5273
  function growTo(memory, bytes) {
5240
- const pages = Math.ceil(bytes / PAGE) - memory.buffer.byteLength / PAGE;
5241
- if (pages > 0) memory.grow(pages);
5274
+ const pages2 = Math.ceil(bytes / PAGE) - memory.buffer.byteLength / PAGE;
5275
+ if (pages2 > 0) memory.grow(pages2);
5242
5276
  }
5243
5277
  function growTable(table, length) {
5244
5278
  if (table.length < length) table.grow(length - table.length);
@@ -6465,14 +6499,20 @@ function restoreExportedMemory(instance, imported, init) {
6465
6499
  fillMemory(instance.exports.memory, init.fork.memory);
6466
6500
  }
6467
6501
  function fillMemory(memory, copy) {
6468
- const pages = copy.byteLength / 65536 - memory.buffer.byteLength / 65536;
6469
- if (pages > 0) memory.grow(pages);
6502
+ const pages2 = copy.byteLength / 65536 - memory.buffer.byteLength / 65536;
6503
+ if (pages2 > 0) memory.grow(pages2);
6470
6504
  new Uint8Array(memory.buffer).set(copy);
6471
6505
  }
6506
+ function sizeOf(main) {
6507
+ return () => main.memory?.buffer.byteLength ?? 0;
6508
+ }
6509
+ function memoryOf(instance, imported) {
6510
+ return imported ?? instance.exports.memory;
6511
+ }
6472
6512
  function kernelOf(init, port2, hooks = { masks: () => null, raise: () => {
6473
- } }) {
6513
+ } }, memory) {
6474
6514
  const transport = new SignalGate(
6475
- createSyncSabTransport(init.sab, port2),
6515
+ createSyncSabTransport(init.sab, port2, memory ? { memory } : {}),
6476
6516
  new Int32Array(init.sab, 0, SAB_HEADER_I32),
6477
6517
  hooks
6478
6518
  ).transport();
@@ -6585,7 +6625,8 @@ async function runWasiProcess(init, port2) {
6585
6625
  call2({ op: "proc-kill", pid: init.pid, sig });
6586
6626
  throw new WasiExit(128 + sig);
6587
6627
  });
6588
- const { transport, sys, call: kernelCall, say } = kernelOf(init, port2, signals);
6628
+ const main = {};
6629
+ const { transport, sys, call: kernelCall, say } = kernelOf(init, port2, signals, sizeOf(main));
6589
6630
  const stats = init.env.SLICC_WASI_STATS === "1" ? new WasiStats() : void 0;
6590
6631
  const call2 = stats ? timedCalls(stats, kernelCall) : kernelCall;
6591
6632
  const { module } = init.program;
@@ -6625,6 +6666,8 @@ async function runWasiProcess(init, port2) {
6625
6666
  ...program ? { program } : {}
6626
6667
  });
6627
6668
  restoreExportedMemory(instance, memory, init);
6669
+ main.memory = memoryOf(instance, memory);
6670
+ publishMemory(init.sab, sizeOf(main)());
6628
6671
  signals.bind(instance.exports);
6629
6672
  host.onRaise = (sig) => signals.raised(sig);
6630
6673
  const exports = instance.exports;