blob: 4ec1b92fbddf8028abf568f11ae7d33764e173ef [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"
Ian Rogers365c1022012-06-22 15:05:28 -070025#include "scoped_jni_thread_state.h"
Elliott Hughes88c5c352012-03-15 18:49:48 -070026#include "scoped_thread_list_lock.h"
Elliott Hughes8cf5bc02012-02-02 16:32:16 -080027#include "space.h"
Elliott Hughes7ede61e2011-09-14 18:18:06 -070028#include "thread.h"
Elliott Hughes88c5c352012-03-15 18:49:48 -070029#include "thread_list.h"
Elliott Hughes7ede61e2011-09-14 18:18:06 -070030#include "toStringArray.h"
31
Elliott Hughes9eebd3b2012-06-08 13:56:31 -070032extern "C" int dlmalloc_trim(size_t);
33extern "C" void dlmalloc_walk_free_pages(void(*)(void*, void*, void*), void*);
34
Elliott Hughes7ede61e2011-09-14 18:18:06 -070035namespace art {
36
Elliott Hughes0512f022012-03-15 22:10:52 -070037static jfloat VMRuntime_getTargetHeapUtilization(JNIEnv*, jobject) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080038 return Runtime::Current()->GetHeap()->GetTargetHeapUtilization();
Elliott Hughes7ede61e2011-09-14 18:18:06 -070039}
40
Elliott Hughes0512f022012-03-15 22:10:52 -070041static void VMRuntime_nativeSetTargetHeapUtilization(JNIEnv*, jobject, jfloat target) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080042 Runtime::Current()->GetHeap()->SetTargetHeapUtilization(target);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070043}
44
Elliott Hughes0512f022012-03-15 22:10:52 -070045static void VMRuntime_startJitCompilation(JNIEnv*, jobject) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -070046}
47
Elliott Hughes0512f022012-03-15 22:10:52 -070048static void VMRuntime_disableJitCompilation(JNIEnv*, jobject) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -070049}
50
Elliott Hughes0512f022012-03-15 22:10:52 -070051static jobject VMRuntime_newNonMovableArray(JNIEnv* env, jobject, jclass javaElementClass, jint length) {
Ian Rogers365c1022012-06-22 15:05:28 -070052 ScopedJniThreadState ts(env);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070053#ifdef MOVING_GARBAGE_COLLECTOR
54 // TODO: right now, we don't have a copying collector, so there's no need
55 // to do anything special here, but we ought to pass the non-movability
56 // through to the allocator.
57 UNIMPLEMENTED(FATAL);
58#endif
59
Ian Rogers365c1022012-06-22 15:05:28 -070060 Class* element_class = ts.Decode<Class*>(javaElementClass);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070061 if (element_class == NULL) {
62 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "element class == null");
63 return NULL;
64 }
65 if (length < 0) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070066 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", length);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070067 return NULL;
68 }
69
70 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
71 std::string descriptor;
72 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080073 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughesc3b77c72011-12-15 20:56:48 -080074 Class* array_class = class_linker->FindClass(descriptor.c_str(), NULL);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070075 Array* result = Array::Alloc(array_class, length);
76 if (result == NULL) {
77 return NULL;
78 }
Ian Rogers365c1022012-06-22 15:05:28 -070079 return ts.AddLocalReference<jobject>(result);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070080}
81
Elliott Hughes0512f022012-03-15 22:10:52 -070082static jlong VMRuntime_addressOf(JNIEnv* env, jobject, jobject javaArray) {
Ian Rogersa15e67d2012-02-28 13:51:55 -080083 if (javaArray == NULL) { // Most likely allocation failed
84 return 0;
85 }
Ian Rogers365c1022012-06-22 15:05:28 -070086 ScopedJniThreadState ts(env);
87 Array* array = ts.Decode<Array*>(javaArray);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070088 if (!array->IsArrayInstance()) {
89 Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;", "not an array");
90 return 0;
91 }
92 // TODO: we should also check that this is a non-movable array.
Ian Rogersa15e67d2012-02-28 13:51:55 -080093 return reinterpret_cast<uintptr_t>(array->GetRawData(array->GetClass()->GetComponentSize()));
Elliott Hughes7ede61e2011-09-14 18:18:06 -070094}
95
Elliott Hughes0512f022012-03-15 22:10:52 -070096static void VMRuntime_clearGrowthLimit(JNIEnv*, jobject) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080097 Runtime::Current()->GetHeap()->ClearGrowthLimit();
Elliott Hughes7ede61e2011-09-14 18:18:06 -070098}
99
Elliott Hughes0512f022012-03-15 22:10:52 -0700100static jboolean VMRuntime_isDebuggerActive(JNIEnv*, jobject) {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700101 return Dbg::IsDebuggerActive();
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700102}
103
Elliott Hughes0512f022012-03-15 22:10:52 -0700104static jobjectArray VMRuntime_properties(JNIEnv* env, jobject) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700105 return toStringArray(env, Runtime::Current()->GetProperties());
106}
107
Brian Carlstrom7d5ffb52012-02-01 14:27:54 -0800108// This is for backward compatibility with dalvik which returned the
109// meaningless "." when no boot classpath or classpath was
110// specified. Unfortunately, some tests were using java.class.path to
111// lookup relative file locations, so they are counting on this to be
112// ".", presumably some applications or libraries could have as well.
Elliott Hughes0512f022012-03-15 22:10:52 -0700113static const char* DefaultToDot(const std::string& class_path) {
Brian Carlstrom7d5ffb52012-02-01 14:27:54 -0800114 return class_path.empty() ? "." : class_path.c_str();
115}
116
Elliott Hughes0512f022012-03-15 22:10:52 -0700117static jstring VMRuntime_bootClassPath(JNIEnv* env, jobject) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800118 return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetBootClassPathString()));
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700119}
120
Elliott Hughes0512f022012-03-15 22:10:52 -0700121static jstring VMRuntime_classPath(JNIEnv* env, jobject) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800122 return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetClassPathString()));
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700123}
124
Elliott Hughes0512f022012-03-15 22:10:52 -0700125static jstring VMRuntime_vmVersion(JNIEnv* env, jobject) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700126 return env->NewStringUTF(Runtime::Current()->GetVersion());
127}
128
Shih-wei Liaofbe5b752012-04-19 00:26:00 -0700129#if !defined(ART_USE_LLVM_COMPILER)
Elliott Hughes88c5c352012-03-15 18:49:48 -0700130static void DisableCheckJniCallback(Thread* t, void*) {
Elliott Hughes88c5c352012-03-15 18:49:48 -0700131 t->GetJniEnv()->SetCheckJniEnabled(false);
132}
Shih-wei Liaofbe5b752012-04-19 00:26:00 -0700133#endif
Elliott Hughes88c5c352012-03-15 18:49:48 -0700134
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700135static void VMRuntime_setTargetSdkVersion(JNIEnv*, jobject, jint targetSdkVersion) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700136 // This is the target SDK version of the app we're about to run.
Elliott Hughesc2dc62d2012-01-17 20:06:12 -0800137 // Note that targetSdkVersion may be CUR_DEVELOPMENT (10000).
138 // Note that targetSdkVersion may be 0, meaning "current".
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700139 if (targetSdkVersion > 0 && targetSdkVersion <= 13 /* honeycomb-mr2 */) {
Elliott Hughes88c5c352012-03-15 18:49:48 -0700140 Runtime* runtime = Runtime::Current();
141 JavaVMExt* vm = runtime->GetJavaVM();
142
TDYa12726467572012-04-17 20:51:22 -0700143#if !defined(ART_USE_LLVM_COMPILER)
Elliott Hughes88c5c352012-03-15 18:49:48 -0700144 if (vm->check_jni) {
145 LOG(WARNING) << "Turning off CheckJNI so we can turn on JNI app bug workarounds...";
146 ScopedThreadListLock thread_list_lock;
147 vm->SetCheckJniEnabled(false);
148 runtime->GetThreadList()->ForEach(DisableCheckJniCallback, NULL);
Ian Rogers475a6442012-02-21 15:39:39 -0800149 }
Elliott Hughes88c5c352012-03-15 18:49:48 -0700150
151 LOG(INFO) << "Turning on JNI app bug workarounds for target SDK version "
152 << targetSdkVersion << "...";
TDYa12726467572012-04-17 20:51:22 -0700153
Elliott Hughes88c5c352012-03-15 18:49:48 -0700154 vm->work_around_app_jni_bugs = true;
TDYa12726467572012-04-17 20:51:22 -0700155#else
156 LOG(WARNING) << "LLVM does not work-around app jni bugs.";
157 vm->work_around_app_jni_bugs = false;
158#endif
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700159 }
160}
161
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700162static void VMRuntime_trimHeap(JNIEnv*, jobject) {
Elliott Hughes37be8fb2012-02-03 09:34:29 -0800163 ScopedHeapLock heap_lock;
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700164
165 // Trim the managed heap.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800166 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700167 const Spaces& spaces = heap->GetSpaces();
168 // TODO: C++0x auto
169 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
170 if ((*cur)->IsAllocSpace()) {
171 uint64_t start_ns = NanoTime();
172 AllocSpace* alloc_space = (*cur)->AsAllocSpace();
173 size_t alloc_space_size = alloc_space->Size();
174 float utilization = static_cast<float>(heap->GetBytesAllocated()) / alloc_space_size;
175 heap->Trim(alloc_space);
176 // Trim the native heap.
177 dlmalloc_trim(0);
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700178#if 0 // TODO: switch over to this when bionic has moved to dlmalloc 2.8.5
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700179 dlmalloc_inspect_all(MspaceMadviseCallback, NULL);
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700180#else
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700181 dlmalloc_walk_free_pages(MspaceMadviseCallback, NULL);
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700182#endif
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700183 LOG(INFO) << "Parallel heap trimming took " << PrettyDuration(NanoTime() - start_ns)
184 << " on a " << PrettySize(alloc_space_size)
185 << " alloc space with " << static_cast<int>(100 * utilization) << "% utilization";
186 }
187 }
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800188}
189
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700190static void VMRuntime_concurrentGC(JNIEnv*, jobject) {
191 Runtime::Current()->GetHeap()->ConcurrentGC();
192}
193
Elliott Hughes0512f022012-03-15 22:10:52 -0700194static JNINativeMethod gMethods[] = {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700195 NATIVE_METHOD(VMRuntime, addressOf, "(Ljava/lang/Object;)J"),
196 NATIVE_METHOD(VMRuntime, bootClassPath, "()Ljava/lang/String;"),
197 NATIVE_METHOD(VMRuntime, classPath, "()Ljava/lang/String;"),
198 NATIVE_METHOD(VMRuntime, clearGrowthLimit, "()V"),
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700199 NATIVE_METHOD(VMRuntime, concurrentGC, "()V"),
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700200 NATIVE_METHOD(VMRuntime, disableJitCompilation, "()V"),
201 NATIVE_METHOD(VMRuntime, getTargetHeapUtilization, "()F"),
202 NATIVE_METHOD(VMRuntime, isDebuggerActive, "()Z"),
203 NATIVE_METHOD(VMRuntime, nativeSetTargetHeapUtilization, "(F)V"),
204 NATIVE_METHOD(VMRuntime, newNonMovableArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
205 NATIVE_METHOD(VMRuntime, properties, "()[Ljava/lang/String;"),
206 NATIVE_METHOD(VMRuntime, setTargetSdkVersion, "(I)V"),
207 NATIVE_METHOD(VMRuntime, startJitCompilation, "()V"),
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800208 NATIVE_METHOD(VMRuntime, trimHeap, "()V"),
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700209 NATIVE_METHOD(VMRuntime, vmVersion, "()Ljava/lang/String;"),
210};
211
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700212void register_dalvik_system_VMRuntime(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700213 REGISTER_NATIVE_METHODS("dalvik/system/VMRuntime");
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700214}
215
216} // namespace art