1 /*
   2  * Copyright (c) 2010, 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 package vm.mlvm.share;
  25 
  26 import vm.share.options.Option;
  27 
  28 import java.util.concurrent.CyclicBarrier;
  29 
  30 
  31 public abstract class MultiThreadedTest extends MlvmTest {
  32 
  33     @Option(name = "threadsExtra", default_value = "1",
  34             description = "Summand of absolute thread count that does not"
  35                     + " depend on CPU count")
  36     private int threadsExtra;
  37 
  38     @Option(name = "threadsPerCpu", default_value = "0",
  39             description = "Summand of absolute thread count that is multiplied"
  40                     + " by CPU count")
  41     private int threadsPerCpu;
  42 
  43     protected MultiThreadedTest() {
  44         // fields will be initialized later by the Option framework
  45     }
  46 
  47     protected abstract boolean runThread(int threadNum) throws Throwable;
  48 
  49     protected int calcThreadNum() {
  50         // TODO: multiply by StressThreadFactor: JDK-8142970
  51         return threadsPerCpu * Runtime.getRuntime().availableProcessors()
  52                 + threadsExtra;
  53     }
  54 
  55     @Override
  56     public boolean run() throws Throwable {
  57         Thread.UncaughtExceptionHandler exHandler = (Thread t, Throwable e) -> {
  58             markTestFailed("Exception in thread %s" + t.getName(), e);
  59         };
  60         int threadNum = calcThreadNum();
  61         Env.traceNormal("Threads to start in this test: " + threadNum);
  62         final CyclicBarrier startBarrier = new CyclicBarrier(threadNum + 1);
  63 
  64         Thread[] threads = new Thread[threadNum];
  65         for (int i = 0; i < threadNum; i++) {
  66             final int ii = i;
  67             threads[i] = new Thread(() -> {
  68                 boolean passed = false;
  69                 try {
  70                     startBarrier.await();
  71                     if (runThread(ii)) {
  72                         passed = true;
  73                     } else {
  74                         Env.complain("Failed test in %s",
  75                                 Thread.currentThread());
  76                     }
  77                 } catch (Throwable e) {
  78                     Env.complain(e, "Caught exception in %s",
  79                             Thread.currentThread());
  80                 }
  81                 if (!passed) {
  82                     markTestFailed("Thread " + Thread.currentThread()
  83                             + " failed");
  84                 }
  85             });
  86             threads[i].setUncaughtExceptionHandler(exHandler);
  87             threads[i].start();
  88         }
  89 
  90         startBarrier.await();
  91         Env.traceNormal(threadNum + " threads have started");
  92 
  93         for (int i = 0; i < threadNum; i++) {
  94             threads[i].join();
  95         }
  96 
  97         Env.traceNormal("All threads have finished");
  98         return true;
  99     }
 100 
 101 }