#module_parameters(ENABLE_TRACING := true); Qtrace_Features :: enum_flags { TIME; CPU_COUNTER; } qtrace :: ( $label: string = "", color: Console_Color = COLOR_DEFAULT, $features: Qtrace_Features = .TIME, loc := #caller_location ) #expand { #if ENABLE_TRACING { 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); } } /** TODO: Include CPU - Cache misses (L1/L2/L3) - IPC (instructions per cycle) - Branch prediction hits/misses - Stall cycles - SIMD utilization Memory - TLB misses - Memory bandwidth (bytes read/written) Threading - Lock contention / wait time - Context switches - Core migration frequency Timing - Frame time per system - Hitches / frame time variance - Function call frequency vs cost (hot paths) Other Things: Regarding hitches/spikes, instead for going towards p95/p99, catch outliers over a certain time frame. Maybe logging/printing in a separate thread? */ #scope_file 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, state: Features_State, $features: Qtrace_Features, proc_name: string, color: Console_Color, loc: Source_Code_Location ) { using loc; print_color("PROCEDURE: %", proc_name, color=color, style=.BOLD); print("\n"); #if label { print_color("Label: %", label, color=color); print("\n"); } #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: %: %:%", fully_pathed_filename, line_number, character_number, color=color ); 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-, formatInt, ) Basic :: #import "Basic"; using,only(print_color, Console_Color) PC :: #import "Print_Color"; using,only(read_cpu_counter) System :: #import "System"; /* ------------------------------------------------------------------------------ This software is available under 2 licenses -- choose whichever you prefer. ------------------------------------------------------------------------------ ALTERNATIVE A - MIT License Copyright (c) 2026 Adam Blazeowsky Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ ALTERNATIVE B - Public Domain (www.unlicense.org) This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ */