Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 27 additions & 40 deletions coordinator/api/profiler_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,23 +198,9 @@ func (b *profileBounds) count(p *int) *int {
return &v
}

func cloneBoolPtr(p *bool) *bool {
if p == nil {
return nil
}
v := *p
return &v
}

func cloneInt64Ptr(p *int64) *int64 {
if p == nil {
return nil
}
v := *p
return &v
}

func cloneIntPtr(p *int) *int {
// cloneProfileValue preserves an absent scalar and gives each projection its
// own value. Range-checked numerics still go through profileBounds.
func cloneProfileValue[T bool | int | int64](p *T) *T {
if p == nil {
return nil
}
Expand Down Expand Up @@ -266,8 +252,8 @@ func decodeInferenceProfile(raw []byte, receivedAt time.Time) (stored *StoredInf

var b profileBounds
stored = &StoredInferenceProfile{
Schema: cloneIntPtr(w.Schema),
WallMS: cloneInt64Ptr(w.WallMS),
Schema: cloneProfileValue(w.Schema),
WallMS: cloneProfileValue(w.WallMS),

DequeuedUS: b.us(w.DequeuedUS),
DecryptedUS: b.us(w.DecryptedUS),
Expand Down Expand Up @@ -318,11 +304,11 @@ func decodeInferenceProfile(raw []byte, receivedAt time.Time) (stored *StoredInf
MLXActiveBytesAtFinish: b.bytes(w.MLXActiveBytesAtFinish),
MLXPeakBytes: b.bytes(w.MLXPeakBytes),

UsageRecovered: cloneBoolPtr(w.UsageRecovered),
LoadCold: cloneBoolPtr(w.LoadCold),
LoadParked: cloneBoolPtr(w.LoadParked),
MTPActive: cloneBoolPtr(w.MTPActive),
LowPowerMode: cloneBoolPtr(w.LowPowerMode),
UsageRecovered: cloneProfileValue(w.UsageRecovered),
LoadCold: cloneProfileValue(w.LoadCold),
LoadParked: cloneProfileValue(w.LoadParked),
MTPActive: cloneProfileValue(w.MTPActive),
LowPowerMode: cloneProfileValue(w.LowPowerMode),

DeadlineMode: w.DeadlineMode.Fold(),
ThermalState: w.ThermalState.Fold(),
Expand Down Expand Up @@ -436,7 +422,8 @@ func spanUS(start, end *int64) *int64 {
// profile into the typed hot columns and the long-tail JSONB. It runs on the
// profile sink path, never on the WS read loop. rec already carries the
// coordinator stamps (WriteDoneUS, CompleteIngressUS, ChunksIn, ReceivedAt)
// this needs; the AttemptProfile is not consulted.
// this needs; the AttemptProfile supplies retained terminal usage for the
// consistency check.
//
// The typed columns, the transport estimate and the consistency flag are
// filled only from a VALID profile; a range/order-flagged profile keeps its
Expand Down Expand Up @@ -472,27 +459,27 @@ func (s *Server) applyProviderProfile(rec *store.RequestProfileRecord, ap *regis
return
}

rec.ProvTotalUS = cloneInt64Ptr(stored.TotalUS)
rec.ProvFirstDeltaUS = cloneInt64Ptr(stored.FirstDeltaUS)
rec.ProvEngineSubmitUS = cloneInt64Ptr(stored.EngineSubmitUS)
rec.ProvEngineAdmittedUS = cloneInt64Ptr(stored.EngineAdmittedUS)
rec.ProvTotalUS = cloneProfileValue(stored.TotalUS)
rec.ProvFirstDeltaUS = cloneProfileValue(stored.FirstDeltaUS)
rec.ProvEngineSubmitUS = cloneProfileValue(stored.EngineSubmitUS)
rec.ProvEngineAdmittedUS = cloneProfileValue(stored.EngineAdmittedUS)
rec.ProvPromptPrepUS = spanUS(stored.PromptPrepStartUS, stored.PromptPrepEndUS)
rec.ProvLoadWaitUS = spanUS(stored.LoadWaitStartUS, stored.LoadWaitEndUS)
rec.ProvLoadCold = cloneBoolPtr(stored.LoadCold)
rec.ProvRunningAtAdmit = cloneIntPtr(stored.RunningAtAdmit)
rec.ProvWaitingAtAdmit = cloneIntPtr(stored.WaitingAtAdmit)
rec.ProvKVBytesInUseAtAdmit = cloneInt64Ptr(stored.KVBytesInUseAtAdmit)
rec.ProvLoadCold = cloneProfileValue(stored.LoadCold)
rec.ProvRunningAtAdmit = cloneProfileValue(stored.RunningAtAdmit)
rec.ProvWaitingAtAdmit = cloneProfileValue(stored.WaitingAtAdmit)
rec.ProvKVBytesInUseAtAdmit = cloneProfileValue(stored.KVBytesInUseAtAdmit)
rec.ProvCancelStage = string(stored.CancelStage)
if e := stored.Engine; e != nil {
rec.EngQueueWaitNS = cloneInt64Ptr(e.AdmittedNS)
rec.EngFirstTokenNS = cloneInt64Ptr(e.FirstTokenNS)
rec.EngPromptComputedNS = cloneInt64Ptr(e.PromptComputedNS)
rec.EngPrefillChunks = cloneIntPtr(e.PrefillChunks)
rec.EngDecodeSteps = cloneIntPtr(e.DecodeSteps)
rec.EngMTPAccepted = cloneIntPtr(e.MTPAccepted)
rec.EngQueueWaitNS = cloneProfileValue(e.AdmittedNS)
rec.EngFirstTokenNS = cloneProfileValue(e.FirstTokenNS)
rec.EngPromptComputedNS = cloneProfileValue(e.PromptComputedNS)
rec.EngPrefillChunks = cloneProfileValue(e.PrefillChunks)
rec.EngDecodeSteps = cloneProfileValue(e.DecodeSteps)
rec.EngMTPAccepted = cloneProfileValue(e.MTPAccepted)
rec.EngFinishReason = string(e.FinishReason)
}
rec.SleptUS = cloneInt64Ptr(stored.SleptUS)
rec.SleptUS = cloneProfileValue(stored.SleptUS)

// Consistency flag (never invalidates). Two checks, each evaluated
// independently and only when both of its inputs are present:
Expand Down
11 changes: 5 additions & 6 deletions coordinator/api/profiler_record.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package api

// Builds the persisted store.RequestProfileRecord from an in-memory
// registry.RequestProfile / AttemptProfile. Runs on whichever goroutine
// finalized the attempt (never under any registry lock) and only enqueues onto
// the profile sink; all JSON encoding of the decision context happens here,
// off the reserve path.
// registry.RequestProfile / AttemptProfile on the profile sink worker.
// Finalizing goroutines only enqueue; flattening, decision JSON encoding and
// sampling happen off the request and reserve paths.

import (
"encoding/json"
Expand All @@ -23,7 +22,7 @@ const (
providerProfileAbsent = "absent"
)

// foldChipFamily maps a provider-reported chip family to {m1,m2,m3,m4,m5,other}.
// foldChipFamily maps m1 through m9 prefixes to their family, else other.
func foldChipFamily(raw string) string {
v := strings.ToLower(strings.TrimSpace(raw))
switch v {
Expand Down Expand Up @@ -335,7 +334,7 @@ func (p *profiler) alwaysRecord(rec *store.RequestProfileRecord) bool {
}

// finalizeAttemptProfile is the ProfileFinalizeFn installed on every request
// profile: build the row, apply sampling, enqueue.
// profile: enqueue the attempt for row construction and sampling on the sink.
func (s *Server) finalizeAttemptProfile(rp *registry.RequestProfile, ap *registry.AttemptProfile) {
if s == nil || s.profiler == nil || !s.profiler.enabled || s.profiler.sink == nil {
return
Expand Down
8 changes: 7 additions & 1 deletion docs/architecture/system-profiler.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# System profiler

> Last updated: 2026-09-08 · commit `0c162cdae`
> Last updated: 2026-09-12 · commit `efaa74522`

The profiler answers "where did the time go, and what did the router know when
it chose?" for one request, without carrying a single prompt-derived byte. It
Expand Down Expand Up @@ -184,6 +184,12 @@ profile wins. Everything else runs on the profile-sink worker
(`decodeInferenceProfile`, `applyProviderProfile`, `coordinator/api/profiler_provider.go`)
after the terminal has been fully processed.

The stored projection owns its optional scalar values. `cloneProfileValue`
preserves `nil`, `false` and zero while copying each scalar; `profileBounds`
performs the separate range checks (`coordinator/api/profiler_provider.go`).
The provider wire struct, stored profile and queryable columns keep their
existing field-by-field mappings and nullability.

| Step | Rule | Outcome (`provider_profile_invalid_reason`) |
|---|---|---|
| 1 | no object on the terminal | `absent` (`providerProfileAbsent`, `profiler_record.go`) |
Expand Down
Loading