Skip to content

profiler #

CPU profiler

vtl.profiler is an opt-in timing and memory-accounting module for #741. It reports operation timing and peak/active CPU memory for bytes that callers explicitly attribute to labels. It does not intercept VTL allocations automatically, so overhead is paid only where instrumentation is inserted.

mut p := profiler.new(true)
p.allocate('weights', 4096)!
value := profiler.timed[f64](mut p, 'forward', fn () !f64 {
    return 1.0
})!
p.release('weights', 4096)!
println(p.report())
  • allocate(label, bytes) / release(label, bytes) track live buffers per label. Over-release and negative sizes are rejected.
  • peak_active_bytes() is the maximum total live bytes; active_bytes(label) and active_total_bytes() report current usage.
  • timed(label, op) records elapsed nanoseconds and still records the event when op fails, then propagates the error.
  • tensor_bytes[T](shape) computes dense byte size with zero-dimension and overflow handling.
  • new(false) keeps call sites in place while recording nothing.

Run the focused tests with:

v test tests/profiler/profiler_test.v

Reusable-buffer planning, accelerator memory attribution, operation-level instrumentation inside VTL kernels, and memory-budget examples remain tracked in #741.

fn new #

fn new(enabled bool) &Profiler

new returns a profiler. A disabled profiler still runs timed operations but records no events, so callers can leave instrumentation in place.

fn tensor_bytes #

fn tensor_bytes[T](shape []int) !i64

tensor_bytes computes the byte size of a dense tensor shape. Zero-sized dimensions are allowed; negative dimensions and overflow are rejected.

fn timed #

fn timed[T](mut p Profiler, label string, op fn () !T) !T

timed runs op, records its elapsed time under label, and returns its result. Errors from op are propagated and the timing is still recorded.

struct Profiler #

struct Profiler {
mut:
	enabled         bool
	active          map[string]i64
	total_active    i64
	peak_active     i64
	total_allocated i64
	timings         []TimingEvent
}

Profiler collects opt-in timing and CPU memory-accounting events. It does not intercept allocations automatically: callers report bytes when they allocate and release a reusable buffer, which keeps instrumentation costs explicit and attribution precise.

fn (Profiler) allocate #

fn (mut p Profiler) allocate(label string, bytes i64) !

allocate records bytes held under label. Negative sizes are rejected.

fn (Profiler) release #

fn (mut p Profiler) release(label string, bytes i64) !

release records bytes returned under label. Releasing more than the currently active bytes for a label is rejected so lifetimes stay balanced.

fn (Profiler) active_total_bytes #

fn (p &Profiler) active_total_bytes() i64

active_total_bytes reports bytes currently active across all labels.

fn (Profiler) peak_active_bytes #

fn (p &Profiler) peak_active_bytes() i64

peak_active_bytes reports the highest simultaneously active byte count.

fn (Profiler) total_allocated_bytes #

fn (p &Profiler) total_allocated_bytes() i64

total_allocated_bytes reports the sum of all reported allocations.

fn (Profiler) active_bytes #

fn (p &Profiler) active_bytes(label string) i64

active_bytes reports bytes currently active for one label.

fn (Profiler) timings #

fn (p &Profiler) timings() []TimingEvent

timings returns a copy of the recorded timing events in call order.

fn (Profiler) report #

fn (p &Profiler) report() string

report returns a short human-readable memory summary.

fn (Profiler) reset #

fn (mut p Profiler) reset()

reset clears all recorded events and active buffers.

struct TimingEvent #

struct TimingEvent {
pub:
	label string
	ns    i64
}

TimingEvent records one timed operation.