aboutsummaryrefslogtreecommitdiff
path: root/spark-common/src/main/java/me/lucko/spark/profiler/node
diff options
context:
space:
mode:
Diffstat (limited to 'spark-common/src/main/java/me/lucko/spark/profiler/node')
-rw-r--r--spark-common/src/main/java/me/lucko/spark/profiler/node/AbstractNode.java113
-rw-r--r--spark-common/src/main/java/me/lucko/spark/profiler/node/StackTraceNode.java78
-rw-r--r--spark-common/src/main/java/me/lucko/spark/profiler/node/ThreadNode.java24
3 files changed, 215 insertions, 0 deletions
diff --git a/spark-common/src/main/java/me/lucko/spark/profiler/node/AbstractNode.java b/spark-common/src/main/java/me/lucko/spark/profiler/node/AbstractNode.java
new file mode 100644
index 0000000..04425c6
--- /dev/null
+++ b/spark-common/src/main/java/me/lucko/spark/profiler/node/AbstractNode.java
@@ -0,0 +1,113 @@
+/*
+ * WarmRoast
+ * Copyright (C) 2013 Albert Pham <http://www.sk89q.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+package me.lucko.spark.profiler.node;
+
+import com.google.gson.stream.JsonWriter;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.LongAdder;
+
+/**
+ * Encapsulates a timed node in the sampling stack.
+ */
+public abstract class AbstractNode {
+
+ private static final int MAX_STACK_DEPTH = 300;
+
+ /**
+ * A map of this nodes children
+ */
+ private final Map<String, StackTraceNode> children = new ConcurrentHashMap<>();
+
+ /**
+ * The accumulated sample time for this node
+ */
+ private final LongAdder totalTime = new LongAdder();
+
+ public long getTotalTime() {
+ return this.totalTime.longValue();
+ }
+
+ private AbstractNode resolveChild(String className, String methodName, int lineNumber) {
+ return this.children.computeIfAbsent(
+ StackTraceNode.generateKey(className, methodName, lineNumber),
+ name -> new StackTraceNode(className, methodName, lineNumber)
+ );
+ }
+
+ public void log(StackTraceElement[] elements, long time) {
+ log(elements, 0, time);
+ }
+
+ private void log(StackTraceElement[] elements, int skip, long time) {
+ this.totalTime.add(time);
+
+ if (skip >= MAX_STACK_DEPTH) {
+ return;
+ }
+
+ if (elements.length - skip == 0) {
+ return;
+ }
+
+ StackTraceElement bottom = elements[elements.length - (skip + 1)];
+ resolveChild(bottom.getClassName(), bottom.getMethodName(), Math.max(0, bottom.getLineNumber())).log(elements, skip + 1, time);
+ }
+
+ private Collection<? extends AbstractNode> getChildren() {
+ if (this.children.isEmpty()) {
+ return Collections.emptyList();
+ }
+
+ List<StackTraceNode> list = new ArrayList<>(this.children.values());
+ list.sort(null);
+ return list;
+ }
+
+ public void serializeTo(JsonWriter writer) throws IOException {
+ writer.beginObject();
+
+ // append metadata about this node
+ appendMetadata(writer);
+
+ // include the total time recorded for this node
+ writer.name("totalTime").value(getTotalTime());
+
+ // append child nodes, if any are present
+ Collection<? extends AbstractNode> childNodes = getChildren();
+ if (!childNodes.isEmpty()) {
+ writer.name("children").beginArray();
+ for (AbstractNode child : childNodes) {
+ child.serializeTo(writer);
+ }
+ writer.endArray();
+ }
+
+ writer.endObject();
+ }
+
+ protected abstract void appendMetadata(JsonWriter writer) throws IOException;
+
+}
diff --git a/spark-common/src/main/java/me/lucko/spark/profiler/node/StackTraceNode.java b/spark-common/src/main/java/me/lucko/spark/profiler/node/StackTraceNode.java
new file mode 100644
index 0000000..706d2e0
--- /dev/null
+++ b/spark-common/src/main/java/me/lucko/spark/profiler/node/StackTraceNode.java
@@ -0,0 +1,78 @@
+/*
+ * WarmRoast
+ * Copyright (C) 2013 Albert Pham <http://www.sk89q.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+package me.lucko.spark.profiler.node;
+
+import com.google.gson.stream.JsonWriter;
+
+import java.io.IOException;
+
+/**
+ * Represents a stack trace element within the {@link AbstractNode node} structure.
+ */
+public final class StackTraceNode extends AbstractNode implements Comparable<StackTraceNode> {
+
+ /**
+ * 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
+ * @return the key
+ */
+ 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 call */
+ private final int lineNumber;
+
+ 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("className").value(this.className);
+ writer.name("methodName").value(this.methodName);
+ if (this.lineNumber != 0) {
+ writer.name("lineNumber").value(this.lineNumber);
+ }
+ }
+
+ private String key() {
+ return generateKey(this.className, this.methodName, this.lineNumber);
+ }
+
+ @Override
+ public int compareTo(StackTraceNode that) {
+ int i = -Long.compare(this.getTotalTime(), that.getTotalTime());
+ if (i != 0) {
+ return i;
+ }
+
+ return this.key().compareTo(that.key());
+ }
+
+}
diff --git a/spark-common/src/main/java/me/lucko/spark/profiler/node/ThreadNode.java b/spark-common/src/main/java/me/lucko/spark/profiler/node/ThreadNode.java
new file mode 100644
index 0000000..10ea67f
--- /dev/null
+++ b/spark-common/src/main/java/me/lucko/spark/profiler/node/ThreadNode.java
@@ -0,0 +1,24 @@
+package me.lucko.spark.profiler.node;
+
+import com.google.gson.stream.JsonWriter;
+
+import java.io.IOException;
+
+/**
+ * The root of a sampling stack for a given thread / thread group.
+ */
+public final class ThreadNode extends AbstractNode {
+
+ /**
+ * The name of this thread
+ */
+ private final String threadName;
+
+ public ThreadNode(String threadName) {
+ this.threadName = threadName;
+ }
+
+ protected void appendMetadata(JsonWriter writer) throws IOException {
+ writer.name("name").value(this.threadName);
+ }
+}