Skip profiling if the dex file is fully compiled

The OatFile does not store the original dex location that was used to
compile the file. So in order to get the oat file for a given code path
we need to iterate over the inner oat dex files and check against them.

Also, we used to pass the unfiltered code_paths set to the saver which
would make the filtering useless. This fixes the parameter passing as
well.

Bug: 27893309
Change-Id: Ia95d61a82a9e74769ebf3329bb1f9c714bcff27f
diff --git a/runtime/jit/profile_saver.cc b/runtime/jit/profile_saver.cc
index 6599df2..a224ebb 100644
--- a/runtime/jit/profile_saver.cc
+++ b/runtime/jit/profile_saver.cc
@@ -265,16 +265,20 @@
 }
 
 static bool ShouldProfileLocation(const std::string& location) {
-  const OatFileManager& oat_manager = Runtime::Current()->GetOatFileManager();
-  const OatFile* oat_file = oat_manager.FindOpenedOatFileFromOatLocation(location);
+  OatFileManager& oat_manager = Runtime::Current()->GetOatFileManager();
+  const OatFile* oat_file = oat_manager.FindOpenedOatFileFromDexLocation(location);
   if (oat_file == nullptr) {
     // This can happen if we fallback to run code directly from the APK.
     // Profile it with the hope that the background dexopt will get us back into
     // a good state.
+    VLOG(profiler) << "Asked to profile a location without an oat file:" << location;
     return true;
   }
   CompilerFilter::Filter filter = oat_file->GetCompilerFilter();
   if (filter == CompilerFilter::kSpeed || CompilerFilter::kEverything) {
+    VLOG(profiler)
+        << "Skip profiling oat file because it's already speed|everything compiled:"
+        << location;
     return false;
   }
   return true;
@@ -294,13 +298,10 @@
   for (const std::string& location : code_paths) {
     if (ShouldProfileLocation(location))  {
       code_paths_to_profile.push_back(location);
-    } else {
-      VLOG(profiler)
-        << "Skip profiling oat file because it's already speed|everything compiled:"
-        << location;
     }
   }
   if (code_paths_to_profile.empty()) {
+    VLOG(profiler) << "No code paths should be profiled.";
     return;
   }
 
@@ -316,11 +317,11 @@
   }
 
   VLOG(profiler) << "Starting profile saver using output file: " << output_filename
-      << ". Tracking: " << Join(code_paths, ':');
+      << ". Tracking: " << Join(code_paths_to_profile, ':');
 
   instance_ = new ProfileSaver(output_filename,
                                jit_code_cache,
-                               code_paths,
+                               code_paths_to_profile,
                                foreign_dex_profile_path,
                                app_data_dir);
 
diff --git a/runtime/oat_file_manager.cc b/runtime/oat_file_manager.cc
index 3846605..20f611e 100644
--- a/runtime/oat_file_manager.cc
+++ b/runtime/oat_file_manager.cc
@@ -74,6 +74,20 @@
   compare.release();
 }
 
+const OatFile* OatFileManager::FindOpenedOatFileFromDexLocation(
+    const std::string& dex_base_location) const {
+  ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
+  for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
+    const std::vector<const OatDexFile*>& oat_dex_files = oat_file->GetOatDexFiles();
+    for (const OatDexFile* oat_dex_file : oat_dex_files) {
+      if (DexFile::GetBaseLocation(oat_dex_file->GetDexFileLocation()) == dex_base_location) {
+        return oat_file.get();
+      }
+    }
+  }
+  return nullptr;
+}
+
 const OatFile* OatFileManager::FindOpenedOatFileFromOatLocation(const std::string& oat_location)
     const {
   ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
diff --git a/runtime/oat_file_manager.h b/runtime/oat_file_manager.h
index 574d0e2..f98102e 100644
--- a/runtime/oat_file_manager.h
+++ b/runtime/oat_file_manager.h
@@ -60,6 +60,11 @@
   const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location) const
       REQUIRES(!Locks::oat_file_manager_lock_);
 
+  // Find the oat file which contains a dex files with the given dex base location,
+  // returns null if there are none.
+  const OatFile* FindOpenedOatFileFromDexLocation(const std::string& dex_base_location) const
+      REQUIRES(!Locks::oat_file_manager_lock_);
+
   // Attempt to reserve a location, returns false if it is already reserved or already in used by
   // an oat file.
   bool RegisterOatFileLocation(const std::string& oat_location)