blob: 4d66e104e45a57eaa0b61f254b14779b2c2ac9a7 [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;
Calin Juravledb4a79a2015-12-23 18:55:08 +020026import android.os.storage.StorageManager;
Fyodor Kupolov74876572015-02-23 17:14:45 -080027import android.util.ArraySet;
28import android.util.Log;
29import android.util.Slog;
30
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -070031import com.android.internal.os.InstallerConnection.InstallerException;
32
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080033import java.io.File;
Fyodor Kupolov74876572015-02-23 17:14:45 -080034import java.io.IOException;
35import java.util.ArrayList;
36import java.util.List;
37
38import dalvik.system.DexFile;
Fyodor Kupolov74876572015-02-23 17:14:45 -080039
Todd Kennedyfa54ab72015-09-25 07:46:12 -070040import static com.android.server.pm.Installer.DEXOPT_BOOTCOMPLETE;
41import static com.android.server.pm.Installer.DEXOPT_DEBUGGABLE;
42import static com.android.server.pm.Installer.DEXOPT_PUBLIC;
43import static com.android.server.pm.Installer.DEXOPT_SAFEMODE;
David Brazdila0e10432016-01-20 14:04:40 +000044import static com.android.server.pm.Installer.DEXOPT_EXTRACTONLY;
Fyodor Kupolov74876572015-02-23 17:14:45 -080045import static com.android.server.pm.InstructionSets.getAppDexInstructionSets;
46import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets;
47
48/**
49 * Helper class for running dexopt command on packages.
50 */
51final class PackageDexOptimizer {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080052 private static final String TAG = "PackageManager.DexOptimizer";
53 static final String OAT_DIR_NAME = "oat";
54 // TODO b/19550105 Remove error codes and use exceptions
Fyodor Kupolov74876572015-02-23 17:14:45 -080055 static final int DEX_OPT_SKIPPED = 0;
56 static final int DEX_OPT_PERFORMED = 1;
57 static final int DEX_OPT_DEFERRED = 2;
58 static final int DEX_OPT_FAILED = -1;
59
60 private final PackageManagerService mPackageManagerService;
Fyodor Kupolov74876572015-02-23 17:14:45 -080061
Fyodor Kupolova627c092015-05-05 18:44:39 -070062 private final PowerManager.WakeLock mDexoptWakeLock;
63 private volatile boolean mSystemReady;
64
Fyodor Kupolov74876572015-02-23 17:14:45 -080065 PackageDexOptimizer(PackageManagerService packageManagerService) {
66 this.mPackageManagerService = packageManagerService;
Fyodor Kupolova627c092015-05-05 18:44:39 -070067 PowerManager powerManager = (PowerManager)packageManagerService.mContext.getSystemService(
68 Context.POWER_SERVICE);
69 mDexoptWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*dexopt*");
Fyodor Kupolov74876572015-02-23 17:14:45 -080070 }
71
Calin Juravledb4a79a2015-12-23 18:55:08 +020072 static boolean canOptimizePackage(PackageParser.Package pkg) {
Calin Juravle6dfd83d2016-01-29 18:23:57 +000073 return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0;
Calin Juravledb4a79a2015-12-23 18:55:08 +020074 }
75
Fyodor Kupolov74876572015-02-23 17:14:45 -080076 /**
77 * Performs dexopt on all code paths and libraries of the specified package for specified
78 * instruction sets.
79 *
80 * <p>Calls to {@link com.android.server.pm.Installer#dexopt} are synchronized on
81 * {@link PackageManagerService#mInstallLock}.
82 */
83 int performDexOpt(PackageParser.Package pkg, String[] instructionSets,
Calin Juravle9dff8542016-01-21 11:41:42 -080084 boolean inclDependencies, boolean useProfiles, boolean extractOnly) {
Fyodor Kupolov74876572015-02-23 17:14:45 -080085 ArraySet<String> done;
86 if (inclDependencies && (pkg.usesLibraries != null || pkg.usesOptionalLibraries != null)) {
87 done = new ArraySet<String>();
88 done.add(pkg.packageName);
89 } else {
90 done = null;
91 }
92 synchronized (mPackageManagerService.mInstallLock) {
Fyodor Kupolova627c092015-05-05 18:44:39 -070093 final boolean useLock = mSystemReady;
94 if (useLock) {
95 mDexoptWakeLock.setWorkSource(new WorkSource(pkg.applicationInfo.uid));
96 mDexoptWakeLock.acquire();
97 }
98 try {
Calin Juravle9dff8542016-01-21 11:41:42 -080099 return performDexOptLI(pkg, instructionSets, done, useProfiles,
David Brazdila0e10432016-01-20 14:04:40 +0000100 extractOnly);
Fyodor Kupolova627c092015-05-05 18:44:39 -0700101 } finally {
102 if (useLock) {
103 mDexoptWakeLock.release();
104 }
105 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800106 }
107 }
108
109 private int performDexOptLI(PackageParser.Package pkg, String[] targetInstructionSets,
Calin Juravle9dff8542016-01-21 11:41:42 -0800110 ArraySet<String> done, boolean useProfiles, boolean extractOnly) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800111 final String[] instructionSets = targetInstructionSets != null ?
112 targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo);
113
114 if (done != null) {
115 done.add(pkg.packageName);
116 if (pkg.usesLibraries != null) {
Nicolas Geoffray27c07372015-11-05 16:54:09 +0000117 performDexOptLibsLI(pkg.usesLibraries, instructionSets, done);
Fyodor Kupolov74876572015-02-23 17:14:45 -0800118 }
119 if (pkg.usesOptionalLibraries != null) {
Nicolas Geoffray27c07372015-11-05 16:54:09 +0000120 performDexOptLibsLI(pkg.usesOptionalLibraries, instructionSets, done);
Fyodor Kupolov74876572015-02-23 17:14:45 -0800121 }
122 }
123
Calin Juravledb4a79a2015-12-23 18:55:08 +0200124 if (!canOptimizePackage(pkg)) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800125 return DEX_OPT_SKIPPED;
126 }
127
128 final boolean vmSafeMode = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0;
129 final boolean debuggable = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
130
131 final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly();
132 boolean performedDexOpt = false;
Fyodor Kupolov74876572015-02-23 17:14:45 -0800133 final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
134 for (String dexCodeInstructionSet : dexCodeInstructionSets) {
Calin Juravledb4a79a2015-12-23 18:55:08 +0200135 if (!useProfiles && pkg.mDexOptPerformed.contains(dexCodeInstructionSet)) {
136 // Skip only if we do not use profiles since they might trigger a recompilation.
Fyodor Kupolov74876572015-02-23 17:14:45 -0800137 continue;
138 }
139
140 for (String path : paths) {
Calin Juravledb4a79a2015-12-23 18:55:08 +0200141 int dexoptNeeded;
Nicolas Geoffray27c07372015-11-05 16:54:09 +0000142 try {
143 dexoptNeeded = DexFile.getDexOptNeeded(path, pkg.packageName,
144 dexCodeInstructionSet, /* defer */false);
145 } catch (IOException ioe) {
146 Slog.w(TAG, "IOException reading apk: " + path, ioe);
147 return DEX_OPT_FAILED;
Narayan Kamath01dcb762015-05-07 17:48:42 +0100148 }
Richard Uhler7b08b352015-03-25 16:25:57 -0700149
Calin Juravledb4a79a2015-12-23 18:55:08 +0200150 if (dexoptNeeded == DexFile.NO_DEXOPT_NEEDED) {
151 if (useProfiles) {
152 // If we do a profile guided compilation then we might recompile the same
153 // package if more profile information is available.
154 dexoptNeeded = DexFile.DEX2OAT_NEEDED;
Narayan Kamath01dcb762015-05-07 17:48:42 +0100155 } else {
Calin Juravledb4a79a2015-12-23 18:55:08 +0200156 // No dexopt needed and we don't use profiles. Nothing to do.
157 continue;
Narayan Kamath01dcb762015-05-07 17:48:42 +0100158 }
Calin Juravledb4a79a2015-12-23 18:55:08 +0200159 }
160 final String dexoptType;
161 String oatDir = null;
162 if (dexoptNeeded == DexFile.DEX2OAT_NEEDED) {
163 dexoptType = "dex2oat";
164 oatDir = createOatDirIfSupported(pkg, dexCodeInstructionSet);
165 } else if (dexoptNeeded == DexFile.PATCHOAT_NEEDED) {
166 dexoptType = "patchoat";
167 } else if (dexoptNeeded == DexFile.SELF_PATCHOAT_NEEDED) {
168 dexoptType = "self patchoat";
169 } else {
170 throw new IllegalStateException("Invalid dexopt needed: " + dexoptNeeded);
171 }
Narayan Kamath01dcb762015-05-07 17:48:42 +0100172
Calin Juravledb4a79a2015-12-23 18:55:08 +0200173 Log.i(TAG, "Running dexopt (" + dexoptType + ") on: " + path + " pkg="
174 + pkg.applicationInfo.packageName + " isa=" + dexCodeInstructionSet
175 + " vmSafeMode=" + vmSafeMode + " debuggable=" + debuggable
David Brazdila0e10432016-01-20 14:04:40 +0000176 + " extractOnly=" + extractOnly + " oatDir = " + oatDir);
Calin Juravledb4a79a2015-12-23 18:55:08 +0200177 final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid);
178 final int dexFlags =
179 (!pkg.isForwardLocked() ? DEXOPT_PUBLIC : 0)
180 | (vmSafeMode ? DEXOPT_SAFEMODE : 0)
181 | (debuggable ? DEXOPT_DEBUGGABLE : 0)
David Brazdila0e10432016-01-20 14:04:40 +0000182 | (extractOnly ? DEXOPT_EXTRACTONLY : 0)
Calin Juravledb4a79a2015-12-23 18:55:08 +0200183 | DEXOPT_BOOTCOMPLETE;
184 try {
185 mPackageManagerService.mInstaller.dexopt(path, sharedGid,
186 pkg.packageName, dexCodeInstructionSet, dexoptNeeded, oatDir,
Calin Juravle9dff8542016-01-21 11:41:42 -0800187 dexFlags, pkg.volumeUuid, useProfiles);
Calin Juravledb4a79a2015-12-23 18:55:08 +0200188 performedDexOpt = true;
189 } catch (InstallerException e) {
190 Slog.w(TAG, "Failed to dexopt", e);
Fyodor Kupolov74876572015-02-23 17:14:45 -0800191 }
192 }
193
David Brazdila0e10432016-01-20 14:04:40 +0000194 if (!extractOnly) {
195 // At this point we haven't failed dexopt and we haven't deferred dexopt. We must
196 // either have either succeeded dexopt, or have had getDexOptNeeded tell us
197 // it isn't required. We therefore mark that this package doesn't need dexopt unless
198 // it's forced. performedDexOpt will tell us whether we performed dex-opt or skipped
199 // it.
200 pkg.mDexOptPerformed.add(dexCodeInstructionSet);
201 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800202 }
203
204 // If we've gotten here, we're sure that no error occurred and that we haven't
205 // deferred dex-opt. We've either dex-opted one more paths or instruction sets or
206 // we've skipped all of them because they are up to date. In both cases this
207 // package doesn't need dexopt any longer.
208 return performedDexOpt ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED;
209 }
210
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800211 /**
212 * Creates oat dir for the specified package. In certain cases oat directory
213 * <strong>cannot</strong> be created:
214 * <ul>
215 * <li>{@code pkg} is a system app, which is not updated.</li>
216 * <li>Package location is not a directory, i.e. monolithic install.</li>
217 * </ul>
218 *
Richard Uhler7b08b352015-03-25 16:25:57 -0700219 * @return Absolute path to the oat directory or null, if oat directory
220 * cannot be created.
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800221 */
222 @Nullable
Todd Kennedy27c24fb2015-09-17 16:49:25 -0700223 private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) {
Fyodor Kupolovebcac162015-09-09 15:56:45 -0700224 if (!pkg.canHaveOatDir()) {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800225 return null;
226 }
227 File codePath = new File(pkg.codePath);
228 if (codePath.isDirectory()) {
229 File oatDir = getOatDir(codePath);
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700230 try {
231 mPackageManagerService.mInstaller.createOatDir(oatDir.getAbsolutePath(),
232 dexInstructionSet);
233 } catch (InstallerException e) {
234 Slog.w(TAG, "Failed to create oat dir", e);
235 return null;
236 }
Richard Uhler7b08b352015-03-25 16:25:57 -0700237 return oatDir.getAbsolutePath();
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800238 }
239 return null;
240 }
241
242 static File getOatDir(File codePath) {
243 return new File(codePath, OAT_DIR_NAME);
244 }
245
Fyodor Kupolov74876572015-02-23 17:14:45 -0800246 private void performDexOptLibsLI(ArrayList<String> libs, String[] instructionSets,
Todd Kennedy8c6e5372015-09-25 14:47:15 -0700247 ArraySet<String> done) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800248 for (String libName : libs) {
249 PackageParser.Package libPkg = mPackageManagerService.findSharedNonSystemLibrary(
250 libName);
251 if (libPkg != null && !done.contains(libName)) {
Calin Juravledb4a79a2015-12-23 18:55:08 +0200252 // TODO: Analyze and investigate if we (should) profile libraries.
253 // Currently this will do a full compilation of the library.
Calin Juravle9dff8542016-01-21 11:41:42 -0800254 performDexOptLI(libPkg, instructionSets, done, /*useProfiles*/ false,
David Brazdila0e10432016-01-20 14:04:40 +0000255 /* extractOnly */ false);
Fyodor Kupolov74876572015-02-23 17:14:45 -0800256 }
257 }
258 }
259
Fyodor Kupolova627c092015-05-05 18:44:39 -0700260 void systemReady() {
261 mSystemReady = true;
262 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800263}