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