Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental compressed memory cache #386

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

public interface AdvancedConfig {

int maxMillisecondsPerTick();

int obfuscationWorkerThreads();
int obfuscationThreads();

boolean hasObfuscationTimeout();

int obfuscationTimeout();
long obfuscationTimeout();

int maxMillisecondsPerTick();

int proximityHiderThreads();
int proximityThreads();

int proximityDefaultBucketSize();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ public interface CacheConfig {

boolean enabled();

int maximumSize();

long expireAfterAccess();

boolean enableDiskCache();

Path baseDirectory();

Path regionFile(ChunkPosition chunkPosition);

int maximumOpenRegionFiles();

long deleteRegionFilesAfterAccess();

boolean enableDiskCache();

int maximumSize();

long expireAfterAccess();

int maximumTaskQueueSize();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ private static int javaMajorVersion() {

/**
* taken from:
* https://github.com/netty/netty/blob/4.1/common/src/main/java/io/netty/util/internal/PlatformDependent0.java#L1011
* https://github.com/netty/netty/blob/7ad2b91515b3affaeadb4b2975cd6d2a8342c403/common/src/main/java/io/netty/util/internal/PlatformDependent0.java#L1037
*/
private static int majorVersion(final String javaSpecVersion) {
final String[] components = javaSpecVersion.split("\\.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static Builder builder() {
private final int weight;

/**
* Flat int array where each entry has a value of one
* Flat int array where each entry has a probability of one
*
* @implNote use flat int array instead of treemap cause O(N*log(N)) is too
* slow since this is the second hottest code path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RegionFileCache extends AbstractRegionFileCache<RegionFile> {
@Override
protected RegionFile createRegionFile(Path path) throws IOException {
boolean isSyncChunkWrites = ((CraftServer) Bukkit.getServer()).getServer().isSyncChunkWrites();
return new RegionFile(path, path.getParent(), RegionFileCompression.b, isSyncChunkWrites);
return new RegionFile(path, path.getParent(), RegionFileCompression.c, isSyncChunkWrites);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RegionFileCache extends AbstractRegionFileCache<RegionFile> {
@Override
protected RegionFile createRegionFile(Path path) throws IOException {
boolean isSyncChunkWrites = ((CraftServer) Bukkit.getServer()).getServer().isSyncChunkWrites();
return new RegionFile(path, path.getParent(), RegionFileCompression.b, isSyncChunkWrites);
return new RegionFile(path, path.getParent(), RegionFileCompression.c, isSyncChunkWrites);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RegionFileCache extends AbstractRegionFileCache<RegionFile> {
@Override
protected RegionFile createRegionFile(Path path) throws IOException {
boolean isSyncChunkWrites = ((CraftServer) Bukkit.getServer()).getServer().isSyncChunkWrites();
return new RegionFile(path, path.getParent(), RegionFileCompression.b, isSyncChunkWrites);
return new RegionFile(path, path.getParent(), RegionFileCompression.c, isSyncChunkWrites);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RegionFileCache extends AbstractRegionFileCache<RegionFile> {
@Override
protected RegionFile createRegionFile(Path path) throws IOException {
boolean isSyncChunkWrites = ((CraftServer) Bukkit.getServer()).getServer().forceSynchronousWrites();
return new RegionFile(path, path.getParent(), RegionFileVersion.VERSION_DEFLATE, isSyncChunkWrites);
return new RegionFile(path, path.getParent(), RegionFileVersion.VERSION_NONE, isSyncChunkWrites);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RegionFileCache extends AbstractRegionFileCache<RegionFile> {
@Override
protected RegionFile createRegionFile(Path path) throws IOException {
boolean isSyncChunkWrites = ((CraftServer) Bukkit.getServer()).getServer().forceSynchronousWrites();
return new RegionFile(path, path.getParent(), RegionFileVersion.VERSION_DEFLATE, isSyncChunkWrites);
return new RegionFile(path, path.getParent(), RegionFileVersion.VERSION_NONE, isSyncChunkWrites);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RegionFileCache extends AbstractRegionFileCache<RegionFile> {
@Override
protected RegionFile createRegionFile(Path path) throws IOException {
boolean isSyncChunkWrites = ((CraftServer) Bukkit.getServer()).getServer().forceSynchronousWrites();
return new RegionFile(path, path.getParent(), RegionFileVersion.VERSION_DEFLATE, isSyncChunkWrites);
return new RegionFile(path, path.getParent(), RegionFileVersion.VERSION_NONE, isSyncChunkWrites);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RegionFileCache extends AbstractRegionFileCache<RegionFile> {
@Override
protected RegionFile createRegionFile(Path path) throws IOException {
boolean isSyncChunkWrites = ((CraftServer) Bukkit.getServer()).getServer().forceSynchronousWrites();
return new RegionFile(path, path.getParent(), RegionFileVersion.VERSION_DEFLATE, isSyncChunkWrites);
return new RegionFile(path, path.getParent(), RegionFileVersion.VERSION_NONE, isSyncChunkWrites);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RegionFileCache extends AbstractRegionFileCache<RegionFile> {
@Override
protected RegionFile createRegionFile(Path path) throws IOException {
boolean isSyncChunkWrites = ((CraftServer) Bukkit.getServer()).getServer().forceSynchronousWrites();
return new RegionFile(path, path.getParent(), RegionFileVersion.VERSION_DEFLATE, isSyncChunkWrites);
return new RegionFile(path, path.getParent(), RegionFileVersion.VERSION_NONE, isSyncChunkWrites);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RegionFileCache extends AbstractRegionFileCache<RegionFile> {
@Override
protected RegionFile createRegionFile(Path path) throws IOException {
boolean isSyncChunkWrites = ((CraftServer) Bukkit.getServer()).getServer().forceSynchronousWrites();
return new RegionFile(path, path.getParent(), RegionFileVersion.VERSION_DEFLATE, isSyncChunkWrites);
return new RegionFile(path, path.getParent(), RegionFileVersion.VERSION_NONE, isSyncChunkWrites);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RegionFileCache extends AbstractRegionFileCache<RegionFile> {
@Override
protected RegionFile createRegionFile(Path path) throws IOException {
boolean isSyncChunkWrites = ((CraftServer) Bukkit.getServer()).getServer().forceSynchronousWrites();
return new RegionFile(path, path.getParent(), RegionFileVersion.VERSION_DEFLATE, isSyncChunkWrites);
return new RegionFile(path, path.getParent(), RegionFileVersion.VERSION_NONE, isSyncChunkWrites);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RegionFileCache extends AbstractRegionFileCache<RegionFile> {
@Override
protected RegionFile createRegionFile(Path path) throws IOException {
boolean isSyncChunkWrites = ((CraftServer) Bukkit.getServer()).getServer().forceSynchronousWrites();
return new RegionFile(path, path.getParent(), RegionFileVersion.VERSION_DEFLATE, isSyncChunkWrites);
return new RegionFile(path, path.getParent(), RegionFileVersion.VERSION_NONE, isSyncChunkWrites);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RegionFileCache extends AbstractRegionFileCache<RegionFile> {
@Override
protected RegionFile createRegionFile(Path path) throws IOException {
boolean isSyncChunkWrites = ((CraftServer) Bukkit.getServer()).getServer().forceSynchronousWrites();
return new RegionFile(path, path.getParent(), RegionFileVersion.VERSION_DEFLATE, isSyncChunkWrites);
return new RegionFile(path, path.getParent(), RegionFileVersion.VERSION_NONE, isSyncChunkWrites);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RegionFileCache extends AbstractRegionFileCache<RegionFile> {
@Override
protected RegionFile createRegionFile(Path path) throws IOException {
boolean isSyncChunkWrites = ((CraftServer) Bukkit.getServer()).getServer().forceSynchronousWrites();
return new RegionFile(null, path, path.getParent(), RegionFileVersion.VERSION_DEFLATE, isSyncChunkWrites);
return new RegionFile(null, path, path.getParent(), RegionFileVersion.VERSION_NONE, isSyncChunkWrites);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RegionFileCache extends AbstractRegionFileCache<RegionFile> {
@Override
protected RegionFile createRegionFile(Path path) throws IOException {
boolean isSyncChunkWrites = ((CraftServer) Bukkit.getServer()).getServer().forceSynchronousWrites();
return new RegionFile(null, path, path.getParent(), RegionFileVersion.VERSION_DEFLATE, isSyncChunkWrites);
return new RegionFile(null, path, path.getParent(), RegionFileVersion.VERSION_NONE, isSyncChunkWrites);
}

@Override
Expand Down
16 changes: 16 additions & 0 deletions orebfuscator-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
<configuration>
<minimizeJar>true</minimizeJar>
<filters>
<filter>
<artifact>*:lz4-java</artifact>
<includes>
<include>**</include>
</includes>
</filter>
<filter>
<artifact>*:*</artifact>
<excludeDefaults>false</excludeDefaults>
Expand All @@ -43,6 +49,10 @@
<pattern>org.joml</pattern>
<shadedPattern>net.imprex.shaded.org.joml</shadedPattern>
</relocation>
<relocation>
<pattern>net.jpountz</pattern>
<shadedPattern>net.imprex.shaded.net.jpountz</shadedPattern>
</relocation>
</relocations>
</configuration>
</plugin>
Expand Down Expand Up @@ -85,6 +95,12 @@
<version>${dependency.bstats.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.lz4</groupId>
<artifactId>lz4-java</artifactId>
<version>${dependency.lz4.version}</version>
<scope>compile</scope>
</dependency>

<!-- Orebfuscator -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class Orebfuscator extends JavaPlugin implements Listener {

private final Thread mainThread = Thread.currentThread();

private OrebfuscatorStatistics statistics;
private OrebfuscatorConfig config;
private OrebfuscatorPlayerMap playerMap;
private UpdateSystem updateSystem;
Expand Down Expand Up @@ -54,6 +55,8 @@ public void onEnable() {
throw new RuntimeException("ProtocolLib can't be found or is disabled! Orebfuscator can't be enabled.");
}

this.statistics = new OrebfuscatorStatistics();

// Load configurations
this.config = new OrebfuscatorConfig(this);

Expand Down Expand Up @@ -138,6 +141,10 @@ public boolean isGameThread() {
return Thread.currentThread() == this.mainThread;
}

public OrebfuscatorStatistics getStatistics() {
return statistics;
}

public OrebfuscatorConfig getOrebfuscatorConfig() {
return this.config;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St

if (args.length == 0) {
sender.sendMessage("You are using " + this.orebfuscator.toString());
sender.sendMessage(this.orebfuscator.getStatistics().toString());
} else if (args[0].equalsIgnoreCase("dump")) {
TemporalAccessor now = OffsetDateTime.now(ZoneOffset.UTC);

Expand All @@ -77,6 +78,8 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
versions.addProperty("orebfuscator", orebfuscator.toString());
root.add("versions", versions);

root.add("statistics", orebfuscator.getStatistics().toJson());

JsonObject plugins = new JsonObject();
for (Plugin bukkitPlugin : Bukkit.getPluginManager().getPlugins()) {
PluginDescriptionFile description = bukkitPlugin.getDescription();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package net.imprex.orebfuscator;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.IntSupplier;

import com.google.gson.JsonObject;

public class OrebfuscatorStatistics {

private static String formatPrecent(double percent) {
return String.format("%.2f%%", percent * 100);
}

private static String formatBytes(long bytes) {
if (bytes > 1073741824L) {
return String.format("%.1f GiB", bytes / 1073741824d);
} else if (bytes > 1048576L) {
return String.format("%.1f MiB", bytes / 1048576d);
} else if (bytes > 1024L) {
return String.format("%.1f KiB", bytes / 1024d);
} else {
return String.format("%d B", bytes);
}
}

private final AtomicLong cacheHitCountMemory = new AtomicLong(0);
private final AtomicLong cacheHitCountDisk = new AtomicLong(0);
private final AtomicLong cacheMissCount = new AtomicLong(0);
private final AtomicLong cacheEstimatedSize = new AtomicLong(0);
private IntSupplier diskCacheQueueLength = () -> 0;
private IntSupplier obfuscationQueueLength = () -> 0;

public void onCacheHitMemory() {
this.cacheHitCountMemory.incrementAndGet();
}

public void onCacheHitDisk() {
this.cacheHitCountDisk.incrementAndGet();
}

public void onCacheMiss() {
this.cacheMissCount.incrementAndGet();
}

public void onCacheSizeChange(int delta) {
this.cacheEstimatedSize.addAndGet(delta);
}

public void setDiskCacheQueueLengthSupplier(IntSupplier supplier) {
this.diskCacheQueueLength = Objects.requireNonNull(supplier);
}

public void setObfuscationQueueLengthSupplier(IntSupplier supplier) {
this.obfuscationQueueLength = Objects.requireNonNull(supplier);
}

@Override
public String toString() {
long cacheHitCountMemory = this.cacheHitCountMemory.get();
long cacheHitCountDisk = this.cacheHitCountDisk.get();
long cacheMissCount = this.cacheMissCount.get();
long cacheEstimatedSize = this.cacheEstimatedSize.get();
long diskCacheQueueLength = this.diskCacheQueueLength.getAsInt();
long obfuscationQueueLength = this.obfuscationQueueLength.getAsInt();

double totalCacheRequest = (double) (cacheHitCountMemory + cacheHitCountDisk + cacheMissCount);
double memoryCacheHitRate = (double) cacheHitCountMemory / totalCacheRequest;
Ingrim4 marked this conversation as resolved.
Show resolved Hide resolved
double diskCacheHitRate = (double) cacheHitCountDisk / totalCacheRequest;

StringBuilder builder = new StringBuilder("Here are some useful statistics:\n");

builder.append(" - memoryCacheHitRate: ").append(formatPrecent(memoryCacheHitRate)).append('\n');
builder.append(" - diskCacheHitRate: ").append(formatPrecent(diskCacheHitRate)).append('\n');
builder.append(" - memoryCacheEstimatedSize: ").append(formatBytes(cacheEstimatedSize)).append('\n');
builder.append(" - diskCacheQueueLength: ").append(diskCacheQueueLength).append('\n');
builder.append(" - obfuscationQueueLength: ").append(obfuscationQueueLength).append('\n');

return builder.toString();
}

public JsonObject toJson() {
JsonObject object = new JsonObject();

object.addProperty("cacheHitCountMemory", this.cacheHitCountMemory.get());
object.addProperty("cacheHitCountDisk", this.cacheHitCountDisk.get());
object.addProperty("cacheMissCount", this.cacheMissCount.get());
object.addProperty("cacheEstimatedSize", this.cacheEstimatedSize.get());
object.addProperty("diskCacheQueueLength", this.diskCacheQueueLength.getAsInt());
object.addProperty("obfuscationQueueLength", this.obfuscationQueueLength.getAsInt());

return object;
}
}
Loading