Compute digest in BackgroundThread which is limited to little cores.

Bug: 149525109
Test: atest --test-mapping apex/blobstore
Change-Id: I70333e1d880c45db15e9e1fe730f3443626aab67
This commit is contained in:
Sudheer Shanka
2020-02-27 15:17:43 -08:00
parent b629d69db0
commit b76f766df7
3 changed files with 29 additions and 7 deletions

View File

@@ -74,6 +74,7 @@ import android.util.Xml;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.os.BackgroundThread;
import com.android.internal.util.CollectionUtils;
import com.android.internal.util.DumpUtils;
import com.android.internal.util.FastXmlSerializer;
@@ -140,6 +141,7 @@ public class BlobStoreManagerService extends SystemService {
private final Context mContext;
private final Handler mHandler;
private final Handler mBackgroundHandler;
private final Injector mInjector;
private final SessionStateChangeListener mSessionStateChangeListener =
new SessionStateChangeListener();
@@ -160,11 +162,12 @@ public class BlobStoreManagerService extends SystemService {
mContext = context;
mInjector = injector;
mHandler = mInjector.initializeMessageHandler();
mBackgroundHandler = mInjector.getBackgroundHandler();
}
private static Handler initializeMessageHandler() {
final HandlerThread handlerThread = new ServiceThread(TAG,
Process.THREAD_PRIORITY_BACKGROUND, true /* allowIo */);
Process.THREAD_PRIORITY_DEFAULT, true /* allowIo */);
handlerThread.start();
final Handler handler = new Handler(handlerThread.getLooper());
Watchdog.getInstance().addThread(handler);
@@ -418,7 +421,7 @@ public class BlobStoreManagerService extends SystemService {
public void onStateChanged(@NonNull BlobStoreSession session) {
mHandler.post(PooledLambda.obtainRunnable(
BlobStoreManagerService::onStateChangedInternal,
BlobStoreManagerService.this, session));
BlobStoreManagerService.this, session).recycleOnUse());
}
}
@@ -437,7 +440,11 @@ public class BlobStoreManagerService extends SystemService {
}
break;
case STATE_COMMITTED:
session.verifyBlobData();
mBackgroundHandler.post(() -> {
session.computeDigest();
mHandler.post(PooledLambda.obtainRunnable(
BlobStoreSession::verifyBlobData, session).recycleOnUse());
});
break;
case STATE_VERIFIED_VALID:
synchronized (mBlobsLock) {
@@ -1412,5 +1419,9 @@ public class BlobStoreManagerService extends SystemService {
public Handler initializeMessageHandler() {
return BlobStoreManagerService.initializeMessageHandler();
}
public Handler getBackgroundHandler() {
return BackgroundThread.getHandler();
}
}
}

View File

@@ -96,6 +96,10 @@ class BlobStoreSession extends IBlobStoreSession.Stub {
@GuardedBy("mRevocableFds")
private ArrayList<RevocableFileDescriptor> mRevocableFds = new ArrayList<>();
// This will be accessed from only one thread at any point of time, so no need to grab
// a lock for this.
private byte[] mDataDigest;
@GuardedBy("mSessionLock")
private int mState = STATE_CLOSED;
@@ -381,19 +385,21 @@ class BlobStoreSession extends IBlobStoreSession.Stub {
}
}
void verifyBlobData() {
byte[] actualDigest = null;
void computeDigest() {
try {
Trace.traceBegin(TRACE_TAG_SYSTEM_SERVER,
"computeBlobDigest-i" + mSessionId + "-l" + getSessionFile().length());
actualDigest = FileUtils.digest(getSessionFile(), mBlobHandle.algorithm);
mDataDigest = FileUtils.digest(getSessionFile(), mBlobHandle.algorithm);
} catch (IOException | NoSuchAlgorithmException e) {
Slog.e(TAG, "Error computing the digest", e);
} finally {
Trace.traceEnd(TRACE_TAG_SYSTEM_SERVER);
}
}
void verifyBlobData() {
synchronized (mSessionLock) {
if (actualDigest != null && Arrays.equals(actualDigest, mBlobHandle.digest)) {
if (mDataDigest != null && Arrays.equals(mDataDigest, mBlobHandle.digest)) {
mState = STATE_VERIFIED_VALID;
// Commit callback will be sent once the data is persisted.
} else {

View File

@@ -343,5 +343,10 @@ public class BlobStoreManagerServiceTest {
public Handler initializeMessageHandler() {
return mHandler;
}
@Override
public Handler getBackgroundHandler() {
return mHandler;
}
}
}