Skip to content

Commit

Permalink
[Wisp] Thread as Wisp whitelist.
Browse files Browse the repository at this point in the history
Summary: Provides a way to run thread as wisp using
configuration

Test Plan: TestThreadAsWispWhiteList

Reviewed-by: yulei

Issue:
dragonwell-project#146
  • Loading branch information
ZhaiMo15 committed Aug 30, 2023
1 parent 5affda2 commit d986772
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.alibaba.wisp.engine;

import java.util.List;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -28,9 +29,14 @@ class ThreadAsWisp {
* @return if condition is satisfied and thread is started as wisp
*/
static boolean tryStart(Thread thread, Runnable target) {
if (!WispConfiguration.ALL_THREAD_AS_WISP
|| WispEngine.isEngineThread(thread)
|| matchBlackList(thread, target)) {
if (WispEngine.isEngineThread(thread)) {
return false;
}
if (WispConfiguration.ALL_THREAD_AS_WISP) {
if (matchList(thread, target, WispConfiguration.getThreadAsWispBlacklist())) {
return false;
}
} else if (!matchList(thread, target, WispConfiguration.getThreadAsWispWhitelist())) {
return false;
}

Expand Down Expand Up @@ -75,8 +81,8 @@ static void exit(Thread thread) {
}
}

private static boolean matchBlackList(Thread thread, Runnable target) {
return Stream.concat(Stream.of(JAVA_LANG_PKG), WispConfiguration.getThreadAsWispBlacklist().stream())
private static boolean matchList(Thread thread, Runnable target, List<String> list) {
return Stream.concat(Stream.of(JAVA_LANG_PKG), list.stream())
.anyMatch(s -> {
Class<?> clazz = (target == null ? thread : target).getClass();
// Java code could start a thread by passing a `target` Runnable argument
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class WispConfiguration {
static final int WISP_CONTROL_GROUP_CFS_PERIOD;

private static List<String> THREAD_AS_WISP_BLACKLIST;
private static List<String> THREAD_AS_WISP_WHITELIST;

static {
Properties p = java.security.AccessController.doPrivileged(
Expand Down Expand Up @@ -134,10 +135,6 @@ private static void checkCompatibility() {
ENABLE_THREAD_AS_WISP, "-Dcom.alibaba.wisp.enableThreadAsWisp=true");
checkDependency(CARRIER_AS_POLLER, "-Dcom.alibaba.wisp.useCarrierAsPoller=true",
ALL_THREAD_AS_WISP, "-Dcom.alibaba.wisp.allThreadAsWisp=true");
if (ENABLE_THREAD_AS_WISP && !ALL_THREAD_AS_WISP) {
throw new IllegalArgumentException("shift thread model by stack configuration is no longer supported," +
" use -XX:+UseWisp2 instead");
}
}

private static void checkDependency(boolean cond, String condStr, boolean preRequire, String preRequireStr) {
Expand Down Expand Up @@ -207,7 +204,7 @@ public Properties run() {
}
}
THREAD_AS_WISP_BLACKLIST = parseListParameter(p, confProp, "com.alibaba.wisp.threadAsWisp.black");

THREAD_AS_WISP_WHITELIST = parseListParameter(p, confProp, "com.alibaba.wisp.threadAsWisp.white");
}

private static final int UNLOADED = 0, LOADING = 1, LOADED = 2;
Expand All @@ -232,4 +229,10 @@ static List<String> getThreadAsWispBlacklist() {
assert THREAD_AS_WISP_BLACKLIST != null;
return THREAD_AS_WISP_BLACKLIST;
}

static List<String> getThreadAsWispWhitelist() {
ensureBizConfigLoaded();
assert THREAD_AS_WISP_WHITELIST != null;
return THREAD_AS_WISP_WHITELIST;
}
}
30 changes: 30 additions & 0 deletions test/jdk/com/alibaba/wisp2/TestThreadAsWispWhiteList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* @test
* @library /test/lib
* @summary test thread as wisp white list
* @modules java.base/jdk.internal.access
* @modules java.base/com.alibaba.wisp.engine:+open
* @run main/othervm -XX:+UseWisp2 -Dcom.alibaba.wisp.enableThreadAsWisp=true -Dcom.alibaba.wisp.allThreadAsWisp=false -Dcom.alibaba.wisp.threadAsWisp.white=name:wisp-* TestThreadAsWispWhiteList
*/

import jdk.internal.access.SharedSecrets;

import java.util.concurrent.FutureTask;

import static jdk.test.lib.Asserts.assertFalse;
import static jdk.test.lib.Asserts.assertTrue;

public class TestThreadAsWispWhiteList {
public static void main(String[] args) throws Exception {
FutureTask<Boolean> future = new FutureTask<>(TestThreadAsWispWhiteList::isRealThread);
new Thread(future, "wisp-1").start();
assertFalse(future.get());
future = new FutureTask<>(TestThreadAsWispWhiteList::isRealThread);
new Thread(future, "thread-1").start();
assertTrue(future.get());
}

private static boolean isRealThread() {
return SharedSecrets.getJavaLangAccess().currentThread0() == Thread.currentThread();
}
}

0 comments on commit d986772

Please sign in to comment.