blob: fe0f1416371db9220df8ee17518820bcc5599c66 [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,
David Brazdil493411a2016-02-01 13:48:46 +000084 boolean inclDependencies, boolean useProfiles, boolean extractOnly, boolean force) {
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 Brazdil493411a2016-02-01 13:48:46 +0000100 extractOnly, force);
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,
David Brazdil493411a2016-02-01 13:48:46 +0000110 ArraySet<String> done, boolean useProfiles, boolean extractOnly, boolean force) {
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
David Brazdil493411a2016-02-01 13:48:46 +0000131 if (useProfiles) {
132 // If we do a profile guided compilation then we might recompile
133 // the same package if more profile information is available.
134 force = true;
135 }
136
Fyodor Kupolov74876572015-02-23 17:14:45 -0800137 final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly();
138 boolean performedDexOpt = false;
Fyodor Kupolov74876572015-02-23 17:14:45 -0800139 final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
140 for (String dexCodeInstructionSet : dexCodeInstructionSets) {
David Brazdil493411a2016-02-01 13:48:46 +0000141 if (!force && pkg.mDexOptPerformed.contains(dexCodeInstructionSet)) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800142 continue;
143 }
144
145 for (String path : paths) {
Calin Juravledb4a79a2015-12-23 18:55:08 +0200146 int dexoptNeeded;
David Brazdil493411a2016-02-01 13:48:46 +0000147
148 if (force) {
149 dexoptNeeded = DexFile.DEX2OAT_NEEDED;
150 } else {
151 try {
152 dexoptNeeded = DexFile.getDexOptNeeded(path, pkg.packageName,
153 dexCodeInstructionSet, /* defer */false);
154 } catch (IOException ioe) {
155 Slog.w(TAG, "IOException reading apk: " + path, ioe);
156 return DEX_OPT_FAILED;
157 }
Narayan Kamath01dcb762015-05-07 17:48:42 +0100158 }
Richard Uhler7b08b352015-03-25 16:25:57 -0700159
Calin Juravledb4a79a2015-12-23 18:55:08 +0200160 if (dexoptNeeded == DexFile.NO_DEXOPT_NEEDED) {
David Brazdil493411a2016-02-01 13:48:46 +0000161 // No dexopt needed and we don't use profiles. Nothing to do.
162 continue;
Calin Juravledb4a79a2015-12-23 18:55:08 +0200163 }
164 final String dexoptType;
165 String oatDir = null;
166 if (dexoptNeeded == DexFile.DEX2OAT_NEEDED) {
167 dexoptType = "dex2oat";
168 oatDir = createOatDirIfSupported(pkg, dexCodeInstructionSet);
169 } else if (dexoptNeeded == DexFile.PATCHOAT_NEEDED) {
170 dexoptType = "patchoat";
171 } else if (dexoptNeeded == DexFile.SELF_PATCHOAT_NEEDED) {
172 dexoptType = "self patchoat";
173 } else {
174 throw new IllegalStateException("Invalid dexopt needed: " + dexoptNeeded);
175 }
Narayan Kamath01dcb762015-05-07 17:48:42 +0100176
Calin Juravledb4a79a2015-12-23 18:55:08 +0200177 Log.i(TAG, "Running dexopt (" + dexoptType + ") on: " + path + " pkg="
178 + pkg.applicationInfo.packageName + " isa=" + dexCodeInstructionSet
179 + " vmSafeMode=" + vmSafeMode + " debuggable=" + debuggable
David Brazdila0e10432016-01-20 14:04:40 +0000180 + " extractOnly=" + extractOnly + " oatDir = " + oatDir);
Calin Juravledb4a79a2015-12-23 18:55:08 +0200181 final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid);
182 final int dexFlags =
183 (!pkg.isForwardLocked() ? DEXOPT_PUBLIC : 0)
184 | (vmSafeMode ? DEXOPT_SAFEMODE : 0)
185 | (debuggable ? DEXOPT_DEBUGGABLE : 0)
David Brazdila0e10432016-01-20 14:04:40 +0000186 | (extractOnly ? DEXOPT_EXTRACTONLY : 0)
Calin Juravledb4a79a2015-12-23 18:55:08 +0200187 | DEXOPT_BOOTCOMPLETE;
188 try {
189 mPackageManagerService.mInstaller.dexopt(path, sharedGid,
190 pkg.packageName, dexCodeInstructionSet, dexoptNeeded, oatDir,
Calin Juravle9dff8542016-01-21 11:41:42 -0800191 dexFlags, pkg.volumeUuid, useProfiles);
Calin Juravledb4a79a2015-12-23 18:55:08 +0200192 performedDexOpt = true;
193 } catch (InstallerException e) {
194 Slog.w(TAG, "Failed to dexopt", e);
Fyodor Kupolov74876572015-02-23 17:14:45 -0800195 }
196 }
197
David Brazdila0e10432016-01-20 14:04:40 +0000198 if (!extractOnly) {
199 // At this point we haven't failed dexopt and we haven't deferred dexopt. We must
200 // either have either succeeded dexopt, or have had getDexOptNeeded tell us
201 // it isn't required. We therefore mark that this package doesn't need dexopt unless
202 // it's forced. performedDexOpt will tell us whether we performed dex-opt or skipped
203 // it.
204 pkg.mDexOptPerformed.add(dexCodeInstructionSet);
205 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800206 }
207
208 // If we've gotten here, we're sure that no error occurred and that we haven't
209 // deferred dex-opt. We've either dex-opted one more paths or instruction sets or
210 // we've skipped all of them because they are up to date. In both cases this
211 // package doesn't need dexopt any longer.
212 return performedDexOpt ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED;
213 }
214
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800215 /**
216 * Creates oat dir for the specified package. In certain cases oat directory
217 * <strong>cannot</strong> be created:
218 * <ul>
219 * <li>{@code pkg} is a system app, which is not updated.</li>
220 * <li>Package location is not a directory, i.e. monolithic install.</li>
221 * </ul>
222 *
Richard Uhler7b08b352015-03-25 16:25:57 -0700223 * @return Absolute path to the oat directory or null, if oat directory
224 * cannot be created.
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800225 */
226 @Nullable
Todd Kennedy27c24fb2015-09-17 16:49:25 -0700227 private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) {
Fyodor Kupolovebcac162015-09-09 15:56:45 -0700228 if (!pkg.canHaveOatDir()) {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800229 return null;
230 }
231 File codePath = new File(pkg.codePath);
232 if (codePath.isDirectory()) {
233 File oatDir = getOatDir(codePath);
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700234 try {
235 mPackageManagerService.mInstaller.createOatDir(oatDir.getAbsolutePath(),
236 dexInstructionSet);
237 } catch (InstallerException e) {
238 Slog.w(TAG, "Failed to create oat dir", e);
239 return null;
240 }
Richard Uhler7b08b352015-03-25 16:25:57 -0700241 return oatDir.getAbsolutePath();
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800242 }
243 return null;
244 }
245
246 static File getOatDir(File codePath) {
247 return new File(codePath, OAT_DIR_NAME);
248 }
249
Fyodor Kupolov74876572015-02-23 17:14:45 -0800250 private void performDexOptLibsLI(ArrayList<String> libs, String[] instructionSets,
Todd Kennedy8c6e5372015-09-25 14:47:15 -0700251 ArraySet<String> done) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800252 for (String libName : libs) {
253 PackageParser.Package libPkg = mPackageManagerService.findSharedNonSystemLibrary(
254 libName);
255 if (libPkg != null && !done.contains(libName)) {
Calin Juravledb4a79a2015-12-23 18:55:08 +0200256 // TODO: Analyze and investigate if we (should) profile libraries.
257 // Currently this will do a full compilation of the library.
Calin Juravle9dff8542016-01-21 11:41:42 -0800258 performDexOptLI(libPkg, instructionSets, done, /*useProfiles*/ false,
David Brazdil493411a2016-02-01 13:48:46 +0000259 /* extractOnly */ false, /* force */ false);
Fyodor Kupolov74876572015-02-23 17:14:45 -0800260 }
261 }
262 }
263
Fyodor Kupolova627c092015-05-05 18:44:39 -0700264 void systemReady() {
265 mSystemReady = true;
266 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800267}