1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
/*
* Copyright (c) 2024, 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
* @bug 8247972
*
* @summary converted from VM Testbase nsk/jvmti/GetObjectMonitorUsage/objmonusage003
* DESCRIPTION
* The test checks if the JVMTI function GetObjectMonitorUsage returns
* the expected values for the owner, entry_count, waiter_count
* fields of JVMTI_monitor_info.
* The testcases are the following:
* - unowned object without any threads waiting
* - unowned object with threads waiting to be notified
* - owned object without any threads waiting
* - owned object with N threads waiting to enter the monitor
* - owned object with N threads waiting to be notified
* - owned object with N threads waiting to enter, from 0 to N threads
* waiting to re-enter, from N to 0 threads waiting to be notified
* - all the above scenarios are executed with platform and virtual threads
* @requires vm.jvmti
* @run main/othervm/native
* -Djdk.virtualThreadScheduler.parallelism=10
* -agentlib:ObjectMonitorUsage ObjectMonitorUsage
*/
public class ObjectMonitorUsage {
final static int NUMBER_OF_ENTERING_THREADS = 4;
final static int NUMBER_OF_WAITING_THREADS = 4;
final static int NUMBER_OF_THREADS = NUMBER_OF_ENTERING_THREADS + NUMBER_OF_WAITING_THREADS;
static Object lockCheck = new Object();
native static int getRes();
native static int setTestedMonitor(Object monitor);
native static void ensureBlockedOnEnter(Thread thread);
native static void ensureWaitingToBeNotified(Thread thread);
native static void check(Object obj, Thread owner,
int entryCount, int waiterCount, int notifyWaiterCount);
static void log(String msg) {
System.out.println(msg);
}
static String vtag(boolean isVirtual) {
return isVirtual ? "virtual" : "platform";
}
static void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
// ignore
}
}
static Thread startTask(int idx, TestTask task, boolean isVirtual, String kind) {
Thread thread = isVirtual ? Thread.ofVirtual().name(kind + "VT" + idx).start(task)
: Thread.ofPlatform().name(kind + "PT" + idx).start(task);
task.setName(thread.getName());
task.waitReady();
return thread;
}
static Thread[] startWaitingThreads(boolean isVirtual) {
Thread[] threads = new Thread[NUMBER_OF_WAITING_THREADS];
for (int i = 0; i < NUMBER_OF_WAITING_THREADS; i++) {
// the WaitingTask has to wait to be notified in lockCheck.wait()
Thread thread = startTask(i, new WaitingTask(), isVirtual, "Waiting");
ensureWaitingToBeNotified(thread);
threads[i] = thread;
}
return threads;
}
static Thread[] startEnteringThreads(boolean isVirtual) {
Thread[] threads = new Thread[NUMBER_OF_ENTERING_THREADS];
for (int i = 0; i < NUMBER_OF_ENTERING_THREADS; i++) {
// the EnteringTask has to be blocked at the lockCheck enter
Thread thread = startTask(i, new EnteringTask(), isVirtual, "Entering");
ensureBlockedOnEnter(thread);
threads[i] = thread;
}
return threads;
}
static void joinThreads(Thread[] threads) {
try {
for (Thread t : threads) {
t.join();
}
} catch (InterruptedException e) {
throw new Error("Unexpected " + e);
}
}
static Thread expOwnerThread() {
return Thread.currentThread().isVirtual() ? null : Thread.currentThread();
}
static int expEntryCount() {
return Thread.currentThread().isVirtual() ? 0 : 1;
}
/* Scenario #0:
* - owning: 0
* - entering: 0
* - re-entering: 0
* - to be notified: N
*/
static void test0(boolean isVirtual) {
String vtag = vtag(isVirtual);
log("\n### test0: started " + vtag);
setTestedMonitor(lockCheck);
Thread[] wThreads = startWaitingThreads(isVirtual);
final int expWaitingCount = isVirtual ? 0 : NUMBER_OF_WAITING_THREADS;
// The numbers below describe the testing scenario, not the expected results.
// The expected numbers are different for virtual threads because
// they are not supported by JVMTI GetObjectMonitorUsage.
// entry count: 0
// count of threads waiting to enter: 0
// count of threads waiting to re-enter: 0
// count of threads waiting to be notified: NUMBER_OF_WAITING_THREADS
check(lockCheck, null, 0, // no owner thread
0, // count of threads waiting to enter: 0
expWaitingCount);
synchronized (lockCheck) {
lockCheck.notifyAll();
}
joinThreads(wThreads);
setTestedMonitor(null);
log("### test0: finished " + vtag);
}
/* Scenario #1:
* - owning: 1
* - entering: N
* - re-entering: 0
* - to be notified: 0
*/
static void test1(boolean isVirtual) {
String vtag = vtag(isVirtual);
log("\n### test1: started " + vtag);
setTestedMonitor(lockCheck);
Thread[] eThreads = null;
synchronized (lockCheck) {
// Virtual threads are not supported by GetObjectMonitorUsage.
// Correct the expected values for the virtual thread case.
int expEnteringCount = isVirtual ? 0 : NUMBER_OF_ENTERING_THREADS;
// The numbers below describe the testing scenario, not the expected results.
// The expected numbers are different for virtual threads because
// they are not supported by JVMTI GetObjectMonitorUsage.
// entry count: 1
// count of threads waiting to enter: 0
// count of threads waiting to re-enter: 0
// count of threads waiting to be notified: 0
check(lockCheck, expOwnerThread(), expEntryCount(), 0, 0);
eThreads = startEnteringThreads(isVirtual);
// The numbers below describe the testing scenario, not the expected results.
// The expected numbers are different for virtual threads because
// they are not supported by JVMTI GetObjectMonitorUsage.
// entry count: 1
// count of threads waiting to enter: NUMBER_OF_ENTERING_THREADS
// count of threads waiting to re-enter: 0
// count of threads waiting to be notified: 0
check(lockCheck, expOwnerThread(), expEntryCount(),
expEnteringCount,
0 /* count of threads waiting to be notified: 0 */);
}
joinThreads(eThreads);
setTestedMonitor(null);
log("### test1: finished " + vtag);
}
/* Scenario #2:
* - owning: 1
* - entering: N
* - re-entering: 0
* - to be notified: N
*/
static void test2(boolean isVirtual) throws Error {
String vtag = vtag(isVirtual);
log("\n### test2: started " + vtag);
setTestedMonitor(lockCheck);
Thread[] wThreads = startWaitingThreads(isVirtual);
Thread[] eThreads = null;
synchronized (lockCheck) {
// Virtual threads are not supported by the GetObjectMonitorUsage.
// Correct the expected values for the virtual thread case.
int expEnteringCount = isVirtual ? 0 : NUMBER_OF_ENTERING_THREADS;
int expWaitingCount = isVirtual ? 0 : NUMBER_OF_WAITING_THREADS;
eThreads = startEnteringThreads(isVirtual);
// The numbers below describe the testing scenario, not the expected results.
// The expected numbers are different for virtual threads because
// they are not supported by JVMTI GetObjectMonitorUsage.
// entry count: 1
// count of threads waiting to enter: NUMBER_OF_ENTERING_THREADS
// count of threads waiting to re-enter: 0
// count of threads waiting to be notified: NUMBER_OF_WAITING_THREADS
check(lockCheck, expOwnerThread(), expEntryCount(),
expEnteringCount,
expWaitingCount);
lockCheck.notifyAll();
}
joinThreads(wThreads);
joinThreads(eThreads);
setTestedMonitor(null);
log("### test2: finished " + vtag);
}
/* Scenario #3:
* Initially we have:
* - owning: 1
* - entering: 0
* - re-entering: 0
* - to be notified: N
*
* The threads waiting to be notified are being notified one-by-one
* until all threads are blocked on re-entering the monitor.
* The numbers of entering/re-entering and waiting threads are checked
* for correctness after each notification.
*/
static void test3(boolean isVirtual) throws Error {
String vtag = vtag(isVirtual);
log("\n### test3: started " + vtag);
setTestedMonitor(lockCheck);
Thread[] wThreads = startWaitingThreads(isVirtual);
Thread[] eThreads = null;
synchronized (lockCheck) {
// Virtual threads are not supported by GetObjectMonitorUsage.
// Correct the expected values for the virtual thread case.
int expEnteringCount = isVirtual ? 0 : NUMBER_OF_ENTERING_THREADS;
int expWaitingCount = isVirtual ? 0 : NUMBER_OF_WAITING_THREADS;
// The numbers below describe the testing scenario, not the expected results.
// The expected numbers are different for virtual threads because
// they are not supported by JVMTI GetObjectMonitorUsage.
// entry count: 1
// count of threads waiting to enter: 0
// count of threads waiting to re-enter: 0
// count of threads waiting to be notified: NUMBER_OF_WAITING_THREADS
check(lockCheck, expOwnerThread(), expEntryCount(),
0, // number of threads waiting to enter or re-enter
expWaitingCount);
eThreads = startEnteringThreads(isVirtual);
// The numbers below describe the testing scenario, not the expected results.
// The expected numbers are different for virtual threads because
// they are not supported by JVMTI GetObjectMonitorUsage.
// entry count: 1
// count of threads waiting to enter: NUMBER_OF_ENTERING_THREADS
// count of threads waiting to re-enter: 0
// count of threads waiting to be notified: NUMBER_OF_WAITING_THREADS
check(lockCheck, expOwnerThread(), expEntryCount(),
expEnteringCount,
expWaitingCount);
for (int i = 0; i < NUMBER_OF_WAITING_THREADS; i++) {
expEnteringCount = isVirtual ? 0 : NUMBER_OF_ENTERING_THREADS + i + 1;
expWaitingCount = isVirtual ? 0 : NUMBER_OF_WAITING_THREADS - i - 1;
lockCheck.notify(); // notify waiting threads one by one
// now the notified WaitingTask has to be blocked on the lockCheck re-enter
// The numbers below describe the testing scenario, not the expected results.
// The expected numbers are different for virtual threads because
// they are not supported by JVMTI GetObjectMonitorUsage.
// entry count: 1
// count of threads waiting to enter: NUMBER_OF_ENTERING_THREADS
// count of threads waiting to re-enter: i + 1
// count of threads waiting to be notified: NUMBER_OF_WAITING_THREADS - i - 1
check(lockCheck, expOwnerThread(), expEntryCount(),
expEnteringCount,
expWaitingCount);
}
}
joinThreads(wThreads);
joinThreads(eThreads);
setTestedMonitor(null);
log("### test3: finished " + vtag);
}
static void test(boolean isVirtual) {
test0(isVirtual);
test1(isVirtual);
test2(isVirtual);
test3(isVirtual);
}
public static void main(String args[]) {
log("\n### main: started\n");
check(lockCheck, null, 0, 0, 0);
test(false); // test platform threads
test(true); // test virtual threads
check(lockCheck, null, 0, 0, 0);
if (getRes() > 0) {
throw new RuntimeException("Failed status returned from the agent");
}
log("\n### main: finished\n");
}
static abstract class TestTask implements Runnable {
volatile boolean ready = false;
String name;
public abstract void run();
String getName() { return name; }
void setName(String name) { this.name = name; }
void waitReady() {
try {
while (!ready) {
Thread.sleep(10);
}
} catch (InterruptedException e) {
throw new Error("Unexpected " + e);
}
}
}
static class EnteringTask extends TestTask {
public void run() {
ready = true;
synchronized (lockCheck) {
}
}
}
static class WaitingTask extends TestTask {
public void run() {
synchronized (lockCheck) {
try {
ready = true;
// no protection against spurious wakeups here
lockCheck.wait();
} catch (InterruptedException e) {
throw new Error("Unexpected " + e);
}
}
}
}
}
|