blob: 0db1f0192d9da4be01fd960b271faa6dce665424 [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 Juravleadbadd52017-03-28 18:19:15 -070023import android.os.FileUtils;
Fyodor Kupolova627c092015-05-05 18:44:39 -070024import android.os.PowerManager;
Fyodor Kupolov28f28552017-05-02 12:11:02 -070025import android.os.SystemClock;
Fyodor Kupolov74876572015-02-23 17:14:45 -080026import android.os.UserHandle;
Fyodor Kupolova627c092015-05-05 18:44:39 -070027import android.os.WorkSource;
Fyodor Kupolov74876572015-02-23 17:14:45 -080028import android.util.Log;
29import android.util.Slog;
30
Calin Juravle2d4b6ad72017-01-04 13:22:14 -080031import com.android.internal.annotations.GuardedBy;
Narayan Kamath88eea9e2016-05-02 14:44:31 +010032import com.android.internal.util.IndentingPrintWriter;
Jeff Sharkey740f5232016-12-09 14:31:26 -070033import com.android.server.pm.Installer.InstallerException;
Calin Juravle3b74c412017-08-03 19:48:37 -070034import com.android.server.pm.dex.DexManager;
Calin Juravle1d0e83d2017-07-17 15:12:01 -070035import com.android.server.pm.dex.DexoptOptions;
Calin Juravle19da1cf2017-07-12 18:52:49 -070036import com.android.server.pm.dex.DexoptUtils;
Calin Juravlef1ff36f2017-07-22 12:33:41 -070037import com.android.server.pm.dex.PackageDexUsage;
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -070038
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080039import java.io.File;
Fyodor Kupolov74876572015-02-23 17:14:45 -080040import java.io.IOException;
Calin Juravle2d4b6ad72017-01-04 13:22:14 -080041import java.util.ArrayList;
Fyodor Kupolov74876572015-02-23 17:14:45 -080042import java.util.List;
43
44import dalvik.system.DexFile;
Fyodor Kupolov74876572015-02-23 17:14:45 -080045
Todd Kennedyfa54ab72015-09-25 07:46:12 -070046import static com.android.server.pm.Installer.DEXOPT_BOOTCOMPLETE;
47import static com.android.server.pm.Installer.DEXOPT_DEBUGGABLE;
Andreas Gampebdd30d82016-03-20 11:32:11 -070048import static com.android.server.pm.Installer.DEXOPT_PROFILE_GUIDED;
Todd Kennedyfa54ab72015-09-25 07:46:12 -070049import static com.android.server.pm.Installer.DEXOPT_PUBLIC;
Calin Juravlec22c30e2017-01-16 19:18:48 -080050import static com.android.server.pm.Installer.DEXOPT_SECONDARY_DEX;
51import static com.android.server.pm.Installer.DEXOPT_FORCE;
52import static com.android.server.pm.Installer.DEXOPT_STORAGE_CE;
53import static com.android.server.pm.Installer.DEXOPT_STORAGE_DE;
Fyodor Kupolov74876572015-02-23 17:14:45 -080054import static com.android.server.pm.InstructionSets.getAppDexInstructionSets;
55import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets;
Calin Juravle2d4b6ad72017-01-04 13:22:14 -080056
Fyodor Kupolov28f28552017-05-02 12:11:02 -070057import static com.android.server.pm.PackageManagerService.WATCHDOG_TIMEOUT;
Nicolas Geoffrayb0818e82017-05-05 10:23:19 +010058
Nicolas Geoffrayd093b202017-05-03 13:11:58 +010059import static dalvik.system.DexFile.getNonProfileGuidedCompilerFilter;
60import static dalvik.system.DexFile.getSafeModeCompilerFilter;
Calin Juravle2d4b6ad72017-01-04 13:22:14 -080061import static dalvik.system.DexFile.isProfileGuidedCompilerFilter;
Fyodor Kupolov74876572015-02-23 17:14:45 -080062
63/**
64 * Helper class for running dexopt command on packages.
65 */
Calin Juravlec22c30e2017-01-16 19:18:48 -080066public class PackageDexOptimizer {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080067 private static final String TAG = "PackageManager.DexOptimizer";
68 static final String OAT_DIR_NAME = "oat";
69 // TODO b/19550105 Remove error codes and use exceptions
Calin Juravlec22c30e2017-01-16 19:18:48 -080070 public static final int DEX_OPT_SKIPPED = 0;
71 public static final int DEX_OPT_PERFORMED = 1;
72 public static final int DEX_OPT_FAILED = -1;
Fyodor Kupolov28f28552017-05-02 12:11:02 -070073 // One minute over PM WATCHDOG_TIMEOUT
74 private static final long WAKELOCK_TIMEOUT_MS = WATCHDOG_TIMEOUT + 1000 * 60;
Fyodor Kupolov74876572015-02-23 17:14:45 -080075
Calin Juravleaae35762017-02-14 17:53:13 -080076 /** Special library name that skips shared libraries check during compilation. */
77 public static final String SKIP_SHARED_LIBRARY_CHECK = "&";
78
Fyodor Kupolov28f28552017-05-02 12:11:02 -070079 @GuardedBy("mInstallLock")
Andreas Gampea8908752015-11-10 08:58:14 -080080 private final Installer mInstaller;
81 private final Object mInstallLock;
Fyodor Kupolov74876572015-02-23 17:14:45 -080082
Fyodor Kupolov28f28552017-05-02 12:11:02 -070083 @GuardedBy("mInstallLock")
Fyodor Kupolova627c092015-05-05 18:44:39 -070084 private final PowerManager.WakeLock mDexoptWakeLock;
85 private volatile boolean mSystemReady;
86
Andreas Gampea8908752015-11-10 08:58:14 -080087 PackageDexOptimizer(Installer installer, Object installLock, Context context,
88 String wakeLockTag) {
89 this.mInstaller = installer;
90 this.mInstallLock = installLock;
91
92 PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
93 mDexoptWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakeLockTag);
94 }
95
96 protected PackageDexOptimizer(PackageDexOptimizer from) {
97 this.mInstaller = from.mInstaller;
98 this.mInstallLock = from.mInstallLock;
99 this.mDexoptWakeLock = from.mDexoptWakeLock;
100 this.mSystemReady = from.mSystemReady;
Fyodor Kupolov74876572015-02-23 17:14:45 -0800101 }
102
Calin Juravledb4a79a2015-12-23 18:55:08 +0200103 static boolean canOptimizePackage(PackageParser.Package pkg) {
Calin Juravle6dfd83d2016-01-29 18:23:57 +0000104 return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0;
Calin Juravledb4a79a2015-12-23 18:55:08 +0200105 }
106
Fyodor Kupolov74876572015-02-23 17:14:45 -0800107 /**
108 * Performs dexopt on all code paths and libraries of the specified package for specified
109 * instruction sets.
110 *
Andreas Gampea8908752015-11-10 08:58:14 -0800111 * <p>Calls to {@link com.android.server.pm.Installer#dexopt} on {@link #mInstaller} are
112 * synchronized on {@link #mInstallLock}.
Fyodor Kupolov74876572015-02-23 17:14:45 -0800113 */
Jeff Haoc7b94822016-03-16 15:56:07 -0700114 int performDexOpt(PackageParser.Package pkg, String[] sharedLibraries,
Calin Juravle1d0e83d2017-07-17 15:12:01 -0700115 String[] instructionSets, CompilerStats.PackageStats packageStats,
Calin Juravle3b74c412017-08-03 19:48:37 -0700116 PackageDexUsage.PackageUseInfo packageUseInfo, DexoptOptions options) {
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800117 if (!canOptimizePackage(pkg)) {
118 return DEX_OPT_SKIPPED;
119 }
Andreas Gampea8908752015-11-10 08:58:14 -0800120 synchronized (mInstallLock) {
Fyodor Kupolov28f28552017-05-02 12:11:02 -0700121 final long acquireTime = acquireWakeLockLI(pkg.applicationInfo.uid);
Fyodor Kupolova627c092015-05-05 18:44:39 -0700122 try {
Calin Juravle1d0e83d2017-07-17 15:12:01 -0700123 return performDexOptLI(pkg, sharedLibraries, instructionSets,
Calin Juravle3b74c412017-08-03 19:48:37 -0700124 packageStats, packageUseInfo, options);
Fyodor Kupolova627c092015-05-05 18:44:39 -0700125 } finally {
Fyodor Kupolov28f28552017-05-02 12:11:02 -0700126 releaseWakeLockLI(acquireTime);
Fyodor Kupolova627c092015-05-05 18:44:39 -0700127 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800128 }
129 }
130
Andreas Gampea8908752015-11-10 08:58:14 -0800131 /**
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800132 * Performs dexopt on all code paths of the given package.
133 * It assumes the install lock is held.
134 */
135 @GuardedBy("mInstallLock")
136 private int performDexOptLI(PackageParser.Package pkg, String[] sharedLibraries,
Calin Juravle1d0e83d2017-07-17 15:12:01 -0700137 String[] targetInstructionSets, CompilerStats.PackageStats packageStats,
Calin Juravle3b74c412017-08-03 19:48:37 -0700138 PackageDexUsage.PackageUseInfo packageUseInfo, DexoptOptions options) {
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800139 final String[] instructionSets = targetInstructionSets != null ?
140 targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo);
141 final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
Jeff Hao5def5e52017-04-19 14:42:16 -0700142 final List<String> paths = pkg.getAllCodePaths();
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800143 final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid);
144
Calin Juravle19da1cf2017-07-12 18:52:49 -0700145 // Get the class loader context dependencies.
146 // For each code path in the package, this array contains the class loader context that
147 // needs to be passed to dexopt in order to ensure correct optimizations.
148 String[] classLoaderContexts = DexoptUtils.getClassLoaderContexts(
149 pkg.applicationInfo, sharedLibraries);
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800150
151 int result = DEX_OPT_SKIPPED;
Jeff Hao5def5e52017-04-19 14:42:16 -0700152 for (int i = 0; i < paths.size(); i++) {
153 // Skip paths that have no code.
154 if ((i == 0 && (pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) == 0) ||
155 (i != 0 && (pkg.splitFlags[i - 1] & ApplicationInfo.FLAG_HAS_CODE) == 0)) {
156 continue;
157 }
158 // Append shared libraries with split dependencies for this split.
159 String path = paths.get(i);
Calin Juravleb6f844d2017-07-17 15:23:21 -0700160 if (options.getSplitName() != null) {
161 // We are asked to compile only a specific split. Check that the current path is
162 // what we are looking for.
163 if (!options.getSplitName().equals(new File(path).getName())) {
164 continue;
165 }
166 }
167
Calin Juravle3b74c412017-08-03 19:48:37 -0700168 final boolean isUsedByOtherApps = options.isDexoptAsSharedLibrary()
169 || packageUseInfo.isUsedByOtherApps();
170 final String compilerFilter = getRealCompilerFilter(pkg.applicationInfo,
171 options.getCompilerFilter(), isUsedByOtherApps);
172 final boolean profileUpdated = options.isCheckForProfileUpdates() &&
173 isProfileUpdated(pkg, sharedGid, compilerFilter);
174
175 // Get the dexopt flags after getRealCompilerFilter to make sure we get the correct
176 // flags.
177 final int dexoptFlags = getDexFlags(pkg, compilerFilter, options.isBootComplete());
178
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800179 for (String dexCodeIsa : dexCodeInstructionSets) {
Calin Juravle19da1cf2017-07-12 18:52:49 -0700180 int newResult = dexOptPath(pkg, path, dexCodeIsa, compilerFilter,
181 profileUpdated, classLoaderContexts[i], dexoptFlags, sharedGid,
182 packageStats, options.isDowngrade());
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800183 // The end result is:
184 // - FAILED if any path failed,
185 // - PERFORMED if at least one path needed compilation,
186 // - SKIPPED when all paths are up to date
187 if ((result != DEX_OPT_FAILED) && (newResult != DEX_OPT_SKIPPED)) {
188 result = newResult;
189 }
190 }
191 }
192 return result;
193 }
194
195 /**
196 * Performs dexopt on the {@code path} belonging to the package {@code pkg}.
197 *
198 * @return
199 * DEX_OPT_FAILED if there was any exception during dexopt
200 * DEX_OPT_PERFORMED if dexopt was performed successfully on the given path.
201 * DEX_OPT_SKIPPED if the path does not need to be deopt-ed.
202 */
203 @GuardedBy("mInstallLock")
204 private int dexOptPath(PackageParser.Package pkg, String path, String isa,
205 String compilerFilter, boolean profileUpdated, String sharedLibrariesPath,
Shubham Ajmera246dccf2017-05-24 17:46:36 -0700206 int dexoptFlags, int uid, CompilerStats.PackageStats packageStats, boolean downgrade) {
207 int dexoptNeeded = getDexoptNeeded(path, isa, compilerFilter, profileUpdated, downgrade);
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800208 if (Math.abs(dexoptNeeded) == DexFile.NO_DEXOPT_NEEDED) {
209 return DEX_OPT_SKIPPED;
210 }
211
212 // TODO(calin): there's no need to try to create the oat dir over and over again,
213 // especially since it involve an extra installd call. We should create
214 // if (if supported) on the fly during the dexopt call.
215 String oatDir = createOatDirIfSupported(pkg, isa);
216
217 Log.i(TAG, "Running dexopt (dexoptNeeded=" + dexoptNeeded + ") on: " + path
218 + " pkg=" + pkg.applicationInfo.packageName + " isa=" + isa
219 + " dexoptFlags=" + printDexoptFlags(dexoptFlags)
220 + " target-filter=" + compilerFilter + " oatDir=" + oatDir
221 + " sharedLibraries=" + sharedLibrariesPath);
222
223 try {
224 long startTime = System.currentTimeMillis();
225
Shubham Ajmera246dccf2017-05-24 17:46:36 -0700226 // TODO: Consider adding 2 different APIs for primary and secondary dexopt.
227 // installd only uses downgrade flag for secondary dex files and ignores it for
228 // primary dex files.
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800229 mInstaller.dexopt(path, uid, pkg.packageName, isa, dexoptNeeded, oatDir, dexoptFlags,
Shubham Ajmera246dccf2017-05-24 17:46:36 -0700230 compilerFilter, pkg.volumeUuid, sharedLibrariesPath, pkg.applicationInfo.seInfo,
231 false /* downgrade*/);
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800232
233 if (packageStats != null) {
234 long endTime = System.currentTimeMillis();
235 packageStats.setCompileTime(path, (int)(endTime - startTime));
236 }
237 return DEX_OPT_PERFORMED;
238 } catch (InstallerException e) {
239 Slog.w(TAG, "Failed to dexopt", e);
240 return DEX_OPT_FAILED;
241 }
242 }
243
244 /**
Calin Juravlec22c30e2017-01-16 19:18:48 -0800245 * Performs dexopt on the secondary dex {@code path} belonging to the app {@code info}.
246 *
247 * @return
248 * DEX_OPT_FAILED if there was any exception during dexopt
249 * DEX_OPT_PERFORMED if dexopt was performed successfully on the given path.
250 * NOTE that DEX_OPT_PERFORMED for secondary dex files includes the case when the dex file
251 * didn't need an update. That's because at the moment we don't get more than success/failure
252 * from installd.
253 *
254 * TODO(calin): Consider adding return codes to installd dexopt invocation (rather than
255 * throwing exceptions). Or maybe make a separate call to installd to get DexOptNeeded, though
256 * that seems wasteful.
257 */
Calin Juravlef1ff36f2017-07-22 12:33:41 -0700258 public int dexOptSecondaryDexPath(ApplicationInfo info, String path,
259 PackageDexUsage.DexUseInfo dexUseInfo, DexoptOptions options) {
Calin Juravlec22c30e2017-01-16 19:18:48 -0800260 synchronized (mInstallLock) {
Fyodor Kupolov28f28552017-05-02 12:11:02 -0700261 final long acquireTime = acquireWakeLockLI(info.uid);
Calin Juravlec22c30e2017-01-16 19:18:48 -0800262 try {
Calin Juravlef1ff36f2017-07-22 12:33:41 -0700263 return dexOptSecondaryDexPathLI(info, path, dexUseInfo, options);
Calin Juravlec22c30e2017-01-16 19:18:48 -0800264 } finally {
Fyodor Kupolov28f28552017-05-02 12:11:02 -0700265 releaseWakeLockLI(acquireTime);
Calin Juravlec22c30e2017-01-16 19:18:48 -0800266 }
267 }
268 }
269
270 @GuardedBy("mInstallLock")
Fyodor Kupolov28f28552017-05-02 12:11:02 -0700271 private long acquireWakeLockLI(final int uid) {
272 // During boot the system doesn't need to instantiate and obtain a wake lock.
273 // PowerManager might not be ready, but that doesn't mean that we can't proceed with
274 // dexopt.
275 if (!mSystemReady) {
276 return -1;
277 }
278 mDexoptWakeLock.setWorkSource(new WorkSource(uid));
279 mDexoptWakeLock.acquire(WAKELOCK_TIMEOUT_MS);
280 return SystemClock.elapsedRealtime();
281 }
282
283 @GuardedBy("mInstallLock")
284 private void releaseWakeLockLI(final long acquireTime) {
285 if (acquireTime < 0) {
286 return;
287 }
288 try {
289 if (mDexoptWakeLock.isHeld()) {
290 mDexoptWakeLock.release();
291 }
292 final long duration = SystemClock.elapsedRealtime() - acquireTime;
293 if (duration >= WAKELOCK_TIMEOUT_MS) {
294 Slog.wtf(TAG, "WakeLock " + mDexoptWakeLock.getTag()
295 + " time out. Operation took " + duration + " ms. Thread: "
296 + Thread.currentThread().getName());
297 }
298 } catch (Exception e) {
299 Slog.wtf(TAG, "Error while releasing " + mDexoptWakeLock.getTag() + " lock", e);
300 }
301 }
302
303 @GuardedBy("mInstallLock")
Calin Juravlef1ff36f2017-07-22 12:33:41 -0700304 private int dexOptSecondaryDexPathLI(ApplicationInfo info, String path,
305 PackageDexUsage.DexUseInfo dexUseInfo, DexoptOptions options) {
306 if (options.isDexoptOnlySharedDex() && !dexUseInfo.isUsedByOtherApps()) {
307 // We are asked to optimize only the dex files used by other apps and this is not
308 // on of them: skip it.
309 return DEX_OPT_SKIPPED;
310 }
311
312 String compilerFilter = getRealCompilerFilter(info, options.getCompilerFilter(),
313 dexUseInfo.isUsedByOtherApps());
Calin Juravleefb1c942017-04-04 20:31:44 -0700314 // Get the dexopt flags after getRealCompilerFilter to make sure we get the correct flags.
Nicolas Geoffray3b4359a2017-05-25 13:53:22 +0100315 // Secondary dex files are currently not compiled at boot.
316 int dexoptFlags = getDexFlags(info, compilerFilter, /* bootComplete */ true)
317 | DEXOPT_SECONDARY_DEX;
Calin Juravlec22c30e2017-01-16 19:18:48 -0800318 // Check the app storage and add the appropriate flags.
Calin Juravleadbadd52017-03-28 18:19:15 -0700319 if (info.deviceProtectedDataDir != null &&
320 FileUtils.contains(info.deviceProtectedDataDir, path)) {
Calin Juravlec22c30e2017-01-16 19:18:48 -0800321 dexoptFlags |= DEXOPT_STORAGE_DE;
Calin Juravleadbadd52017-03-28 18:19:15 -0700322 } else if (info.credentialProtectedDataDir != null &&
323 FileUtils.contains(info.credentialProtectedDataDir, path)) {
Calin Juravlec22c30e2017-01-16 19:18:48 -0800324 dexoptFlags |= DEXOPT_STORAGE_CE;
325 } else {
326 Slog.e(TAG, "Could not infer CE/DE storage for package " + info.packageName);
327 return DEX_OPT_FAILED;
328 }
Calin Juravlec22c30e2017-01-16 19:18:48 -0800329 Log.d(TAG, "Running dexopt on: " + path
Calin Juravlef1ff36f2017-07-22 12:33:41 -0700330 + " pkg=" + info.packageName + " isa=" + dexUseInfo.getLoaderIsas()
Calin Juravlec22c30e2017-01-16 19:18:48 -0800331 + " dexoptFlags=" + printDexoptFlags(dexoptFlags)
332 + " target-filter=" + compilerFilter);
333
Calin Juravlef1ff36f2017-07-22 12:33:41 -0700334 String classLoaderContext;
335 if (dexUseInfo.isUnknownClassLoaderContext() ||
336 dexUseInfo.isUnsupportedClassLoaderContext() ||
337 dexUseInfo.isVariableClassLoaderContext()) {
338 // If we have an unknown (not yet set), unsupported (custom class loaders), or a
339 // variable class loader chain, compile without a context and mark the oat file with
340 // SKIP_SHARED_LIBRARY_CHECK. Note that his might lead to a incorrect compilation.
341 // TODO(calin): We should just extract in this case.
342 classLoaderContext = SKIP_SHARED_LIBRARY_CHECK;
343 } else {
344 classLoaderContext = dexUseInfo.getClassLoaderContext();
345 }
Calin Juravlec22c30e2017-01-16 19:18:48 -0800346 try {
Calin Juravlef1ff36f2017-07-22 12:33:41 -0700347 for (String isa : dexUseInfo.getLoaderIsas()) {
Calin Juravlec22c30e2017-01-16 19:18:48 -0800348 // Reuse the same dexopt path as for the primary apks. We don't need all the
349 // arguments as some (dexopNeeded and oatDir) will be computed by installd because
350 // system server cannot read untrusted app content.
351 // TODO(calin): maybe add a separate call.
352 mInstaller.dexopt(path, info.uid, info.packageName, isa, /*dexoptNeeded*/ 0,
353 /*oatDir*/ null, dexoptFlags,
Calin Juravlef1ff36f2017-07-22 12:33:41 -0700354 compilerFilter, info.volumeUuid, classLoaderContext, info.seInfoUser,
355 options.isDowngrade());
Calin Juravlec22c30e2017-01-16 19:18:48 -0800356 }
357
358 return DEX_OPT_PERFORMED;
359 } catch (InstallerException e) {
360 Slog.w(TAG, "Failed to dexopt", e);
361 return DEX_OPT_FAILED;
362 }
363 }
364
365 /**
Andreas Gampea8908752015-11-10 08:58:14 -0800366 * Adjust the given dexopt-needed value. Can be overridden to influence the decision to
367 * optimize or not (and in what way).
368 */
369 protected int adjustDexoptNeeded(int dexoptNeeded) {
370 return dexoptNeeded;
371 }
372
373 /**
374 * Adjust the given dexopt flags that will be passed to the installer.
375 */
376 protected int adjustDexoptFlags(int dexoptFlags) {
377 return dexoptFlags;
378 }
379
Narayan Kamath88eea9e2016-05-02 14:44:31 +0100380 /**
381 * Dumps the dexopt state of the given package {@code pkg} to the given {@code PrintWriter}.
382 */
383 void dumpDexoptState(IndentingPrintWriter pw, PackageParser.Package pkg) {
384 final String[] instructionSets = getAppDexInstructionSets(pkg.applicationInfo);
385 final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
386
387 final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly();
388
389 for (String instructionSet : dexCodeInstructionSets) {
390 pw.println("Instruction Set: " + instructionSet);
391 pw.increaseIndent();
392 for (String path : paths) {
393 String status = null;
394 try {
395 status = DexFile.getDexFileStatus(path, instructionSet);
396 } catch (IOException ioe) {
397 status = "[Exception]: " + ioe.getMessage();
398 }
399 pw.println("path: " + path);
400 pw.println("status: " + status);
401 }
402 pw.decreaseIndent();
403 }
404 }
405
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800406 /**
407 * Returns the compiler filter that should be used to optimize the package code.
408 * The target filter will be updated if the package code is used by other apps
409 * or if it has the safe mode flag set.
410 */
Calin Juravlec22c30e2017-01-16 19:18:48 -0800411 private String getRealCompilerFilter(ApplicationInfo info, String targetCompilerFilter,
412 boolean isUsedByOtherApps) {
413 int flags = info.flags;
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800414 boolean vmSafeMode = (flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0;
Mathieu Chartier41e4a372016-09-02 16:36:42 -0700415 if (vmSafeMode) {
Nicolas Geoffrayd093b202017-05-03 13:11:58 +0100416 return getSafeModeCompilerFilter(targetCompilerFilter);
Mathieu Chartier41e4a372016-09-02 16:36:42 -0700417 }
418
Calin Juravlec22c30e2017-01-16 19:18:48 -0800419 if (isProfileGuidedCompilerFilter(targetCompilerFilter) && isUsedByOtherApps) {
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800420 // If the dex files is used by other apps, we cannot use profile-guided compilation.
421 return getNonProfileGuidedCompilerFilter(targetCompilerFilter);
Andreas Gampebdd30d82016-03-20 11:32:11 -0700422 }
423
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800424 return targetCompilerFilter;
Fyodor Kupolov74876572015-02-23 17:14:45 -0800425 }
426
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800427 /**
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800428 * Computes the dex flags that needs to be pass to installd for the given package and compiler
429 * filter.
430 */
Nicolas Geoffray3b4359a2017-05-25 13:53:22 +0100431 private int getDexFlags(PackageParser.Package pkg, String compilerFilter,
432 boolean bootComplete) {
433 return getDexFlags(pkg.applicationInfo, compilerFilter, bootComplete);
Calin Juravlec22c30e2017-01-16 19:18:48 -0800434 }
435
Nicolas Geoffray3b4359a2017-05-25 13:53:22 +0100436 private int getDexFlags(ApplicationInfo info, String compilerFilter, boolean bootComplete) {
Calin Juravlec22c30e2017-01-16 19:18:48 -0800437 int flags = info.flags;
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800438 boolean debuggable = (flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
439 // Profile guide compiled oat files should not be public.
440 boolean isProfileGuidedFilter = isProfileGuidedCompilerFilter(compilerFilter);
Calin Juravlec22c30e2017-01-16 19:18:48 -0800441 boolean isPublic = !info.isForwardLocked() && !isProfileGuidedFilter;
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800442 int profileFlag = isProfileGuidedFilter ? DEXOPT_PROFILE_GUIDED : 0;
443 int dexFlags =
444 (isPublic ? DEXOPT_PUBLIC : 0)
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800445 | (debuggable ? DEXOPT_DEBUGGABLE : 0)
446 | profileFlag
Nicolas Geoffray3b4359a2017-05-25 13:53:22 +0100447 | (bootComplete ? DEXOPT_BOOTCOMPLETE : 0);
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800448 return adjustDexoptFlags(dexFlags);
449 }
450
451 /**
452 * Assesses if there's a need to perform dexopt on {@code path} for the given
453 * configuration (isa, compiler filter, profile).
454 */
455 private int getDexoptNeeded(String path, String isa, String compilerFilter,
Shubham Ajmera246dccf2017-05-24 17:46:36 -0700456 boolean newProfile, boolean downgrade) {
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800457 int dexoptNeeded;
458 try {
Shubham Ajmera246dccf2017-05-24 17:46:36 -0700459 dexoptNeeded = DexFile.getDexOptNeeded(path, isa, compilerFilter, newProfile,
460 downgrade);
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800461 } catch (IOException ioe) {
462 Slog.w(TAG, "IOException reading apk: " + path, ioe);
463 return DEX_OPT_FAILED;
464 }
465 return adjustDexoptNeeded(dexoptNeeded);
466 }
467
468 /**
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800469 * Checks if there is an update on the profile information of the {@code pkg}.
470 * If the compiler filter is not profile guided the method returns false.
471 *
472 * Note that this is a "destructive" operation with side effects. Under the hood the
473 * current profile and the reference profile will be merged and subsequent calls
474 * may return a different result.
475 */
476 private boolean isProfileUpdated(PackageParser.Package pkg, int uid, String compilerFilter) {
477 // Check if we are allowed to merge and if the compiler filter is profile guided.
478 if (!isProfileGuidedCompilerFilter(compilerFilter)) {
479 return false;
480 }
481 // Merge profiles. It returns whether or not there was an updated in the profile info.
482 try {
483 return mInstaller.mergeProfiles(uid, pkg.packageName);
484 } catch (InstallerException e) {
485 Slog.w(TAG, "Failed to merge profiles", e);
486 }
487 return false;
488 }
489
490 /**
491 * Creates oat dir for the specified package if needed and supported.
492 * In certain cases oat directory
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800493 * <strong>cannot</strong> be created:
494 * <ul>
495 * <li>{@code pkg} is a system app, which is not updated.</li>
496 * <li>Package location is not a directory, i.e. monolithic install.</li>
497 * </ul>
498 *
Richard Uhler7b08b352015-03-25 16:25:57 -0700499 * @return Absolute path to the oat directory or null, if oat directory
500 * cannot be created.
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800501 */
502 @Nullable
Todd Kennedy27c24fb2015-09-17 16:49:25 -0700503 private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) {
Fyodor Kupolovebcac162015-09-09 15:56:45 -0700504 if (!pkg.canHaveOatDir()) {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800505 return null;
506 }
507 File codePath = new File(pkg.codePath);
508 if (codePath.isDirectory()) {
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800509 // TODO(calin): why do we create this only if the codePath is a directory? (i.e for
510 // cluster packages). It seems that the logic for the folder creation is
511 // split between installd and here.
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800512 File oatDir = getOatDir(codePath);
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700513 try {
Andreas Gampea8908752015-11-10 08:58:14 -0800514 mInstaller.createOatDir(oatDir.getAbsolutePath(), dexInstructionSet);
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700515 } catch (InstallerException e) {
516 Slog.w(TAG, "Failed to create oat dir", e);
517 return null;
518 }
Richard Uhler7b08b352015-03-25 16:25:57 -0700519 return oatDir.getAbsolutePath();
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800520 }
521 return null;
522 }
523
524 static File getOatDir(File codePath) {
525 return new File(codePath, OAT_DIR_NAME);
526 }
527
Fyodor Kupolova627c092015-05-05 18:44:39 -0700528 void systemReady() {
529 mSystemReady = true;
530 }
Andreas Gampea8908752015-11-10 08:58:14 -0800531
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800532 private String printDexoptFlags(int flags) {
533 ArrayList<String> flagsList = new ArrayList<>();
534
535 if ((flags & DEXOPT_BOOTCOMPLETE) == DEXOPT_BOOTCOMPLETE) {
536 flagsList.add("boot_complete");
537 }
538 if ((flags & DEXOPT_DEBUGGABLE) == DEXOPT_DEBUGGABLE) {
539 flagsList.add("debuggable");
540 }
541 if ((flags & DEXOPT_PROFILE_GUIDED) == DEXOPT_PROFILE_GUIDED) {
542 flagsList.add("profile_guided");
543 }
544 if ((flags & DEXOPT_PUBLIC) == DEXOPT_PUBLIC) {
545 flagsList.add("public");
546 }
Calin Juravlec22c30e2017-01-16 19:18:48 -0800547 if ((flags & DEXOPT_SECONDARY_DEX) == DEXOPT_SECONDARY_DEX) {
548 flagsList.add("secondary");
549 }
550 if ((flags & DEXOPT_FORCE) == DEXOPT_FORCE) {
551 flagsList.add("force");
552 }
553 if ((flags & DEXOPT_STORAGE_CE) == DEXOPT_STORAGE_CE) {
554 flagsList.add("storage_ce");
555 }
556 if ((flags & DEXOPT_STORAGE_DE) == DEXOPT_STORAGE_DE) {
557 flagsList.add("storage_de");
558 }
559
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800560 return String.join(",", flagsList);
561 }
562
Andreas Gampea8908752015-11-10 08:58:14 -0800563 /**
564 * A specialized PackageDexOptimizer that overrides already-installed checks, forcing a
565 * dexopt path.
566 */
567 public static class ForcedUpdatePackageDexOptimizer extends PackageDexOptimizer {
568
569 public ForcedUpdatePackageDexOptimizer(Installer installer, Object installLock,
570 Context context, String wakeLockTag) {
571 super(installer, installLock, context, wakeLockTag);
572 }
573
574 public ForcedUpdatePackageDexOptimizer(PackageDexOptimizer from) {
575 super(from);
576 }
577
578 @Override
Andreas Gampea8908752015-11-10 08:58:14 -0800579 protected int adjustDexoptNeeded(int dexoptNeeded) {
Nicolas Geoffray96d12a92017-05-03 11:51:53 +0100580 if (dexoptNeeded == DexFile.NO_DEXOPT_NEEDED) {
581 // Ensure compilation by pretending a compiler filter change on the
582 // apk/odex location (the reason for the '-'. A positive value means
583 // the 'oat' location).
584 return -DexFile.DEX2OAT_FOR_FILTER;
585 }
586 return dexoptNeeded;
Andreas Gampea8908752015-11-10 08:58:14 -0800587 }
Calin Juravlec22c30e2017-01-16 19:18:48 -0800588
589 @Override
590 protected int adjustDexoptFlags(int flags) {
591 // Add DEXOPT_FORCE flag to signal installd that it should force compilation
592 // and discard dexoptanalyzer result.
593 return flags | DEXOPT_FORCE;
594 }
Andreas Gampea8908752015-11-10 08:58:14 -0800595 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800596}