blob: db18fb3635492496a75852b90de45ba348f9bb17 [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"
Mathieu Chartier20f49922018-05-24 16:04:17 -070021#include "dex/class_accessor-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080022#include "dex/dex_file-inl.h"
David Sehr312f3b22018-03-19 08:39:26 -070023#include "dex/method_reference.h"
24#include "dex/type_reference.h"
David Sehr82d046e2018-04-23 08:14:19 -070025#include "profile/profile_compilation_info.h"
Mathieu Chartier2f794552017-06-19 10:58:08 -070026
27namespace art {
28
29using Hotness = ProfileCompilationInfo::MethodHotness;
30
31void 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 Gampe9b031f72018-10-04 11:03:34 -070041 out_profile->MergeWith(*profile, /*merge_classes=*/ false);
Mathieu Chartier2f794552017-06-19 10:58:08 -070042 }
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 Juravlea6c9b782019-09-16 18:57:26 -070064 out_profile->AddMethod(
65 ProfileMethodInfo(ref),
66 static_cast<ProfileCompilationInfo::MethodHotness::Flag>(hotness.GetFlags()));
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070067 inferred_classes.emplace(profile.get(), ref.GetMethodId().class_idx_);
Mathieu Chartier2f794552017-06-19 10:58:08 -070068 }
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 Juravlea6c9b782019-09-16 18:57:26 -070076 out_profile->AddMethod(
77 ProfileMethodInfo(ref),
78 static_cast<ProfileCompilationInfo::MethodHotness::Flag>(hotness.GetFlags()));
Mathieu Chartier2f794552017-06-19 10:58:08 -070079 }
80 }
81 // Walk all of the classes and add them to the profile if they meet the requirements.
Mathieu Chartier20f49922018-05-24 16:04:17 -070082 for (ClassAccessor accessor : dex_file->GetClasses()) {
83 TypeReference ref(dex_file.get(), accessor.GetClassIdx());
Mathieu Chartier2f794552017-06-19 10:58:08 -070084 bool is_clean = true;
Mathieu Chartier20f49922018-05-24 16:04:17 -070085 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 Chartier2f794552017-06-19 10:58:08 -070090 }
Mathieu Chartier20f49922018-05-24 16:04:17 -070091 if ((flags & kAccConstructor) != 0 && (flags & kAccStatic) != 0) {
92 // Class initializer, may get dirtied (not sure).
93 is_clean = false;
Mathieu Chartier2f794552017-06-19 10:58:08 -070094 }
Mathieu Chartier20f49922018-05-24 16:04:17 -070095 };
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 Gampe9b031f72018-10-04 11:03:34 -0700103 /*instance_field_visitor=*/ VoidFunctor(),
Mathieu Chartier20f49922018-05-24 16:04:17 -0700104 method_visitor,
105 method_visitor);
106
Mathieu Chartier2f794552017-06-19 10:58:08 -0700107 ++(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 Chartierfc8b4222017-09-17 13:44:24 -0700111 auto it = inferred_classes.find(std::make_pair(profile.get(), ref.TypeIndex()));
Mathieu Chartier2f794552017-06-19 10:58:08 -0700112 if (it != inferred_classes.end() ||
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700113 profile->ContainsClass(*ref.dex_file, ref.TypeIndex())) {
Mathieu Chartier2f794552017-06-19 10:58:08 -0700114 ++counter;
115 }
116 }
117 if (counter == 0) {
118 continue;
119 }
Calin Juravlea6c9b782019-09-16 18:57:26 -0700120 std::set<dex::TypeIndex> classes;
Mathieu Chartier2f794552017-06-19 10:58:08 -0700121 if (counter >= options.image_class_theshold) {
122 ++class_count;
Calin Juravlea6c9b782019-09-16 18:57:26 -0700123 classes.insert(ref.TypeIndex());
Mathieu Chartier2f794552017-06-19 10:58:08 -0700124 } else if (is_clean && counter >= options.image_class_clean_theshold) {
125 ++clean_class_count;
Calin Juravlea6c9b782019-09-16 18:57:26 -0700126 classes.insert(ref.TypeIndex());
Mathieu Chartier2f794552017-06-19 10:58:08 -0700127 }
Calin Juravlea6c9b782019-09-16 18:57:26 -0700128 out_profile->AddClassesForDex(ref.dex_file, classes.begin(), classes.end());
Mathieu Chartier2f794552017-06-19 10:58:08 -0700129 }
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