mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-01-04 15:41:30 +00:00
Implement MultiThreading in the TectonicPlates unloader
This commit is contained in:
@@ -5,6 +5,8 @@ import com.volmit.iris.core.tools.IrisToolbelt;
|
||||
import com.volmit.iris.engine.framework.Engine;
|
||||
import com.volmit.iris.engine.platform.PlatformChunkGenerator;
|
||||
import com.volmit.iris.util.collection.KMap;
|
||||
import com.volmit.iris.util.format.C;
|
||||
import com.volmit.iris.util.format.Form;
|
||||
import com.volmit.iris.util.misc.getHardware;
|
||||
import com.volmit.iris.util.plugin.IrisService;
|
||||
import com.volmit.iris.util.scheduling.Looper;
|
||||
@@ -93,7 +95,11 @@ public class IrisEngineSVC implements IrisService {
|
||||
try {
|
||||
Engine engine = supplier.get();
|
||||
if (engine != null) {
|
||||
engine.getMantle().unloadTectonicPlate();
|
||||
long unloadStart = System.currentTimeMillis();
|
||||
int count = engine.getMantle().unloadTectonicPlate();
|
||||
if (count > 0) {
|
||||
Iris.info("Unloaded TectonicPlates in " + C.RED + Form.duration(System.currentTimeMillis() - unloadStart, 2));
|
||||
}
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
Iris.reportError(e);
|
||||
|
||||
@@ -178,8 +178,8 @@ public interface EngineMantle extends IObjectPlacer {
|
||||
default void trim(int limit) {
|
||||
getMantle().trim(TimeUnit.SECONDS.toMillis(IrisSettings.get().getPerformance().getMantleKeepAlive()), limit);
|
||||
}
|
||||
default void unloadTectonicPlate(){
|
||||
getMantle().unloadTectonicPlate();
|
||||
default int unloadTectonicPlate(){
|
||||
return getMantle().unloadTectonicPlate();
|
||||
}
|
||||
|
||||
default MultiBurst burst() {
|
||||
|
||||
@@ -399,7 +399,7 @@ public class Mantle {
|
||||
@Getter
|
||||
private final AtomicLong oldestTectonicPlate = new AtomicLong(0);
|
||||
@Getter
|
||||
public final Set<Long> toUnload = new HashSet<>();
|
||||
private Set<Long> toUnload = new HashSet<>();
|
||||
|
||||
/**
|
||||
* Save & unload regions that have not been used for more than the
|
||||
@@ -443,28 +443,42 @@ public class Mantle {
|
||||
}
|
||||
}
|
||||
|
||||
public void unloadTectonicPlate() {
|
||||
public int unloadTectonicPlate() {
|
||||
AtomicInteger i = new AtomicInteger();
|
||||
Set<Long> toUnload = this.toUnload;
|
||||
this.toUnload = new HashSet<>();
|
||||
try {
|
||||
List<Future<?>> futures = new ArrayList<>();
|
||||
ExecutorService service = Executors.newFixedThreadPool(dynamicThreads.get());
|
||||
for (Long id : new ArrayList<>(toUnload)) {
|
||||
hyperLock.withLong(id, () -> {
|
||||
TectonicPlate m = loadedRegions.get(id);
|
||||
if (m != null) {
|
||||
try {
|
||||
m.write(fileForRegion(dataFolder, id));
|
||||
loadedRegions.remove(id);
|
||||
lastUse.remove(id);
|
||||
toUnload.remove(id);
|
||||
Iris.info("Unloaded Tectonic Plate " + C.DARK_GREEN + Cache.keyX(id) + " " + Cache.keyZ(id));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
hyperLock.withLong(id, () ->
|
||||
futures.add(service.submit(() -> {
|
||||
TectonicPlate m = loadedRegions.get(id);
|
||||
if (m != null) {
|
||||
try {
|
||||
m.write(fileForRegion(dataFolder, id));
|
||||
loadedRegions.remove(id);
|
||||
lastUse.remove(id);
|
||||
toUnload.remove(id);
|
||||
i.incrementAndGet();
|
||||
Iris.info("Unloaded Tectonic Plate " + C.DARK_GREEN + Cache.keyX(id) + " " + Cache.keyZ(id));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
})));
|
||||
}
|
||||
while (!futures.isEmpty()) {
|
||||
futures.remove(0).get();
|
||||
}
|
||||
service.shutdown();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
this.toUnload.addAll(toUnload);
|
||||
}
|
||||
ioTectonicUnload.set(true);
|
||||
return i.get();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user