From ca94dbec3e8b409eed38b3f60126c556206a23e7 Mon Sep 17 00:00:00 2001 From: jacob Date: Thu, 3 Jul 2025 14:32:21 -0500 Subject: [PATCH] launch profiler capture program at start --- src/draw.c | 1 - src/gp_dx12.c | 1 - src/job.c | 2 +- src/prof_tracy.h | 3 ++- src/sys.h | 2 +- src/sys_win32.c | 51 ++++++++++++++++++++---------------------------- src/user.c | 25 ++++-------------------- src/user.h | 1 - 8 files changed, 29 insertions(+), 57 deletions(-) diff --git a/src/draw.c b/src/draw.c index 5672e3a8..a16cd6fa 100644 --- a/src/draw.c +++ b/src/draw.c @@ -290,7 +290,6 @@ void draw_grid(struct gp_flow *flow, struct xform xf, u32 bg0_color, u32 bg1_col /* Returns the rect of the text area */ struct rect draw_text(struct gp_flow *flow, struct draw_text_params params) { - __prof; struct arena_temp scratch = scratch_begin_no_conflict(); f32 inv_font_image_width = 1.0 / (f32)params.font->image_width; diff --git a/src/gp_dx12.c b/src/gp_dx12.c index 0241068c..2a5674dd 100644 --- a/src/gp_dx12.c +++ b/src/gp_dx12.c @@ -1475,7 +1475,6 @@ struct gp_flow *gp_flow_alloc(void) i32 gp_push_cmd(struct gp_flow *gp_flow, struct gp_cmd_desc *cmd_desc) { - __prof; i32 ret = 0; struct flow *flow = (struct flow *)gp_flow; if (flow) { diff --git a/src/job.c b/src/job.c index 7c01ea7e..ae87e7ed 100644 --- a/src/job.c +++ b/src/job.c @@ -42,7 +42,7 @@ struct alignas(64) worker_ctx { i32 pin_depth; b32 initialized; }; -STATIC_ASSERT(alignof(struct worker_ctx) == 64); /* To avoid false sharing */ +STATIC_ASSERT(alignof(struct worker_ctx) == 64); /* Avoid false sharing */ /* ========================== * * Global state diff --git a/src/prof_tracy.h b/src/prof_tracy.h index 1abb7c9f..c48a8fc4 100644 --- a/src/prof_tracy.h +++ b/src/prof_tracy.h @@ -11,7 +11,8 @@ #define PROFILING_CAPTURE_FRAME_IMAGE 0 #define PROFILING_LOCKS 0 #define PROFILING_D3D 0 -#define PROFILING_CMD_WSTR L"tracy-profiler.exe -a 127.0.0.1" +#define PROFILING_FILE_WSTR L".tracy" +#define PROFILING_CMD_WSTR L"cmd /C start \"\" /wait tracy-capture.exe -o .tracy -a 127.0.0.1 && start \"\" tracy-profiler.exe .tracy" /* Tracy defines */ #define TRACY_ENABLE diff --git a/src/sys.h b/src/sys.h index f11698a9..7dd4f3c5 100644 --- a/src/sys.h +++ b/src/sys.h @@ -490,7 +490,7 @@ b32 sys_run_command(struct string cmd); i32 sys_current_fiber_id(void); /* ========================== * - * Scratch + * Scratch context * ========================== */ #define SYS_SCRATCH_ARENAS_PER_CTX 2 diff --git a/src/sys_win32.c b/src/sys_win32.c index 92de36ea..0057d4d8 100644 --- a/src/sys_win32.c +++ b/src/sys_win32.c @@ -121,7 +121,7 @@ struct alignas(64) fiber_ctx { u8 pad[40]; /* 40 bytes */ }; STATIC_ASSERT(sizeof(struct fiber_ctx) == 64); /* Assume ctx fits in one cache line (increase if necessary) */ -STATIC_ASSERT(alignof(struct fiber_ctx) == 64); /* To avoid false sharing */ +STATIC_ASSERT(alignof(struct fiber_ctx) == 64); /* Avoid false sharing */ @@ -208,6 +208,7 @@ INTERNAL i32 fiber_ctx_init(void) INTERNAL struct fiber_ctx *fiber_ctx_from_id(i32 id) { + ASSERT(id >= 0 && id < SYS_MAX_FIBERS); return &G.fiber_contexts[id]; } @@ -217,7 +218,7 @@ i32 sys_current_fiber_id(void) } /* ========================== * - * Scratch ctx + * Scratch context * ========================== */ struct sys_scratch_ctx *sys_scratch_ctx_from_fiber_id(i32 id) @@ -1985,14 +1986,7 @@ INTERNAL DWORD WINAPI win32_thread_proc(LPVOID vt) __profthread(t->thread_name_cstr); i32 fiber_id = fiber_ctx_init(); - void *fiber_addr = ConvertThreadToFiber((void *)(i64)fiber_id); - (UNUSED)fiber_addr; - { - i32 test = sys_current_fiber_id(); - (UNUSED)test; - (UNUSED)fiber_ctx_from_id; - DEBUGBREAKABLE; - } + ConvertThreadToFiber((void *)(i64)fiber_id); /* Initialize COM */ CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); @@ -2414,6 +2408,23 @@ int CALLBACK wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev_instance, (UNUSED)cmdline_wstr; (UNUSED)show_code; +#if PROFILING + /* Launch profiler */ + { + __profscope(Launch profiler); + STARTUPINFO si = ZI; + si.cb = sizeof(si); + PROCESS_INFORMATION pi = ZI; + wchar_t cmd[sizeof(PROFILING_CMD_WSTR)] = ZI; + MEMCPY(cmd, PROFILING_CMD_WSTR, sizeof(PROFILING_CMD_WSTR)); + DeleteFileW(PROFILING_FILE_WSTR); + b32 success = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi); + if (!success) { + MessageBoxExW(NULL, L"Failed to launch capture using command '" PROFILING_CMD_WSTR L"'. Is the app in your path?", L"Error", MB_ICONSTOP | MB_SETFOREGROUND | MB_TOPMOST, 0); + } + } +#endif + /* Convert main thread to fiber */ i32 fiber_id = fiber_ctx_init(); ConvertThreadToFiber((void *)(i64)fiber_id); @@ -2591,26 +2602,6 @@ int CALLBACK wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev_instance, return 1; } -#if PROFILING - /* Launch profiler */ - if (!__prof_is_connected()) { - __profscope(Launch profiler); - STARTUPINFO si = ZI; - si.cb = sizeof(si); - PROCESS_INFORMATION pi = ZI; - wchar_t cmd[sizeof(PROFILING_CMD_WSTR)] = ZI; - MEMCPY(cmd, PROFILING_CMD_WSTR, sizeof(PROFILING_CMD_WSTR)); - b32 success = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi); - if (success) { - while (!__prof_is_connected()) { - ix_pause(); - } - } else { - MessageBoxExW(NULL, L"Failed to launch tracy profiler using command " PROFILING_CMD_WSTR, L"Error", MB_ICONSTOP | MB_SETFOREGROUND | MB_TOPMOST, 0); - } - } -#endif - return 0; } diff --git a/src/user.c b/src/user.c index f494eada..90e438a2 100644 --- a/src/user.c +++ b/src/user.c @@ -172,7 +172,6 @@ GLOBAL READONLY enum user_bind_kind g_binds[SYS_BTN_COUNT] = { [SYS_BTN_F1] = USER_BIND_KIND_DEBUG_PAUSE, [SYS_BTN_F2] = USER_BIND_KIND_DEBUG_CAMERA, [SYS_BTN_F3] = USER_BIND_KIND_DEBUG_DRAW, - [SYS_BTN_F4] = USER_BIND_KIND_PROFILER, [SYS_BTN_GRAVE_ACCENT] = USER_BIND_KIND_DEBUG_CONSOLE, [SYS_BTN_F11] = USER_BIND_KIND_FULLSCREEN, [SYS_BTN_MWHEELUP] = USER_BIND_KIND_ZOOM_IN, @@ -850,26 +849,6 @@ INTERNAL void user_update(void) if (G.bind_states[USER_BIND_KIND_DEBUG_CAMERA].num_presses > 0) { G.debug_camera = !G.debug_camera; } - if (G.bind_states[USER_BIND_KIND_PROFILER].num_presses > 0) { - if (G.profiler_launched) { - logf_warning("Profiler already launched"); - } else { -#if PROFILING - __profscope(Launch profiler); - struct string cmd = string_from_wstr_no_limit(scratch.arena, PROFILING_CMD_WSTR); - logf_info("Launching profiler with command \"%F\"", FMT_STR(cmd)); - b32 success = sys_run_command(cmd); - if (success) { - G.profiler_launched = true; - logf_success("Launched profiler successfully"); - } else { - logf_error("Failed to launch profiler using command \"%F\" (is the executable in your PATH?)", FMT_STR(cmd)); - } -#else - logf_warning("Cannot launch profiler: Program is not in profiling mode"); -#endif - } - } { if (G.bind_states[USER_BIND_KIND_DEBUG_FOLLOW].num_presses > 0) { @@ -1718,16 +1697,20 @@ INTERNAL void user_update(void) struct xform xf = XFORM_TRS(.t = crosshair_pos, .s = size); draw_texture(G.ui_gp_flow, DRAW_TEXTURE_PARAMS(.xf = xf, .sprite = crosshair_tag)); +#if 0 struct rect cursor_clip = RECT_FROM_V2(G.user_screen_offset, G.user_size); cursor_clip.pos = v2_add(cursor_clip.pos, v2_mul(size, 0.5f)); cursor_clip.pos = v2_add(cursor_clip.pos, V2(1, 1)); cursor_clip.size = v2_sub(cursor_clip.size, size); sys_window_cursor_hide(G.window); sys_window_cursor_enable_clip(G.window, cursor_clip); +#endif } else { __profscope(Update windows cursor); +#if 0 sys_window_cursor_disable_clip(G.window); sys_window_cursor_show(G.window); +#endif } /* ========================== * diff --git a/src/user.h b/src/user.h index 42ce727e..ad5323b3 100644 --- a/src/user.h +++ b/src/user.h @@ -35,7 +35,6 @@ enum user_bind_kind { USER_BIND_KIND_DEBUG_WALLS, USER_BIND_KIND_DEBUG_FOLLOW, USER_BIND_KIND_DEBUG_DRAW, - USER_BIND_KIND_PROFILER, USER_BIND_KIND_DEBUG_CONSOLE, USER_BIND_KIND_DEBUG_CAMERA, USER_BIND_KIND_DEBUG_PAUSE,