/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License */ package com.android.server.pm; import android.annotation.Nullable; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageParser; import android.content.pm.PackageParser.Package; import android.os.PowerManager; import android.os.UserHandle; import android.os.WorkSource; import android.os.storage.StorageManager; import android.util.ArraySet; import android.util.Log; import android.util.Slog; import com.android.internal.os.InstallerConnection.InstallerException; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import dalvik.system.DexFile; import static com.android.server.pm.Installer.DEXOPT_BOOTCOMPLETE; import static com.android.server.pm.Installer.DEXOPT_DEBUGGABLE; import static com.android.server.pm.Installer.DEXOPT_PUBLIC; import static com.android.server.pm.Installer.DEXOPT_SAFEMODE; import static com.android.server.pm.Installer.DEXOPT_EXTRACTONLY; import static com.android.server.pm.InstructionSets.getAppDexInstructionSets; import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets; /** * Helper class for running dexopt command on packages. */ class PackageDexOptimizer { private static final String TAG = "PackageManager.DexOptimizer"; static final String OAT_DIR_NAME = "oat"; // TODO b/19550105 Remove error codes and use exceptions static final int DEX_OPT_SKIPPED = 0; static final int DEX_OPT_PERFORMED = 1; static final int DEX_OPT_DEFERRED = 2; static final int DEX_OPT_FAILED = -1; private static final boolean DEBUG_DEXOPT = PackageManagerService.DEBUG_DEXOPT; private final Installer mInstaller; private final Object mInstallLock; private final PowerManager.WakeLock mDexoptWakeLock; private volatile boolean mSystemReady; PackageDexOptimizer(Installer installer, Object installLock, Context context, String wakeLockTag) { this.mInstaller = installer; this.mInstallLock = installLock; PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE); mDexoptWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakeLockTag); } protected PackageDexOptimizer(PackageDexOptimizer from) { this.mInstaller = from.mInstaller; this.mInstallLock = from.mInstallLock; this.mDexoptWakeLock = from.mDexoptWakeLock; this.mSystemReady = from.mSystemReady; } static boolean canOptimizePackage(PackageParser.Package pkg) { return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0; } /** * Performs dexopt on all code paths and libraries of the specified package for specified * instruction sets. * *
Calls to {@link com.android.server.pm.Installer#dexopt} on {@link #mInstaller} are
* synchronized on {@link #mInstallLock}.
*/
int performDexOpt(PackageParser.Package pkg, String[] instructionSets, boolean useProfiles,
boolean extractOnly) {
synchronized (mInstallLock) {
final boolean useLock = mSystemReady;
if (useLock) {
mDexoptWakeLock.setWorkSource(new WorkSource(pkg.applicationInfo.uid));
mDexoptWakeLock.acquire();
}
try {
return performDexOptLI(pkg, instructionSets, useProfiles, extractOnly);
} finally {
if (useLock) {
mDexoptWakeLock.release();
}
}
}
}
/**
* Determine whether the package should be skipped for the given instruction set. A return
* value of true means the package will be skipped. A return value of false means that the
* package will be further investigated, and potentially compiled.
*/
protected boolean shouldSkipBasedOnISA(PackageParser.Package pkg, String instructionSet) {
return pkg.mDexOptPerformed.contains(instructionSet);
}
/**
* Adjust the given dexopt-needed value. Can be overridden to influence the decision to
* optimize or not (and in what way).
*/
protected int adjustDexoptNeeded(int dexoptNeeded) {
return dexoptNeeded;
}
/**
* Adjust the given dexopt flags that will be passed to the installer.
*/
protected int adjustDexoptFlags(int dexoptFlags) {
return dexoptFlags;
}
/**
* Update the package status after a successful compilation.
*/
protected void recordSuccessfulDexopt(PackageParser.Package pkg, String instructionSet) {
pkg.mDexOptPerformed.add(instructionSet);
}
private int performDexOptLI(PackageParser.Package pkg, String[] targetInstructionSets,
boolean useProfiles, boolean extractOnly) {
final String[] instructionSets = targetInstructionSets != null ?
targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo);
if (!canOptimizePackage(pkg)) {
return DEX_OPT_SKIPPED;
}
final boolean vmSafeMode = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0;
final boolean debuggable = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
final List
*
*
* @return Absolute path to the oat directory or null, if oat directory
* cannot be created.
*/
@Nullable
private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) {
if (!pkg.canHaveOatDir()) {
return null;
}
File codePath = new File(pkg.codePath);
if (codePath.isDirectory()) {
File oatDir = getOatDir(codePath);
try {
mInstaller.createOatDir(oatDir.getAbsolutePath(), dexInstructionSet);
} catch (InstallerException e) {
Slog.w(TAG, "Failed to create oat dir", e);
return null;
}
return oatDir.getAbsolutePath();
}
return null;
}
static File getOatDir(File codePath) {
return new File(codePath, OAT_DIR_NAME);
}
void systemReady() {
mSystemReady = true;
}
/**
* A specialized PackageDexOptimizer that overrides already-installed checks, forcing a
* dexopt path.
*/
public static class ForcedUpdatePackageDexOptimizer extends PackageDexOptimizer {
public ForcedUpdatePackageDexOptimizer(Installer installer, Object installLock,
Context context, String wakeLockTag) {
super(installer, installLock, context, wakeLockTag);
}
public ForcedUpdatePackageDexOptimizer(PackageDexOptimizer from) {
super(from);
}
@Override
protected boolean shouldSkipBasedOnISA(Package pkg, String instructionSet) {
// Forced compilation, never skip.
return false;
}
@Override
protected int adjustDexoptNeeded(int dexoptNeeded) {
// Ensure compilation, no matter the current state.
// TODO: The return value is wrong when patchoat is needed.
return DexFile.DEX2OAT_NEEDED;
}
}
}