test/tools/javac/lib/JavacTestingAbstractThreadedTest.java

Sun, 27 Jan 2013 19:38:44 +0000

author
vromero
date
Sun, 27 Jan 2013 19:38:44 +0000
changeset 1528
cbcd9b484759
parent 1520
5c956be64b9e
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8006944: javac, combo tests should print out the number of threads used
Reviewed-by: mcimadamore

     1 /*
     2  * Copyright (c) 2010, 2013, 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  */
    24 import java.io.PrintWriter;
    25 import java.io.StringWriter;
    26 import java.util.concurrent.ExecutorService;
    27 import java.util.concurrent.Executors;
    28 import java.util.concurrent.ThreadFactory;
    29 import java.util.concurrent.TimeUnit;
    30 import java.util.concurrent.atomic.AtomicInteger;
    31 import javax.tools.JavaCompiler;
    32 import javax.tools.StandardJavaFileManager;
    33 import javax.tools.ToolProvider;
    35 /**
    36  * An abstract superclass for threaded tests.
    37  *
    38  * This class will try to read a property named test.concurrency.
    39  * The property can be provided by passing this option to jtreg:
    40  * -javaoption:-Dtest.concurrency=#
    41  *
    42  * If the property is not set the class will use a heuristic to determine the
    43  * maximum number of threads that can be fired to execute a given test.
    44  *
    45  * This code will have to be revisited if jprt starts using concurrency for
    46  * for running jtreg tests.
    47  */
    48 public abstract class JavacTestingAbstractThreadedTest {
    50     protected static AtomicInteger numberOfThreads = new AtomicInteger();
    52     protected static int getThreadPoolSize() {
    53         Integer testConc = Integer.getInteger("test.concurrency");
    54         if (testConc != null) return testConc;
    55         int cores = Runtime.getRuntime().availableProcessors();
    56         numberOfThreads.set(Math.max(2, Math.min(8, cores / 2)));
    57         return numberOfThreads.get();
    58     }
    60     protected static void checkAfterExec() throws InterruptedException {
    61         checkAfterExec(true);
    62     };
    64     protected static boolean throwAssertionOnError = true;
    66     protected static boolean printAll = false;
    68     protected static StringWriter errSWriter = new StringWriter();
    69     protected static PrintWriter errWriter = new PrintWriter(errSWriter);
    71     protected static StringWriter outSWriter = new StringWriter();
    72     protected static PrintWriter outWriter = new PrintWriter(outSWriter);
    74     protected static void checkAfterExec(boolean printCheckCount)
    75             throws InterruptedException {
    76         pool.shutdown();
    77         pool.awaitTermination(15, TimeUnit.MINUTES);
    78         if (errCount.get() > 0) {
    79             if (throwAssertionOnError) {
    80                 closePrinters();
    81                 System.err.println(errSWriter.toString());
    82                 throw new AssertionError(
    83                     String.format("%d errors found", errCount.get()));
    84             } else {
    85                 System.err.println(
    86                         String.format("%d errors found", errCount.get()));
    87             }
    88         } else if (printCheckCount) {
    89             outWriter.println("Total check executed: " + checkCount.get());
    90         }
    91         /*
    92          * This output is for supporting debugging. It does not mean that a given
    93          * test had executed that number of threads concurrently. The value printed
    94          * here is the maximum possible amount.
    95          */
    96         closePrinters();
    97         if (printAll) {
    98             System.out.println(errSWriter.toString());
    99             System.out.println(outSWriter.toString());
   100         }
   101         System.out.println("Total number of threads in thread pool: " +
   102                 numberOfThreads.get());
   103     }
   105     protected static void closePrinters() {
   106         errWriter.close();
   107         outWriter.close();
   108     }
   110     protected static void processException(Throwable t) {
   111         errCount.incrementAndGet();
   112         t.printStackTrace(errWriter);
   113         pool.shutdown();
   114     }
   116     //number of checks executed
   117     protected static AtomicInteger checkCount = new AtomicInteger();
   119     //number of errors found while running combo tests
   120     protected static AtomicInteger errCount = new AtomicInteger();
   122     //create default shared JavaCompiler - reused across multiple compilations
   123     protected static JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
   125     protected static ExecutorService pool = Executors.newFixedThreadPool(
   126             getThreadPoolSize(), new ThreadFactory() {
   127         @Override
   128         public Thread newThread(Runnable r) {
   129             Thread t = new Thread(r);
   130             t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
   131                 @Override
   132                 public void uncaughtException(Thread t, Throwable e) {
   133                     pool.shutdown();
   134                     errCount.incrementAndGet();
   135                     e.printStackTrace(System.err);
   136                 }
   137             });
   138             return t;
   139         }
   140     });
   142     /*
   143      * File manager is not thread-safe so it cannot be re-used across multiple
   144      * threads. However we cache per-thread FileManager to avoid excessive
   145      * object creation
   146      */
   147     protected static final ThreadLocal<StandardJavaFileManager> fm =
   148         new ThreadLocal<StandardJavaFileManager>() {
   149             @Override protected StandardJavaFileManager initialValue() {
   150                 return comp.getStandardFileManager(null, null, null);
   151             }
   152         };
   154 }

mercurial