blob: 6c6871fd8dfd93c13c4a3c00edb9d78f79ba6cb7 [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;
Fyodor Kupolova627c092015-05-05 18:44:39 -070023import android.os.PowerManager;
Fyodor Kupolov74876572015-02-23 17:14:45 -080024import android.os.UserHandle;
Fyodor Kupolova627c092015-05-05 18:44:39 -070025import android.os.WorkSource;
Fyodor Kupolov74876572015-02-23 17:14:45 -080026import android.util.ArraySet;
27import android.util.Log;
28import android.util.Slog;
29
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080030import java.io.File;
Fyodor Kupolov74876572015-02-23 17:14:45 -080031import java.io.FileNotFoundException;
32import java.io.IOException;
33import java.util.ArrayList;
34import java.util.List;
35
36import dalvik.system.DexFile;
Fyodor Kupolov74876572015-02-23 17:14:45 -080037
Todd Kennedyfa54ab72015-09-25 07:46:12 -070038import static com.android.server.pm.Installer.DEXOPT_BOOTCOMPLETE;
39import static com.android.server.pm.Installer.DEXOPT_DEBUGGABLE;
40import static com.android.server.pm.Installer.DEXOPT_PUBLIC;
41import static com.android.server.pm.Installer.DEXOPT_SAFEMODE;
Todd Kennedy8c6e5372015-09-25 14:47:15 -070042import static com.android.server.pm.Installer.DEXOPT_USEJIT;
Fyodor Kupolov74876572015-02-23 17:14:45 -080043import static com.android.server.pm.InstructionSets.getAppDexInstructionSets;
44import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets;
45
46/**
47 * Helper class for running dexopt command on packages.
48 */
49final class PackageDexOptimizer {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080050 private static final String TAG = "PackageManager.DexOptimizer";
51 static final String OAT_DIR_NAME = "oat";
52 // TODO b/19550105 Remove error codes and use exceptions
Fyodor Kupolov74876572015-02-23 17:14:45 -080053 static final int DEX_OPT_SKIPPED = 0;
54 static final int DEX_OPT_PERFORMED = 1;
55 static final int DEX_OPT_DEFERRED = 2;
56 static final int DEX_OPT_FAILED = -1;
57
58 private final PackageManagerService mPackageManagerService;
59 private ArraySet<PackageParser.Package> mDeferredDexOpt;
60
Fyodor Kupolova627c092015-05-05 18:44:39 -070061 private final PowerManager.WakeLock mDexoptWakeLock;
62 private volatile boolean mSystemReady;
63
Fyodor Kupolov74876572015-02-23 17:14:45 -080064 PackageDexOptimizer(PackageManagerService packageManagerService) {
65 this.mPackageManagerService = packageManagerService;
Fyodor Kupolova627c092015-05-05 18:44:39 -070066 PowerManager powerManager = (PowerManager)packageManagerService.mContext.getSystemService(
67 Context.POWER_SERVICE);
68 mDexoptWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*dexopt*");
Fyodor Kupolov74876572015-02-23 17:14:45 -080069 }
70
71 /**
72 * Performs dexopt on all code paths and libraries of the specified package for specified
73 * instruction sets.
74 *
75 * <p>Calls to {@link com.android.server.pm.Installer#dexopt} are synchronized on
76 * {@link PackageManagerService#mInstallLock}.
77 */
78 int performDexOpt(PackageParser.Package pkg, String[] instructionSets,
Todd Kennedy8c6e5372015-09-25 14:47:15 -070079 boolean forceDex, boolean defer, boolean inclDependencies,
80 boolean bootComplete, boolean useJit) {
Fyodor Kupolov74876572015-02-23 17:14:45 -080081 ArraySet<String> done;
82 if (inclDependencies && (pkg.usesLibraries != null || pkg.usesOptionalLibraries != null)) {
83 done = new ArraySet<String>();
84 done.add(pkg.packageName);
85 } else {
86 done = null;
87 }
88 synchronized (mPackageManagerService.mInstallLock) {
Fyodor Kupolova627c092015-05-05 18:44:39 -070089 final boolean useLock = mSystemReady;
90 if (useLock) {
91 mDexoptWakeLock.setWorkSource(new WorkSource(pkg.applicationInfo.uid));
92 mDexoptWakeLock.acquire();
93 }
94 try {
Todd Kennedy92b28f32015-09-28 11:20:38 -070095 return performDexOptLI(pkg, instructionSets, forceDex, defer, bootComplete,
96 useJit, done);
Fyodor Kupolova627c092015-05-05 18:44:39 -070097 } finally {
98 if (useLock) {
99 mDexoptWakeLock.release();
100 }
101 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800102 }
103 }
104
105 private int performDexOptLI(PackageParser.Package pkg, String[] targetInstructionSets,
Todd Kennedy8c6e5372015-09-25 14:47:15 -0700106 boolean forceDex, boolean defer, boolean bootComplete, boolean useJit,
107 ArraySet<String> done) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800108 final String[] instructionSets = targetInstructionSets != null ?
109 targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo);
110
111 if (done != null) {
112 done.add(pkg.packageName);
113 if (pkg.usesLibraries != null) {
Andreas Gampe06bb9082015-09-21 13:20:07 -0700114 performDexOptLibsLI(pkg.usesLibraries, instructionSets, forceDex, defer,
Todd Kennedy8c6e5372015-09-25 14:47:15 -0700115 bootComplete, useJit, done);
Fyodor Kupolov74876572015-02-23 17:14:45 -0800116 }
117 if (pkg.usesOptionalLibraries != null) {
118 performDexOptLibsLI(pkg.usesOptionalLibraries, instructionSets, forceDex, defer,
Todd Kennedy8c6e5372015-09-25 14:47:15 -0700119 bootComplete, useJit, done);
Fyodor Kupolov74876572015-02-23 17:14:45 -0800120 }
121 }
122
123 if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) == 0) {
124 return DEX_OPT_SKIPPED;
125 }
126
127 final boolean vmSafeMode = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0;
128 final boolean debuggable = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
129
130 final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly();
131 boolean performedDexOpt = false;
132 // There are three basic cases here:
133 // 1.) we need to dexopt, either because we are forced or it is needed
134 // 2.) we are deferring a needed dexopt
135 // 3.) we are skipping an unneeded dexopt
136 final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
137 for (String dexCodeInstructionSet : dexCodeInstructionSets) {
138 if (!forceDex && pkg.mDexOptPerformed.contains(dexCodeInstructionSet)) {
139 continue;
140 }
141
142 for (String path : paths) {
Narayan Kamath01dcb762015-05-07 17:48:42 +0100143 final int dexoptNeeded;
144 if (forceDex) {
145 dexoptNeeded = DexFile.DEX2OAT_NEEDED;
146 } else {
147 try {
148 dexoptNeeded = DexFile.getDexOptNeeded(path, pkg.packageName,
149 dexCodeInstructionSet, defer);
150 } catch (IOException ioe) {
151 Slog.w(TAG, "IOException reading apk: " + path, ioe);
152 return DEX_OPT_FAILED;
Richard Uhler7b08b352015-03-25 16:25:57 -0700153 }
Narayan Kamath01dcb762015-05-07 17:48:42 +0100154 }
Richard Uhler7b08b352015-03-25 16:25:57 -0700155
Narayan Kamath01dcb762015-05-07 17:48:42 +0100156 if (!forceDex && defer && dexoptNeeded != DexFile.NO_DEXOPT_NEEDED) {
157 // We're deciding to defer a needed dexopt. Don't bother dexopting for other
158 // paths and instruction sets. We'll deal with them all together when we process
159 // our list of deferred dexopts.
160 addPackageForDeferredDexopt(pkg);
161 return DEX_OPT_DEFERRED;
162 }
Richard Uhler7b08b352015-03-25 16:25:57 -0700163
Narayan Kamath01dcb762015-05-07 17:48:42 +0100164 if (dexoptNeeded != DexFile.NO_DEXOPT_NEEDED) {
165 final String dexoptType;
166 String oatDir = null;
167 if (dexoptNeeded == DexFile.DEX2OAT_NEEDED) {
168 dexoptType = "dex2oat";
169 try {
Richard Uhler7b08b352015-03-25 16:25:57 -0700170 oatDir = createOatDirIfSupported(pkg, dexCodeInstructionSet);
Narayan Kamath01dcb762015-05-07 17:48:42 +0100171 } catch (IOException ioe) {
172 Slog.w(TAG, "Unable to create oatDir for package: " + pkg.packageName);
Fyodor Kupolov74876572015-02-23 17:14:45 -0800173 return DEX_OPT_FAILED;
174 }
Narayan Kamath01dcb762015-05-07 17:48:42 +0100175 } else if (dexoptNeeded == DexFile.PATCHOAT_NEEDED) {
176 dexoptType = "patchoat";
177 } else if (dexoptNeeded == DexFile.SELF_PATCHOAT_NEEDED) {
178 dexoptType = "self patchoat";
179 } else {
180 throw new IllegalStateException("Invalid dexopt needed: " + dexoptNeeded);
181 }
182
183 Log.i(TAG, "Running dexopt (" + dexoptType + ") on: " + path + " pkg="
184 + pkg.applicationInfo.packageName + " isa=" + dexCodeInstructionSet
185 + " vmSafeMode=" + vmSafeMode + " debuggable=" + debuggable
Todd Kennedy92b28f32015-09-28 11:20:38 -0700186 + " oatDir = " + oatDir + " bootComplete=" + bootComplete
187 + " useJit=" + useJit);
Narayan Kamath01dcb762015-05-07 17:48:42 +0100188 final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid);
Todd Kennedy9f860262015-09-25 15:06:49 -0700189 final int dexFlags =
190 (!pkg.isForwardLocked() ? DEXOPT_PUBLIC : 0)
191 | (vmSafeMode ? DEXOPT_SAFEMODE : 0)
192 | (debuggable ? DEXOPT_DEBUGGABLE : 0)
Todd Kennedy92b28f32015-09-28 11:20:38 -0700193 | (bootComplete ? DEXOPT_BOOTCOMPLETE : 0)
194 | (useJit ? DEXOPT_USEJIT : 0);
Narayan Kamath01dcb762015-05-07 17:48:42 +0100195 final int ret = mPackageManagerService.mInstaller.dexopt(path, sharedGid,
Todd Kennedy9f860262015-09-25 15:06:49 -0700196 pkg.packageName, dexCodeInstructionSet, dexoptNeeded, oatDir, dexFlags);
Narayan Kamath01dcb762015-05-07 17:48:42 +0100197
198 // Dex2oat might fail due to compiler / verifier errors. We soldier on
199 // regardless, and attempt to interpret the app as a safety net.
200 if (ret == 0) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800201 performedDexOpt = true;
202 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800203 }
204 }
205
206 // At this point we haven't failed dexopt and we haven't deferred dexopt. We must
Richard Uhler7b08b352015-03-25 16:25:57 -0700207 // either have either succeeded dexopt, or have had getDexOptNeeded tell us
Fyodor Kupolov74876572015-02-23 17:14:45 -0800208 // it isn't required. We therefore mark that this package doesn't need dexopt unless
209 // it's forced. performedDexOpt will tell us whether we performed dex-opt or skipped
210 // it.
211 pkg.mDexOptPerformed.add(dexCodeInstructionSet);
212 }
213
214 // If we've gotten here, we're sure that no error occurred and that we haven't
215 // deferred dex-opt. We've either dex-opted one more paths or instruction sets or
216 // we've skipped all of them because they are up to date. In both cases this
217 // package doesn't need dexopt any longer.
218 return performedDexOpt ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED;
219 }
220
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800221 /**
222 * Creates oat dir for the specified package. In certain cases oat directory
223 * <strong>cannot</strong> be created:
224 * <ul>
225 * <li>{@code pkg} is a system app, which is not updated.</li>
226 * <li>Package location is not a directory, i.e. monolithic install.</li>
227 * </ul>
228 *
Richard Uhler7b08b352015-03-25 16:25:57 -0700229 * @return Absolute path to the oat directory or null, if oat directory
230 * cannot be created.
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800231 */
232 @Nullable
Richard Uhler7b08b352015-03-25 16:25:57 -0700233 private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet)
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800234 throws IOException {
Fyodor Kupolovebcac162015-09-09 15:56:45 -0700235 if (!pkg.canHaveOatDir()) {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800236 return null;
237 }
238 File codePath = new File(pkg.codePath);
239 if (codePath.isDirectory()) {
240 File oatDir = getOatDir(codePath);
241 mPackageManagerService.mInstaller.createOatDir(oatDir.getAbsolutePath(),
242 dexInstructionSet);
Richard Uhler7b08b352015-03-25 16:25:57 -0700243 return oatDir.getAbsolutePath();
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800244 }
245 return null;
246 }
247
248 static File getOatDir(File codePath) {
249 return new File(codePath, OAT_DIR_NAME);
250 }
251
Fyodor Kupolov74876572015-02-23 17:14:45 -0800252 private void performDexOptLibsLI(ArrayList<String> libs, String[] instructionSets,
Todd Kennedy8c6e5372015-09-25 14:47:15 -0700253 boolean forceDex, boolean defer, boolean bootComplete, boolean useJit,
254 ArraySet<String> done) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800255 for (String libName : libs) {
256 PackageParser.Package libPkg = mPackageManagerService.findSharedNonSystemLibrary(
257 libName);
258 if (libPkg != null && !done.contains(libName)) {
Todd Kennedy8c6e5372015-09-25 14:47:15 -0700259 performDexOptLI(libPkg, instructionSets, forceDex, defer, bootComplete, useJit, done);
Fyodor Kupolov74876572015-02-23 17:14:45 -0800260 }
261 }
262 }
263
264 /**
265 * Clears set of deferred dexopt packages.
266 * @return content of dexopt set if it was not empty
267 */
268 public ArraySet<PackageParser.Package> clearDeferredDexOptPackages() {
269 ArraySet<PackageParser.Package> result = mDeferredDexOpt;
270 mDeferredDexOpt = null;
271 return result;
272 }
273
274 public void addPackageForDeferredDexopt(PackageParser.Package pkg) {
275 if (mDeferredDexOpt == null) {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800276 mDeferredDexOpt = new ArraySet<>();
Fyodor Kupolov74876572015-02-23 17:14:45 -0800277 }
278 mDeferredDexOpt.add(pkg);
279 }
Fyodor Kupolova627c092015-05-05 18:44:39 -0700280
281 void systemReady() {
282 mSystemReady = true;
283 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800284}