blob: 60238e20828b332201f6e86fcc513a592f5b81ab [file] [log] [blame]
Mathieu Chartier2f794552017-06-19 10:58:08 -07001/*
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"
David Sehr9e734c72018-01-04 17:56:19 -080021#include "dex/dex_file-inl.h"
David Sehr312f3b22018-03-19 08:39:26 -070022#include "dex/method_reference.h"
23#include "dex/type_reference.h"
Andreas Gampe3913e482018-01-22 18:58:01 -080024#include "jit/profile_compilation_info.h"
Mathieu Chartier2f794552017-06-19 10:58:08 -070025
26namespace art {
27
28using Hotness = ProfileCompilationInfo::MethodHotness;
29
30void GenerateBootImageProfile(
31 const std::vector<std::unique_ptr<const DexFile>>& dex_files,
32 const std::vector<std::unique_ptr<const ProfileCompilationInfo>>& profiles,
33 const BootImageOptions& options,
34 bool verbose,
35 ProfileCompilationInfo* out_profile) {
36 for (const std::unique_ptr<const ProfileCompilationInfo>& profile : profiles) {
37 // Avoid merging classes since we may want to only add classes that fit a certain criteria.
38 // If we merged the classes, every single class in each profile would be in the out_profile,
39 // but we want to only included classes that are in at least a few profiles.
40 out_profile->MergeWith(*profile, /*merge_classes*/ false);
41 }
42
43 // Image classes that were added because they are commonly used.
44 size_t class_count = 0;
45 // Image classes that were only added because they were clean.
46 size_t clean_class_count = 0;
47 // Total clean classes.
48 size_t clean_count = 0;
49 // Total dirty classes.
50 size_t dirty_count = 0;
51
52 for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
53 // Inferred classes are classes inferred from method samples.
54 std::set<std::pair<const ProfileCompilationInfo*, dex::TypeIndex>> inferred_classes;
55 for (size_t i = 0; i < dex_file->NumMethodIds(); ++i) {
56 MethodReference ref(dex_file.get(), i);
57 // This counter is how many profiles contain the method as sampled or hot.
58 size_t counter = 0;
59 for (const std::unique_ptr<const ProfileCompilationInfo>& profile : profiles) {
60 Hotness hotness = profile->GetMethodHotness(ref);
61 if (hotness.IsInProfile()) {
62 ++counter;
63 out_profile->AddMethodHotness(ref, hotness);
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070064 inferred_classes.emplace(profile.get(), ref.GetMethodId().class_idx_);
Mathieu Chartier2f794552017-06-19 10:58:08 -070065 }
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();
Mathieu Chartierb7c273c2017-11-10 18:07:56 -080094 while (it.HasNextMethod()) {
Mathieu Chartier2f794552017-06-19 10:58:08 -070095 const uint32_t flags = it.GetMethodAccessFlags();
Vladimir Markob0a6aee2017-10-27 10:34:04 +010096 if ((flags & kAccNative) != 0) {
Mathieu Chartier2f794552017-06-19 10:58:08 -070097 // 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) {
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700113 auto it = inferred_classes.find(std::make_pair(profile.get(), ref.TypeIndex()));
Mathieu Chartier2f794552017-06-19 10:58:08 -0700114 if (it != inferred_classes.end() ||
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700115 profile->ContainsClass(*ref.dex_file, ref.TypeIndex())) {
Mathieu Chartier2f794552017-06-19 10:58:08 -0700116 ++counter;
117 }
118 }
119 if (counter == 0) {
120 continue;
121 }
122 if (counter >= options.image_class_theshold) {
123 ++class_count;
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700124 out_profile->AddClassForDex(ref);
Mathieu Chartier2f794552017-06-19 10:58:08 -0700125 } else if (is_clean && counter >= options.image_class_clean_theshold) {
126 ++clean_class_count;
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700127 out_profile->AddClassForDex(ref);
Mathieu Chartier2f794552017-06-19 10:58:08 -0700128 }
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