odrefresh: use "speed-profile" if a system server jar has a profile.

This change makes the odrefresh's behavior align with the change to the
build system (aosp/2141972)

Bug: 237399630
Test: atest art_standalone_odrefresh_tests
Change-Id: I56dea5582734efa460097afbd7ce1a4eb0e75f8d
diff --git a/odrefresh/odrefresh.cc b/odrefresh/odrefresh.cc
index f829bb8..272b8ef 100644
--- a/odrefresh/odrefresh.cc
+++ b/odrefresh/odrefresh.cc
@@ -407,7 +407,8 @@
   args.emplace_back(Concatenate({"--instruction-set=", isa_str}));
 }
 
-void AddDex2OatProfileAndCompilerFilter(
+// Returns true if any profile has been added.
+bool AddDex2OatProfile(
     /*inout*/ std::vector<std::string>& args,
     /*inout*/ std::vector<std::unique_ptr<File>>& output_files,
     const std::vector<std::string>& profile_paths) {
@@ -420,12 +421,7 @@
       has_any_profile = true;
     }
   }
-
-  if (has_any_profile) {
-    args.emplace_back("--compiler-filter=speed-profile");
-  } else {
-    args.emplace_back("--compiler-filter=speed");
-  }
+  return has_any_profile;
 }
 
 bool AddBootClasspathFds(/*inout*/ std::vector<std::string>& args,
@@ -1444,8 +1440,13 @@
   std::vector<std::unique_ptr<File>> readonly_files_raii;
   const std::string art_boot_profile_file = GetArtRoot() + "/etc/boot-image.prof";
   const std::string framework_boot_profile_file = GetAndroidRoot() + "/etc/boot-image.prof";
-  AddDex2OatProfileAndCompilerFilter(args, readonly_files_raii,
-                                     {art_boot_profile_file, framework_boot_profile_file});
+  bool has_any_profile = AddDex2OatProfile(
+      args, readonly_files_raii, {art_boot_profile_file, framework_boot_profile_file});
+  if (!has_any_profile) {
+    *error_msg = "Missing boot image profile";
+    return false;
+  }
+  args.emplace_back("--compiler-filter=speed-profile");
 
   // Compile as a single image for fewer files and slightly less memory overhead.
   args.emplace_back("--single-image");
@@ -1606,11 +1607,14 @@
 
     const std::string jar_name(android::base::Basename(jar));
     const std::string profile = Concatenate({GetAndroidRoot(), "/framework/", jar_name, ".prof"});
-    std::string compiler_filter = config_.GetSystemServerCompilerFilter();
-    if (compiler_filter == "speed-profile") {
-      AddDex2OatProfileAndCompilerFilter(args, readonly_files_raii, {profile});
-    } else {
+    bool has_any_profile = AddDex2OatProfile(args, readonly_files_raii, {profile});
+    const std::string& compiler_filter = config_.GetSystemServerCompilerFilter();
+    if (!compiler_filter.empty()) {
       args.emplace_back("--compiler-filter=" + compiler_filter);
+    } else if (has_any_profile) {
+      args.emplace_back("--compiler-filter=speed-profile");
+    } else {
+      args.emplace_back("--compiler-filter=speed");
     }
 
     const std::string image_location = GetSystemServerImagePath(/*on_system=*/false, jar);
diff --git a/odrefresh/odrefresh_main.cc b/odrefresh/odrefresh_main.cc
index b186f48..85557af 100644
--- a/odrefresh/odrefresh_main.cc
+++ b/odrefresh/odrefresh_main.cc
@@ -171,7 +171,7 @@
   config->SetZygoteKind(zygote_kind);
 
   if (config->GetSystemServerCompilerFilter().empty()) {
-    std::string filter = GetProperty("dalvik.vm.systemservercompilerfilter", "speed");
+    std::string filter = GetProperty("dalvik.vm.systemservercompilerfilter", "");
     config->SetSystemServerCompilerFilter(filter);
   }
 
diff --git a/odrefresh/odrefresh_test.cc b/odrefresh/odrefresh_test.cc
index ae7cc78..69564a5 100644
--- a/odrefresh/odrefresh_test.cc
+++ b/odrefresh/odrefresh_test.cc
@@ -202,7 +202,7 @@
     config_.SetStandaloneSystemServerJars(Concatenate({services_foo_jar_, ":", services_bar_jar_}));
     config_.SetIsa(InstructionSet::kX86_64);
     config_.SetZygoteKind(ZygoteKind::kZygote64_32);
-    config_.SetSystemServerCompilerFilter("speed");  // specify a default
+    config_.SetSystemServerCompilerFilter("");
     config_.SetArtifactDirectory(dalvik_cache_dir_);
 
     std::string staging_dir = dalvik_cache_dir_ + "/staging";
@@ -362,9 +362,11 @@
       ExitCode::kCompilationSuccess);
 }
 
-TEST_F(OdRefreshTest, CompileSetsCompilerFilterToSpeed) {
-  // Test setup: use "speed" compiler filter.
-  config_.SetSystemServerCompilerFilter("speed");
+// Test setup: The compiler filter is explicitly set to "speed-profile". Use it regardless of
+// whether the profile exists or not. Dex2oat will fall back to "verify" if the profile doesn't
+// exist.
+TEST_F(OdRefreshTest, CompileSetsCompilerFilterWithExplicitValue) {
+  config_.SetSystemServerCompilerFilter("speed-profile");
 
   // Uninteresting calls.
   EXPECT_CALL(
@@ -376,13 +378,12 @@
       *mock_exec_utils_,
       DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", location_provider_jar_})),
                                 Not(Contains(HasSubstr("--profile-file-fd="))),
-                                Contains("--compiler-filter=speed"))))
+                                Contains("--compiler-filter=speed-profile"))))
       .WillOnce(Return(0));
-  EXPECT_CALL(
-      *mock_exec_utils_,
-      DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", services_jar_})),
-                                Not(Contains(HasSubstr("--profile-file-fd="))),
-                                Contains("--compiler-filter=speed"))))
+  EXPECT_CALL(*mock_exec_utils_,
+              DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", services_jar_})),
+                                        Contains(HasSubstr("--profile-file-fd=")),
+                                        Contains("--compiler-filter=speed-profile"))))
       .WillOnce(Return(0));
   EXPECT_EQ(
       odrefresh_->Compile(*metrics_,
@@ -392,11 +393,9 @@
       ExitCode::kCompilationSuccess);
 }
 
-TEST_F(OdRefreshTest, CompileSetsCompilerFilterToSpeedProfile) {
-  // Test setup: with "speed-profile" compiler filter in the request, only apply if there is a
-  // profile, otherwise fallback to speed.
-  config_.SetSystemServerCompilerFilter("speed-profile");
-
+// Test setup: The compiler filter is not explicitly set. Use "speed-profile" if there is a profile,
+// otherwise fall back to "speed".
+TEST_F(OdRefreshTest, CompileSetsCompilerFilterWithDefaultValue) {
   // Uninteresting calls.
   EXPECT_CALL(
       *mock_exec_utils_, DoExecAndReturnCode(_))
@@ -424,36 +423,6 @@
       ExitCode::kCompilationSuccess);
 }
 
-TEST_F(OdRefreshTest, CompileSetsCompilerFilterToVerify) {
-  // Test setup: use "speed" compiler filter.
-  config_.SetSystemServerCompilerFilter("verify");
-
-  // Uninteresting calls.
-  EXPECT_CALL(
-      *mock_exec_utils_, DoExecAndReturnCode(_))
-      .Times(odrefresh_->AllSystemServerJars().size() - 2)
-      .WillRepeatedly(Return(0));
-
-  EXPECT_CALL(
-      *mock_exec_utils_,
-      DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", location_provider_jar_})),
-                                Not(Contains(HasSubstr("--profile-file-fd="))),
-                                Contains("--compiler-filter=verify"))))
-      .WillOnce(Return(0));
-  EXPECT_CALL(
-      *mock_exec_utils_,
-      DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", services_jar_})),
-                                Not(Contains(HasSubstr("--profile-file-fd="))),
-                                Contains("--compiler-filter=verify"))))
-      .WillOnce(Return(0));
-  EXPECT_EQ(
-      odrefresh_->Compile(*metrics_,
-                          CompilationOptions{
-                              .system_server_jars_to_compile = odrefresh_->AllSystemServerJars(),
-                          }),
-      ExitCode::kCompilationSuccess);
-}
-
 TEST_F(OdRefreshTest, OutputFilesAndIsa) {
   EXPECT_CALL(
       *mock_exec_utils_,