Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/mlvm/share/MultiThreadedTest.java
41155 views
/*1* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package vm.mlvm.share;2425import vm.share.options.Option;2627import java.util.concurrent.CyclicBarrier;282930public abstract class MultiThreadedTest extends MlvmTest {3132@Option(name = "threadsExtra", default_value = "1",33description = "Summand of absolute thread count that does not"34+ " depend on CPU count")35private int threadsExtra;3637@Option(name = "threadsPerCpu", default_value = "0",38description = "Summand of absolute thread count that is multiplied"39+ " by CPU count")40private int threadsPerCpu;4142protected MultiThreadedTest() {43// fields will be initialized later by the Option framework44}4546protected abstract boolean runThread(int threadNum) throws Throwable;4748protected int calcThreadNum() {49// TODO: multiply by StressThreadFactor: JDK-814297050return threadsPerCpu * Runtime.getRuntime().availableProcessors()51+ threadsExtra;52}5354@Override55public boolean run() throws Throwable {56Thread.UncaughtExceptionHandler exHandler = (Thread t, Throwable e) -> {57markTestFailed("Exception in thread %s" + t.getName(), e);58};59int threadNum = calcThreadNum();60Env.traceNormal("Threads to start in this test: " + threadNum);61final CyclicBarrier startBarrier = new CyclicBarrier(threadNum + 1);6263Thread[] threads = new Thread[threadNum];64for (int i = 0; i < threadNum; i++) {65final int ii = i;66threads[i] = new Thread(() -> {67boolean passed = false;68try {69startBarrier.await();70if (runThread(ii)) {71passed = true;72} else {73Env.complain("Failed test in %s",74Thread.currentThread());75}76} catch (Throwable e) {77Env.complain(e, "Caught exception in %s",78Thread.currentThread());79}80if (!passed) {81markTestFailed("Thread " + Thread.currentThread()82+ " failed");83}84});85threads[i].setUncaughtExceptionHandler(exHandler);86threads[i].start();87}8889startBarrier.await();90Env.traceNormal(threadNum + " threads have started");9192for (int i = 0; i < threadNum; i++) {93threads[i].join();94}9596Env.traceNormal("All threads have finished");97return true;98}99100}101102103