Skip to content

Commit

Permalink
added units to docs and netwrok tables
Browse files Browse the repository at this point in the history
  • Loading branch information
oh-yes-0-fps committed Oct 17, 2024
1 parent 9272208 commit b3ec9d7
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 17 deletions.
4 changes: 2 additions & 2 deletions wpilibc/src/main/native/cpp/TimedRobot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void TimedRobot::StartCompetition() {
while (m_callbacks.top().expirationTime <= currentTime) {
callback = m_callbacks.pop();

callback.func();
callback();

callback.expirationTime +=
callback.period + (currentTime - callback.expirationTime) /
Expand All @@ -79,7 +79,7 @@ void TimedRobot::EndCompetition() {

TimedRobot::TimedRobot(units::second_t period) : IterativeRobotBase(period) {
m_startTime = std::chrono::microseconds{RobotController::GetFPGATime()};
AddPeriodic([=, this] { LoopFunc(); }, "RobotMain", period);
AddPeriodic([=, this] { LoopFunc(); }, "", period);

int32_t status = 0;
m_notifier = HAL_InitializeNotifier(&status);
Expand Down
8 changes: 8 additions & 0 deletions wpilibc/src/main/native/cpp/Tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,19 @@ using namespace frc;
std::atomic<bool> singleThreadedMode = false;
std::atomic<bool> anyTracesStarted = false;

void PostUnits() {
auto table = nt::NetworkTableInstance::GetDefault().GetTable("/Tracer");
table->GetEntry("TimingUnits").SetString("Milliseconds", 0);
}

Tracer::TracerState::TracerState() {
if (singleThreadedMode && anyTracesStarted) {
FRC_ReportWarning("Cannot start a new trace in single-threaded mode");
m_disabled = true;
}
if (!anyTracesStarted) {
PostUnits();
}
auto inst = nt::NetworkTableInstance::GetDefault();
m_rootTable = inst.GetTable(fmt::format(
"/Tracer/{}", std::hash<std::thread::id>{}(std::this_thread::get_id())));
Expand Down
24 changes: 21 additions & 3 deletions wpilibc/src/main/native/include/frc/TimedRobot.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class TimedRobot : public IterativeRobotBase {
class Callback {
public:
std::function<void()> func;
frc::Tracer::SubstitutiveTracer tracer;
std::optional<frc::Tracer::SubstitutiveTracer> tracer;
std::chrono::microseconds period;
std::chrono::microseconds expirationTime;

Expand All @@ -115,13 +115,31 @@ class TimedRobot : public IterativeRobotBase {
std::chrono::microseconds startTime,
std::chrono::microseconds period, std::chrono::microseconds offset)
: func{std::move(func)},
tracer{frc::Tracer::SubstitutiveTracer{name}},
period{period},
expirationTime(
startTime + offset + period +
(std::chrono::microseconds{frc::RobotController::GetFPGATime()} -
startTime) /
period * period) {}
period * period) {
if (!name.empty()) {
tracer.emplace(name);
} else {
tracer = std::nullopt;
}
}

/**
* Call the callback function.
*/
void operator()() {
if (tracer.has_value()) {
tracer->SubIn();
}
func();
if (tracer.has_value()) {
tracer->SubOut();
}
}

bool operator>(const Callback& rhs) const {
return expirationTime > rhs.expirationTime;
Expand Down
9 changes: 3 additions & 6 deletions wpilibc/src/main/native/include/frc/Tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ class raw_ostream;

namespace frc {
/**
* A class for keeping track of how much time it takes for different parts of
* code to execute. This is done with epochs, that are added to calls to
* AddEpoch() and can be printed with a call to PrintEpochs().
*
* Epochs are a way to partition the time elapsed so that when overruns occur,
* one can determine which parts of an operation consumed the most time.
* A Utility class for tracing code execution time. Will put info to
* NetworkTables under the "Tracer" table. The times outputted by the {@link
* Tracer} are in milliseconds.
*/
class Tracer {
public:
Expand Down
13 changes: 9 additions & 4 deletions wpilibj/src/main/java/edu/wpi/first/wpilibj/TimedRobot.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import edu.wpi.first.hal.NotifierJNI;
import edu.wpi.first.units.measure.Time;
import edu.wpi.first.wpilibj.Tracer.SubstitutiveTracer;

import java.util.Optional;
import java.util.PriorityQueue;

/**
Expand All @@ -32,7 +34,7 @@ static class Callback implements Comparable<Callback> {
* The tracer to use for this callback. This allows callbacks to appear in their own tracer
* table separate from the main RobotLoop tracer table.
*/
public SubstitutiveTracer m_tracerSub;
public Optional<SubstitutiveTracer> m_tracerSub;

/** The period at which to run the callback in microseconds. */
public long m_period;
Expand All @@ -53,7 +55,7 @@ static class Callback implements Comparable<Callback> {
*/
Callback(Runnable func, String name, long startTimeUs, long periodUs, long offsetUs) {
this.m_func = func;
this.m_tracerSub = new SubstitutiveTracer(name);
this.m_tracerSub = Optional.ofNullable(name).map(SubstitutiveTracer::new);
this.m_period = periodUs;
this.m_expirationTime =
startTimeUs
Expand All @@ -63,7 +65,10 @@ static class Callback implements Comparable<Callback> {
}

void call() {
m_tracerSub.subWith(m_func);
m_tracerSub.ifPresentOrElse(
tracer -> tracer.subWith(m_func),
m_func
);
}

@Override
Expand Down Expand Up @@ -108,7 +113,7 @@ protected TimedRobot() {
protected TimedRobot(double period) {
super(period);
m_startTimeUs = RobotController.getFPGATime();
addPeriodic(this::loopFunc, "RobotMain", period);
addPeriodic(this::loopFunc, null, period);
NotifierJNI.setNotifierName(m_notifier, "TimedRobot");

HAL.report(tResourceType.kResourceType_Framework, tInstances.kFramework_Timed);
Expand Down
15 changes: 13 additions & 2 deletions wpilibj/src/main/java/edu/wpi/first/wpilibj/Tracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

/**
* A Utility class for tracing code execution time. Will put info to NetworkTables under the
* "Tracer" table.
* "Tracer" table. The times outputted by the {@link Tracer} are in milliseconds.
*
* <p>Example inside {@code Robot.java}
*
Expand Down Expand Up @@ -117,7 +117,9 @@ private TracerState(String name, boolean threadLocalConstruction) {
true);
this.m_disabled = true;
}
anyTracesStarted.set(true);
if (!anyTracesStarted.get()) {
postUnits();
}
if (name == null) {
this.m_rootTable = NetworkTableInstance.getDefault().getTable("Tracer");
} else {
Expand Down Expand Up @@ -211,7 +213,15 @@ private void endCycle() {
return new TracerState(Thread.currentThread().getName(), true);
});

private static void postUnits() {
NetworkTableInstance.getDefault()
.getTable("Tracer")
.getEntry("TimingUnits")
.setString("Milliseconds");
}

private static void startTraceInner(final String name, final TracerState state) {
anyTracesStarted.set(true);
String stack = state.appendTraceStack(name);
if (state.m_disabled) {
return;
Expand Down Expand Up @@ -285,6 +295,7 @@ public static void disableGcLoggingForCurrentThread() {
*
* <p>This function should be called before any traces are started.
*/
@SuppressWarnings("unused")
public static void enableSingleThreadedMode() {
if (anyTracesStarted.get()) {
DriverStation.reportError(
Expand Down

0 comments on commit b3ec9d7

Please sign in to comment.