-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic deadlock debugging facilities
- Loading branch information
Showing
22 changed files
with
458 additions
and
261 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
c2me-base/src/main/java/com/ishland/c2me/base/common/threadstate/RunningWork.java
This file contains 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,7 @@ | ||
package com.ishland.c2me.base.common.threadstate; | ||
|
||
public interface RunningWork { | ||
|
||
String toString(); | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
c2me-base/src/main/java/com/ishland/c2me/base/common/threadstate/SyncLoadWork.java
This file contains 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,14 @@ | ||
package com.ishland.c2me.base.common.threadstate; | ||
|
||
import net.minecraft.server.world.ServerWorld; | ||
import net.minecraft.util.math.ChunkPos; | ||
import net.minecraft.world.chunk.ChunkStatus; | ||
|
||
public record SyncLoadWork(ServerWorld world, ChunkPos chunkPos, ChunkStatus targetStatus, boolean create) implements RunningWork { | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("Sync load chunk %s to status %s in world %s (create=%s)", chunkPos, targetStatus, world.getRegistryKey().getValue(), create); | ||
} | ||
|
||
} |
80 changes: 80 additions & 0 deletions
80
c2me-base/src/main/java/com/ishland/c2me/base/common/threadstate/ThreadInstrumentation.java
This file contains 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,80 @@ | ||
package com.ishland.c2me.base.common.threadstate; | ||
|
||
import com.google.common.util.concurrent.ThreadFactoryBuilder; | ||
|
||
import java.lang.management.ThreadInfo; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class ThreadInstrumentation { | ||
|
||
private static final ScheduledExecutorService CLEANER = Executors.newSingleThreadScheduledExecutor( | ||
new ThreadFactoryBuilder() | ||
.setNameFormat("ThreadStateHolderCleaner") | ||
.setDaemon(true) | ||
.build() | ||
); | ||
|
||
private static final ConcurrentHashMap<Thread, ThreadState> threadStateMap = new ConcurrentHashMap<>(); | ||
|
||
private static final ThreadLocal<ThreadState> threadStateThreadLocal = ThreadLocal.withInitial(() -> getOrCreate(Thread.currentThread())); | ||
|
||
static { | ||
CLEANER.scheduleAtFixedRate( | ||
() -> threadStateMap.entrySet().removeIf(entry -> !entry.getKey().isAlive()), | ||
30, | ||
30, | ||
TimeUnit.SECONDS | ||
); | ||
} | ||
|
||
public static ThreadState getOrCreate(Thread thread) { | ||
return threadStateMap.computeIfAbsent(thread, unused -> new ThreadState()); | ||
} | ||
|
||
public static ThreadState get(Thread thread) { | ||
return threadStateMap.get(thread); | ||
} | ||
|
||
public static ThreadState getCurrent() { | ||
return threadStateThreadLocal.get(); | ||
} | ||
|
||
public static String printState(ThreadInfo threadInfo) { | ||
return printState(threadInfo.getThreadName(), threadInfo.getThreadId(), findFromTid(threadInfo.getThreadId())); | ||
} | ||
|
||
public static Set<Map.Entry<Thread, ThreadState>> entrySet() { | ||
return Collections.unmodifiableSet(threadStateMap.entrySet()); | ||
} | ||
|
||
public static String printState(String name, long tid, ThreadState state) { | ||
if (state != null) { | ||
RunningWork[] runningWorks = state.toArray(); | ||
if (runningWorks.length != 0) { | ||
StringBuilder builder = new StringBuilder(); | ||
builder.append("Task trace for thread \"").append(name).append("\" Id=").append(tid).append(" (obtained on a best-effort basis)\n"); | ||
for (RunningWork runningWork : runningWorks) { | ||
builder.append(runningWork.toString().indent(4)).append("\n"); | ||
} | ||
return builder.toString(); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private static ThreadState findFromTid(long tid) { | ||
for (Map.Entry<Thread, ThreadState> entry : threadStateMap.entrySet()) { | ||
if (entry.getKey().threadId() == tid) { | ||
return entry.getValue(); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
c2me-base/src/main/java/com/ishland/c2me/base/common/threadstate/ThreadState.java
This file contains 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,62 @@ | ||
package com.ishland.c2me.base.common.threadstate; | ||
|
||
import java.io.Closeable; | ||
import java.util.ArrayDeque; | ||
import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
|
||
public class ThreadState { | ||
|
||
private final ArrayDeque<RunningWork> runningWorks = new ArrayDeque<>(); | ||
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); | ||
|
||
public void push(RunningWork runningWork) { | ||
lock.writeLock().lock(); | ||
try { | ||
runningWorks.push(runningWork); | ||
} finally { | ||
lock.writeLock().unlock(); | ||
} | ||
} | ||
|
||
public void pop(RunningWork runningWork) { | ||
lock.writeLock().lock(); | ||
try { | ||
RunningWork popped = runningWorks.peek(); | ||
if (popped != runningWork) { | ||
IllegalArgumentException exception = new IllegalArgumentException("Corrupt ThreadState"); | ||
exception.printStackTrace(); | ||
throw exception; | ||
} | ||
runningWorks.pop(); | ||
} finally { | ||
lock.writeLock().unlock(); | ||
} | ||
} | ||
|
||
public WorkClosable begin(RunningWork runningWork) { | ||
lock.writeLock().lock(); | ||
try { | ||
runningWorks.push(runningWork); | ||
return new WorkClosable(this, runningWork); | ||
} finally { | ||
lock.writeLock().unlock(); | ||
} | ||
} | ||
|
||
public RunningWork[] toArray() { | ||
lock.readLock().lock(); | ||
try { | ||
return runningWorks.toArray(RunningWork[]::new); | ||
} finally { | ||
lock.readLock().unlock(); | ||
} | ||
} | ||
|
||
public static record WorkClosable(ThreadState state, RunningWork work) implements Closeable { | ||
@Override | ||
public void close() { | ||
state.pop(work); | ||
} | ||
} | ||
|
||
} |
This file contains 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
60 changes: 60 additions & 0 deletions
60
c2me-base/src/main/java/com/ishland/c2me/base/mixin/report/MixinDedicatedServerWatchdog.java
This file contains 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,60 @@ | ||
package com.ishland.c2me.base.mixin.report; | ||
|
||
import com.ishland.c2me.base.common.threadstate.ThreadInstrumentation; | ||
import com.ishland.c2me.base.common.threadstate.ThreadState; | ||
import com.llamalad7.mixinextras.sugar.Local; | ||
import net.minecraft.server.dedicated.DedicatedServerWatchdog; | ||
import net.minecraft.util.crash.CrashReport; | ||
import net.minecraft.util.crash.CrashReportSection; | ||
import org.slf4j.Logger; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
import org.spongepowered.asm.mixin.injection.callback.LocalCapture; | ||
|
||
import java.lang.management.ThreadInfo; | ||
import java.lang.management.ThreadMXBean; | ||
import java.util.Map; | ||
|
||
@Mixin(DedicatedServerWatchdog.class) | ||
public class MixinDedicatedServerWatchdog { | ||
|
||
@Shadow @Final private static Logger LOGGER; | ||
|
||
@Inject(method = "createCrashReport", at = @At(value = "INVOKE", target = "Ljava/lang/management/ThreadInfo;getThreadId()J")) | ||
private static void prependThreadInstrumentation(String message, long threadId, CallbackInfoReturnable<CrashReport> cir, @Local StringBuilder stringBuilder, @Local ThreadInfo threadInfo) { | ||
String state = null; | ||
try { | ||
state = ThreadInstrumentation.printState(threadInfo); | ||
} catch (Throwable t) { | ||
LOGGER.error("Failed to fetch state for thread {}", threadInfo); | ||
} | ||
if (state != null) { | ||
stringBuilder.append(state); | ||
} | ||
} | ||
|
||
@Inject(method = "createCrashReport", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/crash/CrashReport;addElement(Ljava/lang/String;)Lnet/minecraft/util/crash/CrashReportSection;", ordinal = 0), locals = LocalCapture.CAPTURE_FAILHARD) | ||
private static void addInstrumentationData(String message, long threadId, CallbackInfoReturnable<CrashReport> cir, ThreadMXBean threadMXBean, ThreadInfo[] threadInfos, StringBuilder stringBuilder, Error error, CrashReport crashReport) { | ||
CrashReportSection section = crashReport.addElement("Thread trace dump (obtained on a best-effort basis)"); | ||
try { | ||
for (Map.Entry<Thread, ThreadState> entry : ThreadInstrumentation.entrySet()) { | ||
try { | ||
Thread thread = entry.getKey(); | ||
String state = ThreadInstrumentation.printState(thread.getName(), thread.threadId(), entry.getValue()); | ||
if (state != null) { | ||
section.add(thread.getName(), state); | ||
} | ||
} catch (Throwable t) { | ||
LOGGER.error("Failed to dumping state for thread {}", entry.getKey(), t); | ||
} | ||
} | ||
} catch (Throwable t) { | ||
LOGGER.error("Failed to dump all known thread states", t); | ||
} | ||
} | ||
|
||
} |
This file contains 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 was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
c2me-opts-chunk-access/src/main/java/com/ishland/c2me/opts/chunk_access/MixinPlugin.java
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
...-opts-chunk-access/src/main/java/com/ishland/c2me/opts/chunk_access/ModuleEntryPoint.java
This file was deleted.
Oops, something went wrong.
21 changes: 0 additions & 21 deletions
21
...-access/src/main/java/com/ishland/c2me/opts/chunk_access/common/CurrentWorldGenState.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.