Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 17 | #include <limits.h> |
| 18 | |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 19 | #include "class_linker.h" |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 20 | #include "debugger.h" |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 21 | #include "jni_internal.h" |
| 22 | #include "object.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 23 | #include "object_utils.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 24 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 25 | #include "gc/space.h" |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 26 | #include "thread.h" |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 27 | #include "thread_list.h" |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 28 | #include "toStringArray.h" |
| 29 | |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 30 | namespace art { |
| 31 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 32 | static jfloat VMRuntime_getTargetHeapUtilization(JNIEnv*, jobject) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 33 | return Runtime::Current()->GetHeap()->GetTargetHeapUtilization(); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 36 | static void VMRuntime_nativeSetTargetHeapUtilization(JNIEnv*, jobject, jfloat target) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 37 | Runtime::Current()->GetHeap()->SetTargetHeapUtilization(target); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 40 | static void VMRuntime_startJitCompilation(JNIEnv*, jobject) { |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 43 | static void VMRuntime_disableJitCompilation(JNIEnv*, jobject) { |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 46 | static jobject VMRuntime_newNonMovableArray(JNIEnv* env, jobject, jclass javaElementClass, jint length) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 47 | ScopedObjectAccess soa(env); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 48 | #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 Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 55 | Class* element_class = soa.Decode<Class*>(javaElementClass); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 56 | if (element_class == NULL) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 57 | soa.Self()->ThrowNewException("Ljava/lang/NullPointerException;", "element class == null"); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 58 | return NULL; |
| 59 | } |
| 60 | if (length < 0) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 61 | soa.Self()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", length); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 66 | std::string descriptor; |
| 67 | descriptor += "["; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 68 | descriptor += ClassHelper(element_class).GetDescriptor(); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 69 | Class* array_class = class_linker->FindClass(descriptor.c_str(), NULL); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 70 | Array* result = Array::Alloc(soa.Self(), array_class, length); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 71 | if (result == NULL) { |
| 72 | return NULL; |
| 73 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 74 | return soa.AddLocalReference<jobject>(result); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 77 | static jlong VMRuntime_addressOf(JNIEnv* env, jobject, jobject javaArray) { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 78 | if (javaArray == NULL) { // Most likely allocation failed |
| 79 | return 0; |
| 80 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 81 | ScopedObjectAccess soa(env); |
| 82 | Array* array = soa.Decode<Array*>(javaArray); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 83 | if (!array->IsArrayInstance()) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 84 | soa.Self()->ThrowNewException("Ljava/lang/IllegalArgumentException;", "not an array"); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 85 | return 0; |
| 86 | } |
| 87 | // TODO: we should also check that this is a non-movable array. |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 88 | return reinterpret_cast<uintptr_t>(array->GetRawData(array->GetClass()->GetComponentSize())); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 91 | static void VMRuntime_clearGrowthLimit(JNIEnv*, jobject) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 92 | Runtime::Current()->GetHeap()->ClearGrowthLimit(); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 95 | static jboolean VMRuntime_isDebuggerActive(JNIEnv*, jobject) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 96 | return Dbg::IsDebuggerActive(); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 99 | static jobjectArray VMRuntime_properties(JNIEnv* env, jobject) { |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 100 | return toStringArray(env, Runtime::Current()->GetProperties()); |
| 101 | } |
| 102 | |
Brian Carlstrom | 7d5ffb5 | 2012-02-01 14:27:54 -0800 | [diff] [blame] | 103 | // 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 Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 108 | static const char* DefaultToDot(const std::string& class_path) { |
Brian Carlstrom | 7d5ffb5 | 2012-02-01 14:27:54 -0800 | [diff] [blame] | 109 | return class_path.empty() ? "." : class_path.c_str(); |
| 110 | } |
| 111 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 112 | static jstring VMRuntime_bootClassPath(JNIEnv* env, jobject) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 113 | return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetBootClassPathString())); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 116 | static jstring VMRuntime_classPath(JNIEnv* env, jobject) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 117 | return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetClassPathString())); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 120 | static jstring VMRuntime_vmVersion(JNIEnv* env, jobject) { |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 121 | return env->NewStringUTF(Runtime::Current()->GetVersion()); |
| 122 | } |
| 123 | |
Shih-wei Liao | fbe5b75 | 2012-04-19 00:26:00 -0700 | [diff] [blame] | 124 | #if !defined(ART_USE_LLVM_COMPILER) |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 125 | static void DisableCheckJniCallback(Thread* t, void*) { |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 126 | t->GetJniEnv()->SetCheckJniEnabled(false); |
| 127 | } |
Shih-wei Liao | fbe5b75 | 2012-04-19 00:26:00 -0700 | [diff] [blame] | 128 | #endif |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 129 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 130 | static void VMRuntime_setTargetSdkVersion(JNIEnv* env, jobject, jint targetSdkVersion) { |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 131 | // This is the target SDK version of the app we're about to run. |
Elliott Hughes | c2dc62d | 2012-01-17 20:06:12 -0800 | [diff] [blame] | 132 | // Note that targetSdkVersion may be CUR_DEVELOPMENT (10000). |
| 133 | // Note that targetSdkVersion may be 0, meaning "current". |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 134 | if (targetSdkVersion > 0 && targetSdkVersion <= 13 /* honeycomb-mr2 */) { |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 135 | Runtime* runtime = Runtime::Current(); |
| 136 | JavaVMExt* vm = runtime->GetJavaVM(); |
| 137 | |
TDYa127 | 2646757 | 2012-04-17 20:51:22 -0700 | [diff] [blame] | 138 | #if !defined(ART_USE_LLVM_COMPILER) |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 139 | if (vm->check_jni) { |
| 140 | LOG(WARNING) << "Turning off CheckJNI so we can turn on JNI app bug workarounds..."; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 141 | Thread* self = static_cast<JNIEnvExt*>(env)->self; |
| 142 | MutexLock mu(self, *Locks::thread_list_lock_); |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 143 | vm->SetCheckJniEnabled(false); |
| 144 | runtime->GetThreadList()->ForEach(DisableCheckJniCallback, NULL); |
Ian Rogers | 475a644 | 2012-02-21 15:39:39 -0800 | [diff] [blame] | 145 | } |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 146 | |
| 147 | LOG(INFO) << "Turning on JNI app bug workarounds for target SDK version " |
| 148 | << targetSdkVersion << "..."; |
TDYa127 | 2646757 | 2012-04-17 20:51:22 -0700 | [diff] [blame] | 149 | |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 150 | vm->work_around_app_jni_bugs = true; |
TDYa127 | 2646757 | 2012-04-17 20:51:22 -0700 | [diff] [blame] | 151 | #else |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 152 | UNUSED(env); |
TDYa127 | 2646757 | 2012-04-17 20:51:22 -0700 | [diff] [blame] | 153 | LOG(WARNING) << "LLVM does not work-around app jni bugs."; |
| 154 | vm->work_around_app_jni_bugs = false; |
| 155 | #endif |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Mathieu Chartier | 3056d0c | 2012-10-19 10:49:56 -0700 | [diff] [blame^] | 159 | static void VMRuntime_trimHeap(JNIEnv*, jobject) { |
Elliott Hughes | 9eebd3b | 2012-06-08 13:56:31 -0700 | [diff] [blame] | 160 | // Trim the managed heap. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 161 | Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 162 | uint64_t start_ns = NanoTime(); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 163 | DlMallocSpace* alloc_space = heap->GetAllocSpace(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 164 | size_t alloc_space_size = alloc_space->Size(); |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 165 | float utilization = static_cast<float>(alloc_space->GetNumBytesAllocated()) / alloc_space_size; |
Mathieu Chartier | 3056d0c | 2012-10-19 10:49:56 -0700 | [diff] [blame^] | 166 | heap->Trim(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 167 | // 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 Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 175 | static void VMRuntime_concurrentGC(JNIEnv* env, jobject) { |
| 176 | Thread* self = static_cast<JNIEnvExt*>(env)->self; |
| 177 | Runtime::Current()->GetHeap()->ConcurrentGC(self); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 180 | static JNINativeMethod gMethods[] = { |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 181 | 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 Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 185 | NATIVE_METHOD(VMRuntime, concurrentGC, "()V"), |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 186 | 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 Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 194 | NATIVE_METHOD(VMRuntime, trimHeap, "()V"), |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 195 | NATIVE_METHOD(VMRuntime, vmVersion, "()Ljava/lang/String;"), |
| 196 | }; |
| 197 | |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 198 | void register_dalvik_system_VMRuntime(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 199 | REGISTER_NATIVE_METHODS("dalvik/system/VMRuntime"); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | } // namespace art |