vromero@1482: /* vromero@1482: * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. vromero@1482: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. vromero@1482: * vromero@1482: * This code is free software; you can redistribute it and/or modify it vromero@1482: * under the terms of the GNU General Public License version 2 only, as vromero@1482: * published by the Free Software Foundation. vromero@1482: * vromero@1482: * This code is distributed in the hope that it will be useful, but WITHOUT vromero@1482: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or vromero@1482: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License vromero@1482: * version 2 for more details (a copy is included in the LICENSE file that vromero@1482: * accompanied this code). vromero@1482: * vromero@1482: * You should have received a copy of the GNU General Public License version vromero@1482: * 2 along with this work; if not, write to the Free Software Foundation, vromero@1482: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. vromero@1482: * vromero@1482: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA vromero@1482: * or visit www.oracle.com if you need additional information or have any vromero@1482: * questions. vromero@1482: */ vromero@1482: vromero@1482: import java.io.PrintWriter; vromero@1482: import java.io.StringWriter; vromero@1482: import java.util.concurrent.ExecutorService; vromero@1482: import java.util.concurrent.Executors; vromero@1482: import java.util.concurrent.ThreadFactory; vromero@1520: import java.util.concurrent.TimeUnit; vromero@1482: import java.util.concurrent.atomic.AtomicInteger; vromero@1482: import javax.tools.JavaCompiler; vromero@1482: import javax.tools.StandardJavaFileManager; vromero@1482: import javax.tools.ToolProvider; vromero@1482: vromero@1482: /** vromero@1482: * An abstract superclass for threaded tests. vromero@1482: * vromero@1482: * This class will try to read a property named test.concurrency. vromero@1482: * The property can be provided by passing this option to jtreg: vromero@1482: * -javaoption:-Dtest.concurrency=# vromero@1482: * vromero@1482: * If the property is not set the class will use a heuristic to determine the vromero@1482: * maximum number of threads that can be fired to execute a given test. vromero@1528: * vromero@1528: * This code will have to be revisited if jprt starts using concurrency for vromero@1528: * for running jtreg tests. vromero@1482: */ vromero@1482: public abstract class JavacTestingAbstractThreadedTest { vromero@1482: vromero@1528: protected static AtomicInteger numberOfThreads = new AtomicInteger(); vromero@1528: vromero@1482: protected static int getThreadPoolSize() { vromero@1482: Integer testConc = Integer.getInteger("test.concurrency"); vromero@1482: if (testConc != null) return testConc; vromero@1482: int cores = Runtime.getRuntime().availableProcessors(); vromero@1528: numberOfThreads.set(Math.max(2, Math.min(8, cores / 2))); vromero@1528: return numberOfThreads.get(); vromero@1482: } vromero@1482: vromero@1482: protected static void checkAfterExec() throws InterruptedException { vromero@1482: checkAfterExec(true); vromero@1482: }; vromero@1482: vromero@1482: protected static boolean throwAssertionOnError = true; vromero@1482: vromero@1482: protected static boolean printAll = false; vromero@1482: vromero@1482: protected static StringWriter errSWriter = new StringWriter(); vromero@1482: protected static PrintWriter errWriter = new PrintWriter(errSWriter); vromero@1482: vromero@1482: protected static StringWriter outSWriter = new StringWriter(); vromero@1482: protected static PrintWriter outWriter = new PrintWriter(outSWriter); vromero@1482: vromero@1482: protected static void checkAfterExec(boolean printCheckCount) vromero@1482: throws InterruptedException { vromero@1482: pool.shutdown(); vromero@1520: pool.awaitTermination(15, TimeUnit.MINUTES); vromero@1482: if (errCount.get() > 0) { vromero@1482: if (throwAssertionOnError) { vromero@1482: closePrinters(); vromero@1482: System.err.println(errSWriter.toString()); vromero@1482: throw new AssertionError( vromero@1482: String.format("%d errors found", errCount.get())); vromero@1482: } else { vromero@1482: System.err.println( vromero@1482: String.format("%d errors found", errCount.get())); vromero@1482: } vromero@1482: } else if (printCheckCount) { vromero@1482: outWriter.println("Total check executed: " + checkCount.get()); vromero@1482: } vromero@1528: /* vromero@1528: * This output is for supporting debugging. It does not mean that a given vromero@1528: * test had executed that number of threads concurrently. The value printed vromero@1528: * here is the maximum possible amount. vromero@1528: */ vromero@1482: closePrinters(); vromero@1482: if (printAll) { vromero@1482: System.out.println(errSWriter.toString()); vromero@1482: System.out.println(outSWriter.toString()); vromero@1482: } vromero@1528: System.out.println("Total number of threads in thread pool: " + vromero@1528: numberOfThreads.get()); vromero@1482: } vromero@1482: vromero@1482: protected static void closePrinters() { vromero@1482: errWriter.close(); vromero@1482: outWriter.close(); vromero@1482: } vromero@1482: vromero@1482: protected static void processException(Throwable t) { vromero@1482: errCount.incrementAndGet(); vromero@1482: t.printStackTrace(errWriter); vromero@1482: pool.shutdown(); vromero@1482: } vromero@1482: vromero@1482: //number of checks executed vromero@1482: protected static AtomicInteger checkCount = new AtomicInteger(); vromero@1482: vromero@1482: //number of errors found while running combo tests vromero@1482: protected static AtomicInteger errCount = new AtomicInteger(); vromero@1482: vromero@1482: //create default shared JavaCompiler - reused across multiple compilations vromero@1482: protected static JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); vromero@1482: vromero@1482: protected static ExecutorService pool = Executors.newFixedThreadPool( vromero@1482: getThreadPoolSize(), new ThreadFactory() { vromero@1482: @Override vromero@1482: public Thread newThread(Runnable r) { vromero@1482: Thread t = new Thread(r); vromero@1482: t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { vromero@1482: @Override vromero@1482: public void uncaughtException(Thread t, Throwable e) { vromero@1482: pool.shutdown(); vromero@1482: errCount.incrementAndGet(); vromero@1482: e.printStackTrace(System.err); vromero@1482: } vromero@1482: }); vromero@1482: return t; vromero@1482: } vromero@1482: }); vromero@1482: vromero@1482: /* vromero@1482: * File manager is not thread-safe so it cannot be re-used across multiple vromero@1482: * threads. However we cache per-thread FileManager to avoid excessive vromero@1482: * object creation vromero@1482: */ vromero@1482: protected static final ThreadLocal fm = vromero@1482: new ThreadLocal() { vromero@1482: @Override protected StandardJavaFileManager initialValue() { vromero@1482: return comp.getStandardFileManager(null, null, null); vromero@1482: } vromero@1482: }; vromero@1482: vromero@1482: }