1 /*
   2  * Copyright (c) 1997, 2011, 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 #include "precompiled.hpp"
  26 #include "code/codeCache.hpp"
  27 #include "interpreter/interpreter.hpp"
  28 #include "oops/oop.inline.hpp"
  29 #include "oops/symbol.hpp"
  30 #include "runtime/frame.inline.hpp"
  31 #include "runtime/rframe.hpp"
  32 #include "runtime/vframe.hpp"
  33 #include "runtime/vframe_hp.hpp"
  34 
  35 
  36 static RFrame*const  noCaller    = (RFrame*) 0x1;               // no caller (i.e., initial frame)
  37 static RFrame*const  noCallerYet = (RFrame*) 0x0;               // caller not yet computed
  38 
  39 RFrame::RFrame(frame fr, JavaThread* thread, RFrame*const callee) :
  40   _fr(fr), _thread(thread), _callee(callee), _num(callee ? callee->num() + 1 : 0) {
  41   _caller = (RFrame*)noCallerYet;
  42   _invocations = 0;
  43   _distance = 0;
  44 }
  45 
  46 void RFrame::set_distance(int d) {
  47   assert(is_compiled() || d >= 0, "should be positive");
  48   _distance = d;
  49 }
  50 
  51 InterpretedRFrame::InterpretedRFrame(frame fr, JavaThread* thread, RFrame*const callee)
  52 : RFrame(fr, thread, callee) {
  53   RegisterMap map(thread, false);
  54   _vf     = javaVFrame::cast(vframe::new_vframe(&_fr, &map, thread));
  55   _method = _vf->method();
  56   assert(   _vf->is_interpreted_frame(), "must be interpreted");
  57   init();
  58 }
  59 
  60 InterpretedRFrame::InterpretedRFrame(frame fr, JavaThread* thread, Method* m)
  61 : RFrame(fr, thread, NULL) {
  62   RegisterMap map(thread, false);
  63   _vf     = javaVFrame::cast(vframe::new_vframe(&_fr, &map, thread));
  64   _method = m;
  65 
  66   assert(   _vf->is_interpreted_frame(),  "must be interpreted");
  67   init();
  68 }
  69 
  70 CompiledRFrame::CompiledRFrame(frame fr, JavaThread* thread, RFrame*const  callee)
  71 : RFrame(fr, thread, callee) {
  72   init();
  73 }
  74 
  75 CompiledRFrame::CompiledRFrame(frame fr, JavaThread* thread)
  76 : RFrame(fr, thread, NULL) {
  77   init();
  78 }
  79 
  80 DeoptimizedRFrame::DeoptimizedRFrame(frame fr, JavaThread* thread, RFrame*const  callee)
  81 : InterpretedRFrame(fr, thread, callee) {}
  82 
  83 RFrame* RFrame::new_RFrame(frame fr, JavaThread* thread, RFrame*const  callee) {
  84   RFrame* rf;
  85   int dist = callee ? callee->distance() : -1;
  86   if (fr.is_interpreted_frame()) {
  87     rf = new InterpretedRFrame(fr, thread, callee);
  88     dist++;
  89   } else if (fr.is_compiled_frame()) {
  90     // Even deopted frames look compiled because the deopt
  91     // is invisible until it happens.
  92     rf = new CompiledRFrame(fr, thread, callee);
  93   } else {
  94     assert(false, "Unhandled frame type");
  95   }
  96   rf->set_distance(dist);
  97   rf->init();
  98   return rf;
  99 }
 100 
 101 RFrame* RFrame::caller() {
 102   if (_caller != noCallerYet) return (_caller == noCaller) ? NULL : _caller;    // already computed caller
 103 
 104   // caller not yet computed; do it now
 105   if (_fr.is_first_java_frame()) {
 106     _caller = (RFrame*)noCaller;
 107     return NULL;
 108   }
 109 
 110   RegisterMap map(_thread, false);
 111   frame sender = _fr.real_sender(&map);
 112   if (sender.is_java_frame()) {
 113     _caller = new_RFrame(sender, thread(), this);
 114     return _caller;
 115   }
 116 
 117   // Real caller is not java related
 118   _caller = (RFrame*)noCaller;
 119   return NULL;
 120 }
 121 
 122 int InterpretedRFrame::cost() const {
 123   return _method->code_size();    // fix this
 124   //return _method->estimated_inline_cost(_receiverKlass);
 125 }
 126 
 127 int CompiledRFrame::cost() const {
 128   nmethod* nm = top_method()->code();
 129   if (nm != NULL) {
 130     return nm->insts_size();
 131   } else {
 132     return top_method()->code_size();
 133   }
 134 }
 135 
 136 void CompiledRFrame::init() {
 137   RegisterMap map(thread(), false);
 138   vframe* vf = vframe::new_vframe(&_fr, &map, thread());
 139   assert(vf->is_compiled_frame(), "must be compiled");
 140   _nm = compiledVFrame::cast(vf)->code();
 141   vf = vf->top();
 142   _vf = javaVFrame::cast(vf);
 143   _method = CodeCache::find_nmethod(_fr.pc())->method();
 144   assert(_method, "should have found a method");
 145 #ifndef PRODUCT
 146   _invocations = _method->compiled_invocation_count();
 147 #endif
 148 }
 149 
 150 void InterpretedRFrame::init() {
 151   _invocations = _method->invocation_count() + _method->backedge_count();
 152 }
 153 
 154 void RFrame::print(const char* kind) {
 155 #ifndef PRODUCT
 156 #ifdef COMPILER2
 157   int cnt = top_method()->interpreter_invocation_count();
 158 #else
 159   int cnt = top_method()->invocation_count();
 160 #endif
 161   tty->print("%3d %s ", _num, is_interpreted() ? "I" : "C");
 162   top_method()->print_short_name(tty);
 163   tty->print_cr(": inv=%5d(%d) cst=%4d", _invocations, cnt, cost());
 164 #endif
 165 }
 166 
 167 void CompiledRFrame::print() {
 168   RFrame::print("comp");
 169 }
 170 
 171 void InterpretedRFrame::print() {
 172   RFrame::print("int.");
 173 }
 174 
 175 void DeoptimizedRFrame::print() {
 176   RFrame::print("deopt.");
 177 }