Skip to content

Commit

Permalink
Refactored CompileChainWalker + fixed bug + test
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswhocodes committed Aug 14, 2015
1 parent 9c3ce34 commit 23d69b6
Show file tree
Hide file tree
Showing 25 changed files with 719 additions and 365 deletions.
102 changes: 40 additions & 62 deletions src/main/java/org/adoptopenjdk/jitwatch/chain/CompileChainWalker.java
Expand Up @@ -8,7 +8,7 @@
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.ATTR_ID;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.ATTR_METHOD;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.ATTR_REASON;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.DEBUG_LOGGING;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.ATTR_COMPILE_ID;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.TAG_BC;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.TAG_CALL;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.TAG_INLINE_FAIL;
Expand All @@ -21,13 +21,12 @@

import org.adoptopenjdk.jitwatch.journal.IJournalVisitable;
import org.adoptopenjdk.jitwatch.journal.JournalUtil;
import org.adoptopenjdk.jitwatch.model.IMetaMember;
import org.adoptopenjdk.jitwatch.model.IParseDictionary;
import org.adoptopenjdk.jitwatch.model.IReadOnlyJITDataModel;
import org.adoptopenjdk.jitwatch.model.Journal;
import org.adoptopenjdk.jitwatch.model.LogParseException;
import org.adoptopenjdk.jitwatch.model.Tag;
import org.adoptopenjdk.jitwatch.util.InlineUtil;
import org.adoptopenjdk.jitwatch.util.ParseUtil;
import org.adoptopenjdk.jitwatch.util.TooltipUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -39,26 +38,18 @@ public class CompileChainWalker implements IJournalVisitable

private CompileNode root = null;

private IMetaMember metaMember = null;

public CompileChainWalker(IReadOnlyJITDataModel model)
{
this.model = model;
}

public CompileNode buildCallTree(IMetaMember metaMember)
public CompileNode buildCallTree(Journal journal)
{
if (DEBUG_LOGGING)
{
logger.debug("buildCallTree: {}", metaMember.toStringUnqualifiedMethodName(false));
}

this.root = null;
this.metaMember = metaMember;

try
{
JournalUtil.visitParseTagsOfLastTask(metaMember, this);
JournalUtil.visitParseTagsOfLastTask(journal, this);
}
catch (LogParseException lpe)
{
Expand All @@ -71,21 +62,15 @@ public CompileNode buildCallTree(IMetaMember metaMember)
private void processParseTag(Tag parseTag, CompileNode parentNode, IParseDictionary parseDictionary)
{
String methodID = null;
boolean inlined = false;
String inlineReason = null;
CompileNode lastNode = null;

Map<String, String> methodAttrs = new HashMap<>();
Map<String, String> callAttrs = new HashMap<>();

// TODO - this switch code is appearing a lot
// should probably refactor with an interface
// or visitor pattern
for (Tag child : parseTag.getChildren())
{
String tagName = child.getName();
Map<String, String> tagAttrs = child.getAttrs();

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

switch (tagName)
{
Expand All @@ -98,7 +83,6 @@ private void processParseTag(Tag parseTag, CompileNode parentNode, IParseDiction
case TAG_METHOD:
{
methodID = tagAttrs.get(ATTR_ID);
inlined = false; // reset
methodAttrs.clear();
methodAttrs.putAll(tagAttrs);
}
Expand All @@ -107,68 +91,46 @@ private void processParseTag(Tag parseTag, CompileNode parentNode, IParseDiction
case TAG_CALL:
{
methodID = tagAttrs.get(ATTR_METHOD);
inlined = false;
callAttrs.clear();
callAttrs.putAll(tagAttrs);
}
break;

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

inlined = false; // reset

IMetaMember childCall = ParseUtil.lookupMember(methodID, parseDictionary, model);

if (childCall != null)
{
CompileNode childNode = new CompileNode(childCall, methodID);
parentNode.addChild(childNode);

String reason = tagAttrs.get(ATTR_REASON);
String annotationText = InlineUtil.buildInlineAnnotationText(false, reason, callAttrs, methodAttrs, parseDictionary);
childNode.setInlined(inlined, annotationText);
}
else
{
logger.error("TAG_INLINE_FAIL Failed to create CompileNode with null member. Method was {}", methodID);
}

handleInline(parentNode, methodID, parseDictionary, false, methodAttrs, callAttrs, tagAttrs);
methodID = null;
lastNode = null;
}
break;

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

case TAG_PARSE: // call depth
{
String childMethodID = tagAttrs.get(ATTR_METHOD);

IMetaMember childCall = ParseUtil.lookupMember(childMethodID, parseDictionary, model);
CompileNode nextParent = parentNode;

if (childCall != null)
if (lastNode != null)
{
CompileNode childNode = new CompileNode(childCall, childMethodID);
nextParent = lastNode;
}
else if (child.getNamedChildren(TAG_PARSE).size() > 0)
{
CompileNode childNode = new CompileNode(childMethodID);

parentNode.addChild(childNode);

if (methodID != null && methodID.equals(childMethodID))
{
childNode.setInlined(inlined, inlineReason);
}

processParseTag(child, childNode, parseDictionary);
}
else
{
logger.error("TAG_PARSE Failed to create CompileNode with null member. Method was {}", childMethodID);
nextParent = childNode;
}

processParseTag(child, nextParent, parseDictionary);

}
break;

Expand All @@ -178,17 +140,33 @@ private void processParseTag(Tag parseTag, CompileNode parentNode, IParseDiction
}
}

private CompileNode handleInline(CompileNode parentNode, String methodID, IParseDictionary parseDictionary, boolean inlined,
Map<String, String> methodAttrs, Map<String, String> callAttrs, Map<String, String> tagAttrs)
{
CompileNode childNode = new CompileNode(methodID);
parentNode.addChild(childNode);

String reason = tagAttrs.get(ATTR_REASON);
String tooltip = TooltipUtil.buildInlineAnnotationText(inlined, reason, callAttrs, methodAttrs, parseDictionary);

childNode.setInlined(inlined);
childNode.setCompiled(methodAttrs.containsKey(ATTR_COMPILE_ID));
childNode.setTooltipText(tooltip);

return childNode;
}

@Override
public void visitTag(Tag parseTag, IParseDictionary parseDictionary) throws LogParseException
{
String id = parseTag.getAttribute(ATTR_METHOD);
String methodID = parseTag.getAttribute(ATTR_METHOD);

// only initialise on first parse tag.
// there may be multiple if late_inline
// is detected
if (root == null)
{
root = new CompileNode(metaMember, id);
root = CompileNode.createRootNode(methodID, parseDictionary, model);
}

processParseTag(parseTag, root, parseDictionary);
Expand Down
83 changes: 66 additions & 17 deletions src/main/java/org/adoptopenjdk/jitwatch/chain/CompileNode.java
Expand Up @@ -9,25 +9,38 @@
import java.util.List;

import org.adoptopenjdk.jitwatch.model.IMetaMember;
import org.adoptopenjdk.jitwatch.model.IParseDictionary;
import org.adoptopenjdk.jitwatch.model.IReadOnlyJITDataModel;
import org.adoptopenjdk.jitwatch.util.ParseUtil;

import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.*;

public class CompileNode
{
private IMetaMember member;

{
private boolean compiled = false;
private boolean inlined = false;
private String inlineReason = null;
private String tooltip;

private List<CompileNode> children;

private CompileNode parent = null;

private String methodID = null;

public CompileNode(IMetaMember member, String methodID)
private IParseDictionary parseDictionary;
private IReadOnlyJITDataModel model;

public static CompileNode createRootNode(String methodID, IParseDictionary parseDictionary, IReadOnlyJITDataModel model)
{
CompileNode root = new CompileNode(methodID);
root.parseDictionary = parseDictionary;
root.model = model;

return root;
}

public CompileNode(String methodID)
{
this.member = member;
this.methodID = methodID;

children = new ArrayList<>();
Expand All @@ -38,25 +51,24 @@ public String getMethodID()
return methodID;
}

public IMetaMember getMember()
public void setInlined(boolean inlined)
{
return member;
this.inlined = inlined;
}

public void setInlined(boolean inlined, String reason)
public void setCompiled(boolean compiled)
{
this.inlined = inlined;
this.inlineReason = reason;
this.compiled = compiled;
}

public boolean isInlined()
{
return inlined;
}

public String getInlineReason()
public boolean isCompiled()
{
return inlineReason;
return compiled;
}

public void addChild(CompileNode child)
Expand All @@ -75,6 +87,17 @@ public CompileNode getParent()
return parent;
}

public void setTooltipText(String tooltip)
{
this.tooltip = tooltip;
}


public String getTooltipText()
{
return tooltip;
}

@Override
public String toString()
{
Expand All @@ -84,10 +107,36 @@ public String toString()

return builder.toString();
}

private CompileNode getRoot()
{
if (parent == null)
{
return this;
}
else
{
return getParent().getRoot();
}
}

public IMetaMember getMember()
{
CompileNode root = getRoot();

return ParseUtil.lookupMember(methodID, root.parseDictionary, root.model);
}

public String getMemberName()
{
CompileNode root = getRoot();

return ParseUtil.getMethodName(methodID, root.parseDictionary);
}

private void show(CompileNode node, StringBuilder builder, int depth)
{
if (depth > 0)
if (depth >= 0)
{
for (int i = 0; i < depth; i++)
{
Expand All @@ -96,11 +145,11 @@ private void show(CompileNode node, StringBuilder builder, int depth)

builder.append(" -> ");

builder.append(node.getMember().getMemberName());
builder.append(node.getMemberName());

builder.append("[");
builder.append(" [");

if (node.getMember().isCompiled())
if (node.isCompiled())
{
builder.append("C");
}
Expand Down
Expand Up @@ -42,13 +42,16 @@ public Map<String, String> findIntrinsics(IMetaMember member)
{
result = new HashMap<>();

try
if (member != null)
{
JournalUtil.visitParseTagsOfLastTask(member, this);
}
catch (LogParseException e)
{
logger.error("Error while finding intrinsics", e);
try
{
JournalUtil.visitParseTagsOfLastTask(member.getJournal(), this);
}
catch (LogParseException e)
{
logger.error("Error while finding intrinsics for member {}", member, e);
}
}

return result;
Expand Down
Expand Up @@ -48,15 +48,15 @@ public void reset()
@Override
public void visit(IMetaMember metaMember)
{
if (metaMember.isCompiled())
if (metaMember != null && metaMember.isCompiled())
{
try
{
JournalUtil.visitParseTagsOfLastTask(metaMember, this);
JournalUtil.visitParseTagsOfLastTask(metaMember.getJournal(), this);
}
catch (LogParseException e)
{
logger.error("Could not build histo", e);
logger.error("Could not build histo for {}", metaMember.getMemberName(), e);
}
}
}
Expand Down

0 comments on commit 23d69b6

Please sign in to comment.