Add perf console (render fps, flush cost, CPU idle, heap) for the display work; record idle baseline
Firmware / build (push) Successful in 4m8s

This commit is contained in:
2026-09-27 16:43:53 +02:00
parent 43ca6b96ec
commit d3e499efed
8 changed files with 277 additions and 4 deletions
+5 -1
View File
@@ -13,6 +13,7 @@
#include "modules/settings_screen.h" // Hand-coded settings screen (swipe left from Driveline)
#include "modules/serial_lock.h" // Global serial write mutex
#include "modules/tunables.h" // Gauge calibration + alert thresholds (NVS, serial console)
#include "modules/perf_monitor.h" // `perf` console: render fps, flush cost, CPU idle, heap
#include "modules/espnow_link.h" // ESP-NOW transmit leg of the link to Controller B
#include "modules/diag_probe.h" // On-demand OBD-II requests (`diag` console) - the only CAN transmitter
#include "modules/ota_update.h" // Manual firmware updates from Gitea releases (`ota` console)
@@ -57,7 +58,8 @@ static bool memCommand(const char* line) {
// Console lines `cfg` doesn't recognise: diagnostic probe, OTA, Wi-Fi, mem.
static bool consoleExtraCommand(const char* line) {
return DiagProbe::handleCommand(line) || Ota::handleCommand(line) ||
WiFiManager::handleCommand(line) || memCommand(line);
WiFiManager::handleCommand(line) || memCommand(line) ||
PerfMon::handleCommand(line);
}
// ============================================================================
@@ -94,6 +96,7 @@ void setup()
Tunables::init();
DiagProbe::init();
Tunables::setExtraCommandHandler(consoleExtraCommand);
PerfMon::init();
// Load saved switch states from NVS
ESP_LOGI("MAIN", "Loading saved switch states from NVS...");
@@ -154,6 +157,7 @@ void loop()
// LVGL renders in its own task; the screen ticks below only refresh values.
// Serial console: gauge tunables (cfg ...), diagnostic probe (diag ...), updates (ota ...)
Tunables::pollSerial();
PerfMon::tick();
DiagProbe::tick();
Ota::tick(); // confirms a freshly installed image after healthy uptime
+179
View File
@@ -0,0 +1,179 @@
/*
* Performance Monitor implementation. See perf_monitor.h.
*
* onFrame()/onFlush() run in the LVGL task; everything else in loop(). The
* counters are plain 32-bit words read under a short critical section, so the
* hooks cost nothing measurable.
*/
#include "perf_monitor.h"
#include "serial_lock.h"
#include "display/lvgl_v8_port.h"
#include <lvgl.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <esp_heap_caps.h>
#include <esp_timer.h>
#include <string.h>
namespace PerfMon {
// ---- accumulators (LVGL task writes, loop reads) ---------------------------
static portMUX_TYPE g_mux = portMUX_INITIALIZER_UNLOCKED;
static uint32_t g_frames = 0;
static uint32_t g_frame_ms_sum = 0;
static uint32_t g_frame_ms_max = 0;
static uint32_t g_frame_px_sum = 0;
static uint32_t g_flushes = 0;
static uint32_t g_flush_us_sum = 0;
static uint32_t g_flush_px_sum = 0;
// ---- window bookkeeping (loop) --------------------------------------------
static int64_t g_window_start_us = 0;
static uint64_t g_idle_start[2] = {0, 0};
static bool g_periodic = false;
static uint32_t g_period_ms = 1000;
static uint32_t g_last_report_ms = 0;
static void print(const char* fmt, ...) {
char buf[240];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
SerialLock::lock();
Serial.println(buf);
SerialLock::unlock();
}
static uint64_t idleCounter(int core) {
#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS
// Run-time counter unit is microseconds (esp_timer) on ESP-IDF.
return (uint64_t)ulTaskGetIdleRunTimeCounterForCore(core);
#else
(void)core;
return 0;
#endif
}
static void resetWindow() {
portENTER_CRITICAL(&g_mux);
g_frames = g_frame_ms_sum = g_frame_ms_max = g_frame_px_sum = 0;
g_flushes = g_flush_us_sum = g_flush_px_sum = 0;
portEXIT_CRITICAL(&g_mux);
g_window_start_us = esp_timer_get_time();
g_idle_start[0] = idleCounter(0);
g_idle_start[1] = idleCounter(1);
}
void init() {
resetWindow();
}
void onFrame(uint32_t time_ms, uint32_t px) {
portENTER_CRITICAL(&g_mux);
g_frames++;
g_frame_ms_sum += time_ms;
if (time_ms > g_frame_ms_max) g_frame_ms_max = time_ms;
g_frame_px_sum += px;
portEXIT_CRITICAL(&g_mux);
}
void onFlush(uint32_t time_us, uint32_t px) {
portENTER_CRITICAL(&g_mux);
g_flushes++;
g_flush_us_sum += time_us;
g_flush_px_sum += px;
portEXIT_CRITICAL(&g_mux);
}
/** Emit one PERF line for the window since the last reset, then reset. */
static void report() {
int64_t now_us = esp_timer_get_time();
uint64_t win_us = (uint64_t)(now_us - g_window_start_us);
if (win_us < 1000) win_us = 1000;
uint32_t frames, ms_sum, ms_max, px_sum, flushes, fl_us, fl_px;
portENTER_CRITICAL(&g_mux);
frames = g_frames; ms_sum = g_frame_ms_sum; ms_max = g_frame_ms_max; px_sum = g_frame_px_sum;
flushes = g_flushes; fl_us = g_flush_us_sum; fl_px = g_flush_px_sum;
portEXIT_CRITICAL(&g_mux);
uint64_t idle0 = idleCounter(0) - g_idle_start[0];
uint64_t idle1 = idleCounter(1) - g_idle_start[1];
unsigned idle0_pct = (unsigned)((idle0 * 100) / win_us);
unsigned idle1_pct = (unsigned)((idle1 * 100) / win_us);
if (idle0_pct > 100) idle0_pct = 100;
if (idle1_pct > 100) idle1_pct = 100;
// fps x10 for one decimal without floats in the format
unsigned fps10 = (unsigned)((uint64_t)frames * 10000000ULL / win_us);
unsigned avg_ms10 = frames ? (ms_sum * 10) / frames : 0;
unsigned kpx = frames ? (px_sum / frames) / 1000 : 0;
unsigned flush_ms_per_s = (unsigned)(((uint64_t)fl_us * 1000ULL) / win_us); // ms of copying per second
unsigned lv_idle = 0;
if (lvgl_port_lock_available()) {
lv_idle = lv_timer_get_idle();
}
print("PERF,%lu,%u.%u,%u.%u,%lu,%u,%u,%u,%u,%u,%u,%u,%u",
(unsigned long)millis(),
fps10 / 10, fps10 % 10,
avg_ms10 / 10, avg_ms10 % 10,
(unsigned long)ms_max,
kpx,
flush_ms_per_s,
lv_idle,
idle0_pct, idle1_pct,
(unsigned)heap_caps_get_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT),
(unsigned)heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT),
(unsigned)heap_caps_get_free_size(MALLOC_CAP_SPIRAM));
(void)flushes; (void)fl_px;
resetWindow();
}
static void help() {
print("perf one PERF line for the window since the last report");
print("perf on [sec] PERF line every <sec> seconds (default 1) while you provoke symptoms");
print("perf off");
print("PERF,ts,fps,avg_ms,max_ms,kpx/frame,flush_ms/s,lvgl_idle%%,idle0%%,idle1%%,int_free,int_largest,psram_free");
}
bool handleCommand(const char* line) {
char buf[48];
strncpy(buf, line, sizeof(buf) - 1);
buf[sizeof(buf) - 1] = 0;
char* argv[3] = {0};
int argc = 0;
for (char* tok = strtok(buf, " \t"); tok && argc < 3; tok = strtok(NULL, " \t")) argv[argc++] = tok;
if (argc == 0 || strcmp(argv[0], "perf") != 0) return false;
if (argc == 1) { report(); return true; }
if (!strcmp(argv[1], "help")) { help(); return true; }
if (!strcmp(argv[1], "on")) {
long s = (argc == 3) ? strtol(argv[2], NULL, 10) : 1;
if (s < 1) s = 1;
if (s > 60) s = 60;
g_period_ms = (uint32_t)s * 1000;
g_periodic = true;
g_last_report_ms = millis();
resetWindow();
print("[perf] periodic every %ld s (columns: ts,fps,avg_ms,max_ms,kpx,flush_ms/s,lv_idle,idle0,idle1,int_free,int_largest,psram)", s);
return true;
}
if (!strcmp(argv[1], "off")) { g_periodic = false; print("[perf] periodic off"); return true; }
help();
return true;
}
void tick() {
if (!g_periodic) return;
uint32_t now = millis();
if (now - g_last_report_ms >= g_period_ms) {
g_last_report_ms = now;
report();
}
}
} // namespace PerfMon
+46
View File
@@ -0,0 +1,46 @@
/*
* Performance Monitor
*
* Measures what the display pipeline actually does, so display experiments
* (bounce buffers, draw-buffer size, XIP from PSRAM - wiki open question #13)
* can be compared with numbers instead of impressions:
*
* - render frames/s and ms per frame (LVGL's monitor_cb: render + flush)
* - time spent copying pixels into the PSRAM framebuffer (flush_cb)
* - LVGL task idle % (lv_timer_get_idle)
* - CPU idle % per core (FreeRTOS run-time stats)
* - internal heap, largest block, PSRAM free
*
* Console:
* perf one line for the last window
* perf on [sec] print a PERF, line every <sec> s (default 1) - use while
* provoking symptoms (Wi-Fi scan, tuning save, OTA)
* perf off
*
* PERF line (CSV, greppable like CANRAW):
* PERF,<ts_ms>,<fps>,<avg_ms>,<max_ms>,<kpx/frame>,<flush_ms/s>,
* <lvgl_idle%>,<idle0%>,<idle1%>,<int_free>,<int_largest>,<psram_free>
*/
#pragma once
#include <Arduino.h>
namespace PerfMon {
/** Call once in setup(), before the LVGL port starts. */
void init();
/** Hook for lv_disp_drv_t::monitor_cb - one rendered frame (ms, pixels). */
void onFrame(uint32_t time_ms, uint32_t px);
/** Hook for the flush callback - one pixel copy into the framebuffer. */
void onFlush(uint32_t time_us, uint32_t px);
/** Console line beginning with "perf". */
bool handleCommand(const char* line);
/** Periodic reporting. Call from loop(). */
void tick();
} // namespace PerfMon