• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 "jni.h"
18 
19 #include <android-base/logging.h>
20 #include <android-base/macros.h>
21 
22 #include "art_method-inl.h"
23 #include "base/enums.h"
24 #include "dex/dex_file-inl.h"
25 #include "instrumentation.h"
26 #include "jit/jit.h"
27 #include "jit/jit_code_cache.h"
28 #include "jit/profile_compilation_info.h"
29 #include "jit/profiling_info.h"
30 #include "mirror/class-inl.h"
31 #include "nativehelper/ScopedUtfChars.h"
32 #include "oat_file.h"
33 #include "oat_quick_method_header.h"
34 #include "runtime.h"
35 #include "scoped_thread_state_change-inl.h"
36 #include "thread-current-inl.h"
37 
38 namespace art {
39 
40 // public static native boolean hasJit();
41 
GetJitIfEnabled()42 static jit::Jit* GetJitIfEnabled() {
43   Runtime* runtime = Runtime::Current();
44   bool can_jit =
45       runtime != nullptr
46       && runtime->GetJit() != nullptr
47       && runtime->GetInstrumentation()->GetCurrentInstrumentationLevel() !=
48             instrumentation::Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter;
49   return can_jit ? runtime->GetJit() : nullptr;
50 }
51 
Java_Main_hasJit(JNIEnv *,jclass)52 extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasJit(JNIEnv*, jclass) {
53   return GetJitIfEnabled() != nullptr;
54 }
55 
56 // public static native boolean hasOatFile();
57 
Java_Main_hasOatFile(JNIEnv * env,jclass cls)58 extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasOatFile(JNIEnv* env, jclass cls) {
59   ScopedObjectAccess soa(env);
60 
61   ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls);
62   const DexFile& dex_file = klass->GetDexFile();
63   const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
64   return (oat_dex_file != nullptr) ? JNI_TRUE : JNI_FALSE;
65 }
66 
67 // public static native boolean runtimeIsSoftFail();
68 
Java_Main_runtimeIsSoftFail(JNIEnv * env ATTRIBUTE_UNUSED,jclass cls ATTRIBUTE_UNUSED)69 extern "C" JNIEXPORT jboolean JNICALL Java_Main_runtimeIsSoftFail(JNIEnv* env ATTRIBUTE_UNUSED,
70                                                                   jclass cls ATTRIBUTE_UNUSED) {
71   return Runtime::Current()->IsVerificationSoftFail() ? JNI_TRUE : JNI_FALSE;
72 }
73 
74 // public static native boolean isDex2OatEnabled();
75 
Java_Main_isDex2OatEnabled(JNIEnv * env ATTRIBUTE_UNUSED,jclass cls ATTRIBUTE_UNUSED)76 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isDex2OatEnabled(JNIEnv* env ATTRIBUTE_UNUSED,
77                                                                  jclass cls ATTRIBUTE_UNUSED) {
78   return Runtime::Current()->IsDex2OatEnabled();
79 }
80 
81 // public static native boolean hasImage();
82 
Java_Main_hasImage(JNIEnv * env ATTRIBUTE_UNUSED,jclass cls ATTRIBUTE_UNUSED)83 extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasImage(JNIEnv* env ATTRIBUTE_UNUSED,
84                                                          jclass cls ATTRIBUTE_UNUSED) {
85   return Runtime::Current()->GetHeap()->HasBootImageSpace();
86 }
87 
88 // public static native boolean isImageDex2OatEnabled();
89 
Java_Main_isImageDex2OatEnabled(JNIEnv * env ATTRIBUTE_UNUSED,jclass cls ATTRIBUTE_UNUSED)90 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isImageDex2OatEnabled(JNIEnv* env ATTRIBUTE_UNUSED,
91                                                                       jclass cls ATTRIBUTE_UNUSED) {
92   return Runtime::Current()->IsImageDex2OatEnabled();
93 }
94 
95 // public static native boolean compiledWithOptimizing();
96 // Did we use the optimizing compiler to compile this?
97 
Java_Main_compiledWithOptimizing(JNIEnv * env,jclass cls)98 extern "C" JNIEXPORT jboolean JNICALL Java_Main_compiledWithOptimizing(JNIEnv* env, jclass cls) {
99   ScopedObjectAccess soa(env);
100 
101   ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls);
102   const DexFile& dex_file = klass->GetDexFile();
103   const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
104   if (oat_dex_file == nullptr) {
105     // Could be JIT, which also uses optimizing, but conservatively say no.
106     return JNI_FALSE;
107   }
108   const OatFile* oat_file = oat_dex_file->GetOatFile();
109   CHECK(oat_file != nullptr);
110 
111   const char* cmd_line = oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kDex2OatCmdLineKey);
112   CHECK(cmd_line != nullptr);  // Huh? This should not happen.
113 
114   // Check the backend.
115   constexpr const char* kCompilerBackend = "--compiler-backend=";
116   const char* backend = strstr(cmd_line, kCompilerBackend);
117   if (backend != nullptr) {
118     // If it's set, make sure it's optimizing.
119     backend += strlen(kCompilerBackend);
120     if (strncmp(backend, "Optimizing", strlen("Optimizing")) != 0) {
121       return JNI_FALSE;
122     }
123   }
124 
125   // Check the filter.
126   constexpr const char* kCompilerFilter = "--compiler-filter=";
127   const char* filter = strstr(cmd_line, kCompilerFilter);
128   if (filter != nullptr) {
129     // If it's set, make sure it's not interpret-only|verify-none|verify-at-runtime.
130     // Note: The space filter might have an impact on the test, but ignore that for now.
131     filter += strlen(kCompilerFilter);
132     constexpr const char* kInterpretOnly = "interpret-only";
133     constexpr const char* kVerifyNone = "verify-none";
134     constexpr const char* kVerifyAtRuntime = "verify-at-runtime";
135     if (strncmp(filter, kInterpretOnly, strlen(kInterpretOnly)) == 0 ||
136         strncmp(filter, kVerifyNone, strlen(kVerifyNone)) == 0 ||
137         strncmp(filter, kVerifyAtRuntime, strlen(kVerifyAtRuntime)) == 0) {
138       return JNI_FALSE;
139     }
140   }
141 
142   return JNI_TRUE;
143 }
144 
Java_Main_isAotCompiled(JNIEnv * env,jclass,jclass cls,jstring method_name)145 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isAotCompiled(JNIEnv* env,
146                                                               jclass,
147                                                               jclass cls,
148                                                               jstring method_name) {
149   Thread* self = Thread::Current();
150   ScopedObjectAccess soa(self);
151   ScopedUtfChars chars(env, method_name);
152   CHECK(chars.c_str() != nullptr);
153   ArtMethod* method = soa.Decode<mirror::Class>(cls)->FindDeclaredDirectMethodByName(
154         chars.c_str(), kRuntimePointerSize);
155   const void* oat_code = method->GetOatMethodQuickCode(kRuntimePointerSize);
156   if (oat_code == nullptr) {
157     return false;
158   }
159   const void* actual_code = method->GetEntryPointFromQuickCompiledCodePtrSize(kRuntimePointerSize);
160   bool interpreter =
161       Runtime::Current()->GetClassLinker()->ShouldUseInterpreterEntrypoint(method, actual_code);
162   return !interpreter;
163 }
164 
Java_Main_hasJitCompiledEntrypoint(JNIEnv * env,jclass,jclass cls,jstring method_name)165 extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasJitCompiledEntrypoint(JNIEnv* env,
166                                                                          jclass,
167                                                                          jclass cls,
168                                                                          jstring method_name) {
169   jit::Jit* jit = GetJitIfEnabled();
170   if (jit == nullptr) {
171     return false;
172   }
173   Thread* self = Thread::Current();
174   ScopedObjectAccess soa(self);
175   ScopedUtfChars chars(env, method_name);
176   CHECK(chars.c_str() != nullptr);
177   ArtMethod* method = soa.Decode<mirror::Class>(cls)->FindDeclaredDirectMethodByName(
178         chars.c_str(), kRuntimePointerSize);
179   return jit->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode());
180 }
181 
Java_Main_hasJitCompiledCode(JNIEnv * env,jclass,jclass cls,jstring method_name)182 extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasJitCompiledCode(JNIEnv* env,
183                                                                    jclass,
184                                                                    jclass cls,
185                                                                    jstring method_name) {
186   jit::Jit* jit = GetJitIfEnabled();
187   if (jit == nullptr) {
188     return false;
189   }
190   Thread* self = Thread::Current();
191   ScopedObjectAccess soa(self);
192   ScopedUtfChars chars(env, method_name);
193   CHECK(chars.c_str() != nullptr);
194   ArtMethod* method = soa.Decode<mirror::Class>(cls)->FindDeclaredDirectMethodByName(
195         chars.c_str(), kRuntimePointerSize);
196   return jit->GetCodeCache()->ContainsMethod(method);
197 }
198 
Java_Main_ensureJitCompiled(JNIEnv * env,jclass,jclass cls,jstring method_name)199 extern "C" JNIEXPORT void JNICALL Java_Main_ensureJitCompiled(JNIEnv* env,
200                                                              jclass,
201                                                              jclass cls,
202                                                              jstring method_name) {
203   jit::Jit* jit = GetJitIfEnabled();
204   if (jit == nullptr) {
205     return;
206   }
207 
208   Thread* self = Thread::Current();
209   ArtMethod* method = nullptr;
210   {
211     ScopedObjectAccess soa(self);
212 
213     ScopedUtfChars chars(env, method_name);
214     CHECK(chars.c_str() != nullptr);
215     method = soa.Decode<mirror::Class>(cls)->FindDeclaredDirectMethodByName(
216         chars.c_str(), kRuntimePointerSize);
217     if (method == nullptr) {
218       method = soa.Decode<mirror::Class>(cls)->FindDeclaredVirtualMethodByName(
219           chars.c_str(), kRuntimePointerSize);
220     }
221     DCHECK(method != nullptr) << "Unable to find method called " << chars.c_str();
222   }
223 
224   jit::JitCodeCache* code_cache = jit->GetCodeCache();
225   // Update the code cache to make sure the JIT code does not get deleted.
226   // Note: this will apply to all JIT compilations.
227   code_cache->SetGarbageCollectCode(false);
228   while (true) {
229     const void* pc = method->GetEntryPointFromQuickCompiledCode();
230     if (code_cache->ContainsPc(pc)) {
231       break;
232     } else {
233       // Sleep to yield to the compiler thread.
234       usleep(1000);
235       ScopedObjectAccess soa(self);
236       // Make sure there is a profiling info, required by the compiler.
237       ProfilingInfo::Create(self, method, /* retry_allocation */ true);
238       // Will either ensure it's compiled or do the compilation itself.
239       jit->CompileMethod(method, self, /* osr */ false);
240     }
241   }
242 }
243 
Java_Main_hasSingleImplementation(JNIEnv * env,jclass,jclass cls,jstring method_name)244 extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasSingleImplementation(JNIEnv* env,
245                                                                         jclass,
246                                                                         jclass cls,
247                                                                         jstring method_name) {
248   ArtMethod* method = nullptr;
249   ScopedObjectAccess soa(Thread::Current());
250   ScopedUtfChars chars(env, method_name);
251   CHECK(chars.c_str() != nullptr);
252   method = soa.Decode<mirror::Class>(cls)->FindDeclaredVirtualMethodByName(
253       chars.c_str(), kRuntimePointerSize);
254   return method->HasSingleImplementation();
255 }
256 
Java_Main_getHotnessCounter(JNIEnv * env,jclass,jclass cls,jstring method_name)257 extern "C" JNIEXPORT int JNICALL Java_Main_getHotnessCounter(JNIEnv* env,
258                                                              jclass,
259                                                              jclass cls,
260                                                              jstring method_name) {
261   ArtMethod* method = nullptr;
262   {
263     ScopedObjectAccess soa(Thread::Current());
264 
265     ScopedUtfChars chars(env, method_name);
266     CHECK(chars.c_str() != nullptr);
267     method = soa.Decode<mirror::Class>(cls)->FindDeclaredDirectMethodByName(
268         chars.c_str(), kRuntimePointerSize);
269   }
270 
271   return method->GetCounter();
272 }
273 
Java_Main_numberOfDeoptimizations(JNIEnv *,jclass)274 extern "C" JNIEXPORT int JNICALL Java_Main_numberOfDeoptimizations(JNIEnv*, jclass) {
275   return Runtime::Current()->GetNumberOfDeoptimizations();
276 }
277 
Java_Main_fetchProfiles(JNIEnv *,jclass)278 extern "C" JNIEXPORT void JNICALL Java_Main_fetchProfiles(JNIEnv*, jclass) {
279   jit::Jit* jit = GetJitIfEnabled();
280   if (jit == nullptr) {
281     return;
282   }
283   jit::JitCodeCache* code_cache = jit->GetCodeCache();
284   std::vector<ProfileMethodInfo> unused_vector;
285   std::set<std::string> unused_locations;
286   unused_locations.insert("fake_location");
287   ScopedObjectAccess soa(Thread::Current());
288   code_cache->GetProfiledMethods(unused_locations, unused_vector);
289 }
290 
Java_Main_isClassMoveable(JNIEnv *,jclass,jclass cls)291 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isClassMoveable(JNIEnv*,
292                                                                 jclass,
293                                                                 jclass cls) {
294   Runtime* runtime = Runtime::Current();
295   ScopedObjectAccess soa(Thread::Current());
296   ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls);
297   return runtime->GetHeap()->IsMovableObject(klass);
298 }
299 
Java_Main_waitForCompilation(JNIEnv *,jclass)300 extern "C" JNIEXPORT void JNICALL Java_Main_waitForCompilation(JNIEnv*, jclass) {
301   jit::Jit* jit = Runtime::Current()->GetJit();
302   if (jit != nullptr) {
303     jit->WaitForCompilationToFinish(Thread::Current());
304   }
305 }
306 
Java_Main_stopJit(JNIEnv *,jclass)307 extern "C" JNIEXPORT void JNICALL Java_Main_stopJit(JNIEnv*, jclass) {
308   jit::Jit* jit = Runtime::Current()->GetJit();
309   if (jit != nullptr) {
310     jit->Stop();
311   }
312 }
313 
Java_Main_startJit(JNIEnv *,jclass)314 extern "C" JNIEXPORT void JNICALL Java_Main_startJit(JNIEnv*, jclass) {
315   jit::Jit* jit = Runtime::Current()->GetJit();
316   if (jit != nullptr) {
317     jit->Start();
318   }
319 }
320 
321 }  // namespace art
322