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" |
Mathieu Chartier | 20f4992 | 2018-05-24 16:04:17 -0700 | [diff] [blame] | 21 | #include "dex/class_accessor-inl.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 22 | #include "dex/dex_file-inl.h" |
David Sehr | 312f3b2 | 2018-03-19 08:39:26 -0700 | [diff] [blame] | 23 | #include "dex/method_reference.h" |
| 24 | #include "dex/type_reference.h" |
David Sehr | 82d046e | 2018-04-23 08:14:19 -0700 | [diff] [blame] | 25 | #include "profile/profile_compilation_info.h" |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | |
| 29 | using Hotness = ProfileCompilationInfo::MethodHotness; |
| 30 | |
| 31 | void GenerateBootImageProfile( |
| 32 | const std::vector<std::unique_ptr<const DexFile>>& dex_files, |
| 33 | const std::vector<std::unique_ptr<const ProfileCompilationInfo>>& profiles, |
| 34 | const BootImageOptions& options, |
| 35 | bool verbose, |
| 36 | ProfileCompilationInfo* out_profile) { |
| 37 | for (const std::unique_ptr<const ProfileCompilationInfo>& profile : profiles) { |
| 38 | // Avoid merging classes since we may want to only add classes that fit a certain criteria. |
| 39 | // If we merged the classes, every single class in each profile would be in the out_profile, |
| 40 | // but we want to only included classes that are in at least a few profiles. |
Andreas Gampe | 9b031f7 | 2018-10-04 11:03:34 -0700 | [diff] [blame] | 41 | out_profile->MergeWith(*profile, /*merge_classes=*/ false); |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | // Image classes that were added because they are commonly used. |
| 45 | size_t class_count = 0; |
| 46 | // Image classes that were only added because they were clean. |
| 47 | size_t clean_class_count = 0; |
| 48 | // Total clean classes. |
| 49 | size_t clean_count = 0; |
| 50 | // Total dirty classes. |
| 51 | size_t dirty_count = 0; |
| 52 | |
| 53 | for (const std::unique_ptr<const DexFile>& dex_file : dex_files) { |
| 54 | // Inferred classes are classes inferred from method samples. |
| 55 | std::set<std::pair<const ProfileCompilationInfo*, dex::TypeIndex>> inferred_classes; |
| 56 | for (size_t i = 0; i < dex_file->NumMethodIds(); ++i) { |
| 57 | MethodReference ref(dex_file.get(), i); |
| 58 | // This counter is how many profiles contain the method as sampled or hot. |
| 59 | size_t counter = 0; |
| 60 | for (const std::unique_ptr<const ProfileCompilationInfo>& profile : profiles) { |
| 61 | Hotness hotness = profile->GetMethodHotness(ref); |
| 62 | if (hotness.IsInProfile()) { |
| 63 | ++counter; |
Calin Juravle | a6c9b78 | 2019-09-16 18:57:26 -0700 | [diff] [blame] | 64 | out_profile->AddMethod( |
| 65 | ProfileMethodInfo(ref), |
| 66 | static_cast<ProfileCompilationInfo::MethodHotness::Flag>(hotness.GetFlags())); |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 67 | inferred_classes.emplace(profile.get(), ref.GetMethodId().class_idx_); |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | // If the counter is greater or equal to the compile threshold, mark the method as hot. |
| 71 | // Note that all hot methods are also marked as hot in the out profile during the merging |
| 72 | // process. |
| 73 | if (counter >= options.compiled_method_threshold) { |
| 74 | Hotness hotness; |
| 75 | hotness.AddFlag(Hotness::kFlagHot); |
Calin Juravle | a6c9b78 | 2019-09-16 18:57:26 -0700 | [diff] [blame] | 76 | out_profile->AddMethod( |
| 77 | ProfileMethodInfo(ref), |
| 78 | static_cast<ProfileCompilationInfo::MethodHotness::Flag>(hotness.GetFlags())); |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | // Walk all of the classes and add them to the profile if they meet the requirements. |
Mathieu Chartier | 20f4992 | 2018-05-24 16:04:17 -0700 | [diff] [blame] | 82 | for (ClassAccessor accessor : dex_file->GetClasses()) { |
| 83 | TypeReference ref(dex_file.get(), accessor.GetClassIdx()); |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 84 | bool is_clean = true; |
Mathieu Chartier | 20f4992 | 2018-05-24 16:04:17 -0700 | [diff] [blame] | 85 | auto method_visitor = [&](const ClassAccessor::Method& method) { |
| 86 | const uint32_t flags = method.GetAccessFlags(); |
| 87 | if ((flags & kAccNative) != 0) { |
| 88 | // Native method will get dirtied. |
| 89 | is_clean = false; |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 90 | } |
Mathieu Chartier | 20f4992 | 2018-05-24 16:04:17 -0700 | [diff] [blame] | 91 | if ((flags & kAccConstructor) != 0 && (flags & kAccStatic) != 0) { |
| 92 | // Class initializer, may get dirtied (not sure). |
| 93 | is_clean = false; |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 94 | } |
Mathieu Chartier | 20f4992 | 2018-05-24 16:04:17 -0700 | [diff] [blame] | 95 | }; |
| 96 | accessor.VisitFieldsAndMethods( |
| 97 | [&](const ClassAccessor::Field& field) { |
| 98 | if (!field.IsFinal()) { |
| 99 | // Not final static field will probably dirty the class. |
| 100 | is_clean = false; |
| 101 | } |
| 102 | }, |
Andreas Gampe | 9b031f7 | 2018-10-04 11:03:34 -0700 | [diff] [blame] | 103 | /*instance_field_visitor=*/ VoidFunctor(), |
Mathieu Chartier | 20f4992 | 2018-05-24 16:04:17 -0700 | [diff] [blame] | 104 | method_visitor, |
| 105 | method_visitor); |
| 106 | |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 107 | ++(is_clean ? clean_count : dirty_count); |
| 108 | // This counter is how many profiles contain the class. |
| 109 | size_t counter = 0; |
| 110 | for (const std::unique_ptr<const ProfileCompilationInfo>& profile : profiles) { |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 111 | auto it = inferred_classes.find(std::make_pair(profile.get(), ref.TypeIndex())); |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 112 | if (it != inferred_classes.end() || |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 113 | profile->ContainsClass(*ref.dex_file, ref.TypeIndex())) { |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 114 | ++counter; |
| 115 | } |
| 116 | } |
| 117 | if (counter == 0) { |
| 118 | continue; |
| 119 | } |
Calin Juravle | a6c9b78 | 2019-09-16 18:57:26 -0700 | [diff] [blame] | 120 | std::set<dex::TypeIndex> classes; |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 121 | if (counter >= options.image_class_theshold) { |
| 122 | ++class_count; |
Calin Juravle | a6c9b78 | 2019-09-16 18:57:26 -0700 | [diff] [blame] | 123 | classes.insert(ref.TypeIndex()); |
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; |
Calin Juravle | a6c9b78 | 2019-09-16 18:57:26 -0700 | [diff] [blame] | 126 | classes.insert(ref.TypeIndex()); |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 127 | } |
Calin Juravle | a6c9b78 | 2019-09-16 18:57:26 -0700 | [diff] [blame] | 128 | out_profile->AddClassesForDex(ref.dex_file, classes.begin(), classes.end()); |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | if (verbose) { |
| 132 | LOG(INFO) << "Image classes " << class_count + clean_class_count |
| 133 | << " added because clean " << clean_class_count |
| 134 | << " total clean " << clean_count << " total dirty " << dirty_count; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | } // namespace art |