< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskPool.java

Print this page
rev 50962 : [mq]: 8206122-Use-Queue-in-place-of-ArrayList-when-need-to-remove-first-element
   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;
  53 import com.sun.tools.javac.comp.Modules;
  54 import com.sun.tools.javac.main.Arguments;
  55 import com.sun.tools.javac.main.JavaCompiler;
  56 import com.sun.tools.javac.tree.JCTree.JCClassDecl;
  57 
  58 import javax.tools.Diagnostic;
  59 import javax.tools.DiagnosticListener;
  60 import javax.tools.JavaFileManager;
  61 import javax.tools.JavaFileObject;
  62 
  63 import java.util.HashSet;
  64 import java.util.List;
  65 import java.util.Map;
  66 import java.util.Set;
  67 import java.util.stream.Collectors;
  68 import java.util.stream.StreamSupport;
  69 
  70 import com.sun.tools.javac.model.JavacElements;

  71 import com.sun.tools.javac.util.Context;
  72 import com.sun.tools.javac.util.DefinedBy;
  73 import com.sun.tools.javac.util.DefinedBy.Api;
  74 import com.sun.tools.javac.util.Log;
  75 
  76 /**
  77  * A pool of reusable JavacTasks. When a task is no valid anymore, it is returned to the pool,
  78  * and its Context may be reused for future processing in some cases. The reuse is achieved
  79  * by replacing some components (most notably JavaCompiler and Log) with reusable counterparts,
  80  * and by cleaning up leftovers from previous compilation.
  81  * <p>
  82  * For each combination of options, a separate task/context is created and kept, as most option
  83  * values are cached inside components themselves.
  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.HashMap;
  34 import java.util.HashSet;
  35 import java.util.List;
  36 import java.util.Map;
  37 import java.util.Queue;
  38 import java.util.Set;
  39 import java.util.stream.Collectors;
  40 import java.util.stream.StreamSupport;
  41 
  42 import javax.tools.Diagnostic;
  43 import javax.tools.DiagnosticListener;
  44 import javax.tools.JavaFileManager;
  45 import javax.tools.JavaFileObject;
  46 
  47 import com.sun.source.tree.ClassTree;
  48 import com.sun.source.tree.CompilationUnitTree;
  49 import com.sun.source.util.JavacTask;
  50 import com.sun.source.util.TaskEvent;
  51 import com.sun.source.util.TaskEvent.Kind;
  52 import com.sun.source.util.TaskListener;
  53 import com.sun.source.util.TreeScanner;
  54 import com.sun.tools.javac.code.Kinds;
  55 import com.sun.tools.javac.code.Symbol;
  56 import com.sun.tools.javac.code.Symtab;
  57 import com.sun.tools.javac.code.Type;
  58 import com.sun.tools.javac.code.Type.ClassType;
  59 import com.sun.tools.javac.code.TypeTag;
  60 import com.sun.tools.javac.code.Types;
  61 import com.sun.tools.javac.comp.Annotate;
  62 import com.sun.tools.javac.comp.Check;
  63 import com.sun.tools.javac.comp.CompileStates;
  64 import com.sun.tools.javac.comp.Enter;
  65 import com.sun.tools.javac.comp.Modules;
  66 import com.sun.tools.javac.main.Arguments;
  67 import com.sun.tools.javac.main.JavaCompiler;














  68 import com.sun.tools.javac.model.JavacElements;
  69 import com.sun.tools.javac.tree.JCTree.JCClassDecl;
  70 import com.sun.tools.javac.util.Context;
  71 import com.sun.tools.javac.util.DefinedBy;
  72 import com.sun.tools.javac.util.DefinedBy.Api;
  73 import com.sun.tools.javac.util.Log;
  74 
  75 /**
  76  * A pool of reusable JavacTasks. When a task is no valid anymore, it is returned to the pool,
  77  * and its Context may be reused for future processing in some cases. The reuse is achieved
  78  * by replacing some components (most notably JavaCompiler and Log) with reusable counterparts,
  79  * and by cleaning up leftovers from previous compilation.
  80  * <p>
  81  * For each combination of options, a separate task/context is created and kept, as most option
  82  * values are cached inside components themselves.
  83  * <p>
  84  * When the compilation redefines sensitive classes (e.g. classes in the the java.* packages), the
  85  * task/context is not reused.
  86  * <p>
  87  * When the task is reused, then packages that were already listed won't be listed again.
  88  * <p>
  89  * Care must be taken to only return tasks that won't be used by the original caller.
  90  * <p>
  91  * Care must also be taken when custom components are installed, as those are not cleaned when the
  92  * task/context is reused, and subsequent getTask may return a task based on a context with these
  93  * custom components.
  94  *
  95  * <p><b>This is NOT part of any supported API.
  96  * If you write code that depends on this, you do so at your own risk.
  97  * This code and its internal interfaces are subject to change or
  98  * deletion without notice.</b>
  99  */
 100 public class JavacTaskPool {
 101 
 102     private static final JavacTool systemProvider = JavacTool.create();
 103     private static final Queue<ReusableContext> EMPTY_QUEUE = new ArrayDeque<>(0);
 104 
 105     private final int maxPoolSize;
 106     private final Map<List<String>, Queue<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             Queue<ReusableContext> cached =
 163                     options2Contexts.getOrDefault(opts, EMPTY_QUEUE);
 164 
 165             if (cached.isEmpty()) {
 166                 ctx = new ReusableContext(opts);
 167                 statNew++;
 168             } else {
 169                 ctx = cached.remove();
 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 ArrayDeque<>()).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);


< prev index next >