diff options
author | Luck <git@lucko.me> | 2018-11-02 22:12:00 +0000 |
---|---|---|
committer | Luck <git@lucko.me> | 2018-11-02 22:12:00 +0000 |
commit | 7a60c09a9783f25d297aad063e087354b95aaa21 (patch) | |
tree | 9ab42f0104f3d54c89b5dbe3894fc0dd76278812 /spark-common/src/main/java/me/lucko/spark/sampler/node | |
parent | bd5056787953c0d59a3bab133d1c4eba7e2d398e (diff) | |
download | spark-7a60c09a9783f25d297aad063e087354b95aaa21.tar.gz spark-7a60c09a9783f25d297aad063e087354b95aaa21.tar.bz2 spark-7a60c09a9783f25d297aad063e087354b95aaa21.zip |
Add --include-line-numbers flag (#6)
Diffstat (limited to 'spark-common/src/main/java/me/lucko/spark/sampler/node')
-rw-r--r-- | spark-common/src/main/java/me/lucko/spark/sampler/node/AbstractNode.java | 39 | ||||
-rw-r--r-- | spark-common/src/main/java/me/lucko/spark/sampler/node/StackTraceNode.java | 20 |
2 files changed, 44 insertions, 15 deletions
diff --git a/spark-common/src/main/java/me/lucko/spark/sampler/node/AbstractNode.java b/spark-common/src/main/java/me/lucko/spark/sampler/node/AbstractNode.java index e660140..75632c4 100644 --- a/spark-common/src/main/java/me/lucko/spark/sampler/node/AbstractNode.java +++ b/spark-common/src/main/java/me/lucko/spark/sampler/node/AbstractNode.java @@ -53,30 +53,47 @@ public abstract class AbstractNode { return this.totalTime.longValue(); } - private AbstractNode resolveChild(String className, String methodName) { + private AbstractNode resolveChild(String className, String methodName, int lineNumber) { return this.children.computeIfAbsent( - StackTraceNode.generateKey(className, methodName), - name -> new StackTraceNode(className, methodName) + StackTraceNode.generateKey(className, methodName, lineNumber), + name -> new StackTraceNode(className, methodName, lineNumber) ); } - public void log(StackTraceElement[] elements, long time) { - log(elements, 0, time); + public void log(StackTraceElement[] elements, long time, boolean includeLineNumbers) { + log(elements, 0, time, includeLineNumbers); } - private void log(StackTraceElement[] elements, int skip, long time) { + private void log(StackTraceElement[] elements, int offset, long time, boolean includeLineNumbers) { this.totalTime.add(time); - if (skip >= MAX_STACK_DEPTH) { + if (offset >= MAX_STACK_DEPTH) { return; } - if (elements.length - skip == 0) { + if (elements.length - offset == 0) { return; } - - StackTraceElement bottom = elements[elements.length - (skip + 1)]; - resolveChild(bottom.getClassName(), bottom.getMethodName()).log(elements, skip + 1, time); + + // the first element in the array is the top of the call stack, and the last is the root + // offset starts at 0. + + // pointer is determined by subtracting the offset from the index of the last element + int pointer = (elements.length - 1) - offset; + StackTraceElement element = elements[pointer]; + + // the parent stack element is located at pointer+1. + // when the current offset is 0, we know the current pointer is at the last element in the + // array (the root) and therefore there is no parent. + StackTraceElement parent = offset == 0 ? null : elements[pointer + 1]; + + // get the line number of the parent element - the line which called "us" + int lineNumber = parent == null || !includeLineNumbers ? StackTraceNode.NULL_LINE_NUMBER : parent.getLineNumber(); + + // resolve a child element within the structure for the element at pointer + AbstractNode child = resolveChild(element.getClassName(), element.getMethodName(), lineNumber); + // call the log method on the found child, with an incremented offset. + child.log(elements, offset + 1, time, includeLineNumbers); } private Collection<? extends AbstractNode> getChildren() { diff --git a/spark-common/src/main/java/me/lucko/spark/sampler/node/StackTraceNode.java b/spark-common/src/main/java/me/lucko/spark/sampler/node/StackTraceNode.java index d161b42..ee9e985 100644 --- a/spark-common/src/main/java/me/lucko/spark/sampler/node/StackTraceNode.java +++ b/spark-common/src/main/java/me/lucko/spark/sampler/node/StackTraceNode.java @@ -31,34 +31,46 @@ import java.io.IOException; public final class StackTraceNode extends AbstractNode implements Comparable<StackTraceNode> { /** + * Magic number to denote "no present" line number for a node. + */ + public static final int NULL_LINE_NUMBER = -1; + + /** * Forms a key to represent the given node. * * @param className the name of the class * @param methodName the name of the method + * @param lineNumber the line number of the parent method call * @return the key */ - static String generateKey(String className, String methodName) { - return className + "." + methodName; + static String generateKey(String className, String methodName, int lineNumber) { + return className + "." + methodName + "." + lineNumber; } /** The name of the class */ private final String className; /** The name of the method */ private final String methodName; + /** The line number of the invocation which created this node */ + private final int lineNumber; - public StackTraceNode(String className, String methodName) { + public StackTraceNode(String className, String methodName, int lineNumber) { this.className = className; this.methodName = methodName; + this.lineNumber = lineNumber; } @Override protected void appendMetadata(JsonWriter writer) throws IOException { writer.name("cl").value(this.className); writer.name("m").value(this.methodName); + if (this.lineNumber != NULL_LINE_NUMBER) { + writer.name("ln").value(this.lineNumber); + } } private String key() { - return generateKey(this.className, this.methodName); + return generateKey(this.className, this.methodName, this.lineNumber); } @Override |