• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
GenerateBootImageProfile(const std::vector<std::unique_ptr<const DexFile>> & dex_files,const std::vector<std::unique_ptr<const ProfileCompilationInfo>> & profiles,const BootImageOptions & options,bool verbose,ProfileCompilationInfo * out_profile)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);
63           inferred_classes.emplace(profile.get(),
64                                    dex_file->GetMethodId(ref.dex_method_index).class_idx_);
65         }
66       }
67       // If the counter is greater or equal to the compile threshold, mark the method as hot.
68       // Note that all hot methods are also marked as hot in the out profile during the merging
69       // process.
70       if (counter >= options.compiled_method_threshold) {
71         Hotness hotness;
72         hotness.AddFlag(Hotness::kFlagHot);
73         out_profile->AddMethodHotness(ref, hotness);
74       }
75     }
76     // Walk all of the classes and add them to the profile if they meet the requirements.
77     for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
78       const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
79       TypeReference ref(dex_file.get(), class_def.class_idx_);
80       bool is_clean = true;
81       const uint8_t* class_data = dex_file->GetClassData(class_def);
82       if (class_data != nullptr) {
83         ClassDataItemIterator it(*dex_file, class_data);
84         while (it.HasNextStaticField()) {
85           const uint32_t flags = it.GetFieldAccessFlags();
86           if ((flags & kAccFinal) == 0) {
87             // Not final static field will probably dirty the class.
88             is_clean = false;
89             break;
90           }
91           it.Next();
92         }
93         it.SkipInstanceFields();
94         while (it.HasNextDirectMethod() || it.HasNextVirtualMethod()) {
95           const uint32_t flags = it.GetMethodAccessFlags();
96           if ((flags & kAccNative) != 0 || (flags & kAccFastNative) != 0) {
97             // Native method will get dirtied.
98             is_clean = false;
99             break;
100           }
101           if ((flags & kAccConstructor) != 0 && (flags & kAccStatic) != 0) {
102             // Class initializer, may get dirtied (not sure).
103             is_clean = false;
104             break;
105           }
106           it.Next();
107         }
108       }
109       ++(is_clean ? clean_count : dirty_count);
110       // This counter is how many profiles contain the class.
111       size_t counter = 0;
112       for (const std::unique_ptr<const ProfileCompilationInfo>& profile : profiles) {
113         auto it = inferred_classes.find(std::make_pair(profile.get(), ref.type_index));
114         if (it != inferred_classes.end() ||
115             profile->ContainsClass(*ref.dex_file, ref.type_index)) {
116           ++counter;
117         }
118       }
119       if (counter == 0) {
120         continue;
121       }
122       if (counter >= options.image_class_theshold) {
123         ++class_count;
124         out_profile->AddClassesForDex(ref.dex_file, &ref.type_index, &ref.type_index + 1);
125       } else if (is_clean && counter >= options.image_class_clean_theshold) {
126         ++clean_class_count;
127         out_profile->AddClassesForDex(ref.dex_file, &ref.type_index, &ref.type_index + 1);
128       }
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
139