PackageDexOptimizer.java revision 7da652fd43fcbeb3d62093259970f458c20ee076
1/*
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
19import android.annotation.Nullable;
20import android.content.Context;
21import android.content.pm.ApplicationInfo;
22import android.content.pm.PackageInfo;
23import android.content.pm.PackageParser;
24import android.os.Environment;
25import android.os.PowerManager;
26import android.os.UserHandle;
27import android.os.WorkSource;
28import android.util.Log;
29import android.util.Slog;
30
31import com.android.internal.annotations.GuardedBy;
32import com.android.internal.util.IndentingPrintWriter;
33import com.android.server.pm.Installer.InstallerException;
34
35import java.io.File;
36import java.io.IOException;
37import java.util.ArrayList;
38import java.util.List;
39import java.util.Set;
40
41import dalvik.system.DexFile;
42
43import static com.android.server.pm.Installer.DEXOPT_BOOTCOMPLETE;
44import static com.android.server.pm.Installer.DEXOPT_DEBUGGABLE;
45import static com.android.server.pm.Installer.DEXOPT_PROFILE_GUIDED;
46import static com.android.server.pm.Installer.DEXOPT_PUBLIC;
47import static com.android.server.pm.Installer.DEXOPT_SAFEMODE;
48import static com.android.server.pm.Installer.DEXOPT_SECONDARY_DEX;
49import static com.android.server.pm.Installer.DEXOPT_FORCE;
50import static com.android.server.pm.Installer.DEXOPT_STORAGE_CE;
51import static com.android.server.pm.Installer.DEXOPT_STORAGE_DE;
52import static com.android.server.pm.InstructionSets.getAppDexInstructionSets;
53import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets;
54
55import static com.android.server.pm.PackageManagerServiceCompilerMapping.getNonProfileGuidedCompilerFilter;
56import static dalvik.system.DexFile.isProfileGuidedCompilerFilter;
57
58/**
59 * Helper class for running dexopt command on packages.
60 */
61public class PackageDexOptimizer {
62    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
65    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;
68
69    /** Special library name that skips shared libraries check during compilation. */
70    public static final String SKIP_SHARED_LIBRARY_CHECK = "&";
71
72    private final Installer mInstaller;
73    private final Object mInstallLock;
74
75    private final PowerManager.WakeLock mDexoptWakeLock;
76    private volatile boolean mSystemReady;
77
78    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;
92    }
93
94    static boolean canOptimizePackage(PackageParser.Package pkg) {
95        return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0;
96    }
97
98    /**
99     * Performs dexopt on all code paths and libraries of the specified package for specified
100     * instruction sets.
101     *
102     * <p>Calls to {@link com.android.server.pm.Installer#dexopt} on {@link #mInstaller} are
103     * synchronized on {@link #mInstallLock}.
104     */
105    int performDexOpt(PackageParser.Package pkg, String[] sharedLibraries,
106            String[] instructionSets, boolean checkProfiles, String targetCompilationFilter,
107            CompilerStats.PackageStats packageStats, boolean isUsedByOtherApps) {
108        if (!canOptimizePackage(pkg)) {
109            return DEX_OPT_SKIPPED;
110        }
111        synchronized (mInstallLock) {
112            // During boot the system doesn't need to instantiate and obtain a wake lock.
113            // PowerManager might not be ready, but that doesn't mean that we can't proceed with
114            // dexopt.
115            final boolean useLock = mSystemReady;
116            if (useLock) {
117                mDexoptWakeLock.setWorkSource(new WorkSource(pkg.applicationInfo.uid));
118                mDexoptWakeLock.acquire();
119            }
120            try {
121                return performDexOptLI(pkg, sharedLibraries, instructionSets, checkProfiles,
122                        targetCompilationFilter, packageStats, isUsedByOtherApps);
123            } finally {
124                if (useLock) {
125                    mDexoptWakeLock.release();
126                }
127            }
128        }
129    }
130
131    /**
132     * 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,
137            String[] targetInstructionSets, boolean checkForProfileUpdates,
138            String targetCompilerFilter, CompilerStats.PackageStats packageStats,
139            boolean isUsedByOtherApps) {
140        final String[] instructionSets = targetInstructionSets != null ?
141                targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo);
142        final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
143        final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly();
144        final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid);
145
146        final String compilerFilter = getRealCompilerFilter(pkg.applicationInfo,
147                targetCompilerFilter, isUsedByOtherApps);
148        final boolean profileUpdated = checkForProfileUpdates &&
149                isProfileUpdated(pkg, sharedGid, compilerFilter);
150
151        // TODO(calin,jeffhao): shared library paths should be adjusted to include previous code
152        // paths (b/34169257).
153        String sharedLibrariesPath = getSharedLibrariesPath(sharedLibraries);
154        // Get the dexopt flags after getRealCompilerFilter to make sure we get the correct flags.
155        final int dexoptFlags = getDexFlags(pkg, compilerFilter);
156
157        int result = DEX_OPT_SKIPPED;
158        // TODO: Iterate based on dependency hierarchy (currently alphabetically by name)
159        // (b/37480811).
160        String basePathCheck = null;
161        for (String path : paths) {
162            for (String dexCodeIsa : dexCodeInstructionSets) {
163                int newResult = dexOptPath(pkg, path, dexCodeIsa, compilerFilter, profileUpdated,
164                        sharedLibrariesPath, dexoptFlags, sharedGid, packageStats);
165                // The end result is:
166                //  - FAILED if any path failed,
167                //  - PERFORMED if at least one path needed compilation,
168                //  - SKIPPED when all paths are up to date
169                if ((result != DEX_OPT_FAILED) && (newResult != DEX_OPT_SKIPPED)) {
170                    result = newResult;
171                }
172                // Add the relative path of code we just compiled to the shared libraries.
173                int slashIndex = path.lastIndexOf('/') + 1;
174                String relativePath = path.substring(slashIndex);
175                if (sharedLibrariesPath == null) {
176                    sharedLibrariesPath = relativePath;
177                } else {
178                    sharedLibrariesPath += ":" + relativePath;
179                }
180                // Sanity check that the base paths are all the same.
181                String basePath = path.substring(0, slashIndex);
182                if (basePathCheck == null) {
183                    basePathCheck = basePath;
184                } else if (!basePath.equals(basePathCheck)) {
185                    Slog.wtf(TAG, "Split paths have different base paths: " + basePath + " and " +
186                        basePathCheck);
187                }
188            }
189        }
190        return result;
191    }
192
193    /**
194     * Performs dexopt on the {@code path} belonging to the package {@code pkg}.
195     *
196     * @return
197     *      DEX_OPT_FAILED if there was any exception during dexopt
198     *      DEX_OPT_PERFORMED if dexopt was performed successfully on the given path.
199     *      DEX_OPT_SKIPPED if the path does not need to be deopt-ed.
200     */
201    @GuardedBy("mInstallLock")
202    private int dexOptPath(PackageParser.Package pkg, String path, String isa,
203            String compilerFilter, boolean profileUpdated, String sharedLibrariesPath,
204            int dexoptFlags, int uid, CompilerStats.PackageStats packageStats) {
205        int dexoptNeeded = getDexoptNeeded(path, isa, compilerFilter, profileUpdated);
206        if (Math.abs(dexoptNeeded) == DexFile.NO_DEXOPT_NEEDED) {
207            return DEX_OPT_SKIPPED;
208        }
209
210        // TODO(calin): there's no need to try to create the oat dir over and over again,
211        //              especially since it involve an extra installd call. We should create
212        //              if (if supported) on the fly during the dexopt call.
213        String oatDir = createOatDirIfSupported(pkg, isa);
214
215        Log.i(TAG, "Running dexopt (dexoptNeeded=" + dexoptNeeded + ") on: " + path
216                + " pkg=" + pkg.applicationInfo.packageName + " isa=" + isa
217                + " dexoptFlags=" + printDexoptFlags(dexoptFlags)
218                + " target-filter=" + compilerFilter + " oatDir=" + oatDir
219                + " sharedLibraries=" + sharedLibrariesPath);
220
221        try {
222            long startTime = System.currentTimeMillis();
223
224            mInstaller.dexopt(path, uid, pkg.packageName, isa, dexoptNeeded, oatDir, dexoptFlags,
225                    compilerFilter, pkg.volumeUuid, sharedLibrariesPath);
226
227            if (packageStats != null) {
228                long endTime = System.currentTimeMillis();
229                packageStats.setCompileTime(path, (int)(endTime - startTime));
230            }
231            return DEX_OPT_PERFORMED;
232        } catch (InstallerException e) {
233            Slog.w(TAG, "Failed to dexopt", e);
234            return DEX_OPT_FAILED;
235        }
236    }
237
238    /**
239     * Performs dexopt on the secondary dex {@code path} belonging to the app {@code info}.
240     *
241     * @return
242     *      DEX_OPT_FAILED if there was any exception during dexopt
243     *      DEX_OPT_PERFORMED if dexopt was performed successfully on the given path.
244     * NOTE that DEX_OPT_PERFORMED for secondary dex files includes the case when the dex file
245     * didn't need an update. That's because at the moment we don't get more than success/failure
246     * from installd.
247     *
248     * TODO(calin): Consider adding return codes to installd dexopt invocation (rather than
249     * throwing exceptions). Or maybe make a separate call to installd to get DexOptNeeded, though
250     * that seems wasteful.
251     */
252    public int dexOptSecondaryDexPath(ApplicationInfo info, String path, Set<String> isas,
253            String compilerFilter, boolean isUsedByOtherApps) {
254        synchronized (mInstallLock) {
255            // During boot the system doesn't need to instantiate and obtain a wake lock.
256            // PowerManager might not be ready, but that doesn't mean that we can't proceed with
257            // dexopt.
258            final boolean useLock = mSystemReady;
259            if (useLock) {
260                mDexoptWakeLock.setWorkSource(new WorkSource(info.uid));
261                mDexoptWakeLock.acquire();
262            }
263            try {
264                return dexOptSecondaryDexPathLI(info, path, isas, compilerFilter,
265                        isUsedByOtherApps);
266            } finally {
267                if (useLock) {
268                    mDexoptWakeLock.release();
269                }
270            }
271        }
272    }
273
274    @GuardedBy("mInstallLock")
275    private int dexOptSecondaryDexPathLI(ApplicationInfo info, String path, Set<String> isas,
276            String compilerFilter, boolean isUsedByOtherApps) {
277        int dexoptFlags = getDexFlags(info, compilerFilter) | DEXOPT_SECONDARY_DEX;
278        // Check the app storage and add the appropriate flags.
279        if (info.dataDir.equals(info.deviceProtectedDataDir)) {
280            dexoptFlags |= DEXOPT_STORAGE_DE;
281        } else if (info.dataDir.equals(info.credentialProtectedDataDir)) {
282            dexoptFlags |= DEXOPT_STORAGE_CE;
283        } else {
284            Slog.e(TAG, "Could not infer CE/DE storage for package " + info.packageName);
285            return DEX_OPT_FAILED;
286        }
287        compilerFilter = getRealCompilerFilter(info, compilerFilter, isUsedByOtherApps);
288        Log.d(TAG, "Running dexopt on: " + path
289                + " pkg=" + info.packageName + " isa=" + isas
290                + " dexoptFlags=" + printDexoptFlags(dexoptFlags)
291                + " target-filter=" + compilerFilter);
292
293        try {
294            for (String isa : isas) {
295                // Reuse the same dexopt path as for the primary apks. We don't need all the
296                // arguments as some (dexopNeeded and oatDir) will be computed by installd because
297                // system server cannot read untrusted app content.
298                // TODO(calin): maybe add a separate call.
299                mInstaller.dexopt(path, info.uid, info.packageName, isa, /*dexoptNeeded*/ 0,
300                        /*oatDir*/ null, dexoptFlags,
301                        compilerFilter, info.volumeUuid, SKIP_SHARED_LIBRARY_CHECK);
302            }
303
304            return DEX_OPT_PERFORMED;
305        } catch (InstallerException e) {
306            Slog.w(TAG, "Failed to dexopt", e);
307            return DEX_OPT_FAILED;
308        }
309    }
310
311    /**
312     * Adjust the given dexopt-needed value. Can be overridden to influence the decision to
313     * optimize or not (and in what way).
314     */
315    protected int adjustDexoptNeeded(int dexoptNeeded) {
316        return dexoptNeeded;
317    }
318
319    /**
320     * Adjust the given dexopt flags that will be passed to the installer.
321     */
322    protected int adjustDexoptFlags(int dexoptFlags) {
323        return dexoptFlags;
324    }
325
326    /**
327     * Dumps the dexopt state of the given package {@code pkg} to the given {@code PrintWriter}.
328     */
329    void dumpDexoptState(IndentingPrintWriter pw, PackageParser.Package pkg) {
330        final String[] instructionSets = getAppDexInstructionSets(pkg.applicationInfo);
331        final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
332
333        final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly();
334
335        for (String instructionSet : dexCodeInstructionSets) {
336             pw.println("Instruction Set: " + instructionSet);
337             pw.increaseIndent();
338             for (String path : paths) {
339                  String status = null;
340                  try {
341                      status = DexFile.getDexFileStatus(path, instructionSet);
342                  } catch (IOException ioe) {
343                      status = "[Exception]: " + ioe.getMessage();
344                  }
345                  pw.println("path: " + path);
346                  pw.println("status: " + status);
347             }
348             pw.decreaseIndent();
349        }
350    }
351
352    /**
353     * Returns the compiler filter that should be used to optimize the package code.
354     * The target filter will be updated if the package code is used by other apps
355     * or if it has the safe mode flag set.
356     */
357    private String getRealCompilerFilter(ApplicationInfo info, String targetCompilerFilter,
358            boolean isUsedByOtherApps) {
359        int flags = info.flags;
360        boolean vmSafeMode = (flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0;
361        if (vmSafeMode) {
362            // For the compilation, it doesn't really matter what we return here because installd
363            // will replace the filter with interpret-only anyway.
364            // However, we return a non profile guided filter so that we simplify the logic of
365            // merging profiles.
366            // TODO(calin): safe mode path could be simplified if we pass interpret-only from
367            //              here rather than letting installd decide on the filter.
368            return getNonProfileGuidedCompilerFilter(targetCompilerFilter);
369        }
370
371        if (isProfileGuidedCompilerFilter(targetCompilerFilter) && isUsedByOtherApps) {
372            // If the dex files is used by other apps, we cannot use profile-guided compilation.
373            return getNonProfileGuidedCompilerFilter(targetCompilerFilter);
374        }
375
376        return targetCompilerFilter;
377    }
378
379    /**
380     * Computes the dex flags that needs to be pass to installd for the given package and compiler
381     * filter.
382     */
383    private int getDexFlags(PackageParser.Package pkg, String compilerFilter) {
384        return getDexFlags(pkg.applicationInfo, compilerFilter);
385    }
386
387    private int getDexFlags(ApplicationInfo info, String compilerFilter) {
388        int flags = info.flags;
389        boolean vmSafeMode = (flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0;
390        boolean debuggable = (flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
391        // Profile guide compiled oat files should not be public.
392        boolean isProfileGuidedFilter = isProfileGuidedCompilerFilter(compilerFilter);
393        boolean isPublic = !info.isForwardLocked() && !isProfileGuidedFilter;
394        int profileFlag = isProfileGuidedFilter ? DEXOPT_PROFILE_GUIDED : 0;
395        int dexFlags =
396                (isPublic ? DEXOPT_PUBLIC : 0)
397                | (vmSafeMode ? DEXOPT_SAFEMODE : 0)
398                | (debuggable ? DEXOPT_DEBUGGABLE : 0)
399                | profileFlag
400                | DEXOPT_BOOTCOMPLETE;
401        return adjustDexoptFlags(dexFlags);
402    }
403
404    /**
405     * Assesses if there's a need to perform dexopt on {@code path} for the given
406     * configuration (isa, compiler filter, profile).
407     */
408    private int getDexoptNeeded(String path, String isa, String compilerFilter,
409            boolean newProfile) {
410        int dexoptNeeded;
411        try {
412            dexoptNeeded = DexFile.getDexOptNeeded(path, isa, compilerFilter, newProfile);
413        } catch (IOException ioe) {
414            Slog.w(TAG, "IOException reading apk: " + path, ioe);
415            return DEX_OPT_FAILED;
416        }
417        return adjustDexoptNeeded(dexoptNeeded);
418    }
419
420    /**
421     * Computes the shared libraries path that should be passed to dexopt.
422     */
423    private String getSharedLibrariesPath(String[] sharedLibraries) {
424        if (sharedLibraries == null || sharedLibraries.length == 0) {
425            return null;
426        }
427        StringBuilder sb = new StringBuilder();
428        for (String lib : sharedLibraries) {
429            if (sb.length() != 0) {
430                sb.append(":");
431            }
432            sb.append(lib);
433        }
434        return sb.toString();
435    }
436
437    /**
438     * Checks if there is an update on the profile information of the {@code pkg}.
439     * If the compiler filter is not profile guided the method returns false.
440     *
441     * Note that this is a "destructive" operation with side effects. Under the hood the
442     * current profile and the reference profile will be merged and subsequent calls
443     * may return a different result.
444     */
445    private boolean isProfileUpdated(PackageParser.Package pkg, int uid, String compilerFilter) {
446        // Check if we are allowed to merge and if the compiler filter is profile guided.
447        if (!isProfileGuidedCompilerFilter(compilerFilter)) {
448            return false;
449        }
450        // Merge profiles. It returns whether or not there was an updated in the profile info.
451        try {
452            return mInstaller.mergeProfiles(uid, pkg.packageName);
453        } catch (InstallerException e) {
454            Slog.w(TAG, "Failed to merge profiles", e);
455        }
456        return false;
457    }
458
459    /**
460     * Creates oat dir for the specified package if needed and supported.
461     * In certain cases oat directory
462     * <strong>cannot</strong> be created:
463     * <ul>
464     *      <li>{@code pkg} is a system app, which is not updated.</li>
465     *      <li>Package location is not a directory, i.e. monolithic install.</li>
466     * </ul>
467     *
468     * @return Absolute path to the oat directory or null, if oat directory
469     * cannot be created.
470     */
471    @Nullable
472    private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) {
473        if (!pkg.canHaveOatDir()) {
474            return null;
475        }
476        File codePath = new File(pkg.codePath);
477        if (codePath.isDirectory()) {
478            // TODO(calin): why do we create this only if the codePath is a directory? (i.e for
479            //              cluster packages). It seems that the logic for the folder creation is
480            //              split between installd and here.
481            File oatDir = getOatDir(codePath);
482            try {
483                mInstaller.createOatDir(oatDir.getAbsolutePath(), dexInstructionSet);
484            } catch (InstallerException e) {
485                Slog.w(TAG, "Failed to create oat dir", e);
486                return null;
487            }
488            return oatDir.getAbsolutePath();
489        }
490        return null;
491    }
492
493    static File getOatDir(File codePath) {
494        return new File(codePath, OAT_DIR_NAME);
495    }
496
497    void systemReady() {
498        mSystemReady = true;
499    }
500
501    private String printDexoptFlags(int flags) {
502        ArrayList<String> flagsList = new ArrayList<>();
503
504        if ((flags & DEXOPT_BOOTCOMPLETE) == DEXOPT_BOOTCOMPLETE) {
505            flagsList.add("boot_complete");
506        }
507        if ((flags & DEXOPT_DEBUGGABLE) == DEXOPT_DEBUGGABLE) {
508            flagsList.add("debuggable");
509        }
510        if ((flags & DEXOPT_PROFILE_GUIDED) == DEXOPT_PROFILE_GUIDED) {
511            flagsList.add("profile_guided");
512        }
513        if ((flags & DEXOPT_PUBLIC) == DEXOPT_PUBLIC) {
514            flagsList.add("public");
515        }
516        if ((flags & DEXOPT_SAFEMODE) == DEXOPT_SAFEMODE) {
517            flagsList.add("safemode");
518        }
519        if ((flags & DEXOPT_SECONDARY_DEX) == DEXOPT_SECONDARY_DEX) {
520            flagsList.add("secondary");
521        }
522        if ((flags & DEXOPT_FORCE) == DEXOPT_FORCE) {
523            flagsList.add("force");
524        }
525        if ((flags & DEXOPT_STORAGE_CE) == DEXOPT_STORAGE_CE) {
526            flagsList.add("storage_ce");
527        }
528        if ((flags & DEXOPT_STORAGE_DE) == DEXOPT_STORAGE_DE) {
529            flagsList.add("storage_de");
530        }
531
532        return String.join(",", flagsList);
533    }
534
535    /**
536     * A specialized PackageDexOptimizer that overrides already-installed checks, forcing a
537     * dexopt path.
538     */
539    public static class ForcedUpdatePackageDexOptimizer extends PackageDexOptimizer {
540
541        public ForcedUpdatePackageDexOptimizer(Installer installer, Object installLock,
542                Context context, String wakeLockTag) {
543            super(installer, installLock, context, wakeLockTag);
544        }
545
546        public ForcedUpdatePackageDexOptimizer(PackageDexOptimizer from) {
547            super(from);
548        }
549
550        @Override
551        protected int adjustDexoptNeeded(int dexoptNeeded) {
552            // Ensure compilation, no matter the current state.
553            // TODO: The return value is wrong when patchoat is needed.
554            return DexFile.DEX2OAT_FROM_SCRATCH;
555        }
556
557        @Override
558        protected int adjustDexoptFlags(int flags) {
559            // Add DEXOPT_FORCE flag to signal installd that it should force compilation
560            // and discard dexoptanalyzer result.
561            return flags | DEXOPT_FORCE;
562        }
563    }
564}
565