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