@mica-js/simulator 2.7.0 → 2.8.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/dist/index.js CHANGED
@@ -3443,6 +3443,27 @@ var DIRECT_REBUILD_PATIENCE = 4;
3443
3443
  function directRebuildStride(compiled) {
3444
3444
  return compiled < DIRECT_REBUILD_LINEAR_LIMIT ? 1 : Math.max(DIRECT_REBUILD_MIN_BLOCKS, compiled);
3445
3445
  }
3446
+ var checkpointStatsByJit = /* @__PURE__ */ new WeakMap();
3447
+ /** Create the opt-in checkpoint counters once, on the first instrumented Direct frame. */
3448
+ function directCheckpointStats(jit) {
3449
+ let stats = checkpointStatsByJit.get(jit);
3450
+ if (stats === void 0) {
3451
+ stats = {
3452
+ publications: 0,
3453
+ finishes: 0,
3454
+ emptySuffixes: 0,
3455
+ boundaryCalls: 0,
3456
+ continues: 0,
3457
+ exits: 0
3458
+ };
3459
+ checkpointStatsByJit.set(jit, stats);
3460
+ }
3461
+ return stats;
3462
+ }
3463
+ /** Read counters without creating them; undefined means diagnostics stayed off. */
3464
+ function readDirectCheckpointStats(jit) {
3465
+ return checkpointStatsByJit.get(jit);
3466
+ }
3446
3467
  var barriers = /* @__PURE__ */ new WeakMap();
3447
3468
  function directBarriers(jit) {
3448
3469
  let entry = barriers.get(jit);
@@ -3589,7 +3610,7 @@ function ensureMetadataForPc(board, jit, pc) {
3589
3610
  if (board.execution === "throughput" && board.jits.length === 1 && region.memory === jit.options.code) needed.push(...primaryCodeRegions(jit));
3590
3611
  ensureMetadata(board, jit, needed);
3591
3612
  }
3592
- /** Publish the current guard sets into the masks the compiled blocks test at entry. */
3613
+ /** Publish the current subscriber sets into the masks the compiled blocks test on writes. */
3593
3614
  function syncDirectBarriers(jit) {
3594
3615
  const entry = directBarriers(jit);
3595
3616
  const { stateIndex, memoryIndex } = entry;
@@ -3605,31 +3626,67 @@ function syncDirectBarriers(jit) {
3605
3626
  fill(entry.stateMask, stateIndex, jit.directStateGuards);
3606
3627
  fill(entry.memoryMask, memoryIndex, jit.directMemoryGuards);
3607
3628
  }
3629
+ /** Settle one checkpoint boundary and attribute it to its source when diagnostics are on. */
3630
+ function checkpointBoundary(board, stats, source) {
3631
+ if (stats !== void 0) stats.boundaryCalls++;
3632
+ board.boundary(source);
3633
+ }
3634
+ /** Reverse-map the hook's resource slots; slot values are not bank indices. */
3635
+ function entryBlockedNames(jit) {
3636
+ const states = /* @__PURE__ */ new Set();
3637
+ const memories = /* @__PURE__ */ new Set([jit.options.code]);
3638
+ const instance = jit.instance;
3639
+ for (const [name, slot] of instance.stateResources) if (jit.directHookReads.has(slot)) states.add(name);
3640
+ for (const [name, slot] of instance.memoryResources) if (jit.directHookReads.has(slot)) memories.add(name);
3641
+ return {
3642
+ states,
3643
+ memories
3644
+ };
3645
+ }
3608
3646
  function refreshDirectGuards(jit, hookReads) {
3609
- let changed = false;
3647
+ let observersChanged = false;
3648
+ let entryChanged = false;
3610
3649
  const instance = jit.instance;
3611
3650
  const hookCount = jit.directHookReads.size;
3612
- if (hookReads) for (const slot of hookReads.keys()) jit.directHookReads.add(slot);
3613
- if (jit.directObserverRevision === instance.observerRevision && hookCount === jit.directHookReads.size) return;
3651
+ if (hookReads) for (const slot of hookReads.keys()) {
3652
+ if (jit.directHookReads.has(slot)) continue;
3653
+ jit.directHookReads.add(slot);
3654
+ let resource = false;
3655
+ for (const candidate of instance.stateResources.values()) if (candidate === slot) {
3656
+ resource = true;
3657
+ break;
3658
+ }
3659
+ if (!resource) {
3660
+ for (const candidate of instance.memoryResources.values()) if (candidate === slot) {
3661
+ resource = true;
3662
+ break;
3663
+ }
3664
+ }
3665
+ if (resource) entryChanged = true;
3666
+ }
3667
+ if (jit.directObserverRevision === instance.observerRevision && hookCount === jit.directHookReads.size && !entryChanged) return;
3614
3668
  jit.directObserverRevision = instance.observerRevision;
3615
3669
  for (const [name, slot] of instance.stateResources) {
3616
- const blocked = slot.subscribers.size > 0 || jit.directHookReads.has(slot);
3670
+ const blocked = slot.subscribers.size > 0;
3617
3671
  if (jit.directStateGuards.has(name) === blocked) continue;
3618
3672
  if (blocked) jit.directStateGuards.add(name);
3619
3673
  else jit.directStateGuards.delete(name);
3620
- changed = true;
3674
+ observersChanged = true;
3621
3675
  }
3622
3676
  for (const [name, slot] of instance.memoryResources) {
3623
- const blocked = slot.subscribers.size > 0 || jit.directHookReads.has(slot) || name === jit.options.code;
3677
+ const blocked = slot.subscribers.size > 0;
3624
3678
  if (jit.directMemoryGuards.has(name) === blocked) continue;
3625
3679
  if (blocked) jit.directMemoryGuards.add(name);
3626
3680
  else jit.directMemoryGuards.delete(name);
3627
- changed = true;
3681
+ observersChanged = true;
3628
3682
  }
3629
- if (changed) {
3630
- syncDirectBarriers(jit);
3631
- jit.directSkip = void 0;
3683
+ if (entryChanged && jit.direct !== void 0) {
3684
+ jit.direct = void 0;
3685
+ jit.directPrograms.clear();
3686
+ jit.directRevision = -1;
3632
3687
  }
3688
+ if (observersChanged) syncDirectBarriers(jit);
3689
+ if (observersChanged || entryChanged) jit.directSkip = void 0;
3633
3690
  }
3634
3691
  /**
3635
3692
  * Direct programs advance `frame.time` between host calls, while the board only adopts the
@@ -3721,6 +3778,7 @@ function flushDirect(board, instance, layout, frame) {
3721
3778
  function createDirectFrame(board, jit, layout, program, barrier) {
3722
3779
  const instance = jit.instance;
3723
3780
  const { pinObjects, pinNames, pinElectricalSlots } = layout;
3781
+ const checkpointStats = board.checkpointDiagnostics ? directCheckpointStats(jit) : void 0;
3724
3782
  const frame = {
3725
3783
  states: layout.stateValues,
3726
3784
  memories: [...layout.memoryBanks],
@@ -3751,30 +3809,39 @@ function createDirectFrame(board, jit, layout, program, barrier) {
3751
3809
  frame.host = directHost(board, instance, frame);
3752
3810
  if (jit.metadata === void 0) {
3753
3811
  frame.publishCheckpoint = () => {
3812
+ if (checkpointStats !== void 0) checkpointStats.publications++;
3754
3813
  flushDirect(board, instance, layout, frame);
3755
- board.boundary();
3814
+ checkpointBoundary(board, checkpointStats, "checkpointPublish");
3756
3815
  if (jit.directProgramRevision === -1 || !board.canContinueDirect(jit, frame.due)) {
3757
3816
  frame.boundarySettled = true;
3817
+ if (checkpointStats !== void 0) checkpointStats.exits++;
3758
3818
  return false;
3759
3819
  }
3760
3820
  board.current = instance;
3821
+ if (checkpointStats !== void 0) checkpointStats.continues++;
3761
3822
  return true;
3762
3823
  };
3763
3824
  if (jit.options.beforeInstruction === void 0) {
3764
3825
  const pcIndex = layout.states.get(jit.options.pc);
3765
3826
  frame.finishCheckpoint = (deadline, emptySuffix) => {
3827
+ if (checkpointStats !== void 0) {
3828
+ checkpointStats.finishes++;
3829
+ if (emptySuffix) checkpointStats.emptySuffixes++;
3830
+ }
3766
3831
  if (emptySuffix) board.time = frame.time;
3767
3832
  else flushDirect(board, instance, layout, frame);
3768
- board.boundary();
3833
+ checkpointBoundary(board, checkpointStats, "checkpointFinish");
3769
3834
  const direct = jit.direct;
3770
3835
  const nextPc = frame.states[pcIndex];
3771
3836
  if (jit.directProgramRevision === -1 || direct?.frame !== frame || !direct.pcs.has(nextPc) || jit.directSkip === nextPc || jit.directRevision !== jit.revision && jit.revision - jit.directRevision >= directRebuildStride(direct.pcs.size) || frame.time >= deadline || board.events.time < deadline || !emptySuffix && !board.canContinueDirect(jit, frame.time)) {
3772
3837
  frame.boundarySettled = true;
3838
+ if (checkpointStats !== void 0) checkpointStats.exits++;
3773
3839
  return false;
3774
3840
  }
3775
3841
  if (jit.directObserverRevision !== instance.observerRevision) refreshDirectGuards(jit);
3776
3842
  frame.stopAfterInstruction = false;
3777
3843
  board.current = instance;
3844
+ if (checkpointStats !== void 0) checkpointStats.continues++;
3778
3845
  return true;
3779
3846
  };
3780
3847
  }
@@ -3840,8 +3907,9 @@ function runDirect(board, jit, hookReads) {
3840
3907
  jit.directRevision = jit.revision;
3841
3908
  } else {
3842
3909
  const { states, memories, pins } = layout;
3910
+ const entry = entryBlockedNames(jit);
3843
3911
  const cache = jit.directCache ??= compileDirectJit.createCache();
3844
- const program = compileDirectJit(instructions, {
3912
+ const binding = {
3845
3913
  states,
3846
3914
  memories,
3847
3915
  pins,
@@ -3853,8 +3921,13 @@ function runDirect(board, jit, hookReads) {
3853
3921
  ...jit.functions === void 0 ? {} : { functions: jit.functions },
3854
3922
  split: true,
3855
3923
  shardSize: DIRECT_SHARD_SIZE,
3856
- delayLoops
3857
- }, cache);
3924
+ delayLoops,
3925
+ entryBlockedStates: entry.states,
3926
+ entryBlockedMemories: entry.memories,
3927
+ blockedStates: jit.directStateGuards,
3928
+ blockedMemories: jit.directMemoryGuards
3929
+ };
3930
+ const program = compileDirectJit(instructions, binding, cache);
3858
3931
  jit.directRevision = jit.revision;
3859
3932
  jit.directMisses.clear();
3860
3933
  const stats = directStats(jit);
@@ -3897,7 +3970,7 @@ function runDirect(board, jit, hookReads) {
3897
3970
  wrote = flushDirect(board, instance, layout, frame);
3898
3971
  }
3899
3972
  if (frame.boundarySettled) frame.boundarySettled = false;
3900
- else if (reason !== "fallback" || wrote || board.time !== start) board.boundary();
3973
+ else if (reason !== "fallback" || wrote || board.time !== start) board.boundary("runDirect");
3901
3974
  if (reason === "fallback") {
3902
3975
  const nextPc = finite(instance.states.get(jit.options.pc), "pc");
3903
3976
  if (!(effectivePolling(jit).has(nextPc) || jit.cache.get(nextPc)?.polling)) {
@@ -3950,7 +4023,7 @@ function waitForPolling(board, jit, dependencies, duration, advance) {
3950
4023
  jit.pollingPeriod = 0;
3951
4024
  board.parkWaiter(jit);
3952
4025
  const token = ++jit.pollingToken;
3953
- board.boundary();
4026
+ board.boundary("polling");
3954
4027
  if (duration === 0 || advance === void 0) return;
3955
4028
  const due = time(board.time + duration);
3956
4029
  board.events.push(due, () => {
@@ -3965,7 +4038,7 @@ function waitForPolling(board, jit, dependencies, duration, advance) {
3965
4038
  return;
3966
4039
  }
3967
4040
  jit.instance.syncCall(advance, [duration]);
3968
- board.boundary();
4041
+ board.boundary("polling");
3969
4042
  board.queueJit(jit);
3970
4043
  });
3971
4044
  }
@@ -3975,7 +4048,7 @@ function jitSignal(board, jit, signal) {
3975
4048
  if (duration === 0) return false;
3976
4049
  const due = time(board.time + duration);
3977
4050
  board.events.push(due, () => board.queueJit(jit));
3978
- board.boundary();
4051
+ board.boundary("jitSignal");
3979
4052
  return true;
3980
4053
  }
3981
4054
  if (signal.kind === "waitUntil") {
@@ -3985,13 +4058,13 @@ function jitSignal(board, jit, signal) {
3985
4058
  jit.pollingWait = false;
3986
4059
  jit.dependencies = checked.dependencies;
3987
4060
  board.parkWaiter(jit);
3988
- board.boundary();
4061
+ board.boundary("jitSignal");
3989
4062
  return true;
3990
4063
  }
3991
4064
  if (signal.kind === "stop") {
3992
4065
  jit.stopped = true;
3993
4066
  jit.execution = void 0;
3994
- board.boundary();
4067
+ board.boundary("jitSignal");
3995
4068
  return true;
3996
4069
  }
3997
4070
  if (signal.kind === "redirect") {
@@ -4003,7 +4076,7 @@ function jitSignal(board, jit, signal) {
4003
4076
  const dependencies = polling ? pollingDependencies(jit, polling, jit.currentPc) : void 0;
4004
4077
  jit.automaticPolling = false;
4005
4078
  jit.pollReads = void 0;
4006
- board.boundary();
4079
+ board.boundary("jitSignal");
4007
4080
  if (duration === 0) board.queueJit(jit);
4008
4081
  else board.events.push(time(board.time + duration), () => {
4009
4082
  if (!jit.active || jit.stopped) return;
@@ -4134,7 +4207,7 @@ function runJit(board, jit) {
4134
4207
  })() : jit.execution.next();
4135
4208
  if (step.done) {
4136
4209
  jit.execution = void 0;
4137
- board.boundary();
4210
+ board.boundary("runJit");
4138
4211
  board.queueJit(jit);
4139
4212
  return;
4140
4213
  }
@@ -4145,6 +4218,41 @@ function runJit(board, jit) {
4145
4218
  //#region src/runtime.ts
4146
4219
  var OBSERVATION_PROBE = Symbol.for("mica-js.simulator/observations");
4147
4220
  var PROCESS_PROBE = Symbol.for("mica-js.simulator/processes");
4221
+ var CHECKPOINT_PROBE = Symbol.for("mica-js.simulator/checkpoints");
4222
+ /**
4223
+ * Internal diagnostics opt-in. It stays a `globalThis` symbol so the runtime remains
4224
+ * browser-compatible and the public SDK surface does not grow:
4225
+ *
4226
+ * `globalThis[Symbol.for('mica-js.simulator/checkpoint-experiment')]?.diagnostics`
4227
+ *
4228
+ * Counters default to off; they classify boundary sources without changing runtime behavior.
4229
+ */
4230
+ var CHECKPOINT_EXPERIMENT = Symbol.for("mica-js.simulator/checkpoint-experiment");
4231
+ var EMPTY_BOUNDARY_SOURCES = Object.freeze({
4232
+ checkpointPublish: 0,
4233
+ checkpointFinish: 0,
4234
+ jitSignal: 0,
4235
+ runJit: 0,
4236
+ runDirect: 0,
4237
+ polling: 0,
4238
+ process: 0,
4239
+ other: 0
4240
+ });
4241
+ function createBoundaryStats() {
4242
+ return {
4243
+ calls: 0,
4244
+ empty: 0,
4245
+ publishes: 0,
4246
+ batches: 0,
4247
+ reactions: 0,
4248
+ captures: 0,
4249
+ sources: { ...EMPTY_BOUNDARY_SOURCES }
4250
+ };
4251
+ }
4252
+ function checkpointDiagnosticsEnabled() {
4253
+ const value = globalThis[CHECKPOINT_EXPERIMENT];
4254
+ return typeof value === "object" && value !== null && value.diagnostics === true;
4255
+ }
4148
4256
  var EMPTY_QUEUE_ENTRY = Object.freeze({});
4149
4257
  var libraries = /* @__PURE__ */ new WeakMap();
4150
4258
  function createDeviceLibrary(entries) {
@@ -4239,6 +4347,9 @@ var BoardRuntime = class {
4239
4347
  pollingWaiterCount = 0;
4240
4348
  jits = [];
4241
4349
  instances = [];
4350
+ /** Aggregate counters are disabled by default so formal runs avoid per-checkpoint writes. */
4351
+ checkpointDiagnostics;
4352
+ boundaryStats;
4242
4353
  network;
4243
4354
  primary;
4244
4355
  execution;
@@ -4266,6 +4377,8 @@ var BoardRuntime = class {
4266
4377
  "execution",
4267
4378
  "cpuQuantum"
4268
4379
  ]);
4380
+ this.checkpointDiagnostics = checkpointDiagnosticsEnabled();
4381
+ this.boundaryStats = this.checkpointDiagnostics ? createBoundaryStats() : void 0;
4269
4382
  const execution = Object.hasOwn(options, "execution") ? options.execution : "throughput";
4270
4383
  if (execution !== "throughput" && execution !== "precise") throw new TypeError("Invalid execution mode");
4271
4384
  this.execution = execution;
@@ -4371,6 +4484,10 @@ var BoardRuntime = class {
4371
4484
  value: () => this.processReport(),
4372
4485
  enumerable: false
4373
4486
  });
4487
+ Object.defineProperty(this, CHECKPOINT_PROBE, {
4488
+ value: () => this.checkpointReport(),
4489
+ enumerable: false
4490
+ });
4374
4491
  } catch (cause) {
4375
4492
  const error = cause instanceof SimulationError ? cause : this.wrap(cause, "INVALID_CONFIGURATION", "assembly", this.current);
4376
4493
  this.primary = error;
@@ -4428,6 +4545,37 @@ var BoardRuntime = class {
4428
4545
  bindings: Object.freeze(this.instances.map((instance) => instance.bindingReport()))
4429
4546
  });
4430
4547
  }
4548
+ /**
4549
+ * Read-only diagnostic view of boundary and Direct-checkpoint counters. Reachable only via
4550
+ * `Symbol.for('mica-js.simulator/checkpoints')`; zero counters mean diagnostics were off.
4551
+ */
4552
+ checkpointReport() {
4553
+ const boundary = this.boundaryStats;
4554
+ return Object.freeze({
4555
+ diagnostics: this.checkpointDiagnostics,
4556
+ boundary: Object.freeze({
4557
+ calls: boundary?.calls ?? 0,
4558
+ empty: boundary?.empty ?? 0,
4559
+ publishes: boundary?.publishes ?? 0,
4560
+ batches: boundary?.batches ?? 0,
4561
+ reactions: boundary?.reactions ?? 0,
4562
+ captures: boundary?.captures ?? 0,
4563
+ sources: Object.freeze({ ...boundary?.sources ?? EMPTY_BOUNDARY_SOURCES })
4564
+ }),
4565
+ checkpoints: Object.freeze(this.instances.flatMap((instance) => instance.jits.map((jit) => {
4566
+ const stats = readDirectCheckpointStats(jit);
4567
+ return Object.freeze({
4568
+ instance: instance.id,
4569
+ publications: stats?.publications ?? 0,
4570
+ finishes: stats?.finishes ?? 0,
4571
+ emptySuffixes: stats?.emptySuffixes ?? 0,
4572
+ boundaryCalls: stats?.boundaryCalls ?? 0,
4573
+ continues: stats?.continues ?? 0,
4574
+ exits: stats?.exits ?? 0
4575
+ });
4576
+ })))
4577
+ });
4578
+ }
4431
4579
  assertAlive(instance) {
4432
4580
  if (this.status === "disposed" || instance && !instance.valid) throw this.error("OBJECT_DISPOSED", "Object is disposed", instance);
4433
4581
  }
@@ -4857,10 +5005,25 @@ var BoardRuntime = class {
4857
5005
  current.splice(at, 0, after);
4858
5006
  }
4859
5007
  }
4860
- boundary() {
5008
+ boundary(source = "other") {
5009
+ const stats = this.boundaryStats;
5010
+ if (stats !== void 0) {
5011
+ stats.calls++;
5012
+ stats.sources[source]++;
5013
+ }
5014
+ let worked = false;
4861
5015
  for (;;) {
4862
- if (this.pendingElectricalSlot !== -1) this.publish();
4863
- if (!this.observations.size) return;
5016
+ if (this.pendingElectricalSlot !== -1) {
5017
+ if (stats !== void 0) stats.publishes++;
5018
+ this.publish();
5019
+ worked = true;
5020
+ }
5021
+ if (!this.observations.size) {
5022
+ if (stats !== void 0 && !worked) stats.empty++;
5023
+ return;
5024
+ }
5025
+ if (stats !== void 0) stats.batches++;
5026
+ worked = true;
4864
5027
  const reactions = this.reactionBatch;
4865
5028
  if (reactions.length) reactions.length = 0;
4866
5029
  const changes = this.changeObservations;
@@ -4875,8 +5038,10 @@ var BoardRuntime = class {
4875
5038
  this.changePrevious.length = 0;
4876
5039
  this.changeCurrent.length = 0;
4877
5040
  }
4878
- if (reactions.length) this.runBoundaryReactions(reactions);
4879
- else return;
5041
+ if (reactions.length) {
5042
+ if (stats !== void 0) stats.reactions += reactions.length;
5043
+ this.runBoundaryReactions(reactions);
5044
+ } else return;
4880
5045
  }
4881
5046
  }
4882
5047
  /** One due receiver: chains collapse their members, plain sources apply inline. */
@@ -4895,7 +5060,9 @@ var BoardRuntime = class {
4895
5060
  /** Shared capture barrier and ordered body runner for generic and static boundaries. */
4896
5061
  runBoundaryReactions(reactions) {
4897
5062
  try {
5063
+ const stats = this.boundaryStats;
4898
5064
  for (const reaction of reactions) if (reaction.declaration.captures.length) {
5065
+ if (stats !== void 0) stats.captures += reaction.declaration.captures.length;
4899
5066
  const frame = reaction.frame;
4900
5067
  frame.captures = reaction.declaration.captures.map((capture) => snapshot(computation(capture.value, reaction.instance.host)));
4901
5068
  }
@@ -4953,7 +5120,7 @@ var BoardRuntime = class {
4953
5120
  for (;;) {
4954
5121
  const step = process.execution.next(value);
4955
5122
  if (step.done) {
4956
- this.boundary();
5123
+ this.boundary("process");
4957
5124
  process.execution = void 0;
4958
5125
  process.state = "idle";
4959
5126
  if (process.pending) {
@@ -4971,13 +5138,13 @@ var BoardRuntime = class {
4971
5138
  }
4972
5139
  process.state = "waiting";
4973
5140
  channel.receiver = process;
4974
- this.boundary();
5141
+ this.boundary("process");
4975
5142
  return;
4976
5143
  }
4977
5144
  if (signal.kind !== "ticks") throw new TypeError("Invalid process suspension");
4978
5145
  const duration = time(signal.duration), due = time(this.time + duration);
4979
5146
  process.state = "waiting";
4980
- this.boundary();
5147
+ this.boundary("process");
4981
5148
  if (duration === 0) {
4982
5149
  process.state = "queued";
4983
5150
  this.models.push(process);
@@ -5005,7 +5172,7 @@ var BoardRuntime = class {
5005
5172
  for (;;) {
5006
5173
  state.value = input;
5007
5174
  if (!program.run(host, void 0, void 0, state)) {
5008
- this.boundary();
5175
+ this.boundary("process");
5009
5176
  process.state = "idle";
5010
5177
  if (process.pending) {
5011
5178
  process.pending = false;
@@ -5021,12 +5188,12 @@ var BoardRuntime = class {
5021
5188
  }
5022
5189
  process.state = "waiting";
5023
5190
  channel.receiver = process;
5024
- this.boundary();
5191
+ this.boundary("process");
5025
5192
  return;
5026
5193
  }
5027
5194
  const duration = state.duration, due = time(this.time + duration);
5028
5195
  process.state = "waiting";
5029
- this.boundary();
5196
+ this.boundary("process");
5030
5197
  if (duration === 0) {
5031
5198
  process.state = "queued";
5032
5199
  this.models.push(process);