1 /* 2 * Copyright (C) 2025 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.am; 18 19 import static com.android.internal.util.FrameworkStatsLog.BROADCAST_PROCESSED; 20 21 import android.annotation.NonNull; 22 import android.annotation.Nullable; 23 24 import com.android.internal.util.FrameworkStatsLog; 25 26 final class BroadcastProcessedEventRecord { 27 28 /** 29 * Minimum threshold for logging the broadcast processed event. 30 */ 31 private static final int MIN_THRESHOLD_FOR_LOGGING_TIME_MILLIS = 10; 32 33 @Nullable 34 private String mIntentAction; 35 36 private int mSenderUid; 37 38 private int mReceiverUid; 39 40 private int mNumberOfReceivers; 41 42 @NonNull 43 private String mReceiverProcessName; 44 45 private long mTotalBroadcastFinishTimeMillis; 46 47 private long mMaxReceiverFinishTimeMillis = Long.MIN_VALUE; 48 49 @NonNull 50 private int[] mBroadcastTypes; 51 52 @NonNull setBroadcastTypes(@onNull int[] broadcastTypes)53 public BroadcastProcessedEventRecord setBroadcastTypes(@NonNull int[] broadcastTypes) { 54 this.mBroadcastTypes = broadcastTypes; 55 return this; 56 } 57 58 @NonNull setReceiverProcessName( @onNull String receiverProcessName)59 public BroadcastProcessedEventRecord setReceiverProcessName( 60 @NonNull String receiverProcessName) { 61 mReceiverProcessName = receiverProcessName; 62 return this; 63 } 64 65 @NonNull setIntentAction(@ullable String intentAction)66 public BroadcastProcessedEventRecord setIntentAction(@Nullable String intentAction) { 67 mIntentAction = intentAction; 68 return this; 69 } 70 71 @NonNull setSenderUid(int uid)72 public BroadcastProcessedEventRecord setSenderUid(int uid) { 73 mSenderUid = uid; 74 return this; 75 } 76 77 @NonNull setReceiverUid(int uid)78 public BroadcastProcessedEventRecord setReceiverUid(int uid) { 79 mReceiverUid = uid; 80 return this; 81 } 82 addReceiverFinishTime(long timeMillis)83 public void addReceiverFinishTime(long timeMillis) { 84 mTotalBroadcastFinishTimeMillis += timeMillis; 85 mMaxReceiverFinishTimeMillis = Math.max(mMaxReceiverFinishTimeMillis, timeMillis); 86 mNumberOfReceivers++; 87 } 88 89 @Nullable getIntentActionForTest()90 String getIntentActionForTest() { 91 return mIntentAction; 92 } 93 getSenderUidForTest()94 int getSenderUidForTest() { 95 return mSenderUid; 96 } 97 getReceiverUidForTest()98 int getReceiverUidForTest() { 99 return mReceiverUid; 100 } 101 getNumberOfReceiversForTest()102 int getNumberOfReceiversForTest() { 103 return mNumberOfReceivers; 104 } 105 106 @NonNull getReceiverProcessNameForTest()107 String getReceiverProcessNameForTest() { 108 return mReceiverProcessName; 109 } 110 getTotalBroadcastFinishTimeMillisForTest()111 long getTotalBroadcastFinishTimeMillisForTest() { 112 return mTotalBroadcastFinishTimeMillis; 113 } 114 getMaxReceiverFinishTimeMillisForTest()115 long getMaxReceiverFinishTimeMillisForTest() { 116 return mMaxReceiverFinishTimeMillis; 117 } 118 119 @NonNull getBroadcastTypesForTest()120 int[] getBroadcastTypesForTest() { 121 return mBroadcastTypes; 122 } 123 logToStatsD()124 public void logToStatsD() { 125 // We do not care about the processes where total time to process the 126 // broadcast is less than 10ms/ are quick to process the broadcast. 127 if (mTotalBroadcastFinishTimeMillis <= MIN_THRESHOLD_FOR_LOGGING_TIME_MILLIS) { 128 return; 129 } 130 131 FrameworkStatsLog.write( 132 BROADCAST_PROCESSED, 133 mIntentAction, 134 mSenderUid, 135 mReceiverUid, 136 mNumberOfReceivers, 137 mReceiverProcessName, 138 mTotalBroadcastFinishTimeMillis, 139 mMaxReceiverFinishTimeMillis, 140 mBroadcastTypes); 141 } 142 } 143