blob: 09ca251ff375963059a633a57acf8e60b2bb0ba5 [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
Elliott Hugheseac76672012-05-24 21:56:51 -070017#include <limits.h>
18
Elliott Hughes7ede61e2011-09-14 18:18:06 -070019#include "class_linker.h"
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070020#include "debugger.h"
Elliott Hughes7ede61e2011-09-14 18:18:06 -070021#include "jni_internal.h"
22#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080023#include "object_utils.h"
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080024#include "scoped_heap_lock.h"
Elliott Hughes88c5c352012-03-15 18:49:48 -070025#include "scoped_thread_list_lock.h"
Elliott Hughes8cf5bc02012-02-02 16:32:16 -080026#include "space.h"
Elliott Hughes7ede61e2011-09-14 18:18:06 -070027#include "thread.h"
Elliott Hughes88c5c352012-03-15 18:49:48 -070028#include "thread_list.h"
Elliott Hughes7ede61e2011-09-14 18:18:06 -070029#include "toStringArray.h"
30
Elliott Hughes9eebd3b2012-06-08 13:56:31 -070031extern "C" int dlmalloc_trim(size_t);
32extern "C" void dlmalloc_walk_free_pages(void(*)(void*, void*, void*), void*);
33
Elliott Hughes7ede61e2011-09-14 18:18:06 -070034namespace art {
35
Elliott Hughes0512f022012-03-15 22:10:52 -070036static jfloat VMRuntime_getTargetHeapUtilization(JNIEnv*, jobject) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080037 return Runtime::Current()->GetHeap()->GetTargetHeapUtilization();
Elliott Hughes7ede61e2011-09-14 18:18:06 -070038}
39
Elliott Hughes0512f022012-03-15 22:10:52 -070040static void VMRuntime_nativeSetTargetHeapUtilization(JNIEnv*, jobject, jfloat target) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080041 Runtime::Current()->GetHeap()->SetTargetHeapUtilization(target);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070042}
43
Elliott Hughes0512f022012-03-15 22:10:52 -070044static void VMRuntime_startJitCompilation(JNIEnv*, jobject) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -070045}
46
Elliott Hughes0512f022012-03-15 22:10:52 -070047static void VMRuntime_disableJitCompilation(JNIEnv*, jobject) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -070048}
49
Elliott Hughes0512f022012-03-15 22:10:52 -070050static jobject VMRuntime_newNonMovableArray(JNIEnv* env, jobject, jclass javaElementClass, jint length) {
Elliott Hughes34e06962012-04-09 13:55:55 -070051 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070052#ifdef MOVING_GARBAGE_COLLECTOR
53 // TODO: right now, we don't have a copying collector, so there's no need
54 // to do anything special here, but we ought to pass the non-movability
55 // through to the allocator.
56 UNIMPLEMENTED(FATAL);
57#endif
58
59 Class* element_class = Decode<Class*>(env, javaElementClass);
60 if (element_class == NULL) {
61 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "element class == null");
62 return NULL;
63 }
64 if (length < 0) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070065 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", length);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070066 return NULL;
67 }
68
69 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
70 std::string descriptor;
71 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080072 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughesc3b77c72011-12-15 20:56:48 -080073 Class* array_class = class_linker->FindClass(descriptor.c_str(), NULL);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070074 Array* result = Array::Alloc(array_class, length);
75 if (result == NULL) {
76 return NULL;
77 }
78 return AddLocalReference<jobject>(env, result);
79}
80
Elliott Hughes0512f022012-03-15 22:10:52 -070081static jlong VMRuntime_addressOf(JNIEnv* env, jobject, jobject javaArray) {
Ian Rogersa15e67d2012-02-28 13:51:55 -080082 if (javaArray == NULL) { // Most likely allocation failed
83 return 0;
84 }
Elliott Hughes34e06962012-04-09 13:55:55 -070085 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070086 Array* array = Decode<Array*>(env, javaArray);
87 if (!array->IsArrayInstance()) {
88 Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;", "not an array");
89 return 0;
90 }
91 // TODO: we should also check that this is a non-movable array.
Ian Rogersa15e67d2012-02-28 13:51:55 -080092 return reinterpret_cast<uintptr_t>(array->GetRawData(array->GetClass()->GetComponentSize()));
Elliott Hughes7ede61e2011-09-14 18:18:06 -070093}
94
Elliott Hughes0512f022012-03-15 22:10:52 -070095static void VMRuntime_clearGrowthLimit(JNIEnv*, jobject) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080096 Runtime::Current()->GetHeap()->ClearGrowthLimit();
Elliott Hughes7ede61e2011-09-14 18:18:06 -070097}
98
Elliott Hughes0512f022012-03-15 22:10:52 -070099static jboolean VMRuntime_isDebuggerActive(JNIEnv*, jobject) {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700100 return Dbg::IsDebuggerActive();
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700101}
102
Elliott Hughes0512f022012-03-15 22:10:52 -0700103static jobjectArray VMRuntime_properties(JNIEnv* env, jobject) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700104 return toStringArray(env, Runtime::Current()->GetProperties());
105}
106
Brian Carlstrom7d5ffb52012-02-01 14:27:54 -0800107// This is for backward compatibility with dalvik which returned the
108// meaningless "." when no boot classpath or classpath was
109// specified. Unfortunately, some tests were using java.class.path to
110// lookup relative file locations, so they are counting on this to be
111// ".", presumably some applications or libraries could have as well.
Elliott Hughes0512f022012-03-15 22:10:52 -0700112static const char* DefaultToDot(const std::string& class_path) {
Brian Carlstrom7d5ffb52012-02-01 14:27:54 -0800113 return class_path.empty() ? "." : class_path.c_str();
114}
115
Elliott Hughes0512f022012-03-15 22:10:52 -0700116static jstring VMRuntime_bootClassPath(JNIEnv* env, jobject) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800117 return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetBootClassPathString()));
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700118}
119
Elliott Hughes0512f022012-03-15 22:10:52 -0700120static jstring VMRuntime_classPath(JNIEnv* env, jobject) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800121 return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetClassPathString()));
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700122}
123
Elliott Hughes0512f022012-03-15 22:10:52 -0700124static jstring VMRuntime_vmVersion(JNIEnv* env, jobject) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700125 return env->NewStringUTF(Runtime::Current()->GetVersion());
126}
127
Shih-wei Liaofbe5b752012-04-19 00:26:00 -0700128#if !defined(ART_USE_LLVM_COMPILER)
Elliott Hughes88c5c352012-03-15 18:49:48 -0700129static void DisableCheckJniCallback(Thread* t, void*) {
Elliott Hughes88c5c352012-03-15 18:49:48 -0700130 t->GetJniEnv()->SetCheckJniEnabled(false);
131}
Shih-wei Liaofbe5b752012-04-19 00:26:00 -0700132#endif
Elliott Hughes88c5c352012-03-15 18:49:48 -0700133
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700134static void VMRuntime_setTargetSdkVersion(JNIEnv*, jobject, jint targetSdkVersion) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700135 // This is the target SDK version of the app we're about to run.
Elliott Hughesc2dc62d2012-01-17 20:06:12 -0800136 // Note that targetSdkVersion may be CUR_DEVELOPMENT (10000).
137 // Note that targetSdkVersion may be 0, meaning "current".
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700138 if (targetSdkVersion > 0 && targetSdkVersion <= 13 /* honeycomb-mr2 */) {
Elliott Hughes88c5c352012-03-15 18:49:48 -0700139 Runtime* runtime = Runtime::Current();
140 JavaVMExt* vm = runtime->GetJavaVM();
141
TDYa12726467572012-04-17 20:51:22 -0700142#if !defined(ART_USE_LLVM_COMPILER)
Elliott Hughes88c5c352012-03-15 18:49:48 -0700143 if (vm->check_jni) {
144 LOG(WARNING) << "Turning off CheckJNI so we can turn on JNI app bug workarounds...";
145 ScopedThreadListLock thread_list_lock;
146 vm->SetCheckJniEnabled(false);
147 runtime->GetThreadList()->ForEach(DisableCheckJniCallback, NULL);
Ian Rogers475a6442012-02-21 15:39:39 -0800148 }
Elliott Hughes88c5c352012-03-15 18:49:48 -0700149
150 LOG(INFO) << "Turning on JNI app bug workarounds for target SDK version "
151 << targetSdkVersion << "...";
TDYa12726467572012-04-17 20:51:22 -0700152
Elliott Hughes88c5c352012-03-15 18:49:48 -0700153 vm->work_around_app_jni_bugs = true;
TDYa12726467572012-04-17 20:51:22 -0700154#else
155 LOG(WARNING) << "LLVM does not work-around app jni bugs.";
156 vm->work_around_app_jni_bugs = false;
157#endif
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700158 }
159}
160
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700161static void VMRuntime_trimHeap(JNIEnv*, jobject) {
Elliott Hughes37be8fb2012-02-03 09:34:29 -0800162 ScopedHeapLock heap_lock;
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700163
164 // Trim the managed heap.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800165 Heap* heap = Runtime::Current()->GetHeap();
166 size_t alloc_space_size = heap->GetAllocSpace()->Size();
167 float utilization = static_cast<float>(heap->GetBytesAllocated()) / alloc_space_size;
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800168 uint64_t start_ns = NanoTime();
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700169
170 heap->Trim();
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700171
172 // Trim the native heap.
173 dlmalloc_trim(0);
174#if 0 // TODO: switch over to this when bionic has moved to dlmalloc 2.8.5
175 dlmalloc_inspect_all(MspaceMadviseCallback, NULL);
176#else
177 dlmalloc_walk_free_pages(MspaceMadviseCallback, NULL);
178#endif
179
Elliott Hughes0d0ba692012-02-03 17:28:52 -0800180 LOG(INFO) << "Parallel heap trimming took " << PrettyDuration(NanoTime() - start_ns)
181 << " on a " << PrettySize(alloc_space_size)
182 << " heap with " << static_cast<int>(100 * utilization) << "% utilization";
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800183}
184
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700185static void VMRuntime_concurrentGC(JNIEnv*, jobject) {
186 Runtime::Current()->GetHeap()->ConcurrentGC();
187}
188
Elliott Hughes0512f022012-03-15 22:10:52 -0700189static JNINativeMethod gMethods[] = {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700190 NATIVE_METHOD(VMRuntime, addressOf, "(Ljava/lang/Object;)J"),
191 NATIVE_METHOD(VMRuntime, bootClassPath, "()Ljava/lang/String;"),
192 NATIVE_METHOD(VMRuntime, classPath, "()Ljava/lang/String;"),
193 NATIVE_METHOD(VMRuntime, clearGrowthLimit, "()V"),
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700194 NATIVE_METHOD(VMRuntime, concurrentGC, "()V"),
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700195 NATIVE_METHOD(VMRuntime, disableJitCompilation, "()V"),
196 NATIVE_METHOD(VMRuntime, getTargetHeapUtilization, "()F"),
197 NATIVE_METHOD(VMRuntime, isDebuggerActive, "()Z"),
198 NATIVE_METHOD(VMRuntime, nativeSetTargetHeapUtilization, "(F)V"),
199 NATIVE_METHOD(VMRuntime, newNonMovableArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
200 NATIVE_METHOD(VMRuntime, properties, "()[Ljava/lang/String;"),
201 NATIVE_METHOD(VMRuntime, setTargetSdkVersion, "(I)V"),
202 NATIVE_METHOD(VMRuntime, startJitCompilation, "()V"),
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800203 NATIVE_METHOD(VMRuntime, trimHeap, "()V"),
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700204 NATIVE_METHOD(VMRuntime, vmVersion, "()Ljava/lang/String;"),
205};
206
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700207void register_dalvik_system_VMRuntime(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700208 REGISTER_NATIVE_METHODS("dalvik/system/VMRuntime");
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700209}
210
211} // namespace art