1 /*
   2  * Copyright (c) 2016, 2019, 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 package org.graalvm.compiler.hotspot;
  26 
  27 import static org.graalvm.compiler.hotspot.HotSpotGraalCompiler.fmt;
  28 import static org.graalvm.compiler.hotspot.HotSpotGraalCompiler.str;
  29 
  30 import java.util.Comparator;
  31 import java.util.HashMap;
  32 import java.util.Map;
  33 import java.util.Map.Entry;
  34 import java.util.SortedSet;
  35 import java.util.TreeSet;
  36 
  37 import org.graalvm.compiler.debug.TTY;
  38 import org.graalvm.compiler.options.Option;
  39 import org.graalvm.compiler.options.OptionKey;
  40 import org.graalvm.compiler.options.OptionType;
  41 import org.graalvm.compiler.options.OptionValues;
  42 
  43 import jdk.vm.ci.code.CompilationRequest;
  44 import jdk.vm.ci.meta.ResolvedJavaMethod;
  45 
  46 class CompilationCounters {
  47 
  48     public static class Options {
  49         // @formatter:off
  50         @Option(help = "The number of compilations allowed for any method before " +
  51                        "the VM exits (a value of 0 means there is no limit).", type = OptionType.Debug)
  52         public static final OptionKey<Integer> CompilationCountLimit = new OptionKey<>(0);
  53         // @formatter:on
  54     }
  55 
  56     private final OptionValues options;
  57 
  58     CompilationCounters(OptionValues options) {
  59         TTY.println("Warning: Compilation counters enabled, excessive recompilation of a method will cause a failure!");
  60         this.options = options;
  61     }
  62 
  63     private final Map<ResolvedJavaMethod, Integer> counters = new HashMap<>();
  64 
  65     /**
  66      * Counts the number of compilations for the {@link ResolvedJavaMethod} of the
  67      * {@link CompilationRequest}. If the number of compilations exceeds
  68      * {@link Options#CompilationCountLimit} this method prints an error message and exits the VM.
  69      *
  70      * @param method the method about to be compiled
  71      */
  72     synchronized void countCompilation(ResolvedJavaMethod method) {
  73         Integer val = counters.get(method);
  74         val = val != null ? val + 1 : 1;
  75         counters.put(method, val);
  76         if (val > Options.CompilationCountLimit.getValue(options)) {
  77             TTY.printf("Error. Method %s was compiled too many times. Number of compilations: %d\n", fmt(method),
  78                             CompilationCounters.Options.CompilationCountLimit.getValue(options));
  79             TTY.println("==================================== High compilation counters ====================================");
  80             SortedSet<Map.Entry<ResolvedJavaMethod, Integer>> sortedCounters = new TreeSet<>(new CounterComparator());
  81             for (Map.Entry<ResolvedJavaMethod, Integer> e : counters.entrySet()) {
  82                 sortedCounters.add(e);
  83             }
  84             for (Map.Entry<ResolvedJavaMethod, Integer> entry : sortedCounters) {
  85                 if (entry.getValue() >= Options.CompilationCountLimit.getValue(options) / 2) {
  86                     TTY.out.printf("%d\t%s%n", entry.getValue(), str(entry.getKey()));
  87                 }
  88             }
  89             TTY.flush();
  90             HotSpotGraalServices.exit(-1);
  91         }
  92     }
  93 
  94     static final class CounterComparator implements Comparator<Map.Entry<ResolvedJavaMethod, Integer>> {
  95         @Override
  96         public int compare(Entry<ResolvedJavaMethod, Integer> o1, Entry<ResolvedJavaMethod, Integer> o2) {
  97             if (o1.getValue() < o2.getValue()) {
  98                 return -1;
  99             }
 100             if (o1.getValue() > o2.getValue()) {
 101                 return 1;
 102             }
 103             return str(o1.getKey()).compareTo(str(o2.getKey()));
 104         }
 105     }
 106 }