blob: f134e40b9c590ad78274cbf37adf95ffbde3e825 [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;
Calin Juravled479b522016-02-24 16:22:03 +000023import android.os.Environment;
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;
Fyodor Kupolov74876572015-02-23 17:14:45 -080027import android.util.Log;
28import android.util.Slog;
29
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -070030import com.android.internal.os.InstallerConnection.InstallerException;
31
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080032import java.io.File;
Fyodor Kupolov74876572015-02-23 17:14:45 -080033import java.io.IOException;
Fyodor Kupolov74876572015-02-23 17:14:45 -080034import 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;
Andreas Gampebdd30d82016-03-20 11:32:11 -070040import static com.android.server.pm.Installer.DEXOPT_PROFILE_GUIDED;
Todd Kennedyfa54ab72015-09-25 07:46:12 -070041import static com.android.server.pm.Installer.DEXOPT_PUBLIC;
42import static com.android.server.pm.Installer.DEXOPT_SAFEMODE;
Fyodor Kupolov74876572015-02-23 17:14:45 -080043import static com.android.server.pm.InstructionSets.getAppDexInstructionSets;
44import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets;
Andreas Gampe47c170a2016-03-30 17:21:19 -070045import static com.android.server.pm.PackageManagerServiceCompilerMapping.getNonProfileGuidedCompilerFilter;
Fyodor Kupolov74876572015-02-23 17:14:45 -080046
47/**
48 * Helper class for running dexopt command on packages.
49 */
Andreas Gampea8908752015-11-10 08:58:14 -080050class PackageDexOptimizer {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080051 private static final String TAG = "PackageManager.DexOptimizer";
52 static final String OAT_DIR_NAME = "oat";
53 // TODO b/19550105 Remove error codes and use exceptions
Fyodor Kupolov74876572015-02-23 17:14:45 -080054 static final int DEX_OPT_SKIPPED = 0;
55 static final int DEX_OPT_PERFORMED = 1;
Fyodor Kupolov74876572015-02-23 17:14:45 -080056 static final int DEX_OPT_FAILED = -1;
57
Andreas Gampea8908752015-11-10 08:58:14 -080058 private final Installer mInstaller;
59 private final Object mInstallLock;
Fyodor Kupolov74876572015-02-23 17:14:45 -080060
Fyodor Kupolova627c092015-05-05 18:44:39 -070061 private final PowerManager.WakeLock mDexoptWakeLock;
62 private volatile boolean mSystemReady;
63
Andreas Gampea8908752015-11-10 08:58:14 -080064 PackageDexOptimizer(Installer installer, Object installLock, Context context,
65 String wakeLockTag) {
66 this.mInstaller = installer;
67 this.mInstallLock = installLock;
68
69 PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
70 mDexoptWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakeLockTag);
71 }
72
73 protected PackageDexOptimizer(PackageDexOptimizer from) {
74 this.mInstaller = from.mInstaller;
75 this.mInstallLock = from.mInstallLock;
76 this.mDexoptWakeLock = from.mDexoptWakeLock;
77 this.mSystemReady = from.mSystemReady;
Fyodor Kupolov74876572015-02-23 17:14:45 -080078 }
79
Calin Juravledb4a79a2015-12-23 18:55:08 +020080 static boolean canOptimizePackage(PackageParser.Package pkg) {
Calin Juravle6dfd83d2016-01-29 18:23:57 +000081 return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0;
Calin Juravledb4a79a2015-12-23 18:55:08 +020082 }
83
Fyodor Kupolov74876572015-02-23 17:14:45 -080084 /**
85 * Performs dexopt on all code paths and libraries of the specified package for specified
86 * instruction sets.
87 *
Andreas Gampea8908752015-11-10 08:58:14 -080088 * <p>Calls to {@link com.android.server.pm.Installer#dexopt} on {@link #mInstaller} are
89 * synchronized on {@link #mInstallLock}.
Fyodor Kupolov74876572015-02-23 17:14:45 -080090 */
Jeff Haoc7b94822016-03-16 15:56:07 -070091 int performDexOpt(PackageParser.Package pkg, String[] sharedLibraries,
92 String[] instructionSets, boolean checkProfiles, String targetCompilationFilter) {
Andreas Gampea8908752015-11-10 08:58:14 -080093 synchronized (mInstallLock) {
Fyodor Kupolova627c092015-05-05 18:44:39 -070094 final boolean useLock = mSystemReady;
95 if (useLock) {
96 mDexoptWakeLock.setWorkSource(new WorkSource(pkg.applicationInfo.uid));
97 mDexoptWakeLock.acquire();
98 }
99 try {
Jeff Haoc7b94822016-03-16 15:56:07 -0700100 return performDexOptLI(pkg, sharedLibraries, instructionSets, checkProfiles,
Andreas Gampebdd30d82016-03-20 11:32:11 -0700101 targetCompilationFilter);
Fyodor Kupolova627c092015-05-05 18:44:39 -0700102 } finally {
103 if (useLock) {
104 mDexoptWakeLock.release();
105 }
106 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800107 }
108 }
109
Andreas Gampea8908752015-11-10 08:58:14 -0800110 /**
Andreas Gampea8908752015-11-10 08:58:14 -0800111 * Adjust the given dexopt-needed value. Can be overridden to influence the decision to
112 * optimize or not (and in what way).
113 */
114 protected int adjustDexoptNeeded(int dexoptNeeded) {
115 return dexoptNeeded;
116 }
117
118 /**
119 * Adjust the given dexopt flags that will be passed to the installer.
120 */
121 protected int adjustDexoptFlags(int dexoptFlags) {
122 return dexoptFlags;
123 }
124
Jeff Haoc7b94822016-03-16 15:56:07 -0700125 private int performDexOptLI(PackageParser.Package pkg, String[] sharedLibraries,
126 String[] targetInstructionSets, boolean checkProfiles, String targetCompilerFilter) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800127 final String[] instructionSets = targetInstructionSets != null ?
128 targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo);
129
Calin Juravledb4a79a2015-12-23 18:55:08 +0200130 if (!canOptimizePackage(pkg)) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800131 return DEX_OPT_SKIPPED;
132 }
133
Andreas Gampebdd30d82016-03-20 11:32:11 -0700134 final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly();
135 final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid);
136
137 boolean isProfileGuidedFilter = DexFile.isProfileGuidedCompilerFilter(targetCompilerFilter);
138 // If any part of the app is used by other apps, we cannot use profile-guided
139 // compilation.
David Brazdil90e26992016-04-18 14:08:52 +0100140 if (isProfileGuidedFilter && isUsedByOtherApps(pkg)) {
141 checkProfiles = false;
Andreas Gampebdd30d82016-03-20 11:32:11 -0700142
David Brazdil90e26992016-04-18 14:08:52 +0100143 targetCompilerFilter = getNonProfileGuidedCompilerFilter(targetCompilerFilter);
144 if (DexFile.isProfileGuidedCompilerFilter(targetCompilerFilter)) {
145 throw new IllegalStateException(targetCompilerFilter);
Andreas Gampebdd30d82016-03-20 11:32:11 -0700146 }
David Brazdil90e26992016-04-18 14:08:52 +0100147 isProfileGuidedFilter = false;
Andreas Gampebdd30d82016-03-20 11:32:11 -0700148 }
149
150 // If we're asked to take profile updates into account, check now.
151 boolean newProfile = false;
152 if (checkProfiles && isProfileGuidedFilter) {
153 // Merge profiles, see if we need to do anything.
154 try {
155 newProfile = mInstaller.mergeProfiles(sharedGid, pkg.packageName);
156 } catch (InstallerException e) {
157 Slog.w(TAG, "Failed to merge profiles", e);
158 }
159 }
160
Fyodor Kupolov74876572015-02-23 17:14:45 -0800161 final boolean vmSafeMode = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0;
162 final boolean debuggable = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
163
Fyodor Kupolov74876572015-02-23 17:14:45 -0800164 boolean performedDexOpt = false;
David Brazdil6a3b2d22016-04-08 16:09:06 +0100165 boolean successfulDexOpt = true;
166
Fyodor Kupolov74876572015-02-23 17:14:45 -0800167 final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
168 for (String dexCodeInstructionSet : dexCodeInstructionSets) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800169 for (String path : paths) {
Calin Juravledb4a79a2015-12-23 18:55:08 +0200170 int dexoptNeeded;
Andreas Gampea8908752015-11-10 08:58:14 -0800171 try {
Calin Juravle693f9972016-02-25 15:26:20 +0000172 dexoptNeeded = DexFile.getDexOptNeeded(path,
Andreas Gampebdd30d82016-03-20 11:32:11 -0700173 dexCodeInstructionSet, targetCompilerFilter, newProfile);
Andreas Gampea8908752015-11-10 08:58:14 -0800174 } catch (IOException ioe) {
175 Slog.w(TAG, "IOException reading apk: " + path, ioe);
176 return DEX_OPT_FAILED;
Narayan Kamath01dcb762015-05-07 17:48:42 +0100177 }
Andreas Gampea8908752015-11-10 08:58:14 -0800178 dexoptNeeded = adjustDexoptNeeded(dexoptNeeded);
Andreas Gampe47c170a2016-03-30 17:21:19 -0700179 if (PackageManagerService.DEBUG_DEXOPT) {
180 Log.i(TAG, "DexoptNeeded for " + path + "@" + targetCompilerFilter + " is " +
181 dexoptNeeded);
182 }
Richard Uhler7b08b352015-03-25 16:25:57 -0700183
Calin Juravledb4a79a2015-12-23 18:55:08 +0200184 final String dexoptType;
185 String oatDir = null;
Calin Juravle693f9972016-02-25 15:26:20 +0000186 switch (dexoptNeeded) {
187 case DexFile.NO_DEXOPT_NEEDED:
188 continue;
189 case DexFile.DEX2OAT_NEEDED:
190 dexoptType = "dex2oat";
191 oatDir = createOatDirIfSupported(pkg, dexCodeInstructionSet);
192 break;
193 case DexFile.PATCHOAT_NEEDED:
194 dexoptType = "patchoat";
195 break;
196 case DexFile.SELF_PATCHOAT_NEEDED:
197 dexoptType = "self patchoat";
198 break;
199 default:
200 throw new IllegalStateException("Invalid dexopt:" + dexoptNeeded);
Calin Juravledb4a79a2015-12-23 18:55:08 +0200201 }
Narayan Kamath01dcb762015-05-07 17:48:42 +0100202
Jeff Haoc7b94822016-03-16 15:56:07 -0700203 String sharedLibrariesPath = null;
204 if (sharedLibraries != null && sharedLibraries.length != 0) {
205 StringBuilder sb = new StringBuilder();
206 for (String lib : sharedLibraries) {
207 if (sb.length() != 0) {
208 sb.append(":");
209 }
210 sb.append(lib);
211 }
212 sharedLibrariesPath = sb.toString();
213 }
Calin Juravledb4a79a2015-12-23 18:55:08 +0200214 Log.i(TAG, "Running dexopt (" + dexoptType + ") on: " + path + " pkg="
215 + pkg.applicationInfo.packageName + " isa=" + dexCodeInstructionSet
216 + " vmSafeMode=" + vmSafeMode + " debuggable=" + debuggable
Jeff Haoc7b94822016-03-16 15:56:07 -0700217 + " target-filter=" + targetCompilerFilter + " oatDir = " + oatDir
218 + " sharedLibraries=" + sharedLibrariesPath);
Calin Juravled479b522016-02-24 16:22:03 +0000219 // Profile guide compiled oat files should not be public.
Andreas Gampebdd30d82016-03-20 11:32:11 -0700220 final boolean isPublic = !pkg.isForwardLocked() && !isProfileGuidedFilter;
221 final int profileFlag = isProfileGuidedFilter ? DEXOPT_PROFILE_GUIDED : 0;
Andreas Gampea8908752015-11-10 08:58:14 -0800222 final int dexFlags = adjustDexoptFlags(
Calin Juravled479b522016-02-24 16:22:03 +0000223 ( isPublic ? DEXOPT_PUBLIC : 0)
Calin Juravledb4a79a2015-12-23 18:55:08 +0200224 | (vmSafeMode ? DEXOPT_SAFEMODE : 0)
225 | (debuggable ? DEXOPT_DEBUGGABLE : 0)
Andreas Gampebdd30d82016-03-20 11:32:11 -0700226 | profileFlag
Andreas Gampea8908752015-11-10 08:58:14 -0800227 | DEXOPT_BOOTCOMPLETE);
228
Calin Juravledb4a79a2015-12-23 18:55:08 +0200229 try {
Andreas Gampea8908752015-11-10 08:58:14 -0800230 mInstaller.dexopt(path, sharedGid, pkg.packageName, dexCodeInstructionSet,
Jeff Haoc7b94822016-03-16 15:56:07 -0700231 dexoptNeeded, oatDir, dexFlags, targetCompilerFilter, pkg.volumeUuid,
232 sharedLibrariesPath);
Calin Juravledb4a79a2015-12-23 18:55:08 +0200233 performedDexOpt = true;
234 } catch (InstallerException e) {
235 Slog.w(TAG, "Failed to dexopt", e);
David Brazdil6a3b2d22016-04-08 16:09:06 +0100236 successfulDexOpt = false;
Fyodor Kupolov74876572015-02-23 17:14:45 -0800237 }
238 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800239 }
240
David Brazdil6a3b2d22016-04-08 16:09:06 +0100241 if (successfulDexOpt) {
242 // If we've gotten here, we're sure that no error occurred. We've either
243 // dex-opted one or more paths or instruction sets or we've skipped
244 // all of them because they are up to date. In both cases this package
245 // doesn't need dexopt any longer.
246 return performedDexOpt ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED;
247 } else {
248 return DEX_OPT_FAILED;
249 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800250 }
251
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800252 /**
253 * Creates oat dir for the specified package. In certain cases oat directory
254 * <strong>cannot</strong> be created:
255 * <ul>
256 * <li>{@code pkg} is a system app, which is not updated.</li>
257 * <li>Package location is not a directory, i.e. monolithic install.</li>
258 * </ul>
259 *
Richard Uhler7b08b352015-03-25 16:25:57 -0700260 * @return Absolute path to the oat directory or null, if oat directory
261 * cannot be created.
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800262 */
263 @Nullable
Todd Kennedy27c24fb2015-09-17 16:49:25 -0700264 private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) {
Fyodor Kupolovebcac162015-09-09 15:56:45 -0700265 if (!pkg.canHaveOatDir()) {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800266 return null;
267 }
268 File codePath = new File(pkg.codePath);
269 if (codePath.isDirectory()) {
270 File oatDir = getOatDir(codePath);
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700271 try {
Andreas Gampea8908752015-11-10 08:58:14 -0800272 mInstaller.createOatDir(oatDir.getAbsolutePath(), dexInstructionSet);
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700273 } catch (InstallerException e) {
274 Slog.w(TAG, "Failed to create oat dir", e);
275 return null;
276 }
Richard Uhler7b08b352015-03-25 16:25:57 -0700277 return oatDir.getAbsolutePath();
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800278 }
279 return null;
280 }
281
282 static File getOatDir(File codePath) {
283 return new File(codePath, OAT_DIR_NAME);
284 }
285
Fyodor Kupolova627c092015-05-05 18:44:39 -0700286 void systemReady() {
287 mSystemReady = true;
288 }
Andreas Gampea8908752015-11-10 08:58:14 -0800289
David Brazdil90e26992016-04-18 14:08:52 +0100290 /**
291 * Returns true if the profiling data collected for the given app indicate
292 * that the apps's APK has been loaded by another app.
293 * Note that this returns false for all forward-locked apps and apps without
294 * any collected profiling data.
295 */
296 public static boolean isUsedByOtherApps(PackageParser.Package pkg) {
297 if (pkg.isForwardLocked()) {
298 // Skip the check for forward locked packages since they don't share their code.
299 return false;
Calin Juravled479b522016-02-24 16:22:03 +0000300 }
David Brazdil90e26992016-04-18 14:08:52 +0100301
302 for (String apkPath : pkg.getAllCodePathsExcludingResourceOnly()) {
303 try {
304 apkPath = new File(apkPath).getCanonicalPath();
305 } catch (IOException e) {
306 // Log an error but continue without it.
307 Slog.w(TAG, "Failed to get canonical path", e);
308 }
309 String useMarker = apkPath.replace('/', '@');
310 final int[] currentUserIds = UserManagerService.getInstance().getUserIds();
311 for (int i = 0; i < currentUserIds.length; i++) {
312 File profileDir =
313 Environment.getDataProfilesDeForeignDexDirectory(currentUserIds[i]);
314 File foreignUseMark = new File(profileDir, useMarker);
315 if (foreignUseMark.exists()) {
316 return true;
317 }
Calin Juravled479b522016-02-24 16:22:03 +0000318 }
319 }
320 return false;
321 }
322
Andreas Gampea8908752015-11-10 08:58:14 -0800323 /**
324 * A specialized PackageDexOptimizer that overrides already-installed checks, forcing a
325 * dexopt path.
326 */
327 public static class ForcedUpdatePackageDexOptimizer extends PackageDexOptimizer {
328
329 public ForcedUpdatePackageDexOptimizer(Installer installer, Object installLock,
330 Context context, String wakeLockTag) {
331 super(installer, installLock, context, wakeLockTag);
332 }
333
334 public ForcedUpdatePackageDexOptimizer(PackageDexOptimizer from) {
335 super(from);
336 }
337
338 @Override
Andreas Gampea8908752015-11-10 08:58:14 -0800339 protected int adjustDexoptNeeded(int dexoptNeeded) {
340 // Ensure compilation, no matter the current state.
341 // TODO: The return value is wrong when patchoat is needed.
342 return DexFile.DEX2OAT_NEEDED;
343 }
344 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800345}