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