Skip to content

feat(memory): TraceSink API — one event stream for phases, kernels, adapters, allocations, counters and plans (SKEEP-003 P2, S1.5) - #1061

Merged
michalharakal merged 3 commits into
developfrom
feature/1017-trace-sink-api
Aug 23, 2026
Merged

michalharakal merged 3 commits into
developfrom
feature/1017-trace-sink-api

Conversation

@michalharakal

Copy link
Copy Markdown
Contributor

Summary

SKEEP-003 slice S1.5 (milestone M1 #1002, PRD M1-F7 — API half; the Perfetto / JFR / android.os.Trace exporters follow in #1025): TraceSink, the one event stream that stage-performance measurement, allocation tracking and the debugger surfaces consume (§4.9).

  • sk.ainet.lang.memory.trace.TraceEvent (sealed, opt-in @ExperimentalMemoryApi): PhaseBegin / PhaseEnd (load · compile · prefill · decode(step) · module spans), KernelRun (op, kernel key, input/output TensorIds, bytes read/written, duration), AdapterInserted (kind, from/to Format, bytes, target, scope — the "hidden 12 GB" of GGUF DEQUANTIZE_TO_FP32 over-allocates: 1.1B Q4_K_M needs >12 GB heap transiently (~4.4 GB legit) #782 becomes visible), Allocation / Free (storage id, ScopeKind, bytes, origin TensorId, debug site), ScopeReset, Counter, Plan (the M0 MemoryPlan, for the plan-vs-actual check [S1.9] M1: plan-vs-actual — allocation-event totals vs MemoryPlan, printed; CI assertion (> 10 % fails) #1030). TraceClock = monotonic nanoseconds.
  • TraceSink (isEnabled + emit), NoopTraceSink (default — one check, no allocation), RecordingTraceSink (ring buffer with emitted/dropped counters), CompositeTraceSink; inline helpers TraceSink.phase { } and TraceSink.kernel { } that short-circuit when disabled and time the block (also on exception); MemoryPlan.emit(sink).
  • ExecutionContext.traceSink default member → NoopTraceSink; opt-in per context, no change for existing implementations.
  • TraceSinkTest (ring capacity/order, helpers incl. the exception path, identity fields, composite fan-out, plan event, clock). BCV dumps regenerated — lang-core, backend-cpu, compile-dag gain the inherited getTraceSink(); additions only.

Lands before the Storage slices (#1018…) so allocation events exist from the first Storage.allocate.

Test plan

Full local gate (scripts/pr-gate.sh, JDK 25); results in the first comment. Targeted: lang-core sk.ainet.lang.memory.* 30/30.

Closes #1017

🤖 Generated with Claude Code

michalharakal and others added 2 commits August 23, 2026 11:18
…dapters, allocations, counters and plans (SKEEP-003 P2)

Milestone M1 (#1002), PRD M1-F7 (API half; exporters follow in #1025).

- sk.ainet.lang.memory.trace.TraceEvent (sealed): PhaseBegin/PhaseEnd
  (load · compile · prefill · decode(step) · module spans), KernelRun
  (op, kernel key, input/output TensorIds, bytes read/written, duration),
  AdapterInserted (kind, from/to Format, bytes, target, scope), Allocation
  / Free (storageId, ScopeKind, bytes, origin TensorId, debug site),
  ScopeReset (live bytes before/after), Counter (platform samples), Plan
  (the M0 MemoryPlan, for the plan-vs-actual check #1030). TraceClock:
  monotonic nanoseconds.
- TraceSink (isEnabled + emit), NoopTraceSink (the default: one check,
  no allocation), RecordingTraceSink (ring buffer with emitted/dropped
  counters; the debugger, tests and #1030 read it), CompositeTraceSink;
  inline helpers TraceSink.phase { } and TraceSink.kernel { } that
  short-circuit when disabled and time the block; MemoryPlan.emit(sink).
- ExecutionContext.traceSink: default member returning NoopTraceSink —
  opt-in per context, no change for existing implementations.
- TraceSinkTest (ring buffer, ordering, phase/kernel helpers incl.
  exception path, identity fields on events, composite fan-out, plan
  event, clock). BCV dumps regenerated (lang-core, backend-cpu,
  compile-dag gain the inherited getTraceSink()).

Closes #1017

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…ageId, StorageClosedException, Heap on every target (SKEEP-003 P2)

Milestone M1 (#1002), PRD M1-F1 (core). SKEEP-003 §4.2–4.4, rules 1–2:
exactly one byte owner; ownership is a constructor argument and then
enforced.

- sk.ainet.lang.memory.Storage (sealed, AutoCloseable): id, sizeBytes,
  owner, domain, scope (from the owner), debugOrigin TensorId, isAlive,
  isMutable, checkAlive() → StorageClosedException(id, origin), close()
  (idempotent; owned = freed once, borrowed = forgotten, alias =
  detached; emits TraceEvent.Free), slice() → Owner.Alias view that
  keeps the parent alive, delegates mutability and dies with it.
  Kinds: Heap (final, common), OffHeap / Mapped / Device (abstract,
  bound per platform in #1019/#1020; Device is a placeholder).
- Storage.Heap: one Kotlin array (floats | ints | bytes) + arrayOffset +
  sizeBytes; floats()/ints()/bytes() allocate owned storage for a scope
  kind and emit TraceEvent.Allocation; wrap(array, …) borrows without
  copying (the #782 copyOf replacement) and is never freed by us.
  Kernels unwrap once per call (floats/ints/bytes + arrayOffset), per
  the Phase-2 spike (#1016).
- Owner (sealed): Owned(scope) | Borrowed(external) | Alias(parent).
  StorageId: monotonic per-process value class (kotlin.concurrent.atomics).
  MemoryDomain gains HOST_OFFHEAP (additive).
- StorageTest: owned lifecycle + trace events, ids, borrowed release
  without touching the lender's bytes, alias lifetime/mutability/
  nesting, slice bounds/alignment, rendering. BCV: lang-core jvm dump
  regenerated (additions only).

Closes #1018

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@michalharakal

Copy link
Copy Markdown
Contributor Author

Local gate scripts/pr-gate.sh (JDK 25) on d1bae07:

=== pr-gate: JVM tests ===                                      BUILD SUCCESSFUL in 4m 49s
=== pr-gate: apiCheck ===                                       BUILD SUCCESSFUL in 18s
=== pr-gate: verifyNpmPins jsTest wasmJsTest wasmWasiTest ===   BUILD SUCCESSFUL in 2m 42s
=== pr-gate: linuxX64Test ===                                   BUILD SUCCESSFUL in 1m 30s
=== pr-gate: assemble ===                                       BUILD SUCCESSFUL
=== pr-gate: :skainet-test:skainet-test-java:test ===           BUILD SUCCESSFUL
pr-gate: all legs passed.

Targeted: lang-core sk.ainet.lang.memory.* 30/30.

@github-actions

Copy link
Copy Markdown

📖 Documentation Preview

The documentation has been built successfully for this PR.

Generated Files:

  • Operator documentation: docs/modules/operators/_generated_/
  • JSON schema output: operators.json

Artifacts:

  • Download the documentation-preview-1061 artifact to view the complete documentation locally.

This comment will be updated automatically when the PR is updated.

…-common-heap

feat(memory): Storage — the one byte owner: sealed kinds, Owner, StorageId, StorageClosedException, Heap on every target (SKEEP-003 P2, S1.1a)
@github-actions

Copy link
Copy Markdown

📖 Documentation Preview

The documentation has been built successfully for this PR.

Generated Files:

  • Operator documentation: docs/modules/operators/_generated_/
  • JSON schema output: operators.json

Artifacts:

  • Download the documentation-preview-1061 artifact to view the complete documentation locally.

This comment will be updated automatically when the PR is updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[S1.5] P2: TraceSink API + phase/kernel/adapter/allocation event types (no-op default)

1 participant