@ai-ecoverse/slicc-kernel 1.66.0 → 1.67.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.
@@ -3115,8 +3115,8 @@ var init_wasix_process = __esm({
3115
3115
  ];
3116
3116
  return { stdio, inherit, cwd, opened };
3117
3117
  }
3118
- applyFdOp(op, slots2, cwd) {
3119
- const { map, nulls, opened } = slots2;
3118
+ applyFdOp(op, slots3, cwd) {
3119
+ const { map, nulls, opened } = slots3;
3120
3120
  const { fds } = this.host;
3121
3121
  const point = (fd, kfd) => {
3122
3122
  if (kfd === void 0) {
@@ -3512,12 +3512,14 @@ function sabViews(sab) {
3512
3512
  window: new Uint8Array(sab, SAB_HEADER_BYTES)
3513
3513
  };
3514
3514
  }
3515
- 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_I_TOTAL_HI, SAB_I_OFFSET_HI, SAB_I_KILLED, SAB_I_ASYNC, SAB_I_STOP, U32, 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, decoder2;
3515
+ var SAB_HEADER_I32, SAB_CLOCK_OFFSET, SAB_CLOCK_I64, 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_I_TOTAL_HI, SAB_I_OFFSET_HI, SAB_I_KILLED, SAB_I_ASYNC, SAB_I_STOP, U32, 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, decoder2;
3516
3516
  var init_sync_sab_wire = __esm({
3517
3517
  "src/realm/sync-sab-wire.ts"() {
3518
3518
  "use strict";
3519
3519
  SAB_HEADER_I32 = 16;
3520
- SAB_HEADER_BYTES = SAB_HEADER_I32 * 4;
3520
+ SAB_CLOCK_OFFSET = SAB_HEADER_I32 * 4;
3521
+ SAB_CLOCK_I64 = 3;
3522
+ SAB_HEADER_BYTES = SAB_CLOCK_OFFSET + SAB_CLOCK_I64 * 8;
3521
3523
  SAB_I_STATE = 0;
3522
3524
  SAB_I_SEQ = 1;
3523
3525
  SAB_I_STATUS = 2;
@@ -4496,7 +4498,7 @@ function port(n) {
4496
4498
  function netOps(kernel, io) {
4497
4499
  const name2 = async (fd, peer) => addressOf(await kernel.json({ op: "sock-name", fd, peer }));
4498
4500
  const connection = async (fd) => {
4499
- const [local, remote] = [await name2(fd, false), await name2(fd, true)];
4501
+ const [local2, remote] = [await name2(fd, false), await name2(fd, true)];
4500
4502
  const stop = new AbortController();
4501
4503
  let closed;
4502
4504
  const close = () => {
@@ -4521,7 +4523,7 @@ function netOps(kernel, io) {
4521
4523
  close: async () => void await shut(SHUT_WR),
4522
4524
  abort: () => close()
4523
4525
  });
4524
- return { fd, local, remote, readable: readable2, writable: writable2, close };
4526
+ return { fd, local: local2, remote, readable: readable2, writable: writable2, close };
4525
4527
  };
4526
4528
  const resolve = async (host, signal) => {
4527
4529
  if (IPV4.test(host)) return host;
@@ -4682,11 +4684,48 @@ var init_js_run = __esm({
4682
4684
  }
4683
4685
  });
4684
4686
 
4687
+ // src/kernel/cpu-clock.ts
4688
+ function epochMicros() {
4689
+ return Math.floor((performance.timeOrigin + performance.now()) * 1e3);
4690
+ }
4691
+ function slots2(sab) {
4692
+ return new BigInt64Array(sab, LOCAL, SAB_CLOCK_I64);
4693
+ }
4694
+ function useLocalClock(sab) {
4695
+ local = sab && sab.byteLength >= LOCAL + SAB_CLOCK_I64 * 8 ? slots2(sab) : void 0;
4696
+ if (!local) return;
4697
+ const wait2 = (typed, index3, value2, timeout) => localWait(() => rawWait(typed, index3, value2, timeout));
4698
+ Atomics.wait = wait2;
4699
+ }
4700
+ function localWait(wait2) {
4701
+ const clock = local;
4702
+ if (!clock) return wait2();
4703
+ const start2 = BigInt(epochMicros());
4704
+ const idle = Atomics.load(clock, IDLE);
4705
+ Atomics.store(clock, IDLE, idle - start2);
4706
+ try {
4707
+ return wait2();
4708
+ } finally {
4709
+ const end = BigInt(epochMicros());
4710
+ Atomics.store(clock, IDLE, idle + (end > start2 ? end - start2 : 0n));
4711
+ }
4712
+ }
4713
+ var LOCAL, IDLE, local, rawWait;
4714
+ var init_cpu_clock = __esm({
4715
+ "src/kernel/cpu-clock.ts"() {
4716
+ "use strict";
4717
+ init_sync_sab_wire();
4718
+ LOCAL = SAB_CLOCK_OFFSET;
4719
+ IDLE = 1;
4720
+ rawWait = Atomics.wait.bind(Atomics);
4721
+ }
4722
+ });
4723
+
4685
4724
  // src/realm/sync-sab-bridge.ts
4686
4725
  function createSyncSabTransport(sab, port3, deps = {}) {
4687
4726
  const views = sabViews(sab);
4688
4727
  const { header, window } = views;
4689
- const wait2 = deps.wait ?? ((a, i, v, t) => Atomics.wait(a, i, v, t));
4728
+ const wait2 = deps.wait ?? ((a, i, v, t) => rawWait(a, i, v, t));
4690
4729
  const now = deps.now ?? (() => performance.now());
4691
4730
  let seq = 0;
4692
4731
  function awaitReady(id, deadline, label) {
@@ -4890,6 +4929,7 @@ function createSyncFsSabBridge(transport, opts = {}) {
4890
4929
  var init_sync_sab_bridge = __esm({
4891
4930
  "src/realm/sync-sab-bridge.ts"() {
4892
4931
  "use strict";
4932
+ init_cpu_clock();
4893
4933
  init_sync_fs_wire();
4894
4934
  init_sync_sab_wire();
4895
4935
  }
@@ -8024,13 +8064,13 @@ function describeInherited(Fs, sys, streams, livePath, actions = []) {
8024
8064
  if (from) table.set(target, { stream: from.stream, cloexec: false });
8025
8065
  else table.delete(target);
8026
8066
  }
8027
- const slots2 = /* @__PURE__ */ new Map();
8067
+ const slots3 = /* @__PURE__ */ new Map();
8028
8068
  for (const [fd, { stream, cloexec }] of table) {
8029
- if (fd > 2 && !cloexec) slots2.set(fd, stream);
8069
+ if (fd > 2 && !cloexec) slots3.set(fd, stream);
8030
8070
  }
8031
8071
  const promote = vfsPromoter(Fs, sys, streams, livePath);
8032
8072
  const out = [];
8033
- for (const [fd, stream] of slots2) {
8073
+ for (const [fd, stream] of slots3) {
8034
8074
  promote(stream);
8035
8075
  const slot = inheritedSlot(fd, stream);
8036
8076
  if (slot) out.push(slot);
@@ -8229,12 +8269,12 @@ function anySelect(Fs, transport, read2, write2, timeoutMs) {
8229
8269
  const kernelRead = read2.filter((fd) => !own(fd));
8230
8270
  const kernelWrite = write2.filter((fd) => !own(fd));
8231
8271
  for (; ; ) {
8232
- const local = ownReady(Fs, read2.filter(own), write2.filter(own));
8233
- const pending = local.read.length > 0 || local.write.length > 0;
8272
+ const local2 = ownReady(Fs, read2.filter(own), write2.filter(own));
8273
+ const pending = local2.read.length > 0 || local2.write.length > 0;
8234
8274
  const slice = pending ? 0 : Math.min(SELECT_SLICE_MS, Math.max(0, deadline - Date.now()));
8235
8275
  const got = kernelSelect(Fs, transport, kernelRead, kernelWrite, slice);
8236
8276
  if (typeof got === "number") return got;
8237
- const ready = { read: [...local.read, ...got.read], write: [...local.write, ...got.write] };
8277
+ const ready = { read: [...local2.read, ...got.read], write: [...local2.write, ...got.write] };
8238
8278
  if (ready.read.length > 0 || ready.write.length > 0 || Date.now() >= deadline) {
8239
8279
  return ready;
8240
8280
  }
@@ -8754,8 +8794,8 @@ function pidStat(info, boot) {
8754
8794
  0,
8755
8795
  0,
8756
8796
  0,
8757
- 0,
8758
- 0,
8797
+ ticks(info.utime ?? 0),
8798
+ ticks(info.stime ?? 0),
8759
8799
  0,
8760
8800
  0,
8761
8801
  20,
@@ -8848,19 +8888,23 @@ function systemFile(name2, listing, now, memory) {
8848
8888
  if (name2 === "meminfo") {
8849
8889
  return memoryInfo(
8850
8890
  memory,
8851
- listing.processes.reduce((sum, p) => sum + p.memory, 0)
8891
+ listing.processes.reduce((sum2, p) => sum2 + p.memory, 0)
8852
8892
  );
8853
8893
  }
8854
8894
  const live = listing.processes.filter((p) => p.state !== "Z");
8855
8895
  if (name2 === "loadavg") {
8856
8896
  const last = Math.max(0, ...listing.processes.map((p) => p.pid));
8857
- return `0.00 0.00 0.00 1/${live.length} ${last}
8897
+ const [one, five, fifteen] = (listing.load ?? [0, 0, 0]).map((n) => n.toFixed(2));
8898
+ return `${one} ${five} ${fifteen} 1/${live.length} ${last}
8858
8899
  `;
8859
8900
  }
8860
- const idle = ticks(now - listing.boot);
8901
+ const sum = (key) => listing.cpu?.[key] ?? listing.processes.reduce((n, p) => n + (p[key] ?? 0), 0);
8902
+ const user = ticks(sum("utime"));
8903
+ const system = ticks(sum("stime"));
8904
+ const idle = listing.cpu ? ticks(listing.cpu.idle) : Math.max(0, ticks(now - listing.boot) - user - system);
8861
8905
  return [
8862
- `cpu 0 0 0 ${idle} 0 0 0 0 0 0`,
8863
- `cpu0 0 0 0 ${idle} 0 0 0 0 0 0`,
8906
+ `cpu ${user} 0 ${system} ${idle} 0 0 0 0 0 0`,
8907
+ `cpu0 ${user} 0 ${system} ${idle} 0 0 0 0 0 0`,
8864
8908
  "intr 0",
8865
8909
  "ctxt 0",
8866
8910
  `btime ${Math.floor(listing.boot / 1e3)}`,
@@ -12681,6 +12725,7 @@ function nodeLockdown(scope2 = globalThis) {
12681
12725
  init_js_runtime();
12682
12726
 
12683
12727
  // src/process/process-entry.ts
12728
+ init_cpu_clock();
12684
12729
  init_protocol();
12685
12730
  init_process_runtime();
12686
12731
  var loadWasi = () => Promise.resolve().then(() => (init_wasi_runtime(), wasi_runtime_exports));
@@ -12718,6 +12763,8 @@ function processEntry(port3, run = runWasmProcess, wasi = loadWasi, js = loadJs,
12718
12763
  const type = data?.type;
12719
12764
  if (type !== WASM_THREAD_INIT && type !== WASM_PROCESS_INIT) return;
12720
12765
  const refusal2 = sealOnce();
12766
+ const init = data;
12767
+ if (type === WASM_THREAD_INIT || init?.program?.abi !== "js") useLocalClock(init?.sab);
12721
12768
  if (type === WASM_THREAD_INIT) {
12722
12769
  if (refusal2) return failed5(refusal2.error);
12723
12770
  wasi().then((m) => m.runWasiThread(data, port3)).catch(failed5);