1 /*
   2  * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2012, 2014 SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 // This file defines a set of macros which are used by the c++-interpreter
  27 // for updating a method's methodData object.
  28 
  29 
  30 #ifndef SHARE_VM_INTERPRETER_BYTECODEINTERPRETERPROFILING_HPP
  31 #define SHARE_VM_INTERPRETER_BYTECODEINTERPRETERPROFILING_HPP
  32 
  33 
  34 // Global settings /////////////////////////////////////////////////////////////
  35 
  36 
  37 // Enables profiling support.
  38 #if defined(COMPILER2)
  39 #define CC_INTERP_PROFILE
  40 #endif
  41 
  42 // Enables assertions for profiling code (also works in product-builds).
  43 // #define CC_INTERP_PROFILE_WITH_ASSERTIONS
  44 
  45 
  46 #ifdef CC_INTERP
  47 
  48 // Empty dummy implementations if profiling code is switched off. //////////////
  49 
  50 #ifndef CC_INTERP_PROFILE
  51 
  52 #define SET_MDX(mdx)
  53 
  54 #define BI_PROFILE_GET_OR_CREATE_METHOD_DATA(exception_handler)                \
  55   if (ProfileInterpreter) {                                                    \
  56     ShouldNotReachHere();                                                      \
  57   }
  58 
  59 #define BI_PROFILE_ALIGN_TO_CURRENT_BCI()
  60 
  61 #define BI_PROFILE_UPDATE_JUMP()
  62 #define BI_PROFILE_UPDATE_BRANCH(is_taken)
  63 #define BI_PROFILE_UPDATE_RET(bci)
  64 #define BI_PROFILE_SUBTYPECHECK_FAILED(receiver)
  65 #define BI_PROFILE_UPDATE_CHECKCAST(null_seen, receiver)
  66 #define BI_PROFILE_UPDATE_INSTANCEOF(null_seen, receiver)
  67 #define BI_PROFILE_UPDATE_CALL()
  68 #define BI_PROFILE_UPDATE_FINALCALL()
  69 #define BI_PROFILE_UPDATE_VIRTUALCALL(receiver)
  70 #define BI_PROFILE_UPDATE_SWITCH(switch_index)
  71 
  72 
  73 #else
  74 
  75 
  76 // Non-dummy implementations ///////////////////////////////////////////////////
  77 
  78 // Accessors for the current method data pointer 'mdx'.
  79 #define MDX()        (istate->mdx())
  80 #define SET_MDX(mdx)                                                           \
  81   if (TraceProfileInterpreter) {                                               \
  82     /* Let it look like TraceBytecodes' format. */                             \
  83     tty->print_cr("[%d]           %4d  "                                       \
  84                   "mdx " PTR_FORMAT "(%d)"                                     \
  85                   "  "                                                         \
  86                   " \t-> " PTR_FORMAT "(%d)",                                  \
  87                 (int) THREAD->osthread()->thread_id(),                         \
  88                 BCI(),                                                         \
  89                 p2i(MDX()),                                                    \
  90                 (MDX() == NULL                                                 \
  91                  ? 0                                                           \
  92                  : istate->method()->method_data()->dp_to_di((address)MDX())), \
  93                 p2i(mdx),                                                      \
  94                 istate->method()->method_data()->dp_to_di((address)mdx)        \
  95                 );                                                             \
  96   };                                                                           \
  97   istate->set_mdx(mdx);
  98 
  99 
 100 // Dumps the profiling method data for the current method.
 101 #ifdef PRODUCT
 102 #define BI_PROFILE_PRINT_METHOD_DATA()
 103 #else  // PRODUCT
 104 #define BI_PROFILE_PRINT_METHOD_DATA()                                         \
 105   {                                                                            \
 106     ttyLocker ttyl;                                                            \
 107     MethodData *md = istate->method()->method_data();                          \
 108     tty->cr();                                                                 \
 109     tty->print("method data at mdx " PTR_FORMAT "(0) for",                     \
 110                p2i(md->data_layout_at(md->bci_to_di(0))));                     \
 111     istate->method()->print_short_name(tty);                                   \
 112     tty->cr();                                                                 \
 113     if (md != NULL) {                                                          \
 114       md->print_data_on(tty);                                                  \
 115       address mdx = (address) MDX();                                           \
 116       if (mdx != NULL) {                                                       \
 117         tty->print_cr("current mdx " PTR_FORMAT "(%d)",                        \
 118                       p2i(mdx),                                                \
 119                       istate->method()->method_data()->dp_to_di(mdx));         \
 120       }                                                                        \
 121     } else {                                                                   \
 122       tty->print_cr("no method data");                                         \
 123     }                                                                          \
 124   }
 125 #endif // PRODUCT
 126 
 127 
 128 // Gets or creates the profiling method data and initializes mdx.
 129 #define BI_PROFILE_GET_OR_CREATE_METHOD_DATA(exception_handler)                \
 130   if (ProfileInterpreter && MDX() == NULL) {                                   \
 131     /* Mdx is not yet initialized for this activation. */                      \
 132     MethodData *md = istate->method()->method_data();                          \
 133     if (md == NULL) {                                                          \
 134       MethodCounters* mcs;                                                     \
 135       GET_METHOD_COUNTERS(mcs);                                                \
 136       /* The profiling method data doesn't exist for this method, */           \
 137       /* create it if the counters have overflowed. */                         \
 138       if (mcs->invocation_counter()                                            \
 139                          ->reached_ProfileLimit(mcs->backedge_counter())) {    \
 140         /* Must use CALL_VM, because an async exception may be pending. */     \
 141         CALL_VM((InterpreterRuntime::profile_method(THREAD)),                  \
 142                 exception_handler);                                            \
 143         md = istate->method()->method_data();                                  \
 144         if (md != NULL) {                                                      \
 145           if (TraceProfileInterpreter) {                                       \
 146             BI_PROFILE_PRINT_METHOD_DATA();                                    \
 147           }                                                                    \
 148           Method *m = istate->method();                                        \
 149           int bci = m->bci_from(pc);                                           \
 150           jint di = md->bci_to_di(bci);                                        \
 151           SET_MDX(md->data_layout_at(di));                                     \
 152         }                                                                      \
 153       }                                                                        \
 154     } else {                                                                   \
 155       /* The profiling method data exists, align the method data pointer */    \
 156       /* mdx to the current bytecode index. */                                 \
 157       if (TraceProfileInterpreter) {                                           \
 158         BI_PROFILE_PRINT_METHOD_DATA();                                        \
 159       }                                                                        \
 160       SET_MDX(md->data_layout_at(md->bci_to_di(BCI())));                       \
 161     }                                                                          \
 162   }
 163 
 164 
 165 // Asserts that the current method data pointer mdx corresponds
 166 // to the current bytecode.
 167 #if defined(CC_INTERP_PROFILE_WITH_ASSERTIONS)
 168 #define BI_PROFILE_CHECK_MDX()                                                 \
 169   {                                                                            \
 170     MethodData *md = istate->method()->method_data();                          \
 171     address mdx  = (address) MDX();                                            \
 172     address mdx2 = (address) md->data_layout_at(md->bci_to_di(BCI()));         \
 173     guarantee(md   != NULL, "1");                                              \
 174     guarantee(mdx  != NULL, "2");                                              \
 175     guarantee(mdx2 != NULL, "3");                                              \
 176     if (mdx != mdx2) {                                                         \
 177       BI_PROFILE_PRINT_METHOD_DATA();                                          \
 178       fatal3("invalid mdx at bci %d:"                                          \
 179              " was " PTR_FORMAT                                                \
 180              " but expected " PTR_FORMAT,                                      \
 181              BCI(),                                                            \
 182              mdx,                                                              \
 183              mdx2);                                                            \
 184     }                                                                          \
 185   }
 186 #else
 187 #define BI_PROFILE_CHECK_MDX()
 188 #endif
 189 
 190 
 191 // Aligns the method data pointer mdx to the current bytecode index.
 192 #define BI_PROFILE_ALIGN_TO_CURRENT_BCI()                                      \
 193   if (ProfileInterpreter && MDX() != NULL) {                                   \
 194     MethodData *md = istate->method()->method_data();                          \
 195     SET_MDX(md->data_layout_at(md->bci_to_di(BCI())));                         \
 196   }
 197 
 198 
 199 // Updates profiling data for a jump.
 200 #define BI_PROFILE_UPDATE_JUMP()                                               \
 201   if (ProfileInterpreter && MDX() != NULL) {                                   \
 202     BI_PROFILE_CHECK_MDX();                                                    \
 203     JumpData::increment_taken_count_no_overflow(MDX());                        \
 204     /* Remember last branch taken count. */                                    \
 205     mdo_last_branch_taken_count = JumpData::taken_count(MDX());                \
 206     SET_MDX(JumpData::advance_taken(MDX()));                                   \
 207   }
 208 
 209 
 210 // Updates profiling data for a taken/not taken branch.
 211 #define BI_PROFILE_UPDATE_BRANCH(is_taken)                                     \
 212   if (ProfileInterpreter && MDX() != NULL) {                                   \
 213     BI_PROFILE_CHECK_MDX();                                                    \
 214     if (is_taken) {                                                            \
 215       BranchData::increment_taken_count_no_overflow(MDX());                    \
 216       /* Remember last branch taken count. */                                  \
 217       mdo_last_branch_taken_count = BranchData::taken_count(MDX());            \
 218       SET_MDX(BranchData::advance_taken(MDX()));                               \
 219     } else {                                                                   \
 220       BranchData::increment_not_taken_count_no_overflow(MDX());                \
 221       SET_MDX(BranchData::advance_not_taken(MDX()));                           \
 222     }                                                                          \
 223   }
 224 
 225 
 226 // Updates profiling data for a ret with given bci.
 227 #define BI_PROFILE_UPDATE_RET(bci)                                             \
 228   if (ProfileInterpreter && MDX() != NULL) {                                   \
 229     BI_PROFILE_CHECK_MDX();                                                    \
 230     MethodData *md = istate->method()->method_data();                          \
 231 /* FIXME: there is more to do here than increment and advance(mdx)! */         \
 232     CounterData::increment_count_no_overflow(MDX());                           \
 233     SET_MDX(RetData::advance(md, bci));                                        \
 234   }
 235 
 236 // Decrement counter at checkcast if the subtype check fails (as template
 237 // interpreter does!).
 238 #define BI_PROFILE_SUBTYPECHECK_FAILED(receiver)                               \
 239   if (ProfileInterpreter && MDX() != NULL) {                                   \
 240     BI_PROFILE_CHECK_MDX();                                                    \
 241     ReceiverTypeData::increment_receiver_count_no_overflow(MDX(), receiver);   \
 242     ReceiverTypeData::decrement_count(MDX());                                  \
 243   }
 244 
 245 // Updates profiling data for a checkcast (was a null seen? which receiver?).
 246 #define BI_PROFILE_UPDATE_CHECKCAST(null_seen, receiver)                       \
 247   if (ProfileInterpreter && MDX() != NULL) {                                   \
 248     BI_PROFILE_CHECK_MDX();                                                    \
 249     if (null_seen) {                                                           \
 250       ReceiverTypeData::set_null_seen(MDX());                                  \
 251     } else {                                                                   \
 252       /* Template interpreter doesn't increment count. */                      \
 253       /* ReceiverTypeData::increment_count_no_overflow(MDX()); */              \
 254       ReceiverTypeData::increment_receiver_count_no_overflow(MDX(), receiver); \
 255     }                                                                          \
 256     SET_MDX(ReceiverTypeData::advance(MDX()));                                 \
 257   }
 258 
 259 
 260 // Updates profiling data for an instanceof (was a null seen? which receiver?).
 261 #define BI_PROFILE_UPDATE_INSTANCEOF(null_seen, receiver)                      \
 262   BI_PROFILE_UPDATE_CHECKCAST(null_seen, receiver)
 263 
 264 
 265 // Updates profiling data for a call.
 266 #define BI_PROFILE_UPDATE_CALL()                                               \
 267   if (ProfileInterpreter && MDX() != NULL) {                                   \
 268     BI_PROFILE_CHECK_MDX();                                                    \
 269     CounterData::increment_count_no_overflow(MDX());                           \
 270     SET_MDX(CounterData::advance(MDX()));                                      \
 271   }
 272 
 273 
 274 // Updates profiling data for a final call.
 275 #define BI_PROFILE_UPDATE_FINALCALL()                                          \
 276   if (ProfileInterpreter && MDX() != NULL) {                                   \
 277     BI_PROFILE_CHECK_MDX();                                                    \
 278     VirtualCallData::increment_count_no_overflow(MDX());                       \
 279     SET_MDX(VirtualCallData::advance(MDX()));                                  \
 280   }
 281 
 282 
 283 // Updates profiling data for a virtual call with given receiver Klass.
 284 #define BI_PROFILE_UPDATE_VIRTUALCALL(receiver)                                \
 285   if (ProfileInterpreter && MDX() != NULL) {                                   \
 286     BI_PROFILE_CHECK_MDX();                                                    \
 287     VirtualCallData::increment_receiver_count_no_overflow(MDX(), receiver);    \
 288     SET_MDX(VirtualCallData::advance(MDX()));                                  \
 289   }
 290 
 291 
 292 // Updates profiling data for a switch (tabelswitch or lookupswitch) with
 293 // given taken index (-1 means default case was taken).
 294 #define BI_PROFILE_UPDATE_SWITCH(switch_index)                                 \
 295   if (ProfileInterpreter && MDX() != NULL) {                                   \
 296     BI_PROFILE_CHECK_MDX();                                                    \
 297     MultiBranchData::increment_count_no_overflow(MDX(), switch_index);         \
 298     SET_MDX(MultiBranchData::advance(MDX(), switch_index));                    \
 299   }
 300 
 301 
 302 // The end /////////////////////////////////////////////////////////////////////
 303 
 304 #endif // CC_INTERP_PROFILE
 305 
 306 #endif // CC_INTERP
 307 
 308 #endif // SHARE_VM_INTERPRETER_BYTECODECINTERPRETERPROFILING_HPP