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" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 20 | #include "common_throws.h" |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 21 | #include "debugger.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 22 | #include "dex_file-inl.h" |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 23 | #include "gc/accounting/card_table-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 24 | #include "gc/allocator/dlmalloc.h" |
Mathieu Chartier | 8235331 | 2013-07-18 10:47:51 -0700 | [diff] [blame] | 25 | #include "gc/heap.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 26 | #include "gc/space/dlmalloc_space.h" |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 27 | #include "intern_table.h" |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 28 | #include "jni_internal.h" |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 29 | #include "mirror/art_method-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 30 | #include "mirror/class-inl.h" |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 31 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 32 | #include "mirror/object-inl.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 33 | #include "object_utils.h" |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 34 | #include "scoped_fast_native_object_access.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 35 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 36 | #include "thread.h" |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 37 | #include "thread_list.h" |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 38 | #include "toStringArray.h" |
| 39 | |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 40 | namespace art { |
| 41 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 42 | static jfloat VMRuntime_getTargetHeapUtilization(JNIEnv*, jobject) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 43 | return Runtime::Current()->GetHeap()->GetTargetHeapUtilization(); |
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_nativeSetTargetHeapUtilization(JNIEnv*, jobject, jfloat target) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 47 | Runtime::Current()->GetHeap()->SetTargetHeapUtilization(target); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 50 | static void VMRuntime_startJitCompilation(JNIEnv*, jobject) { |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 53 | static void VMRuntime_disableJitCompilation(JNIEnv*, jobject) { |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 56 | static jobject VMRuntime_newNonMovableArray(JNIEnv* env, jobject, jclass javaElementClass, |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 57 | jint length) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 58 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 59 | mirror::Class* element_class = soa.Decode<mirror::Class*>(javaElementClass); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 60 | if (element_class == NULL) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 61 | ThrowNullPointerException(NULL, "element class == null"); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 62 | return NULL; |
| 63 | } |
| 64 | if (length < 0) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 65 | ThrowNegativeArraySizeException(length); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 66 | return NULL; |
| 67 | } |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 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(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 72 | SirtRef<mirror::ClassLoader> class_loader(soa.Self(), nullptr); |
| 73 | mirror::Class* array_class = class_linker->FindClass(descriptor.c_str(), class_loader); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 74 | mirror::Array* result = mirror::Array::Alloc<true>(soa.Self(), array_class, length, |
| 75 | Runtime::Current()->GetHeap()->GetCurrentNonMovingAllocator()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 76 | return soa.AddLocalReference<jobject>(result); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 79 | static jlong VMRuntime_addressOf(JNIEnv* env, jobject, jobject javaArray) { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 80 | if (javaArray == NULL) { // Most likely allocation failed |
| 81 | return 0; |
| 82 | } |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 83 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 84 | mirror::Array* array = soa.Decode<mirror::Array*>(javaArray); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 85 | if (!array->IsArrayInstance()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 86 | ThrowIllegalArgumentException(NULL, "not an array"); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 87 | return 0; |
| 88 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 89 | if (Runtime::Current()->GetHeap()->IsMovableObject(array)) { |
| 90 | ThrowRuntimeException("Trying to get address of movable array object"); |
| 91 | return 0; |
| 92 | } |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 93 | return reinterpret_cast<uintptr_t>(array->GetRawData(array->GetClass()->GetComponentSize())); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 96 | static void VMRuntime_clearGrowthLimit(JNIEnv*, jobject) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 97 | Runtime::Current()->GetHeap()->ClearGrowthLimit(); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 100 | static jboolean VMRuntime_isDebuggerActive(JNIEnv*, jobject) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 101 | return Dbg::IsDebuggerActive(); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 104 | static jobjectArray VMRuntime_properties(JNIEnv* env, jobject) { |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 105 | return toStringArray(env, Runtime::Current()->GetProperties()); |
| 106 | } |
| 107 | |
Brian Carlstrom | 7d5ffb5 | 2012-02-01 14:27:54 -0800 | [diff] [blame] | 108 | // 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 Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 113 | static const char* DefaultToDot(const std::string& class_path) { |
Brian Carlstrom | 7d5ffb5 | 2012-02-01 14:27:54 -0800 | [diff] [blame] | 114 | return class_path.empty() ? "." : class_path.c_str(); |
| 115 | } |
| 116 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 117 | static jstring VMRuntime_bootClassPath(JNIEnv* env, jobject) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 118 | return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetBootClassPathString())); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 121 | static jstring VMRuntime_classPath(JNIEnv* env, jobject) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 122 | return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetClassPathString())); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 125 | static jstring VMRuntime_vmVersion(JNIEnv* env, jobject) { |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 126 | return env->NewStringUTF(Runtime::Current()->GetVersion()); |
| 127 | } |
| 128 | |
Brian Carlstrom | cef450c | 2013-06-28 14:38:26 -0700 | [diff] [blame] | 129 | static jstring VMRuntime_vmLibrary(JNIEnv* env, jobject) { |
| 130 | return env->NewStringUTF(kIsDebugBuild ? "libartd.so" : "libart.so"); |
| 131 | } |
| 132 | |
Alex Klyubin | 881392f | 2013-10-24 16:31:56 -0700 | [diff] [blame] | 133 | static void VMRuntime_setTargetSdkVersionNative(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(); |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 140 | if (vm->check_jni) { |
Ian Rogers | 1a4d6d8 | 2013-08-13 20:07:05 -0700 | [diff] [blame] | 141 | LOG(INFO) << "CheckJNI enabled: not enabling JNI app bug workarounds."; |
| 142 | } else { |
| 143 | LOG(INFO) << "Turning on JNI app bug workarounds for target SDK version " |
| 144 | << targetSdkVersion << "..."; |
| 145 | |
| 146 | vm->work_around_app_jni_bugs = true; |
Ian Rogers | 475a644 | 2012-02-21 15:39:39 -0800 | [diff] [blame] | 147 | } |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 151 | static void VMRuntime_registerNativeAllocation(JNIEnv* env, jobject, jint bytes) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 152 | if (UNLIKELY(bytes < 0)) { |
| 153 | ScopedObjectAccess soa(env); |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 154 | ThrowRuntimeException("allocation size negative %d", bytes); |
| 155 | return; |
| 156 | } |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 157 | Runtime::Current()->GetHeap()->RegisterNativeAllocation(env, bytes); |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static void VMRuntime_registerNativeFree(JNIEnv* env, jobject, jint bytes) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 161 | if (UNLIKELY(bytes < 0)) { |
| 162 | ScopedObjectAccess soa(env); |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 163 | ThrowRuntimeException("allocation size negative %d", bytes); |
| 164 | return; |
| 165 | } |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 166 | Runtime::Current()->GetHeap()->RegisterNativeFree(env, bytes); |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Mathieu Chartier | 3056d0c | 2012-10-19 10:49:56 -0700 | [diff] [blame] | 169 | static void VMRuntime_trimHeap(JNIEnv*, jobject) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 170 | Runtime::Current()->GetHeap()->Trim(); |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 173 | static void VMRuntime_concurrentGC(JNIEnv* env, jobject) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 174 | Thread* self = ThreadForEnv(env); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 175 | Runtime::Current()->GetHeap()->ConcurrentGC(self); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 178 | typedef std::map<std::string, mirror::String*> StringTable; |
| 179 | |
Brian Carlstrom | 57e6d8a | 2013-10-20 22:53:26 -0700 | [diff] [blame] | 180 | static mirror::Object* PreloadDexCachesStringsVisitor(mirror::Object* root, void* arg) { |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 181 | StringTable& table = *reinterpret_cast<StringTable*>(arg); |
| 182 | mirror::String* string = const_cast<mirror::Object*>(root)->AsString(); |
| 183 | // LOG(INFO) << "VMRuntime.preloadDexCaches interned=" << string->ToModifiedUtf8(); |
| 184 | table[string->ToModifiedUtf8()] = string; |
Brian Carlstrom | 57e6d8a | 2013-10-20 22:53:26 -0700 | [diff] [blame] | 185 | return root; |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // Based on ClassLinker::ResolveString. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 189 | static void PreloadDexCachesResolveString(SirtRef<mirror::DexCache>& dex_cache, |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 190 | uint32_t string_idx, |
| 191 | StringTable& strings) |
| 192 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 193 | mirror::String* string = dex_cache->GetResolvedString(string_idx); |
| 194 | if (string != NULL) { |
| 195 | return; |
| 196 | } |
| 197 | const DexFile* dex_file = dex_cache->GetDexFile(); |
Ian Rogers | cf5077a | 2013-10-31 12:37:54 -0700 | [diff] [blame] | 198 | const char* utf8 = dex_file->StringDataByIdx(string_idx); |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 199 | string = strings[utf8]; |
| 200 | if (string == NULL) { |
| 201 | return; |
| 202 | } |
| 203 | // LOG(INFO) << "VMRuntime.preloadDexCaches resolved string=" << utf8; |
| 204 | dex_cache->SetResolvedString(string_idx, string); |
| 205 | } |
| 206 | |
| 207 | // Based on ClassLinker::ResolveType. |
| 208 | static void PreloadDexCachesResolveType(mirror::DexCache* dex_cache, uint32_t type_idx) |
| 209 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 210 | mirror::Class* klass = dex_cache->GetResolvedType(type_idx); |
| 211 | if (klass != NULL) { |
| 212 | return; |
| 213 | } |
| 214 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 215 | const char* class_name = dex_file->StringByTypeIdx(type_idx); |
| 216 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 217 | if (class_name[1] == '\0') { |
| 218 | klass = linker->FindPrimitiveClass(class_name[0]); |
| 219 | } else { |
| 220 | klass = linker->LookupClass(class_name, NULL); |
| 221 | } |
| 222 | if (klass == NULL) { |
| 223 | return; |
| 224 | } |
| 225 | // LOG(INFO) << "VMRuntime.preloadDexCaches resolved klass=" << class_name; |
| 226 | dex_cache->SetResolvedType(type_idx, klass); |
| 227 | // Skip uninitialized classes because filled static storage entry implies it is initialized. |
| 228 | if (!klass->IsInitialized()) { |
| 229 | // LOG(INFO) << "VMRuntime.preloadDexCaches uninitialized klass=" << class_name; |
| 230 | return; |
| 231 | } |
| 232 | // LOG(INFO) << "VMRuntime.preloadDexCaches static storage klass=" << class_name; |
| 233 | dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass); |
| 234 | } |
| 235 | |
| 236 | // Based on ClassLinker::ResolveField. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 237 | static void PreloadDexCachesResolveField(SirtRef<mirror::DexCache>& dex_cache, |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 238 | uint32_t field_idx, |
| 239 | bool is_static) |
| 240 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 241 | mirror::ArtField* field = dex_cache->GetResolvedField(field_idx); |
| 242 | if (field != NULL) { |
| 243 | return; |
| 244 | } |
| 245 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 246 | const DexFile::FieldId& field_id = dex_file->GetFieldId(field_idx); |
| 247 | mirror::Class* klass = dex_cache->GetResolvedType(field_id.class_idx_); |
| 248 | if (klass == NULL) { |
| 249 | return; |
| 250 | } |
| 251 | if (is_static) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 252 | field = klass->FindStaticField(dex_cache.get(), field_idx); |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 253 | } else { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 254 | field = klass->FindInstanceField(dex_cache.get(), field_idx); |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 255 | } |
| 256 | if (field == NULL) { |
| 257 | return; |
| 258 | } |
| 259 | // LOG(INFO) << "VMRuntime.preloadDexCaches resolved field " << PrettyField(field); |
| 260 | dex_cache->SetResolvedField(field_idx, field); |
| 261 | } |
| 262 | |
| 263 | // Based on ClassLinker::ResolveMethod. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 264 | static void PreloadDexCachesResolveMethod(SirtRef<mirror::DexCache>& dex_cache, |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 265 | uint32_t method_idx, |
| 266 | InvokeType invoke_type) |
| 267 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 268 | mirror::ArtMethod* method = dex_cache->GetResolvedMethod(method_idx); |
| 269 | if (method != NULL) { |
| 270 | return; |
| 271 | } |
| 272 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 273 | const DexFile::MethodId& method_id = dex_file->GetMethodId(method_idx); |
| 274 | mirror::Class* klass = dex_cache->GetResolvedType(method_id.class_idx_); |
| 275 | if (klass == NULL) { |
| 276 | return; |
| 277 | } |
| 278 | switch (invoke_type) { |
| 279 | case kDirect: |
| 280 | case kStatic: |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 281 | method = klass->FindDirectMethod(dex_cache.get(), method_idx); |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 282 | break; |
| 283 | case kInterface: |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 284 | method = klass->FindInterfaceMethod(dex_cache.get(), method_idx); |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 285 | break; |
| 286 | case kSuper: |
| 287 | case kVirtual: |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 288 | method = klass->FindVirtualMethod(dex_cache.get(), method_idx); |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 289 | break; |
| 290 | default: |
| 291 | LOG(FATAL) << "Unreachable - invocation type: " << invoke_type; |
| 292 | } |
| 293 | if (method == NULL) { |
| 294 | return; |
| 295 | } |
| 296 | // LOG(INFO) << "VMRuntime.preloadDexCaches resolved method " << PrettyMethod(method); |
| 297 | dex_cache->SetResolvedMethod(method_idx, method); |
| 298 | } |
| 299 | |
| 300 | struct DexCacheStats { |
| 301 | uint32_t num_strings; |
| 302 | uint32_t num_types; |
| 303 | uint32_t num_fields; |
| 304 | uint32_t num_methods; |
| 305 | uint32_t num_static_storage; |
| 306 | DexCacheStats() : num_strings(0), |
| 307 | num_types(0), |
| 308 | num_fields(0), |
| 309 | num_methods(0), |
| 310 | num_static_storage(0) {} |
| 311 | }; |
| 312 | |
| 313 | static const bool kPreloadDexCachesEnabled = true; |
| 314 | |
| 315 | // Disabled because it takes a long time (extra half second) but |
| 316 | // gives almost no benefit in terms of saving private dirty pages. |
| 317 | static const bool kPreloadDexCachesStrings = false; |
| 318 | |
| 319 | static const bool kPreloadDexCachesTypes = true; |
| 320 | static const bool kPreloadDexCachesFieldsAndMethods = true; |
| 321 | |
| 322 | static const bool kPreloadDexCachesCollectStats = true; |
| 323 | |
| 324 | static void PreloadDexCachesStatsTotal(DexCacheStats* total) { |
| 325 | if (!kPreloadDexCachesCollectStats) { |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 330 | const std::vector<const DexFile*>& boot_class_path = linker->GetBootClassPath(); |
| 331 | for (size_t i = 0; i< boot_class_path.size(); i++) { |
| 332 | const DexFile* dex_file = boot_class_path[i]; |
| 333 | CHECK(dex_file != NULL); |
| 334 | total->num_strings += dex_file->NumStringIds(); |
| 335 | total->num_fields += dex_file->NumFieldIds(); |
| 336 | total->num_methods += dex_file->NumMethodIds(); |
| 337 | total->num_types += dex_file->NumTypeIds(); |
| 338 | total->num_static_storage += dex_file->NumTypeIds(); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | static void PreloadDexCachesStatsFilled(DexCacheStats* filled) |
| 343 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 344 | if (!kPreloadDexCachesCollectStats) { |
| 345 | return; |
| 346 | } |
| 347 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 348 | const std::vector<const DexFile*>& boot_class_path = linker->GetBootClassPath(); |
| 349 | for (size_t i = 0; i< boot_class_path.size(); i++) { |
| 350 | const DexFile* dex_file = boot_class_path[i]; |
| 351 | CHECK(dex_file != NULL); |
| 352 | mirror::DexCache* dex_cache = linker->FindDexCache(*dex_file); |
| 353 | for (size_t i = 0; i < dex_cache->NumStrings(); i++) { |
| 354 | mirror::String* string = dex_cache->GetResolvedString(i); |
| 355 | if (string != NULL) { |
| 356 | filled->num_strings++; |
| 357 | } |
| 358 | } |
| 359 | for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) { |
| 360 | mirror::Class* klass = dex_cache->GetResolvedType(i); |
| 361 | if (klass != NULL) { |
| 362 | filled->num_types++; |
| 363 | } |
| 364 | } |
| 365 | for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) { |
| 366 | mirror::ArtField* field = dex_cache->GetResolvedField(i); |
| 367 | if (field != NULL) { |
| 368 | filled->num_fields++; |
| 369 | } |
| 370 | } |
| 371 | for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) { |
| 372 | mirror::ArtMethod* method = dex_cache->GetResolvedMethod(i); |
| 373 | if (method != NULL) { |
| 374 | filled->num_methods++; |
| 375 | } |
| 376 | } |
| 377 | for (size_t i = 0; i < dex_cache->NumInitializedStaticStorage(); i++) { |
| 378 | mirror::StaticStorageBase* klass = dex_cache->GetInitializedStaticStorage()->Get(i); |
| 379 | if (klass != NULL) { |
| 380 | filled->num_static_storage++; |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | // TODO: http://b/11309598 This code was ported over based on the |
| 387 | // Dalvik version. However, ART has similar code in other places such |
| 388 | // as the CompilerDriver. This code could probably be refactored to |
| 389 | // serve both uses. |
| 390 | static void VMRuntime_preloadDexCaches(JNIEnv* env, jobject) { |
| 391 | if (!kPreloadDexCachesEnabled) { |
| 392 | return; |
| 393 | } |
| 394 | |
| 395 | ScopedObjectAccess soa(env); |
| 396 | |
| 397 | DexCacheStats total; |
| 398 | DexCacheStats before; |
| 399 | if (kPreloadDexCachesCollectStats) { |
| 400 | LOG(INFO) << "VMRuntime.preloadDexCaches starting"; |
| 401 | PreloadDexCachesStatsTotal(&total); |
| 402 | PreloadDexCachesStatsFilled(&before); |
| 403 | } |
| 404 | |
| 405 | Runtime* runtime = Runtime::Current(); |
| 406 | ClassLinker* linker = runtime->GetClassLinker(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 407 | Thread* self = ThreadForEnv(env); |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 408 | |
| 409 | // We use a std::map to avoid heap allocating StringObjects to lookup in gDvm.literalStrings |
| 410 | StringTable strings; |
| 411 | if (kPreloadDexCachesStrings) { |
| 412 | runtime->GetInternTable()->VisitRoots(PreloadDexCachesStringsVisitor, &strings, false, false); |
| 413 | } |
| 414 | |
| 415 | const std::vector<const DexFile*>& boot_class_path = linker->GetBootClassPath(); |
| 416 | for (size_t i = 0; i< boot_class_path.size(); i++) { |
| 417 | const DexFile* dex_file = boot_class_path[i]; |
| 418 | CHECK(dex_file != NULL); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 419 | SirtRef<mirror::DexCache> dex_cache(self, linker->FindDexCache(*dex_file)); |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 420 | |
| 421 | if (kPreloadDexCachesStrings) { |
| 422 | for (size_t i = 0; i < dex_cache->NumStrings(); i++) { |
| 423 | PreloadDexCachesResolveString(dex_cache, i, strings); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | if (kPreloadDexCachesTypes) { |
| 428 | for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 429 | PreloadDexCachesResolveType(dex_cache.get(), i); |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
| 433 | if (kPreloadDexCachesFieldsAndMethods) { |
| 434 | for (size_t class_def_index = 0; |
| 435 | class_def_index < dex_file->NumClassDefs(); |
| 436 | class_def_index++) { |
| 437 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
| 438 | const byte* class_data = dex_file->GetClassData(class_def); |
| 439 | if (class_data == NULL) { |
| 440 | continue; |
| 441 | } |
| 442 | ClassDataItemIterator it(*dex_file, class_data); |
| 443 | for (; it.HasNextStaticField(); it.Next()) { |
| 444 | uint32_t field_idx = it.GetMemberIndex(); |
| 445 | PreloadDexCachesResolveField(dex_cache, field_idx, true); |
| 446 | } |
| 447 | for (; it.HasNextInstanceField(); it.Next()) { |
| 448 | uint32_t field_idx = it.GetMemberIndex(); |
| 449 | PreloadDexCachesResolveField(dex_cache, field_idx, false); |
| 450 | } |
| 451 | for (; it.HasNextDirectMethod(); it.Next()) { |
| 452 | uint32_t method_idx = it.GetMemberIndex(); |
| 453 | InvokeType invoke_type = it.GetMethodInvokeType(class_def); |
| 454 | PreloadDexCachesResolveMethod(dex_cache, method_idx, invoke_type); |
| 455 | } |
| 456 | for (; it.HasNextVirtualMethod(); it.Next()) { |
| 457 | uint32_t method_idx = it.GetMemberIndex(); |
| 458 | InvokeType invoke_type = it.GetMethodInvokeType(class_def); |
| 459 | PreloadDexCachesResolveMethod(dex_cache, method_idx, invoke_type); |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | if (kPreloadDexCachesCollectStats) { |
| 466 | DexCacheStats after; |
| 467 | PreloadDexCachesStatsFilled(&after); |
| 468 | LOG(INFO) << StringPrintf("VMRuntime.preloadDexCaches strings total=%d before=%d after=%d", |
| 469 | total.num_strings, before.num_strings, after.num_strings); |
| 470 | LOG(INFO) << StringPrintf("VMRuntime.preloadDexCaches types total=%d before=%d after=%d", |
| 471 | total.num_types, before.num_types, after.num_types); |
| 472 | LOG(INFO) << StringPrintf("VMRuntime.preloadDexCaches fields total=%d before=%d after=%d", |
| 473 | total.num_fields, before.num_fields, after.num_fields); |
| 474 | LOG(INFO) << StringPrintf("VMRuntime.preloadDexCaches methods total=%d before=%d after=%d", |
| 475 | total.num_methods, before.num_methods, after.num_methods); |
| 476 | LOG(INFO) << StringPrintf("VMRuntime.preloadDexCaches storage total=%d before=%d after=%d", |
| 477 | total.num_static_storage, |
| 478 | before.num_static_storage, |
| 479 | after.num_static_storage); |
| 480 | LOG(INFO) << StringPrintf("VMRuntime.preloadDexCaches finished"); |
| 481 | } |
| 482 | } |
| 483 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 484 | static JNINativeMethod gMethods[] = { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 485 | NATIVE_METHOD(VMRuntime, addressOf, "!(Ljava/lang/Object;)J"), |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 486 | NATIVE_METHOD(VMRuntime, bootClassPath, "()Ljava/lang/String;"), |
| 487 | NATIVE_METHOD(VMRuntime, classPath, "()Ljava/lang/String;"), |
| 488 | NATIVE_METHOD(VMRuntime, clearGrowthLimit, "()V"), |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 489 | NATIVE_METHOD(VMRuntime, concurrentGC, "()V"), |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 490 | NATIVE_METHOD(VMRuntime, disableJitCompilation, "()V"), |
| 491 | NATIVE_METHOD(VMRuntime, getTargetHeapUtilization, "()F"), |
| 492 | NATIVE_METHOD(VMRuntime, isDebuggerActive, "()Z"), |
| 493 | NATIVE_METHOD(VMRuntime, nativeSetTargetHeapUtilization, "(F)V"), |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 494 | NATIVE_METHOD(VMRuntime, newNonMovableArray, "!(Ljava/lang/Class;I)Ljava/lang/Object;"), |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 495 | NATIVE_METHOD(VMRuntime, properties, "()[Ljava/lang/String;"), |
Alex Klyubin | 881392f | 2013-10-24 16:31:56 -0700 | [diff] [blame] | 496 | NATIVE_METHOD(VMRuntime, setTargetSdkVersionNative, "(I)V"), |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 497 | NATIVE_METHOD(VMRuntime, registerNativeAllocation, "(I)V"), |
| 498 | NATIVE_METHOD(VMRuntime, registerNativeFree, "(I)V"), |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 499 | NATIVE_METHOD(VMRuntime, startJitCompilation, "()V"), |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 500 | NATIVE_METHOD(VMRuntime, trimHeap, "()V"), |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 501 | NATIVE_METHOD(VMRuntime, vmVersion, "()Ljava/lang/String;"), |
Brian Carlstrom | cef450c | 2013-06-28 14:38:26 -0700 | [diff] [blame] | 502 | NATIVE_METHOD(VMRuntime, vmLibrary, "()Ljava/lang/String;"), |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 503 | NATIVE_METHOD(VMRuntime, preloadDexCaches, "()V"), |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 504 | }; |
| 505 | |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 506 | void register_dalvik_system_VMRuntime(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 507 | REGISTER_NATIVE_METHODS("dalvik/system/VMRuntime"); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | } // namespace art |