Skip to content

Commit

Permalink
Uncommon trap detection
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswhocodes committed Aug 12, 2015
1 parent 93acbe0 commit e240852
Show file tree
Hide file tree
Showing 16 changed files with 701 additions and 931 deletions.
Expand Up @@ -84,6 +84,8 @@ private void processParseTag(Tag parseTag, CompileNode parentNode, IParseDiction
{ {
String tagName = child.getName(); String tagName = child.getName();
Map<String, String> tagAttrs = child.getAttrs(); Map<String, String> tagAttrs = child.getAttrs();

//System.out.println("CCW: " + child.toString(false));


switch (tagName) switch (tagName)
{ {
Expand Down Expand Up @@ -113,6 +115,8 @@ private void processParseTag(Tag parseTag, CompileNode parentNode, IParseDiction


case TAG_INLINE_FAIL: case TAG_INLINE_FAIL:
{ {
//System.out.println("INLINE FAIL!");

inlined = false; // reset inlined = false; // reset


IMetaMember childCall = ParseUtil.lookupMember(methodID, parseDictionary, model); IMetaMember childCall = ParseUtil.lookupMember(methodID, parseDictionary, model);
Expand All @@ -123,7 +127,7 @@ private void processParseTag(Tag parseTag, CompileNode parentNode, IParseDiction
parentNode.addChild(childNode); parentNode.addChild(childNode);


String reason = tagAttrs.get(ATTR_REASON); String reason = tagAttrs.get(ATTR_REASON);
String annotationText = InlineUtil.buildInlineAnnotationText(false, reason, callAttrs, methodAttrs); String annotationText = InlineUtil.buildInlineAnnotationText(false, reason, callAttrs, methodAttrs, parseDictionary);
childNode.setInlined(inlined, annotationText); childNode.setInlined(inlined, annotationText);
} }
else else
Expand All @@ -136,9 +140,10 @@ private void processParseTag(Tag parseTag, CompileNode parentNode, IParseDiction
break; break;


case TAG_INLINE_SUCCESS: case TAG_INLINE_SUCCESS:
//System.out.println("INLINE SUCCESS!");
inlined = true; inlined = true;
String reason = tagAttrs.get(ATTR_REASON); String reason = tagAttrs.get(ATTR_REASON);
inlineReason = InlineUtil.buildInlineAnnotationText(true, reason, callAttrs, methodAttrs); inlineReason = InlineUtil.buildInlineAnnotationText(true, reason, callAttrs, methodAttrs, parseDictionary);
break; break;


case TAG_PARSE: // call depth case TAG_PARSE: // call depth
Expand Down
Expand Up @@ -93,6 +93,7 @@ private JITWatchConstants()
public static final String TAG_ELIMINATE_ALLOCATION = "eliminate_allocation"; public static final String TAG_ELIMINATE_ALLOCATION = "eliminate_allocation";
public static final String TAG_ELIMINATE_LOCK = "eliminate_lock"; public static final String TAG_ELIMINATE_LOCK = "eliminate_lock";
public static final String TAG_JVMS = "jvms"; public static final String TAG_JVMS = "jvms";
public static final String TAG_UNCOMMON_TRAP = "uncommon_trap";


public static final String TAG_COMMAND = "command"; public static final String TAG_COMMAND = "command";


Expand Down Expand Up @@ -129,6 +130,8 @@ private JITWatchConstants()
public static final String ATTR_BRANCH_PROB = "prob"; public static final String ATTR_BRANCH_PROB = "prob";
public static final String ATTR_COUNT = "count"; public static final String ATTR_COUNT = "count";
public static final String ATTR_PROF_FACTOR = "prof_factor"; public static final String ATTR_PROF_FACTOR = "prof_factor";
public static final String ATTR_ACTION = "action";
public static final String ATTR_COMMENT = "comment";


public static final String ALWAYS = "always"; public static final String ALWAYS = "always";
public static final String NEVER = "never"; public static final String NEVER = "never";
Expand Down
Expand Up @@ -139,9 +139,11 @@ private static ClassBC parsedByteCodeFrom(String fqClassName, String byteCodeStr


if (byteCodeString != null) if (byteCodeString != null)
{ {
String[] bytecodeLines = byteCodeString.split(S_NEWLINE);

try try
{ {
result = parse(fqClassName, byteCodeString); result = parse(fqClassName, bytecodeLines);
} }
catch (Exception ex) catch (Exception ex)
{ {
Expand Down Expand Up @@ -203,12 +205,10 @@ private static String[] buildClassPathFromClassLocations(Collection<String> clas
} }


// TODO refactor this class - better stateful than all statics // TODO refactor this class - better stateful than all statics
public static ClassBC parse(String fqClassName, String bytecode) public static ClassBC parse(String fqClassName, String[] bytecodeLines)
{ {
ClassBC classBytecode = new ClassBC(fqClassName); ClassBC classBytecode = new ClassBC(fqClassName);


String[] lines = bytecode.split(S_NEWLINE);

int pos = 0; int pos = 0;


StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
Expand All @@ -219,9 +219,9 @@ public static ClassBC parse(String fqClassName, String bytecode)


MemberBytecode memberBytecode = null; MemberBytecode memberBytecode = null;


while (pos < lines.length) while (pos < bytecodeLines.length)
{ {
String line = lines[pos].trim(); String line = bytecodeLines[pos].trim();


if (DEBUG_LOGGING_BYTECODE) if (DEBUG_LOGGING_BYTECODE)
{ {
Expand All @@ -237,9 +237,9 @@ public static ClassBC parse(String fqClassName, String bytecode)
section = changeSection(nextSection); section = changeSection(nextSection);
pos++; pos++;


if (pos < lines.length) if (pos < bytecodeLines.length)
{ {
line = lines[pos].trim(); line = bytecodeLines[pos].trim();
} }
} }


Expand Down
Expand Up @@ -41,6 +41,9 @@
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;


import org.adoptopenjdk.jitwatch.model.assembly.AssemblyMethod; import org.adoptopenjdk.jitwatch.model.assembly.AssemblyMethod;
import org.adoptopenjdk.jitwatch.model.bytecode.BytecodeInstruction;
import org.adoptopenjdk.jitwatch.model.bytecode.ClassBC;
import org.adoptopenjdk.jitwatch.model.bytecode.MemberBytecode;
import org.adoptopenjdk.jitwatch.util.ParseUtil; import org.adoptopenjdk.jitwatch.util.ParseUtil;
import org.adoptopenjdk.jitwatch.util.StringUtil; import org.adoptopenjdk.jitwatch.util.StringUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
Expand Down Expand Up @@ -244,6 +247,43 @@ public List<String> getQueuedAttributes()


return attrList; return attrList;
} }

@Override
public MemberBytecode getMemberBytecode()
{
MemberBytecode result = null;

if (metaClass != null)
{
ClassBC classBytecode = metaClass.getClassBytecode();

if (classBytecode != null)
{
result = classBytecode.getMemberBytecode(this);
}
}

return result;
}

public List<BytecodeInstruction> getInstructions()
{
List<BytecodeInstruction> result = null;

MemberBytecode memberBytecode = getMemberBytecode();

if (memberBytecode != null)
{
result = memberBytecode.getInstructions();
}
else
{
result = new ArrayList<>();
}

return result;
}



@Override @Override
public MetaClass getMetaClass() public MetaClass getMetaClass()
Expand Down
Expand Up @@ -9,12 +9,18 @@
import java.util.Map; import java.util.Map;


import org.adoptopenjdk.jitwatch.model.assembly.AssemblyMethod; import org.adoptopenjdk.jitwatch.model.assembly.AssemblyMethod;
import org.adoptopenjdk.jitwatch.model.bytecode.BytecodeInstruction;
import org.adoptopenjdk.jitwatch.model.bytecode.MemberBytecode;


public interface IMetaMember public interface IMetaMember
{ {
List<String> getQueuedAttributes(); List<String> getQueuedAttributes();


MetaClass getMetaClass(); MetaClass getMetaClass();

MemberBytecode getMemberBytecode();

List<BytecodeInstruction> getInstructions();


void addJournalEntry(Tag entry); void addJournalEntry(Tag entry);
Journal getJournal(); Journal getJournal();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/adoptopenjdk/jitwatch/model/MetaClass.java
Expand Up @@ -115,6 +115,11 @@ public ClassBC getClassBytecode(IReadOnlyJITDataModel model, List<String> classL


return classBytecode; return classBytecode;
} }

public ClassBC getClassBytecode()
{
return classBytecode;
}


private void loadInnerClasses(List<String> innerClassNames, IReadOnlyJITDataModel model, List<String> classLocations) private void loadInnerClasses(List<String> innerClassNames, IReadOnlyJITDataModel model, List<String> classLocations)
{ {
Expand Down
Expand Up @@ -2,5 +2,5 @@


public enum BCAnnotationType public enum BCAnnotationType
{ {
BRANCH, INLINE_SUCCESS, INLINE_FAIL, ELIMINATED_ALLOCATION, LOCK_ELISION, LOCK_COARSEN, INTRINSIC_USED; BRANCH, INLINE_SUCCESS, INLINE_FAIL, ELIMINATED_ALLOCATION, LOCK_ELISION, LOCK_COARSEN, INTRINSIC_USED, UNCOMMON_TRAP;
} }
Expand Up @@ -32,6 +32,7 @@
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.TAG_JVMS; import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.TAG_JVMS;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.TAG_METHOD; import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.TAG_METHOD;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.TAG_PARSE; import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.TAG_PARSE;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.TAG_UNCOMMON_TRAP;


import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
Expand All @@ -58,17 +59,14 @@ public class BytecodeAnnotationBuilder implements IJournalVisitable


private IMetaMember member; private IMetaMember member;


private List<BytecodeInstruction> instructions;

private IReadOnlyJITDataModel model; private IReadOnlyJITDataModel model;


private BytecodeAnnotations bcAnnotations = new BytecodeAnnotations(); private BytecodeAnnotations bcAnnotations = new BytecodeAnnotations();


public BytecodeAnnotations buildBytecodeAnnotations(final IMetaMember member, final List<BytecodeInstruction> instructions, public BytecodeAnnotations buildBytecodeAnnotations(final IMetaMember member, IReadOnlyJITDataModel model)
IReadOnlyJITDataModel model) throws AnnotationException throws AnnotationException
{ {
this.member = member; this.member = member;
this.instructions = instructions;
this.model = model; this.model = model;


bcAnnotations.clear(); bcAnnotations.clear();
Expand Down Expand Up @@ -120,7 +118,6 @@ public void visitTag(Tag tag, IParseDictionary parseDictionary) throws LogParseE
case TAG_ELIMINATE_LOCK: case TAG_ELIMINATE_LOCK:
visitTagEliminateLock(tag, parseDictionary); visitTagEliminateLock(tag, parseDictionary);
break; break;

} }
} }


Expand All @@ -134,7 +131,7 @@ private void visitTagParse(Tag tag, IParseDictionary parseDictionary) throws Log
{ {
final CompilerName compilerName = JournalUtil.getCompilerNameForLastTask(member.getJournal()); final CompilerName compilerName = JournalUtil.getCompilerNameForLastTask(member.getJournal());


buildParseTagAnnotations(tag, bcAnnotations, instructions, compilerName); buildParseTagAnnotations(tag, bcAnnotations, compilerName, parseDictionary);
} }
catch (Exception e) catch (Exception e)
{ {
Expand All @@ -144,7 +141,7 @@ private void visitTagParse(Tag tag, IParseDictionary parseDictionary) throws Log
} }


private void visitTagEliminateAllocation(Tag tag, IParseDictionary parseDictionary) private void visitTagEliminateAllocation(Tag tag, IParseDictionary parseDictionary)
{ {
List<Tag> childrenJVMS = tag.getNamedChildren(TAG_JVMS); List<Tag> childrenJVMS = tag.getNamedChildren(TAG_JVMS);


for (Tag tagJVMS : childrenJVMS) for (Tag tagJVMS : childrenJVMS)
Expand All @@ -157,7 +154,7 @@ private void visitTagEliminateAllocation(Tag tag, IParseDictionary parseDictiona
{ {
int bciValue = Integer.parseInt(bci); int bciValue = Integer.parseInt(bci);


BytecodeInstruction instr = getInstructionAtIndex(instructions, bciValue); BytecodeInstruction instr = getInstructionAtIndex(bciValue);


if (instr != null) if (instr != null)
{ {
Expand All @@ -179,8 +176,8 @@ private void visitTagEliminateAllocation(Tag tag, IParseDictionary parseDictiona
} }
} }


bcAnnotations.addAnnotation(bciValue, bcAnnotations.addAnnotation(bciValue, new LineAnnotation(builder.toString(),
new LineAnnotation(builder.toString(), BCAnnotationType.ELIMINATED_ALLOCATION)); BCAnnotationType.ELIMINATED_ALLOCATION));


if (instr.getOpcode() == Opcode.NEW) if (instr.getOpcode() == Opcode.NEW)
{ {
Expand Down Expand Up @@ -256,10 +253,10 @@ private void visitTagEliminateLock(Tag tag, IParseDictionary parseDictionary)


if (bciValue != -1) if (bciValue != -1)
{ {
bcAnnotations.addAnnotation(bciValue, bcAnnotations.addAnnotation(bciValue, new LineAnnotation(builder.toString().trim(),
new LineAnnotation(builder.toString().trim(), BCAnnotationType.LOCK_ELISION)); BCAnnotationType.LOCK_ELISION));


BytecodeInstruction instr = getInstructionAtIndex(instructions, bciValue); BytecodeInstruction instr = getInstructionAtIndex(bciValue);


if (instr != null && instr.isLock()) if (instr != null && instr.isLock())
{ {
Expand All @@ -276,8 +273,18 @@ private void visitTagEliminateLock(Tag tag, IParseDictionary parseDictionary)
} }
} }


private void buildParseTagAnnotations(Tag parseTag, BytecodeAnnotations annotations, List<BytecodeInstruction> instructions, private void visitTagUncommonTrap(Tag tag)
CompilerName compilerName) throws AnnotationException {
UncommonTrap trap = UncommonTrap.parse(tag);

if (trap != null)
{
bcAnnotations.addAnnotation(trap.getBCI(), new LineAnnotation(trap.toString(), BCAnnotationType.UNCOMMON_TRAP));
}
}

private void buildParseTagAnnotations(Tag parseTag, BytecodeAnnotations annotations, CompilerName compilerName, IParseDictionary parseDictionary)
throws AnnotationException
{ {
if (DEBUG_LOGGING) if (DEBUG_LOGGING)
{ {
Expand Down Expand Up @@ -339,7 +346,7 @@ private void buildParseTagAnnotations(Tag parseTag, BytecodeAnnotations annotati
logger.debug("BC Tag {} {}", currentBytecode, code); logger.debug("BC Tag {} {}", currentBytecode, code);
} }


currentInstruction = getInstructionAtIndex(instructions, currentBytecode); currentInstruction = getInstructionAtIndex(currentBytecode);


if (DEBUG_LOGGING_BYTECODE) if (DEBUG_LOGGING_BYTECODE)
{ {
Expand Down Expand Up @@ -393,7 +400,7 @@ private void buildParseTagAnnotations(Tag parseTag, BytecodeAnnotations annotati
} }


String reason = tagAttrs.get(ATTR_REASON); String reason = tagAttrs.get(ATTR_REASON);
String annotationText = InlineUtil.buildInlineAnnotationText(true, reason, callAttrs, methodAttrs); String annotationText = InlineUtil.buildInlineAnnotationText(true, reason, callAttrs, methodAttrs, parseDictionary);


bcAnnotations.addAnnotation(currentBytecode, bcAnnotations.addAnnotation(currentBytecode,
new LineAnnotation(annotationText, BCAnnotationType.INLINE_SUCCESS)); new LineAnnotation(annotationText, BCAnnotationType.INLINE_SUCCESS));
Expand All @@ -411,7 +418,7 @@ private void buildParseTagAnnotations(Tag parseTag, BytecodeAnnotations annotati
} }


String reason = tagAttrs.get(ATTR_REASON); String reason = tagAttrs.get(ATTR_REASON);
String annotationText = InlineUtil.buildInlineAnnotationText(false, reason, callAttrs, methodAttrs); String annotationText = InlineUtil.buildInlineAnnotationText(false, reason, callAttrs, methodAttrs, parseDictionary);


bcAnnotations.addAnnotation(currentBytecode, new LineAnnotation(annotationText, BCAnnotationType.INLINE_FAIL)); bcAnnotations.addAnnotation(currentBytecode, new LineAnnotation(annotationText, BCAnnotationType.INLINE_FAIL));
} }
Expand All @@ -425,7 +432,7 @@ private void buildParseTagAnnotations(Tag parseTag, BytecodeAnnotations annotati
{ {
if (!sanityCheckBranch(currentInstruction)) if (!sanityCheckBranch(currentInstruction))
{ {
throw new AnnotationException("Expected a branch instruction (in BRANCH)", currentBytecode, throw new AnnotationException("Expected a branch instruction (BRANCH)", currentBytecode,
currentInstruction); currentInstruction);
} }


Expand All @@ -442,24 +449,23 @@ private void buildParseTagAnnotations(Tag parseTag, BytecodeAnnotations annotati
{ {
if (!sanityCheckIntrinsic(currentInstruction)) if (!sanityCheckIntrinsic(currentInstruction))
{ {
for (BytecodeInstruction ins : instructions) throw new AnnotationException("Expected an invoke instruction (INTRINSIC)", currentBytecode,
{
logger.info("! instruction: {}", ins);
}

throw new AnnotationException("Expected an invoke instruction (IN INTRINSIC)", currentBytecode,
currentInstruction); currentInstruction);
} }


StringBuilder reason = new StringBuilder(); StringBuilder reason = new StringBuilder();
reason.append("Intrinsic: ").append(tagAttrs.get(ATTR_ID)); reason.append("Intrinsic: ").append(tagAttrs.get(ATTR_ID));


bcAnnotations.addAnnotation(currentBytecode, bcAnnotations.addAnnotation(currentBytecode, new LineAnnotation(reason.toString(),
new LineAnnotation(reason.toString(), BCAnnotationType.INTRINSIC_USED)); BCAnnotationType.INTRINSIC_USED));
} }
} }
break; break;


case TAG_UNCOMMON_TRAP:
visitTagUncommonTrap(child);
break;

default: default:
break; break;
} }
Expand Down Expand Up @@ -527,11 +533,11 @@ public static boolean sanityCheckBranch(BytecodeInstruction instr)
return sane; return sane;
} }


private BytecodeInstruction getInstructionAtIndex(List<BytecodeInstruction> instructions, int index) private BytecodeInstruction getInstructionAtIndex(int index)
{ {
BytecodeInstruction found = null; BytecodeInstruction found = null;


for (BytecodeInstruction instruction : instructions) for (BytecodeInstruction instruction : member.getInstructions())
{ {
if (instruction.getOffset() == index) if (instruction.getOffset() == index)
{ {
Expand Down

0 comments on commit e240852

Please sign in to comment.