Skip to content

Commit

Permalink
Merge branch 'feature/profiler'
Browse files Browse the repository at this point in the history
  • Loading branch information
Mumfrey committed Aug 21, 2017
2 parents ad79bf9 + e93b809 commit 6fe9f0a
Show file tree
Hide file tree
Showing 11 changed files with 1,156 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ public final void injectIntoClassLoader(LaunchClassLoader classLoader) {
* mixin tweaker in their manifest) and add agents for them
*/
private void scanClasspath() {
for (URL url : Launch.classLoader.getSources()) {
URL[] sources = Launch.classLoader.getSources().toArray(new URL[0]);
for (URL url : sources) {
try {
URI uri = url.toURI();
if (this.containers.containsKey(uri)) {
Expand Down
29 changes: 26 additions & 3 deletions src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.spongepowered.asm.util.ITokenProvider;
import org.spongepowered.asm.util.JavaVersion;
import org.spongepowered.asm.util.PrettyPrinter;
import org.spongepowered.asm.util.perf.Profiler;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
Expand Down Expand Up @@ -320,6 +321,12 @@ public static enum Option {
* Enable strict checking for mixin targets
*/
DEBUG_TARGETS(Option.DEBUG_STRICT, "targets"),

/**
* Enable the performance profiler for all mixin operations (normally it
* is only enabled during mixin prepare operations)
*/
DEBUG_PROFILER(Option.DEBUG_ALL, Inherit.ALLOW_OVERRIDE, "profiler"),

/**
* Dumps the bytecode for the target class to disk when mixin
Expand Down Expand Up @@ -857,6 +864,11 @@ public void append(LogEvent event) {
* Logger
*/
private static final Logger logger = LogManager.getLogger("mixin");

/**
* Performance profiler
*/
private static final Profiler profiler = new Profiler();

/**
* The phase for this environment
Expand Down Expand Up @@ -947,9 +959,10 @@ private void printHeader(Object version) {
String codeSource = this.getCodeSource();
MixinEnvironment.logger.info("SpongePowered MIXIN Subsystem Version={} Source={} Env={}", version, codeSource, side);

if (this.getOption(Option.DEBUG_VERBOSE)) {
boolean verbose = this.getOption(Option.DEBUG_VERBOSE);
if (verbose || this.getOption(Option.DEBUG_EXPORT) || this.getOption(Option.DEBUG_PROFILER)) {
PrettyPrinter printer = new PrettyPrinter(32);
printer.add("SpongePowered MIXIN (Verbose debugging enabled)").centre().hr();
printer.add("SpongePowered MIXIN%s", verbose ? " (Verbose debugging enabled)" : "").centre().hr();
printer.kv("Code source", codeSource);
printer.kv("Internal Version", version);
printer.kv("Java 8 Supported", CompatibilityLevel.JAVA_8.isSupported()).hr();
Expand Down Expand Up @@ -1359,7 +1372,8 @@ private static Phase getCurrentPhase() {
public static void init(Phase phase) {
if (MixinEnvironment.currentPhase == Phase.NOT_INITIALISED) {
MixinEnvironment.currentPhase = phase;
MixinEnvironment.getEnvironment(phase);
MixinEnvironment env = MixinEnvironment.getEnvironment(phase);
MixinEnvironment.getProfiler().setActive(env.getOption(Option.DEBUG_PROFILER));

@SuppressWarnings("unused")
MixinLogger logSpy = new MixinLogger();
Expand Down Expand Up @@ -1432,6 +1446,15 @@ public static void setCompatibilityLevel(CompatibilityLevel level) throws Illega
}
}

/**
* Get the performance profiler
*
* @return profiler
*/
public static Profiler getProfiler() {
return MixinEnvironment.profiler;
}

/**
* Internal callback
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
import org.spongepowered.asm.mixin.transformer.MixinInfo.MixinClassNode;
import org.spongepowered.asm.util.Annotations;
import org.spongepowered.asm.util.ClassSignature;
import org.spongepowered.asm.util.perf.Profiler;
import org.spongepowered.asm.util.perf.Profiler.Section;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -576,6 +578,8 @@ protected String getDisplayFormat() {
}

private static final Logger logger = LogManager.getLogger("mixin");

private static final Profiler profiler = MixinEnvironment.getProfiler();

private static final String JAVA_LANG_OBJECT = "java/lang/Object";

Expand Down Expand Up @@ -711,44 +715,49 @@ private ClassInfo() {
* @param classNode Class node to inspect
*/
private ClassInfo(ClassNode classNode) {
this.name = classNode.name;
this.superName = classNode.superName != null ? classNode.superName : ClassInfo.JAVA_LANG_OBJECT;
this.methods = new HashSet<Method>();
this.fields = new HashSet<Field>();
this.isInterface = ((classNode.access & Opcodes.ACC_INTERFACE) != 0);
this.interfaces = new HashSet<String>();
this.access = classNode.access;
this.isMixin = classNode instanceof MixinClassNode;
this.mixin = this.isMixin ? ((MixinClassNode)classNode).getMixin() : null;

this.interfaces.addAll(classNode.interfaces);

for (MethodNode method : classNode.methods) {
this.addMethod(method, this.isMixin);
}

boolean isProbablyStatic = true;
String outerName = classNode.outerClass;
for (FieldNode field : classNode.fields) {
if ((field.access & Opcodes.ACC_SYNTHETIC) != 0) {
if (field.name.startsWith("this$")) {
isProbablyStatic = false;
if (outerName == null) {
outerName = field.desc;
if (outerName != null && outerName.startsWith("L")) {
outerName = outerName.substring(1, outerName.length() - 1);
Section timer = ClassInfo.profiler.begin(Profiler.ROOT, "class.meta");
try {
this.name = classNode.name;
this.superName = classNode.superName != null ? classNode.superName : ClassInfo.JAVA_LANG_OBJECT;
this.methods = new HashSet<Method>();
this.fields = new HashSet<Field>();
this.isInterface = ((classNode.access & Opcodes.ACC_INTERFACE) != 0);
this.interfaces = new HashSet<String>();
this.access = classNode.access;
this.isMixin = classNode instanceof MixinClassNode;
this.mixin = this.isMixin ? ((MixinClassNode)classNode).getMixin() : null;

this.interfaces.addAll(classNode.interfaces);

for (MethodNode method : classNode.methods) {
this.addMethod(method, this.isMixin);
}

boolean isProbablyStatic = true;
String outerName = classNode.outerClass;
for (FieldNode field : classNode.fields) {
if ((field.access & Opcodes.ACC_SYNTHETIC) != 0) {
if (field.name.startsWith("this$")) {
isProbablyStatic = false;
if (outerName == null) {
outerName = field.desc;
if (outerName != null && outerName.startsWith("L")) {
outerName = outerName.substring(1, outerName.length() - 1);
}
}
}
}

this.fields.add(new Field(field, this.isMixin));
}

this.fields.add(new Field(field, this.isMixin));
this.isProbablyStatic = isProbablyStatic;
this.outerName = outerName;
this.methodMapper = new MethodMapper(MixinEnvironment.getCurrentEnvironment(), this);
this.signature = ClassSignature.ofLazy(classNode);
} finally {
timer.end();
}

this.isProbablyStatic = isProbablyStatic;
this.outerName = outerName;
this.methodMapper = new MethodMapper(MixinEnvironment.getCurrentEnvironment(), this);
this.signature = ClassSignature.ofLazy(classNode);
}

void addInterface(String iface) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,14 @@
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
import org.spongepowered.asm.mixin.transformer.ClassInfo.Method;
import org.spongepowered.asm.mixin.transformer.MixinInfo.MixinMethodNode;
import org.spongepowered.asm.util.Counter;

import com.google.common.base.Strings;

/**
* Maintains method remaps for a target class
*/
public class MethodMapper {

/**
* Mutable integer
*/
static class Counter {
public int value;
}

/**
* Logger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
import org.spongepowered.asm.util.Constants;
import org.spongepowered.asm.util.ConstraintParser;
import org.spongepowered.asm.util.ConstraintParser.Constraint;
import org.spongepowered.asm.util.perf.Profiler;
import org.spongepowered.asm.util.perf.Profiler.Section;
import org.spongepowered.asm.util.throwables.ConstraintViolationException;
import org.spongepowered.asm.util.throwables.InvalidConstraintException;

Expand Down Expand Up @@ -230,6 +232,11 @@ public String toString() {
*/
protected final ClassNode targetClass;

/**
* Profiler
*/
protected final Profiler profiler = MixinEnvironment.getProfiler();

MixinApplicatorStandard(TargetClassContext context) {
this.context = context;
this.targetName = context.getClassName();
Expand All @@ -255,9 +262,11 @@ void apply(SortedSet<MixinInfo> mixins) {
}

for (ApplicatorPass pass : ApplicatorPass.values()) {
Section timer = this.profiler.begin("pass", pass.name().toLowerCase());
for (MixinTargetContext context : mixinContexts) {
this.applyMixin(current = context, pass);
}
timer.end();
}

for (MixinTargetContext context : mixinContexts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
import org.spongepowered.asm.util.Annotations;
import org.spongepowered.asm.util.Bytecode;
import org.spongepowered.asm.util.launchwrapper.LaunchClassLoaderUtil;
import org.spongepowered.asm.util.perf.Profiler;
import org.spongepowered.asm.util.perf.Profiler.Section;

import com.google.common.base.Function;
import com.google.common.base.Functions;
Expand Down Expand Up @@ -677,6 +679,11 @@ static SubType getTypeFor(MixinInfo mixin) {
*/
private final transient Logger logger = LogManager.getLogger("mixin");

/**
* Profiler
*/
private final transient Profiler profiler = MixinEnvironment.getProfiler();

/**
* Parent configuration which declares this mixin
*/
Expand Down Expand Up @@ -875,7 +882,7 @@ private void readTargets(Collection<ClassInfo> outTargets, Collection<String> in
this.logger.error(message);
}

if (this.plugin == null || suppressPlugin || this.plugin.shouldApplyMixin(targetName, this.className)) {
if (this.shouldApplyMixin(suppressPlugin, targetName)) {
ClassInfo targetInfo = this.getTarget(targetName, checkPublic);
if (targetInfo != null && !outTargets.contains(targetInfo)) {
outTargets.add(targetInfo);
Expand All @@ -885,6 +892,13 @@ private void readTargets(Collection<ClassInfo> outTargets, Collection<String> in
}
}

private boolean shouldApplyMixin(boolean suppressPlugin, String targetName) {
Section pluginTimer = this.profiler.begin("plugin");
boolean result = this.plugin == null || suppressPlugin || this.plugin.shouldApplyMixin(targetName, this.className);
pluginTimer.end();
return result;
}

private ClassInfo getTarget(String targetName, boolean checkPublic) throws InvalidMixinException {
ClassInfo targetInfo = ClassInfo.forName(targetName);
if (targetInfo == null) {
Expand Down Expand Up @@ -1121,7 +1135,10 @@ Set<String> getInterfaces() {
*/
MixinTargetContext createContextFor(TargetClassContext target) {
MixinClassNode classNode = this.getClassNode(ClassReader.EXPAND_FRAMES);
return this.type.createPreProcessor(classNode).prepare().createContextFor(target);
Section preTimer = this.profiler.begin("pre");
MixinTargetContext preProcessor = this.type.createPreProcessor(classNode).prepare().createContextFor(target);
preTimer.end();
return preProcessor;
}

/**
Expand Down Expand Up @@ -1179,7 +1196,9 @@ public int compareTo(MixinInfo other) {
*/
public void preApply(String transformedName, ClassNode targetClass) {
if (this.plugin != null) {
Section pluginTimer = this.profiler.begin("plugin");
this.plugin.preApply(transformedName, targetClass, this.className, this);
pluginTimer.end();
}
}

Expand All @@ -1188,7 +1207,9 @@ public void preApply(String transformedName, ClassNode targetClass) {
*/
public void postApply(String transformedName, ClassNode targetClass) {
if (this.plugin != null) {
Section pluginTimer = this.profiler.begin("plugin");
this.plugin.postApply(transformedName, targetClass, this.className, this);
pluginTimer.end();
}

this.parent.postApply(transformedName, targetClass);
Expand Down
Loading

0 comments on commit 6fe9f0a

Please sign in to comment.