Author:ptrace
Comitter:ptrace
Date:2026-08-17 19:15:53 UTC
diff --git a/0x2_Qtrace.jai b/0x2_Qtrace.jai
index b8b7e41..63a25a9 100644
--- a/0x2_Qtrace.jai
+++ b/0x2_Qtrace.jai
@@ -3,12 +3,25 @@
#module_parameters(ENABLE_TRACING := true);
Qtrace_Features :: enum_flags {
TIME;
CPU_COUNTER;
}
qtrace :: (
$label: string = "", color: Console_Color = COLOR_DEFAULT, loc := #caller_location
$label: string = "",
color: Console_Color = COLOR_DEFAULT,
$features: Qtrace_Features = .TIME,
loc := #caller_location
) #expand {
#if ENABLE_TRACING {
ts_start := current_time_monotonic();
`defer delta_report(label, ts_start, #procedure_name(), color, loc);
state: Features_State;
#if features & .TIME then state.time.ts_start = current_time_monotonic();
#if features & .CPU_COUNTER then state.cpu_counter.count = read_cpu_counter();
`defer delta_report(label, state, features, #procedure_name(), color, loc);
}
}
@@ -53,20 +66,28 @@ qtrace :: (
COLOR_DEFAULT :: Console_Color.YELLOW;
Features_State :: struct {
time: Feature_Time;
cpu_counter: Feature_CPU_Counter;
}
Feature_Time :: struct {
ts_start: Apollo_Time;
}
Feature_CPU_Counter :: struct {
count: u64;
}
delta_report :: (
$label: string,
ts_start: Apollo_Time,
state: Features_State,
$features: Qtrace_Features,
proc_name: string,
color: Console_Color,
loc: Source_Code_Location
) {
ts_now := current_time_monotonic();
delta := ts_now - ts_start;
us := to_microseconds(delta);
ms := to_milliseconds(delta);
s := to_seconds(delta);
using loc;
print_color("PROCEDURE: %", proc_name, color=color, style=.BOLD);
@@ -75,7 +96,10 @@ delta_report :: (
print_color("Label: %", label, color=color);
print("\n");
}
print_color("Delta: % us % ms % s", us, ms, s, color=color);
#if features & .TIME then feature_time(state.time, color);
#if features & .CPU_COUNTER then feature_cpu_counter(state.cpu_counter, color);
print("\n");
print_color(
"Location: %: %:%",
@@ -87,14 +111,42 @@ delta_report :: (
print("\n\n");
}
feature_time :: (using ft: Feature_Time, color: Console_Color) {
ts_now := current_time_monotonic();
delta := ts_now - ts_start;
us := to_microseconds(delta);
ms := to_milliseconds(delta);
s := to_seconds(delta);
print_color("Time Delta: % us % ms % s", us, ms, s, color=color);
newline();
}
feature_cpu_counter :: (using fc: Feature_CPU_Counter, color: Console_Color) {
count_now := read_cpu_counter();
delta := count_now - count;
delta_fmt := formatInt(delta, digits_per_comma=3, comma_string="_");
print_color("CPU Delta: %", delta_fmt, color=color);
newline();
}
newline :: () #expand {
print("\n");
}
using,only(
print, Source_Code_Location,
Apollo_Time, current_time_monotonic, to_seconds, to_microseconds, to_milliseconds,
operator -,
operator-,
formatInt,
) Basic :: #import "Basic";
using,only(print_color, Console_Color) PC :: #import "Print_Color";
using,only(read_cpu_counter) System :: #import "System";
/*
diff --git a/tests/run_tests.jai b/tests/run_tests.jai
index 169e22e..d59fedc 100644
--- a/tests/run_tests.jai
+++ b/tests/run_tests.jai
@@ -38,7 +38,7 @@ main :: () {
ok := true;
{
qtrace("Test Suite", color=.BLUE);
qtrace("Test Suite", color=.BLUE, features=.TIME | .CPU_COUNTER);
ok &= bp.run();
ok &= rc.run();