Skip to content

v2.profiler #

Constants #

const default_allocator = Allocator{
	alloc_fn:   default_alloc
	free_fn:    default_free
	realloc_fn: default_realloc
	ctx:        unsafe { nil }
}

Default allocator - just wraps malloc/free/realloc

fn alloc #

fn alloc(size int) voidptr

User code calls these - they use context.allocator implicitly This provides the Jai-like implicit allocation tracking

fn format_bytes #

fn format_bytes(bytes u64) string

format_bytes converts bytes to a human-readable string

fn frame_end #

fn frame_end()

frame_end signals the end of a frame and collects frame data Call this at the end of each game/application frame

fn free #

fn free(ptr voidptr)

fn get_allocs #

fn get_allocs() []AllocRecord

get_allocs returns a copy of all allocation records

fn get_allocs_for_frame #

fn get_allocs_for_frame(frame_num u64) []AllocRecord

get_allocs_for_frame returns allocations made in a specific frame

fn get_frame_count #

fn get_frame_count() u64

get_frame_count returns the current frame number

fn get_frames #

fn get_frames() []FrameData

get_frames returns a copy of all frame data

fn get_leaks #

fn get_leaks() []AllocRecord

get_leaks returns allocations that were never freed

fn get_live_allocs #

fn get_live_allocs() []AllocRecord

get_live_allocs returns currently live (not freed) allocations

fn get_state #

fn get_state() &ProfilerState

get_state returns the global profiler state

fn get_statistics #

fn get_statistics() Statistics

get_statistics returns current profiler statistics

fn init_profiler_state #

fn init_profiler_state()

init_profiler_state initializes the global profiler state Must be called before using the profiler

fn pop_allocator #

fn pop_allocator(old Allocator)

fn profiler_alloc_with_location #

fn profiler_alloc_with_location(size int, file string, line int) voidptr

profiler_alloc_with_location records allocation with source location

fn profiler_allocator #

fn profiler_allocator() Allocator

profiler_allocator returns an Allocator that tracks all allocations

fn profiler_disable #

fn profiler_disable()

profiler_disable disables allocation tracking

fn profiler_enable #

fn profiler_enable()

profiler_enable enables allocation tracking

fn profiler_init #

fn profiler_init()

profiler_init initializes the profiler system Must be called before enabling profiling

fn profiler_is_enabled #

fn profiler_is_enabled() bool

profiler_is_enabled returns whether profiling is active

fn push_allocator #

fn push_allocator(a Allocator) Allocator

Scoped allocator helper - saves and restores the allocator Usage: old := profiler.push_allocator(my_allocator) defer { profiler.pop_allocator(old) } // ... all allocations here use my_allocator

fn realloc #

fn realloc(ptr voidptr, new_size int) voidptr

fn reset #

fn reset()

reset clears all profiler data

fn restore_allocator #

fn restore_allocator(old Allocator)

restore_allocator restores the previous allocator

fn use_profiler_allocator #

fn use_profiler_allocator() Allocator

use_profiler_allocator enables the profiler allocator in the current context Returns the old allocator for restoration

struct AllocRecord #

struct AllocRecord {
pub:
	ptr       voidptr
	size      int
	frame     u64
	file      string // Source file
	line      int    // Source line
	timestamp i64    // Monotonic time in nanoseconds
pub mut:
	freed      bool
	free_frame u64
}

AllocRecord tracks a single allocation

struct Allocator #

struct Allocator {
pub:
	alloc_fn   fn (size int, ctx voidptr) voidptr                  = default_alloc
	free_fn    fn (ptr voidptr, ctx voidptr)                       = default_free
	realloc_fn fn (ptr voidptr, new_size int, ctx voidptr) voidptr = default_realloc
	ctx        voidptr // User data for allocator implementation
}

Allocator interface - can be swapped at runtime Similar to Jai's context.allocator design

struct Context #

struct Context {
pub mut:
	allocator Allocator = default_allocator
}

struct FrameData #

struct FrameData {
pub:
	frame_num u64
pub mut:
	new_bytes   u64
	freed_bytes u64
	live_bytes  u64
	new_allocs  []int // Indices into allocs array
	freed_idxs  []int // Indices of allocations freed this frame
}

FrameData aggregates allocation info for a single frame

struct ProfilerState #

@[heap]
struct ProfilerState {
pub mut:
	enabled       bool
	current_frame u64
	allocs        []AllocRecord
	alloc_map     map[voidptr]int // ptr -> index in allocs
	frames        []FrameData
	peak_bytes    u64
	live_bytes    u64
	total_allocs  u64
	total_frees   u64
	mu            &sync.Mutex = unsafe { nil }
}

ProfilerState holds all profiling data

struct Statistics #

struct Statistics {
pub:
	live_bytes   u64
	peak_bytes   u64
	total_allocs u64
	total_frees  u64
	leak_count   int
	frame_count  u64
}

Statistics returns summary stats