Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 10f7c412 authored by Simranjit Kohli's avatar Simranjit Kohli Committed by Android (Google) Code Review
Browse files

Merge "[Autofill PCC Metrics]: Add latency to FieldClassificationService Bug:...

Merge "[Autofill PCC Metrics]: Add latency to FieldClassificationService Bug: 269484106 Test: m" into udc-dev
parents ea443c5b 6ac125da
Loading
Loading
Loading
Loading
+94 −0
Original line number Diff line number Diff line
/*
 * 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() {
        }
    }
}
+13 −3
Original line number Diff line number Diff line
@@ -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();
    }
}