This repository was archived by the owner on Jan 24, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Profiling framework #114
Open
rcahoon
wants to merge
3
commits into
main
Choose a base branch
from
rcahoon/profiling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Profiling framework #114
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package com.team766.framework; | ||
|
|
||
| import com.team766.config.ConfigFileReader; | ||
| import com.team766.library.RateLimiter; | ||
| import com.team766.library.ValueProvider; | ||
| import com.team766.logging.Category; | ||
| import com.team766.logging.Severity; | ||
| import org.littletonrobotics.junction.Logger; | ||
|
|
||
| public class Profiling { | ||
| private static double getTime() { | ||
| return System.currentTimeMillis(); | ||
| } | ||
|
|
||
| public static class Scope implements AutoCloseable { | ||
| private double start; | ||
| private int index; | ||
| private String name; | ||
|
|
||
| private void start(String name, int index) { | ||
| this.name = name; | ||
| this.index = index; | ||
| this.start = getTime(); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| if (index != scopeIndex - 1) { | ||
| throw new IllegalStateException( | ||
| "Ended scope " | ||
| + name | ||
| + " but expected end of scope " | ||
| + scopes[scopeIndex - 1]); | ||
| } | ||
| --scopeIndex; | ||
| addSample(name, getTime() - start); | ||
| name = null; | ||
| } | ||
| } | ||
|
|
||
| private static final int PROFILING_SCOPE_STACK_DEPTH = 20; | ||
|
|
||
| private static final ValueProvider<Boolean> profilingEnabled = | ||
|
dejabot marked this conversation as resolved.
|
||
| ConfigFileReader.instance.getBoolean("profiling.enabled"); | ||
| private static final Scope[] scopes = new Scope[PROFILING_SCOPE_STACK_DEPTH]; | ||
| private static int scopeIndex = 0; | ||
| private static RateLimiter logWarningLimiter = new RateLimiter(1.0); | ||
|
|
||
| static { | ||
| for (int i = 0; i < scopes.length; ++i) { | ||
| scopes[i] = new Scope(); | ||
| } | ||
| } | ||
|
|
||
| public static Scope scope(String name) { | ||
| if (!profilingEnabled.valueOr(false)) { | ||
| return null; | ||
| } | ||
| if (scopeIndex >= scopes.length) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. log some kind of warning?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 done
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pushed? |
||
| if (logWarningLimiter.next()) { | ||
| com.team766.logging.Logger.get(Category.FRAMEWORK) | ||
| .logRaw( | ||
| Severity.ERROR, | ||
| "Exceeded the maximum stack depth of profiling scopes. Increase " | ||
| + "PROFILING_SCOPE_STACK_DEPTH or reduce the number of " | ||
| + "profiling scopes."); | ||
| } | ||
| return null; | ||
| } | ||
| final var scope = scopes[scopeIndex]; | ||
| scope.start(name, scopeIndex); | ||
| ++scopeIndex; | ||
| return scope; | ||
| } | ||
|
|
||
| public static void addSample(String name, double duration) { | ||
|
dejabot marked this conversation as resolved.
|
||
| if (!profilingEnabled.valueOr(false)) { | ||
| return; | ||
| } | ||
| Logger.recordOutput("Profiling/" + name, duration); | ||
| } | ||
|
|
||
| private Profiling() {} | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.