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