From e181b0e2209fd7a2e843bbb7cfe2151497450325 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Thu, 25 Jan 2024 13:39:32 +0100 Subject: Update profiling macros --- src/main.c | 54 ++++++++++++++++++++++++++++---- src/profiling.c | 95 ++++++++++++++++++++++++++++++++++++++------------------- src/sequencer.c | 12 -------- 3 files changed, 112 insertions(+), 49 deletions(-) diff --git a/src/main.c b/src/main.c index 6f1d0e6..cec540e 100644 --- a/src/main.c +++ b/src/main.c @@ -48,6 +48,9 @@ WITH REGARD TO THIS SOFTWARE. #include "gba/gba.h" +#define PROF_ENABLE 0 +#include "profiling.c" + #include "renderer_m0.c" #include "globals.c" #include "assets.c" @@ -57,9 +60,6 @@ WITH REGARD TO THIS SOFTWARE. #include "scale.c" #include "sequencer.c" -#define PROF_ENABLE 0 -#include "profiling.c" - static int frames = 0; void @@ -191,10 +191,52 @@ render(void) { void handle_input(void) { - if (key_tap(KEY_SELECT) && key_hold(KEY_START)) { - PROF_SHOW(); - clear_screen = true; +#if PROF_ENABLE > 0 + if (key_hold(KEY_SELECT)) { + if (key_tap(KEY_DOWN)) { + PROF_SHOW(); + clear_screen = true; + redraw_trigs = true; + redraw_channels = true; + redraw_pattern_buttons = true; + redraw_bank_buttons = true; + redraw_bpm = true; + redraw_play_pause = true; + redraw_params = true; + redraw_scale = true; + return; + } + static int prof_detail = 0; + if (key_tap(KEY_RIGHT)) { + prof_detail = CLAMP(prof_detail + 1, 0, 3); + PROF_DETAIL(prof_detail); + clear_screen = true; + redraw_trigs = true; + redraw_channels = true; + redraw_pattern_buttons = true; + redraw_bank_buttons = true; + redraw_bpm = true; + redraw_play_pause = true; + redraw_params = true; + redraw_scale = true; + return; + } + if (key_tap(KEY_LEFT)) { + prof_detail = CLAMP(prof_detail - 1, 0, 3); + PROF_DETAIL(prof_detail); + clear_screen = true; + redraw_trigs = true; + redraw_channels = true; + redraw_pattern_buttons = true; + redraw_bank_buttons = true; + redraw_bpm = true; + redraw_play_pause = true; + redraw_params = true; + redraw_scale = true; + return; + } } +#endif switch (scene) { case SCENE_SETTINGS: { handle_settings_input(); diff --git a/src/profiling.c b/src/profiling.c index 07f4bbf..44e12fd 100644 --- a/src/profiling.c +++ b/src/profiling.c @@ -2,6 +2,9 @@ // Profiling macros. // +// NOTE: Profiling uses the last two timers to count cycles, and thus can't be +// used for measuring applications that use them. + #ifndef PROF_ENABLE #define PROF_ENABLE 0 #endif @@ -9,7 +12,7 @@ #if PROF_ENABLE > 0 #ifndef PROF_RESET_MINMAX -#define PROF_RESET_MINMAX true +#define PROF_RESET_MINMAX false #endif // Maximum number of profiling to monitor. @@ -20,31 +23,33 @@ typedef enum ProfType { PROF_FLIP, PROF_FILL, PROF_NUM, + PROF_END, } ProfType; -char *prof_type_str[PROF_NUM] = { - "INPUT ", - "UPDATE ", - "RENDER ", - "FLIPBUF", - "SCRFILL", +char *prof_type_str[PROF_END] = { + "INPUT ", + "UPDATE ", + "RENDER ", + "FLIPBUF ", + "SCRFILL ", }; u32 prof_frame_time = 0; u32 prof_frame_count = 0; -u32 prof_frame_avg = -1; +u32 prof_frame_avg = 0; u32 prof_frame_time_max = 0; -u32 prof_times[PROF_NUM] = {0}; -u32 prof_count[PROF_NUM] = {0}; -u32 prof_avg[PROF_NUM] = {0}; -u32 prof_max[PROF_NUM] = {0}; -u32 prof_min[PROF_NUM] = {0}; +u32 prof_times[PROF_END] = {0}; +u32 prof_count[PROF_END] = {0}; +u32 prof_avg[PROF_END] = {0}; +u32 prof_max[PROF_END] = {0}; +u32 prof_min[PROF_END] = {0}; bool prof_reset_minmax = PROF_RESET_MINMAX; bool prof_show = true; +u8 prof_detail_level = 2; #define PROF_INIT() do { \ - for (size_t i = 0; i < PROF_NUM; i++) { \ + for (size_t i = 0; i < PROF_END; i++) { \ prof_min[i] = -1; \ } \ } while(0); @@ -66,23 +71,43 @@ bool prof_show = true; #define FRAME_END() do { \ prof_frame_count++;\ - prof_frame_time_max = MAX(prof_frame_time_max, profile_measure());\ + u32 frame_time = profile_measure();\ + prof_frame_time_max = MAX(prof_frame_time_max, frame_time);\ prof_frame_time += profile_stop();\ if (prof_show) { \ - draw_filled_rect(0, 0, SCREEN_WIDTH - 1, 8 * (PROF_NUM + 1), 0); \ - txt_drawf_small("FRAME TIME/FPS: %.9l/%.2l", 0, 0, COL_FG, \ - prof_frame_avg, \ - (u32)((u64)280896 * 60 / (prof_frame_avg + 1)));\ - txt_drawf_small("MAX: %.9l/%l", 8 * 19, 0, COL_FG, \ - prof_frame_time_max, 280896);\ + u32 fps = (u64)280896 * 60 / (prof_frame_avg + 1); \ + if (prof_frame_avg == 0) { \ + fps = 0; \ + } \ + if (prof_detail_level > 0) {\ + draw_filled_rect(0, 0, SCREEN_WIDTH - 1, 8, 2); \ + txt_drawf_small("AVG/MAX/VDRAW: %.9l/%.9l/%l FPS: %.3l CPU: %.3l", 0, 0, COL_FG, \ + prof_frame_avg, prof_frame_time_max, 280896, fps, (u64)prof_frame_avg * 100 / 280896);\ + } else { \ + draw_filled_rect(8 * 25 + 4, 0, SCREEN_WIDTH - 1, 8, 2); \ + txt_drawf_small("CPU: %.3l", 8 * 25 + 4, 0, COL_FG, \ + (u64)prof_frame_avg * 100 / 280896);\ + } \ + if (prof_detail_level >= 3) {\ + draw_filled_rect(0, 8, SCREEN_WIDTH - 1, 8 * (PROF_NUM + 1), 2); \ + } else if (prof_detail_level >= 2) {\ + draw_filled_rect(0, 8, 8 * 15, 8 * (PROF_NUM + 1), 2); \ + } \ for (size_t idx = 0; idx < PROF_NUM; idx++) { \ - txt_drawf_small("%s %.9l (%.9l %.9l) %08x:%08x", 0, 8 * (idx + 1), COL_FG, \ - prof_type_str[idx], \ - prof_avg[idx], \ - prof_min[idx], \ - prof_max[idx], \ - prof_avg[idx], \ - prof_max[idx]);\ + if (prof_detail_level >= 3) {\ + txt_drawf_small("%s %.9l (%.9l %.9l) %08x:%08x", 0, 8 * (idx + 1), COL_FG, \ + prof_type_str[idx], \ + prof_avg[idx], \ + prof_min[idx], \ + prof_max[idx], \ + prof_avg[idx], \ + prof_max[idx]);\ + } else if (prof_detail_level >= 2) {\ + txt_drawf_small("%s %.9l/%.9l", 0, 8 * (idx + 1), COL_FG, \ + prof_type_str[idx], \ + prof_avg[idx], \ + prof_max[idx]);\ + }\ }; \ } \ if (prof_frame_count >= PROF_ENABLE) { \ @@ -91,7 +116,6 @@ bool prof_show = true; if (prof_reset_minmax) { \ prof_min[idx] = -1; \ prof_max[idx] = 0; \ - prof_frame_time_max = 0; \ } \ prof_times[idx] = 0; \ prof_count[idx] = 0; \ @@ -106,12 +130,21 @@ bool prof_show = true; prof_show ^= 1; \ } while(0) +#define PROF_DETAIL(N) do { \ + prof_detail_level = (N); \ +} while(0) + #else // No profiling. #define PROF_INIT() #define PROF(F,VAR) do {F;} while(0) -#define FRAME_START() -#define FRAME_END() +#define FRAME_START() do { \ + profile_start();\ +} while(0) +#define FRAME_END() do { \ + frame_time = profile_stop();\ +} while(0) #define PROF_SHOW() +#define PROF_DETAIL(N) #endif diff --git a/src/sequencer.c b/src/sequencer.c index 4f46e1f..7570a7a 100644 --- a/src/sequencer.c +++ b/src/sequencer.c @@ -387,18 +387,6 @@ env_start: } current_wave[k][j] = next; } - // DEBUG: - // if (k < 4) { - // int x = 0; - // int y = 18 * k; - // draw_filled_rect(x, y, 100 + x, 16 + y, COL_BG); - // draw_wave_pattern(¤t_wave[k], 0 + x, 0 + y, 1); - // } else { - // int x = 100; - // int y = 18 * (k - 4); - // draw_filled_rect(x, y, 100 + x, 16 + y, COL_BG); - // draw_wave_pattern(¤t_wave[k], 0 + x, 0 + y, 1); - // } } wave_env_ticks = 0; wave_env_prog = 0; -- cgit v1.2.1