diff options
-rw-r--r-- | services/autofill/java/com/android/server/autofill/FieldClassificationEventLogger.java | 94 | ||||
-rw-r--r-- | services/autofill/java/com/android/server/autofill/RemoteFieldClassificationService.java | 16 |
2 files changed, 107 insertions, 3 deletions
diff --git a/services/autofill/java/com/android/server/autofill/FieldClassificationEventLogger.java b/services/autofill/java/com/android/server/autofill/FieldClassificationEventLogger.java new file mode 100644 index 000000000000..ffb4632164a3 --- /dev/null +++ b/services/autofill/java/com/android/server/autofill/FieldClassificationEventLogger.java @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2023 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.autofill; + +import static com.android.internal.util.FrameworkStatsLog.AUTOFILL_FIELD_CLASSIFICATION_EVENT_REPORTED; +import static com.android.server.autofill.Helper.sVerbose; + +import android.util.Slog; + +import com.android.internal.util.FrameworkStatsLog; + +import java.util.Optional; + +/** + * Helper class to log Field Classification stats. + */ +public final class FieldClassificationEventLogger { + private static final String TAG = "FieldClassificationEventLogger"; + private Optional<FieldClassificationEventInternal> mEventInternal; + + private FieldClassificationEventLogger() { + mEventInternal = Optional.empty(); + } + + /** + * A factory constructor to create FieldClassificationEventLogger. + */ + public static FieldClassificationEventLogger createLogger() { + return new FieldClassificationEventLogger(); + } + + /** + * Reset mEventInternal before logging for a new request. It shall be called for each + * FieldClassification request. + */ + public void startNewLogForRequest() { + if (!mEventInternal.isEmpty()) { + Slog.w(TAG, "FieldClassificationEventLogger is not empty before starting for a new " + + "request"); + } + mEventInternal = Optional.of(new FieldClassificationEventInternal()); + } + + /** + * Set latency as long as mEventInternal presents. + */ + public void maybeSetLatencyMillis(long timestamp) { + mEventInternal.ifPresent(event -> { + event.mLatencyClassificationRequestMillis = timestamp; + }); + } + + /** + * Log an AUTOFILL_FIELD_CLASSIFICATION_EVENT_REPORTED event. + */ + public void logAndEndEvent() { + if (!mEventInternal.isPresent()) { + Slog.w(TAG, "Shouldn't be logging AutofillFieldClassificationEventInternal again for " + + "same event"); + return; + } + FieldClassificationEventInternal event = mEventInternal.get(); + if (sVerbose) { + Slog.v(TAG, "Log AutofillFieldClassificationEventReported:" + + " mLatencyClassificationRequestMillis=" + + event.mLatencyClassificationRequestMillis); + } + FrameworkStatsLog.write( + AUTOFILL_FIELD_CLASSIFICATION_EVENT_REPORTED, + event.mLatencyClassificationRequestMillis); + mEventInternal = Optional.empty(); + } + + private static final class FieldClassificationEventInternal { + long mLatencyClassificationRequestMillis = -1; + + FieldClassificationEventInternal() { + } + } +} diff --git a/services/autofill/java/com/android/server/autofill/RemoteFieldClassificationService.java b/services/autofill/java/com/android/server/autofill/RemoteFieldClassificationService.java index 99a2291016e9..feae56e89784 100644 --- a/services/autofill/java/com/android/server/autofill/RemoteFieldClassificationService.java +++ b/services/autofill/java/com/android/server/autofill/RemoteFieldClassificationService.java @@ -30,6 +30,7 @@ import android.content.pm.PackageManager; import android.content.pm.ServiceInfo; import android.os.ICancellationSignal; import android.os.RemoteException; +import android.os.SystemClock; import android.service.assist.classification.FieldClassificationRequest; import android.service.assist.classification.FieldClassificationResponse; import android.service.assist.classification.FieldClassificationService; @@ -132,7 +133,7 @@ final class RemoteFieldClassificationService public void onFieldClassificationRequest(@NonNull FieldClassificationRequest request, FieldClassificationServiceCallbacks fieldClassificationServiceCallbacks) { - + final long startTime = SystemClock.elapsedRealtime(); if (sVerbose) { Slog.v(TAG, "onFieldClassificationRequest request:" + request); } @@ -144,6 +145,7 @@ final class RemoteFieldClassificationService new IFieldClassificationCallback.Stub() { @Override public void onCancellable(ICancellationSignal cancellation) { + logLatency(startTime); if (sDebug) { Log.d(TAG, "onCancellable"); } @@ -151,15 +153,15 @@ final class RemoteFieldClassificationService @Override public void onSuccess(FieldClassificationResponse response) { + logLatency(startTime); if (sDebug) { Log.d(TAG, "onSuccess Response: " + response); } - fieldClassificationServiceCallbacks - .onClassificationRequestSuccess(response); } @Override public void onFailure() { + logLatency(startTime); if (sDebug) { Log.d(TAG, "onFailure"); } @@ -174,4 +176,12 @@ final class RemoteFieldClassificationService public void cancel() throws RemoteException {} })); } + + private void logLatency(long startTime) { + final FieldClassificationEventLogger logger = FieldClassificationEventLogger.createLogger(); + logger.startNewLogForRequest(); + logger.maybeSetLatencyMillis( + SystemClock.elapsedRealtime() - startTime); + logger.logAndEndEvent(); + } } |