diff options
author | Luck <git@lucko.me> | 2023-01-01 16:15:50 +0000 |
---|---|---|
committer | Luck <git@lucko.me> | 2023-01-01 16:15:58 +0000 |
commit | dcdaacd7deb40be939bf91379f7391c02481cc48 (patch) | |
tree | 3d90c30d45fd9ad8cf7af37984875beae14f4b51 /spark-common/src/main/java/me/lucko/spark/common/sampler | |
parent | 7157ceee5401ac269e755dbe44ef74d76d1a3f0e (diff) | |
download | spark-dcdaacd7deb40be939bf91379f7391c02481cc48.tar.gz spark-dcdaacd7deb40be939bf91379f7391c02481cc48.tar.bz2 spark-dcdaacd7deb40be939bf91379f7391c02481cc48.zip |
Refactor thread grouper/dumper config parse
Diffstat (limited to 'spark-common/src/main/java/me/lucko/spark/common/sampler')
3 files changed, 66 insertions, 51 deletions
diff --git a/spark-common/src/main/java/me/lucko/spark/common/sampler/BackgroundSamplerManager.java b/spark-common/src/main/java/me/lucko/spark/common/sampler/BackgroundSamplerManager.java index ff1b3ac..4e9ca9e 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/sampler/BackgroundSamplerManager.java +++ b/spark-common/src/main/java/me/lucko/spark/common/sampler/BackgroundSamplerManager.java @@ -24,10 +24,7 @@ import me.lucko.spark.common.SparkPlatform; import me.lucko.spark.common.platform.PlatformInfo; import me.lucko.spark.common.util.Configuration; -import java.util.Arrays; -import java.util.Set; import java.util.logging.Level; -import java.util.stream.Collectors; public class BackgroundSamplerManager { @@ -105,42 +102,20 @@ public class BackgroundSamplerManager { private void startSampler() { boolean forceJavaEngine = this.configuration.getString(OPTION_ENGINE, "async").equals("java"); - String grouperName = this.configuration.getString(OPTION_THREAD_GROUPER, "byPool"); - String dumperName = this.configuration.getString(OPTION_THREAD_DUMPER, "default"); - - ThreadGrouper threadGrouper; - switch (grouperName) { - case "asOne": - threadGrouper = ThreadGrouper.AS_ONE; - break; - case "byName": - threadGrouper = ThreadGrouper.BY_NAME; - break; - default: - threadGrouper = ThreadGrouper.BY_POOL; - break; - } - ThreadDumper threadDumper; - switch (dumperName) { - case "default": - threadDumper = this.platform.getPlugin().getDefaultThreadDumper(); - break; - case "*": - case "all": - threadDumper = ThreadDumper.ALL; - break; - default: - Set<String> threadNames = Arrays.stream(dumperName.split(",")).collect(Collectors.toSet()); - threadDumper = new ThreadDumper.Specific(threadNames); - break; + ThreadGrouper threadGrouper = ThreadGrouper.parseConfigSetting(this.configuration.getString(OPTION_THREAD_GROUPER, "by-pool")); + ThreadDumper threadDumper = ThreadDumper.parseConfigSetting(this.configuration.getString(OPTION_THREAD_DUMPER, "default")); + if (threadDumper == null) { + threadDumper = this.platform.getPlugin().getDefaultThreadDumper(); } + int interval = this.configuration.getInteger(OPTION_INTERVAL, 10); + Sampler sampler = new SamplerBuilder() .background(true) .threadDumper(threadDumper) .threadGrouper(threadGrouper) - .samplingInterval(this.configuration.getInteger(OPTION_INTERVAL, 10)) + .samplingInterval(interval) .forceJavaSampler(forceJavaEngine) .start(this.platform); diff --git a/spark-common/src/main/java/me/lucko/spark/common/sampler/ThreadDumper.java b/spark-common/src/main/java/me/lucko/spark/common/sampler/ThreadDumper.java index fd0c413..62e2dda 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/sampler/ThreadDumper.java +++ b/spark-common/src/main/java/me/lucko/spark/common/sampler/ThreadDumper.java @@ -55,6 +55,24 @@ public interface ThreadDumper { SamplerMetadata.ThreadDumper getMetadata(); /** + * Creates a new {@link ThreadDumper} by parsing the given config setting. + * + * @param setting the config setting + * @return the thread dumper + */ + static ThreadDumper parseConfigSetting(String setting) { + switch (setting) { + case "default": + return null; + case "all": + return ALL; + default: + Set<String> threadNames = Arrays.stream(setting.split(",")).collect(Collectors.toSet()); + return new ThreadDumper.Specific(threadNames); + } + } + + /** * Implementation of {@link ThreadDumper} that generates data for all threads. */ ThreadDumper ALL = new ThreadDumper() { diff --git a/spark-common/src/main/java/me/lucko/spark/common/sampler/ThreadGrouper.java b/spark-common/src/main/java/me/lucko/spark/common/sampler/ThreadGrouper.java index 9ad84df..b6cfbea 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/sampler/ThreadGrouper.java +++ b/spark-common/src/main/java/me/lucko/spark/common/sampler/ThreadGrouper.java @@ -35,6 +35,47 @@ import java.util.regex.Pattern; public interface ThreadGrouper { /** + * Gets the group for the given thread. + * + * @param threadId the id of the thread + * @param threadName the name of the thread + * @return the group + */ + String getGroup(long threadId, String threadName); + + /** + * Gets the label to use for a given group. + * + * @param group the group + * @return the label + */ + String getLabel(String group); + + /** + * Gets the metadata enum instance for this thread grouper. + * + * @return proto metadata + */ + SamplerMetadata.DataAggregator.ThreadGrouper asProto(); + + /** + * Creates a new {@link ThreadGrouper} by parsing the given config setting. + * + * @param setting the config setting + * @return the thread grouper + */ + static ThreadGrouper parseConfigSetting(String setting) { + switch (setting) { + case "as-one": + return AS_ONE; + case "by-name": + return BY_NAME; + default: + return BY_POOL; + } + } + + /** * Implementation of {@link ThreadGrouper} that just groups by thread name. */ ThreadGrouper BY_NAME = new ThreadGrouper() { @@ -126,23 +167,4 @@ public interface ThreadGrouper { } }; - /** - * Gets the group for the given thread. - * - * @param threadId the id of the thread - * @param threadName the name of the thread - * @return the group - */ - String getGroup(long threadId, String threadName); - - /** - * Gets the label to use for a given group. - * - * @param group the group - * @return the label - */ - String getLabel(String group); - - SamplerMetadata.DataAggregator.ThreadGrouper asProto(); - } |