blob: 271657fec35bf60f39df236cf6f0eab4f5c9c124 [file] [log] [blame]
Andreas Gampe89df7bf2015-09-30 13:13:21 -07001/*
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
Andreas Gampe542451c2016-07-26 09:02:02 -070019#include "base/enums.h"
Andreas Gampe89df7bf2015-09-30 13:13:21 -070020#include "base/logging.h"
21#include "dex_file-inl.h"
Alex Lightdba61482016-12-21 08:20:29 -080022#include "instrumentation.h"
Nicolas Geoffray491617a2016-07-19 17:06:23 +010023#include "jit/jit.h"
24#include "jit/jit_code_cache.h"
Andreas Gampe89df7bf2015-09-30 13:13:21 -070025#include "mirror/class-inl.h"
Nicolas Geoffray491617a2016-07-19 17:06:23 +010026#include "oat_quick_method_header.h"
Andreas Gampe89df7bf2015-09-30 13:13:21 -070027#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070028#include "scoped_thread_state_change-inl.h"
Nicolas Geoffray491617a2016-07-19 17:06:23 +010029#include "ScopedUtfChars.h"
Andreas Gampe89df7bf2015-09-30 13:13:21 -070030#include "thread-inl.h"
31
32namespace art {
33
Alex Lightdba61482016-12-21 08:20:29 -080034// public static native boolean hasJit();
35
36extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasJit(JNIEnv*, jclass) {
37 Runtime* runtime = Runtime::Current();
38 return runtime != nullptr
39 && runtime->GetJit() != nullptr
40 && runtime->GetInstrumentation()->GetCurrentInstrumentationLevel() !=
41 instrumentation::Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter;
42}
43
Andreas Gampe89df7bf2015-09-30 13:13:21 -070044// public static native boolean hasOatFile();
45
46extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasOatFile(JNIEnv* env, jclass cls) {
47 ScopedObjectAccess soa(env);
48
Mathieu Chartier0795f232016-09-27 18:43:30 -070049 ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls);
Andreas Gampe89df7bf2015-09-30 13:13:21 -070050 const DexFile& dex_file = klass->GetDexFile();
51 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
52 return (oat_dex_file != nullptr) ? JNI_TRUE : JNI_FALSE;
53}
54
55// public static native boolean runtimeIsSoftFail();
56
57extern "C" JNIEXPORT jboolean JNICALL Java_Main_runtimeIsSoftFail(JNIEnv* env ATTRIBUTE_UNUSED,
58 jclass cls ATTRIBUTE_UNUSED) {
59 return Runtime::Current()->IsVerificationSoftFail() ? JNI_TRUE : JNI_FALSE;
60}
61
62// public static native boolean isDex2OatEnabled();
63
64extern "C" JNIEXPORT jboolean JNICALL Java_Main_isDex2OatEnabled(JNIEnv* env ATTRIBUTE_UNUSED,
65 jclass cls ATTRIBUTE_UNUSED) {
66 return Runtime::Current()->IsDex2OatEnabled();
67}
68
69// public static native boolean hasImage();
70
71extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasImage(JNIEnv* env ATTRIBUTE_UNUSED,
72 jclass cls ATTRIBUTE_UNUSED) {
Jeff Haodcdc85b2015-12-04 14:06:18 -080073 return Runtime::Current()->GetHeap()->HasBootImageSpace();
Andreas Gampe89df7bf2015-09-30 13:13:21 -070074}
75
76// public static native boolean isImageDex2OatEnabled();
77
78extern "C" JNIEXPORT jboolean JNICALL Java_Main_isImageDex2OatEnabled(JNIEnv* env ATTRIBUTE_UNUSED,
79 jclass cls ATTRIBUTE_UNUSED) {
80 return Runtime::Current()->IsImageDex2OatEnabled();
81}
82
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -070083// public static native boolean compiledWithOptimizing();
84// Did we use the optimizing compiler to compile this?
85
86extern "C" JNIEXPORT jboolean JNICALL Java_Main_compiledWithOptimizing(JNIEnv* env, jclass cls) {
87 ScopedObjectAccess soa(env);
88
Mathieu Chartier0795f232016-09-27 18:43:30 -070089 ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls);
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -070090 const DexFile& dex_file = klass->GetDexFile();
91 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
92 if (oat_dex_file == nullptr) {
93 // Could be JIT, which also uses optimizing, but conservatively say no.
94 return JNI_FALSE;
95 }
96 const OatFile* oat_file = oat_dex_file->GetOatFile();
97 CHECK(oat_file != nullptr);
98
99 const char* cmd_line = oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kDex2OatCmdLineKey);
100 CHECK(cmd_line != nullptr); // Huh? This should not happen.
101
102 // Check the backend.
103 constexpr const char* kCompilerBackend = "--compiler-backend=";
104 const char* backend = strstr(cmd_line, kCompilerBackend);
105 if (backend != nullptr) {
106 // If it's set, make sure it's optimizing.
107 backend += strlen(kCompilerBackend);
108 if (strncmp(backend, "Optimizing", strlen("Optimizing")) != 0) {
109 return JNI_FALSE;
110 }
111 }
112
113 // Check the filter.
114 constexpr const char* kCompilerFilter = "--compiler-filter=";
115 const char* filter = strstr(cmd_line, kCompilerFilter);
116 if (filter != nullptr) {
117 // If it's set, make sure it's not interpret-only|verify-none|verify-at-runtime.
118 // Note: The space filter might have an impact on the test, but ignore that for now.
119 filter += strlen(kCompilerFilter);
120 constexpr const char* kInterpretOnly = "interpret-only";
121 constexpr const char* kVerifyNone = "verify-none";
122 constexpr const char* kVerifyAtRuntime = "verify-at-runtime";
123 if (strncmp(filter, kInterpretOnly, strlen(kInterpretOnly)) == 0 ||
124 strncmp(filter, kVerifyNone, strlen(kVerifyNone)) == 0 ||
125 strncmp(filter, kVerifyAtRuntime, strlen(kVerifyAtRuntime)) == 0) {
126 return JNI_FALSE;
127 }
128 }
129
130 return JNI_TRUE;
131}
132
Nicolas Geoffray51c17fa2016-11-25 15:56:12 +0000133extern "C" JNIEXPORT jboolean JNICALL Java_Main_isAotCompiled(JNIEnv* env,
134 jclass,
135 jclass cls,
136 jstring method_name) {
137 Thread* self = Thread::Current();
138 ScopedObjectAccess soa(self);
139 ScopedUtfChars chars(env, method_name);
140 CHECK(chars.c_str() != nullptr);
141 ArtMethod* method = soa.Decode<mirror::Class>(cls)->FindDeclaredDirectMethodByName(
142 chars.c_str(), kRuntimePointerSize);
143 const void* code = method->GetOatMethodQuickCode(kRuntimePointerSize);
144 jit::Jit* jit = Runtime::Current()->GetJit();
145 if (jit != nullptr && jit->GetCodeCache()->ContainsPc(code)) {
146 return true;
147 }
148 return code != nullptr;
149}
150
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100151extern "C" JNIEXPORT void JNICALL Java_Main_ensureJitCompiled(JNIEnv* env,
152 jclass,
153 jclass cls,
154 jstring method_name) {
Alex Lighta64fa432017-01-09 13:31:06 -0800155 Runtime* runtime = Runtime::Current();
156 if (runtime == nullptr) {
157 // We must be on the RI. We will just roll with it.
158 return;
159 }
160 jit::Jit* jit = runtime->GetJit();
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100161 if (jit == nullptr) {
162 return;
163 }
164
Nicolas Geoffray44cea192016-08-03 20:48:13 +0100165 Thread* self = Thread::Current();
Nicolas Geoffrayd55f4ab2016-08-02 18:49:25 +0100166 ArtMethod* method = nullptr;
167 {
Nicolas Geoffray44cea192016-08-03 20:48:13 +0100168 ScopedObjectAccess soa(self);
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100169
Nicolas Geoffrayd55f4ab2016-08-02 18:49:25 +0100170 ScopedUtfChars chars(env, method_name);
171 CHECK(chars.c_str() != nullptr);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700172 method = soa.Decode<mirror::Class>(cls)->FindDeclaredDirectMethodByName(
Nicolas Geoffrayd55f4ab2016-08-02 18:49:25 +0100173 chars.c_str(), kRuntimePointerSize);
Alex Lighta64fa432017-01-09 13:31:06 -0800174 if (method == nullptr) {
175 method = soa.Decode<mirror::Class>(cls)->FindDeclaredVirtualMethodByName(
176 chars.c_str(), kRuntimePointerSize);
177 }
178 DCHECK(method != nullptr) << "Unable to find method called " << chars.c_str();
Nicolas Geoffrayd55f4ab2016-08-02 18:49:25 +0100179 }
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100180
181 jit::JitCodeCache* code_cache = jit->GetCodeCache();
182 OatQuickMethodHeader* header = nullptr;
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100183 while (true) {
184 header = OatQuickMethodHeader::FromEntryPoint(method->GetEntryPointFromQuickCompiledCode());
185 if (code_cache->ContainsPc(header->GetCode())) {
186 break;
187 } else {
188 // Sleep to yield to the compiler thread.
189 usleep(1000);
Nicolas Geoffray44cea192016-08-03 20:48:13 +0100190 ScopedObjectAccess soa(self);
Nicolas Geoffrayd55f4ab2016-08-02 18:49:25 +0100191 // Make sure there is a profiling info, required by the compiler.
Nicolas Geoffray44cea192016-08-03 20:48:13 +0100192 ProfilingInfo::Create(self, method, /* retry_allocation */ true);
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100193 // Will either ensure it's compiled or do the compilation itself.
Nicolas Geoffray44cea192016-08-03 20:48:13 +0100194 jit->CompileMethod(method, self, /* osr */ false);
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100195 }
196 }
197}
198
Mingyao Yang063fc772016-08-02 11:02:54 -0700199extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasSingleImplementation(JNIEnv* env,
200 jclass,
201 jclass cls,
202 jstring method_name) {
203 ArtMethod* method = nullptr;
204 ScopedObjectAccess soa(Thread::Current());
205 ScopedUtfChars chars(env, method_name);
206 CHECK(chars.c_str() != nullptr);
207 method = soa.Decode<mirror::Class>(cls)->FindDeclaredVirtualMethodByName(
208 chars.c_str(), kRuntimePointerSize);
209 return method->HasSingleImplementation();
210}
211
Calin Juravle857f0582016-12-20 14:36:59 +0000212extern "C" JNIEXPORT int JNICALL Java_Main_getHotnessCounter(JNIEnv* env,
213 jclass,
214 jclass cls,
215 jstring method_name) {
216 jit::Jit* jit = Runtime::Current()->GetJit();
217 if (jit == nullptr) {
218 // The hotness counter is valid only under JIT.
219 // If we don't JIT return 0 to match test expectations.
220 return 0;
221 }
222
223 ArtMethod* method = nullptr;
224 {
225 ScopedObjectAccess soa(Thread::Current());
226
227 ScopedUtfChars chars(env, method_name);
228 CHECK(chars.c_str() != nullptr);
229 method = soa.Decode<mirror::Class>(cls)->FindDeclaredDirectMethodByName(
230 chars.c_str(), kRuntimePointerSize);
231 }
232
233 return method->GetCounter();
234}
235
Andreas Gampe89df7bf2015-09-30 13:13:21 -0700236} // namespace art