diff options
author | Luck <git@lucko.me> | 2018-05-30 16:23:28 +0100 |
---|---|---|
committer | Luck <git@lucko.me> | 2018-05-30 16:23:28 +0100 |
commit | 645b2b34542c63c5416ada762c04efcacc3ca17f (patch) | |
tree | a32e7129ca1bba82c39dd8f2982c99019e5e17cd /common/src/main/java/me/lucko | |
parent | 46b5064045f631ff66ed4a144bed6919e069bbbd (diff) | |
download | spark-645b2b34542c63c5416ada762c04efcacc3ca17f.tar.gz spark-645b2b34542c63c5416ada762c04efcacc3ca17f.tar.bz2 spark-645b2b34542c63c5416ada762c04efcacc3ca17f.zip |
Use a workerpool per sampler, then shut it down once profiling is complete
Diffstat (limited to 'common/src/main/java/me/lucko')
3 files changed, 14 insertions, 33 deletions
diff --git a/common/src/main/java/me/lucko/spark/common/CommandHandler.java b/common/src/main/java/me/lucko/spark/common/CommandHandler.java index 11f0e46..7e7bc50 100644 --- a/common/src/main/java/me/lucko/spark/common/CommandHandler.java +++ b/common/src/main/java/me/lucko/spark/common/CommandHandler.java @@ -3,7 +3,6 @@ package me.lucko.spark.common; import com.google.common.collect.HashMultimap; import com.google.common.collect.SetMultimap; import com.google.common.collect.Sets; -import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.google.gson.JsonObject; import me.lucko.spark.common.http.Bytebin; @@ -19,9 +18,6 @@ import java.util.List; import java.util.Set; import java.util.Timer; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.SynchronousQueue; -import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -42,16 +38,6 @@ public abstract class CommandHandler<T> { */ private final Timer samplingThread = new Timer("spark-sampling-thread", true); - /** - * The worker {@link ExecutorService} being used by the {@link #activeSampler}. - */ - private final ExecutorService workerPool = new ThreadPoolExecutor( - 1, 6, - 30L, TimeUnit.SECONDS, - new SynchronousQueue<>(), - new ThreadFactoryBuilder().setNameFormat("spark-worker-%d").build() - ); - /** Guards {@link #activeSampler} */ private final Object[] activeSamplerMutex = new Object[0]; /** The WarmRoast instance currently running, if any */ @@ -156,7 +142,7 @@ public abstract class CommandHandler<T> { builder.completeAfter(timeoutSeconds, TimeUnit.SECONDS); } builder.samplingInterval(intervalMillis); - sampler = this.activeSampler = builder.start(this.samplingThread, this.workerPool); + sampler = this.activeSampler = builder.start(this.samplingThread); sendPrefixedMessage(sender, "&bProfiler now active!"); if (timeoutSeconds == -1) { diff --git a/common/src/main/java/me/lucko/spark/profiler/Sampler.java b/common/src/main/java/me/lucko/spark/profiler/Sampler.java index 5db978c..9696c03 100644 --- a/common/src/main/java/me/lucko/spark/profiler/Sampler.java +++ b/common/src/main/java/me/lucko/spark/profiler/Sampler.java @@ -18,6 +18,7 @@ package me.lucko.spark.profiler; +import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.google.gson.JsonArray; import com.google.gson.JsonObject; @@ -32,14 +33,15 @@ import java.util.TimerTask; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Phaser; +import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicInteger; /** * Main sampler class. */ public class Sampler extends TimerTask { + private static final AtomicInteger THREAD_ID = new AtomicInteger(0); /** The thread management interface for the current JVM */ private final ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); @@ -48,9 +50,9 @@ public class Sampler extends TimerTask { private final Map<String, StackNode> threadData = new ConcurrentHashMap<>(); /** The worker pool for inserting stack nodes */ - private ExecutorService workerPool; - /** The phaser used to track pending stack node data */ - private final Phaser phaser = new Phaser(); + private final ExecutorService workerPool = Executors.newFixedThreadPool( + 6, new ThreadFactoryBuilder().setNameFormat("spark-worker-" + THREAD_ID.getAndIncrement()).build() + ); /** A future to encapsulation the completion of this sampler instance */ private final CompletableFuture<Sampler> future = new CompletableFuture<>(); @@ -74,10 +76,8 @@ public class Sampler extends TimerTask { * Starts the sampler. * * @param samplingThread the timer to schedule the sampling on - * @param workerPool the worker pool */ - public void start(Timer samplingThread, ExecutorService workerPool) { - this.workerPool = workerPool; + public void start(Timer samplingThread) { samplingThread.scheduleAtFixedRate(this, 0, this.interval); this.startTime = System.currentTimeMillis(); } @@ -88,9 +88,6 @@ public class Sampler extends TimerTask { node.log(data.stack, Sampler.this.interval); } catch (Exception e) { e.printStackTrace(); - } finally { - // countdown the phaser - this.phaser.arriveAndDeregister(); } } @@ -101,9 +98,10 @@ public class Sampler extends TimerTask { */ public Map<String, StackNode> getData() { // wait for all pending data to be inserted + this.workerPool.shutdown(); try { - this.phaser.awaitAdvanceInterruptibly(this.phaser.getPhase(), 15, TimeUnit.SECONDS); - } catch (InterruptedException | TimeoutException e) { + this.workerPool.awaitTermination(15, TimeUnit.SECONDS); + } catch (InterruptedException e) { e.printStackTrace(); } @@ -145,8 +143,6 @@ public class Sampler extends TimerTask { // form the queued data QueuedThreadInfo queuedData = new QueuedThreadInfo(threadName, stack); - // mark that this data is pending - this.phaser.register(); // schedule insertion of the data this.workerPool.execute(queuedData); } diff --git a/common/src/main/java/me/lucko/spark/profiler/SamplerBuilder.java b/common/src/main/java/me/lucko/spark/profiler/SamplerBuilder.java index fe6ee20..8e79b33 100644 --- a/common/src/main/java/me/lucko/spark/profiler/SamplerBuilder.java +++ b/common/src/main/java/me/lucko/spark/profiler/SamplerBuilder.java @@ -3,7 +3,6 @@ package me.lucko.spark.profiler; import com.google.common.base.Preconditions; import java.util.Timer; -import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; /** @@ -34,9 +33,9 @@ public class SamplerBuilder { return this; } - public Sampler start(Timer samplingThread, ExecutorService workerPool) { + public Sampler start(Timer samplingThread) { Sampler sampler = new Sampler(this.samplingInterval, this.threadDumper, this.timeout); - sampler.start(samplingThread, workerPool); + sampler.start(samplingThread); return sampler; } |