blob: 9c400418db36ff3890adaa0fded70ccb546ce144 [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"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070024#include "scoped_thread_state_change.h"
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070025#include "gc/space.h"
Elliott Hughes7ede61e2011-09-14 18:18:06 -070026#include "thread.h"
Elliott Hughes88c5c352012-03-15 18:49:48 -070027#include "thread_list.h"
Elliott Hughes7ede61e2011-09-14 18:18:06 -070028#include "toStringArray.h"
29
Elliott Hughes7ede61e2011-09-14 18:18:06 -070030namespace art {
31
Elliott Hughes0512f022012-03-15 22:10:52 -070032static jfloat VMRuntime_getTargetHeapUtilization(JNIEnv*, jobject) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080033 return Runtime::Current()->GetHeap()->GetTargetHeapUtilization();
Elliott Hughes7ede61e2011-09-14 18:18:06 -070034}
35
Elliott Hughes0512f022012-03-15 22:10:52 -070036static void VMRuntime_nativeSetTargetHeapUtilization(JNIEnv*, jobject, jfloat target) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080037 Runtime::Current()->GetHeap()->SetTargetHeapUtilization(target);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070038}
39
Elliott Hughes0512f022012-03-15 22:10:52 -070040static void VMRuntime_startJitCompilation(JNIEnv*, jobject) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -070041}
42
Elliott Hughes0512f022012-03-15 22:10:52 -070043static void VMRuntime_disableJitCompilation(JNIEnv*, jobject) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -070044}
45
Elliott Hughes0512f022012-03-15 22:10:52 -070046static jobject VMRuntime_newNonMovableArray(JNIEnv* env, jobject, jclass javaElementClass, jint length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070047 ScopedObjectAccess soa(env);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070048#ifdef MOVING_GARBAGE_COLLECTOR
49 // TODO: right now, we don't have a copying collector, so there's no need
50 // to do anything special here, but we ought to pass the non-movability
51 // through to the allocator.
52 UNIMPLEMENTED(FATAL);
53#endif
54
Ian Rogers00f7d0e2012-07-19 15:28:27 -070055 Class* element_class = soa.Decode<Class*>(javaElementClass);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070056 if (element_class == NULL) {
Ian Rogers50b35e22012-10-04 10:09:15 -070057 soa.Self()->ThrowNewException("Ljava/lang/NullPointerException;", "element class == null");
Elliott Hughes7ede61e2011-09-14 18:18:06 -070058 return NULL;
59 }
60 if (length < 0) {
Ian Rogers50b35e22012-10-04 10:09:15 -070061 soa.Self()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", length);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070062 return NULL;
63 }
64
65 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
66 std::string descriptor;
67 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080068 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughesc3b77c72011-12-15 20:56:48 -080069 Class* array_class = class_linker->FindClass(descriptor.c_str(), NULL);
Ian Rogers50b35e22012-10-04 10:09:15 -070070 Array* result = Array::Alloc(soa.Self(), array_class, length);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070071 if (result == NULL) {
72 return NULL;
73 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070074 return soa.AddLocalReference<jobject>(result);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070075}
76
Elliott Hughes0512f022012-03-15 22:10:52 -070077static jlong VMRuntime_addressOf(JNIEnv* env, jobject, jobject javaArray) {
Ian Rogersa15e67d2012-02-28 13:51:55 -080078 if (javaArray == NULL) { // Most likely allocation failed
79 return 0;
80 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070081 ScopedObjectAccess soa(env);
82 Array* array = soa.Decode<Array*>(javaArray);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070083 if (!array->IsArrayInstance()) {
Ian Rogers50b35e22012-10-04 10:09:15 -070084 soa.Self()->ThrowNewException("Ljava/lang/IllegalArgumentException;", "not an array");
Elliott Hughes7ede61e2011-09-14 18:18:06 -070085 return 0;
86 }
87 // TODO: we should also check that this is a non-movable array.
Ian Rogersa15e67d2012-02-28 13:51:55 -080088 return reinterpret_cast<uintptr_t>(array->GetRawData(array->GetClass()->GetComponentSize()));
Elliott Hughes7ede61e2011-09-14 18:18:06 -070089}
90
Elliott Hughes0512f022012-03-15 22:10:52 -070091static void VMRuntime_clearGrowthLimit(JNIEnv*, jobject) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080092 Runtime::Current()->GetHeap()->ClearGrowthLimit();
Elliott Hughes7ede61e2011-09-14 18:18:06 -070093}
94
Elliott Hughes0512f022012-03-15 22:10:52 -070095static jboolean VMRuntime_isDebuggerActive(JNIEnv*, jobject) {
Elliott Hughesc0f09332012-03-26 13:27:06 -070096 return Dbg::IsDebuggerActive();
Elliott Hughes7ede61e2011-09-14 18:18:06 -070097}
98
Elliott Hughes0512f022012-03-15 22:10:52 -070099static jobjectArray VMRuntime_properties(JNIEnv* env, jobject) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700100 return toStringArray(env, Runtime::Current()->GetProperties());
101}
102
Brian Carlstrom7d5ffb52012-02-01 14:27:54 -0800103// This is for backward compatibility with dalvik which returned the
104// meaningless "." when no boot classpath or classpath was
105// specified. Unfortunately, some tests were using java.class.path to
106// lookup relative file locations, so they are counting on this to be
107// ".", presumably some applications or libraries could have as well.
Elliott Hughes0512f022012-03-15 22:10:52 -0700108static const char* DefaultToDot(const std::string& class_path) {
Brian Carlstrom7d5ffb52012-02-01 14:27:54 -0800109 return class_path.empty() ? "." : class_path.c_str();
110}
111
Elliott Hughes0512f022012-03-15 22:10:52 -0700112static jstring VMRuntime_bootClassPath(JNIEnv* env, jobject) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800113 return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetBootClassPathString()));
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700114}
115
Elliott Hughes0512f022012-03-15 22:10:52 -0700116static jstring VMRuntime_classPath(JNIEnv* env, jobject) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800117 return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetClassPathString()));
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700118}
119
Elliott Hughes0512f022012-03-15 22:10:52 -0700120static jstring VMRuntime_vmVersion(JNIEnv* env, jobject) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700121 return env->NewStringUTF(Runtime::Current()->GetVersion());
122}
123
Shih-wei Liaofbe5b752012-04-19 00:26:00 -0700124#if !defined(ART_USE_LLVM_COMPILER)
Elliott Hughes88c5c352012-03-15 18:49:48 -0700125static void DisableCheckJniCallback(Thread* t, void*) {
Elliott Hughes88c5c352012-03-15 18:49:48 -0700126 t->GetJniEnv()->SetCheckJniEnabled(false);
127}
Shih-wei Liaofbe5b752012-04-19 00:26:00 -0700128#endif
Elliott Hughes88c5c352012-03-15 18:49:48 -0700129
Ian Rogers50b35e22012-10-04 10:09:15 -0700130static void VMRuntime_setTargetSdkVersion(JNIEnv* env, jobject, jint targetSdkVersion) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700131 // This is the target SDK version of the app we're about to run.
Elliott Hughesc2dc62d2012-01-17 20:06:12 -0800132 // Note that targetSdkVersion may be CUR_DEVELOPMENT (10000).
133 // Note that targetSdkVersion may be 0, meaning "current".
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700134 if (targetSdkVersion > 0 && targetSdkVersion <= 13 /* honeycomb-mr2 */) {
Elliott Hughes88c5c352012-03-15 18:49:48 -0700135 Runtime* runtime = Runtime::Current();
136 JavaVMExt* vm = runtime->GetJavaVM();
137
TDYa12726467572012-04-17 20:51:22 -0700138#if !defined(ART_USE_LLVM_COMPILER)
Elliott Hughes88c5c352012-03-15 18:49:48 -0700139 if (vm->check_jni) {
140 LOG(WARNING) << "Turning off CheckJNI so we can turn on JNI app bug workarounds...";
Ian Rogers50b35e22012-10-04 10:09:15 -0700141 Thread* self = static_cast<JNIEnvExt*>(env)->self;
142 MutexLock mu(self, *Locks::thread_list_lock_);
Elliott Hughes88c5c352012-03-15 18:49:48 -0700143 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 << "...";
TDYa12726467572012-04-17 20:51:22 -0700149
Elliott Hughes88c5c352012-03-15 18:49:48 -0700150 vm->work_around_app_jni_bugs = true;
TDYa12726467572012-04-17 20:51:22 -0700151#else
Ian Rogers50b35e22012-10-04 10:09:15 -0700152 UNUSED(env);
TDYa12726467572012-04-17 20:51:22 -0700153 LOG(WARNING) << "LLVM does not work-around app jni bugs.";
154 vm->work_around_app_jni_bugs = false;
155#endif
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700156 }
157}
158
Mathieu Chartier3056d0c2012-10-19 10:49:56 -0700159static void VMRuntime_trimHeap(JNIEnv*, jobject) {
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700160 // Trim the managed heap.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800161 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700162 uint64_t start_ns = NanoTime();
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700163 DlMallocSpace* alloc_space = heap->GetAllocSpace();
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700164 size_t alloc_space_size = alloc_space->Size();
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700165 float utilization = static_cast<float>(alloc_space->GetNumBytesAllocated()) / alloc_space_size;
Mathieu Chartier3056d0c2012-10-19 10:49:56 -0700166 heap->Trim();
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700167 // Trim the native heap.
168 dlmalloc_trim(0);
169 dlmalloc_inspect_all(MspaceMadviseCallback, NULL);
170 LOG(INFO) << "Parallel heap trimming took " << PrettyDuration(NanoTime() - start_ns)
171 << " on a " << PrettySize(alloc_space_size)
172 << " alloc space with " << static_cast<int>(100 * utilization) << "% utilization";
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800173}
174
Ian Rogers81d425b2012-09-27 16:03:43 -0700175static void VMRuntime_concurrentGC(JNIEnv* env, jobject) {
176 Thread* self = static_cast<JNIEnvExt*>(env)->self;
177 Runtime::Current()->GetHeap()->ConcurrentGC(self);
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700178}
179
Elliott Hughes0512f022012-03-15 22:10:52 -0700180static JNINativeMethod gMethods[] = {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700181 NATIVE_METHOD(VMRuntime, addressOf, "(Ljava/lang/Object;)J"),
182 NATIVE_METHOD(VMRuntime, bootClassPath, "()Ljava/lang/String;"),
183 NATIVE_METHOD(VMRuntime, classPath, "()Ljava/lang/String;"),
184 NATIVE_METHOD(VMRuntime, clearGrowthLimit, "()V"),
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700185 NATIVE_METHOD(VMRuntime, concurrentGC, "()V"),
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700186 NATIVE_METHOD(VMRuntime, disableJitCompilation, "()V"),
187 NATIVE_METHOD(VMRuntime, getTargetHeapUtilization, "()F"),
188 NATIVE_METHOD(VMRuntime, isDebuggerActive, "()Z"),
189 NATIVE_METHOD(VMRuntime, nativeSetTargetHeapUtilization, "(F)V"),
190 NATIVE_METHOD(VMRuntime, newNonMovableArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
191 NATIVE_METHOD(VMRuntime, properties, "()[Ljava/lang/String;"),
192 NATIVE_METHOD(VMRuntime, setTargetSdkVersion, "(I)V"),
193 NATIVE_METHOD(VMRuntime, startJitCompilation, "()V"),
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800194 NATIVE_METHOD(VMRuntime, trimHeap, "()V"),
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700195 NATIVE_METHOD(VMRuntime, vmVersion, "()Ljava/lang/String;"),
196};
197
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700198void register_dalvik_system_VMRuntime(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700199 REGISTER_NATIVE_METHODS("dalvik/system/VMRuntime");
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700200}
201
202} // namespace art