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 "base/logging.h"
20 #include "dex_file-inl.h"
21 #include "jit/jit.h"
22 #include "jit/jit_code_cache.h"
23 #include "mirror/class-inl.h"
24 #include "nth_caller_visitor.h"
25 #include "oat_quick_method_header.h"
26 #include "runtime.h"
27 #include "scoped_thread_state_change.h"
28 #include "ScopedUtfChars.h"
29 #include "stack.h"
30 #include "thread-inl.h"
31
32 namespace art {
33
34 // public static native boolean hasOatFile();
35
Java_Main_hasOatFile(JNIEnv * env,jclass cls)36 extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasOatFile(JNIEnv* env, jclass cls) {
37 ScopedObjectAccess soa(env);
38
39 mirror::Class* klass = soa.Decode<mirror::Class*>(cls);
40 const DexFile& dex_file = klass->GetDexFile();
41 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
42 return (oat_dex_file != nullptr) ? JNI_TRUE : JNI_FALSE;
43 }
44
45 // public static native boolean runtimeIsSoftFail();
46
Java_Main_runtimeIsSoftFail(JNIEnv * env ATTRIBUTE_UNUSED,jclass cls ATTRIBUTE_UNUSED)47 extern "C" JNIEXPORT jboolean JNICALL Java_Main_runtimeIsSoftFail(JNIEnv* env ATTRIBUTE_UNUSED,
48 jclass cls ATTRIBUTE_UNUSED) {
49 return Runtime::Current()->IsVerificationSoftFail() ? JNI_TRUE : JNI_FALSE;
50 }
51
52 // public static native boolean isDex2OatEnabled();
53
Java_Main_isDex2OatEnabled(JNIEnv * env ATTRIBUTE_UNUSED,jclass cls ATTRIBUTE_UNUSED)54 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isDex2OatEnabled(JNIEnv* env ATTRIBUTE_UNUSED,
55 jclass cls ATTRIBUTE_UNUSED) {
56 return Runtime::Current()->IsDex2OatEnabled();
57 }
58
59 // public static native boolean hasImage();
60
Java_Main_hasImage(JNIEnv * env ATTRIBUTE_UNUSED,jclass cls ATTRIBUTE_UNUSED)61 extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasImage(JNIEnv* env ATTRIBUTE_UNUSED,
62 jclass cls ATTRIBUTE_UNUSED) {
63 return Runtime::Current()->GetHeap()->HasBootImageSpace();
64 }
65
66 // public static native boolean isImageDex2OatEnabled();
67
Java_Main_isImageDex2OatEnabled(JNIEnv * env ATTRIBUTE_UNUSED,jclass cls ATTRIBUTE_UNUSED)68 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isImageDex2OatEnabled(JNIEnv* env ATTRIBUTE_UNUSED,
69 jclass cls ATTRIBUTE_UNUSED) {
70 return Runtime::Current()->IsImageDex2OatEnabled();
71 }
72
73 // public static native boolean compiledWithOptimizing();
74 // Did we use the optimizing compiler to compile this?
75
Java_Main_compiledWithOptimizing(JNIEnv * env,jclass cls)76 extern "C" JNIEXPORT jboolean JNICALL Java_Main_compiledWithOptimizing(JNIEnv* env, jclass cls) {
77 ScopedObjectAccess soa(env);
78
79 mirror::Class* klass = soa.Decode<mirror::Class*>(cls);
80 const DexFile& dex_file = klass->GetDexFile();
81 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
82 if (oat_dex_file == nullptr) {
83 // Could be JIT, which also uses optimizing, but conservatively say no.
84 return JNI_FALSE;
85 }
86 const OatFile* oat_file = oat_dex_file->GetOatFile();
87 CHECK(oat_file != nullptr);
88
89 const char* cmd_line = oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kDex2OatCmdLineKey);
90 CHECK(cmd_line != nullptr); // Huh? This should not happen.
91
92 // Check the backend.
93 constexpr const char* kCompilerBackend = "--compiler-backend=";
94 const char* backend = strstr(cmd_line, kCompilerBackend);
95 if (backend != nullptr) {
96 // If it's set, make sure it's optimizing.
97 backend += strlen(kCompilerBackend);
98 if (strncmp(backend, "Optimizing", strlen("Optimizing")) != 0) {
99 return JNI_FALSE;
100 }
101 }
102
103 // Check the filter.
104 constexpr const char* kCompilerFilter = "--compiler-filter=";
105 const char* filter = strstr(cmd_line, kCompilerFilter);
106 if (filter != nullptr) {
107 // If it's set, make sure it's not interpret-only|verify-none|verify-at-runtime.
108 // Note: The space filter might have an impact on the test, but ignore that for now.
109 filter += strlen(kCompilerFilter);
110 constexpr const char* kInterpretOnly = "interpret-only";
111 constexpr const char* kVerifyNone = "verify-none";
112 constexpr const char* kVerifyAtRuntime = "verify-at-runtime";
113 if (strncmp(filter, kInterpretOnly, strlen(kInterpretOnly)) == 0 ||
114 strncmp(filter, kVerifyNone, strlen(kVerifyNone)) == 0 ||
115 strncmp(filter, kVerifyAtRuntime, strlen(kVerifyAtRuntime)) == 0) {
116 return JNI_FALSE;
117 }
118 }
119
120 return JNI_TRUE;
121 }
122
Java_Main_ensureJitCompiled(JNIEnv * env,jclass,jclass cls,jstring method_name)123 extern "C" JNIEXPORT void JNICALL Java_Main_ensureJitCompiled(JNIEnv* env,
124 jclass,
125 jclass cls,
126 jstring method_name) {
127 jit::Jit* jit = Runtime::Current()->GetJit();
128 if (jit == nullptr) {
129 return;
130 }
131
132 ScopedObjectAccess soa(Thread::Current());
133
134 ScopedUtfChars chars(env, method_name);
135 CHECK(chars.c_str() != nullptr);
136
137 mirror::Class* klass = soa.Decode<mirror::Class*>(cls);
138 ArtMethod* method = klass->FindDeclaredDirectMethodByName(chars.c_str(), sizeof(void*));
139
140 jit::JitCodeCache* code_cache = jit->GetCodeCache();
141 OatQuickMethodHeader* header = nullptr;
142 // Make sure there is a profiling info, required by the compiler.
143 ProfilingInfo::Create(soa.Self(), method, /* retry_allocation */ true);
144 while (true) {
145 header = OatQuickMethodHeader::FromEntryPoint(method->GetEntryPointFromQuickCompiledCode());
146 if (code_cache->ContainsPc(header->GetCode())) {
147 break;
148 } else {
149 // Sleep to yield to the compiler thread.
150 usleep(1000);
151 // Will either ensure it's compiled or do the compilation itself.
152 jit->CompileMethod(method, soa.Self(), /* osr */ false);
153 }
154 }
155 }
156
157 } // namespace art
158