Merge changes from topic "dynamic_system.data_transfer.shared_memory.size"

* changes:
  dynsystem.SparseInputStream: Implement read(buf, off, len)
  dynsystem: Enlarge default shared memory size and allow size override
This commit is contained in:
Yi-yo Chiang
2022-03-22 02:28:07 +00:00
committed by Gerrit Code Review
2 changed files with 26 additions and 17 deletions

View File

@@ -23,9 +23,11 @@ import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
import android.os.MemoryFile; import android.os.MemoryFile;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.os.SystemProperties;
import android.os.image.DynamicSystemManager; import android.os.image.DynamicSystemManager;
import android.service.persistentdata.PersistentDataBlockManager; import android.service.persistentdata.PersistentDataBlockManager;
import android.util.Log; import android.util.Log;
import android.util.Range;
import android.webkit.URLUtil; import android.webkit.URLUtil;
import org.json.JSONException; import org.json.JSONException;
@@ -48,7 +50,12 @@ class InstallationAsyncTask extends AsyncTask<String, Long, Throwable> {
private static final String TAG = "InstallationAsyncTask"; private static final String TAG = "InstallationAsyncTask";
private static final int READ_BUFFER_SIZE = 1 << 13; private static final int MIN_SHARED_MEMORY_SIZE = 8 << 10; // 8KiB
private static final int MAX_SHARED_MEMORY_SIZE = 1024 << 10; // 1MiB
private static final int DEFAULT_SHARED_MEMORY_SIZE = 64 << 10; // 64KiB
private static final String SHARED_MEMORY_SIZE_PROP =
"dynamic_system.data_transfer.shared_memory.size";
private static final long MIN_PROGRESS_TO_PUBLISH = 1 << 27; private static final long MIN_PROGRESS_TO_PUBLISH = 1 << 27;
private static final List<String> UNSUPPORTED_PARTITIONS = private static final List<String> UNSUPPORTED_PARTITIONS =
@@ -131,6 +138,7 @@ class InstallationAsyncTask extends AsyncTask<String, Long, Throwable> {
void onResult(int resultCode, Throwable detail); void onResult(int resultCode, Throwable detail);
} }
private final int mSharedMemorySize;
private final String mUrl; private final String mUrl;
private final String mDsuSlot; private final String mDsuSlot;
private final String mPublicKey; private final String mPublicKey;
@@ -164,6 +172,11 @@ class InstallationAsyncTask extends AsyncTask<String, Long, Throwable> {
Context context, Context context,
DynamicSystemManager dynSystem, DynamicSystemManager dynSystem,
ProgressListener listener) { ProgressListener listener) {
mSharedMemorySize =
Range.create(MIN_SHARED_MEMORY_SIZE, MAX_SHARED_MEMORY_SIZE)
.clamp(
SystemProperties.getInt(
SHARED_MEMORY_SIZE_PROP, DEFAULT_SHARED_MEMORY_SIZE));
mUrl = url; mUrl = url;
mDsuSlot = dsuSlot; mDsuSlot = dsuSlot;
mPublicKey = publicKey; mPublicKey = publicKey;
@@ -541,10 +554,10 @@ class InstallationAsyncTask extends AsyncTask<String, Long, Throwable> {
Log.d(TAG, "Start installing: " + partitionName); Log.d(TAG, "Start installing: " + partitionName);
MemoryFile memoryFile = new MemoryFile("dsu_" + partitionName, READ_BUFFER_SIZE); MemoryFile memoryFile = new MemoryFile("dsu_" + partitionName, mSharedMemorySize);
ParcelFileDescriptor pfd = new ParcelFileDescriptor(memoryFile.getFileDescriptor()); ParcelFileDescriptor pfd = new ParcelFileDescriptor(memoryFile.getFileDescriptor());
mInstallationSession.setAshmem(pfd, READ_BUFFER_SIZE); mInstallationSession.setAshmem(pfd, memoryFile.length());
mPartitionName = partitionName; mPartitionName = partitionName;
mPartitionSize = partitionSize; mPartitionSize = partitionSize;
@@ -553,10 +566,10 @@ class InstallationAsyncTask extends AsyncTask<String, Long, Throwable> {
long prevInstalledSize = 0; long prevInstalledSize = 0;
long installedSize = 0; long installedSize = 0;
byte[] bytes = new byte[READ_BUFFER_SIZE]; byte[] bytes = new byte[memoryFile.length()];
int numBytesRead; int numBytesRead;
while ((numBytesRead = sis.read(bytes, 0, READ_BUFFER_SIZE)) != -1) { while ((numBytesRead = sis.read(bytes, 0, bytes.length)) != -1) {
if (isCancelled()) { if (isCancelled()) {
return; return;
} }

View File

@@ -133,36 +133,32 @@ public class SparseInputStream extends InputStream {
return mLeft == 0; return mLeft == 0;
} }
/** @Override
* It overrides the InputStream.read(byte[] buf) public int read(byte[] buf, int off, int len) throws IOException {
*/
public int read(byte[] buf) throws IOException {
if (!mIsSparse) { if (!mIsSparse) {
return mIn.read(buf); return mIn.read(buf, off, len);
} }
if (prepareChunk()) return -1; if (prepareChunk()) return -1;
int n = -1; int n = -1;
switch (mCur.mChunkType) { switch (mCur.mChunkType) {
case SparseChunk.RAW: case SparseChunk.RAW:
n = mIn.read(buf, 0, (int) min(mLeft, buf.length)); n = mIn.read(buf, off, (int) min(mLeft, len));
mLeft -= n; mLeft -= n;
return n; return n;
case SparseChunk.DONTCARE: case SparseChunk.DONTCARE:
n = (int) min(mLeft, buf.length); n = (int) min(mLeft, len);
Arrays.fill(buf, 0, n - 1, (byte) 0); Arrays.fill(buf, off, off + n, (byte) 0);
mLeft -= n; mLeft -= n;
return n; return n;
case SparseChunk.FILL: case SparseChunk.FILL:
// The FILL type is rarely used, so use a simple implmentation. // The FILL type is rarely used, so use a simple implmentation.
return super.read(buf); return super.read(buf, off, len);
default: default:
throw new IOException("Unsupported Chunk:" + mCur.toString()); throw new IOException("Unsupported Chunk:" + mCur.toString());
} }
} }
/** @Override
* It overrides the InputStream.read()
*/
public int read() throws IOException { public int read() throws IOException {
if (!mIsSparse) { if (!mIsSparse) {
return mIn.read(); return mIn.read();