1 /*
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 "base/enums.h"
19 #include "jit/jit.h"
20 #include "jit/jit_code_cache.h"
21 #include "jit/profiling_info.h"
22 #include "mirror/class.h"
23 #include "nativehelper/ScopedUtfChars.h"
24 #include "oat_quick_method_header.h"
25 #include "scoped_thread_state_change-inl.h"
26 #include "stack_map.h"
27 
28 namespace art {
29 
do_checks(ArtMethod * method,ScopedObjectAccess & soa)30 static bool do_checks(ArtMethod* method, ScopedObjectAccess& soa)
31     REQUIRES_SHARED(Locks::mutator_lock_) {
32   jit::Jit* jit = Runtime::Current()->GetJit();
33   jit::JitCodeCache* code_cache = jit->GetCodeCache();
34 
35   OatQuickMethodHeader* header = nullptr;
36   // Infinite loop... Test harness will have its own timeout.
37   while (true) {
38     const void* pc = method->GetEntryPointFromQuickCompiledCode();
39     if (code_cache->ContainsPc(pc) &&
40         !CodeInfo::IsBaseline(
41             OatQuickMethodHeader::FromEntryPoint(pc)->GetOptimizedCodeInfoPtr())) {
42       header = OatQuickMethodHeader::FromEntryPoint(pc);
43       break;
44     } else {
45       ScopedThreadSuspension sts(soa.Self(), kSuspended);
46       // Sleep to yield to the compiler thread.
47       usleep(1000);
48     }
49     // Will either ensure it's compiled or do the compilation itself.
50     jit->CompileMethod(method, soa.Self(), CompilationKind::kOptimized, /*prejit=*/ false);
51   }
52 
53   CodeInfo info(header);
54   return info.HasInlineInfo();
55 }
56 
Java_Main_ensureJittedAndPolymorphicInline566(JNIEnv * env,jclass cls,jstring method_name)57 extern "C" JNIEXPORT bool JNICALL Java_Main_ensureJittedAndPolymorphicInline566(JNIEnv* env,
58                                                                                 jclass cls,
59                                                                                 jstring method_name) {
60   jit::Jit* jit = Runtime::Current()->GetJit();
61   if (jit == nullptr) {
62     return true;
63   }
64 
65   // The test only works when we use tiered JIT.
66   if (jit->JitAtFirstUse()) {
67     return true;
68   }
69 
70   ScopedObjectAccess soa(Thread::Current());
71   ScopedUtfChars chars(env, method_name);
72   ArtMethod* method = soa.Decode<mirror::Class>(cls)->FindDeclaredDirectMethodByName(
73       chars.c_str(), kRuntimePointerSize);
74 
75   if (method == nullptr) {
76     return false;
77   }
78 
79   return do_checks(method, soa);
80 }
81 
82 }  // namespace art
83