From d09393621158c59828728d63116339b1327050c2 Mon Sep 17 00:00:00 2001 From: silverweed Date: Thu, 11 Jul 2024 15:27:38 +0200 Subject: [PATCH] print mem used --- imgui.ini | 2 +- src/mem.cpp | 14 ++++++++++++-- src/render.cpp | 20 ++++++++++++++++---- src/rntviewer.cpp | 1 - src/types.h | 4 ++++ 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/imgui.ini b/imgui.ini index d39c017..2742fa6 100644 --- a/imgui.ini +++ b/imgui.ini @@ -8,7 +8,7 @@ Size=1152,1414 [Window][main] Pos=0,0 -Size=1777,680 +Size=1183,680 [Window][Hex View] Pos=91,62 diff --git a/src/mem.cpp b/src/mem.cpp index f4e0602..5b1ac5f 100644 --- a/src/mem.cpp +++ b/src/mem.cpp @@ -1,7 +1,6 @@ // Mostly taken from the raddebugger -- thanks, Ryan. #define align_pow2(x, b) (((x) + (b) - 1) & ( ~((b) - 1))) -#define SLLStackPush_N(f,n,next) ((n)->next=(f), (f)=(n)) #define SLLStackPop_N(f,next) ((f)=(f)->next) #ifdef ENABLE_ASAN @@ -60,6 +59,16 @@ void arena_release(Arena *arena) } } +internal u64 +arena_mem_used(Arena *arena) +{ + u64 tot = 0; + for (Arena *node = arena->cur, *prev = 0; node; node = prev) { + tot += node->pos - node->base_pos; + } + return tot; +} + internal u64 arena_huge_push_threshold() { @@ -86,7 +95,8 @@ void *arena_push_impl(Arena *arena, u64 size) if (new_block) { new_block->base_pos = cur->base_pos + cur->res; - SLLStackPush_N(arena->cur, new_block, prev); + new_block->prev = arena->cur; + arena->cur = new_block; cur = new_block; pos_mem = align_pow2(cur->pos, cur->align); diff --git a/src/render.cpp b/src/render.cpp index bc19d25..3af7bf1 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -21,6 +21,15 @@ f32 calc_avg_dt_ms(const Delta_Time_Accum &accum) return res; } +internal +String8 to_pretty_size(Arena *arena, u64 bytes) +{ + if (bytes >= GiB(1)) return push_str8f(arena, "%.1f GiB", (f32)bytes / GiB(1)); + if (bytes >= MiB(1)) return push_str8f(arena, "%.1f MiB", (f32)bytes / MiB(1)); + if (bytes >= KiB(1)) return push_str8f(arena, "%.1f KiB", (f32)bytes / KiB(1)); + return push_str8f(arena, "%zu B", bytes); +} + internal u32 mem_edit_bg_color_fn(const u8 *data, u64 off, const void *user_data) { @@ -84,19 +93,22 @@ void update_and_render(Arena *arena, App_State &app, f32 delta_time_ms) String8 ntpl_desc = rntuple_description(scratch.arena, app.rntinfo.anchor); ImGui::Text("Inspecting RNTuple '%s' (%s)", app.ntpl_name, ntpl_desc.c()); - // Draw avg delta time + // Draw stats { ImGui::SameLine(); + u64 mem_used = arena_mem_used(arena); f32 avg_dt = calc_avg_dt_ms(app.delta_time_accum); - String8 dt_txt = push_str8f(scratch.arena, "avg dt: %.1f", avg_dt); - f32 pos_x = (ImGui::GetCursorPosX() + ImGui::GetColumnWidth() - ImGui::CalcTextSize(dt_txt.c()).x + String8 stat_txt = push_str8f(scratch.arena, "mem used: %s | avg dt: %.1f", + to_pretty_size(scratch.arena, mem_used), avg_dt); + + f32 pos_x = (ImGui::GetCursorPosX() + ImGui::GetColumnWidth() - ImGui::CalcTextSize(stat_txt.c()).x - ImGui::GetScrollX() - 2 * ImGui::GetStyle().ItemSpacing.x); if (pos_x > ImGui::GetCursorPosX()) ImGui::SetCursorPosX(pos_x); - ImGui::Text("%s", dt_txt.c()); + ImGui::Text("%s", stat_txt.c()); } ImGui::Separator(); diff --git a/src/rntviewer.cpp b/src/rntviewer.cpp index 0759ce4..c2f29e7 100644 --- a/src/rntviewer.cpp +++ b/src/rntviewer.cpp @@ -61,7 +61,6 @@ void app_cleanup(App_State &app) os_stop_file_watch(app); os_unmap_file(app.inspected_fmem, app.inspected_file_size); if (app.inspected_file) fclose(app.inspected_file); - app.inspected_file = nullptr; } int main(int argc, char **argv) diff --git a/src/types.h b/src/types.h index 130b0fb..21fccdc 100644 --- a/src/types.h +++ b/src/types.h @@ -18,3 +18,7 @@ using i64 = int64_t; #define max(a, b) ((a) > (b)) ? (a) : (b) #define min(a, b) ((a) < (b)) ? (a) : (b) + +#define KiB(b) (b * 1024) +#define MiB(b) (KiB(b) * 1024) +#define GiB(b) (MiB(b) * 1024)