OtaDexoptService.java revision e523aa4f6bf24d3f65ec9a8a79fa8ae23a62b8ed
1/*
2 * Copyright (C) 2016 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 static com.android.server.pm.InstructionSets.getAppDexInstructionSets;
20import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets;
21import static com.android.server.pm.PackageManagerServiceCompilerMapping.getCompilerFilterForReason;
22
23import android.annotation.Nullable;
24import android.content.Context;
25import android.content.pm.IOtaDexopt;
26import android.content.pm.PackageParser;
27import android.os.Environment;
28import android.os.RemoteException;
29import android.os.ResultReceiver;
30import android.os.ServiceManager;
31import android.os.ShellCallback;
32import android.os.storage.StorageManager;
33import android.text.TextUtils;
34import android.util.Log;
35import android.util.Slog;
36
37import com.android.internal.logging.MetricsLogger;
38import com.android.server.pm.Installer.InstallerException;
39
40import java.io.File;
41import java.io.FileDescriptor;
42import java.util.ArrayList;
43import java.util.Arrays;
44import java.util.Collection;
45import java.util.List;
46import java.util.concurrent.TimeUnit;
47
48/**
49 * A service for A/B OTA dexopting.
50 *
51 * {@hide}
52 */
53public class OtaDexoptService extends IOtaDexopt.Stub {
54    private final static String TAG = "OTADexopt";
55    private final static boolean DEBUG_DEXOPT = true;
56
57    // The synthetic library dependencies denoting "no checks."
58    private final static String[] NO_LIBRARIES = new String[] { "&" };
59
60    // The amount of "available" (free - low threshold) space necessary at the start of an OTA to
61    // not bulk-delete unused apps' odex files.
62    private final static long BULK_DELETE_THRESHOLD = 1024 * 1024 * 1024;  // 1GB.
63
64    private final Context mContext;
65    private final PackageManagerService mPackageManagerService;
66
67    // TODO: Evaluate the need for WeakReferences here.
68
69    /**
70     * The list of dexopt invocations for all work.
71     */
72    private List<String> mDexoptCommands;
73
74    private int completeSize;
75
76    // MetricsLogger properties.
77
78    // Space before and after.
79    private long availableSpaceBefore;
80    private long availableSpaceAfterBulkDelete;
81    private long availableSpaceAfterDexopt;
82
83    // Packages.
84    private int importantPackageCount;
85    private int otherPackageCount;
86
87    // Number of dexopt commands. This may be different from the count of packages.
88    private int dexoptCommandCountTotal;
89    private int dexoptCommandCountExecuted;
90
91    // For spent time.
92    private long otaDexoptTimeStart;
93
94
95    public OtaDexoptService(Context context, PackageManagerService packageManagerService) {
96        this.mContext = context;
97        this.mPackageManagerService = packageManagerService;
98    }
99
100    public static OtaDexoptService main(Context context,
101            PackageManagerService packageManagerService) {
102        OtaDexoptService ota = new OtaDexoptService(context, packageManagerService);
103        ServiceManager.addService("otadexopt", ota);
104
105        // Now it's time to check whether we need to move any A/B artifacts.
106        ota.moveAbArtifacts(packageManagerService.mInstaller);
107
108        return ota;
109    }
110
111    @Override
112    public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
113            String[] args, ShellCallback callback, ResultReceiver resultReceiver) {
114        (new OtaDexoptShellCommand(this)).exec(
115                this, in, out, err, args, callback, resultReceiver);
116    }
117
118    @Override
119    public synchronized void prepare() throws RemoteException {
120        if (mDexoptCommands != null) {
121            throw new IllegalStateException("already called prepare()");
122        }
123        final List<PackageParser.Package> important;
124        final List<PackageParser.Package> others;
125        synchronized (mPackageManagerService.mPackages) {
126            // Important: the packages we need to run with ab-ota compiler-reason.
127            important = PackageManagerServiceUtils.getPackagesForDexopt(
128                    mPackageManagerService.mPackages.values(), mPackageManagerService);
129            // Others: we should optimize this with the (first-)boot compiler-reason.
130            others = new ArrayList<>(mPackageManagerService.mPackages.values());
131            others.removeAll(important);
132
133            // Pre-size the array list by over-allocating by a factor of 1.5.
134            mDexoptCommands = new ArrayList<>(3 * mPackageManagerService.mPackages.size() / 2);
135        }
136
137        for (PackageParser.Package p : important) {
138            mDexoptCommands.addAll(generatePackageDexopts(p, PackageManagerService.REASON_AB_OTA));
139        }
140        for (PackageParser.Package p : others) {
141            // We assume here that there are no core apps left.
142            if (p.coreApp) {
143                throw new IllegalStateException("Found a core app that's not important");
144            }
145            mDexoptCommands.addAll(
146                    generatePackageDexopts(p, PackageManagerService.REASON_FIRST_BOOT));
147        }
148        completeSize = mDexoptCommands.size();
149
150        long spaceAvailable = getAvailableSpace();
151        if (spaceAvailable < BULK_DELETE_THRESHOLD) {
152            Log.i(TAG, "Low on space, deleting oat files in an attempt to free up space: "
153                    + PackageManagerServiceUtils.packagesToString(others));
154            for (PackageParser.Package pkg : others) {
155                mPackageManagerService.deleteOatArtifactsOfPackage(pkg.packageName);
156            }
157        }
158        long spaceAvailableNow = getAvailableSpace();
159
160        prepareMetricsLogging(important.size(), others.size(), spaceAvailable, spaceAvailableNow);
161    }
162
163    @Override
164    public synchronized void cleanup() throws RemoteException {
165        if (DEBUG_DEXOPT) {
166            Log.i(TAG, "Cleaning up OTA Dexopt state.");
167        }
168        mDexoptCommands = null;
169        availableSpaceAfterDexopt = getAvailableSpace();
170
171        performMetricsLogging();
172    }
173
174    @Override
175    public synchronized boolean isDone() throws RemoteException {
176        if (mDexoptCommands == null) {
177            throw new IllegalStateException("done() called before prepare()");
178        }
179
180        return mDexoptCommands.isEmpty();
181    }
182
183    @Override
184    public synchronized float getProgress() throws RemoteException {
185        // Approximate the progress by the amount of already completed commands.
186        if (completeSize == 0) {
187            return 1f;
188        }
189        int commandsLeft = mDexoptCommands.size();
190        return (completeSize - commandsLeft) / ((float)completeSize);
191    }
192
193    @Override
194    public synchronized String nextDexoptCommand() throws RemoteException {
195        if (mDexoptCommands == null) {
196            throw new IllegalStateException("dexoptNextPackage() called before prepare()");
197        }
198
199        if (mDexoptCommands.isEmpty()) {
200            return "(all done)";
201        }
202
203        String next = mDexoptCommands.remove(0);
204
205        if (getAvailableSpace() > 0) {
206            dexoptCommandCountExecuted++;
207
208            Log.d(TAG, "Next command: " + next);
209            return next;
210        } else {
211            if (DEBUG_DEXOPT) {
212                Log.w(TAG, "Not enough space for OTA dexopt, stopping with "
213                        + (mDexoptCommands.size() + 1) + " commands left.");
214            }
215            mDexoptCommands.clear();
216            return "(no free space)";
217        }
218    }
219
220    private long getMainLowSpaceThreshold() {
221        File dataDir = Environment.getDataDirectory();
222        @SuppressWarnings("deprecation")
223        long lowThreshold = StorageManager.from(mContext).getStorageLowBytes(dataDir);
224        if (lowThreshold == 0) {
225            throw new IllegalStateException("Invalid low memory threshold");
226        }
227        return lowThreshold;
228    }
229
230    /**
231     * Returns the difference of free space to the low-storage-space threshold. Positive values
232     * indicate free bytes.
233     */
234    private long getAvailableSpace() {
235        // TODO: If apps are not installed in the internal /data partition, we should compare
236        //       against that storage's free capacity.
237        long lowThreshold = getMainLowSpaceThreshold();
238
239        File dataDir = Environment.getDataDirectory();
240        long usableSpace = dataDir.getUsableSpace();
241
242        return usableSpace - lowThreshold;
243    }
244
245    /**
246     * Generate all dexopt commands for the given package.
247     */
248    private synchronized List<String> generatePackageDexopts(PackageParser.Package pkg,
249            int compilationReason) {
250        // Intercept and collect dexopt requests
251        final List<String> commands = new ArrayList<String>();
252        final Installer collectingInstaller = new Installer(mContext, true) {
253            /**
254             * Encode the dexopt command into a string.
255             *
256             * Note: If you have to change the signature of this function, increase the version
257             *       number, and update the counterpart in
258             *       frameworks/native/cmds/installd/otapreopt.cpp.
259             */
260            @Override
261            public void dexopt(String apkPath, int uid, @Nullable String pkgName,
262                    String instructionSet, int dexoptNeeded, @Nullable String outputPath,
263                    int dexFlags, String compilerFilter, @Nullable String volumeUuid,
264                    @Nullable String sharedLibraries, @Nullable String seInfo) throws InstallerException {
265                final StringBuilder builder = new StringBuilder();
266
267                // The version. Right now it's 2.
268                builder.append("2 ");
269
270                builder.append("dexopt");
271
272                encodeParameter(builder, apkPath);
273                encodeParameter(builder, uid);
274                encodeParameter(builder, pkgName);
275                encodeParameter(builder, instructionSet);
276                encodeParameter(builder, dexoptNeeded);
277                encodeParameter(builder, outputPath);
278                encodeParameter(builder, dexFlags);
279                encodeParameter(builder, compilerFilter);
280                encodeParameter(builder, volumeUuid);
281                encodeParameter(builder, sharedLibraries);
282                encodeParameter(builder, seInfo);
283
284                commands.add(builder.toString());
285            }
286
287            /**
288             * Encode a parameter as necessary for the commands string.
289             */
290            private void encodeParameter(StringBuilder builder, Object arg) {
291                builder.append(' ');
292
293                if (arg == null) {
294                    builder.append('!');
295                    return;
296                }
297
298                String txt = String.valueOf(arg);
299                if (txt.indexOf('\0') != -1 || txt.indexOf(' ') != -1 || "!".equals(txt)) {
300                    throw new IllegalArgumentException(
301                            "Invalid argument while executing " + arg);
302                }
303                builder.append(txt);
304            }
305        };
306
307        // Use the package manager install and install lock here for the OTA dex optimizer.
308        PackageDexOptimizer optimizer = new OTADexoptPackageDexOptimizer(
309                collectingInstaller, mPackageManagerService.mInstallLock, mContext);
310
311        String[] libraryDependencies = pkg.usesLibraryFiles;
312        if (pkg.isSystemApp()) {
313            // For system apps, we want to avoid classpaths checks.
314            libraryDependencies = NO_LIBRARIES;
315        }
316
317        optimizer.performDexOpt(pkg, libraryDependencies,
318                null /* ISAs */, false /* checkProfiles */,
319                getCompilerFilterForReason(compilationReason),
320                null /* CompilerStats.PackageStats */,
321                mPackageManagerService.getDexManager().isUsedByOtherApps(pkg.packageName),
322                true /* bootComplete */);
323
324        mPackageManagerService.getDexManager().dexoptSecondaryDex(pkg.packageName,
325                getCompilerFilterForReason(compilationReason),
326                false /* force */,
327                false /* compileOnlySharedDex */);
328        return commands;
329    }
330
331    @Override
332    public synchronized void dexoptNextPackage() throws RemoteException {
333        throw new UnsupportedOperationException();
334    }
335
336    private void moveAbArtifacts(Installer installer) {
337        if (mDexoptCommands != null) {
338            throw new IllegalStateException("Should not be ota-dexopting when trying to move.");
339        }
340
341        if (!mPackageManagerService.isUpgrade()) {
342            Slog.d(TAG, "No upgrade, skipping A/B artifacts check.");
343            return;
344        }
345
346        // Look into all packages.
347        Collection<PackageParser.Package> pkgs = mPackageManagerService.getPackages();
348        int packagePaths = 0;
349        int pathsSuccessful = 0;
350        for (PackageParser.Package pkg : pkgs) {
351            if (pkg == null) {
352                continue;
353            }
354
355            // Does the package have code? If not, there won't be any artifacts.
356            if (!PackageDexOptimizer.canOptimizePackage(pkg)) {
357                continue;
358            }
359            if (pkg.codePath == null) {
360                Slog.w(TAG, "Package " + pkg + " can be optimized but has null codePath");
361                continue;
362            }
363
364            // If the path is in /system or /vendor, ignore. It will have been ota-dexopted into
365            // /data/ota and moved into the dalvik-cache already.
366            if (pkg.codePath.startsWith("/system") || pkg.codePath.startsWith("/vendor")) {
367                continue;
368            }
369
370            final String[] instructionSets = getAppDexInstructionSets(pkg.applicationInfo);
371            final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly();
372            final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
373            for (String dexCodeInstructionSet : dexCodeInstructionSets) {
374                for (String path : paths) {
375                    String oatDir = PackageDexOptimizer.getOatDir(new File(pkg.codePath)).
376                            getAbsolutePath();
377
378                    // TODO: Check first whether there is an artifact, to save the roundtrip time.
379
380                    packagePaths++;
381                    try {
382                        installer.moveAb(path, dexCodeInstructionSet, oatDir);
383                        pathsSuccessful++;
384                    } catch (InstallerException e) {
385                    }
386                }
387            }
388        }
389        Slog.i(TAG, "Moved " + pathsSuccessful + "/" + packagePaths);
390    }
391
392    /**
393     * Initialize logging fields.
394     */
395    private void prepareMetricsLogging(int important, int others, long spaceBegin, long spaceBulk) {
396        availableSpaceBefore = spaceBegin;
397        availableSpaceAfterBulkDelete = spaceBulk;
398        availableSpaceAfterDexopt = 0;
399
400        importantPackageCount = important;
401        otherPackageCount = others;
402
403        dexoptCommandCountTotal = mDexoptCommands.size();
404        dexoptCommandCountExecuted = 0;
405
406        otaDexoptTimeStart = System.nanoTime();
407    }
408
409    private static int inMegabytes(long value) {
410        long in_mega_bytes = value / (1024 * 1024);
411        if (in_mega_bytes > Integer.MAX_VALUE) {
412            Log.w(TAG, "Recording " + in_mega_bytes + "MB of free space, overflowing range");
413            return Integer.MAX_VALUE;
414        }
415        return (int)in_mega_bytes;
416    }
417
418    private void performMetricsLogging() {
419        long finalTime = System.nanoTime();
420
421        MetricsLogger.histogram(mContext, "ota_dexopt_available_space_before_mb",
422                inMegabytes(availableSpaceBefore));
423        MetricsLogger.histogram(mContext, "ota_dexopt_available_space_after_bulk_delete_mb",
424                inMegabytes(availableSpaceAfterBulkDelete));
425        MetricsLogger.histogram(mContext, "ota_dexopt_available_space_after_dexopt_mb",
426                inMegabytes(availableSpaceAfterDexopt));
427
428        MetricsLogger.histogram(mContext, "ota_dexopt_num_important_packages",
429                importantPackageCount);
430        MetricsLogger.histogram(mContext, "ota_dexopt_num_other_packages", otherPackageCount);
431
432        MetricsLogger.histogram(mContext, "ota_dexopt_num_commands", dexoptCommandCountTotal);
433        MetricsLogger.histogram(mContext, "ota_dexopt_num_commands_executed",
434                dexoptCommandCountExecuted);
435
436        final int elapsedTimeSeconds =
437                (int) TimeUnit.NANOSECONDS.toSeconds(finalTime - otaDexoptTimeStart);
438        MetricsLogger.histogram(mContext, "ota_dexopt_time_s", elapsedTimeSeconds);
439    }
440
441    private static class OTADexoptPackageDexOptimizer extends
442            PackageDexOptimizer.ForcedUpdatePackageDexOptimizer {
443        public OTADexoptPackageDexOptimizer(Installer installer, Object installLock,
444                Context context) {
445            super(installer, installLock, context, "*otadexopt*");
446        }
447    }
448}
449