Skip to content

Commit

Permalink
Merge branch 'main' into rlog-r2
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbonner committed Mar 13, 2024
2 parents ad212a9 + ce678df commit 5768b56
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

package org.littletonrobotics.junction;

import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.List;

import edu.wpi.first.hal.DriverStationJNI;
import edu.wpi.first.hal.FRCNetComm.tInstances;
import edu.wpi.first.hal.FRCNetComm.tResourceType;
Expand All @@ -32,11 +36,11 @@
* Notifier instance.
*/
public class LoggedRobot extends IterativeRobotBase {

public static final double defaultPeriodSecs = 0.02;
private final int notifier = NotifierJNI.initializeNotifier();
private final long periodUs;
private long nextCycleUs = 0;
private final GcStatsCollector gcStatsCollector = new GcStatsCollector();

private boolean useTiming = true;

Expand Down Expand Up @@ -113,6 +117,7 @@ public void startCompetition() {
loopFunc();
long userCodeEnd = Logger.getRealTimestamp();

gcStatsCollector.update();
Logger.periodicAfterUser(userCodeEnd - userCodeStart, userCodeStart - periodicBeforeStart);
}
}
Expand All @@ -127,4 +132,27 @@ public void endCompetition() {
public void setUseTiming(boolean useTiming) {
this.useTiming = useTiming;
}

private static final class GcStatsCollector {
private List<GarbageCollectorMXBean> gcBeans = ManagementFactory.getGarbageCollectorMXBeans();
private final long[] lastTimes = new long[gcBeans.size()];
private final long[] lastCounts = new long[gcBeans.size()];

public void update() {
long accumTime = 0;
long accumCounts = 0;
for (int i = 0; i < gcBeans.size(); i++) {
long gcTime = gcBeans.get(i).getCollectionTime();
long gcCount = gcBeans.get(i).getCollectionCount();
accumTime += gcTime - lastTimes[i];
accumCounts += gcCount - lastCounts[i];

lastTimes[i] = gcTime;
lastCounts[i] = gcCount;
}

Logger.recordOutput("LoggedRobot/GCTimeMS", (double) accumTime);
Logger.recordOutput("LoggedRobot/GCCounts", (double) accumCounts);
}
}
}
38 changes: 26 additions & 12 deletions junction/core/src/org/littletonrobotics/junction/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ public class Logger {
private static LogTable entry = new LogTable(0);
private static LogTable outputTable;
private static Map<String, String> metadata = new HashMap<>();
private static ConsoleSource console;
private static ConsoleSource console = null;
private static List<LoggedDashboardInput> dashboardInputs = new ArrayList<>();
private static Supplier<ByteBuffer[]> urclSupplier = null;
private static boolean deterministicTimestamps = true;
private static boolean enableConsole = false;

private static LogReplaySource replaySource;
private static final BlockingQueue<LogTable> receiverQueue = new ArrayBlockingQueue<LogTable>(receiverQueueCapcity);
Expand Down Expand Up @@ -160,6 +161,13 @@ public static void disableDeterministicTimestamps() {
deterministicTimestamps = false;
}

/**
* Disables automatic console capture.
*/
public static void disableConsoleCapture() {
enableConsole = false;
}

/**
* Returns whether a replay source is currently being used.
*/
Expand All @@ -176,10 +184,12 @@ public static void start() {
running = true;

// Start console capture
if (RobotBase.isReal()) {
console = new RIOConsoleSource();
} else {
console = new SimConsoleSource();
if (enableConsole) {
if (RobotBase.isReal()) {
console = new RIOConsoleSource();
} else {
console = new SimConsoleSource();
}
}

// Start replay source
Expand Down Expand Up @@ -217,10 +227,12 @@ public static void start() {
public static void end() {
if (running) {
running = false;
try {
console.close();
} catch (Exception e) {
DriverStation.reportError("Failed to stop console capture.", true);
if (console != null) {
try {
console.close();
} catch (Exception e) {
DriverStation.reportError("Failed to stop console capture.", true);
}
}
if (replaySource != null) {
replaySource.end();
Expand Down Expand Up @@ -316,9 +328,11 @@ static void periodicAfterUser(long userCodeLength, long periodicBeforeLength) {
long autoLogStart = getRealTimestamp();
AutoLogOutputManager.periodic();
long consoleCaptureStart = getRealTimestamp();
String consoleData = console.getNewData();
if (!consoleData.isEmpty()) {
recordOutput("Console", consoleData.trim());
if (enableConsole) {
String consoleData = console.getNewData();
if (!consoleData.isEmpty()) {
recordOutput("Console", consoleData.trim());
}
}
long consoleCaptureEnd = getRealTimestamp();

Expand Down

0 comments on commit 5768b56

Please sign in to comment.