Annotate boot profiles with the origin package names

When profiling the boot image the samples are not annotated with the
package name that generating them.

Bug: 139884006
Test: m test-art-host-gtest
Change-Id: Ie90765eea0b930660081d4e8a4ea2cda09f72524
diff --git a/runtime/jit/profile_saver.cc b/runtime/jit/profile_saver.cc
index 03d08a8..fc6ec45 100644
--- a/runtime/jit/profile_saver.cc
+++ b/runtime/jit/profile_saver.cc
@@ -471,7 +471,8 @@
             static_cast<Hotness::Flag>(flags),
             dex_file,
             indices.begin(),
-            indices.end());
+            indices.end(),
+            GetProfileSampleAnnotation());
       }
     }
     for (const auto& pair : sampled_methods.GetMap()) {
@@ -485,7 +486,8 @@
         cached_info->AddMethodsForDex(startup ? Hotness::kFlagStartup : Hotness::kFlagPostStartup,
                                       dex_file,
                                       indices.begin(),
-                                      indices.end());
+                                      indices.end(),
+                                      GetProfileSampleAnnotation());
       }
     }
     for (const auto& pair : resolved_classes.GetMap()) {
@@ -496,7 +498,10 @@
         VLOG(profiler) << "Added " << classes.size() << " classes for location "
                        << base_location
                        << " (" << dex_file->GetLocation() << ")";
-        cached_info->AddClassesForDex(dex_file, classes.begin(), classes.end());
+        cached_info->AddClassesForDex(dex_file,
+                                      classes.begin(),
+                                      classes.end(),
+                                      GetProfileSampleAnnotation());
       } else {
         VLOG(profiler) << "Location not found " << base_location;
       }
@@ -571,7 +576,8 @@
       // If this happens we clear the profile data and for the save to ensure the file is cleared.
       if (!info.AddMethods(
               profile_methods,
-              static_cast<Hotness::Flag>(Hotness::kFlagHot | Hotness::kFlagPostStartup))) {
+              static_cast<Hotness::Flag>(Hotness::kFlagHot | Hotness::kFlagPostStartup),
+              GetProfileSampleAnnotation())) {
         LOG(WARNING) << "Could not add methods to the existing profiler. "
             << "Clearing the profile data.";
         info.ClearData();
@@ -722,7 +728,7 @@
     std::set<std::string> code_paths_keys;
     for (const std::string& location : code_paths) {
       // Use the profile base key for checking file uniqueness (as it is constructed solely based
-      // on the location and ignores other metadata like architecture).
+      // on the location and ignores other metadata like origin package).
       code_paths_keys.insert(ProfileCompilationInfo::GetProfileDexFileBaseKey(location));
     }
     for (const DexFile* dex_file : runtime->GetClassLinker()->GetBootClassPath()) {
@@ -966,4 +972,19 @@
   }
 }
 
+ProfileCompilationInfo::ProfileSampleAnnotation ProfileSaver::GetProfileSampleAnnotation() {
+  // Ideally, this would be cached in the ProfileSaver class, when we start the thread.
+  // However the profile is initialized before the process package name is set and fixing this
+  // would require unnecessary complex synchronizations.
+  std::string package_name = Runtime::Current()->GetProcessPackageName();
+  if (package_name.empty()) {
+    package_name = "unknown";
+  }
+  // We only use annotation for the boot image profiles. Regular apps do not use the extra
+  // metadata and as such there is no need to pay the cost (storage and computational)
+  // that comes with the annotations.
+  return options_.GetProfileBootClassPath()
+      ? ProfileCompilationInfo::ProfileSampleAnnotation(package_name)
+      : ProfileCompilationInfo::ProfileSampleAnnotation::kNone;
+}
 }   // namespace art
diff --git a/runtime/jit/profile_saver.h b/runtime/jit/profile_saver.h
index 150aeec..5cd8bff 100644
--- a/runtime/jit/profile_saver.h
+++ b/runtime/jit/profile_saver.h
@@ -102,6 +102,10 @@
   // and put the result in tracked_dex_base_locations_.
   void ResolveTrackedLocations() REQUIRES(!Locks::profiler_lock_);
 
+  // Get the profile metadata that should be associated with the profile session during the current
+  // profile saver session.
+  ProfileCompilationInfo::ProfileSampleAnnotation GetProfileSampleAnnotation();
+
   // The only instance of the saver.
   static ProfileSaver* instance_ GUARDED_BY(Locks::profiler_lock_);
   // Profile saver thread.