3
0
Mirror von https://github.com/PaperMC/Paper.git synchronisiert 2024-12-15 19:10:09 +01:00
Paper/Spigot-Server-Patches/0500-Cleanup-Region-Files-Direct-Memory-on-close.patch
Aikar f37381ea8a
Optimize Network Manager to not need synchronization
Removes synchronization from sending packets
Makes normal packet sends no longer need to be wrapped and queued like it use to work.
Adds more packet queue immunities on top of keep alive to let the following scenarios go out
without delay:
  - Keep Alive
  - Chat
  - Kick
  - All of the packets during the Player Joined World event

Hoping that latter one helps join timeout issues more too for slow connections.

Removes processing packet queue off of main thread
  - for the few cases where it is allowed, order is not necessary nor
    should it even be happening concurrently in first place (handshaking/login/status)

Ensures packets sent asynchronously are dispatched on main thread

This helps ensure safety for ProtocolLib as packet listeners
are commonly accessing world state. This will allow you to schedule
a packet to be sent async, but itll be dispatched sync for packet
listeners to process.

This should solve some deadlock risks

This may provide a decent performance improvement because thread synchronization incurs a cache reset
so by avoiding ever entering a synchronized block, we get to avoid that, and packet sending is a really
hot activity.
2020-05-06 05:28:47 -04:00

88 Zeilen
3.6 KiB
Diff

From cee647e81a3c64791e2e231fbadf7b4da897b1c3 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Mon, 4 May 2020 00:38:13 -0400
Subject: [PATCH] Cleanup Region Files Direct Memory on close
Mojang was semi leaking native memory here by relying on finalizers
to clean up the direct memory.
Finalizers have no guarantee on when they will be ran, and since this is
old generation memory, it might be a while before its called.
diff --git a/src/main/java/net/minecraft/server/RegionFile.java b/src/main/java/net/minecraft/server/RegionFile.java
index df728e2c0a2..20927d55c67 100644
--- a/src/main/java/net/minecraft/server/RegionFile.java
+++ b/src/main/java/net/minecraft/server/RegionFile.java
@@ -30,7 +30,7 @@ public class RegionFile implements AutoCloseable {
private final FileChannel dataFile;
private final java.nio.file.Path d;
private final RegionFileCompression e;
- private final ByteBuffer f;
+ private final ByteBuffer f; private ByteBuffer getFileBuffer() { return f; } // Paper - clean up direct buffers on close
private final IntBuffer g;
private final IntBuffer h;
private final RegionFileBitSet freeSectors;
@@ -403,10 +403,59 @@ public class RegionFile implements AutoCloseable {
}
} finally { // Paper start - Prevent regionfiles from being closed during use
this.fileLock.unlock();
+ if (getFileBuffer().isDirect()) cleanDirectByteBuffer(getFileBuffer()); // Paper - clean up direct buffers on close
}
} // Paper end
}
+ // Paper start
+ private static int getVersion() {
+ String version = System.getProperty("java.version");
+ if(version.startsWith("1.")) {
+ version = version.substring(2, 3);
+ } else {
+ int dot = version.indexOf(".");
+ if(dot != -1) { version = version.substring(0, dot); }
+ } return Integer.parseInt(version);
+ }
+ static java.lang.reflect.Method unsafeClean;
+ static sun.misc.Unsafe unsafe = com.destroystokyo.paper.utils.UnsafeUtils.getUnsafe();
+ static java.util.function.Consumer<ByteBuffer> cleaner;
+ static {
+ try {
+ if (unsafe != null) {
+ unsafeClean = unsafe.getClass().getMethod("invokeCleaner", ByteBuffer.class);
+ if (unsafeClean != null) {
+ cleaner = (buf) -> {
+ try {
+ unsafeClean.invoke(unsafe, buf);
+ } catch (Exception ex) {
+ com.destroystokyo.paper.util.SneakyThrow.sneaky(ex);
+ }
+ };
+ LOGGER.info("[RegionFile] Using Java 9+ invokeCleaner DirectByteBuffer cleanup method");
+ }
+ }
+ } catch (java.lang.NoSuchMethodException e) {}
+ if (cleaner == null && getVersion() <= 8) {
+ cleaner = (buf) -> {
+ ((sun.nio.ch.DirectBuffer) buf).cleaner().clean();
+ };
+ LOGGER.info("[RegionFile] Using Java 8 DirectByteBuffer cleanup method");
+ }
+ }
+ public static void cleanDirectByteBuffer(ByteBuffer toBeDestroyed) {
+ try {
+ if (cleaner != null) {
+ cleaner.accept(toBeDestroyed);
+ }
+ } catch (Exception ex) {
+ LOGGER.warn("Failed automatically cleaning DirectByteBuffer");
+ ex.printStackTrace();
+ cleaner = null;
+ }
+ }
+ // Paper end
private void c() throws IOException {
int i = (int) this.dataFile.size();
--
2.26.2