mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2025-12-27 19:19:07 +00:00
Auto stash before revert of "Drop it "
This commit is contained in:
147
src/main/java/com/volmit/iris/util/format/MemoryMonitor.java
Normal file
147
src/main/java/com/volmit/iris/util/format/MemoryMonitor.java
Normal file
@@ -0,0 +1,147 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.util.format;
|
||||
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.util.math.RollingSequence;
|
||||
import com.volmit.iris.util.scheduling.ChronoLatch;
|
||||
import com.volmit.iris.util.scheduling.Looper;
|
||||
|
||||
public class MemoryMonitor {
|
||||
private Looper looper;
|
||||
private long usedMemory;
|
||||
private long garbageMemory;
|
||||
private long garbageLast;
|
||||
private long garbageBin;
|
||||
private long pressure;
|
||||
private ChronoLatch cl;
|
||||
private RollingSequence pressureAvg;
|
||||
private Runtime runtime;
|
||||
|
||||
public MemoryMonitor(int sampleDelay){
|
||||
this.runtime = Runtime.getRuntime();
|
||||
usedMemory = -1;
|
||||
pressureAvg = new RollingSequence(Math.max(Math.min(100, 1000/sampleDelay), 3));
|
||||
garbageBin = 0;
|
||||
garbageMemory = -1;
|
||||
cl = new ChronoLatch(1000);
|
||||
garbageLast = 0;
|
||||
pressure = 0;
|
||||
|
||||
looper = new Looper() {
|
||||
@Override
|
||||
protected long loop() {
|
||||
sample();
|
||||
return sampleDelay;
|
||||
}
|
||||
};
|
||||
looper.setPriority(Thread.MIN_PRIORITY);
|
||||
looper.setName("Memory Monitor");
|
||||
looper.start();
|
||||
}
|
||||
|
||||
public long getGarbageBytes()
|
||||
{
|
||||
return garbageMemory;
|
||||
}
|
||||
|
||||
public long getUsedBytes()
|
||||
{
|
||||
return usedMemory;
|
||||
}
|
||||
|
||||
public long getMaxBytes()
|
||||
{
|
||||
return runtime.maxMemory();
|
||||
}
|
||||
|
||||
public long getPressure()
|
||||
{
|
||||
return (long) pressureAvg.getAverage();
|
||||
}
|
||||
|
||||
public double getUsagePercent()
|
||||
{
|
||||
return usedMemory / (double)getMaxBytes();
|
||||
}
|
||||
|
||||
private void sample() {
|
||||
long used = getVMUse();
|
||||
if(usedMemory == -1)
|
||||
{
|
||||
usedMemory = used;
|
||||
garbageMemory = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if(used < usedMemory)
|
||||
{
|
||||
usedMemory = used;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
garbageMemory = used - usedMemory;
|
||||
}
|
||||
|
||||
long g = garbageMemory - garbageLast;
|
||||
|
||||
if(g >= 0)
|
||||
{
|
||||
garbageBin+= g;
|
||||
garbageLast = garbageMemory;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
garbageMemory = 0;
|
||||
garbageLast = 0;
|
||||
}
|
||||
|
||||
if(cl.flip())
|
||||
{
|
||||
if(garbageMemory > 0)
|
||||
{
|
||||
pressure = garbageBin;
|
||||
garbageBin = 0;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
pressure = 0;
|
||||
garbageBin = 0;
|
||||
}
|
||||
}
|
||||
|
||||
pressureAvg.put(pressure);
|
||||
}
|
||||
|
||||
private long getVMUse() {
|
||||
return runtime.totalMemory() - runtime.freeMemory();
|
||||
}
|
||||
|
||||
public void close()
|
||||
{
|
||||
if(looper != null)
|
||||
{
|
||||
looper.interrupt();
|
||||
looper = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,9 @@ public enum MantleFlag {
|
||||
REAL,
|
||||
CARVED,
|
||||
FLUID_BODIES,
|
||||
INITIAL_SPAWNED_MARKER;
|
||||
INITIAL_SPAWNED_MARKER,
|
||||
CLEANED,
|
||||
PLANNED;
|
||||
|
||||
static StateList getStateList() {
|
||||
return new StateList(MantleFlag.values());
|
||||
|
||||
@@ -152,7 +152,7 @@ public interface MatterSlice<T> extends Hunk<T>, PaletteType<T> {
|
||||
|
||||
default void write(DataOutputStream dos) throws IOException {
|
||||
dos.writeUTF(getType().getCanonicalName());
|
||||
if(IrisSettings.get().getPerformance().isUseExperimentalMantleMemoryCompression() && (this instanceof PaletteOrHunk f && f.isPalette()))
|
||||
if((this instanceof PaletteOrHunk f && f.isPalette()))
|
||||
{
|
||||
PalettedContainer<T> c = f.palette();
|
||||
List<T> palette = new ArrayList<>();
|
||||
@@ -192,7 +192,7 @@ public interface MatterSlice<T> extends Hunk<T>, PaletteType<T> {
|
||||
}
|
||||
|
||||
default void read(DataInputStream din) throws IOException {
|
||||
if(IrisSettings.get().getPerformance().isUseExperimentalMantleMemoryCompression() && (this instanceof PaletteOrHunk f && f.isPalette()))
|
||||
if((this instanceof PaletteOrHunk f && f.isPalette()))
|
||||
{
|
||||
PalettedContainer<T> c = new PalettedContainer<>();
|
||||
List<T> palette = new ArrayList<>();
|
||||
|
||||
@@ -1,99 +0,0 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.util.matter;
|
||||
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.core.IrisSettings;
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
import com.volmit.iris.util.format.Form;
|
||||
import com.volmit.iris.util.io.IO;
|
||||
import com.volmit.iris.util.mantle.Mantle;
|
||||
import com.volmit.iris.util.mantle.TectonicPlate;
|
||||
import com.volmit.iris.util.math.RNG;
|
||||
import com.volmit.iris.util.nbt.tag.ByteArrayTag;
|
||||
import com.volmit.iris.util.scheduling.J;
|
||||
import com.volmit.iris.util.scheduling.PrecisionStopwatch;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class MatterTest {
|
||||
public static long memorySample()
|
||||
{
|
||||
Runtime rt = Runtime.getRuntime();
|
||||
System.gc();
|
||||
System.gc();
|
||||
return rt.totalMemory() - rt.freeMemory();
|
||||
}
|
||||
|
||||
public static void test()
|
||||
{
|
||||
for(Thread i : Thread.getAllStackTraces().keySet())
|
||||
{
|
||||
if(i.getId() != Thread.currentThread().getId())
|
||||
{
|
||||
try {
|
||||
i.wait(10000);
|
||||
} catch (Throwable ignored) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.gc();
|
||||
System.gc();
|
||||
J.sleep(250);
|
||||
|
||||
try {
|
||||
|
||||
double ms = 0;
|
||||
long a = memorySample();
|
||||
PrecisionStopwatch p = PrecisionStopwatch.start();
|
||||
IrisSettings.get().getPerformance().setUseExperimentalMantleMemoryCompression(true);
|
||||
Mantle mantle = new Mantle(new File("mantle-test/legacy"), 256);
|
||||
|
||||
for(int i = 0; i < 512; i++)
|
||||
{
|
||||
for(int j = 0; j < 255; j++)
|
||||
{
|
||||
for(int k = 0; k < 512; k++)
|
||||
{
|
||||
mantle.set(i,j,k,RNG.r.chance(0.5));
|
||||
mantle.set(i,j,k,RNG.r.chance(0.5)?"a" : "b");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ms += p.getMilliseconds();
|
||||
long b = memorySample() - a;
|
||||
Iris.info("Memory: " + Form.memSize(b, 0) + " (" + Form.f(b) + " bytes)");
|
||||
p = PrecisionStopwatch.start();
|
||||
mantle.saveAll();
|
||||
mantle.close();
|
||||
ms+=p.getMilliseconds();
|
||||
Iris.info("Closed, done! took " + Form.duration(ms, 2));
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,12 +18,15 @@
|
||||
|
||||
package com.volmit.iris.util.nbt.mca;
|
||||
|
||||
import com.volmit.iris.core.pregenerator.syndicate.SyndicateServer;
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
import com.volmit.iris.util.math.Position2;
|
||||
import com.volmit.iris.util.nbt.tag.CompoundTag;
|
||||
import com.volmit.iris.util.scheduling.J;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.atomic.AtomicReferenceArray;
|
||||
|
||||
@SuppressWarnings("ALL")
|
||||
@@ -37,6 +40,7 @@ public class MCAFile {
|
||||
private final int regionX;
|
||||
private final int regionZ;
|
||||
private AtomicReferenceArray<Chunk> chunks;
|
||||
private ConcurrentLinkedQueue<Runnable> afterSave;
|
||||
|
||||
/**
|
||||
* MCAFile represents a world save file used by Minecraft to store world
|
||||
@@ -50,6 +54,7 @@ public class MCAFile {
|
||||
public MCAFile(int regionX, int regionZ) {
|
||||
this.regionX = regionX;
|
||||
this.regionZ = regionZ;
|
||||
afterSave = new ConcurrentLinkedQueue<>();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -198,6 +203,11 @@ public class MCAFile {
|
||||
raf.seek(globalOffset * 4096L - 1);
|
||||
raf.write(0);
|
||||
}
|
||||
|
||||
J.a(() -> {
|
||||
afterSave.forEach(i -> i.run());
|
||||
}, 20);
|
||||
|
||||
return chunksWritten;
|
||||
}
|
||||
|
||||
@@ -326,4 +336,8 @@ public class MCAFile {
|
||||
}
|
||||
return chunk.getBlockStateAt(blockX, blockY, blockZ);
|
||||
}
|
||||
|
||||
public void afterSave(Runnable o) {
|
||||
afterSave.add(o);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -329,4 +329,8 @@ public class NBTWorld {
|
||||
public int size() {
|
||||
return loadedRegions.size();
|
||||
}
|
||||
|
||||
public boolean isLoaded(int x, int z) {
|
||||
return loadedRegions.containsKey(Cache.key(x, z));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user