118 lines
4.8 KiB
C
118 lines
4.8 KiB
C
#include "debug_history.h"
|
|
|
|
#include <string.h>
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
// One radio writer; HTTP readers only copy under this lock. Every critical
|
|
// section is bounded by one row/status copy, never a traversal or formatting.
|
|
static portMUX_TYPE s_lock = portMUX_INITIALIZER_UNLOCKED;
|
|
static debug_record_t s_rows[DEBUG_HISTORY_CAPACITY];
|
|
static debug_status_t s_status;
|
|
static uint32_t s_latest;
|
|
|
|
static uint32_t oldest_id(uint32_t latest)
|
|
{
|
|
return latest > DEBUG_HISTORY_CAPACITY
|
|
? latest - DEBUG_HISTORY_CAPACITY + 1 : 1;
|
|
}
|
|
|
|
void debug_history_record(const debug_record_t *record)
|
|
{
|
|
// Classify outside the critical section. These boot-lifetime counters are
|
|
// independent of the resettable console/OLED statistics and ring eviction.
|
|
const bool received = strcmp(record->source, "rx") == 0;
|
|
const bool completed = strcmp(record->outcome, "ok") == 0
|
|
|| strcmp(record->outcome, "ok_polled") == 0;
|
|
const bool failed = strcmp(record->outcome, "encode_failed") == 0
|
|
|| strcmp(record->outcome, "start_failed") == 0
|
|
|| strcmp(record->outcome, "radio_timeout") == 0
|
|
|| strcmp(record->outcome, "irq_timeout") == 0;
|
|
const bool skipped = strcmp(record->outcome, "repeat_disabled") == 0
|
|
|| strcmp(record->outcome, "whitelist") == 0
|
|
|| strcmp(record->outcome, "duplicate") == 0
|
|
|| strcmp(record->outcome, "duty_cycle") == 0
|
|
|| strcmp(record->outcome, "hop_policy") == 0;
|
|
|
|
portENTER_CRITICAL(&s_lock);
|
|
const uint32_t id = ++s_latest;
|
|
debug_record_t *slot = &s_rows[(id - 1) % DEBUG_HISTORY_CAPACITY];
|
|
*slot = *record;
|
|
slot->id = id;
|
|
if (received) {
|
|
if (record->crc_ok) {
|
|
s_status.rx_valid++;
|
|
s_status.last_rx_us = record->timestamp_us;
|
|
if (completed) s_status.repeats_completed++;
|
|
else if (failed) s_status.tx_failed++;
|
|
else if (skipped) s_status.skipped++;
|
|
} else {
|
|
s_status.rx_rejected++;
|
|
}
|
|
}
|
|
// Synthetic TX is visible in the last-TX age, never in RX/repeat totals.
|
|
if (completed) s_status.last_tx_us = record->tx_done_us;
|
|
portEXIT_CRITICAL(&s_lock);
|
|
}
|
|
|
|
void debug_history_publish_status(const debug_status_t *status)
|
|
{
|
|
portENTER_CRITICAL(&s_lock);
|
|
// Deliberately do not overwrite counters or last-event timestamps owned
|
|
// by record(), even if this health sample predates a finalized RX.
|
|
s_status.sampled_at_us = status->sampled_at_us;
|
|
s_status.heartbeat_us = status->heartbeat_us;
|
|
s_status.duty_used_us = status->duty_used_us;
|
|
s_status.duty_budget_us = status->duty_budget_us;
|
|
s_status.recoveries = status->recoveries;
|
|
s_status.repeat_delay_ms = status->repeat_delay_ms;
|
|
s_status.chip_mode = status->chip_mode;
|
|
s_status.config_intact = status->config_intact;
|
|
s_status.repeat_enabled = status->repeat_enabled;
|
|
portEXIT_CRITICAL(&s_lock);
|
|
}
|
|
|
|
void debug_history_snapshot(uint32_t after, debug_snapshot_t *out)
|
|
{
|
|
portENTER_CRITICAL(&s_lock);
|
|
out->cursor = s_latest;
|
|
out->status = s_status;
|
|
portEXIT_CRITICAL(&s_lock);
|
|
|
|
out->oldest_id = out->cursor ? oldest_id(out->cursor) : 0;
|
|
out->evicted = out->cursor > DEBUG_HISTORY_CAPACITY
|
|
? out->cursor - DEBUG_HISTORY_CAPACITY : 0;
|
|
out->reset = after > out->cursor
|
|
|| (after != 0 && after < out->evicted);
|
|
out->count = 0;
|
|
if (!out->cursor) return;
|
|
|
|
uint32_t id = after == 0 || out->reset ? out->oldest_id : after + 1;
|
|
// cursor fixes the upper bound at entry, so a busy producer cannot make
|
|
// this read unbounded. The immutable ID is a slot-generation guard: an
|
|
// overwritten slot must never be returned as the originally requested RX.
|
|
while (id != 0 && id <= out->cursor) {
|
|
portENTER_CRITICAL(&s_lock);
|
|
const debug_record_t *slot = &s_rows[(id - 1) % DEBUG_HISTORY_CAPACITY];
|
|
const bool present = slot->id == id;
|
|
if (present) out->rows[out->count] = *slot;
|
|
const uint32_t oldest = present ? 0 : oldest_id(s_latest);
|
|
portEXIT_CRITICAL(&s_lock);
|
|
|
|
if (present) {
|
|
out->count++;
|
|
id++;
|
|
} else {
|
|
// Concurrent eviction: discard the copied prefix and return only
|
|
// a contiguous surviving suffix of the entry-time window. A fully
|
|
// evicted window returns no rows; the next cursor request catches
|
|
// up normally. Never retry or hold the producer while copying 256
|
|
// rows. oldest_id/evicted now describe the observed loss boundary.
|
|
out->reset = true;
|
|
out->count = 0;
|
|
out->oldest_id = oldest;
|
|
out->evicted = oldest - 1;
|
|
id = oldest;
|
|
}
|
|
}
|
|
}
|