1 /*
   2  * Copyright (c) 2014, 2016, 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  * @bug 8044131
  27  * @summary Makes sure sjavac poolsize option is honored.
  28  * @modules jdk.compiler/com.sun.tools.sjavac.comp
  29  *          jdk.compiler/com.sun.tools.sjavac.server
  30  * @build Wrapper
  31  * @run main Wrapper PooledExecution
  32  */
  33 import java.util.concurrent.CountDownLatch;
  34 import java.util.concurrent.atomic.AtomicInteger;
  35 
  36 import com.sun.tools.javac.main.Main.Result;
  37 import com.sun.tools.sjavac.comp.PooledSjavac;
  38 import com.sun.tools.sjavac.server.Sjavac;
  39 
  40 
  41 public class PooledExecution {
  42 
  43     public static void main(String[] args) throws InterruptedException {
  44         new PooledExecutionTest().runTest();
  45     }
  46 
  47     static class PooledExecutionTest {
  48 
  49         final int POOL_SIZE = 15;
  50         final int NUM_REQUESTS = 100;
  51 
  52         // Number of tasks that has not yet started
  53         CountDownLatch leftToStart = new CountDownLatch(NUM_REQUESTS);
  54 
  55         // Highest number of concurrently active request seen
  56         int highWaterMark = 0;
  57 
  58         public void runTest() throws InterruptedException {
  59             ConcurrencyLoggingService loggingService = new ConcurrencyLoggingService();
  60             final Sjavac service = new PooledSjavac(loggingService, POOL_SIZE);
  61 
  62             // Keep track of the number of finished tasks so we can make sure all
  63             // tasks finishes gracefully upon shutdown.
  64             Thread[] tasks = new Thread[NUM_REQUESTS];
  65             final AtomicInteger tasksFinished = new AtomicInteger(0);
  66 
  67             for (int i = 0; i < NUM_REQUESTS; i++) {
  68                 tasks[i] = new Thread() {
  69                     public void run() {
  70                         service.compile(new String[0]);
  71                         tasksFinished.incrementAndGet();
  72                     }
  73                 };
  74                 tasks[i].start();
  75             }
  76 
  77             // Wait for all tasks to start (but not necessarily run to completion)
  78             leftToStart.await();
  79 
  80             // Shutdown before all tasks are completed
  81             System.out.println("Shutting down!");
  82             service.shutdown();
  83 
  84             // Wait for all tasks to complete
  85             for (Thread t : tasks)
  86                 t.join();
  87 
  88             if (tasksFinished.get() != NUM_REQUESTS) {
  89                 throw new AssertionError(tasksFinished.get() + " out of " +
  90                         NUM_REQUESTS + " finished. Broken shutdown?");
  91             }
  92 
  93             if (highWaterMark > POOL_SIZE) {
  94                 throw new AssertionError("Pool size overused: " + highWaterMark +
  95                                          " used out of " + POOL_SIZE + " allowed.");
  96             }
  97 
  98             // Assuming more than POOL_SIZE requests can be processed within 1 sek:
  99             if (highWaterMark < POOL_SIZE) {
 100                 throw new AssertionError("Pool size underused: " + highWaterMark +
 101                                          " used out of " + POOL_SIZE + " allowed.");
 102             }
 103         }
 104 
 105 
 106         private class ConcurrencyLoggingService implements Sjavac {
 107 
 108             // Keeps track of currently active requests
 109             AtomicInteger activeRequests = new AtomicInteger(0);
 110 
 111             @Override
 112             public Result compile(String[] args) {
 113                 leftToStart.countDown();
 114                 int numActiveRequests = activeRequests.incrementAndGet();
 115                 System.out.printf("Left to start: %2d / Currently active: %2d%n",
 116                                   leftToStart.getCount(),
 117                                   numActiveRequests);
 118                 highWaterMark = Math.max(highWaterMark, numActiveRequests);
 119                 try {
 120                     Thread.sleep(1000);
 121                 } catch (InterruptedException ie) {
 122                     throw new RuntimeException("Interrupted", ie);
 123                 }
 124                 activeRequests.decrementAndGet();
 125                 System.out.println("Task completed");
 126                 return Result.OK;
 127             }
 128 
 129             @Override
 130             public void shutdown() {
 131             }
 132         }
 133     }
 134 }