diff options
| author | 2024-09-05 05:22:04 +0000 | |
|---|---|---|
| committer | 2024-09-05 05:22:04 +0000 | |
| commit | 79afb49c7f7ac25bdef2f3b6a8df3d72c841f93f (patch) | |
| tree | ab2bdb2b29c7883879401cd8537ed857a6750f54 | |
| parent | bb3f3fe7cdf216abe0ec7f07d3f3884c6f3cf91d (diff) | |
| parent | 61d234303a4f4db21952282bb0962c8618b57de6 (diff) | |
Merge "Code refactoring: Move dexopt code to DexOptHelper" into main
| -rw-r--r-- | services/core/java/com/android/server/pm/DexOptHelper.java | 73 | ||||
| -rw-r--r-- | services/core/java/com/android/server/pm/InstallPackageHelper.java | 68 |
2 files changed, 75 insertions, 66 deletions
diff --git a/services/core/java/com/android/server/pm/DexOptHelper.java b/services/core/java/com/android/server/pm/DexOptHelper.java index e34bdc60cfdb..9ecc7b9a805d 100644 --- a/services/core/java/com/android/server/pm/DexOptHelper.java +++ b/services/core/java/com/android/server/pm/DexOptHelper.java @@ -19,6 +19,7 @@ package com.android.server.pm; import static android.content.pm.PackageManager.INSTALL_REASON_DEVICE_RESTORE; import static android.content.pm.PackageManager.INSTALL_REASON_DEVICE_SETUP; import static android.os.Trace.TRACE_TAG_DALVIK; +import static android.os.Trace.TRACE_TAG_PACKAGE_MANAGER; import static android.os.incremental.IncrementalManager.isIncrementalPath; import static com.android.server.LocalManagerRegistry.ManagerNotFoundException; @@ -739,6 +740,78 @@ public final class DexOptHelper { } /** + * Perform dexopt if needed for the installation + */ + static void performDexoptIfNeeded(InstallRequest installRequest, DexManager dexManager, + Context context, PackageManagerTracedLock.RawLock installLock) { + + // Construct the DexoptOptions early to see if we should skip running dexopt. + // + // Do not run PackageDexOptimizer through the local performDexOpt + // method because `pkg` may not be in `mPackages` yet. + // + // Also, don't fail application installs if the dexopt step fails. + DexoptOptions dexoptOptions = getDexoptOptionsByInstallRequest(installRequest, dexManager); + // Check whether we need to dexopt the app. + // + // NOTE: it is IMPORTANT to call dexopt: + // - after doRename which will sync the package data from AndroidPackage and + // its corresponding ApplicationInfo. + // - after installNewPackageLIF or replacePackageLIF which will update result with the + // uid of the application (pkg.applicationInfo.uid). + // This update happens in place! + // + // We only need to dexopt if the package meets ALL of the following conditions: + // 1) it is not an instant app or if it is then dexopt is enabled via gservices. + // 2) it is not debuggable. + // 3) it is not on Incremental File System. + // + // Note that we do not dexopt instant apps by default. dexopt can take some time to + // complete, so we skip this step during installation. Instead, we'll take extra time + // the first time the instant app starts. It's preferred to do it this way to provide + // continuous progress to the useur instead of mysteriously blocking somewhere in the + // middle of running an instant app. The default behaviour can be overridden + // via gservices. + // + // Furthermore, dexopt may be skipped, depending on the install scenario and current + // state of the device. + // + // TODO(b/174695087): instantApp and onIncremental should be removed and their install + // path moved to SCENARIO_FAST. + + final boolean performDexopt = DexOptHelper.shouldPerformDexopt(installRequest, + dexoptOptions, context); + if (performDexopt) { + // dexopt can take long, and ArtService doesn't require installd, so we release + // the lock here and re-acquire the lock after dexopt is finished. + if (installLock != null) { + installLock.unlock(); + } + try { + Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "dexopt"); + + // This mirrors logic from commitReconciledScanResultLocked, where the library + // files needed for dexopt are assigned. + PackageSetting realPkgSetting = installRequest.getRealPackageSetting(); + // Unfortunately, the updated system app flag is only tracked on this + // PackageSetting + boolean isUpdatedSystemApp = + installRequest.getScannedPackageSetting().isUpdatedSystemApp(); + realPkgSetting.getPkgState().setUpdatedSystemApp(isUpdatedSystemApp); + + DexoptResult dexOptResult = DexOptHelper.dexoptPackageUsingArtService( + installRequest, dexoptOptions); + installRequest.onDexoptFinished(dexOptResult); + } finally { + Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER); + if (installLock != null) { + installLock.lock(); + } + } + } + } + + /** * Use ArtService to perform dexopt by the given InstallRequest. */ static DexoptResult dexoptPackageUsingArtService(InstallRequest installRequest, diff --git a/services/core/java/com/android/server/pm/InstallPackageHelper.java b/services/core/java/com/android/server/pm/InstallPackageHelper.java index 662c7929ff81..43a285cba4b9 100644 --- a/services/core/java/com/android/server/pm/InstallPackageHelper.java +++ b/services/core/java/com/android/server/pm/InstallPackageHelper.java @@ -172,11 +172,9 @@ import com.android.internal.util.ArrayUtils; import com.android.internal.util.CollectionUtils; import com.android.server.EventLogTags; import com.android.server.SystemConfig; -import com.android.server.art.model.DexoptResult; import com.android.server.criticalevents.CriticalEventLog; import com.android.server.pm.dex.ArtManagerService; import com.android.server.pm.dex.DexManager; -import com.android.server.pm.dex.DexoptOptions; import com.android.server.pm.parsing.PackageCacher; import com.android.server.pm.parsing.pkg.AndroidPackageUtils; import com.android.server.pm.permission.Permission; @@ -2633,70 +2631,8 @@ final class InstallPackageHelper { pkg.getBaseApkPath(), pkg.getSplitCodePaths()); } - // Construct the DexoptOptions early to see if we should skip running dexopt. - // - // Do not run PackageDexOptimizer through the local performDexOpt - // method because `pkg` may not be in `mPackages` yet. - // - // Also, don't fail application installs if the dexopt step fails. - DexoptOptions dexoptOptions = DexOptHelper.getDexoptOptionsByInstallRequest( - installRequest, mDexManager); - // Check whether we need to dexopt the app. - // - // NOTE: it is IMPORTANT to call dexopt: - // - after doRename which will sync the package data from AndroidPackage and - // its corresponding ApplicationInfo. - // - after installNewPackageLIF or replacePackageLIF which will update result with the - // uid of the application (pkg.applicationInfo.uid). - // This update happens in place! - // - // We only need to dexopt if the package meets ALL of the following conditions: - // 1) it is not an instant app or if it is then dexopt is enabled via gservices. - // 2) it is not debuggable. - // 3) it is not on Incremental File System. - // - // Note that we do not dexopt instant apps by default. dexopt can take some time to - // complete, so we skip this step during installation. Instead, we'll take extra time - // the first time the instant app starts. It's preferred to do it this way to provide - // continuous progress to the useur instead of mysteriously blocking somewhere in the - // middle of running an instant app. The default behaviour can be overridden - // via gservices. - // - // Furthermore, dexopt may be skipped, depending on the install scenario and current - // state of the device. - // - // TODO(b/174695087): instantApp and onIncremental should be removed and their install - // path moved to SCENARIO_FAST. - - final boolean performDexopt = DexOptHelper.shouldPerformDexopt(installRequest, - dexoptOptions, mContext); - if (performDexopt) { - // dexopt can take long, and ArtService doesn't require installd, so we release - // the lock here and re-acquire the lock after dexopt is finished. - PackageManagerTracedLock.RawLock installLock = mPm.mInstallLock.getRawLock(); - installLock.unlock(); - try { - Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "dexopt"); - - // This mirrors logic from commitReconciledScanResultLocked, where the library - // files needed for dexopt are assigned. - PackageSetting realPkgSetting = installRequest.getRealPackageSetting(); - - // Unfortunately, the updated system app flag is only tracked on this - // PackageSetting - boolean isUpdatedSystemApp = - installRequest.getScannedPackageSetting().isUpdatedSystemApp(); - - realPkgSetting.getPkgState().setUpdatedSystemApp(isUpdatedSystemApp); - - DexoptResult dexOptResult = DexOptHelper.dexoptPackageUsingArtService( - installRequest, dexoptOptions); - installRequest.onDexoptFinished(dexOptResult); - Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER); - } finally { - installLock.lock(); - } - } + DexOptHelper.performDexoptIfNeeded(installRequest, mDexManager, mContext, + mPm.mInstallLock.getRawLock()); } PackageManagerServiceUtils.waitForNativeBinariesExtractionForIncremental( incrementalStorages); |