xref: /aosp_15_r20/art/test/570-checker-osr/osr.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "art_method-inl.h"
18*795d594fSAndroid Build Coastguard Worker #include "jit/jit.h"
19*795d594fSAndroid Build Coastguard Worker #include "jit/jit_code_cache.h"
20*795d594fSAndroid Build Coastguard Worker #include "jit/profiling_info.h"
21*795d594fSAndroid Build Coastguard Worker #include "nativehelper/ScopedUtfChars.h"
22*795d594fSAndroid Build Coastguard Worker #include "oat/oat_quick_method_header.h"
23*795d594fSAndroid Build Coastguard Worker #include "oat/stack_map.h"
24*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
25*795d594fSAndroid Build Coastguard Worker #include "stack.h"
26*795d594fSAndroid Build Coastguard Worker #include "thread-current-inl.h"
27*795d594fSAndroid Build Coastguard Worker 
28*795d594fSAndroid Build Coastguard Worker namespace art {
29*795d594fSAndroid Build Coastguard Worker 
30*795d594fSAndroid Build Coastguard Worker namespace {
31*795d594fSAndroid Build Coastguard Worker 
32*795d594fSAndroid Build Coastguard Worker template <typename Handler>
ProcessMethodWithName(JNIEnv * env,jstring method_name,const Handler & handler)33*795d594fSAndroid Build Coastguard Worker void ProcessMethodWithName(JNIEnv* env, jstring method_name, const Handler& handler) {
34*795d594fSAndroid Build Coastguard Worker   ScopedUtfChars chars(env, method_name);
35*795d594fSAndroid Build Coastguard Worker   CHECK(chars.c_str() != nullptr);
36*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
37*795d594fSAndroid Build Coastguard Worker   StackVisitor::WalkStack(
38*795d594fSAndroid Build Coastguard Worker       [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
39*795d594fSAndroid Build Coastguard Worker         std::string m_name(stack_visitor->GetMethod()->GetName());
40*795d594fSAndroid Build Coastguard Worker 
41*795d594fSAndroid Build Coastguard Worker         if (m_name.compare(chars.c_str()) == 0) {
42*795d594fSAndroid Build Coastguard Worker           handler(stack_visitor);
43*795d594fSAndroid Build Coastguard Worker           return false;
44*795d594fSAndroid Build Coastguard Worker         }
45*795d594fSAndroid Build Coastguard Worker         return true;
46*795d594fSAndroid Build Coastguard Worker       },
47*795d594fSAndroid Build Coastguard Worker       soa.Self(),
48*795d594fSAndroid Build Coastguard Worker       /* context= */ nullptr,
49*795d594fSAndroid Build Coastguard Worker       art::StackVisitor::StackWalkKind::kIncludeInlinedFrames);
50*795d594fSAndroid Build Coastguard Worker }
51*795d594fSAndroid Build Coastguard Worker 
52*795d594fSAndroid Build Coastguard Worker }  // namespace
53*795d594fSAndroid Build Coastguard Worker 
Java_Main_isInOsrCode(JNIEnv * env,jclass,jstring method_name)54*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInOsrCode(JNIEnv* env,
55*795d594fSAndroid Build Coastguard Worker                                                             jclass,
56*795d594fSAndroid Build Coastguard Worker                                                             jstring method_name) {
57*795d594fSAndroid Build Coastguard Worker   jit::Jit* jit = Runtime::Current()->GetJit();
58*795d594fSAndroid Build Coastguard Worker   if (jit == nullptr) {
59*795d594fSAndroid Build Coastguard Worker     // Just return true for non-jit configurations to stop the infinite loop.
60*795d594fSAndroid Build Coastguard Worker     return JNI_TRUE;
61*795d594fSAndroid Build Coastguard Worker   }
62*795d594fSAndroid Build Coastguard Worker   bool in_osr_code = false;
63*795d594fSAndroid Build Coastguard Worker   ProcessMethodWithName(
64*795d594fSAndroid Build Coastguard Worker       env,
65*795d594fSAndroid Build Coastguard Worker       method_name,
66*795d594fSAndroid Build Coastguard Worker       [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
67*795d594fSAndroid Build Coastguard Worker         ArtMethod* m = stack_visitor->GetMethod();
68*795d594fSAndroid Build Coastguard Worker         const OatQuickMethodHeader* header =
69*795d594fSAndroid Build Coastguard Worker             Runtime::Current()->GetJit()->GetCodeCache()->LookupOsrMethodHeader(m);
70*795d594fSAndroid Build Coastguard Worker         if (header != nullptr && header == stack_visitor->GetCurrentOatQuickMethodHeader()) {
71*795d594fSAndroid Build Coastguard Worker           in_osr_code = true;
72*795d594fSAndroid Build Coastguard Worker         }
73*795d594fSAndroid Build Coastguard Worker       });
74*795d594fSAndroid Build Coastguard Worker   return in_osr_code;
75*795d594fSAndroid Build Coastguard Worker }
76*795d594fSAndroid Build Coastguard Worker 
Java_Main_isInInterpreter(JNIEnv * env,jclass,jstring method_name)77*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInInterpreter(JNIEnv* env,
78*795d594fSAndroid Build Coastguard Worker                                                                 jclass,
79*795d594fSAndroid Build Coastguard Worker                                                                 jstring method_name) {
80*795d594fSAndroid Build Coastguard Worker   if (!Runtime::Current()->UseJitCompilation()) {
81*795d594fSAndroid Build Coastguard Worker     // The return value is irrelevant if we're not using JIT.
82*795d594fSAndroid Build Coastguard Worker     return false;
83*795d594fSAndroid Build Coastguard Worker   }
84*795d594fSAndroid Build Coastguard Worker   bool in_interpreter = false;
85*795d594fSAndroid Build Coastguard Worker   ProcessMethodWithName(
86*795d594fSAndroid Build Coastguard Worker       env,
87*795d594fSAndroid Build Coastguard Worker       method_name,
88*795d594fSAndroid Build Coastguard Worker       [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
89*795d594fSAndroid Build Coastguard Worker         ArtMethod* m = stack_visitor->GetMethod();
90*795d594fSAndroid Build Coastguard Worker         const OatQuickMethodHeader* header =
91*795d594fSAndroid Build Coastguard Worker             Runtime::Current()->GetJit()->GetCodeCache()->LookupOsrMethodHeader(m);
92*795d594fSAndroid Build Coastguard Worker         if ((header == nullptr || header != stack_visitor->GetCurrentOatQuickMethodHeader()) &&
93*795d594fSAndroid Build Coastguard Worker             (stack_visitor->IsShadowFrame() ||
94*795d594fSAndroid Build Coastguard Worker              stack_visitor->GetCurrentOatQuickMethodHeader()->IsNterpMethodHeader())) {
95*795d594fSAndroid Build Coastguard Worker           in_interpreter = true;
96*795d594fSAndroid Build Coastguard Worker         }
97*795d594fSAndroid Build Coastguard Worker       });
98*795d594fSAndroid Build Coastguard Worker   return in_interpreter;
99*795d594fSAndroid Build Coastguard Worker }
100*795d594fSAndroid Build Coastguard Worker 
Java_Main_ensureHasOsrCode(JNIEnv * env,jclass,jstring method_name)101*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT void JNICALL Java_Main_ensureHasOsrCode(JNIEnv* env,
102*795d594fSAndroid Build Coastguard Worker                                                              jclass,
103*795d594fSAndroid Build Coastguard Worker                                                              jstring method_name) {
104*795d594fSAndroid Build Coastguard Worker   if (!Runtime::Current()->UseJitCompilation()) {
105*795d594fSAndroid Build Coastguard Worker     return;
106*795d594fSAndroid Build Coastguard Worker   }
107*795d594fSAndroid Build Coastguard Worker   ProcessMethodWithName(
108*795d594fSAndroid Build Coastguard Worker       env,
109*795d594fSAndroid Build Coastguard Worker       method_name,
110*795d594fSAndroid Build Coastguard Worker       [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
111*795d594fSAndroid Build Coastguard Worker         ArtMethod* m = stack_visitor->GetMethod();
112*795d594fSAndroid Build Coastguard Worker         jit::Jit* jit = Runtime::Current()->GetJit();
113*795d594fSAndroid Build Coastguard Worker         while (jit->GetCodeCache()->LookupOsrMethodHeader(m) == nullptr) {
114*795d594fSAndroid Build Coastguard Worker           // Sleep to yield to the compiler thread.
115*795d594fSAndroid Build Coastguard Worker           usleep(1000);
116*795d594fSAndroid Build Coastguard Worker           // Will either ensure it's compiled or do the compilation itself.
117*795d594fSAndroid Build Coastguard Worker           jit->CompileMethod(
118*795d594fSAndroid Build Coastguard Worker               m, Thread::Current(), CompilationKind::kOsr, /*prejit=*/ false);
119*795d594fSAndroid Build Coastguard Worker         }
120*795d594fSAndroid Build Coastguard Worker       });
121*795d594fSAndroid Build Coastguard Worker }
122*795d594fSAndroid Build Coastguard Worker 
123*795d594fSAndroid Build Coastguard Worker }  // namespace art
124