Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <memory> |
| 18 | #include <set> |
| 19 | |
| 20 | #include "boot_image_profile.h" |
| 21 | #include "dex_file-inl.h" |
| 22 | #include "method_reference.h" |
| 23 | #include "type_reference.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | using Hotness = ProfileCompilationInfo::MethodHotness; |
| 28 | |
| 29 | void GenerateBootImageProfile( |
| 30 | const std::vector<std::unique_ptr<const DexFile>>& dex_files, |
| 31 | const std::vector<std::unique_ptr<const ProfileCompilationInfo>>& profiles, |
| 32 | const BootImageOptions& options, |
| 33 | bool verbose, |
| 34 | ProfileCompilationInfo* out_profile) { |
| 35 | for (const std::unique_ptr<const ProfileCompilationInfo>& profile : profiles) { |
| 36 | // Avoid merging classes since we may want to only add classes that fit a certain criteria. |
| 37 | // If we merged the classes, every single class in each profile would be in the out_profile, |
| 38 | // but we want to only included classes that are in at least a few profiles. |
| 39 | out_profile->MergeWith(*profile, /*merge_classes*/ false); |
| 40 | } |
| 41 | |
| 42 | // Image classes that were added because they are commonly used. |
| 43 | size_t class_count = 0; |
| 44 | // Image classes that were only added because they were clean. |
| 45 | size_t clean_class_count = 0; |
| 46 | // Total clean classes. |
| 47 | size_t clean_count = 0; |
| 48 | // Total dirty classes. |
| 49 | size_t dirty_count = 0; |
| 50 | |
| 51 | for (const std::unique_ptr<const DexFile>& dex_file : dex_files) { |
| 52 | // Inferred classes are classes inferred from method samples. |
| 53 | std::set<std::pair<const ProfileCompilationInfo*, dex::TypeIndex>> inferred_classes; |
| 54 | for (size_t i = 0; i < dex_file->NumMethodIds(); ++i) { |
| 55 | MethodReference ref(dex_file.get(), i); |
| 56 | // This counter is how many profiles contain the method as sampled or hot. |
| 57 | size_t counter = 0; |
| 58 | for (const std::unique_ptr<const ProfileCompilationInfo>& profile : profiles) { |
| 59 | Hotness hotness = profile->GetMethodHotness(ref); |
| 60 | if (hotness.IsInProfile()) { |
| 61 | ++counter; |
| 62 | out_profile->AddMethodHotness(ref, hotness); |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 63 | inferred_classes.emplace(profile.get(), ref.GetMethodId().class_idx_); |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | // If the counter is greater or equal to the compile threshold, mark the method as hot. |
| 67 | // Note that all hot methods are also marked as hot in the out profile during the merging |
| 68 | // process. |
| 69 | if (counter >= options.compiled_method_threshold) { |
| 70 | Hotness hotness; |
| 71 | hotness.AddFlag(Hotness::kFlagHot); |
| 72 | out_profile->AddMethodHotness(ref, hotness); |
| 73 | } |
| 74 | } |
| 75 | // Walk all of the classes and add them to the profile if they meet the requirements. |
| 76 | for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) { |
| 77 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(i); |
| 78 | TypeReference ref(dex_file.get(), class_def.class_idx_); |
| 79 | bool is_clean = true; |
| 80 | const uint8_t* class_data = dex_file->GetClassData(class_def); |
| 81 | if (class_data != nullptr) { |
| 82 | ClassDataItemIterator it(*dex_file, class_data); |
| 83 | while (it.HasNextStaticField()) { |
| 84 | const uint32_t flags = it.GetFieldAccessFlags(); |
| 85 | if ((flags & kAccFinal) == 0) { |
| 86 | // Not final static field will probably dirty the class. |
| 87 | is_clean = false; |
| 88 | break; |
| 89 | } |
| 90 | it.Next(); |
| 91 | } |
| 92 | it.SkipInstanceFields(); |
| 93 | while (it.HasNextDirectMethod() || it.HasNextVirtualMethod()) { |
| 94 | const uint32_t flags = it.GetMethodAccessFlags(); |
| 95 | if ((flags & kAccNative) != 0 || (flags & kAccFastNative) != 0) { |
| 96 | // Native method will get dirtied. |
| 97 | is_clean = false; |
| 98 | break; |
| 99 | } |
| 100 | if ((flags & kAccConstructor) != 0 && (flags & kAccStatic) != 0) { |
| 101 | // Class initializer, may get dirtied (not sure). |
| 102 | is_clean = false; |
| 103 | break; |
| 104 | } |
| 105 | it.Next(); |
| 106 | } |
| 107 | } |
| 108 | ++(is_clean ? clean_count : dirty_count); |
| 109 | // This counter is how many profiles contain the class. |
| 110 | size_t counter = 0; |
| 111 | for (const std::unique_ptr<const ProfileCompilationInfo>& profile : profiles) { |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 112 | auto it = inferred_classes.find(std::make_pair(profile.get(), ref.TypeIndex())); |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 113 | if (it != inferred_classes.end() || |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 114 | profile->ContainsClass(*ref.dex_file, ref.TypeIndex())) { |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 115 | ++counter; |
| 116 | } |
| 117 | } |
| 118 | if (counter == 0) { |
| 119 | continue; |
| 120 | } |
| 121 | if (counter >= options.image_class_theshold) { |
| 122 | ++class_count; |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 123 | out_profile->AddClassForDex(ref); |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 124 | } else if (is_clean && counter >= options.image_class_clean_theshold) { |
| 125 | ++clean_class_count; |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 126 | out_profile->AddClassForDex(ref); |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | } |
| 130 | if (verbose) { |
| 131 | LOG(INFO) << "Image classes " << class_count + clean_class_count |
| 132 | << " added because clean " << clean_class_count |
| 133 | << " total clean " << clean_count << " total dirty " << dirty_count; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | } // namespace art |