blob: 0fffdfd4dfda03cef566e876afd711ad736cc8bb [file] [log] [blame]
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "art_method.h"
18#include "jit/jit.h"
19#include "jit/jit_code_cache.h"
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000020#include "jit/profiling_info.h"
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000021#include "oat_quick_method_header.h"
22#include "scoped_thread_state_change.h"
23#include "stack_map.h"
24
25namespace art {
26
27class OsrVisitor : public StackVisitor {
28 public:
29 explicit OsrVisitor(Thread* thread)
30 SHARED_REQUIRES(Locks::mutator_lock_)
31 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000032 in_osr_method_(false),
33 in_interpreter_(false) {}
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000034
35 bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) {
36 ArtMethod* m = GetMethod();
37 std::string m_name(m->GetName());
38
39 if ((m_name.compare("$noinline$returnInt") == 0) ||
40 (m_name.compare("$noinline$returnFloat") == 0) ||
41 (m_name.compare("$noinline$returnDouble") == 0) ||
42 (m_name.compare("$noinline$returnLong") == 0) ||
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +000043 (m_name.compare("$noinline$deopt") == 0) ||
44 (m_name.compare("$noinline$inlineCache") == 0)) {
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000045 const OatQuickMethodHeader* header =
46 Runtime::Current()->GetJit()->GetCodeCache()->LookupOsrMethodHeader(m);
47 if (header != nullptr && header == GetCurrentOatQuickMethodHeader()) {
48 in_osr_method_ = true;
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000049 } else if (IsCurrentFrameInInterpreter()) {
50 in_interpreter_ = true;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000051 }
52 return false;
53 }
54 return true;
55 }
56
57 bool in_osr_method_;
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000058 bool in_interpreter_;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000059};
60
61extern "C" JNIEXPORT jboolean JNICALL Java_Main_ensureInOsrCode(JNIEnv*, jclass) {
62 jit::Jit* jit = Runtime::Current()->GetJit();
63 if (jit == nullptr) {
64 // Just return true for non-jit configurations to stop the infinite loop.
65 return JNI_TRUE;
66 }
67 ScopedObjectAccess soa(Thread::Current());
68 OsrVisitor visitor(soa.Self());
69 visitor.WalkStack();
70 return visitor.in_osr_method_;
71}
72
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000073extern "C" JNIEXPORT jboolean JNICALL Java_Main_ensureInInterpreter(JNIEnv*, jclass) {
74 ScopedObjectAccess soa(Thread::Current());
75 OsrVisitor visitor(soa.Self());
76 visitor.WalkStack();
77 return visitor.in_interpreter_;
78}
79
80class ProfilingInfoVisitor : public StackVisitor {
81 public:
82 explicit ProfilingInfoVisitor(Thread* thread)
83 SHARED_REQUIRES(Locks::mutator_lock_)
84 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
85
86 bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) {
87 ArtMethod* m = GetMethod();
88 std::string m_name(m->GetName());
89
90 if (m_name.compare("$noinline$inlineCache") == 0) {
91 ProfilingInfo::Create(Thread::Current(), m, /* retry_allocation */ true);
92 return false;
93 }
94 return true;
95 }
96};
97
98extern "C" JNIEXPORT void JNICALL Java_Main_ensureHasProfilingInfo(JNIEnv*, jclass) {
99 ScopedObjectAccess soa(Thread::Current());
100 ProfilingInfoVisitor visitor(soa.Self());
101 visitor.WalkStack();
102}
103
104class OsrCheckVisitor : public StackVisitor {
105 public:
106 explicit OsrCheckVisitor(Thread* thread)
107 SHARED_REQUIRES(Locks::mutator_lock_)
108 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
109
110 bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) {
111 ArtMethod* m = GetMethod();
112 std::string m_name(m->GetName());
113
114 jit::Jit* jit = Runtime::Current()->GetJit();
115 if (m_name.compare("$noinline$inlineCache") == 0 && jit != nullptr) {
116 while (jit->GetCodeCache()->LookupOsrMethodHeader(m) == nullptr) {
117 // Sleep to yield to the compiler thread.
118 sleep(0);
119 // Will either ensure it's compiled or do the compilation itself.
120 jit->CompileMethod(m, Thread::Current(), /* osr */ true);
121 }
122 return false;
123 }
124 return true;
125 }
126};
127
128extern "C" JNIEXPORT void JNICALL Java_Main_ensureHasOsrCode(JNIEnv*, jclass) {
129 ScopedObjectAccess soa(Thread::Current());
130 OsrCheckVisitor visitor(soa.Self());
131 visitor.WalkStack();
132}
133
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000134} // namespace art