1 /*
2 * Copyright (c) 2015, 2017, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.tools.javac.api;
27
28 import java.io.PrintStream;
29 import java.io.Writer;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.HashMap;
34
35 import com.sun.source.tree.ClassTree;
36 import com.sun.source.tree.CompilationUnitTree;
37 import com.sun.source.util.JavacTask;
38 import com.sun.source.util.TaskEvent;
39 import com.sun.source.util.TaskEvent.Kind;
40 import com.sun.source.util.TaskListener;
41 import com.sun.source.util.TreeScanner;
42 import com.sun.tools.javac.code.Kinds;
43 import com.sun.tools.javac.code.Symbol;
44 import com.sun.tools.javac.code.Symtab;
45 import com.sun.tools.javac.code.Type;
46 import com.sun.tools.javac.code.Type.ClassType;
47 import com.sun.tools.javac.code.TypeTag;
48 import com.sun.tools.javac.code.Types;
49 import com.sun.tools.javac.comp.Annotate;
50 import com.sun.tools.javac.comp.Check;
51 import com.sun.tools.javac.comp.CompileStates;
52 import com.sun.tools.javac.comp.Enter;
84 * <p>
85 * When the compilation redefines sensitive classes (e.g. classes in the the java.* packages), the
86 * task/context is not reused.
87 * <p>
88 * When the task is reused, then packages that were already listed won't be listed again.
89 * <p>
90 * Care must be taken to only return tasks that won't be used by the original caller.
91 * <p>
92 * Care must also be taken when custom components are installed, as those are not cleaned when the
93 * task/context is reused, and subsequent getTask may return a task based on a context with these
94 * custom components.
95 *
96 * <p><b>This is NOT part of any supported API.
97 * If you write code that depends on this, you do so at your own risk.
98 * This code and its internal interfaces are subject to change or
99 * deletion without notice.</b>
100 */
101 public class JavacTaskPool {
102
103 private static final JavacTool systemProvider = JavacTool.create();
104
105 private final int maxPoolSize;
106 private final Map<List<String>, List<ReusableContext>> options2Contexts = new HashMap<>();
107 private int id;
108
109 private int statReused = 0;
110 private int statNew = 0;
111 private int statPolluted = 0;
112 private int statRemoved = 0;
113
114 /**Creates the pool.
115 *
116 * @param maxPoolSize maximum number of tasks/context that will be kept in the pool.
117 */
118 public JavacTaskPool(int maxPoolSize) {
119 this.maxPoolSize = maxPoolSize;
120 }
121
122 /**Creates a new task as if by {@link javax.tools.JavaCompiler#getTask} and runs the provided
123 * worker with it. The task is only valid while the worker is running. The internal structures
124 * may be reused from some previous compilation.
125 *
126 * @param out a Writer for additional output from the compiler;
142 * {@linkplain Throwable#getCause() cause} will be the error in
143 * user code.
144 * @throws IllegalArgumentException if any of the options are invalid,
145 * or if any of the given compilation units are of other kind than
146 * {@linkplain JavaFileObject.Kind#SOURCE source}
147 */
148 public <Z> Z getTask(Writer out,
149 JavaFileManager fileManager,
150 DiagnosticListener<? super JavaFileObject> diagnosticListener,
151 Iterable<String> options,
152 Iterable<String> classes,
153 Iterable<? extends JavaFileObject> compilationUnits,
154 Worker<Z> worker) {
155 List<String> opts =
156 StreamSupport.stream(options.spliterator(), false)
157 .collect(Collectors.toCollection(ArrayList::new));
158
159 ReusableContext ctx;
160
161 synchronized (this) {
162 List<ReusableContext> cached =
163 options2Contexts.getOrDefault(opts, Collections.emptyList());
164
165 if (cached.isEmpty()) {
166 ctx = new ReusableContext(opts);
167 statNew++;
168 } else {
169 ctx = cached.remove(0);
170 statReused++;
171 }
172 }
173
174 ctx.useCount++;
175
176 JavacTaskImpl task =
177 (JavacTaskImpl) systemProvider.getTask(out, fileManager, diagnosticListener,
178 opts, classes, compilationUnits, ctx);
179
180 task.addTaskListener(ctx);
181
182 Z result = worker.withTask(task);
183
184 //not returning the context to the pool if task crashes with an exception
185 //the task/context may be in a broken state
186 ctx.clear();
187 if (ctx.polluted) {
188 statPolluted++;
189 } else {
190 task.cleanup();
191 synchronized (this) {
192 while (cacheSize() + 1 > maxPoolSize) {
193 ReusableContext toRemove =
194 options2Contexts.values()
195 .stream()
196 .flatMap(Collection::stream)
197 .sorted((c1, c2) -> c1.timeStamp < c2.timeStamp ? -1 : 1)
198 .findFirst()
199 .get();
200 options2Contexts.get(toRemove.arguments).remove(toRemove);
201 statRemoved++;
202 }
203 options2Contexts.computeIfAbsent(ctx.arguments, x -> new ArrayList<>()).add(ctx);
204 ctx.timeStamp = id++;
205 }
206 }
207
208 return result;
209 }
210 //where:
211 private long cacheSize() {
212 return options2Contexts.values().stream().flatMap(Collection::stream).count();
213 }
214
215 public void printStatistics(PrintStream out) {
216 out.println(statReused + " reused Contexts");
217 out.println(statNew + " newly created Contexts");
218 out.println(statPolluted + " polluted Contexts");
219 out.println(statRemoved + " removed Contexts");
220 }
221
222 public interface Worker<Z> {
223 public Z withTask(JavacTask task);
|
1 /*
2 * Copyright (c) 2015, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.tools.javac.api;
27
28 import java.io.PrintStream;
29 import java.io.Writer;
30 import java.util.ArrayDeque;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.Deque;
34 import java.util.HashMap;
35
36 import com.sun.source.tree.ClassTree;
37 import com.sun.source.tree.CompilationUnitTree;
38 import com.sun.source.util.JavacTask;
39 import com.sun.source.util.TaskEvent;
40 import com.sun.source.util.TaskEvent.Kind;
41 import com.sun.source.util.TaskListener;
42 import com.sun.source.util.TreeScanner;
43 import com.sun.tools.javac.code.Kinds;
44 import com.sun.tools.javac.code.Symbol;
45 import com.sun.tools.javac.code.Symtab;
46 import com.sun.tools.javac.code.Type;
47 import com.sun.tools.javac.code.Type.ClassType;
48 import com.sun.tools.javac.code.TypeTag;
49 import com.sun.tools.javac.code.Types;
50 import com.sun.tools.javac.comp.Annotate;
51 import com.sun.tools.javac.comp.Check;
52 import com.sun.tools.javac.comp.CompileStates;
53 import com.sun.tools.javac.comp.Enter;
85 * <p>
86 * When the compilation redefines sensitive classes (e.g. classes in the the java.* packages), the
87 * task/context is not reused.
88 * <p>
89 * When the task is reused, then packages that were already listed won't be listed again.
90 * <p>
91 * Care must be taken to only return tasks that won't be used by the original caller.
92 * <p>
93 * Care must also be taken when custom components are installed, as those are not cleaned when the
94 * task/context is reused, and subsequent getTask may return a task based on a context with these
95 * custom components.
96 *
97 * <p><b>This is NOT part of any supported API.
98 * If you write code that depends on this, you do so at your own risk.
99 * This code and its internal interfaces are subject to change or
100 * deletion without notice.</b>
101 */
102 public class JavacTaskPool {
103
104 private static final JavacTool systemProvider = JavacTool.create();
105 private static final Deque<ReusableContext> EMPTY_DEQUE = new ArrayDeque<>(0);
106
107 private final int maxPoolSize;
108 private final Map<List<String>, Deque<ReusableContext>> options2Contexts = new HashMap<>();
109 private int id;
110
111 private int statReused = 0;
112 private int statNew = 0;
113 private int statPolluted = 0;
114 private int statRemoved = 0;
115
116 /**Creates the pool.
117 *
118 * @param maxPoolSize maximum number of tasks/context that will be kept in the pool.
119 */
120 public JavacTaskPool(int maxPoolSize) {
121 this.maxPoolSize = maxPoolSize;
122 }
123
124 /**Creates a new task as if by {@link javax.tools.JavaCompiler#getTask} and runs the provided
125 * worker with it. The task is only valid while the worker is running. The internal structures
126 * may be reused from some previous compilation.
127 *
128 * @param out a Writer for additional output from the compiler;
144 * {@linkplain Throwable#getCause() cause} will be the error in
145 * user code.
146 * @throws IllegalArgumentException if any of the options are invalid,
147 * or if any of the given compilation units are of other kind than
148 * {@linkplain JavaFileObject.Kind#SOURCE source}
149 */
150 public <Z> Z getTask(Writer out,
151 JavaFileManager fileManager,
152 DiagnosticListener<? super JavaFileObject> diagnosticListener,
153 Iterable<String> options,
154 Iterable<String> classes,
155 Iterable<? extends JavaFileObject> compilationUnits,
156 Worker<Z> worker) {
157 List<String> opts =
158 StreamSupport.stream(options.spliterator(), false)
159 .collect(Collectors.toCollection(ArrayList::new));
160
161 ReusableContext ctx;
162
163 synchronized (this) {
164 Deque<ReusableContext> cached =
165 options2Contexts.getOrDefault(opts, EMPTY_DEQUE);
166
167 if (cached.isEmpty()) {
168 ctx = new ReusableContext(opts);
169 statNew++;
170 } else {
171 ctx = cached.remove();
172 statReused++;
173 }
174 }
175
176 ctx.useCount++;
177
178 JavacTaskImpl task =
179 (JavacTaskImpl) systemProvider.getTask(out, fileManager, diagnosticListener,
180 opts, classes, compilationUnits, ctx);
181
182 task.addTaskListener(ctx);
183
184 Z result = worker.withTask(task);
185
186 //not returning the context to the pool if task crashes with an exception
187 //the task/context may be in a broken state
188 ctx.clear();
189 if (ctx.polluted) {
190 statPolluted++;
191 } else {
192 task.cleanup();
193 synchronized (this) {
194 while (cacheSize() + 1 > maxPoolSize) {
195 ReusableContext toRemove =
196 options2Contexts.values()
197 .stream()
198 .flatMap(Collection::stream)
199 .sorted((c1, c2) -> c1.timeStamp < c2.timeStamp ? -1 : 1)
200 .findFirst()
201 .get();
202 options2Contexts.get(toRemove.arguments).remove(toRemove);
203 statRemoved++;
204 }
205 options2Contexts.computeIfAbsent(ctx.arguments, x -> new ArrayDeque<>()).add(ctx);
206 ctx.timeStamp = id++;
207 }
208 }
209
210 return result;
211 }
212 //where:
213 private long cacheSize() {
214 return options2Contexts.values().stream().flatMap(Collection::stream).count();
215 }
216
217 public void printStatistics(PrintStream out) {
218 out.println(statReused + " reused Contexts");
219 out.println(statNew + " newly created Contexts");
220 out.println(statPolluted + " polluted Contexts");
221 out.println(statRemoved + " removed Contexts");
222 }
223
224 public interface Worker<Z> {
225 public Z withTask(JavacTask task);
|