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; |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 21 | import static com.android.server.pm.PackageManagerServiceCompilerMapping.getCompilerFilterForReason; |
Andreas Gampe | abcbe2f | 2016-02-26 11:25:36 -0800 | [diff] [blame] | 22 | |
Jeff Sharkey | c98c7bc | 2016-12-07 14:57:34 -0700 | [diff] [blame] | 23 | import android.annotation.Nullable; |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 24 | import android.content.Context; |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 25 | import android.content.pm.IOtaDexopt; |
| 26 | import android.content.pm.PackageParser; |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 27 | import android.os.Environment; |
| 28 | import android.os.RemoteException; |
| 29 | import android.os.ResultReceiver; |
| 30 | import android.os.ServiceManager; |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 31 | import android.os.storage.StorageManager; |
Jeff Sharkey | c98c7bc | 2016-12-07 14:57:34 -0700 | [diff] [blame] | 32 | import android.text.TextUtils; |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 33 | import android.util.Log; |
Andreas Gampe | abcbe2f | 2016-02-26 11:25:36 -0800 | [diff] [blame] | 34 | import android.util.Slog; |
Jeff Sharkey | c98c7bc | 2016-12-07 14:57:34 -0700 | [diff] [blame] | 35 | |
Andreas Gampe | 112e014 | 2016-09-13 11:12:13 -0700 | [diff] [blame] | 36 | import com.android.internal.logging.MetricsLogger; |
Jeff Sharkey | 740f523 | 2016-12-09 14:31:26 -0700 | [diff] [blame] | 37 | import com.android.server.pm.Installer.InstallerException; |
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 | d15300c | 2016-06-23 20:27:12 -0700 | [diff] [blame] | 41 | import java.util.ArrayList; |
Jeff Sharkey | c98c7bc | 2016-12-07 14:57:34 -0700 | [diff] [blame] | 42 | import java.util.Arrays; |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 43 | import java.util.Collection; |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 44 | import java.util.List; |
Andreas Gampe | 112e014 | 2016-09-13 11:12:13 -0700 | [diff] [blame] | 45 | import java.util.concurrent.TimeUnit; |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * A service for A/B OTA dexopting. |
| 49 | * |
| 50 | * {@hide} |
| 51 | */ |
| 52 | public class OtaDexoptService extends IOtaDexopt.Stub { |
| 53 | private final static String TAG = "OTADexopt"; |
| 54 | private final static boolean DEBUG_DEXOPT = true; |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 55 | |
Andreas Gampe | 3f95d0b | 2016-08-01 22:08:26 -0700 | [diff] [blame] | 56 | // The synthetic library dependencies denoting "no checks." |
| 57 | private final static String[] NO_LIBRARIES = new String[] { "&" }; |
| 58 | |
Andreas Gampe | 1920d7b4 | 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 | d15300c | 2016-06-23 20:27:12 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
Andreas Gampe | 1418673 | 2016-07-28 16:54:41 -0700 | [diff] [blame] | 69 | * The list of dexopt invocations for all work. |
Andreas Gampe | d15300c | 2016-06-23 20:27:12 -0700 | [diff] [blame] | 70 | */ |
Andreas Gampe | 1418673 | 2016-07-28 16:54:41 -0700 | [diff] [blame] | 71 | private List<String> mDexoptCommands; |
Andreas Gampe | d15300c | 2016-06-23 20:27:12 -0700 | [diff] [blame] | 72 | |
Andreas Gampe | ff8ab4c | 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 | 112e014 | 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; |
| 97 | |
Andreas Gampe | abcbe2f | 2016-02-26 11:25:36 -0800 | [diff] [blame] | 98 | // Now it's time to check whether we need to move any A/B artifacts. |
| 99 | moveAbArtifacts(packageManagerService.mInstaller); |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | public static OtaDexoptService main(Context context, |
| 103 | PackageManagerService packageManagerService) { |
| 104 | OtaDexoptService ota = new OtaDexoptService(context, packageManagerService); |
| 105 | ServiceManager.addService("otadexopt", ota); |
| 106 | |
| 107 | return ota; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
| 112 | String[] args, ResultReceiver resultReceiver) throws RemoteException { |
| 113 | (new OtaDexoptShellCommand(this)).exec( |
| 114 | this, in, out, err, args, resultReceiver); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public synchronized void prepare() throws RemoteException { |
Andreas Gampe | 1418673 | 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 | 94f9be2 | 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 | 1418673 | 2016-07-28 16:54:41 -0700 | [diff] [blame] | 125 | // Important: the packages we need to run with ab-ota compiler-reason. |
Todd Kennedy | 94f9be2 | 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 | 1418673 | 2016-07-28 16:54:41 -0700 | [diff] [blame] | 128 | // Others: we should optimize this with the (first-)boot compiler-reason. |
Todd Kennedy | 94f9be2 | 2016-08-18 11:22:52 -0700 | [diff] [blame] | 129 | others = new ArrayList<>(mPackageManagerService.mPackages.values()); |
Andreas Gampe | 1418673 | 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 | 94f9be2 | 2016-08-18 11:22:52 -0700 | [diff] [blame] | 134 | } |
Andreas Gampe | 1418673 | 2016-07-28 16:54:41 -0700 | [diff] [blame] | 135 | |
Todd Kennedy | 94f9be2 | 2016-08-18 11:22:52 -0700 | [diff] [blame] | 136 | for (PackageParser.Package p : important) { |
| 137 | // Make sure that core apps are optimized according to their own "reason". |
| 138 | // If the core apps are not preopted in the B OTA, and REASON_AB_OTA is not speed |
| 139 | // (by default is speed-profile) they will be interepreted/JITed. This in itself is |
| 140 | // not a problem as we will end up doing profile guided compilation. However, some |
| 141 | // core apps may be loaded by system server which doesn't JIT and we need to make |
| 142 | // sure we don't interpret-only |
| 143 | int compilationReason = p.coreApp |
| 144 | ? PackageManagerService.REASON_CORE_APP |
| 145 | : PackageManagerService.REASON_AB_OTA; |
| 146 | mDexoptCommands.addAll(generatePackageDexopts(p, compilationReason)); |
| 147 | } |
| 148 | for (PackageParser.Package p : others) { |
| 149 | // We assume here that there are no core apps left. |
| 150 | if (p.coreApp) { |
| 151 | throw new IllegalStateException("Found a core app that's not important"); |
Andreas Gampe | 1418673 | 2016-07-28 16:54:41 -0700 | [diff] [blame] | 152 | } |
Todd Kennedy | 94f9be2 | 2016-08-18 11:22:52 -0700 | [diff] [blame] | 153 | mDexoptCommands.addAll( |
| 154 | generatePackageDexopts(p, PackageManagerService.REASON_FIRST_BOOT)); |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 155 | } |
Andreas Gampe | 1418673 | 2016-07-28 16:54:41 -0700 | [diff] [blame] | 156 | completeSize = mDexoptCommands.size(); |
Andreas Gampe | 1920d7b4 | 2016-09-09 17:50:20 -0700 | [diff] [blame] | 157 | |
Andreas Gampe | 112e014 | 2016-09-13 11:12:13 -0700 | [diff] [blame] | 158 | long spaceAvailable = getAvailableSpace(); |
| 159 | if (spaceAvailable < BULK_DELETE_THRESHOLD) { |
Andreas Gampe | 1920d7b4 | 2016-09-09 17:50:20 -0700 | [diff] [blame] | 160 | Log.i(TAG, "Low on space, deleting oat files in an attempt to free up space: " |
| 161 | + PackageManagerServiceUtils.packagesToString(others)); |
| 162 | for (PackageParser.Package pkg : others) { |
| 163 | deleteOatArtifactsOfPackage(pkg); |
| 164 | } |
| 165 | } |
Andreas Gampe | 112e014 | 2016-09-13 11:12:13 -0700 | [diff] [blame] | 166 | long spaceAvailableNow = getAvailableSpace(); |
| 167 | |
| 168 | prepareMetricsLogging(important.size(), others.size(), spaceAvailable, spaceAvailableNow); |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public synchronized void cleanup() throws RemoteException { |
| 173 | if (DEBUG_DEXOPT) { |
| 174 | Log.i(TAG, "Cleaning up OTA Dexopt state."); |
| 175 | } |
Andreas Gampe | 1418673 | 2016-07-28 16:54:41 -0700 | [diff] [blame] | 176 | mDexoptCommands = null; |
Andreas Gampe | 13018f3 | 2016-09-19 15:18:46 -0700 | [diff] [blame] | 177 | availableSpaceAfterDexopt = getAvailableSpace(); |
Andreas Gampe | 112e014 | 2016-09-13 11:12:13 -0700 | [diff] [blame] | 178 | |
| 179 | performMetricsLogging(); |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public synchronized boolean isDone() throws RemoteException { |
Andreas Gampe | 1418673 | 2016-07-28 16:54:41 -0700 | [diff] [blame] | 184 | if (mDexoptCommands == null) { |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 185 | throw new IllegalStateException("done() called before prepare()"); |
| 186 | } |
| 187 | |
Andreas Gampe | 1418673 | 2016-07-28 16:54:41 -0700 | [diff] [blame] | 188 | return mDexoptCommands.isEmpty(); |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | @Override |
Andreas Gampe | ff8ab4c | 2016-06-10 15:21:39 -0700 | [diff] [blame] | 192 | public synchronized float getProgress() throws RemoteException { |
Andreas Gampe | 1418673 | 2016-07-28 16:54:41 -0700 | [diff] [blame] | 193 | // Approximate the progress by the amount of already completed commands. |
Andreas Gampe | ff8ab4c | 2016-06-10 15:21:39 -0700 | [diff] [blame] | 194 | if (completeSize == 0) { |
| 195 | return 1f; |
| 196 | } |
Andreas Gampe | 1418673 | 2016-07-28 16:54:41 -0700 | [diff] [blame] | 197 | int commandsLeft = mDexoptCommands.size(); |
| 198 | return (completeSize - commandsLeft) / ((float)completeSize); |
Andreas Gampe | d15300c | 2016-06-23 20:27:12 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public synchronized String nextDexoptCommand() throws RemoteException { |
Andreas Gampe | 1418673 | 2016-07-28 16:54:41 -0700 | [diff] [blame] | 203 | if (mDexoptCommands == null) { |
Andreas Gampe | d15300c | 2016-06-23 20:27:12 -0700 | [diff] [blame] | 204 | throw new IllegalStateException("dexoptNextPackage() called before prepare()"); |
| 205 | } |
| 206 | |
Andreas Gampe | 1418673 | 2016-07-28 16:54:41 -0700 | [diff] [blame] | 207 | if (mDexoptCommands.isEmpty()) { |
| 208 | return "(all done)"; |
| 209 | } |
Andreas Gampe | d15300c | 2016-06-23 20:27:12 -0700 | [diff] [blame] | 210 | |
Andreas Gampe | 1418673 | 2016-07-28 16:54:41 -0700 | [diff] [blame] | 211 | String next = mDexoptCommands.remove(0); |
Andreas Gampe | d15300c | 2016-06-23 20:27:12 -0700 | [diff] [blame] | 212 | |
Andreas Gampe | 1920d7b4 | 2016-09-09 17:50:20 -0700 | [diff] [blame] | 213 | if (getAvailableSpace() > 0) { |
Andreas Gampe | 112e014 | 2016-09-13 11:12:13 -0700 | [diff] [blame] | 214 | dexoptCommandCountExecuted++; |
| 215 | |
Andreas Gampe | 1418673 | 2016-07-28 16:54:41 -0700 | [diff] [blame] | 216 | return next; |
| 217 | } else { |
Andreas Gampe | 1920d7b4 | 2016-09-09 17:50:20 -0700 | [diff] [blame] | 218 | if (DEBUG_DEXOPT) { |
| 219 | Log.w(TAG, "Not enough space for OTA dexopt, stopping with " |
| 220 | + (mDexoptCommands.size() + 1) + " commands left."); |
| 221 | } |
Andreas Gampe | 1418673 | 2016-07-28 16:54:41 -0700 | [diff] [blame] | 222 | mDexoptCommands.clear(); |
| 223 | return "(no free space)"; |
Andreas Gampe | d15300c | 2016-06-23 20:27:12 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
Andreas Gampe | 1920d7b4 | 2016-09-09 17:50:20 -0700 | [diff] [blame] | 227 | private long getMainLowSpaceThreshold() { |
Andreas Gampe | d15300c | 2016-06-23 20:27:12 -0700 | [diff] [blame] | 228 | File dataDir = Environment.getDataDirectory(); |
| 229 | @SuppressWarnings("deprecation") |
| 230 | long lowThreshold = StorageManager.from(mContext).getStorageLowBytes(dataDir); |
| 231 | if (lowThreshold == 0) { |
| 232 | throw new IllegalStateException("Invalid low memory threshold"); |
| 233 | } |
Andreas Gampe | 1920d7b4 | 2016-09-09 17:50:20 -0700 | [diff] [blame] | 234 | return lowThreshold; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Returns the difference of free space to the low-storage-space threshold. Positive values |
| 239 | * indicate free bytes. |
| 240 | */ |
| 241 | private long getAvailableSpace() { |
| 242 | // TODO: If apps are not installed in the internal /data partition, we should compare |
| 243 | // against that storage's free capacity. |
| 244 | long lowThreshold = getMainLowSpaceThreshold(); |
| 245 | |
| 246 | File dataDir = Environment.getDataDirectory(); |
Andreas Gampe | d15300c | 2016-06-23 20:27:12 -0700 | [diff] [blame] | 247 | long usableSpace = dataDir.getUsableSpace(); |
Andreas Gampe | 1920d7b4 | 2016-09-09 17:50:20 -0700 | [diff] [blame] | 248 | |
| 249 | return usableSpace - lowThreshold; |
| 250 | } |
| 251 | |
| 252 | private static String getOatDir(PackageParser.Package pkg) { |
| 253 | if (!pkg.canHaveOatDir()) { |
| 254 | return null; |
| 255 | } |
| 256 | File codePath = new File(pkg.codePath); |
| 257 | if (codePath.isDirectory()) { |
| 258 | return PackageDexOptimizer.getOatDir(codePath).getAbsolutePath(); |
| 259 | } |
| 260 | return null; |
| 261 | } |
| 262 | |
| 263 | private void deleteOatArtifactsOfPackage(PackageParser.Package pkg) { |
| 264 | String[] instructionSets = getAppDexInstructionSets(pkg.applicationInfo); |
| 265 | for (String codePath : pkg.getAllCodePaths()) { |
| 266 | for (String isa : instructionSets) { |
| 267 | try { |
| 268 | mPackageManagerService.mInstaller.deleteOdex(codePath, isa, getOatDir(pkg)); |
| 269 | } catch (InstallerException e) { |
| 270 | Log.e(TAG, "Failed deleting oat files for " + codePath, e); |
| 271 | } |
| 272 | } |
| 273 | } |
Andreas Gampe | 1418673 | 2016-07-28 16:54:41 -0700 | [diff] [blame] | 274 | } |
Andreas Gampe | d15300c | 2016-06-23 20:27:12 -0700 | [diff] [blame] | 275 | |
Andreas Gampe | 1418673 | 2016-07-28 16:54:41 -0700 | [diff] [blame] | 276 | /** |
| 277 | * Generate all dexopt commands for the given package. |
| 278 | */ |
| 279 | private synchronized List<String> generatePackageDexopts(PackageParser.Package pkg, |
| 280 | int compilationReason) { |
Jeff Sharkey | c98c7bc | 2016-12-07 14:57:34 -0700 | [diff] [blame] | 281 | // Intercept and collect dexopt requests |
| 282 | final List<String> commands = new ArrayList<String>(); |
| 283 | final Installer collectingInstaller = new Installer(mContext, true) { |
| 284 | @Override |
| 285 | public void dexopt(String apkPath, int uid, @Nullable String pkgName, |
| 286 | String instructionSet, int dexoptNeeded, @Nullable String outputPath, |
| 287 | int dexFlags, String compilerFilter, @Nullable String volumeUuid, |
| 288 | @Nullable String sharedLibraries) throws InstallerException { |
| 289 | commands.add(buildCommand("dexopt", |
| 290 | apkPath, |
| 291 | uid, |
| 292 | pkgName, |
| 293 | instructionSet, |
| 294 | dexoptNeeded, |
| 295 | outputPath, |
| 296 | dexFlags, |
| 297 | compilerFilter, |
| 298 | volumeUuid, |
| 299 | sharedLibraries)); |
| 300 | } |
| 301 | }; |
Andreas Gampe | d15300c | 2016-06-23 20:27:12 -0700 | [diff] [blame] | 302 | |
| 303 | // Use the package manager install and install lock here for the OTA dex optimizer. |
| 304 | PackageDexOptimizer optimizer = new OTADexoptPackageDexOptimizer( |
| 305 | collectingInstaller, mPackageManagerService.mInstallLock, mContext); |
Calin Juravle | 9438528 | 2016-07-12 13:56:34 +0100 | [diff] [blame] | 306 | |
Andreas Gampe | 3f95d0b | 2016-08-01 22:08:26 -0700 | [diff] [blame] | 307 | String[] libraryDependencies = pkg.usesLibraryFiles; |
| 308 | if (pkg.isSystemApp()) { |
| 309 | // For system apps, we want to avoid classpaths checks. |
| 310 | libraryDependencies = NO_LIBRARIES; |
| 311 | } |
| 312 | |
| 313 | optimizer.performDexOpt(pkg, libraryDependencies, |
Andreas Gampe | d15300c | 2016-06-23 20:27:12 -0700 | [diff] [blame] | 314 | null /* ISAs */, false /* checkProfiles */, |
Andreas Gampe | 7e619a9 | 2016-07-12 22:42:41 -0700 | [diff] [blame] | 315 | getCompilerFilterForReason(compilationReason), |
| 316 | null /* CompilerStats.PackageStats */); |
Andreas Gampe | d15300c | 2016-06-23 20:27:12 -0700 | [diff] [blame] | 317 | |
Jeff Sharkey | c98c7bc | 2016-12-07 14:57:34 -0700 | [diff] [blame] | 318 | return commands; |
Andreas Gampe | ff8ab4c | 2016-06-10 15:21:39 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | @Override |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 322 | public synchronized void dexoptNextPackage() throws RemoteException { |
Andreas Gampe | 1418673 | 2016-07-28 16:54:41 -0700 | [diff] [blame] | 323 | throw new UnsupportedOperationException(); |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Andreas Gampe | abcbe2f | 2016-02-26 11:25:36 -0800 | [diff] [blame] | 326 | private void moveAbArtifacts(Installer installer) { |
Andreas Gampe | 1418673 | 2016-07-28 16:54:41 -0700 | [diff] [blame] | 327 | if (mDexoptCommands != null) { |
Andreas Gampe | abcbe2f | 2016-02-26 11:25:36 -0800 | [diff] [blame] | 328 | throw new IllegalStateException("Should not be ota-dexopting when trying to move."); |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 329 | } |
Andreas Gampe | abcbe2f | 2016-02-26 11:25:36 -0800 | [diff] [blame] | 330 | |
| 331 | // Look into all packages. |
| 332 | Collection<PackageParser.Package> pkgs = mPackageManagerService.getPackages(); |
| 333 | for (PackageParser.Package pkg : pkgs) { |
| 334 | if (pkg == null) { |
| 335 | continue; |
| 336 | } |
| 337 | |
| 338 | // Does the package have code? If not, there won't be any artifacts. |
| 339 | if (!PackageDexOptimizer.canOptimizePackage(pkg)) { |
| 340 | continue; |
| 341 | } |
| 342 | if (pkg.codePath == null) { |
| 343 | Slog.w(TAG, "Package " + pkg + " can be optimized but has null codePath"); |
| 344 | continue; |
| 345 | } |
| 346 | |
| 347 | // If the path is in /system or /vendor, ignore. It will have been ota-dexopted into |
| 348 | // /data/ota and moved into the dalvik-cache already. |
| 349 | if (pkg.codePath.startsWith("/system") || pkg.codePath.startsWith("/vendor")) { |
| 350 | continue; |
| 351 | } |
| 352 | |
| 353 | final String[] instructionSets = getAppDexInstructionSets(pkg.applicationInfo); |
| 354 | final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly(); |
| 355 | final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets); |
| 356 | for (String dexCodeInstructionSet : dexCodeInstructionSets) { |
| 357 | for (String path : paths) { |
| 358 | String oatDir = PackageDexOptimizer.getOatDir(new File(pkg.codePath)). |
| 359 | getAbsolutePath(); |
| 360 | |
| 361 | // TODO: Check first whether there is an artifact, to save the roundtrip time. |
| 362 | |
| 363 | try { |
| 364 | installer.moveAb(path, dexCodeInstructionSet, oatDir); |
| 365 | } catch (InstallerException e) { |
| 366 | } |
| 367 | } |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 368 | } |
| 369 | } |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Andreas Gampe | 112e014 | 2016-09-13 11:12:13 -0700 | [diff] [blame] | 372 | /** |
| 373 | * Initialize logging fields. |
| 374 | */ |
| 375 | private void prepareMetricsLogging(int important, int others, long spaceBegin, long spaceBulk) { |
| 376 | availableSpaceBefore = spaceBegin; |
| 377 | availableSpaceAfterBulkDelete = spaceBulk; |
| 378 | availableSpaceAfterDexopt = 0; |
| 379 | |
| 380 | importantPackageCount = important; |
| 381 | otherPackageCount = others; |
| 382 | |
| 383 | dexoptCommandCountTotal = mDexoptCommands.size(); |
| 384 | dexoptCommandCountExecuted = 0; |
| 385 | |
| 386 | otaDexoptTimeStart = System.nanoTime(); |
| 387 | } |
| 388 | |
| 389 | private static int inMegabytes(long value) { |
| 390 | long in_mega_bytes = value / (1024 * 1024); |
| 391 | if (in_mega_bytes > Integer.MAX_VALUE) { |
| 392 | Log.w(TAG, "Recording " + in_mega_bytes + "MB of free space, overflowing range"); |
| 393 | return Integer.MAX_VALUE; |
| 394 | } |
| 395 | return (int)in_mega_bytes; |
| 396 | } |
| 397 | |
| 398 | private void performMetricsLogging() { |
| 399 | long finalTime = System.nanoTime(); |
| 400 | |
| 401 | MetricsLogger.histogram(mContext, "ota_dexopt_available_space_before_mb", |
| 402 | inMegabytes(availableSpaceBefore)); |
| 403 | MetricsLogger.histogram(mContext, "ota_dexopt_available_space_after_bulk_delete_mb", |
| 404 | inMegabytes(availableSpaceAfterBulkDelete)); |
| 405 | MetricsLogger.histogram(mContext, "ota_dexopt_available_space_after_dexopt_mb", |
| 406 | inMegabytes(availableSpaceAfterDexopt)); |
| 407 | |
| 408 | MetricsLogger.histogram(mContext, "ota_dexopt_num_important_packages", |
| 409 | importantPackageCount); |
| 410 | MetricsLogger.histogram(mContext, "ota_dexopt_num_other_packages", otherPackageCount); |
| 411 | |
| 412 | MetricsLogger.histogram(mContext, "ota_dexopt_num_commands", dexoptCommandCountTotal); |
| 413 | MetricsLogger.histogram(mContext, "ota_dexopt_num_commands_executed", |
| 414 | dexoptCommandCountExecuted); |
| 415 | |
| 416 | final int elapsedTimeSeconds = |
| 417 | (int) TimeUnit.NANOSECONDS.toSeconds(finalTime - otaDexoptTimeStart); |
| 418 | MetricsLogger.histogram(mContext, "ota_dexopt_time_s", elapsedTimeSeconds); |
| 419 | } |
| 420 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 421 | private static class OTADexoptPackageDexOptimizer extends |
| 422 | PackageDexOptimizer.ForcedUpdatePackageDexOptimizer { |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 423 | public OTADexoptPackageDexOptimizer(Installer installer, Object installLock, |
| 424 | Context context) { |
| 425 | super(installer, installLock, context, "*otadexopt*"); |
| 426 | } |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 427 | } |
Andreas Gampe | d15300c | 2016-06-23 20:27:12 -0700 | [diff] [blame] | 428 | |
Jeff Sharkey | c98c7bc | 2016-12-07 14:57:34 -0700 | [diff] [blame] | 429 | /** |
| 430 | * Cook up argument list in the format that {@code installd} expects. |
| 431 | */ |
| 432 | private static String buildCommand(Object... args) { |
| 433 | final StringBuilder builder = new StringBuilder(); |
| 434 | for (Object arg : args) { |
| 435 | String escaped; |
| 436 | if (arg == null) { |
| 437 | escaped = ""; |
| 438 | } else { |
| 439 | escaped = String.valueOf(arg); |
| 440 | } |
| 441 | if (escaped.indexOf('\0') != -1 || escaped.indexOf(' ') != -1 || "!".equals(escaped)) { |
| 442 | throw new IllegalArgumentException( |
| 443 | "Invalid argument while executing " + Arrays.toString(args)); |
| 444 | } |
| 445 | if (TextUtils.isEmpty(escaped)) { |
| 446 | escaped = "!"; |
| 447 | } |
| 448 | builder.append(' ').append(escaped); |
Andreas Gampe | d15300c | 2016-06-23 20:27:12 -0700 | [diff] [blame] | 449 | } |
Jeff Sharkey | c98c7bc | 2016-12-07 14:57:34 -0700 | [diff] [blame] | 450 | return builder.toString(); |
Andreas Gampe | d15300c | 2016-06-23 20:27:12 -0700 | [diff] [blame] | 451 | } |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 452 | } |