1 /*
   2  * Copyright (c) 2011, 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 jdk.vm.ci.hotspot;
  24 
  25 import jdk.vm.ci.meta.JavaMethodProfile;
  26 import jdk.vm.ci.meta.JavaTypeProfile;
  27 import jdk.vm.ci.meta.ProfilingInfo;
  28 import jdk.vm.ci.meta.TriState;
  29 
  30 /**
  31  * Base class for accessing the different kinds of data in a HotSpot {@code MethodData}. This is
  32  * similar to {@link ProfilingInfo}, but most methods require a {@link HotSpotMethodData} and the
  33  * exact position within the method data.
  34  */
  35 abstract class HotSpotMethodDataAccessor {
  36 
  37     final int tag;
  38     final int staticSize;
  39     final HotSpotVMConfig config;
  40 
  41     protected HotSpotMethodDataAccessor(HotSpotVMConfig config, int tag, int staticSize) {
  42         this.config = config;
  43         this.tag = tag;
  44         this.staticSize = staticSize;
  45     }
  46 
  47     /**
  48      * Returns the tag stored in the LayoutData header.
  49      *
  50      * @return tag stored in the LayoutData header
  51      */
  52     int getTag() {
  53         return tag;
  54     }
  55 
  56     static int readTag(HotSpotVMConfig config, HotSpotMethodData data, int position) {
  57         final int tag = data.readUnsignedByte(position, config.dataLayoutTagOffset);
  58         assert tag >= config.dataLayoutNoTag && tag <= config.dataLayoutSpeculativeTrapDataTag : "profile data tag out of bounds: " + tag;
  59         return tag;
  60     }
  61 
  62     /**
  63      * Returns the BCI stored in the LayoutData header.
  64      *
  65      * @return an integer between 0 and {@link Short#MAX_VALUE} inclusive, or -1 if not supported
  66      */
  67     int getBCI(HotSpotMethodData data, int position) {
  68         return data.readUnsignedShort(position, config.dataLayoutBCIOffset);
  69     }
  70 
  71     /**
  72      * Computes the size for the specific data at the given position.
  73      *
  74      * @return a value greater than 0
  75      */
  76     final int getSize(HotSpotMethodData data, int position) {
  77         int size = staticSize + getDynamicSize(data, position);
  78         // Sanity check against VM
  79         int vmSize = HotSpotJVMCIRuntime.runtime().compilerToVm.methodDataProfileDataSize(data.metaspaceMethodData, position);
  80         assert size == vmSize : size + " != " + vmSize;
  81         return size;
  82     }
  83 
  84     TriState getExceptionSeen(HotSpotMethodData data, int position) {
  85         final int exceptionsMask = 1 << config.bitDataExceptionSeenFlag;
  86         return TriState.get((getFlags(data, position) & exceptionsMask) != 0);
  87     }
  88 
  89     /**
  90      * @param data
  91      * @param position
  92      */
  93     JavaTypeProfile getTypeProfile(HotSpotMethodData data, int position) {
  94         return null;
  95     }
  96 
  97     /**
  98      * @param data
  99      * @param position
 100      */
 101     JavaMethodProfile getMethodProfile(HotSpotMethodData data, int position) {
 102         return null;
 103     }
 104 
 105     /**
 106      * @param data
 107      * @param position
 108      */
 109     double getBranchTakenProbability(HotSpotMethodData data, int position) {
 110         return -1;
 111     }
 112 
 113     /**
 114      * @param data
 115      * @param position
 116      */
 117     double[] getSwitchProbabilities(HotSpotMethodData data, int position) {
 118         return null;
 119     }
 120 
 121     /**
 122      * @param data
 123      * @param position
 124      */
 125     int getExecutionCount(HotSpotMethodData data, int position) {
 126         return -1;
 127     }
 128 
 129     /**
 130      * @param data
 131      * @param position
 132      */
 133     TriState getNullSeen(HotSpotMethodData data, int position) {
 134         return TriState.UNKNOWN;
 135     }
 136 
 137     protected int getFlags(HotSpotMethodData data, int position) {
 138         return data.readUnsignedByte(position, config.dataLayoutFlagsOffset);
 139     }
 140 
 141     /**
 142      * @param data
 143      * @param position
 144      */
 145     protected int getDynamicSize(HotSpotMethodData data, int position) {
 146         return 0;
 147     }
 148 
 149     abstract StringBuilder appendTo(StringBuilder sb, HotSpotMethodData data, int pos);
 150 
 151 }