moved a bunch of stuff

This commit is contained in:
gnembon committed 2019-05-12 01:48:19 -03:00
1 parent 72c276f976
commit ecced620a4
30 files changed
+4450 -51

No files matched your search

@@ -0,0 +1,372 @@
package carpet.utils;
import carpet.CarpetSettings;
import net.minecraft.entity.Entity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.util.registry.Registry;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
public class CarpetProfiler
{
private static final HashMap<String, Long> time_repo = new HashMap<>();
public static int tick_health_requested = 0;
private static int tick_health_elapsed = 0;
private static int test_type = 0; //1 for ticks, 2 for entities;
private static String current_section = null;
private static long current_section_start = 0;
private static long current_tick_start = 0;
private static String [] GENERAL_SECTIONS = {"Network", "Autosave"};
private static String [] DIMENSIONS = {"Overworld","The End","The Nether"};
private static String [] SECTIONS = {"Spawning","Blocks","Entities","Tile Entities","Entities(client)","Tile Entities(client)","Villages"};
public static void prepare_tick_report(int ticks)
{
//maybe add so it only spams the sending player, but honestly - all may want to see it
time_repo.clear();
test_type = 1;
time_repo.put("tick",0L);
time_repo.put("Network",0L);
time_repo.put("Autosave",0L);
for (String section: GENERAL_SECTIONS)
{
time_repo.put(section,0L);
}
for (String level: DIMENSIONS)
{
for (String section: SECTIONS)
{
time_repo.put(level+"."+section,0L);
}
}
tick_health_elapsed = ticks;
tick_health_requested = ticks;
current_tick_start = 0L;
current_section_start = 0L;
current_section = null;
}
public static void start_section(String dimension, String name)
{
if (tick_health_requested == 0L || test_type != 1)
{
return;
}
if (current_tick_start == 0L)
{
return;
}
if (current_section != null)
{
end_current_section();
}
String key = name;
if (dimension != null)
{
key = dimension+"."+name;
}
current_section = key;
current_section_start = System.nanoTime();
}
public static class ProfilerToken
{
public String section;
public long start;
public ProfilerToken(String name, long start)
{
this.section = name;
this.start = start;
}
}
public static ProfilerToken start_section_concurrent(String dimension, String name, boolean isRemote)
{
if (tick_health_requested == 0L || test_type != 1)
{
return null;
}
if (current_tick_start == 0L)
{
return null;
}
String key = name;
if (dimension != null)
{
key = dimension+"."+name;
}
if (isRemote) key += "(client)";
long time = System.nanoTime();
return new ProfilerToken(key, time);
}
public static ProfilerToken start_entity_section(String dimension, Entity e)
{
if (tick_health_requested == 0L || test_type != 2)
{
return null;
}
if (current_tick_start == 0L)
{
return null;
}
String section = dimension+"."+ Registry.ENTITY_TYPE.getId(e.getType()).toString().replaceFirst("minecraft:","");
if (e.getEntityWorld().isClient)
section += "(client)";
long section_start = System.nanoTime();
return new ProfilerToken(section, section_start);
}
public static ProfilerToken start_tileentity_section(String dimension, BlockEntity e)
{
if (tick_health_requested == 0L || test_type != 2)
{
return null;
}
if (current_tick_start == 0L)
{
return null;
}
String section = dimension+"."+ BlockEntityType.getId(e.getType()).toString().replaceFirst("minecraft:","");
if (e.getWorld() == null)
section += "??";
else if (e.getWorld().isClient)
section += "(client)";
long section_start = System.nanoTime();
return new ProfilerToken(section, section_start);
}
public static void end_current_section()
{
if (tick_health_requested == 0L || test_type != 1)
{
return;
}
long end_time = System.nanoTime();
if (current_tick_start == 0L)
{
return;
}
if (current_section == null)
{
CarpetSettings.LOG.error("finishing section that hasn't started");
return;
}
//CarpetSettings.LOG.error("finishing section "+current_section);
time_repo.put(current_section,time_repo.get(current_section)+end_time-current_section_start);
current_section = null;
current_section_start = 0;
}
public static void end_current_section_concurrent(ProfilerToken tok)
{
if (tick_health_requested == 0L || test_type != 1)
{
return;
}
long end_time = System.nanoTime();
if (current_tick_start == 0L)
{
return;
}
if (tok == null)
{
CarpetSettings.LOG.error("finishing section that hasn't started");
return;
}
//CarpetSettings.LOG.error("finishing section "+current_section);
time_repo.put(tok.section,time_repo.get(tok.section)+end_time-tok.start);
}
public static void end_current_entity_section(ProfilerToken tok)
{
if (tick_health_requested == 0L || test_type != 2)
{
return;
}
long end_time = System.nanoTime();
if (current_tick_start == 0L)
{
return;
}
if (tok == null)
{
CarpetSettings.LOG.error("finishing entity/TE section that hasn't started");
return;
}
String time_section = "t."+tok.section;
String count_section = "c."+tok.section;
time_repo.put(time_section,time_repo.getOrDefault(time_section,0L)+end_time-tok.start);
time_repo.put(count_section,time_repo.getOrDefault(count_section,0L)+1);
}
public static void start_tick_profiling()
{
current_tick_start = System.nanoTime();
}
public static void end_tick_profiling(MinecraftServer server)
{
if (current_tick_start == 0L)
{
return;
}
time_repo.put("tick",time_repo.get("tick")+System.nanoTime()-current_tick_start);
tick_health_elapsed --;
//CarpetSettings.LOG.error("tick count current at "+time_repo.get("tick"));
if (tick_health_elapsed <= 0)
{
finalize_tick_report(server);
}
}
public static void finalize_tick_report(MinecraftServer server)
{
if (test_type == 1)
{
finalize_tick_report_for_time(server);
}
if (test_type == 2)
{
finalize_tick_report_for_entities(server);
}
cleanup_tick_report();
}
public static void cleanup_tick_report()
{
time_repo.clear();
time_repo.put("tick",0L);
test_type = 0;
tick_health_elapsed = 0;
tick_health_requested = 0;
current_tick_start = 0L;
current_section_start = 0L;
current_section = null;
}
public static void finalize_tick_report_for_time(MinecraftServer server)
{
//print stats
long total_tick_time = time_repo.get("tick");
double divider = 1.0D/tick_health_requested/1000000;
Messenger.print_server_message(server, String.format("Average tick time: %.3fms",divider*total_tick_time));
long accumulated = 0L;
for (String section: GENERAL_SECTIONS)
{
double amount = divider*time_repo.get(section);
if (amount > 0.01)
{
accumulated += time_repo.get(section);
Messenger.print_server_message(server, String.format("%s: %.3fms", section, amount));
}
}
for (String dimension: DIMENSIONS)
{
boolean hasSomethin = false;
for (String section: SECTIONS)
{
double amount = divider*time_repo.get(dimension+"."+section);
if (amount > 0.01)
{
hasSomethin = true;
break;
}
}
if (!(hasSomethin))
{
continue;
}
Messenger.print_server_message(server, dimension+":");
for (String section: SECTIONS)
{
double amount = divider*time_repo.get(dimension+"."+section);
if (amount > 0.01)
{
if (!(section.endsWith("(client)")))
accumulated += time_repo.get(dimension+"."+section);
Messenger.print_server_message(server, String.format(" - %s: %.3fms", section, amount));
}
}
}
long rest = total_tick_time-accumulated;
Messenger.print_server_message(server, String.format("The Rest, whatever that might be: %.3fms",divider*rest));
}
public static void finalize_tick_report_for_entities(MinecraftServer server)
{
//print stats
long total_tick_time = time_repo.get("tick");
double divider = 1.0D/tick_health_requested/1000000;
double divider_1 = 1.0D/(tick_health_requested-1)/1000000;
Messenger.print_server_message(server, String.format("Average tick time: %.3fms",divider*total_tick_time));
time_repo.remove("tick");
Messenger.print_server_message(server, "Top 10 counts:");
int total = 0;
for ( Map.Entry<String, Long> entry : time_repo.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).collect(Collectors.toList()) )
{
if (entry.getKey().startsWith("t."))
{
continue;
}
total++;
if (total > 10)
{
continue;
}
String[] parts = entry.getKey().split("\\.");
String dim = parts[1];
String name = parts[2];
int penalty = name.endsWith("(client)") ? 1 :0;
Messenger.print_server_message(server, String.format(" - %s in %s: %.3f",name, dim, 1.0D*entry.getValue()/(tick_health_requested-penalty)));
}
Messenger.print_server_message(server, "Top 10 grossing:");
total = 0;
for ( Map.Entry<String, Long> entry : time_repo.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).collect(Collectors.toList()) )
{
if (entry.getKey().startsWith("c."))
{
continue;
}
total++;
if (total > 10)
{
continue;
}
String[] parts = entry.getKey().split("\\.");
String dim = parts[1];
String name = parts[2];
double applicableDivider = name.endsWith("(client)") ? divider :divider_1;
Messenger.print_server_message(server, String.format(" - %s in %s: %.3fms",name, dim, applicableDivider*entry.getValue()));
}
}
public static void prepare_entity_report(int ticks)
{
//maybe add so it only spams the sending player, but honestly - all may want to see it
time_repo.clear();
time_repo.put("tick",0L);
test_type = 2;
tick_health_elapsed = ticks;
tick_health_requested = ticks;
current_tick_start = 0L;
current_section_start = 0L;
current_section = null;
}
}
+101
View File
@@ -0,0 +1,101 @@
package carpet.utils;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.item.ItemStack;
import net.minecraft.network.chat.BaseComponent;
import net.minecraft.world.World;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
// not really needed anymore
public class EntityInfo
{
private static String makeTime(long ticks)
{
long secs = ticks/20;
if (secs < 60)
{
return String.format("%d\"", secs);
}
if (secs < 60*60)
{
return String.format("%d'%d\"", secs/60, secs%60);
}
return String.format("%dh%d'%d\"", secs/60/60, (secs % (60*60))/60,(secs % (60*60))%60 );
}
private static String display_item(ItemStack item)
{
if (item == null)
{
return null;
}
if (item.isEmpty()) // func_190926_b()
{
return null;
} // func_190916_E()
String stackname = item.getAmount()>1?String.format("%dx%s",item.getAmount(), item.getDisplayName().getString()):item.getDisplayName().getString();
if (item.isDamaged())
{
stackname += String.format(" %d/%d", item.getMaxUseTime()-item.getDamage(), item.getMaxUseTime());
}
if (item.hasEnchantments())
{
stackname += " ( ";
Map<Enchantment, Integer> enchants = EnchantmentHelper.getEnchantments(item);
for (Enchantment e: enchants.keySet())
{
int level = enchants.get(e);
String enstring = e.getTextComponent(level).getString();
stackname += enstring+" ";
}
stackname += ")";
}
return stackname;
}
public static String entity_short_string(Entity e)
{
if (e == null)
{
return "None";
}
return String.format("%s at [%.1f, %.1f, %.1f]",e.getDisplayName().getString(), e.x, e.y, e.y);
}
private static double get_speed(double internal)
{
return 43.1*internal;
}
private static double get_horse_speed_percent(double internal)
{
double min = 0.45*0.25;
double max = (0.45+0.9)*0.25;
return 100*(internal-min)/(max-min);
}
private static double get_horse_jump(double x)
{
return -0.1817584952 * x*x*x + 3.689713992 * x*x + 2.128599134 * x - 0.343930367;
}
private static double get_horse_jump_percent(double internal)
{
double min = 0.4;
double max = 1.0;
return 100*(internal-min)/(max-min);
}
public static List<BaseComponent> entityInfo(Entity e, World source_world)
{
List<BaseComponent> lst = new ArrayList<>();
lst.add(Messenger.s("w Use /data get entity command"));
return lst;
}
}
@@ -4,7 +4,7 @@ import carpet.helpers.HopperCounter;
import carpet.helpers.TickSpeed;
import carpet.logging.LoggerRegistry;
import carpet.logging.logHelpers.PacketCounter;
import carpet.mixins.logging.PlayerListHeaderS2CPacketMixin;
import carpet.mixins.PlayerListHeaderS2CPacketMixin;
import net.minecraft.entity.EntityCategory;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.ServerPlayerEntity;
@@ -0,0 +1,184 @@
package carpet.utils;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.block.Material;
import net.minecraft.block.BlockState;
import net.minecraft.entity.EntityCategory;
import net.minecraft.entity.SpawnRestriction;
import net.minecraft.entity.SpawnType;
import net.minecraft.entity.WaterCreatureEntity;
import net.minecraft.entity.mob.AmbientEntity;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.mob.Monster;
import net.minecraft.entity.passive.PassiveEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.SpawnHelper;
import net.minecraft.world.biome.Biome;
import java.util.ArrayList;
import java.util.List;
public class PerimeterDiagnostics
{
public static class Result
{
public int liquid;
public int ground;
public int specific;
public List<BlockPos> samples;
Result()
{
samples = new ArrayList<>();
}
}
private Biome.SpawnEntry sle;
private ServerWorld worldServer;
private EntityCategory ctype;
private MobEntity el;
private PerimeterDiagnostics(ServerWorld server, EntityCategory ctype, MobEntity el)
{
this.sle = null;
this.worldServer = server;
this.ctype = ctype;
this.el = el;
}
public static Result countSpots(ServerWorld worldserver, BlockPos epos, MobEntity el)
{
BlockPos pos;
//List<BlockPos> samples = new ArrayList<BlockPos>();
//if (el != null) CarpetSettings.LOG.error(String.format("Got %s to check",el.toString()));
int eY = epos.getY();
int eX = epos.getX();
int eZ = epos.getZ();
Result result = new Result();
//int ground_spawns = 0;
//int liquid_spawns = 0;
//int specific_spawns = 0;
boolean add_water = false;
boolean add_ground = false;
EntityCategory ctype = null;
if (el != null)
{
if (el instanceof WaterCreatureEntity)
{
add_water = true;
ctype = EntityCategory.WATER_CREATURE;
}
else if (el instanceof PassiveEntity)
{
add_ground = true;
ctype = EntityCategory.CREATURE;
}
else if (el instanceof Monster)
{
add_ground = true;
ctype = EntityCategory.MONSTER;
}
else if (el instanceof AmbientEntity)
{
ctype = EntityCategory.AMBIENT;
}
}
PerimeterDiagnostics diagnostic = new PerimeterDiagnostics(worldserver,ctype,el);
for (int x = -128; x <= 128; ++x)
{
for (int z = -128; z <= 128; ++z)
{
if (x*x + z*z > 128*128) // cut out a cyllinder first
{
continue;
}
for (int y= 0; y < 256; ++y)
{
if ((Math.abs(y-eY)>128) )
{
continue;
}
int distsq = (x)*(x)+(eY-y)*(eY-y)+(z)*(z);
if (distsq > 128*128 || distsq < 24*24)
{
continue;
}
pos = new BlockPos(eX+x, y, eZ+z);
BlockState iblockstate = worldserver.getBlockState(pos);
BlockState iblockstate_down = worldserver.getBlockState(pos.down());
BlockState iblockstate_up = worldserver.getBlockState(pos.up());
if ( iblockstate.getMaterial() == Material.WATER && iblockstate_down.getMaterial() == Material.WATER && !iblockstate_up.isSimpleFullBlock(worldserver, pos))
{
result.liquid++;
if (add_water && diagnostic.check_entity_spawn(pos))
{
result.specific++;
if (result.samples.size() < 10)
{
result.samples.add(pos);
}
}
}
else
{
if (iblockstate_down.isSimpleFullBlock(worldserver, pos))
{
Block block = iblockstate_down.getBlock();
boolean flag = block != Blocks.BEDROCK && block != Blocks.BARRIER;
if( flag && SpawnHelper.isClearForSpawn(worldserver, pos, iblockstate, iblockstate.getFluidState()) && SpawnHelper.isClearForSpawn(worldserver, pos.up(), iblockstate_up, iblockstate_up.getFluidState()))
{
result.ground ++;
if (add_ground && diagnostic.check_entity_spawn(pos))
{
result.specific++;
if (result.samples.size() < 10)
{
result.samples.add(pos);
}
}
}
}
}
}
}
}
//ashMap<String,Integer> result= new HashMap<>();
//result.put("Potential in-water spawning spaces", liquid_spawns);
//result.put("Potential on-ground spawning spaces", ground_spawns);
//if (el != null) result.put(String.format("%s spawning spaces",el.getDisplayName().getUnformattedText()),specific_spawns);
return result;
}
private boolean check_entity_spawn(BlockPos pos)
{
if (sle == null || !worldServer.getChunkManager().getChunkGenerator().getEntitySpawnList(ctype, pos).contains(sle))
{
sle = null;
for (Biome.SpawnEntry sle: worldServer.getChunkManager().getChunkGenerator().getEntitySpawnList(ctype, pos))
{
if (el.getType() == sle.type)
{
this.sle = sle;
break;
}
}
if (sle == null || !worldServer.getChunkManager().getChunkGenerator().getEntitySpawnList(ctype, pos).contains(sle))
{
return false;
}
}
SpawnRestriction.Location spt = SpawnRestriction.getLocation(sle.type);
if (SpawnHelper.canSpawn(spt, worldServer, pos, sle.type))
{
el.setPositionAndAngles((float)pos.getX() + 0.5F, (float)pos.getY(), (float)pos.getZ()+0.5F, 0.0F, 0.0F);
return el.canSpawn(worldServer) && el.canSpawn(worldServer, SpawnType.NATURAL) && worldServer.doesNotCollide(el); // check collision rules once they stop fiddling with them after 1.14.1
}
return false;
}
}