[PERF] Runtime Telemetry Hot Path
This commit is contained in:
+31
-21
@@ -1,27 +1,37 @@
|
||||
---
|
||||
id: LSN-0026
|
||||
ticket: perf-runtime-telemetry-hot-path
|
||||
title: Modelo de Telemetria Push-based
|
||||
title: Push-based Telemetry Model
|
||||
created: 2026-04-10
|
||||
tags: [performance, telemetry, atomics]
|
||||
---
|
||||
# Modelo de Telemetria Push-based
|
||||
O sistema de telemetria do PROMETEU evoluiu de um modelo de varredura sob demanda (pull) para um modelo de contadores incrementais (push), visando minimizar o impacto no *hot path* do runtime.
|
||||
## O Problema Original
|
||||
Anteriormente, a cada *host tick*, o runtime solicitava informações de uso de memória dos bancos de assets. Isso resultava em:
|
||||
- Varreduras $O(n)$ sobre mapas de recursos.
|
||||
- Múltiplas aquisições de *locks* de leitura em cada tick.
|
||||
- Overhead desnecessário em hardwares handheld, onde cada microssegundo conta.
|
||||
## A Solução: Modelo Push com Atômicos
|
||||
A solução implementada utiliza `AtomicUsize` nos drivers e na VM para manter o estado do sistema em tempo real com custo $O(1)$ de leitura e escrita:
|
||||
1. **Drivers (Assets):** Contadores atômicos em cada `BankPolicy` são atualizados durante `load`, `commit` e `cancel`.
|
||||
2. **VM (Heap):** Um contador `used_bytes` na struct `Heap` rastreia alocações e liberações (sweep).
|
||||
3. **System (Logs):** O `LogService` rastreia a pressão de logs emitida em cada frame.
|
||||
## Dois Níveis de Observabilidade
|
||||
Para equilibrar performance e depuração, a coleta foi dividida:
|
||||
- **Snapshot de Frame (Sempre):** Captura automática no fim de cada frame lógico. Custo irrelevante ($O(1)$). Serve ao `Certifier` e ao log histórico.
|
||||
- **Tick de Host (Sob Demanda):** A coleta detalhada em cada tick só ocorre se `inspection_active` estiver habilitado (ex: Overlay F1 ligado).
|
||||
## Lições Aprendidas
|
||||
- **Desacoplamento de Gatilhos:** Não devemos usar o estado do `Certifier` para habilitar funcionalidades de depuração visual (como o overlay), pois eles têm propósitos e custos diferentes.
|
||||
- **Consistência Eventual é Suficiente:** Para métricas de telemetria, não é necessário travar o sistema para obter um valor exato a cada nanossegundo. A leitura relaxada de atômicos é suficiente e muito mais performática.
|
||||
- **Isolamento de Custo:** Mover a lógica de agregação para o driver simplifica o runtime e garante que o custo de telemetria seja pago apenas durante mutações de estado, e não repetidamente durante a execução estável.
|
||||
|
||||
# Push-based Telemetry Model
|
||||
|
||||
The PROMETEU telemetry system evolved from an on-demand scan model (pull) to an incremental counter model (push), aiming to minimize the impact on the runtime's hot path.
|
||||
|
||||
## The Original Problem
|
||||
|
||||
Previously, at every host tick, the runtime requested memory usage information from the asset banks. This resulted in:
|
||||
- $O(n)$ scans over resource maps.
|
||||
- Multiple read lock acquisitions in every tick.
|
||||
- Unnecessary overhead on handheld hardware, where every microsecond counts.
|
||||
|
||||
## The Solution: Push Model with Atomics
|
||||
|
||||
The implemented solution uses `AtomicUsize` in drivers and the VM to maintain the system state in real-time with $O(1)$ read and write cost:
|
||||
1. **Drivers (Assets):** Atomic counters in each `BankPolicy` are updated during `load`, `commit`, and `cancel`.
|
||||
2. **VM (Heap):** A `used_bytes` counter in the `Heap` struct tracks allocations and deallocations (sweep).
|
||||
3. **System (Logs):** The `LogService` tracks log pressure emitted in each frame.
|
||||
|
||||
## Two Levels of Observability
|
||||
|
||||
To balance performance and debugging, the collection was divided:
|
||||
- **Frame Snapshot (Always):** Automatic capture at the end of each logical frame. Irrelevant cost ($O(1)$). Serves the `Certifier` and historical logs.
|
||||
- **Host Tick (On-Demand):** Detailed collection in every tick only occurs if `inspection_active` is enabled (e.g., F1 Overlay on).
|
||||
|
||||
## Lessons Learned
|
||||
|
||||
- **Trigger Decoupling:** We should not use the `Certifier` state to enable visual debugging features (like the overlay), as they have different purposes and costs.
|
||||
- **Eventual Consistency is Sufficient:** For telemetry metrics, it is not necessary to lock the system to obtain an exact value every nanosecond. Relaxed atomic reading is sufficient and much more performant.
|
||||
- **Cost Isolation:** Moving the aggregation logic to the driver simplifies the runtime and ensures that the telemetry cost is paid only during state mutations, rather than repeatedly during stable execution.
|
||||
|
||||
Reference in New Issue
Block a user