diff options
author | Luck <git@lucko.me> | 2018-10-08 16:04:51 +0100 |
---|---|---|
committer | Luck <git@lucko.me> | 2018-10-08 16:04:51 +0100 |
commit | 648167064ad2064fc5ab77fb57b347253ac9d468 (patch) | |
tree | 5b44e40b144cdb013cc66e67a08b0208392fb6f5 /spark-common/src/main/java/me/lucko/spark/sampler/node | |
parent | a342e45839970129ce5cdf1f5bad8da5c607106b (diff) | |
download | spark-648167064ad2064fc5ab77fb57b347253ac9d468.tar.gz spark-648167064ad2064fc5ab77fb57b347253ac9d468.tar.bz2 spark-648167064ad2064fc5ab77fb57b347253ac9d468.zip |
reorganise some packages
Diffstat (limited to 'spark-common/src/main/java/me/lucko/spark/sampler/node')
3 files changed, 234 insertions, 0 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 new file mode 100644 index 0000000..e660140 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/sampler/node/AbstractNode.java @@ -0,0 +1,116 @@ +/* + * This file is part of spark. + * + * Copyright (C) Albert Pham <http://www.sk89q.com> + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * 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.sampler.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) { + return this.children.computeIfAbsent( + StackTraceNode.generateKey(className, methodName), + name -> new StackTraceNode(className, methodName) + ); + } + + 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()).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("t").value(getTotalTime()); + + // append child nodes, if any are present + Collection<? extends AbstractNode> childNodes = getChildren(); + if (!childNodes.isEmpty()) { + writer.name("c").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/sampler/node/StackTraceNode.java b/spark-common/src/main/java/me/lucko/spark/sampler/node/StackTraceNode.java new file mode 100644 index 0000000..d161b42 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/sampler/node/StackTraceNode.java @@ -0,0 +1,74 @@ +/* + * This file is part of spark. + * + * Copyright (C) Albert Pham <http://www.sk89q.com> + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * 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.sampler.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 + * @return the key + */ + static String generateKey(String className, String methodName) { + return className + "." + methodName; + } + + /** The name of the class */ + private final String className; + /** The name of the method */ + private final String methodName; + + public StackTraceNode(String className, String methodName) { + this.className = className; + this.methodName = methodName; + } + + @Override + protected void appendMetadata(JsonWriter writer) throws IOException { + writer.name("cl").value(this.className); + writer.name("m").value(this.methodName); + } + + private String key() { + return generateKey(this.className, this.methodName); + } + + @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/sampler/node/ThreadNode.java b/spark-common/src/main/java/me/lucko/spark/sampler/node/ThreadNode.java new file mode 100644 index 0000000..2acce21 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/sampler/node/ThreadNode.java @@ -0,0 +1,44 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * 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.sampler.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); + } +} |