blob: f26e122580b4272a3c4679359ec4a8973ecb1710 [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"
Nicolas Geoffray491617a2016-07-19 17:06:23 +010022#include "jit/jit.h"
23#include "jit/jit_code_cache.h"
Andreas Gampe89df7bf2015-09-30 13:13:21 -070024#include "mirror/class-inl.h"
Nicolas Geoffray491617a2016-07-19 17:06:23 +010025#include "oat_quick_method_header.h"
Andreas Gampe89df7bf2015-09-30 13:13:21 -070026#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070027#include "scoped_thread_state_change-inl.h"
Nicolas Geoffray491617a2016-07-19 17:06:23 +010028#include "ScopedUtfChars.h"
Andreas Gampe89df7bf2015-09-30 13:13:21 -070029#include "thread-inl.h"
30
31namespace art {
32
33// public static native boolean hasOatFile();
34
35extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasOatFile(JNIEnv* env, jclass cls) {
36 ScopedObjectAccess soa(env);
37
Mathieu Chartier0795f232016-09-27 18:43:30 -070038 ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls);
Andreas Gampe89df7bf2015-09-30 13:13:21 -070039 const DexFile& dex_file = klass->GetDexFile();
40 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
41 return (oat_dex_file != nullptr) ? JNI_TRUE : JNI_FALSE;
42}
43
44// public static native boolean runtimeIsSoftFail();
45
46extern "C" JNIEXPORT jboolean JNICALL Java_Main_runtimeIsSoftFail(JNIEnv* env ATTRIBUTE_UNUSED,
47 jclass cls ATTRIBUTE_UNUSED) {
48 return Runtime::Current()->IsVerificationSoftFail() ? JNI_TRUE : JNI_FALSE;
49}
50
51// public static native boolean isDex2OatEnabled();
52
53extern "C" JNIEXPORT jboolean JNICALL Java_Main_isDex2OatEnabled(JNIEnv* env ATTRIBUTE_UNUSED,
54 jclass cls ATTRIBUTE_UNUSED) {
55 return Runtime::Current()->IsDex2OatEnabled();
56}
57
58// public static native boolean hasImage();
59
60extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasImage(JNIEnv* env ATTRIBUTE_UNUSED,
61 jclass cls ATTRIBUTE_UNUSED) {
Jeff Haodcdc85b2015-12-04 14:06:18 -080062 return Runtime::Current()->GetHeap()->HasBootImageSpace();
Andreas Gampe89df7bf2015-09-30 13:13:21 -070063}
64
65// public static native boolean isImageDex2OatEnabled();
66
67extern "C" JNIEXPORT jboolean JNICALL Java_Main_isImageDex2OatEnabled(JNIEnv* env ATTRIBUTE_UNUSED,
68 jclass cls ATTRIBUTE_UNUSED) {
69 return Runtime::Current()->IsImageDex2OatEnabled();
70}
71
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -070072// public static native boolean compiledWithOptimizing();
73// Did we use the optimizing compiler to compile this?
74
75extern "C" JNIEXPORT jboolean JNICALL Java_Main_compiledWithOptimizing(JNIEnv* env, jclass cls) {
76 ScopedObjectAccess soa(env);
77
Mathieu Chartier0795f232016-09-27 18:43:30 -070078 ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls);
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -070079 const DexFile& dex_file = klass->GetDexFile();
80 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
81 if (oat_dex_file == nullptr) {
82 // Could be JIT, which also uses optimizing, but conservatively say no.
83 return JNI_FALSE;
84 }
85 const OatFile* oat_file = oat_dex_file->GetOatFile();
86 CHECK(oat_file != nullptr);
87
88 const char* cmd_line = oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kDex2OatCmdLineKey);
89 CHECK(cmd_line != nullptr); // Huh? This should not happen.
90
91 // Check the backend.
92 constexpr const char* kCompilerBackend = "--compiler-backend=";
93 const char* backend = strstr(cmd_line, kCompilerBackend);
94 if (backend != nullptr) {
95 // If it's set, make sure it's optimizing.
96 backend += strlen(kCompilerBackend);
97 if (strncmp(backend, "Optimizing", strlen("Optimizing")) != 0) {
98 return JNI_FALSE;
99 }
100 }
101
102 // Check the filter.
103 constexpr const char* kCompilerFilter = "--compiler-filter=";
104 const char* filter = strstr(cmd_line, kCompilerFilter);
105 if (filter != nullptr) {
106 // If it's set, make sure it's not interpret-only|verify-none|verify-at-runtime.
107 // Note: The space filter might have an impact on the test, but ignore that for now.
108 filter += strlen(kCompilerFilter);
109 constexpr const char* kInterpretOnly = "interpret-only";
110 constexpr const char* kVerifyNone = "verify-none";
111 constexpr const char* kVerifyAtRuntime = "verify-at-runtime";
112 if (strncmp(filter, kInterpretOnly, strlen(kInterpretOnly)) == 0 ||
113 strncmp(filter, kVerifyNone, strlen(kVerifyNone)) == 0 ||
114 strncmp(filter, kVerifyAtRuntime, strlen(kVerifyAtRuntime)) == 0) {
115 return JNI_FALSE;
116 }
117 }
118
119 return JNI_TRUE;
120}
121
Nicolas Geoffray51c17fa2016-11-25 15:56:12 +0000122extern "C" JNIEXPORT jboolean JNICALL Java_Main_isAotCompiled(JNIEnv* env,
123 jclass,
124 jclass cls,
125 jstring method_name) {
126 Thread* self = Thread::Current();
127 ScopedObjectAccess soa(self);
128 ScopedUtfChars chars(env, method_name);
129 CHECK(chars.c_str() != nullptr);
130 ArtMethod* method = soa.Decode<mirror::Class>(cls)->FindDeclaredDirectMethodByName(
131 chars.c_str(), kRuntimePointerSize);
132 const void* code = method->GetOatMethodQuickCode(kRuntimePointerSize);
133 jit::Jit* jit = Runtime::Current()->GetJit();
134 if (jit != nullptr && jit->GetCodeCache()->ContainsPc(code)) {
135 return true;
136 }
137 return code != nullptr;
138}
139
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100140extern "C" JNIEXPORT void JNICALL Java_Main_ensureJitCompiled(JNIEnv* env,
141 jclass,
142 jclass cls,
143 jstring method_name) {
144 jit::Jit* jit = Runtime::Current()->GetJit();
145 if (jit == nullptr) {
146 return;
147 }
148
Nicolas Geoffray44cea192016-08-03 20:48:13 +0100149 Thread* self = Thread::Current();
Nicolas Geoffrayd55f4ab2016-08-02 18:49:25 +0100150 ArtMethod* method = nullptr;
151 {
Nicolas Geoffray44cea192016-08-03 20:48:13 +0100152 ScopedObjectAccess soa(self);
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100153
Nicolas Geoffrayd55f4ab2016-08-02 18:49:25 +0100154 ScopedUtfChars chars(env, method_name);
155 CHECK(chars.c_str() != nullptr);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700156 method = soa.Decode<mirror::Class>(cls)->FindDeclaredDirectMethodByName(
Nicolas Geoffrayd55f4ab2016-08-02 18:49:25 +0100157 chars.c_str(), kRuntimePointerSize);
158 }
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100159
160 jit::JitCodeCache* code_cache = jit->GetCodeCache();
161 OatQuickMethodHeader* header = nullptr;
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100162 while (true) {
163 header = OatQuickMethodHeader::FromEntryPoint(method->GetEntryPointFromQuickCompiledCode());
164 if (code_cache->ContainsPc(header->GetCode())) {
165 break;
166 } else {
167 // Sleep to yield to the compiler thread.
168 usleep(1000);
Nicolas Geoffray44cea192016-08-03 20:48:13 +0100169 ScopedObjectAccess soa(self);
Nicolas Geoffrayd55f4ab2016-08-02 18:49:25 +0100170 // Make sure there is a profiling info, required by the compiler.
Nicolas Geoffray44cea192016-08-03 20:48:13 +0100171 ProfilingInfo::Create(self, method, /* retry_allocation */ true);
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100172 // Will either ensure it's compiled or do the compilation itself.
Nicolas Geoffray44cea192016-08-03 20:48:13 +0100173 jit->CompileMethod(method, self, /* osr */ false);
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100174 }
175 }
176}
177
Mingyao Yang063fc772016-08-02 11:02:54 -0700178extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasSingleImplementation(JNIEnv* env,
179 jclass,
180 jclass cls,
181 jstring method_name) {
182 ArtMethod* method = nullptr;
183 ScopedObjectAccess soa(Thread::Current());
184 ScopedUtfChars chars(env, method_name);
185 CHECK(chars.c_str() != nullptr);
186 method = soa.Decode<mirror::Class>(cls)->FindDeclaredVirtualMethodByName(
187 chars.c_str(), kRuntimePointerSize);
188 return method->HasSingleImplementation();
189}
190
Calin Juravle857f0582016-12-20 14:36:59 +0000191extern "C" JNIEXPORT int JNICALL Java_Main_getHotnessCounter(JNIEnv* env,
192 jclass,
193 jclass cls,
194 jstring method_name) {
195 jit::Jit* jit = Runtime::Current()->GetJit();
196 if (jit == nullptr) {
197 // The hotness counter is valid only under JIT.
198 // If we don't JIT return 0 to match test expectations.
199 return 0;
200 }
201
202 ArtMethod* method = nullptr;
203 {
204 ScopedObjectAccess soa(Thread::Current());
205
206 ScopedUtfChars chars(env, method_name);
207 CHECK(chars.c_str() != nullptr);
208 method = soa.Decode<mirror::Class>(cls)->FindDeclaredDirectMethodByName(
209 chars.c_str(), kRuntimePointerSize);
210 }
211
212 return method->GetCounter();
213}
214
Andreas Gampe89df7bf2015-09-30 13:13:21 -0700215} // namespace art