| 1 |
/* |
| 2 |
* Copyright (C) 2021 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License |
| 15 |
*/ |
| 16 |
|
| 17 |
package com.android.server.art; |
| 18 |
|
| 19 |
import static android.os.ParcelFileDescriptor.AutoCloseInputStream; |
| 20 |
|
| 21 |
import static com.android.server.art.DexUseManagerLocal.DetailedSecondaryDexInfo; |
| 22 |
import static com.android.server.art.model.DexoptStatus.DexContainerFileDexoptStatus; |
| 23 |
import static com.android.server.art.testing.TestingUtils.deepEq; |
| 24 |
import static com.android.server.art.testing.TestingUtils.inAnyOrder; |
| 25 |
import static com.android.server.art.testing.TestingUtils.inAnyOrderDeepEquals; |
| 26 |
|
| 27 |
import static com.google.common.truth.Truth.assertThat; |
| 28 |
|
| 29 |
import static org.mockito.AdditionalMatchers.not; |
| 30 |
import static org.mockito.ArgumentMatchers.anyString; |
| 31 |
import static org.mockito.ArgumentMatchers.eq; |
| 32 |
import static org.mockito.Mockito.any; |
| 33 |
import static org.mockito.Mockito.anyBoolean; |
| 34 |
import static org.mockito.Mockito.anyInt; |
| 35 |
import static org.mockito.Mockito.anyLong; |
| 36 |
import static org.mockito.Mockito.argThat; |
| 37 |
import static org.mockito.Mockito.doReturn; |
| 38 |
import static org.mockito.Mockito.isNull; |
| 39 |
import static org.mockito.Mockito.lenient; |
| 40 |
import static org.mockito.Mockito.mock; |
| 41 |
import static org.mockito.Mockito.never; |
| 42 |
import static org.mockito.Mockito.same; |
| 43 |
import static org.mockito.Mockito.times; |
| 44 |
import static org.mockito.Mockito.verify; |
| 45 |
import static org.mockito.Mockito.when; |
| 46 |
|
| 47 |
import android.apphibernation.AppHibernationManager; |
| 48 |
import android.os.CancellationSignal; |
| 49 |
import android.os.ParcelFileDescriptor; |
| 50 |
import android.os.Process; |
| 51 |
import android.os.ServiceSpecificException; |
| 52 |
import android.os.SystemProperties; |
| 53 |
import android.os.UserHandle; |
| 54 |
import android.os.UserManager; |
| 55 |
import android.os.storage.StorageManager; |
| 56 |
|
| 57 |
import androidx.test.filters.SmallTest; |
| 58 |
|
| 59 |
import com.android.modules.utils.pm.PackageStateModulesUtils; |
| 60 |
import com.android.server.art.model.Config; |
| 61 |
import com.android.server.art.model.DeleteResult; |
| 62 |
import com.android.server.art.model.DexoptParams; |
| 63 |
import com.android.server.art.model.DexoptResult; |
| 64 |
import com.android.server.art.model.DexoptStatus; |
| 65 |
import com.android.server.art.testing.StaticMockitoRule; |
| 66 |
import com.android.server.art.testing.TestingUtils; |
| 67 |
import com.android.server.pm.PackageManagerLocal; |
| 68 |
import com.android.server.pm.pkg.AndroidPackage; |
| 69 |
import com.android.server.pm.pkg.AndroidPackageSplit; |
| 70 |
import com.android.server.pm.pkg.PackageState; |
| 71 |
import com.android.server.pm.pkg.PackageUserState; |
| 72 |
|
| 73 |
import org.junit.Before; |
| 74 |
import org.junit.Rule; |
| 75 |
import org.junit.Test; |
| 76 |
import org.junit.runner.RunWith; |
| 77 |
import org.junit.runners.Parameterized; |
| 78 |
import org.junit.runners.Parameterized.Parameter; |
| 79 |
import org.junit.runners.Parameterized.Parameters; |
| 80 |
import org.mockito.Mock; |
| 81 |
|
| 82 |
import java.io.File; |
| 83 |
import java.io.FileWriter; |
| 84 |
import java.io.IOException; |
| 85 |
import java.io.InputStream; |
| 86 |
import java.nio.charset.StandardCharsets; |
| 87 |
import java.nio.file.Files; |
| 88 |
import java.nio.file.Path; |
| 89 |
import java.util.List; |
| 90 |
import java.util.Map; |
| 91 |
import java.util.Set; |
| 92 |
import java.util.concurrent.ForkJoinPool; |
| 93 |
import java.util.concurrent.TimeUnit; |
| 94 |
import java.util.function.Consumer; |
| 95 |
import java.util.stream.Collectors; |
| 96 |
|
| 97 |
@SmallTest |
| 98 |
@RunWith(Parameterized.class) |
| 99 |
public class ArtManagerLocalTest { |
| 100 |
private static final String PKG_NAME_1 = "com.example.foo"; |
| 101 |
private static final String PKG_NAME_2 = "com.android.bar"; |
| 102 |
private static final String PKG_NAME_HIBERNATING = "com.example.hibernating"; |
| 103 |
private static final int INACTIVE_DAYS = 1; |
| 104 |
private static final long CURRENT_TIME_MS = 10000000000l; |
| 105 |
private static final long RECENT_TIME_MS = |
| 106 |
CURRENT_TIME_MS - TimeUnit.DAYS.toMillis(INACTIVE_DAYS) + 1; |
| 107 |
private static final long NOT_RECENT_TIME_MS = |
| 108 |
CURRENT_TIME_MS - TimeUnit.DAYS.toMillis(INACTIVE_DAYS) - 1; |
| 109 |
|
| 110 |
@Rule |
| 111 |
public StaticMockitoRule mockitoRule = new StaticMockitoRule( |
| 112 |
SystemProperties.class, Constants.class, PackageStateModulesUtils.class); |
| 113 |
|
| 114 |
@Mock private ArtManagerLocal.Injector mInjector; |
| 115 |
@Mock private PackageManagerLocal mPackageManagerLocal; |
| 116 |
@Mock private PackageManagerLocal.FilteredSnapshot mSnapshot; |
| 117 |
@Mock private IArtd mArtd; |
| 118 |
@Mock private DexoptHelper mDexoptHelper; |
| 119 |
@Mock private AppHibernationManager mAppHibernationManager; |
| 120 |
@Mock private UserManager mUserManager; |
| 121 |
@Mock private DexUseManagerLocal mDexUseManager; |
| 122 |
@Mock private StorageManager mStorageManager; |
| 123 |
private PackageState mPkgState1; |
| 124 |
private AndroidPackage mPkg1; |
| 125 |
private List<DetailedSecondaryDexInfo> mSecondaryDexInfo1; |
| 126 |
private Config mConfig; |
| 127 |
|
| 128 |
// True if the primary dex'es are in a system partition. |
| 129 |
@Parameter(0) public boolean mIsInSystemPartition; |
| 130 |
// True if the primary dex'es are in Incremental FS. |
| 131 |
@Parameter(1) public boolean mIsInIncrementalFs; |
| 132 |
// True if the artifacts should be in dalvik-cache. |
| 133 |
@Parameter(2) public boolean mExpectedIsInDalvikCache; |
| 134 |
|
| 135 |
private ArtManagerLocal mArtManagerLocal; |
| 136 |
|
| 137 |
@Parameters(name = "isInReadonlyPartition={0},isInIncrementalFs={1}," |
| 138 |
+ "expectedIsInDalvikCache={2}") |
| 139 |
public static Iterable<Object[]> |
| 140 |
data() { |
| 141 |
return List.of(new Object[] {true, false, true}, new Object[] {false, true, true}, |
| 142 |
new Object[] {false, false, false}); |
| 143 |
} |
| 144 |
|
| 145 |
@Before |
| 146 |
public void setUp() throws Exception { |
| 147 |
mConfig = new Config(); |
| 148 |
|
| 149 |
// Use `lenient()` to suppress `UnnecessaryStubbingException` thrown by the strict stubs. |
| 150 |
// These are the default test setups. They may or may not be used depending on the code path |
| 151 |
// that each test case examines. |
| 152 |
lenient().when(mInjector.getPackageManagerLocal()).thenReturn(mPackageManagerLocal); |
| 153 |
lenient().when(mInjector.getArtd()).thenReturn(mArtd); |
| 154 |
lenient().when(mInjector.getDexoptHelper()).thenReturn(mDexoptHelper); |
| 155 |
lenient().when(mInjector.getConfig()).thenReturn(mConfig); |
| 156 |
lenient().when(mInjector.getAppHibernationManager()).thenReturn(mAppHibernationManager); |
| 157 |
lenient().when(mInjector.getUserManager()).thenReturn(mUserManager); |
| 158 |
lenient().when(mInjector.isSystemUiPackage(any())).thenReturn(false); |
| 159 |
lenient().when(mInjector.isLauncherPackage(any())).thenReturn(false); |
| 160 |
lenient().when(mInjector.getDexUseManager()).thenReturn(mDexUseManager); |
| 161 |
lenient().when(mInjector.getCurrentTimeMillis()).thenReturn(CURRENT_TIME_MS); |
| 162 |
lenient().when(mInjector.getStorageManager()).thenReturn(mStorageManager); |
| 163 |
|
| 164 |
Path tempDir = Files.createTempDirectory("temp"); |
| 165 |
tempDir.toFile().deleteOnExit(); |
| 166 |
lenient().when(mInjector.getTempDir()).thenReturn(tempDir.toString()); |
| 167 |
|
| 168 |
lenient().when(SystemProperties.get(eq("pm.dexopt.install"))).thenReturn("speed-profile"); |
| 169 |
lenient().when(SystemProperties.get(eq("pm.dexopt.bg-dexopt"))).thenReturn("speed-profile"); |
| 170 |
lenient().when(SystemProperties.get(eq("pm.dexopt.first-boot"))).thenReturn("verify"); |
| 171 |
lenient() |
| 172 |
.when(SystemProperties.get(eq("pm.dexopt.boot-after-mainline-update"))) |
| 173 |
.thenReturn("verify"); |
| 174 |
lenient().when(SystemProperties.get(eq("pm.dexopt.inactive"))).thenReturn("verify"); |
| 175 |
lenient() |
| 176 |
.when(SystemProperties.getInt(eq("pm.dexopt.bg-dexopt.concurrency"), anyInt())) |
| 177 |
.thenReturn(3); |
| 178 |
lenient() |
| 179 |
.when(SystemProperties.getInt( |
| 180 |
eq("pm.dexopt.boot-after-mainline-update.concurrency"), anyInt())) |
| 181 |
.thenReturn(3); |
| 182 |
lenient() |
| 183 |
.when(SystemProperties.getInt(eq("pm.dexopt.inactive.concurrency"), anyInt())) |
| 184 |
.thenReturn(3); |
| 185 |
lenient() |
| 186 |
.when(SystemProperties.getInt( |
| 187 |
eq("pm.dexopt.downgrade_after_inactive_days"), anyInt())) |
| 188 |
.thenReturn(INACTIVE_DAYS); |
| 189 |
|
| 190 |
// No ISA translation. |
| 191 |
lenient() |
| 192 |
.when(SystemProperties.get(argThat(arg -> arg.startsWith("ro.dalvik.vm.isa.")))) |
| 193 |
.thenReturn(""); |
| 194 |
|
| 195 |
lenient().when(Constants.getPreferredAbi()).thenReturn("arm64-v8a"); |
| 196 |
lenient().when(Constants.getNative64BitAbi()).thenReturn("arm64-v8a"); |
| 197 |
lenient().when(Constants.getNative32BitAbi()).thenReturn("armeabi-v7a"); |
| 198 |
lenient().when(Constants.isBootImageProfilingEnabled()).thenReturn(true); |
| 199 |
|
| 200 |
lenient().when(mAppHibernationManager.isHibernatingGlobally(any())).thenReturn(false); |
| 201 |
lenient().when(mAppHibernationManager.isOatArtifactDeletionEnabled()).thenReturn(true); |
| 202 |
|
| 203 |
lenient() |
| 204 |
.when(mUserManager.getUserHandles(anyBoolean())) |
| 205 |
.thenReturn(List.of(UserHandle.of(0), UserHandle.of(1))); |
| 206 |
|
| 207 |
// All packages are by default recently used. |
| 208 |
lenient().when(mDexUseManager.getPackageLastUsedAtMs(any())).thenReturn(RECENT_TIME_MS); |
| 209 |
mSecondaryDexInfo1 = createSecondaryDexInfo(); |
| 210 |
lenient() |
| 211 |
.doReturn(mSecondaryDexInfo1) |
| 212 |
.when(mDexUseManager) |
| 213 |
.getSecondaryDexInfo(eq(PKG_NAME_1)); |
| 214 |
lenient() |
| 215 |
.doReturn(mSecondaryDexInfo1) |
| 216 |
.when(mDexUseManager) |
| 217 |
.getFilteredDetailedSecondaryDexInfo(eq(PKG_NAME_1)); |
| 218 |
|
| 219 |
simulateStorageNotLow(); |
| 220 |
|
| 221 |
lenient().when(mPackageManagerLocal.withFilteredSnapshot()).thenReturn(mSnapshot); |
| 222 |
List<PackageState> pkgStates = createPackageStates(); |
| 223 |
for (PackageState pkgState : pkgStates) { |
| 224 |
lenient() |
| 225 |
.when(mSnapshot.getPackageState(pkgState.getPackageName())) |
| 226 |
.thenReturn(pkgState); |
| 227 |
} |
| 228 |
var packageStateMap = pkgStates.stream().collect( |
| 229 |
Collectors.toMap(PackageState::getPackageName, it -> it)); |
| 230 |
lenient().when(mSnapshot.getPackageStates()).thenReturn(packageStateMap); |
| 231 |
mPkgState1 = mSnapshot.getPackageState(PKG_NAME_1); |
| 232 |
mPkg1 = mPkgState1.getAndroidPackage(); |
| 233 |
|
| 234 |
lenient().when(mArtd.isIncrementalFsPath(any())).thenReturn(mIsInIncrementalFs); |
| 235 |
|
| 236 |
// By default, none of the profiles are usable. |
| 237 |
lenient().when(mArtd.isProfileUsable(any(), any())).thenReturn(false); |
| 238 |
lenient().when(mArtd.copyAndRewriteProfile(any(), any(), any())).thenReturn(false); |
| 239 |
|
| 240 |
mArtManagerLocal = new ArtManagerLocal(mInjector); |
| 241 |
} |
| 242 |
|
| 243 |
@Test |
| 244 |
public void testdeleteDexoptArtifacts() throws Exception { |
| 245 |
when(mArtd.deleteArtifacts(any())).thenReturn(1l); |
| 246 |
|
| 247 |
DeleteResult result = mArtManagerLocal.deleteDexoptArtifacts(mSnapshot, PKG_NAME_1); |
| 248 |
assertThat(result.getFreedBytes()).isEqualTo(5); |
| 249 |
|
| 250 |
verify(mArtd).deleteArtifacts(deepEq(AidlUtils.buildArtifactsPath( |
| 251 |
"/data/app/foo/base.apk", "arm64", mExpectedIsInDalvikCache))); |
| 252 |
verify(mArtd).deleteArtifacts(deepEq(AidlUtils.buildArtifactsPath( |
| 253 |
"/data/app/foo/base.apk", "arm", mExpectedIsInDalvikCache))); |
| 254 |
verify(mArtd).deleteArtifacts(deepEq(AidlUtils.buildArtifactsPath( |
| 255 |
"/data/app/foo/split_0.apk", "arm64", mExpectedIsInDalvikCache))); |
| 256 |
verify(mArtd).deleteArtifacts(deepEq(AidlUtils.buildArtifactsPath( |
| 257 |
"/data/app/foo/split_0.apk", "arm", mExpectedIsInDalvikCache))); |
| 258 |
verify(mArtd).deleteArtifacts(deepEq(AidlUtils.buildArtifactsPath( |
| 259 |
"/data/user/0/foo/1.apk", "arm64", false /* isInDalvikCache */))); |
| 260 |
|
| 261 |
// Verify that there are no more calls than the ones above. |
| 262 |
verify(mArtd, times(5)).deleteArtifacts(any()); |
| 263 |
} |
| 264 |
|
| 265 |
@Test |
| 266 |
public void testdeleteDexoptArtifactsTranslatedIsas() throws Exception { |
| 267 |
lenient().when(SystemProperties.get("ro.dalvik.vm.isa.arm64")).thenReturn("x86_64"); |
| 268 |
lenient().when(SystemProperties.get("ro.dalvik.vm.isa.arm")).thenReturn("x86"); |
| 269 |
lenient().when(Constants.getPreferredAbi()).thenReturn("x86_64"); |
| 270 |
lenient().when(Constants.getNative64BitAbi()).thenReturn("x86_64"); |
| 271 |
lenient().when(Constants.getNative32BitAbi()).thenReturn("x86"); |
| 272 |
when(mSecondaryDexInfo1.get(0).abiNames()).thenReturn(Set.of("x86_64")); |
| 273 |
|
| 274 |
when(mArtd.deleteArtifacts(any())).thenReturn(1l); |
| 275 |
|
| 276 |
DeleteResult result = mArtManagerLocal.deleteDexoptArtifacts(mSnapshot, PKG_NAME_1); |
| 277 |
assertThat(result.getFreedBytes()).isEqualTo(5); |
| 278 |
|
| 279 |
verify(mArtd).deleteArtifacts(deepEq(AidlUtils.buildArtifactsPath( |
| 280 |
"/data/app/foo/base.apk", "x86_64", mExpectedIsInDalvikCache))); |
| 281 |
verify(mArtd).deleteArtifacts(deepEq(AidlUtils.buildArtifactsPath( |
| 282 |
"/data/app/foo/base.apk", "x86", mExpectedIsInDalvikCache))); |
| 283 |
verify(mArtd).deleteArtifacts(deepEq(AidlUtils.buildArtifactsPath( |
| 284 |
"/data/app/foo/split_0.apk", "x86_64", mExpectedIsInDalvikCache))); |
| 285 |
verify(mArtd).deleteArtifacts(deepEq(AidlUtils.buildArtifactsPath( |
| 286 |
"/data/app/foo/split_0.apk", "x86", mExpectedIsInDalvikCache))); |
| 287 |
// We assume that the ISA got from `DexUseManagerLocal` is already the translated one. |
| 288 |
verify(mArtd).deleteArtifacts(deepEq(AidlUtils.buildArtifactsPath( |
| 289 |
"/data/user/0/foo/1.apk", "x86_64", false /* isInDalvikCache */))); |
| 290 |
|
| 291 |
// Verify that there are no more calls than the ones above. |
| 292 |
verify(mArtd, times(5)).deleteArtifacts(any()); |
| 293 |
} |
| 294 |
|
| 295 |
@Test(expected = IllegalArgumentException.class) |
| 296 |
public void testdeleteDexoptArtifactsPackageNotFound() throws Exception { |
| 297 |
when(mSnapshot.getPackageState(anyString())).thenReturn(null); |
| 298 |
|
| 299 |
mArtManagerLocal.deleteDexoptArtifacts(mSnapshot, PKG_NAME_1); |
| 300 |
} |
| 301 |
|
| 302 |
@Test(expected = IllegalArgumentException.class) |
| 303 |
public void testdeleteDexoptArtifactsNoPackage() throws Exception { |
| 304 |
when(mPkgState1.getAndroidPackage()).thenReturn(null); |
| 305 |
|
| 306 |
mArtManagerLocal.deleteDexoptArtifacts(mSnapshot, PKG_NAME_1); |
| 307 |
} |
| 308 |
|
| 309 |
@Test |
| 310 |
public void testGetDexoptStatus() throws Exception { |
| 311 |
doReturn(createGetDexoptStatusResult( |
| 312 |
"speed", "compilation-reason-0", "location-debug-string-0")) |
| 313 |
.when(mArtd) |
| 314 |
.getDexoptStatus("/data/app/foo/base.apk", "arm64", "PCL[]"); |
| 315 |
doReturn(createGetDexoptStatusResult( |
| 316 |
"speed-profile", "compilation-reason-1", "location-debug-string-1")) |
| 317 |
.when(mArtd) |
| 318 |
.getDexoptStatus("/data/app/foo/base.apk", "arm", "PCL[]"); |
| 319 |
doReturn(createGetDexoptStatusResult( |
| 320 |
"verify", "compilation-reason-2", "location-debug-string-2")) |
| 321 |
.when(mArtd) |
| 322 |
.getDexoptStatus("/data/app/foo/split_0.apk", "arm64", "PCL[base.apk]"); |
| 323 |
doReturn(createGetDexoptStatusResult( |
| 324 |
"extract", "compilation-reason-3", "location-debug-string-3")) |
| 325 |
.when(mArtd) |
| 326 |
.getDexoptStatus("/data/app/foo/split_0.apk", "arm", "PCL[base.apk]"); |
| 327 |
doReturn(createGetDexoptStatusResult("run-from-apk", "unknown", "unknown")) |
| 328 |
.when(mArtd) |
| 329 |
.getDexoptStatus("/data/user/0/foo/1.apk", "arm64", "CLC"); |
| 330 |
|
| 331 |
DexoptStatus result = mArtManagerLocal.getDexoptStatus(mSnapshot, PKG_NAME_1); |
| 332 |
|
| 333 |
assertThat(result.getDexContainerFileDexoptStatuses()) |
| 334 |
.comparingElementsUsing(TestingUtils.<DexContainerFileDexoptStatus>deepEquality()) |
| 335 |
.containsExactly( |
| 336 |
DexContainerFileDexoptStatus.create("/data/app/foo/base.apk", |
| 337 |
true /* isPrimaryDex */, true /* isPrimaryAbi */, "arm64-v8a", |
| 338 |
"speed", "compilation-reason-0", "location-debug-string-0"), |
| 339 |
DexContainerFileDexoptStatus.create("/data/app/foo/base.apk", |
| 340 |
true /* isPrimaryDex */, false /* isPrimaryAbi */, "armeabi-v7a", |
| 341 |
"speed-profile", "compilation-reason-1", "location-debug-string-1"), |
| 342 |
DexContainerFileDexoptStatus.create("/data/app/foo/split_0.apk", |
| 343 |
true /* isPrimaryDex */, true /* isPrimaryAbi */, "arm64-v8a", |
| 344 |
"verify", "compilation-reason-2", "location-debug-string-2"), |
| 345 |
DexContainerFileDexoptStatus.create("/data/app/foo/split_0.apk", |
| 346 |
true /* isPrimaryDex */, false /* isPrimaryAbi */, "armeabi-v7a", |
| 347 |
"extract", "compilation-reason-3", "location-debug-string-3"), |
| 348 |
DexContainerFileDexoptStatus.create("/data/user/0/foo/1.apk", |
| 349 |
false /* isPrimaryDex */, true /* isPrimaryAbi */, "arm64-v8a", |
| 350 |
"run-from-apk", "unknown", "unknown")); |
| 351 |
} |
| 352 |
|
| 353 |
@Test(expected = IllegalArgumentException.class) |
| 354 |
public void testGetDexoptStatusPackageNotFound() throws Exception { |
| 355 |
when(mSnapshot.getPackageState(anyString())).thenReturn(null); |
| 356 |
|
| 357 |
mArtManagerLocal.getDexoptStatus(mSnapshot, PKG_NAME_1); |
| 358 |
} |
| 359 |
|
| 360 |
@Test(expected = IllegalArgumentException.class) |
| 361 |
public void testGetDexoptStatusNoPackage() throws Exception { |
| 362 |
when(mPkgState1.getAndroidPackage()).thenReturn(null); |
| 363 |
|
| 364 |
mArtManagerLocal.getDexoptStatus(mSnapshot, PKG_NAME_1); |
| 365 |
} |
| 366 |
|
| 367 |
@Test |
| 368 |
public void testGetDexoptStatusNonFatalError() throws Exception { |
| 369 |
when(mArtd.getDexoptStatus(any(), any(), any())) |
| 370 |
.thenThrow(new ServiceSpecificException(1 /* errorCode */, "some error message")); |
| 371 |
|
| 372 |
DexoptStatus result = mArtManagerLocal.getDexoptStatus(mSnapshot, PKG_NAME_1); |
| 373 |
|
| 374 |
List<DexContainerFileDexoptStatus> statuses = result.getDexContainerFileDexoptStatuses(); |
| 375 |
assertThat(statuses.size()).isEqualTo(5); |
| 376 |
|
| 377 |
for (DexContainerFileDexoptStatus status : statuses) { |
| 378 |
assertThat(status.getCompilerFilter()).isEqualTo("error"); |
| 379 |
assertThat(status.getCompilationReason()).isEqualTo("error"); |
| 380 |
assertThat(status.getLocationDebugString()).isEqualTo("some error message"); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
@Test |
| 385 |
public void testClearAppProfiles() throws Exception { |
| 386 |
mArtManagerLocal.clearAppProfiles(mSnapshot, PKG_NAME_1); |
| 387 |
|
| 388 |
verify(mArtd).deleteProfile( |
| 389 |
deepEq(AidlUtils.buildProfilePathForPrimaryRef(PKG_NAME_1, "primary"))); |
| 390 |
verify(mArtd).deleteProfile(deepEq( |
| 391 |
AidlUtils.buildProfilePathForPrimaryCur(0 /* userId */, PKG_NAME_1, "primary"))); |
| 392 |
verify(mArtd).deleteProfile(deepEq( |
| 393 |
AidlUtils.buildProfilePathForPrimaryCur(1 /* userId */, PKG_NAME_1, "primary"))); |
| 394 |
|
| 395 |
verify(mArtd).deleteProfile( |
| 396 |
deepEq(AidlUtils.buildProfilePathForSecondaryRef("/data/user/0/foo/1.apk"))); |
| 397 |
verify(mArtd).deleteProfile( |
| 398 |
deepEq(AidlUtils.buildProfilePathForSecondaryCur("/data/user/0/foo/1.apk"))); |
| 399 |
} |
| 400 |
|
| 401 |
@Test(expected = IllegalArgumentException.class) |
| 402 |
public void testClearAppProfilesPackageNotFound() throws Exception { |
| 403 |
when(mSnapshot.getPackageState(anyString())).thenReturn(null); |
| 404 |
|
| 405 |
mArtManagerLocal.clearAppProfiles(mSnapshot, PKG_NAME_1); |
| 406 |
} |
| 407 |
|
| 408 |
@Test(expected = IllegalArgumentException.class) |
| 409 |
public void testClearAppProfilesNoPackage() throws Exception { |
| 410 |
when(mPkgState1.getAndroidPackage()).thenReturn(null); |
| 411 |
|
| 412 |
mArtManagerLocal.clearAppProfiles(mSnapshot, PKG_NAME_1); |
| 413 |
} |
| 414 |
|
| 415 |
@Test |
| 416 |
public void testDexoptPackage() throws Exception { |
| 417 |
var params = new DexoptParams.Builder("install").build(); |
| 418 |
var result = mock(DexoptResult.class); |
| 419 |
var cancellationSignal = new CancellationSignal(); |
| 420 |
|
| 421 |
when(mDexoptHelper.dexopt(any(), deepEq(List.of(PKG_NAME_1)), same(params), |
| 422 |
same(cancellationSignal), any())) |
| 423 |
.thenReturn(result); |
| 424 |
|
| 425 |
assertThat( |
| 426 |
mArtManagerLocal.dexoptPackage(mSnapshot, PKG_NAME_1, params, cancellationSignal)) |
| 427 |
.isSameInstanceAs(result); |
| 428 |
} |
| 429 |
|
| 430 |
@Test |
| 431 |
public void testResetDexoptStatus() throws Exception { |
| 432 |
var result = mock(DexoptResult.class); |
| 433 |
var cancellationSignal = new CancellationSignal(); |
| 434 |
|
| 435 |
when(mDexoptHelper.dexopt( |
| 436 |
any(), deepEq(List.of(PKG_NAME_1)), any(), same(cancellationSignal), any())) |
| 437 |
.thenReturn(result); |
| 438 |
|
| 439 |
assertThat(mArtManagerLocal.resetDexoptStatus(mSnapshot, PKG_NAME_1, cancellationSignal)) |
| 440 |
.isSameInstanceAs(result); |
| 441 |
|
| 442 |
verify(mArtd).deleteProfile( |
| 443 |
deepEq(AidlUtils.buildProfilePathForPrimaryRef(PKG_NAME_1, "primary"))); |
| 444 |
verify(mArtd).deleteProfile(deepEq( |
| 445 |
AidlUtils.buildProfilePathForPrimaryCur(0 /* userId */, PKG_NAME_1, "primary"))); |
| 446 |
verify(mArtd).deleteProfile(deepEq( |
| 447 |
AidlUtils.buildProfilePathForPrimaryCur(1 /* userId */, PKG_NAME_1, "primary"))); |
| 448 |
|
| 449 |
verify(mArtd).deleteArtifacts(deepEq(AidlUtils.buildArtifactsPath( |
| 450 |
"/data/app/foo/base.apk", "arm64", mExpectedIsInDalvikCache))); |
| 451 |
verify(mArtd).deleteArtifacts(deepEq(AidlUtils.buildArtifactsPath( |
| 452 |
"/data/app/foo/base.apk", "arm", mExpectedIsInDalvikCache))); |
| 453 |
verify(mArtd).deleteArtifacts(deepEq(AidlUtils.buildArtifactsPath( |
| 454 |
"/data/app/foo/split_0.apk", "arm64", mExpectedIsInDalvikCache))); |
| 455 |
verify(mArtd).deleteArtifacts(deepEq(AidlUtils.buildArtifactsPath( |
| 456 |
"/data/app/foo/split_0.apk", "arm", mExpectedIsInDalvikCache))); |
| 457 |
|
| 458 |
verify(mArtd).deleteProfile( |
| 459 |
deepEq(AidlUtils.buildProfilePathForSecondaryRef("/data/user/0/foo/1.apk"))); |
| 460 |
verify(mArtd).deleteProfile( |
| 461 |
deepEq(AidlUtils.buildProfilePathForSecondaryCur("/data/user/0/foo/1.apk"))); |
| 462 |
|
| 463 |
verify(mArtd).deleteArtifacts(deepEq(AidlUtils.buildArtifactsPath( |
| 464 |
"/data/user/0/foo/1.apk", "arm64", false /* isInDalvikCache */))); |
| 465 |
} |
| 466 |
|
| 467 |
@Test |
| 468 |
public void testDexoptPackages() throws Exception { |
| 469 |
var dexoptResult = mock(DexoptResult.class); |
| 470 |
var cancellationSignal = new CancellationSignal(); |
| 471 |
when(mDexUseManager.getPackageLastUsedAtMs(PKG_NAME_2)).thenReturn(CURRENT_TIME_MS); |
| 472 |
simulateStorageLow(); |
| 473 |
|
| 474 |
// It should use the default package list and params. The list is sorted by last active |
| 475 |
// time in descending order. |
| 476 |
doReturn(dexoptResult) |
| 477 |
.when(mDexoptHelper) |
| 478 |
.dexopt(any(), deepEq(List.of(PKG_NAME_2, PKG_NAME_1)), |
| 479 |
argThat(params -> params.getReason().equals("bg-dexopt")), |
| 480 |
same(cancellationSignal), any(), any(), any()); |
| 481 |
|
| 482 |
assertThat(mArtManagerLocal.dexoptPackages(mSnapshot, "bg-dexopt", cancellationSignal, |
| 483 |
null /* processCallbackExecutor */, null /* processCallback */)) |
| 484 |
.isSameInstanceAs(dexoptResult); |
| 485 |
|
| 486 |
// Nothing to downgrade. |
| 487 |
verify(mDexoptHelper, never()) |
| 488 |
.dexopt(any(), any(), argThat(params -> params.getReason().equals("inactive")), |
| 489 |
any(), any(), any(), any()); |
| 490 |
} |
| 491 |
|
| 492 |
@Test |
| 493 |
public void testDexoptPackagesRecentlyInstalled() throws Exception { |
| 494 |
// The package is recently installed but hasn't been used. |
| 495 |
PackageUserState userState = mPkgState1.getStateForUser(UserHandle.of(1)); |
| 496 |
when(userState.getFirstInstallTimeMillis()).thenReturn(RECENT_TIME_MS); |
| 497 |
when(mDexUseManager.getPackageLastUsedAtMs(PKG_NAME_1)).thenReturn(0l); |
| 498 |
simulateStorageLow(); |
| 499 |
|
| 500 |
var result = mock(DexoptResult.class); |
| 501 |
var cancellationSignal = new CancellationSignal(); |
| 502 |
|
| 503 |
// PKG_NAME_1 should be dexopted. |
| 504 |
doReturn(result) |
| 505 |
.when(mDexoptHelper) |
| 506 |
.dexopt(any(), inAnyOrder(PKG_NAME_1, PKG_NAME_2), |
| 507 |
argThat(params -> params.getReason().equals("bg-dexopt")), any(), any(), |
| 508 |
any(), any()); |
| 509 |
|
| 510 |
mArtManagerLocal.dexoptPackages(mSnapshot, "bg-dexopt", cancellationSignal, |
| 511 |
null /* processCallbackExecutor */, null /* processCallback */); |
| 512 |
|
| 513 |
// PKG_NAME_1 should not be downgraded. |
| 514 |
verify(mDexoptHelper, never()) |
| 515 |
.dexopt(any(), any(), argThat(params -> params.getReason().equals("inactive")), |
| 516 |
any(), any(), any(), any()); |
| 517 |
} |
| 518 |
|
| 519 |
@Test |
| 520 |
public void testDexoptPackagesInactive() throws Exception { |
| 521 |
// PKG_NAME_1 is neither recently installed nor recently used. |
| 522 |
PackageUserState userState = mPkgState1.getStateForUser(UserHandle.of(1)); |
| 523 |
when(userState.getFirstInstallTimeMillis()).thenReturn(NOT_RECENT_TIME_MS); |
| 524 |
when(mDexUseManager.getPackageLastUsedAtMs(PKG_NAME_1)).thenReturn(NOT_RECENT_TIME_MS); |
| 525 |
simulateStorageLow(); |
| 526 |
|
| 527 |
var result = mock(DexoptResult.class); |
| 528 |
var cancellationSignal = new CancellationSignal(); |
| 529 |
|
| 530 |
// PKG_NAME_1 should not be dexopted. |
| 531 |
doReturn(result) |
| 532 |
.when(mDexoptHelper) |
| 533 |
.dexopt(any(), deepEq(List.of(PKG_NAME_2)), |
| 534 |
argThat(params -> params.getReason().equals("bg-dexopt")), any(), any(), |
| 535 |
any(), any()); |
| 536 |
|
| 537 |
// PKG_NAME_1 should be downgraded. |
| 538 |
doReturn(result) |
| 539 |
.when(mDexoptHelper) |
| 540 |
.dexopt(any(), deepEq(List.of(PKG_NAME_1)), |
| 541 |
argThat(params -> params.getReason().equals("inactive")), any(), any(), |
| 542 |
any(), any()); |
| 543 |
|
| 544 |
mArtManagerLocal.dexoptPackages(mSnapshot, "bg-dexopt", cancellationSignal, |
| 545 |
null /* processCallbackExecutor */, null /* processCallback */); |
| 546 |
} |
| 547 |
|
| 548 |
@Test |
| 549 |
public void testDexoptPackagesInactiveStorageNotLow() throws Exception { |
| 550 |
// PKG_NAME_1 is neither recently installed nor recently used. |
| 551 |
PackageUserState userState = mPkgState1.getStateForUser(UserHandle.of(1)); |
| 552 |
when(userState.getFirstInstallTimeMillis()).thenReturn(NOT_RECENT_TIME_MS); |
| 553 |
when(mDexUseManager.getPackageLastUsedAtMs(PKG_NAME_1)).thenReturn(NOT_RECENT_TIME_MS); |
| 554 |
|
| 555 |
var result = mock(DexoptResult.class); |
| 556 |
var cancellationSignal = new CancellationSignal(); |
| 557 |
|
| 558 |
// PKG_NAME_1 should not be dexopted. |
| 559 |
doReturn(result) |
| 560 |
.when(mDexoptHelper) |
| 561 |
.dexopt(any(), deepEq(List.of(PKG_NAME_2)), |
| 562 |
argThat(params -> params.getReason().equals("bg-dexopt")), any(), any(), |
| 563 |
any(), any()); |
| 564 |
|
| 565 |
mArtManagerLocal.dexoptPackages(mSnapshot, "bg-dexopt", cancellationSignal, |
| 566 |
null /* processCallbackExecutor */, null /* processCallback */); |
| 567 |
|
| 568 |
// PKG_NAME_1 should not be downgraded because the storage is not low. |
| 569 |
verify(mDexoptHelper, never()) |
| 570 |
.dexopt(any(), any(), argThat(params -> params.getReason().equals("inactive")), |
| 571 |
any(), any(), any(), any()); |
| 572 |
} |
| 573 |
|
| 574 |
@Test |
| 575 |
public void testDexoptPackagesBootAfterMainlineUpdate() throws Exception { |
| 576 |
var result = mock(DexoptResult.class); |
| 577 |
var cancellationSignal = new CancellationSignal(); |
| 578 |
|
| 579 |
lenient().when(mInjector.isSystemUiPackage(PKG_NAME_1)).thenReturn(true); |
| 580 |
lenient().when(mInjector.isLauncherPackage(PKG_NAME_2)).thenReturn(true); |
| 581 |
|
| 582 |
// It should dexopt the system UI and the launcher. |
| 583 |
when(mDexoptHelper.dexopt( |
| 584 |
any(), inAnyOrder(PKG_NAME_1, PKG_NAME_2), any(), any(), any(), any(), any())) |
| 585 |
.thenReturn(result); |
| 586 |
|
| 587 |
mArtManagerLocal.dexoptPackages(mSnapshot, "boot-after-mainline-update", cancellationSignal, |
| 588 |
null /* processCallbackExecutor */, null /* processCallback */); |
| 589 |
} |
| 590 |
|
| 591 |
@Test |
| 592 |
public void testDexoptPackagesBootAfterMainlineUpdatePackagesNotFound() throws Exception { |
| 593 |
var result = mock(DexoptResult.class); |
| 594 |
var cancellationSignal = new CancellationSignal(); |
| 595 |
// PKG_NAME_1 is neither recently installed nor recently used. |
| 596 |
PackageUserState userState = mPkgState1.getStateForUser(UserHandle.of(1)); |
| 597 |
lenient().when(userState.getFirstInstallTimeMillis()).thenReturn(NOT_RECENT_TIME_MS); |
| 598 |
lenient() |
| 599 |
.when(mDexUseManager.getPackageLastUsedAtMs(PKG_NAME_1)) |
| 600 |
.thenReturn(NOT_RECENT_TIME_MS); |
| 601 |
simulateStorageLow(); |
| 602 |
|
| 603 |
// It should dexopt the system UI and the launcher, but they are not found. |
| 604 |
when(mDexoptHelper.dexopt(any(), deepEq(List.of()), any(), any(), any(), any(), any())) |
| 605 |
.thenReturn(result); |
| 606 |
|
| 607 |
mArtManagerLocal.dexoptPackages(mSnapshot, "boot-after-mainline-update", cancellationSignal, |
| 608 |
null /* processCallbackExecutor */, null /* processCallback */); |
| 609 |
|
| 610 |
// It should never downgrade apps, even if the storage is low. |
| 611 |
verify(mDexoptHelper, never()) |
| 612 |
.dexopt(any(), any(), argThat(params -> params.getReason().equals("inactive")), |
| 613 |
any(), any(), any(), any()); |
| 614 |
} |
| 615 |
|
| 616 |
@Test |
| 617 |
public void testDexoptPackagesOverride() throws Exception { |
| 618 |
// PKG_NAME_1 is neither recently installed nor recently used. |
| 619 |
PackageUserState userState = mPkgState1.getStateForUser(UserHandle.of(1)); |
| 620 |
when(userState.getFirstInstallTimeMillis()).thenReturn(NOT_RECENT_TIME_MS); |
| 621 |
when(mDexUseManager.getPackageLastUsedAtMs(PKG_NAME_1)).thenReturn(NOT_RECENT_TIME_MS); |
| 622 |
simulateStorageLow(); |
| 623 |
|
| 624 |
var params = new DexoptParams.Builder("bg-dexopt").build(); |
| 625 |
var result = mock(DexoptResult.class); |
| 626 |
var cancellationSignal = new CancellationSignal(); |
| 627 |
|
| 628 |
mArtManagerLocal.setBatchDexoptStartCallback(ForkJoinPool.commonPool(), |
| 629 |
(snapshot, reason, defaultPackages, builder, passedSignal) -> { |
| 630 |
assertThat(reason).isEqualTo("bg-dexopt"); |
| 631 |
assertThat(defaultPackages).containsExactly(PKG_NAME_2); |
| 632 |
assertThat(passedSignal).isSameInstanceAs(cancellationSignal); |
| 633 |
builder.setPackages(List.of(PKG_NAME_1)).setDexoptParams(params); |
| 634 |
}); |
| 635 |
|
| 636 |
// It should use the overridden package list and params. |
| 637 |
doReturn(result) |
| 638 |
.when(mDexoptHelper) |
| 639 |
.dexopt(any(), deepEq(List.of(PKG_NAME_1)), same(params), any(), any(), any(), |
| 640 |
any()); |
| 641 |
|
| 642 |
mArtManagerLocal.dexoptPackages(mSnapshot, "bg-dexopt", cancellationSignal, |
| 643 |
null /* processCallbackExecutor */, null /* processCallback */); |
| 644 |
|
| 645 |
// It should not downgrade PKG_NAME_1 because it's in the overridden package list. It should |
| 646 |
// not downgrade PKG_NAME_2 either because it's not an inactive package. |
| 647 |
verify(mDexoptHelper, never()) |
| 648 |
.dexopt(any(), any(), argThat(params2 -> params2.getReason().equals("inactive")), |
| 649 |
any(), any(), any(), any()); |
| 650 |
} |
| 651 |
|
| 652 |
@Test |
| 653 |
public void testDexoptPackagesOverrideCleared() throws Exception { |
| 654 |
var params = new DexoptParams.Builder("bg-dexopt").build(); |
| 655 |
var result = mock(DexoptResult.class); |
| 656 |
var cancellationSignal = new CancellationSignal(); |
| 657 |
|
| 658 |
mArtManagerLocal.setBatchDexoptStartCallback(ForkJoinPool.commonPool(), |
| 659 |
(snapshot, reason, defaultPackages, builder, passedSignal) -> { |
| 660 |
builder.setPackages(List.of(PKG_NAME_1)).setDexoptParams(params); |
| 661 |
}); |
| 662 |
mArtManagerLocal.clearBatchDexoptStartCallback(); |
| 663 |
|
| 664 |
// It should use the default package list and params. |
| 665 |
when(mDexoptHelper.dexopt(any(), inAnyOrder(PKG_NAME_1, PKG_NAME_2), not(same(params)), |
| 666 |
same(cancellationSignal), any(), any(), any())) |
| 667 |
.thenReturn(result); |
| 668 |
|
| 669 |
assertThat(mArtManagerLocal.dexoptPackages(mSnapshot, "bg-dexopt", cancellationSignal, |
| 670 |
null /* processCallbackExecutor */, null /* processCallback */)) |
| 671 |
.isSameInstanceAs(result); |
| 672 |
} |
| 673 |
|
| 674 |
@Test(expected = IllegalStateException.class) |
| 675 |
public void testDexoptPackagesOverrideReasonChanged() throws Exception { |
| 676 |
var params = new DexoptParams.Builder("first-boot").build(); |
| 677 |
var cancellationSignal = new CancellationSignal(); |
| 678 |
|
| 679 |
mArtManagerLocal.setBatchDexoptStartCallback(ForkJoinPool.commonPool(), |
| 680 |
(snapshot, reason, defaultPackages, builder, passedSignal) -> { |
| 681 |
builder.setDexoptParams(params); |
| 682 |
}); |
| 683 |
|
| 684 |
mArtManagerLocal.dexoptPackages(mSnapshot, "bg-dexopt", cancellationSignal, |
| 685 |
null /* processCallbackExecutor */, null /* processCallback */); |
| 686 |
} |
| 687 |
|
| 688 |
@Test |
| 689 |
public void testSnapshotAppProfile() throws Exception { |
| 690 |
var options = new MergeProfileOptions(); |
| 691 |
options.forceMerge = true; |
| 692 |
options.forBootImage = false; |
| 693 |
|
| 694 |
File tempFile = File.createTempFile("primary", ".prof"); |
| 695 |
tempFile.deleteOnExit(); |
| 696 |
|
| 697 |
ProfilePath refProfile = AidlUtils.buildProfilePathForPrimaryRef(PKG_NAME_1, "primary"); |
| 698 |
String dexPath = "/data/app/foo/base.apk"; |
| 699 |
|
| 700 |
when(mArtd.isProfileUsable(deepEq(refProfile), eq(dexPath))).thenReturn(true); |
| 701 |
|
| 702 |
when(mArtd.mergeProfiles(deepEq(List.of(refProfile, |
| 703 |
AidlUtils.buildProfilePathForPrimaryCur( |
| 704 |
0 /* userId */, PKG_NAME_1, "primary"), |
| 705 |
AidlUtils.buildProfilePathForPrimaryCur( |
| 706 |
1 /* userId */, PKG_NAME_1, "primary"))), |
| 707 |
isNull(), |
| 708 |
deepEq(AidlUtils.buildOutputProfileForPrimary(PKG_NAME_1, "primary", |
| 709 |
Process.SYSTEM_UID, Process.SYSTEM_UID, false /* isPublic */)), |
| 710 |
deepEq(List.of(dexPath)), deepEq(options))) |
| 711 |
.thenAnswer(invocation -> { |
| 712 |
try (var writer = new FileWriter(tempFile)) { |
| 713 |
writer.write("snapshot"); |
| 714 |
} catch (IOException e) { |
| 715 |
throw new RuntimeException(e); |
| 716 |
} |
| 717 |
var output = invocation.<OutputProfile>getArgument(2); |
| 718 |
output.profilePath.tmpPath = tempFile.getPath(); |
| 719 |
return true; |
| 720 |
}); |
| 721 |
|
| 722 |
ParcelFileDescriptor fd = |
| 723 |
mArtManagerLocal.snapshotAppProfile(mSnapshot, PKG_NAME_1, null /* splitName */); |
| 724 |
|
| 725 |
verify(mArtd).deleteProfile( |
| 726 |
argThat(profile -> profile.getTmpProfilePath().tmpPath.equals(tempFile.getPath()))); |
| 727 |
|
| 728 |
assertThat(fd.getStatSize()).isGreaterThan(0); |
| 729 |
try (InputStream inputStream = new AutoCloseInputStream(fd)) { |
| 730 |
String contents = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8); |
| 731 |
assertThat(contents).isEqualTo("snapshot"); |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
@Test |
| 736 |
public void testSnapshotAppProfileFromDm() throws Exception { |
| 737 |
String tempPathForRef = "/temp/path/for/ref"; |
| 738 |
File tempFileForSnapshot = File.createTempFile("primary", ".prof"); |
| 739 |
tempFileForSnapshot.deleteOnExit(); |
| 740 |
|
| 741 |
ProfilePath refProfile = AidlUtils.buildProfilePathForPrimaryRef(PKG_NAME_1, "primary"); |
| 742 |
String dexPath = "/data/app/foo/base.apk"; |
| 743 |
|
| 744 |
// Simulate that the reference profile doesn't exist. |
| 745 |
when(mArtd.isProfileUsable(deepEq(refProfile), eq(dexPath))).thenReturn(false); |
| 746 |
|
| 747 |
// The DM file is usable. |
| 748 |
when(mArtd.copyAndRewriteProfile( |
| 749 |
deepEq(AidlUtils.buildProfilePathForDm(dexPath)), any(), eq(dexPath))) |
| 750 |
.thenAnswer(invocation -> { |
| 751 |
var output = invocation.<OutputProfile>getArgument(1); |
| 752 |
output.profilePath.tmpPath = tempPathForRef; |
| 753 |
return true; |
| 754 |
}); |
| 755 |
|
| 756 |
// Verify that the reference file initialized from the DM file is used. |
| 757 |
when(mArtd.mergeProfiles( |
| 758 |
argThat(profiles |
| 759 |
-> profiles.stream().anyMatch(profile |
| 760 |
-> profile.getTag() == ProfilePath.tmpProfilePath |
| 761 |
&& profile.getTmpProfilePath().tmpPath.equals( |
| 762 |
tempPathForRef))), |
| 763 |
isNull(), any(), deepEq(List.of(dexPath)), any())) |
| 764 |
.thenAnswer(invocation -> { |
| 765 |
var output = invocation.<OutputProfile>getArgument(2); |
| 766 |
output.profilePath.tmpPath = tempFileForSnapshot.getPath(); |
| 767 |
return true; |
| 768 |
}); |
| 769 |
|
| 770 |
ParcelFileDescriptor fd = |
| 771 |
mArtManagerLocal.snapshotAppProfile(mSnapshot, PKG_NAME_1, null /* splitName */); |
| 772 |
|
| 773 |
verify(mArtd).deleteProfile(argThat(profile |
| 774 |
-> profile.getTmpProfilePath().tmpPath.equals(tempFileForSnapshot.getPath()))); |
| 775 |
verify(mArtd).deleteProfile( |
| 776 |
argThat(profile -> profile.getTmpProfilePath().tmpPath.equals(tempPathForRef))); |
| 777 |
} |
| 778 |
|
| 779 |
@Test |
| 780 |
public void testSnapshotAppProfileSplit() throws Exception { |
| 781 |
ProfilePath refProfile = |
| 782 |
AidlUtils.buildProfilePathForPrimaryRef(PKG_NAME_1, "split_0.split"); |
| 783 |
String dexPath = "/data/app/foo/split_0.apk"; |
| 784 |
|
| 785 |
when(mArtd.isProfileUsable(deepEq(refProfile), eq(dexPath))).thenReturn(true); |
| 786 |
|
| 787 |
when(mArtd.mergeProfiles(deepEq(List.of(refProfile, |
| 788 |
AidlUtils.buildProfilePathForPrimaryCur( |
| 789 |
0 /* userId */, PKG_NAME_1, "split_0.split"), |
| 790 |
AidlUtils.buildProfilePathForPrimaryCur( |
| 791 |
1 /* userId */, PKG_NAME_1, "split_0.split"))), |
| 792 |
isNull(), |
| 793 |
deepEq(AidlUtils.buildOutputProfileForPrimary(PKG_NAME_1, "split_0.split", |
| 794 |
Process.SYSTEM_UID, Process.SYSTEM_UID, false /* isPublic */)), |
| 795 |
deepEq(List.of(dexPath)), any())) |
| 796 |
.thenReturn(false); |
| 797 |
|
| 798 |
mArtManagerLocal.snapshotAppProfile(mSnapshot, PKG_NAME_1, "split_0"); |
| 799 |
} |
| 800 |
|
| 801 |
@Test |
| 802 |
public void testSnapshotAppProfileEmpty() throws Exception { |
| 803 |
when(mArtd.mergeProfiles(any(), any(), any(), any(), any())).thenReturn(false); |
| 804 |
|
| 805 |
ParcelFileDescriptor fd = |
| 806 |
mArtManagerLocal.snapshotAppProfile(mSnapshot, PKG_NAME_1, null /* splitName */); |
| 807 |
|
| 808 |
verify(mArtd, never()).deleteProfile(any()); |
| 809 |
|
| 810 |
assertThat(fd.getStatSize()).isEqualTo(0); |
| 811 |
try (InputStream inputStream = new AutoCloseInputStream(fd)) { |
| 812 |
assertThat(inputStream.readAllBytes()).isEmpty(); |
| 813 |
} |
| 814 |
} |
| 815 |
|
| 816 |
@Test(expected = IllegalArgumentException.class) |
| 817 |
public void testSnapshotAppProfilePackageNotFound() throws Exception { |
| 818 |
when(mSnapshot.getPackageState(anyString())).thenReturn(null); |
| 819 |
|
| 820 |
mArtManagerLocal.snapshotAppProfile(mSnapshot, PKG_NAME_1, null /* splitName */); |
| 821 |
} |
| 822 |
|
| 823 |
@Test(expected = IllegalArgumentException.class) |
| 824 |
public void testSnapshotAppProfileNoPackage() throws Exception { |
| 825 |
when(mPkgState1.getAndroidPackage()).thenReturn(null); |
| 826 |
|
| 827 |
mArtManagerLocal.snapshotAppProfile(mSnapshot, PKG_NAME_1, null /* splitName */); |
| 828 |
} |
| 829 |
|
| 830 |
@Test(expected = IllegalArgumentException.class) |
| 831 |
public void testSnapshotAppProfileSplitNotFound() throws Exception { |
| 832 |
mArtManagerLocal.snapshotAppProfile(mSnapshot, PKG_NAME_1, "non-existent-split"); |
| 833 |
} |
| 834 |
|
| 835 |
@Test |
| 836 |
public void testDumpAppProfile() throws Exception { |
| 837 |
var options = new MergeProfileOptions(); |
| 838 |
options.dumpOnly = true; |
| 839 |
|
| 840 |
when(mArtd.mergeProfiles(any(), isNull(), any(), any(), deepEq(options))) |
| 841 |
.thenReturn(false); // A non-empty merge is tested in `testSnapshotAppProfile`. |
| 842 |
|
| 843 |
ParcelFileDescriptor fd = mArtManagerLocal.dumpAppProfile( |
| 844 |
mSnapshot, PKG_NAME_1, null /* splitName */, false /* dumpClassesAndMethods */); |
| 845 |
} |
| 846 |
|
| 847 |
@Test |
| 848 |
public void testDumpAppProfileDumpClassesAndMethods() throws Exception { |
| 849 |
var options = new MergeProfileOptions(); |
| 850 |
options.dumpClassesAndMethods = true; |
| 851 |
|
| 852 |
when(mArtd.mergeProfiles(any(), isNull(), any(), any(), deepEq(options))) |
| 853 |
.thenReturn(false); // A non-empty merge is tested in `testSnapshotAppProfile`. |
| 854 |
|
| 855 |
ParcelFileDescriptor fd = mArtManagerLocal.dumpAppProfile( |
| 856 |
mSnapshot, PKG_NAME_1, null /* splitName */, true /* dumpClassesAndMethods */); |
| 857 |
} |
| 858 |
|
| 859 |
@Test |
| 860 |
public void testSnapshotBootImageProfile() throws Exception { |
| 861 |
// `lenient()` is required to allow mocking the same method multiple times. |
| 862 |
lenient().when(Constants.getenv("BOOTCLASSPATH")).thenReturn("bcp0:bcp1"); |
| 863 |
lenient().when(Constants.getenv("SYSTEMSERVERCLASSPATH")).thenReturn("sscp0:sscp1"); |
| 864 |
lenient().when(Constants.getenv("STANDALONE_SYSTEMSERVER_JARS")).thenReturn("sssj0:sssj1"); |
| 865 |
|
| 866 |
var options = new MergeProfileOptions(); |
| 867 |
options.forceMerge = true; |
| 868 |
options.forBootImage = true; |
| 869 |
|
| 870 |
when(mArtd.mergeProfiles( |
| 871 |
inAnyOrderDeepEquals( |
| 872 |
AidlUtils.buildProfilePathForPrimaryRef("android", "primary"), |
| 873 |
AidlUtils.buildProfilePathForPrimaryCur( |
| 874 |
0 /* userId */, "android", "primary"), |
| 875 |
AidlUtils.buildProfilePathForPrimaryCur( |
| 876 |
1 /* userId */, "android", "primary"), |
| 877 |
AidlUtils.buildProfilePathForPrimaryRef(PKG_NAME_1, "primary"), |
| 878 |
AidlUtils.buildProfilePathForPrimaryCur( |
| 879 |
0 /* userId */, PKG_NAME_1, "primary"), |
| 880 |
AidlUtils.buildProfilePathForPrimaryCur( |
| 881 |
1 /* userId */, PKG_NAME_1, "primary"), |
| 882 |
AidlUtils.buildProfilePathForPrimaryRef(PKG_NAME_1, "split_0.split"), |
| 883 |
AidlUtils.buildProfilePathForPrimaryCur( |
| 884 |
0 /* userId */, PKG_NAME_1, "split_0.split"), |
| 885 |
AidlUtils.buildProfilePathForPrimaryCur( |
| 886 |
1 /* userId */, PKG_NAME_1, "split_0.split"), |
| 887 |
AidlUtils.buildProfilePathForPrimaryRef(PKG_NAME_2, "primary"), |
| 888 |
AidlUtils.buildProfilePathForPrimaryCur( |
| 889 |
0 /* userId */, PKG_NAME_2, "primary"), |
| 890 |
AidlUtils.buildProfilePathForPrimaryCur( |
| 891 |
1 /* userId */, PKG_NAME_2, "primary"), |
| 892 |
AidlUtils.buildProfilePathForPrimaryRef( |
| 893 |
PKG_NAME_HIBERNATING, "primary"), |
| 894 |
AidlUtils.buildProfilePathForPrimaryCur( |
| 895 |
0 /* userId */, PKG_NAME_HIBERNATING, "primary"), |
| 896 |
AidlUtils.buildProfilePathForPrimaryCur( |
| 897 |
1 /* userId */, PKG_NAME_HIBERNATING, "primary")), |
| 898 |
isNull(), |
| 899 |
deepEq(AidlUtils.buildOutputProfileForPrimary("android", "primary", |
| 900 |
Process.SYSTEM_UID, Process.SYSTEM_UID, false /* isPublic */)), |
| 901 |
deepEq(List.of("bcp0", "bcp1", "sscp0", "sscp1", "sssj0", "sssj1")), |
| 902 |
deepEq(options))) |
| 903 |
.thenReturn(false); // A non-empty merge is tested in `testSnapshotAppProfile`. |
| 904 |
|
| 905 |
mArtManagerLocal.snapshotBootImageProfile(mSnapshot); |
| 906 |
} |
| 907 |
|
| 908 |
@Test |
| 909 |
public void testCleanup() throws Exception { |
| 910 |
// It should keep all artifacts. |
| 911 |
doReturn(createGetDexoptStatusResult("speed-profile", "bg-dexopt", "location")) |
| 912 |
.when(mArtd) |
| 913 |
.getDexoptStatus(eq("/data/app/foo/base.apk"), eq("arm64"), any()); |
| 914 |
doReturn(createGetDexoptStatusResult("verify", "cmdline", "location")) |
| 915 |
.when(mArtd) |
| 916 |
.getDexoptStatus(eq("/data/user/0/foo/1.apk"), eq("arm64"), any()); |
| 917 |
|
| 918 |
// It should only keep VDEX files. |
| 919 |
doReturn(createGetDexoptStatusResult("verify", "vdex", "location")) |
| 920 |
.when(mArtd) |
| 921 |
.getDexoptStatus(eq("/data/app/foo/split_0.apk"), eq("arm64"), any()); |
| 922 |
doReturn(createGetDexoptStatusResult("verify", "vdex", "location")) |
| 923 |
.when(mArtd) |
| 924 |
.getDexoptStatus(eq("/data/app/foo/split_0.apk"), eq("arm"), any()); |
| 925 |
|
| 926 |
// It should not keep any artifacts. |
| 927 |
doReturn(createGetDexoptStatusResult("run-from-apk", "unknown", "unknown")) |
| 928 |
.when(mArtd) |
| 929 |
.getDexoptStatus(eq("/data/app/foo/base.apk"), eq("arm"), any()); |
| 930 |
|
| 931 |
when(mSnapshot.getPackageStates()).thenReturn(Map.of(PKG_NAME_1, mPkgState1)); |
| 932 |
mArtManagerLocal.cleanup(mSnapshot); |
| 933 |
|
| 934 |
verify(mArtd).cleanup( |
| 935 |
inAnyOrderDeepEquals(AidlUtils.buildProfilePathForPrimaryRef(PKG_NAME_1, "primary"), |
| 936 |
AidlUtils.buildProfilePathForPrimaryCur( |
| 937 |
0 /* userId */, PKG_NAME_1, "primary"), |
| 938 |
AidlUtils.buildProfilePathForPrimaryCur( |
| 939 |
1 /* userId */, PKG_NAME_1, "primary"), |
| 940 |
AidlUtils.buildProfilePathForPrimaryRef(PKG_NAME_1, "split_0.split"), |
| 941 |
AidlUtils.buildProfilePathForPrimaryCur( |
| 942 |
0 /* userId */, PKG_NAME_1, "split_0.split"), |
| 943 |
AidlUtils.buildProfilePathForPrimaryCur( |
| 944 |
1 /* userId */, PKG_NAME_1, "split_0.split"), |
| 945 |
AidlUtils.buildProfilePathForSecondaryRef("/data/user/0/foo/1.apk"), |
| 946 |
AidlUtils.buildProfilePathForSecondaryCur("/data/user/0/foo/1.apk")), |
| 947 |
inAnyOrderDeepEquals(AidlUtils.buildArtifactsPath("/data/app/foo/base.apk", "arm64", |
| 948 |
mExpectedIsInDalvikCache), |
| 949 |
AidlUtils.buildArtifactsPath( |
| 950 |
"/data/user/0/foo/1.apk", "arm64", false /* isInDalvikCache */)), |
| 951 |
inAnyOrderDeepEquals( |
| 952 |
VdexPath.artifactsPath(AidlUtils.buildArtifactsPath( |
| 953 |
"/data/app/foo/split_0.apk", "arm64", mExpectedIsInDalvikCache)), |
| 954 |
VdexPath.artifactsPath(AidlUtils.buildArtifactsPath( |
| 955 |
"/data/app/foo/split_0.apk", "arm", mExpectedIsInDalvikCache)))); |
| 956 |
} |
| 957 |
|
| 958 |
private AndroidPackage createPackage(boolean multiSplit) { |
| 959 |
AndroidPackage pkg = mock(AndroidPackage.class); |
| 960 |
|
| 961 |
var baseSplit = mock(AndroidPackageSplit.class); |
| 962 |
lenient().when(baseSplit.getPath()).thenReturn("/data/app/foo/base.apk"); |
| 963 |
lenient().when(baseSplit.isHasCode()).thenReturn(true); |
| 964 |
|
| 965 |
if (multiSplit) { |
| 966 |
// split_0 has code while split_1 doesn't. |
| 967 |
var split0 = mock(AndroidPackageSplit.class); |
| 968 |
lenient().when(split0.getName()).thenReturn("split_0"); |
| 969 |
lenient().when(split0.getPath()).thenReturn("/data/app/foo/split_0.apk"); |
| 970 |
lenient().when(split0.isHasCode()).thenReturn(true); |
| 971 |
var split1 = mock(AndroidPackageSplit.class); |
| 972 |
lenient().when(split1.getName()).thenReturn("split_1"); |
| 973 |
lenient().when(split1.getPath()).thenReturn("/data/app/foo/split_1.apk"); |
| 974 |
lenient().when(split1.isHasCode()).thenReturn(false); |
| 975 |
|
| 976 |
lenient().when(pkg.getSplits()).thenReturn(List.of(baseSplit, split0, split1)); |
| 977 |
} else { |
| 978 |
lenient().when(pkg.getSplits()).thenReturn(List.of(baseSplit)); |
| 979 |
} |
| 980 |
|
| 981 |
return pkg; |
| 982 |
} |
| 983 |
|
| 984 |
private PackageUserState createPackageUserState() { |
| 985 |
PackageUserState pkgUserState = mock(PackageUserState.class); |
| 986 |
lenient().when(pkgUserState.isInstalled()).thenReturn(true); |
| 987 |
// All packages are by default pre-installed. |
| 988 |
lenient().when(pkgUserState.getFirstInstallTimeMillis()).thenReturn(0l); |
| 989 |
return pkgUserState; |
| 990 |
} |
| 991 |
|
| 992 |
private PackageState createPackageState( |
| 993 |
String packageName, boolean isDexoptable, boolean multiSplit) { |
| 994 |
PackageState pkgState = mock(PackageState.class); |
| 995 |
|
| 996 |
lenient().when(pkgState.getPackageName()).thenReturn(packageName); |
| 997 |
lenient().when(pkgState.getPrimaryCpuAbi()).thenReturn("arm64-v8a"); |
| 998 |
lenient().when(pkgState.getSecondaryCpuAbi()).thenReturn("armeabi-v7a"); |
| 999 |
lenient().when(pkgState.isSystem()).thenReturn(mIsInSystemPartition); |
| 1000 |
lenient().when(pkgState.isUpdatedSystemApp()).thenReturn(false); |
| 1001 |
|
| 1002 |
AndroidPackage pkg = createPackage(multiSplit); |
| 1003 |
lenient().when(pkgState.getAndroidPackage()).thenReturn(pkg); |
| 1004 |
|
| 1005 |
PackageUserState pkgUserState0 = createPackageUserState(); |
| 1006 |
lenient().when(pkgState.getStateForUser(UserHandle.of(0))).thenReturn(pkgUserState0); |
| 1007 |
PackageUserState pkgUserState1 = createPackageUserState(); |
| 1008 |
lenient().when(pkgState.getStateForUser(UserHandle.of(1))).thenReturn(pkgUserState1); |
| 1009 |
|
| 1010 |
lenient().when(PackageStateModulesUtils.isDexoptable(pkgState)).thenReturn(isDexoptable); |
| 1011 |
|
| 1012 |
return pkgState; |
| 1013 |
} |
| 1014 |
|
| 1015 |
private List<PackageState> createPackageStates() { |
| 1016 |
PackageState pkgState1 = |
| 1017 |
createPackageState(PKG_NAME_1, true /* isDexoptable */, true /* multiSplit */); |
| 1018 |
|
| 1019 |
PackageState pkgState2 = |
| 1020 |
createPackageState(PKG_NAME_2, true /* isDexoptable */, false /* multiSplit */); |
| 1021 |
|
| 1022 |
// This should not be dexopted because it's hibernating. However, it should be included |
| 1023 |
// when snapshotting boot image profile. |
| 1024 |
PackageState pkgHibernatingState = createPackageState( |
| 1025 |
PKG_NAME_HIBERNATING, true /* isDexoptable */, false /* multiSplit */); |
| 1026 |
lenient() |
| 1027 |
.when(mAppHibernationManager.isHibernatingGlobally(PKG_NAME_HIBERNATING)) |
| 1028 |
.thenReturn(true); |
| 1029 |
|
| 1030 |
// This should not be dexopted because it's not dexoptable. |
| 1031 |
PackageState nonDexoptablePkgState = createPackageState( |
| 1032 |
"com.example.non-dexoptable", false /* isDexoptable */, false /* multiSplit */); |
| 1033 |
|
| 1034 |
return List.of(pkgState1, pkgState2, pkgHibernatingState, nonDexoptablePkgState); |
| 1035 |
} |
| 1036 |
|
| 1037 |
private GetDexoptStatusResult createGetDexoptStatusResult( |
| 1038 |
String compilerFilter, String compilationReason, String locationDebugString) { |
| 1039 |
var getDexoptStatusResult = new GetDexoptStatusResult(); |
| 1040 |
getDexoptStatusResult.compilerFilter = compilerFilter; |
| 1041 |
getDexoptStatusResult.compilationReason = compilationReason; |
| 1042 |
getDexoptStatusResult.locationDebugString = locationDebugString; |
| 1043 |
return getDexoptStatusResult; |
| 1044 |
} |
| 1045 |
|
| 1046 |
private List<DetailedSecondaryDexInfo> createSecondaryDexInfo() throws Exception { |
| 1047 |
var dexInfo = mock(DetailedSecondaryDexInfo.class); |
| 1048 |
lenient().when(dexInfo.dexPath()).thenReturn("/data/user/0/foo/1.apk"); |
| 1049 |
lenient().when(dexInfo.abiNames()).thenReturn(Set.of("arm64-v8a")); |
| 1050 |
lenient().when(dexInfo.classLoaderContext()).thenReturn("CLC"); |
| 1051 |
return List.of(dexInfo); |
| 1052 |
} |
| 1053 |
|
| 1054 |
private void simulateStorageLow() throws Exception { |
| 1055 |
lenient() |
| 1056 |
.when(mStorageManager.getAllocatableBytes(any())) |
| 1057 |
.thenReturn(ArtManagerLocal.DOWNGRADE_THRESHOLD_ABOVE_LOW_BYTES - 1); |
| 1058 |
} |
| 1059 |
|
| 1060 |
private void simulateStorageNotLow() throws Exception { |
| 1061 |
lenient() |
| 1062 |
.when(mStorageManager.getAllocatableBytes(any())) |
| 1063 |
.thenReturn(ArtManagerLocal.DOWNGRADE_THRESHOLD_ABOVE_LOW_BYTES); |
| 1064 |
} |
| 1065 |
} |