Logo

index : 0x2lib

Library extension for Jai

  • summary
  • about
  • tree
  • log
  • branches
<< path: root/public/0x2lib.git/html/0x2_Qtrace.jai blob: 63a25a930523fa90e6496febcbc3aeef4bbd64a0 [raw] [clear marker]

        
0
1
2#module_parameters(ENABLE_TRACING := true);
3
4
5Qtrace_Features :: enum_flags {
6 TIME;
7 CPU_COUNTER;
8}
9
10
11qtrace :: (
12 $label: string = "",
13 color: Console_Color = COLOR_DEFAULT,
14 $features: Qtrace_Features = .TIME,
15 loc := #caller_location
16) #expand {
17 #if ENABLE_TRACING {
18 state: Features_State;
19
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();
22
23 `defer delta_report(label, state, features, #procedure_name(), color, loc);
24 }
25}
26
27
28/** TODO: Include
29
30 CPU
31 - Cache misses (L1/L2/L3)
32 - IPC (instructions per cycle)
33 - Branch prediction hits/misses
34 - Stall cycles
35 - SIMD utilization
36
37 Memory
38 - TLB misses
39 - Memory bandwidth (bytes read/written)
40
41 Threading
42 - Lock contention / wait time
43 - Context switches
44 - Core migration frequency
45
46 Timing
47 - Frame time per system
48 - Hitches / frame time variance
49 - Function call frequency vs cost (hot paths)
50
51
52 Other Things:
53
54 Regarding hitches/spikes, instead for going towards p95/p99, catch outliers over a certain
55 time frame.
56
57 Maybe logging/printing in a separate thread?
58
59*/
60
61
62#scope_file
63
64
65COLOR_DEFAULT :: Console_Color.YELLOW;
66
67
68Features_State :: struct {
69 time: Feature_Time;
70 cpu_counter: Feature_CPU_Counter;
71}
72
73Feature_Time :: struct {
74 ts_start: Apollo_Time;
75}
76
77Feature_CPU_Counter :: struct {
78 count: u64;
79}
80
81
82delta_report :: (
83 $label: string,
84 state: Features_State,
85 $features: Qtrace_Features,
86 proc_name: string,
87 color: Console_Color,
88 loc: Source_Code_Location
89) {
90 using loc;
91
92 print_color("PROCEDURE: %", proc_name, color=color, style=.BOLD);
93 print("\n");
94 #if label {
95 print_color("Label: %", label, color=color);
96 print("\n");
97 }
98
99 #if features & .TIME then feature_time(state.time, color);
100 #if features & .CPU_COUNTER then feature_cpu_counter(state.cpu_counter, color);
101
102 print("\n");
103 print_color(
104 "Location: %: %:%",
105 fully_pathed_filename,
106 line_number,
107 character_number,
108 color=color
109 );
110 print("\n\n");
111}
112
113feature_time :: (using ft: Feature_Time, color: Console_Color) {
114 ts_now := current_time_monotonic();
115 delta := ts_now - ts_start;
116
117 us := to_microseconds(delta);
118 ms := to_milliseconds(delta);
119 s := to_seconds(delta);
120
121 print_color("Time Delta: % us % ms % s", us, ms, s, color=color);
122 newline();
123}
124
125feature_cpu_counter :: (using fc: Feature_CPU_Counter, color: Console_Color) {
126 count_now := read_cpu_counter();
127 delta := count_now - count;
128
129 delta_fmt := formatInt(delta, digits_per_comma=3, comma_string="_");
130
131 print_color("CPU Delta: %", delta_fmt, color=color);
132 newline();
133}
134
135newline :: () #expand {
136 print("\n");
137}
138
139
140using,only(
141 print, Source_Code_Location,
142 Apollo_Time, current_time_monotonic, to_seconds, to_microseconds, to_milliseconds,
143 operator-,
144 formatInt,
145) Basic :: #import "Basic";
146
147using,only(print_color, Console_Color) PC :: #import "Print_Color";
148using,only(read_cpu_counter) System :: #import "System";
149
150
151/*
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
171SOFTWARE.
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------------------------------------------------------------------------------
191*/
192
193
Copyright 2026  E766CB298A6D1E64 | Git-Thing heavily inspired by cgit