blob: 49639b04122378d51c1bb062d1a7ed67c5be1fbd [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;
Calin Juravle72183632017-01-16 19:18:48 -080022import android.content.pm.PackageInfo;
Fyodor Kupolov74876572015-02-23 17:14:45 -080023import android.content.pm.PackageParser;
Calin Juravled479b522016-02-24 16:22:03 +000024import android.os.Environment;
Fyodor Kupolova627c092015-05-05 18:44:39 -070025import android.os.PowerManager;
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 Juravlebb105252017-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;
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -070034
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080035import java.io.File;
Fyodor Kupolov74876572015-02-23 17:14:45 -080036import java.io.IOException;
Calin Juravlebb105252017-01-04 13:22:14 -080037import java.util.ArrayList;
Fyodor Kupolov74876572015-02-23 17:14:45 -080038import java.util.List;
Calin Juravle72183632017-01-16 19:18:48 -080039import java.util.Set;
Fyodor Kupolov74876572015-02-23 17:14:45 -080040
41import dalvik.system.DexFile;
Fyodor Kupolov74876572015-02-23 17:14:45 -080042
Todd Kennedyfa54ab72015-09-25 07:46:12 -070043import static com.android.server.pm.Installer.DEXOPT_BOOTCOMPLETE;
44import static com.android.server.pm.Installer.DEXOPT_DEBUGGABLE;
Andreas Gampebdd30d82016-03-20 11:32:11 -070045import static com.android.server.pm.Installer.DEXOPT_PROFILE_GUIDED;
Todd Kennedyfa54ab72015-09-25 07:46:12 -070046import static com.android.server.pm.Installer.DEXOPT_PUBLIC;
Calin Juravle72183632017-01-16 19:18:48 -080047import static com.android.server.pm.Installer.DEXOPT_SECONDARY_DEX;
48import static com.android.server.pm.Installer.DEXOPT_FORCE;
49import static com.android.server.pm.Installer.DEXOPT_STORAGE_CE;
50import static com.android.server.pm.Installer.DEXOPT_STORAGE_DE;
Fyodor Kupolov74876572015-02-23 17:14:45 -080051import static com.android.server.pm.InstructionSets.getAppDexInstructionSets;
52import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets;
Calin Juravlebb105252017-01-04 13:22:14 -080053
Nicolas Geoffrayd093b202017-05-03 13:11:58 +010054import static dalvik.system.DexFile.getNonProfileGuidedCompilerFilter;
55import static dalvik.system.DexFile.getSafeModeCompilerFilter;
Calin Juravlebb105252017-01-04 13:22:14 -080056import static dalvik.system.DexFile.isProfileGuidedCompilerFilter;
Fyodor Kupolov74876572015-02-23 17:14:45 -080057
58/**
59 * Helper class for running dexopt command on packages.
60 */
Calin Juravle72183632017-01-16 19:18:48 -080061public class PackageDexOptimizer {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080062 private static final String TAG = "PackageManager.DexOptimizer";
63 static final String OAT_DIR_NAME = "oat";
64 // TODO b/19550105 Remove error codes and use exceptions
Calin Juravle72183632017-01-16 19:18:48 -080065 public static final int DEX_OPT_SKIPPED = 0;
66 public static final int DEX_OPT_PERFORMED = 1;
67 public static final int DEX_OPT_FAILED = -1;
Fyodor Kupolov74876572015-02-23 17:14:45 -080068
Calin Juravlef8afcdc2017-02-14 17:53:13 -080069 /** Special library name that skips shared libraries check during compilation. */
70 public static final String SKIP_SHARED_LIBRARY_CHECK = "&";
71
Andreas Gampea8908752015-11-10 08:58:14 -080072 private final Installer mInstaller;
73 private final Object mInstallLock;
Fyodor Kupolov74876572015-02-23 17:14:45 -080074
Fyodor Kupolova627c092015-05-05 18:44:39 -070075 private final PowerManager.WakeLock mDexoptWakeLock;
76 private volatile boolean mSystemReady;
77
Andreas Gampea8908752015-11-10 08:58:14 -080078 PackageDexOptimizer(Installer installer, Object installLock, Context context,
79 String wakeLockTag) {
80 this.mInstaller = installer;
81 this.mInstallLock = installLock;
82
83 PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
84 mDexoptWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakeLockTag);
85 }
86
87 protected PackageDexOptimizer(PackageDexOptimizer from) {
88 this.mInstaller = from.mInstaller;
89 this.mInstallLock = from.mInstallLock;
90 this.mDexoptWakeLock = from.mDexoptWakeLock;
91 this.mSystemReady = from.mSystemReady;
Fyodor Kupolov74876572015-02-23 17:14:45 -080092 }
93
Calin Juravledb4a79a2015-12-23 18:55:08 +020094 static boolean canOptimizePackage(PackageParser.Package pkg) {
Calin Juravle6dfd83d2016-01-29 18:23:57 +000095 return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0;
Calin Juravledb4a79a2015-12-23 18:55:08 +020096 }
97
Fyodor Kupolov74876572015-02-23 17:14:45 -080098 /**
99 * Performs dexopt on all code paths and libraries of the specified package for specified
100 * instruction sets.
101 *
Andreas Gampea8908752015-11-10 08:58:14 -0800102 * <p>Calls to {@link com.android.server.pm.Installer#dexopt} on {@link #mInstaller} are
103 * synchronized on {@link #mInstallLock}.
Fyodor Kupolov74876572015-02-23 17:14:45 -0800104 */
Jeff Haoc7b94822016-03-16 15:56:07 -0700105 int performDexOpt(PackageParser.Package pkg, String[] sharedLibraries,
Andreas Gampe7e619a92016-07-12 22:42:41 -0700106 String[] instructionSets, boolean checkProfiles, String targetCompilationFilter,
Nicolas Geoffray3b4359a2017-05-25 13:53:22 +0100107 CompilerStats.PackageStats packageStats, boolean isUsedByOtherApps,
108 boolean bootComplete) {
Calin Juravlebb105252017-01-04 13:22:14 -0800109 if (!canOptimizePackage(pkg)) {
110 return DEX_OPT_SKIPPED;
111 }
Andreas Gampea8908752015-11-10 08:58:14 -0800112 synchronized (mInstallLock) {
Calin Juravle72183632017-01-16 19:18:48 -0800113 // During boot the system doesn't need to instantiate and obtain a wake lock.
114 // PowerManager might not be ready, but that doesn't mean that we can't proceed with
115 // dexopt.
Fyodor Kupolova627c092015-05-05 18:44:39 -0700116 final boolean useLock = mSystemReady;
117 if (useLock) {
118 mDexoptWakeLock.setWorkSource(new WorkSource(pkg.applicationInfo.uid));
119 mDexoptWakeLock.acquire();
120 }
121 try {
Jeff Haoc7b94822016-03-16 15:56:07 -0700122 return performDexOptLI(pkg, sharedLibraries, instructionSets, checkProfiles,
Nicolas Geoffray3b4359a2017-05-25 13:53:22 +0100123 targetCompilationFilter, packageStats, isUsedByOtherApps, bootComplete);
Fyodor Kupolova627c092015-05-05 18:44:39 -0700124 } finally {
125 if (useLock) {
126 mDexoptWakeLock.release();
127 }
128 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800129 }
130 }
131
Andreas Gampea8908752015-11-10 08:58:14 -0800132 /**
Calin Juravlebb105252017-01-04 13:22:14 -0800133 * Performs dexopt on all code paths of the given package.
134 * It assumes the install lock is held.
135 */
136 @GuardedBy("mInstallLock")
137 private int performDexOptLI(PackageParser.Package pkg, String[] sharedLibraries,
138 String[] targetInstructionSets, boolean checkForProfileUpdates,
Calin Juravlec6494492017-03-01 19:55:35 -0800139 String targetCompilerFilter, CompilerStats.PackageStats packageStats,
Nicolas Geoffray3b4359a2017-05-25 13:53:22 +0100140 boolean isUsedByOtherApps, boolean bootComplete) {
Calin Juravlebb105252017-01-04 13:22:14 -0800141 final String[] instructionSets = targetInstructionSets != null ?
142 targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo);
143 final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
144 final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly();
145 final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid);
146
Calin Juravle72183632017-01-16 19:18:48 -0800147 final String compilerFilter = getRealCompilerFilter(pkg.applicationInfo,
Calin Juravlec6494492017-03-01 19:55:35 -0800148 targetCompilerFilter, isUsedByOtherApps);
Calin Juravlebb105252017-01-04 13:22:14 -0800149 final boolean profileUpdated = checkForProfileUpdates &&
150 isProfileUpdated(pkg, sharedGid, compilerFilter);
Calin Juravle72183632017-01-16 19:18:48 -0800151
Calin Juravlebb105252017-01-04 13:22:14 -0800152 // TODO(calin,jeffhao): shared library paths should be adjusted to include previous code
153 // paths (b/34169257).
Jeff Hao7da652f2017-04-05 17:09:59 -0700154 String sharedLibrariesPath = getSharedLibrariesPath(sharedLibraries);
155 // Get the dexopt flags after getRealCompilerFilter to make sure we get the correct flags.
Nicolas Geoffray3b4359a2017-05-25 13:53:22 +0100156 final int dexoptFlags = getDexFlags(pkg, compilerFilter, bootComplete);
Calin Juravlebb105252017-01-04 13:22:14 -0800157
158 int result = DEX_OPT_SKIPPED;
Jeff Hao7da652f2017-04-05 17:09:59 -0700159 // TODO: Iterate based on dependency hierarchy (currently alphabetically by name)
160 // (b/37480811).
161 String basePathCheck = null;
Calin Juravlebb105252017-01-04 13:22:14 -0800162 for (String path : paths) {
163 for (String dexCodeIsa : dexCodeInstructionSets) {
164 int newResult = dexOptPath(pkg, path, dexCodeIsa, compilerFilter, profileUpdated,
165 sharedLibrariesPath, dexoptFlags, sharedGid, packageStats);
166 // The end result is:
167 // - FAILED if any path failed,
168 // - PERFORMED if at least one path needed compilation,
169 // - SKIPPED when all paths are up to date
170 if ((result != DEX_OPT_FAILED) && (newResult != DEX_OPT_SKIPPED)) {
171 result = newResult;
172 }
Jeff Hao7da652f2017-04-05 17:09:59 -0700173 // Add the relative path of code we just compiled to the shared libraries.
174 int slashIndex = path.lastIndexOf('/') + 1;
175 String relativePath = path.substring(slashIndex);
176 if (sharedLibrariesPath == null) {
177 sharedLibrariesPath = relativePath;
178 } else {
179 sharedLibrariesPath += ":" + relativePath;
180 }
181 // Sanity check that the base paths are all the same.
182 String basePath = path.substring(0, slashIndex);
183 if (basePathCheck == null) {
184 basePathCheck = basePath;
185 } else if (!basePath.equals(basePathCheck)) {
186 Slog.wtf(TAG, "Split paths have different base paths: " + basePath + " and " +
187 basePathCheck);
188 }
Calin Juravlebb105252017-01-04 13:22:14 -0800189 }
190 }
191 return result;
192 }
193
194 /**
195 * Performs dexopt on the {@code path} belonging to the package {@code pkg}.
196 *
197 * @return
198 * DEX_OPT_FAILED if there was any exception during dexopt
199 * DEX_OPT_PERFORMED if dexopt was performed successfully on the given path.
200 * DEX_OPT_SKIPPED if the path does not need to be deopt-ed.
201 */
202 @GuardedBy("mInstallLock")
203 private int dexOptPath(PackageParser.Package pkg, String path, String isa,
204 String compilerFilter, boolean profileUpdated, String sharedLibrariesPath,
205 int dexoptFlags, int uid, CompilerStats.PackageStats packageStats) {
206 int dexoptNeeded = getDexoptNeeded(path, isa, compilerFilter, profileUpdated);
207 if (Math.abs(dexoptNeeded) == DexFile.NO_DEXOPT_NEEDED) {
208 return DEX_OPT_SKIPPED;
209 }
210
211 // TODO(calin): there's no need to try to create the oat dir over and over again,
212 // especially since it involve an extra installd call. We should create
213 // if (if supported) on the fly during the dexopt call.
214 String oatDir = createOatDirIfSupported(pkg, isa);
215
216 Log.i(TAG, "Running dexopt (dexoptNeeded=" + dexoptNeeded + ") on: " + path
217 + " pkg=" + pkg.applicationInfo.packageName + " isa=" + isa
218 + " dexoptFlags=" + printDexoptFlags(dexoptFlags)
219 + " target-filter=" + compilerFilter + " oatDir=" + oatDir
220 + " sharedLibraries=" + sharedLibrariesPath);
221
222 try {
223 long startTime = System.currentTimeMillis();
224
225 mInstaller.dexopt(path, uid, pkg.packageName, isa, dexoptNeeded, oatDir, dexoptFlags,
226 compilerFilter, pkg.volumeUuid, sharedLibrariesPath);
227
228 if (packageStats != null) {
229 long endTime = System.currentTimeMillis();
230 packageStats.setCompileTime(path, (int)(endTime - startTime));
231 }
232 return DEX_OPT_PERFORMED;
233 } catch (InstallerException e) {
234 Slog.w(TAG, "Failed to dexopt", e);
235 return DEX_OPT_FAILED;
236 }
237 }
238
239 /**
Calin Juravle72183632017-01-16 19:18:48 -0800240 * Performs dexopt on the secondary dex {@code path} belonging to the app {@code info}.
241 *
242 * @return
243 * DEX_OPT_FAILED if there was any exception during dexopt
244 * DEX_OPT_PERFORMED if dexopt was performed successfully on the given path.
245 * NOTE that DEX_OPT_PERFORMED for secondary dex files includes the case when the dex file
246 * didn't need an update. That's because at the moment we don't get more than success/failure
247 * from installd.
248 *
249 * TODO(calin): Consider adding return codes to installd dexopt invocation (rather than
250 * throwing exceptions). Or maybe make a separate call to installd to get DexOptNeeded, though
251 * that seems wasteful.
252 */
253 public int dexOptSecondaryDexPath(ApplicationInfo info, String path, Set<String> isas,
254 String compilerFilter, boolean isUsedByOtherApps) {
255 synchronized (mInstallLock) {
256 // During boot the system doesn't need to instantiate and obtain a wake lock.
257 // PowerManager might not be ready, but that doesn't mean that we can't proceed with
258 // dexopt.
259 final boolean useLock = mSystemReady;
260 if (useLock) {
261 mDexoptWakeLock.setWorkSource(new WorkSource(info.uid));
262 mDexoptWakeLock.acquire();
263 }
264 try {
265 return dexOptSecondaryDexPathLI(info, path, isas, compilerFilter,
266 isUsedByOtherApps);
267 } finally {
268 if (useLock) {
269 mDexoptWakeLock.release();
270 }
271 }
272 }
273 }
274
275 @GuardedBy("mInstallLock")
276 private int dexOptSecondaryDexPathLI(ApplicationInfo info, String path, Set<String> isas,
277 String compilerFilter, boolean isUsedByOtherApps) {
Nicolas Geoffray3b4359a2017-05-25 13:53:22 +0100278 // Secondary dex files are currently not compiled at boot.
279 int dexoptFlags = getDexFlags(info, compilerFilter, /* bootComplete */ true)
280 | DEXOPT_SECONDARY_DEX;
Calin Juravle72183632017-01-16 19:18:48 -0800281 // Check the app storage and add the appropriate flags.
282 if (info.dataDir.equals(info.deviceProtectedDataDir)) {
283 dexoptFlags |= DEXOPT_STORAGE_DE;
284 } else if (info.dataDir.equals(info.credentialProtectedDataDir)) {
285 dexoptFlags |= DEXOPT_STORAGE_CE;
286 } else {
287 Slog.e(TAG, "Could not infer CE/DE storage for package " + info.packageName);
288 return DEX_OPT_FAILED;
289 }
290 compilerFilter = getRealCompilerFilter(info, compilerFilter, isUsedByOtherApps);
291 Log.d(TAG, "Running dexopt on: " + path
292 + " pkg=" + info.packageName + " isa=" + isas
293 + " dexoptFlags=" + printDexoptFlags(dexoptFlags)
294 + " target-filter=" + compilerFilter);
295
296 try {
297 for (String isa : isas) {
298 // Reuse the same dexopt path as for the primary apks. We don't need all the
299 // arguments as some (dexopNeeded and oatDir) will be computed by installd because
300 // system server cannot read untrusted app content.
301 // TODO(calin): maybe add a separate call.
302 mInstaller.dexopt(path, info.uid, info.packageName, isa, /*dexoptNeeded*/ 0,
303 /*oatDir*/ null, dexoptFlags,
Calin Juravlef8afcdc2017-02-14 17:53:13 -0800304 compilerFilter, info.volumeUuid, SKIP_SHARED_LIBRARY_CHECK);
Calin Juravle72183632017-01-16 19:18:48 -0800305 }
306
307 return DEX_OPT_PERFORMED;
308 } catch (InstallerException e) {
309 Slog.w(TAG, "Failed to dexopt", e);
310 return DEX_OPT_FAILED;
311 }
312 }
313
314 /**
Andreas Gampea8908752015-11-10 08:58:14 -0800315 * Adjust the given dexopt-needed value. Can be overridden to influence the decision to
316 * optimize or not (and in what way).
317 */
318 protected int adjustDexoptNeeded(int dexoptNeeded) {
319 return dexoptNeeded;
320 }
321
322 /**
323 * Adjust the given dexopt flags that will be passed to the installer.
324 */
325 protected int adjustDexoptFlags(int dexoptFlags) {
326 return dexoptFlags;
327 }
328
Narayan Kamath88eea9e2016-05-02 14:44:31 +0100329 /**
330 * Dumps the dexopt state of the given package {@code pkg} to the given {@code PrintWriter}.
331 */
332 void dumpDexoptState(IndentingPrintWriter pw, PackageParser.Package pkg) {
333 final String[] instructionSets = getAppDexInstructionSets(pkg.applicationInfo);
334 final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
335
336 final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly();
337
338 for (String instructionSet : dexCodeInstructionSets) {
339 pw.println("Instruction Set: " + instructionSet);
340 pw.increaseIndent();
341 for (String path : paths) {
342 String status = null;
343 try {
344 status = DexFile.getDexFileStatus(path, instructionSet);
345 } catch (IOException ioe) {
346 status = "[Exception]: " + ioe.getMessage();
347 }
348 pw.println("path: " + path);
349 pw.println("status: " + status);
350 }
351 pw.decreaseIndent();
352 }
353 }
354
Calin Juravlebb105252017-01-04 13:22:14 -0800355 /**
356 * Returns the compiler filter that should be used to optimize the package code.
357 * The target filter will be updated if the package code is used by other apps
358 * or if it has the safe mode flag set.
359 */
Calin Juravle72183632017-01-16 19:18:48 -0800360 private String getRealCompilerFilter(ApplicationInfo info, String targetCompilerFilter,
361 boolean isUsedByOtherApps) {
362 int flags = info.flags;
Calin Juravlebb105252017-01-04 13:22:14 -0800363 boolean vmSafeMode = (flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0;
Mathieu Chartier41e4a372016-09-02 16:36:42 -0700364 if (vmSafeMode) {
Nicolas Geoffrayd093b202017-05-03 13:11:58 +0100365 return getSafeModeCompilerFilter(targetCompilerFilter);
Mathieu Chartier41e4a372016-09-02 16:36:42 -0700366 }
367
Calin Juravle72183632017-01-16 19:18:48 -0800368 if (isProfileGuidedCompilerFilter(targetCompilerFilter) && isUsedByOtherApps) {
Calin Juravlebb105252017-01-04 13:22:14 -0800369 // If the dex files is used by other apps, we cannot use profile-guided compilation.
370 return getNonProfileGuidedCompilerFilter(targetCompilerFilter);
Andreas Gampebdd30d82016-03-20 11:32:11 -0700371 }
372
Calin Juravlebb105252017-01-04 13:22:14 -0800373 return targetCompilerFilter;
Fyodor Kupolov74876572015-02-23 17:14:45 -0800374 }
375
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800376 /**
Calin Juravlebb105252017-01-04 13:22:14 -0800377 * Computes the dex flags that needs to be pass to installd for the given package and compiler
378 * filter.
379 */
Nicolas Geoffray3b4359a2017-05-25 13:53:22 +0100380 private int getDexFlags(PackageParser.Package pkg, String compilerFilter,
381 boolean bootComplete) {
382 return getDexFlags(pkg.applicationInfo, compilerFilter, bootComplete);
Calin Juravle72183632017-01-16 19:18:48 -0800383 }
384
Nicolas Geoffray3b4359a2017-05-25 13:53:22 +0100385 private int getDexFlags(ApplicationInfo info, String compilerFilter, boolean bootComplete) {
Calin Juravle72183632017-01-16 19:18:48 -0800386 int flags = info.flags;
Calin Juravlebb105252017-01-04 13:22:14 -0800387 boolean debuggable = (flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
388 // Profile guide compiled oat files should not be public.
389 boolean isProfileGuidedFilter = isProfileGuidedCompilerFilter(compilerFilter);
Calin Juravle72183632017-01-16 19:18:48 -0800390 boolean isPublic = !info.isForwardLocked() && !isProfileGuidedFilter;
Calin Juravlebb105252017-01-04 13:22:14 -0800391 int profileFlag = isProfileGuidedFilter ? DEXOPT_PROFILE_GUIDED : 0;
392 int dexFlags =
393 (isPublic ? DEXOPT_PUBLIC : 0)
Calin Juravlebb105252017-01-04 13:22:14 -0800394 | (debuggable ? DEXOPT_DEBUGGABLE : 0)
395 | profileFlag
Nicolas Geoffray3b4359a2017-05-25 13:53:22 +0100396 | (bootComplete ? DEXOPT_BOOTCOMPLETE : 0);
Calin Juravlebb105252017-01-04 13:22:14 -0800397 return adjustDexoptFlags(dexFlags);
398 }
399
400 /**
401 * Assesses if there's a need to perform dexopt on {@code path} for the given
402 * configuration (isa, compiler filter, profile).
403 */
404 private int getDexoptNeeded(String path, String isa, String compilerFilter,
405 boolean newProfile) {
406 int dexoptNeeded;
407 try {
408 dexoptNeeded = DexFile.getDexOptNeeded(path, isa, compilerFilter, newProfile);
409 } catch (IOException ioe) {
410 Slog.w(TAG, "IOException reading apk: " + path, ioe);
411 return DEX_OPT_FAILED;
412 }
413 return adjustDexoptNeeded(dexoptNeeded);
414 }
415
416 /**
417 * Computes the shared libraries path that should be passed to dexopt.
418 */
419 private String getSharedLibrariesPath(String[] sharedLibraries) {
420 if (sharedLibraries == null || sharedLibraries.length == 0) {
421 return null;
422 }
423 StringBuilder sb = new StringBuilder();
424 for (String lib : sharedLibraries) {
425 if (sb.length() != 0) {
426 sb.append(":");
427 }
428 sb.append(lib);
429 }
430 return sb.toString();
431 }
432
433 /**
434 * Checks if there is an update on the profile information of the {@code pkg}.
435 * If the compiler filter is not profile guided the method returns false.
436 *
437 * Note that this is a "destructive" operation with side effects. Under the hood the
438 * current profile and the reference profile will be merged and subsequent calls
439 * may return a different result.
440 */
441 private boolean isProfileUpdated(PackageParser.Package pkg, int uid, String compilerFilter) {
442 // Check if we are allowed to merge and if the compiler filter is profile guided.
443 if (!isProfileGuidedCompilerFilter(compilerFilter)) {
444 return false;
445 }
446 // Merge profiles. It returns whether or not there was an updated in the profile info.
447 try {
448 return mInstaller.mergeProfiles(uid, pkg.packageName);
449 } catch (InstallerException e) {
450 Slog.w(TAG, "Failed to merge profiles", e);
451 }
452 return false;
453 }
454
455 /**
456 * Creates oat dir for the specified package if needed and supported.
457 * In certain cases oat directory
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800458 * <strong>cannot</strong> be created:
459 * <ul>
460 * <li>{@code pkg} is a system app, which is not updated.</li>
461 * <li>Package location is not a directory, i.e. monolithic install.</li>
462 * </ul>
463 *
Richard Uhler7b08b352015-03-25 16:25:57 -0700464 * @return Absolute path to the oat directory or null, if oat directory
465 * cannot be created.
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800466 */
467 @Nullable
Todd Kennedy27c24fb2015-09-17 16:49:25 -0700468 private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) {
Fyodor Kupolovebcac162015-09-09 15:56:45 -0700469 if (!pkg.canHaveOatDir()) {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800470 return null;
471 }
472 File codePath = new File(pkg.codePath);
473 if (codePath.isDirectory()) {
Calin Juravlebb105252017-01-04 13:22:14 -0800474 // TODO(calin): why do we create this only if the codePath is a directory? (i.e for
475 // cluster packages). It seems that the logic for the folder creation is
476 // split between installd and here.
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800477 File oatDir = getOatDir(codePath);
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700478 try {
Andreas Gampea8908752015-11-10 08:58:14 -0800479 mInstaller.createOatDir(oatDir.getAbsolutePath(), dexInstructionSet);
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700480 } catch (InstallerException e) {
481 Slog.w(TAG, "Failed to create oat dir", e);
482 return null;
483 }
Richard Uhler7b08b352015-03-25 16:25:57 -0700484 return oatDir.getAbsolutePath();
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800485 }
486 return null;
487 }
488
489 static File getOatDir(File codePath) {
490 return new File(codePath, OAT_DIR_NAME);
491 }
492
Fyodor Kupolova627c092015-05-05 18:44:39 -0700493 void systemReady() {
494 mSystemReady = true;
495 }
Andreas Gampea8908752015-11-10 08:58:14 -0800496
Calin Juravlebb105252017-01-04 13:22:14 -0800497 private String printDexoptFlags(int flags) {
498 ArrayList<String> flagsList = new ArrayList<>();
499
500 if ((flags & DEXOPT_BOOTCOMPLETE) == DEXOPT_BOOTCOMPLETE) {
501 flagsList.add("boot_complete");
502 }
503 if ((flags & DEXOPT_DEBUGGABLE) == DEXOPT_DEBUGGABLE) {
504 flagsList.add("debuggable");
505 }
506 if ((flags & DEXOPT_PROFILE_GUIDED) == DEXOPT_PROFILE_GUIDED) {
507 flagsList.add("profile_guided");
508 }
509 if ((flags & DEXOPT_PUBLIC) == DEXOPT_PUBLIC) {
510 flagsList.add("public");
511 }
Calin Juravle72183632017-01-16 19:18:48 -0800512 if ((flags & DEXOPT_SECONDARY_DEX) == DEXOPT_SECONDARY_DEX) {
513 flagsList.add("secondary");
514 }
515 if ((flags & DEXOPT_FORCE) == DEXOPT_FORCE) {
516 flagsList.add("force");
517 }
518 if ((flags & DEXOPT_STORAGE_CE) == DEXOPT_STORAGE_CE) {
519 flagsList.add("storage_ce");
520 }
521 if ((flags & DEXOPT_STORAGE_DE) == DEXOPT_STORAGE_DE) {
522 flagsList.add("storage_de");
523 }
524
Calin Juravlebb105252017-01-04 13:22:14 -0800525 return String.join(",", flagsList);
526 }
527
Andreas Gampea8908752015-11-10 08:58:14 -0800528 /**
529 * A specialized PackageDexOptimizer that overrides already-installed checks, forcing a
530 * dexopt path.
531 */
532 public static class ForcedUpdatePackageDexOptimizer extends PackageDexOptimizer {
533
534 public ForcedUpdatePackageDexOptimizer(Installer installer, Object installLock,
535 Context context, String wakeLockTag) {
536 super(installer, installLock, context, wakeLockTag);
537 }
538
539 public ForcedUpdatePackageDexOptimizer(PackageDexOptimizer from) {
540 super(from);
541 }
542
543 @Override
Andreas Gampea8908752015-11-10 08:58:14 -0800544 protected int adjustDexoptNeeded(int dexoptNeeded) {
Nicolas Geoffray96d12a92017-05-03 11:51:53 +0100545 if (dexoptNeeded == DexFile.NO_DEXOPT_NEEDED) {
546 // Ensure compilation by pretending a compiler filter change on the
547 // apk/odex location (the reason for the '-'. A positive value means
548 // the 'oat' location).
549 return -DexFile.DEX2OAT_FOR_FILTER;
550 }
551 return dexoptNeeded;
Andreas Gampea8908752015-11-10 08:58:14 -0800552 }
Calin Juravle72183632017-01-16 19:18:48 -0800553
554 @Override
555 protected int adjustDexoptFlags(int flags) {
556 // Add DEXOPT_FORCE flag to signal installd that it should force compilation
557 // and discard dexoptanalyzer result.
558 return flags | DEXOPT_FORCE;
559 }
Andreas Gampea8908752015-11-10 08:58:14 -0800560 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800561}