Package com.aembot.lib.tracing
Class Tracer
java.lang.Object
com.aembot.lib.tracing.Tracer
Lightweight tracing system for measuring function execution times. Uses a circular buffer of
pre-allocated loops to avoid GC during robot loop.
Usage:
// In robotPeriodic():
Tracer.beginLoop();
// ... robot code (traced via @Traced annotations)
Tracer.endLoop();
// Export when disabled:
Tracer.exportToJson("/U/logs/trace.json");
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringDefault category used when none is specified -
Method Summary
Modifier and TypeMethodDescriptionstatic voidBegin a new trace loop.static intBegin a trace span for a function call.static intBegin a trace span with a pre-resolved category index.static intBegin a trace span for a function call with a category.static voidendLoop()End the current trace loop.static voidendSpan(int spanIndex) End a trace span.static voidexportRecentToJson(String path, int numLoops) Export the most recent N loops to Chrome Tracing JSON format.static voidexportToJson(String path) Export traces to Chrome Tracing JSON format.static intGet the buffer sizeGet the category list (for export)static bytegetCategoryIndex(String category) Get or register a category index.static TraceLoopGet the current loop being written tostatic intGet the current loop index in the circular bufferstatic TraceLoop[]getLoops()Get all loops in the bufferGet the thread name cache (for export)static intGet total number of loops recordedstatic booleanCheck if tracing is enabledstatic voidsetEnabled(boolean enable) Enable or disable tracing.static TraceScopeCreate a trace scope for a function.static TraceScopeCreate a trace scope for a function with a category.
-
Field Details
-
DEFAULT_CATEGORY
Default category used when none is specified- See Also:
-
-
Method Details
-
getCategoryIndex
Get or register a category index. Returns existing index if category already registered. Uses HashMap for O(1) lookup. Public for use by interceptors that cache the index.- Parameters:
category- The category name- Returns:
- The byte index for this category
-
beginLoop
public static void beginLoop()Begin a new trace loop. Call this at the start of robotPeriodic().This advances the circular buffer and resets the current loop.
-
endLoop
public static void endLoop()End the current trace loop. Call this at the end of robotPeriodic().Records the loop end time.
-
trace
Create a trace scope for a function. Use with try-with-resources.Usage:
try (var t = Tracer.trace("MyClass.myMethod")) { // Method body - automatically timed }- Parameters:
name- The name of the function/operation being traced- Returns:
- A TraceScope that will automatically end the span when closed
-
trace
Create a trace scope for a function with a category. Use with try-with-resources.Usage:
try (var t = Tracer.trace("periodic", "Drivetrain")) { // Method body - automatically timed and categorized }- Parameters:
name- The name of the function/operation being tracedcategory- The category/subsystem (e.g., "Drivetrain", "Vision")- Returns:
- A TraceScope that will automatically end the span when closed
-
beginSpan
Begin a trace span for a function call.- Parameters:
name- The name of the function/operation being traced- Returns:
- The span index (pass to endSpan), or -1 if tracing is disabled/full
-
beginSpan
Begin a trace span for a function call with a category.- Parameters:
name- The name of the function/operation being tracedcategory- The category/subsystem (e.g., "Drivetrain", "Vision")- Returns:
- The span index (pass to endSpan), or -1 if tracing is disabled/full
-
beginSpan
Begin a trace span with a pre-resolved category index. Faster than string category lookup. Use this from interceptors that cache the category index at first call.- Parameters:
name- The name of the function/operation being tracedcategoryIndex- The category index (from getCategoryIndex)- Returns:
- The span index (pass to endSpan), or -1 if tracing is disabled/full
-
endSpan
public static void endSpan(int spanIndex) End a trace span.- Parameters:
spanIndex- The index returned by beginSpan
-
isEnabled
public static boolean isEnabled()Check if tracing is enabled -
setEnabled
public static void setEnabled(boolean enable) Enable or disable tracing. Disable for competition to eliminate all overhead.- Parameters:
enable- Whether to enable tracing
-
getCurrentLoop
Get the current loop being written to -
getLoops
Get all loops in the buffer -
getBufferSize
public static int getBufferSize()Get the buffer size -
getLoopIndex
public static int getLoopIndex()Get the current loop index in the circular buffer -
getTotalLoopCount
public static int getTotalLoopCount()Get total number of loops recorded -
getCategories
Get the category list (for export) -
getThreadNames
Get the thread name cache (for export) -
exportToJson
Export traces to Chrome Tracing JSON format.- Parameters:
path- The file path to write to
-
exportRecentToJson
Export the most recent N loops to Chrome Tracing JSON format.- Parameters:
path- The file path to write tonumLoops- Number of recent loops to export
-