1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2017 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 <gtest/gtest.h>
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include <string>
20*795d594fSAndroid Build Coastguard Worker
21*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set.h"
22*795d594fSAndroid Build Coastguard Worker #include "base/compiler_filter.h"
23*795d594fSAndroid Build Coastguard Worker #include "dexopt_test.h"
24*795d594fSAndroid Build Coastguard Worker #include "dexoptanalyzer.h"
25*795d594fSAndroid Build Coastguard Worker
26*795d594fSAndroid Build Coastguard Worker namespace art {
27*795d594fSAndroid Build Coastguard Worker namespace dexoptanalyzer {
28*795d594fSAndroid Build Coastguard Worker
29*795d594fSAndroid Build Coastguard Worker class DexoptAnalyzerTest : public DexoptTest {
30*795d594fSAndroid Build Coastguard Worker protected:
GetDexoptAnalyzerCmd()31*795d594fSAndroid Build Coastguard Worker std::string GetDexoptAnalyzerCmd() {
32*795d594fSAndroid Build Coastguard Worker std::string file_path = GetArtBinDir() + "/dexoptanalyzer";
33*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild) {
34*795d594fSAndroid Build Coastguard Worker file_path += 'd';
35*795d594fSAndroid Build Coastguard Worker }
36*795d594fSAndroid Build Coastguard Worker EXPECT_TRUE(OS::FileExists(file_path.c_str())) << file_path << " should be a valid file path";
37*795d594fSAndroid Build Coastguard Worker return file_path;
38*795d594fSAndroid Build Coastguard Worker }
39*795d594fSAndroid Build Coastguard Worker
Analyze(const std::string & dex_file,CompilerFilter::Filter compiler_filter,ProfileAnalysisResult profile_analysis_result,const char * class_loader_context,bool downgrade=false)40*795d594fSAndroid Build Coastguard Worker int Analyze(const std::string& dex_file,
41*795d594fSAndroid Build Coastguard Worker CompilerFilter::Filter compiler_filter,
42*795d594fSAndroid Build Coastguard Worker ProfileAnalysisResult profile_analysis_result,
43*795d594fSAndroid Build Coastguard Worker const char* class_loader_context,
44*795d594fSAndroid Build Coastguard Worker bool downgrade = false) {
45*795d594fSAndroid Build Coastguard Worker std::string dexoptanalyzer_cmd = GetDexoptAnalyzerCmd();
46*795d594fSAndroid Build Coastguard Worker std::vector<std::string> argv_str;
47*795d594fSAndroid Build Coastguard Worker argv_str.push_back(dexoptanalyzer_cmd);
48*795d594fSAndroid Build Coastguard Worker argv_str.push_back("--dex-file=" + dex_file);
49*795d594fSAndroid Build Coastguard Worker argv_str.push_back("--isa=" + std::string(GetInstructionSetString(kRuntimeISA)));
50*795d594fSAndroid Build Coastguard Worker argv_str.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(compiler_filter));
51*795d594fSAndroid Build Coastguard Worker argv_str.push_back("--profile-analysis-result=" +
52*795d594fSAndroid Build Coastguard Worker std::to_string(static_cast<int>(profile_analysis_result)));
53*795d594fSAndroid Build Coastguard Worker if (downgrade) {
54*795d594fSAndroid Build Coastguard Worker argv_str.push_back("--downgrade");
55*795d594fSAndroid Build Coastguard Worker }
56*795d594fSAndroid Build Coastguard Worker
57*795d594fSAndroid Build Coastguard Worker argv_str.push_back("--runtime-arg");
58*795d594fSAndroid Build Coastguard Worker argv_str.push_back(GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()));
59*795d594fSAndroid Build Coastguard Worker argv_str.push_back("--runtime-arg");
60*795d594fSAndroid Build Coastguard Worker argv_str.push_back(GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()));
61*795d594fSAndroid Build Coastguard Worker argv_str.push_back("--image=" + GetImageLocation());
62*795d594fSAndroid Build Coastguard Worker argv_str.push_back("--android-data=" + android_data_);
63*795d594fSAndroid Build Coastguard Worker if (class_loader_context != nullptr) {
64*795d594fSAndroid Build Coastguard Worker argv_str.push_back("--class-loader-context=" + std::string(class_loader_context));
65*795d594fSAndroid Build Coastguard Worker }
66*795d594fSAndroid Build Coastguard Worker
67*795d594fSAndroid Build Coastguard Worker std::string error;
68*795d594fSAndroid Build Coastguard Worker return ExecAndReturnCode(argv_str, &error);
69*795d594fSAndroid Build Coastguard Worker }
70*795d594fSAndroid Build Coastguard Worker
DexoptanalyzerToOatFileAssistant(int dexoptanalyzerResult)71*795d594fSAndroid Build Coastguard Worker int DexoptanalyzerToOatFileAssistant(int dexoptanalyzerResult) {
72*795d594fSAndroid Build Coastguard Worker switch (dexoptanalyzerResult) {
73*795d594fSAndroid Build Coastguard Worker case 0: return OatFileAssistant::kNoDexOptNeeded;
74*795d594fSAndroid Build Coastguard Worker case 1: return OatFileAssistant::kDex2OatFromScratch;
75*795d594fSAndroid Build Coastguard Worker case 2: return OatFileAssistant::kDex2OatForBootImage;
76*795d594fSAndroid Build Coastguard Worker case 3: return OatFileAssistant::kDex2OatForFilter;
77*795d594fSAndroid Build Coastguard Worker case 4: return -OatFileAssistant::kDex2OatForBootImage;
78*795d594fSAndroid Build Coastguard Worker case 5: return -OatFileAssistant::kDex2OatForFilter;
79*795d594fSAndroid Build Coastguard Worker default: return dexoptanalyzerResult;
80*795d594fSAndroid Build Coastguard Worker }
81*795d594fSAndroid Build Coastguard Worker }
82*795d594fSAndroid Build Coastguard Worker
83*795d594fSAndroid Build Coastguard Worker // Verify that the output of dexoptanalyzer for the given arguments is the same
84*795d594fSAndroid Build Coastguard Worker // as the output of OatFileAssistant::GetDexOptNeeded.
Verify(const std::string & dex_file,CompilerFilter::Filter compiler_filter,ProfileAnalysisResult profile_analysis_result=ProfileAnalysisResult::kDontOptimizeSmallDelta,bool downgrade=false,const char * class_loader_context="PCL[]")85*795d594fSAndroid Build Coastguard Worker void Verify(const std::string& dex_file,
86*795d594fSAndroid Build Coastguard Worker CompilerFilter::Filter compiler_filter,
87*795d594fSAndroid Build Coastguard Worker ProfileAnalysisResult profile_analysis_result =
88*795d594fSAndroid Build Coastguard Worker ProfileAnalysisResult::kDontOptimizeSmallDelta,
89*795d594fSAndroid Build Coastguard Worker bool downgrade = false,
90*795d594fSAndroid Build Coastguard Worker const char* class_loader_context = "PCL[]") {
91*795d594fSAndroid Build Coastguard Worker std::unique_ptr<ClassLoaderContext> context = class_loader_context == nullptr
92*795d594fSAndroid Build Coastguard Worker ? nullptr
93*795d594fSAndroid Build Coastguard Worker : ClassLoaderContext::Create(class_loader_context);
94*795d594fSAndroid Build Coastguard Worker if (context != nullptr) {
95*795d594fSAndroid Build Coastguard Worker std::vector<int> context_fds;
96*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(context->OpenDexFiles("", context_fds, /*only_read_checksums*/ true));
97*795d594fSAndroid Build Coastguard Worker }
98*795d594fSAndroid Build Coastguard Worker
99*795d594fSAndroid Build Coastguard Worker int dexoptanalyzerResult = Analyze(
100*795d594fSAndroid Build Coastguard Worker dex_file, compiler_filter, profile_analysis_result, class_loader_context, downgrade);
101*795d594fSAndroid Build Coastguard Worker dexoptanalyzerResult = DexoptanalyzerToOatFileAssistant(dexoptanalyzerResult);
102*795d594fSAndroid Build Coastguard Worker OatFileAssistant oat_file_assistant(dex_file.c_str(),
103*795d594fSAndroid Build Coastguard Worker kRuntimeISA,
104*795d594fSAndroid Build Coastguard Worker context.get(),
105*795d594fSAndroid Build Coastguard Worker /*load_executable=*/ false);
106*795d594fSAndroid Build Coastguard Worker bool assume_profile_changed = profile_analysis_result == ProfileAnalysisResult::kOptimize;
107*795d594fSAndroid Build Coastguard Worker int assistantResult = oat_file_assistant.GetDexOptNeeded(
108*795d594fSAndroid Build Coastguard Worker compiler_filter, assume_profile_changed, downgrade);
109*795d594fSAndroid Build Coastguard Worker EXPECT_EQ(assistantResult, dexoptanalyzerResult);
110*795d594fSAndroid Build Coastguard Worker }
111*795d594fSAndroid Build Coastguard Worker };
112*795d594fSAndroid Build Coastguard Worker
113*795d594fSAndroid Build Coastguard Worker // The tests below exercise the same test case from oat_file_assistant_test.cc.
114*795d594fSAndroid Build Coastguard Worker
115*795d594fSAndroid Build Coastguard Worker // Case: We have a DEX file, but no ODEX file for it.
TEST_F(DexoptAnalyzerTest,DexNoOat)116*795d594fSAndroid Build Coastguard Worker TEST_F(DexoptAnalyzerTest, DexNoOat) {
117*795d594fSAndroid Build Coastguard Worker std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
118*795d594fSAndroid Build Coastguard Worker Copy(GetDexSrc1(), dex_location);
119*795d594fSAndroid Build Coastguard Worker
120*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kSpeed);
121*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kVerify);
122*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kSpeedProfile);
123*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kSpeed,
124*795d594fSAndroid Build Coastguard Worker ProfileAnalysisResult::kDontOptimizeSmallDelta, false, nullptr);
125*795d594fSAndroid Build Coastguard Worker }
126*795d594fSAndroid Build Coastguard Worker
127*795d594fSAndroid Build Coastguard Worker // Case: We have a DEX file and up-to-date ODEX file for it.
TEST_F(DexoptAnalyzerTest,OatUpToDate)128*795d594fSAndroid Build Coastguard Worker TEST_F(DexoptAnalyzerTest, OatUpToDate) {
129*795d594fSAndroid Build Coastguard Worker std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
130*795d594fSAndroid Build Coastguard Worker std::string odex_location = GetOdexDir() + "/OatUpToDate.odex";
131*795d594fSAndroid Build Coastguard Worker Copy(GetDexSrc1(), dex_location);
132*795d594fSAndroid Build Coastguard Worker GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
133*795d594fSAndroid Build Coastguard Worker
134*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kSpeed);
135*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kVerify);
136*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kEverything);
137*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kSpeed,
138*795d594fSAndroid Build Coastguard Worker ProfileAnalysisResult::kDontOptimizeSmallDelta, false, nullptr);
139*795d594fSAndroid Build Coastguard Worker }
140*795d594fSAndroid Build Coastguard Worker
141*795d594fSAndroid Build Coastguard Worker // Case: We have a DEX file and speed-profile ODEX file for it.
TEST_F(DexoptAnalyzerTest,ProfileOatUpToDate)142*795d594fSAndroid Build Coastguard Worker TEST_F(DexoptAnalyzerTest, ProfileOatUpToDate) {
143*795d594fSAndroid Build Coastguard Worker std::string dex_location = GetScratchDir() + "/ProfileOatUpToDate.jar";
144*795d594fSAndroid Build Coastguard Worker std::string odex_location = GetOdexDir() + "/ProfileOatUpToDate.odex";
145*795d594fSAndroid Build Coastguard Worker Copy(GetDexSrc1(), dex_location);
146*795d594fSAndroid Build Coastguard Worker GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeedProfile);
147*795d594fSAndroid Build Coastguard Worker
148*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kSpeedProfile,
149*795d594fSAndroid Build Coastguard Worker ProfileAnalysisResult::kDontOptimizeSmallDelta);
150*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kVerify, ProfileAnalysisResult::kDontOptimizeSmallDelta);
151*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kSpeedProfile, ProfileAnalysisResult::kOptimize);
152*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kVerify, ProfileAnalysisResult::kOptimize);
153*795d594fSAndroid Build Coastguard Worker }
154*795d594fSAndroid Build Coastguard Worker
155*795d594fSAndroid Build Coastguard Worker // Case: We have a DEX file, verify odex file for it, and we ask if it's up to date
156*795d594fSAndroid Build Coastguard Worker // when the profiles are empty or full.
TEST_F(DexoptAnalyzerTest,VerifyAndEmptyProfiles)157*795d594fSAndroid Build Coastguard Worker TEST_F(DexoptAnalyzerTest, VerifyAndEmptyProfiles) {
158*795d594fSAndroid Build Coastguard Worker std::string dex_location = GetScratchDir() + "/VerifyAndEmptyProfiles.jar";
159*795d594fSAndroid Build Coastguard Worker std::string odex_location = GetOdexDir() + "/VerifyAndEmptyProfiles.odex";
160*795d594fSAndroid Build Coastguard Worker Copy(GetDexSrc1(), dex_location);
161*795d594fSAndroid Build Coastguard Worker
162*795d594fSAndroid Build Coastguard Worker GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kVerify);
163*795d594fSAndroid Build Coastguard Worker
164*795d594fSAndroid Build Coastguard Worker // If we want to speed-profile something that was verified, do it even if
165*795d594fSAndroid Build Coastguard Worker // the profile analysis returns kDontOptimizeSmallDelta (it means that we do have profile data,
166*795d594fSAndroid Build Coastguard Worker // so a transition verify -> speed-profile is still worth).
167*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(
168*795d594fSAndroid Build Coastguard Worker static_cast<int>(ReturnCode::kDex2OatForFilterOdex),
169*795d594fSAndroid Build Coastguard Worker Analyze(dex_location, CompilerFilter::kSpeedProfile,
170*795d594fSAndroid Build Coastguard Worker ProfileAnalysisResult::kDontOptimizeSmallDelta, "PCL[]"));
171*795d594fSAndroid Build Coastguard Worker // If we want to speed-profile something that was verified but the profiles are empty,
172*795d594fSAndroid Build Coastguard Worker // don't do it - there will be no gain.
173*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(
174*795d594fSAndroid Build Coastguard Worker static_cast<int>(ReturnCode::kNoDexOptNeeded),
175*795d594fSAndroid Build Coastguard Worker Analyze(dex_location, CompilerFilter::kSpeedProfile,
176*795d594fSAndroid Build Coastguard Worker ProfileAnalysisResult::kDontOptimizeEmptyProfiles, "PCL[]"));
177*795d594fSAndroid Build Coastguard Worker // Standard case where we need to re-compile a speed-profile because of sufficient new
178*795d594fSAndroid Build Coastguard Worker // information in the profile.
179*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(
180*795d594fSAndroid Build Coastguard Worker static_cast<int>(ReturnCode::kDex2OatForFilterOdex),
181*795d594fSAndroid Build Coastguard Worker Analyze(dex_location, CompilerFilter::kSpeedProfile,
182*795d594fSAndroid Build Coastguard Worker ProfileAnalysisResult::kOptimize, "PCL[]"));
183*795d594fSAndroid Build Coastguard Worker }
184*795d594fSAndroid Build Coastguard Worker
TEST_F(DexoptAnalyzerTest,Downgrade)185*795d594fSAndroid Build Coastguard Worker TEST_F(DexoptAnalyzerTest, Downgrade) {
186*795d594fSAndroid Build Coastguard Worker std::string dex_location = GetScratchDir() + "/Downgrade.jar";
187*795d594fSAndroid Build Coastguard Worker std::string odex_location = GetOdexDir() + "/Downgrade.odex";
188*795d594fSAndroid Build Coastguard Worker Copy(GetDexSrc1(), dex_location);
189*795d594fSAndroid Build Coastguard Worker GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kVerify);
190*795d594fSAndroid Build Coastguard Worker
191*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kSpeedProfile,
192*795d594fSAndroid Build Coastguard Worker ProfileAnalysisResult::kDontOptimizeSmallDelta, true);
193*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kVerify,
194*795d594fSAndroid Build Coastguard Worker ProfileAnalysisResult::kDontOptimizeSmallDelta, true);
195*795d594fSAndroid Build Coastguard Worker }
196*795d594fSAndroid Build Coastguard Worker
197*795d594fSAndroid Build Coastguard Worker // Case: We have a MultiDEX file and up-to-date ODEX file for it.
TEST_F(DexoptAnalyzerTest,MultiDexOatUpToDate)198*795d594fSAndroid Build Coastguard Worker TEST_F(DexoptAnalyzerTest, MultiDexOatUpToDate) {
199*795d594fSAndroid Build Coastguard Worker std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
200*795d594fSAndroid Build Coastguard Worker std::string odex_location = GetOdexDir() + "/MultiDexOatUpToDate.odex";
201*795d594fSAndroid Build Coastguard Worker
202*795d594fSAndroid Build Coastguard Worker Copy(GetMultiDexSrc1(), dex_location);
203*795d594fSAndroid Build Coastguard Worker GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
204*795d594fSAndroid Build Coastguard Worker
205*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kSpeed, ProfileAnalysisResult::kDontOptimizeSmallDelta);
206*795d594fSAndroid Build Coastguard Worker }
207*795d594fSAndroid Build Coastguard Worker
208*795d594fSAndroid Build Coastguard Worker // Case: We have a MultiDEX file where the secondary dex file is out of date.
TEST_F(DexoptAnalyzerTest,MultiDexSecondaryOutOfDate)209*795d594fSAndroid Build Coastguard Worker TEST_F(DexoptAnalyzerTest, MultiDexSecondaryOutOfDate) {
210*795d594fSAndroid Build Coastguard Worker std::string dex_location = GetScratchDir() + "/MultiDexSecondaryOutOfDate.jar";
211*795d594fSAndroid Build Coastguard Worker std::string odex_location = GetOdexDir() + "/MultiDexSecondaryOutOfDate.odex";
212*795d594fSAndroid Build Coastguard Worker
213*795d594fSAndroid Build Coastguard Worker // Compile code for GetMultiDexSrc1.
214*795d594fSAndroid Build Coastguard Worker Copy(GetMultiDexSrc1(), dex_location);
215*795d594fSAndroid Build Coastguard Worker GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
216*795d594fSAndroid Build Coastguard Worker
217*795d594fSAndroid Build Coastguard Worker // Now overwrite the dex file with GetMultiDexSrc2 so the secondary checksum
218*795d594fSAndroid Build Coastguard Worker // is out of date.
219*795d594fSAndroid Build Coastguard Worker Copy(GetMultiDexSrc2(), dex_location);
220*795d594fSAndroid Build Coastguard Worker
221*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kSpeed, ProfileAnalysisResult::kDontOptimizeSmallDelta);
222*795d594fSAndroid Build Coastguard Worker }
223*795d594fSAndroid Build Coastguard Worker
224*795d594fSAndroid Build Coastguard Worker // Case: We have a DEX file and an ODEX file out of date with respect to the
225*795d594fSAndroid Build Coastguard Worker // dex checksum.
TEST_F(DexoptAnalyzerTest,OatDexOutOfDate)226*795d594fSAndroid Build Coastguard Worker TEST_F(DexoptAnalyzerTest, OatDexOutOfDate) {
227*795d594fSAndroid Build Coastguard Worker std::string dex_location = GetScratchDir() + "/OatDexOutOfDate.jar";
228*795d594fSAndroid Build Coastguard Worker std::string odex_location = GetOdexDir() + "/OatDexOutOfDate.odex";
229*795d594fSAndroid Build Coastguard Worker
230*795d594fSAndroid Build Coastguard Worker // We create a dex, generate an oat for it, then overwrite the dex with a
231*795d594fSAndroid Build Coastguard Worker // different dex to make the oat out of date.
232*795d594fSAndroid Build Coastguard Worker Copy(GetDexSrc1(), dex_location);
233*795d594fSAndroid Build Coastguard Worker GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
234*795d594fSAndroid Build Coastguard Worker Copy(GetDexSrc2(), dex_location);
235*795d594fSAndroid Build Coastguard Worker
236*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kSpeed);
237*795d594fSAndroid Build Coastguard Worker }
238*795d594fSAndroid Build Coastguard Worker
239*795d594fSAndroid Build Coastguard Worker // Case: We have a DEX file and an ODEX file out of date with respect to the
240*795d594fSAndroid Build Coastguard Worker // boot image.
TEST_F(DexoptAnalyzerTest,OatImageOutOfDate)241*795d594fSAndroid Build Coastguard Worker TEST_F(DexoptAnalyzerTest, OatImageOutOfDate) {
242*795d594fSAndroid Build Coastguard Worker std::string dex_location = GetScratchDir() + "/OatImageOutOfDate.jar";
243*795d594fSAndroid Build Coastguard Worker std::string odex_location = GetOdexDir() + "/OatImageOutOfDate.odex";
244*795d594fSAndroid Build Coastguard Worker
245*795d594fSAndroid Build Coastguard Worker Copy(GetDexSrc1(), dex_location);
246*795d594fSAndroid Build Coastguard Worker GenerateOatForTest(dex_location,
247*795d594fSAndroid Build Coastguard Worker odex_location,
248*795d594fSAndroid Build Coastguard Worker CompilerFilter::kSpeed,
249*795d594fSAndroid Build Coastguard Worker /*with_alternate_image=*/true);
250*795d594fSAndroid Build Coastguard Worker
251*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kVerify);
252*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kSpeed);
253*795d594fSAndroid Build Coastguard Worker }
254*795d594fSAndroid Build Coastguard Worker
255*795d594fSAndroid Build Coastguard Worker // Case: We have a DEX file and an ODEX file, but no OAT file.
TEST_F(DexoptAnalyzerTest,DexOdexNoOat)256*795d594fSAndroid Build Coastguard Worker TEST_F(DexoptAnalyzerTest, DexOdexNoOat) {
257*795d594fSAndroid Build Coastguard Worker std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
258*795d594fSAndroid Build Coastguard Worker std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
259*795d594fSAndroid Build Coastguard Worker
260*795d594fSAndroid Build Coastguard Worker Copy(GetDexSrc1(), dex_location);
261*795d594fSAndroid Build Coastguard Worker GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
262*795d594fSAndroid Build Coastguard Worker
263*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kSpeed);
264*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kEverything);
265*795d594fSAndroid Build Coastguard Worker }
266*795d594fSAndroid Build Coastguard Worker
267*795d594fSAndroid Build Coastguard Worker // Case: We have a stripped (or resource-only) DEX file, no ODEX file and no
268*795d594fSAndroid Build Coastguard Worker // OAT file. Expect: The status is kNoDexOptNeeded.
TEST_F(DexoptAnalyzerTest,ResourceOnlyDex)269*795d594fSAndroid Build Coastguard Worker TEST_F(DexoptAnalyzerTest, ResourceOnlyDex) {
270*795d594fSAndroid Build Coastguard Worker std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar";
271*795d594fSAndroid Build Coastguard Worker
272*795d594fSAndroid Build Coastguard Worker Copy(GetResourceOnlySrc1(), dex_location);
273*795d594fSAndroid Build Coastguard Worker
274*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kSpeed);
275*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kVerify);
276*795d594fSAndroid Build Coastguard Worker }
277*795d594fSAndroid Build Coastguard Worker
278*795d594fSAndroid Build Coastguard Worker // Case: We have a DEX file, an ODEX file and an OAT file.
TEST_F(DexoptAnalyzerTest,OdexOatOverlap)279*795d594fSAndroid Build Coastguard Worker TEST_F(DexoptAnalyzerTest, OdexOatOverlap) {
280*795d594fSAndroid Build Coastguard Worker std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
281*795d594fSAndroid Build Coastguard Worker std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
282*795d594fSAndroid Build Coastguard Worker std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat";
283*795d594fSAndroid Build Coastguard Worker
284*795d594fSAndroid Build Coastguard Worker Copy(GetDexSrc1(), dex_location);
285*795d594fSAndroid Build Coastguard Worker GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
286*795d594fSAndroid Build Coastguard Worker
287*795d594fSAndroid Build Coastguard Worker // Create the oat file by copying the odex so they are located in the same
288*795d594fSAndroid Build Coastguard Worker // place in memory.
289*795d594fSAndroid Build Coastguard Worker Copy(odex_location, oat_location);
290*795d594fSAndroid Build Coastguard Worker
291*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kSpeed);
292*795d594fSAndroid Build Coastguard Worker }
293*795d594fSAndroid Build Coastguard Worker
294*795d594fSAndroid Build Coastguard Worker // Case: Non-standard extension for dex file.
TEST_F(DexoptAnalyzerTest,LongDexExtension)295*795d594fSAndroid Build Coastguard Worker TEST_F(DexoptAnalyzerTest, LongDexExtension) {
296*795d594fSAndroid Build Coastguard Worker std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
297*795d594fSAndroid Build Coastguard Worker Copy(GetDexSrc1(), dex_location);
298*795d594fSAndroid Build Coastguard Worker
299*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kSpeed);
300*795d594fSAndroid Build Coastguard Worker }
301*795d594fSAndroid Build Coastguard Worker
302*795d594fSAndroid Build Coastguard Worker // Case: Very short, non-existent Dex location.
TEST_F(DexoptAnalyzerTest,ShortDexLocation)303*795d594fSAndroid Build Coastguard Worker TEST_F(DexoptAnalyzerTest, ShortDexLocation) {
304*795d594fSAndroid Build Coastguard Worker std::string dex_location = "/xx";
305*795d594fSAndroid Build Coastguard Worker
306*795d594fSAndroid Build Coastguard Worker Verify(dex_location, CompilerFilter::kSpeed);
307*795d594fSAndroid Build Coastguard Worker }
308*795d594fSAndroid Build Coastguard Worker
309*795d594fSAndroid Build Coastguard Worker // Case: We have a DEX file and up-to-date OAT file for it, and we check with
310*795d594fSAndroid Build Coastguard Worker // a class loader context.
TEST_F(DexoptAnalyzerTest,ClassLoaderContext)311*795d594fSAndroid Build Coastguard Worker TEST_F(DexoptAnalyzerTest, ClassLoaderContext) {
312*795d594fSAndroid Build Coastguard Worker std::string dex_location1 = GetScratchDir() + "/DexToAnalyze.jar";
313*795d594fSAndroid Build Coastguard Worker std::string odex_location1 = GetOdexDir() + "/DexToAnalyze.odex";
314*795d594fSAndroid Build Coastguard Worker std::string dex_location2 = GetScratchDir() + "/DexInContext.jar";
315*795d594fSAndroid Build Coastguard Worker Copy(GetDexSrc1(), dex_location1);
316*795d594fSAndroid Build Coastguard Worker Copy(GetDexSrc2(), dex_location2);
317*795d594fSAndroid Build Coastguard Worker
318*795d594fSAndroid Build Coastguard Worker std::string class_loader_context = "PCL[" + dex_location2 + "]";
319*795d594fSAndroid Build Coastguard Worker std::string class_loader_context_option = "--class-loader-context=PCL[" + dex_location2 + "]";
320*795d594fSAndroid Build Coastguard Worker
321*795d594fSAndroid Build Coastguard Worker // Generate the odex to get the class loader context also open the dex files.
322*795d594fSAndroid Build Coastguard Worker GenerateOdexForTest(dex_location1, odex_location1, CompilerFilter::kSpeed, /* compilation_reason= */ nullptr, /* extra_args= */ { class_loader_context_option });
323*795d594fSAndroid Build Coastguard Worker
324*795d594fSAndroid Build Coastguard Worker Verify(dex_location1, CompilerFilter::kSpeed, ProfileAnalysisResult::kDontOptimizeSmallDelta,
325*795d594fSAndroid Build Coastguard Worker false, class_loader_context.c_str());
326*795d594fSAndroid Build Coastguard Worker }
327*795d594fSAndroid Build Coastguard Worker
328*795d594fSAndroid Build Coastguard Worker } // namespace dexoptanalyzer
329*795d594fSAndroid Build Coastguard Worker } // namespace art
330