Add ThreadSleepPerformancePatch variants for Build 42.21 to optimize waiting loops. Replace Thread.sleep calls with hook-based alternatives. Include hooks, utility and tests.
This commit is contained in:
1 parent
529e48ed5a
commit
88cd68b5cb
12 files changed
+603
-1
No files matched your search
+6
-1
@@ -32,7 +32,12 @@ public final class B42PatchSet implements PatchSet
|
||||
new WorldGenChunkRulesPerformancePatch(),
|
||||
new BitHeaderPoolPerformancePatch(),
|
||||
new IsoChunkSaveDirectoryPerformancePatch(),
|
||||
new RolesSavePerformancePatch()
|
||||
new RolesSavePerformancePatch(),
|
||||
new ServerChunkLoaderQuitPerformancePatch(),
|
||||
new ZipBackupWaitPerformancePatch(),
|
||||
new ServerPlayersVehiclesRoundPerformancePatch(),
|
||||
new ServerPlayersVehiclesStopPerformancePatch(),
|
||||
new ServerPlayerDBSaveWaitPerformancePatch()
|
||||
);
|
||||
|
||||
@Override
|
||||
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.perf;
|
||||
|
||||
import dev.overlord.zomboidforge.api.GameVersion;
|
||||
import dev.overlord.zomboidforge.patcher.Anchor;
|
||||
import dev.overlord.zomboidforge.patcher.Hook;
|
||||
import dev.overlord.zomboidforge.patcher.Patches.ThreadSleepPerformancePatch;
|
||||
|
||||
/**
|
||||
* {@code ServerChunkLoader.quit()} waits for the chunk loading thread, then for the chunk saving
|
||||
* thread, each with {@code while (thread.isAlive()) Thread.sleep(500);}: at least 1 s of every
|
||||
* shutdown although both threads end within milliseconds. The sleeps become
|
||||
* {@code WaitHooks.pollSleep} (at most 5 ms).
|
||||
*/
|
||||
public final class ServerChunkLoaderQuitPerformancePatch extends ThreadSleepPerformancePatch
|
||||
{
|
||||
private static final Hook HOOK = new Hook("dev/overlord/zomboidforge/compat/b42/hooks/WaitHooks", "pollSleep", "(J)V");
|
||||
|
||||
public ServerChunkLoaderQuitPerformancePatch() {
|
||||
super("zombie/network/ServerChunkLoader", new Anchor.Method("quit", "()V"), 2, ServerChunkLoaderQuitPerformancePatch.HOOK);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public boolean appliesTo(GameVersion version) { return version.matches("42.21"); }
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.perf;
|
||||
|
||||
import dev.overlord.zomboidforge.api.GameVersion;
|
||||
import dev.overlord.zomboidforge.patcher.Anchor;
|
||||
import dev.overlord.zomboidforge.patcher.Hook;
|
||||
import dev.overlord.zomboidforge.patcher.Patches.ThreadSleepPerformancePatch;
|
||||
|
||||
/**
|
||||
* {@code ServerPlayerDB.saveFinishWait()} waits until the {@code ServerPlayersVehicles} thread
|
||||
* has saved the queued players: {@code while (!charactersToSave.isEmpty()) Thread.sleep(100);}.
|
||||
* The thread saves them at the start of its next round, up to 500 ms later. The sleep becomes
|
||||
* {@code WaitHooks.wakeVehiclesAndPoll}: it starts that round now
|
||||
* ({@link ServerPlayersVehiclesRoundPerformancePatch}) and polls every 5 ms.
|
||||
*/
|
||||
public final class ServerPlayerDBSaveWaitPerformancePatch extends ThreadSleepPerformancePatch
|
||||
{
|
||||
private static final Hook HOOK = new Hook("dev/overlord/zomboidforge/compat/b42/hooks/WaitHooks", "wakeVehiclesAndPoll", "(J)V");
|
||||
|
||||
public ServerPlayerDBSaveWaitPerformancePatch() {
|
||||
super("zombie/savefile/ServerPlayerDB", new Anchor.Method("saveFinishWait", "()V"), 1, ServerPlayerDBSaveWaitPerformancePatch.HOOK);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public boolean appliesTo(GameVersion version) { return version.matches("42.21"); }
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.perf;
|
||||
|
||||
import dev.overlord.zomboidforge.api.GameVersion;
|
||||
import dev.overlord.zomboidforge.patcher.Anchor;
|
||||
import dev.overlord.zomboidforge.patcher.Hook;
|
||||
import dev.overlord.zomboidforge.patcher.Patches.ThreadSleepPerformancePatch;
|
||||
|
||||
/**
|
||||
* The {@code ServerPlayersVehicles} thread saves queued players and streams vehicles, then sleeps
|
||||
* 500 ms ({@code SPVThread.runInner}). The sleep becomes {@code WaitHooks.vehicleRoundSleep}: the
|
||||
* same 500 ms, but {@link ServerPlayersVehiclesStopPerformancePatch} and
|
||||
* {@link ServerPlayerDBSaveWaitPerformancePatch} can end it early.
|
||||
*/
|
||||
public final class ServerPlayersVehiclesRoundPerformancePatch extends ThreadSleepPerformancePatch
|
||||
{
|
||||
private static final Hook HOOK = new Hook("dev/overlord/zomboidforge/compat/b42/hooks/WaitHooks", "vehicleRoundSleep", "(J)V");
|
||||
|
||||
public ServerPlayersVehiclesRoundPerformancePatch() {
|
||||
super("zombie/network/ServerPlayersVehicles$SPVThread", new Anchor.Method("runInner", "()V"), 1, ServerPlayersVehiclesRoundPerformancePatch.HOOK);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public boolean appliesTo(GameVersion version) { return version.matches("42.21"); }
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.perf;
|
||||
|
||||
import dev.overlord.zomboidforge.api.GameVersion;
|
||||
import dev.overlord.zomboidforge.patcher.Anchor;
|
||||
import dev.overlord.zomboidforge.patcher.Hook;
|
||||
import dev.overlord.zomboidforge.patcher.Patches.ThreadSleepPerformancePatch;
|
||||
|
||||
/**
|
||||
* {@code ServerPlayersVehicles.stop()} sets the thread's stop flag and waits with
|
||||
* {@code while (thread.isAlive()) Thread.sleep(100);}, while the thread may sleep up to 500 ms
|
||||
* before it sees the flag. The sleep becomes {@code WaitHooks.wakeVehiclesAndPoll}: it ends the
|
||||
* thread's round sleep ({@link ServerPlayersVehiclesRoundPerformancePatch}) and polls every 5 ms.
|
||||
*/
|
||||
public final class ServerPlayersVehiclesStopPerformancePatch extends ThreadSleepPerformancePatch
|
||||
{
|
||||
private static final Hook HOOK = new Hook("dev/overlord/zomboidforge/compat/b42/hooks/WaitHooks", "wakeVehiclesAndPoll", "(J)V");
|
||||
|
||||
public ServerPlayersVehiclesStopPerformancePatch() {
|
||||
super("zombie/network/ServerPlayersVehicles", new Anchor.Method("stop", "()V"), 1, ServerPlayersVehiclesStopPerformancePatch.HOOK);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public boolean appliesTo(GameVersion version) { return version.matches("42.21"); }
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.perf;
|
||||
|
||||
import dev.overlord.zomboidforge.api.GameVersion;
|
||||
import dev.overlord.zomboidforge.patcher.Anchor;
|
||||
import dev.overlord.zomboidforge.patcher.Hook;
|
||||
import dev.overlord.zomboidforge.patcher.Patches.ThreadSleepPerformancePatch;
|
||||
|
||||
/**
|
||||
* {@code ZipBackup.waitFinish()}, called at the start of the shutdown, waits for a running
|
||||
* periodic backup with {@code while (backupThread.isAlive()) Thread.sleep(500);}. The sleep
|
||||
* becomes {@code WaitHooks.pollSleep} (at most 5 ms), so the shutdown continues when the backup
|
||||
* ends instead of up to 500 ms later.
|
||||
*/
|
||||
public final class ZipBackupWaitPerformancePatch extends ThreadSleepPerformancePatch
|
||||
{
|
||||
private static final Hook HOOK = new Hook("dev/overlord/zomboidforge/compat/b42/hooks/WaitHooks", "pollSleep", "(J)V");
|
||||
|
||||
public ZipBackupWaitPerformancePatch() {
|
||||
super("zombie/core/backup/ZipBackup", new Anchor.Method("waitFinish", "()V"), 1, ZipBackupWaitPerformancePatch.HOOK);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public boolean appliesTo(GameVersion version) { return version.matches("42.21"); }
|
||||
}
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.perf;
|
||||
|
||||
import dev.overlord.zomboidforge.api.GameVersion;
|
||||
import dev.overlord.zomboidforge.compat.b42.patches.support.ClassDefiner;
|
||||
import dev.overlord.zomboidforge.patcher.Patches.ThreadSleepPerformancePatch;
|
||||
|
||||
import org.jspecify.annotations.NonNull;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.tree.AbstractInsnNode;
|
||||
import org.objectweb.asm.tree.ClassNode;
|
||||
import org.objectweb.asm.tree.MethodInsnNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
|
||||
import static org.objectweb.asm.Opcodes.INVOKESTATIC;
|
||||
import static org.objectweb.asm.Opcodes.LCONST_1;
|
||||
import static org.objectweb.asm.Opcodes.RETURN;
|
||||
import static org.objectweb.asm.Opcodes.V25;
|
||||
|
||||
/** The five Build 42 waiting-loop patches: each with its target, number of sleeps and hook. */
|
||||
class ThreadSleepPatchesTest
|
||||
{
|
||||
private static final String HOOKS = "dev/overlord/zomboidforge/compat/b42/hooks/WaitHooks";
|
||||
|
||||
/** Each patch with its target method, number of sleeps and hook. */
|
||||
private record Case(ThreadSleepPerformancePatch patch, String target, String method, int sleeps, String hook)
|
||||
{
|
||||
@Override
|
||||
public @NonNull String toString() { return this.patch.getClass().getSimpleName(); }
|
||||
}
|
||||
|
||||
private static Stream<Case> cases()
|
||||
{
|
||||
return Stream.of(
|
||||
new Case(new ServerChunkLoaderQuitPerformancePatch(), "zombie/network/ServerChunkLoader", "quit", 2, "pollSleep"),
|
||||
new Case(new ZipBackupWaitPerformancePatch(), "zombie/core/backup/ZipBackup", "waitFinish", 1, "pollSleep"),
|
||||
new Case(new ServerPlayersVehiclesRoundPerformancePatch(), "zombie/network/ServerPlayersVehicles$SPVThread", "runInner", 1, "vehicleRoundSleep"),
|
||||
new Case(new ServerPlayersVehiclesStopPerformancePatch(), "zombie/network/ServerPlayersVehicles", "stop", 1, "wakeVehiclesAndPoll"),
|
||||
new Case(new ServerPlayerDBSaveWaitPerformancePatch(), "zombie/savefile/ServerPlayerDB", "saveFinishWait", 1, "wakeVehiclesAndPoll")
|
||||
);
|
||||
}
|
||||
|
||||
/** The target class with {@code method()} calling {@code Thread.sleep(1L)} {@code sleeps} times. */
|
||||
private static ClassNode target(Case test, int sleeps)
|
||||
{
|
||||
ClassNode node = new ClassNode();
|
||||
node.visit(V25, ACC_PUBLIC, test.target(), null, "java/lang/Object", null);
|
||||
|
||||
MethodVisitor method = node.visitMethod(ACC_PUBLIC, test.method(), "()V", null, null);
|
||||
for (int i = 0; i < sleeps; i++)
|
||||
{
|
||||
method.visitInsn(LCONST_1);
|
||||
method.visitMethodInsn(INVOKESTATIC, "java/lang/Thread", "sleep", "(J)V", false);
|
||||
}
|
||||
method.visitInsn(RETURN);
|
||||
method.visitMaxs(0, 0);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
private static List<MethodInsnNode> calls(ClassNode node)
|
||||
{
|
||||
List<MethodInsnNode> calls = new ArrayList<>();
|
||||
|
||||
for (AbstractInsnNode instruction : node.methods.getFirst().instructions) {
|
||||
if (instruction instanceof MethodInsnNode call) calls.add(call);
|
||||
}
|
||||
|
||||
return calls;
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("cases")
|
||||
void anchorMatchesTheTarget(Case test)
|
||||
{
|
||||
test.patch().anchor().verify(ThreadSleepPatchesTest.target(test, test.sleeps()));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("cases")
|
||||
void redirectsEverySleepToItsHook(Case test) throws ClassNotFoundException
|
||||
{
|
||||
ClassNode node = ThreadSleepPatchesTest.target(test, test.sleeps());
|
||||
test.patch().apply(node);
|
||||
|
||||
List<MethodInsnNode> calls = ThreadSleepPatchesTest.calls(node);
|
||||
assertEquals(test.sleeps(), calls.size());
|
||||
for (MethodInsnNode call : calls)
|
||||
{
|
||||
assertEquals(INVOKESTATIC, call.getOpcode());
|
||||
assertEquals(ThreadSleepPatchesTest.HOOKS, call.owner);
|
||||
assertEquals(test.hook(), call.name);
|
||||
assertEquals("(J)V", call.desc);
|
||||
}
|
||||
assertEquals(test.hook(), test.patch().hook().name());
|
||||
|
||||
// Initializing links the class, which runs the bytecode verifier on the changed method.
|
||||
ClassDefiner definer = new ClassDefiner();
|
||||
definer.define(node);
|
||||
Class.forName(node.name.replace('/', '.'), true, definer);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("cases")
|
||||
void failsForAnotherNumberOfSleeps(Case test)
|
||||
{
|
||||
ClassNode node = ThreadSleepPatchesTest.target(test, test.sleeps() + 1);
|
||||
|
||||
IllegalStateException e = assertThrows(IllegalStateException.class, () -> test.patch().apply(node));
|
||||
assertTrue(e.getMessage().contains("found " + (test.sleeps() + 1)), e.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("cases")
|
||||
void appliesOnlyToVerifiedVersions(Case test)
|
||||
{
|
||||
assertTrue(test.patch().appliesTo(GameVersion.parse("42.21.0")));
|
||||
assertTrue(test.patch().appliesTo(GameVersion.parse("42.21.3")));
|
||||
assertFalse(test.patch().appliesTo(GameVersion.parse("42.20.3")));
|
||||
assertFalse(test.patch().appliesTo(GameVersion.parse("42.22.0")));
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.hooks;
|
||||
|
||||
import dev.overlord.zomboidforge.compat.b42.util.WakeableSleep;
|
||||
|
||||
/**
|
||||
* Hooks for the waiting loops of the shutdown and the world save, which poll with
|
||||
* {@code Thread.sleep(100)} or {@code Thread.sleep(500)}: {@code ServerChunkLoader.quit},
|
||||
* {@code ZipBackup.waitFinish}, {@code ServerPlayersVehicles.stop},
|
||||
* {@code ServerPlayerDB.saveFinishWait}, and the round sleep of the
|
||||
* {@code ServerPlayersVehicles} thread. The loops check the same conditions as in vanilla; the
|
||||
* hooks only let them notice sooner that the condition holds.
|
||||
*/
|
||||
public final class WaitHooks
|
||||
{
|
||||
/** Longest sleep of a polling loop. */
|
||||
private static final long POLL_MILLIS = 5L;
|
||||
|
||||
/** The round sleep of the {@code ServerPlayersVehicles} thread. */
|
||||
private static final WakeableSleep VEHICLE_ROUND = new WakeableSleep();
|
||||
|
||||
private WaitHooks() {}
|
||||
|
||||
/** Replaces {@code Thread.sleep(millis)} in a polling loop: sleeps at most 5 ms. */
|
||||
public static void pollSleep(long millis) throws InterruptedException
|
||||
{
|
||||
Thread.sleep(Math.min(millis, WaitHooks.POLL_MILLIS));
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces {@code Thread.sleep(500)} at the end of a round of the {@code ServerPlayersVehicles}
|
||||
* thread ({@code SPVThread.runInner}): the same sleep, unless {@link #wakeVehiclesAndPoll}
|
||||
* ends it early.
|
||||
*/
|
||||
public static void vehicleRoundSleep(long millis) throws InterruptedException
|
||||
{
|
||||
WaitHooks.VEHICLE_ROUND.sleep(millis);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces {@code Thread.sleep(100)} in loops that wait for the {@code ServerPlayersVehicles}
|
||||
* thread ({@code ServerPlayersVehicles.stop}: until it ended; {@code
|
||||
* ServerPlayerDB.saveFinishWait}: until it saved the queued players): wakes the thread, so its
|
||||
* next round starts now, then sleeps at most 5 ms.
|
||||
*/
|
||||
public static void wakeVehiclesAndPoll(long millis) throws InterruptedException
|
||||
{
|
||||
WaitHooks.VEHICLE_ROUND.wake();
|
||||
Thread.sleep(Math.min(millis, WaitHooks.POLL_MILLIS));
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.util;
|
||||
|
||||
/**
|
||||
* A sleep that another thread can end early. {@link #wake()} ends the current
|
||||
* {@link #sleep(long)}, or the next one if no thread sleeps at the moment, so a wake between two
|
||||
* sleeps is not lost. Unlike {@code Thread.interrupt()}, waking does not close NIO channels the
|
||||
* sleeping thread uses.
|
||||
*/
|
||||
public final class WakeableSleep
|
||||
{
|
||||
private final Object lock = new Object();
|
||||
private boolean woken;
|
||||
|
||||
/** Sleeps {@code millis} milliseconds, or until {@link #wake()}; nothing for {@code millis <= 0}, as {@code Thread.sleep}. */
|
||||
public void sleep(long millis) throws InterruptedException
|
||||
{
|
||||
if (millis <= 0) return;
|
||||
|
||||
long deadline = System.nanoTime() + millis * 1_000_000L;
|
||||
synchronized (this.lock)
|
||||
{
|
||||
// wait() may return without a notify; the loop keeps the full time unless woken.
|
||||
long remaining = millis;
|
||||
while (!this.woken && remaining > 0)
|
||||
{
|
||||
this.lock.wait(remaining);
|
||||
remaining = (deadline - System.nanoTime()) / 1_000_000L;
|
||||
}
|
||||
this.woken = false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Ends the current or the next {@link #sleep(long)}. */
|
||||
public void wake()
|
||||
{
|
||||
synchronized (this.lock)
|
||||
{
|
||||
this.woken = true;
|
||||
this.lock.notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class WakeableSleepTest
|
||||
{
|
||||
private static long millisOf(Runnable sleep)
|
||||
{
|
||||
long start = System.nanoTime();
|
||||
sleep.run();
|
||||
return (System.nanoTime() - start) / 1_000_000L;
|
||||
}
|
||||
|
||||
private static Runnable sleeping(WakeableSleep sleep, long millis)
|
||||
{
|
||||
return () -> {
|
||||
try { sleep.sleep(millis); }
|
||||
catch (InterruptedException e) { throw new IllegalStateException(e); }
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
void sleepsTheFullTimeWithoutAWake()
|
||||
{
|
||||
long slept = WakeableSleepTest.millisOf(WakeableSleepTest.sleeping(new WakeableSleep(), 200));
|
||||
|
||||
assertTrue(slept >= 195, () -> "Slept only " + slept + " ms.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aWakeEndsTheSleepEarly() throws InterruptedException
|
||||
{
|
||||
WakeableSleep sleep = new WakeableSleep();
|
||||
CountDownLatch started = new CountDownLatch(1);
|
||||
long[] slept = new long[1];
|
||||
|
||||
Thread sleeper = new Thread(() -> {
|
||||
started.countDown();
|
||||
slept[0] = WakeableSleepTest.millisOf(WakeableSleepTest.sleeping(sleep, 10_000));
|
||||
});
|
||||
sleeper.start();
|
||||
started.await();
|
||||
Thread.sleep(50);
|
||||
|
||||
sleep.wake();
|
||||
sleeper.join(5_000);
|
||||
|
||||
assertTrue(slept[0] < 5_000, () -> "Slept " + slept[0] + " ms despite the wake.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aWakeBeforeTheSleepEndsTheNextSleep()
|
||||
{
|
||||
WakeableSleep sleep = new WakeableSleep();
|
||||
sleep.wake();
|
||||
|
||||
long first = WakeableSleepTest.millisOf(WakeableSleepTest.sleeping(sleep, 5_000));
|
||||
long second = WakeableSleepTest.millisOf(WakeableSleepTest.sleeping(sleep, 100));
|
||||
|
||||
assertTrue(first < 1_000, () -> "Slept " + first + " ms despite the earlier wake.");
|
||||
assertTrue(second >= 95, () -> "The wake ended a second sleep too: " + second + " ms.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNotSleepForZeroOrLess()
|
||||
{
|
||||
assertTrue(WakeableSleepTest.millisOf(WakeableSleepTest.sleeping(new WakeableSleep(), 0)) < 50);
|
||||
assertTrue(WakeableSleepTest.millisOf(WakeableSleepTest.sleeping(new WakeableSleep(), -1)) < 50);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user