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