Skip to content

Commit 4be2706

Browse files
committed
Restored stepwise logging to monitor asynchronous steps
1 parent d8e045a commit 4be2706

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

src/main/java/bwapi/BWClientConfiguration.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ public class BWClientConfiguration {
5050
*/
5151
public int asyncFrameBufferSize = 10;
5252

53+
/**
54+
* Toggles verbose logging, particularly of synchronization steps.
55+
*/
56+
public boolean logVerbosely = false;
57+
5358
/**
5459
* Checks that the configuration is in a valid state. Throws an IllegalArgumentException if it isn't.
5560
*/
@@ -61,4 +66,10 @@ public void validate() {
6166
throw new IllegalArgumentException("asyncFrameBufferSize needs to be a positive number (There needs to be at least one frame buffer).");
6267
}
6368
}
69+
70+
public void log(String value) {
71+
if (logVerbosely) {
72+
System.out.println(value);
73+
}
74+
}
6475
}

src/main/java/bwapi/BotWrapper.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ Game getGame() {
7676
*/
7777
void onFrame() {
7878
if (configuration.async) {
79+
configuration.log("Main: onFrame asynchronous start");
7980
long startNanos = System.nanoTime();
8081
long endNanos = startNanos + configuration.asyncFrameDurationMs * 1000000;
8182
if (botThread == null) {
83+
configuration.log("Main: Starting bot thread");
8284
botThread = createBotThread();
8385
botThread.setName("JBWAPI Bot");
8486
botThread.start();
@@ -88,35 +90,44 @@ void onFrame() {
8890
If buffer is full, it will wait until it has capacity
8991
Wait for empty buffer OR termination condition
9092
*/
93+
configuration.log("Main: Enqueuing frame");
9194
boolean isFrameZero = liveClientData.gameData().getFrameCount() == 0;
9295
frameBuffer.enqueueFrame();
9396
performanceMetrics.bwapiResponse.startTiming();
9497
frameBuffer.lockSize.lock();
9598
try {
9699
while (!frameBuffer.empty()) {
100+
configuration.log("Main: Waiting for empty frame buffer");
97101

98102
// Make bot exceptions fall through to the main thread.
99103
Throwable lastThrow = getLastBotThrow();
100104
if (lastThrow != null) {
105+
configuration.log("Main: Rethrowing bot throwable");
101106
throw new RuntimeException(lastThrow);
102107
}
103108

104109
if (configuration.unlimitedFrameZero && isFrameZero) {
110+
configuration.log("Main: Waiting indefinitely on frame 0");
105111
frameBuffer.conditionSize.await();
106112
} else {
107113
long remainingNanos = endNanos - System.nanoTime();
108114
if (remainingNanos <= 0) break;
115+
configuration.log("Main: Waiting " + remainingNanos / 1000000 + "ms for bot");
109116
frameBuffer.conditionSize.awaitNanos(remainingNanos);
110117
}
111118
}
112119
} catch(InterruptedException ignored) {
113120
} finally {
114121
frameBuffer.lockSize.unlock();
115122
performanceMetrics.bwapiResponse.stopTiming();
123+
configuration.log("Main: onFrame asynchronous end");
116124
}
117125
} else {
126+
configuration.log("Main: onFrame synchronous start");
118127
handleEvents();
128+
configuration.log("Main: onFrame synchronous end");
119129
}
130+
120131
}
121132

122133
/**
@@ -139,20 +150,25 @@ Throwable getLastBotThrow() {
139150

140151
private Thread createBotThread() {
141152
return new Thread(() -> {
153+
configuration.log("Bot: Thread started");
142154
while ( ! gameOver) {
155+
configuration.log("Bot: Attempting to handle next frame");
143156
frameBuffer.lockSize.lock();
144157
try {
145158
while (frameBuffer.empty()) {
159+
configuration.log("Bot: Waiting for next frame");
146160
performanceMetrics.botIdle.startTiming();
147161
frameBuffer.conditionSize.awaitUninterruptibly();
148162
}
149163
performanceMetrics.botIdle.stopTiming();
150164
} finally {
151165
frameBuffer.lockSize.unlock();
152166
}
167+
configuration.log("Bot: Peeking next frame");
153168
game.clientData().setBuffer(frameBuffer.peek());
154169
performanceMetrics.frameBufferSize.record(frameBuffer.framesBuffered() - 1);
155170
try {
171+
configuration.log("Bot: Handling frame #" + game.getFrameCount());
156172
handleEvents();
157173
} finally {
158174
// In the case where t

src/main/java/bwapi/Game.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ private static boolean hasPower(final int x, final int y, final UnitType unitTyp
142142
Call this method in EventHander::OnMatchStart
143143
*/
144144
void init() {
145-
System.out.println("Game.init()"); // TODO: REMOVE!
146145
visibleUnits.clear();
147146

148147
final int forceCount = gameData().getForceCount();

src/main/java/bwapi/PerformanceMetric.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class PerformanceMetric {
88

99
public double minValue = Long.MAX_VALUE;
1010
public double maxValue = Long.MIN_VALUE;
11+
public double lastValue = 0;
1112
public double avgValue = 0;
1213
public double avgValueExceeding = 0;
1314
public int samples = 0;
@@ -44,6 +45,7 @@ void stopTiming() {
4445
}
4546

4647
void record(double value) {
48+
lastValue = value;
4749
minValue = Math.min(minValue, value);
4850
maxValue = Math.max(maxValue, value);
4951
avgValue = (avgValue * samples + value) / (samples + 1d);

src/test/java/bwapi/SynchronizationTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,12 @@ public void async_IfDelay_ThenBuffer() throws InterruptedException {
6666
environment.configuration.async = true;
6767
environment.configuration.asyncFrameDurationMs = 1;
6868
environment.configuration.asyncFrameBufferSize = 3;
69+
environment.configuration.logVerbosely = true;
6970
environment.onFrame(1, () -> {
70-
sleepUnchecked(5);
71+
sleepUnchecked(50);
7172
assertEquals("Bot should be observing an old frame", 1, environment.bwClient.getGame().getFrameCount());
72-
assertEquals("Bot should be behind the live game", 2, environment.bwClient.framesBehind());
7373
assertEquals("Client should be as far ahead as the frame buffer allows", 3, environment.liveGameData().getFrameCount());
74+
assertEquals("Bot should be behind the live game", 2, environment.bwClient.framesBehind());
7475
});
7576
environment.runGame();
7677
}

0 commit comments

Comments
 (0)