blob: 64af21313a1d38e56a30450ecccf4fe4c1590c03 [file] [log] [blame]
Fyodor Kupolov74876572015-02-23 17:14:45 -08001/*
2 * Copyright (C) 2015 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
17package com.android.server.pm;
18
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080019import android.annotation.Nullable;
Fyodor Kupolova627c092015-05-05 18:44:39 -070020import android.content.Context;
Fyodor Kupolov74876572015-02-23 17:14:45 -080021import android.content.pm.ApplicationInfo;
22import android.content.pm.PackageParser;
Andreas Gampea8908752015-11-10 08:58:14 -080023import android.content.pm.PackageParser.Package;
Fyodor Kupolova627c092015-05-05 18:44:39 -070024import android.os.PowerManager;
Fyodor Kupolov74876572015-02-23 17:14:45 -080025import android.os.UserHandle;
Fyodor Kupolova627c092015-05-05 18:44:39 -070026import android.os.WorkSource;
Calin Juravledb4a79a2015-12-23 18:55:08 +020027import android.os.storage.StorageManager;
Fyodor Kupolov74876572015-02-23 17:14:45 -080028import android.util.ArraySet;
29import android.util.Log;
30import android.util.Slog;
31
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -070032import com.android.internal.os.InstallerConnection.InstallerException;
33
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080034import java.io.File;
Fyodor Kupolov74876572015-02-23 17:14:45 -080035import java.io.IOException;
36import java.util.ArrayList;
37import java.util.List;
38
39import dalvik.system.DexFile;
Fyodor Kupolov74876572015-02-23 17:14:45 -080040
Todd Kennedyfa54ab72015-09-25 07:46:12 -070041import static com.android.server.pm.Installer.DEXOPT_BOOTCOMPLETE;
42import static com.android.server.pm.Installer.DEXOPT_DEBUGGABLE;
43import static com.android.server.pm.Installer.DEXOPT_PUBLIC;
44import static com.android.server.pm.Installer.DEXOPT_SAFEMODE;
David Brazdila0e10432016-01-20 14:04:40 +000045import static com.android.server.pm.Installer.DEXOPT_EXTRACTONLY;
Fyodor Kupolov74876572015-02-23 17:14:45 -080046import static com.android.server.pm.InstructionSets.getAppDexInstructionSets;
47import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets;
48
49/**
50 * Helper class for running dexopt command on packages.
51 */
Andreas Gampea8908752015-11-10 08:58:14 -080052class PackageDexOptimizer {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080053 private static final String TAG = "PackageManager.DexOptimizer";
54 static final String OAT_DIR_NAME = "oat";
55 // TODO b/19550105 Remove error codes and use exceptions
Fyodor Kupolov74876572015-02-23 17:14:45 -080056 static final int DEX_OPT_SKIPPED = 0;
57 static final int DEX_OPT_PERFORMED = 1;
58 static final int DEX_OPT_DEFERRED = 2;
59 static final int DEX_OPT_FAILED = -1;
60
Andreas Gampea8908752015-11-10 08:58:14 -080061 private static final boolean DEBUG_DEXOPT = PackageManagerService.DEBUG_DEXOPT;
62
63 private final Installer mInstaller;
64 private final Object mInstallLock;
Fyodor Kupolov74876572015-02-23 17:14:45 -080065
Fyodor Kupolova627c092015-05-05 18:44:39 -070066 private final PowerManager.WakeLock mDexoptWakeLock;
67 private volatile boolean mSystemReady;
68
Andreas Gampea8908752015-11-10 08:58:14 -080069 PackageDexOptimizer(Installer installer, Object installLock, Context context,
70 String wakeLockTag) {
71 this.mInstaller = installer;
72 this.mInstallLock = installLock;
73
74 PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
75 mDexoptWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakeLockTag);
76 }
77
78 protected PackageDexOptimizer(PackageDexOptimizer from) {
79 this.mInstaller = from.mInstaller;
80 this.mInstallLock = from.mInstallLock;
81 this.mDexoptWakeLock = from.mDexoptWakeLock;
82 this.mSystemReady = from.mSystemReady;
Fyodor Kupolov74876572015-02-23 17:14:45 -080083 }
84
Calin Juravledb4a79a2015-12-23 18:55:08 +020085 static boolean canOptimizePackage(PackageParser.Package pkg) {
Calin Juravle6dfd83d2016-01-29 18:23:57 +000086 return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0;
Calin Juravledb4a79a2015-12-23 18:55:08 +020087 }
88
Fyodor Kupolov74876572015-02-23 17:14:45 -080089 /**
90 * Performs dexopt on all code paths and libraries of the specified package for specified
91 * instruction sets.
92 *
Andreas Gampea8908752015-11-10 08:58:14 -080093 * <p>Calls to {@link com.android.server.pm.Installer#dexopt} on {@link #mInstaller} are
94 * synchronized on {@link #mInstallLock}.
Fyodor Kupolov74876572015-02-23 17:14:45 -080095 */
Andreas Gampea8908752015-11-10 08:58:14 -080096 int performDexOpt(PackageParser.Package pkg, String[] instructionSets, boolean useProfiles,
97 boolean extractOnly) {
98 synchronized (mInstallLock) {
Fyodor Kupolova627c092015-05-05 18:44:39 -070099 final boolean useLock = mSystemReady;
100 if (useLock) {
101 mDexoptWakeLock.setWorkSource(new WorkSource(pkg.applicationInfo.uid));
102 mDexoptWakeLock.acquire();
103 }
104 try {
Andreas Gampea8908752015-11-10 08:58:14 -0800105 return performDexOptLI(pkg, instructionSets, useProfiles, extractOnly);
Fyodor Kupolova627c092015-05-05 18:44:39 -0700106 } finally {
107 if (useLock) {
108 mDexoptWakeLock.release();
109 }
110 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800111 }
112 }
113
Andreas Gampea8908752015-11-10 08:58:14 -0800114 /**
115 * Determine whether the package should be skipped for the given instruction set. A return
116 * value of true means the package will be skipped. A return value of false means that the
117 * package will be further investigated, and potentially compiled.
118 */
119 protected boolean shouldSkipBasedOnISA(PackageParser.Package pkg, String instructionSet) {
120 return pkg.mDexOptPerformed.contains(instructionSet);
121 }
122
123 /**
124 * Adjust the given dexopt-needed value. Can be overridden to influence the decision to
125 * optimize or not (and in what way).
126 */
127 protected int adjustDexoptNeeded(int dexoptNeeded) {
128 return dexoptNeeded;
129 }
130
131 /**
132 * Adjust the given dexopt flags that will be passed to the installer.
133 */
134 protected int adjustDexoptFlags(int dexoptFlags) {
135 return dexoptFlags;
136 }
137
138 /**
139 * Update the package status after a successful compilation.
140 */
141 protected void recordSuccessfulDexopt(PackageParser.Package pkg, String instructionSet) {
142 pkg.mDexOptPerformed.add(instructionSet);
143 }
144
Fyodor Kupolov74876572015-02-23 17:14:45 -0800145 private int performDexOptLI(PackageParser.Package pkg, String[] targetInstructionSets,
Andreas Gampea8908752015-11-10 08:58:14 -0800146 boolean useProfiles, boolean extractOnly) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800147 final String[] instructionSets = targetInstructionSets != null ?
148 targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo);
149
Calin Juravledb4a79a2015-12-23 18:55:08 +0200150 if (!canOptimizePackage(pkg)) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800151 return DEX_OPT_SKIPPED;
152 }
153
154 final boolean vmSafeMode = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0;
155 final boolean debuggable = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
156
157 final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly();
158 boolean performedDexOpt = false;
Fyodor Kupolov74876572015-02-23 17:14:45 -0800159 final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
160 for (String dexCodeInstructionSet : dexCodeInstructionSets) {
Andreas Gampea8908752015-11-10 08:58:14 -0800161 if (!useProfiles && shouldSkipBasedOnISA(pkg, dexCodeInstructionSet)) {
162 // Skip only if we do not use profiles since they might trigger a recompilation.
Fyodor Kupolov74876572015-02-23 17:14:45 -0800163 continue;
164 }
165
166 for (String path : paths) {
Calin Juravledb4a79a2015-12-23 18:55:08 +0200167 int dexoptNeeded;
David Brazdil493411a2016-02-01 13:48:46 +0000168
Andreas Gampea8908752015-11-10 08:58:14 -0800169 try {
170 dexoptNeeded = DexFile.getDexOptNeeded(path, pkg.packageName,
171 dexCodeInstructionSet, /* defer */false);
172 } catch (IOException ioe) {
173 Slog.w(TAG, "IOException reading apk: " + path, ioe);
174 return DEX_OPT_FAILED;
Narayan Kamath01dcb762015-05-07 17:48:42 +0100175 }
Andreas Gampea8908752015-11-10 08:58:14 -0800176 dexoptNeeded = adjustDexoptNeeded(dexoptNeeded);
Richard Uhler7b08b352015-03-25 16:25:57 -0700177
Calin Juravledb4a79a2015-12-23 18:55:08 +0200178 if (dexoptNeeded == DexFile.NO_DEXOPT_NEEDED) {
David Brazdil493411a2016-02-01 13:48:46 +0000179 // No dexopt needed and we don't use profiles. Nothing to do.
180 continue;
Calin Juravledb4a79a2015-12-23 18:55:08 +0200181 }
182 final String dexoptType;
183 String oatDir = null;
184 if (dexoptNeeded == DexFile.DEX2OAT_NEEDED) {
185 dexoptType = "dex2oat";
186 oatDir = createOatDirIfSupported(pkg, dexCodeInstructionSet);
187 } else if (dexoptNeeded == DexFile.PATCHOAT_NEEDED) {
188 dexoptType = "patchoat";
189 } else if (dexoptNeeded == DexFile.SELF_PATCHOAT_NEEDED) {
190 dexoptType = "self patchoat";
191 } else {
192 throw new IllegalStateException("Invalid dexopt needed: " + dexoptNeeded);
193 }
Narayan Kamath01dcb762015-05-07 17:48:42 +0100194
Andreas Gampea8908752015-11-10 08:58:14 -0800195
Calin Juravledb4a79a2015-12-23 18:55:08 +0200196 Log.i(TAG, "Running dexopt (" + dexoptType + ") on: " + path + " pkg="
197 + pkg.applicationInfo.packageName + " isa=" + dexCodeInstructionSet
198 + " vmSafeMode=" + vmSafeMode + " debuggable=" + debuggable
David Brazdila0e10432016-01-20 14:04:40 +0000199 + " extractOnly=" + extractOnly + " oatDir = " + oatDir);
Calin Juravledb4a79a2015-12-23 18:55:08 +0200200 final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid);
Andreas Gampea8908752015-11-10 08:58:14 -0800201 final int dexFlags = adjustDexoptFlags(
Calin Juravledb4a79a2015-12-23 18:55:08 +0200202 (!pkg.isForwardLocked() ? DEXOPT_PUBLIC : 0)
203 | (vmSafeMode ? DEXOPT_SAFEMODE : 0)
204 | (debuggable ? DEXOPT_DEBUGGABLE : 0)
David Brazdila0e10432016-01-20 14:04:40 +0000205 | (extractOnly ? DEXOPT_EXTRACTONLY : 0)
Andreas Gampea8908752015-11-10 08:58:14 -0800206 | DEXOPT_BOOTCOMPLETE);
207
Calin Juravledb4a79a2015-12-23 18:55:08 +0200208 try {
Andreas Gampea8908752015-11-10 08:58:14 -0800209 mInstaller.dexopt(path, sharedGid, pkg.packageName, dexCodeInstructionSet,
210 dexoptNeeded, oatDir, dexFlags, pkg.volumeUuid, useProfiles);
Calin Juravledb4a79a2015-12-23 18:55:08 +0200211 performedDexOpt = true;
212 } catch (InstallerException e) {
213 Slog.w(TAG, "Failed to dexopt", e);
Fyodor Kupolov74876572015-02-23 17:14:45 -0800214 }
215 }
216
David Brazdila0e10432016-01-20 14:04:40 +0000217 if (!extractOnly) {
218 // At this point we haven't failed dexopt and we haven't deferred dexopt. We must
219 // either have either succeeded dexopt, or have had getDexOptNeeded tell us
220 // it isn't required. We therefore mark that this package doesn't need dexopt unless
221 // it's forced. performedDexOpt will tell us whether we performed dex-opt or skipped
222 // it.
Andreas Gampea8908752015-11-10 08:58:14 -0800223 recordSuccessfulDexopt(pkg, dexCodeInstructionSet);
David Brazdila0e10432016-01-20 14:04:40 +0000224 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800225 }
226
227 // If we've gotten here, we're sure that no error occurred and that we haven't
228 // deferred dex-opt. We've either dex-opted one more paths or instruction sets or
229 // we've skipped all of them because they are up to date. In both cases this
230 // package doesn't need dexopt any longer.
231 return performedDexOpt ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED;
232 }
233
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800234 /**
235 * Creates oat dir for the specified package. In certain cases oat directory
236 * <strong>cannot</strong> be created:
237 * <ul>
238 * <li>{@code pkg} is a system app, which is not updated.</li>
239 * <li>Package location is not a directory, i.e. monolithic install.</li>
240 * </ul>
241 *
Richard Uhler7b08b352015-03-25 16:25:57 -0700242 * @return Absolute path to the oat directory or null, if oat directory
243 * cannot be created.
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800244 */
245 @Nullable
Todd Kennedy27c24fb2015-09-17 16:49:25 -0700246 private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) {
Fyodor Kupolovebcac162015-09-09 15:56:45 -0700247 if (!pkg.canHaveOatDir()) {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800248 return null;
249 }
250 File codePath = new File(pkg.codePath);
251 if (codePath.isDirectory()) {
252 File oatDir = getOatDir(codePath);
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700253 try {
Andreas Gampea8908752015-11-10 08:58:14 -0800254 mInstaller.createOatDir(oatDir.getAbsolutePath(), dexInstructionSet);
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700255 } catch (InstallerException e) {
256 Slog.w(TAG, "Failed to create oat dir", e);
257 return null;
258 }
Richard Uhler7b08b352015-03-25 16:25:57 -0700259 return oatDir.getAbsolutePath();
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800260 }
261 return null;
262 }
263
264 static File getOatDir(File codePath) {
265 return new File(codePath, OAT_DIR_NAME);
266 }
267
Fyodor Kupolova627c092015-05-05 18:44:39 -0700268 void systemReady() {
269 mSystemReady = true;
270 }
Andreas Gampea8908752015-11-10 08:58:14 -0800271
272 /**
273 * A specialized PackageDexOptimizer that overrides already-installed checks, forcing a
274 * dexopt path.
275 */
276 public static class ForcedUpdatePackageDexOptimizer extends PackageDexOptimizer {
277
278 public ForcedUpdatePackageDexOptimizer(Installer installer, Object installLock,
279 Context context, String wakeLockTag) {
280 super(installer, installLock, context, wakeLockTag);
281 }
282
283 public ForcedUpdatePackageDexOptimizer(PackageDexOptimizer from) {
284 super(from);
285 }
286
287 @Override
288 protected boolean shouldSkipBasedOnISA(Package pkg, String instructionSet) {
289 // Forced compilation, never skip.
290 return false;
291 }
292
293 @Override
294 protected int adjustDexoptNeeded(int dexoptNeeded) {
295 // Ensure compilation, no matter the current state.
296 // TODO: The return value is wrong when patchoat is needed.
297 return DexFile.DEX2OAT_NEEDED;
298 }
299 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800300}