001/*
002 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package jdk.internal.jvmci.meta;
024
025/**
026 * This object holds probability information for a set of items that were profiled at a specific
027 * BCI. The precision of the supplied values may vary, but a runtime that provides this information
028 * should be aware that it will be used to guide performance-critical decisions like speculative
029 * inlining, etc.
030 *
031 * @param <T> a subclass of AbstractProfiledItem
032 * @param <U> the class of the items that are profiled at the specific BCI and for which
033 *            probabilities are stored. E.g., a ResolvedJavaType or a ResolvedJavaMethod.
034 */
035public abstract class AbstractJavaProfile<T extends AbstractProfiledItem<U>, U> {
036
037    private final double notRecordedProbability;
038    private final T[] pitems;
039
040    public AbstractJavaProfile(double notRecordedProbability, T[] pitems) {
041        this.pitems = pitems;
042        assert !Double.isNaN(notRecordedProbability);
043        this.notRecordedProbability = notRecordedProbability;
044        assert isSorted();
045        assert totalProbablility() >= 0 && totalProbablility() <= 1.0001 : totalProbablility() + " " + this;
046    }
047
048    private double totalProbablility() {
049        double total = notRecordedProbability;
050        for (T item : pitems) {
051            total += item.probability;
052        }
053        return total;
054    }
055
056    /**
057     * Determines if an array of profiled items are sorted in descending order of their
058     * probabilities.
059     */
060    private boolean isSorted() {
061        for (int i = 1; i < pitems.length; i++) {
062            if (pitems[i - 1].getProbability() < pitems[i].getProbability()) {
063                return false;
064            }
065        }
066        return true;
067    }
068
069    /**
070     * Returns the estimated probability of all types that could not be recorded due to profiling
071     * limitations.
072     *
073     * @return double value &ge; 0.0 and &le; 1.0
074     */
075    public double getNotRecordedProbability() {
076        return notRecordedProbability;
077    }
078
079    protected T[] getItems() {
080        return pitems;
081    }
082
083    /**
084     * Searches for an entry of a given resolved Java type.
085     *
086     * @param type the type for which an entry should be searched
087     * @return the entry or null if no entry for this type can be found
088     */
089    public T findEntry(ResolvedJavaType type) {
090        if (pitems != null) {
091            for (T pt : pitems) {
092                if (pt.getItem().equals(type)) {
093                    return pt;
094                }
095            }
096        }
097        return null;
098    }
099
100    @Override
101    public String toString() {
102        StringBuilder builder = new StringBuilder();
103        builder.append(this.getClass().getName());
104        builder.append("[");
105        if (pitems != null) {
106            for (T pt : pitems) {
107                builder.append(pt.toString());
108                builder.append(", ");
109            }
110        }
111        builder.append(this.notRecordedProbability);
112        builder.append("]");
113        return builder.toString();
114    }
115
116    public boolean isIncluded(U item) {
117        if (this.getNotRecordedProbability() > 0.0) {
118            return true;
119        } else {
120            for (int i = 0; i < getItems().length; i++) {
121                T pitem = getItems()[i];
122                U curType = pitem.getItem();
123                if (curType == item) {
124                    return true;
125                }
126            }
127        }
128        return false;
129    }
130
131    @Override
132    public boolean equals(Object obj) {
133        if (obj == this) {
134            return true;
135        }
136        if (!(obj instanceof AbstractJavaProfile)) {
137            return false;
138        }
139        AbstractJavaProfile<?, ?> that = (AbstractJavaProfile<?, ?>) obj;
140        if (that.notRecordedProbability != notRecordedProbability) {
141            return false;
142        }
143        if (that.pitems.length != pitems.length) {
144            return false;
145        }
146        for (int i = 0; i < pitems.length; ++i) {
147            if (!pitems[i].equals(that.pitems[i])) {
148                return false;
149            }
150        }
151        return true;
152    }
153
154    @Override
155    public int hashCode() {
156        return (int) Double.doubleToLongBits(notRecordedProbability) + pitems.length * 13;
157    }
158}