<<
path:
root/public/0x2lib.git/html/0x2_Qtrace.jai
blob: 63a25a930523fa90e6496febcbc3aeef4bbd64a0
[raw]
[clear marker]
2#module_parameters(ENABLE_TRACING := true);
5Qtrace_Features :: enum_flags {
13 color: Console_Color = COLOR_DEFAULT,
14 $features: Qtrace_Features = .TIME,
15 loc := #caller_location
18 state: Features_State;
20 #if features & .TIME then state.time.ts_start = current_time_monotonic();
21 #if features & .CPU_COUNTER then state.cpu_counter.count = read_cpu_counter();
23 `defer delta_report(label, state, features, #procedure_name(), color, loc);
31 - Cache misses (L1/L2/L3)
32 - IPC (instructions per cycle)
33 - Branch prediction hits/misses
39 - Memory bandwidth (bytes read/written)
42 - Lock contention / wait time
44 - Core migration frequency
47 - Frame time per system
48 - Hitches / frame time variance
49 - Function call frequency vs cost (hot paths)
54 Regarding hitches/spikes, instead for going towards p95/p99, catch outliers over a certain
57 Maybe logging/printing in a separate thread?
65COLOR_DEFAULT :: Console_Color.YELLOW;
68Features_State :: struct {
70 cpu_counter: Feature_CPU_Counter;
73Feature_Time :: struct {
74 ts_start: Apollo_Time;
77Feature_CPU_Counter :: struct {
84 state: Features_State,
85 $features: Qtrace_Features,
88 loc: Source_Code_Location
92 print_color("PROCEDURE: %", proc_name, color=color, style=.BOLD);
95 print_color("Label: %", label, color=color);
99 #if features & .TIME then feature_time(state.time, color);
100 #if features & .CPU_COUNTER then feature_cpu_counter(state.cpu_counter, color);
105 fully_pathed_filename,
113feature_time :: (using ft: Feature_Time, color: Console_Color) {
114 ts_now := current_time_monotonic();
115 delta := ts_now - ts_start;
117 us := to_microseconds(delta);
118 ms := to_milliseconds(delta);
119 s := to_seconds(delta);
121 print_color("Time Delta: % us % ms % s", us, ms, s, color=color);
125feature_cpu_counter :: (using fc: Feature_CPU_Counter, color: Console_Color) {
126 count_now := read_cpu_counter();
127 delta := count_now - count;
129 delta_fmt := formatInt(delta, digits_per_comma=3, comma_string="_");
131 print_color("CPU Delta: %", delta_fmt, color=color);
135newline :: () #expand {
141 print, Source_Code_Location,
142 Apollo_Time, current_time_monotonic, to_seconds, to_microseconds, to_milliseconds,
145) Basic :: #import "Basic";
147using,only(print_color, Console_Color) PC :: #import "Print_Color";
148using,only(read_cpu_counter) System :: #import "System";
152------------------------------------------------------------------------------
153This software is available under 2 licenses -- choose whichever you prefer.
154------------------------------------------------------------------------------
155ALTERNATIVE A - MIT License
156Copyright (c) 2026 Adam Blazeowsky
157Permission is hereby granted, free of charge, to any person obtaining a copy of
158this software and associated documentation files (the "Software"), to deal in
159the Software without restriction, including without limitation the rights to
160use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
161of the Software, and to permit persons to whom the Software is furnished to do
162so, subject to the following conditions:
163The above copyright notice and this permission notice shall be included in all
164copies or substantial portions of the Software.
165THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
166IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
167FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
168AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
169LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
170OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
172------------------------------------------------------------------------------
173ALTERNATIVE B - Public Domain (www.unlicense.org)
174This is free and unencumbered software released into the public domain.
175Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
176software, either in source code form or as a compiled binary, for any purpose,
177commercial or non-commercial, and by any means.
178In jurisdictions that recognize copyright laws, the author or authors of this
179software dedicate any and all copyright interest in the software to the public
180domain. We make this dedication for the benefit of the public at large and to
181the detriment of our heirs and successors. We intend this dedication to be an
182overt act of relinquishment in perpetuity of all present and future rights to
183this software under copyright law.
184THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
185IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
186FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
187AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
188ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
189WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
190------------------------------------------------------------------------------