1 /*
  2  * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test id=default
 26  * @summary Test virtual threads using synchronized
 27  * @library /test/lib
 28  * @requires vm.continuations
 29  * @modules java.base/java.lang:+open
 30  * @run junit/othervm MonitorsTest
 31  */
 32 
 33 /*
 34  * @test id=Xint
 35  * @library /test/lib
 36  * @requires vm.continuations
 37  * @modules java.base/java.lang:+open
 38  * @run junit/othervm -Xint MonitorsTest
 39  */
 40 
 41 /*
 42  * @test id=Xcomp-TieredStopAtLevel3
 43  * @library /test/lib
 44  * @requires vm.continuations
 45  * @modules java.base/java.lang:+open
 46  * @run junit/othervm -Xcomp -XX:TieredStopAtLevel=3 MonitorsTest
 47  */
 48 
 49 /*
 50  * @test id=Xcomp-noTieredCompilation
 51  * @summary Test virtual threads using synchronized
 52  * @library /test/lib
 53  * @requires vm.continuations
 54  * @modules java.base/java.lang:+open
 55  * @run junit/othervm -Xcomp -XX:-TieredCompilation MonitorsTest
 56  */
 57 
 58 /*
 59  * @test id=gc
 60  * @requires vm.debug == true & vm.continuations
 61  * @library /test/lib
 62  * @modules java.base/java.lang:+open
 63  * @run junit/othervm -XX:+UnlockDiagnosticVMOptions -XX:+FullGCALot -XX:FullGCALotInterval=1000 MonitorsTest
 64  */
 65 
 66 import java.util.concurrent.atomic.AtomicInteger;
 67 import java.util.concurrent.*;
 68 
 69 import jdk.test.lib.thread.VThreadScheduler;
 70 import org.junit.jupiter.api.Test;
 71 import static org.junit.jupiter.api.Assertions.*;
 72 
 73 class MonitorsTest {
 74     final int CARRIER_COUNT = 8;
 75     ExecutorService scheduler = Executors.newFixedThreadPool(CARRIER_COUNT);
 76 
 77     static final Object globalLock = new Object();
 78     static volatile boolean finish = false;
 79     static volatile int counter = 0;
 80 
 81     /////////////////////////////////////////////////////////////////////
 82     //////////////////////////// BASIC TESTS ////////////////////////////
 83     /////////////////////////////////////////////////////////////////////
 84 
 85     static final Runnable FOO = () -> {
 86         Object lock = new Object();
 87         synchronized(lock) {
 88             while(!finish) {
 89                 Thread.yield();
 90             }
 91         }
 92         System.err.println("Exiting FOO from thread " + Thread.currentThread().getName());
 93     };
 94 
 95     static final Runnable BAR = () -> {
 96         synchronized(globalLock) {
 97             counter++;
 98         }
 99         System.err.println("Exiting BAR from thread " + Thread.currentThread().getName());
100     };
101 
102     /**
103      *  Test yield while holding monitor.
104      */
105     @Test
106     void testBasic() throws Exception {
107         final int VT_COUNT = CARRIER_COUNT;
108 
109         // Create first batch of VT threads.
110         Thread firstBatch[] = new Thread[VT_COUNT];
111         for (int i = 0; i < VT_COUNT; i++) {
112             firstBatch[i] = VThreadScheduler.virtualThreadBuilder(scheduler).name("FirstBatchVT-" + i).start(FOO);
113         }
114 
115         // Give time for all threads to reach Thread.yield
116         Thread.sleep(1000);
117 
118         // Create second batch of VT threads.
119         Thread secondBatch[] = new Thread[VT_COUNT];
120         for (int i = 0; i < VT_COUNT; i++) {
121             secondBatch[i] = VThreadScheduler.virtualThreadBuilder(scheduler).name("SecondBatchVT-" + i).start(BAR);
122         }
123 
124         while(counter != VT_COUNT) {}
125 
126         finish = true;
127 
128         for (int i = 0; i < VT_COUNT; i++) {
129             firstBatch[i].join();
130         }
131         for (int i = 0; i < VT_COUNT; i++) {
132             secondBatch[i].join();
133         }
134     }
135 
136     static final Runnable BAR2 = () -> {
137         synchronized(globalLock) {
138             counter++;
139         }
140         recursive2(10);
141         System.err.println("Exiting BAR2 from thread " + Thread.currentThread().getName() + "with counter=" + counter);
142     };
143 
144     static void recursive2(int count) {
145         synchronized(Thread.currentThread()) {
146             if (count > 0) {
147                 recursive2(count - 1);
148             } else {
149                 synchronized(globalLock) {
150                     counter++;
151                     Thread.yield();
152                 }
153             }
154         }
155     }
156 
157     /**
158      *  Test yield while holding monitor with recursive locking.
159      */
160     @Test
161     void testRecursive() throws Exception {
162         final int VT_COUNT = CARRIER_COUNT;
163         counter = 0;
164         finish = false;
165 
166         // Create first batch of VT threads.
167         Thread firstBatch[] = new Thread[VT_COUNT];
168         for (int i = 0; i < VT_COUNT; i++) {
169             firstBatch[i] = VThreadScheduler.virtualThreadBuilder(scheduler).name("FirstBatchVT-" + i).start(FOO);
170         }
171 
172         // Give time for all threads to reach Thread.yield
173         Thread.sleep(1000);
174 
175         // Create second batch of VT threads.
176         Thread secondBatch[] = new Thread[VT_COUNT];
177         for (int i = 0; i < VT_COUNT; i++) {
178             secondBatch[i] = VThreadScheduler.virtualThreadBuilder(scheduler).name("SecondBatchVT-" + i).start(BAR2);
179         }
180 
181         while(counter != 2*VT_COUNT) {}
182 
183         finish = true;
184 
185         for (int i = 0; i < VT_COUNT; i++) {
186             firstBatch[i].join();
187         }
188         for (int i = 0; i < VT_COUNT; i++) {
189             secondBatch[i].join();
190         }
191     }
192 
193     static final Runnable FOO3 = () -> {
194         synchronized(globalLock) {
195             while(!finish) {
196                 Thread.yield();
197             }
198         }
199         System.err.println("Exiting FOO3 from thread " + Thread.currentThread().getName());
200     };
201 
202     /**
203      *  Test contention on monitorenter.
204      */
205     @Test
206     void testContention() throws Exception {
207         final int VT_COUNT = CARRIER_COUNT * 8;
208         counter = 0;
209         finish = false;
210 
211         // Create batch of VT threads.
212         Thread batch[] = new Thread[VT_COUNT];
213         for (int i = 0; i < VT_COUNT; i++) {
214             batch[i] = VThreadScheduler.virtualThreadBuilder(scheduler).name("BatchVT-" + i).start(FOO3);
215         }
216 
217         // Give time for all threads to reach synchronized(globalLock)
218         Thread.sleep(2000);
219 
220         finish = true;
221 
222         for (int i = 0; i < VT_COUNT; i++) {
223             batch[i].join();
224         }
225     }
226 
227     /////////////////////////////////////////////////////////////////////
228     //////////////////////////// MAIN TESTS /////////////////////////////
229     /////////////////////////////////////////////////////////////////////
230 
231     static final int MONITORS_CNT = 12;
232     static Object[] globalLockArray;
233     static AtomicInteger workerCount = new AtomicInteger(0);
234 
235     static void recursive4_1(int depth, int lockNumber) {
236         if (depth > 0) {
237             recursive4_1(depth - 1, lockNumber);
238         } else {
239             if (Math.random() < 0.5) {
240                 Thread.yield();
241             }
242             recursive4_2(lockNumber);
243         }
244     }
245 
246     static void recursive4_2(int lockNumber) {
247         if (lockNumber + 2 <= MONITORS_CNT - 1) {
248             lockNumber += 2;
249             synchronized(globalLockArray[lockNumber]) {
250                 Thread.yield();
251                 recursive4_2(lockNumber);
252             }
253         }
254     }
255 
256     static final Runnable FOO4 = () -> {
257         while (!finish) {
258             int lockNumber = ThreadLocalRandom.current().nextInt(0, MONITORS_CNT - 1);
259             synchronized(globalLockArray[lockNumber]) {
260                 recursive4_1(lockNumber, lockNumber);
261             }
262         }
263         workerCount.getAndIncrement();
264         System.err.println("Exiting FOO4 from thread " + Thread.currentThread().getName());
265     };
266 
267     /**
268      *  Test contention on monitorenter with extra monitors on stack shared by all threads.
269      */
270     @Test
271     void testContentionMultipleMonitors() throws Exception {
272         final int VT_COUNT = CARRIER_COUNT * 8;
273         workerCount.getAndSet(0);
274         finish = false;
275 
276         globalLockArray = new Object[MONITORS_CNT];
277         for (int i = 0; i < MONITORS_CNT; i++) {
278             globalLockArray[i] = new Object();
279         }
280 
281         Thread batch[] = new Thread[VT_COUNT];
282         for (int i = 0; i < VT_COUNT; i++) {
283             batch[i] = VThreadScheduler.virtualThreadBuilder(scheduler).name("BatchVT-" + i).start(FOO4);
284         }
285 
286         Thread.sleep(10000);
287         finish = true;
288 
289         for (int i = 0; i < VT_COUNT; i++) {
290             batch[i].join();
291         }
292 
293         if (workerCount.get() != VT_COUNT) {
294             throw new RuntimeException("testContentionMultipleMonitors2 failed. Expected " + VT_COUNT + "but found " + workerCount.get());
295         }
296     }
297 
298 
299     static void recursive5_1(int depth, int lockNumber, Object[] myLockArray) {
300         if (depth > 0) {
301             recursive5_1(depth - 1, lockNumber, myLockArray);
302         } else {
303             if (Math.random() < 0.5) {
304                 Thread.yield();
305             }
306             recursive5_2(lockNumber, myLockArray);
307         }
308     }
309 
310     static void recursive5_2(int lockNumber, Object[] myLockArray) {
311         if (lockNumber + 2 <= MONITORS_CNT - 1) {
312             lockNumber += 2;
313             synchronized (myLockArray[lockNumber]) {
314                 if (Math.random() < 0.5) {
315                     Thread.yield();
316                 }
317                 synchronized (globalLockArray[lockNumber]) {
318                     Thread.yield();
319                     recursive5_2(lockNumber, myLockArray);
320                 }
321             }
322         }
323     }
324 
325     static final Runnable FOO5 = () -> {
326         Object[] myLockArray = new Object[MONITORS_CNT];
327         for (int i = 0; i < MONITORS_CNT; i++) {
328             myLockArray[i] = new Object();
329         }
330 
331         while (!finish) {
332             int lockNumber = ThreadLocalRandom.current().nextInt(0, MONITORS_CNT - 1);
333             synchronized (myLockArray[lockNumber]) {
334                 synchronized (globalLockArray[lockNumber]) {
335                     recursive5_1(lockNumber, lockNumber, myLockArray);
336                 }
337             }
338         }
339         workerCount.getAndIncrement();
340         System.err.println("Exiting FOO5 from thread " + Thread.currentThread().getName());
341     };
342 
343     /**
344      *  Test contention on monitorenter with extra monitors on stack both local only and shared by all threads.
345      */
346     @Test
347     void testContentionMultipleMonitors2() throws Exception {
348         final int VT_COUNT = CARRIER_COUNT * 8;
349         workerCount.getAndSet(0);
350         finish = false;
351 
352         globalLockArray = new Object[MONITORS_CNT];
353         for (int i = 0; i < MONITORS_CNT; i++) {
354             globalLockArray[i] = new Object();
355         }
356 
357         // Create batch of VT threads.
358         Thread batch[] = new Thread[VT_COUNT];
359         for (int i = 0; i < VT_COUNT; i++) {
360             //Thread.ofVirtual().name("FirstBatchVT-" + i).start(FOO);
361             batch[i] = VThreadScheduler.virtualThreadBuilder(scheduler).name("BatchVT-" + i).start(FOO5);
362         }
363 
364         Thread.sleep(10000);
365 
366         finish = true;
367 
368         for (int i = 0; i < VT_COUNT; i++) {
369             batch[i].join();
370         }
371 
372         if (workerCount.get() != VT_COUNT) {
373             throw new RuntimeException("testContentionMultipleMonitors2 failed. Expected " + VT_COUNT + "but found " + workerCount.get());
374         }
375     }
376 
377     static synchronized void recursive6(int depth, Object myLock) {
378         if (depth > 0) {
379             recursive6(depth - 1, myLock);
380         } else {
381             if (Math.random() < 0.5) {
382                 Thread.yield();
383             } else {
384                 synchronized (myLock) {
385                     Thread.yield();
386                 }
387             }
388         }
389     }
390 
391     static final Runnable FOO6 = () -> {
392         Object myLock = new Object();
393 
394         while (!finish) {
395             int lockNumber = ThreadLocalRandom.current().nextInt(0, MONITORS_CNT - 1);
396             synchronized (myLock) {
397                 synchronized (globalLockArray[lockNumber]) {
398                     recursive6(lockNumber, myLock);
399                 }
400             }
401         }
402         workerCount.getAndIncrement();
403         System.err.println("Exiting FOO5 from thread " + Thread.currentThread().getName());
404     };
405 
406     /**
407      *  Test contention on monitorenter with synchronized methods.
408      */
409     @Test
410     void testContentionMultipleMonitors3() throws Exception {
411         final int VT_COUNT = CARRIER_COUNT * 8;
412         workerCount.getAndSet(0);
413         finish = false;
414 
415 
416         globalLockArray = new Object[MONITORS_CNT];
417         for (int i = 0; i < MONITORS_CNT; i++) {
418             globalLockArray[i] = new Object();
419         }
420 
421         // Create batch of VT threads.
422         Thread batch[] = new Thread[VT_COUNT];
423         for (int i = 0; i < VT_COUNT; i++) {
424             batch[i] = VThreadScheduler.virtualThreadBuilder(scheduler).name("BatchVT-" + i).start(FOO6);
425         }
426 
427         Thread.sleep(10000);
428 
429         finish = true;
430 
431         for (int i = 0; i < VT_COUNT; i++) {
432             batch[i].join();
433         }
434 
435         if (workerCount.get() != VT_COUNT) {
436             throw new RuntimeException("testContentionMultipleMonitors2 failed. Expected " + VT_COUNT + "but found " + workerCount.get());
437         }
438     }
439 
440     @Test
441     void waitNotifyTest() throws Exception {
442         int threadCount = 1000;
443         int waitTime = 50;
444         long start = System.currentTimeMillis();
445         Thread[] vthread = new Thread[threadCount];
446         while (System.currentTimeMillis() - start < 5000) {
447             CountDownLatch latchStart = new CountDownLatch(threadCount);
448             CountDownLatch latchFinish = new CountDownLatch(threadCount);
449             Object object = new Object();
450             for (int i = 0; i < threadCount; i++) {
451                 vthread[i] = Thread.ofVirtual().start(() -> {
452                     synchronized (object) {
453                         try {
454                             latchStart.countDown();
455                             object.wait(waitTime);
456                         } catch (InterruptedException e) {
457                             //do nothing;
458                         }
459                     }
460                     latchFinish.countDown();
461                 });
462             }
463             try {
464                 latchStart.await();
465                 synchronized (object) {
466                     object.notifyAll();
467                 }
468                 latchFinish.await();
469                 for (int i = 0; i < threadCount; i++) {
470                     vthread[i].join();
471                 }
472             } catch (InterruptedException e) {
473                 //do nothing;
474             }
475         }
476     }
477 }