8296323: JVMTI can_support_virtual_threads not available for agents loaded into running VM
Reviewed-by: alanb, rrich
This commit is contained in:
parent
974cb8370d
commit
e661c5a3d0
src/hotspot/share/prims
test
hotspot/jtreg/serviceability/jvmti/vthread/VirtualThreadStartTest
lib/jdk/test/lib/jvmti
@ -198,12 +198,8 @@ private:
|
||||
public:
|
||||
JvmtiVirtualThreadEventMark(JavaThread *thread) :
|
||||
JvmtiEventMark(thread) {
|
||||
JvmtiThreadState* state = thread->jvmti_thread_state();
|
||||
if (state != NULL && state->is_virtual()) {
|
||||
_jthread = to_jobject(thread->vthread());
|
||||
} else {
|
||||
_jthread = to_jobject(thread->threadObj());
|
||||
}
|
||||
assert(thread->vthread() != NULL || thread->threadObj() == NULL, "sanity check");
|
||||
_jthread = to_jobject(thread->vthread());
|
||||
};
|
||||
jthread jni_thread() { return (jthread)_jthread; }
|
||||
};
|
||||
|
@ -97,6 +97,7 @@ jvmtiCapabilities JvmtiManageCapabilities::init_always_capabilities() {
|
||||
jc.can_generate_object_free_events = 1;
|
||||
jc.can_generate_resource_exhaustion_heap_events = 1;
|
||||
jc.can_generate_resource_exhaustion_threads_events = 1;
|
||||
jc.can_support_virtual_threads = 1;
|
||||
return jc;
|
||||
}
|
||||
|
||||
@ -121,7 +122,6 @@ jvmtiCapabilities JvmtiManageCapabilities::init_onload_capabilities() {
|
||||
jc.can_get_current_contended_monitor = 1;
|
||||
jc.can_generate_early_vmstart = 1;
|
||||
jc.can_generate_early_class_hook_events = 1;
|
||||
jc.can_support_virtual_threads = 1;
|
||||
return jc;
|
||||
}
|
||||
|
||||
|
66
test/hotspot/jtreg/serviceability/jvmti/vthread/VirtualThreadStartTest/VirtualThreadStartTest.java
Normal file
66
test/hotspot/jtreg/serviceability/jvmti/vthread/VirtualThreadStartTest/VirtualThreadStartTest.java
Normal file
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code 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
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @summary Verifies JVMTI can_support_virtual_threads works for agents loaded at startup and into running VM
|
||||
* @requires vm.jvmti
|
||||
* @requires vm.continuations
|
||||
* @enablePreview
|
||||
* @run main/othervm/native -agentlib:VirtualThreadStartTest VirtualThreadStartTest
|
||||
* @run main/othervm/native -agentlib:VirtualThreadStartTest=can_support_virtual_threads VirtualThreadStartTest
|
||||
* @run main/othervm/native -Djdk.attach.allowAttachSelf=true VirtualThreadStartTest attach
|
||||
* @run main/othervm/native -Djdk.attach.allowAttachSelf=true VirtualThreadStartTest attach can_support_virtual_threads
|
||||
*/
|
||||
|
||||
import com.sun.tools.attach.VirtualMachine;
|
||||
|
||||
public class VirtualThreadStartTest {
|
||||
private static final String AGENT_LIB = "VirtualThreadStartTest";
|
||||
private static final int THREAD_CNT = 10;
|
||||
|
||||
private static native int getAndResetStartedThreads();
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
System.out.println("loading " + AGENT_LIB + " lib");
|
||||
|
||||
if (args.length > 0 && args[0].equals("attach")) { // agent loaded into running VM case
|
||||
String arg = args.length == 2 ? args[1] : "";
|
||||
VirtualMachine vm = VirtualMachine.attach(String.valueOf(ProcessHandle.current().pid()));
|
||||
vm.loadAgentLibrary(AGENT_LIB, arg);
|
||||
} else {
|
||||
System.loadLibrary(AGENT_LIB);
|
||||
}
|
||||
getAndResetStartedThreads();
|
||||
|
||||
for (int i = 0; i < THREAD_CNT; i++) {
|
||||
Thread.ofVirtual().name("Tested-VT-" + i).start(() -> {}).join();
|
||||
}
|
||||
|
||||
int startedThreads = getAndResetStartedThreads();
|
||||
System.out.println("ThreadStart event count: " + startedThreads + ", expected: " + THREAD_CNT);
|
||||
if (startedThreads != THREAD_CNT) {
|
||||
throw new RuntimeException("Failed: wrong ThreadStart event count");
|
||||
}
|
||||
}
|
||||
}
|
144
test/hotspot/jtreg/serviceability/jvmti/vthread/VirtualThreadStartTest/libVirtualThreadStartTest.cpp
Normal file
144
test/hotspot/jtreg/serviceability/jvmti/vthread/VirtualThreadStartTest/libVirtualThreadStartTest.cpp
Normal file
@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code 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
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <jvmti.h>
|
||||
#include "jvmti_common.h"
|
||||
|
||||
extern "C" {
|
||||
|
||||
static jvmtiEnv *jvmti;
|
||||
static int started_thread_cnt = 0;
|
||||
static jrawMonitorID agent_event_lock = NULL;
|
||||
static const char* TESTED_TNAME_START = "Tested-VT";
|
||||
static const size_t TESTED_TNAME_START_LEN = strlen(TESTED_TNAME_START);
|
||||
static bool can_support_vt_enabled = false;
|
||||
|
||||
void JNICALL ThreadStart(jvmtiEnv *jvmti, JNIEnv* jni, jthread thread) {
|
||||
char* tname = get_thread_name(jvmti, jni, thread);
|
||||
|
||||
RawMonitorLocker agent_start_locker(jvmti, jni, agent_event_lock);
|
||||
|
||||
if (tname != NULL && strncmp(tname, TESTED_TNAME_START, TESTED_TNAME_START_LEN) == 0) {
|
||||
jboolean is_virtual = jni->IsVirtualThread(thread);
|
||||
if (!is_virtual) {
|
||||
fatal(jni, "Failed: tested thread expected to be virtual");
|
||||
}
|
||||
if (can_support_vt_enabled) {
|
||||
fatal(jni, "Failed: expected VirtualThreadStart instead of ThreadStart event");
|
||||
}
|
||||
printf("ThreadStart event: %s\n", tname);
|
||||
started_thread_cnt++;
|
||||
}
|
||||
deallocate(jvmti, jni, (void*)tname);
|
||||
}
|
||||
|
||||
void JNICALL VirtualThreadStart(jvmtiEnv *jvmti, JNIEnv* jni, jthread thread) {
|
||||
char* tname = get_thread_name(jvmti, jni, thread);
|
||||
|
||||
RawMonitorLocker agent_start_locker(jvmti, jni, agent_event_lock);
|
||||
|
||||
if (tname != NULL && strncmp(tname, TESTED_TNAME_START, TESTED_TNAME_START_LEN) == 0) {
|
||||
jboolean is_virtual = jni->IsVirtualThread(thread);
|
||||
if (!is_virtual) {
|
||||
fatal(jni, "Failed: tested thread expected to be virtual");
|
||||
}
|
||||
if (!can_support_vt_enabled) {
|
||||
fatal(jni, "Failed: expected ThreadStart instead of VirtualThreadStart event");
|
||||
}
|
||||
printf("VirtualThreadStart event: %s\n", tname);
|
||||
started_thread_cnt++;
|
||||
}
|
||||
deallocate(jvmti, jni, (void*)tname);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_VirtualThreadStartTest_getAndResetStartedThreads(JNIEnv* jni, jclass clazz) {
|
||||
RawMonitorLocker agent_start_locker(jvmti, jni, agent_event_lock);
|
||||
|
||||
int result = started_thread_cnt;
|
||||
started_thread_cnt = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
jint agent_init(JavaVM *jvm, char *options, void *reserved) {
|
||||
jvmtiCapabilities caps;
|
||||
jvmtiEventCallbacks callbacks;
|
||||
jvmtiError err;
|
||||
|
||||
if (jvm->GetEnv((void **) (&jvmti), JVMTI_VERSION) != JNI_OK) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
memset(&caps, 0, sizeof(caps));
|
||||
memset(&callbacks, 0, sizeof(callbacks));
|
||||
callbacks.ThreadStart = &ThreadStart;
|
||||
callbacks.VirtualThreadStart = &VirtualThreadStart;
|
||||
|
||||
if (options != NULL && strcmp(options, "can_support_virtual_threads") == 0) {
|
||||
can_support_vt_enabled = true;
|
||||
caps.can_support_virtual_threads = 1;
|
||||
|
||||
err = jvmti->AddCapabilities(&caps);
|
||||
if (err != JVMTI_ERROR_NONE) {
|
||||
LOG("Agent init: error in JVMTI AddCapabilities: %s (%d)\n", TranslateError(err), err);
|
||||
return JNI_ERR;
|
||||
}
|
||||
err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VIRTUAL_THREAD_START, NULL);
|
||||
if (err != JVMTI_ERROR_NONE) {
|
||||
LOG("Agent init: error in JVMTI SetEventNotificationMode: %s (%d)\n", TranslateError(err), err);
|
||||
return JNI_ERR;
|
||||
}
|
||||
} else {
|
||||
err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_THREAD_START, NULL);
|
||||
if (err != JVMTI_ERROR_NONE) {
|
||||
LOG("Agent init: error in JVMTI SetEventNotificationMode: %s (%d)\n", TranslateError(err), err);
|
||||
return JNI_ERR;
|
||||
}
|
||||
}
|
||||
printf("agent_init: can_support_virtual_threads capability: %d\n",
|
||||
caps.can_support_virtual_threads);
|
||||
|
||||
err = jvmti->SetEventCallbacks(&callbacks, (jint)sizeof(callbacks));
|
||||
if (err != JVMTI_ERROR_NONE) {
|
||||
LOG("Agent init: error in JVMTI AddCapabilities: %s (%d)\n", TranslateError(err), err);
|
||||
return JNI_ERR;
|
||||
}
|
||||
agent_event_lock = create_raw_monitor(jvmti, "agent_event_lock");
|
||||
|
||||
return JNI_OK;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
|
||||
LOG("Agent_OnLoad started\n");
|
||||
return agent_init(jvm, options, reserved);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {
|
||||
LOG("Agent_OnAttach started\n");
|
||||
return agent_init(jvm, options, reserved);
|
||||
}
|
||||
|
||||
} // extern "C"
|
@ -294,7 +294,16 @@ get_thread_name(jvmtiEnv *jvmti, JNIEnv* jni, jthread thread) {
|
||||
}
|
||||
check_jvmti_status(jni, err, "get_thread_name: error in JVMTI GetThreadInfo call");
|
||||
|
||||
return thr_info.name == NULL ? (char*)"<Unnamed thread>" : thr_info.name;
|
||||
static const char* UNNAMED_STR = "<Unnamed thread>";
|
||||
static size_t UNNAMED_LEN = strlen(UNNAMED_STR);
|
||||
char* tname = thr_info.name;
|
||||
if (tname == NULL) {
|
||||
err = jvmti->Allocate((jlong)(UNNAMED_LEN + 1), (unsigned char**)&tname);
|
||||
check_jvmti_status(jni, err, "get_method_class_name: error in JVMTI Allocate");
|
||||
strncpy(tname, UNNAMED_STR, UNNAMED_LEN);
|
||||
tname[UNNAMED_LEN] = '\0';
|
||||
}
|
||||
return tname;
|
||||
}
|
||||
|
||||
static char*
|
||||
|
Loading…
x
Reference in New Issue
Block a user