This commit is contained in:
AlgorithmX2 2014-08-14 19:59:32 -05:00
commit 881654d48d
4 changed files with 1 additions and 408 deletions

View file

@ -19,8 +19,6 @@ import appeng.core.sync.network.NetworkHandler;
import appeng.hooks.TickHandler;
import appeng.integration.IntegrationRegistry;
import appeng.integration.IntegrationType;
import appeng.server.AECommand;
import appeng.services.Profiler;
import appeng.services.VersionChecker;
import appeng.util.Platform;
@ -36,7 +34,6 @@ import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.event.FMLServerAboutToStartEvent;
import cpw.mods.fml.common.event.FMLServerStartingEvent;
import cpw.mods.fml.common.event.FMLServerStoppingEvent;
import cpw.mods.fml.common.network.NetworkRegistry;
@ -129,12 +126,6 @@ public class AppEng
Registration.instance.PreInit( event );
if ( AEConfig.instance.isFeatureEnabled( AEFeature.Profiler ) )
{
AELog.info( "Starting Profiler" );
startService( "AE2 Profiler", (new Thread( Profiler.instance = new Profiler() )) );
}
if ( AEConfig.instance.isFeatureEnabled( AEFeature.VersionChecker ) )
{
AELog.info( "Starting VersionChecker" );
@ -209,10 +200,4 @@ public class AppEng
WorldSettings.getInstance().init();
}
@EventHandler
public void serverStarting(FMLServerStartingEvent evt)
{
evt.registerServerCommand( new AECommand( evt.getServer() ) );
}
}

View file

@ -46,7 +46,7 @@ public enum AEFeature
MassCannonBlockDamage("BlockFeatures"), TinyTNTBlockDamage("BlockFeatures"), Facades("Facades"),
Profiler("Services", false), VersionChecker("Services"), UnsupportedDeveloperTools("Misc", false), Creative("Misc"),
VersionChecker("Services"), UnsupportedDeveloperTools("Misc", false), Creative("Misc"),
GrinderLogging("Misc", false), Logging("Misc"), IntegrationLogging("Misc", false), CustomRecipes("Crafting", false), WebsiteRecipes("Misc", false),

View file

@ -1,116 +0,0 @@
package appeng.server;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.WrongUsageException;
import net.minecraft.server.MinecraftServer;
import appeng.services.Profiler;
public class AECommand extends CommandBase
{
public AECommand(MinecraftServer server) {
}
@Override
public String getCommandName()
{
return "ae";
}
@Override
public String getCommandUsage(ICommandSender icommandsender)
{
return "commands.ae.usage";
}
@Override
public int getRequiredPermissionLevel()
{
return 2;
}
@Override
public void processCommand(ICommandSender sender, String[] args)
{
if ( args.length == 0 )
{
throw new WrongUsageException( "commands.ae.usage" );
}
else if ( "help".equals( args[0] ) )
{
throw new WrongUsageException( "commands.ae.usage" );
}
else if ( "profile".equals( args[0] ) )
{
if ( args.length > 1 && "start".equals( args[1] ) )
beginProfile( sender, args );
else if ( args.length > 1 && "stop".equals( args[1] ) )
endProfile( sender, args );
else
throw new WrongUsageException( "commands.ae.profile.usage" );
}
else
{
throw new WrongUsageException( "commands.ae.usage" );
}
}
private void endProfile(ICommandSender sender, String[] args)
{
if ( Profiler.instance == null )
{
throw new CommandException( "commands.ae.profiling.nojdk" );
}
if ( Profiler.instance.isRunning )
{
if ( Profiler.instance.profile )
{
Profiler.instance.profile = false;
synchronized (Profiler.instance.lock)
{
Profiler.instance.lock.notify();
}
}
else
throw new CommandException( "commands.ae.profiling.inactive" );
}
else
throw new CommandException( "commands.ae.profiling.nojdk" );
}
private void beginProfile(ICommandSender sender, String[] args)
{
if ( Profiler.instance == null )
{
throw new CommandException( "commands.ae.profiling.nojdk" );
}
if ( Profiler.instance.isRunning )
{
if ( Profiler.instance.profile )
throw new CommandException( "commands.ae.profiling.active" );
else
{
Profiler.instance.profile = true;
synchronized (Profiler.instance.lock)
{
Profiler.instance.lock.notify();
}
}
}
else
throw new CommandException( "commands.ae.profiling.nojdk" );
}
/**
* wtf?
*/
@Override
public int compareTo(Object arg0)
{
return 1;
}
}

View file

@ -1,276 +0,0 @@
package appeng.services;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import org.apache.commons.lang3.StringUtils;
import appeng.core.AELog;
public class Profiler implements Runnable
{
public static Profiler instance = null;
public boolean profile = false;
public boolean isRunning = true;
public final Object lock = new Object();
public static String getCommandOutput(String command)
{
String output = null; // the string to return
Process process = null;
BufferedReader reader = null;
InputStreamReader streamReader = null;
InputStream stream = null;
try
{
process = Runtime.getRuntime().exec( command );
stream = process.getInputStream();
streamReader = new InputStreamReader( stream );
reader = new BufferedReader( streamReader );
String currentLine = null;
StringBuilder commandOutput = new StringBuilder();
while ((currentLine = reader.readLine()) != null)
{
commandOutput.append( currentLine );
}
int returnCode = process.waitFor();
if ( returnCode == 0 )
{
output = commandOutput.toString();
}
}
catch (IOException e)
{
output = null;
}
catch (InterruptedException e)
{
}
finally
{
if ( stream != null )
{
try
{
stream.close();
}
catch (IOException e)
{
}
}
if ( streamReader != null )
{
try
{
streamReader.close();
}
catch (IOException e)
{
}
}
if ( reader != null )
{
try
{
streamReader.close();
}
catch (IOException e)
{
}
}
}
return output;
}
private String findJDK()
{
if ( System.getProperty( "os.name" ).contains( "win" ) || System.getProperty( "os.name" ).contains( "Win" ) )
{
String path = getCommandOutput( "where javac" );
if ( path == null || path.isEmpty() )
{
}
else
{
File javacFile = new File( path );
File jdkInstallationDir = javacFile.getParentFile().getParentFile();
return jdkInstallationDir.getPath();
}
}
else
{
String response = getCommandOutput( "whereis javac" );
if ( response == null )
{
}
else
{
int pathStartIndex = response.indexOf( '/' );
if ( pathStartIndex == -1 )
{
}
else
{
String path = response.substring( pathStartIndex, response.length() );
File javacFile = new File( path );
File jdkInstallationDir = javacFile.getParentFile().getParentFile();
return jdkInstallationDir.getPath();
}
}
}
return null;
}
Class ProfilingSettingsPresets;
Class CPUResultsSnapshot;
Class StackTraceSnapshotBuilder;
Class LoadedSnapshot;
Class Lookup;
Class LookupProvider;
Class ResultsSnapshot;
Class ProfilingSettings;
Method addStacktrace;
Method createSnapshot;
Method createCPUPreset;
Method save;
Constructor LoadedSnapshot_Constructor;
@Override
public void run()
{
instance = this;
try
{
String JDKpath = findJDK();
String root = StringUtils.join( new String[] { JDKpath, "lib", "visualvm" }, File.separator );
String Base = StringUtils.join( new String[] { root, "profiler", "modules", "" }, File.separator );
String BaseB = StringUtils.join( new String[] { root, "platform", "lib", "" }, File.separator );
File a = new File( Base + "org-netbeans-lib-profiler.jar" );
File b = new File( Base + "org-netbeans-lib-profiler-common.jar" );
File c = new File( Base + "org-netbeans-modules-profiler.jar" );
File d = new File( BaseB + "org-openide-util.jar" );
File e = new File( BaseB + "org-openide-util-lookup.jar" );
ClassLoader cl = URLClassLoader.newInstance( new URL[] { a.toURI().toURL(), b.toURI().toURL(), c.toURI().toURL(), d.toURI().toURL(), e.toURI().toURL() } );
ProfilingSettingsPresets = cl.loadClass( "org.netbeans.lib.profiler.common.ProfilingSettingsPresets" );
ProfilingSettings = cl.loadClass( "org.netbeans.lib.profiler.common.ProfilingSettings" );
CPUResultsSnapshot = cl.loadClass( "org.netbeans.lib.profiler.results.cpu.CPUResultsSnapshot" );
ResultsSnapshot = cl.loadClass( "org.netbeans.lib.profiler.results.ResultsSnapshot" );
StackTraceSnapshotBuilder = cl.loadClass( "org.netbeans.lib.profiler.results.cpu.StackTraceSnapshotBuilder" );
LoadedSnapshot = cl.loadClass( "org.netbeans.modules.profiler.LoadedSnapshot" );
Lookup = cl.loadClass( "org.openide.util.Lookup" );
for (Class dc : Lookup.getDeclaredClasses())
{
if ( dc.getSimpleName().equals( "Provider" ) )
LookupProvider = dc;
}
if ( LookupProvider == null )
throw new ClassNotFoundException( "Lookup.Provider" );
addStacktrace = StackTraceSnapshotBuilder.getMethod( "addStacktrace", ThreadInfo[].class, long.class );
createSnapshot = StackTraceSnapshotBuilder.getMethod( "createSnapshot", long.class );
createCPUPreset = ProfilingSettingsPresets.getMethod( "createCPUPreset" );
save = LoadedSnapshot.getMethod( "save", DataOutputStream.class );
LoadedSnapshot_Constructor = LoadedSnapshot.getConstructor( ResultsSnapshot, ProfilingSettings, File.class, LookupProvider );
}
catch (Throwable t)
{
isRunning = false;
AELog.info( "Unable to find/load JDK, profiling disabled." );
return;
}
int limit = 0;
while (isRunning)
{
if ( profile )
{
limit = 9000;
try
{
Object StackTraceSnapshotBuilder_Instance = StackTraceSnapshotBuilder.newInstance();
ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();
while (profile)
{
if ( limit-- < 0 )
profile = false;
for (long th : mxBean.getAllThreadIds())
{
ThreadInfo ti = mxBean.getThreadInfo( th, Integer.MAX_VALUE );
addStacktrace.invoke( StackTraceSnapshotBuilder_Instance, new ThreadInfo[] { ti }, System.nanoTime() );
}
Thread.sleep( 20 );
}
Object CPUResultsSnapshot_Instance = createSnapshot.invoke( StackTraceSnapshotBuilder_Instance, System.currentTimeMillis() );
Object LoadedSnapshot_Instance = LoadedSnapshot_Constructor.newInstance( CPUResultsSnapshot_Instance, createCPUPreset.invoke( ProfilingSettingsPresets ), null,
null );
FileOutputStream bout = new FileOutputStream( new File( "ae-latest-profile.nps" ) );
DataOutputStream out = new DataOutputStream( bout );
save.invoke( LoadedSnapshot_Instance, out );
out.flush();
bout.close();
}
catch (Throwable t)
{
AELog.severe( "Error while profiling" );
AELog.error( t );
profile = false;
return;
}
}
else
{
try
{
synchronized (lock)
{
lock.wait();
}
}
catch (InterruptedException e)
{
}
}
}
}
}