base mod created
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package net.minecraft.profiler;
|
||||
|
||||
public interface ISnooperInfo
|
||||
{
|
||||
void addServerStatsToSnooper(Snooper playerSnooper);
|
||||
|
||||
void addServerTypeToSnooper(Snooper playerSnooper);
|
||||
|
||||
/**
|
||||
* Returns whether snooping is enabled or not.
|
||||
*/
|
||||
boolean isSnooperEnabled();
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
package net.minecraft.profiler;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class Profiler
|
||||
{
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
/** List of parent sections */
|
||||
private final List<String> sectionList = Lists.<String>newArrayList();
|
||||
/** List of timestamps (System.nanoTime) */
|
||||
private final List<Long> timestampList = Lists.<Long>newArrayList();
|
||||
/** Flag profiling enabled */
|
||||
public boolean profilingEnabled;
|
||||
/** Current profiling section */
|
||||
private String profilingSection = "";
|
||||
/** Profiling map */
|
||||
private final Map<String, Long> profilingMap = Maps.<String, Long>newHashMap();
|
||||
|
||||
/**
|
||||
* Clear profiling.
|
||||
*/
|
||||
public void clearProfiling()
|
||||
{
|
||||
this.profilingMap.clear();
|
||||
this.profilingSection = "";
|
||||
this.sectionList.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start section
|
||||
*/
|
||||
public void startSection(String name)
|
||||
{
|
||||
if (this.profilingEnabled)
|
||||
{
|
||||
if (!this.profilingSection.isEmpty())
|
||||
{
|
||||
this.profilingSection = this.profilingSection + ".";
|
||||
}
|
||||
|
||||
this.profilingSection = this.profilingSection + name;
|
||||
this.sectionList.add(this.profilingSection);
|
||||
this.timestampList.add(Long.valueOf(System.nanoTime()));
|
||||
}
|
||||
}
|
||||
|
||||
public void func_194340_a(Supplier<String> p_194340_1_)
|
||||
{
|
||||
if (this.profilingEnabled)
|
||||
{
|
||||
this.startSection(p_194340_1_.get());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* End section
|
||||
*/
|
||||
public void endSection()
|
||||
{
|
||||
if (this.profilingEnabled)
|
||||
{
|
||||
long i = System.nanoTime();
|
||||
long j = ((Long)this.timestampList.remove(this.timestampList.size() - 1)).longValue();
|
||||
this.sectionList.remove(this.sectionList.size() - 1);
|
||||
long k = i - j;
|
||||
|
||||
if (this.profilingMap.containsKey(this.profilingSection))
|
||||
{
|
||||
this.profilingMap.put(this.profilingSection, Long.valueOf(((Long)this.profilingMap.get(this.profilingSection)).longValue() + k));
|
||||
}
|
||||
else
|
||||
{
|
||||
this.profilingMap.put(this.profilingSection, Long.valueOf(k));
|
||||
}
|
||||
|
||||
if (k > 100000000L)
|
||||
{
|
||||
LOGGER.warn("Something's taking too long! '{}' took aprox {} ms", this.profilingSection, Double.valueOf((double)k / 1000000.0D));
|
||||
}
|
||||
|
||||
this.profilingSection = this.sectionList.isEmpty() ? "" : (String)this.sectionList.get(this.sectionList.size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get profiling data
|
||||
*/
|
||||
public List<Profiler.Result> getProfilingData(String profilerName)
|
||||
{
|
||||
if (!this.profilingEnabled)
|
||||
{
|
||||
return Collections.<Profiler.Result>emptyList();
|
||||
}
|
||||
else
|
||||
{
|
||||
long i = this.profilingMap.containsKey("root") ? ((Long)this.profilingMap.get("root")).longValue() : 0L;
|
||||
long j = this.profilingMap.containsKey(profilerName) ? ((Long)this.profilingMap.get(profilerName)).longValue() : -1L;
|
||||
List<Profiler.Result> list = Lists.<Profiler.Result>newArrayList();
|
||||
|
||||
if (!profilerName.isEmpty())
|
||||
{
|
||||
profilerName = profilerName + ".";
|
||||
}
|
||||
|
||||
long k = 0L;
|
||||
|
||||
for (String s : this.profilingMap.keySet())
|
||||
{
|
||||
if (s.length() > profilerName.length() && s.startsWith(profilerName) && s.indexOf(".", profilerName.length() + 1) < 0)
|
||||
{
|
||||
k += ((Long)this.profilingMap.get(s)).longValue();
|
||||
}
|
||||
}
|
||||
|
||||
float f = (float)k;
|
||||
|
||||
if (k < j)
|
||||
{
|
||||
k = j;
|
||||
}
|
||||
|
||||
if (i < k)
|
||||
{
|
||||
i = k;
|
||||
}
|
||||
|
||||
for (String s1 : this.profilingMap.keySet())
|
||||
{
|
||||
if (s1.length() > profilerName.length() && s1.startsWith(profilerName) && s1.indexOf(".", profilerName.length() + 1) < 0)
|
||||
{
|
||||
long l = ((Long)this.profilingMap.get(s1)).longValue();
|
||||
double d0 = (double)l * 100.0D / (double)k;
|
||||
double d1 = (double)l * 100.0D / (double)i;
|
||||
String s2 = s1.substring(profilerName.length());
|
||||
list.add(new Profiler.Result(s2, d0, d1));
|
||||
}
|
||||
}
|
||||
|
||||
for (String s3 : this.profilingMap.keySet())
|
||||
{
|
||||
this.profilingMap.put(s3, Long.valueOf(((Long)this.profilingMap.get(s3)).longValue() * 999L / 1000L));
|
||||
}
|
||||
|
||||
if ((float)k > f)
|
||||
{
|
||||
list.add(new Profiler.Result("unspecified", (double)((float)k - f) * 100.0D / (double)k, (double)((float)k - f) * 100.0D / (double)i));
|
||||
}
|
||||
|
||||
Collections.sort(list);
|
||||
list.add(0, new Profiler.Result(profilerName, 100.0D, (double)k * 100.0D / (double)i));
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* End current section and start a new section
|
||||
*/
|
||||
public void endStartSection(String name)
|
||||
{
|
||||
this.endSection();
|
||||
this.startSection(name);
|
||||
}
|
||||
|
||||
public String getNameOfLastSection()
|
||||
{
|
||||
return this.sectionList.isEmpty() ? "[UNKNOWN]" : (String)this.sectionList.get(this.sectionList.size() - 1);
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public void func_194339_b(Supplier<String> p_194339_1_)
|
||||
{
|
||||
this.endSection();
|
||||
this.func_194340_a(p_194339_1_);
|
||||
}
|
||||
|
||||
public static final class Result implements Comparable<Profiler.Result>
|
||||
{
|
||||
public double usePercentage;
|
||||
public double totalUsePercentage;
|
||||
public String profilerName;
|
||||
|
||||
public Result(String profilerName, double usePercentage, double totalUsePercentage)
|
||||
{
|
||||
this.profilerName = profilerName;
|
||||
this.usePercentage = usePercentage;
|
||||
this.totalUsePercentage = totalUsePercentage;
|
||||
}
|
||||
|
||||
public int compareTo(Profiler.Result p_compareTo_1_)
|
||||
{
|
||||
if (p_compareTo_1_.usePercentage < this.usePercentage)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return p_compareTo_1_.usePercentage > this.usePercentage ? 1 : p_compareTo_1_.profilerName.compareTo(this.profilerName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a color to display the profiler, generated from the hash code of the profiler's name
|
||||
*/
|
||||
@SideOnly(Side.CLIENT)
|
||||
public int getColor()
|
||||
{
|
||||
return (this.profilerName.hashCode() & 11184810) + 4473924;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Forge: Fix for MC-117087, World.updateEntities is wasting time calling Class.getSimpleName() when the profiler is not active
|
||||
*/
|
||||
@Deprecated // TODO: remove (1.13)
|
||||
public void startSection(Class<?> profiledClass)
|
||||
{
|
||||
if (this.profilingEnabled)
|
||||
{
|
||||
startSection(profiledClass.getSimpleName());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
package net.minecraft.profiler;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.lang.management.RuntimeMXBean;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.UUID;
|
||||
import java.util.Map.Entry;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.util.HttpUtil;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
|
||||
public class Snooper
|
||||
{
|
||||
/** The snooper Map of stats */
|
||||
private final Map<String, Object> snooperStats = Maps.<String, Object>newHashMap();
|
||||
/** The client Map of stats */
|
||||
private final Map<String, Object> clientStats = Maps.<String, Object>newHashMap();
|
||||
private final String uniqueID = UUID.randomUUID().toString();
|
||||
/** URL of the server to send the report to */
|
||||
private final URL serverUrl;
|
||||
private final ISnooperInfo playerStatsCollector;
|
||||
/** set to fire the snooperThread every 15 mins */
|
||||
private final Timer threadTrigger = new Timer("Snooper Timer", true);
|
||||
private final Object syncLock = new Object();
|
||||
private final long minecraftStartTimeMilis;
|
||||
private boolean isRunning;
|
||||
/** incremented on every getSelfCounterFor */
|
||||
private int selfCounter;
|
||||
|
||||
public Snooper(String side, ISnooperInfo playerStatCollector, long startTime)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.serverUrl = new URL("http://snoop.minecraft.net/" + side + "?version=" + 2);
|
||||
}
|
||||
catch (MalformedURLException var6)
|
||||
{
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
this.playerStatsCollector = playerStatCollector;
|
||||
this.minecraftStartTimeMilis = startTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Note issuing start multiple times is not an error.
|
||||
*/
|
||||
public void startSnooper()
|
||||
{
|
||||
if (!this.isRunning)
|
||||
{
|
||||
this.isRunning = true;
|
||||
this.addOSData();
|
||||
this.threadTrigger.schedule(new TimerTask()
|
||||
{
|
||||
public void run()
|
||||
{
|
||||
if (Snooper.this.playerStatsCollector.isSnooperEnabled())
|
||||
{
|
||||
Map<String, Object> map;
|
||||
|
||||
synchronized (Snooper.this.syncLock)
|
||||
{
|
||||
map = Maps.<String, Object>newHashMap(Snooper.this.clientStats);
|
||||
|
||||
if (Snooper.this.selfCounter == 0)
|
||||
{
|
||||
map.putAll(Snooper.this.snooperStats);
|
||||
}
|
||||
|
||||
map.put("snooper_count", Integer.valueOf(Snooper.this.selfCounter++));
|
||||
map.put("snooper_token", Snooper.this.uniqueID);
|
||||
}
|
||||
|
||||
MinecraftServer minecraftserver = Snooper.this.playerStatsCollector instanceof MinecraftServer ? (MinecraftServer)Snooper.this.playerStatsCollector : null;
|
||||
HttpUtil.postMap(Snooper.this.serverUrl, map, true, minecraftserver == null ? null : minecraftserver.getServerProxy());
|
||||
}
|
||||
}
|
||||
}, 0L, 900000L);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add OS data into the snooper
|
||||
*/
|
||||
private void addOSData()
|
||||
{
|
||||
this.addJvmArgsToSnooper();
|
||||
this.addClientStat("snooper_token", this.uniqueID);
|
||||
this.addStatToSnooper("snooper_token", this.uniqueID);
|
||||
this.addStatToSnooper("os_name", System.getProperty("os.name"));
|
||||
this.addStatToSnooper("os_version", System.getProperty("os.version"));
|
||||
this.addStatToSnooper("os_architecture", System.getProperty("os.arch"));
|
||||
this.addStatToSnooper("java_version", System.getProperty("java.version"));
|
||||
this.addClientStat("version", "1.12.2");
|
||||
this.playerStatsCollector.addServerTypeToSnooper(this);
|
||||
}
|
||||
|
||||
private void addJvmArgsToSnooper()
|
||||
{
|
||||
RuntimeMXBean runtimemxbean = ManagementFactory.getRuntimeMXBean();
|
||||
List<String> list = runtimemxbean.getInputArguments();
|
||||
int i = 0;
|
||||
|
||||
for (String s : list)
|
||||
{
|
||||
if (s.startsWith("-X"))
|
||||
{
|
||||
this.addClientStat("jvm_arg[" + i++ + "]", s);
|
||||
}
|
||||
}
|
||||
|
||||
this.addClientStat("jvm_args", Integer.valueOf(i));
|
||||
}
|
||||
|
||||
public void addMemoryStatsToSnooper()
|
||||
{
|
||||
this.addStatToSnooper("memory_total", Long.valueOf(Runtime.getRuntime().totalMemory()));
|
||||
this.addStatToSnooper("memory_max", Long.valueOf(Runtime.getRuntime().maxMemory()));
|
||||
this.addStatToSnooper("memory_free", Long.valueOf(Runtime.getRuntime().freeMemory()));
|
||||
this.addStatToSnooper("cpu_cores", Integer.valueOf(Runtime.getRuntime().availableProcessors()));
|
||||
this.playerStatsCollector.addServerStatsToSnooper(this);
|
||||
}
|
||||
|
||||
public void addClientStat(String statName, Object statValue)
|
||||
{
|
||||
synchronized (this.syncLock)
|
||||
{
|
||||
this.clientStats.put(statName, statValue);
|
||||
}
|
||||
}
|
||||
|
||||
public void addStatToSnooper(String statName, Object statValue)
|
||||
{
|
||||
synchronized (this.syncLock)
|
||||
{
|
||||
this.snooperStats.put(statName, statValue);
|
||||
}
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public Map<String, String> getCurrentStats()
|
||||
{
|
||||
Map<String, String> map = Maps.<String, String>newLinkedHashMap();
|
||||
|
||||
synchronized (this.syncLock)
|
||||
{
|
||||
this.addMemoryStatsToSnooper();
|
||||
|
||||
for (Entry<String, Object> entry : this.snooperStats.entrySet())
|
||||
{
|
||||
map.put(entry.getKey(), entry.getValue().toString());
|
||||
}
|
||||
|
||||
for (Entry<String, Object> entry1 : this.clientStats.entrySet())
|
||||
{
|
||||
map.put(entry1.getKey(), entry1.getValue().toString());
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isSnooperRunning()
|
||||
{
|
||||
return this.isRunning;
|
||||
}
|
||||
|
||||
public void stopSnooper()
|
||||
{
|
||||
this.threadTrigger.cancel();
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public String getUniqueID()
|
||||
{
|
||||
return this.uniqueID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the saved value of System#currentTimeMillis when the game started
|
||||
*/
|
||||
public long getMinecraftStartTimeMillis()
|
||||
{
|
||||
return this.minecraftStartTimeMilis;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// Auto generated package-info by MCP
|
||||
@ParametersAreNonnullByDefault
|
||||
@MethodsReturnNonnullByDefault
|
||||
package net.minecraft.profiler;
|
||||
|
||||
import mcp.MethodsReturnNonnullByDefault;
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
Reference in New Issue
Block a user