Merge "Compute digest in BackgroundThread which is limited to little cores." into rvc-dev

This commit is contained in:
Sudheer Shanka
2020-03-03 20:08:39 +00:00
committed by Android (Google) Code Review
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.GuardedBy;
import com.android.internal.annotations.VisibleForTesting; import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.os.BackgroundThread;
import com.android.internal.util.CollectionUtils; import com.android.internal.util.CollectionUtils;
import com.android.internal.util.DumpUtils; import com.android.internal.util.DumpUtils;
import com.android.internal.util.FastXmlSerializer; import com.android.internal.util.FastXmlSerializer;
@@ -140,6 +141,7 @@ public class BlobStoreManagerService extends SystemService {
private final Context mContext; private final Context mContext;
private final Handler mHandler; private final Handler mHandler;
private final Handler mBackgroundHandler;
private final Injector mInjector; private final Injector mInjector;
private final SessionStateChangeListener mSessionStateChangeListener = private final SessionStateChangeListener mSessionStateChangeListener =
new SessionStateChangeListener(); new SessionStateChangeListener();
@@ -160,11 +162,12 @@ public class BlobStoreManagerService extends SystemService {
mContext = context; mContext = context;
mInjector = injector; mInjector = injector;
mHandler = mInjector.initializeMessageHandler(); mHandler = mInjector.initializeMessageHandler();
mBackgroundHandler = mInjector.getBackgroundHandler();
} }
private static Handler initializeMessageHandler() { private static Handler initializeMessageHandler() {
final HandlerThread handlerThread = new ServiceThread(TAG, final HandlerThread handlerThread = new ServiceThread(TAG,
Process.THREAD_PRIORITY_BACKGROUND, true /* allowIo */); Process.THREAD_PRIORITY_DEFAULT, true /* allowIo */);
handlerThread.start(); handlerThread.start();
final Handler handler = new Handler(handlerThread.getLooper()); final Handler handler = new Handler(handlerThread.getLooper());
Watchdog.getInstance().addThread(handler); Watchdog.getInstance().addThread(handler);
@@ -418,7 +421,7 @@ public class BlobStoreManagerService extends SystemService {
public void onStateChanged(@NonNull BlobStoreSession session) { public void onStateChanged(@NonNull BlobStoreSession session) {
mHandler.post(PooledLambda.obtainRunnable( mHandler.post(PooledLambda.obtainRunnable(
BlobStoreManagerService::onStateChangedInternal, BlobStoreManagerService::onStateChangedInternal,
BlobStoreManagerService.this, session)); BlobStoreManagerService.this, session).recycleOnUse());
} }
} }
@@ -437,7 +440,11 @@ public class BlobStoreManagerService extends SystemService {
} }
break; break;
case STATE_COMMITTED: case STATE_COMMITTED:
session.verifyBlobData(); mBackgroundHandler.post(() -> {
session.computeDigest();
mHandler.post(PooledLambda.obtainRunnable(
BlobStoreSession::verifyBlobData, session).recycleOnUse());
});
break; break;
case STATE_VERIFIED_VALID: case STATE_VERIFIED_VALID:
synchronized (mBlobsLock) { synchronized (mBlobsLock) {
@@ -1412,5 +1419,9 @@ public class BlobStoreManagerService extends SystemService {
public Handler initializeMessageHandler() { public Handler initializeMessageHandler() {
return BlobStoreManagerService.initializeMessageHandler(); return BlobStoreManagerService.initializeMessageHandler();
} }
public Handler getBackgroundHandler() {
return BackgroundThread.getHandler();
}
} }
} }

View File

@@ -96,6 +96,10 @@ class BlobStoreSession extends IBlobStoreSession.Stub {
@GuardedBy("mRevocableFds") @GuardedBy("mRevocableFds")
private ArrayList<RevocableFileDescriptor> mRevocableFds = new ArrayList<>(); 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") @GuardedBy("mSessionLock")
private int mState = STATE_CLOSED; private int mState = STATE_CLOSED;
@@ -381,19 +385,21 @@ class BlobStoreSession extends IBlobStoreSession.Stub {
} }
} }
void verifyBlobData() { void computeDigest() {
byte[] actualDigest = null;
try { try {
Trace.traceBegin(TRACE_TAG_SYSTEM_SERVER, Trace.traceBegin(TRACE_TAG_SYSTEM_SERVER,
"computeBlobDigest-i" + mSessionId + "-l" + getSessionFile().length()); "computeBlobDigest-i" + mSessionId + "-l" + getSessionFile().length());
actualDigest = FileUtils.digest(getSessionFile(), mBlobHandle.algorithm); mDataDigest = FileUtils.digest(getSessionFile(), mBlobHandle.algorithm);
} catch (IOException | NoSuchAlgorithmException e) { } catch (IOException | NoSuchAlgorithmException e) {
Slog.e(TAG, "Error computing the digest", e); Slog.e(TAG, "Error computing the digest", e);
} finally { } finally {
Trace.traceEnd(TRACE_TAG_SYSTEM_SERVER); Trace.traceEnd(TRACE_TAG_SYSTEM_SERVER);
} }
}
void verifyBlobData() {
synchronized (mSessionLock) { synchronized (mSessionLock) {
if (actualDigest != null && Arrays.equals(actualDigest, mBlobHandle.digest)) { if (mDataDigest != null && Arrays.equals(mDataDigest, mBlobHandle.digest)) {
mState = STATE_VERIFIED_VALID; mState = STATE_VERIFIED_VALID;
// Commit callback will be sent once the data is persisted. // Commit callback will be sent once the data is persisted.
} else { } else {

View File

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