Packet threading debug command!!

(Also packet threading config option)
This commit is contained in:
BallOfEnergy 2024-12-18 10:52:33 -06:00
parent 6ccb585c40
commit 4f06914c39
5 changed files with 123 additions and 20 deletions

View File

@ -0,0 +1,78 @@
package com.hbm.commands;
import com.hbm.config.GeneralConfig;
import com.hbm.handler.radiation.ChunkRadiationManager;
import com.hbm.handler.threading.PacketThreading;
import com.hbm.util.BobMathUtil;
import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import static com.hbm.handler.threading.PacketThreading.processedCnt;
import static com.hbm.handler.threading.PacketThreading.totalCnt;
public class CommandPacketInfo extends CommandBase {
@Override
public String getCommandName() {
return "ntmpackets";
}
@Override
public String getCommandUsage(ICommandSender sender) {
return "/ntmpackets <infoall/threadpool/packets>";
}
@Override
public void processCommand(ICommandSender sender, String[] args) {
if(args.length == 1 && "infoall".equals(args[0])) {
sender.addChatMessage(new ChatComponentText("NTM Packet Debugger"));
if(GeneralConfig.enablePacketThreading)
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.GREEN + "Packet Threading Active"));
else
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "Packet Threading Inactive"));
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.YELLOW + "Thread Pool Info"));
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.YELLOW + "Thread pool size: " + PacketThreading.threadPool.getPoolSize()));
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.YELLOW + "Thread pool queue: " + PacketThreading.threadPool.getQueue().size()));
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.YELLOW + "Packet Info: " + PacketThreading.threadPool.getPoolSize()));
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.YELLOW + "Amount Processed: " + processedCnt));
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.YELLOW + "Amount Remaining: " + totalCnt));
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.YELLOW + "% Processed: " + BobMathUtil.roundDecimal(((double) processedCnt / totalCnt) * 100, 2)));
return;
}
if(args.length == 1 && "threadpool".equals(args[0])) {
sender.addChatMessage(new ChatComponentText("NTM Packet Debugger"));
if(GeneralConfig.enablePacketThreading)
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.GREEN + "Packet Threading Active"));
else
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "Packet Threading Inactive"));
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.YELLOW + "Thread Pool Info"));
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.YELLOW + "Thread pool size: " + PacketThreading.threadPool.getPoolSize()));
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.YELLOW + "Thread pool queue: " + PacketThreading.threadPool.getQueue().size()));
return;
}
if(args.length == 1 && "packets".equals(args[0])) {
sender.addChatMessage(new ChatComponentText("NTM Packet Debugger"));
if(GeneralConfig.enablePacketThreading)
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.GREEN + "Packet Threading Active"));
else
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "Packet Threading Inactive"));
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.YELLOW + "Packet Info: " + PacketThreading.threadPool.getPoolSize()));
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.YELLOW + "Amount Processed: " + processedCnt));
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.YELLOW + "Amount Remaining: " + totalCnt));
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.YELLOW + "% Processed: " + BobMathUtil.roundDecimal(((double) processedCnt / totalCnt) * 100, 2)));
return;
}
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + getCommandUsage(sender)));
}
}

View File

@ -6,6 +6,8 @@ public class GeneralConfig {
public static boolean enableThermosPreventer = true; public static boolean enableThermosPreventer = true;
public static boolean enablePacketThreading = true;
public static boolean enableDebugMode = true; public static boolean enableDebugMode = true;
public static boolean enableMycelium = false; public static boolean enableMycelium = false;
public static boolean enablePlutoniumOre = false; public static boolean enablePlutoniumOre = false;
@ -73,6 +75,9 @@ public class GeneralConfig {
final String CATEGORY_GENERAL = CommonConfig.CATEGORY_GENERAL; final String CATEGORY_GENERAL = CommonConfig.CATEGORY_GENERAL;
enableThermosPreventer = config.get(CATEGORY_GENERAL, "0.00_crashOnThermos", true, "When set to true, will prevent the mod to launch on Thermos servers. Only disable this if you understand what \"tileentities.yml\" is, and how it severely cripples the mod.").getBoolean(true); enableThermosPreventer = config.get(CATEGORY_GENERAL, "0.00_crashOnThermos", true, "When set to true, will prevent the mod to launch on Thermos servers. Only disable this if you understand what \"tileentities.yml\" is, and how it severely cripples the mod.").getBoolean(true);
enablePacketThreading = config.get(CATEGORY_GENERAL, "0.01_enablePacketThreading", true, "Enables creation of a separate thread to increase packet processing speed on servers. Disable this if you are having anomalous crashes related to memory connections.").getBoolean(true);
enableDebugMode = config.get(CATEGORY_GENERAL, "1.00_enableDebugMode", false, "Enable debugging mode").getBoolean(false); enableDebugMode = config.get(CATEGORY_GENERAL, "1.00_enableDebugMode", false, "Enable debugging mode").getBoolean(false);
enableMycelium = config.get(CATEGORY_GENERAL, "1.01_enableMyceliumSpread", false, "Allows glowing mycelium to spread").getBoolean(false); enableMycelium = config.get(CATEGORY_GENERAL, "1.01_enableMyceliumSpread", false, "Allows glowing mycelium to spread").getBoolean(false);
enablePlutoniumOre = config.get(CATEGORY_GENERAL, "1.02_enablePlutoniumNetherOre", false, "Enables plutonium ore generation in the nether").getBoolean(false); enablePlutoniumOre = config.get(CATEGORY_GENERAL, "1.02_enablePlutoniumNetherOre", false, "Enables plutonium ore generation in the nether").getBoolean(false);

View File

@ -1,6 +1,7 @@
package com.hbm.handler.threading; package com.hbm.handler.threading;
import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.hbm.config.GeneralConfig;
import com.hbm.main.MainRegistry; import com.hbm.main.MainRegistry;
import com.hbm.packet.PacketDispatcher; import com.hbm.packet.PacketDispatcher;
import com.hbm.packet.PrecompiledPacket; import com.hbm.packet.PrecompiledPacket;
@ -15,13 +16,21 @@ public class PacketThreading {
private static final ThreadFactory packetThreadFactory = new ThreadFactoryBuilder().setNameFormat("NTM-Packet-Thread-%d").build(); private static final ThreadFactory packetThreadFactory = new ThreadFactoryBuilder().setNameFormat("NTM-Packet-Thread-%d").build();
private static final ThreadPoolExecutor threadPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(1, packetThreadFactory); public static final ThreadPoolExecutor threadPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(1, packetThreadFactory);
private static int totalCnt = 0; public static int totalCnt = 0;
private static int processedCnt = 0; public static int processedCnt = 0;
private static final List<Future<?>> futureList = new ArrayList<>(); public static final List<Future<?>> futureList = new ArrayList<>();
/**
* Sets up thread pool settings during mod initialization.
*/
public static void init() {
threadPool.setKeepAliveTime(50, TimeUnit.MILLISECONDS);
threadPool.allowCoreThreadTimeOut(true);
}
/** /**
* Adds a packet to the thread pool to be processed in the future. This is only compatible with the `sendToAllAround` dispatch operation. * Adds a packet to the thread pool to be processed in the future. This is only compatible with the `sendToAllAround` dispatch operation.
@ -35,12 +44,18 @@ public class PacketThreading {
((PrecompiledPacket) message).getPreBuf(); // Gets the precompiled buffer, doing nothing if it already exists. ((PrecompiledPacket) message).getPreBuf(); // Gets the precompiled buffer, doing nothing if it already exists.
totalCnt++; totalCnt++;
futureList.add(threadPool.submit(() -> {
Runnable task = () -> {
PacketDispatcher.wrapper.sendToAllAround(message, target); PacketDispatcher.wrapper.sendToAllAround(message, target);
if(message instanceof PrecompiledPacket) if(message instanceof PrecompiledPacket)
((PrecompiledPacket) message).getPreBuf().release(); ((PrecompiledPacket) message).getPreBuf().release();
processedCnt++; processedCnt++;
})); };
if(GeneralConfig.enablePacketThreading)
futureList.add(threadPool.submit(task)); // Thread it
else
task.run(); // no threading :(
} }
/** /**
@ -48,7 +63,7 @@ public class PacketThreading {
*/ */
public static void waitUntilThreadFinished() { public static void waitUntilThreadFinished() {
try { try {
if (!(processedCnt >= totalCnt)) { if (!(processedCnt >= totalCnt) && !GeneralConfig.enablePacketThreading) {
for (Future<?> future : futureList) { for (Future<?> future : futureList) {
future.get(50, TimeUnit.MILLISECONDS); // I HATE EVERYTHING future.get(50, TimeUnit.MILLISECONDS); // I HATE EVERYTHING
} }

View File

@ -20,6 +20,7 @@ import com.hbm.handler.imc.IMCHandler;
import com.hbm.handler.neutron.NeutronHandler; import com.hbm.handler.neutron.NeutronHandler;
import com.hbm.handler.pollution.PollutionHandler; import com.hbm.handler.pollution.PollutionHandler;
import com.hbm.handler.radiation.ChunkRadiationManager; import com.hbm.handler.radiation.ChunkRadiationManager;
import com.hbm.handler.threading.PacketThreading;
import com.hbm.hazard.HazardRegistry; import com.hbm.hazard.HazardRegistry;
import com.hbm.inventory.FluidContainerRegistry; import com.hbm.inventory.FluidContainerRegistry;
import com.hbm.inventory.OreDictManager; import com.hbm.inventory.OreDictManager;
@ -904,6 +905,8 @@ public class MainRegistry {
PacketDispatcher.registerPackets(); PacketDispatcher.registerPackets();
PacketThreading.init();
NeutronHandler neutronHandler = new NeutronHandler(); NeutronHandler neutronHandler = new NeutronHandler();
MinecraftForge.EVENT_BUS.register(neutronHandler); MinecraftForge.EVENT_BUS.register(neutronHandler);
FMLCommonHandler.instance().bus().register(neutronHandler); FMLCommonHandler.instance().bus().register(neutronHandler);
@ -935,6 +938,7 @@ public class MainRegistry {
event.registerServerCommand(new CommandDebugChunkLoad()); event.registerServerCommand(new CommandDebugChunkLoad());
event.registerServerCommand(new CommandSatellites()); event.registerServerCommand(new CommandSatellites());
event.registerServerCommand(new CommandRadiation()); event.registerServerCommand(new CommandRadiation());
event.registerServerCommand(new CommandPacketInfo());
} }
@EventHandler @EventHandler

View File

@ -877,6 +877,7 @@ public class TileEntityMachineExcavator extends TileEntityMachineBase implements
HashMap<UpgradeType, Integer> upgrades = new HashMap<>(); HashMap<UpgradeType, Integer> upgrades = new HashMap<>();
upgrades.put(UpgradeType.SPEED, 3); upgrades.put(UpgradeType.SPEED, 3);
upgrades.put(UpgradeType.POWER, 3); upgrades.put(UpgradeType.POWER, 3);
upgrades.put(UpgradeType.EFFECT, 3);
return upgrades; return upgrades;
} }