blob: d29a6231c2f5eabe5585021bc418b29ba4bc0420 [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.IOException;
32import java.util.ArrayList;
33import java.util.List;
34
35import dalvik.system.DexFile;
Fyodor Kupolov74876572015-02-23 17:14:45 -080036
Todd Kennedyfa54ab72015-09-25 07:46:12 -070037import static com.android.server.pm.Installer.DEXOPT_BOOTCOMPLETE;
38import static com.android.server.pm.Installer.DEXOPT_DEBUGGABLE;
39import static com.android.server.pm.Installer.DEXOPT_PUBLIC;
40import static com.android.server.pm.Installer.DEXOPT_SAFEMODE;
Todd Kennedy8c6e5372015-09-25 14:47:15 -070041import static com.android.server.pm.Installer.DEXOPT_USEJIT;
Fyodor Kupolov74876572015-02-23 17:14:45 -080042import static com.android.server.pm.InstructionSets.getAppDexInstructionSets;
43import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets;
44
45/**
46 * Helper class for running dexopt command on packages.
47 */
48final class PackageDexOptimizer {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080049 private static final String TAG = "PackageManager.DexOptimizer";
50 static final String OAT_DIR_NAME = "oat";
51 // TODO b/19550105 Remove error codes and use exceptions
Fyodor Kupolov74876572015-02-23 17:14:45 -080052 static final int DEX_OPT_SKIPPED = 0;
53 static final int DEX_OPT_PERFORMED = 1;
54 static final int DEX_OPT_DEFERRED = 2;
55 static final int DEX_OPT_FAILED = -1;
56
57 private final PackageManagerService mPackageManagerService;
Fyodor Kupolov74876572015-02-23 17:14:45 -080058
Fyodor Kupolova627c092015-05-05 18:44:39 -070059 private final PowerManager.WakeLock mDexoptWakeLock;
60 private volatile boolean mSystemReady;
61
Fyodor Kupolov74876572015-02-23 17:14:45 -080062 PackageDexOptimizer(PackageManagerService packageManagerService) {
63 this.mPackageManagerService = packageManagerService;
Fyodor Kupolova627c092015-05-05 18:44:39 -070064 PowerManager powerManager = (PowerManager)packageManagerService.mContext.getSystemService(
65 Context.POWER_SERVICE);
66 mDexoptWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*dexopt*");
Fyodor Kupolov74876572015-02-23 17:14:45 -080067 }
68
69 /**
70 * Performs dexopt on all code paths and libraries of the specified package for specified
71 * instruction sets.
72 *
73 * <p>Calls to {@link com.android.server.pm.Installer#dexopt} are synchronized on
74 * {@link PackageManagerService#mInstallLock}.
75 */
76 int performDexOpt(PackageParser.Package pkg, String[] instructionSets,
Nicolas Geoffray27c07372015-11-05 16:54:09 +000077 boolean inclDependencies) {
Fyodor Kupolov74876572015-02-23 17:14:45 -080078 ArraySet<String> done;
79 if (inclDependencies && (pkg.usesLibraries != null || pkg.usesOptionalLibraries != null)) {
80 done = new ArraySet<String>();
81 done.add(pkg.packageName);
82 } else {
83 done = null;
84 }
85 synchronized (mPackageManagerService.mInstallLock) {
Fyodor Kupolova627c092015-05-05 18:44:39 -070086 final boolean useLock = mSystemReady;
87 if (useLock) {
88 mDexoptWakeLock.setWorkSource(new WorkSource(pkg.applicationInfo.uid));
89 mDexoptWakeLock.acquire();
90 }
91 try {
Nicolas Geoffray27c07372015-11-05 16:54:09 +000092 return performDexOptLI(pkg, instructionSets, done);
Fyodor Kupolova627c092015-05-05 18:44:39 -070093 } finally {
94 if (useLock) {
95 mDexoptWakeLock.release();
96 }
97 }
Fyodor Kupolov74876572015-02-23 17:14:45 -080098 }
99 }
100
101 private int performDexOptLI(PackageParser.Package pkg, String[] targetInstructionSets,
Todd Kennedy8c6e5372015-09-25 14:47:15 -0700102 ArraySet<String> done) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800103 final String[] instructionSets = targetInstructionSets != null ?
104 targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo);
105
106 if (done != null) {
107 done.add(pkg.packageName);
108 if (pkg.usesLibraries != null) {
Nicolas Geoffray27c07372015-11-05 16:54:09 +0000109 performDexOptLibsLI(pkg.usesLibraries, instructionSets, done);
Fyodor Kupolov74876572015-02-23 17:14:45 -0800110 }
111 if (pkg.usesOptionalLibraries != null) {
Nicolas Geoffray27c07372015-11-05 16:54:09 +0000112 performDexOptLibsLI(pkg.usesOptionalLibraries, instructionSets, done);
Fyodor Kupolov74876572015-02-23 17:14:45 -0800113 }
114 }
115
116 if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) == 0) {
117 return DEX_OPT_SKIPPED;
118 }
119
120 final boolean vmSafeMode = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0;
121 final boolean debuggable = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
122
123 final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly();
124 boolean performedDexOpt = false;
125 // There are three basic cases here:
126 // 1.) we need to dexopt, either because we are forced or it is needed
127 // 2.) we are deferring a needed dexopt
128 // 3.) we are skipping an unneeded dexopt
129 final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
130 for (String dexCodeInstructionSet : dexCodeInstructionSets) {
Nicolas Geoffray27c07372015-11-05 16:54:09 +0000131 if (pkg.mDexOptPerformed.contains(dexCodeInstructionSet)) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800132 continue;
133 }
134
135 for (String path : paths) {
Narayan Kamath01dcb762015-05-07 17:48:42 +0100136 final int dexoptNeeded;
Nicolas Geoffray27c07372015-11-05 16:54:09 +0000137 try {
138 dexoptNeeded = DexFile.getDexOptNeeded(path, pkg.packageName,
139 dexCodeInstructionSet, /* defer */false);
140 } catch (IOException ioe) {
141 Slog.w(TAG, "IOException reading apk: " + path, ioe);
142 return DEX_OPT_FAILED;
Narayan Kamath01dcb762015-05-07 17:48:42 +0100143 }
Richard Uhler7b08b352015-03-25 16:25:57 -0700144
Narayan Kamath01dcb762015-05-07 17:48:42 +0100145 if (dexoptNeeded != DexFile.NO_DEXOPT_NEEDED) {
146 final String dexoptType;
147 String oatDir = null;
148 if (dexoptNeeded == DexFile.DEX2OAT_NEEDED) {
149 dexoptType = "dex2oat";
Todd Kennedy27c24fb2015-09-17 16:49:25 -0700150 oatDir = createOatDirIfSupported(pkg, dexCodeInstructionSet);
Narayan Kamath01dcb762015-05-07 17:48:42 +0100151 } else if (dexoptNeeded == DexFile.PATCHOAT_NEEDED) {
152 dexoptType = "patchoat";
153 } else if (dexoptNeeded == DexFile.SELF_PATCHOAT_NEEDED) {
154 dexoptType = "self patchoat";
155 } else {
156 throw new IllegalStateException("Invalid dexopt needed: " + dexoptNeeded);
157 }
158
159 Log.i(TAG, "Running dexopt (" + dexoptType + ") on: " + path + " pkg="
160 + pkg.applicationInfo.packageName + " isa=" + dexCodeInstructionSet
161 + " vmSafeMode=" + vmSafeMode + " debuggable=" + debuggable
Nicolas Geoffray27c07372015-11-05 16:54:09 +0000162 + " oatDir = " + oatDir);
Narayan Kamath01dcb762015-05-07 17:48:42 +0100163 final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid);
Todd Kennedy9f860262015-09-25 15:06:49 -0700164 final int dexFlags =
165 (!pkg.isForwardLocked() ? DEXOPT_PUBLIC : 0)
166 | (vmSafeMode ? DEXOPT_SAFEMODE : 0)
167 | (debuggable ? DEXOPT_DEBUGGABLE : 0)
Nicolas Geoffray27c07372015-11-05 16:54:09 +0000168 | DEXOPT_BOOTCOMPLETE;
Narayan Kamath01dcb762015-05-07 17:48:42 +0100169 final int ret = mPackageManagerService.mInstaller.dexopt(path, sharedGid,
Todd Kennedy9f860262015-09-25 15:06:49 -0700170 pkg.packageName, dexCodeInstructionSet, dexoptNeeded, oatDir, dexFlags);
Narayan Kamath01dcb762015-05-07 17:48:42 +0100171
Nicolas Geoffray27c07372015-11-05 16:54:09 +0000172 // Dex2oat might fail due to compiler / verifier errors.
Narayan Kamath01dcb762015-05-07 17:48:42 +0100173 if (ret == 0) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800174 performedDexOpt = true;
175 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800176 }
177 }
178
179 // At this point we haven't failed dexopt and we haven't deferred dexopt. We must
Richard Uhler7b08b352015-03-25 16:25:57 -0700180 // either have either succeeded dexopt, or have had getDexOptNeeded tell us
Fyodor Kupolov74876572015-02-23 17:14:45 -0800181 // it isn't required. We therefore mark that this package doesn't need dexopt unless
182 // it's forced. performedDexOpt will tell us whether we performed dex-opt or skipped
183 // it.
184 pkg.mDexOptPerformed.add(dexCodeInstructionSet);
185 }
186
187 // If we've gotten here, we're sure that no error occurred and that we haven't
188 // deferred dex-opt. We've either dex-opted one more paths or instruction sets or
189 // we've skipped all of them because they are up to date. In both cases this
190 // package doesn't need dexopt any longer.
191 return performedDexOpt ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED;
192 }
193
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800194 /**
195 * Creates oat dir for the specified package. In certain cases oat directory
196 * <strong>cannot</strong> be created:
197 * <ul>
198 * <li>{@code pkg} is a system app, which is not updated.</li>
199 * <li>Package location is not a directory, i.e. monolithic install.</li>
200 * </ul>
201 *
Richard Uhler7b08b352015-03-25 16:25:57 -0700202 * @return Absolute path to the oat directory or null, if oat directory
203 * cannot be created.
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800204 */
205 @Nullable
Todd Kennedy27c24fb2015-09-17 16:49:25 -0700206 private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) {
Fyodor Kupolovebcac162015-09-09 15:56:45 -0700207 if (!pkg.canHaveOatDir()) {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800208 return null;
209 }
210 File codePath = new File(pkg.codePath);
211 if (codePath.isDirectory()) {
212 File oatDir = getOatDir(codePath);
213 mPackageManagerService.mInstaller.createOatDir(oatDir.getAbsolutePath(),
214 dexInstructionSet);
Richard Uhler7b08b352015-03-25 16:25:57 -0700215 return oatDir.getAbsolutePath();
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800216 }
217 return null;
218 }
219
220 static File getOatDir(File codePath) {
221 return new File(codePath, OAT_DIR_NAME);
222 }
223
Fyodor Kupolov74876572015-02-23 17:14:45 -0800224 private void performDexOptLibsLI(ArrayList<String> libs, String[] instructionSets,
Todd Kennedy8c6e5372015-09-25 14:47:15 -0700225 ArraySet<String> done) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800226 for (String libName : libs) {
227 PackageParser.Package libPkg = mPackageManagerService.findSharedNonSystemLibrary(
228 libName);
229 if (libPkg != null && !done.contains(libName)) {
Nicolas Geoffray27c07372015-11-05 16:54:09 +0000230 performDexOptLI(libPkg, instructionSets, done);
Fyodor Kupolov74876572015-02-23 17:14:45 -0800231 }
232 }
233 }
234
Fyodor Kupolova627c092015-05-05 18:44:39 -0700235 void systemReady() {
236 mSystemReady = true;
237 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800238}