1 /*
  2  * Copyright (c) 2003, 2018, 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
 26  *
 27  * @summary Test verifies set/get TLS data and verifies it's consistency.
 28  * Test set TLS with thread name which it belongs to and verify this information when getting test.
 29  *  -- cbThreadStart
 30  *  -- by AgentThread
 31  *
 32  * Test doesn't verify that TLS is not NULL because for some threads TLS is not initialized initially.
 33  * TODO:
 34  *  -- verify that TLS is not NULL (not possible to do with jvmti, ThreadStart might be called too late)
 35  *  -- add more events where TLS is set *first time*, it is needed to test lazily jvmtThreadState init
 36  *  -- set/get TLS from other JavaThreads (not from agent and current thread)
 37  *  -- set/get for suspened (blocked?) threads
 38  *  -- split test to "sanity" and "stress" version
 39  *  -- update properties to run jvmti stress tests non-concurrently?
 40  *
 41  *
 42  * @library /test/lib
 43  * @compile --enable-preview -source ${jdk.version} SetGetThreadLocalStorageStressTest.java
 44  * @run main/othervm/native --enable-preview -agentlib:SetGetThreadLocalStorageStress SetGetThreadLocalStorageStressTest
 45  */
 46 
 47 
 48 import jdk.test.lib.jvmti.DebugeeClass;
 49 
 50 import java.util.ArrayList;
 51 import java.util.concurrent.locks.LockSupport;
 52 
 53 
 54 public class SetGetThreadLocalStorageStressTest extends DebugeeClass {
 55 
 56     static final int DEFAULT_ITERATIONS = 10;
 57 
 58     static {
 59         System.loadLibrary("SetGetThreadLocalStorageStress");
 60     }
 61 
 62     static int status = DebugeeClass.TEST_PASSED;
 63 
 64     public static void main(String argv[]) throws InterruptedException {
 65         int size = DEFAULT_ITERATIONS;
 66         int kernelThreadNum = Runtime.getRuntime().availableProcessors() / 4;
 67         int virtualThreadNum = Runtime.getRuntime().availableProcessors();
 68 
 69         if (kernelThreadNum == 0) {
 70             kernelThreadNum = 2;
 71         }
 72 
 73         if (argv.length > 0) {
 74             size = Integer.parseInt(argv[0]);
 75         }
 76 
 77         // need to sync start with agent thread only when main is started
 78         checkStatus(status);
 79 
 80         long uniqID = 0;
 81         for (int c = 0; c < size; c++) {
 82             ArrayList<Thread> threads = new ArrayList<>(kernelThreadNum + virtualThreadNum);
 83             for (int i = 0; i < kernelThreadNum; i++) {
 84                 TaskMonitor task = new TaskMonitor();
 85                 threads.add(Thread.ofPlatform()
 86                         .name("KernelThread-" + uniqID++)
 87                         .unstarted(task));
 88             }
 89 
 90             for (int i = 0; i < virtualThreadNum; i++) {
 91                 TaskMonitor task = new TaskMonitor();
 92                 threads.add(Thread.ofVirtual()
 93                         .name("VirtualThread-" + uniqID++)
 94                         .unstarted(task));
 95             }
 96 
 97             for (Thread t : threads) {
 98                 t.start();
 99             }
100 
101             for (Thread t : threads) {
102                 t.join();
103             }
104         }
105     }
106 }
107 
108 
109 class TaskMonitor implements Runnable {
110 
111     public void run() {
112         LockSupport.parkNanos(1);
113     }
114 }