Skip to content

Commit

Permalink
async-profiler support (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucko authored Mar 10, 2021
1 parent fd6736f commit 9ee26a1
Show file tree
Hide file tree
Showing 37 changed files with 1,810 additions and 260 deletions.
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
allprojects {
group = 'me.lucko'
version = '1.4-SNAPSHOT'
version = '1.5-SNAPSHOT'

configurations {
compileClasspath // Fabric-loom needs this for remap jar for some reason
Expand All @@ -13,7 +13,7 @@ subprojects {
apply plugin: 'idea'

ext {
pluginVersion = '1.4.3'
pluginVersion = '1.5.0'
pluginDescription = 'spark is a performance profiling plugin based on sk89q\'s WarmRoast profiler'
}

Expand All @@ -28,6 +28,7 @@ subprojects {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://repo.lucko.me/" }
maven { url "https://jitpack.io" }
}

}
13 changes: 10 additions & 3 deletions spark-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ plugins {
}

dependencies {
compile 'com.github.jvm-profiling-tools:async-profiler:v2.0-rc'
compile 'org.ow2.asm:asm:7.1'
compile 'com.google.protobuf:protobuf-java:3.14.0'
compile 'com.squareup.okhttp3:okhttp:3.14.1'
compile 'com.squareup.okio:okio:1.17.3'
compile 'org.tukaani:xz:1.8'
compile('net.kyori:adventure-api:4.1.1') {
compile('net.kyori:adventure-api:4.7.0') {
exclude(module: 'checker-qual')
}
compile('net.kyori:adventure-text-serializer-gson:4.1.1') {
compile('net.kyori:adventure-text-serializer-gson:4.7.0') {
exclude(module: 'adventure-api')
exclude(module: 'gson')
}
compile('net.kyori:adventure-text-serializer-legacy:4.1.1') {
compile('net.kyori:adventure-text-serializer-legacy:4.7.0') {
exclude(module: 'adventure-api')
}
compile('net.kyori:adventure-text-feature-pagination:4.0.0-SNAPSHOT') {
Expand All @@ -25,6 +26,12 @@ dependencies {
compileOnly 'com.google.guava:guava:19.0'
}

processResources {
from(sourceSets.main.resources.srcDirs) {
include 'libasyncProfiler.so'
}
}

protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.14.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public void executeCommand(CommandSender sender, String[] args) {
if (command.aliases().contains(alias)) {
try {
command.executor().execute(this, sender, resp, new Arguments(rawArgs));
} catch (IllegalArgumentException e) {
} catch (Arguments.ParseException e) {
resp.replyPrefixed(text(e.getMessage(), RED));
}
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public Arguments(List<String> rawArgs) {

if (flag == null || matches) {
if (!matches) {
throw new IllegalArgumentException("Expected flag at position " + i + " but got '" + arg + "' instead!");
throw new ParseException("Expected flag at position " + i + " but got '" + arg + "' instead!");
}

// store existing value, if present
Expand Down Expand Up @@ -83,7 +83,7 @@ public int intFlag(String key) {
try {
return Math.abs(Integer.parseInt(it.next()));
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid input for '" + key + "' argument. Please specify a number!");
throw new ParseException("Invalid input for '" + key + "' argument. Please specify a number!");
}
}
return -1; // undefined
Expand All @@ -95,7 +95,7 @@ public double doubleFlag(String key) {
try {
return Math.abs(Double.parseDouble(it.next()));
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid input for '" + key + "' argument. Please specify a number!");
throw new ParseException("Invalid input for '" + key + "' argument. Please specify a number!");
}
}
return -1; // undefined
Expand All @@ -108,4 +108,21 @@ public Set<String> stringFlag(String key) {
public boolean boolFlag(String key) {
return this.parsedArgs.containsKey(key);
}

public static final class ParseException extends IllegalArgumentException {
public ParseException() {
}

public ParseException(String s) {
super(s);
}

public ParseException(String message, Throwable cause) {
super(message, cause);
}

public ParseException(Throwable cause) {
super(cause);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import me.lucko.spark.common.sampler.ThreadDumper;
import me.lucko.spark.common.sampler.ThreadGrouper;
import me.lucko.spark.common.sampler.ThreadNodeOrder;
import me.lucko.spark.common.sampler.async.AsyncSampler;
import me.lucko.spark.common.sampler.node.MergeMode;
import me.lucko.spark.common.sampler.tick.TickHook;
import me.lucko.spark.common.util.MethodDisambiguator;
Expand All @@ -55,13 +56,13 @@
public class SamplerModule implements CommandModule {
private static final MediaType SPARK_SAMPLER_MEDIA_TYPE = MediaType.parse("application/x-spark-sampler");

/** The WarmRoast instance currently running, if any */
/** The sampler instance currently running, if any */
private Sampler activeSampler = null;

@Override
public void close() {
if (this.activeSampler != null) {
this.activeSampler.cancel();
this.activeSampler.stop();
this.activeSampler = null;
}
}
Expand All @@ -83,6 +84,7 @@ public void registerCommands(Consumer<Command> consumer) {
.argumentUsage("only-ticks-over", "tick length millis")
.argumentUsage("ignore-sleeping", null)
.argumentUsage("ignore-native", null)
.argumentUsage("force-java-sampler", null)
.argumentUsage("order-by-time", null)
.argumentUsage("separate-parent-calls", null)
.executor((platform, sender, resp, arguments) -> {
Expand Down Expand Up @@ -118,7 +120,7 @@ public void registerCommands(Consumer<Command> consumer) {
if (this.activeSampler == null) {
resp.replyPrefixed(text("There isn't an active sampling task running."));
} else {
this.activeSampler.cancel();
this.activeSampler.stop();
resp.broadcastPrefixed(text("The active sampling operation has been stopped! Uploading results..."));
ThreadNodeOrder threadOrder = arguments.boolFlag("order-by-time") ? ThreadNodeOrder.BY_TIME : ThreadNodeOrder.BY_NAME;
String comment = Iterables.getFirst(arguments.stringFlag("comment"), null);
Expand Down Expand Up @@ -149,6 +151,7 @@ public void registerCommands(Consumer<Command> consumer) {

boolean ignoreSleeping = arguments.boolFlag("ignore-sleeping");
boolean ignoreNative = arguments.boolFlag("ignore-native");
boolean forceJavaSampler = arguments.boolFlag("force-java-sampler");

Set<String> threads = arguments.stringFlag("thread");
ThreadDumper threadDumper;
Expand Down Expand Up @@ -201,19 +204,25 @@ public void registerCommands(Consumer<Command> consumer) {
builder.samplingInterval(intervalMillis);
builder.ignoreSleeping(ignoreSleeping);
builder.ignoreNative(ignoreNative);
builder.forceJavaSampler(forceJavaSampler);
if (ticksOver != -1) {
builder.ticksOver(ticksOver, tickHook);
}
Sampler sampler = this.activeSampler = builder.start();

resp.broadcastPrefixed(text("Profiler now active!", GOLD));
resp.broadcastPrefixed(text()
.append(text("Profiler now active!", GOLD))
.append(space())
.append(text("(" + (sampler instanceof AsyncSampler ? "async" : "built-in java") + ")", DARK_GRAY))
.build()
);
if (timeoutSeconds == -1) {
resp.broadcastPrefixed(text("Use '/" + platform.getPlugin().getCommandName() + " profiler --stop' to stop profiling and upload the results."));
} else {
resp.broadcastPrefixed(text("The results will be automatically returned after the profiler has been running for " + timeoutSeconds + " seconds."));
}

CompletableFuture<Sampler> future = this.activeSampler.getFuture();
CompletableFuture<? extends Sampler> future = this.activeSampler.getFuture();

// send message if profiling fails
future.whenCompleteAsync((s, throwable) -> {
Expand Down Expand Up @@ -253,8 +262,8 @@ public void registerCommands(Consumer<Command> consumer) {

List<String> opts = new ArrayList<>(Arrays.asList("--info", "--stop", "--cancel",
"--timeout", "--regex", "--combine-all", "--not-combined", "--interval",
"--only-ticks-over", "--ignore-sleeping", "--ignore-native", "--order-by-time",
"--separate-parent-calls", "--comment"));
"--only-ticks-over", "--ignore-sleeping", "--ignore-native", "--force-java-sampler",
"--order-by-time", "--separate-parent-calls", "--comment"));
opts.removeAll(arguments);
opts.add("--thread"); // allowed multiple times

Expand Down
Loading

0 comments on commit 9ee26a1

Please sign in to comment.