/* * 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 #include #include #include #include #include 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 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