diff options
40 files changed, 2609 insertions, 702 deletions
diff --git a/Android.mk b/Android.mk index 2b12416f8b4e..4e036b598027 100644 --- a/Android.mk +++ b/Android.mk @@ -906,8 +906,7 @@ endef # FRAMEWORKS_BASE_SUBDIRS comes from build/core/pathmap.mk dirs_to_document := \ $(dirs_to_check_apis) \ - $(addprefix ../../, $(FRAMEWORKS_DATA_BINDING_JAVA_SRC_DIRS)) \ - $(addprefix ../../, $(FRAMEWORKS_SUPPORT_JAVA_SRC_DIRS)) \ + $(addprefix ../../, $(FRAMEWORKS_DATA_BINDING_JAVA_SRC_DIRS)) patterns_to_not_document := \ $(call find-no-docs-pattern, $(dirs_to_document)) @@ -942,7 +941,8 @@ framework_docs_LOCAL_DROIDDOC_SOURCE_PATH := \ framework_docs_LOCAL_INTERMEDIATE_SOURCES := \ $(framework_res_source_path)/android/R.java \ $(framework_res_source_path)/android/Manifest.java \ - $(framework_res_source_path)/com/android/internal/R.java + $(framework_res_source_path)/com/android/internal/R.java \ + $(patsubst $(TARGET_OUT_COMMON_INTERMEDIATES)/%,%,$(libcore_to_document_generated)) framework_docs_LOCAL_API_CHECK_JAVA_LIBRARIES := \ core-oj \ @@ -955,6 +955,8 @@ framework_docs_LOCAL_API_CHECK_JAVA_LIBRARIES := \ framework \ voip-common +# Platform docs can refer to Support Library APIs, but we don't actually build +# them as part of the docs target, so we need to include them on the classpath. framework_docs_LOCAL_JAVA_LIBRARIES := \ $(framework_docs_LOCAL_API_CHECK_JAVA_LIBRARIES) \ $(FRAMEWORKS_SUPPORT_JAVA_LIBRARIES) @@ -1001,20 +1003,15 @@ framework_docs_LOCAL_DROIDDOC_OPTIONS := \ -werror -hide 111 -hide 113 -hide 121 \ -overview $(LOCAL_PATH)/core/java/overview.html \ -# Allow the support library to add its own droiddoc options. -include $(LOCAL_PATH)/../support/droiddoc.mk - framework_docs_LOCAL_API_CHECK_ADDITIONAL_JAVA_DIR:= \ $(call intermediates-dir-for,JAVA_LIBRARIES,framework,,COMMON) framework_docs_LOCAL_ADDITIONAL_JAVA_DIR:= \ - $(framework_docs_LOCAL_API_CHECK_ADDITIONAL_JAVA_DIR) \ - $(foreach lib,$(FRAMEWORKS_SUPPORT_JAVA_LIBRARIES),$(call intermediates-dir-for,JAVA_LIBRARIES,$(lib),,COMMON)) \ - $(foreach lib,$(FRAMEWORKS_SUPPORT_JAVA_LIBRARIES),$(call intermediates-dir-for,JAVA_LIBRARIES,$(lib)-res,,COMMON)) + $(framework_docs_LOCAL_API_CHECK_ADDITIONAL_JAVA_DIR) framework_docs_LOCAL_ADDITIONAL_DEPENDENCIES := \ frameworks/base/docs/knowntags.txt \ - libcore/Docs.mk + $(libcore_to_document_generated) samples_dir := development/samples/browseable @@ -1053,6 +1050,11 @@ framework_docs_LOCAL_DROIDDOC_OPTIONS += \ -resourcesdir $(LOCAL_PATH)/docs/html/reference/images/ \ -resourcesoutdir reference/android/images/ +# Federate Support Library references against local API file. +framework_docs_LOCAL_DROIDDOC_OPTIONS += \ + -federate SupportLib https://developer.android.com \ + -federationapi SupportLib prebuilts/sdk/current/support-api.txt + # ==== the api stubs and current.xml =========================== include $(CLEAR_VARS) diff --git a/api/current.txt b/api/current.txt index eca65be47373..3bff61f290e7 100644 --- a/api/current.txt +++ b/api/current.txt @@ -18040,13 +18040,11 @@ package android.icu.text { method public synchronized void applyLocalizedPattern(java.lang.String); method public synchronized void applyPattern(java.lang.String); method public synchronized boolean areSignificantDigitsUsed(); - method public synchronized boolean equals(java.lang.Object); method public java.lang.StringBuffer format(double, java.lang.StringBuffer, java.text.FieldPosition); method public java.lang.StringBuffer format(long, java.lang.StringBuffer, java.text.FieldPosition); method public java.lang.StringBuffer format(java.math.BigInteger, java.lang.StringBuffer, java.text.FieldPosition); method public java.lang.StringBuffer format(java.math.BigDecimal, java.lang.StringBuffer, java.text.FieldPosition); method public java.lang.StringBuffer format(android.icu.math.BigDecimal, java.lang.StringBuffer, java.text.FieldPosition); - method public synchronized android.icu.util.Currency getCurrency(); method public synchronized android.icu.text.CurrencyPluralInfo getCurrencyPluralInfo(); method public synchronized android.icu.util.Currency.CurrencyUsage getCurrencyUsage(); method public synchronized android.icu.text.DecimalFormatSymbols getDecimalFormatSymbols(); @@ -18054,12 +18052,8 @@ package android.icu.text { method public synchronized int getGroupingSize(); method public synchronized java.math.MathContext getMathContext(); method public synchronized android.icu.math.MathContext getMathContextICU(); - method public synchronized int getMaximumFractionDigits(); - method public synchronized int getMaximumIntegerDigits(); method public synchronized int getMaximumSignificantDigits(); method public synchronized byte getMinimumExponentDigits(); - method public synchronized int getMinimumFractionDigits(); - method public synchronized int getMinimumIntegerDigits(); method public synchronized int getMinimumSignificantDigits(); method public synchronized int getMultiplier(); method public synchronized java.lang.String getNegativePrefix(); @@ -18070,19 +18064,13 @@ package android.icu.text { method public synchronized java.lang.String getPositivePrefix(); method public synchronized java.lang.String getPositiveSuffix(); method public synchronized java.math.BigDecimal getRoundingIncrement(); - method public synchronized int getRoundingMode(); method public synchronized int getSecondaryGroupingSize(); - method public synchronized int hashCode(); method public synchronized boolean isDecimalPatternMatchRequired(); method public synchronized boolean isDecimalSeparatorAlwaysShown(); method public synchronized boolean isExponentSignAlwaysShown(); - method public synchronized boolean isGroupingUsed(); method public synchronized boolean isParseBigDecimal(); - method public synchronized boolean isParseIntegerOnly(); - method public synchronized boolean isParseStrict(); method public synchronized boolean isScientificNotation(); method public java.lang.Number parse(java.lang.String, java.text.ParsePosition); - method public synchronized void setCurrency(android.icu.util.Currency); method public synchronized void setCurrencyPluralInfo(android.icu.text.CurrencyPluralInfo); method public synchronized void setCurrencyUsage(android.icu.util.Currency.CurrencyUsage); method public synchronized void setDecimalFormatSymbols(android.icu.text.DecimalFormatSymbols); @@ -18091,15 +18079,10 @@ package android.icu.text { method public synchronized void setExponentSignAlwaysShown(boolean); method public synchronized void setFormatWidth(int); method public synchronized void setGroupingSize(int); - method public synchronized void setGroupingUsed(boolean); method public synchronized void setMathContext(java.math.MathContext); method public synchronized void setMathContextICU(android.icu.math.MathContext); - method public synchronized void setMaximumFractionDigits(int); - method public synchronized void setMaximumIntegerDigits(int); method public synchronized void setMaximumSignificantDigits(int); method public synchronized void setMinimumExponentDigits(byte); - method public synchronized void setMinimumFractionDigits(int); - method public synchronized void setMinimumIntegerDigits(int); method public synchronized void setMinimumSignificantDigits(int); method public synchronized void setMultiplier(int); method public synchronized void setNegativePrefix(java.lang.String); @@ -18107,15 +18090,12 @@ package android.icu.text { method public synchronized void setPadCharacter(char); method public synchronized void setPadPosition(int); method public synchronized void setParseBigDecimal(boolean); - method public synchronized void setParseIntegerOnly(boolean); method public deprecated void setParseMaxDigits(int); - method public synchronized void setParseStrict(boolean); method public synchronized void setPositivePrefix(java.lang.String); method public synchronized void setPositiveSuffix(java.lang.String); method public synchronized void setRoundingIncrement(java.math.BigDecimal); method public synchronized void setRoundingIncrement(android.icu.math.BigDecimal); method public synchronized void setRoundingIncrement(double); - method public synchronized void setRoundingMode(int); method public synchronized void setScientificNotation(boolean); method public synchronized void setSecondaryGroupingSize(int); method public synchronized void setSignificantDigitsUsed(boolean); @@ -25557,7 +25537,6 @@ package android.net { public static final class IpSecManager.SecurityParameterIndex implements java.lang.AutoCloseable { method public void close(); - method protected void finalize(); method public int getSpi(); } diff --git a/api/system-current.txt b/api/system-current.txt index 2e2cb4c56cdf..d2f919702217 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -19548,13 +19548,11 @@ package android.icu.text { method public synchronized void applyLocalizedPattern(java.lang.String); method public synchronized void applyPattern(java.lang.String); method public synchronized boolean areSignificantDigitsUsed(); - method public synchronized boolean equals(java.lang.Object); method public java.lang.StringBuffer format(double, java.lang.StringBuffer, java.text.FieldPosition); method public java.lang.StringBuffer format(long, java.lang.StringBuffer, java.text.FieldPosition); method public java.lang.StringBuffer format(java.math.BigInteger, java.lang.StringBuffer, java.text.FieldPosition); method public java.lang.StringBuffer format(java.math.BigDecimal, java.lang.StringBuffer, java.text.FieldPosition); method public java.lang.StringBuffer format(android.icu.math.BigDecimal, java.lang.StringBuffer, java.text.FieldPosition); - method public synchronized android.icu.util.Currency getCurrency(); method public synchronized android.icu.text.CurrencyPluralInfo getCurrencyPluralInfo(); method public synchronized android.icu.util.Currency.CurrencyUsage getCurrencyUsage(); method public synchronized android.icu.text.DecimalFormatSymbols getDecimalFormatSymbols(); @@ -19562,12 +19560,8 @@ package android.icu.text { method public synchronized int getGroupingSize(); method public synchronized java.math.MathContext getMathContext(); method public synchronized android.icu.math.MathContext getMathContextICU(); - method public synchronized int getMaximumFractionDigits(); - method public synchronized int getMaximumIntegerDigits(); method public synchronized int getMaximumSignificantDigits(); method public synchronized byte getMinimumExponentDigits(); - method public synchronized int getMinimumFractionDigits(); - method public synchronized int getMinimumIntegerDigits(); method public synchronized int getMinimumSignificantDigits(); method public synchronized int getMultiplier(); method public synchronized java.lang.String getNegativePrefix(); @@ -19578,19 +19572,13 @@ package android.icu.text { method public synchronized java.lang.String getPositivePrefix(); method public synchronized java.lang.String getPositiveSuffix(); method public synchronized java.math.BigDecimal getRoundingIncrement(); - method public synchronized int getRoundingMode(); method public synchronized int getSecondaryGroupingSize(); - method public synchronized int hashCode(); method public synchronized boolean isDecimalPatternMatchRequired(); method public synchronized boolean isDecimalSeparatorAlwaysShown(); method public synchronized boolean isExponentSignAlwaysShown(); - method public synchronized boolean isGroupingUsed(); method public synchronized boolean isParseBigDecimal(); - method public synchronized boolean isParseIntegerOnly(); - method public synchronized boolean isParseStrict(); method public synchronized boolean isScientificNotation(); method public java.lang.Number parse(java.lang.String, java.text.ParsePosition); - method public synchronized void setCurrency(android.icu.util.Currency); method public synchronized void setCurrencyPluralInfo(android.icu.text.CurrencyPluralInfo); method public synchronized void setCurrencyUsage(android.icu.util.Currency.CurrencyUsage); method public synchronized void setDecimalFormatSymbols(android.icu.text.DecimalFormatSymbols); @@ -19599,15 +19587,10 @@ package android.icu.text { method public synchronized void setExponentSignAlwaysShown(boolean); method public synchronized void setFormatWidth(int); method public synchronized void setGroupingSize(int); - method public synchronized void setGroupingUsed(boolean); method public synchronized void setMathContext(java.math.MathContext); method public synchronized void setMathContextICU(android.icu.math.MathContext); - method public synchronized void setMaximumFractionDigits(int); - method public synchronized void setMaximumIntegerDigits(int); method public synchronized void setMaximumSignificantDigits(int); method public synchronized void setMinimumExponentDigits(byte); - method public synchronized void setMinimumFractionDigits(int); - method public synchronized void setMinimumIntegerDigits(int); method public synchronized void setMinimumSignificantDigits(int); method public synchronized void setMultiplier(int); method public synchronized void setNegativePrefix(java.lang.String); @@ -19615,15 +19598,12 @@ package android.icu.text { method public synchronized void setPadCharacter(char); method public synchronized void setPadPosition(int); method public synchronized void setParseBigDecimal(boolean); - method public synchronized void setParseIntegerOnly(boolean); method public deprecated void setParseMaxDigits(int); - method public synchronized void setParseStrict(boolean); method public synchronized void setPositivePrefix(java.lang.String); method public synchronized void setPositiveSuffix(java.lang.String); method public synchronized void setRoundingIncrement(java.math.BigDecimal); method public synchronized void setRoundingIncrement(android.icu.math.BigDecimal); method public synchronized void setRoundingIncrement(double); - method public synchronized void setRoundingMode(int); method public synchronized void setScientificNotation(boolean); method public synchronized void setSecondaryGroupingSize(int); method public synchronized void setSignificantDigitsUsed(boolean); @@ -27753,7 +27733,6 @@ package android.net { public static final class IpSecManager.SecurityParameterIndex implements java.lang.AutoCloseable { method public void close(); - method protected void finalize(); method public int getSpi(); } diff --git a/api/test-current.txt b/api/test-current.txt index ba693063a33c..d8a71a53ef2d 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -18092,13 +18092,11 @@ package android.icu.text { method public synchronized void applyLocalizedPattern(java.lang.String); method public synchronized void applyPattern(java.lang.String); method public synchronized boolean areSignificantDigitsUsed(); - method public synchronized boolean equals(java.lang.Object); method public java.lang.StringBuffer format(double, java.lang.StringBuffer, java.text.FieldPosition); method public java.lang.StringBuffer format(long, java.lang.StringBuffer, java.text.FieldPosition); method public java.lang.StringBuffer format(java.math.BigInteger, java.lang.StringBuffer, java.text.FieldPosition); method public java.lang.StringBuffer format(java.math.BigDecimal, java.lang.StringBuffer, java.text.FieldPosition); method public java.lang.StringBuffer format(android.icu.math.BigDecimal, java.lang.StringBuffer, java.text.FieldPosition); - method public synchronized android.icu.util.Currency getCurrency(); method public synchronized android.icu.text.CurrencyPluralInfo getCurrencyPluralInfo(); method public synchronized android.icu.util.Currency.CurrencyUsage getCurrencyUsage(); method public synchronized android.icu.text.DecimalFormatSymbols getDecimalFormatSymbols(); @@ -18106,12 +18104,8 @@ package android.icu.text { method public synchronized int getGroupingSize(); method public synchronized java.math.MathContext getMathContext(); method public synchronized android.icu.math.MathContext getMathContextICU(); - method public synchronized int getMaximumFractionDigits(); - method public synchronized int getMaximumIntegerDigits(); method public synchronized int getMaximumSignificantDigits(); method public synchronized byte getMinimumExponentDigits(); - method public synchronized int getMinimumFractionDigits(); - method public synchronized int getMinimumIntegerDigits(); method public synchronized int getMinimumSignificantDigits(); method public synchronized int getMultiplier(); method public synchronized java.lang.String getNegativePrefix(); @@ -18122,19 +18116,13 @@ package android.icu.text { method public synchronized java.lang.String getPositivePrefix(); method public synchronized java.lang.String getPositiveSuffix(); method public synchronized java.math.BigDecimal getRoundingIncrement(); - method public synchronized int getRoundingMode(); method public synchronized int getSecondaryGroupingSize(); - method public synchronized int hashCode(); method public synchronized boolean isDecimalPatternMatchRequired(); method public synchronized boolean isDecimalSeparatorAlwaysShown(); method public synchronized boolean isExponentSignAlwaysShown(); - method public synchronized boolean isGroupingUsed(); method public synchronized boolean isParseBigDecimal(); - method public synchronized boolean isParseIntegerOnly(); - method public synchronized boolean isParseStrict(); method public synchronized boolean isScientificNotation(); method public java.lang.Number parse(java.lang.String, java.text.ParsePosition); - method public synchronized void setCurrency(android.icu.util.Currency); method public synchronized void setCurrencyPluralInfo(android.icu.text.CurrencyPluralInfo); method public synchronized void setCurrencyUsage(android.icu.util.Currency.CurrencyUsage); method public synchronized void setDecimalFormatSymbols(android.icu.text.DecimalFormatSymbols); @@ -18143,15 +18131,10 @@ package android.icu.text { method public synchronized void setExponentSignAlwaysShown(boolean); method public synchronized void setFormatWidth(int); method public synchronized void setGroupingSize(int); - method public synchronized void setGroupingUsed(boolean); method public synchronized void setMathContext(java.math.MathContext); method public synchronized void setMathContextICU(android.icu.math.MathContext); - method public synchronized void setMaximumFractionDigits(int); - method public synchronized void setMaximumIntegerDigits(int); method public synchronized void setMaximumSignificantDigits(int); method public synchronized void setMinimumExponentDigits(byte); - method public synchronized void setMinimumFractionDigits(int); - method public synchronized void setMinimumIntegerDigits(int); method public synchronized void setMinimumSignificantDigits(int); method public synchronized void setMultiplier(int); method public synchronized void setNegativePrefix(java.lang.String); @@ -18159,15 +18142,12 @@ package android.icu.text { method public synchronized void setPadCharacter(char); method public synchronized void setPadPosition(int); method public synchronized void setParseBigDecimal(boolean); - method public synchronized void setParseIntegerOnly(boolean); method public deprecated void setParseMaxDigits(int); - method public synchronized void setParseStrict(boolean); method public synchronized void setPositivePrefix(java.lang.String); method public synchronized void setPositiveSuffix(java.lang.String); method public synchronized void setRoundingIncrement(java.math.BigDecimal); method public synchronized void setRoundingIncrement(android.icu.math.BigDecimal); method public synchronized void setRoundingIncrement(double); - method public synchronized void setRoundingMode(int); method public synchronized void setScientificNotation(boolean); method public synchronized void setSecondaryGroupingSize(int); method public synchronized void setSignificantDigitsUsed(boolean); @@ -25667,7 +25647,6 @@ package android.net { public static final class IpSecManager.SecurityParameterIndex implements java.lang.AutoCloseable { method public void close(); - method protected void finalize(); method public int getSpi(); } diff --git a/core/java/android/app/DexLoadReporter.java b/core/java/android/app/DexLoadReporter.java index 5f61e07ec46e..d15dd6d71f94 100644 --- a/core/java/android/app/DexLoadReporter.java +++ b/core/java/android/app/DexLoadReporter.java @@ -19,6 +19,7 @@ package android.app; import android.os.FileUtils; import android.os.RemoteException; import android.os.SystemProperties; +import android.system.ErrnoException; import android.util.Slog; import com.android.internal.annotations.GuardedBy; @@ -26,10 +27,11 @@ import com.android.internal.annotations.GuardedBy; import dalvik.system.BaseDexClassLoader; import dalvik.system.VMRuntime; +import libcore.io.Libcore; + import java.io.File; import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -114,13 +116,18 @@ import java.util.Set; registerSecondaryDexForProfiling(dexPathsForRegistration); } - private void notifyPackageManager(List<BaseDexClassLoader> ignored, + private void notifyPackageManager(List<BaseDexClassLoader> classLoadersChain, List<String> classPaths) { + // Get the class loader names for the binder call. + List<String> classLoadersNames = new ArrayList<>(classPaths.size()); + for (BaseDexClassLoader bdc : classLoadersChain) { + classLoadersNames.add(bdc.getClass().getName()); + } String packageName = ActivityThread.currentPackageName(); try { // Notify only the paths of the first class loader for now. ActivityThread.getPackageManager().notifyDexLoad( - packageName, Arrays.asList(classPaths.get(0).split(File.pathSeparator)), + packageName, classLoadersNames, classPaths, VMRuntime.getRuntime().vmInstructionSet()); } catch (RemoteException re) { Slog.e(TAG, "Failed to notify PM about dex load for package " + packageName, re); @@ -148,22 +155,50 @@ import java.util.Set; // The dex path is not a secondary dex file. Nothing to do. return; } - File secondaryProfile = getSecondaryProfileFile(dexPath); + + File realDexPath; + try { + // Secondary dex profiles are stored in the oat directory, next to the real dex file + // and have the same name with 'cur.prof' appended. We use the realpath because that + // is what installd is using when processing the dex file. + // NOTE: Keep in sync with installd. + realDexPath = new File(Libcore.os.realpath(dexPath)); + } catch (ErrnoException ex) { + Slog.e(TAG, "Failed to get the real path of secondary dex " + dexPath + + ":" + ex.getMessage()); + // Do not continue with registration if we could not retrieve the real path. + return; + } + + // NOTE: Keep this in sync with installd expectations. + File secondaryProfileDir = new File(realDexPath.getParent(), "oat"); + File secondaryProfile = new File(secondaryProfileDir, realDexPath.getName() + ".cur.prof"); + + // Create the profile if not already there. + // Returns true if the file was created, false if the file already exists. + // or throws exceptions in case of errors. + if (!secondaryProfileDir.exists()) { + if (!secondaryProfileDir.mkdir()) { + Slog.e(TAG, "Could not create the profile directory: " + secondaryProfile); + // Do not continue with registration if we could not create the oat dir. + return; + } + } + try { - // Create the profile if not already there. - // Returns true if the file was created, false if the file already exists. - // or throws exceptions in case of errors. boolean created = secondaryProfile.createNewFile(); if (DEBUG && created) { Slog.i(TAG, "Created profile for secondary dex: " + secondaryProfile); } } catch (IOException ex) { - Slog.e(TAG, "Failed to create profile for secondary dex " + secondaryProfile + - ":" + ex.getMessage()); - // Don't move forward with the registration if we failed to create the profile. + Slog.e(TAG, "Failed to create profile for secondary dex " + dexPath + + ":" + ex.getMessage()); + // Do not continue with registration if we could not create the profile files. return; } + // If we got here, the dex paths is a secondary dex and we were able to create the profile. + // Register the path to the runtime. VMRuntime.registerAppInfo(secondaryProfile.getPath(), new String[] { dexPath }); } @@ -177,11 +212,4 @@ import java.util.Set; } return false; } - - // Secondary dex profiles are stored next to the dex file and have the same - // name with '.prof' appended. - // NOTE: Keep in sync with installd. - private File getSecondaryProfileFile(String dexPath) { - return new File(dexPath + ".prof"); - } } diff --git a/core/java/android/app/usage/ExternalStorageStats.java b/core/java/android/app/usage/ExternalStorageStats.java index d7e570fea4d7..f00e5c29cd75 100644 --- a/core/java/android/app/usage/ExternalStorageStats.java +++ b/core/java/android/app/usage/ExternalStorageStats.java @@ -33,6 +33,7 @@ public final class ExternalStorageStats implements Parcelable { /** {@hide} */ public long videoBytes; /** {@hide} */ public long imageBytes; /** {@hide} */ public long appBytes; + /** {@hide} */ public long obbBytes; /** * Return the total bytes used by all files in the shared/external storage @@ -97,6 +98,11 @@ public final class ExternalStorageStats implements Parcelable { } /** {@hide} */ + public @BytesLong long getObbBytes() { + return obbBytes; + } + + /** {@hide} */ public ExternalStorageStats() { } @@ -107,6 +113,7 @@ public final class ExternalStorageStats implements Parcelable { this.videoBytes = in.readLong(); this.imageBytes = in.readLong(); this.appBytes = in.readLong(); + this.obbBytes = in.readLong(); } @Override @@ -121,6 +128,7 @@ public final class ExternalStorageStats implements Parcelable { dest.writeLong(videoBytes); dest.writeLong(imageBytes); dest.writeLong(appBytes); + dest.writeLong(obbBytes); } public static final Creator<ExternalStorageStats> CREATOR = new Creator<ExternalStorageStats>() { diff --git a/core/java/android/content/pm/IPackageManager.aidl b/core/java/android/content/pm/IPackageManager.aidl index 4b44a177ec20..c9afd6b7e930 100644 --- a/core/java/android/content/pm/IPackageManager.aidl +++ b/core/java/android/content/pm/IPackageManager.aidl @@ -469,11 +469,19 @@ interface IPackageManager { * Notify the package manager that a list of dex files have been loaded. * * @param loadingPackageName the name of the package who performs the load - * @param dexPats the list of the dex files paths that have been loaded + * @param classLoadersNames the names of the class loaders present in the loading chain. The + * list encodes the class loader chain in the natural order. The first class loader has + * the second one as its parent and so on. The dex files present in the class path of the + * first class loader will be recorded in the usage file. + * @param classPaths the class paths corresponding to the class loaders names from + * {@param classLoadersNames}. The the first element corresponds to the first class loader + * and so on. A classpath is represented as a list of dex files separated by + * {@code File.pathSeparator}. + * The dex files found in the first class path will be recorded in the usage file. * @param loaderIsa the ISA of the loader process */ - oneway void notifyDexLoad(String loadingPackageName, in List<String> dexPaths, - String loaderIsa); + oneway void notifyDexLoad(String loadingPackageName, in List<String> classLoadersNames, + in List<String> classPaths, String loaderIsa); /** * Register an application dex module with the package manager. @@ -508,21 +516,13 @@ interface IPackageManager { in boolean isSharedModule, IDexModuleRegisterCallback callback); /** - * Ask the package manager to perform a dex-opt for the given reason. The package - * manager will map the reason to a compiler filter according to the current system - * configuration. - */ - boolean performDexOpt(String packageName, boolean checkProfiles, - int compileReason, boolean force, boolean bootComplete); - - /** * Ask the package manager to perform a dex-opt with the given compiler filter. * * Note: exposed only for the shell command to allow moving packages explicitly to a * definite state. */ boolean performDexOptMode(String packageName, boolean checkProfiles, - String targetCompilerFilter, boolean force, boolean bootComplete); + String targetCompilerFilter, boolean force, boolean bootComplete, String splitName); /** * Ask the package manager to perform a dex-opt with the given compiler filter on the diff --git a/core/java/android/net/OWNERS b/core/java/android/net/OWNERS index 0f1e2597f39e..d1ce60e9ff08 100644 --- a/core/java/android/net/OWNERS +++ b/core/java/android/net/OWNERS @@ -1,6 +1,6 @@ ek@google.com hugobenichi@google.com -jsharkey@google.com +jsharkey@android.com lorenzo@google.com satk@google.com silberst@google.com diff --git a/core/java/android/os/Debug.java b/core/java/android/os/Debug.java index 75fea5253674..2efde23275ab 100644 --- a/core/java/android/os/Debug.java +++ b/core/java/android/os/Debug.java @@ -23,7 +23,6 @@ import android.util.Log; import com.android.internal.util.FastPrintWriter; import com.android.internal.util.TypedProperties; -import dalvik.bytecode.OpcodeInfo; import dalvik.system.VMDebug; import org.apache.harmony.dalvik.ddmc.Chunk; @@ -48,8 +47,6 @@ import java.util.HashMap; import java.util.Map; - - /** * Provides various debugging methods for Android applications, including * tracing and allocation counts. @@ -1948,13 +1945,7 @@ public final class Debug */ @Deprecated public static class InstructionCount { - private static final int NUM_INSTR = - OpcodeInfo.MAXIMUM_PACKED_VALUE + 1; - - private int[] mCounts; - public InstructionCount() { - mCounts = new int[NUM_INSTR]; } /** @@ -1964,13 +1955,7 @@ public final class Debug * @return true if counting was started */ public boolean resetAndStart() { - try { - VMDebug.startInstructionCounting(); - VMDebug.resetInstructionCount(); - } catch (UnsupportedOperationException uoe) { - return false; - } - return true; + return false; } /** @@ -1978,13 +1963,7 @@ public final class Debug * counting process. */ public boolean collect() { - try { - VMDebug.stopInstructionCounting(); - VMDebug.getInstructionCount(mCounts); - } catch (UnsupportedOperationException uoe) { - return false; - } - return true; + return false; } /** @@ -1992,13 +1971,7 @@ public final class Debug * all threads). */ public int globalTotal() { - int count = 0; - - for (int i = 0; i < NUM_INSTR; i++) { - count += mCounts[i]; - } - - return count; + return 0; } /** @@ -2006,15 +1979,7 @@ public final class Debug * executed globally. */ public int globalMethodInvocations() { - int count = 0; - - for (int i = 0; i < NUM_INSTR; i++) { - if (OpcodeInfo.isInvoke(i)) { - count += mCounts[i]; - } - } - - return count; + return 0; } } diff --git a/core/java/android/os/HwBinder.java b/core/java/android/os/HwBinder.java index 270e63f408a7..5e2a0815b60d 100644 --- a/core/java/android/os/HwBinder.java +++ b/core/java/android/os/HwBinder.java @@ -16,10 +16,10 @@ package android.os; -import java.util.ArrayList; -import java.util.NoSuchElementException; import libcore.util.NativeAllocationRegistry; +import java.util.NoSuchElementException; + /** @hide */ public abstract class HwBinder implements IHwBinder { private static final String TAG = "HwBinder"; @@ -46,9 +46,16 @@ public abstract class HwBinder implements IHwBinder { public native final void registerService(String serviceName) throws RemoteException; - public static native final IHwBinder getService( + public static final IHwBinder getService( String iface, String serviceName) + throws RemoteException, NoSuchElementException { + return getService(iface, serviceName, false /* retry */); + } + public static native final IHwBinder getService( + String iface, + String serviceName, + boolean retry) throws RemoteException, NoSuchElementException; public static native final void configureRpcThreadpool( diff --git a/core/java/android/os/HwBlob.java b/core/java/android/os/HwBlob.java index 88226f0a1665..5e9b9ae3d49a 100644 --- a/core/java/android/os/HwBlob.java +++ b/core/java/android/os/HwBlob.java @@ -43,6 +43,18 @@ public class HwBlob { public native final double getDouble(long offset); public native final String getString(long offset); + /** + The copyTo... methods copy the blob's data, starting from the given + byte offset, into the array. A total of "size" _elements_ are copied. + */ + public native final void copyToBoolArray(long offset, boolean[] array, int size); + public native final void copyToInt8Array(long offset, byte[] array, int size); + public native final void copyToInt16Array(long offset, short[] array, int size); + public native final void copyToInt32Array(long offset, int[] array, int size); + public native final void copyToInt64Array(long offset, long[] array, int size); + public native final void copyToFloatArray(long offset, float[] array, int size); + public native final void copyToDoubleArray(long offset, double[] array, int size); + public native final void putBool(long offset, boolean x); public native final void putInt8(long offset, byte x); public native final void putInt16(long offset, short x); @@ -52,6 +64,14 @@ public class HwBlob { public native final void putDouble(long offset, double x); public native final void putString(long offset, String x); + public native final void putBoolArray(long offset, boolean[] x); + public native final void putInt8Array(long offset, byte[] x); + public native final void putInt16Array(long offset, short[] x); + public native final void putInt32Array(long offset, int[] x); + public native final void putInt64Array(long offset, long[] x); + public native final void putFloatArray(long offset, float[] x); + public native final void putDoubleArray(long offset, double[] x); + public native final void putBlob(long offset, HwBlob blob); public native final long handle(); diff --git a/core/java/com/android/internal/app/IMediaContainerService.aidl b/core/java/com/android/internal/app/IMediaContainerService.aidl index 81ea191462f0..36e4c1c61f0e 100644 --- a/core/java/com/android/internal/app/IMediaContainerService.aidl +++ b/core/java/com/android/internal/app/IMediaContainerService.aidl @@ -27,9 +27,6 @@ interface IMediaContainerService { PackageInfoLite getMinimalPackageInfo(String packagePath, int flags, String abiOverride); ObbInfo getObbInfo(String filename); - long calculateDirectorySize(String directory); - /** Return file system stats: [0] is total bytes, [1] is available bytes */ - long[] getFileSystemStats(String path); void clearDirectory(String directory); long calculateInstalledSize(String packagePath, boolean isForwardLocked, String abiOverride); } diff --git a/core/java/com/android/internal/net/OWNERS b/core/java/com/android/internal/net/OWNERS index 7cb32ff793e7..e2064a8099ae 100644 --- a/core/java/com/android/internal/net/OWNERS +++ b/core/java/com/android/internal/net/OWNERS @@ -2,5 +2,5 @@ set noparent ek@google.com hugobenichi@google.com -jsharkey@google.com +jsharkey@android.com lorenzo@google.com diff --git a/core/java/com/android/internal/os/ZygoteInit.java b/core/java/com/android/internal/os/ZygoteInit.java index 6d8b81170e21..3cda820eb276 100644 --- a/core/java/com/android/internal/os/ZygoteInit.java +++ b/core/java/com/android/internal/os/ZygoteInit.java @@ -546,13 +546,14 @@ public class ZygoteInit { for (String classPathElement : classPathElements) { // System server is fully AOTed and never profiled // for profile guided compilation. - // TODO: Make this configurable between INTERPRET_ONLY, SPEED, SPACE and EVERYTHING? + String systemServerFilter = SystemProperties.get( + "dalvik.vm.systemservercompilerfilter", "speed"); int dexoptNeeded; try { dexoptNeeded = DexFile.getDexOptNeeded( - classPathElement, instructionSet, "speed", - false /* newProfile */, false /* downgrade */); + classPathElement, instructionSet, systemServerFilter, + null /* classLoaderContext */, false /* newProfile */, false /* downgrade */); } catch (FileNotFoundException ignored) { // Do not add to the classpath. Log.w(TAG, "Missing classpath element for system server: " + classPathElement); @@ -570,13 +571,13 @@ public class ZygoteInit { final String packageName = "*"; final String outputPath = null; final int dexFlags = 0; - final String compilerFilter = "speed"; + final String compilerFilter = systemServerFilter; final String uuid = StorageManager.UUID_PRIVATE_INTERNAL; final String seInfo = null; try { installd.dexopt(classPathElement, Process.SYSTEM_UID, packageName, instructionSet, dexoptNeeded, outputPath, dexFlags, compilerFilter, - uuid, sharedLibraries, seInfo); + uuid, sharedLibraries, seInfo, false /* downgrade */); } catch (RemoteException | ServiceSpecificException e) { // Ignore (but log), we need this on the classpath for fallback mode. Log.w(TAG, "Failed compiling classpath element for system server: " diff --git a/core/jni/android_os_HwBinder.cpp b/core/jni/android_os_HwBinder.cpp index c4f22eeb8b95..08d952791ef6 100644 --- a/core/jni/android_os_HwBinder.cpp +++ b/core/jni/android_os_HwBinder.cpp @@ -296,7 +296,8 @@ static jobject JHwBinder_native_getService( JNIEnv *env, jclass /* clazzObj */, jstring ifaceNameObj, - jstring serviceNameObj) { + jstring serviceNameObj, + jboolean retry) { using ::android::hidl::base::V1_0::IBase; using ::android::hardware::details::getRawServiceInternal; @@ -319,8 +320,7 @@ static jobject JHwBinder_native_getService( serviceName = str.c_str(); } - // TODO(b/67981006): true /* retry */ - sp<IBase> ret = getRawServiceInternal(ifaceName, serviceName, false /* retry */, false /* getStub */); + sp<IBase> ret = getRawServiceInternal(ifaceName, serviceName, retry /* retry */, false /* getStub */); sp<hardware::IBinder> service = hardware::toBinder<hidl::base::V1_0::IBase>(ret); if (service == NULL) { @@ -360,7 +360,7 @@ static JNINativeMethod gMethods[] = { { "registerService", "(Ljava/lang/String;)V", (void *)JHwBinder_native_registerService }, - { "getService", "(Ljava/lang/String;Ljava/lang/String;)L" PACKAGE_PATH "/IHwBinder;", + { "getService", "(Ljava/lang/String;Ljava/lang/String;Z)L" PACKAGE_PATH "/IHwBinder;", (void *)JHwBinder_native_getService }, { "configureRpcThreadpool", "(JZ)V", diff --git a/core/jni/android_os_HwBlob.cpp b/core/jni/android_os_HwBlob.cpp index 40d49b7662d9..737ec47e8ffb 100644 --- a/core/jni/android_os_HwBlob.cpp +++ b/core/jni/android_os_HwBlob.cpp @@ -60,12 +60,12 @@ sp<JHwBlob> JHwBlob::SetNativeContext( JNIEnv *env, jobject thiz, const sp<JHwBlob> &context) { sp<JHwBlob> old = (JHwBlob *)env->GetLongField(thiz, gFields.contextID); - if (context != NULL) { - context->incStrong(NULL /* id */); + if (context != nullptr) { + context->incStrong(nullptr /* id */); } - if (old != NULL) { - old->decStrong(NULL /* id */); + if (old != nullptr) { + old->decStrong(nullptr /* id */); } env->SetLongField(thiz, gFields.contextID, (long)context.get()); @@ -150,6 +150,10 @@ const void *JHwBlob::data() const { return mBuffer; } +void *JHwBlob::data() { + return mBuffer; +} + size_t JHwBlob::size() const { return mSize; } @@ -242,8 +246,8 @@ using namespace android; static void releaseNativeContext(void *nativeContext) { sp<JHwBlob> parcel = (JHwBlob *)nativeContext; - if (parcel != NULL) { - parcel->decStrong(NULL /* id */); + if (parcel != nullptr) { + parcel->decStrong(nullptr /* id */); } } @@ -313,6 +317,82 @@ static jstring JHwBlob_native_getString( return env->NewStringUTF(s->c_str()); } +#define DEFINE_BLOB_ARRAY_COPIER(Suffix,Type,NewType) \ +static void JHwBlob_native_copyTo ## Suffix ## Array( \ + JNIEnv *env, \ + jobject thiz, \ + jlong offset, \ + Type ## Array array, \ + jint size) { \ + if (array == nullptr) { \ + jniThrowException(env, "java/lang/NullPointerException", nullptr); \ + return; \ + } \ + \ + if (env->GetArrayLength(array) < size) { \ + signalExceptionForError(env, BAD_VALUE); \ + return; \ + } \ + \ + sp<JHwBlob> blob = JHwBlob::GetNativeContext(env, thiz); \ + \ + if ((offset + size * sizeof(Type)) > blob->size()) { \ + signalExceptionForError(env, -ERANGE); \ + return; \ + } \ + \ + env->Set ## NewType ## ArrayRegion( \ + array, \ + 0 /* start */, \ + size, \ + reinterpret_cast<const Type *>( \ + static_cast<const uint8_t *>(blob->data()) + offset)); \ +} + +static void JHwBlob_native_copyToBoolArray( + JNIEnv *env, + jobject thiz, + jlong offset, + jbooleanArray array, + jint size) { + if (array == nullptr) { + jniThrowException(env, "java/lang/NullPointerException", nullptr); + return; + } + + if (env->GetArrayLength(array) < size) { + signalExceptionForError(env, BAD_VALUE); + return; + } + + sp<JHwBlob> blob = JHwBlob::GetNativeContext(env, thiz); + + if ((offset + size * sizeof(bool)) > blob->size()) { + signalExceptionForError(env, -ERANGE); + return; + } + + const bool *src = + reinterpret_cast<const bool *>( + static_cast<const uint8_t *>(blob->data()) + offset); + + jboolean *dst = env->GetBooleanArrayElements(array, nullptr /* isCopy */); + + for (jint i = 0; i < size; ++i) { + dst[i] = src[i]; + } + + env->ReleaseBooleanArrayElements(array, dst, 0 /* mode */); + dst = nullptr; +} + +DEFINE_BLOB_ARRAY_COPIER(Int8,jbyte,Byte) +DEFINE_BLOB_ARRAY_COPIER(Int16,jshort,Short) +DEFINE_BLOB_ARRAY_COPIER(Int32,jint,Int) +DEFINE_BLOB_ARRAY_COPIER(Int64,jlong,Long) +DEFINE_BLOB_ARRAY_COPIER(Float,jfloat,Float) +DEFINE_BLOB_ARRAY_COPIER(Double,jdouble,Double) + #define DEFINE_BLOB_PUTTER(Suffix,Type) \ static void JHwBlob_native_put ## Suffix( \ JNIEnv *env, jobject thiz, jlong offset, Type x) { \ @@ -375,6 +455,72 @@ static void JHwBlob_native_putString( blob->putBlob(offset + hidl_string::kOffsetOfBuffer, subBlob); } +#define DEFINE_BLOB_ARRAY_PUTTER(Suffix,Type,NewType) \ +static void JHwBlob_native_put ## Suffix ## Array( \ + JNIEnv *env, jobject thiz, jlong offset, Type ## Array array) { \ + \ + if (array == nullptr) { \ + jniThrowException(env, "java/lang/NullPointerException", nullptr); \ + return; \ + } \ + \ + sp<JHwBlob> blob = JHwBlob::GetNativeContext(env, thiz); \ + \ + jsize len = env->GetArrayLength(array); \ + \ + Type *src = \ + env->Get ## NewType ## ArrayElements(array, nullptr /* isCopy */); \ + \ + status_t err = blob->write(offset, src, len * sizeof(Type)); \ + \ + env->Release ## NewType ## ArrayElements(array, src, 0 /* mode */); \ + src = nullptr; \ + \ + if (err != OK) { \ + signalExceptionForError(env, err); \ + } \ +} + +DEFINE_BLOB_ARRAY_PUTTER(Int8,jbyte,Byte) +DEFINE_BLOB_ARRAY_PUTTER(Int16,jshort,Short) +DEFINE_BLOB_ARRAY_PUTTER(Int32,jint,Int) +DEFINE_BLOB_ARRAY_PUTTER(Int64,jlong,Long) +DEFINE_BLOB_ARRAY_PUTTER(Float,jfloat,Float) +DEFINE_BLOB_ARRAY_PUTTER(Double,jdouble,Double) + +static void JHwBlob_native_putBoolArray( + JNIEnv *env, jobject thiz, jlong offset, jbooleanArray array) { + + if (array == nullptr) { + jniThrowException(env, "java/lang/NullPointerException", nullptr); + return; + } + + sp<JHwBlob> blob = JHwBlob::GetNativeContext(env, thiz); + + jsize len = env->GetArrayLength(array); + + if ((offset + len * sizeof(bool)) > blob->size()) { + signalExceptionForError(env, -ERANGE); + return; + } + + const jboolean *src = + env->GetBooleanArrayElements(array, nullptr /* isCopy */); + + bool *dst = reinterpret_cast<bool *>( + static_cast<uint8_t *>(blob->data()) + offset); + + for (jsize i = 0; i < len; ++i) { + dst[i] = src[i]; + } + + env->ReleaseBooleanArrayElements( + array, const_cast<jboolean *>(src), 0 /* mode */); + + src = nullptr; +} + static void JHwBlob_native_putBlob( JNIEnv *env, jobject thiz, jlong offset, jobject blobObj) { if (blobObj == nullptr) { @@ -413,6 +559,14 @@ static JNINativeMethod gMethods[] = { { "getDouble", "(J)D", (void *)JHwBlob_native_getDouble }, { "getString", "(J)Ljava/lang/String;", (void *)JHwBlob_native_getString }, + { "copyToBoolArray", "(J[ZI)V", (void *)JHwBlob_native_copyToBoolArray }, + { "copyToInt8Array", "(J[BI)V", (void *)JHwBlob_native_copyToInt8Array }, + { "copyToInt16Array", "(J[SI)V", (void *)JHwBlob_native_copyToInt16Array }, + { "copyToInt32Array", "(J[II)V", (void *)JHwBlob_native_copyToInt32Array }, + { "copyToInt64Array", "(J[JI)V", (void *)JHwBlob_native_copyToInt64Array }, + { "copyToFloatArray", "(J[FI)V", (void *)JHwBlob_native_copyToFloatArray }, + { "copyToDoubleArray", "(J[DI)V", (void *)JHwBlob_native_copyToDoubleArray }, + { "putBool", "(JZ)V", (void *)JHwBlob_native_putBool }, { "putInt8", "(JB)V", (void *)JHwBlob_native_putInt8 }, { "putInt16", "(JS)V", (void *)JHwBlob_native_putInt16 }, @@ -422,6 +576,14 @@ static JNINativeMethod gMethods[] = { { "putDouble", "(JD)V", (void *)JHwBlob_native_putDouble }, { "putString", "(JLjava/lang/String;)V", (void *)JHwBlob_native_putString }, + { "putBoolArray", "(J[Z)V", (void *)JHwBlob_native_putBoolArray }, + { "putInt8Array", "(J[B)V", (void *)JHwBlob_native_putInt8Array }, + { "putInt16Array", "(J[S)V", (void *)JHwBlob_native_putInt16Array }, + { "putInt32Array", "(J[I)V", (void *)JHwBlob_native_putInt32Array }, + { "putInt64Array", "(J[J)V", (void *)JHwBlob_native_putInt64Array }, + { "putFloatArray", "(J[F)V", (void *)JHwBlob_native_putFloatArray }, + { "putDoubleArray", "(J[D)V", (void *)JHwBlob_native_putDoubleArray }, + { "putBlob", "(JL" PACKAGE_PATH "/HwBlob;)V", (void *)JHwBlob_native_putBlob }, diff --git a/core/jni/android_os_HwBlob.h b/core/jni/android_os_HwBlob.h index 39393cb76c23..6b1db639ac19 100644 --- a/core/jni/android_os_HwBlob.h +++ b/core/jni/android_os_HwBlob.h @@ -50,6 +50,8 @@ struct JHwBlob : public RefBase { size_t offset, const android::hardware::hidl_string **s) const; const void *data() const; + void *data(); + size_t size() const; status_t putBlob(size_t offset, const sp<JHwBlob> &blob); diff --git a/packages/DefaultContainerService/src/com/android/defcontainer/DefaultContainerService.java b/packages/DefaultContainerService/src/com/android/defcontainer/DefaultContainerService.java index 934787774637..3800e6f7e509 100644 --- a/packages/DefaultContainerService/src/com/android/defcontainer/DefaultContainerService.java +++ b/packages/DefaultContainerService/src/com/android/defcontainer/DefaultContainerService.java @@ -213,27 +213,6 @@ public class DefaultContainerService extends IntentService { } @Override - public long calculateDirectorySize(String path) throws RemoteException { - Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); - - final File dir = Environment.maybeTranslateEmulatedPathToInternal(new File(path)); - if (dir.exists() && dir.isDirectory()) { - final String targetPath = dir.getAbsolutePath(); - return MeasurementUtils.measureDirectory(targetPath); - } else { - return 0L; - } - } - - @Override - public long[] getFileSystemStats(String path) { - Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); - - final File file = new File(path); - return new long[] { file.getTotalSpace(), file.getUsableSpace() }; - } - - @Override public void clearDirectory(String path) throws RemoteException { Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/PanProfile.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/PanProfile.java index 7bda2314f4c4..3299cb2d1221 100755 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/PanProfile.java +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/PanProfile.java @@ -32,7 +32,7 @@ import java.util.List; /** * PanProfile handles Bluetooth PAN profile (NAP and PANU). */ -public final class PanProfile implements LocalBluetoothProfile { +public class PanProfile implements LocalBluetoothProfile { private static final String TAG = "PanProfile"; private static boolean V = true; diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java index e2ebbeb411fb..9ac08f97c8f0 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java @@ -51,7 +51,7 @@ public class CachedBluetoothDeviceTest { @Mock private A2dpProfile mA2dpProfile; @Mock - private HidProfile mHidProfile; + private PanProfile mPanProfile; @Mock private BluetoothDevice mDevice; private CachedBluetoothDevice mCachedDevice; @@ -65,7 +65,7 @@ public class CachedBluetoothDeviceTest { when(mAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON); when(mHfpProfile.isProfileReady()).thenReturn(true); when(mA2dpProfile.isProfileReady()).thenReturn(true); - when(mHidProfile.isProfileReady()).thenReturn(true); + when(mPanProfile.isProfileReady()).thenReturn(true); mCachedDevice = spy( new CachedBluetoothDevice(mContext, mAdapter, mProfileManager, mDevice)); doAnswer((invocation) -> mBatteryLevel).when(mCachedDevice).getBatteryLevel(); @@ -83,37 +83,37 @@ public class CachedBluetoothDeviceTest { @Test public void testGetConnectionSummary_testSingleProfileConnectDisconnect() { // Test without battery level - // Set HID profile to be connected and test connection state summary - mCachedDevice.onProfileStateChanged(mHidProfile, BluetoothProfile.STATE_CONNECTED); + // Set PAN profile to be connected and test connection state summary + mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_CONNECTED); assertThat(mCachedDevice.getConnectionSummary()).isEqualTo(mContext.getString( R.string.bluetooth_connected)); - // Set HID profile to be disconnected and test connection state summary - mCachedDevice.onProfileStateChanged(mHidProfile, BluetoothProfile.STATE_DISCONNECTED); + // Set PAN profile to be disconnected and test connection state summary + mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED); assertThat(mCachedDevice.getConnectionSummary()).isNull(); // Test with battery level mBatteryLevel = 10; - // Set HID profile to be connected and test connection state summary - mCachedDevice.onProfileStateChanged(mHidProfile, BluetoothProfile.STATE_CONNECTED); + // Set PAN profile to be connected and test connection state summary + mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_CONNECTED); assertThat(mCachedDevice.getConnectionSummary()).isEqualTo(mContext.getString( R.string.bluetooth_connected_battery_level, com.android.settingslib.Utils.formatPercentage(mBatteryLevel))); - // Set HID profile to be disconnected and test connection state summary - mCachedDevice.onProfileStateChanged(mHidProfile, BluetoothProfile.STATE_DISCONNECTED); + // Set PAN profile to be disconnected and test connection state summary + mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED); assertThat(mCachedDevice.getConnectionSummary()).isNull(); // Test with BluetoothDevice.BATTERY_LEVEL_UNKNOWN battery level mBatteryLevel = BluetoothDevice.BATTERY_LEVEL_UNKNOWN; - // Set HID profile to be connected and test connection state summary - mCachedDevice.onProfileStateChanged(mHidProfile, BluetoothProfile.STATE_CONNECTED); + // Set PAN profile to be connected and test connection state summary + mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_CONNECTED); assertThat(mCachedDevice.getConnectionSummary()).isEqualTo(mContext.getString( R.string.bluetooth_connected)); - // Set HID profile to be disconnected and test connection state summary - mCachedDevice.onProfileStateChanged(mHidProfile, BluetoothProfile.STATE_DISCONNECTED); + // Set PAN profile to be disconnected and test connection state summary + mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED); assertThat(mCachedDevice.getConnectionSummary()).isNull(); } @@ -121,10 +121,10 @@ public class CachedBluetoothDeviceTest { public void testGetConnectionSummary_testMultipleProfileConnectDisconnect() { mBatteryLevel = 10; - // Set HFP, A2DP and HID profile to be connected and test connection state summary + // Set HFP, A2DP and PAN profile to be connected and test connection state summary mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); - mCachedDevice.onProfileStateChanged(mHidProfile, BluetoothProfile.STATE_CONNECTED); + mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_CONNECTED); assertThat(mCachedDevice.getConnectionSummary()).isEqualTo(mContext.getString( R.string.bluetooth_connected_battery_level, com.android.settingslib.Utils.formatPercentage(mBatteryLevel))); @@ -149,7 +149,7 @@ public class CachedBluetoothDeviceTest { com.android.settingslib.Utils.formatPercentage(mBatteryLevel))); // Disconnect all profiles and test connection state summary - mCachedDevice.onProfileStateChanged(mHidProfile, BluetoothProfile.STATE_DISCONNECTED); + mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED); assertThat(mCachedDevice.getConnectionSummary()).isNull(); } } diff --git a/services/core/java/com/android/server/net/OWNERS b/services/core/java/com/android/server/net/OWNERS index 061fd8da9d60..6b77d8326a39 100644 --- a/services/core/java/com/android/server/net/OWNERS +++ b/services/core/java/com/android/server/net/OWNERS @@ -2,7 +2,7 @@ set noparent ek@google.com hugobenichi@google.com -jsharkey@google.com +jsharkey@android.com lorenzo@google.com satk@google.com silberst@google.com diff --git a/services/core/java/com/android/server/pm/BackgroundDexOptService.java b/services/core/java/com/android/server/pm/BackgroundDexOptService.java index 6749afb4fee0..e5c48ccf5442 100644 --- a/services/core/java/com/android/server/pm/BackgroundDexOptService.java +++ b/services/core/java/com/android/server/pm/BackgroundDexOptService.java @@ -18,7 +18,6 @@ package com.android.server.pm; import static com.android.server.pm.PackageManagerService.DEBUG_DEXOPT; -import android.app.AlarmManager; import android.app.job.JobInfo; import android.app.job.JobParameters; import android.app.job.JobScheduler; @@ -38,8 +37,10 @@ import android.util.Log; import com.android.server.pm.dex.DexManager; import com.android.server.LocalServices; import com.android.server.PinnerService; +import com.android.server.pm.dex.DexoptOptions; import java.io.File; +import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.TimeUnit; @@ -73,6 +74,9 @@ public class BackgroundDexOptService extends JobService { // Optimizations should be aborted. No space left on device. private static final int OPTIMIZE_ABORT_NO_SPACE_LEFT = 3; + // Used for calculating space threshold for downgrading unused apps. + private static final int LOW_THRESHOLD_MULTIPLIER_FOR_DOWNGRADE = 2; + /** * Set of failed packages remembered across job runs. */ @@ -92,6 +96,9 @@ public class BackgroundDexOptService extends JobService { private final File mDataDir = Environment.getDataDirectory(); + private static final long mDowngradeUnusedAppsThresholdInMillis = + getDowngradeUnusedAppsThresholdInMillis(); + public static void schedule(Context context) { JobScheduler js = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); @@ -211,11 +218,10 @@ public class BackgroundDexOptService extends JobService { // Unfortunately this will also means that "pm.dexopt.boot=speed-profile" will // behave differently than "pm.dexopt.bg-dexopt=speed-profile" but that's a // trade-off worth doing to save boot time work. - int result = pm.performDexOptWithStatus(pkg, - /* checkProfiles */ false, + int result = pm.performDexOptWithStatus(new DexoptOptions( + pkg, PackageManagerService.REASON_BOOT, - /* force */ false, - /* bootComplete */ true); + DexoptOptions.DEXOPT_BOOT_COMPLETE)); if (result == PackageDexOptimizer.DEX_OPT_PERFORMED) { updatedPackages.add(pkg); } @@ -243,7 +249,8 @@ public class BackgroundDexOptService extends JobService { } // Optimize the given packages and return the optimization result (one of the OPTIMIZE_* codes). - private int idleOptimization(PackageManagerService pm, ArraySet<String> pkgs, Context context) { + private int idleOptimization(PackageManagerService pm, ArraySet<String> pkgs, + Context context) { Log.i(TAG, "Performing idle optimizations"); // If post-boot update is still running, request that it exits early. mExitPostBootUpdate.set(true); @@ -274,9 +281,16 @@ public class BackgroundDexOptService extends JobService { long lowStorageThreshold, boolean is_for_primary_dex, ArraySet<String> failedPackageNames) { ArraySet<String> updatedPackages = new ArraySet<>(); + Set<String> unusedPackages = pm.getUnusedPackages(mDowngradeUnusedAppsThresholdInMillis); + // Only downgrade apps when space is low on device. + // Threshold is selected above the lowStorageThreshold so that we can pro-actively clean + // up disk before user hits the actual lowStorageThreshold. + final long lowStorageThresholdForDowngrade = LOW_THRESHOLD_MULTIPLIER_FOR_DOWNGRADE * + lowStorageThreshold; + boolean shouldDowngrade = shouldDowngrade(lowStorageThresholdForDowngrade); for (String pkg : pkgs) { int abort_code = abortIdleOptimizations(lowStorageThreshold); - if (abort_code != OPTIMIZE_CONTINUE) { + if (abort_code == OPTIMIZE_ABORT_BY_JOB_SCHEDULER) { return abort_code; } @@ -284,30 +298,58 @@ public class BackgroundDexOptService extends JobService { if (failedPackageNames.contains(pkg)) { // Skip previously failing package continue; + } + } + + int reason; + boolean downgrade; + // Downgrade unused packages. + if (unusedPackages.contains(pkg) && shouldDowngrade) { + // This applies for system apps or if packages location is not a directory, i.e. + // monolithic install. + if (is_for_primary_dex && !pm.canHaveOatDir(pkg)) { + // For apps that don't have the oat directory, instead of downgrading, + // remove their compiler artifacts from dalvik cache. + pm.deleteOatArtifactsOfPackage(pkg); + continue; } else { - // Conservatively add package to the list of failing ones in case performDexOpt - // never returns. - failedPackageNames.add(pkg); + reason = PackageManagerService.REASON_INACTIVE_PACKAGE_DOWNGRADE; + downgrade = true; } + } else if (abort_code != OPTIMIZE_ABORT_NO_SPACE_LEFT) { + reason = PackageManagerService.REASON_BACKGROUND_DEXOPT; + downgrade = false; + } else { + // can't dexopt because of low space. + continue; + } + + synchronized (failedPackageNames) { + // Conservatively add package to the list of failing ones in case + // performDexOpt never returns. + failedPackageNames.add(pkg); } // Optimize package if needed. Note that there can be no race between // concurrent jobs because PackageDexOptimizer.performDexOpt is synchronized. boolean success; + int dexoptFlags = + DexoptOptions.DEXOPT_CHECK_FOR_PROFILES_UPDATES | + DexoptOptions.DEXOPT_BOOT_COMPLETE | + (downgrade ? DexoptOptions.DEXOPT_DOWNGRADE : 0) | + DexoptOptions.DEXOPT_IDLE_BACKGROUND_JOB; if (is_for_primary_dex) { - int result = pm.performDexOptWithStatus(pkg, - /* checkProfiles */ true, + int result = pm.performDexOptWithStatus(new DexoptOptions(pkg, PackageManagerService.REASON_BACKGROUND_DEXOPT, - /* force */ false, - /* bootComplete */ true); + dexoptFlags)); success = result != PackageDexOptimizer.DEX_OPT_FAILED; if (result == PackageDexOptimizer.DEX_OPT_PERFORMED) { updatedPackages.add(pkg); } } else { - success = pm.performDexOptSecondary(pkg, + success = pm.performDexOpt(new DexoptOptions(pkg, PackageManagerService.REASON_BACKGROUND_DEXOPT, - /* force */ false); + dexoptFlags | DexoptOptions.DEXOPT_ONLY_SECONDARY_DEX)); } if (success) { // Dexopt succeeded, remove package from the list of failing ones. @@ -347,6 +389,16 @@ public class BackgroundDexOptService extends JobService { return OPTIMIZE_CONTINUE; } + // Evaluate whether apps should be downgraded. + private boolean shouldDowngrade(long lowStorageThresholdForDowngrade) { + long usableSpace = mDataDir.getUsableSpace(); + if (usableSpace < lowStorageThresholdForDowngrade) { + return true; + } + + return false; + } + /** * Execute the idle optimizations immediately. */ @@ -415,4 +467,14 @@ public class BackgroundDexOptService extends JobService { pinnerService.update(updatedPackages); } } + + private static long getDowngradeUnusedAppsThresholdInMillis() { + final String sysPropKey = "pm.dexopt.downgrade_after_inactive_days"; + String sysPropValue = SystemProperties.get(sysPropKey); + if (sysPropValue == null || sysPropValue.isEmpty()) { + Log.w(TAG, "SysProp " + sysPropKey + " not set"); + return Long.MAX_VALUE; + } + return TimeUnit.DAYS.toMillis(Long.parseLong(sysPropValue)); + } } diff --git a/services/core/java/com/android/server/pm/Installer.java b/services/core/java/com/android/server/pm/Installer.java index 5c4c0401b63c..210eb1385035 100644 --- a/services/core/java/com/android/server/pm/Installer.java +++ b/services/core/java/com/android/server/pm/Installer.java @@ -56,6 +56,8 @@ public class Installer extends SystemService { public static final int DEXOPT_STORAGE_CE = 1 << 7; /** Indicates that the dex file passed to dexopt in on DE storage. */ public static final int DEXOPT_STORAGE_DE = 1 << 8; + /** Indicates that dexopt is invoked from the background service. */ + public static final int DEXOPT_IDLE_BACKGROUND_JOB = 1 << 9; // NOTE: keep in sync with installd public static final int FLAG_CLEAR_CACHE_ONLY = 1 << 8; @@ -258,7 +260,7 @@ public class Installer extends SystemService { public long[] getExternalSize(String uuid, int userId, int flags, int[] appIds) throws InstallerException { - if (!checkBeforeRemote()) return new long[4]; + if (!checkBeforeRemote()) return new long[6]; try { return mInstalld.getExternalSize(uuid, userId, flags, appIds); } catch (Exception e) { @@ -279,13 +281,13 @@ public class Installer extends SystemService { public void dexopt(String apkPath, int uid, @Nullable String pkgName, String instructionSet, int dexoptNeeded, @Nullable String outputPath, int dexFlags, String compilerFilter, @Nullable String volumeUuid, @Nullable String sharedLibraries, - @Nullable String seInfo) + @Nullable String seInfo, boolean downgrade) throws InstallerException { assertValidInstructionSet(instructionSet); if (!checkBeforeRemote()) return; try { mInstalld.dexopt(apkPath, uid, pkgName, instructionSet, dexoptNeeded, outputPath, - dexFlags, compilerFilter, volumeUuid, sharedLibraries, seInfo); + dexFlags, compilerFilter, volumeUuid, sharedLibraries, seInfo, downgrade); } catch (Exception e) { throw InstallerException.from(e); } diff --git a/services/core/java/com/android/server/pm/OtaDexoptService.java b/services/core/java/com/android/server/pm/OtaDexoptService.java index a43f8afa4d97..da6e26e17122 100644 --- a/services/core/java/com/android/server/pm/OtaDexoptService.java +++ b/services/core/java/com/android/server/pm/OtaDexoptService.java @@ -18,7 +18,6 @@ package com.android.server.pm; import static com.android.server.pm.InstructionSets.getAppDexInstructionSets; import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets; -import static com.android.server.pm.PackageManagerServiceCompilerMapping.getCompilerFilterForReason; import android.annotation.Nullable; import android.content.Context; @@ -30,17 +29,16 @@ import android.os.ResultReceiver; import android.os.ServiceManager; import android.os.ShellCallback; import android.os.storage.StorageManager; -import android.text.TextUtils; import android.util.Log; import android.util.Slog; import com.android.internal.logging.MetricsLogger; import com.android.server.pm.Installer.InstallerException; +import com.android.server.pm.dex.DexoptOptions; import java.io.File; import java.io.FileDescriptor; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.concurrent.TimeUnit; @@ -261,11 +259,12 @@ public class OtaDexoptService extends IOtaDexopt.Stub { public void dexopt(String apkPath, int uid, @Nullable String pkgName, String instructionSet, int dexoptNeeded, @Nullable String outputPath, int dexFlags, String compilerFilter, @Nullable String volumeUuid, - @Nullable String sharedLibraries, @Nullable String seInfo) throws InstallerException { + @Nullable String sharedLibraries, @Nullable String seInfo, boolean downgrade) + throws InstallerException { final StringBuilder builder = new StringBuilder(); - // The version. Right now it's 2. - builder.append("2 "); + // The version. Right now it's 3. + builder.append("3 "); builder.append("dexopt"); @@ -280,6 +279,7 @@ public class OtaDexoptService extends IOtaDexopt.Stub { encodeParameter(builder, volumeUuid); encodeParameter(builder, sharedLibraries); encodeParameter(builder, seInfo); + encodeParameter(builder, downgrade); commands.add(builder.toString()); } @@ -314,12 +314,18 @@ public class OtaDexoptService extends IOtaDexopt.Stub { libraryDependencies = NO_LIBRARIES; } + optimizer.performDexOpt(pkg, libraryDependencies, - null /* ISAs */, false /* checkProfiles */, - getCompilerFilterForReason(compilationReason), + null /* ISAs */, null /* CompilerStats.PackageStats */, - mPackageManagerService.getDexManager().isUsedByOtherApps(pkg.packageName), - true /* bootComplete */); + mPackageManagerService.getDexManager().getPackageUseInfoOrDefault(pkg.packageName), + new DexoptOptions(pkg.packageName, compilationReason, + DexoptOptions.DEXOPT_BOOT_COMPLETE)); + + mPackageManagerService.getDexManager().dexoptSecondaryDex( + new DexoptOptions(pkg.packageName, compilationReason, + DexoptOptions.DEXOPT_ONLY_SECONDARY_DEX | + DexoptOptions.DEXOPT_BOOT_COMPLETE)); return commands; } diff --git a/services/core/java/com/android/server/pm/PackageDexOptimizer.java b/services/core/java/com/android/server/pm/PackageDexOptimizer.java index 7e9596adf138..dfa828df5236 100644 --- a/services/core/java/com/android/server/pm/PackageDexOptimizer.java +++ b/services/core/java/com/android/server/pm/PackageDexOptimizer.java @@ -19,9 +19,7 @@ package com.android.server.pm; import android.annotation.Nullable; import android.content.Context; import android.content.pm.ApplicationInfo; -import android.content.pm.PackageInfo; import android.content.pm.PackageParser; -import android.os.Environment; import android.os.FileUtils; import android.os.PowerManager; import android.os.SystemClock; @@ -30,17 +28,20 @@ import android.os.UserHandle; import android.os.WorkSource; import android.util.Log; import android.util.Slog; -import android.util.SparseArray; import com.android.internal.annotations.GuardedBy; import com.android.internal.util.IndentingPrintWriter; import com.android.server.pm.Installer.InstallerException; +import com.android.server.pm.dex.DexManager; +import com.android.server.pm.dex.DexoptOptions; +import com.android.server.pm.dex.DexoptUtils; +import com.android.server.pm.dex.PackageDexUsage; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; -import java.util.Set; +import java.util.Map; import dalvik.system.DexFile; @@ -52,6 +53,7 @@ import static com.android.server.pm.Installer.DEXOPT_SECONDARY_DEX; import static com.android.server.pm.Installer.DEXOPT_FORCE; import static com.android.server.pm.Installer.DEXOPT_STORAGE_CE; import static com.android.server.pm.Installer.DEXOPT_STORAGE_DE; +import static com.android.server.pm.Installer.DEXOPT_IDLE_BACKGROUND_JOB; import static com.android.server.pm.InstructionSets.getAppDexInstructionSets; import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets; @@ -123,17 +125,16 @@ public class PackageDexOptimizer { * synchronized on {@link #mInstallLock}. */ int performDexOpt(PackageParser.Package pkg, String[] sharedLibraries, - String[] instructionSets, boolean checkProfiles, String targetCompilationFilter, - CompilerStats.PackageStats packageStats, boolean isUsedByOtherApps, - boolean bootComplete) { + String[] instructionSets, CompilerStats.PackageStats packageStats, + PackageDexUsage.PackageUseInfo packageUseInfo, DexoptOptions options) { if (!canOptimizePackage(pkg)) { return DEX_OPT_SKIPPED; } synchronized (mInstallLock) { final long acquireTime = acquireWakeLockLI(pkg.applicationInfo.uid); try { - return performDexOptLI(pkg, sharedLibraries, instructionSets, checkProfiles, - targetCompilationFilter, packageStats, isUsedByOtherApps, bootComplete); + return performDexOptLI(pkg, sharedLibraries, instructionSets, + packageStats, packageUseInfo, options); } finally { releaseWakeLockLI(acquireTime); } @@ -146,26 +147,19 @@ public class PackageDexOptimizer { */ @GuardedBy("mInstallLock") private int performDexOptLI(PackageParser.Package pkg, String[] sharedLibraries, - String[] targetInstructionSets, boolean checkForProfileUpdates, - String targetCompilerFilter, CompilerStats.PackageStats packageStats, - boolean isUsedByOtherApps, boolean bootComplete) { + String[] targetInstructionSets, CompilerStats.PackageStats packageStats, + PackageDexUsage.PackageUseInfo packageUseInfo, DexoptOptions options) { final String[] instructionSets = targetInstructionSets != null ? targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo); final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets); final List<String> paths = pkg.getAllCodePaths(); final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid); - final String compilerFilter = getRealCompilerFilter(pkg.applicationInfo, - targetCompilerFilter, isUsedByOtherApps); - final boolean profileUpdated = checkForProfileUpdates && - isProfileUpdated(pkg, sharedGid, compilerFilter); - - final String sharedLibrariesPath = getSharedLibrariesPath(sharedLibraries); - // Get the dexopt flags after getRealCompilerFilter to make sure we get the correct flags. - final int dexoptFlags = getDexFlags(pkg, compilerFilter, bootComplete); - // Get the dependencies of each split in the package. For each code path in the package, - // this array contains the relative paths of each split it depends on, separated by colons. - String[] splitDependencies = getSplitDependencies(pkg); + // Get the class loader context dependencies. + // For each code path in the package, this array contains the class loader context that + // needs to be passed to dexopt in order to ensure correct optimizations. + String[] classLoaderContexts = DexoptUtils.getClassLoaderContexts( + pkg.applicationInfo, sharedLibraries); int result = DEX_OPT_SKIPPED; for (int i = 0; i < paths.size(); i++) { @@ -176,16 +170,29 @@ public class PackageDexOptimizer { } // Append shared libraries with split dependencies for this split. String path = paths.get(i); - String sharedLibrariesPathWithSplits; - if (sharedLibrariesPath != null && splitDependencies[i] != null) { - sharedLibrariesPathWithSplits = sharedLibrariesPath + ":" + splitDependencies[i]; - } else { - sharedLibrariesPathWithSplits = - splitDependencies[i] != null ? splitDependencies[i] : sharedLibrariesPath; + if (options.getSplitName() != null) { + // We are asked to compile only a specific split. Check that the current path is + // what we are looking for. + if (!options.getSplitName().equals(new File(path).getName())) { + continue; + } } + + final boolean isUsedByOtherApps = options.isDexoptAsSharedLibrary() + || packageUseInfo.isUsedByOtherApps(path); + final String compilerFilter = getRealCompilerFilter(pkg.applicationInfo, + options.getCompilerFilter(), isUsedByOtherApps); + final boolean profileUpdated = options.isCheckForProfileUpdates() && + isProfileUpdated(pkg, sharedGid, compilerFilter); + + // Get the dexopt flags after getRealCompilerFilter to make sure we get the correct + // flags. + final int dexoptFlags = getDexFlags(pkg, compilerFilter, options.isBootComplete()); + for (String dexCodeIsa : dexCodeInstructionSets) { - int newResult = dexOptPath(pkg, path, dexCodeIsa, compilerFilter, profileUpdated, - sharedLibrariesPathWithSplits, dexoptFlags, sharedGid, packageStats); + int newResult = dexOptPath(pkg, path, dexCodeIsa, compilerFilter, + profileUpdated, classLoaderContexts[i], dexoptFlags, sharedGid, + packageStats, options.isDowngrade()); // The end result is: // - FAILED if any path failed, // - PERFORMED if at least one path needed compilation, @@ -208,9 +215,10 @@ public class PackageDexOptimizer { */ @GuardedBy("mInstallLock") private int dexOptPath(PackageParser.Package pkg, String path, String isa, - String compilerFilter, boolean profileUpdated, String sharedLibrariesPath, - int dexoptFlags, int uid, CompilerStats.PackageStats packageStats) { - int dexoptNeeded = getDexoptNeeded(path, isa, compilerFilter, profileUpdated); + String compilerFilter, boolean profileUpdated, String classLoaderContext, + int dexoptFlags, int uid, CompilerStats.PackageStats packageStats, boolean downgrade) { + int dexoptNeeded = getDexoptNeeded(path, isa, compilerFilter, classLoaderContext, + profileUpdated, downgrade); if (Math.abs(dexoptNeeded) == DexFile.NO_DEXOPT_NEEDED) { return DEX_OPT_SKIPPED; } @@ -223,14 +231,18 @@ public class PackageDexOptimizer { Log.i(TAG, "Running dexopt (dexoptNeeded=" + dexoptNeeded + ") on: " + path + " pkg=" + pkg.applicationInfo.packageName + " isa=" + isa + " dexoptFlags=" + printDexoptFlags(dexoptFlags) - + " target-filter=" + compilerFilter + " oatDir=" + oatDir - + " sharedLibraries=" + sharedLibrariesPath); + + " targetFilter=" + compilerFilter + " oatDir=" + oatDir + + " classLoaderContext=" + classLoaderContext); try { long startTime = System.currentTimeMillis(); + // TODO: Consider adding 2 different APIs for primary and secondary dexopt. + // installd only uses downgrade flag for secondary dex files and ignores it for + // primary dex files. mInstaller.dexopt(path, uid, pkg.packageName, isa, dexoptNeeded, oatDir, dexoptFlags, - compilerFilter, pkg.volumeUuid, sharedLibrariesPath, pkg.applicationInfo.seInfo); + compilerFilter, pkg.volumeUuid, classLoaderContext, pkg.applicationInfo.seInfo, + false /* downgrade*/); if (packageStats != null) { long endTime = System.currentTimeMillis(); @@ -257,13 +269,12 @@ public class PackageDexOptimizer { * throwing exceptions). Or maybe make a separate call to installd to get DexOptNeeded, though * that seems wasteful. */ - public int dexOptSecondaryDexPath(ApplicationInfo info, String path, Set<String> isas, - String compilerFilter, boolean isUsedByOtherApps) { + public int dexOptSecondaryDexPath(ApplicationInfo info, String path, + PackageDexUsage.DexUseInfo dexUseInfo, DexoptOptions options) { synchronized (mInstallLock) { final long acquireTime = acquireWakeLockLI(info.uid); try { - return dexOptSecondaryDexPathLI(info, path, isas, compilerFilter, - isUsedByOtherApps); + return dexOptSecondaryDexPathLI(info, path, dexUseInfo, options); } finally { releaseWakeLockLI(acquireTime); } @@ -304,9 +315,16 @@ public class PackageDexOptimizer { } @GuardedBy("mInstallLock") - private int dexOptSecondaryDexPathLI(ApplicationInfo info, String path, Set<String> isas, - String compilerFilter, boolean isUsedByOtherApps) { - compilerFilter = getRealCompilerFilter(info, compilerFilter, isUsedByOtherApps); + private int dexOptSecondaryDexPathLI(ApplicationInfo info, String path, + PackageDexUsage.DexUseInfo dexUseInfo, DexoptOptions options) { + if (options.isDexoptOnlySharedDex() && !dexUseInfo.isUsedByOtherApps()) { + // We are asked to optimize only the dex files used by other apps and this is not + // on of them: skip it. + return DEX_OPT_SKIPPED; + } + + String compilerFilter = getRealCompilerFilter(info, options.getCompilerFilter(), + dexUseInfo.isUsedByOtherApps()); // Get the dexopt flags after getRealCompilerFilter to make sure we get the correct flags. // Secondary dex files are currently not compiled at boot. int dexoptFlags = getDexFlags(info, compilerFilter, /* bootComplete */ true) @@ -323,19 +341,32 @@ public class PackageDexOptimizer { return DEX_OPT_FAILED; } Log.d(TAG, "Running dexopt on: " + path - + " pkg=" + info.packageName + " isa=" + isas + + " pkg=" + info.packageName + " isa=" + dexUseInfo.getLoaderIsas() + " dexoptFlags=" + printDexoptFlags(dexoptFlags) + " target-filter=" + compilerFilter); + String classLoaderContext; + if (dexUseInfo.isUnknownClassLoaderContext() || + dexUseInfo.isUnsupportedClassLoaderContext() || + dexUseInfo.isVariableClassLoaderContext()) { + // If we have an unknown (not yet set), unsupported (custom class loaders), or a + // variable class loader chain, compile without a context and mark the oat file with + // SKIP_SHARED_LIBRARY_CHECK. Note that his might lead to a incorrect compilation. + // TODO(calin): We should just extract in this case. + classLoaderContext = SKIP_SHARED_LIBRARY_CHECK; + } else { + classLoaderContext = dexUseInfo.getClassLoaderContext(); + } try { - for (String isa : isas) { + for (String isa : dexUseInfo.getLoaderIsas()) { // Reuse the same dexopt path as for the primary apks. We don't need all the // arguments as some (dexopNeeded and oatDir) will be computed by installd because // system server cannot read untrusted app content. // TODO(calin): maybe add a separate call. mInstaller.dexopt(path, info.uid, info.packageName, isa, /*dexoptNeeded*/ 0, /*oatDir*/ null, dexoptFlags, - compilerFilter, info.volumeUuid, SKIP_SHARED_LIBRARY_CHECK, info.seInfoUser); + compilerFilter, info.volumeUuid, classLoaderContext, info.seInfoUser, + options.isDowngrade()); } return DEX_OPT_PERFORMED; @@ -363,26 +394,60 @@ public class PackageDexOptimizer { /** * Dumps the dexopt state of the given package {@code pkg} to the given {@code PrintWriter}. */ - void dumpDexoptState(IndentingPrintWriter pw, PackageParser.Package pkg) { + void dumpDexoptState(IndentingPrintWriter pw, PackageParser.Package pkg, + PackageDexUsage.PackageUseInfo useInfo) { final String[] instructionSets = getAppDexInstructionSets(pkg.applicationInfo); final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets); final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly(); - for (String instructionSet : dexCodeInstructionSets) { - pw.println("Instruction Set: " + instructionSet); - pw.increaseIndent(); - for (String path : paths) { - String status = null; - try { - status = DexFile.getDexFileStatus(path, instructionSet); - } catch (IOException ioe) { - status = "[Exception]: " + ioe.getMessage(); - } - pw.println("path: " + path); - pw.println("status: " + status); - } - pw.decreaseIndent(); + for (String path : paths) { + pw.println("path: " + path); + pw.increaseIndent(); + + for (String isa : dexCodeInstructionSets) { + String status = null; + try { + status = DexFile.getDexFileStatus(path, isa); + } catch (IOException ioe) { + status = "[Exception]: " + ioe.getMessage(); + } + pw.println(isa + ": " + status); + } + + if (useInfo.isUsedByOtherApps(path)) { + pw.println("used be other apps: " + useInfo.getLoadingPackages(path)); + } + + Map<String, PackageDexUsage.DexUseInfo> dexUseInfoMap = useInfo.getDexUseInfoMap(); + + if (!dexUseInfoMap.isEmpty()) { + pw.println("known secondary dex files:"); + pw.increaseIndent(); + for (Map.Entry<String, PackageDexUsage.DexUseInfo> e : dexUseInfoMap.entrySet()) { + String dex = e.getKey(); + PackageDexUsage.DexUseInfo dexUseInfo = e.getValue(); + pw.println(dex); + pw.increaseIndent(); + for (String isa : dexUseInfo.getLoaderIsas()) { + String status = null; + try { + status = DexFile.getDexFileStatus(path, isa); + } catch (IOException ioe) { + status = "[Exception]: " + ioe.getMessage(); + } + pw.println(isa + ": " + status); + } + + pw.println("class loader context: " + dexUseInfo.getClassLoaderContext()); + if (dexUseInfo.isUsedByOtherApps()) { + pw.println("used be other apps: " + dexUseInfo.getLoadingPackages()); + } + pw.decreaseIndent(); + } + pw.decreaseIndent(); + } + pw.decreaseIndent(); } } @@ -436,11 +501,11 @@ public class PackageDexOptimizer { * configuration (isa, compiler filter, profile). */ private int getDexoptNeeded(String path, String isa, String compilerFilter, - boolean newProfile) { + String classLoaderContext, boolean newProfile, boolean downgrade) { int dexoptNeeded; try { - dexoptNeeded = DexFile.getDexOptNeeded(path, isa, compilerFilter, newProfile, - false /* downgrade */); + dexoptNeeded = DexFile.getDexOptNeeded(path, isa, compilerFilter, classLoaderContext, + newProfile, downgrade); } catch (IOException ioe) { Slog.w(TAG, "IOException reading apk: " + path, ioe); return DEX_OPT_FAILED; @@ -449,86 +514,6 @@ public class PackageDexOptimizer { } /** - * Computes the shared libraries path that should be passed to dexopt. - */ - private String getSharedLibrariesPath(String[] sharedLibraries) { - if (sharedLibraries == null || sharedLibraries.length == 0) { - return null; - } - StringBuilder sb = new StringBuilder(); - for (String lib : sharedLibraries) { - if (sb.length() != 0) { - sb.append(":"); - } - sb.append(lib); - } - return sb.toString(); - } - - /** - * Walks dependency tree and gathers the dependencies for each split in a split apk. - * The split paths are stored as relative paths, separated by colons. - */ - private String[] getSplitDependencies(PackageParser.Package pkg) { - // Convert all the code paths to relative paths. - String baseCodePath = new File(pkg.baseCodePath).getParent(); - List<String> paths = pkg.getAllCodePaths(); - String[] splitDependencies = new String[paths.size()]; - for (int i = 0; i < paths.size(); i++) { - File pathFile = new File(paths.get(i)); - String fileName = pathFile.getName(); - paths.set(i, fileName); - - // Sanity check that the base paths of the splits are all the same. - String basePath = pathFile.getParent(); - if (!basePath.equals(baseCodePath)) { - Slog.wtf(TAG, "Split paths have different base paths: " + basePath + " and " + - baseCodePath); - } - } - - // If there are no other dependencies, fill in the implicit dependency on the base apk. - SparseArray<int[]> dependencies = pkg.applicationInfo.splitDependencies; - if (dependencies == null) { - for (int i = 1; i < paths.size(); i++) { - splitDependencies[i] = paths.get(0); - } - return splitDependencies; - } - - // Fill in the dependencies, skipping the base apk which has no dependencies. - for (int i = 1; i < dependencies.size(); i++) { - getParentDependencies(dependencies.keyAt(i), paths, dependencies, splitDependencies); - } - - return splitDependencies; - } - - /** - * Recursive method to generate dependencies for a particular split. - * The index is a key from the package's splitDependencies. - */ - private String getParentDependencies(int index, List<String> paths, - SparseArray<int[]> dependencies, String[] splitDependencies) { - // The base apk is always first, and has no dependencies. - if (index == 0) { - return null; - } - // Return the result if we've computed the dependencies for this index already. - if (splitDependencies[index] != null) { - return splitDependencies[index]; - } - // Get the dependencies for the parent of this index and append its path to it. - int parent = dependencies.get(index)[0]; - String parentDependencies = - getParentDependencies(parent, paths, dependencies, splitDependencies); - String path = parentDependencies == null ? paths.get(parent) : - parentDependencies + ":" + paths.get(parent); - splitDependencies[index] = path; - return path; - } - - /** * Checks if there is an update on the profile information of the {@code pkg}. * If the compiler filter is not profile guided the method returns false. * @@ -619,6 +604,9 @@ public class PackageDexOptimizer { if ((flags & DEXOPT_STORAGE_DE) == DEXOPT_STORAGE_DE) { flagsList.add("storage_de"); } + if ((flags & DEXOPT_IDLE_BACKGROUND_JOB) == DEXOPT_IDLE_BACKGROUND_JOB) { + flagsList.add("idle_background_job"); + } return String.join(",", flagsList); } diff --git a/services/core/java/com/android/server/pm/PackageManagerService.java b/services/core/java/com/android/server/pm/PackageManagerService.java index da1df78b4f47..a8ea4eaee4fd 100644 --- a/services/core/java/com/android/server/pm/PackageManagerService.java +++ b/services/core/java/com/android/server/pm/PackageManagerService.java @@ -280,6 +280,8 @@ import com.android.server.pm.PermissionsState.PermissionState; import com.android.server.pm.Settings.DatabaseVersion; import com.android.server.pm.Settings.VersionInfo; import com.android.server.pm.dex.DexManager; +import com.android.server.pm.dex.DexoptOptions; +import com.android.server.pm.dex.PackageDexUsage; import com.android.server.storage.DeviceStorageMonitorInternal; import dalvik.system.CloseGuard; @@ -559,8 +561,9 @@ public class PackageManagerService extends IPackageManager.Stub public static final int REASON_INSTALL = 2; public static final int REASON_BACKGROUND_DEXOPT = 3; public static final int REASON_AB_OTA = 4; + public static final int REASON_INACTIVE_PACKAGE_DOWNGRADE = 5; - public static final int REASON_LAST = REASON_AB_OTA; + public static final int REASON_LAST = REASON_INACTIVE_PACKAGE_DOWNGRADE; /** All dangerous permission names in the same order as the events in MetricsEvent */ private static final List<String> ALL_DANGEROUS_PERMISSIONS = Arrays.asList( @@ -9392,24 +9395,42 @@ public class PackageManagerService extends IPackageManager.Stub // Unfortunately this will also means that "pm.dexopt.boot=speed-profile" will // behave differently than "pm.dexopt.bg-dexopt=speed-profile" but that's a // trade-off worth doing to save boot time work. - int dexOptStatus = performDexOptTraced(pkg.packageName, - false /* checkProfiles */, + int dexoptFlags = bootComplete ? DexoptOptions.DEXOPT_BOOT_COMPLETE : 0; + int primaryDexOptStaus = performDexOptTraced(new DexoptOptions( + pkg.packageName, compilerFilter, - false /* force */, - bootComplete); - switch (dexOptStatus) { - case PackageDexOptimizer.DEX_OPT_PERFORMED: - numberOfPackagesOptimized++; - break; - case PackageDexOptimizer.DEX_OPT_SKIPPED: - numberOfPackagesSkipped++; - break; - case PackageDexOptimizer.DEX_OPT_FAILED: - numberOfPackagesFailed++; - break; - default: - Log.e(TAG, "Unexpected dexopt return code " + dexOptStatus); - break; + dexoptFlags)); + + boolean secondaryDexOptStatus = true; + if (pkg.isSystemApp()) { + // Only dexopt shared secondary dex files belonging to system apps to not slow down + // too much boot after an OTA. + int secondaryDexoptFlags = dexoptFlags | + DexoptOptions.DEXOPT_ONLY_SECONDARY_DEX | + DexoptOptions.DEXOPT_ONLY_SHARED_DEX; + mDexManager.dexoptSecondaryDex(new DexoptOptions( + pkg.packageName, + compilerFilter, + secondaryDexoptFlags)); + } + + if (secondaryDexOptStatus) { + switch (primaryDexOptStaus) { + case PackageDexOptimizer.DEX_OPT_PERFORMED: + numberOfPackagesOptimized++; + break; + case PackageDexOptimizer.DEX_OPT_SKIPPED: + numberOfPackagesSkipped++; + break; + case PackageDexOptimizer.DEX_OPT_FAILED: + numberOfPackagesFailed++; + break; + default: + Log.e(TAG, "Unexpected dexopt return code " + primaryDexOptStaus); + break; + } + } else { + numberOfPackagesFailed++; } } @@ -9440,7 +9461,8 @@ public class PackageManagerService extends IPackageManager.Stub } @Override - public void notifyDexLoad(String loadingPackageName, List<String> dexPaths, String loaderIsa) { + public void notifyDexLoad(String loadingPackageName, List<String> classLoaderNames, + List<String> classPaths, String loaderIsa) { int userId = UserHandle.getCallingUserId(); ApplicationInfo ai = getApplicationInfo(loadingPackageName, /*flags*/ 0, userId); if (ai == null) { @@ -9448,7 +9470,7 @@ public class PackageManagerService extends IPackageManager.Stub + loadingPackageName + ", user=" + userId); return; } - mDexManager.notifyDexLoad(ai, dexPaths, loaderIsa, userId); + mDexManager.notifyDexLoad(ai, classLoaderNames, classPaths, loaderIsa, userId); } @Override @@ -9476,17 +9498,53 @@ public class PackageManagerService extends IPackageManager.Stub } } + /** + * Ask the package manager to perform a dex-opt with the given compiler filter. + * + * Note: exposed only for the shell command to allow moving packages explicitly to a + * definite state. + */ @Override - public boolean performDexOpt(String packageName, - boolean checkProfiles, int compileReason, boolean force, boolean bootComplete) { + public boolean performDexOptMode(String packageName, + boolean checkProfiles, String targetCompilerFilter, boolean force, + boolean bootComplete, String splitName) { + int flags = (checkProfiles ? DexoptOptions.DEXOPT_CHECK_FOR_PROFILES_UPDATES : 0) | + (force ? DexoptOptions.DEXOPT_FORCE : 0) | + (bootComplete ? DexoptOptions.DEXOPT_BOOT_COMPLETE : 0); + return performDexOpt(new DexoptOptions(packageName, targetCompilerFilter, + splitName, flags)); + } + + /** + * Ask the package manager to perform a dex-opt with the given compiler filter on the + * secondary dex files belonging to the given package. + * + * Note: exposed only for the shell command to allow moving packages explicitly to a + * definite state. + */ + @Override + public boolean performDexOptSecondary(String packageName, String compilerFilter, + boolean force) { + int flags = DexoptOptions.DEXOPT_ONLY_SECONDARY_DEX | + DexoptOptions.DEXOPT_CHECK_FOR_PROFILES_UPDATES | + DexoptOptions.DEXOPT_BOOT_COMPLETE | + (force ? DexoptOptions.DEXOPT_FORCE : 0); + return performDexOpt(new DexoptOptions(packageName, compilerFilter, flags)); + } + + /*package*/ boolean performDexOpt(DexoptOptions options) { if (getInstantAppPackageName(Binder.getCallingUid()) != null) { return false; - } else if (isInstantApp(packageName, UserHandle.getCallingUserId())) { + } else if (isInstantApp(options.getPackageName(), UserHandle.getCallingUserId())) { return false; } - int dexoptStatus = performDexOptWithStatus( - packageName, checkProfiles, compileReason, force, bootComplete); - return dexoptStatus != PackageDexOptimizer.DEX_OPT_FAILED; + + if (options.isDexoptOnlySecondaryDex()) { + return mDexManager.dexoptSecondaryDex(options); + } else { + int dexoptStatus = performDexOptWithStatus(options); + return dexoptStatus != PackageDexOptimizer.DEX_OPT_FAILED; + } } /** @@ -9495,33 +9553,14 @@ public class PackageManagerService extends IPackageManager.Stub * {@link PackageDexOptimizer#DEX_OPT_PERFORMED} * {@link PackageDexOptimizer#DEX_OPT_FAILED} */ - /* package */ int performDexOptWithStatus(String packageName, - boolean checkProfiles, int compileReason, boolean force, boolean bootComplete) { - return performDexOptTraced(packageName, checkProfiles, - getCompilerFilterForReason(compileReason), force, bootComplete); + /* package */ int performDexOptWithStatus(DexoptOptions options) { + return performDexOptTraced(options); } - @Override - public boolean performDexOptMode(String packageName, - boolean checkProfiles, String targetCompilerFilter, boolean force, - boolean bootComplete) { - if (getInstantAppPackageName(Binder.getCallingUid()) != null) { - return false; - } else if (isInstantApp(packageName, UserHandle.getCallingUserId())) { - return false; - } - int dexOptStatus = performDexOptTraced(packageName, checkProfiles, - targetCompilerFilter, force, bootComplete); - return dexOptStatus != PackageDexOptimizer.DEX_OPT_FAILED; - } - - private int performDexOptTraced(String packageName, - boolean checkProfiles, String targetCompilerFilter, boolean force, - boolean bootComplete) { + private int performDexOptTraced(DexoptOptions options) { Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "dexopt"); try { - return performDexOptInternal(packageName, checkProfiles, - targetCompilerFilter, force, bootComplete); + return performDexOptInternal(options); } finally { Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER); } @@ -9529,12 +9568,10 @@ public class PackageManagerService extends IPackageManager.Stub // Run dexopt on a given package. Returns true if dexopt did not fail, i.e. // if the package can now be considered up to date for the given filter. - private int performDexOptInternal(String packageName, - boolean checkProfiles, String targetCompilerFilter, boolean force, - boolean bootComplete) { + private int performDexOptInternal(DexoptOptions options) { PackageParser.Package p; synchronized (mPackages) { - p = mPackages.get(packageName); + p = mPackages.get(options.getPackageName()); if (p == null) { // Package could not be found. Report failure. return PackageDexOptimizer.DEX_OPT_FAILED; @@ -9545,8 +9582,7 @@ public class PackageManagerService extends IPackageManager.Stub long callingId = Binder.clearCallingIdentity(); try { synchronized (mInstallLock) { - return performDexOptInternalWithDependenciesLI(p, checkProfiles, - targetCompilerFilter, force, bootComplete); + return performDexOptInternalWithDependenciesLI(p, options); } } finally { Binder.restoreCallingIdentity(callingId); @@ -9566,12 +9602,11 @@ public class PackageManagerService extends IPackageManager.Stub } private int performDexOptInternalWithDependenciesLI(PackageParser.Package p, - boolean checkProfiles, String targetCompilerFilter, - boolean force, boolean bootComplete) { + DexoptOptions options) { // Select the dex optimizer based on the force parameter. // Note: The force option is rarely used (cmdline input for testing, mostly), so it's OK to // allocate an object here. - PackageDexOptimizer pdo = force + PackageDexOptimizer pdo = options.isForce() ? new PackageDexOptimizer.ForcedUpdatePackageDexOptimizer(mPackageDexOptimizer) : mPackageDexOptimizer; @@ -9585,39 +9620,19 @@ public class PackageManagerService extends IPackageManager.Stub Collection<PackageParser.Package> deps = findSharedNonSystemLibraries(p); final String[] instructionSets = getAppDexInstructionSets(p.applicationInfo); if (!deps.isEmpty()) { + DexoptOptions libraryOptions = new DexoptOptions(options.getPackageName(), + options.getCompilerFilter(), options.getSplitName(), + options.getFlags() | DexoptOptions.DEXOPT_AS_SHARED_LIBRARY); for (PackageParser.Package depPackage : deps) { // TODO: Analyze and investigate if we (should) profile libraries. pdo.performDexOpt(depPackage, null /* sharedLibraries */, instructionSets, - false /* checkProfiles */, - targetCompilerFilter, getOrCreateCompilerPackageStats(depPackage), - true /* isUsedByOtherApps */, - bootComplete); + mDexManager.getPackageUseInfoOrDefault(depPackage.packageName), libraryOptions); } } - return pdo.performDexOpt(p, p.usesLibraryFiles, instructionSets, checkProfiles, - targetCompilerFilter, getOrCreateCompilerPackageStats(p), - mDexManager.isUsedByOtherApps(p.packageName), bootComplete); - } - - // Performs dexopt on the used secondary dex files belonging to the given package. - // Returns true if all dex files were process successfully (which could mean either dexopt or - // skip). Returns false if any of the files caused errors. - @Override - public boolean performDexOptSecondary(String packageName, String compilerFilter, - boolean force) { - if (getInstantAppPackageName(Binder.getCallingUid()) != null) { - return false; - } else if (isInstantApp(packageName, UserHandle.getCallingUserId())) { - return false; - } - mDexManager.reconcileSecondaryDexFiles(packageName); - return mDexManager.dexoptSecondaryDex(packageName, compilerFilter, force); - } - - public boolean performDexOptSecondary(String packageName, int compileReason, - boolean force) { - return mDexManager.dexoptSecondaryDex(packageName, compileReason, force); + return pdo.performDexOpt(p, p.usesLibraryFiles, instructionSets, + getOrCreateCompilerPackageStats(p), + mDexManager.getPackageUseInfoOrDefault(p.packageName), options); } /** @@ -9743,6 +9758,7 @@ public class PackageManagerService extends IPackageManager.Stub public void shutdown() { mPackageUsage.writeNow(mPackages); mCompilerStats.writeNow(); + mDexManager.writePackageDexUsageNow(); } @Override @@ -9793,10 +9809,11 @@ public class PackageManagerService extends IPackageManager.Stub // Whoever is calling forceDexOpt wants a compiled package. // Don't use profiles since that may cause compilation to be skipped. - final int res = performDexOptInternalWithDependenciesLI(pkg, - false /* checkProfiles */, getDefaultCompilerFilter(), - true /* force */, - true /* bootComplete */); + final int res = performDexOptInternalWithDependenciesLI( + pkg, + new DexoptOptions(packageName, + getDefaultCompilerFilter(), + DexoptOptions.DEXOPT_FORCE | DexoptOptions.DEXOPT_BOOT_COMPLETE)); Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER); if (res != PackageDexOptimizer.DEX_OPT_PERFORMED) { @@ -16227,7 +16244,7 @@ public class PackageManagerService extends IPackageManager.Stub } } - private void removeDexFiles(List<String> allCodePaths, String[] instructionSets) { + void removeDexFiles(List<String> allCodePaths, String[] instructionSets) { if (!allCodePaths.isEmpty()) { if (instructionSets == null) { throw new IllegalStateException("instructionSet == null"); @@ -18260,12 +18277,14 @@ public class PackageManagerService extends IPackageManager.Stub // method because `pkg` may not be in `mPackages` yet. // // Also, don't fail application installs if the dexopt step fails. + DexoptOptions dexoptOptions = new DexoptOptions(pkg.packageName, + REASON_INSTALL, + DexoptOptions.DEXOPT_BOOT_COMPLETE); mPackageDexOptimizer.performDexOpt(pkg, pkg.usesLibraryFiles, - null /* instructionSets */, false /* checkProfiles */, - getCompilerFilterForReason(REASON_INSTALL), + null /* instructionSets */, getOrCreateCompilerPackageStats(pkg), - mDexManager.isUsedByOtherApps(pkg.packageName), - true /* bootComplete */); + mDexManager.getPackageUseInfoOrDefault(pkg.packageName), + dexoptOptions); Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER); } @@ -22536,7 +22555,8 @@ Slog.v(TAG, ":: stepped forward, applying functor at tag " + parser.getName()); for (PackageParser.Package pkg : packages) { ipw.println("[" + pkg.packageName + "]"); ipw.increaseIndent(); - mPackageDexOptimizer.dumpDexoptState(ipw, pkg); + mPackageDexOptimizer.dumpDexoptState(ipw, pkg, + mDexManager.getPackageUseInfoOrDefault(pkg.packageName)); ipw.decreaseIndent(); } } @@ -25039,6 +25059,29 @@ Slog.v(TAG, ":: stepped forward, applying functor at tag " + parser.getName()); } } } + + Set<String> getUnusedPackages(long downgradeTimeThresholdMillis) { + Set<String> unusedPackages = new HashSet<>(); + long currentTimeInMillis = System.currentTimeMillis(); + synchronized (mPackages) { + for (PackageParser.Package pkg : mPackages.values()) { + PackageSetting ps = mSettings.mPackages.get(pkg.packageName); + if (ps == null) { + continue; + } + PackageDexUsage.PackageUseInfo packageUseInfo = + getDexManager().getPackageUseInfoOrDefault(pkg.packageName); + if (PackageManagerServiceUtils + .isUnusedSinceTimeInMillis(ps.firstInstallTime, currentTimeInMillis, + downgradeTimeThresholdMillis, packageUseInfo, + pkg.getLatestPackageUseTimeInMills(), + pkg.getLatestForegroundPackageUseTimeInMills())) { + unusedPackages.add(pkg.packageName); + } + } + } + return unusedPackages; + } } interface PackageSender { diff --git a/services/core/java/com/android/server/pm/PackageManagerServiceCompilerMapping.java b/services/core/java/com/android/server/pm/PackageManagerServiceCompilerMapping.java index ec248f5719b3..0a9480b4bbe8 100644 --- a/services/core/java/com/android/server/pm/PackageManagerServiceCompilerMapping.java +++ b/services/core/java/com/android/server/pm/PackageManagerServiceCompilerMapping.java @@ -26,7 +26,7 @@ import dalvik.system.DexFile; public class PackageManagerServiceCompilerMapping { // Names for compilation reasons. static final String REASON_STRINGS[] = { - "first-boot", "boot", "install", "bg-dexopt", "ab-ota" + "first-boot", "boot", "install", "bg-dexopt", "ab-ota", "inactive" }; // Static block to ensure the strings array is of the right length. @@ -47,7 +47,16 @@ public class PackageManagerServiceCompilerMapping { // Load the property for the given reason and check for validity. This will throw an // exception in case the reason or value are invalid. private static String getAndCheckValidity(int reason) { - String sysPropValue = SystemProperties.get(getSystemPropertyName(reason)); + String sysPropName = getSystemPropertyName(reason); + String sysPropValue; + // TODO: This is a temporary hack to keep marlin booting on aosp/master while we + // figure out how to deal with these system properties that currently appear on + // vendor. + if ("pm.dexopt.inactive".equals(sysPropName)) { + sysPropValue = "verify"; + } else { + sysPropValue = SystemProperties.get(sysPropName); + } if (sysPropValue == null || sysPropValue.isEmpty() || !DexFile.isValidCompilerFilter(sysPropValue)) { throw new IllegalStateException("Value \"" + sysPropValue +"\" not valid " diff --git a/services/core/java/com/android/server/pm/PackageManagerServiceUtils.java b/services/core/java/com/android/server/pm/PackageManagerServiceUtils.java index eb1e7bd922e3..1e0ce7a215be 100644 --- a/services/core/java/com/android/server/pm/PackageManagerServiceUtils.java +++ b/services/core/java/com/android/server/pm/PackageManagerServiceUtils.java @@ -16,6 +16,9 @@ package com.android.server.pm; +import com.android.server.pm.dex.DexManager; +import com.android.server.pm.dex.PackageDexUsage; + import static com.android.server.pm.PackageManagerService.DEBUG_DEXOPT; import static com.android.server.pm.PackageManagerService.TAG; @@ -140,9 +143,11 @@ public class PackageManagerServiceUtils { sortTemp, packageManagerService); // Give priority to apps used by other apps. + DexManager dexManager = packageManagerService.getDexManager(); applyPackageFilter((pkg) -> - packageManagerService.getDexManager().isUsedByOtherApps(pkg.packageName), result, - remainingPkgs, sortTemp, packageManagerService); + dexManager.getPackageUseInfoOrDefault(pkg.packageName) + .isAnyCodePathUsedByOtherApps(), + result, remainingPkgs, sortTemp, packageManagerService); // Filter out packages that aren't recently used, add all remaining apps. // TODO: add a property to control this? @@ -186,6 +191,41 @@ public class PackageManagerServiceUtils { } /** + * Checks if the package was inactive during since <code>thresholdTimeinMillis</code>. + * Package is considered active, if: + * 1) It was active in foreground. + * 2) It was active in background and also used by other apps. + * + * If it doesn't have sufficient information about the package, it return <code>false</code>. + */ + static boolean isUnusedSinceTimeInMillis(long firstInstallTime, long currentTimeInMillis, + long thresholdTimeinMillis, PackageDexUsage.PackageUseInfo packageUseInfo, + long latestPackageUseTimeInMillis, long latestForegroundPackageUseTimeInMillis) { + + if (currentTimeInMillis - firstInstallTime < thresholdTimeinMillis) { + return false; + } + + // If the app was active in foreground during the threshold period. + boolean isActiveInForeground = (currentTimeInMillis + - latestForegroundPackageUseTimeInMillis) + < thresholdTimeinMillis; + + if (isActiveInForeground) { + return false; + } + + // If the app was active in background during the threshold period and was used + // by other packages. + boolean isActiveInBackgroundAndUsedByOtherPackages = ((currentTimeInMillis + - latestPackageUseTimeInMillis) + < thresholdTimeinMillis) + && packageUseInfo.isAnyCodePathUsedByOtherApps(); + + return !isActiveInBackgroundAndUsedByOtherPackages; + } + + /** * Returns the canonicalized path of {@code path} as per {@code realpath(3)} * semantics. */ diff --git a/services/core/java/com/android/server/pm/PackageManagerShellCommand.java b/services/core/java/com/android/server/pm/PackageManagerShellCommand.java index 20d7b28c55e1..10ceba418f22 100644 --- a/services/core/java/com/android/server/pm/PackageManagerShellCommand.java +++ b/services/core/java/com/android/server/pm/PackageManagerShellCommand.java @@ -365,6 +365,7 @@ class PackageManagerShellCommand extends ShellCommand { String compilationReason = null; String checkProfilesRaw = null; boolean secondaryDex = false; + String split = null; String opt; while ((opt = getNextOption()) != null) { @@ -395,6 +396,9 @@ class PackageManagerShellCommand extends ShellCommand { case "--secondary-dex": secondaryDex = true; break; + case "--split": + split = getNextArgRequired(); + break; default: pw.println("Error: Unknown option: " + opt); return 1; @@ -423,6 +427,16 @@ class PackageManagerShellCommand extends ShellCommand { return 1; } + if (allPackages && split != null) { + pw.println("-a cannot be specified together with --split"); + return 1; + } + + if (secondaryDex && split != null) { + pw.println("--secondary-dex cannot be specified together with --split"); + return 1; + } + String targetCompilerFilter; if (compilerFilter != null) { if (!DexFile.isValidCompilerFilter(compilerFilter)) { @@ -472,7 +486,7 @@ class PackageManagerShellCommand extends ShellCommand { targetCompilerFilter, forceCompilation) : mInterface.performDexOptMode(packageName, checkProfiles, targetCompilerFilter, forceCompilation, - true /* bootComplete */); + true /* bootComplete */, split); if (!result) { failedPackages.add(packageName); } @@ -1609,7 +1623,7 @@ class PackageManagerShellCommand extends ShellCommand { pw.println(" help"); pw.println(" Print this help text."); pw.println(""); - pw.println(" compile [-m MODE | -r REASON] [-f] [-c]"); + pw.println(" compile [-m MODE | -r REASON] [-f] [-c] [--split SPLIT_NAME]"); pw.println(" [--reset] [--check-prof (true | false)] (-a | TARGET-PACKAGE)"); pw.println(" Trigger compilation of TARGET-PACKAGE or all packages if \"-a\"."); pw.println(" Options:"); @@ -1635,6 +1649,7 @@ class PackageManagerShellCommand extends ShellCommand { pw.println(" --reset: restore package to its post-install state"); pw.println(" --check-prof (true | false): look at profiles when doing dexopt?"); pw.println(" --secondary-dex: compile app secondary dex files"); + pw.println(" --split SPLIT: compile only the given split name"); pw.println(" bg-dexopt-job"); pw.println(" Execute the background optimizations immediately."); pw.println(" Note that the command only runs the background optimizer logic. It may"); diff --git a/services/core/java/com/android/server/pm/dex/DexManager.java b/services/core/java/com/android/server/pm/dex/DexManager.java index be50eeeefc24..62747547f320 100644 --- a/services/core/java/com/android/server/pm/dex/DexManager.java +++ b/services/core/java/com/android/server/pm/dex/DexManager.java @@ -36,6 +36,8 @@ import com.android.server.pm.PackageManagerServiceCompilerMapping; import java.io.File; import java.io.IOException; +import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.HashMap; import java.util.HashSet; @@ -81,6 +83,19 @@ public class DexManager { private static int DEX_SEARCH_FOUND_SPLIT = 2; // dex file is a split apk private static int DEX_SEARCH_FOUND_SECONDARY = 3; // dex file is a secondary dex + /** + * We do not record packages that have no secondary dex files or that are not used by other + * apps. This is an optimization to reduce the amount of data that needs to be written to + * disk (apps will not usually be shared so this trims quite a bit the number we record). + * + * To make this behaviour transparent to the callers which need use information on packages, + * DexManager will return this DEFAULT instance from + * {@link DexManager#getPackageUseInfoOrDefault}. It has no data about secondary dex files and + * is marked as not being used by other apps. This reflects the intended behaviour when we don't + * find the package in the underlying data file. + */ + private final static PackageUseInfo DEFAULT_USE_INFO = new PackageUseInfo(); + public DexManager(IPackageManager pms, PackageDexOptimizer pdo, Installer installer, Object installLock) { mPackageCodeLocationsCache = new HashMap<>(); @@ -97,29 +112,55 @@ public class DexManager { * return as fast as possible. * * @param loadingAppInfo the package performing the load - * @param dexPaths the list of dex files being loaded + * @param classLoadersNames the names of the class loaders present in the loading chain. The + * list encodes the class loader chain in the natural order. The first class loader has + * the second one as its parent and so on. The dex files present in the class path of the + * first class loader will be recorded in the usage file. + * @param classPaths the class paths corresponding to the class loaders names from + * {@param classLoadersNames}. The the first element corresponds to the first class loader + * and so on. A classpath is represented as a list of dex files separated by + * {@code File.pathSeparator}. + * The dex files found in the first class path will be recorded in the usage file. * @param loaderIsa the ISA of the app loading the dex files * @param loaderUserId the user id which runs the code loading the dex files */ - public void notifyDexLoad(ApplicationInfo loadingAppInfo, List<String> dexPaths, - String loaderIsa, int loaderUserId) { + public void notifyDexLoad(ApplicationInfo loadingAppInfo, List<String> classLoadersNames, + List<String> classPaths, String loaderIsa, int loaderUserId) { try { - notifyDexLoadInternal(loadingAppInfo, dexPaths, loaderIsa, loaderUserId); + notifyDexLoadInternal(loadingAppInfo, classLoadersNames, classPaths, loaderIsa, + loaderUserId); } catch (Exception e) { Slog.w(TAG, "Exception while notifying dex load for package " + loadingAppInfo.packageName, e); } } - private void notifyDexLoadInternal(ApplicationInfo loadingAppInfo, List<String> dexPaths, - String loaderIsa, int loaderUserId) { + private void notifyDexLoadInternal(ApplicationInfo loadingAppInfo, + List<String> classLoaderNames, List<String> classPaths, String loaderIsa, + int loaderUserId) { + if (classLoaderNames.size() != classPaths.size()) { + Slog.wtf(TAG, "Bad call to noitfyDexLoad: args have different size"); + return; + } + if (classLoaderNames.isEmpty()) { + Slog.wtf(TAG, "Bad call to notifyDexLoad: class loaders list is empty"); + return; + } if (!PackageManagerServiceUtils.checkISA(loaderIsa)) { - Slog.w(TAG, "Loading dex files " + dexPaths + " in unsupported ISA: " + + Slog.w(TAG, "Loading dex files " + classPaths + " in unsupported ISA: " + loaderIsa + "?"); return; } - for (String dexPath : dexPaths) { + // The classpath is represented as a list of dex files separated by File.pathSeparator. + String[] dexPathsToRegister = classPaths.get(0).split(File.pathSeparator); + + // Encode the class loader contexts for the dexPathsToRegister. + String[] classLoaderContexts = DexoptUtils.processContextForDexLoad( + classLoaderNames, classPaths); + + int dexPathIndex = 0; + for (String dexPath : dexPathsToRegister) { // Find the owning package name. DexSearchResult searchResult = getDexPackage(loadingAppInfo, dexPath, loaderUserId); @@ -147,23 +188,25 @@ public class DexManager { // Record dex file usage. If the current usage is a new pattern (e.g. new secondary, // or UsedBytOtherApps), record will return true and we trigger an async write // to disk to make sure we don't loose the data in case of a reboot. + + // A null classLoaderContexts means that there are unsupported class loaders in the + // chain. + String classLoaderContext = classLoaderContexts == null + ? PackageDexUsage.UNSUPPORTED_CLASS_LOADER_CONTEXT + : classLoaderContexts[dexPathIndex]; if (mPackageDexUsage.record(searchResult.mOwningPackageName, - dexPath, loaderUserId, loaderIsa, isUsedByOtherApps, primaryOrSplit)) { + dexPath, loaderUserId, loaderIsa, isUsedByOtherApps, primaryOrSplit, + loadingAppInfo.packageName, classLoaderContext)) { mPackageDexUsage.maybeWriteAsync(); } } else { - // This can happen in a few situations: - // - bogus dex loads - // - recent installs/uninstalls that we didn't detect. - // - new installed splits // If we can't find the owner of the dex we simply do not track it. The impact is // that the dex file will not be considered for offline optimizations. - // TODO(calin): add hooks for move/uninstall notifications to - // capture package moves or obsolete packages. if (DEBUG) { Slog.i(TAG, "Could not find owning package for dex file: " + dexPath); } } + dexPathIndex++; } } @@ -269,6 +312,8 @@ public class DexManager { private void loadInternal(Map<Integer, List<PackageInfo>> existingPackages) { Map<String, Set<Integer>> packageToUsersMap = new HashMap<>(); + Map<String, Set<String>> packageToCodePaths = new HashMap<>(); + // Cache the code locations for the installed packages. This allows for // faster lookups (no locks) when finding what package owns the dex file. for (Map.Entry<Integer, List<PackageInfo>> entry : existingPackages.entrySet()) { @@ -278,54 +323,72 @@ public class DexManager { // Cache the code locations. cachePackageInfo(pi, userId); - // Cache a map from package name to the set of user ids who installed the package. + // Cache two maps: + // - from package name to the set of user ids who installed the package. + // - from package name to the set of code paths. // We will use it to sync the data and remove obsolete entries from // mPackageDexUsage. Set<Integer> users = putIfAbsent( packageToUsersMap, pi.packageName, new HashSet<>()); users.add(userId); + + Set<String> codePaths = putIfAbsent( + packageToCodePaths, pi.packageName, new HashSet<>()); + codePaths.add(pi.applicationInfo.sourceDir); + if (pi.applicationInfo.splitSourceDirs != null) { + Collections.addAll(codePaths, pi.applicationInfo.splitSourceDirs); + } } } mPackageDexUsage.read(); - mPackageDexUsage.syncData(packageToUsersMap); + mPackageDexUsage.syncData(packageToUsersMap, packageToCodePaths); } /** * Get the package dex usage for the given package name. - * @return the package data or null if there is no data available for this package. + * If there is no usage info the method will return a default {@code PackageUseInfo} with + * no data about secondary dex files and marked as not being used by other apps. + * + * Note that no use info means the package was not used or it was used but not by other apps. + * Also, note that right now we might prune packages which are not used by other apps. + * TODO(calin): maybe we should not (prune) so we can have an accurate view when we try + * to access the package use. */ - public PackageUseInfo getPackageUseInfo(String packageName) { - return mPackageDexUsage.getPackageUseInfo(packageName); + public PackageUseInfo getPackageUseInfoOrDefault(String packageName) { + PackageUseInfo useInfo = mPackageDexUsage.getPackageUseInfo(packageName); + return useInfo == null ? DEFAULT_USE_INFO : useInfo; } /** - * Perform dexopt on the package {@code packageName} secondary dex files. - * @return true if all secondary dex files were processed successfully (compiled or skipped - * because they don't need to be compiled).. + * Return whether or not the manager has usage information on the give package. + * + * Note that no use info means the package was not used or it was used but not by other apps. + * Also, note that right now we might prune packages which are not used by other apps. + * TODO(calin): maybe we should not (prune) so we can have an accurate view when we try + * to access the package use. */ - public boolean dexoptSecondaryDex(String packageName, int compilerReason, boolean force) { - return dexoptSecondaryDex(packageName, - PackageManagerServiceCompilerMapping.getCompilerFilterForReason(compilerReason), - force); + /*package*/ boolean hasInfoOnPackage(String packageName) { + return mPackageDexUsage.getPackageUseInfo(packageName) != null; } /** - * Perform dexopt on the package {@code packageName} secondary dex files. + * Perform dexopt on with the given {@code options} on the secondary dex files. * @return true if all secondary dex files were processed successfully (compiled or skipped * because they don't need to be compiled).. */ - public boolean dexoptSecondaryDex(String packageName, String compilerFilter, boolean force) { + public boolean dexoptSecondaryDex(DexoptOptions options) { // Select the dex optimizer based on the force parameter. // Forced compilation is done through ForcedUpdatePackageDexOptimizer which will adjust // the necessary dexopt flags to make sure that compilation is not skipped. This avoid // passing the force flag through the multitude of layers. // Note: The force option is rarely used (cmdline input for testing, mostly), so it's OK to // allocate an object here. - PackageDexOptimizer pdo = force + PackageDexOptimizer pdo = options.isForce() ? new PackageDexOptimizer.ForcedUpdatePackageDexOptimizer(mPackageDexOptimizer) : mPackageDexOptimizer; - PackageUseInfo useInfo = getPackageUseInfo(packageName); + String packageName = options.getPackageName(); + PackageUseInfo useInfo = getPackageUseInfoOrDefault(packageName); if (useInfo == null || useInfo.getDexUseInfoMap().isEmpty()) { if (DEBUG) { Slog.d(TAG, "No secondary dex use for package:" + packageName); @@ -337,7 +400,8 @@ public class DexManager { for (Map.Entry<String, DexUseInfo> entry : useInfo.getDexUseInfoMap().entrySet()) { String dexPath = entry.getKey(); DexUseInfo dexUseInfo = entry.getValue(); - PackageInfo pkg = null; + + PackageInfo pkg; try { pkg = mPackageManager.getPackageInfo(packageName, /*flags*/0, dexUseInfo.getOwnerUserId()); @@ -356,7 +420,7 @@ public class DexManager { } int result = pdo.dexOptSecondaryDexPath(pkg.applicationInfo, dexPath, - dexUseInfo.getLoaderIsas(), compilerFilter, dexUseInfo.isUsedByOtherApps()); + dexUseInfo, options); success = success && (result != PackageDexOptimizer.DEX_OPT_FAILED); } return success; @@ -368,7 +432,7 @@ public class DexManager { * deleted, update the internal records and delete any generated oat files. */ public void reconcileSecondaryDexFiles(String packageName) { - PackageUseInfo useInfo = getPackageUseInfo(packageName); + PackageUseInfo useInfo = getPackageUseInfoOrDefault(packageName); if (useInfo == null || useInfo.getDexUseInfoMap().isEmpty()) { if (DEBUG) { Slog.d(TAG, "No secondary dex use for package:" + packageName); @@ -439,6 +503,8 @@ public class DexManager { } } + // TODO(calin): questionable API in the presence of class loaders context. Needs amends as the + // compilation happening here will use a pessimistic context. public RegisterDexModuleResult registerDexModule(ApplicationInfo info, String dexPath, boolean isUsedByOtherApps, int userId) { // Find the owning package record. @@ -457,11 +523,11 @@ public class DexManager { // We found the package. Now record the usage for all declared ISAs. boolean update = false; - Set<String> isas = new HashSet<>(); for (String isa : getAppDexInstructionSets(info)) { - isas.add(isa); boolean newUpdate = mPackageDexUsage.record(searchResult.mOwningPackageName, - dexPath, userId, isa, isUsedByOtherApps, /*primaryOrSplit*/ false); + dexPath, userId, isa, isUsedByOtherApps, /*primaryOrSplit*/ false, + searchResult.mOwningPackageName, + PackageDexUsage.UNKNOWN_CLASS_LOADER_CONTEXT); update |= newUpdate; } if (update) { @@ -471,8 +537,13 @@ public class DexManager { // Try to optimize the package according to the install reason. String compilerFilter = PackageManagerServiceCompilerMapping.getCompilerFilterForReason( PackageManagerService.REASON_INSTALL); - int result = mPackageDexOptimizer.dexOptSecondaryDexPath(info, dexPath, isas, - compilerFilter, isUsedByOtherApps); + DexUseInfo dexUseInfo = mPackageDexUsage.getPackageUseInfo(searchResult.mOwningPackageName) + .getDexUseInfoMap().get(dexPath); + + DexoptOptions options = new DexoptOptions(info.packageName, compilerFilter, /*flags*/0); + + int result = mPackageDexOptimizer.dexOptSecondaryDexPath(info, dexPath, dexUseInfo, + options); // If we fail to optimize the package log an error but don't propagate the error // back to the app. The app cannot do much about it and the background job @@ -493,23 +564,6 @@ public class DexManager { } /** - * Return true if the profiling data collected for the given app indicate - * that the apps's APK has been loaded by another app. - * Note that this returns false for all apps without any collected profiling data. - */ - public boolean isUsedByOtherApps(String packageName) { - PackageUseInfo useInfo = getPackageUseInfo(packageName); - if (useInfo == null) { - // No use info, means the package was not used or it was used but not by other apps. - // Note that right now we might prune packages which are not used by other apps. - // TODO(calin): maybe we should not (prune) so we can have an accurate view when we try - // to access the package use. - return false; - } - return useInfo.isUsedByOtherApps(); - } - - /** * Retrieves the package which owns the given dexPath. */ private DexSearchResult getDexPackage( @@ -566,6 +620,13 @@ public class DexManager { return existingValue == null ? newValue : existingValue; } + /** + * Writes the in-memory package dex usage to disk right away. + */ + public void writePackageDexUsageNow() { + mPackageDexUsage.writeNow(); + } + public static class RegisterDexModuleResult { public RegisterDexModuleResult() { this(false, null); diff --git a/services/core/java/com/android/server/pm/dex/DexoptOptions.java b/services/core/java/com/android/server/pm/dex/DexoptOptions.java new file mode 100644 index 000000000000..0966770d4897 --- /dev/null +++ b/services/core/java/com/android/server/pm/dex/DexoptOptions.java @@ -0,0 +1,151 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.server.pm.dex; + +import static com.android.server.pm.PackageManagerServiceCompilerMapping.getCompilerFilterForReason; + +import android.annotation.Nullable; + +/** + * Options used for dexopt invocations. + */ +public final class DexoptOptions { + // When set, the profiles will be checked for updates before calling dexopt. If + // the apps profiles didn't update in a meaningful way (decided by the compiler), dexopt + // will be skipped. + // Currently this only affects the optimization of primary apks. Secondary dex files + // will always check the profiles for updates. + public static final int DEXOPT_CHECK_FOR_PROFILES_UPDATES = 1 << 0; + + // When set, dexopt will execute unconditionally (even if not needed). + public static final int DEXOPT_FORCE = 1 << 1; + + // Whether or not the invocation of dexopt is done after the boot is completed. This is used + // in order to adjust the priority of the compilation thread. + public static final int DEXOPT_BOOT_COMPLETE = 1 << 2; + + // When set, the dexopt invocation will optimize only the secondary dex files. If false, dexopt + // will only consider the primary apk. + public static final int DEXOPT_ONLY_SECONDARY_DEX = 1 << 3; + + // When set, dexopt will optimize only dex files that are used by other apps. + // Currently, this flag is ignored for primary apks. + public static final int DEXOPT_ONLY_SHARED_DEX = 1 << 4; + + // When set, dexopt will attempt to scale down the optimizations previously applied in order + // save disk space. + public static final int DEXOPT_DOWNGRADE = 1 << 5; + + // When set, dexopt will compile the dex file as a shared library even if it is not actually + // used by other apps. This is used to force the compilation or shared libraries declared + // with in the manifest with ''uses-library' before we have a chance to detect they are + // actually shared at runtime. + public static final int DEXOPT_AS_SHARED_LIBRARY = 1 << 6; + + // When set, indicates that dexopt is invoked from the background service. + public static final int DEXOPT_IDLE_BACKGROUND_JOB = 1 << 9; + + // The name of package to optimize. + private final String mPackageName; + + // The intended target compiler filter. Note that dexopt might adjust the filter before the + // execution based on factors like: vmSafeMode and packageUsedByOtherApps. + private final String mCompilerFilter; + + // The set of flags for the dexopt options. It's a mix of the DEXOPT_* flags. + private final int mFlags; + + // When not null, dexopt will optimize only the split identified by this name. + // It only applies for primary apk and it's always null if mOnlySecondaryDex is true. + private final String mSplitName; + + public DexoptOptions(String packageName, String compilerFilter, int flags) { + this(packageName, compilerFilter, /*splitName*/ null, flags); + } + + public DexoptOptions(String packageName, int compilerReason, int flags) { + this(packageName, getCompilerFilterForReason(compilerReason), flags); + } + + public DexoptOptions(String packageName, String compilerFilter, String splitName, int flags) { + int validityMask = + DEXOPT_CHECK_FOR_PROFILES_UPDATES | + DEXOPT_FORCE | + DEXOPT_BOOT_COMPLETE | + DEXOPT_ONLY_SECONDARY_DEX | + DEXOPT_ONLY_SHARED_DEX | + DEXOPT_DOWNGRADE | + DEXOPT_AS_SHARED_LIBRARY | + DEXOPT_IDLE_BACKGROUND_JOB; + if ((flags & (~validityMask)) != 0) { + throw new IllegalArgumentException("Invalid flags : " + Integer.toHexString(flags)); + } + + mPackageName = packageName; + mCompilerFilter = compilerFilter; + mFlags = flags; + mSplitName = splitName; + } + + public String getPackageName() { + return mPackageName; + } + + public boolean isCheckForProfileUpdates() { + return (mFlags & DEXOPT_CHECK_FOR_PROFILES_UPDATES) != 0; + } + + public String getCompilerFilter() { + return mCompilerFilter; + } + + public boolean isForce() { + return (mFlags & DEXOPT_FORCE) != 0; + } + + public boolean isBootComplete() { + return (mFlags & DEXOPT_BOOT_COMPLETE) != 0; + } + + public boolean isDexoptOnlySecondaryDex() { + return (mFlags & DEXOPT_ONLY_SECONDARY_DEX) != 0; + } + + public boolean isDexoptOnlySharedDex() { + return (mFlags & DEXOPT_ONLY_SHARED_DEX) != 0; + } + + public boolean isDowngrade() { + return (mFlags & DEXOPT_DOWNGRADE) != 0; + } + + public boolean isDexoptAsSharedLibrary() { + return (mFlags & DEXOPT_AS_SHARED_LIBRARY) != 0; + } + + public boolean isDexoptIdleBackgroundJob() { + return (mFlags & DEXOPT_IDLE_BACKGROUND_JOB) != 0; + } + + public String getSplitName() { + return mSplitName; + } + + public int getFlags() { + return mFlags; + } +} diff --git a/services/core/java/com/android/server/pm/dex/DexoptUtils.java b/services/core/java/com/android/server/pm/dex/DexoptUtils.java new file mode 100644 index 000000000000..bc8bf5e1fc4c --- /dev/null +++ b/services/core/java/com/android/server/pm/dex/DexoptUtils.java @@ -0,0 +1,327 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.server.pm.dex; + +import android.content.pm.ApplicationInfo; +import android.util.Slog; +import android.util.SparseArray; + +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public final class DexoptUtils { + private static final String TAG = "DexoptUtils"; + + private DexoptUtils() {} + + /** + * Creates the class loader context dependencies for each of the application code paths. + * The returned array contains the class loader contexts that needs to be passed to dexopt in + * order to ensure correct optimizations. + * + * A class loader context describes how the class loader chain should be built by dex2oat + * in order to ensure that classes are resolved during compilation as they would be resolved + * at runtime. The context will be encoded in the compiled code. If at runtime the dex file is + * loaded in a different context (with a different set of class loaders or a different + * classpath), the compiled code will be rejected. + * + * Note that the class loader context only includes dependencies and not the code path itself. + * The contexts are created based on the application split dependency list and + * the provided shared libraries. + * + * All the code paths encoded in the context will be relative to the base directory. This + * enables stage compilation where compiler artifacts may be moved around. + * + * The result is indexed as follows: + * - index 0 contains the context for the base apk + * - index 1 to n contain the context for the splits in the order determined by + * {@code info.getSplitCodePaths()} + * + * IMPORTANT: keep this logic in sync with the loading code in {@link android.app.LoadedApk} + * and pay attention to the way the classpath is created for the non isolated mode in: + * {@link android.app.LoadedApk#makePaths( + * android.app.ActivityThread, boolean, ApplicationInfo, List, List)}. + */ + public static String[] getClassLoaderContexts(ApplicationInfo info, String[] sharedLibraries) { + // The base class loader context contains only the shared library. + String sharedLibrariesClassPath = encodeClasspath(sharedLibraries); + String baseApkContextClassLoader = encodeClassLoader( + sharedLibrariesClassPath, "dalvik.system.PathClassLoader"); + + if (info.getSplitCodePaths() == null) { + // The application has no splits. + return new String[] {baseApkContextClassLoader}; + } + + // The application has splits. Compute their class loader contexts. + + // First, cache the relative paths of the splits and do some sanity checks + String[] splitRelativeCodePaths = getSplitRelativeCodePaths(info); + + // The splits have an implicit dependency on the base apk. + // This means that we have to add the base apk file in addition to the shared libraries. + String baseApkName = new File(info.getBaseCodePath()).getName(); + String sharedLibrariesAndBaseClassPath = + encodeClasspath(sharedLibrariesClassPath, baseApkName); + + // The result is stored in classLoaderContexts. + // Index 0 is the class loaded context for the base apk. + // Index `i` is the class loader context encoding for split `i`. + String[] classLoaderContexts = new String[/*base apk*/ 1 + splitRelativeCodePaths.length]; + classLoaderContexts[0] = baseApkContextClassLoader; + + if (!info.requestsIsolatedSplitLoading() || info.splitDependencies == null) { + // If the app didn't request for the splits to be loaded in isolation or if it does not + // declare inter-split dependencies, then all the splits will be loaded in the base + // apk class loader (in the order of their definition). + String classpath = sharedLibrariesAndBaseClassPath; + for (int i = 1; i < classLoaderContexts.length; i++) { + classLoaderContexts[i] = encodeClassLoader(classpath, "dalvik.system.PathClassLoader"); + classpath = encodeClasspath(classpath, splitRelativeCodePaths[i - 1]); + } + } else { + // In case of inter-split dependencies, we need to walk the dependency chain of each + // split. We do this recursively and store intermediate results in classLoaderContexts. + + // First, look at the split class loaders and cache their individual contexts (i.e. + // the class loader + the name of the split). This is an optimization to avoid + // re-computing them during the recursive call. + // The cache is stored in splitClassLoaderEncodingCache. The difference between this and + // classLoaderContexts is that the later contains the full chain of class loaders for + // a given split while splitClassLoaderEncodingCache only contains a single class loader + // encoding. + String[] splitClassLoaderEncodingCache = new String[splitRelativeCodePaths.length]; + for (int i = 0; i < splitRelativeCodePaths.length; i++) { + splitClassLoaderEncodingCache[i] = encodeClassLoader(splitRelativeCodePaths[i], + "dalvik.system.PathClassLoader"); + } + String splitDependencyOnBase = encodeClassLoader( + sharedLibrariesAndBaseClassPath, "dalvik.system.PathClassLoader"); + SparseArray<int[]> splitDependencies = info.splitDependencies; + for (int i = 1; i < splitDependencies.size(); i++) { + getParentDependencies(splitDependencies.keyAt(i), splitClassLoaderEncodingCache, + splitDependencies, classLoaderContexts, splitDependencyOnBase); + } + + // At this point classLoaderContexts contains only the parent dependencies. + // We also need to add the class loader of the current split which should + // come first in the context. + for (int i = 1; i < classLoaderContexts.length; i++) { + String splitClassLoader = encodeClassLoader("", "dalvik.system.PathClassLoader"); + classLoaderContexts[i] = encodeClassLoaderChain( + splitClassLoader, classLoaderContexts[i]); + } + } + + return classLoaderContexts; + } + + /** + * Recursive method to generate the class loader context dependencies for the split with the + * given index. {@param classLoaderContexts} acts as an accumulator. Upton return + * {@code classLoaderContexts[index]} will contain the split dependency. + * During computation, the method may resolve the dependencies of other splits as it traverses + * the entire parent chain. The result will also be stored in {@param classLoaderContexts}. + * + * Note that {@code index 0} denotes the base apk and it is special handled. When the + * recursive call hits {@code index 0} the method returns {@code splitDependencyOnBase}. + * {@code classLoaderContexts[0]} is not modified in this method. + * + * @param index the index of the split (Note that index 0 denotes the base apk) + * @param splitClassLoaderEncodingCache the class loader encoding for the individual splits. + * It contains only the split class loader and not the the base. The split + * with {@code index} has its context at {@code splitClassLoaderEncodingCache[index - 1]}. + * @param splitDependencies the dependencies for all splits. Note that in this array index 0 + * is the base and splits start from index 1. + * @param classLoaderContexts the result accumulator. index 0 is the base and never set. Splits + * start at index 1. + * @param splitDependencyOnBase the encoding of the implicit split dependency on base. + */ + private static String getParentDependencies(int index, String[] splitClassLoaderEncodingCache, + SparseArray<int[]> splitDependencies, String[] classLoaderContexts, + String splitDependencyOnBase) { + // If we hit the base apk return its custom dependency list which is + // sharedLibraries + base.apk + if (index == 0) { + return splitDependencyOnBase; + } + // Return the result if we've computed the splitDependencies for this index already. + if (classLoaderContexts[index] != null) { + return classLoaderContexts[index]; + } + // Get the splitDependencies for the parent of this index and append its path to it. + int parent = splitDependencies.get(index)[0]; + String parentDependencies = getParentDependencies(parent, splitClassLoaderEncodingCache, + splitDependencies, classLoaderContexts, splitDependencyOnBase); + + // The split context is: `parent context + parent dependencies context`. + String splitContext = (parent == 0) ? + parentDependencies : + encodeClassLoaderChain(splitClassLoaderEncodingCache[parent - 1], parentDependencies); + classLoaderContexts[index] = splitContext; + return splitContext; + } + + /** + * Encodes the shared libraries classpathElements in a format accepted by dexopt. + * NOTE: Keep this in sync with the dexopt expectations! Right now that is + * a list separated by ':'. + */ + private static String encodeClasspath(String[] classpathElements) { + if (classpathElements == null || classpathElements.length == 0) { + return ""; + } + StringBuilder sb = new StringBuilder(); + for (String element : classpathElements) { + if (sb.length() != 0) { + sb.append(":"); + } + sb.append(element); + } + return sb.toString(); + } + + /** + * Adds an element to the encoding of an existing classpath. + * {@see PackageDexOptimizer.encodeClasspath(String[])} + */ + private static String encodeClasspath(String classpath, String newElement) { + return classpath.isEmpty() ? newElement : (classpath + ":" + newElement); + } + + /** + * Encodes a single class loader dependency starting from {@param path} and + * {@param classLoaderName}. + * NOTE: Keep this in sync with the dexopt expectations! Right now that is either "PCL[path]" + * for a PathClassLoader or "DLC[path]" for a DelegateLastClassLoader. + */ + private static String encodeClassLoader(String classpath, String classLoaderName) { + String classLoaderDexoptEncoding = classLoaderName; + if ("dalvik.system.PathClassLoader".equals(classLoaderName)) { + classLoaderDexoptEncoding = "PCL"; + } else { + Slog.wtf(TAG, "Unsupported classLoaderName: " + classLoaderName); + } + return classLoaderDexoptEncoding + "[" + classpath + "]"; + } + + /** + * Links to dependencies together in a format accepted by dexopt. + * NOTE: Keep this in sync with the dexopt expectations! Right now that is a list of split + * dependencies {@see encodeClassLoader} separated by ';'. + */ + private static String encodeClassLoaderChain(String cl1, String cl2) { + if (cl1.isEmpty()) return cl2; + if (cl2.isEmpty()) return cl1; + return cl1 + ";" + cl2; + } + + /** + * Compute the class loader context for the dex files present in the classpath of the first + * class loader from the given list (referred in the code as the {@code loadingClassLoader}). + * Each dex files gets its own class loader context in the returned array. + * + * Example: + * If classLoadersNames = {"dalvik.system.DelegateLastClassLoader", + * "dalvik.system.PathClassLoader"} and classPaths = {"foo.dex:bar.dex", "other.dex"} + * The output will be + * {"DLC[];PCL[other.dex]", "DLC[foo.dex];PCL[other.dex]"} + * with "DLC[];PCL[other.dex]" being the context for "foo.dex" + * and "DLC[foo.dex];PCL[other.dex]" the context for "bar.dex". + * + * If any of the class loaders names is unsupported the method will return null. + * + * The argument lists must be non empty and of the same size. + * + * @param classLoadersNames the names of the class loaders present in the loading chain. The + * list encodes the class loader chain in the natural order. The first class loader has + * the second one as its parent and so on. + * @param classPaths the class paths for the elements of {@param classLoadersNames}. The + * the first element corresponds to the first class loader and so on. A classpath is + * represented as a list of dex files separated by {@code File.pathSeparator}. + * The return context will be for the dex files found in the first class path. + */ + /*package*/ static String[] processContextForDexLoad(List<String> classLoadersNames, + List<String> classPaths) { + if (classLoadersNames.size() != classPaths.size()) { + throw new IllegalArgumentException( + "The size of the class loader names and the dex paths do not match."); + } + if (classLoadersNames.isEmpty()) { + throw new IllegalArgumentException("Empty classLoadersNames"); + } + + // Compute the context for the parent class loaders. + String parentContext = ""; + // We know that these lists are actually ArrayLists so getting the elements by index + // is fine (they come over binder). Even if something changes we expect the sizes to be + // very small and it shouldn't matter much. + for (int i = 1; i < classLoadersNames.size(); i++) { + if (!isValidClassLoaderName(classLoadersNames.get(i))) { + return null; + } + String classpath = encodeClasspath(classPaths.get(i).split(File.pathSeparator)); + parentContext = encodeClassLoaderChain(parentContext, + encodeClassLoader(classpath, classLoadersNames.get(i))); + } + + // Now compute the class loader context for each dex file from the first classpath. + String loadingClassLoader = classLoadersNames.get(0); + if (!isValidClassLoaderName(loadingClassLoader)) { + return null; + } + String[] loadedDexPaths = classPaths.get(0).split(File.pathSeparator); + String[] loadedDexPathsContext = new String[loadedDexPaths.length]; + String currentLoadedDexPathClasspath = ""; + for (int i = 0; i < loadedDexPaths.length; i++) { + String dexPath = loadedDexPaths[i]; + String currentContext = encodeClassLoader( + currentLoadedDexPathClasspath, loadingClassLoader); + loadedDexPathsContext[i] = encodeClassLoaderChain(currentContext, parentContext); + currentLoadedDexPathClasspath = encodeClasspath(currentLoadedDexPathClasspath, dexPath); + } + return loadedDexPathsContext; + } + + // AOSP-only hack. + private static boolean isValidClassLoaderName(String name) { + return "dalvik.system.PathClassLoader".equals(name) || "dalvik.system.DexClassLoader".equals(name); + } + + /** + * Returns the relative paths of the splits declared by the application {@code info}. + * Assumes that the application declares a non-null array of splits. + */ + private static String[] getSplitRelativeCodePaths(ApplicationInfo info) { + String baseCodePath = new File(info.getBaseCodePath()).getParent(); + String[] splitCodePaths = info.getSplitCodePaths(); + String[] splitRelativeCodePaths = new String[splitCodePaths.length]; + for (int i = 0; i < splitCodePaths.length; i++) { + File pathFile = new File(splitCodePaths[i]); + splitRelativeCodePaths[i] = pathFile.getName(); + // Sanity check that the base paths of the splits are all the same. + String basePath = pathFile.getParent(); + if (!basePath.equals(baseCodePath)) { + Slog.wtf(TAG, "Split paths have different base paths: " + basePath + " and " + + baseCodePath); + } + } + return splitRelativeCodePaths; + } +} diff --git a/services/core/java/com/android/server/pm/dex/PackageDexUsage.java b/services/core/java/com/android/server/pm/dex/PackageDexUsage.java index 8a66f12cb6d9..a4a0a54b39d1 100644 --- a/services/core/java/com/android/server/pm/dex/PackageDexUsage.java +++ b/services/core/java/com/android/server/pm/dex/PackageDexUsage.java @@ -26,7 +26,6 @@ import com.android.server.pm.AbstractStatsBase; import com.android.server.pm.PackageManagerServiceUtils; import java.io.BufferedReader; -import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; @@ -35,14 +34,19 @@ import java.io.OutputStreamWriter; import java.io.Reader; import java.io.StringWriter; import java.io.Writer; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; import java.util.Iterator; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; import dalvik.system.VMRuntime; import libcore.io.IoUtils; +import libcore.util.Objects; /** * Stat file which store usage information about dex files. @@ -50,19 +54,44 @@ import libcore.io.IoUtils; public class PackageDexUsage extends AbstractStatsBase<Void> { private final static String TAG = "PackageDexUsage"; - private final static int PACKAGE_DEX_USAGE_VERSION = 1; + // We support previous version to ensure that the usage list remains valid cross OTAs. + private final static int PACKAGE_DEX_USAGE_SUPPORTED_VERSION_1 = 1; + // Version 2 added: + // - the list of packages that load the dex files + // - class loader contexts for secondary dex files + // - usage for all code paths (including splits) + private final static int PACKAGE_DEX_USAGE_SUPPORTED_VERSION_2 = 2; + + private final static int PACKAGE_DEX_USAGE_VERSION = PACKAGE_DEX_USAGE_SUPPORTED_VERSION_2; + private final static String PACKAGE_DEX_USAGE_VERSION_HEADER = "PACKAGE_MANAGER__PACKAGE_DEX_USAGE__"; private final static String SPLIT_CHAR = ","; + private final static String CODE_PATH_LINE_CHAR = "+"; private final static String DEX_LINE_CHAR = "#"; + private final static String LOADING_PACKAGE_CHAR = "@"; + + // One of the things we record about dex files is the class loader context that was used to + // load them. That should be stable but if it changes we don't keep track of variable contexts. + // Instead we put a special marker in the dex usage file in order to recognize the case and + // skip optimizations on that dex files. + /*package*/ static final String VARIABLE_CLASS_LOADER_CONTEXT = + "=VariableClassLoaderContext="; + // The marker used for unsupported class loader contexts. + /*package*/ static final String UNSUPPORTED_CLASS_LOADER_CONTEXT = + "=UnsupportedClassLoaderContext="; + // The markers used for unknown class loader contexts. This can happen if the dex file was + // recorded in a previous version and we didn't have a chance to update its usage. + /*package*/ static final String UNKNOWN_CLASS_LOADER_CONTEXT = + "=UnknownClassLoaderContext="; // Map which structures the information we have on a package. // Maps package name to package data (which stores info about UsedByOtherApps and // secondary dex files.). // Access to this map needs synchronized. @GuardedBy("mPackageUseInfoMap") - private Map<String, PackageUseInfo> mPackageUseInfoMap; + private final Map<String, PackageUseInfo> mPackageUseInfoMap; public PackageDexUsage() { super("package-dex-usage.list", "PackageDexUsage_DiskWriter", /*lock*/ false); @@ -75,21 +104,28 @@ public class PackageDexUsage extends AbstractStatsBase<Void> { * Note this is called when apps load dex files and as such it should return * as fast as possible. * - * @param loadingPackage the package performing the load + * @param owningPackageName the package owning the dex path * @param dexPath the path of the dex files being loaded * @param ownerUserId the user id which runs the code loading the dex files * @param loaderIsa the ISA of the app loading the dex files * @param isUsedByOtherApps whether or not this dex file was not loaded by its owning package * @param primaryOrSplit whether or not the dex file is a primary/split dex. True indicates * the file is either primary or a split. False indicates the file is secondary dex. + * @param loadingPackageName the package performing the load. Recorded only if it is different + * than {@param owningPackageName}. * @return true if the dex load constitutes new information, or false if this information * has been seen before. */ public boolean record(String owningPackageName, String dexPath, int ownerUserId, - String loaderIsa, boolean isUsedByOtherApps, boolean primaryOrSplit) { + String loaderIsa, boolean isUsedByOtherApps, boolean primaryOrSplit, + String loadingPackageName, String classLoaderContext) { if (!PackageManagerServiceUtils.checkISA(loaderIsa)) { throw new IllegalArgumentException("loaderIsa " + loaderIsa + " is unsupported"); } + if (classLoaderContext == null) { + throw new IllegalArgumentException("Null classLoaderContext"); + } + synchronized (mPackageUseInfoMap) { PackageUseInfo packageUseInfo = mPackageUseInfoMap.get(owningPackageName); if (packageUseInfo == null) { @@ -99,12 +135,16 @@ public class PackageDexUsage extends AbstractStatsBase<Void> { // If we have a primary or a split apk, set isUsedByOtherApps. // We do not need to record the loaderIsa or the owner because we compile // primaries for all users and all ISAs. - packageUseInfo.mIsUsedByOtherApps = isUsedByOtherApps; + packageUseInfo.mergeCodePathUsedByOtherApps(dexPath, isUsedByOtherApps, + owningPackageName, loadingPackageName); } else { // For secondary dex files record the loaderISA and the owner. We'll need // to know under which user to compile and for what ISA. - packageUseInfo.mDexUseInfoMap.put( - dexPath, new DexUseInfo(isUsedByOtherApps, ownerUserId, loaderIsa)); + DexUseInfo newData = new DexUseInfo(isUsedByOtherApps, ownerUserId, + classLoaderContext, loaderIsa); + packageUseInfo.mDexUseInfoMap.put(dexPath, newData); + maybeAddLoadingPackage(owningPackageName, loadingPackageName, + newData.mLoadingPackages); } mPackageUseInfoMap.put(owningPackageName, packageUseInfo); return true; @@ -113,10 +153,14 @@ public class PackageDexUsage extends AbstractStatsBase<Void> { if (primaryOrSplit) { // We have a possible update on the primary apk usage. Merge // isUsedByOtherApps information and return if there was an update. - return packageUseInfo.merge(isUsedByOtherApps); + return packageUseInfo.mergeCodePathUsedByOtherApps( + dexPath, isUsedByOtherApps, owningPackageName, loadingPackageName); } else { DexUseInfo newData = new DexUseInfo( - isUsedByOtherApps, ownerUserId, loaderIsa); + isUsedByOtherApps, ownerUserId, classLoaderContext, loaderIsa); + boolean updateLoadingPackages = maybeAddLoadingPackage(owningPackageName, + loadingPackageName, newData.mLoadingPackages); + DexUseInfo existingData = packageUseInfo.mDexUseInfoMap.get(dexPath); if (existingData == null) { // It's the first time we see this dex file. @@ -138,7 +182,7 @@ public class PackageDexUsage extends AbstractStatsBase<Void> { } // Merge the information into the existing data. // Returns true if there was an update. - return existingData.merge(newData); + return existingData.merge(newData) || updateLoadingPackages; } } } @@ -157,8 +201,12 @@ public class PackageDexUsage extends AbstractStatsBase<Void> { * Convenience method for async writes which does not force the user to pass a useless * (Void) null. */ - public void maybeWriteAsync() { - maybeWriteAsync((Void) null); + /*package*/ void maybeWriteAsync() { + maybeWriteAsync(null); + } + + /*package*/ void writeNow() { + writeInternal(null); } @Override @@ -185,16 +233,18 @@ public class PackageDexUsage extends AbstractStatsBase<Void> { * * file_magic_version * package_name_1 + * +code_path1 + * @ loading_package_1_1, loading_package_1_2... + * +code_path2 + * @ loading_package_2_1, loading_package_2_2... * #dex_file_path_1_1 * user_1_1, used_by_other_app_1_1, user_isa_1_1_1, user_isa_1_1_2 + * @ loading_package_1_1_1, loading_package_1_1_2... + * class_loader_context_1_1 * #dex_file_path_1_2 * user_1_2, used_by_other_app_1_2, user_isa_1_2_1, user_isa_1_2_2 - * ... - * package_name_2 - * #dex_file_path_2_1 - * user_2_1, used_by_other_app_2_1, user_isa_2_1_1, user_isa_2_1_2 - * #dex_file_path_2_2, - * user_2_2, used_by_other_app_2_2, user_isa_2_2_1, user_isa_2_2_2 + * @ loading_package_1_2_1, loading_package_1_2_2... + * class_loader_context_1_2 * ... */ /* package */ void write(Writer out) { @@ -211,9 +261,16 @@ public class PackageDexUsage extends AbstractStatsBase<Void> { // Write the package line. String packageName = pEntry.getKey(); PackageUseInfo packageUseInfo = pEntry.getValue(); - - fpw.println(String.join(SPLIT_CHAR, packageName, - writeBoolean(packageUseInfo.mIsUsedByOtherApps))); + fpw.println(packageName); + + // Write the code paths used by other apps. + for (Map.Entry<String, Set<String>> codeEntry : + packageUseInfo.mCodePathsUsedByOtherApps.entrySet()) { + String codePath = codeEntry.getKey(); + Set<String> loadingPackages = codeEntry.getValue(); + fpw.println(CODE_PATH_LINE_CHAR + codePath); + fpw.println(LOADING_PACKAGE_CHAR + String.join(SPLIT_CHAR, loadingPackages)); + } // Write dex file lines. for (Map.Entry<String, DexUseInfo> dEntry : packageUseInfo.mDexUseInfoMap.entrySet()) { @@ -221,11 +278,14 @@ public class PackageDexUsage extends AbstractStatsBase<Void> { DexUseInfo dexUseInfo = dEntry.getValue(); fpw.println(DEX_LINE_CHAR + dexPath); fpw.print(String.join(SPLIT_CHAR, Integer.toString(dexUseInfo.mOwnerUserId), - writeBoolean(dexUseInfo.mIsUsedByOtherApps))); + writeBoolean(dexUseInfo.mIsUsedByOtherApps))); for (String isa : dexUseInfo.mLoaderIsas) { fpw.print(SPLIT_CHAR + isa); } fpw.println(); + fpw.println(LOADING_PACKAGE_CHAR + + String.join(SPLIT_CHAR, dexUseInfo.mLoadingPackages)); + fpw.println(dexUseInfo.getClassLoaderContext()); } } fpw.flush(); @@ -252,6 +312,7 @@ public class PackageDexUsage extends AbstractStatsBase<Void> { BufferedReader in = new BufferedReader(reader); // Read header, do version check. String versionLine = in.readLine(); + int version; if (versionLine == null) { throw new IllegalStateException("No version line found."); } else { @@ -259,48 +320,56 @@ public class PackageDexUsage extends AbstractStatsBase<Void> { // TODO(calin): the caller is responsible to clear the file. throw new IllegalStateException("Invalid version line: " + versionLine); } - int version = Integer.parseInt( + version = Integer.parseInt( versionLine.substring(PACKAGE_DEX_USAGE_VERSION_HEADER.length())); - if (version != PACKAGE_DEX_USAGE_VERSION) { + if (!isSupportedVersion(version)) { throw new IllegalStateException("Unexpected version: " + version); } } - String s = null; - String currentPakage = null; - PackageUseInfo currentPakageData = null; + String line; + String currentPackage = null; + PackageUseInfo currentPackageData = null; Set<String> supportedIsas = new HashSet<>(); for (String abi : Build.SUPPORTED_ABIS) { supportedIsas.add(VMRuntime.getInstructionSet(abi)); } - while ((s = in.readLine()) != null) { - if (s.startsWith(DEX_LINE_CHAR)) { + while ((line = in.readLine()) != null) { + if (line.startsWith(DEX_LINE_CHAR)) { // This is the start of the the dex lines. - // We expect two lines for each dex entry: + // We expect 4 lines for each dex entry: // #dexPaths + // @loading_package_1,loading_package_2,... + // class_loader_context // onwerUserId,isUsedByOtherApps,isa1,isa2 - if (currentPakage == null) { + if (currentPackage == null) { throw new IllegalStateException( "Malformed PackageDexUsage file. Expected package line before dex line."); } - // First line is the dex path. - String dexPath = s.substring(DEX_LINE_CHAR.length()); - // Next line is the dex data. - s = in.readLine(); - if (s == null) { - throw new IllegalStateException("Could not fine dexUseInfo for line: " + s); - } + // Line 1 is the dex path. + String dexPath = line.substring(DEX_LINE_CHAR.length()); - // We expect at least 3 elements (isUsedByOtherApps, userId, isa). - String[] elems = s.split(SPLIT_CHAR); + // Line 2 is the dex data: (userId, isUsedByOtherApps, isa). + line = in.readLine(); + if (line == null) { + throw new IllegalStateException("Could not find dexUseInfo line"); + } + String[] elems = line.split(SPLIT_CHAR); if (elems.length < 3) { - throw new IllegalStateException("Invalid PackageDexUsage line: " + s); + throw new IllegalStateException("Invalid PackageDexUsage line: " + line); } + + // In version 2 we added the loading packages and class loader context. + Set<String> loadingPackages = maybeReadLoadingPackages(in, version); + String classLoaderContext = maybeReadClassLoaderContext(in, version); + int ownerUserId = Integer.parseInt(elems[0]); boolean isUsedByOtherApps = readBoolean(elems[1]); - DexUseInfo dexUseInfo = new DexUseInfo(isUsedByOtherApps, ownerUserId); + DexUseInfo dexUseInfo = new DexUseInfo(isUsedByOtherApps, ownerUserId, + classLoaderContext, /*isa*/ null); + dexUseInfo.mLoadingPackages.addAll(loadingPackages); for (int i = 2; i < elems.length; i++) { String isa = elems[i]; if (supportedIsas.contains(isa)) { @@ -317,18 +386,37 @@ public class PackageDexUsage extends AbstractStatsBase<Void> { "unsupported isas. dexPath=" + dexPath); continue; } - currentPakageData.mDexUseInfoMap.put(dexPath, dexUseInfo); + currentPackageData.mDexUseInfoMap.put(dexPath, dexUseInfo); + } else if (line.startsWith(CODE_PATH_LINE_CHAR)) { + // This is a code path used by other apps line. + if (version < PACKAGE_DEX_USAGE_SUPPORTED_VERSION_2) { + throw new IllegalArgumentException("Unexpected code path line when parsing " + + "PackageDexUseData: " + line); + } + + // Expects 2 lines: + // +code_paths + // @loading_packages + String codePath = line.substring(CODE_PATH_LINE_CHAR.length()); + Set<String> loadingPackages = maybeReadLoadingPackages(in, version); + currentPackageData.mCodePathsUsedByOtherApps.put(codePath, loadingPackages); } else { // This is a package line. - // We expect it to be: `packageName,isUsedByOtherApps`. - String[] elems = s.split(SPLIT_CHAR); - if (elems.length != 2) { - throw new IllegalStateException("Invalid PackageDexUsage line: " + s); + if (version >= PACKAGE_DEX_USAGE_SUPPORTED_VERSION_2) { + currentPackage = line; + currentPackageData = new PackageUseInfo(); + } else { + // Old version (<2) + // We expect it to be: `packageName,isUsedByOtherApps`. + String[] elems = line.split(SPLIT_CHAR); + if (elems.length != 2) { + throw new IllegalStateException("Invalid PackageDexUsage line: " + line); + } + currentPackage = elems[0]; + currentPackageData = new PackageUseInfo(); + currentPackageData.mUsedByOtherAppsBeforeUpgrade = readBoolean(elems[1]); } - currentPakage = elems[0]; - currentPakageData = new PackageUseInfo(); - currentPakageData.mIsUsedByOtherApps = readBoolean(elems[1]); - data.put(currentPakage, currentPakageData); + data.put(currentPackage, currentPackageData); } } @@ -339,9 +427,67 @@ public class PackageDexUsage extends AbstractStatsBase<Void> { } /** + * Reads the class loader context encoding from the buffer {@code in} if + * {@code version} is at least {PACKAGE_DEX_USAGE_VERSION}. + */ + private String maybeReadClassLoaderContext(BufferedReader in, int version) throws IOException { + String context = null; + if (version >= PACKAGE_DEX_USAGE_SUPPORTED_VERSION_2) { + context = in.readLine(); + if (context == null) { + throw new IllegalStateException("Could not find the classLoaderContext line."); + } + } + // The context might be empty if we didn't have the chance to update it after a version + // upgrade. In this case return the special marker so that we recognize this is an unknown + // context. + return context == null ? UNKNOWN_CLASS_LOADER_CONTEXT : context; + } + + /** + * Reads the list of loading packages from the buffer {@code in} if + * {@code version} is at least {PACKAGE_DEX_USAGE_SUPPORTED_VERSION_2}. + */ + private Set<String> maybeReadLoadingPackages(BufferedReader in, int version) + throws IOException { + if (version >= PACKAGE_DEX_USAGE_SUPPORTED_VERSION_2) { + String line = in.readLine(); + if (line == null) { + throw new IllegalStateException("Could not find the loadingPackages line."); + } + // We expect that most of the times the list of loading packages will be empty. + if (line.length() == LOADING_PACKAGE_CHAR.length()) { + return Collections.emptySet(); + } else { + Set<String> result = new HashSet<>(); + Collections.addAll(result, + line.substring(LOADING_PACKAGE_CHAR.length()).split(SPLIT_CHAR)); + return result; + } + } else { + return Collections.emptySet(); + } + } + + /** + * Utility method which adds {@param loadingPackage} to {@param loadingPackages} only if it's + * not equal to {@param owningPackage} + */ + private boolean maybeAddLoadingPackage(String owningPackage, String loadingPackage, + Set<String> loadingPackages) { + return !owningPackage.equals(loadingPackage) && loadingPackages.add(loadingPackage); + } + + private boolean isSupportedVersion(int version) { + return version == PACKAGE_DEX_USAGE_SUPPORTED_VERSION_1 + || version == PACKAGE_DEX_USAGE_SUPPORTED_VERSION_2; + } + + /** * Syncs the existing data with the set of available packages by removing obsolete entries. */ - public void syncData(Map<String, Set<Integer>> packageToUsersMap) { + /*package*/ void syncData(Map<String, Set<Integer>> packageToUsersMap, + Map<String, Set<String>> packageToCodePaths) { synchronized (mPackageUseInfoMap) { Iterator<Map.Entry<String, PackageUseInfo>> pIt = mPackageUseInfoMap.entrySet().iterator(); @@ -365,8 +511,26 @@ public class PackageDexUsage extends AbstractStatsBase<Void> { dIt.remove(); } } - if (!packageUseInfo.mIsUsedByOtherApps - && packageUseInfo.mDexUseInfoMap.isEmpty()) { + + // Sync the code paths. + Set<String> codePaths = packageToCodePaths.get(packageName); + Iterator<Map.Entry<String, Set<String>>> codeIt = + packageUseInfo.mCodePathsUsedByOtherApps.entrySet().iterator(); + while (codeIt.hasNext()) { + if (!codePaths.contains(codeIt.next().getKey())) { + codeIt.remove(); + } + } + + // In case the package was marked as used by other apps in a previous version + // propagate the flag to all the code paths. + // See mUsedByOtherAppsBeforeUpgrade docs on why it is important to do it. + if (packageUseInfo.mUsedByOtherAppsBeforeUpgrade) { + for (String codePath : codePaths) { + packageUseInfo.mergeCodePathUsedByOtherApps(codePath, true, null, null); + } + } else if (!packageUseInfo.isAnyCodePathUsedByOtherApps() + && packageUseInfo.mDexUseInfoMap.isEmpty()) { // The package is not used by other apps and we removed all its dex files // records. Remove the entire package record as well. pIt.remove(); @@ -380,14 +544,13 @@ public class PackageDexUsage extends AbstractStatsBase<Void> { * Clears the {@code usesByOtherApps} marker for the package {@code packageName}. * @return true if the package usage info was updated. */ - public boolean clearUsedByOtherApps(String packageName) { + /*package*/ boolean clearUsedByOtherApps(String packageName) { synchronized (mPackageUseInfoMap) { PackageUseInfo packageUseInfo = mPackageUseInfoMap.get(packageName); - if (packageUseInfo == null || !packageUseInfo.mIsUsedByOtherApps) { + if (packageUseInfo == null) { return false; } - packageUseInfo.mIsUsedByOtherApps = false; - return true; + return packageUseInfo.clearCodePathUsedByOtherApps(); } } @@ -408,7 +571,7 @@ public class PackageDexUsage extends AbstractStatsBase<Void> { * @return true if the record was found and actually deleted, * false if the record doesn't exist */ - public boolean removeUserPackage(String packageName, int userId) { + /*package*/ boolean removeUserPackage(String packageName, int userId) { synchronized (mPackageUseInfoMap) { PackageUseInfo packageUseInfo = mPackageUseInfoMap.get(packageName); if (packageUseInfo == null) { @@ -426,7 +589,8 @@ public class PackageDexUsage extends AbstractStatsBase<Void> { } // If no secondary dex info is left and the package is not used by other apps // remove the data since it is now useless. - if (packageUseInfo.mDexUseInfoMap.isEmpty() && !packageUseInfo.mIsUsedByOtherApps) { + if (packageUseInfo.mDexUseInfoMap.isEmpty() + && !packageUseInfo.isAnyCodePathUsedByOtherApps()) { mPackageUseInfoMap.remove(packageName); updated = true; } @@ -440,7 +604,7 @@ public class PackageDexUsage extends AbstractStatsBase<Void> { * @return true if the record was found and actually deleted, * false if the record doesn't exist */ - public boolean removeDexFile(String packageName, String dexFile, int userId) { + /*package*/ boolean removeDexFile(String packageName, String dexFile, int userId) { synchronized (mPackageUseInfoMap) { PackageUseInfo packageUseInfo = mPackageUseInfoMap.get(packageName); if (packageUseInfo == null) { @@ -462,7 +626,7 @@ public class PackageDexUsage extends AbstractStatsBase<Void> { return false; } - public PackageUseInfo getPackageUseInfo(String packageName) { + /*package*/ PackageUseInfo getPackageUseInfo(String packageName) { synchronized (mPackageUseInfoMap) { PackageUseInfo useInfo = mPackageUseInfoMap.get(packageName); // The useInfo contains a map for secondary dex files which could be modified @@ -477,7 +641,7 @@ public class PackageDexUsage extends AbstractStatsBase<Void> { /** * Return all packages that contain records of secondary dex files. */ - public Set<String> getAllPackagesWithSecondaryDexFiles() { + /*package*/ Set<String> getAllPackagesWithSecondaryDexFiles() { Set<String> packages = new HashSet<>(); synchronized (mPackageUseInfoMap) { for (Map.Entry<String, PackageUseInfo> entry : mPackageUseInfoMap.entrySet()) { @@ -515,15 +679,6 @@ public class PackageDexUsage extends AbstractStatsBase<Void> { throw new IllegalArgumentException("Unknown bool encoding: " + bool); } - private boolean contains(int[] array, int elem) { - for (int i = 0; i < array.length; i++) { - if (elem == array[i]) { - return true; - } - } - return false; - } - public String dump() { StringWriter sw = new StringWriter(); write(sw); @@ -534,39 +689,95 @@ public class PackageDexUsage extends AbstractStatsBase<Void> { * Stores data on how a package and its dex files are used. */ public static class PackageUseInfo { - // This flag is for the primary and split apks. It is set to true whenever one of them - // is loaded by another app. - private boolean mIsUsedByOtherApps; + // The app's code paths that are used by other apps. + // The key is the code path and the value is the set of loading packages. + private final Map<String, Set<String>> mCodePathsUsedByOtherApps; // Map dex paths to their data (isUsedByOtherApps, owner id, loader isa). private final Map<String, DexUseInfo> mDexUseInfoMap; + // Keeps track of whether or not this package was used by other apps before + // we upgraded to VERSION 4 which records the info for each code path separately. + // This is unwanted complexity but without it we risk to profile guide compile + // something that supposed to be shared. For example: + // 1) we determine that chrome is used by another app + // 2) we take an OTA which upgrades the way we keep track of usage data + // 3) chrome doesn't get used until the background job executes + // 4) as part of the backgound job we now think that chrome is not used by others + // and we speed-profile. + // 5) as a result the next time someone uses chrome it will extract from apk since + // the compiled code will be private. + private boolean mUsedByOtherAppsBeforeUpgrade; + public PackageUseInfo() { - mIsUsedByOtherApps = false; + mCodePathsUsedByOtherApps = new HashMap<>(); mDexUseInfoMap = new HashMap<>(); } // Creates a deep copy of the `other`. public PackageUseInfo(PackageUseInfo other) { - mIsUsedByOtherApps = other.mIsUsedByOtherApps; + mCodePathsUsedByOtherApps = new HashMap<>(); + for (Map.Entry<String, Set<String>> e : other.mCodePathsUsedByOtherApps.entrySet()) { + mCodePathsUsedByOtherApps.put(e.getKey(), new HashSet<>(e.getValue())); + } + mDexUseInfoMap = new HashMap<>(); for (Map.Entry<String, DexUseInfo> e : other.mDexUseInfoMap.entrySet()) { mDexUseInfoMap.put(e.getKey(), new DexUseInfo(e.getValue())); } } - private boolean merge(boolean isUsedByOtherApps) { - boolean oldIsUsedByOtherApps = mIsUsedByOtherApps; - mIsUsedByOtherApps = mIsUsedByOtherApps || isUsedByOtherApps; - return oldIsUsedByOtherApps != this.mIsUsedByOtherApps; + private boolean mergeCodePathUsedByOtherApps(String codePath, boolean isUsedByOtherApps, + String owningPackageName, String loadingPackage) { + if (!isUsedByOtherApps) { + // Nothing to update if the the code path is not used by other apps. + return false; + } + + boolean newCodePath = false; + Set<String> loadingPackages = mCodePathsUsedByOtherApps.get(codePath); + if (loadingPackages == null) { + loadingPackages = new HashSet<>(); + mCodePathsUsedByOtherApps.put(codePath, loadingPackages); + newCodePath = true; + } + boolean newLoadingPackage = loadingPackage != null + && !loadingPackage.equals(owningPackageName) + && loadingPackages.add(loadingPackage); + return newCodePath || newLoadingPackage; } - public boolean isUsedByOtherApps() { - return mIsUsedByOtherApps; + public boolean isUsedByOtherApps(String codePath) { + return mCodePathsUsedByOtherApps.containsKey(codePath); } public Map<String, DexUseInfo> getDexUseInfoMap() { return mDexUseInfoMap; } + + public Set<String> getLoadingPackages(String codePath) { + return mCodePathsUsedByOtherApps.getOrDefault(codePath, null); + } + + public boolean isAnyCodePathUsedByOtherApps() { + return !mCodePathsUsedByOtherApps.isEmpty(); + } + + /** + * Clears the usedByOtherApps markers from all code paths. + * Returns whether or not there was an update. + */ + /*package*/ boolean clearCodePathUsedByOtherApps() { + // Update mUsedByOtherAppsBeforeUpgrade as well to be consistent with + // the new data. This is not saved to disk so we don't need to return it. + mUsedByOtherAppsBeforeUpgrade = true; + + if (mCodePathsUsedByOtherApps.isEmpty()) { + return false; + } else { + mCodePathsUsedByOtherApps.clear(); + return true; + } + } } /** @@ -575,33 +786,59 @@ public class PackageDexUsage extends AbstractStatsBase<Void> { public static class DexUseInfo { private boolean mIsUsedByOtherApps; private final int mOwnerUserId; + // The class loader context for the dex file. This encodes the class loader chain + // (class loader type + class path) in a format compatible to dex2oat. + // See {@code DexoptUtils.processContextForDexLoad}. + private String mClassLoaderContext; + // The instructions sets of the applications loading the dex file. private final Set<String> mLoaderIsas; + // Packages who load this dex file. + private final Set<String> mLoadingPackages; - public DexUseInfo(boolean isUsedByOtherApps, int ownerUserId) { - this(isUsedByOtherApps, ownerUserId, null); - } - - public DexUseInfo(boolean isUsedByOtherApps, int ownerUserId, String loaderIsa) { + public DexUseInfo(boolean isUsedByOtherApps, int ownerUserId, String classLoaderContext, + String loaderIsa) { mIsUsedByOtherApps = isUsedByOtherApps; mOwnerUserId = ownerUserId; + mClassLoaderContext = classLoaderContext; mLoaderIsas = new HashSet<>(); if (loaderIsa != null) { mLoaderIsas.add(loaderIsa); } + mLoadingPackages = new HashSet<>(); } // Creates a deep copy of the `other`. public DexUseInfo(DexUseInfo other) { mIsUsedByOtherApps = other.mIsUsedByOtherApps; mOwnerUserId = other.mOwnerUserId; + mClassLoaderContext = other.mClassLoaderContext; mLoaderIsas = new HashSet<>(other.mLoaderIsas); + mLoadingPackages = new HashSet<>(other.mLoadingPackages); } private boolean merge(DexUseInfo dexUseInfo) { boolean oldIsUsedByOtherApps = mIsUsedByOtherApps; mIsUsedByOtherApps = mIsUsedByOtherApps || dexUseInfo.mIsUsedByOtherApps; boolean updateIsas = mLoaderIsas.addAll(dexUseInfo.mLoaderIsas); - return updateIsas || (oldIsUsedByOtherApps != mIsUsedByOtherApps); + boolean updateLoadingPackages = mLoadingPackages.addAll(dexUseInfo.mLoadingPackages); + + String oldClassLoaderContext = mClassLoaderContext; + if (UNKNOWN_CLASS_LOADER_CONTEXT.equals(mClassLoaderContext)) { + // Can happen if we read a previous version. + mClassLoaderContext = dexUseInfo.mClassLoaderContext; + } else if (UNSUPPORTED_CLASS_LOADER_CONTEXT.equals(dexUseInfo.mClassLoaderContext)) { + // We detected an unsupported context. + mClassLoaderContext = UNSUPPORTED_CLASS_LOADER_CONTEXT; + } else if (!UNSUPPORTED_CLASS_LOADER_CONTEXT.equals(mClassLoaderContext) && + !Objects.equal(mClassLoaderContext, dexUseInfo.mClassLoaderContext)) { + // We detected a context change. + mClassLoaderContext = VARIABLE_CLASS_LOADER_CONTEXT; + } + + return updateIsas || + (oldIsUsedByOtherApps != mIsUsedByOtherApps) || + updateLoadingPackages + || !Objects.equal(oldClassLoaderContext, mClassLoaderContext); } public boolean isUsedByOtherApps() { @@ -615,5 +852,25 @@ public class PackageDexUsage extends AbstractStatsBase<Void> { public Set<String> getLoaderIsas() { return mLoaderIsas; } + + public Set<String> getLoadingPackages() { + return mLoadingPackages; + } + + public String getClassLoaderContext() { return mClassLoaderContext; } + + public boolean isUnsupportedClassLoaderContext() { + return UNSUPPORTED_CLASS_LOADER_CONTEXT.equals(mClassLoaderContext); + } + + public boolean isUnknownClassLoaderContext() { + // The class loader context may be unknown if we loaded the data from a previous version + // which didn't save the context. + return UNKNOWN_CLASS_LOADER_CONTEXT.equals(mClassLoaderContext); + } + + public boolean isVariableClassLoaderContext() { + return VARIABLE_CLASS_LOADER_CONTEXT.equals(mClassLoaderContext); + } } } diff --git a/services/tests/servicestests/src/com/android/server/pm/dex/DexManagerTests.java b/services/tests/servicestests/src/com/android/server/pm/dex/DexManagerTests.java index afc0f67fe993..4db9a30a11ca 100644 --- a/services/tests/servicestests/src/com/android/server/pm/dex/DexManagerTests.java +++ b/services/tests/servicestests/src/com/android/server/pm/dex/DexManagerTests.java @@ -23,10 +23,14 @@ import android.os.UserHandle; import android.support.test.filters.SmallTest; import android.support.test.runner.AndroidJUnit4; +import dalvik.system.DelegateLastClassLoader; +import dalvik.system.PathClassLoader; import dalvik.system.VMRuntime; +import java.io.File; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -48,6 +52,10 @@ import static com.android.server.pm.dex.PackageDexUsage.DexUseInfo; @RunWith(AndroidJUnit4.class) @SmallTest public class DexManagerTests { + private static final String PATH_CLASS_LOADER_NAME = PathClassLoader.class.getName(); + private static final String DELEGATE_LAST_CLASS_LOADER_NAME = + DelegateLastClassLoader.class.getName(); + private DexManager mDexManager; private TestData mFooUser0; @@ -56,6 +64,9 @@ public class DexManagerTests { private TestData mInvalidIsa; private TestData mDoesNotExist; + private TestData mBarUser0UnsupportedClassLoader; + private TestData mBarUser0DelegateLastClassLoader; + private int mUser0; private int mUser1; @@ -68,12 +79,17 @@ public class DexManagerTests { String foo = "foo"; String bar = "bar"; - mFooUser0 = new TestData(foo, isa, mUser0); - mBarUser0 = new TestData(bar, isa, mUser0); - mBarUser1 = new TestData(bar, isa, mUser1); + mFooUser0 = new TestData(foo, isa, mUser0, PATH_CLASS_LOADER_NAME); + mBarUser0 = new TestData(bar, isa, mUser0, PATH_CLASS_LOADER_NAME); + mBarUser1 = new TestData(bar, isa, mUser1, PATH_CLASS_LOADER_NAME); mInvalidIsa = new TestData("INVALID", "INVALID_ISA", mUser0); mDoesNotExist = new TestData("DOES.NOT.EXIST", isa, mUser1); + mBarUser0UnsupportedClassLoader = new TestData(bar, isa, mUser0, + "unsupported.class_loader"); + mBarUser0DelegateLastClassLoader = new TestData(bar, isa, mUser0, + DELEGATE_LAST_CLASS_LOADER_NAME); + mDexManager = new DexManager(null, null, null, null); // Foo and Bar are available to user0. @@ -90,7 +106,7 @@ public class DexManagerTests { notifyDexLoad(mFooUser0, mFooUser0.getBaseAndSplitDexPaths(), mUser0); // Package is not used by others, so we should get nothing back. - assertNull(getPackageUseInfo(mFooUser0)); + assertNoUseInfo(mFooUser0); } @Test @@ -100,8 +116,7 @@ public class DexManagerTests { // Bar is used by others now and should be in our records PackageUseInfo pui = getPackageUseInfo(mBarUser0); - assertNotNull(pui); - assertTrue(pui.isUsedByOtherApps()); + assertIsUsedByOtherApps(mBarUser0, pui, true); assertTrue(pui.getDexUseInfoMap().isEmpty()); } @@ -112,8 +127,7 @@ public class DexManagerTests { notifyDexLoad(mFooUser0, fooSecondaries, mUser0); PackageUseInfo pui = getPackageUseInfo(mFooUser0); - assertNotNull(pui); - assertFalse(pui.isUsedByOtherApps()); + assertIsUsedByOtherApps(mFooUser0, pui, false); assertEquals(fooSecondaries.size(), pui.getDexUseInfoMap().size()); assertSecondaryUse(mFooUser0, pui, fooSecondaries, /*isUsedByOtherApps*/false, mUser0); } @@ -125,8 +139,7 @@ public class DexManagerTests { notifyDexLoad(mFooUser0, barSecondaries, mUser0); PackageUseInfo pui = getPackageUseInfo(mBarUser0); - assertNotNull(pui); - assertFalse(pui.isUsedByOtherApps()); + assertIsUsedByOtherApps(mBarUser0, pui, false); assertEquals(barSecondaries.size(), pui.getDexUseInfoMap().size()); assertSecondaryUse(mFooUser0, pui, barSecondaries, /*isUsedByOtherApps*/true, mUser0); } @@ -149,8 +162,7 @@ public class DexManagerTests { // Check bar usage. Should be used by other app (for primary and barSecondaries). PackageUseInfo pui = getPackageUseInfo(mBarUser0); - assertNotNull(pui); - assertTrue(pui.isUsedByOtherApps()); + assertIsUsedByOtherApps(mBarUser0, pui, true); assertEquals(barSecondaries.size() + barSecondariesForOwnUse.size(), pui.getDexUseInfoMap().size()); @@ -160,8 +172,7 @@ public class DexManagerTests { // Check foo usage. Should not be used by other app. pui = getPackageUseInfo(mFooUser0); - assertNotNull(pui); - assertFalse(pui.isUsedByOtherApps()); + assertIsUsedByOtherApps(mFooUser0, pui, false); assertEquals(fooSecondaries.size(), pui.getDexUseInfoMap().size()); assertSecondaryUse(mFooUser0, pui, fooSecondaries, /*isUsedByOtherApps*/false, mUser0); } @@ -169,22 +180,22 @@ public class DexManagerTests { @Test public void testPackageUseInfoNotFound() { // Assert we don't get back data we did not previously record. - assertNull(getPackageUseInfo(mFooUser0)); + assertNoUseInfo(mFooUser0); } @Test public void testInvalidIsa() { // Notifying with an invalid ISA should be ignored. notifyDexLoad(mInvalidIsa, mInvalidIsa.getSecondaryDexPaths(), mUser0); - assertNull(getPackageUseInfo(mInvalidIsa)); + assertNoUseInfo(mInvalidIsa); } @Test - public void testNotExistingPackate() { + public void testNotExistingPackage() { // Notifying about the load of a package which was previously not // register in DexManager#load should be ignored. notifyDexLoad(mDoesNotExist, mDoesNotExist.getBaseAndSplitDexPaths(), mUser0); - assertNull(getPackageUseInfo(mDoesNotExist)); + assertNoUseInfo(mDoesNotExist); } @Test @@ -192,7 +203,7 @@ public class DexManagerTests { // Bar from User1 tries to load secondary dex files from User0 Bar. // Request should be ignored. notifyDexLoad(mBarUser1, mBarUser0.getSecondaryDexPaths(), mUser1); - assertNull(getPackageUseInfo(mBarUser1)); + assertNoUseInfo(mBarUser1); } @Test @@ -201,7 +212,7 @@ public class DexManagerTests { // Note that the PackageManagerService already filters this out but we // still check that nothing goes unexpected in DexManager. notifyDexLoad(mBarUser0, mFooUser0.getBaseAndSplitDexPaths(), mUser1); - assertNull(getPackageUseInfo(mBarUser1)); + assertNoUseInfo(mBarUser1); } @Test @@ -213,7 +224,7 @@ public class DexManagerTests { // Before we notify about the installation of the newPackage if mFoo // is trying to load something from it we should not find it. notifyDexLoad(mFooUser0, newSecondaries, mUser0); - assertNull(getPackageUseInfo(newPackage)); + assertNoUseInfo(newPackage); // Notify about newPackage install and let mFoo load its dexes. mDexManager.notifyPackageInstalled(newPackage.mPackageInfo, mUser0); @@ -221,8 +232,7 @@ public class DexManagerTests { // We should get back the right info. PackageUseInfo pui = getPackageUseInfo(newPackage); - assertNotNull(pui); - assertFalse(pui.isUsedByOtherApps()); + assertIsUsedByOtherApps(newPackage, pui, false); assertEquals(newSecondaries.size(), pui.getDexUseInfoMap().size()); assertSecondaryUse(newPackage, pui, newSecondaries, /*isUsedByOtherApps*/true, mUser0); } @@ -238,8 +248,7 @@ public class DexManagerTests { notifyDexLoad(newPackage, newSecondaries, mUser0); PackageUseInfo pui = getPackageUseInfo(newPackage); - assertNotNull(pui); - assertFalse(pui.isUsedByOtherApps()); + assertIsUsedByOtherApps(newPackage, pui, false); assertEquals(newSecondaries.size(), pui.getDexUseInfoMap().size()); assertSecondaryUse(newPackage, pui, newSecondaries, /*isUsedByOtherApps*/false, mUser0); } @@ -251,8 +260,7 @@ public class DexManagerTests { // Bar is used by others now and should be in our records. PackageUseInfo pui = getPackageUseInfo(mBarUser0); - assertNotNull(pui); - assertTrue(pui.isUsedByOtherApps()); + assertIsUsedByOtherApps(mBarUser0, pui, true); assertTrue(pui.getDexUseInfoMap().isEmpty()); // Notify that bar is updated. @@ -262,8 +270,7 @@ public class DexManagerTests { // The usedByOtherApps flag should be clear now. pui = getPackageUseInfo(mBarUser0); - assertNotNull(pui); - assertFalse(pui.isUsedByOtherApps()); + assertIsUsedByOtherApps(mBarUser0, pui, false); } @Test @@ -275,8 +282,7 @@ public class DexManagerTests { // We shouldn't find yet the new split as we didn't notify the package update. notifyDexLoad(mFooUser0, newSplits, mUser0); - PackageUseInfo pui = getPackageUseInfo(mBarUser0); - assertNull(pui); + assertNoUseInfo(mBarUser0); // Notify that bar is updated. splitSourceDirs will contain the updated path. mDexManager.notifyPackageUpdated(mBarUser0.getPackageName(), @@ -285,9 +291,9 @@ public class DexManagerTests { // Now, when the split is loaded we will find it and we should mark Bar as usedByOthers. notifyDexLoad(mFooUser0, newSplits, mUser0); - pui = getPackageUseInfo(mBarUser0); + PackageUseInfo pui = getPackageUseInfo(mBarUser0); assertNotNull(pui); - assertTrue(pui.isUsedByOtherApps()); + assertIsUsedByOtherApps(newSplits, pui, true); } @Test @@ -319,8 +325,7 @@ public class DexManagerTests { // Foo should still be around since it's used by other apps but with no // secondary dex info. PackageUseInfo pui = getPackageUseInfo(mFooUser0); - assertNotNull(pui); - assertTrue(pui.isUsedByOtherApps()); + assertIsUsedByOtherApps(mFooUser0, pui, true); assertTrue(pui.getDexUseInfoMap().isEmpty()); } @@ -334,8 +339,7 @@ public class DexManagerTests { // Foo should not be around since all its secondary dex info were deleted // and it is not used by other apps. - PackageUseInfo pui = getPackageUseInfo(mFooUser0); - assertNull(pui); + assertNoUseInfo(mFooUser0); } @Test @@ -347,8 +351,7 @@ public class DexManagerTests { mDexManager.notifyPackageDataDestroyed(mBarUser0.getPackageName(), UserHandle.USER_ALL); // Bar should not be around since it was removed for all users. - PackageUseInfo pui = getPackageUseInfo(mBarUser0); - assertNull(pui); + assertNoUseInfo(mBarUser0); } @Test @@ -357,7 +360,7 @@ public class DexManagerTests { // Load a dex file from framework. notifyDexLoad(mFooUser0, Arrays.asList(frameworkDex), mUser0); // The dex file should not be recognized as a package. - assertNull(mDexManager.getPackageUseInfo(frameworkDex)); + assertFalse(mDexManager.hasInfoOnPackage(frameworkDex)); } @Test @@ -367,14 +370,83 @@ public class DexManagerTests { notifyDexLoad(mFooUser0, fooSecondaries, mUser0); PackageUseInfo pui = getPackageUseInfo(mFooUser0); - assertNotNull(pui); - assertFalse(pui.isUsedByOtherApps()); + assertIsUsedByOtherApps(mFooUser0, pui, false); assertEquals(fooSecondaries.size(), pui.getDexUseInfoMap().size()); assertSecondaryUse(mFooUser0, pui, fooSecondaries, /*isUsedByOtherApps*/false, mUser0); } + @Test + public void testNotifyUnsupportedClassLoader() { + List<String> secondaries = mBarUser0UnsupportedClassLoader.getSecondaryDexPaths(); + notifyDexLoad(mBarUser0UnsupportedClassLoader, secondaries, mUser0); + + PackageUseInfo pui = getPackageUseInfo(mBarUser0UnsupportedClassLoader); + assertIsUsedByOtherApps(mBarUser0UnsupportedClassLoader, pui, false); + assertEquals(secondaries.size(), pui.getDexUseInfoMap().size()); + // We expect that all the contexts are unsupported. + String[] expectedContexts = + Collections.nCopies(secondaries.size(), + PackageDexUsage.UNSUPPORTED_CLASS_LOADER_CONTEXT).toArray(new String[0]); + assertSecondaryUse(mBarUser0UnsupportedClassLoader, pui, secondaries, + /*isUsedByOtherApps*/false, mUser0, expectedContexts); + } + + @Test + public void testNotifyVariableClassLoader() { + // Record bar secondaries with the default PathClassLoader. + List<String> secondaries = mBarUser0.getSecondaryDexPaths(); + + notifyDexLoad(mBarUser0, secondaries, mUser0); + PackageUseInfo pui = getPackageUseInfo(mBarUser0); + assertIsUsedByOtherApps(mBarUser0, pui, false); + assertEquals(secondaries.size(), pui.getDexUseInfoMap().size()); + assertSecondaryUse(mFooUser0, pui, secondaries, /*isUsedByOtherApps*/false, mUser0); + + // Record bar secondaries again with a different class loader. This will change the context. + notifyDexLoad(mBarUser0DelegateLastClassLoader, secondaries, mUser0); + + pui = getPackageUseInfo(mBarUser0); + assertIsUsedByOtherApps(mBarUser0, pui, false); + assertEquals(secondaries.size(), pui.getDexUseInfoMap().size()); + // We expect that all the contexts to be changed to variable now. + String[] expectedContexts = + Collections.nCopies(secondaries.size(), + PackageDexUsage.VARIABLE_CLASS_LOADER_CONTEXT).toArray(new String[0]); + assertSecondaryUse(mFooUser0, pui, secondaries, /*isUsedByOtherApps*/false, mUser0, + expectedContexts); + } + + @Test + public void testNotifyUnsupportedClassLoaderDoesNotChange() { + List<String> secondaries = mBarUser0UnsupportedClassLoader.getSecondaryDexPaths(); + notifyDexLoad(mBarUser0UnsupportedClassLoader, secondaries, mUser0); + + PackageUseInfo pui = getPackageUseInfo(mBarUser0UnsupportedClassLoader); + assertIsUsedByOtherApps(mBarUser0UnsupportedClassLoader, pui, false); + assertEquals(secondaries.size(), pui.getDexUseInfoMap().size()); + // We expect that all the contexts are unsupported. + String[] expectedContexts = + Collections.nCopies(secondaries.size(), + PackageDexUsage.UNSUPPORTED_CLASS_LOADER_CONTEXT).toArray(new String[0]); + assertSecondaryUse(mBarUser0UnsupportedClassLoader, pui, secondaries, + /*isUsedByOtherApps*/false, mUser0, expectedContexts); + + // Record bar secondaries again with a different class loader. This will change the context. + // However, because the context was already marked as unsupported we should not chage it. + notifyDexLoad(mBarUser0DelegateLastClassLoader, secondaries, mUser0); + pui = getPackageUseInfo(mBarUser0UnsupportedClassLoader); + assertSecondaryUse(mBarUser0UnsupportedClassLoader, pui, secondaries, + /*isUsedByOtherApps*/false, mUser0, expectedContexts); + + } + + private void assertSecondaryUse(TestData testData, PackageUseInfo pui, - List<String> secondaries, boolean isUsedByOtherApps, int ownerUserId) { + List<String> secondaries, boolean isUsedByOtherApps, int ownerUserId, + String[] expectedContexts) { + assertNotNull(expectedContexts); + assertEquals(expectedContexts.length, secondaries.size()); + int index = 0; for (String dex : secondaries) { DexUseInfo dui = pui.getDexUseInfoMap().get(dex); assertNotNull(dui); @@ -382,16 +454,50 @@ public class DexManagerTests { assertEquals(ownerUserId, dui.getOwnerUserId()); assertEquals(1, dui.getLoaderIsas().size()); assertTrue(dui.getLoaderIsas().contains(testData.mLoaderIsa)); + assertEquals(expectedContexts[index++], dui.getClassLoaderContext()); } } + private void assertSecondaryUse(TestData testData, PackageUseInfo pui, + List<String> secondaries, boolean isUsedByOtherApps, int ownerUserId) { + String[] expectedContexts = DexoptUtils.processContextForDexLoad( + Arrays.asList(testData.mClassLoader), + Arrays.asList(String.join(File.pathSeparator, secondaries))); + assertSecondaryUse(testData, pui, secondaries, isUsedByOtherApps, ownerUserId, + expectedContexts); + } + + private void assertIsUsedByOtherApps(TestData testData, PackageUseInfo pui, + boolean isUsedByOtherApps) { + assertIsUsedByOtherApps(testData.getBaseAndSplitDexPaths(), pui, isUsedByOtherApps); + } + private void assertIsUsedByOtherApps(List<String> codePaths, PackageUseInfo pui, + boolean isUsedByOtherApps) { + for (String codePath : codePaths) { + assertEquals(codePath, isUsedByOtherApps, pui.isUsedByOtherApps(codePath)); + } + } private void notifyDexLoad(TestData testData, List<String> dexPaths, int loaderUserId) { - mDexManager.notifyDexLoad(testData.mPackageInfo.applicationInfo, dexPaths, + // By default, assume a single class loader in the chain. + // This makes writing tests much easier. + List<String> classLoaders = Arrays.asList(testData.mClassLoader); + List<String> classPaths = Arrays.asList(String.join(File.pathSeparator, dexPaths)); + notifyDexLoad(testData, classLoaders, classPaths, loaderUserId); + } + + private void notifyDexLoad(TestData testData, List<String> classLoader, List<String> classPaths, + int loaderUserId) { + mDexManager.notifyDexLoad(testData.mPackageInfo.applicationInfo, classLoader, classPaths, testData.mLoaderIsa, loaderUserId); } private PackageUseInfo getPackageUseInfo(TestData testData) { - return mDexManager.getPackageUseInfo(testData.mPackageInfo.packageName); + assertTrue(mDexManager.hasInfoOnPackage(testData.mPackageInfo.packageName)); + return mDexManager.getPackageUseInfoOrDefault(testData.mPackageInfo.packageName); + } + + private void assertNoUseInfo(TestData testData) { + assertFalse(mDexManager.hasInfoOnPackage(testData.mPackageInfo.packageName)); } private static PackageInfo getMockPackageInfo(String packageName, int userId) { @@ -416,10 +522,16 @@ public class DexManagerTests { private static class TestData { private final PackageInfo mPackageInfo; private final String mLoaderIsa; + private final String mClassLoader; - private TestData(String packageName, String loaderIsa, int userId) { + private TestData(String packageName, String loaderIsa, int userId, String classLoader) { mPackageInfo = getMockPackageInfo(packageName, userId); mLoaderIsa = loaderIsa; + mClassLoader = classLoader; + } + + private TestData(String packageName, String loaderIsa, int userId) { + this(packageName, loaderIsa, userId, PATH_CLASS_LOADER_NAME); } private String getPackageName() { diff --git a/services/tests/servicestests/src/com/android/server/pm/dex/DexoptOptionsTests.java b/services/tests/servicestests/src/com/android/server/pm/dex/DexoptOptionsTests.java new file mode 100644 index 000000000000..c2072df75bd8 --- /dev/null +++ b/services/tests/servicestests/src/com/android/server/pm/dex/DexoptOptionsTests.java @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.server.pm.dex; + + +import static com.android.server.pm.PackageManagerServiceCompilerMapping.getCompilerFilterForReason; + +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import com.android.server.pm.PackageManagerService; +import com.android.server.pm.PackageManagerServiceCompilerMapping; + +@RunWith(AndroidJUnit4.class) +@SmallTest +public class DexoptOptionsTests { + private final static String mPackageName = "test.android.com"; + private final static String mCompilerFilter = + PackageManagerServiceCompilerMapping.getDefaultCompilerFilter(); + private final static String mSplitName = "split-A.apk"; + + @Test + public void testCreateDexoptOptionsEmpty() { + DexoptOptions opt = new DexoptOptions(mPackageName, mCompilerFilter, /*flags*/ 0); + assertEquals(mPackageName, opt.getPackageName()); + assertEquals(mCompilerFilter, opt.getCompilerFilter()); + assertEquals(null, opt.getSplitName()); + assertFalse(opt.isBootComplete()); + assertFalse(opt.isCheckForProfileUpdates()); + assertFalse(opt.isDexoptOnlySecondaryDex()); + assertFalse(opt.isDexoptOnlySharedDex()); + assertFalse(opt.isDowngrade()); + assertFalse(opt.isForce()); + assertFalse(opt.isDexoptIdleBackgroundJob()); + } + + @Test + public void testCreateDexoptOptionsFull() { + int flags = + DexoptOptions.DEXOPT_FORCE | + DexoptOptions.DEXOPT_BOOT_COMPLETE | + DexoptOptions.DEXOPT_CHECK_FOR_PROFILES_UPDATES | + DexoptOptions.DEXOPT_ONLY_SECONDARY_DEX | + DexoptOptions.DEXOPT_ONLY_SHARED_DEX | + DexoptOptions.DEXOPT_DOWNGRADE | + DexoptOptions.DEXOPT_AS_SHARED_LIBRARY | + DexoptOptions.DEXOPT_IDLE_BACKGROUND_JOB; + + DexoptOptions opt = new DexoptOptions(mPackageName, mCompilerFilter, flags); + assertEquals(mPackageName, opt.getPackageName()); + assertEquals(mCompilerFilter, opt.getCompilerFilter()); + assertEquals(null, opt.getSplitName()); + assertTrue(opt.isBootComplete()); + assertTrue(opt.isCheckForProfileUpdates()); + assertTrue(opt.isDexoptOnlySecondaryDex()); + assertTrue(opt.isDexoptOnlySharedDex()); + assertTrue(opt.isDowngrade()); + assertTrue(opt.isForce()); + assertTrue(opt.isDexoptAsSharedLibrary()); + assertTrue(opt.isDexoptIdleBackgroundJob()); + } + + @Test + public void testCreateDexoptOptionsReason() { + int flags = + DexoptOptions.DEXOPT_FORCE | + DexoptOptions.DEXOPT_BOOT_COMPLETE | + DexoptOptions.DEXOPT_CHECK_FOR_PROFILES_UPDATES; + + int[] reasons = new int[] { + PackageManagerService.REASON_FIRST_BOOT, + PackageManagerService.REASON_BOOT, + PackageManagerService.REASON_INSTALL, + PackageManagerService.REASON_BACKGROUND_DEXOPT, + PackageManagerService.REASON_AB_OTA, + PackageManagerService.REASON_INACTIVE_PACKAGE_DOWNGRADE,}; + + for (int reason : reasons) { + DexoptOptions opt = new DexoptOptions(mPackageName, reason, flags); + assertEquals(mPackageName, opt.getPackageName()); + assertEquals(getCompilerFilterForReason(reason), opt.getCompilerFilter()); + assertEquals(null, opt.getSplitName()); + assertTrue(opt.isBootComplete()); + assertTrue(opt.isCheckForProfileUpdates()); + assertFalse(opt.isDexoptOnlySecondaryDex()); + assertFalse(opt.isDexoptOnlySharedDex()); + assertFalse(opt.isDowngrade()); + assertTrue(opt.isForce()); + assertFalse(opt.isDexoptAsSharedLibrary()); + } + } + + @Test + public void testCreateDexoptOptionsSplit() { + int flags = DexoptOptions.DEXOPT_FORCE | DexoptOptions.DEXOPT_BOOT_COMPLETE; + + DexoptOptions opt = new DexoptOptions(mPackageName, mCompilerFilter, mSplitName, flags); + assertEquals(mPackageName, opt.getPackageName()); + assertEquals(mCompilerFilter, opt.getCompilerFilter()); + assertEquals(mSplitName, opt.getSplitName()); + assertTrue(opt.isBootComplete()); + assertFalse(opt.isCheckForProfileUpdates()); + assertFalse(opt.isDexoptOnlySecondaryDex()); + assertFalse(opt.isDexoptOnlySharedDex()); + assertFalse(opt.isDowngrade()); + assertTrue(opt.isForce()); + assertFalse(opt.isDexoptAsSharedLibrary()); + } + + @Test + public void testCreateDexoptInvalid() { + boolean gotException = false; + try { + int invalidFlags = 999; + new DexoptOptions(mPackageName, mCompilerFilter, invalidFlags); + } catch (IllegalArgumentException ignore) { + gotException = true; + } + + assertTrue(gotException); + } +} diff --git a/services/tests/servicestests/src/com/android/server/pm/dex/DexoptUtilsTest.java b/services/tests/servicestests/src/com/android/server/pm/dex/DexoptUtilsTest.java new file mode 100644 index 000000000000..ff7bd723d23d --- /dev/null +++ b/services/tests/servicestests/src/com/android/server/pm/dex/DexoptUtilsTest.java @@ -0,0 +1,279 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.server.pm.dex; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import android.content.pm.ApplicationInfo; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; +import android.util.SparseArray; + +import dalvik.system.DelegateLastClassLoader; +import dalvik.system.DexClassLoader; +import dalvik.system.PathClassLoader; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.io.File; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +@RunWith(AndroidJUnit4.class) +@SmallTest +public class DexoptUtilsTest { + private static final String DEX_CLASS_LOADER_NAME = DexClassLoader.class.getName(); + private static final String PATH_CLASS_LOADER_NAME = PathClassLoader.class.getName(); + private static final String DELEGATE_LAST_CLASS_LOADER_NAME = + DelegateLastClassLoader.class.getName(); + + private ApplicationInfo createMockApplicationInfo(String baseClassLoader, boolean addSplits, + boolean addSplitDependencies) { + ApplicationInfo ai = new ApplicationInfo(); + String codeDir = "/data/app/mock.android.com"; + ai.setBaseCodePath(codeDir + "/base.dex"); + ai.privateFlags = ai.privateFlags | ApplicationInfo.PRIVATE_FLAG_ISOLATED_SPLIT_LOADING; + + if (addSplits) { + ai.setSplitCodePaths(new String[]{ + codeDir + "/base-1.dex", + codeDir + "/base-2.dex", + codeDir + "/base-3.dex", + codeDir + "/base-4.dex", + codeDir + "/base-5.dex", + codeDir + "/base-6.dex"}); + + if (addSplitDependencies) { + ai.splitDependencies = new SparseArray<>(6 + 1); + ai.splitDependencies.put(0, new int[] {-1}); // base has no dependency + ai.splitDependencies.put(1, new int[] {2}); // split 1 depends on 2 + ai.splitDependencies.put(2, new int[] {4}); // split 2 depends on 4 + ai.splitDependencies.put(3, new int[] {4}); // split 3 depends on 4 + ai.splitDependencies.put(4, new int[] {0}); // split 4 depends on base + ai.splitDependencies.put(5, new int[] {0}); // split 5 depends on base + ai.splitDependencies.put(6, new int[] {5}); // split 6 depends on 5 + } + } + return ai; + } + + @Test + public void testSplitChain() { + ApplicationInfo ai = createMockApplicationInfo(PATH_CLASS_LOADER_NAME, true, true); + String[] sharedLibrary = new String[] {"a.dex", "b.dex"}; + String[] contexts = DexoptUtils.getClassLoaderContexts(ai, sharedLibrary); + + assertEquals(7, contexts.length); + assertEquals("PCL[a.dex:b.dex]", contexts[0]); + assertEquals("PCL[];PCL[base-2.dex];PCL[base-4.dex];PCL[a.dex:b.dex:base.dex]", + contexts[1]); + assertEquals("PCL[];PCL[base-4.dex];PCL[a.dex:b.dex:base.dex]", contexts[2]); + assertEquals("PCL[];PCL[base-4.dex];PCL[a.dex:b.dex:base.dex]", contexts[3]); + assertEquals("PCL[];PCL[a.dex:b.dex:base.dex]", contexts[4]); + assertEquals("PCL[];PCL[a.dex:b.dex:base.dex]", contexts[5]); + assertEquals("PCL[];PCL[base-5.dex];PCL[a.dex:b.dex:base.dex]", contexts[6]); + } + + @Test + public void testSplitChainNoSplitDependencies() { + ApplicationInfo ai = createMockApplicationInfo(PATH_CLASS_LOADER_NAME, true, false); + String[] sharedLibrary = new String[] {"a.dex", "b.dex"}; + String[] contexts = DexoptUtils.getClassLoaderContexts(ai, sharedLibrary); + + assertEquals(7, contexts.length); + assertEquals("PCL[a.dex:b.dex]", contexts[0]); + assertEquals("PCL[a.dex:b.dex:base.dex]", contexts[1]); + assertEquals("PCL[a.dex:b.dex:base.dex:base-1.dex]", contexts[2]); + assertEquals("PCL[a.dex:b.dex:base.dex:base-1.dex:base-2.dex]", contexts[3]); + assertEquals("PCL[a.dex:b.dex:base.dex:base-1.dex:base-2.dex:base-3.dex]", contexts[4]); + assertEquals( + "PCL[a.dex:b.dex:base.dex:base-1.dex:base-2.dex:base-3.dex:base-4.dex]", + contexts[5]); + assertEquals( + "PCL[a.dex:b.dex:base.dex:base-1.dex:base-2.dex:base-3.dex:base-4.dex:base-5.dex]", + contexts[6]); + } + + @Test + public void testSplitChainNoIsolationNoSharedLibrary() { + ApplicationInfo ai = createMockApplicationInfo(PATH_CLASS_LOADER_NAME, true, true); + ai.privateFlags = ai.privateFlags & (~ApplicationInfo.PRIVATE_FLAG_ISOLATED_SPLIT_LOADING); + String[] contexts = DexoptUtils.getClassLoaderContexts(ai, null); + + assertEquals(7, contexts.length); + assertEquals("PCL[]", contexts[0]); + assertEquals("PCL[base.dex]", contexts[1]); + assertEquals("PCL[base.dex:base-1.dex]", contexts[2]); + assertEquals("PCL[base.dex:base-1.dex:base-2.dex]", contexts[3]); + assertEquals("PCL[base.dex:base-1.dex:base-2.dex:base-3.dex]", contexts[4]); + assertEquals("PCL[base.dex:base-1.dex:base-2.dex:base-3.dex:base-4.dex]", contexts[5]); + assertEquals( + "PCL[base.dex:base-1.dex:base-2.dex:base-3.dex:base-4.dex:base-5.dex]", + contexts[6]); + } + @Test + public void testSplitChainNoSharedLibraries() { + ApplicationInfo ai = createMockApplicationInfo( + DELEGATE_LAST_CLASS_LOADER_NAME, true, true); + String[] contexts = DexoptUtils.getClassLoaderContexts(ai, null); + + assertEquals(7, contexts.length); + assertEquals("PCL[]", contexts[0]); + assertEquals("PCL[];PCL[base-2.dex];PCL[base-4.dex];PCL[base.dex]", contexts[1]); + assertEquals("PCL[];PCL[base-4.dex];PCL[base.dex]", contexts[2]); + assertEquals("PCL[];PCL[base-4.dex];PCL[base.dex]", contexts[3]); + assertEquals("PCL[];PCL[base.dex]", contexts[4]); + assertEquals("PCL[];PCL[base.dex]", contexts[5]); + assertEquals("PCL[];PCL[base-5.dex];PCL[base.dex]", contexts[6]); + } + + @Test + public void testSplitChainWithNullPrimaryClassLoader() { + // A null classLoaderName should mean PathClassLoader. + ApplicationInfo ai = createMockApplicationInfo(null, true, true); + String[] sharedLibrary = new String[] {"a.dex", "b.dex"}; + String[] contexts = DexoptUtils.getClassLoaderContexts(ai, sharedLibrary); + + assertEquals(7, contexts.length); + assertEquals("PCL[a.dex:b.dex]", contexts[0]); + assertEquals("PCL[];PCL[base-2.dex];PCL[base-4.dex];PCL[a.dex:b.dex:base.dex]", contexts[1]); + assertEquals("PCL[];PCL[base-4.dex];PCL[a.dex:b.dex:base.dex]", contexts[2]); + assertEquals("PCL[];PCL[base-4.dex];PCL[a.dex:b.dex:base.dex]", contexts[3]); + assertEquals("PCL[];PCL[a.dex:b.dex:base.dex]", contexts[4]); + assertEquals("PCL[];PCL[a.dex:b.dex:base.dex]", contexts[5]); + assertEquals("PCL[];PCL[base-5.dex];PCL[a.dex:b.dex:base.dex]", contexts[6]); + } + + @Test + public void tesNoSplits() { + ApplicationInfo ai = createMockApplicationInfo(PATH_CLASS_LOADER_NAME, false, false); + String[] sharedLibrary = new String[] {"a.dex", "b.dex"}; + String[] contexts = DexoptUtils.getClassLoaderContexts(ai, sharedLibrary); + + assertEquals(1, contexts.length); + assertEquals("PCL[a.dex:b.dex]", contexts[0]); + } + + @Test + public void tesNoSplitsNullClassLoaderName() { + ApplicationInfo ai = createMockApplicationInfo(null, false, false); + String[] sharedLibrary = new String[] {"a.dex", "b.dex"}; + String[] contexts = DexoptUtils.getClassLoaderContexts(ai, sharedLibrary); + + assertEquals(1, contexts.length); + assertEquals("PCL[a.dex:b.dex]", contexts[0]); + } + + @Test + public void tesNoSplitDelegateLast() { + ApplicationInfo ai = createMockApplicationInfo( + DELEGATE_LAST_CLASS_LOADER_NAME, false, false); + String[] sharedLibrary = new String[] {"a.dex", "b.dex"}; + String[] contexts = DexoptUtils.getClassLoaderContexts(ai, sharedLibrary); + + assertEquals(1, contexts.length); + assertEquals("PCL[a.dex:b.dex]", contexts[0]); + } + + @Test + public void tesNoSplitsNoSharedLibraries() { + ApplicationInfo ai = createMockApplicationInfo(PATH_CLASS_LOADER_NAME, false, false); + String[] contexts = DexoptUtils.getClassLoaderContexts(ai, null); + + assertEquals(1, contexts.length); + assertEquals("PCL[]", contexts[0]); + } + + @Test + public void tesNoSplitDelegateLastNoSharedLibraries() { + ApplicationInfo ai = createMockApplicationInfo( + DELEGATE_LAST_CLASS_LOADER_NAME, false, false); + String[] contexts = DexoptUtils.getClassLoaderContexts(ai, null); + + assertEquals(1, contexts.length); + assertEquals("PCL[]", contexts[0]); + } + + @Test + public void testProcessContextForDexLoad() { + List<String> classLoaders = Arrays.asList( + DELEGATE_LAST_CLASS_LOADER_NAME, + PATH_CLASS_LOADER_NAME, + PATH_CLASS_LOADER_NAME); + List<String> classPaths = Arrays.asList( + String.join(File.pathSeparator, "foo.dex", "bar.dex"), + String.join(File.pathSeparator, "parent1.dex"), + String.join(File.pathSeparator, "parent2.dex", "parent3.dex")); + String[] context = DexoptUtils.processContextForDexLoad(classLoaders, classPaths); + assertNotNull(context); + assertEquals(2, context.length); + assertEquals("DLC[];PCL[parent1.dex];PCL[parent2.dex:parent3.dex]", context[0]); + assertEquals("DLC[foo.dex];PCL[parent1.dex];PCL[parent2.dex:parent3.dex]", context[1]); + } + + @Test + public void testProcessContextForDexLoadSingleElement() { + List<String> classLoaders = Arrays.asList(PATH_CLASS_LOADER_NAME); + List<String> classPaths = Arrays.asList( + String.join(File.pathSeparator, "foo.dex", "bar.dex", "zoo.dex")); + String[] context = DexoptUtils.processContextForDexLoad(classLoaders, classPaths); + assertNotNull(context); + assertEquals(3, context.length); + assertEquals("PCL[]", context[0]); + assertEquals("PCL[foo.dex]", context[1]); + assertEquals("PCL[foo.dex:bar.dex]", context[2]); + } + + @Test + public void testProcessContextForDexLoadUnsupported() { + List<String> classLoaders = Arrays.asList( + DELEGATE_LAST_CLASS_LOADER_NAME, + "unsupported.class.loader"); + List<String> classPaths = Arrays.asList( + String.join(File.pathSeparator, "foo.dex", "bar.dex"), + String.join(File.pathSeparator, "parent1.dex")); + String[] context = DexoptUtils.processContextForDexLoad(classLoaders, classPaths); + assertNull(context); + } + + @Test + public void testProcessContextForDexLoadIllegalCallEmptyList() { + boolean gotException = false; + try { + DexoptUtils.processContextForDexLoad(Collections.emptyList(), Collections.emptyList()); + } catch (IllegalArgumentException ignore) { + gotException = true; + } + assertTrue(gotException); + } + + @Test + public void testProcessContextForDexLoadIllegalCallDifferentSize() { + boolean gotException = false; + try { + DexoptUtils.processContextForDexLoad(Collections.emptyList(), Arrays.asList("a")); + } catch (IllegalArgumentException ignore) { + gotException = true; + } + assertTrue(gotException); + } +} diff --git a/services/tests/servicestests/src/com/android/server/pm/dex/PackageDexUsageTests.java b/services/tests/servicestests/src/com/android/server/pm/dex/PackageDexUsageTests.java index 2e99433149ea..69a148db8b63 100644 --- a/services/tests/servicestests/src/com/android/server/pm/dex/PackageDexUsageTests.java +++ b/services/tests/servicestests/src/com/android/server/pm/dex/PackageDexUsageTests.java @@ -21,6 +21,7 @@ import android.support.test.filters.SmallTest; import android.support.test.runner.AndroidJUnit4; import dalvik.system.VMRuntime; +import java.util.Collections; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -72,25 +73,25 @@ public class PackageDexUsageTests { String isa = VMRuntime.getInstructionSet(Build.SUPPORTED_ABIS[0]); mFooBaseUser0 = new TestData(fooPackageName, - fooCodeDir + "base.apk", 0, isa, false, true); + fooCodeDir + "base.apk", 0, isa, false, true, fooPackageName); mFooSplit1User0 = new TestData(fooPackageName, - fooCodeDir + "split-1.apk", 0, isa, false, true); + fooCodeDir + "split-1.apk", 0, isa, false, true, fooPackageName); mFooSplit2UsedByOtherApps0 = new TestData(fooPackageName, - fooCodeDir + "split-2.apk", 0, isa, true, true); + fooCodeDir + "split-2.apk", 0, isa, true, true, "used.by.other.com"); mFooSecondary1User0 = new TestData(fooPackageName, - fooDataDir + "sec-1.dex", 0, isa, false, false); + fooDataDir + "sec-1.dex", 0, isa, false, false, fooPackageName); mFooSecondary1User1 = new TestData(fooPackageName, - fooDataDir + "sec-1.dex", 1, isa, false, false); + fooDataDir + "sec-1.dex", 1, isa, false, false, fooPackageName); mFooSecondary2UsedByOtherApps0 = new TestData(fooPackageName, - fooDataDir + "sec-2.dex", 0, isa, true, false); + fooDataDir + "sec-2.dex", 0, isa, true, false, "used.by.other.com"); mInvalidIsa = new TestData(fooPackageName, - fooCodeDir + "base.apk", 0, "INVALID_ISA", false, true); + fooCodeDir + "base.apk", 0, "INVALID_ISA", false, true, "INALID_USER"); String barPackageName = "com.google.bar"; String barCodeDir = "/data/app/com.google.bar/"; @@ -98,11 +99,11 @@ public class PackageDexUsageTests { String barDataDir1 = "/data/user/1/com.google.bar/"; mBarBaseUser0 = new TestData(barPackageName, - barCodeDir + "base.apk", 0, isa, false, true); + barCodeDir + "base.apk", 0, isa, false, true, barPackageName); mBarSecondary1User0 = new TestData(barPackageName, - barDataDir + "sec-1.dex", 0, isa, false, false); + barDataDir + "sec-1.dex", 0, isa, false, false, barPackageName); mBarSecondary2User1 = new TestData(barPackageName, - barDataDir1 + "sec-2.dex", 1, isa, false, false); + barDataDir1 + "sec-2.dex", 1, isa, false, false, barPackageName); } @Test @@ -249,7 +250,10 @@ public class PackageDexUsageTests { Map<String, Set<Integer>> packageToUsersMap = new HashMap<>(); packageToUsersMap.put(mBarSecondary2User1.mPackageName, new HashSet<>(Arrays.asList(mBarSecondary2User1.mOwnerUserId))); - mPackageDexUsage.syncData(packageToUsersMap); + Map<String, Set<String>> packageToCodePaths = new HashMap<>(); + packageToCodePaths.put(mBarBaseUser0.mPackageName, + new HashSet<>(Arrays.asList(mBarBaseUser0.mDexFile))); + mPackageDexUsage.syncData(packageToUsersMap, packageToCodePaths); // Assert that only user 1 files are there. assertPackageDexUsage(mBarBaseUser0, mBarSecondary2User1); @@ -319,7 +323,8 @@ public class PackageDexUsageTests { mFooSplit2UsedByOtherApps0.mOwnerUserId, mFooSplit2UsedByOtherApps0.mLoaderIsa, /*mIsUsedByOtherApps*/false, - mFooSplit2UsedByOtherApps0.mPrimaryOrSplit); + mFooSplit2UsedByOtherApps0.mPrimaryOrSplit, + mFooSplit2UsedByOtherApps0.mUsedBy); assertPackageDexUsage(noLongerUsedByOtherApps); } @@ -332,14 +337,223 @@ public class PackageDexUsageTests { assertFalse(mPackageDexUsage.clearUsedByOtherApps(mFooSplit2UsedByOtherApps0.mPackageName)); } + @Test + public void testRecordDexFileUsers() { + PackageDexUsage packageDexUsageRecordUsers = new PackageDexUsage(); + Set<String> users = new HashSet<>(Arrays.asList( + new String[] {"another.package.1"})); + Set<String> usersExtra = new HashSet<>(Arrays.asList( + new String[] {"another.package.2", "another.package.3"})); + + assertTrue(record(packageDexUsageRecordUsers, mFooSplit2UsedByOtherApps0, users)); + assertTrue(record(packageDexUsageRecordUsers, mFooSplit2UsedByOtherApps0, usersExtra)); + + assertTrue(record(packageDexUsageRecordUsers, mFooSecondary1User0, users)); + assertTrue(record(packageDexUsageRecordUsers, mFooSecondary1User0, usersExtra)); + + packageDexUsageRecordUsers = writeAndReadBack(packageDexUsageRecordUsers); + // Verify that the users were recorded. + Set<String> userAll = new HashSet<>(users); + userAll.addAll(usersExtra); + assertPackageDexUsage(packageDexUsageRecordUsers, userAll, mFooSplit2UsedByOtherApps0, + mFooSecondary1User0); + } + + @Test + public void testRecordDexFileUsersNotTheOwningPackage() { + PackageDexUsage packageDexUsageRecordUsers = new PackageDexUsage(); + Set<String> users = new HashSet<>(Arrays.asList( + new String[] {mFooSplit2UsedByOtherApps0.mPackageName})); + Set<String> usersExtra = new HashSet<>(Arrays.asList( + new String[] {"another.package.2", "another.package.3"})); + + assertTrue(record(packageDexUsageRecordUsers, mFooSplit2UsedByOtherApps0, users)); + assertTrue(record(packageDexUsageRecordUsers, mFooSplit2UsedByOtherApps0, usersExtra)); + + assertTrue(record(packageDexUsageRecordUsers, mFooSecondary1User0, users)); + assertTrue(record(packageDexUsageRecordUsers, mFooSecondary1User0, usersExtra)); + + packageDexUsageRecordUsers = writeAndReadBack(packageDexUsageRecordUsers); + // Verify that only the non owning packages were recorded. + assertPackageDexUsage(packageDexUsageRecordUsers, usersExtra, mFooSplit2UsedByOtherApps0, + mFooSecondary1User0); + } + + @Test + public void testRecordClassLoaderContextVariableContext() { + // Record a secondary dex file. + assertTrue(record(mFooSecondary1User0)); + // Now update its context. + TestData fooSecondary1User0NewContext = mFooSecondary1User0.updateClassLoaderContext( + "PCL[new_context.dex]"); + assertTrue(record(fooSecondary1User0NewContext)); + + // Not check that the context was switch to variable. + TestData expectedContext = mFooSecondary1User0.updateClassLoaderContext( + PackageDexUsage.VARIABLE_CLASS_LOADER_CONTEXT); + + assertPackageDexUsage(null, expectedContext); + writeAndReadBack(); + assertPackageDexUsage(null, expectedContext); + } + + @Test + public void testRecordClassLoaderContextUnsupportedContext() { + // Record a secondary dex file. + assertTrue(record(mFooSecondary1User0)); + // Now update its context. + TestData unsupportedContext = mFooSecondary1User0.updateClassLoaderContext( + PackageDexUsage.UNSUPPORTED_CLASS_LOADER_CONTEXT); + assertTrue(record(unsupportedContext)); + + assertPackageDexUsage(null, unsupportedContext); + writeAndReadBack(); + assertPackageDexUsage(null, unsupportedContext); + } + + @Test + public void testRecordClassLoaderContextTransitionFromUnknown() { + // Record a secondary dex file. + TestData unknownContext = mFooSecondary1User0.updateClassLoaderContext( + PackageDexUsage.UNKNOWN_CLASS_LOADER_CONTEXT); + assertTrue(record(unknownContext)); + + assertPackageDexUsage(null, unknownContext); + writeAndReadBack(); + assertPackageDexUsage(null, unknownContext); + + // Now update the secondary dex record with a class loader context. This simulates the + // version 2 to version 3 upgrade. + + assertTrue(record(mFooSecondary1User0)); + + assertPackageDexUsage(null, mFooSecondary1User0); + writeAndReadBack(); + assertPackageDexUsage(null, mFooSecondary1User0); + } + + @Test + public void testDexUsageClassLoaderContext() { + final boolean isUsedByOtherApps = false; + final int userId = 0; + PackageDexUsage.DexUseInfo validContext = new DexUseInfo(isUsedByOtherApps, userId, + "valid_context", "arm"); + assertFalse(validContext.isUnknownClassLoaderContext()); + assertFalse(validContext.isUnsupportedClassLoaderContext()); + assertFalse(validContext.isVariableClassLoaderContext()); + + PackageDexUsage.DexUseInfo unsupportedContext = new DexUseInfo(isUsedByOtherApps, userId, + PackageDexUsage.UNSUPPORTED_CLASS_LOADER_CONTEXT, "arm"); + assertFalse(unsupportedContext.isUnknownClassLoaderContext()); + assertTrue(unsupportedContext.isUnsupportedClassLoaderContext()); + assertFalse(unsupportedContext.isVariableClassLoaderContext()); + + PackageDexUsage.DexUseInfo variableContext = new DexUseInfo(isUsedByOtherApps, userId, + PackageDexUsage.VARIABLE_CLASS_LOADER_CONTEXT, "arm"); + assertFalse(variableContext.isUnknownClassLoaderContext()); + assertFalse(variableContext.isUnsupportedClassLoaderContext()); + assertTrue(variableContext.isVariableClassLoaderContext()); + + PackageDexUsage.DexUseInfo unknownContext = new DexUseInfo(isUsedByOtherApps, userId, + PackageDexUsage.UNKNOWN_CLASS_LOADER_CONTEXT, "arm"); + assertTrue(unknownContext.isUnknownClassLoaderContext()); + assertFalse(unknownContext.isUnsupportedClassLoaderContext()); + assertFalse(unknownContext.isVariableClassLoaderContext()); + } + + @Test + public void testReadVersion1() { + String isa = VMRuntime.getInstructionSet(Build.SUPPORTED_ABIS[0]); + // Equivalent to + // record(mFooSplit2UsedByOtherApps0); + // record(mFooSecondary1User0); + // record(mFooSecondary2UsedByOtherApps0); + // record(mBarBaseUser0); + // record(mBarSecondary1User0); + String content = "PACKAGE_MANAGER__PACKAGE_DEX_USAGE__1\n" + + "com.google.foo,1\n" + + "#/data/user/0/com.google.foo/sec-1.dex\n" + + "0,0," + isa + "\n" + + "#/data/user/0/com.google.foo/sec-2.dex\n" + + "0,1," + isa + "\n" + + "com.google.bar,0\n" + + "#/data/user/0/com.google.bar/sec-1.dex\n" + + "0,0," + isa + "\n"; + + PackageDexUsage packageDexUsage = new PackageDexUsage(); + try { + packageDexUsage.read(new StringReader(content)); + } catch (IOException e) { + fail(); + } + + // After the read we must sync the data to fill the missing information on the code paths. + Map<String, Set<Integer>> packageToUsersMap = new HashMap<>(); + Map<String, Set<String>> packageToCodePaths = new HashMap<>(); + + // Handle foo package. + packageToUsersMap.put(mFooSplit2UsedByOtherApps0.mPackageName, + new HashSet<>(Arrays.asList(mFooSplit2UsedByOtherApps0.mOwnerUserId))); + packageToCodePaths.put(mFooSplit2UsedByOtherApps0.mPackageName, + new HashSet<>(Arrays.asList(mFooSplit2UsedByOtherApps0.mDexFile, + mFooSplit1User0.mDexFile, mFooBaseUser0.mDexFile))); + // Handle bar package. + packageToUsersMap.put(mBarBaseUser0.mPackageName, + new HashSet<>(Arrays.asList(mBarBaseUser0.mOwnerUserId))); + packageToCodePaths.put(mBarBaseUser0.mPackageName, + new HashSet<>(Arrays.asList(mBarBaseUser0.mDexFile))); + + // Sync the data. + packageDexUsage.syncData(packageToUsersMap, packageToCodePaths); + + // Update the class loaders to unknown before asserting if needed. Before version 2 we + // didn't have any. + String unknown = PackageDexUsage.UNKNOWN_CLASS_LOADER_CONTEXT; + TestData fooBaseUser0 = mFooBaseUser0.updateClassLoaderContext(unknown); + TestData fooSplit1User0 = mFooSplit1User0.updateClassLoaderContext(unknown); + TestData fooSplit2UsedByOtherApps0 = + mFooSplit2UsedByOtherApps0.updateClassLoaderContext(unknown); + TestData fooSecondary1User0 = mFooSecondary1User0.updateClassLoaderContext(unknown); + TestData fooSecondary2UsedByOtherApps0 = + mFooSecondary2UsedByOtherApps0.updateClassLoaderContext(unknown); + TestData barBaseUser0 = mBarBaseUser0.updateClassLoaderContext(unknown); + TestData barSecondary1User0 = mBarSecondary1User0.updateClassLoaderContext(unknown); + + // Assert foo code paths. Note that we ignore the users during upgrade. + final Set<String> ignoredUsers = null; + assertPackageDexUsage(packageDexUsage, ignoredUsers, + fooSplit2UsedByOtherApps0, fooSecondary1User0, fooSecondary2UsedByOtherApps0); + // Because fooSplit2UsedByOtherApps0 is used by others, all the other code paths must + // share the same data. + assertPackageDexUsage(packageDexUsage, ignoredUsers, + fooSplit1User0.updateUseByOthers(true), + fooSecondary1User0, fooSecondary2UsedByOtherApps0); + assertPackageDexUsage(packageDexUsage, ignoredUsers, fooBaseUser0.updateUseByOthers(true), + fooSecondary1User0, fooSecondary2UsedByOtherApps0); + + // Assert bar code paths. Note that we ignore the users during upgrade. + assertPackageDexUsage(packageDexUsage, ignoredUsers, barBaseUser0, barSecondary1User0); + } + private void assertPackageDexUsage(TestData primary, TestData... secondaries) { + assertPackageDexUsage(mPackageDexUsage, null, primary, secondaries); + } + + private void assertPackageDexUsage(PackageDexUsage packageDexUsage, Set<String> users, + TestData primary, TestData... secondaries) { String packageName = primary == null ? secondaries[0].mPackageName : primary.mPackageName; - boolean primaryUsedByOtherApps = primary == null ? false : primary.mUsedByOtherApps; - PackageUseInfo pInfo = mPackageDexUsage.getPackageUseInfo(packageName); + boolean primaryUsedByOtherApps = primary != null && primary.mUsedByOtherApps; + PackageUseInfo pInfo = packageDexUsage.getPackageUseInfo(packageName); // Check package use info assertNotNull(pInfo); - assertEquals(primaryUsedByOtherApps, pInfo.isUsedByOtherApps()); + if (primary != null) { + assertEquals(primaryUsedByOtherApps, pInfo.isUsedByOtherApps(primary.mDexFile)); + if (users != null) { + assertEquals(pInfo.getLoadingPackages(primary.mDexFile), users); + } + } + Map<String, DexUseInfo> dexUseInfoMap = pInfo.getDexUseInfoMap(); assertEquals(secondaries.length, dexUseInfoMap.size()); @@ -351,24 +565,45 @@ public class PackageDexUsageTests { assertEquals(testData.mOwnerUserId, dInfo.getOwnerUserId()); assertEquals(1, dInfo.getLoaderIsas().size()); assertTrue(dInfo.getLoaderIsas().contains(testData.mLoaderIsa)); + if (users != null) { + assertEquals(dInfo.getLoadingPackages(), users); + } + + assertEquals(testData.mClassLoaderContext, dInfo.getClassLoaderContext()); } } private boolean record(TestData testData) { return mPackageDexUsage.record(testData.mPackageName, testData.mDexFile, - testData.mOwnerUserId, testData.mLoaderIsa, testData.mUsedByOtherApps, - testData.mPrimaryOrSplit); + testData.mOwnerUserId, testData.mLoaderIsa, testData.mUsedByOtherApps, + testData.mPrimaryOrSplit, testData.mUsedBy, testData.mClassLoaderContext); + } + + private boolean record(PackageDexUsage packageDexUsage, TestData testData, Set<String> users) { + boolean result = true; + for (String user : users) { + result = result && packageDexUsage.record(testData.mPackageName, testData.mDexFile, + testData.mOwnerUserId, testData.mLoaderIsa, testData.mUsedByOtherApps, + testData.mPrimaryOrSplit, user, testData.mClassLoaderContext); + } + return result; } private void writeAndReadBack() { + mPackageDexUsage = writeAndReadBack(mPackageDexUsage); + } + + private PackageDexUsage writeAndReadBack(PackageDexUsage packageDexUsage) { try { StringWriter writer = new StringWriter(); - mPackageDexUsage.write(writer); + packageDexUsage.write(writer); - mPackageDexUsage = new PackageDexUsage(); - mPackageDexUsage.read(new StringReader(writer.toString())); + PackageDexUsage newPackageDexUsage = new PackageDexUsage(); + newPackageDexUsage.read(new StringReader(writer.toString())); + return newPackageDexUsage; } catch (IOException e) { fail("Unexpected IOException: " + e.getMessage()); + return null; } } @@ -379,16 +614,35 @@ public class PackageDexUsageTests { private final String mLoaderIsa; private final boolean mUsedByOtherApps; private final boolean mPrimaryOrSplit; + private final String mUsedBy; + private final String mClassLoaderContext; private TestData(String packageName, String dexFile, int ownerUserId, - String loaderIsa, boolean isUsedByOtherApps, boolean primaryOrSplit) { + String loaderIsa, boolean isUsedByOtherApps, boolean primaryOrSplit, String usedBy) { + this(packageName, dexFile, ownerUserId, loaderIsa, isUsedByOtherApps, primaryOrSplit, + usedBy, "DefaultClassLoaderContextFor_" + dexFile); + } + private TestData(String packageName, String dexFile, int ownerUserId, + String loaderIsa, boolean isUsedByOtherApps, boolean primaryOrSplit, String usedBy, + String classLoaderContext) { mPackageName = packageName; mDexFile = dexFile; mOwnerUserId = ownerUserId; mLoaderIsa = loaderIsa; mUsedByOtherApps = isUsedByOtherApps; mPrimaryOrSplit = primaryOrSplit; + mUsedBy = usedBy; + mClassLoaderContext = classLoaderContext; } + private TestData updateClassLoaderContext(String newContext) { + return new TestData(mPackageName, mDexFile, mOwnerUserId, mLoaderIsa, mUsedByOtherApps, + mPrimaryOrSplit, mUsedBy, newContext); + } + + private TestData updateUseByOthers(boolean newUsedByOthers) { + return new TestData(mPackageName, mDexFile, mOwnerUserId, mLoaderIsa, newUsedByOthers, + mPrimaryOrSplit, mUsedBy, mClassLoaderContext); + } } } diff --git a/services/usage/java/com/android/server/usage/StorageStatsService.java b/services/usage/java/com/android/server/usage/StorageStatsService.java index 9f4fb85f64c4..21b11b058160 100644 --- a/services/usage/java/com/android/server/usage/StorageStatsService.java +++ b/services/usage/java/com/android/server/usage/StorageStatsService.java @@ -392,6 +392,7 @@ public class StorageStatsService extends IStorageStatsManager.Stub { res.videoBytes = stats[2]; res.imageBytes = stats[3]; res.appBytes = stats[4]; + res.obbBytes = stats[5]; return res; } diff --git a/telephony/java/android/telephony/CarrierConfigManager.java b/telephony/java/android/telephony/CarrierConfigManager.java index 91b2d520d806..2e621f12bc3a 100644 --- a/telephony/java/android/telephony/CarrierConfigManager.java +++ b/telephony/java/android/telephony/CarrierConfigManager.java @@ -1508,6 +1508,13 @@ public class CarrierConfigManager { "boosted_lte_earfcns_string_array"; /** + * Determine whether to use only RSRP for the number of LTE signal bars. + * @hide + */ + public static final String KEY_USE_ONLY_RSRP_FOR_LTE_SIGNAL_BAR_BOOL = + "use_only_rsrp_for_lte_signal_bar_bool"; + + /** * Key identifying if voice call barring notification is required to be shown to the user. * @hide */ @@ -1862,6 +1869,7 @@ public class CarrierConfigManager { null); sDefaults.putInt(KEY_LTE_EARFCNS_RSRP_BOOST_INT, 0); sDefaults.putStringArray(KEY_BOOSTED_LTE_EARFCNS_STRING_ARRAY, null); + sDefaults.putBoolean(KEY_USE_ONLY_RSRP_FOR_LTE_SIGNAL_BAR_BOOL, false); sDefaults.putBoolean(KEY_DISABLE_VOICE_BARRING_NOTIFICATION_BOOL, false); sDefaults.putInt(IMSI_KEY_EXPIRATION_DAYS_TIME_INT, IMSI_ENCRYPTION_DAYS_TIME_DISABLED); sDefaults.putString(IMSI_KEY_DOWNLOAD_URL_STRING, null); diff --git a/telephony/java/android/telephony/SignalStrength.java b/telephony/java/android/telephony/SignalStrength.java index c8b4776522c6..de02de7ba17d 100644 --- a/telephony/java/android/telephony/SignalStrength.java +++ b/telephony/java/android/telephony/SignalStrength.java @@ -68,6 +68,7 @@ public class SignalStrength implements Parcelable { private int mTdScdmaRscp; private boolean isGsm; // This value is set by the ServiceStateTracker onSignalStrengthResult + private boolean mUseOnlyRsrpForLteLevel; // Use only RSRP for the number of LTE signal bar. /** * Create a new SignalStrength from a intent notifier Bundle @@ -108,6 +109,7 @@ public class SignalStrength implements Parcelable { mLteRsrpBoost = 0; mTdScdmaRscp = INVALID; isGsm = true; + mUseOnlyRsrpForLteLevel = false; } /** @@ -134,6 +136,7 @@ public class SignalStrength implements Parcelable { mLteRsrpBoost = 0; mTdScdmaRscp = INVALID; isGsm = gsmFlag; + mUseOnlyRsrpForLteLevel = false; } /** @@ -145,10 +148,10 @@ public class SignalStrength implements Parcelable { int cdmaDbm, int cdmaEcio, int evdoDbm, int evdoEcio, int evdoSnr, int lteSignalStrength, int lteRsrp, int lteRsrq, int lteRssnr, int lteCqi, - int lteRsrpBoost, int tdScdmaRscp, boolean gsmFlag) { + int lteRsrpBoost, int tdScdmaRscp, boolean gsmFlag, boolean lteLevelBaseOnRsrp) { initialize(gsmSignalStrength, gsmBitErrorRate, cdmaDbm, cdmaEcio, evdoDbm, evdoEcio, evdoSnr, lteSignalStrength, lteRsrp, - lteRsrq, lteRssnr, lteCqi, lteRsrpBoost, gsmFlag); + lteRsrq, lteRssnr, lteCqi, lteRsrpBoost, gsmFlag, lteLevelBaseOnRsrp); mTdScdmaRscp = tdScdmaRscp; } @@ -164,7 +167,7 @@ public class SignalStrength implements Parcelable { int tdScdmaRscp, boolean gsmFlag) { initialize(gsmSignalStrength, gsmBitErrorRate, cdmaDbm, cdmaEcio, evdoDbm, evdoEcio, evdoSnr, lteSignalStrength, lteRsrp, - lteRsrq, lteRssnr, lteCqi, 0, gsmFlag); + lteRsrq, lteRssnr, lteCqi, 0, gsmFlag, false); mTdScdmaRscp = tdScdmaRscp; } @@ -180,7 +183,7 @@ public class SignalStrength implements Parcelable { boolean gsmFlag) { initialize(gsmSignalStrength, gsmBitErrorRate, cdmaDbm, cdmaEcio, evdoDbm, evdoEcio, evdoSnr, lteSignalStrength, lteRsrp, - lteRsrq, lteRssnr, lteCqi, 0, gsmFlag); + lteRsrq, lteRssnr, lteCqi, 0, gsmFlag, false); } /** @@ -194,7 +197,7 @@ public class SignalStrength implements Parcelable { boolean gsmFlag) { initialize(gsmSignalStrength, gsmBitErrorRate, cdmaDbm, cdmaEcio, evdoDbm, evdoEcio, evdoSnr, 99, INVALID, - INVALID, INVALID, INVALID, 0, gsmFlag); + INVALID, INVALID, INVALID, 0, gsmFlag, false); } /** @@ -228,7 +231,7 @@ public class SignalStrength implements Parcelable { boolean gsm) { initialize(gsmSignalStrength, gsmBitErrorRate, cdmaDbm, cdmaEcio, evdoDbm, evdoEcio, evdoSnr, 99, INVALID, - INVALID, INVALID, INVALID, 0, gsm); + INVALID, INVALID, INVALID, 0, gsm, false); } /** @@ -248,6 +251,7 @@ public class SignalStrength implements Parcelable { * @param lteCqi * @param lteRsrpBoost * @param gsm + * @param useOnlyRsrpForLteLevel * * @hide */ @@ -255,7 +259,7 @@ public class SignalStrength implements Parcelable { int cdmaDbm, int cdmaEcio, int evdoDbm, int evdoEcio, int evdoSnr, int lteSignalStrength, int lteRsrp, int lteRsrq, int lteRssnr, int lteCqi, - int lteRsrpBoost, boolean gsm) { + int lteRsrpBoost, boolean gsm, boolean useOnlyRsrpForLteLevel) { mGsmSignalStrength = gsmSignalStrength; mGsmBitErrorRate = gsmBitErrorRate; mCdmaDbm = cdmaDbm; @@ -271,6 +275,7 @@ public class SignalStrength implements Parcelable { mLteRsrpBoost = lteRsrpBoost; mTdScdmaRscp = INVALID; isGsm = gsm; + mUseOnlyRsrpForLteLevel = useOnlyRsrpForLteLevel; if (DBG) log("initialize: " + toString()); } @@ -293,6 +298,7 @@ public class SignalStrength implements Parcelable { mLteRsrpBoost = s.mLteRsrpBoost; mTdScdmaRscp = s.mTdScdmaRscp; isGsm = s.isGsm; + mUseOnlyRsrpForLteLevel = s.mUseOnlyRsrpForLteLevel; } /** @@ -318,6 +324,7 @@ public class SignalStrength implements Parcelable { mLteRsrpBoost = in.readInt(); mTdScdmaRscp = in.readInt(); isGsm = (in.readInt() != 0); + mUseOnlyRsrpForLteLevel = (in.readInt() != 0); } /** @@ -366,6 +373,7 @@ public class SignalStrength implements Parcelable { out.writeInt(mLteRsrpBoost); out.writeInt(mTdScdmaRscp); out.writeInt(isGsm ? 1 : 0); + out.writeInt(mUseOnlyRsrpForLteLevel ? 1 : 0); } /** @@ -449,6 +457,17 @@ public class SignalStrength implements Parcelable { } /** + * @param useOnlyRsrpForLteLevel true if it uses only RSRP for the number of LTE signal bar, + * otherwise false. + * + * Used by phone to use only RSRP or not for the number of LTE signal bar. + * @hide + */ + public void setUseOnlyRsrpForLteLevel(boolean useOnlyRsrpForLteLevel) { + mUseOnlyRsrpForLteLevel = useOnlyRsrpForLteLevel; + } + + /** * @param lteRsrpBoost - signal strength offset * * Used by phone to set the lte signal strength offset which will be @@ -835,6 +854,13 @@ public class SignalStrength implements Parcelable { } } + if (useOnlyRsrpForLteLevel()) { + log("getLTELevel - rsrp = " + rsrpIconLevel); + if (rsrpIconLevel != -1) { + return rsrpIconLevel; + } + } + /* * Values are -200 dB to +300 (SNR*10dB) RS_SNR >= 13.0 dB =>4 bars 4.5 * dB <= RS_SNR < 13.0 dB => 3 bars 1.0 dB <= RS_SNR < 4.5 dB => 2 bars @@ -915,6 +941,15 @@ public class SignalStrength implements Parcelable { } /** + * @return true if it uses only RSRP for the number of LTE signal bar, otherwise false. + * + * @hide + */ + public boolean useOnlyRsrpForLteLevel() { + return this.mUseOnlyRsrpForLteLevel; + } + + /** * @return get TD_SCDMA dbm * * @hide @@ -974,7 +1009,8 @@ public class SignalStrength implements Parcelable { + (mEvdoDbm * primeNum) + (mEvdoEcio * primeNum) + (mEvdoSnr * primeNum) + (mLteSignalStrength * primeNum) + (mLteRsrp * primeNum) + (mLteRsrq * primeNum) + (mLteRssnr * primeNum) + (mLteCqi * primeNum) - + (mLteRsrpBoost * primeNum) + (mTdScdmaRscp * primeNum) + (isGsm ? 1 : 0)); + + (mLteRsrpBoost * primeNum) + (mTdScdmaRscp * primeNum) + (isGsm ? 1 : 0) + + (mUseOnlyRsrpForLteLevel ? 1 : 0)); } /** @@ -1008,7 +1044,8 @@ public class SignalStrength implements Parcelable { && mLteCqi == s.mLteCqi && mLteRsrpBoost == s.mLteRsrpBoost && mTdScdmaRscp == s.mTdScdmaRscp - && isGsm == s.isGsm); + && isGsm == s.isGsm + && mUseOnlyRsrpForLteLevel == s.mUseOnlyRsrpForLteLevel); } /** @@ -1031,7 +1068,9 @@ public class SignalStrength implements Parcelable { + " " + mLteCqi + " " + mLteRsrpBoost + " " + mTdScdmaRscp - + " " + (isGsm ? "gsm|lte" : "cdma")); + + " " + (isGsm ? "gsm|lte" : "cdma") + + " " + (mUseOnlyRsrpForLteLevel ? "use_only_rsrp_for_lte_level" : + "use_rsrp_and_rssnr_for_lte_level")); } /** Returns the signal strength related to GSM. */ @@ -1086,6 +1125,7 @@ public class SignalStrength implements Parcelable { mLteRsrpBoost = m.getInt("lteRsrpBoost"); mTdScdmaRscp = m.getInt("TdScdma"); isGsm = m.getBoolean("isGsm"); + mUseOnlyRsrpForLteLevel = m.getBoolean("useOnlyRsrpForLteLevel"); } /** @@ -1110,6 +1150,7 @@ public class SignalStrength implements Parcelable { m.putInt("lteRsrpBoost", mLteRsrpBoost); m.putInt("TdScdma", mTdScdmaRscp); m.putBoolean("isGsm", isGsm); + m.putBoolean("useOnlyRsrpForLteLevel", mUseOnlyRsrpForLteLevel); } /** |