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