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

@ -5,7 +5,9 @@ import net.minecraftforge.common.config.Configuration;
public class GeneralConfig {
public static boolean enableThermosPreventer = true;
public static boolean enablePacketThreading = true;
public static boolean enableDebugMode = true;
public static boolean enableMycelium = false;
public static boolean enablePlutoniumOre = false;
@ -40,7 +42,7 @@ public class GeneralConfig {
public static int hintPos = 0;
public static boolean enableExpensiveMode = false;
public static boolean enable528 = false;
public static boolean enable528ReasimBoilers = true;
public static boolean enable528ColtanDeposit = true;
@ -68,11 +70,14 @@ public class GeneralConfig {
public static boolean enableLBSMSafeMEDrives = true;
public static boolean enableLBSMIGen = true;
public static int schrabRate = 20;
public static void loadFromConfig(Configuration config) {
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);
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);
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);
@ -112,14 +117,14 @@ public class GeneralConfig {
"Note that a value below 28 or above 200 can cause buggy sounds and issues with other mods running out of sound memory.", 100);
enableExpensiveMode = config.get(CATEGORY_GENERAL, "1.99_enableExpensiveMode", false, "It does what the name implies.").getBoolean(false);
final String CATEGORY_528 = CommonConfig.CATEGORY_528;
config.addCustomCategoryComment(CATEGORY_528, "CAUTION\n"
+ "528 Mode: Please proceed with caution!\n"
+ "528-Modus: Lassen Sie Vorsicht walten!\n"
+ "способ-528: действовать с осторожностью!");
enable528 = CommonConfig.createConfigBool(config, CATEGORY_528, "enable528Mode", "The central toggle for 528 mode.", false);
enable528ReasimBoilers = CommonConfig.createConfigBool(config, CATEGORY_528, "X528_forceReasimBoilers", "Keeps the RBMK dial for ReaSim boilers on, preventing use of non-ReaSim boiler columns and forcing the use of steam in-/outlets", true);
enable528ColtanDeposit = CommonConfig.createConfigBool(config, CATEGORY_528, "X528_enableColtanDepsoit", "Enables the coltan deposit. A large amount of coltan will spawn around a single random location in the world.", true);
@ -131,15 +136,15 @@ public class GeneralConfig {
enable528NetherBurn = CommonConfig.createConfigBool(config, CATEGORY_528, "X528_enable528NetherBurn", "Whether players burn in the nether", true);
coltanRate = CommonConfig.createConfigInt(config, CATEGORY_528, "X528_oreColtanFrequency", "Determines how many coltan ore veins are to be expected in a chunk. These values do not affect the frequency in deposits, and only apply if random coltan spanwing is enabled.", 2);
bedrockRate = CommonConfig.createConfigInt(config, CATEGORY_528, "X528_bedrockColtanFrequency", "Determines how often (1 in X) bedrock coltan ores spawn. Applies for both the bedrock ores in the coltan deposit (if applicable) and the random bedrock ores (if applicable)", 50);
final String CATEGORY_LBSM = CommonConfig.CATEGORY_LBSM;
config.addCustomCategoryComment(CATEGORY_LBSM,
"Will most likely break standard progression!\n"
+ "However, the game gets generally easier and more enjoyable for casual players.\n"
+ "Progression-braking recipes are usually not too severe, so the mode is generally server-friendly!");
enableLBSM = CommonConfig.createConfigBool(config, CATEGORY_LBSM, "enableLessBullshitMode", "The central toggle for LBS mode. Forced OFF when 528 is enabled!", false);
enableLBSMFullSchrab = CommonConfig.createConfigBool(config, CATEGORY_LBSM, "LBSM_fullSchrab", "When enabled, this will replace schraranium with full schrabidium ingots in the transmutator's output", true);
enableLBSMShorterDecay = CommonConfig.createConfigBool(config, CATEGORY_LBSM, "LBSM_shortDecay", "When enabled, this will highly accelerate the speed at which nuclear waste disposal drums decay their contents. 60x faster than 528 mode and 5-12x faster than on normal mode.", true);
@ -155,7 +160,7 @@ public class GeneralConfig {
enableLBSMSafeMEDrives = CommonConfig.createConfigBool(config, CATEGORY_LBSM, "LBSM_safeMEDrives", "When enabled, prevents ME Drives and Portable Cells from becoming radioactive", true);
enableLBSMIGen = CommonConfig.createConfigBool(config, CATEGORY_LBSM, "LBSM_iGen", "When enabled, restores the industrial generator to pre-nerf power", true);
schrabRate = CommonConfig.createConfigInt(config, CATEGORY_LBSM, "LBSM_schrabOreRate", "Changes the amount of uranium ore needed on average to create one schrabidium ore using nukes. Standard mode value is 100", 20);
if(enable528) enableLBSM = false;
}
}

View File

@ -1,6 +1,7 @@
package com.hbm.handler.threading;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.hbm.config.GeneralConfig;
import com.hbm.main.MainRegistry;
import com.hbm.packet.PacketDispatcher;
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 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.
@ -35,12 +44,18 @@ public class PacketThreading {
((PrecompiledPacket) message).getPreBuf(); // Gets the precompiled buffer, doing nothing if it already exists.
totalCnt++;
futureList.add(threadPool.submit(() -> {
Runnable task = () -> {
PacketDispatcher.wrapper.sendToAllAround(message, target);
if(message instanceof PrecompiledPacket)
((PrecompiledPacket) message).getPreBuf().release();
processedCnt++;
}));
if(message instanceof PrecompiledPacket)
((PrecompiledPacket) message).getPreBuf().release();
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() {
try {
if (!(processedCnt >= totalCnt)) {
if (!(processedCnt >= totalCnt) && !GeneralConfig.enablePacketThreading) {
for (Future<?> future : futureList) {
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.pollution.PollutionHandler;
import com.hbm.handler.radiation.ChunkRadiationManager;
import com.hbm.handler.threading.PacketThreading;
import com.hbm.hazard.HazardRegistry;
import com.hbm.inventory.FluidContainerRegistry;
import com.hbm.inventory.OreDictManager;
@ -904,6 +905,8 @@ public class MainRegistry {
PacketDispatcher.registerPackets();
PacketThreading.init();
NeutronHandler neutronHandler = new NeutronHandler();
MinecraftForge.EVENT_BUS.register(neutronHandler);
FMLCommonHandler.instance().bus().register(neutronHandler);
@ -935,6 +938,7 @@ public class MainRegistry {
event.registerServerCommand(new CommandDebugChunkLoad());
event.registerServerCommand(new CommandSatellites());
event.registerServerCommand(new CommandRadiation());
event.registerServerCommand(new CommandPacketInfo());
}
@EventHandler
@ -1496,7 +1500,7 @@ public class MainRegistry {
ignoreMappings.add("hbm:tile.statue_elb");
ignoreMappings.add("hbm:tile.statue_elb_g");
ignoreMappings.add("hbm:tile.statue_elb_w");
remapItems.put("hbm:item.gadget_explosive8", ModItems.early_explosive_lenses);
remapItems.put("hbm:item.man_explosive8", ModItems.explosive_lenses);
remapItems.put("hbm:item.briquette_lignite", ModItems.briquette);

View File

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