From 31ee71697c7333683103c126a79189f9dac6bb32 Mon Sep 17 00:00:00 2001 From: Ivan Chiang Date: Thu, 8 Dec 2022 11:11:00 +0000 Subject: [PATCH] Hotword: Add uid into the original metrics We add new uid filed in HotwordDetectionServiceInitResultReported, HotwordDetectionServiceRestarted and HotwordDetectorKeyphraseTriggered. Update the metrics to log it. Test: statsd_testdrive Bug: 261793715 Change-Id: Ia49d5981be296c1143074774cc6c00fcc25671e6 Merged-In: Ia49d5981be296c1143074774cc6c00fcc25671e6 --- .../HotwordDetectionConnection.java | 85 +++++++++++-------- .../HotwordMetricsLogger.java | 12 +-- .../VoiceInteractionManagerServiceImpl.java | 2 +- 3 files changed, 58 insertions(+), 41 deletions(-) diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/HotwordDetectionConnection.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/HotwordDetectionConnection.java index 6f7d80caa1471..72db70bc351df 100644 --- a/services/voiceinteraction/java/com/android/server/voiceinteraction/HotwordDetectionConnection.java +++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/HotwordDetectionConnection.java @@ -267,7 +267,8 @@ final class HotwordDetectionConnection { synchronized (mLock) { restartProcessLocked(); HotwordMetricsLogger.writeServiceRestartEvent(mDetectorType, - HOTWORD_DETECTION_SERVICE_RESTARTED__REASON__SCHEDULE); + HOTWORD_DETECTION_SERVICE_RESTARTED__REASON__SCHEDULE, + mVoiceInteractionServiceUid); } }, mReStartPeriodSeconds, mReStartPeriodSeconds, TimeUnit.SECONDS); } @@ -302,7 +303,8 @@ final class HotwordDetectionConnection { // conditions with audio reading in the service. restartProcessLocked(); HotwordMetricsLogger.writeServiceRestartEvent(mDetectorType, - HOTWORD_DETECTION_SERVICE_RESTARTED__REASON__AUDIO_SERVICE_DIED); + HOTWORD_DETECTION_SERVICE_RESTARTED__REASON__AUDIO_SERVICE_DIED, + mVoiceInteractionServiceUid); } } @@ -333,13 +335,14 @@ final class HotwordDetectionConnection { try { mCallback.onStatusReported(status); HotwordMetricsLogger.writeServiceInitResultEvent(mDetectorType, - initResultMetricsResult); + initResultMetricsResult, mVoiceInteractionServiceUid); } catch (RemoteException e) { // TODO: Add a new atom for RemoteException case, the error doesn't very // correct here Slog.w(TAG, "Failed to report initialization status: " + e); HotwordMetricsLogger.writeServiceInitResultEvent(mDetectorType, - METRICS_INIT_CALLBACK_STATE_ERROR); + METRICS_INIT_CALLBACK_STATE_ERROR, + mVoiceInteractionServiceUid); } } }; @@ -362,11 +365,12 @@ final class HotwordDetectionConnection { try { mCallback.onStatusReported(INITIALIZATION_STATUS_UNKNOWN); HotwordMetricsLogger.writeServiceInitResultEvent(mDetectorType, - METRICS_INIT_UNKNOWN_TIMEOUT); + METRICS_INIT_UNKNOWN_TIMEOUT, mVoiceInteractionServiceUid); } catch (RemoteException e) { Slog.w(TAG, "Failed to report initialization status UNKNOWN", e); HotwordMetricsLogger.writeServiceInitResultEvent(mDetectorType, - METRICS_INIT_CALLBACK_STATE_ERROR); + METRICS_INIT_CALLBACK_STATE_ERROR, + mVoiceInteractionServiceUid); } } else if (err != null) { Slog.w(TAG, "Failed to update state: " + err); @@ -469,12 +473,14 @@ final class HotwordDetectionConnection { synchronized (mLock) { HotwordMetricsLogger.writeKeyphraseTriggerEvent( mDetectorType, - HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__DETECTED); + HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__DETECTED, + mVoiceInteractionServiceUid); if (!mPerformingSoftwareHotwordDetection) { Slog.i(TAG, "Hotword detection has already completed"); HotwordMetricsLogger.writeKeyphraseTriggerEvent( mDetectorType, - METRICS_KEYPHRASE_TRIGGERED_DETECT_UNEXPECTED_CALLBACK); + METRICS_KEYPHRASE_TRIGGERED_DETECT_UNEXPECTED_CALLBACK, + mVoiceInteractionServiceUid); return; } mPerformingSoftwareHotwordDetection = false; @@ -483,7 +489,8 @@ final class HotwordDetectionConnection { } catch (SecurityException e) { HotwordMetricsLogger.writeKeyphraseTriggerEvent( mDetectorType, - METRICS_KEYPHRASE_TRIGGERED_DETECT_SECURITY_EXCEPTION); + METRICS_KEYPHRASE_TRIGGERED_DETECT_SECURITY_EXCEPTION, + mVoiceInteractionServiceUid); mSoftwareCallback.onError(); return; } @@ -512,7 +519,8 @@ final class HotwordDetectionConnection { } HotwordMetricsLogger.writeKeyphraseTriggerEvent( mDetectorType, - HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__REJECTED); + HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__REJECTED, + mVoiceInteractionServiceUid); // onRejected isn't allowed here, and we are not expecting it. } }; @@ -660,12 +668,14 @@ final class HotwordDetectionConnection { } HotwordMetricsLogger.writeKeyphraseTriggerEvent( mDetectorType, - HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__DETECTED); + HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__DETECTED, + mVoiceInteractionServiceUid); if (!mValidatingDspTrigger) { Slog.i(TAG, "Ignoring #onDetected due to a process restart"); HotwordMetricsLogger.writeKeyphraseTriggerEvent( mDetectorType, - METRICS_KEYPHRASE_TRIGGERED_DETECT_UNEXPECTED_CALLBACK); + METRICS_KEYPHRASE_TRIGGERED_DETECT_UNEXPECTED_CALLBACK, + mVoiceInteractionServiceUid); return; } mValidatingDspTrigger = false; @@ -675,7 +685,8 @@ final class HotwordDetectionConnection { Slog.i(TAG, "Ignoring #onDetected due to a SecurityException", e); HotwordMetricsLogger.writeKeyphraseTriggerEvent( mDetectorType, - METRICS_KEYPHRASE_TRIGGERED_DETECT_SECURITY_EXCEPTION); + METRICS_KEYPHRASE_TRIGGERED_DETECT_SECURITY_EXCEPTION, + mVoiceInteractionServiceUid); externalCallback.onError(CALLBACK_ONDETECTED_GOT_SECURITY_EXCEPTION); return; } @@ -708,12 +719,14 @@ final class HotwordDetectionConnection { } HotwordMetricsLogger.writeKeyphraseTriggerEvent( mDetectorType, - HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__REJECTED); + HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__REJECTED, + mVoiceInteractionServiceUid); if (!mValidatingDspTrigger) { Slog.i(TAG, "Ignoring #onRejected due to a process restart"); HotwordMetricsLogger.writeKeyphraseTriggerEvent( mDetectorType, - METRICS_KEYPHRASE_TRIGGERED_REJECT_UNEXPECTED_CALLBACK); + METRICS_KEYPHRASE_TRIGGERED_REJECT_UNEXPECTED_CALLBACK, + mVoiceInteractionServiceUid); return; } mValidatingDspTrigger = false; @@ -727,21 +740,20 @@ final class HotwordDetectionConnection { synchronized (mLock) { mValidatingDspTrigger = true; - mRemoteHotwordDetectionService.run( - service -> { - // TODO: avoid allocate every time - mCancellationKeyPhraseDetectionFuture = mScheduledExecutorService.schedule( - () -> HotwordMetricsLogger - .writeKeyphraseTriggerEvent(mDetectorType, - HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__DETECT_TIMEOUT), - VALIDATION_TIMEOUT_MILLIS, - TimeUnit.MILLISECONDS); - service.detectFromDspSource( - recognitionEvent, - recognitionEvent.getCaptureFormat(), - VALIDATION_TIMEOUT_MILLIS, - internalCallback); - }); + mRemoteHotwordDetectionService.run(service -> { + // TODO: avoid allocate every time + mCancellationKeyPhraseDetectionFuture = mScheduledExecutorService.schedule( + () -> HotwordMetricsLogger.writeKeyphraseTriggerEvent(mDetectorType, + HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__DETECT_TIMEOUT, + mVoiceInteractionServiceUid), + VALIDATION_TIMEOUT_MILLIS, + TimeUnit.MILLISECONDS); + service.detectFromDspSource( + recognitionEvent, + recognitionEvent.getCaptureFormat(), + VALIDATION_TIMEOUT_MILLIS, + internalCallback); + }); } } @@ -789,7 +801,8 @@ final class HotwordDetectionConnection { mCallback.onRejected(new HotwordRejectedResult.Builder().build()); HotwordMetricsLogger.writeKeyphraseTriggerEvent( mDetectorType, - HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__REJECTED_FROM_RESTART); + HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__REJECTED_FROM_RESTART, + mVoiceInteractionServiceUid); } catch (RemoteException e) { Slog.w(TAG, "Failed to call #rejected"); } @@ -835,11 +848,13 @@ final class HotwordDetectionConnection { private SoundTrigger.KeyphraseRecognitionEvent mRecognitionEvent; private final HotwordDetectionConnection mHotwordDetectionConnection; private final IHotwordRecognitionStatusCallback mExternalCallback; + private final int mVoiceInteractionServiceUid; SoundTriggerCallback(IHotwordRecognitionStatusCallback callback, - HotwordDetectionConnection connection) { + HotwordDetectionConnection connection, int uid) { mHotwordDetectionConnection = connection; mExternalCallback = callback; + mVoiceInteractionServiceUid = uid; } @Override @@ -852,14 +867,16 @@ final class HotwordDetectionConnection { if (useHotwordDetectionService) { HotwordMetricsLogger.writeKeyphraseTriggerEvent( HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__DETECTOR_TYPE__TRUSTED_DETECTOR_DSP, - HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__KEYPHRASE_TRIGGER); + HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__KEYPHRASE_TRIGGER, + mVoiceInteractionServiceUid); mRecognitionEvent = recognitionEvent; mHotwordDetectionConnection.detectFromDspSource( recognitionEvent, mExternalCallback); } else { HotwordMetricsLogger.writeKeyphraseTriggerEvent( HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__DETECTOR_TYPE__NORMAL_DETECTOR, - HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__KEYPHRASE_TRIGGER); + HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__KEYPHRASE_TRIGGER, + mVoiceInteractionServiceUid); mExternalCallback.onKeyphraseDetected(recognitionEvent, null); } } diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/HotwordMetricsLogger.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/HotwordMetricsLogger.java index 940aed34b7fba..61c18be6f1333 100644 --- a/services/voiceinteraction/java/com/android/server/voiceinteraction/HotwordMetricsLogger.java +++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/HotwordMetricsLogger.java @@ -64,28 +64,28 @@ public final class HotwordMetricsLogger { /** * Logs information related to hotword detection service init result. */ - public static void writeServiceInitResultEvent(int detectorType, int result) { + public static void writeServiceInitResultEvent(int detectorType, int result, int uid) { int metricsDetectorType = getInitMetricsDetectorType(detectorType); FrameworkStatsLog.write(FrameworkStatsLog.HOTWORD_DETECTION_SERVICE_INIT_RESULT_REPORTED, - metricsDetectorType, result); + metricsDetectorType, result, uid); } /** * Logs information related to hotword detection service restarting. */ - public static void writeServiceRestartEvent(int detectorType, int reason) { + public static void writeServiceRestartEvent(int detectorType, int reason, int uid) { int metricsDetectorType = getRestartMetricsDetectorType(detectorType); FrameworkStatsLog.write(FrameworkStatsLog.HOTWORD_DETECTION_SERVICE_RESTARTED, - metricsDetectorType, reason); + metricsDetectorType, reason, uid); } /** * Logs information related to keyphrase trigger. */ - public static void writeKeyphraseTriggerEvent(int detectorType, int result) { + public static void writeKeyphraseTriggerEvent(int detectorType, int result, int uid) { int metricsDetectorType = getKeyphraseMetricsDetectorType(detectorType); FrameworkStatsLog.write(FrameworkStatsLog.HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED, - metricsDetectorType, result); + metricsDetectorType, result, uid); } /** diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerServiceImpl.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerServiceImpl.java index f90fbb249f5dc..140dc2b016e8d 100644 --- a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerServiceImpl.java +++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerServiceImpl.java @@ -711,7 +711,7 @@ class VoiceInteractionManagerServiceImpl implements VoiceInteractionSessionConne Slog.d(TAG, "createSoundTriggerCallbackLocked"); } return new HotwordDetectionConnection.SoundTriggerCallback(callback, - mHotwordDetectionConnection); + mHotwordDetectionConnection, mInfo.getServiceInfo().applicationInfo.uid); } private static ServiceInfo getServiceInfoLocked(@NonNull ComponentName componentName,