blob: 3799bcf1a5fed7eb7d70ab119bc746f663f3bc2d [file] [log] [blame]
Elliott Hughes7ede61e2011-09-14 18:18:06 -07001/*
2 * Copyright (C) 2008 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 "class_linker.h"
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070018#include "debugger.h"
Elliott Hughes7ede61e2011-09-14 18:18:06 -070019#include "jni_internal.h"
20#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080021#include "object_utils.h"
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080022#include "scoped_heap_lock.h"
Elliott Hughes88c5c352012-03-15 18:49:48 -070023#include "scoped_thread_list_lock.h"
Elliott Hughes8cf5bc02012-02-02 16:32:16 -080024#include "space.h"
Elliott Hughes7ede61e2011-09-14 18:18:06 -070025#include "thread.h"
Elliott Hughes88c5c352012-03-15 18:49:48 -070026#include "thread_list.h"
Elliott Hughes7ede61e2011-09-14 18:18:06 -070027
28#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
29#include "toStringArray.h"
30
31#include <limits.h>
32
33namespace art {
34
Elliott Hughes0512f022012-03-15 22:10:52 -070035static jfloat VMRuntime_getTargetHeapUtilization(JNIEnv*, jobject) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080036 return Runtime::Current()->GetHeap()->GetTargetHeapUtilization();
Elliott Hughes7ede61e2011-09-14 18:18:06 -070037}
38
Elliott Hughes0512f022012-03-15 22:10:52 -070039static void VMRuntime_nativeSetTargetHeapUtilization(JNIEnv*, jobject, jfloat target) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080040 Runtime::Current()->GetHeap()->SetTargetHeapUtilization(target);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070041}
42
Elliott Hughes0512f022012-03-15 22:10:52 -070043static void VMRuntime_startJitCompilation(JNIEnv*, jobject) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -070044}
45
Elliott Hughes0512f022012-03-15 22:10:52 -070046static void VMRuntime_disableJitCompilation(JNIEnv*, jobject) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -070047}
48
Elliott Hughes0512f022012-03-15 22:10:52 -070049static jobject VMRuntime_newNonMovableArray(JNIEnv* env, jobject, jclass javaElementClass, jint length) {
Brian Carlstromb82b6872011-10-26 17:18:07 -070050 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070051#ifdef MOVING_GARBAGE_COLLECTOR
52 // TODO: right now, we don't have a copying collector, so there's no need
53 // to do anything special here, but we ought to pass the non-movability
54 // through to the allocator.
55 UNIMPLEMENTED(FATAL);
56#endif
57
58 Class* element_class = Decode<Class*>(env, javaElementClass);
59 if (element_class == NULL) {
60 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "element class == null");
61 return NULL;
62 }
63 if (length < 0) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070064 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", length);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070065 return NULL;
66 }
67
68 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
69 std::string descriptor;
70 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080071 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughesc3b77c72011-12-15 20:56:48 -080072 Class* array_class = class_linker->FindClass(descriptor.c_str(), NULL);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070073 Array* result = Array::Alloc(array_class, length);
74 if (result == NULL) {
75 return NULL;
76 }
77 return AddLocalReference<jobject>(env, result);
78}
79
Elliott Hughes0512f022012-03-15 22:10:52 -070080static jlong VMRuntime_addressOf(JNIEnv* env, jobject, jobject javaArray) {
Ian Rogersa15e67d2012-02-28 13:51:55 -080081 if (javaArray == NULL) { // Most likely allocation failed
82 return 0;
83 }
Brian Carlstromb82b6872011-10-26 17:18:07 -070084 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070085 Array* array = Decode<Array*>(env, javaArray);
86 if (!array->IsArrayInstance()) {
87 Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;", "not an array");
88 return 0;
89 }
90 // TODO: we should also check that this is a non-movable array.
Ian Rogersa15e67d2012-02-28 13:51:55 -080091 return reinterpret_cast<uintptr_t>(array->GetRawData(array->GetClass()->GetComponentSize()));
Elliott Hughes7ede61e2011-09-14 18:18:06 -070092}
93
Elliott Hughes0512f022012-03-15 22:10:52 -070094static void VMRuntime_clearGrowthLimit(JNIEnv*, jobject) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080095 Runtime::Current()->GetHeap()->ClearGrowthLimit();
Elliott Hughes7ede61e2011-09-14 18:18:06 -070096}
97
Elliott Hughes0512f022012-03-15 22:10:52 -070098static jboolean VMRuntime_isDebuggerActive(JNIEnv*, jobject) {
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070099 return Dbg::IsDebuggerConnected();
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700100}
101
Elliott Hughes0512f022012-03-15 22:10:52 -0700102static jobjectArray VMRuntime_properties(JNIEnv* env, jobject) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700103 return toStringArray(env, Runtime::Current()->GetProperties());
104}
105
Brian Carlstrom7d5ffb52012-02-01 14:27:54 -0800106// This is for backward compatibility with dalvik which returned the
107// meaningless "." when no boot classpath or classpath was
108// specified. Unfortunately, some tests were using java.class.path to
109// lookup relative file locations, so they are counting on this to be
110// ".", presumably some applications or libraries could have as well.
Elliott Hughes0512f022012-03-15 22:10:52 -0700111static const char* DefaultToDot(const std::string& class_path) {
Brian Carlstrom7d5ffb52012-02-01 14:27:54 -0800112 return class_path.empty() ? "." : class_path.c_str();
113}
114
Elliott Hughes0512f022012-03-15 22:10:52 -0700115static jstring VMRuntime_bootClassPath(JNIEnv* env, jobject) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800116 return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetBootClassPathString()));
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700117}
118
Elliott Hughes0512f022012-03-15 22:10:52 -0700119static jstring VMRuntime_classPath(JNIEnv* env, jobject) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800120 return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetClassPathString()));
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700121}
122
Elliott Hughes0512f022012-03-15 22:10:52 -0700123static jstring VMRuntime_vmVersion(JNIEnv* env, jobject) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700124 return env->NewStringUTF(Runtime::Current()->GetVersion());
125}
126
Elliott Hughes88c5c352012-03-15 18:49:48 -0700127static void DisableCheckJniCallback(Thread* t, void*) {
128 LOG(INFO) << "Disabling CheckJNI for " << *t;
129 t->GetJniEnv()->SetCheckJniEnabled(false);
130}
131
Elliott Hughes0512f022012-03-15 22:10:52 -0700132static void VMRuntime_setTargetSdkVersion(JNIEnv* env, jobject, jint targetSdkVersion) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700133 // This is the target SDK version of the app we're about to run.
Elliott Hughesc2dc62d2012-01-17 20:06:12 -0800134 // Note that targetSdkVersion may be CUR_DEVELOPMENT (10000).
135 // Note that targetSdkVersion may be 0, meaning "current".
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700136 if (targetSdkVersion > 0 && targetSdkVersion <= 13 /* honeycomb-mr2 */) {
Elliott Hughes88c5c352012-03-15 18:49:48 -0700137 Runtime* runtime = Runtime::Current();
138 JavaVMExt* vm = runtime->GetJavaVM();
139
140 if (vm->check_jni) {
141 LOG(WARNING) << "Turning off CheckJNI so we can turn on JNI app bug workarounds...";
142 ScopedThreadListLock thread_list_lock;
143 vm->SetCheckJniEnabled(false);
144 runtime->GetThreadList()->ForEach(DisableCheckJniCallback, NULL);
Ian Rogers475a6442012-02-21 15:39:39 -0800145 }
Elliott Hughes88c5c352012-03-15 18:49:48 -0700146
147 LOG(INFO) << "Turning on JNI app bug workarounds for target SDK version "
148 << targetSdkVersion << "...";
149 vm->work_around_app_jni_bugs = true;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700150 }
151}
152
Elliott Hughes0512f022012-03-15 22:10:52 -0700153static void VMRuntime_trimHeap(JNIEnv* env, jobject) {
Elliott Hughes37be8fb2012-02-03 09:34:29 -0800154 ScopedHeapLock heap_lock;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800155 Heap* heap = Runtime::Current()->GetHeap();
156 size_t alloc_space_size = heap->GetAllocSpace()->Size();
157 float utilization = static_cast<float>(heap->GetBytesAllocated()) / alloc_space_size;
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800158 uint64_t start_ns = NanoTime();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800159 heap->GetAllocSpace()->Trim();
Elliott Hughes0d0ba692012-02-03 17:28:52 -0800160 LOG(INFO) << "Parallel heap trimming took " << PrettyDuration(NanoTime() - start_ns)
161 << " on a " << PrettySize(alloc_space_size)
162 << " heap with " << static_cast<int>(100 * utilization) << "% utilization";
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800163}
164
Elliott Hughes0512f022012-03-15 22:10:52 -0700165static JNINativeMethod gMethods[] = {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700166 NATIVE_METHOD(VMRuntime, addressOf, "(Ljava/lang/Object;)J"),
167 NATIVE_METHOD(VMRuntime, bootClassPath, "()Ljava/lang/String;"),
168 NATIVE_METHOD(VMRuntime, classPath, "()Ljava/lang/String;"),
169 NATIVE_METHOD(VMRuntime, clearGrowthLimit, "()V"),
170 NATIVE_METHOD(VMRuntime, disableJitCompilation, "()V"),
171 NATIVE_METHOD(VMRuntime, getTargetHeapUtilization, "()F"),
172 NATIVE_METHOD(VMRuntime, isDebuggerActive, "()Z"),
173 NATIVE_METHOD(VMRuntime, nativeSetTargetHeapUtilization, "(F)V"),
174 NATIVE_METHOD(VMRuntime, newNonMovableArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
175 NATIVE_METHOD(VMRuntime, properties, "()[Ljava/lang/String;"),
176 NATIVE_METHOD(VMRuntime, setTargetSdkVersion, "(I)V"),
177 NATIVE_METHOD(VMRuntime, startJitCompilation, "()V"),
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800178 NATIVE_METHOD(VMRuntime, trimHeap, "()V"),
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700179 NATIVE_METHOD(VMRuntime, vmVersion, "()Ljava/lang/String;"),
180};
181
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700182void register_dalvik_system_VMRuntime(JNIEnv* env) {
183 jniRegisterNativeMethods(env, "dalvik/system/VMRuntime", gMethods, NELEM(gMethods));
184}
185
186} // namespace art