1 /* 2 * Copyright (c) 2013, 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 jdk.vm.ci.meta; 24 25 /** 26 * This object holds probability information for a set of items that were profiled at a specific 27 * BCI. The precision of the supplied values may vary, but a runtime that provides this information 28 * should be aware that it will be used to guide performance-critical decisions like speculative 29 * inlining, etc. 30 * 31 * @param <T> a subclass of AbstractProfiledItem 32 * @param <U> the class of the items that are profiled at the specific BCI and for which 33 * probabilities are stored. E.g., a ResolvedJavaType or a ResolvedJavaMethod. 34 */ 35 public abstract class AbstractJavaProfile<T extends AbstractProfiledItem<U>, U> { 36 37 private final double notRecordedProbability; 38 private final T[] pitems; 39 40 /** 41 * 42 * @param notRecordedProbability 43 * @param pitems 44 */ 45 @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "caller transfers ownership of the `pitems` array parameter") 46 public AbstractJavaProfile(double notRecordedProbability, T[] pitems) { 47 this.pitems = pitems; 48 assert !Double.isNaN(notRecordedProbability); 49 this.notRecordedProbability = notRecordedProbability; 50 assert isSorted(); 51 assert totalProbablility() >= 0 && totalProbablility() <= 1.0001 : totalProbablility() + " " + this; 52 } 53 54 private double totalProbablility() { 55 double total = notRecordedProbability; 56 for (T item : pitems) { 57 total += item.probability; 58 } 59 return total; 60 } 61 62 /** 63 * Determines if an array of profiled items are sorted in descending order of their 64 * probabilities. 65 */ 66 private boolean isSorted() { 67 for (int i = 1; i < pitems.length; i++) { 68 if (pitems[i - 1].getProbability() < pitems[i].getProbability()) { 69 return false; 70 } 71 } 72 return true; 73 } 74 75 /** 76 * Returns the estimated probability of all types that could not be recorded due to profiling 77 * limitations. 78 * 79 * @return double value ≥ 0.0 and ≤ 1.0 80 */ 81 public double getNotRecordedProbability() { 82 return notRecordedProbability; 83 } 84 85 protected T[] getItems() { 86 return pitems; 87 } 88 89 /** 90 * Searches for an entry of a given resolved Java type. 91 * 92 * @param type the type for which an entry should be searched 93 * @return the entry or null if no entry for this type can be found 94 */ 95 public T findEntry(ResolvedJavaType type) { 96 if (pitems != null) { 97 for (T pt : pitems) { 98 if (pt.getItem().equals(type)) { 99 return pt; 100 } 101 } 102 } 103 return null; 104 } 105 106 @Override 107 public String toString() { 108 StringBuilder builder = new StringBuilder(); 109 builder.append(this.getClass().getName()); 110 builder.append("["); 111 if (pitems != null) { 112 for (T pt : pitems) { 113 builder.append(pt.toString()); 114 builder.append(", "); 115 } 116 } 117 builder.append(this.notRecordedProbability); 118 builder.append("]"); 119 return builder.toString(); 120 } 121 122 public boolean isIncluded(U item) { 123 if (this.getNotRecordedProbability() > 0.0) { 124 return true; 125 } else { 126 for (int i = 0; i < getItems().length; i++) { 127 T pitem = getItems()[i]; 128 U curType = pitem.getItem(); 129 if (curType == item) { 130 return true; 131 } 132 } 133 } 134 return false; 135 } 136 137 @Override 138 public boolean equals(Object obj) { 139 if (obj == this) { 140 return true; 141 } 142 if (!(obj instanceof AbstractJavaProfile)) { 143 return false; 144 } 145 AbstractJavaProfile<?, ?> that = (AbstractJavaProfile<?, ?>) obj; 146 if (that.notRecordedProbability != notRecordedProbability) { 147 return false; 148 } 149 if (that.pitems.length != pitems.length) { 150 return false; 151 } 152 for (int i = 0; i < pitems.length; ++i) { 153 if (!pitems[i].equals(that.pitems[i])) { 154 return false; 155 } 156 } 157 return true; 158 } 159 160 @Override 161 public int hashCode() { 162 return (int) Double.doubleToLongBits(notRecordedProbability) + pitems.length * 13; 163 } 164 }