Implement MultiThreading in the TectonicPlates unloader

This commit is contained in:
CrazyDev22
2023-12-21 22:30:08 +01:00
parent 85e8ffeaa3
commit 650d38e212
3 changed files with 39 additions and 19 deletions
@@ -5,6 +5,8 @@ import com.volmit.iris.core.tools.IrisToolbelt;
import com.volmit.iris.engine.framework.Engine; import com.volmit.iris.engine.framework.Engine;
import com.volmit.iris.engine.platform.PlatformChunkGenerator; import com.volmit.iris.engine.platform.PlatformChunkGenerator;
import com.volmit.iris.util.collection.KMap; 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.misc.getHardware;
import com.volmit.iris.util.plugin.IrisService; import com.volmit.iris.util.plugin.IrisService;
import com.volmit.iris.util.scheduling.Looper; import com.volmit.iris.util.scheduling.Looper;
@@ -93,7 +95,11 @@ public class IrisEngineSVC implements IrisService {
try { try {
Engine engine = supplier.get(); Engine engine = supplier.get();
if (engine != null) { 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) { } catch (Throwable e) {
Iris.reportError(e); Iris.reportError(e);
@@ -178,8 +178,8 @@ public interface EngineMantle extends IObjectPlacer {
default void trim(int limit) { default void trim(int limit) {
getMantle().trim(TimeUnit.SECONDS.toMillis(IrisSettings.get().getPerformance().getMantleKeepAlive()), limit); getMantle().trim(TimeUnit.SECONDS.toMillis(IrisSettings.get().getPerformance().getMantleKeepAlive()), limit);
} }
default void unloadTectonicPlate(){ default int unloadTectonicPlate(){
getMantle().unloadTectonicPlate(); return getMantle().unloadTectonicPlate();
} }
default MultiBurst burst() { default MultiBurst burst() {
@@ -399,7 +399,7 @@ public class Mantle {
@Getter @Getter
private final AtomicLong oldestTectonicPlate = new AtomicLong(0); private final AtomicLong oldestTectonicPlate = new AtomicLong(0);
@Getter @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 * 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 { try {
List<Future<?>> futures = new ArrayList<>();
ExecutorService service = Executors.newFixedThreadPool(dynamicThreads.get());
for (Long id : new ArrayList<>(toUnload)) { for (Long id : new ArrayList<>(toUnload)) {
hyperLock.withLong(id, () -> { hyperLock.withLong(id, () ->
TectonicPlate m = loadedRegions.get(id); futures.add(service.submit(() -> {
if (m != null) { TectonicPlate m = loadedRegions.get(id);
try { if (m != null) {
m.write(fileForRegion(dataFolder, id)); try {
loadedRegions.remove(id); m.write(fileForRegion(dataFolder, id));
lastUse.remove(id); loadedRegions.remove(id);
toUnload.remove(id); lastUse.remove(id);
Iris.info("Unloaded Tectonic Plate " + C.DARK_GREEN + Cache.keyX(id) + " " + Cache.keyZ(id)); toUnload.remove(id);
} catch (IOException e) { i.incrementAndGet();
e.printStackTrace(); 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) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally {
this.toUnload.addAll(toUnload);
} }
ioTectonicUnload.set(true); ioTectonicUnload.set(true);
return i.get();
} }