From a96b3392edad759e8083cc15840c86c2f3f8673d Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 31 Dec 2018 20:03:11 +0000 Subject: Some minor optimizations to ThreadDumper implementations --- .../java/me/lucko/spark/sampler/ThreadDumper.java | 18 +++++-- .../java/me/lucko/spark/util/ThreadFinder.java | 60 ++++++++++++++++++++++ .../src/main/java/me/lucko/spark/util/Threads.java | 58 --------------------- 3 files changed, 75 insertions(+), 61 deletions(-) create mode 100644 spark-common/src/main/java/me/lucko/spark/util/ThreadFinder.java delete mode 100644 spark-common/src/main/java/me/lucko/spark/util/Threads.java (limited to 'spark-common') diff --git a/spark-common/src/main/java/me/lucko/spark/sampler/ThreadDumper.java b/spark-common/src/main/java/me/lucko/spark/sampler/ThreadDumper.java index 61706f4..38cdd36 100644 --- a/spark-common/src/main/java/me/lucko/spark/sampler/ThreadDumper.java +++ b/spark-common/src/main/java/me/lucko/spark/sampler/ThreadDumper.java @@ -21,11 +21,13 @@ package me.lucko.spark.sampler; -import me.lucko.spark.util.Threads; +import me.lucko.spark.util.ThreadFinder; import java.lang.management.ThreadInfo; import java.lang.management.ThreadMXBean; import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.regex.Pattern; @@ -56,6 +58,7 @@ public interface ThreadDumper { * Implementation of {@link ThreadDumper} that generates data for a specific set of threads. */ final class Specific implements ThreadDumper { + private final ThreadFinder threadFinder = new ThreadFinder(); private final long[] ids; public Specific(long[] ids) { @@ -64,7 +67,7 @@ public interface ThreadDumper { public Specific(Set names) { Set namesLower = names.stream().map(String::toLowerCase).collect(Collectors.toSet()); - this.ids = Threads.getThreads() + this.ids = this.threadFinder.getThreads() .filter(t -> namesLower.contains(t.getName().toLowerCase())) .mapToLong(Thread::getId) .toArray(); @@ -80,7 +83,9 @@ public interface ThreadDumper { * Implementation of {@link ThreadDumper} that generates data for a regex matched set of threads. */ final class Regex implements ThreadDumper { + private final ThreadFinder threadFinder = new ThreadFinder(); private final Set namePatterns; + private final Map cache = new HashMap<>(); public Regex(Set namePatterns) { this.namePatterns = namePatterns.stream() @@ -97,13 +102,20 @@ public interface ThreadDumper { @Override public Iterable dumpThreads(ThreadMXBean threadBean) { - return Threads.getThreads() + return this.threadFinder.getThreads() .filter(thread -> { + Boolean result = this.cache.get(thread.getId()); + if (result != null) { + return result; + } + for (Pattern pattern : this.namePatterns) { if (pattern.matcher(thread.getName()).matches()) { + this.cache.put(thread.getId(), true); return true; } } + this.cache.put(thread.getId(), false); return false; }) .map(thread -> threadBean.getThreadInfo(thread.getId())) diff --git a/spark-common/src/main/java/me/lucko/spark/util/ThreadFinder.java b/spark-common/src/main/java/me/lucko/spark/util/ThreadFinder.java new file mode 100644 index 0000000..a74f85a --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/util/ThreadFinder.java @@ -0,0 +1,60 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * 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 . + */ + +package me.lucko.spark.util; + +import java.util.Arrays; +import java.util.Objects; +import java.util.stream.Stream; + +/** + * Utility to find active threads. + */ +public final class ThreadFinder { + + private static final ThreadGroup ROOT_THREAD_GROUP; + static { + ThreadGroup rootGroup = Thread.currentThread().getThreadGroup(); + ThreadGroup parentGroup; + while ((parentGroup = rootGroup.getParent()) != null) { + rootGroup = parentGroup; + } + ROOT_THREAD_GROUP = rootGroup; + } + + // cache the approx active count at the time of construction. + // the usages of this class are likely to be somewhat short-lived, so it's good + // enough to just cache a value on init. + private final int approxActiveCount = ROOT_THREAD_GROUP.activeCount(); + + /** + * Gets a stream of all known active threads. + * + * @return a stream of threads + */ + public Stream getThreads() { + Thread[] threads = new Thread[this.approxActiveCount + 20]; // +20 to allow a bit of growth for newly created threads + while (ROOT_THREAD_GROUP.enumerate(threads, true ) == threads.length) { + threads = new Thread[threads.length * 2]; + } + return Arrays.stream(threads).filter(Objects::nonNull); + } + +} diff --git a/spark-common/src/main/java/me/lucko/spark/util/Threads.java b/spark-common/src/main/java/me/lucko/spark/util/Threads.java deleted file mode 100644 index d1efdff..0000000 --- a/spark-common/src/main/java/me/lucko/spark/util/Threads.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * This file is part of spark. - * - * Copyright (c) lucko (Luck) - * 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 . - */ - -package me.lucko.spark.util; - -import java.util.Arrays; -import java.util.Objects; -import java.util.stream.Stream; - -/** - * Utilities for working with {@link Thread}s. - */ -public final class Threads { - - private Threads() {} - - private static final ThreadGroup ROOT_THREAD_GROUP; - - static { - ThreadGroup rootGroup = Thread.currentThread().getThreadGroup(); - ThreadGroup parentGroup; - while ((parentGroup = rootGroup.getParent()) != null) { - rootGroup = parentGroup; - } - ROOT_THREAD_GROUP = rootGroup; - } - - /** - * Gets a stream of all known active threads. - * - * @return a stream of threads - */ - public static Stream getThreads() { - Thread[] threads = new Thread[ROOT_THREAD_GROUP.activeCount()]; - while (ROOT_THREAD_GROUP.enumerate(threads, true ) == threads.length) { - threads = new Thread[threads.length * 2]; - } - return Arrays.stream(threads).filter(Objects::nonNull); - } - -} -- cgit