• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include "dalvik_system_VMRuntime.h"
18 
19 #ifdef ART_TARGET_ANDROID
20 #include <sys/time.h>
21 #include <sys/resource.h>
22 extern "C" void android_set_application_target_sdk_version(uint32_t version);
23 #endif
24 #include <limits.h>
25 #include "nativehelper/ScopedUtfChars.h"
26 
27 #pragma GCC diagnostic push
28 #pragma GCC diagnostic ignored "-Wshadow"
29 #include "nativehelper/toStringArray.h"
30 #pragma GCC diagnostic pop
31 
32 #include "android-base/stringprintf.h"
33 
34 #include "art_method-inl.h"
35 #include "arch/instruction_set.h"
36 #include "base/enums.h"
37 #include "class_linker-inl.h"
38 #include "common_throws.h"
39 #include "debugger.h"
40 #include "dex_file-inl.h"
41 #include "dex_file_types.h"
42 #include "gc/accounting/card_table-inl.h"
43 #include "gc/allocator/dlmalloc.h"
44 #include "gc/heap.h"
45 #include "gc/space/dlmalloc_space.h"
46 #include "gc/space/image_space.h"
47 #include "gc/task_processor.h"
48 #include "intern_table.h"
49 #include "java_vm_ext.h"
50 #include "jni_internal.h"
51 #include "mirror/class-inl.h"
52 #include "mirror/dex_cache-inl.h"
53 #include "mirror/object-inl.h"
54 #include "native_util.h"
55 #include "nativehelper/jni_macros.h"
56 #include "runtime.h"
57 #include "scoped_fast_native_object_access-inl.h"
58 #include "scoped_thread_state_change-inl.h"
59 #include "thread.h"
60 #include "thread_list.h"
61 
62 namespace art {
63 
64 using android::base::StringPrintf;
65 
VMRuntime_getTargetHeapUtilization(JNIEnv *,jobject)66 static jfloat VMRuntime_getTargetHeapUtilization(JNIEnv*, jobject) {
67   return Runtime::Current()->GetHeap()->GetTargetHeapUtilization();
68 }
69 
VMRuntime_nativeSetTargetHeapUtilization(JNIEnv *,jobject,jfloat target)70 static void VMRuntime_nativeSetTargetHeapUtilization(JNIEnv*, jobject, jfloat target) {
71   Runtime::Current()->GetHeap()->SetTargetHeapUtilization(target);
72 }
73 
VMRuntime_startJitCompilation(JNIEnv *,jobject)74 static void VMRuntime_startJitCompilation(JNIEnv*, jobject) {
75 }
76 
VMRuntime_disableJitCompilation(JNIEnv *,jobject)77 static void VMRuntime_disableJitCompilation(JNIEnv*, jobject) {
78 }
79 
VMRuntime_newNonMovableArray(JNIEnv * env,jobject,jclass javaElementClass,jint length)80 static jobject VMRuntime_newNonMovableArray(JNIEnv* env, jobject, jclass javaElementClass,
81                                             jint length) {
82   ScopedFastNativeObjectAccess soa(env);
83   if (UNLIKELY(length < 0)) {
84     ThrowNegativeArraySizeException(length);
85     return nullptr;
86   }
87   ObjPtr<mirror::Class> element_class = soa.Decode<mirror::Class>(javaElementClass);
88   if (UNLIKELY(element_class == nullptr)) {
89     ThrowNullPointerException("element class == null");
90     return nullptr;
91   }
92   Runtime* runtime = Runtime::Current();
93   ObjPtr<mirror::Class> array_class =
94       runtime->GetClassLinker()->FindArrayClass(soa.Self(), &element_class);
95   if (UNLIKELY(array_class == nullptr)) {
96     return nullptr;
97   }
98   gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentNonMovingAllocator();
99   ObjPtr<mirror::Array> result = mirror::Array::Alloc<true>(soa.Self(),
100                                                             array_class,
101                                                             length,
102                                                             array_class->GetComponentSizeShift(),
103                                                             allocator);
104   return soa.AddLocalReference<jobject>(result);
105 }
106 
VMRuntime_newUnpaddedArray(JNIEnv * env,jobject,jclass javaElementClass,jint length)107 static jobject VMRuntime_newUnpaddedArray(JNIEnv* env, jobject, jclass javaElementClass,
108                                           jint length) {
109   ScopedFastNativeObjectAccess soa(env);
110   if (UNLIKELY(length < 0)) {
111     ThrowNegativeArraySizeException(length);
112     return nullptr;
113   }
114   ObjPtr<mirror::Class> element_class = soa.Decode<mirror::Class>(javaElementClass);
115   if (UNLIKELY(element_class == nullptr)) {
116     ThrowNullPointerException("element class == null");
117     return nullptr;
118   }
119   Runtime* runtime = Runtime::Current();
120   ObjPtr<mirror::Class> array_class = runtime->GetClassLinker()->FindArrayClass(soa.Self(),
121                                                                                 &element_class);
122   if (UNLIKELY(array_class == nullptr)) {
123     return nullptr;
124   }
125   gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
126   ObjPtr<mirror::Array> result = mirror::Array::Alloc<true, true>(
127       soa.Self(),
128       array_class,
129       length,
130       array_class->GetComponentSizeShift(),
131       allocator);
132   return soa.AddLocalReference<jobject>(result);
133 }
134 
VMRuntime_addressOf(JNIEnv * env,jobject,jobject javaArray)135 static jlong VMRuntime_addressOf(JNIEnv* env, jobject, jobject javaArray) {
136   if (javaArray == nullptr) {  // Most likely allocation failed
137     return 0;
138   }
139   ScopedFastNativeObjectAccess soa(env);
140   ObjPtr<mirror::Array> array = soa.Decode<mirror::Array>(javaArray);
141   if (!array->IsArrayInstance()) {
142     ThrowIllegalArgumentException("not an array");
143     return 0;
144   }
145   if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
146     ThrowRuntimeException("Trying to get address of movable array object");
147     return 0;
148   }
149   return reinterpret_cast<uintptr_t>(array->GetRawData(array->GetClass()->GetComponentSize(), 0));
150 }
151 
VMRuntime_clearGrowthLimit(JNIEnv *,jobject)152 static void VMRuntime_clearGrowthLimit(JNIEnv*, jobject) {
153   Runtime::Current()->GetHeap()->ClearGrowthLimit();
154 }
155 
VMRuntime_clampGrowthLimit(JNIEnv *,jobject)156 static void VMRuntime_clampGrowthLimit(JNIEnv*, jobject) {
157   Runtime::Current()->GetHeap()->ClampGrowthLimit();
158 }
159 
VMRuntime_isDebuggerActive(JNIEnv *,jobject)160 static jboolean VMRuntime_isDebuggerActive(JNIEnv*, jobject) {
161   return Dbg::IsDebuggerActive();
162 }
163 
VMRuntime_isNativeDebuggable(JNIEnv *,jobject)164 static jboolean VMRuntime_isNativeDebuggable(JNIEnv*, jobject) {
165   return Runtime::Current()->IsNativeDebuggable();
166 }
167 
VMRuntime_properties(JNIEnv * env,jobject)168 static jobjectArray VMRuntime_properties(JNIEnv* env, jobject) {
169   return toStringArray(env, Runtime::Current()->GetProperties());
170 }
171 
172 // This is for backward compatibility with dalvik which returned the
173 // meaningless "." when no boot classpath or classpath was
174 // specified. Unfortunately, some tests were using java.class.path to
175 // lookup relative file locations, so they are counting on this to be
176 // ".", presumably some applications or libraries could have as well.
DefaultToDot(const std::string & class_path)177 static const char* DefaultToDot(const std::string& class_path) {
178   return class_path.empty() ? "." : class_path.c_str();
179 }
180 
VMRuntime_bootClassPath(JNIEnv * env,jobject)181 static jstring VMRuntime_bootClassPath(JNIEnv* env, jobject) {
182   return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetBootClassPathString()));
183 }
184 
VMRuntime_classPath(JNIEnv * env,jobject)185 static jstring VMRuntime_classPath(JNIEnv* env, jobject) {
186   return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetClassPathString()));
187 }
188 
VMRuntime_vmVersion(JNIEnv * env,jobject)189 static jstring VMRuntime_vmVersion(JNIEnv* env, jobject) {
190   return env->NewStringUTF(Runtime::GetVersion());
191 }
192 
VMRuntime_vmLibrary(JNIEnv * env,jobject)193 static jstring VMRuntime_vmLibrary(JNIEnv* env, jobject) {
194   return env->NewStringUTF(kIsDebugBuild ? "libartd.so" : "libart.so");
195 }
196 
VMRuntime_vmInstructionSet(JNIEnv * env,jobject)197 static jstring VMRuntime_vmInstructionSet(JNIEnv* env, jobject) {
198   InstructionSet isa = Runtime::Current()->GetInstructionSet();
199   const char* isa_string = GetInstructionSetString(isa);
200   return env->NewStringUTF(isa_string);
201 }
202 
VMRuntime_is64Bit(JNIEnv *,jobject)203 static jboolean VMRuntime_is64Bit(JNIEnv*, jobject) {
204   bool is64BitMode = (sizeof(void*) == sizeof(uint64_t));
205   return is64BitMode ? JNI_TRUE : JNI_FALSE;
206 }
207 
VMRuntime_isCheckJniEnabled(JNIEnv * env,jobject)208 static jboolean VMRuntime_isCheckJniEnabled(JNIEnv* env, jobject) {
209   return down_cast<JNIEnvExt*>(env)->vm->IsCheckJniEnabled() ? JNI_TRUE : JNI_FALSE;
210 }
211 
VMRuntime_setTargetSdkVersionNative(JNIEnv *,jobject,jint target_sdk_version)212 static void VMRuntime_setTargetSdkVersionNative(JNIEnv*, jobject, jint target_sdk_version) {
213   // This is the target SDK version of the app we're about to run. It is intended that this a place
214   // where workarounds can be enabled.
215   // Note that targetSdkVersion may be CUR_DEVELOPMENT (10000).
216   // Note that targetSdkVersion may be 0, meaning "current".
217   Runtime::Current()->SetTargetSdkVersion(target_sdk_version);
218 
219 #ifdef ART_TARGET_ANDROID
220   // This part is letting libc/dynamic linker know about current app's
221   // target sdk version to enable compatibility workarounds.
222   android_set_application_target_sdk_version(static_cast<uint32_t>(target_sdk_version));
223 #endif
224 }
225 
VMRuntime_registerNativeAllocation(JNIEnv * env,jobject,jint bytes)226 static void VMRuntime_registerNativeAllocation(JNIEnv* env, jobject, jint bytes) {
227   if (UNLIKELY(bytes < 0)) {
228     ScopedObjectAccess soa(env);
229     ThrowRuntimeException("allocation size negative %d", bytes);
230     return;
231   }
232   Runtime::Current()->GetHeap()->RegisterNativeAllocation(env, static_cast<size_t>(bytes));
233 }
234 
VMRuntime_registerSensitiveThread(JNIEnv *,jobject)235 static void VMRuntime_registerSensitiveThread(JNIEnv*, jobject) {
236   Runtime::Current()->RegisterSensitiveThread();
237 }
238 
VMRuntime_registerNativeFree(JNIEnv * env,jobject,jint bytes)239 static void VMRuntime_registerNativeFree(JNIEnv* env, jobject, jint bytes) {
240   if (UNLIKELY(bytes < 0)) {
241     ScopedObjectAccess soa(env);
242     ThrowRuntimeException("allocation size negative %d", bytes);
243     return;
244   }
245   Runtime::Current()->GetHeap()->RegisterNativeFree(env, static_cast<size_t>(bytes));
246 }
247 
VMRuntime_updateProcessState(JNIEnv *,jobject,jint process_state)248 static void VMRuntime_updateProcessState(JNIEnv*, jobject, jint process_state) {
249   Runtime* runtime = Runtime::Current();
250   runtime->UpdateProcessState(static_cast<ProcessState>(process_state));
251 }
252 
VMRuntime_trimHeap(JNIEnv * env,jobject)253 static void VMRuntime_trimHeap(JNIEnv* env, jobject) {
254   Runtime::Current()->GetHeap()->Trim(ThreadForEnv(env));
255 }
256 
VMRuntime_concurrentGC(JNIEnv * env,jobject)257 static void VMRuntime_concurrentGC(JNIEnv* env, jobject) {
258   Runtime::Current()->GetHeap()->ConcurrentGC(ThreadForEnv(env), gc::kGcCauseBackground, true);
259 }
260 
VMRuntime_requestHeapTrim(JNIEnv * env,jobject)261 static void VMRuntime_requestHeapTrim(JNIEnv* env, jobject) {
262   Runtime::Current()->GetHeap()->RequestTrim(ThreadForEnv(env));
263 }
264 
VMRuntime_requestConcurrentGC(JNIEnv * env,jobject)265 static void VMRuntime_requestConcurrentGC(JNIEnv* env, jobject) {
266   Runtime::Current()->GetHeap()->RequestConcurrentGC(ThreadForEnv(env),
267                                                      gc::kGcCauseBackground,
268                                                      true);
269 }
270 
VMRuntime_startHeapTaskProcessor(JNIEnv * env,jobject)271 static void VMRuntime_startHeapTaskProcessor(JNIEnv* env, jobject) {
272   Runtime::Current()->GetHeap()->GetTaskProcessor()->Start(ThreadForEnv(env));
273 }
274 
VMRuntime_stopHeapTaskProcessor(JNIEnv * env,jobject)275 static void VMRuntime_stopHeapTaskProcessor(JNIEnv* env, jobject) {
276   Runtime::Current()->GetHeap()->GetTaskProcessor()->Stop(ThreadForEnv(env));
277 }
278 
VMRuntime_runHeapTasks(JNIEnv * env,jobject)279 static void VMRuntime_runHeapTasks(JNIEnv* env, jobject) {
280   Runtime::Current()->GetHeap()->GetTaskProcessor()->RunAllTasks(ThreadForEnv(env));
281 }
282 
283 typedef std::map<std::string, ObjPtr<mirror::String>> StringTable;
284 
285 class PreloadDexCachesStringsVisitor : public SingleRootVisitor {
286  public:
PreloadDexCachesStringsVisitor(StringTable * table)287   explicit PreloadDexCachesStringsVisitor(StringTable* table) : table_(table) { }
288 
VisitRoot(mirror::Object * root,const RootInfo & info ATTRIBUTE_UNUSED)289   void VisitRoot(mirror::Object* root, const RootInfo& info ATTRIBUTE_UNUSED)
290       OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
291     ObjPtr<mirror::String> string = root->AsString();
292     table_->operator[](string->ToModifiedUtf8()) = string;
293   }
294 
295  private:
296   StringTable* const table_;
297 };
298 
299 // Based on ClassLinker::ResolveString.
PreloadDexCachesResolveString(ObjPtr<mirror::DexCache> dex_cache,dex::StringIndex string_idx,StringTable & strings)300 static void PreloadDexCachesResolveString(
301     ObjPtr<mirror::DexCache> dex_cache, dex::StringIndex string_idx, StringTable& strings)
302     REQUIRES_SHARED(Locks::mutator_lock_) {
303   uint32_t slot_idx = dex_cache->StringSlotIndex(string_idx);
304   auto pair = dex_cache->GetStrings()[slot_idx].load(std::memory_order_relaxed);
305   if (!pair.object.IsNull()) {
306     return;  // The entry already contains some String.
307   }
308   const DexFile* dex_file = dex_cache->GetDexFile();
309   const char* utf8 = dex_file->StringDataByIdx(string_idx);
310   ObjPtr<mirror::String> string = strings[utf8];
311   if (string == nullptr) {
312     return;
313   }
314   // LOG(INFO) << "VMRuntime.preloadDexCaches resolved string=" << utf8;
315   dex_cache->SetResolvedString(string_idx, string);
316 }
317 
318 // Based on ClassLinker::ResolveType.
PreloadDexCachesResolveType(Thread * self,ObjPtr<mirror::DexCache> dex_cache,dex::TypeIndex type_idx)319 static void PreloadDexCachesResolveType(Thread* self,
320                                         ObjPtr<mirror::DexCache> dex_cache,
321                                         dex::TypeIndex type_idx)
322     REQUIRES_SHARED(Locks::mutator_lock_) {
323   uint32_t slot_idx = dex_cache->TypeSlotIndex(type_idx);
324   auto pair = dex_cache->GetResolvedTypes()[slot_idx].load(std::memory_order_relaxed);
325   if (!pair.object.IsNull()) {
326     return;  // The entry already contains some Class.
327   }
328   const DexFile* dex_file = dex_cache->GetDexFile();
329   const char* class_name = dex_file->StringByTypeIdx(type_idx);
330   ClassLinker* linker = Runtime::Current()->GetClassLinker();
331   ObjPtr<mirror::Class> klass = (class_name[1] == '\0')
332       ? linker->FindPrimitiveClass(class_name[0])
333       : linker->LookupClass(self, class_name, nullptr);
334   if (klass == nullptr) {
335     return;
336   }
337   // LOG(INFO) << "VMRuntime.preloadDexCaches resolved klass=" << class_name;
338   dex_cache->SetResolvedType(type_idx, klass);
339   // Skip uninitialized classes because filled static storage entry implies it is initialized.
340   if (!klass->IsInitialized()) {
341     // LOG(INFO) << "VMRuntime.preloadDexCaches uninitialized klass=" << class_name;
342     return;
343   }
344   // LOG(INFO) << "VMRuntime.preloadDexCaches static storage klass=" << class_name;
345 }
346 
347 // Based on ClassLinker::ResolveField.
PreloadDexCachesResolveField(ObjPtr<mirror::DexCache> dex_cache,uint32_t field_idx,bool is_static)348 static void PreloadDexCachesResolveField(ObjPtr<mirror::DexCache> dex_cache,
349                                          uint32_t field_idx,
350                                          bool is_static)
351     REQUIRES_SHARED(Locks::mutator_lock_) {
352   uint32_t slot_idx = dex_cache->FieldSlotIndex(field_idx);
353   auto pair = mirror::DexCache::GetNativePairPtrSize(dex_cache->GetResolvedFields(),
354                                                      slot_idx,
355                                                      kRuntimePointerSize);
356   if (pair.object != nullptr) {
357     return;  // The entry already contains some ArtField.
358   }
359   const DexFile* dex_file = dex_cache->GetDexFile();
360   const DexFile::FieldId& field_id = dex_file->GetFieldId(field_idx);
361   ObjPtr<mirror::Class> klass =
362       ClassLinker::LookupResolvedType(field_id.class_idx_, dex_cache, nullptr);
363   if (klass == nullptr) {
364     return;
365   }
366   ArtField* field = is_static
367       ? mirror::Class::FindStaticField(Thread::Current(), klass, dex_cache, field_idx)
368       : klass->FindInstanceField(dex_cache, field_idx);
369   if (field == nullptr) {
370     return;
371   }
372   dex_cache->SetResolvedField(field_idx, field, kRuntimePointerSize);
373 }
374 
375 // Based on ClassLinker::ResolveMethod.
PreloadDexCachesResolveMethod(ObjPtr<mirror::DexCache> dex_cache,uint32_t method_idx)376 static void PreloadDexCachesResolveMethod(ObjPtr<mirror::DexCache> dex_cache, uint32_t method_idx)
377     REQUIRES_SHARED(Locks::mutator_lock_) {
378   uint32_t slot_idx = dex_cache->MethodSlotIndex(method_idx);
379   auto pair = mirror::DexCache::GetNativePairPtrSize(dex_cache->GetResolvedMethods(),
380                                                      slot_idx,
381                                                      kRuntimePointerSize);
382   if (pair.object != nullptr) {
383     return;  // The entry already contains some ArtMethod.
384   }
385   const DexFile* dex_file = dex_cache->GetDexFile();
386   const DexFile::MethodId& method_id = dex_file->GetMethodId(method_idx);
387   ObjPtr<mirror::Class> klass =
388       ClassLinker::LookupResolvedType(method_id.class_idx_, dex_cache, nullptr);
389   if (klass == nullptr) {
390     return;
391   }
392   ArtMethod* method = klass->IsInterface()
393       ? klass->FindInterfaceMethod(dex_cache, method_idx, kRuntimePointerSize)
394       : klass->FindClassMethod(dex_cache, method_idx, kRuntimePointerSize);
395   if (method == nullptr) {
396     return;
397   }
398   dex_cache->SetResolvedMethod(method_idx, method, kRuntimePointerSize);
399 }
400 
401 struct DexCacheStats {
402     uint32_t num_strings;
403     uint32_t num_types;
404     uint32_t num_fields;
405     uint32_t num_methods;
DexCacheStatsart::DexCacheStats406     DexCacheStats() : num_strings(0),
407                       num_types(0),
408                       num_fields(0),
409                       num_methods(0) {}
410 };
411 
412 static const bool kPreloadDexCachesEnabled = true;
413 
414 // Disabled because it takes a long time (extra half second) but
415 // gives almost no benefit in terms of saving private dirty pages.
416 static const bool kPreloadDexCachesStrings = false;
417 
418 static const bool kPreloadDexCachesTypes = true;
419 static const bool kPreloadDexCachesFieldsAndMethods = true;
420 
421 static const bool kPreloadDexCachesCollectStats = true;
422 
PreloadDexCachesStatsTotal(DexCacheStats * total)423 static void PreloadDexCachesStatsTotal(DexCacheStats* total) {
424   if (!kPreloadDexCachesCollectStats) {
425     return;
426   }
427 
428   ClassLinker* linker = Runtime::Current()->GetClassLinker();
429   const std::vector<const DexFile*>& boot_class_path = linker->GetBootClassPath();
430   for (size_t i = 0; i< boot_class_path.size(); i++) {
431     const DexFile* dex_file = boot_class_path[i];
432     CHECK(dex_file != nullptr);
433     total->num_strings += dex_file->NumStringIds();
434     total->num_fields += dex_file->NumFieldIds();
435     total->num_methods += dex_file->NumMethodIds();
436     total->num_types += dex_file->NumTypeIds();
437   }
438 }
439 
PreloadDexCachesStatsFilled(DexCacheStats * filled)440 static void PreloadDexCachesStatsFilled(DexCacheStats* filled)
441     REQUIRES_SHARED(Locks::mutator_lock_) {
442   if (!kPreloadDexCachesCollectStats) {
443     return;
444   }
445   // TODO: Update for hash-based DexCache arrays.
446   ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
447   Thread* const self = Thread::Current();
448   for (const DexFile* dex_file : class_linker->GetBootClassPath()) {
449     CHECK(dex_file != nullptr);
450     // In fallback mode, not all boot classpath components might be registered, yet.
451     if (!class_linker->IsDexFileRegistered(self, *dex_file)) {
452       continue;
453     }
454     ObjPtr<mirror::DexCache> const dex_cache = class_linker->FindDexCache(self, *dex_file);
455     DCHECK(dex_cache != nullptr);  // Boot class path dex caches are never unloaded.
456     for (size_t j = 0, num_strings = dex_cache->NumStrings(); j < num_strings; ++j) {
457       auto pair = dex_cache->GetStrings()[j].load(std::memory_order_relaxed);
458       if (!pair.object.IsNull()) {
459         filled->num_strings++;
460       }
461     }
462     for (size_t j = 0, num_types = dex_cache->NumResolvedTypes(); j < num_types; ++j) {
463       auto pair = dex_cache->GetResolvedTypes()[j].load(std::memory_order_relaxed);
464       if (!pair.object.IsNull()) {
465         filled->num_types++;
466       }
467     }
468     for (size_t j = 0, num_fields = dex_cache->NumResolvedFields(); j < num_fields; ++j) {
469       auto pair = mirror::DexCache::GetNativePairPtrSize(dex_cache->GetResolvedFields(),
470                                                          j,
471                                                          kRuntimePointerSize);
472       if (pair.object != nullptr) {
473         filled->num_fields++;
474       }
475     }
476     for (size_t j = 0, num_methods = dex_cache->NumResolvedMethods(); j < num_methods; ++j) {
477       auto pair = mirror::DexCache::GetNativePairPtrSize(dex_cache->GetResolvedMethods(),
478                                                          j,
479                                                          kRuntimePointerSize);
480       if (pair.object != nullptr) {
481         filled->num_methods++;
482       }
483     }
484   }
485 }
486 
487 // TODO: http://b/11309598 This code was ported over based on the
488 // Dalvik version. However, ART has similar code in other places such
489 // as the CompilerDriver. This code could probably be refactored to
490 // serve both uses.
VMRuntime_preloadDexCaches(JNIEnv * env,jobject)491 static void VMRuntime_preloadDexCaches(JNIEnv* env, jobject) {
492   if (!kPreloadDexCachesEnabled) {
493     return;
494   }
495 
496   ScopedObjectAccess soa(env);
497 
498   DexCacheStats total;
499   DexCacheStats before;
500   if (kPreloadDexCachesCollectStats) {
501     LOG(INFO) << "VMRuntime.preloadDexCaches starting";
502     PreloadDexCachesStatsTotal(&total);
503     PreloadDexCachesStatsFilled(&before);
504   }
505 
506   Runtime* runtime = Runtime::Current();
507   ClassLinker* linker = runtime->GetClassLinker();
508 
509   // We use a std::map to avoid heap allocating StringObjects to lookup in gDvm.literalStrings
510   StringTable strings;
511   if (kPreloadDexCachesStrings) {
512     PreloadDexCachesStringsVisitor visitor(&strings);
513     runtime->GetInternTable()->VisitRoots(&visitor, kVisitRootFlagAllRoots);
514   }
515 
516   const std::vector<const DexFile*>& boot_class_path = linker->GetBootClassPath();
517   for (size_t i = 0; i < boot_class_path.size(); i++) {
518     const DexFile* dex_file = boot_class_path[i];
519     CHECK(dex_file != nullptr);
520     ObjPtr<mirror::DexCache> dex_cache = linker->RegisterDexFile(*dex_file, nullptr);
521     CHECK(dex_cache != nullptr);  // Boot class path dex caches are never unloaded.
522     if (kPreloadDexCachesStrings) {
523       for (size_t j = 0; j < dex_cache->NumStrings(); j++) {
524         PreloadDexCachesResolveString(dex_cache, dex::StringIndex(j), strings);
525       }
526     }
527 
528     if (kPreloadDexCachesTypes) {
529       for (size_t j = 0; j < dex_cache->NumResolvedTypes(); j++) {
530         PreloadDexCachesResolveType(soa.Self(), dex_cache, dex::TypeIndex(j));
531       }
532     }
533 
534     if (kPreloadDexCachesFieldsAndMethods) {
535       for (size_t class_def_index = 0;
536            class_def_index < dex_file->NumClassDefs();
537            class_def_index++) {
538         const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
539         const uint8_t* class_data = dex_file->GetClassData(class_def);
540         if (class_data == nullptr) {
541           continue;
542         }
543         ClassDataItemIterator it(*dex_file, class_data);
544         for (; it.HasNextStaticField(); it.Next()) {
545           uint32_t field_idx = it.GetMemberIndex();
546           PreloadDexCachesResolveField(dex_cache, field_idx, true);
547         }
548         for (; it.HasNextInstanceField(); it.Next()) {
549           uint32_t field_idx = it.GetMemberIndex();
550           PreloadDexCachesResolveField(dex_cache, field_idx, false);
551         }
552         for (; it.HasNextDirectMethod(); it.Next()) {
553           uint32_t method_idx = it.GetMemberIndex();
554           PreloadDexCachesResolveMethod(dex_cache, method_idx);
555         }
556         for (; it.HasNextVirtualMethod(); it.Next()) {
557           uint32_t method_idx = it.GetMemberIndex();
558           PreloadDexCachesResolveMethod(dex_cache, method_idx);
559         }
560       }
561     }
562   }
563 
564   if (kPreloadDexCachesCollectStats) {
565     DexCacheStats after;
566     PreloadDexCachesStatsFilled(&after);
567     LOG(INFO) << StringPrintf("VMRuntime.preloadDexCaches strings total=%d before=%d after=%d",
568                               total.num_strings, before.num_strings, after.num_strings);
569     LOG(INFO) << StringPrintf("VMRuntime.preloadDexCaches types total=%d before=%d after=%d",
570                               total.num_types, before.num_types, after.num_types);
571     LOG(INFO) << StringPrintf("VMRuntime.preloadDexCaches fields total=%d before=%d after=%d",
572                               total.num_fields, before.num_fields, after.num_fields);
573     LOG(INFO) << StringPrintf("VMRuntime.preloadDexCaches methods total=%d before=%d after=%d",
574                               total.num_methods, before.num_methods, after.num_methods);
575     LOG(INFO) << StringPrintf("VMRuntime.preloadDexCaches finished");
576   }
577 }
578 
579 
580 /*
581  * This is called by the framework when it knows the application directory and
582  * process name.
583  */
VMRuntime_registerAppInfo(JNIEnv * env,jclass clazz ATTRIBUTE_UNUSED,jstring profile_file,jobjectArray code_paths)584 static void VMRuntime_registerAppInfo(JNIEnv* env,
585                                       jclass clazz ATTRIBUTE_UNUSED,
586                                       jstring profile_file,
587                                       jobjectArray code_paths) {
588   std::vector<std::string> code_paths_vec;
589   int code_paths_length = env->GetArrayLength(code_paths);
590   for (int i = 0; i < code_paths_length; i++) {
591     jstring code_path = reinterpret_cast<jstring>(env->GetObjectArrayElement(code_paths, i));
592     const char* raw_code_path = env->GetStringUTFChars(code_path, nullptr);
593     code_paths_vec.push_back(raw_code_path);
594     env->ReleaseStringUTFChars(code_path, raw_code_path);
595   }
596 
597   const char* raw_profile_file = env->GetStringUTFChars(profile_file, nullptr);
598   std::string profile_file_str(raw_profile_file);
599   env->ReleaseStringUTFChars(profile_file, raw_profile_file);
600 
601   Runtime::Current()->RegisterAppInfo(code_paths_vec, profile_file_str);
602 }
603 
VMRuntime_isBootClassPathOnDisk(JNIEnv * env,jclass,jstring java_instruction_set)604 static jboolean VMRuntime_isBootClassPathOnDisk(JNIEnv* env, jclass, jstring java_instruction_set) {
605   ScopedUtfChars instruction_set(env, java_instruction_set);
606   if (instruction_set.c_str() == nullptr) {
607     return JNI_FALSE;
608   }
609   InstructionSet isa = GetInstructionSetFromString(instruction_set.c_str());
610   if (isa == kNone) {
611     ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
612     std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str()));
613     env->ThrowNew(iae.get(), message.c_str());
614     return JNI_FALSE;
615   }
616   std::string error_msg;
617   std::unique_ptr<ImageHeader> image_header(gc::space::ImageSpace::ReadImageHeader(
618       Runtime::Current()->GetImageLocation().c_str(), isa, &error_msg));
619   return image_header.get() != nullptr;
620 }
621 
VMRuntime_getCurrentInstructionSet(JNIEnv * env,jclass)622 static jstring VMRuntime_getCurrentInstructionSet(JNIEnv* env, jclass) {
623   return env->NewStringUTF(GetInstructionSetString(kRuntimeISA));
624 }
625 
VMRuntime_didPruneDalvikCache(JNIEnv * env ATTRIBUTE_UNUSED,jclass klass ATTRIBUTE_UNUSED)626 static jboolean VMRuntime_didPruneDalvikCache(JNIEnv* env ATTRIBUTE_UNUSED,
627                                               jclass klass ATTRIBUTE_UNUSED) {
628   return Runtime::Current()->GetPrunedDalvikCache() ? JNI_TRUE : JNI_FALSE;
629 }
630 
VMRuntime_setSystemDaemonThreadPriority(JNIEnv * env ATTRIBUTE_UNUSED,jclass klass ATTRIBUTE_UNUSED)631 static void VMRuntime_setSystemDaemonThreadPriority(JNIEnv* env ATTRIBUTE_UNUSED,
632                                                     jclass klass ATTRIBUTE_UNUSED) {
633 #ifdef ART_TARGET_ANDROID
634   Thread* self = Thread::Current();
635   DCHECK(self != nullptr);
636   pid_t tid = self->GetTid();
637   // We use a priority lower than the default for the system daemon threads (eg HeapTaskDaemon) to
638   // avoid jank due to CPU contentions between GC and other UI-related threads. b/36631902.
639   // We may use a native priority that doesn't have a corresponding java.lang.Thread-level priority.
640   static constexpr int kSystemDaemonNiceValue = 4;  // priority 124
641   if (setpriority(PRIO_PROCESS, tid, kSystemDaemonNiceValue) != 0) {
642     PLOG(INFO) << *self << " setpriority(PRIO_PROCESS, " << tid << ", "
643                << kSystemDaemonNiceValue << ") failed";
644   }
645 #endif
646 }
647 
648 static JNINativeMethod gMethods[] = {
649   FAST_NATIVE_METHOD(VMRuntime, addressOf, "(Ljava/lang/Object;)J"),
650   NATIVE_METHOD(VMRuntime, bootClassPath, "()Ljava/lang/String;"),
651   NATIVE_METHOD(VMRuntime, clampGrowthLimit, "()V"),
652   NATIVE_METHOD(VMRuntime, classPath, "()Ljava/lang/String;"),
653   NATIVE_METHOD(VMRuntime, clearGrowthLimit, "()V"),
654   NATIVE_METHOD(VMRuntime, concurrentGC, "()V"),
655   NATIVE_METHOD(VMRuntime, disableJitCompilation, "()V"),
656   NATIVE_METHOD(VMRuntime, getTargetHeapUtilization, "()F"),
657   FAST_NATIVE_METHOD(VMRuntime, isDebuggerActive, "()Z"),
658   FAST_NATIVE_METHOD(VMRuntime, isNativeDebuggable, "()Z"),
659   NATIVE_METHOD(VMRuntime, nativeSetTargetHeapUtilization, "(F)V"),
660   FAST_NATIVE_METHOD(VMRuntime, newNonMovableArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
661   FAST_NATIVE_METHOD(VMRuntime, newUnpaddedArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
662   NATIVE_METHOD(VMRuntime, properties, "()[Ljava/lang/String;"),
663   NATIVE_METHOD(VMRuntime, setTargetSdkVersionNative, "(I)V"),
664   NATIVE_METHOD(VMRuntime, registerNativeAllocation, "(I)V"),
665   NATIVE_METHOD(VMRuntime, registerSensitiveThread, "()V"),
666   NATIVE_METHOD(VMRuntime, registerNativeFree, "(I)V"),
667   NATIVE_METHOD(VMRuntime, requestConcurrentGC, "()V"),
668   NATIVE_METHOD(VMRuntime, requestHeapTrim, "()V"),
669   NATIVE_METHOD(VMRuntime, runHeapTasks, "()V"),
670   NATIVE_METHOD(VMRuntime, updateProcessState, "(I)V"),
671   NATIVE_METHOD(VMRuntime, startHeapTaskProcessor, "()V"),
672   NATIVE_METHOD(VMRuntime, startJitCompilation, "()V"),
673   NATIVE_METHOD(VMRuntime, stopHeapTaskProcessor, "()V"),
674   NATIVE_METHOD(VMRuntime, trimHeap, "()V"),
675   NATIVE_METHOD(VMRuntime, vmVersion, "()Ljava/lang/String;"),
676   NATIVE_METHOD(VMRuntime, vmLibrary, "()Ljava/lang/String;"),
677   NATIVE_METHOD(VMRuntime, vmInstructionSet, "()Ljava/lang/String;"),
678   FAST_NATIVE_METHOD(VMRuntime, is64Bit, "()Z"),
679   FAST_NATIVE_METHOD(VMRuntime, isCheckJniEnabled, "()Z"),
680   NATIVE_METHOD(VMRuntime, preloadDexCaches, "()V"),
681   NATIVE_METHOD(VMRuntime, registerAppInfo, "(Ljava/lang/String;[Ljava/lang/String;)V"),
682   NATIVE_METHOD(VMRuntime, isBootClassPathOnDisk, "(Ljava/lang/String;)Z"),
683   NATIVE_METHOD(VMRuntime, getCurrentInstructionSet, "()Ljava/lang/String;"),
684   NATIVE_METHOD(VMRuntime, didPruneDalvikCache, "()Z"),
685   NATIVE_METHOD(VMRuntime, setSystemDaemonThreadPriority, "()V"),
686 };
687 
register_dalvik_system_VMRuntime(JNIEnv * env)688 void register_dalvik_system_VMRuntime(JNIEnv* env) {
689   REGISTER_NATIVE_METHODS("dalvik/system/VMRuntime");
690 }
691 
692 }  // namespace art
693