Consolidate profile methods

Remove unnecessary profile APIs or the ones that were added only for tests.
This makes the profile data flow much easier to understand (as we have to follow
fewer entry points when adding methods and classes). It will also make it easier
to restructure the profile format with new data.

Test: m test-art-host-gtest
Change-Id: I09ea91229278877d16b12af7a0b356ccceb4b520
diff --git a/dexlayout/dexlayout_test.cc b/dexlayout/dexlayout_test.cc
index 391a548..c3abaa5 100644
--- a/dexlayout/dexlayout_test.cc
+++ b/dexlayout/dexlayout_test.cc
@@ -325,8 +325,7 @@
 
   // Create a profile with some subset of methods and classes.
   void CreateProfile(const std::string& input_dex,
-                     const std::string& out_profile,
-                     const std::string& dex_location) {
+                     const std::string& out_profile) {
     std::vector<std::unique_ptr<const DexFile>> dex_files;
     std::string error_msg;
     const ArtDexFileLoader dex_file_loader;
@@ -343,7 +342,6 @@
     size_t profile_methods = 0;
     size_t profile_classes = 0;
     ProfileCompilationInfo pfi;
-    std::set<DexCacheResolvedClasses> classes;
     for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
       for (uint32_t i = 0; i < dex_file->NumMethodIds(); i += 2) {
         uint8_t flags = 0u;
@@ -356,25 +354,23 @@
           ++profile_methods;
         }
         pfi.AddMethodIndex(static_cast<ProfileCompilationInfo::MethodHotness::Flag>(flags),
-                           dex_location,
+                           dex_file->GetLocation(),
                            dex_file->GetLocationChecksum(),
                            /*method_idx=*/i,
                            dex_file->NumMethodIds());
       }
-      DexCacheResolvedClasses cur_classes(dex_location,
-                                          dex_location,
-                                          dex_file->GetLocationChecksum(),
-                                          dex_file->NumMethodIds());
       // Add every even class too.
+      std::set<dex::TypeIndex> classes;
       for (uint32_t i = 0; i < dex_file->NumClassDefs(); i += 1) {
         if ((i & 2) == 0) {
-          cur_classes.AddClass(dex_file->GetClassDef(i).class_idx_);
+          classes.insert(dex::TypeIndex(dex_file->GetClassDef(i).class_idx_));
           ++profile_classes;
         }
       }
-      classes.insert(cur_classes);
+      if (!classes.empty()) {
+        pfi.AddClassesForDex(dex_file.get(), classes.begin(), classes.end());
+      }
     }
-    pfi.AddClasses(classes);
     // Write to provided file.
     std::unique_ptr<File> file(OS::CreateEmptyFile(out_profile.c_str()));
     ASSERT_TRUE(file != nullptr);
@@ -397,7 +393,7 @@
     std::string dex_file = tmp_dir + "classes.dex";
     WriteFileBase64(kDexFileLayoutInputDex, dex_file.c_str());
     std::string profile_file = tmp_dir + "primary.prof";
-    CreateProfile(dex_file, profile_file, dex_file);
+    CreateProfile(dex_file, profile_file);
     // WriteFileBase64(kDexFileLayoutInputProfile, profile_file.c_str());
     std::string output_dex = tmp_dir + "classes.dex.new";
 
@@ -439,7 +435,7 @@
     }
 
     std::string profile_file = tmp_dir + "primary.prof";
-    CreateProfile(dex_file, profile_file, dex_file);
+    CreateProfile(dex_file, profile_file);
     std::string output_dex = tmp_dir + "classes.dex.new";
     std::string second_output_dex = tmp_dir + "classes.dex.new.new";
 
@@ -452,8 +448,13 @@
 
     // Recreate the profile with the new dex location. This is required so that the profile dex
     // location matches.
-    CreateProfile(dex_file, profile_file, output_dex);
+    // For convenience we just copy the previous dex file to the new location so we can re-use it
+    // for profile generation.
 
+    // Don't check the output. The exec cmd wrongfully coplains that the cp cmd fails.
+    std::vector<std::string> cp_args = {"/usr/bin/cp", dex_file, output_dex};
+    art::Exec(cp_args, error_msg);
+    CreateProfile(output_dex, profile_file);
     // -v makes sure that the layout did not corrupt the dex file.
     // -i since the checksum won't match from the first layout.
     std::vector<std::string> second_dexlayout_args =
@@ -520,7 +521,7 @@
       EXPECT_EQ(dex_file->GetFile()->Flush(), 0);
     }
     if (profile_file != nullptr) {
-      CreateProfile(dex_file->GetFilename(), profile_file->GetFilename(), dex_file->GetFilename());
+      CreateProfile(dex_file->GetFilename(), profile_file->GetFilename());
     }
 
     std::string error_msg;