1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include <errno.h>
18*795d594fSAndroid Build Coastguard Worker #include <stdio.h>
19*795d594fSAndroid Build Coastguard Worker #include <stdlib.h>
20*795d594fSAndroid Build Coastguard Worker #include <sys/file.h>
21*795d594fSAndroid Build Coastguard Worker #include <sys/param.h>
22*795d594fSAndroid Build Coastguard Worker #include <unistd.h>
23*795d594fSAndroid Build Coastguard Worker
24*795d594fSAndroid Build Coastguard Worker #include <cstdint>
25*795d594fSAndroid Build Coastguard Worker #include <fstream>
26*795d594fSAndroid Build Coastguard Worker #include <iostream>
27*795d594fSAndroid Build Coastguard Worker #include <optional>
28*795d594fSAndroid Build Coastguard Worker #include <ostream>
29*795d594fSAndroid Build Coastguard Worker #include <set>
30*795d594fSAndroid Build Coastguard Worker #include <string>
31*795d594fSAndroid Build Coastguard Worker #include <string_view>
32*795d594fSAndroid Build Coastguard Worker #include <tuple>
33*795d594fSAndroid Build Coastguard Worker #include <unordered_set>
34*795d594fSAndroid Build Coastguard Worker #include <vector>
35*795d594fSAndroid Build Coastguard Worker
36*795d594fSAndroid Build Coastguard Worker #include "android-base/parsebool.h"
37*795d594fSAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
38*795d594fSAndroid Build Coastguard Worker #include "android-base/strings.h"
39*795d594fSAndroid Build Coastguard Worker #include "base/array_ref.h"
40*795d594fSAndroid Build Coastguard Worker #include "base/dumpable.h"
41*795d594fSAndroid Build Coastguard Worker #include "base/logging.h" // For InitLogging.
42*795d594fSAndroid Build Coastguard Worker #include "base/mem_map.h"
43*795d594fSAndroid Build Coastguard Worker #include "base/scoped_flock.h"
44*795d594fSAndroid Build Coastguard Worker #include "base/stl_util.h"
45*795d594fSAndroid Build Coastguard Worker #include "base/time_utils.h"
46*795d594fSAndroid Build Coastguard Worker #include "base/unix_file/fd_file.h"
47*795d594fSAndroid Build Coastguard Worker #include "base/utils.h"
48*795d594fSAndroid Build Coastguard Worker #include "base/zip_archive.h"
49*795d594fSAndroid Build Coastguard Worker #include "boot_image_profile.h"
50*795d594fSAndroid Build Coastguard Worker #include "dex/art_dex_file_loader.h"
51*795d594fSAndroid Build Coastguard Worker #include "dex/bytecode_utils.h"
52*795d594fSAndroid Build Coastguard Worker #include "dex/class_accessor-inl.h"
53*795d594fSAndroid Build Coastguard Worker #include "dex/class_reference.h"
54*795d594fSAndroid Build Coastguard Worker #include "dex/code_item_accessors-inl.h"
55*795d594fSAndroid Build Coastguard Worker #include "dex/descriptors_names.h"
56*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file.h"
57*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file_loader.h"
58*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file_structs.h"
59*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file_types.h"
60*795d594fSAndroid Build Coastguard Worker #include "dex/method_reference.h"
61*795d594fSAndroid Build Coastguard Worker #include "dex/type_reference.h"
62*795d594fSAndroid Build Coastguard Worker #include "profile/profile_boot_info.h"
63*795d594fSAndroid Build Coastguard Worker #include "profile/profile_compilation_info.h"
64*795d594fSAndroid Build Coastguard Worker #include "profile_assistant.h"
65*795d594fSAndroid Build Coastguard Worker #include "profman/profman_result.h"
66*795d594fSAndroid Build Coastguard Worker #include "inline_cache_format_util.h"
67*795d594fSAndroid Build Coastguard Worker
68*795d594fSAndroid Build Coastguard Worker namespace art {
69*795d594fSAndroid Build Coastguard Worker
70*795d594fSAndroid Build Coastguard Worker using ProfileSampleAnnotation = ProfileCompilationInfo::ProfileSampleAnnotation;
71*795d594fSAndroid Build Coastguard Worker
72*795d594fSAndroid Build Coastguard Worker static int original_argc;
73*795d594fSAndroid Build Coastguard Worker static char** original_argv;
74*795d594fSAndroid Build Coastguard Worker
CommandLine()75*795d594fSAndroid Build Coastguard Worker static std::string CommandLine() {
76*795d594fSAndroid Build Coastguard Worker std::vector<std::string> command;
77*795d594fSAndroid Build Coastguard Worker command.reserve(original_argc);
78*795d594fSAndroid Build Coastguard Worker for (int i = 0; i < original_argc; ++i) {
79*795d594fSAndroid Build Coastguard Worker command.push_back(original_argv[i]);
80*795d594fSAndroid Build Coastguard Worker }
81*795d594fSAndroid Build Coastguard Worker return android::base::Join(command, ' ');
82*795d594fSAndroid Build Coastguard Worker }
83*795d594fSAndroid Build Coastguard Worker
FdIsValid(int fd)84*795d594fSAndroid Build Coastguard Worker static bool FdIsValid(int fd) {
85*795d594fSAndroid Build Coastguard Worker return fd != File::kInvalidFd;
86*795d594fSAndroid Build Coastguard Worker }
87*795d594fSAndroid Build Coastguard Worker
UsageErrorV(const char * fmt,va_list ap)88*795d594fSAndroid Build Coastguard Worker static void UsageErrorV(const char* fmt, va_list ap) {
89*795d594fSAndroid Build Coastguard Worker std::string error;
90*795d594fSAndroid Build Coastguard Worker android::base::StringAppendV(&error, fmt, ap);
91*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << error;
92*795d594fSAndroid Build Coastguard Worker }
93*795d594fSAndroid Build Coastguard Worker
UsageError(const char * fmt,...)94*795d594fSAndroid Build Coastguard Worker static void UsageError(const char* fmt, ...) {
95*795d594fSAndroid Build Coastguard Worker va_list ap;
96*795d594fSAndroid Build Coastguard Worker va_start(ap, fmt);
97*795d594fSAndroid Build Coastguard Worker UsageErrorV(fmt, ap);
98*795d594fSAndroid Build Coastguard Worker va_end(ap);
99*795d594fSAndroid Build Coastguard Worker }
100*795d594fSAndroid Build Coastguard Worker
Usage(const char * fmt,...)101*795d594fSAndroid Build Coastguard Worker NO_RETURN static void Usage(const char *fmt, ...) {
102*795d594fSAndroid Build Coastguard Worker va_list ap;
103*795d594fSAndroid Build Coastguard Worker va_start(ap, fmt);
104*795d594fSAndroid Build Coastguard Worker UsageErrorV(fmt, ap);
105*795d594fSAndroid Build Coastguard Worker va_end(ap);
106*795d594fSAndroid Build Coastguard Worker
107*795d594fSAndroid Build Coastguard Worker UsageError("Command: %s", CommandLine().c_str());
108*795d594fSAndroid Build Coastguard Worker UsageError("Usage: profman [options]...");
109*795d594fSAndroid Build Coastguard Worker UsageError("");
110*795d594fSAndroid Build Coastguard Worker UsageError(" --dump-only: dumps the content of the specified profile files");
111*795d594fSAndroid Build Coastguard Worker UsageError(" to standard output (default) in a human readable form.");
112*795d594fSAndroid Build Coastguard Worker UsageError("");
113*795d594fSAndroid Build Coastguard Worker UsageError(" --dump-output-to-fd=<number>: redirects --dump-only output to a file descriptor.");
114*795d594fSAndroid Build Coastguard Worker UsageError("");
115*795d594fSAndroid Build Coastguard Worker UsageError(" --dump-classes-and-methods: dumps a sorted list of classes and methods that are");
116*795d594fSAndroid Build Coastguard Worker UsageError(" in the specified profile file to standard output (default) in a human");
117*795d594fSAndroid Build Coastguard Worker UsageError(" readable form. The output is valid input for --create-profile-from");
118*795d594fSAndroid Build Coastguard Worker UsageError("");
119*795d594fSAndroid Build Coastguard Worker UsageError(" --profile-file=<filename>: specify profiler output file to use for compilation.");
120*795d594fSAndroid Build Coastguard Worker UsageError(" Can be specified multiple time, in which case the data from the different");
121*795d594fSAndroid Build Coastguard Worker UsageError(" profiles will be aggregated. Can also be specified zero times, in which case");
122*795d594fSAndroid Build Coastguard Worker UsageError(" profman will still analyze the reference profile against the given --apk and");
123*795d594fSAndroid Build Coastguard Worker UsageError(" return exit code based on whether the reference profile is empty and whether");
124*795d594fSAndroid Build Coastguard Worker UsageError(" an error occurs, but no merge will happen.");
125*795d594fSAndroid Build Coastguard Worker UsageError("");
126*795d594fSAndroid Build Coastguard Worker UsageError(" --profile-file-fd=<number>: same as --profile-file but accepts a file descriptor.");
127*795d594fSAndroid Build Coastguard Worker UsageError(" Cannot be used together with --profile-file.");
128*795d594fSAndroid Build Coastguard Worker UsageError("");
129*795d594fSAndroid Build Coastguard Worker UsageError(" --reference-profile-file=<filename>: specify a reference profile.");
130*795d594fSAndroid Build Coastguard Worker UsageError(" The data in this file will be compared with the data obtained by merging");
131*795d594fSAndroid Build Coastguard Worker UsageError(" all the files specified with --profile-file or --profile-file-fd.");
132*795d594fSAndroid Build Coastguard Worker UsageError(" If the exit code is ProfmanResult::kCompile then all --profile-file will be");
133*795d594fSAndroid Build Coastguard Worker UsageError(" merged into --reference-profile-file. ");
134*795d594fSAndroid Build Coastguard Worker UsageError("");
135*795d594fSAndroid Build Coastguard Worker UsageError(" --reference-profile-file-fd=<number>: same as --reference-profile-file but");
136*795d594fSAndroid Build Coastguard Worker UsageError(" accepts a file descriptor. Cannot be used together with");
137*795d594fSAndroid Build Coastguard Worker UsageError(" --reference-profile-file.");
138*795d594fSAndroid Build Coastguard Worker UsageError("");
139*795d594fSAndroid Build Coastguard Worker UsageError(" --generate-test-profile=<filename>: generates a random profile file for testing.");
140*795d594fSAndroid Build Coastguard Worker UsageError(" --generate-test-profile-num-dex=<number>: number of dex files that should be");
141*795d594fSAndroid Build Coastguard Worker UsageError(" included in the generated profile. Defaults to 20.");
142*795d594fSAndroid Build Coastguard Worker UsageError(" --generate-test-profile-method-percentage=<number>: the percentage from the maximum");
143*795d594fSAndroid Build Coastguard Worker UsageError(" number of methods that should be generated. Defaults to 5.");
144*795d594fSAndroid Build Coastguard Worker UsageError(" --generate-test-profile-class-percentage=<number>: the percentage from the maximum");
145*795d594fSAndroid Build Coastguard Worker UsageError(" number of classes that should be generated. Defaults to 5.");
146*795d594fSAndroid Build Coastguard Worker UsageError(" --generate-test-profile-seed=<number>: seed for random number generator used when");
147*795d594fSAndroid Build Coastguard Worker UsageError(" generating random test profiles. Defaults to using NanoTime.");
148*795d594fSAndroid Build Coastguard Worker UsageError("");
149*795d594fSAndroid Build Coastguard Worker UsageError(" --create-profile-from=<filename>: creates a profile from a list of classes,");
150*795d594fSAndroid Build Coastguard Worker UsageError(" methods and inline caches.");
151*795d594fSAndroid Build Coastguard Worker UsageError(" --output-profile-type=(app|boot|bprof): Select output profile format for");
152*795d594fSAndroid Build Coastguard Worker UsageError(" the --create-profile-from option. Default: app.");
153*795d594fSAndroid Build Coastguard Worker UsageError("");
154*795d594fSAndroid Build Coastguard Worker UsageError(" --dex-location=<string>: location string to use with corresponding");
155*795d594fSAndroid Build Coastguard Worker UsageError(" apk-fd to find dex files");
156*795d594fSAndroid Build Coastguard Worker UsageError("");
157*795d594fSAndroid Build Coastguard Worker UsageError(" --apk-fd=<number>: file descriptor containing an open APK to");
158*795d594fSAndroid Build Coastguard Worker UsageError(" search for dex files");
159*795d594fSAndroid Build Coastguard Worker UsageError(" --apk=<filename>: an APK to search for dex files");
160*795d594fSAndroid Build Coastguard Worker UsageError(" --skip-apk-verification: do not attempt to verify APKs");
161*795d594fSAndroid Build Coastguard Worker UsageError("");
162*795d594fSAndroid Build Coastguard Worker UsageError(" --generate-boot-image-profile: Generate a boot image profile based on input");
163*795d594fSAndroid Build Coastguard Worker UsageError(" profiles. Requires passing in dex files to inspect properties of classes.");
164*795d594fSAndroid Build Coastguard Worker UsageError(" --method-threshold=percentage between 0 and 100");
165*795d594fSAndroid Build Coastguard Worker UsageError(" what threshold to apply to the methods when deciding whether or not to");
166*795d594fSAndroid Build Coastguard Worker UsageError(" include it in the final profile.");
167*795d594fSAndroid Build Coastguard Worker UsageError(" --class-threshold=percentage between 0 and 100");
168*795d594fSAndroid Build Coastguard Worker UsageError(" what threshold to apply to the classes when deciding whether or not to");
169*795d594fSAndroid Build Coastguard Worker UsageError(" include it in the final profile.");
170*795d594fSAndroid Build Coastguard Worker UsageError(" --clean-class-threshold=percentage between 0 and 100");
171*795d594fSAndroid Build Coastguard Worker UsageError(" what threshold to apply to the clean classes when deciding whether or not to");
172*795d594fSAndroid Build Coastguard Worker UsageError(" include it in the final profile.");
173*795d594fSAndroid Build Coastguard Worker UsageError(" --preloaded-class-threshold=percentage between 0 and 100");
174*795d594fSAndroid Build Coastguard Worker UsageError(" what threshold to apply to the classes when deciding whether or not to");
175*795d594fSAndroid Build Coastguard Worker UsageError(" include it in the final preloaded classes.");
176*795d594fSAndroid Build Coastguard Worker UsageError(" --preloaded-classes-denylist=file");
177*795d594fSAndroid Build Coastguard Worker UsageError(" a file listing the classes that should not be preloaded in Zygote");
178*795d594fSAndroid Build Coastguard Worker UsageError(" --upgrade-startup-to-hot=true|false:");
179*795d594fSAndroid Build Coastguard Worker UsageError(" whether or not to upgrade startup methods to hot");
180*795d594fSAndroid Build Coastguard Worker UsageError(" --special-package=pkg_name:percentage between 0 and 100");
181*795d594fSAndroid Build Coastguard Worker UsageError(" what threshold to apply to the methods/classes that are used by the given");
182*795d594fSAndroid Build Coastguard Worker UsageError(" package when deciding whether or not to include it in the final profile.");
183*795d594fSAndroid Build Coastguard Worker UsageError(" --debug-append-uses=bool: whether or not to append package use as debug info.");
184*795d594fSAndroid Build Coastguard Worker UsageError(" --out-profile-path=path: boot image profile output path");
185*795d594fSAndroid Build Coastguard Worker UsageError(" --out-preloaded-classes-path=path: preloaded classes output path");
186*795d594fSAndroid Build Coastguard Worker UsageError(" --copy-and-update-profile-key: if present, profman will copy the profile from");
187*795d594fSAndroid Build Coastguard Worker UsageError(" the file passed with --profile-fd(file) to the profile passed with");
188*795d594fSAndroid Build Coastguard Worker UsageError(" --reference-profile-fd(file) and update at the same time the profile-key");
189*795d594fSAndroid Build Coastguard Worker UsageError(" of entries corresponding to the apks passed with --apk(-fd).");
190*795d594fSAndroid Build Coastguard Worker UsageError(" --boot-image-merge: indicates that this merge is for a boot image profile.");
191*795d594fSAndroid Build Coastguard Worker UsageError(" In this case, the reference profile must have a boot profile version.");
192*795d594fSAndroid Build Coastguard Worker UsageError(" --force-merge: performs a forced merge, without analyzing if there is a");
193*795d594fSAndroid Build Coastguard Worker UsageError(" significant difference between before and after the merge.");
194*795d594fSAndroid Build Coastguard Worker UsageError(" Deprecated. Use --force-merge-and-analyze instead.");
195*795d594fSAndroid Build Coastguard Worker UsageError(" --force-merge-and-analyze: performs a forced merge and analyzes if there is any");
196*795d594fSAndroid Build Coastguard Worker UsageError(" difference between before and after the merge.");
197*795d594fSAndroid Build Coastguard Worker UsageError(" --min-new-methods-percent-change=percentage between 0 and 100 (default 2)");
198*795d594fSAndroid Build Coastguard Worker UsageError(" the min percent of new methods to trigger a compilation.");
199*795d594fSAndroid Build Coastguard Worker UsageError(" --min-new-classes-percent-change=percentage between 0 and 100 (default 2)");
200*795d594fSAndroid Build Coastguard Worker UsageError(" the min percent of new classes to trigger a compilation.");
201*795d594fSAndroid Build Coastguard Worker UsageError("");
202*795d594fSAndroid Build Coastguard Worker
203*795d594fSAndroid Build Coastguard Worker exit(ProfmanResult::kErrorUsage);
204*795d594fSAndroid Build Coastguard Worker }
205*795d594fSAndroid Build Coastguard Worker
206*795d594fSAndroid Build Coastguard Worker // Note: make sure you update the Usage if you change these values.
207*795d594fSAndroid Build Coastguard Worker static constexpr uint16_t kDefaultTestProfileNumDex = 20;
208*795d594fSAndroid Build Coastguard Worker static constexpr uint16_t kDefaultTestProfileMethodPercentage = 5;
209*795d594fSAndroid Build Coastguard Worker static constexpr uint16_t kDefaultTestProfileClassPercentage = 5;
210*795d594fSAndroid Build Coastguard Worker
211*795d594fSAndroid Build Coastguard Worker // Separators used when parsing human friendly representation of profiles.
212*795d594fSAndroid Build Coastguard Worker static const std::string kMethodSep = "->"; // NOLINT [runtime/string] [4]
213*795d594fSAndroid Build Coastguard Worker static const std::string kClassAllMethods = "*"; // NOLINT [runtime/string] [4]
214*795d594fSAndroid Build Coastguard Worker static constexpr char kAnnotationStart = '{';
215*795d594fSAndroid Build Coastguard Worker static constexpr char kAnnotationEnd = '}';
216*795d594fSAndroid Build Coastguard Worker static constexpr char kProfileParsingFirstCharInSignature = '(';
217*795d594fSAndroid Build Coastguard Worker static constexpr char kMethodFlagStringHot = 'H';
218*795d594fSAndroid Build Coastguard Worker static constexpr char kMethodFlagStringStartup = 'S';
219*795d594fSAndroid Build Coastguard Worker static constexpr char kMethodFlagStringPostStartup = 'P';
220*795d594fSAndroid Build Coastguard Worker
Abort(const char * msg)221*795d594fSAndroid Build Coastguard Worker NO_RETURN static void Abort(const char* msg) {
222*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << msg;
223*795d594fSAndroid Build Coastguard Worker exit(1);
224*795d594fSAndroid Build Coastguard Worker }
225*795d594fSAndroid Build Coastguard Worker template <typename T>
ParseUintValue(const std::string & option_name,const std::string & value,T * out,T min=std::numeric_limits<T>::min (),T max=std::numeric_limits<T>::max ())226*795d594fSAndroid Build Coastguard Worker static void ParseUintValue(const std::string& option_name,
227*795d594fSAndroid Build Coastguard Worker const std::string& value,
228*795d594fSAndroid Build Coastguard Worker T* out,
229*795d594fSAndroid Build Coastguard Worker T min = std::numeric_limits<T>::min(),
230*795d594fSAndroid Build Coastguard Worker T max = std::numeric_limits<T>::max()) {
231*795d594fSAndroid Build Coastguard Worker int64_t parsed_integer_value = 0;
232*795d594fSAndroid Build Coastguard Worker if (!android::base::ParseInt(
233*795d594fSAndroid Build Coastguard Worker value,
234*795d594fSAndroid Build Coastguard Worker &parsed_integer_value,
235*795d594fSAndroid Build Coastguard Worker static_cast<int64_t>(min),
236*795d594fSAndroid Build Coastguard Worker static_cast<int64_t>(max))) {
237*795d594fSAndroid Build Coastguard Worker Usage("Failed to parse %s '%s' as an integer", option_name.c_str(), value.c_str());
238*795d594fSAndroid Build Coastguard Worker }
239*795d594fSAndroid Build Coastguard Worker if (parsed_integer_value < 0) {
240*795d594fSAndroid Build Coastguard Worker Usage("%s passed a negative value %" PRId64, option_name.c_str(), parsed_integer_value);
241*795d594fSAndroid Build Coastguard Worker }
242*795d594fSAndroid Build Coastguard Worker if (static_cast<uint64_t>(parsed_integer_value) >
243*795d594fSAndroid Build Coastguard Worker static_cast<std::make_unsigned_t<T>>(std::numeric_limits<T>::max())) {
244*795d594fSAndroid Build Coastguard Worker Usage("%s passed a value %" PRIu64 " above max (%" PRIu64 ")",
245*795d594fSAndroid Build Coastguard Worker option_name.c_str(),
246*795d594fSAndroid Build Coastguard Worker static_cast<uint64_t>(parsed_integer_value),
247*795d594fSAndroid Build Coastguard Worker static_cast<uint64_t>(std::numeric_limits<T>::max()));
248*795d594fSAndroid Build Coastguard Worker }
249*795d594fSAndroid Build Coastguard Worker *out = dchecked_integral_cast<T>(parsed_integer_value);
250*795d594fSAndroid Build Coastguard Worker }
251*795d594fSAndroid Build Coastguard Worker
252*795d594fSAndroid Build Coastguard Worker template <typename T>
ParseUintOption(const char * raw_option,std::string_view option_prefix,T * out,T min=std::numeric_limits<T>::min (),T max=std::numeric_limits<T>::max ())253*795d594fSAndroid Build Coastguard Worker static void ParseUintOption(const char* raw_option,
254*795d594fSAndroid Build Coastguard Worker std::string_view option_prefix,
255*795d594fSAndroid Build Coastguard Worker T* out,
256*795d594fSAndroid Build Coastguard Worker T min = std::numeric_limits<T>::min(),
257*795d594fSAndroid Build Coastguard Worker T max = std::numeric_limits<T>::max()) {
258*795d594fSAndroid Build Coastguard Worker DCHECK(option_prefix.ends_with("="));
259*795d594fSAndroid Build Coastguard Worker DCHECK(std::string_view(raw_option).starts_with(option_prefix))
260*795d594fSAndroid Build Coastguard Worker << raw_option << " " << option_prefix;
261*795d594fSAndroid Build Coastguard Worker std::string option_name(option_prefix.substr(option_prefix.size() - 1u));
262*795d594fSAndroid Build Coastguard Worker const char* value_string = raw_option + option_prefix.size();
263*795d594fSAndroid Build Coastguard Worker
264*795d594fSAndroid Build Coastguard Worker ParseUintValue(option_name, value_string, out, min, max);
265*795d594fSAndroid Build Coastguard Worker }
266*795d594fSAndroid Build Coastguard Worker
ParseBoolOption(std::string_view option,std::string_view option_prefix,bool * out)267*795d594fSAndroid Build Coastguard Worker static void ParseBoolOption(std::string_view option,
268*795d594fSAndroid Build Coastguard Worker std::string_view option_prefix,
269*795d594fSAndroid Build Coastguard Worker bool* out) {
270*795d594fSAndroid Build Coastguard Worker DCHECK(option_prefix.ends_with("="));
271*795d594fSAndroid Build Coastguard Worker DCHECK(option.starts_with(option_prefix)) << option << " " << option_prefix;
272*795d594fSAndroid Build Coastguard Worker const std::string_view value_string = option.substr(option_prefix.size());
273*795d594fSAndroid Build Coastguard Worker android::base::ParseBoolResult result = android::base::ParseBool(value_string);
274*795d594fSAndroid Build Coastguard Worker if (result == android::base::ParseBoolResult::kError) {
275*795d594fSAndroid Build Coastguard Worker std::string option_name(option_prefix.substr(option_prefix.size() - 1u));
276*795d594fSAndroid Build Coastguard Worker Usage("Failed to parse %s '%s' as an integer", option_name.c_str(), value_string);
277*795d594fSAndroid Build Coastguard Worker }
278*795d594fSAndroid Build Coastguard Worker
279*795d594fSAndroid Build Coastguard Worker *out = result == android::base::ParseBoolResult::kTrue;
280*795d594fSAndroid Build Coastguard Worker }
281*795d594fSAndroid Build Coastguard Worker
282*795d594fSAndroid Build Coastguard Worker enum class OutputProfileType {
283*795d594fSAndroid Build Coastguard Worker kApp,
284*795d594fSAndroid Build Coastguard Worker kBoot,
285*795d594fSAndroid Build Coastguard Worker kBprof,
286*795d594fSAndroid Build Coastguard Worker };
287*795d594fSAndroid Build Coastguard Worker
ParseOutputProfileType(std::string_view option,std::string_view option_prefix,OutputProfileType * out)288*795d594fSAndroid Build Coastguard Worker static void ParseOutputProfileType(std::string_view option,
289*795d594fSAndroid Build Coastguard Worker std::string_view option_prefix,
290*795d594fSAndroid Build Coastguard Worker OutputProfileType* out) {
291*795d594fSAndroid Build Coastguard Worker DCHECK(option_prefix.ends_with("="));
292*795d594fSAndroid Build Coastguard Worker DCHECK(option.starts_with(option_prefix)) << option << " " << option_prefix;
293*795d594fSAndroid Build Coastguard Worker const std::string_view value_string = option.substr(option_prefix.size());
294*795d594fSAndroid Build Coastguard Worker if (value_string == "app") {
295*795d594fSAndroid Build Coastguard Worker *out = OutputProfileType::kApp;
296*795d594fSAndroid Build Coastguard Worker } else if (value_string == "boot") {
297*795d594fSAndroid Build Coastguard Worker *out = OutputProfileType::kBoot;
298*795d594fSAndroid Build Coastguard Worker } else if (value_string == "bprof") {
299*795d594fSAndroid Build Coastguard Worker *out = OutputProfileType::kBprof;
300*795d594fSAndroid Build Coastguard Worker } else {
301*795d594fSAndroid Build Coastguard Worker std::string option_name(option_prefix.substr(option_prefix.size() - 1u));
302*795d594fSAndroid Build Coastguard Worker Usage("Failed to parse %s '%s' as (app|boot|bprof)", option_name.c_str(), value_string);
303*795d594fSAndroid Build Coastguard Worker }
304*795d594fSAndroid Build Coastguard Worker }
305*795d594fSAndroid Build Coastguard Worker
306*795d594fSAndroid Build Coastguard Worker // TODO(calin): This class has grown too much from its initial design. Split the functionality
307*795d594fSAndroid Build Coastguard Worker // into smaller, more contained pieces.
308*795d594fSAndroid Build Coastguard Worker class ProfMan final {
309*795d594fSAndroid Build Coastguard Worker public:
ProfMan()310*795d594fSAndroid Build Coastguard Worker ProfMan() :
311*795d594fSAndroid Build Coastguard Worker reference_profile_file_fd_(File::kInvalidFd),
312*795d594fSAndroid Build Coastguard Worker dump_only_(false),
313*795d594fSAndroid Build Coastguard Worker dump_classes_and_methods_(false),
314*795d594fSAndroid Build Coastguard Worker generate_boot_image_profile_(false),
315*795d594fSAndroid Build Coastguard Worker output_profile_type_(OutputProfileType::kApp),
316*795d594fSAndroid Build Coastguard Worker dump_output_to_fd_(File::kInvalidFd),
317*795d594fSAndroid Build Coastguard Worker test_profile_num_dex_(kDefaultTestProfileNumDex),
318*795d594fSAndroid Build Coastguard Worker test_profile_method_percerntage_(kDefaultTestProfileMethodPercentage),
319*795d594fSAndroid Build Coastguard Worker test_profile_class_percentage_(kDefaultTestProfileClassPercentage),
320*795d594fSAndroid Build Coastguard Worker test_profile_seed_(NanoTime()),
321*795d594fSAndroid Build Coastguard Worker start_ns_(NanoTime()),
322*795d594fSAndroid Build Coastguard Worker copy_and_update_profile_key_(false),
323*795d594fSAndroid Build Coastguard Worker profile_assistant_options_(ProfileAssistant::Options()) {}
324*795d594fSAndroid Build Coastguard Worker
~ProfMan()325*795d594fSAndroid Build Coastguard Worker ~ProfMan() {
326*795d594fSAndroid Build Coastguard Worker LogCompletionTime();
327*795d594fSAndroid Build Coastguard Worker }
328*795d594fSAndroid Build Coastguard Worker
ParseArgs(int argc,char ** argv)329*795d594fSAndroid Build Coastguard Worker void ParseArgs(int argc, char **argv) {
330*795d594fSAndroid Build Coastguard Worker original_argc = argc;
331*795d594fSAndroid Build Coastguard Worker original_argv = argv;
332*795d594fSAndroid Build Coastguard Worker
333*795d594fSAndroid Build Coastguard Worker MemMap::Init();
334*795d594fSAndroid Build Coastguard Worker InitLogging(argv, Abort);
335*795d594fSAndroid Build Coastguard Worker
336*795d594fSAndroid Build Coastguard Worker // Skip over the command name.
337*795d594fSAndroid Build Coastguard Worker argv++;
338*795d594fSAndroid Build Coastguard Worker argc--;
339*795d594fSAndroid Build Coastguard Worker
340*795d594fSAndroid Build Coastguard Worker if (argc == 0) {
341*795d594fSAndroid Build Coastguard Worker Usage("No arguments specified");
342*795d594fSAndroid Build Coastguard Worker }
343*795d594fSAndroid Build Coastguard Worker
344*795d594fSAndroid Build Coastguard Worker for (int i = 0; i < argc; ++i) {
345*795d594fSAndroid Build Coastguard Worker const char* raw_option = argv[i];
346*795d594fSAndroid Build Coastguard Worker const std::string_view option(raw_option);
347*795d594fSAndroid Build Coastguard Worker const bool log_options = false;
348*795d594fSAndroid Build Coastguard Worker if (log_options) {
349*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "profman: option[" << i << "]=" << argv[i];
350*795d594fSAndroid Build Coastguard Worker }
351*795d594fSAndroid Build Coastguard Worker if (option == "--dump-only") {
352*795d594fSAndroid Build Coastguard Worker dump_only_ = true;
353*795d594fSAndroid Build Coastguard Worker } else if (option == "--dump-classes-and-methods") {
354*795d594fSAndroid Build Coastguard Worker dump_classes_and_methods_ = true;
355*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--create-profile-from=")) {
356*795d594fSAndroid Build Coastguard Worker create_profile_from_file_ = std::string(option.substr(strlen("--create-profile-from=")));
357*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--output-profile-type=")) {
358*795d594fSAndroid Build Coastguard Worker ParseOutputProfileType(option, "--output-profile-type=", &output_profile_type_);
359*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--dump-output-to-fd=")) {
360*795d594fSAndroid Build Coastguard Worker ParseUintOption(raw_option, "--dump-output-to-fd=", &dump_output_to_fd_);
361*795d594fSAndroid Build Coastguard Worker } else if (option == "--generate-boot-image-profile") {
362*795d594fSAndroid Build Coastguard Worker generate_boot_image_profile_ = true;
363*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--method-threshold=")) {
364*795d594fSAndroid Build Coastguard Worker ParseUintOption(raw_option,
365*795d594fSAndroid Build Coastguard Worker "--method-threshold=",
366*795d594fSAndroid Build Coastguard Worker &boot_image_options_.method_threshold,
367*795d594fSAndroid Build Coastguard Worker 0u,
368*795d594fSAndroid Build Coastguard Worker 100u);
369*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--class-threshold=")) {
370*795d594fSAndroid Build Coastguard Worker ParseUintOption(raw_option,
371*795d594fSAndroid Build Coastguard Worker "--class-threshold=",
372*795d594fSAndroid Build Coastguard Worker &boot_image_options_.image_class_threshold,
373*795d594fSAndroid Build Coastguard Worker 0u,
374*795d594fSAndroid Build Coastguard Worker 100u);
375*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--clean-class-threshold=")) {
376*795d594fSAndroid Build Coastguard Worker ParseUintOption(raw_option,
377*795d594fSAndroid Build Coastguard Worker "--clean-class-threshold=",
378*795d594fSAndroid Build Coastguard Worker &boot_image_options_.image_class_clean_threshold,
379*795d594fSAndroid Build Coastguard Worker 0u,
380*795d594fSAndroid Build Coastguard Worker 100u);
381*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--preloaded-class-threshold=")) {
382*795d594fSAndroid Build Coastguard Worker ParseUintOption(raw_option,
383*795d594fSAndroid Build Coastguard Worker "--preloaded-class-threshold=",
384*795d594fSAndroid Build Coastguard Worker &boot_image_options_.preloaded_class_threshold,
385*795d594fSAndroid Build Coastguard Worker 0u,
386*795d594fSAndroid Build Coastguard Worker 100u);
387*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--preloaded-classes-denylist=")) {
388*795d594fSAndroid Build Coastguard Worker std::string preloaded_classes_denylist =
389*795d594fSAndroid Build Coastguard Worker std::string(option.substr(strlen("--preloaded-classes-denylist=")));
390*795d594fSAndroid Build Coastguard Worker // Read the user-specified list of methods.
391*795d594fSAndroid Build Coastguard Worker std::unique_ptr<std::set<std::string>>
392*795d594fSAndroid Build Coastguard Worker denylist(ReadCommentedInputFromFile<std::set<std::string>>(
393*795d594fSAndroid Build Coastguard Worker preloaded_classes_denylist.c_str(), nullptr)); // No post-processing.
394*795d594fSAndroid Build Coastguard Worker boot_image_options_.preloaded_classes_denylist.insert(
395*795d594fSAndroid Build Coastguard Worker denylist->begin(), denylist->end());
396*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--upgrade-startup-to-hot=")) {
397*795d594fSAndroid Build Coastguard Worker ParseBoolOption(option,
398*795d594fSAndroid Build Coastguard Worker "--upgrade-startup-to-hot=",
399*795d594fSAndroid Build Coastguard Worker &boot_image_options_.upgrade_startup_to_hot);
400*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--special-package=")) {
401*795d594fSAndroid Build Coastguard Worker std::vector<std::string> values;
402*795d594fSAndroid Build Coastguard Worker Split(std::string(option.substr(strlen("--special-package="))), ':', &values);
403*795d594fSAndroid Build Coastguard Worker if (values.size() != 2) {
404*795d594fSAndroid Build Coastguard Worker Usage("--special-package needs to be specified as pkg_name:threshold");
405*795d594fSAndroid Build Coastguard Worker }
406*795d594fSAndroid Build Coastguard Worker uint32_t threshold;
407*795d594fSAndroid Build Coastguard Worker ParseUintValue("special-package", values[1], &threshold, 0u, 100u);
408*795d594fSAndroid Build Coastguard Worker boot_image_options_.special_packages_thresholds.Overwrite(values[0], threshold);
409*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--debug-append-uses=")) {
410*795d594fSAndroid Build Coastguard Worker ParseBoolOption(option,
411*795d594fSAndroid Build Coastguard Worker "--debug-append-uses=",
412*795d594fSAndroid Build Coastguard Worker &boot_image_options_.append_package_use_list);
413*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--out-profile-path=")) {
414*795d594fSAndroid Build Coastguard Worker boot_profile_out_path_ = std::string(option.substr(strlen("--out-profile-path=")));
415*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--out-preloaded-classes-path=")) {
416*795d594fSAndroid Build Coastguard Worker preloaded_classes_out_path_ = std::string(
417*795d594fSAndroid Build Coastguard Worker option.substr(strlen("--out-preloaded-classes-path=")));
418*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--profile-file=")) {
419*795d594fSAndroid Build Coastguard Worker profile_files_.push_back(std::string(option.substr(strlen("--profile-file="))));
420*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--profile-file-fd=")) {
421*795d594fSAndroid Build Coastguard Worker ParseFdForCollection(raw_option, "--profile-file-fd=", &profile_files_fd_);
422*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--reference-profile-file=")) {
423*795d594fSAndroid Build Coastguard Worker reference_profile_file_ = std::string(option.substr(strlen("--reference-profile-file=")));
424*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--reference-profile-file-fd=")) {
425*795d594fSAndroid Build Coastguard Worker ParseUintOption(raw_option, "--reference-profile-file-fd=", &reference_profile_file_fd_);
426*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--dex-location=")) {
427*795d594fSAndroid Build Coastguard Worker dex_locations_.push_back(std::string(option.substr(strlen("--dex-location="))));
428*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--apk-fd=")) {
429*795d594fSAndroid Build Coastguard Worker ParseFdForCollection(raw_option, "--apk-fd=", &apks_fd_);
430*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--apk=")) {
431*795d594fSAndroid Build Coastguard Worker apk_files_.push_back(std::string(option.substr(strlen("--apk="))));
432*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--generate-test-profile=")) {
433*795d594fSAndroid Build Coastguard Worker test_profile_ = std::string(option.substr(strlen("--generate-test-profile=")));
434*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--generate-test-profile-num-dex=")) {
435*795d594fSAndroid Build Coastguard Worker ParseUintOption(raw_option,
436*795d594fSAndroid Build Coastguard Worker "--generate-test-profile-num-dex=",
437*795d594fSAndroid Build Coastguard Worker &test_profile_num_dex_);
438*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--generate-test-profile-method-percentage=")) {
439*795d594fSAndroid Build Coastguard Worker ParseUintOption(raw_option,
440*795d594fSAndroid Build Coastguard Worker "--generate-test-profile-method-percentage=",
441*795d594fSAndroid Build Coastguard Worker &test_profile_method_percerntage_);
442*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--generate-test-profile-class-percentage=")) {
443*795d594fSAndroid Build Coastguard Worker ParseUintOption(raw_option,
444*795d594fSAndroid Build Coastguard Worker "--generate-test-profile-class-percentage=",
445*795d594fSAndroid Build Coastguard Worker &test_profile_class_percentage_);
446*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--generate-test-profile-seed=")) {
447*795d594fSAndroid Build Coastguard Worker ParseUintOption(raw_option, "--generate-test-profile-seed=", &test_profile_seed_);
448*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--min-new-methods-percent-change=")) {
449*795d594fSAndroid Build Coastguard Worker uint32_t min_new_methods_percent_change;
450*795d594fSAndroid Build Coastguard Worker ParseUintOption(raw_option,
451*795d594fSAndroid Build Coastguard Worker "--min-new-methods-percent-change=",
452*795d594fSAndroid Build Coastguard Worker &min_new_methods_percent_change,
453*795d594fSAndroid Build Coastguard Worker 0u,
454*795d594fSAndroid Build Coastguard Worker 100u);
455*795d594fSAndroid Build Coastguard Worker profile_assistant_options_.SetMinNewMethodsPercentChangeForCompilation(
456*795d594fSAndroid Build Coastguard Worker min_new_methods_percent_change);
457*795d594fSAndroid Build Coastguard Worker } else if (option.starts_with("--min-new-classes-percent-change=")) {
458*795d594fSAndroid Build Coastguard Worker uint32_t min_new_classes_percent_change;
459*795d594fSAndroid Build Coastguard Worker ParseUintOption(raw_option,
460*795d594fSAndroid Build Coastguard Worker "--min-new-classes-percent-change=",
461*795d594fSAndroid Build Coastguard Worker &min_new_classes_percent_change,
462*795d594fSAndroid Build Coastguard Worker 0u,
463*795d594fSAndroid Build Coastguard Worker 100u);
464*795d594fSAndroid Build Coastguard Worker profile_assistant_options_.SetMinNewClassesPercentChangeForCompilation(
465*795d594fSAndroid Build Coastguard Worker min_new_classes_percent_change);
466*795d594fSAndroid Build Coastguard Worker } else if (option == "--copy-and-update-profile-key") {
467*795d594fSAndroid Build Coastguard Worker copy_and_update_profile_key_ = true;
468*795d594fSAndroid Build Coastguard Worker } else if (option == "--boot-image-merge") {
469*795d594fSAndroid Build Coastguard Worker profile_assistant_options_.SetBootImageMerge(true);
470*795d594fSAndroid Build Coastguard Worker } else if (option == "--force-merge") {
471*795d594fSAndroid Build Coastguard Worker // For backward compatibility only.
472*795d594fSAndroid Build Coastguard Worker // TODO(jiakaiz): Remove this when S and T are no longer supported.
473*795d594fSAndroid Build Coastguard Worker profile_assistant_options_.SetForceMerge(true);
474*795d594fSAndroid Build Coastguard Worker } else if (option == "--force-merge-and-analyze") {
475*795d594fSAndroid Build Coastguard Worker profile_assistant_options_.SetForceMergeAndAnalyze(true);
476*795d594fSAndroid Build Coastguard Worker } else {
477*795d594fSAndroid Build Coastguard Worker Usage("Unknown argument '%s'", raw_option);
478*795d594fSAndroid Build Coastguard Worker }
479*795d594fSAndroid Build Coastguard Worker }
480*795d594fSAndroid Build Coastguard Worker
481*795d594fSAndroid Build Coastguard Worker // Validate global consistency between file/fd options.
482*795d594fSAndroid Build Coastguard Worker if (!profile_files_.empty() && !profile_files_fd_.empty()) {
483*795d594fSAndroid Build Coastguard Worker Usage("Profile files should not be specified with both --profile-file-fd and --profile-file");
484*795d594fSAndroid Build Coastguard Worker }
485*795d594fSAndroid Build Coastguard Worker if (!reference_profile_file_.empty() && FdIsValid(reference_profile_file_fd_)) {
486*795d594fSAndroid Build Coastguard Worker Usage("Reference profile should not be specified with both "
487*795d594fSAndroid Build Coastguard Worker "--reference-profile-file-fd and --reference-profile-file");
488*795d594fSAndroid Build Coastguard Worker }
489*795d594fSAndroid Build Coastguard Worker if (!apk_files_.empty() && !apks_fd_.empty()) {
490*795d594fSAndroid Build Coastguard Worker Usage("APK files should not be specified with both --apk-fd and --apk");
491*795d594fSAndroid Build Coastguard Worker }
492*795d594fSAndroid Build Coastguard Worker }
493*795d594fSAndroid Build Coastguard Worker
494*795d594fSAndroid Build Coastguard Worker struct ProfileFilterKey {
ProfileFilterKeyart::ProfMan::ProfileFilterKey495*795d594fSAndroid Build Coastguard Worker ProfileFilterKey(const std::string& dex_location, uint32_t checksum)
496*795d594fSAndroid Build Coastguard Worker : dex_location_(dex_location), checksum_(checksum) {}
497*795d594fSAndroid Build Coastguard Worker const std::string dex_location_;
498*795d594fSAndroid Build Coastguard Worker uint32_t checksum_;
499*795d594fSAndroid Build Coastguard Worker
operator ==art::ProfMan::ProfileFilterKey500*795d594fSAndroid Build Coastguard Worker bool operator==(const ProfileFilterKey& other) const {
501*795d594fSAndroid Build Coastguard Worker return checksum_ == other.checksum_ && dex_location_ == other.dex_location_;
502*795d594fSAndroid Build Coastguard Worker }
operator <art::ProfMan::ProfileFilterKey503*795d594fSAndroid Build Coastguard Worker bool operator<(const ProfileFilterKey& other) const {
504*795d594fSAndroid Build Coastguard Worker return checksum_ == other.checksum_
505*795d594fSAndroid Build Coastguard Worker ? dex_location_ < other.dex_location_
506*795d594fSAndroid Build Coastguard Worker : checksum_ < other.checksum_;
507*795d594fSAndroid Build Coastguard Worker }
508*795d594fSAndroid Build Coastguard Worker };
509*795d594fSAndroid Build Coastguard Worker
ProcessProfiles()510*795d594fSAndroid Build Coastguard Worker ProfmanResult::ProcessingResult ProcessProfiles() {
511*795d594fSAndroid Build Coastguard Worker // Validate that a reference profile was passed, at the very least. It's okay that profiles are
512*795d594fSAndroid Build Coastguard Worker // missing, in which case profman will still analyze the reference profile (to check whether
513*795d594fSAndroid Build Coastguard Worker // it's empty), but no merge will happen.
514*795d594fSAndroid Build Coastguard Worker if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
515*795d594fSAndroid Build Coastguard Worker Usage("No reference profile file specified.");
516*795d594fSAndroid Build Coastguard Worker }
517*795d594fSAndroid Build Coastguard Worker if ((!profile_files_.empty() && FdIsValid(reference_profile_file_fd_)) ||
518*795d594fSAndroid Build Coastguard Worker (!profile_files_fd_.empty() && !FdIsValid(reference_profile_file_fd_))) {
519*795d594fSAndroid Build Coastguard Worker Usage("Options --profile-file-fd and --reference-profile-file-fd "
520*795d594fSAndroid Build Coastguard Worker "should only be used together");
521*795d594fSAndroid Build Coastguard Worker }
522*795d594fSAndroid Build Coastguard Worker
523*795d594fSAndroid Build Coastguard Worker // Check if we have any apks which we should use to filter the profile data.
524*795d594fSAndroid Build Coastguard Worker std::set<ProfileFilterKey> profile_filter_keys;
525*795d594fSAndroid Build Coastguard Worker if (!GetProfileFilterKeyFromApks(&profile_filter_keys)) {
526*795d594fSAndroid Build Coastguard Worker return ProfmanResult::kErrorIO;
527*795d594fSAndroid Build Coastguard Worker }
528*795d594fSAndroid Build Coastguard Worker
529*795d594fSAndroid Build Coastguard Worker // Build the profile filter function. If the set of keys is empty it means we
530*795d594fSAndroid Build Coastguard Worker // don't have any apks; as such we do not filter anything.
531*795d594fSAndroid Build Coastguard Worker const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn =
532*795d594fSAndroid Build Coastguard Worker [profile_filter_keys](const std::string& profile_key, uint32_t checksum) {
533*795d594fSAndroid Build Coastguard Worker if (profile_filter_keys.empty()) {
534*795d594fSAndroid Build Coastguard Worker // No --apk was specified. Accept all dex files.
535*795d594fSAndroid Build Coastguard Worker return true;
536*795d594fSAndroid Build Coastguard Worker } else {
537*795d594fSAndroid Build Coastguard Worker // Remove any annotations from the profile key before comparing with the keys we get from apks.
538*795d594fSAndroid Build Coastguard Worker std::string base_key = ProfileCompilationInfo::GetBaseKeyFromAugmentedKey(profile_key);
539*795d594fSAndroid Build Coastguard Worker return profile_filter_keys.find(ProfileFilterKey(base_key, checksum)) !=
540*795d594fSAndroid Build Coastguard Worker profile_filter_keys.end();
541*795d594fSAndroid Build Coastguard Worker }
542*795d594fSAndroid Build Coastguard Worker };
543*795d594fSAndroid Build Coastguard Worker
544*795d594fSAndroid Build Coastguard Worker ProfmanResult::ProcessingResult result;
545*795d594fSAndroid Build Coastguard Worker
546*795d594fSAndroid Build Coastguard Worker if (reference_profile_file_.empty()) {
547*795d594fSAndroid Build Coastguard Worker // The file doesn't need to be flushed here (ProcessProfiles will do it)
548*795d594fSAndroid Build Coastguard Worker // so don't check the usage.
549*795d594fSAndroid Build Coastguard Worker File file(reference_profile_file_fd_, false);
550*795d594fSAndroid Build Coastguard Worker result = ProfileAssistant::ProcessProfiles(profile_files_fd_,
551*795d594fSAndroid Build Coastguard Worker reference_profile_file_fd_,
552*795d594fSAndroid Build Coastguard Worker filter_fn,
553*795d594fSAndroid Build Coastguard Worker profile_assistant_options_);
554*795d594fSAndroid Build Coastguard Worker CloseAllFds(profile_files_fd_, "profile_files_fd_");
555*795d594fSAndroid Build Coastguard Worker } else {
556*795d594fSAndroid Build Coastguard Worker result = ProfileAssistant::ProcessProfiles(profile_files_,
557*795d594fSAndroid Build Coastguard Worker reference_profile_file_,
558*795d594fSAndroid Build Coastguard Worker filter_fn,
559*795d594fSAndroid Build Coastguard Worker profile_assistant_options_);
560*795d594fSAndroid Build Coastguard Worker }
561*795d594fSAndroid Build Coastguard Worker return result;
562*795d594fSAndroid Build Coastguard Worker }
563*795d594fSAndroid Build Coastguard Worker
GetProfileFilterKeyFromApks(std::set<ProfileFilterKey> * profile_filter_keys)564*795d594fSAndroid Build Coastguard Worker bool GetProfileFilterKeyFromApks(std::set<ProfileFilterKey>* profile_filter_keys) {
565*795d594fSAndroid Build Coastguard Worker return ForEachApkFile([&](File file, const std::string& location) {
566*795d594fSAndroid Build Coastguard Worker ArtDexFileLoader dex_file_loader(&file, location);
567*795d594fSAndroid Build Coastguard Worker std::vector<std::pair<std::string, uint32_t>> checksums;
568*795d594fSAndroid Build Coastguard Worker std::string error_msg;
569*795d594fSAndroid Build Coastguard Worker if (!dex_file_loader.GetMultiDexChecksums(&checksums, &error_msg)) {
570*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "Open failed for '" << location << "' " << error_msg;
571*795d594fSAndroid Build Coastguard Worker return false;
572*795d594fSAndroid Build Coastguard Worker }
573*795d594fSAndroid Build Coastguard Worker for (const auto& [multi_dex_location, checksum] : checksums) {
574*795d594fSAndroid Build Coastguard Worker profile_filter_keys->emplace(
575*795d594fSAndroid Build Coastguard Worker ProfileCompilationInfo::GetProfileDexFileBaseKey(multi_dex_location), checksum);
576*795d594fSAndroid Build Coastguard Worker }
577*795d594fSAndroid Build Coastguard Worker return true;
578*795d594fSAndroid Build Coastguard Worker });
579*795d594fSAndroid Build Coastguard Worker }
580*795d594fSAndroid Build Coastguard Worker
OpenApkFilesFromLocations(std::vector<std::unique_ptr<const DexFile>> * dex_files)581*795d594fSAndroid Build Coastguard Worker bool OpenApkFilesFromLocations(std::vector<std::unique_ptr<const DexFile>>* dex_files) {
582*795d594fSAndroid Build Coastguard Worker auto process_fn = [dex_files](std::unique_ptr<const DexFile>&& dex_file) {
583*795d594fSAndroid Build Coastguard Worker dex_files->emplace_back(std::move(dex_file));
584*795d594fSAndroid Build Coastguard Worker };
585*795d594fSAndroid Build Coastguard Worker return OpenApkFilesFromLocations(process_fn);
586*795d594fSAndroid Build Coastguard Worker }
587*795d594fSAndroid Build Coastguard Worker
OpenApkFilesFromLocations(const std::function<void (std::unique_ptr<const DexFile> &&)> & process_fn)588*795d594fSAndroid Build Coastguard Worker bool OpenApkFilesFromLocations(
589*795d594fSAndroid Build Coastguard Worker const std::function<void(std::unique_ptr<const DexFile>&&)>& process_fn) {
590*795d594fSAndroid Build Coastguard Worker static constexpr bool kVerifyChecksum = true;
591*795d594fSAndroid Build Coastguard Worker std::string error_msg;
592*795d594fSAndroid Build Coastguard Worker std::vector<std::unique_ptr<const DexFile>> dex_files_for_location;
593*795d594fSAndroid Build Coastguard Worker bool result = ForEachApkFile([&](File file, const std::string& location) {
594*795d594fSAndroid Build Coastguard Worker ArtDexFileLoader dex_file_loader(&file, location);
595*795d594fSAndroid Build Coastguard Worker if (!dex_file_loader.Open(/*verify=*/false,
596*795d594fSAndroid Build Coastguard Worker kVerifyChecksum,
597*795d594fSAndroid Build Coastguard Worker /*allow_no_dex_files=*/true,
598*795d594fSAndroid Build Coastguard Worker &error_msg,
599*795d594fSAndroid Build Coastguard Worker &dex_files_for_location)) {
600*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "Open failed for '" << location << "' " << error_msg;
601*795d594fSAndroid Build Coastguard Worker return false;
602*795d594fSAndroid Build Coastguard Worker }
603*795d594fSAndroid Build Coastguard Worker return true;
604*795d594fSAndroid Build Coastguard Worker });
605*795d594fSAndroid Build Coastguard Worker if (!result) {
606*795d594fSAndroid Build Coastguard Worker return false;
607*795d594fSAndroid Build Coastguard Worker }
608*795d594fSAndroid Build Coastguard Worker for (std::unique_ptr<const DexFile>& dex_file : dex_files_for_location) {
609*795d594fSAndroid Build Coastguard Worker process_fn(std::move(dex_file));
610*795d594fSAndroid Build Coastguard Worker }
611*795d594fSAndroid Build Coastguard Worker return true;
612*795d594fSAndroid Build Coastguard Worker }
613*795d594fSAndroid Build Coastguard Worker
ForEachApkFile(const std::function<bool (File file,const std::string & location)> & process_fn)614*795d594fSAndroid Build Coastguard Worker bool ForEachApkFile(
615*795d594fSAndroid Build Coastguard Worker const std::function<bool(File file, const std::string& location)>& process_fn) {
616*795d594fSAndroid Build Coastguard Worker bool use_apk_fd_list = !apks_fd_.empty();
617*795d594fSAndroid Build Coastguard Worker if (use_apk_fd_list) {
618*795d594fSAndroid Build Coastguard Worker // Get the APKs from the collection of FDs.
619*795d594fSAndroid Build Coastguard Worker if (dex_locations_.empty()) {
620*795d594fSAndroid Build Coastguard Worker // Try to compute the dex locations from the file paths of the descriptions.
621*795d594fSAndroid Build Coastguard Worker // This will make it easier to invoke profman with --apk-fd and without
622*795d594fSAndroid Build Coastguard Worker // being force to pass --dex-location when the location would be the apk path.
623*795d594fSAndroid Build Coastguard Worker if (!ComputeDexLocationsFromApkFds()) {
624*795d594fSAndroid Build Coastguard Worker return false;
625*795d594fSAndroid Build Coastguard Worker }
626*795d594fSAndroid Build Coastguard Worker } else {
627*795d594fSAndroid Build Coastguard Worker if (dex_locations_.size() != apks_fd_.size()) {
628*795d594fSAndroid Build Coastguard Worker Usage("The number of apk-fds must match the number of dex-locations.");
629*795d594fSAndroid Build Coastguard Worker }
630*795d594fSAndroid Build Coastguard Worker }
631*795d594fSAndroid Build Coastguard Worker } else if (!apk_files_.empty()) {
632*795d594fSAndroid Build Coastguard Worker if (dex_locations_.empty()) {
633*795d594fSAndroid Build Coastguard Worker // If no dex locations are specified use the apk names as locations.
634*795d594fSAndroid Build Coastguard Worker dex_locations_ = apk_files_;
635*795d594fSAndroid Build Coastguard Worker } else if (dex_locations_.size() != apk_files_.size()) {
636*795d594fSAndroid Build Coastguard Worker Usage("The number of apk-fds must match the number of dex-locations.");
637*795d594fSAndroid Build Coastguard Worker }
638*795d594fSAndroid Build Coastguard Worker } else {
639*795d594fSAndroid Build Coastguard Worker // No APKs were specified.
640*795d594fSAndroid Build Coastguard Worker CHECK(dex_locations_.empty());
641*795d594fSAndroid Build Coastguard Worker return true;
642*795d594fSAndroid Build Coastguard Worker }
643*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < dex_locations_.size(); ++i) {
644*795d594fSAndroid Build Coastguard Worker // We do not need to verify the apk for processing profiles.
645*795d594fSAndroid Build Coastguard Worker if (use_apk_fd_list) {
646*795d594fSAndroid Build Coastguard Worker File file(apks_fd_[i], /*check_usage=*/false);
647*795d594fSAndroid Build Coastguard Worker if (!process_fn(std::move(file), dex_locations_[i])) {
648*795d594fSAndroid Build Coastguard Worker return false;
649*795d594fSAndroid Build Coastguard Worker }
650*795d594fSAndroid Build Coastguard Worker } else {
651*795d594fSAndroid Build Coastguard Worker File file(apk_files_[i], O_RDONLY, /*check_usage=*/false);
652*795d594fSAndroid Build Coastguard Worker if (file.Fd() < 0) {
653*795d594fSAndroid Build Coastguard Worker PLOG(ERROR) << "Unable to open '" << apk_files_[i] << "'";
654*795d594fSAndroid Build Coastguard Worker return false;
655*795d594fSAndroid Build Coastguard Worker }
656*795d594fSAndroid Build Coastguard Worker if (!process_fn(std::move(file), dex_locations_[i])) {
657*795d594fSAndroid Build Coastguard Worker return false;
658*795d594fSAndroid Build Coastguard Worker }
659*795d594fSAndroid Build Coastguard Worker }
660*795d594fSAndroid Build Coastguard Worker }
661*795d594fSAndroid Build Coastguard Worker return true;
662*795d594fSAndroid Build Coastguard Worker }
663*795d594fSAndroid Build Coastguard Worker
664*795d594fSAndroid Build Coastguard Worker // Get the dex locations from the apk fds.
665*795d594fSAndroid Build Coastguard Worker // The methods reads the links from /proc/self/fd/ to find the original apk paths
666*795d594fSAndroid Build Coastguard Worker // and puts them in the dex_locations_ vector.
ComputeDexLocationsFromApkFds()667*795d594fSAndroid Build Coastguard Worker bool ComputeDexLocationsFromApkFds() {
668*795d594fSAndroid Build Coastguard Worker #ifdef _WIN32
669*795d594fSAndroid Build Coastguard Worker PLOG(ERROR) << "ComputeDexLocationsFromApkFds is unsupported on Windows.";
670*795d594fSAndroid Build Coastguard Worker return false;
671*795d594fSAndroid Build Coastguard Worker #else
672*795d594fSAndroid Build Coastguard Worker // We can't use a char array of PATH_MAX size without exceeding the frame size.
673*795d594fSAndroid Build Coastguard Worker // So we use a vector as the buffer for the path.
674*795d594fSAndroid Build Coastguard Worker std::vector<char> buffer(PATH_MAX, 0);
675*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < apks_fd_.size(); ++i) {
676*795d594fSAndroid Build Coastguard Worker std::string fd_path = "/proc/self/fd/" + std::to_string(apks_fd_[i]);
677*795d594fSAndroid Build Coastguard Worker ssize_t len = readlink(fd_path.c_str(), buffer.data(), buffer.size() - 1);
678*795d594fSAndroid Build Coastguard Worker if (len == -1) {
679*795d594fSAndroid Build Coastguard Worker PLOG(ERROR) << "Could not open path from fd";
680*795d594fSAndroid Build Coastguard Worker return false;
681*795d594fSAndroid Build Coastguard Worker }
682*795d594fSAndroid Build Coastguard Worker
683*795d594fSAndroid Build Coastguard Worker buffer[len] = '\0';
684*795d594fSAndroid Build Coastguard Worker dex_locations_.push_back(buffer.data());
685*795d594fSAndroid Build Coastguard Worker }
686*795d594fSAndroid Build Coastguard Worker return true;
687*795d594fSAndroid Build Coastguard Worker #endif
688*795d594fSAndroid Build Coastguard Worker }
689*795d594fSAndroid Build Coastguard Worker
LoadProfile(const std::string & filename,int fd,bool for_boot_image)690*795d594fSAndroid Build Coastguard Worker std::unique_ptr<const ProfileCompilationInfo> LoadProfile(const std::string& filename,
691*795d594fSAndroid Build Coastguard Worker int fd,
692*795d594fSAndroid Build Coastguard Worker bool for_boot_image) {
693*795d594fSAndroid Build Coastguard Worker if (!filename.empty()) {
694*795d594fSAndroid Build Coastguard Worker #ifdef _WIN32
695*795d594fSAndroid Build Coastguard Worker int flags = O_RDWR;
696*795d594fSAndroid Build Coastguard Worker #else
697*795d594fSAndroid Build Coastguard Worker int flags = O_RDWR | O_CLOEXEC;
698*795d594fSAndroid Build Coastguard Worker #endif
699*795d594fSAndroid Build Coastguard Worker fd = open(filename.c_str(), flags);
700*795d594fSAndroid Build Coastguard Worker if (fd < 0) {
701*795d594fSAndroid Build Coastguard Worker PLOG(ERROR) << "Cannot open " << filename;
702*795d594fSAndroid Build Coastguard Worker return nullptr;
703*795d594fSAndroid Build Coastguard Worker }
704*795d594fSAndroid Build Coastguard Worker }
705*795d594fSAndroid Build Coastguard Worker std::unique_ptr<ProfileCompilationInfo> info(new ProfileCompilationInfo(for_boot_image));
706*795d594fSAndroid Build Coastguard Worker if (!info->Load(fd)) {
707*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "Cannot load profile info from fd=" << fd << "\n";
708*795d594fSAndroid Build Coastguard Worker return nullptr;
709*795d594fSAndroid Build Coastguard Worker }
710*795d594fSAndroid Build Coastguard Worker return info;
711*795d594fSAndroid Build Coastguard Worker }
712*795d594fSAndroid Build Coastguard Worker
DumpOneProfile(const std::string & banner,const std::string & filename,int fd,const std::vector<std::unique_ptr<const DexFile>> * dex_files,std::string * dump)713*795d594fSAndroid Build Coastguard Worker int DumpOneProfile(const std::string& banner,
714*795d594fSAndroid Build Coastguard Worker const std::string& filename,
715*795d594fSAndroid Build Coastguard Worker int fd,
716*795d594fSAndroid Build Coastguard Worker const std::vector<std::unique_ptr<const DexFile>>* dex_files,
717*795d594fSAndroid Build Coastguard Worker std::string* dump) {
718*795d594fSAndroid Build Coastguard Worker // For dumping, try loading as app profile and if that fails try loading as boot profile.
719*795d594fSAndroid Build Coastguard Worker std::unique_ptr<const ProfileCompilationInfo> info =
720*795d594fSAndroid Build Coastguard Worker LoadProfile(filename, fd, /*for_boot_image=*/ false);
721*795d594fSAndroid Build Coastguard Worker if (info == nullptr) {
722*795d594fSAndroid Build Coastguard Worker info = LoadProfile(filename, fd, /*for_boot_image=*/ true);
723*795d594fSAndroid Build Coastguard Worker }
724*795d594fSAndroid Build Coastguard Worker if (info == nullptr) {
725*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "Cannot load profile info from filename=" << filename << " fd=" << fd;
726*795d594fSAndroid Build Coastguard Worker return -1;
727*795d594fSAndroid Build Coastguard Worker }
728*795d594fSAndroid Build Coastguard Worker *dump += banner + "\n" + info->DumpInfo(MakeNonOwningPointerVector(*dex_files)) + "\n";
729*795d594fSAndroid Build Coastguard Worker return 0;
730*795d594fSAndroid Build Coastguard Worker }
731*795d594fSAndroid Build Coastguard Worker
DumpProfileInfo()732*795d594fSAndroid Build Coastguard Worker int DumpProfileInfo() {
733*795d594fSAndroid Build Coastguard Worker // Validate that at least one profile file or reference was specified.
734*795d594fSAndroid Build Coastguard Worker if (profile_files_.empty() && profile_files_fd_.empty() &&
735*795d594fSAndroid Build Coastguard Worker reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
736*795d594fSAndroid Build Coastguard Worker Usage("No profile files or reference profile specified.");
737*795d594fSAndroid Build Coastguard Worker }
738*795d594fSAndroid Build Coastguard Worker static const char* kEmptyString = "";
739*795d594fSAndroid Build Coastguard Worker static const char* kOrdinaryProfile = "=== profile ===";
740*795d594fSAndroid Build Coastguard Worker static const char* kReferenceProfile = "=== reference profile ===";
741*795d594fSAndroid Build Coastguard Worker static const char* kDexFiles = "=== Dex files ===";
742*795d594fSAndroid Build Coastguard Worker
743*795d594fSAndroid Build Coastguard Worker std::vector<std::unique_ptr<const DexFile>> dex_files;
744*795d594fSAndroid Build Coastguard Worker OpenApkFilesFromLocations(&dex_files);
745*795d594fSAndroid Build Coastguard Worker
746*795d594fSAndroid Build Coastguard Worker std::string dump;
747*795d594fSAndroid Build Coastguard Worker
748*795d594fSAndroid Build Coastguard Worker // Dump checkfiles and corresponding checksums.
749*795d594fSAndroid Build Coastguard Worker dump += kDexFiles;
750*795d594fSAndroid Build Coastguard Worker dump += "\n";
751*795d594fSAndroid Build Coastguard Worker for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
752*795d594fSAndroid Build Coastguard Worker std::ostringstream oss;
753*795d594fSAndroid Build Coastguard Worker oss << dex_file->GetLocation()
754*795d594fSAndroid Build Coastguard Worker << " [checksum=" << std::hex << dex_file->GetLocationChecksum() << "]\n";
755*795d594fSAndroid Build Coastguard Worker dump += oss.str();
756*795d594fSAndroid Build Coastguard Worker }
757*795d594fSAndroid Build Coastguard Worker
758*795d594fSAndroid Build Coastguard Worker // Dump individual profile files.
759*795d594fSAndroid Build Coastguard Worker if (!profile_files_fd_.empty()) {
760*795d594fSAndroid Build Coastguard Worker for (int profile_file_fd : profile_files_fd_) {
761*795d594fSAndroid Build Coastguard Worker int ret = DumpOneProfile(kOrdinaryProfile,
762*795d594fSAndroid Build Coastguard Worker kEmptyString,
763*795d594fSAndroid Build Coastguard Worker profile_file_fd,
764*795d594fSAndroid Build Coastguard Worker &dex_files,
765*795d594fSAndroid Build Coastguard Worker &dump);
766*795d594fSAndroid Build Coastguard Worker if (ret != 0) {
767*795d594fSAndroid Build Coastguard Worker return ret;
768*795d594fSAndroid Build Coastguard Worker }
769*795d594fSAndroid Build Coastguard Worker }
770*795d594fSAndroid Build Coastguard Worker }
771*795d594fSAndroid Build Coastguard Worker for (const std::string& profile_file : profile_files_) {
772*795d594fSAndroid Build Coastguard Worker int ret = DumpOneProfile(kOrdinaryProfile, profile_file, File::kInvalidFd, &dex_files, &dump);
773*795d594fSAndroid Build Coastguard Worker if (ret != 0) {
774*795d594fSAndroid Build Coastguard Worker return ret;
775*795d594fSAndroid Build Coastguard Worker }
776*795d594fSAndroid Build Coastguard Worker }
777*795d594fSAndroid Build Coastguard Worker // Dump reference profile file.
778*795d594fSAndroid Build Coastguard Worker if (FdIsValid(reference_profile_file_fd_)) {
779*795d594fSAndroid Build Coastguard Worker int ret = DumpOneProfile(kReferenceProfile,
780*795d594fSAndroid Build Coastguard Worker kEmptyString,
781*795d594fSAndroid Build Coastguard Worker reference_profile_file_fd_,
782*795d594fSAndroid Build Coastguard Worker &dex_files,
783*795d594fSAndroid Build Coastguard Worker &dump);
784*795d594fSAndroid Build Coastguard Worker if (ret != 0) {
785*795d594fSAndroid Build Coastguard Worker return ret;
786*795d594fSAndroid Build Coastguard Worker }
787*795d594fSAndroid Build Coastguard Worker }
788*795d594fSAndroid Build Coastguard Worker if (!reference_profile_file_.empty()) {
789*795d594fSAndroid Build Coastguard Worker int ret = DumpOneProfile(kReferenceProfile,
790*795d594fSAndroid Build Coastguard Worker reference_profile_file_,
791*795d594fSAndroid Build Coastguard Worker File::kInvalidFd,
792*795d594fSAndroid Build Coastguard Worker &dex_files,
793*795d594fSAndroid Build Coastguard Worker &dump);
794*795d594fSAndroid Build Coastguard Worker if (ret != 0) {
795*795d594fSAndroid Build Coastguard Worker return ret;
796*795d594fSAndroid Build Coastguard Worker }
797*795d594fSAndroid Build Coastguard Worker }
798*795d594fSAndroid Build Coastguard Worker if (!FdIsValid(dump_output_to_fd_)) {
799*795d594fSAndroid Build Coastguard Worker std::cout << dump;
800*795d594fSAndroid Build Coastguard Worker } else {
801*795d594fSAndroid Build Coastguard Worker unix_file::FdFile out_fd(dump_output_to_fd_, /*check_usage=*/ false);
802*795d594fSAndroid Build Coastguard Worker if (!out_fd.WriteFully(dump.c_str(), dump.length())) {
803*795d594fSAndroid Build Coastguard Worker return -1;
804*795d594fSAndroid Build Coastguard Worker }
805*795d594fSAndroid Build Coastguard Worker }
806*795d594fSAndroid Build Coastguard Worker return 0;
807*795d594fSAndroid Build Coastguard Worker }
808*795d594fSAndroid Build Coastguard Worker
ShouldOnlyDumpProfile()809*795d594fSAndroid Build Coastguard Worker bool ShouldOnlyDumpProfile() {
810*795d594fSAndroid Build Coastguard Worker return dump_only_;
811*795d594fSAndroid Build Coastguard Worker }
812*795d594fSAndroid Build Coastguard Worker
GetClassNamesAndMethods(const ProfileCompilationInfo & profile_info,std::vector<std::unique_ptr<const DexFile>> * dex_files,std::set<std::string> * out_lines)813*795d594fSAndroid Build Coastguard Worker bool GetClassNamesAndMethods(const ProfileCompilationInfo& profile_info,
814*795d594fSAndroid Build Coastguard Worker std::vector<std::unique_ptr<const DexFile>>* dex_files,
815*795d594fSAndroid Build Coastguard Worker std::set<std::string>* out_lines) {
816*795d594fSAndroid Build Coastguard Worker for (const std::unique_ptr<const DexFile>& dex_file : *dex_files) {
817*795d594fSAndroid Build Coastguard Worker std::set<dex::TypeIndex> class_types;
818*795d594fSAndroid Build Coastguard Worker std::set<uint16_t> hot_methods;
819*795d594fSAndroid Build Coastguard Worker std::set<uint16_t> startup_methods;
820*795d594fSAndroid Build Coastguard Worker std::set<uint16_t> post_startup_methods;
821*795d594fSAndroid Build Coastguard Worker std::set<uint16_t> combined_methods;
822*795d594fSAndroid Build Coastguard Worker if (profile_info.GetClassesAndMethods(*dex_file.get(),
823*795d594fSAndroid Build Coastguard Worker &class_types,
824*795d594fSAndroid Build Coastguard Worker &hot_methods,
825*795d594fSAndroid Build Coastguard Worker &startup_methods,
826*795d594fSAndroid Build Coastguard Worker &post_startup_methods)) {
827*795d594fSAndroid Build Coastguard Worker for (const dex::TypeIndex& type_index : class_types) {
828*795d594fSAndroid Build Coastguard Worker out_lines->insert(profile_info.GetTypeDescriptor(dex_file.get(), type_index));
829*795d594fSAndroid Build Coastguard Worker }
830*795d594fSAndroid Build Coastguard Worker combined_methods = hot_methods;
831*795d594fSAndroid Build Coastguard Worker combined_methods.insert(startup_methods.begin(), startup_methods.end());
832*795d594fSAndroid Build Coastguard Worker combined_methods.insert(post_startup_methods.begin(), post_startup_methods.end());
833*795d594fSAndroid Build Coastguard Worker for (uint16_t dex_method_idx : combined_methods) {
834*795d594fSAndroid Build Coastguard Worker const dex::MethodId& id = dex_file->GetMethodId(dex_method_idx);
835*795d594fSAndroid Build Coastguard Worker std::string signature_string(dex_file->GetMethodSignature(id).ToString());
836*795d594fSAndroid Build Coastguard Worker std::string type_string(dex_file->GetTypeDescriptor(dex_file->GetTypeId(id.class_idx_)));
837*795d594fSAndroid Build Coastguard Worker std::string method_name(dex_file->GetMethodName(id));
838*795d594fSAndroid Build Coastguard Worker std::string flags_string;
839*795d594fSAndroid Build Coastguard Worker if (hot_methods.find(dex_method_idx) != hot_methods.end()) {
840*795d594fSAndroid Build Coastguard Worker flags_string += kMethodFlagStringHot;
841*795d594fSAndroid Build Coastguard Worker }
842*795d594fSAndroid Build Coastguard Worker if (startup_methods.find(dex_method_idx) != startup_methods.end()) {
843*795d594fSAndroid Build Coastguard Worker flags_string += kMethodFlagStringStartup;
844*795d594fSAndroid Build Coastguard Worker }
845*795d594fSAndroid Build Coastguard Worker if (post_startup_methods.find(dex_method_idx) != post_startup_methods.end()) {
846*795d594fSAndroid Build Coastguard Worker flags_string += kMethodFlagStringPostStartup;
847*795d594fSAndroid Build Coastguard Worker }
848*795d594fSAndroid Build Coastguard Worker FlattenProfileData::ItemMetadata metadata;
849*795d594fSAndroid Build Coastguard Worker metadata.ExtractInlineCacheInfo(profile_info, dex_file.get(), dex_method_idx);
850*795d594fSAndroid Build Coastguard Worker std::string inline_cache_string = GetInlineCacheLine(metadata.GetInlineCache());
851*795d594fSAndroid Build Coastguard Worker out_lines->insert(ART_FORMAT("{}{}{}{}{}{}",
852*795d594fSAndroid Build Coastguard Worker flags_string,
853*795d594fSAndroid Build Coastguard Worker type_string,
854*795d594fSAndroid Build Coastguard Worker kMethodSep,
855*795d594fSAndroid Build Coastguard Worker method_name,
856*795d594fSAndroid Build Coastguard Worker signature_string,
857*795d594fSAndroid Build Coastguard Worker inline_cache_string));
858*795d594fSAndroid Build Coastguard Worker }
859*795d594fSAndroid Build Coastguard Worker }
860*795d594fSAndroid Build Coastguard Worker }
861*795d594fSAndroid Build Coastguard Worker return true;
862*795d594fSAndroid Build Coastguard Worker }
863*795d594fSAndroid Build Coastguard Worker
GetClassNamesAndMethods(int fd,std::vector<std::unique_ptr<const DexFile>> * dex_files,std::set<std::string> * out_lines)864*795d594fSAndroid Build Coastguard Worker bool GetClassNamesAndMethods(int fd,
865*795d594fSAndroid Build Coastguard Worker std::vector<std::unique_ptr<const DexFile>>* dex_files,
866*795d594fSAndroid Build Coastguard Worker std::set<std::string>* out_lines) {
867*795d594fSAndroid Build Coastguard Worker // For dumping, try loading as app profile and if that fails try loading as boot profile.
868*795d594fSAndroid Build Coastguard Worker for (bool for_boot_image : {false, true}) {
869*795d594fSAndroid Build Coastguard Worker ProfileCompilationInfo profile_info(for_boot_image);
870*795d594fSAndroid Build Coastguard Worker if (profile_info.Load(fd)) {
871*795d594fSAndroid Build Coastguard Worker return GetClassNamesAndMethods(profile_info, dex_files, out_lines);
872*795d594fSAndroid Build Coastguard Worker }
873*795d594fSAndroid Build Coastguard Worker }
874*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "Cannot load profile info";
875*795d594fSAndroid Build Coastguard Worker return false;
876*795d594fSAndroid Build Coastguard Worker }
877*795d594fSAndroid Build Coastguard Worker
GetClassNamesAndMethods(const std::string & profile_file,std::vector<std::unique_ptr<const DexFile>> * dex_files,std::set<std::string> * out_lines)878*795d594fSAndroid Build Coastguard Worker bool GetClassNamesAndMethods(const std::string& profile_file,
879*795d594fSAndroid Build Coastguard Worker std::vector<std::unique_ptr<const DexFile>>* dex_files,
880*795d594fSAndroid Build Coastguard Worker std::set<std::string>* out_lines) {
881*795d594fSAndroid Build Coastguard Worker #ifdef _WIN32
882*795d594fSAndroid Build Coastguard Worker int flags = O_RDONLY;
883*795d594fSAndroid Build Coastguard Worker #else
884*795d594fSAndroid Build Coastguard Worker int flags = O_RDONLY | O_CLOEXEC;
885*795d594fSAndroid Build Coastguard Worker #endif
886*795d594fSAndroid Build Coastguard Worker int fd = open(profile_file.c_str(), flags);
887*795d594fSAndroid Build Coastguard Worker if (!FdIsValid(fd)) {
888*795d594fSAndroid Build Coastguard Worker PLOG(ERROR) << "Cannot open " << profile_file;
889*795d594fSAndroid Build Coastguard Worker return false;
890*795d594fSAndroid Build Coastguard Worker }
891*795d594fSAndroid Build Coastguard Worker if (!GetClassNamesAndMethods(fd, dex_files, out_lines)) {
892*795d594fSAndroid Build Coastguard Worker return false;
893*795d594fSAndroid Build Coastguard Worker }
894*795d594fSAndroid Build Coastguard Worker if (close(fd) < 0) {
895*795d594fSAndroid Build Coastguard Worker PLOG(WARNING) << "Failed to close descriptor";
896*795d594fSAndroid Build Coastguard Worker }
897*795d594fSAndroid Build Coastguard Worker return true;
898*795d594fSAndroid Build Coastguard Worker }
899*795d594fSAndroid Build Coastguard Worker
DumpClassesAndMethods()900*795d594fSAndroid Build Coastguard Worker int DumpClassesAndMethods() {
901*795d594fSAndroid Build Coastguard Worker // Validate that at least one profile file or reference was specified.
902*795d594fSAndroid Build Coastguard Worker if (profile_files_.empty() && profile_files_fd_.empty() &&
903*795d594fSAndroid Build Coastguard Worker reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
904*795d594fSAndroid Build Coastguard Worker Usage("No profile files or reference profile specified.");
905*795d594fSAndroid Build Coastguard Worker }
906*795d594fSAndroid Build Coastguard Worker
907*795d594fSAndroid Build Coastguard Worker // Open the dex files to get the names for classes.
908*795d594fSAndroid Build Coastguard Worker std::vector<std::unique_ptr<const DexFile>> dex_files;
909*795d594fSAndroid Build Coastguard Worker OpenApkFilesFromLocations(&dex_files);
910*795d594fSAndroid Build Coastguard Worker // Build a vector of class names from individual profile files.
911*795d594fSAndroid Build Coastguard Worker std::set<std::string> class_names;
912*795d594fSAndroid Build Coastguard Worker if (!profile_files_fd_.empty()) {
913*795d594fSAndroid Build Coastguard Worker for (int profile_file_fd : profile_files_fd_) {
914*795d594fSAndroid Build Coastguard Worker if (!GetClassNamesAndMethods(profile_file_fd, &dex_files, &class_names)) {
915*795d594fSAndroid Build Coastguard Worker return -1;
916*795d594fSAndroid Build Coastguard Worker }
917*795d594fSAndroid Build Coastguard Worker }
918*795d594fSAndroid Build Coastguard Worker }
919*795d594fSAndroid Build Coastguard Worker if (!profile_files_.empty()) {
920*795d594fSAndroid Build Coastguard Worker for (const std::string& profile_file : profile_files_) {
921*795d594fSAndroid Build Coastguard Worker if (!GetClassNamesAndMethods(profile_file, &dex_files, &class_names)) {
922*795d594fSAndroid Build Coastguard Worker return -1;
923*795d594fSAndroid Build Coastguard Worker }
924*795d594fSAndroid Build Coastguard Worker }
925*795d594fSAndroid Build Coastguard Worker }
926*795d594fSAndroid Build Coastguard Worker // Concatenate class names from reference profile file.
927*795d594fSAndroid Build Coastguard Worker if (FdIsValid(reference_profile_file_fd_)) {
928*795d594fSAndroid Build Coastguard Worker if (!GetClassNamesAndMethods(reference_profile_file_fd_, &dex_files, &class_names)) {
929*795d594fSAndroid Build Coastguard Worker return -1;
930*795d594fSAndroid Build Coastguard Worker }
931*795d594fSAndroid Build Coastguard Worker }
932*795d594fSAndroid Build Coastguard Worker if (!reference_profile_file_.empty()) {
933*795d594fSAndroid Build Coastguard Worker if (!GetClassNamesAndMethods(reference_profile_file_, &dex_files, &class_names)) {
934*795d594fSAndroid Build Coastguard Worker return -1;
935*795d594fSAndroid Build Coastguard Worker }
936*795d594fSAndroid Build Coastguard Worker }
937*795d594fSAndroid Build Coastguard Worker // Dump the class names.
938*795d594fSAndroid Build Coastguard Worker std::string dump;
939*795d594fSAndroid Build Coastguard Worker for (const std::string& class_name : class_names) {
940*795d594fSAndroid Build Coastguard Worker dump += class_name + std::string("\n");
941*795d594fSAndroid Build Coastguard Worker }
942*795d594fSAndroid Build Coastguard Worker if (!FdIsValid(dump_output_to_fd_)) {
943*795d594fSAndroid Build Coastguard Worker std::cout << dump;
944*795d594fSAndroid Build Coastguard Worker } else {
945*795d594fSAndroid Build Coastguard Worker unix_file::FdFile out_fd(dump_output_to_fd_, /*check_usage=*/ false);
946*795d594fSAndroid Build Coastguard Worker if (!out_fd.WriteFully(dump.c_str(), dump.length())) {
947*795d594fSAndroid Build Coastguard Worker return -1;
948*795d594fSAndroid Build Coastguard Worker }
949*795d594fSAndroid Build Coastguard Worker }
950*795d594fSAndroid Build Coastguard Worker return 0;
951*795d594fSAndroid Build Coastguard Worker }
952*795d594fSAndroid Build Coastguard Worker
ShouldOnlyDumpClassesAndMethods()953*795d594fSAndroid Build Coastguard Worker bool ShouldOnlyDumpClassesAndMethods() {
954*795d594fSAndroid Build Coastguard Worker return dump_classes_and_methods_;
955*795d594fSAndroid Build Coastguard Worker }
956*795d594fSAndroid Build Coastguard Worker
957*795d594fSAndroid Build Coastguard Worker // Read lines from the given file, dropping comments and empty lines. Post-process each line with
958*795d594fSAndroid Build Coastguard Worker // the given function.
959*795d594fSAndroid Build Coastguard Worker template <typename T>
ReadCommentedInputFromFile(const char * input_filename,std::function<std::string (const char *)> * process)960*795d594fSAndroid Build Coastguard Worker static T* ReadCommentedInputFromFile(
961*795d594fSAndroid Build Coastguard Worker const char* input_filename, std::function<std::string(const char*)>* process) {
962*795d594fSAndroid Build Coastguard Worker std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
963*795d594fSAndroid Build Coastguard Worker if (input_file.get() == nullptr) {
964*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "Failed to open input file " << input_filename;
965*795d594fSAndroid Build Coastguard Worker return nullptr;
966*795d594fSAndroid Build Coastguard Worker }
967*795d594fSAndroid Build Coastguard Worker std::unique_ptr<T> result(
968*795d594fSAndroid Build Coastguard Worker ReadCommentedInputStream<T>(*input_file, process));
969*795d594fSAndroid Build Coastguard Worker input_file->close();
970*795d594fSAndroid Build Coastguard Worker return result.release();
971*795d594fSAndroid Build Coastguard Worker }
972*795d594fSAndroid Build Coastguard Worker
973*795d594fSAndroid Build Coastguard Worker // Read lines from the given stream, dropping comments and empty lines. Post-process each line
974*795d594fSAndroid Build Coastguard Worker // with the given function.
975*795d594fSAndroid Build Coastguard Worker template <typename T>
ReadCommentedInputStream(std::istream & in_stream,std::function<std::string (const char *)> * process)976*795d594fSAndroid Build Coastguard Worker static T* ReadCommentedInputStream(
977*795d594fSAndroid Build Coastguard Worker std::istream& in_stream,
978*795d594fSAndroid Build Coastguard Worker std::function<std::string(const char*)>* process) {
979*795d594fSAndroid Build Coastguard Worker std::unique_ptr<T> output(new T());
980*795d594fSAndroid Build Coastguard Worker while (in_stream.good()) {
981*795d594fSAndroid Build Coastguard Worker std::string dot;
982*795d594fSAndroid Build Coastguard Worker std::getline(in_stream, dot);
983*795d594fSAndroid Build Coastguard Worker if (dot.starts_with("#") || dot.empty()) {
984*795d594fSAndroid Build Coastguard Worker continue;
985*795d594fSAndroid Build Coastguard Worker }
986*795d594fSAndroid Build Coastguard Worker if (process != nullptr) {
987*795d594fSAndroid Build Coastguard Worker std::string descriptor((*process)(dot.c_str()));
988*795d594fSAndroid Build Coastguard Worker output->insert(output->end(), descriptor);
989*795d594fSAndroid Build Coastguard Worker } else {
990*795d594fSAndroid Build Coastguard Worker output->insert(output->end(), dot);
991*795d594fSAndroid Build Coastguard Worker }
992*795d594fSAndroid Build Coastguard Worker }
993*795d594fSAndroid Build Coastguard Worker return output.release();
994*795d594fSAndroid Build Coastguard Worker }
995*795d594fSAndroid Build Coastguard Worker
996*795d594fSAndroid Build Coastguard Worker // Find class definition for a descriptor.
FindClassDef(const std::vector<std::unique_ptr<const DexFile>> & dex_files,std::string_view klass_descriptor,TypeReference * class_ref)997*795d594fSAndroid Build Coastguard Worker const dex::ClassDef* FindClassDef(const std::vector<std::unique_ptr<const DexFile>>& dex_files,
998*795d594fSAndroid Build Coastguard Worker std::string_view klass_descriptor,
999*795d594fSAndroid Build Coastguard Worker /*out*/ TypeReference* class_ref) {
1000*795d594fSAndroid Build Coastguard Worker for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
1001*795d594fSAndroid Build Coastguard Worker const dex::TypeId* type_id = dex_file->FindTypeId(klass_descriptor);
1002*795d594fSAndroid Build Coastguard Worker if (type_id != nullptr) {
1003*795d594fSAndroid Build Coastguard Worker dex::TypeIndex type_index = dex_file->GetIndexForTypeId(*type_id);
1004*795d594fSAndroid Build Coastguard Worker const dex::ClassDef* class_def = dex_file->FindClassDef(type_index);
1005*795d594fSAndroid Build Coastguard Worker if (class_def != nullptr) {
1006*795d594fSAndroid Build Coastguard Worker *class_ref = TypeReference(dex_file.get(), type_index);
1007*795d594fSAndroid Build Coastguard Worker return class_def;
1008*795d594fSAndroid Build Coastguard Worker }
1009*795d594fSAndroid Build Coastguard Worker }
1010*795d594fSAndroid Build Coastguard Worker }
1011*795d594fSAndroid Build Coastguard Worker return nullptr;
1012*795d594fSAndroid Build Coastguard Worker }
1013*795d594fSAndroid Build Coastguard Worker
1014*795d594fSAndroid Build Coastguard Worker // Find class klass_descriptor in the given dex_files and store its reference
1015*795d594fSAndroid Build Coastguard Worker // in the out parameter class_ref.
1016*795d594fSAndroid Build Coastguard Worker // Return true if a reference of the class was found in any of the dex_files.
FindClass(const std::vector<std::unique_ptr<const DexFile>> & dex_files,std::string_view klass_descriptor,TypeReference * class_ref)1017*795d594fSAndroid Build Coastguard Worker bool FindClass(const std::vector<std::unique_ptr<const DexFile>>& dex_files,
1018*795d594fSAndroid Build Coastguard Worker std::string_view klass_descriptor,
1019*795d594fSAndroid Build Coastguard Worker /*out*/ TypeReference* class_ref) {
1020*795d594fSAndroid Build Coastguard Worker for (const std::unique_ptr<const DexFile>& dex_file_ptr : dex_files) {
1021*795d594fSAndroid Build Coastguard Worker const DexFile* dex_file = dex_file_ptr.get();
1022*795d594fSAndroid Build Coastguard Worker const dex::TypeId* type_id = dex_file->FindTypeId(klass_descriptor);
1023*795d594fSAndroid Build Coastguard Worker if (type_id != nullptr) {
1024*795d594fSAndroid Build Coastguard Worker *class_ref = TypeReference(dex_file, dex_file->GetIndexForTypeId(*type_id));
1025*795d594fSAndroid Build Coastguard Worker return true;
1026*795d594fSAndroid Build Coastguard Worker }
1027*795d594fSAndroid Build Coastguard Worker }
1028*795d594fSAndroid Build Coastguard Worker return false;
1029*795d594fSAndroid Build Coastguard Worker }
1030*795d594fSAndroid Build Coastguard Worker
1031*795d594fSAndroid Build Coastguard Worker // Find the method specified by method_spec in the class class_ref.
FindMethodIndex(const TypeReference & class_ref,std::string_view method_spec)1032*795d594fSAndroid Build Coastguard Worker uint32_t FindMethodIndex(const TypeReference& class_ref,
1033*795d594fSAndroid Build Coastguard Worker std::string_view method_spec) {
1034*795d594fSAndroid Build Coastguard Worker const DexFile* dex_file = class_ref.dex_file;
1035*795d594fSAndroid Build Coastguard Worker
1036*795d594fSAndroid Build Coastguard Worker size_t signature_start = method_spec.find(kProfileParsingFirstCharInSignature);
1037*795d594fSAndroid Build Coastguard Worker if (signature_start == std::string_view::npos) {
1038*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "Invalid method name and signature: " << method_spec;
1039*795d594fSAndroid Build Coastguard Worker return dex::kDexNoIndex;
1040*795d594fSAndroid Build Coastguard Worker }
1041*795d594fSAndroid Build Coastguard Worker
1042*795d594fSAndroid Build Coastguard Worker const std::string_view name = method_spec.substr(0u, signature_start);
1043*795d594fSAndroid Build Coastguard Worker const std::string_view signature = method_spec.substr(signature_start);
1044*795d594fSAndroid Build Coastguard Worker
1045*795d594fSAndroid Build Coastguard Worker const dex::StringId* name_id = dex_file->FindStringId(std::string(name).c_str());
1046*795d594fSAndroid Build Coastguard Worker if (name_id == nullptr) {
1047*795d594fSAndroid Build Coastguard Worker LOG(WARNING) << "Could not find name: " << name;
1048*795d594fSAndroid Build Coastguard Worker return dex::kDexNoIndex;
1049*795d594fSAndroid Build Coastguard Worker }
1050*795d594fSAndroid Build Coastguard Worker dex::TypeIndex return_type_idx;
1051*795d594fSAndroid Build Coastguard Worker std::vector<dex::TypeIndex> param_type_idxs;
1052*795d594fSAndroid Build Coastguard Worker if (!dex_file->CreateTypeList(signature, &return_type_idx, ¶m_type_idxs)) {
1053*795d594fSAndroid Build Coastguard Worker LOG(WARNING) << "Could not create type list: " << signature;
1054*795d594fSAndroid Build Coastguard Worker return dex::kDexNoIndex;
1055*795d594fSAndroid Build Coastguard Worker }
1056*795d594fSAndroid Build Coastguard Worker const dex::ProtoId* proto_id = dex_file->FindProtoId(return_type_idx, param_type_idxs);
1057*795d594fSAndroid Build Coastguard Worker if (proto_id == nullptr) {
1058*795d594fSAndroid Build Coastguard Worker LOG(WARNING) << "Could not find proto_id: " << name;
1059*795d594fSAndroid Build Coastguard Worker return dex::kDexNoIndex;
1060*795d594fSAndroid Build Coastguard Worker }
1061*795d594fSAndroid Build Coastguard Worker const dex::MethodId* method_id = dex_file->FindMethodId(
1062*795d594fSAndroid Build Coastguard Worker dex_file->GetTypeId(class_ref.TypeIndex()), *name_id, *proto_id);
1063*795d594fSAndroid Build Coastguard Worker if (method_id == nullptr) {
1064*795d594fSAndroid Build Coastguard Worker LOG(WARNING) << "Could not find method_id: " << name;
1065*795d594fSAndroid Build Coastguard Worker return dex::kDexNoIndex;
1066*795d594fSAndroid Build Coastguard Worker }
1067*795d594fSAndroid Build Coastguard Worker
1068*795d594fSAndroid Build Coastguard Worker return dex_file->GetIndexForMethodId(*method_id);
1069*795d594fSAndroid Build Coastguard Worker }
1070*795d594fSAndroid Build Coastguard Worker
1071*795d594fSAndroid Build Coastguard Worker template <typename Visitor>
VisitAllInstructions(const TypeReference & class_ref,uint16_t method_idx,Visitor visitor)1072*795d594fSAndroid Build Coastguard Worker void VisitAllInstructions(const TypeReference& class_ref, uint16_t method_idx, Visitor visitor) {
1073*795d594fSAndroid Build Coastguard Worker const DexFile* dex_file = class_ref.dex_file;
1074*795d594fSAndroid Build Coastguard Worker const dex::ClassDef* def = dex_file->FindClassDef(class_ref.TypeIndex());
1075*795d594fSAndroid Build Coastguard Worker if (def == nullptr) {
1076*795d594fSAndroid Build Coastguard Worker return;
1077*795d594fSAndroid Build Coastguard Worker }
1078*795d594fSAndroid Build Coastguard Worker std::optional<uint32_t> offset = dex_file->GetCodeItemOffset(*def, method_idx);
1079*795d594fSAndroid Build Coastguard Worker if (offset.has_value()) {
1080*795d594fSAndroid Build Coastguard Worker for (const DexInstructionPcPair& inst :
1081*795d594fSAndroid Build Coastguard Worker CodeItemInstructionAccessor(*dex_file, dex_file->GetCodeItem(*offset))) {
1082*795d594fSAndroid Build Coastguard Worker if (!visitor(inst)) {
1083*795d594fSAndroid Build Coastguard Worker break;
1084*795d594fSAndroid Build Coastguard Worker }
1085*795d594fSAndroid Build Coastguard Worker }
1086*795d594fSAndroid Build Coastguard Worker } else {
1087*795d594fSAndroid Build Coastguard Worker LOG(WARNING) << "Could not find method " << method_idx;
1088*795d594fSAndroid Build Coastguard Worker }
1089*795d594fSAndroid Build Coastguard Worker }
1090*795d594fSAndroid Build Coastguard Worker
1091*795d594fSAndroid Build Coastguard Worker // Get dex-pcs of any virtual + interface invokes referencing a method of the
1092*795d594fSAndroid Build Coastguard Worker // 'target' type in the given method.
GetAllInvokes(const TypeReference & class_ref,uint16_t method_idx,dex::TypeIndex target,std::vector<uint32_t> * dex_pcs)1093*795d594fSAndroid Build Coastguard Worker void GetAllInvokes(const TypeReference& class_ref,
1094*795d594fSAndroid Build Coastguard Worker uint16_t method_idx,
1095*795d594fSAndroid Build Coastguard Worker dex::TypeIndex target,
1096*795d594fSAndroid Build Coastguard Worker /*out*/ std::vector<uint32_t>* dex_pcs) {
1097*795d594fSAndroid Build Coastguard Worker const DexFile* dex_file = class_ref.dex_file;
1098*795d594fSAndroid Build Coastguard Worker VisitAllInstructions(class_ref, method_idx, [&](const DexInstructionPcPair& inst) -> bool {
1099*795d594fSAndroid Build Coastguard Worker switch (inst->Opcode()) {
1100*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_INTERFACE:
1101*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_INTERFACE_RANGE:
1102*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_VIRTUAL:
1103*795d594fSAndroid Build Coastguard Worker case Instruction::INVOKE_VIRTUAL_RANGE: {
1104*795d594fSAndroid Build Coastguard Worker const dex::MethodId& meth = dex_file->GetMethodId(inst->VRegB());
1105*795d594fSAndroid Build Coastguard Worker if (meth.class_idx_ == target) {
1106*795d594fSAndroid Build Coastguard Worker dex_pcs->push_back(inst.DexPc());
1107*795d594fSAndroid Build Coastguard Worker }
1108*795d594fSAndroid Build Coastguard Worker break;
1109*795d594fSAndroid Build Coastguard Worker }
1110*795d594fSAndroid Build Coastguard Worker default:
1111*795d594fSAndroid Build Coastguard Worker break;
1112*795d594fSAndroid Build Coastguard Worker }
1113*795d594fSAndroid Build Coastguard Worker return true;
1114*795d594fSAndroid Build Coastguard Worker });
1115*795d594fSAndroid Build Coastguard Worker }
1116*795d594fSAndroid Build Coastguard Worker
1117*795d594fSAndroid Build Coastguard Worker // Given a method, return true if the method has a single INVOKE_VIRTUAL in its byte code.
1118*795d594fSAndroid Build Coastguard Worker // Upon success it returns true and stores the method index and the invoke dex pc
1119*795d594fSAndroid Build Coastguard Worker // in the output parameters.
1120*795d594fSAndroid Build Coastguard Worker // The format of the method spec is "inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;".
HasSingleInvoke(const TypeReference & class_ref,uint16_t method_index,uint32_t * dex_pc)1121*795d594fSAndroid Build Coastguard Worker bool HasSingleInvoke(const TypeReference& class_ref,
1122*795d594fSAndroid Build Coastguard Worker uint16_t method_index,
1123*795d594fSAndroid Build Coastguard Worker /*out*/ uint32_t* dex_pc) {
1124*795d594fSAndroid Build Coastguard Worker bool found_invoke = false;
1125*795d594fSAndroid Build Coastguard Worker bool found_multiple_invokes = false;
1126*795d594fSAndroid Build Coastguard Worker VisitAllInstructions(class_ref, method_index, [&](const DexInstructionPcPair& inst) -> bool {
1127*795d594fSAndroid Build Coastguard Worker if (inst->Opcode() == Instruction::INVOKE_VIRTUAL ||
1128*795d594fSAndroid Build Coastguard Worker inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
1129*795d594fSAndroid Build Coastguard Worker inst->Opcode() == Instruction::INVOKE_INTERFACE ||
1130*795d594fSAndroid Build Coastguard Worker inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE) {
1131*795d594fSAndroid Build Coastguard Worker if (found_invoke) {
1132*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "Multiple invoke INVOKE_VIRTUAL found: "
1133*795d594fSAndroid Build Coastguard Worker << class_ref.dex_file->PrettyMethod(method_index);
1134*795d594fSAndroid Build Coastguard Worker return false;
1135*795d594fSAndroid Build Coastguard Worker }
1136*795d594fSAndroid Build Coastguard Worker found_invoke = true;
1137*795d594fSAndroid Build Coastguard Worker *dex_pc = inst.DexPc();
1138*795d594fSAndroid Build Coastguard Worker }
1139*795d594fSAndroid Build Coastguard Worker return true;
1140*795d594fSAndroid Build Coastguard Worker });
1141*795d594fSAndroid Build Coastguard Worker if (!found_invoke) {
1142*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "Could not find any INVOKE_VIRTUAL/INTERFACE: "
1143*795d594fSAndroid Build Coastguard Worker << class_ref.dex_file->PrettyMethod(method_index);
1144*795d594fSAndroid Build Coastguard Worker }
1145*795d594fSAndroid Build Coastguard Worker return found_invoke && !found_multiple_invokes;
1146*795d594fSAndroid Build Coastguard Worker }
1147*795d594fSAndroid Build Coastguard Worker
1148*795d594fSAndroid Build Coastguard Worker struct InlineCacheSegment {
1149*795d594fSAndroid Build Coastguard Worker public:
1150*795d594fSAndroid Build Coastguard Worker using IcArray =
1151*795d594fSAndroid Build Coastguard Worker std::array<std::string_view, ProfileCompilationInfo::kIndividualInlineCacheSize + 1>;
SplitInlineCacheSegmentart::ProfMan::InlineCacheSegment1152*795d594fSAndroid Build Coastguard Worker static void SplitInlineCacheSegment(std::string_view ic_line,
1153*795d594fSAndroid Build Coastguard Worker /*out*/ std::vector<InlineCacheSegment>* res) {
1154*795d594fSAndroid Build Coastguard Worker if (ic_line[0] != kProfileParsingInlineChacheTargetSep) {
1155*795d594fSAndroid Build Coastguard Worker // single target
1156*795d594fSAndroid Build Coastguard Worker InlineCacheSegment out;
1157*795d594fSAndroid Build Coastguard Worker Split(ic_line, kProfileParsingTypeSep, &out.inline_caches_);
1158*795d594fSAndroid Build Coastguard Worker res->push_back(out);
1159*795d594fSAndroid Build Coastguard Worker return;
1160*795d594fSAndroid Build Coastguard Worker }
1161*795d594fSAndroid Build Coastguard Worker std::vector<std::string_view> targets_and_resolutions;
1162*795d594fSAndroid Build Coastguard Worker // Avoid a zero-length entry.
1163*795d594fSAndroid Build Coastguard Worker for (std::string_view t :
1164*795d594fSAndroid Build Coastguard Worker SplitString(ic_line.substr(1), kProfileParsingInlineChacheTargetSep)) {
1165*795d594fSAndroid Build Coastguard Worker InlineCacheSegment out;
1166*795d594fSAndroid Build Coastguard Worker // The target may be an array for methods defined in `j.l.Object`, such as `clone()`.
1167*795d594fSAndroid Build Coastguard Worker size_t recv_end;
1168*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(t[0] == '[')) {
1169*795d594fSAndroid Build Coastguard Worker recv_end = t.find_first_not_of('[', 1u);
1170*795d594fSAndroid Build Coastguard Worker DCHECK_NE(recv_end, std::string_view::npos);
1171*795d594fSAndroid Build Coastguard Worker if (t[recv_end] == 'L') {
1172*795d594fSAndroid Build Coastguard Worker recv_end = t.find_first_of(';', recv_end + 1u);
1173*795d594fSAndroid Build Coastguard Worker DCHECK_NE(recv_end, std::string_view::npos);
1174*795d594fSAndroid Build Coastguard Worker } else {
1175*795d594fSAndroid Build Coastguard Worker // Primitive array.
1176*795d594fSAndroid Build Coastguard Worker DCHECK_NE(Primitive::GetType(t[recv_end]), Primitive::kPrimNot);
1177*795d594fSAndroid Build Coastguard Worker }
1178*795d594fSAndroid Build Coastguard Worker } else {
1179*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(t[0], 'L') << "Target is not a class? " << t;
1180*795d594fSAndroid Build Coastguard Worker recv_end = t.find_first_of(';', 1u);
1181*795d594fSAndroid Build Coastguard Worker DCHECK_NE(recv_end, std::string_view::npos);
1182*795d594fSAndroid Build Coastguard Worker }
1183*795d594fSAndroid Build Coastguard Worker out.receiver_ = t.substr(0, recv_end + 1);
1184*795d594fSAndroid Build Coastguard Worker Split(t.substr(recv_end + 1), kProfileParsingTypeSep, &out.inline_caches_);
1185*795d594fSAndroid Build Coastguard Worker res->push_back(out);
1186*795d594fSAndroid Build Coastguard Worker }
1187*795d594fSAndroid Build Coastguard Worker }
1188*795d594fSAndroid Build Coastguard Worker
IsSingleReceiverart::ProfMan::InlineCacheSegment1189*795d594fSAndroid Build Coastguard Worker bool IsSingleReceiver() const {
1190*795d594fSAndroid Build Coastguard Worker return !receiver_.has_value();
1191*795d594fSAndroid Build Coastguard Worker }
1192*795d594fSAndroid Build Coastguard Worker
GetReceiverTypeart::ProfMan::InlineCacheSegment1193*795d594fSAndroid Build Coastguard Worker std::string_view GetReceiverType() const {
1194*795d594fSAndroid Build Coastguard Worker DCHECK(!IsSingleReceiver());
1195*795d594fSAndroid Build Coastguard Worker return *receiver_;
1196*795d594fSAndroid Build Coastguard Worker }
1197*795d594fSAndroid Build Coastguard Worker
GetIcTargetsart::ProfMan::InlineCacheSegment1198*795d594fSAndroid Build Coastguard Worker const IcArray& GetIcTargets() const {
1199*795d594fSAndroid Build Coastguard Worker return inline_caches_;
1200*795d594fSAndroid Build Coastguard Worker }
1201*795d594fSAndroid Build Coastguard Worker
NumIcTargetsart::ProfMan::InlineCacheSegment1202*795d594fSAndroid Build Coastguard Worker size_t NumIcTargets() const {
1203*795d594fSAndroid Build Coastguard Worker return std::count_if(
1204*795d594fSAndroid Build Coastguard Worker inline_caches_.begin(), inline_caches_.end(), [](const auto& x) { return !x.empty(); });
1205*795d594fSAndroid Build Coastguard Worker }
1206*795d594fSAndroid Build Coastguard Worker
Dumpart::ProfMan::InlineCacheSegment1207*795d594fSAndroid Build Coastguard Worker std::ostream& Dump(std::ostream& os) const {
1208*795d594fSAndroid Build Coastguard Worker if (!IsSingleReceiver()) {
1209*795d594fSAndroid Build Coastguard Worker os << "[" << GetReceiverType();
1210*795d594fSAndroid Build Coastguard Worker }
1211*795d594fSAndroid Build Coastguard Worker bool first = true;
1212*795d594fSAndroid Build Coastguard Worker for (std::string_view target : inline_caches_) {
1213*795d594fSAndroid Build Coastguard Worker if (target.empty()) {
1214*795d594fSAndroid Build Coastguard Worker break;
1215*795d594fSAndroid Build Coastguard Worker } else if (!first) {
1216*795d594fSAndroid Build Coastguard Worker os << ",";
1217*795d594fSAndroid Build Coastguard Worker }
1218*795d594fSAndroid Build Coastguard Worker first = false;
1219*795d594fSAndroid Build Coastguard Worker os << target;
1220*795d594fSAndroid Build Coastguard Worker }
1221*795d594fSAndroid Build Coastguard Worker return os;
1222*795d594fSAndroid Build Coastguard Worker }
1223*795d594fSAndroid Build Coastguard Worker
1224*795d594fSAndroid Build Coastguard Worker private:
1225*795d594fSAndroid Build Coastguard Worker std::optional<std::string_view> receiver_;
1226*795d594fSAndroid Build Coastguard Worker // Max number of ics in the profile file. Don't need to store more than this
1227*795d594fSAndroid Build Coastguard Worker // (although internally we can have as many as we want). If we fill this up
1228*795d594fSAndroid Build Coastguard Worker // we are megamorphic.
1229*795d594fSAndroid Build Coastguard Worker IcArray inline_caches_;
1230*795d594fSAndroid Build Coastguard Worker
1231*795d594fSAndroid Build Coastguard Worker friend std::ostream& operator<<(std::ostream& os, const InlineCacheSegment& ics);
1232*795d594fSAndroid Build Coastguard Worker };
1233*795d594fSAndroid Build Coastguard Worker
1234*795d594fSAndroid Build Coastguard Worker struct ClassMethodReference {
1235*795d594fSAndroid Build Coastguard Worker TypeReference type_;
1236*795d594fSAndroid Build Coastguard Worker uint32_t method_index_;
1237*795d594fSAndroid Build Coastguard Worker
operator ==art::ProfMan::ClassMethodReference1238*795d594fSAndroid Build Coastguard Worker bool operator==(const ClassMethodReference& ref) {
1239*795d594fSAndroid Build Coastguard Worker return ref.type_ == type_ && ref.method_index_ == method_index_;
1240*795d594fSAndroid Build Coastguard Worker }
operator !=art::ProfMan::ClassMethodReference1241*795d594fSAndroid Build Coastguard Worker bool operator!=(const ClassMethodReference& ref) {
1242*795d594fSAndroid Build Coastguard Worker return !(*this == ref);
1243*795d594fSAndroid Build Coastguard Worker }
1244*795d594fSAndroid Build Coastguard Worker };
1245*795d594fSAndroid Build Coastguard Worker
1246*795d594fSAndroid Build Coastguard Worker // Try to perform simple method resolution to produce a more useful profile.
1247*795d594fSAndroid Build Coastguard Worker // This will resolve to the nearest class+method-index which is within the
1248*795d594fSAndroid Build Coastguard Worker // same dexfile and in a declared supertype of the starting class. It will
1249*795d594fSAndroid Build Coastguard Worker // return nullopt if it cannot find an appropriate method or the nearest
1250*795d594fSAndroid Build Coastguard Worker // possibility is private.
1251*795d594fSAndroid Build Coastguard Worker // TODO: This should ideally support looking in other dex files. That's getting
1252*795d594fSAndroid Build Coastguard Worker // to the point of needing to have a whole class-linker so it's probably not
1253*795d594fSAndroid Build Coastguard Worker // worth it.
ResolveMethod(TypeReference class_ref,uint32_t method_index)1254*795d594fSAndroid Build Coastguard Worker std::optional<ClassMethodReference> ResolveMethod(TypeReference class_ref,
1255*795d594fSAndroid Build Coastguard Worker uint32_t method_index) {
1256*795d594fSAndroid Build Coastguard Worker const DexFile* dex = class_ref.dex_file;
1257*795d594fSAndroid Build Coastguard Worker const dex::ClassDef* def = dex->FindClassDef(class_ref.TypeIndex());
1258*795d594fSAndroid Build Coastguard Worker if (def == nullptr || method_index >= dex->NumMethodIds()) {
1259*795d594fSAndroid Build Coastguard Worker // Class not in dex-file.
1260*795d594fSAndroid Build Coastguard Worker return std::nullopt;
1261*795d594fSAndroid Build Coastguard Worker }
1262*795d594fSAndroid Build Coastguard Worker if (dex->GetClassData(*def) == nullptr) {
1263*795d594fSAndroid Build Coastguard Worker // Class has no fields or methods.
1264*795d594fSAndroid Build Coastguard Worker return std::nullopt;
1265*795d594fSAndroid Build Coastguard Worker }
1266*795d594fSAndroid Build Coastguard Worker if (LIKELY(dex->GetCodeItemOffset(*def, method_index).has_value())) {
1267*795d594fSAndroid Build Coastguard Worker return ClassMethodReference{class_ref, method_index};
1268*795d594fSAndroid Build Coastguard Worker }
1269*795d594fSAndroid Build Coastguard Worker // What to look for.
1270*795d594fSAndroid Build Coastguard Worker const dex::MethodId& method_id = dex->GetMethodId(method_index);
1271*795d594fSAndroid Build Coastguard Worker // No going between different dexs so use name and proto directly
1272*795d594fSAndroid Build Coastguard Worker const dex::ProtoIndex& method_proto = method_id.proto_idx_;
1273*795d594fSAndroid Build Coastguard Worker const dex::StringIndex& method_name = method_id.name_idx_;
1274*795d594fSAndroid Build Coastguard Worker // Floyd's algo to prevent infinite loops.
1275*795d594fSAndroid Build Coastguard Worker // Slow-iterator position for Floyd's
1276*795d594fSAndroid Build Coastguard Worker dex::TypeIndex slow_class_type = def->class_idx_;
1277*795d594fSAndroid Build Coastguard Worker // Whether to take a step with the slow iterator.
1278*795d594fSAndroid Build Coastguard Worker bool update_slow = false;
1279*795d594fSAndroid Build Coastguard Worker for (dex::TypeIndex cur_candidate = def->superclass_idx_;
1280*795d594fSAndroid Build Coastguard Worker cur_candidate != dex::TypeIndex::Invalid() && cur_candidate != slow_class_type;) {
1281*795d594fSAndroid Build Coastguard Worker const dex::ClassDef* cur_class_def = dex->FindClassDef(cur_candidate);
1282*795d594fSAndroid Build Coastguard Worker if (cur_class_def == nullptr) {
1283*795d594fSAndroid Build Coastguard Worker // We left the dex file.
1284*795d594fSAndroid Build Coastguard Worker return std::nullopt;
1285*795d594fSAndroid Build Coastguard Worker }
1286*795d594fSAndroid Build Coastguard Worker const dex::MethodId* cur_id =
1287*795d594fSAndroid Build Coastguard Worker dex->FindMethodIdByIndex(cur_candidate, method_name, method_proto);
1288*795d594fSAndroid Build Coastguard Worker if (cur_id != nullptr) {
1289*795d594fSAndroid Build Coastguard Worker if (dex->GetCodeItemOffset(*cur_class_def, dex->GetIndexForMethodId(*cur_id)).has_value()) {
1290*795d594fSAndroid Build Coastguard Worker return ClassMethodReference{TypeReference(dex, cur_candidate),
1291*795d594fSAndroid Build Coastguard Worker dex->GetIndexForMethodId(*cur_id)};
1292*795d594fSAndroid Build Coastguard Worker }
1293*795d594fSAndroid Build Coastguard Worker }
1294*795d594fSAndroid Build Coastguard Worker // Floyd's algo step.
1295*795d594fSAndroid Build Coastguard Worker cur_candidate = cur_class_def->superclass_idx_;
1296*795d594fSAndroid Build Coastguard Worker slow_class_type =
1297*795d594fSAndroid Build Coastguard Worker update_slow ? dex->FindClassDef(slow_class_type)->superclass_idx_ : slow_class_type;
1298*795d594fSAndroid Build Coastguard Worker update_slow = !update_slow;
1299*795d594fSAndroid Build Coastguard Worker }
1300*795d594fSAndroid Build Coastguard Worker return std::nullopt;
1301*795d594fSAndroid Build Coastguard Worker }
1302*795d594fSAndroid Build Coastguard Worker
1303*795d594fSAndroid Build Coastguard Worker // Process a line defining a class or a method and its inline caches.
1304*795d594fSAndroid Build Coastguard Worker // Upon success return true and add the class or the method info to profile.
1305*795d594fSAndroid Build Coastguard Worker // Inline caches are identified by the type of the declared receiver type.
1306*795d594fSAndroid Build Coastguard Worker // The possible line formats are:
1307*795d594fSAndroid Build Coastguard Worker // "LJustTheClass;".
1308*795d594fSAndroid Build Coastguard Worker // "LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;".
1309*795d594fSAndroid Build Coastguard Worker // "LTestInline;->inlineMissingTypes(LSuper;)I+missing_types".
1310*795d594fSAndroid Build Coastguard Worker // // Note no ',' after [LTarget;
1311*795d594fSAndroid Build Coastguard Worker // "LTestInline;->multiInlinePolymorphic(LSuper;)I+]LTarget1;LResA;,LResB;]LTarget2;LResC;,LResD;".
1312*795d594fSAndroid Build Coastguard Worker // "LTestInline;->multiInlinePolymorphic(LSuper;)I+]LTarget1;missing_types]LTarget2;LResC;,LResD;".
1313*795d594fSAndroid Build Coastguard Worker // "{annotation}LTestInline;->inlineNoInlineCaches(LSuper;)I".
1314*795d594fSAndroid Build Coastguard Worker // "LTestInline;->*".
1315*795d594fSAndroid Build Coastguard Worker // The method and classes are searched only in the given dex files.
ProcessLine(const std::vector<std::unique_ptr<const DexFile>> & dex_files,std::string_view maybe_annotated_line,ProfileCompilationInfo * profile)1316*795d594fSAndroid Build Coastguard Worker bool ProcessLine(const std::vector<std::unique_ptr<const DexFile>>& dex_files,
1317*795d594fSAndroid Build Coastguard Worker std::string_view maybe_annotated_line,
1318*795d594fSAndroid Build Coastguard Worker /*out*/ProfileCompilationInfo* profile) {
1319*795d594fSAndroid Build Coastguard Worker // First, process the annotation.
1320*795d594fSAndroid Build Coastguard Worker if (maybe_annotated_line.empty()) {
1321*795d594fSAndroid Build Coastguard Worker return true;
1322*795d594fSAndroid Build Coastguard Worker }
1323*795d594fSAndroid Build Coastguard Worker // Working line variable which will contain the user input without the annotations.
1324*795d594fSAndroid Build Coastguard Worker std::string_view line = maybe_annotated_line;
1325*795d594fSAndroid Build Coastguard Worker
1326*795d594fSAndroid Build Coastguard Worker std::string_view annotation_string;
1327*795d594fSAndroid Build Coastguard Worker if (maybe_annotated_line[0] == kAnnotationStart) {
1328*795d594fSAndroid Build Coastguard Worker size_t end_pos = maybe_annotated_line.find(kAnnotationEnd, 0);
1329*795d594fSAndroid Build Coastguard Worker if (end_pos == std::string::npos || end_pos == 0) {
1330*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "Invalid line: " << maybe_annotated_line;
1331*795d594fSAndroid Build Coastguard Worker return false;
1332*795d594fSAndroid Build Coastguard Worker }
1333*795d594fSAndroid Build Coastguard Worker annotation_string = maybe_annotated_line.substr(1, end_pos - 1);
1334*795d594fSAndroid Build Coastguard Worker // Update the working line.
1335*795d594fSAndroid Build Coastguard Worker line = maybe_annotated_line.substr(end_pos + 1);
1336*795d594fSAndroid Build Coastguard Worker }
1337*795d594fSAndroid Build Coastguard Worker
1338*795d594fSAndroid Build Coastguard Worker ProfileSampleAnnotation annotation = annotation_string.empty()
1339*795d594fSAndroid Build Coastguard Worker ? ProfileSampleAnnotation::kNone
1340*795d594fSAndroid Build Coastguard Worker : ProfileSampleAnnotation(std::string(annotation_string));
1341*795d594fSAndroid Build Coastguard Worker
1342*795d594fSAndroid Build Coastguard Worker // Now process the rest of the line.
1343*795d594fSAndroid Build Coastguard Worker std::string_view klass;
1344*795d594fSAndroid Build Coastguard Worker std::string_view method_str;
1345*795d594fSAndroid Build Coastguard Worker bool is_hot = false;
1346*795d594fSAndroid Build Coastguard Worker bool is_startup = false;
1347*795d594fSAndroid Build Coastguard Worker bool is_post_startup = false;
1348*795d594fSAndroid Build Coastguard Worker const size_t method_sep_index = line.find(kMethodSep, 0);
1349*795d594fSAndroid Build Coastguard Worker if (method_sep_index == std::string::npos) {
1350*795d594fSAndroid Build Coastguard Worker klass = line;
1351*795d594fSAndroid Build Coastguard Worker } else {
1352*795d594fSAndroid Build Coastguard Worker // The method prefix flags are only valid for method strings.
1353*795d594fSAndroid Build Coastguard Worker size_t start_index = 0;
1354*795d594fSAndroid Build Coastguard Worker while (start_index < line.size() && line[start_index] != 'L') {
1355*795d594fSAndroid Build Coastguard Worker const char c = line[start_index];
1356*795d594fSAndroid Build Coastguard Worker if (c == kMethodFlagStringHot) {
1357*795d594fSAndroid Build Coastguard Worker is_hot = true;
1358*795d594fSAndroid Build Coastguard Worker } else if (c == kMethodFlagStringStartup) {
1359*795d594fSAndroid Build Coastguard Worker is_startup = true;
1360*795d594fSAndroid Build Coastguard Worker } else if (c == kMethodFlagStringPostStartup) {
1361*795d594fSAndroid Build Coastguard Worker is_post_startup = true;
1362*795d594fSAndroid Build Coastguard Worker } else {
1363*795d594fSAndroid Build Coastguard Worker LOG(WARNING) << "Invalid flag " << c;
1364*795d594fSAndroid Build Coastguard Worker return false;
1365*795d594fSAndroid Build Coastguard Worker }
1366*795d594fSAndroid Build Coastguard Worker ++start_index;
1367*795d594fSAndroid Build Coastguard Worker }
1368*795d594fSAndroid Build Coastguard Worker klass = line.substr(start_index, method_sep_index - start_index);
1369*795d594fSAndroid Build Coastguard Worker method_str = line.substr(method_sep_index + kMethodSep.size());
1370*795d594fSAndroid Build Coastguard Worker }
1371*795d594fSAndroid Build Coastguard Worker
1372*795d594fSAndroid Build Coastguard Worker if (!IsValidDescriptor(std::string(klass).c_str())) {
1373*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "Invalid descriptor: " << klass;
1374*795d594fSAndroid Build Coastguard Worker return false;
1375*795d594fSAndroid Build Coastguard Worker }
1376*795d594fSAndroid Build Coastguard Worker
1377*795d594fSAndroid Build Coastguard Worker if (method_str.empty()) {
1378*795d594fSAndroid Build Coastguard Worker auto array_it = std::find_if(klass.begin(), klass.end(), [](char c) { return c != '['; });
1379*795d594fSAndroid Build Coastguard Worker size_t array_dim = std::distance(klass.begin(), array_it);
1380*795d594fSAndroid Build Coastguard Worker if (klass.size() == array_dim + 1u) {
1381*795d594fSAndroid Build Coastguard Worker // Attribute primitive types and their arrays to the first dex file.
1382*795d594fSAndroid Build Coastguard Worker profile->AddClass(*dex_files[0], klass, annotation);
1383*795d594fSAndroid Build Coastguard Worker return true;
1384*795d594fSAndroid Build Coastguard Worker }
1385*795d594fSAndroid Build Coastguard Worker // Attribute non-primitive classes and their arrays to the dex file with the definition.
1386*795d594fSAndroid Build Coastguard Worker TypeReference class_ref(/* dex_file= */ nullptr, dex::TypeIndex());
1387*795d594fSAndroid Build Coastguard Worker if (FindClassDef(dex_files, klass.substr(array_dim), &class_ref) == nullptr) {
1388*795d594fSAndroid Build Coastguard Worker LOG(WARNING) << "Could not find class definition: " << klass.substr(array_dim);
1389*795d594fSAndroid Build Coastguard Worker return false;
1390*795d594fSAndroid Build Coastguard Worker }
1391*795d594fSAndroid Build Coastguard Worker if (array_dim != 0) {
1392*795d594fSAndroid Build Coastguard Worker // Let the ProfileCompilationInfo find the type index or add an extra descriptor.
1393*795d594fSAndroid Build Coastguard Worker return profile->AddClass(*class_ref.dex_file, klass, annotation);
1394*795d594fSAndroid Build Coastguard Worker } else {
1395*795d594fSAndroid Build Coastguard Worker return profile->AddClass(*class_ref.dex_file, class_ref.TypeIndex(), annotation);
1396*795d594fSAndroid Build Coastguard Worker }
1397*795d594fSAndroid Build Coastguard Worker }
1398*795d594fSAndroid Build Coastguard Worker
1399*795d594fSAndroid Build Coastguard Worker DCHECK_NE(klass[0], '[');
1400*795d594fSAndroid Build Coastguard Worker TypeReference class_ref(/* dex_file= */ nullptr, dex::TypeIndex());
1401*795d594fSAndroid Build Coastguard Worker const dex::ClassDef* class_def = FindClassDef(dex_files, klass, &class_ref);
1402*795d594fSAndroid Build Coastguard Worker if (class_def == nullptr) {
1403*795d594fSAndroid Build Coastguard Worker LOG(WARNING) << "Could not find class definition: " << klass;
1404*795d594fSAndroid Build Coastguard Worker return false;
1405*795d594fSAndroid Build Coastguard Worker }
1406*795d594fSAndroid Build Coastguard Worker
1407*795d594fSAndroid Build Coastguard Worker uint32_t flags = 0;
1408*795d594fSAndroid Build Coastguard Worker if (is_hot) {
1409*795d594fSAndroid Build Coastguard Worker flags |= ProfileCompilationInfo::MethodHotness::kFlagHot;
1410*795d594fSAndroid Build Coastguard Worker }
1411*795d594fSAndroid Build Coastguard Worker if (is_startup) {
1412*795d594fSAndroid Build Coastguard Worker flags |= ProfileCompilationInfo::MethodHotness::kFlagStartup;
1413*795d594fSAndroid Build Coastguard Worker }
1414*795d594fSAndroid Build Coastguard Worker if (is_post_startup) {
1415*795d594fSAndroid Build Coastguard Worker flags |= ProfileCompilationInfo::MethodHotness::kFlagPostStartup;
1416*795d594fSAndroid Build Coastguard Worker }
1417*795d594fSAndroid Build Coastguard Worker
1418*795d594fSAndroid Build Coastguard Worker if (method_str == kClassAllMethods) {
1419*795d594fSAndroid Build Coastguard Worker // Start by adding the class.
1420*795d594fSAndroid Build Coastguard Worker profile->AddClass(*class_ref.dex_file, class_ref.TypeIndex(), annotation);
1421*795d594fSAndroid Build Coastguard Worker uint16_t class_def_index = class_ref.dex_file->GetIndexForClassDef(*class_def);
1422*795d594fSAndroid Build Coastguard Worker ClassAccessor accessor(*class_ref.dex_file, class_def_index);
1423*795d594fSAndroid Build Coastguard Worker std::vector<ProfileMethodInfo> methods;
1424*795d594fSAndroid Build Coastguard Worker for (const ClassAccessor::Method& method : accessor.GetMethods()) {
1425*795d594fSAndroid Build Coastguard Worker if (method.GetCodeItemOffset() != 0) {
1426*795d594fSAndroid Build Coastguard Worker // Add all of the methods that have code to the profile.
1427*795d594fSAndroid Build Coastguard Worker methods.push_back(ProfileMethodInfo(method.GetReference()));
1428*795d594fSAndroid Build Coastguard Worker }
1429*795d594fSAndroid Build Coastguard Worker }
1430*795d594fSAndroid Build Coastguard Worker // TODO: Check return value?
1431*795d594fSAndroid Build Coastguard Worker profile->AddMethods(
1432*795d594fSAndroid Build Coastguard Worker methods, static_cast<ProfileCompilationInfo::MethodHotness::Flag>(flags), annotation);
1433*795d594fSAndroid Build Coastguard Worker return true;
1434*795d594fSAndroid Build Coastguard Worker }
1435*795d594fSAndroid Build Coastguard Worker
1436*795d594fSAndroid Build Coastguard Worker // Process the method.
1437*795d594fSAndroid Build Coastguard Worker std::string method_spec;
1438*795d594fSAndroid Build Coastguard Worker
1439*795d594fSAndroid Build Coastguard Worker // If none of the flags are set, default to hot.
1440*795d594fSAndroid Build Coastguard Worker // TODO: Why is this done after we have already calculated `flags`?
1441*795d594fSAndroid Build Coastguard Worker is_hot = is_hot || (!is_hot && !is_startup && !is_post_startup);
1442*795d594fSAndroid Build Coastguard Worker
1443*795d594fSAndroid Build Coastguard Worker // Lifetime of segments is same as method_elems since it contains pointers into the string-data
1444*795d594fSAndroid Build Coastguard Worker std::vector<InlineCacheSegment> segments;
1445*795d594fSAndroid Build Coastguard Worker std::vector<std::string_view> method_elems;
1446*795d594fSAndroid Build Coastguard Worker Split(method_str, kProfileParsingInlineChacheSep, &method_elems);
1447*795d594fSAndroid Build Coastguard Worker if (method_elems.size() == 2) {
1448*795d594fSAndroid Build Coastguard Worker method_spec = method_elems[0];
1449*795d594fSAndroid Build Coastguard Worker InlineCacheSegment::SplitInlineCacheSegment(method_elems[1], &segments);
1450*795d594fSAndroid Build Coastguard Worker } else if (method_elems.size() == 1) {
1451*795d594fSAndroid Build Coastguard Worker method_spec = method_elems[0];
1452*795d594fSAndroid Build Coastguard Worker } else {
1453*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "Invalid method line: " << line;
1454*795d594fSAndroid Build Coastguard Worker return false;
1455*795d594fSAndroid Build Coastguard Worker }
1456*795d594fSAndroid Build Coastguard Worker
1457*795d594fSAndroid Build Coastguard Worker const uint32_t method_index = FindMethodIndex(class_ref, method_spec);
1458*795d594fSAndroid Build Coastguard Worker if (method_index == dex::kDexNoIndex) {
1459*795d594fSAndroid Build Coastguard Worker LOG(WARNING) << "Could not find method " << klass << "->" << method_spec;
1460*795d594fSAndroid Build Coastguard Worker return false;
1461*795d594fSAndroid Build Coastguard Worker }
1462*795d594fSAndroid Build Coastguard Worker
1463*795d594fSAndroid Build Coastguard Worker std::optional<ClassMethodReference>
1464*795d594fSAndroid Build Coastguard Worker resolved_class_method_ref = ResolveMethod(class_ref, method_index);
1465*795d594fSAndroid Build Coastguard Worker
1466*795d594fSAndroid Build Coastguard Worker std::vector<ProfileMethodInfo::ProfileInlineCache> inline_caches;
1467*795d594fSAndroid Build Coastguard Worker // We can only create inline-caches when we actually have code we can
1468*795d594fSAndroid Build Coastguard Worker // examine. If we couldn't resolve the method don't bother trying to create
1469*795d594fSAndroid Build Coastguard Worker // inline-caches.
1470*795d594fSAndroid Build Coastguard Worker if (resolved_class_method_ref) {
1471*795d594fSAndroid Build Coastguard Worker for (const InlineCacheSegment& segment : segments) {
1472*795d594fSAndroid Build Coastguard Worker std::vector<uint32_t> dex_pcs;
1473*795d594fSAndroid Build Coastguard Worker if (segment.IsSingleReceiver()) {
1474*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(segments.size(), 1u);
1475*795d594fSAndroid Build Coastguard Worker dex_pcs.resize(1, -1);
1476*795d594fSAndroid Build Coastguard Worker // TODO This single invoke format should really be phased out and
1477*795d594fSAndroid Build Coastguard Worker // removed.
1478*795d594fSAndroid Build Coastguard Worker if (!HasSingleInvoke(class_ref, method_index, &dex_pcs[0])) {
1479*795d594fSAndroid Build Coastguard Worker return false;
1480*795d594fSAndroid Build Coastguard Worker }
1481*795d594fSAndroid Build Coastguard Worker } else {
1482*795d594fSAndroid Build Coastguard Worker // Get the type-ref the method code will use.
1483*795d594fSAndroid Build Coastguard Worker std::string_view receiver_descriptor = segment.GetReceiverType();
1484*795d594fSAndroid Build Coastguard Worker const dex::TypeId *type_id = class_ref.dex_file->FindTypeId(receiver_descriptor);
1485*795d594fSAndroid Build Coastguard Worker if (type_id == nullptr) {
1486*795d594fSAndroid Build Coastguard Worker LOG(WARNING) << "Could not find class: "
1487*795d594fSAndroid Build Coastguard Worker << segment.GetReceiverType() << " in dex-file "
1488*795d594fSAndroid Build Coastguard Worker << class_ref.dex_file << ". Ignoring IC group: '"
1489*795d594fSAndroid Build Coastguard Worker << segment << "'";
1490*795d594fSAndroid Build Coastguard Worker continue;
1491*795d594fSAndroid Build Coastguard Worker }
1492*795d594fSAndroid Build Coastguard Worker dex::TypeIndex target_index =
1493*795d594fSAndroid Build Coastguard Worker class_ref.dex_file->GetIndexForTypeId(*type_id);
1494*795d594fSAndroid Build Coastguard Worker
1495*795d594fSAndroid Build Coastguard Worker GetAllInvokes(resolved_class_method_ref->type_,
1496*795d594fSAndroid Build Coastguard Worker resolved_class_method_ref->method_index_,
1497*795d594fSAndroid Build Coastguard Worker target_index,
1498*795d594fSAndroid Build Coastguard Worker &dex_pcs);
1499*795d594fSAndroid Build Coastguard Worker }
1500*795d594fSAndroid Build Coastguard Worker bool missing_types = segment.GetIcTargets()[0] == kMissingTypesMarker;
1501*795d594fSAndroid Build Coastguard Worker bool megamorphic_types =
1502*795d594fSAndroid Build Coastguard Worker segment.GetIcTargets()[0] == kMegamorphicTypesMarker;
1503*795d594fSAndroid Build Coastguard Worker std::vector<TypeReference> classes;
1504*795d594fSAndroid Build Coastguard Worker if (!missing_types && !megamorphic_types) {
1505*795d594fSAndroid Build Coastguard Worker classes.reserve(segment.NumIcTargets());
1506*795d594fSAndroid Build Coastguard Worker for (const std::string_view& ic_class : segment.GetIcTargets()) {
1507*795d594fSAndroid Build Coastguard Worker if (ic_class.empty()) {
1508*795d594fSAndroid Build Coastguard Worker break;
1509*795d594fSAndroid Build Coastguard Worker }
1510*795d594fSAndroid Build Coastguard Worker if (!IsValidDescriptor(std::string(ic_class).c_str())) {
1511*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "Invalid descriptor for inline cache: " << ic_class;
1512*795d594fSAndroid Build Coastguard Worker return false;
1513*795d594fSAndroid Build Coastguard Worker }
1514*795d594fSAndroid Build Coastguard Worker // TODO: Allow referencing classes without a `dex::TypeId` in any of the dex files.
1515*795d594fSAndroid Build Coastguard Worker TypeReference ic_class_ref(/* dex_file= */ nullptr, dex::TypeIndex());
1516*795d594fSAndroid Build Coastguard Worker if (!FindClass(dex_files, ic_class, &ic_class_ref)) {
1517*795d594fSAndroid Build Coastguard Worker LOG(segment.IsSingleReceiver() ? ERROR : WARNING)
1518*795d594fSAndroid Build Coastguard Worker << "Could not find class: " << ic_class << " in " << segment;
1519*795d594fSAndroid Build Coastguard Worker if (segment.IsSingleReceiver()) {
1520*795d594fSAndroid Build Coastguard Worker return false;
1521*795d594fSAndroid Build Coastguard Worker } else {
1522*795d594fSAndroid Build Coastguard Worker // Be a bit more forgiving with profiles from servers.
1523*795d594fSAndroid Build Coastguard Worker missing_types = true;
1524*795d594fSAndroid Build Coastguard Worker classes.clear();
1525*795d594fSAndroid Build Coastguard Worker break;
1526*795d594fSAndroid Build Coastguard Worker }
1527*795d594fSAndroid Build Coastguard Worker }
1528*795d594fSAndroid Build Coastguard Worker classes.push_back(ic_class_ref);
1529*795d594fSAndroid Build Coastguard Worker }
1530*795d594fSAndroid Build Coastguard Worker }
1531*795d594fSAndroid Build Coastguard Worker for (size_t dex_pc : dex_pcs) {
1532*795d594fSAndroid Build Coastguard Worker inline_caches.emplace_back(dex_pc, missing_types, classes, megamorphic_types);
1533*795d594fSAndroid Build Coastguard Worker }
1534*795d594fSAndroid Build Coastguard Worker }
1535*795d594fSAndroid Build Coastguard Worker }
1536*795d594fSAndroid Build Coastguard Worker MethodReference ref(class_ref.dex_file, method_index);
1537*795d594fSAndroid Build Coastguard Worker if (is_hot) {
1538*795d594fSAndroid Build Coastguard Worker ClassMethodReference orig_cmr { class_ref, method_index };
1539*795d594fSAndroid Build Coastguard Worker if (!inline_caches.empty() &&
1540*795d594fSAndroid Build Coastguard Worker resolved_class_method_ref &&
1541*795d594fSAndroid Build Coastguard Worker orig_cmr != *resolved_class_method_ref) {
1542*795d594fSAndroid Build Coastguard Worker // We have inline-caches on a method that doesn't actually exist. We
1543*795d594fSAndroid Build Coastguard Worker // want to put the inline caches on the resolved version of the method
1544*795d594fSAndroid Build Coastguard Worker // (if we could find one) and just mark the actual method as present.
1545*795d594fSAndroid Build Coastguard Worker const DexFile *dex = resolved_class_method_ref->type_.dex_file;
1546*795d594fSAndroid Build Coastguard Worker LOG(VERBOSE) << "Adding "
1547*795d594fSAndroid Build Coastguard Worker << dex->PrettyMethod(
1548*795d594fSAndroid Build Coastguard Worker resolved_class_method_ref->method_index_)
1549*795d594fSAndroid Build Coastguard Worker << " as alias for " << dex->PrettyMethod(method_index);
1550*795d594fSAndroid Build Coastguard Worker // The inline-cache refers to a supertype of the actual profile line.
1551*795d594fSAndroid Build Coastguard Worker // Include this supertype method in the profile as well.
1552*795d594fSAndroid Build Coastguard Worker MethodReference resolved_ref(class_ref.dex_file,
1553*795d594fSAndroid Build Coastguard Worker resolved_class_method_ref->method_index_);
1554*795d594fSAndroid Build Coastguard Worker profile->AddMethod(
1555*795d594fSAndroid Build Coastguard Worker ProfileMethodInfo(resolved_ref, inline_caches),
1556*795d594fSAndroid Build Coastguard Worker static_cast<ProfileCompilationInfo::MethodHotness::Flag>(flags),
1557*795d594fSAndroid Build Coastguard Worker annotation);
1558*795d594fSAndroid Build Coastguard Worker profile->AddMethod(
1559*795d594fSAndroid Build Coastguard Worker ProfileMethodInfo(ref),
1560*795d594fSAndroid Build Coastguard Worker static_cast<ProfileCompilationInfo::MethodHotness::Flag>(flags),
1561*795d594fSAndroid Build Coastguard Worker annotation);
1562*795d594fSAndroid Build Coastguard Worker } else {
1563*795d594fSAndroid Build Coastguard Worker profile->AddMethod(
1564*795d594fSAndroid Build Coastguard Worker ProfileMethodInfo(ref, inline_caches),
1565*795d594fSAndroid Build Coastguard Worker static_cast<ProfileCompilationInfo::MethodHotness::Flag>(flags),
1566*795d594fSAndroid Build Coastguard Worker annotation);
1567*795d594fSAndroid Build Coastguard Worker }
1568*795d594fSAndroid Build Coastguard Worker }
1569*795d594fSAndroid Build Coastguard Worker if (flags != 0) {
1570*795d594fSAndroid Build Coastguard Worker if (!profile->AddMethod(ProfileMethodInfo(ref),
1571*795d594fSAndroid Build Coastguard Worker static_cast<ProfileCompilationInfo::MethodHotness::Flag>(flags),
1572*795d594fSAndroid Build Coastguard Worker annotation)) {
1573*795d594fSAndroid Build Coastguard Worker return false;
1574*795d594fSAndroid Build Coastguard Worker }
1575*795d594fSAndroid Build Coastguard Worker DCHECK(profile->GetMethodHotness(ref, annotation).IsInProfile()) << method_spec;
1576*795d594fSAndroid Build Coastguard Worker }
1577*795d594fSAndroid Build Coastguard Worker return true;
1578*795d594fSAndroid Build Coastguard Worker }
1579*795d594fSAndroid Build Coastguard Worker
ProcessBootLine(const std::vector<std::unique_ptr<const DexFile>> & dex_files,std::string_view line,ProfileBootInfo * boot_profiling_info)1580*795d594fSAndroid Build Coastguard Worker bool ProcessBootLine(const std::vector<std::unique_ptr<const DexFile>>& dex_files,
1581*795d594fSAndroid Build Coastguard Worker std::string_view line,
1582*795d594fSAndroid Build Coastguard Worker ProfileBootInfo* boot_profiling_info) {
1583*795d594fSAndroid Build Coastguard Worker const size_t method_sep_index = line.find(kMethodSep, 0);
1584*795d594fSAndroid Build Coastguard Worker if (method_sep_index == std::string_view::npos) {
1585*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "Invalid boot line: " << line;
1586*795d594fSAndroid Build Coastguard Worker return false;
1587*795d594fSAndroid Build Coastguard Worker }
1588*795d594fSAndroid Build Coastguard Worker std::string_view klass_str = line.substr(0, method_sep_index);
1589*795d594fSAndroid Build Coastguard Worker std::string_view method_str = line.substr(method_sep_index + kMethodSep.size());
1590*795d594fSAndroid Build Coastguard Worker
1591*795d594fSAndroid Build Coastguard Worker TypeReference class_ref(/* dex_file= */ nullptr, dex::TypeIndex());
1592*795d594fSAndroid Build Coastguard Worker if (FindClassDef(dex_files, klass_str, &class_ref) == nullptr) {
1593*795d594fSAndroid Build Coastguard Worker LOG(WARNING) << "Could not find class definition: " << klass_str;
1594*795d594fSAndroid Build Coastguard Worker return false;
1595*795d594fSAndroid Build Coastguard Worker }
1596*795d594fSAndroid Build Coastguard Worker
1597*795d594fSAndroid Build Coastguard Worker const uint32_t method_index = FindMethodIndex(class_ref, method_str);
1598*795d594fSAndroid Build Coastguard Worker if (method_index == dex::kDexNoIndex) {
1599*795d594fSAndroid Build Coastguard Worker LOG(WARNING) << "Could not find method: " << line;
1600*795d594fSAndroid Build Coastguard Worker return false;
1601*795d594fSAndroid Build Coastguard Worker }
1602*795d594fSAndroid Build Coastguard Worker boot_profiling_info->Add(class_ref.dex_file, method_index);
1603*795d594fSAndroid Build Coastguard Worker return true;
1604*795d594fSAndroid Build Coastguard Worker }
1605*795d594fSAndroid Build Coastguard Worker
OpenReferenceProfile() const1606*795d594fSAndroid Build Coastguard Worker int OpenReferenceProfile() const {
1607*795d594fSAndroid Build Coastguard Worker int fd = reference_profile_file_fd_;
1608*795d594fSAndroid Build Coastguard Worker if (!FdIsValid(fd)) {
1609*795d594fSAndroid Build Coastguard Worker CHECK(!reference_profile_file_.empty());
1610*795d594fSAndroid Build Coastguard Worker #ifdef _WIN32
1611*795d594fSAndroid Build Coastguard Worker int flags = O_CREAT | O_TRUNC | O_WRONLY;
1612*795d594fSAndroid Build Coastguard Worker #else
1613*795d594fSAndroid Build Coastguard Worker int flags = O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC;
1614*795d594fSAndroid Build Coastguard Worker #endif
1615*795d594fSAndroid Build Coastguard Worker fd = open(reference_profile_file_.c_str(), flags, 0644);
1616*795d594fSAndroid Build Coastguard Worker if (fd < 0) {
1617*795d594fSAndroid Build Coastguard Worker PLOG(ERROR) << "Cannot open " << reference_profile_file_;
1618*795d594fSAndroid Build Coastguard Worker return File::kInvalidFd;
1619*795d594fSAndroid Build Coastguard Worker }
1620*795d594fSAndroid Build Coastguard Worker }
1621*795d594fSAndroid Build Coastguard Worker return fd;
1622*795d594fSAndroid Build Coastguard Worker }
1623*795d594fSAndroid Build Coastguard Worker
1624*795d594fSAndroid Build Coastguard Worker // Create and store a ProfileBootInfo.
CreateBootProfile()1625*795d594fSAndroid Build Coastguard Worker int CreateBootProfile() {
1626*795d594fSAndroid Build Coastguard Worker // Validate parameters for this command.
1627*795d594fSAndroid Build Coastguard Worker if (apk_files_.empty() && apks_fd_.empty()) {
1628*795d594fSAndroid Build Coastguard Worker Usage("APK files must be specified");
1629*795d594fSAndroid Build Coastguard Worker }
1630*795d594fSAndroid Build Coastguard Worker if (dex_locations_.empty()) {
1631*795d594fSAndroid Build Coastguard Worker Usage("DEX locations must be specified");
1632*795d594fSAndroid Build Coastguard Worker }
1633*795d594fSAndroid Build Coastguard Worker if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
1634*795d594fSAndroid Build Coastguard Worker Usage("Reference profile must be specified with --reference-profile-file or "
1635*795d594fSAndroid Build Coastguard Worker "--reference-profile-file-fd");
1636*795d594fSAndroid Build Coastguard Worker }
1637*795d594fSAndroid Build Coastguard Worker if (!profile_files_.empty() || !profile_files_fd_.empty()) {
1638*795d594fSAndroid Build Coastguard Worker Usage("Profile must be specified with --reference-profile-file or "
1639*795d594fSAndroid Build Coastguard Worker "--reference-profile-file-fd");
1640*795d594fSAndroid Build Coastguard Worker }
1641*795d594fSAndroid Build Coastguard Worker // Open the profile output file if needed.
1642*795d594fSAndroid Build Coastguard Worker int fd = OpenReferenceProfile();
1643*795d594fSAndroid Build Coastguard Worker if (!FdIsValid(fd)) {
1644*795d594fSAndroid Build Coastguard Worker return -1;
1645*795d594fSAndroid Build Coastguard Worker }
1646*795d594fSAndroid Build Coastguard Worker // Read the user-specified list of methods.
1647*795d594fSAndroid Build Coastguard Worker std::unique_ptr<std::vector<std::string>>
1648*795d594fSAndroid Build Coastguard Worker user_lines(ReadCommentedInputFromFile<std::vector<std::string>>(
1649*795d594fSAndroid Build Coastguard Worker create_profile_from_file_.c_str(), nullptr)); // No post-processing.
1650*795d594fSAndroid Build Coastguard Worker
1651*795d594fSAndroid Build Coastguard Worker // Open the dex files to look up classes and methods.
1652*795d594fSAndroid Build Coastguard Worker std::vector<std::unique_ptr<const DexFile>> dex_files;
1653*795d594fSAndroid Build Coastguard Worker OpenApkFilesFromLocations(&dex_files);
1654*795d594fSAndroid Build Coastguard Worker
1655*795d594fSAndroid Build Coastguard Worker // Process the lines one by one and add the successful ones to the profile.
1656*795d594fSAndroid Build Coastguard Worker ProfileBootInfo info;
1657*795d594fSAndroid Build Coastguard Worker
1658*795d594fSAndroid Build Coastguard Worker for (const auto& line : *user_lines) {
1659*795d594fSAndroid Build Coastguard Worker ProcessBootLine(dex_files, line, &info);
1660*795d594fSAndroid Build Coastguard Worker }
1661*795d594fSAndroid Build Coastguard Worker
1662*795d594fSAndroid Build Coastguard Worker // Write the profile file.
1663*795d594fSAndroid Build Coastguard Worker CHECK(info.Save(fd));
1664*795d594fSAndroid Build Coastguard Worker
1665*795d594fSAndroid Build Coastguard Worker if (close(fd) < 0) {
1666*795d594fSAndroid Build Coastguard Worker PLOG(WARNING) << "Failed to close descriptor";
1667*795d594fSAndroid Build Coastguard Worker }
1668*795d594fSAndroid Build Coastguard Worker
1669*795d594fSAndroid Build Coastguard Worker return 0;
1670*795d594fSAndroid Build Coastguard Worker }
1671*795d594fSAndroid Build Coastguard Worker
1672*795d594fSAndroid Build Coastguard Worker // Creates a profile from a human friendly textual representation.
1673*795d594fSAndroid Build Coastguard Worker // The expected input format is:
1674*795d594fSAndroid Build Coastguard Worker // # Classes
1675*795d594fSAndroid Build Coastguard Worker // Ljava/lang/Comparable;
1676*795d594fSAndroid Build Coastguard Worker // Ljava/lang/Math;
1677*795d594fSAndroid Build Coastguard Worker // # Methods with inline caches
1678*795d594fSAndroid Build Coastguard Worker // LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;
1679*795d594fSAndroid Build Coastguard Worker // LTestInline;->noInlineCache(LSuper;)I
CreateProfile()1680*795d594fSAndroid Build Coastguard Worker int CreateProfile() {
1681*795d594fSAndroid Build Coastguard Worker // Validate parameters for this command.
1682*795d594fSAndroid Build Coastguard Worker if (apk_files_.empty() && apks_fd_.empty()) {
1683*795d594fSAndroid Build Coastguard Worker Usage("APK files must be specified");
1684*795d594fSAndroid Build Coastguard Worker }
1685*795d594fSAndroid Build Coastguard Worker if (dex_locations_.empty()) {
1686*795d594fSAndroid Build Coastguard Worker Usage("DEX locations must be specified");
1687*795d594fSAndroid Build Coastguard Worker }
1688*795d594fSAndroid Build Coastguard Worker if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
1689*795d594fSAndroid Build Coastguard Worker Usage("Reference profile must be specified with --reference-profile-file or "
1690*795d594fSAndroid Build Coastguard Worker "--reference-profile-file-fd");
1691*795d594fSAndroid Build Coastguard Worker }
1692*795d594fSAndroid Build Coastguard Worker if (!profile_files_.empty() || !profile_files_fd_.empty()) {
1693*795d594fSAndroid Build Coastguard Worker Usage("Profile must be specified with --reference-profile-file or "
1694*795d594fSAndroid Build Coastguard Worker "--reference-profile-file-fd");
1695*795d594fSAndroid Build Coastguard Worker }
1696*795d594fSAndroid Build Coastguard Worker // Open the profile output file if needed.
1697*795d594fSAndroid Build Coastguard Worker int fd = OpenReferenceProfile();
1698*795d594fSAndroid Build Coastguard Worker if (!FdIsValid(fd)) {
1699*795d594fSAndroid Build Coastguard Worker return -1;
1700*795d594fSAndroid Build Coastguard Worker }
1701*795d594fSAndroid Build Coastguard Worker // Read the user-specified list of classes and methods.
1702*795d594fSAndroid Build Coastguard Worker std::unique_ptr<std::unordered_set<std::string>>
1703*795d594fSAndroid Build Coastguard Worker user_lines(ReadCommentedInputFromFile<std::unordered_set<std::string>>(
1704*795d594fSAndroid Build Coastguard Worker create_profile_from_file_.c_str(), nullptr)); // No post-processing.
1705*795d594fSAndroid Build Coastguard Worker
1706*795d594fSAndroid Build Coastguard Worker // Open the dex files to look up classes and methods.
1707*795d594fSAndroid Build Coastguard Worker std::vector<std::unique_ptr<const DexFile>> dex_files;
1708*795d594fSAndroid Build Coastguard Worker OpenApkFilesFromLocations(&dex_files);
1709*795d594fSAndroid Build Coastguard Worker
1710*795d594fSAndroid Build Coastguard Worker // Process the lines one by one and add the successful ones to the profile.
1711*795d594fSAndroid Build Coastguard Worker bool for_boot_image = GetOutputProfileType() == OutputProfileType::kBoot;
1712*795d594fSAndroid Build Coastguard Worker ProfileCompilationInfo info(for_boot_image);
1713*795d594fSAndroid Build Coastguard Worker
1714*795d594fSAndroid Build Coastguard Worker if (for_boot_image) {
1715*795d594fSAndroid Build Coastguard Worker // Add all dex files to the profile. This is needed for jitzygote to indicate
1716*795d594fSAndroid Build Coastguard Worker // which dex files are part of the boot image extension to compile in memory.
1717*795d594fSAndroid Build Coastguard Worker for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
1718*795d594fSAndroid Build Coastguard Worker if (info.FindOrAddDexFile(*dex_file) == info.MaxProfileIndex()) {
1719*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "Failed to add dex file to boot image profile: " << dex_file->GetLocation();
1720*795d594fSAndroid Build Coastguard Worker return -1;
1721*795d594fSAndroid Build Coastguard Worker }
1722*795d594fSAndroid Build Coastguard Worker }
1723*795d594fSAndroid Build Coastguard Worker }
1724*795d594fSAndroid Build Coastguard Worker
1725*795d594fSAndroid Build Coastguard Worker for (const auto& line : *user_lines) {
1726*795d594fSAndroid Build Coastguard Worker ProcessLine(dex_files, line, &info);
1727*795d594fSAndroid Build Coastguard Worker }
1728*795d594fSAndroid Build Coastguard Worker
1729*795d594fSAndroid Build Coastguard Worker // Write the profile file.
1730*795d594fSAndroid Build Coastguard Worker CHECK(info.Save(fd));
1731*795d594fSAndroid Build Coastguard Worker if (close(fd) < 0) {
1732*795d594fSAndroid Build Coastguard Worker PLOG(WARNING) << "Failed to close descriptor";
1733*795d594fSAndroid Build Coastguard Worker }
1734*795d594fSAndroid Build Coastguard Worker return 0;
1735*795d594fSAndroid Build Coastguard Worker }
1736*795d594fSAndroid Build Coastguard Worker
ShouldCreateBootImageProfile() const1737*795d594fSAndroid Build Coastguard Worker bool ShouldCreateBootImageProfile() const {
1738*795d594fSAndroid Build Coastguard Worker return generate_boot_image_profile_;
1739*795d594fSAndroid Build Coastguard Worker }
1740*795d594fSAndroid Build Coastguard Worker
GetOutputProfileType() const1741*795d594fSAndroid Build Coastguard Worker OutputProfileType GetOutputProfileType() const {
1742*795d594fSAndroid Build Coastguard Worker return output_profile_type_;
1743*795d594fSAndroid Build Coastguard Worker }
1744*795d594fSAndroid Build Coastguard Worker
1745*795d594fSAndroid Build Coastguard Worker // Create and store a ProfileCompilationInfo for the boot image.
CreateBootImageProfile()1746*795d594fSAndroid Build Coastguard Worker int CreateBootImageProfile() {
1747*795d594fSAndroid Build Coastguard Worker // Open the input profile file.
1748*795d594fSAndroid Build Coastguard Worker if (profile_files_.size() < 1) {
1749*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "At least one --profile-file must be specified.";
1750*795d594fSAndroid Build Coastguard Worker return -1;
1751*795d594fSAndroid Build Coastguard Worker }
1752*795d594fSAndroid Build Coastguard Worker // Open the dex files.
1753*795d594fSAndroid Build Coastguard Worker std::vector<std::unique_ptr<const DexFile>> dex_files;
1754*795d594fSAndroid Build Coastguard Worker OpenApkFilesFromLocations(&dex_files);
1755*795d594fSAndroid Build Coastguard Worker if (dex_files.empty()) {
1756*795d594fSAndroid Build Coastguard Worker PLOG(ERROR) << "Expected dex files for creating boot profile";
1757*795d594fSAndroid Build Coastguard Worker return -2;
1758*795d594fSAndroid Build Coastguard Worker }
1759*795d594fSAndroid Build Coastguard Worker
1760*795d594fSAndroid Build Coastguard Worker if (!GenerateBootImageProfile(dex_files,
1761*795d594fSAndroid Build Coastguard Worker profile_files_,
1762*795d594fSAndroid Build Coastguard Worker boot_image_options_,
1763*795d594fSAndroid Build Coastguard Worker boot_profile_out_path_,
1764*795d594fSAndroid Build Coastguard Worker preloaded_classes_out_path_)) {
1765*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "There was an error when generating the boot image profiles";
1766*795d594fSAndroid Build Coastguard Worker return -4;
1767*795d594fSAndroid Build Coastguard Worker }
1768*795d594fSAndroid Build Coastguard Worker return 0;
1769*795d594fSAndroid Build Coastguard Worker }
1770*795d594fSAndroid Build Coastguard Worker
ShouldCreateProfile()1771*795d594fSAndroid Build Coastguard Worker bool ShouldCreateProfile() {
1772*795d594fSAndroid Build Coastguard Worker return !create_profile_from_file_.empty();
1773*795d594fSAndroid Build Coastguard Worker }
1774*795d594fSAndroid Build Coastguard Worker
GenerateTestProfile()1775*795d594fSAndroid Build Coastguard Worker int GenerateTestProfile() {
1776*795d594fSAndroid Build Coastguard Worker // Validate parameters for this command.
1777*795d594fSAndroid Build Coastguard Worker if (test_profile_method_percerntage_ > 100) {
1778*795d594fSAndroid Build Coastguard Worker Usage("Invalid percentage for --generate-test-profile-method-percentage");
1779*795d594fSAndroid Build Coastguard Worker }
1780*795d594fSAndroid Build Coastguard Worker if (test_profile_class_percentage_ > 100) {
1781*795d594fSAndroid Build Coastguard Worker Usage("Invalid percentage for --generate-test-profile-class-percentage");
1782*795d594fSAndroid Build Coastguard Worker }
1783*795d594fSAndroid Build Coastguard Worker // If given APK files or DEX locations, check that they're ok.
1784*795d594fSAndroid Build Coastguard Worker if (!apk_files_.empty() || !apks_fd_.empty() || !dex_locations_.empty()) {
1785*795d594fSAndroid Build Coastguard Worker if (apk_files_.empty() && apks_fd_.empty()) {
1786*795d594fSAndroid Build Coastguard Worker Usage("APK files must be specified when passing DEX locations to --generate-test-profile");
1787*795d594fSAndroid Build Coastguard Worker }
1788*795d594fSAndroid Build Coastguard Worker if (dex_locations_.empty()) {
1789*795d594fSAndroid Build Coastguard Worker Usage("DEX locations must be specified when passing APK files to --generate-test-profile");
1790*795d594fSAndroid Build Coastguard Worker }
1791*795d594fSAndroid Build Coastguard Worker }
1792*795d594fSAndroid Build Coastguard Worker // ShouldGenerateTestProfile confirms !test_profile_.empty().
1793*795d594fSAndroid Build Coastguard Worker #ifdef _WIN32
1794*795d594fSAndroid Build Coastguard Worker int flags = O_CREAT | O_TRUNC | O_WRONLY;
1795*795d594fSAndroid Build Coastguard Worker #else
1796*795d594fSAndroid Build Coastguard Worker int flags = O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC;
1797*795d594fSAndroid Build Coastguard Worker #endif
1798*795d594fSAndroid Build Coastguard Worker int profile_test_fd = open(test_profile_.c_str(), flags, 0644);
1799*795d594fSAndroid Build Coastguard Worker if (profile_test_fd < 0) {
1800*795d594fSAndroid Build Coastguard Worker PLOG(ERROR) << "Cannot open " << test_profile_;
1801*795d594fSAndroid Build Coastguard Worker return -1;
1802*795d594fSAndroid Build Coastguard Worker }
1803*795d594fSAndroid Build Coastguard Worker bool result;
1804*795d594fSAndroid Build Coastguard Worker if (apk_files_.empty() && apks_fd_.empty() && dex_locations_.empty()) {
1805*795d594fSAndroid Build Coastguard Worker result = ProfileCompilationInfo::GenerateTestProfile(profile_test_fd,
1806*795d594fSAndroid Build Coastguard Worker test_profile_num_dex_,
1807*795d594fSAndroid Build Coastguard Worker test_profile_method_percerntage_,
1808*795d594fSAndroid Build Coastguard Worker test_profile_class_percentage_,
1809*795d594fSAndroid Build Coastguard Worker test_profile_seed_);
1810*795d594fSAndroid Build Coastguard Worker } else {
1811*795d594fSAndroid Build Coastguard Worker // Open the dex files to look up classes and methods.
1812*795d594fSAndroid Build Coastguard Worker std::vector<std::unique_ptr<const DexFile>> dex_files;
1813*795d594fSAndroid Build Coastguard Worker OpenApkFilesFromLocations(&dex_files);
1814*795d594fSAndroid Build Coastguard Worker // Create a random profile file based on the set of dex files.
1815*795d594fSAndroid Build Coastguard Worker result = ProfileCompilationInfo::GenerateTestProfile(profile_test_fd,
1816*795d594fSAndroid Build Coastguard Worker dex_files,
1817*795d594fSAndroid Build Coastguard Worker test_profile_method_percerntage_,
1818*795d594fSAndroid Build Coastguard Worker test_profile_class_percentage_,
1819*795d594fSAndroid Build Coastguard Worker test_profile_seed_);
1820*795d594fSAndroid Build Coastguard Worker }
1821*795d594fSAndroid Build Coastguard Worker close(profile_test_fd); // ignore close result.
1822*795d594fSAndroid Build Coastguard Worker return result ? 0 : -1;
1823*795d594fSAndroid Build Coastguard Worker }
1824*795d594fSAndroid Build Coastguard Worker
ShouldGenerateTestProfile()1825*795d594fSAndroid Build Coastguard Worker bool ShouldGenerateTestProfile() {
1826*795d594fSAndroid Build Coastguard Worker return !test_profile_.empty();
1827*795d594fSAndroid Build Coastguard Worker }
1828*795d594fSAndroid Build Coastguard Worker
ShouldCopyAndUpdateProfileKey() const1829*795d594fSAndroid Build Coastguard Worker bool ShouldCopyAndUpdateProfileKey() const {
1830*795d594fSAndroid Build Coastguard Worker return copy_and_update_profile_key_;
1831*795d594fSAndroid Build Coastguard Worker }
1832*795d594fSAndroid Build Coastguard Worker
CopyAndUpdateProfileKey()1833*795d594fSAndroid Build Coastguard Worker ProfmanResult::CopyAndUpdateResult CopyAndUpdateProfileKey() {
1834*795d594fSAndroid Build Coastguard Worker // Validate that at least one profile file was passed, as well as a reference profile.
1835*795d594fSAndroid Build Coastguard Worker if (!(profile_files_.size() == 1 ^ profile_files_fd_.size() == 1)) {
1836*795d594fSAndroid Build Coastguard Worker Usage("Only one profile file should be specified.");
1837*795d594fSAndroid Build Coastguard Worker }
1838*795d594fSAndroid Build Coastguard Worker if (reference_profile_file_.empty() && !FdIsValid(reference_profile_file_fd_)) {
1839*795d594fSAndroid Build Coastguard Worker Usage("No reference profile file specified.");
1840*795d594fSAndroid Build Coastguard Worker }
1841*795d594fSAndroid Build Coastguard Worker
1842*795d594fSAndroid Build Coastguard Worker if (apk_files_.empty() && apks_fd_.empty()) {
1843*795d594fSAndroid Build Coastguard Worker Usage("No apk files specified");
1844*795d594fSAndroid Build Coastguard Worker }
1845*795d594fSAndroid Build Coastguard Worker
1846*795d594fSAndroid Build Coastguard Worker bool use_fds = profile_files_fd_.size() == 1;
1847*795d594fSAndroid Build Coastguard Worker
1848*795d594fSAndroid Build Coastguard Worker ProfileCompilationInfo profile;
1849*795d594fSAndroid Build Coastguard Worker // Do not clear if invalid. The input might be an archive.
1850*795d594fSAndroid Build Coastguard Worker bool load_ok = use_fds
1851*795d594fSAndroid Build Coastguard Worker ? profile.Load(profile_files_fd_[0])
1852*795d594fSAndroid Build Coastguard Worker : profile.Load(profile_files_[0], /*clear_if_invalid=*/ false);
1853*795d594fSAndroid Build Coastguard Worker if (load_ok) {
1854*795d594fSAndroid Build Coastguard Worker // Open the dex files to look up classes and methods.
1855*795d594fSAndroid Build Coastguard Worker std::vector<std::unique_ptr<const DexFile>> dex_files;
1856*795d594fSAndroid Build Coastguard Worker OpenApkFilesFromLocations(&dex_files);
1857*795d594fSAndroid Build Coastguard Worker bool matched = false;
1858*795d594fSAndroid Build Coastguard Worker if (!profile.UpdateProfileKeys(dex_files, &matched)) {
1859*795d594fSAndroid Build Coastguard Worker return ProfmanResult::kCopyAndUpdateErrorFailedToUpdateProfile;
1860*795d594fSAndroid Build Coastguard Worker }
1861*795d594fSAndroid Build Coastguard Worker bool result = use_fds
1862*795d594fSAndroid Build Coastguard Worker ? profile.Save(reference_profile_file_fd_)
1863*795d594fSAndroid Build Coastguard Worker : profile.Save(reference_profile_file_, /*bytes_written=*/ nullptr);
1864*795d594fSAndroid Build Coastguard Worker if (!result) {
1865*795d594fSAndroid Build Coastguard Worker return ProfmanResult::kCopyAndUpdateErrorFailedToSaveProfile;
1866*795d594fSAndroid Build Coastguard Worker }
1867*795d594fSAndroid Build Coastguard Worker return matched ? ProfmanResult::kCopyAndUpdateSuccess : ProfmanResult::kCopyAndUpdateNoMatch;
1868*795d594fSAndroid Build Coastguard Worker } else {
1869*795d594fSAndroid Build Coastguard Worker return ProfmanResult::kCopyAndUpdateErrorFailedToLoadProfile;
1870*795d594fSAndroid Build Coastguard Worker }
1871*795d594fSAndroid Build Coastguard Worker }
1872*795d594fSAndroid Build Coastguard Worker
1873*795d594fSAndroid Build Coastguard Worker private:
ParseFdForCollection(const char * raw_option,std::string_view option_prefix,std::vector<int> * fds)1874*795d594fSAndroid Build Coastguard Worker static void ParseFdForCollection(const char* raw_option,
1875*795d594fSAndroid Build Coastguard Worker std::string_view option_prefix,
1876*795d594fSAndroid Build Coastguard Worker std::vector<int>* fds) {
1877*795d594fSAndroid Build Coastguard Worker int fd;
1878*795d594fSAndroid Build Coastguard Worker ParseUintOption(raw_option, option_prefix, &fd);
1879*795d594fSAndroid Build Coastguard Worker fds->push_back(fd);
1880*795d594fSAndroid Build Coastguard Worker }
1881*795d594fSAndroid Build Coastguard Worker
CloseAllFds(const std::vector<int> & fds,const char * descriptor)1882*795d594fSAndroid Build Coastguard Worker static void CloseAllFds(const std::vector<int>& fds, const char* descriptor) {
1883*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < fds.size(); i++) {
1884*795d594fSAndroid Build Coastguard Worker if (close(fds[i]) < 0) {
1885*795d594fSAndroid Build Coastguard Worker PLOG(WARNING) << "Failed to close descriptor for "
1886*795d594fSAndroid Build Coastguard Worker << descriptor << " at index " << i << ": " << fds[i];
1887*795d594fSAndroid Build Coastguard Worker }
1888*795d594fSAndroid Build Coastguard Worker }
1889*795d594fSAndroid Build Coastguard Worker }
1890*795d594fSAndroid Build Coastguard Worker
LogCompletionTime()1891*795d594fSAndroid Build Coastguard Worker void LogCompletionTime() {
1892*795d594fSAndroid Build Coastguard Worker static constexpr uint64_t kLogThresholdTime = MsToNs(100); // 100ms
1893*795d594fSAndroid Build Coastguard Worker uint64_t time_taken = NanoTime() - start_ns_;
1894*795d594fSAndroid Build Coastguard Worker if (time_taken > kLogThresholdTime) {
1895*795d594fSAndroid Build Coastguard Worker LOG(WARNING) << "profman took " << PrettyDuration(time_taken);
1896*795d594fSAndroid Build Coastguard Worker }
1897*795d594fSAndroid Build Coastguard Worker }
1898*795d594fSAndroid Build Coastguard Worker
1899*795d594fSAndroid Build Coastguard Worker std::vector<std::string> profile_files_;
1900*795d594fSAndroid Build Coastguard Worker std::vector<int> profile_files_fd_;
1901*795d594fSAndroid Build Coastguard Worker std::vector<std::string> dex_locations_;
1902*795d594fSAndroid Build Coastguard Worker std::vector<std::string> apk_files_;
1903*795d594fSAndroid Build Coastguard Worker std::vector<int> apks_fd_;
1904*795d594fSAndroid Build Coastguard Worker std::string reference_profile_file_;
1905*795d594fSAndroid Build Coastguard Worker int reference_profile_file_fd_;
1906*795d594fSAndroid Build Coastguard Worker bool dump_only_;
1907*795d594fSAndroid Build Coastguard Worker bool dump_classes_and_methods_;
1908*795d594fSAndroid Build Coastguard Worker bool generate_boot_image_profile_;
1909*795d594fSAndroid Build Coastguard Worker OutputProfileType output_profile_type_;
1910*795d594fSAndroid Build Coastguard Worker int dump_output_to_fd_;
1911*795d594fSAndroid Build Coastguard Worker BootImageOptions boot_image_options_;
1912*795d594fSAndroid Build Coastguard Worker std::string test_profile_;
1913*795d594fSAndroid Build Coastguard Worker std::string create_profile_from_file_;
1914*795d594fSAndroid Build Coastguard Worker uint16_t test_profile_num_dex_;
1915*795d594fSAndroid Build Coastguard Worker uint16_t test_profile_method_percerntage_;
1916*795d594fSAndroid Build Coastguard Worker uint16_t test_profile_class_percentage_;
1917*795d594fSAndroid Build Coastguard Worker uint32_t test_profile_seed_;
1918*795d594fSAndroid Build Coastguard Worker uint64_t start_ns_;
1919*795d594fSAndroid Build Coastguard Worker bool copy_and_update_profile_key_;
1920*795d594fSAndroid Build Coastguard Worker ProfileAssistant::Options profile_assistant_options_;
1921*795d594fSAndroid Build Coastguard Worker std::string boot_profile_out_path_;
1922*795d594fSAndroid Build Coastguard Worker std::string preloaded_classes_out_path_;
1923*795d594fSAndroid Build Coastguard Worker };
1924*795d594fSAndroid Build Coastguard Worker
operator <<(std::ostream & os,const ProfMan::InlineCacheSegment & ics)1925*795d594fSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& os, const ProfMan::InlineCacheSegment& ics) {
1926*795d594fSAndroid Build Coastguard Worker return ics.Dump(os);
1927*795d594fSAndroid Build Coastguard Worker }
1928*795d594fSAndroid Build Coastguard Worker
1929*795d594fSAndroid Build Coastguard Worker // See ProfmanResult for return codes.
profman(int argc,char ** argv)1930*795d594fSAndroid Build Coastguard Worker static int profman(int argc, char** argv) {
1931*795d594fSAndroid Build Coastguard Worker ProfMan profman;
1932*795d594fSAndroid Build Coastguard Worker
1933*795d594fSAndroid Build Coastguard Worker // Parse arguments. Argument mistakes will lead to exit(EXIT_FAILURE) in UsageError.
1934*795d594fSAndroid Build Coastguard Worker profman.ParseArgs(argc, argv);
1935*795d594fSAndroid Build Coastguard Worker
1936*795d594fSAndroid Build Coastguard Worker // Initialize MemMap for ZipArchive::OpenFromFd.
1937*795d594fSAndroid Build Coastguard Worker MemMap::Init();
1938*795d594fSAndroid Build Coastguard Worker
1939*795d594fSAndroid Build Coastguard Worker if (profman.ShouldGenerateTestProfile()) {
1940*795d594fSAndroid Build Coastguard Worker return profman.GenerateTestProfile();
1941*795d594fSAndroid Build Coastguard Worker }
1942*795d594fSAndroid Build Coastguard Worker if (profman.ShouldOnlyDumpProfile()) {
1943*795d594fSAndroid Build Coastguard Worker return profman.DumpProfileInfo();
1944*795d594fSAndroid Build Coastguard Worker }
1945*795d594fSAndroid Build Coastguard Worker if (profman.ShouldOnlyDumpClassesAndMethods()) {
1946*795d594fSAndroid Build Coastguard Worker return profman.DumpClassesAndMethods();
1947*795d594fSAndroid Build Coastguard Worker }
1948*795d594fSAndroid Build Coastguard Worker if (profman.ShouldCreateProfile()) {
1949*795d594fSAndroid Build Coastguard Worker if (profman.GetOutputProfileType() == OutputProfileType::kBprof) {
1950*795d594fSAndroid Build Coastguard Worker return profman.CreateBootProfile();
1951*795d594fSAndroid Build Coastguard Worker } else {
1952*795d594fSAndroid Build Coastguard Worker return profman.CreateProfile();
1953*795d594fSAndroid Build Coastguard Worker }
1954*795d594fSAndroid Build Coastguard Worker }
1955*795d594fSAndroid Build Coastguard Worker
1956*795d594fSAndroid Build Coastguard Worker if (profman.ShouldCreateBootImageProfile()) {
1957*795d594fSAndroid Build Coastguard Worker return profman.CreateBootImageProfile();
1958*795d594fSAndroid Build Coastguard Worker }
1959*795d594fSAndroid Build Coastguard Worker
1960*795d594fSAndroid Build Coastguard Worker if (profman.ShouldCopyAndUpdateProfileKey()) {
1961*795d594fSAndroid Build Coastguard Worker return profman.CopyAndUpdateProfileKey();
1962*795d594fSAndroid Build Coastguard Worker }
1963*795d594fSAndroid Build Coastguard Worker
1964*795d594fSAndroid Build Coastguard Worker // Process profile information and assess if we need to do a profile guided compilation.
1965*795d594fSAndroid Build Coastguard Worker // This operation involves I/O.
1966*795d594fSAndroid Build Coastguard Worker return profman.ProcessProfiles();
1967*795d594fSAndroid Build Coastguard Worker }
1968*795d594fSAndroid Build Coastguard Worker
1969*795d594fSAndroid Build Coastguard Worker } // namespace art
1970*795d594fSAndroid Build Coastguard Worker
main(int argc,char ** argv)1971*795d594fSAndroid Build Coastguard Worker int main(int argc, char **argv) {
1972*795d594fSAndroid Build Coastguard Worker return art::profman(argc, argv);
1973*795d594fSAndroid Build Coastguard Worker }
1974