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