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