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