Call madvise hints for image dex files

After opening image spaces, perform madvise hints for the
corresponding dex files.

Moved kMadviseDexFileAccesses to oat_file.cc to not have this enabled
yet.

Bug: 63178181
Test: make and flash

Change-Id: I60f918274251629cd0e829f0862d9a2528216c08
diff --git a/runtime/class_linker.cc b/runtime/class_linker.cc
index e135bcc..34fc73e 100644
--- a/runtime/class_linker.cc
+++ b/runtime/class_linker.cc
@@ -1020,6 +1020,9 @@
                            std::make_move_iterator(dex_files.begin()),
                            std::make_move_iterator(dex_files.end()));
   }
+  for (const std::unique_ptr<const DexFile>& dex_file : boot_dex_files_) {
+    OatDexFile::MadviseDexFile(*dex_file, MadviseState::kMadviseStateAtLoad);
+  }
   FinishInit(self);
 
   VLOG(startup) << __FUNCTION__ << " exiting";
diff --git a/runtime/oat_file.cc b/runtime/oat_file.cc
index 5b3440b..0d04368 100644
--- a/runtime/oat_file.cc
+++ b/runtime/oat_file.cc
@@ -74,6 +74,9 @@
 // For debugging, Open will print DlOpen error message if set to true.
 static constexpr bool kPrintDlOpenErrorMessage = false;
 
+// If true, we advise the kernel about dex file mem map accesses.
+static constexpr bool kMadviseDexFileAccesses = false;
+
 // Note for OatFileBase and descendents:
 //
 // These are used in OatFile::Open to try all our loaders.
@@ -1495,6 +1498,9 @@
 
 // Madvise the dex file based on the state we are moving to.
 void OatDexFile::MadviseDexFile(const DexFile& dex_file, MadviseState state) {
+  if (!kMadviseDexFileAccesses) {
+    return;
+  }
   if (state == MadviseState::kMadviseStateAtLoad) {
     // Default every dex file to MADV_RANDOM when its loaded by default.
     MadviseLargestPageAlignedRegion(dex_file.Begin(),
diff --git a/runtime/oat_file_manager.cc b/runtime/oat_file_manager.cc
index de8f7ed..499f356 100644
--- a/runtime/oat_file_manager.cc
+++ b/runtime/oat_file_manager.cc
@@ -51,9 +51,6 @@
 // If true, we attempt to load the application image if it exists.
 static constexpr bool kEnableAppImage = true;
 
-// If true, we advise the kernel about dex file mem map accesses.
-static constexpr bool kMadviseDexFileAccesses = false;
-
 const OatFile* OatFileManager::RegisterOatFile(std::unique_ptr<const OatFile> oat_file) {
   WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
   DCHECK(oat_file != nullptr);
@@ -572,7 +569,7 @@
     }
     if (dex_files.empty()) {
       error_msgs->push_back("Failed to open dex files from " + source_oat_file->GetLocation());
-    } else if (kMadviseDexFileAccesses) {
+    } else {
       // Opened dex files from an oat file, madvise them to their loaded state.
        for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
          OatDexFile::MadviseDexFile(*dex_file, MadviseState::kMadviseStateAtLoad);