diff options
| author | 2024-10-25 12:44:33 -0700 | |
|---|---|---|
| committer | 2024-10-28 23:12:40 +0000 | |
| commit | 053419e86a486aead52da265848c3791377aa5a8 (patch) | |
| tree | 6921ddb9c215c36ec67d77ca13df1554b4e6ab57 | |
| parent | 7c04f065a631f33b0b3b0e924b7100599898c805 (diff) | |
Make the PIC stringblock local
The shared memory string block can be ephemeral in the Java layer.
This change uses the persistent copy in the native layer.  This saves
8Kb for every process in the system.
Flag: android.app.pic_uses_shared_memory
Bug: 360897450
Test: atest
 * FrameworksCoreTests:PropertyInvalidatedCacheTests
 * FrameworksCoreTests:IpcDataCacheTest
 * CtsOsTestCases:IpcDataCacheTest
Change-Id: I7913507a5428a0d32afe2d41f026f104fccf4a88
| -rw-r--r-- | core/java/android/app/PropertyInvalidatedCache.java | 58 | 
1 files changed, 29 insertions, 29 deletions
diff --git a/core/java/android/app/PropertyInvalidatedCache.java b/core/java/android/app/PropertyInvalidatedCache.java index bc9e709420f1..14e6b3bcf81b 100644 --- a/core/java/android/app/PropertyInvalidatedCache.java +++ b/core/java/android/app/PropertyInvalidatedCache.java @@ -1787,14 +1787,6 @@ public class PropertyInvalidatedCache<Query, Result> {          // block.          private static final int MAX_STRING_LENGTH = 63; -        // The raw byte block.  Strings are stored as run-length encoded byte arrays.  The first -        // byte is the length of the following string.  It is an axiom of the system that the -        // string block is initially all zeros and that it is write-once memory: new strings are -        // appended to existing strings, so there is never a need to revisit strings that have -        // already been pulled from the string block. -        @GuardedBy("mLock") -        private final byte[] mStringBlock; -          // The expected hash code of the string block.  If the hash over the string block equals          // this value, then the string block is valid.  Otherwise, the block is not valid and          // should be re-read.  An invalid block generally means that a client has read the shared @@ -1806,12 +1798,15 @@ public class PropertyInvalidatedCache<Query, Result> {          // logging.          private final int mMaxNonce; +        // The size of the native byte block. +        private final int mMaxByte; +          /** @hide */          @VisibleForTesting          public NonceStore(long ptr, boolean mutable) {              mPtr = ptr;              mMutable = mutable; -            mStringBlock = new byte[nativeGetMaxByte(ptr)]; +            mMaxByte = nativeGetMaxByte(ptr);              mMaxNonce = nativeGetMaxNonce(ptr);              refreshStringBlockLocked();          } @@ -1870,17 +1865,17 @@ public class PropertyInvalidatedCache<Query, Result> {          // and the block hash is not checked.  The function skips past strings that have already          // been read, and then processes any new strings.          @GuardedBy("mLock") -        private void updateStringMapLocked() { +        private void updateStringMapLocked(byte[] block) {              int index = 0;              int offset = 0; -            while (offset < mStringBlock.length && mStringBlock[offset] != 0) { +            while (offset < block.length && block[offset] != 0) {                  if (index > mHighestIndex) {                      // Only record the string if it has not been seen yet. -                    final String s = new String(mStringBlock, offset+1, mStringBlock[offset]); +                    final String s = new String(block, offset+1, block[offset]);                      mStringHandle.put(s, index);                      mHighestIndex = index;                  } -                offset += mStringBlock[offset] + 1; +                offset += block[offset] + 1;                  index++;              }              mStringBytes = offset; @@ -1889,24 +1884,21 @@ public class PropertyInvalidatedCache<Query, Result> {          // Append a string to the string block and update the hash.  This does not write the block          // to shared memory.          @GuardedBy("mLock") -        private void appendStringToMapLocked(@NonNull String str) { +        private void appendStringToMapLocked(@NonNull String str, @NonNull byte[] block) {              int offset = 0; -            while (offset < mStringBlock.length && mStringBlock[offset] != 0) { -                offset += mStringBlock[offset] + 1; +            while (offset < block.length && block[offset] != 0) { +                offset += block[offset] + 1;              }              final byte[] strBytes = str.getBytes(); -            if (offset + strBytes.length >= mStringBlock.length) { +            if (offset + strBytes.length >= block.length) {                  // Overflow.  Do not add the string to the block; the string will remain undefined.                  return;              } -            mStringBlock[offset] = (byte) strBytes.length; -            offset++; -            for (int i = 0; i < strBytes.length; i++, offset++) { -                mStringBlock[offset] = strBytes[i]; -            } -            mBlockHash = Arrays.hashCode(mStringBlock); +            block[offset] = (byte) strBytes.length; +            System.arraycopy(strBytes, 0, block, offset+1, strBytes.length); +            mBlockHash = Arrays.hashCode(block);          }          // Possibly update the string block.  If the native shared memory has a new block hash, @@ -1917,8 +1909,9 @@ public class PropertyInvalidatedCache<Query, Result> {                  // The fastest way to know that the shared memory string block has not changed.                  return;              } -            final int hash = nativeGetByteBlock(mPtr, mBlockHash, mStringBlock); -            if (hash != Arrays.hashCode(mStringBlock)) { +            byte[] block = new byte[mMaxByte]; +            final int hash = nativeGetByteBlock(mPtr, mBlockHash, block); +            if (hash != Arrays.hashCode(block)) {                  // This is a partial read: ignore it.  The next time someone needs this string                  // the memory will be read again and should succeed.  Set the local hash to                  // zero to ensure that the next read attempt will actually read from shared @@ -1930,7 +1923,7 @@ public class PropertyInvalidatedCache<Query, Result> {              // The hash has changed.  Update the strings from the byte block.              mStringUpdated++;              mBlockHash = hash; -            updateStringMapLocked(); +            updateStringMapLocked(block);          }          // Throw an exception if the string cannot be stored in the string block. @@ -1963,6 +1956,9 @@ public class PropertyInvalidatedCache<Query, Result> {              }          } +        static final AtomicLong sStoreCount = new AtomicLong(); + +          // Add a string to the local copy of the block and write the block to shared memory.          // Return the index of the new string.  If the string has already been recorded, the          // shared memory is not updated but the index of the existing string is returned. @@ -1972,9 +1968,11 @@ public class PropertyInvalidatedCache<Query, Result> {                  if (handle == null) {                      throwIfImmutable();                      throwIfBadString(str); -                    appendStringToMapLocked(str); -                    nativeSetByteBlock(mPtr, mBlockHash, mStringBlock); -                    updateStringMapLocked(); +                    byte[] block = new byte[mMaxByte]; +                    nativeGetByteBlock(mPtr, 0, block); +                    appendStringToMapLocked(str, block); +                    nativeSetByteBlock(mPtr, mBlockHash, block); +                    updateStringMapLocked(block);                      handle = mStringHandle.get(str);                  }                  return handle; @@ -2038,6 +2036,7 @@ public class PropertyInvalidatedCache<Query, Result> {       * @param mPtr the pointer to the native shared memory.       * @return the number of nonces supported by the shared memory.       */ +    @FastNative      private static native int nativeGetMaxNonce(long mPtr);      /** @@ -2046,6 +2045,7 @@ public class PropertyInvalidatedCache<Query, Result> {       * @param mPtr the pointer to the native shared memory.       * @return the number of string bytes supported by the shared memory.       */ +    @FastNative      private static native int nativeGetMaxByte(long mPtr);      /**  |