1 /* 2 * Copyright (C) 2023 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.credentials.metrics; 18 19 import java.util.LinkedHashMap; 20 import java.util.Map; 21 22 /** 23 * This handles metrics collected prior to any remote calls to providers. 24 * Some types are redundant across these metric collectors, but that has debug use-cases as 25 * these data-types are available at different moments of the flow (and typically, one can feed 26 * into the next). 27 */ 28 public class InitialPhaseMetric { 29 private static final String TAG = "InitialPhaseMetric"; 30 31 // The api being called, default set to unknown 32 private int mApiName = ApiName.UNKNOWN.getMetricCode(); 33 // The caller uid of the calling application, default to -1 34 private int mCallerUid = -1; 35 // The session id to unite multiple atom emits 36 private final int mSessionIdCaller; 37 38 // Raw timestamps in nanoseconds, *the only* one logged as such (i.e. 64 bits) since it is a 39 // reference point. 40 private long mCredentialServiceStartedTimeNanoseconds = -1; 41 42 // A reference point to give this object utility to capture latency. Can be directly handed 43 // over to the next latency object. 44 private long mCredentialServiceBeginQueryTimeNanoseconds = -1; 45 46 // Indicates if the origin was specified when making this API request 47 private boolean mOriginSpecified = false; 48 49 // Stores the deduped request information, particularly {"req":5} 50 private Map<String, Integer> mRequestCounts = new LinkedHashMap<>(); 51 52 // The session id of autofill if the request is from autofill, defaults to -1 53 private int mAutofillSessionId = -1; 54 55 // The request id of autofill if the request is from autofill, defaults to -1 56 private int mAutofillRequestId = -1; 57 58 // Indicates if this API call used the prepare flow, defaults to false 59 private boolean mApiUsedPrepareFlow = false; 60 61 InitialPhaseMetric(int sessionIdTrackOne)62 public InitialPhaseMetric(int sessionIdTrackOne) { 63 mSessionIdCaller = sessionIdTrackOne; 64 } 65 66 /* ---------- Latencies ---------- */ 67 68 /* -- Direct Latency Utility -- */ 69 getServiceStartToQueryLatencyMicroseconds()70 public int getServiceStartToQueryLatencyMicroseconds() { 71 return (int) ((mCredentialServiceStartedTimeNanoseconds 72 - mCredentialServiceBeginQueryTimeNanoseconds) / 1000); 73 } 74 75 /* -- Timestamps -- */ 76 setCredentialServiceStartedTimeNanoseconds( long credentialServiceStartedTimeNanoseconds )77 public void setCredentialServiceStartedTimeNanoseconds( 78 long credentialServiceStartedTimeNanoseconds 79 ) { 80 mCredentialServiceStartedTimeNanoseconds = credentialServiceStartedTimeNanoseconds; 81 } 82 setCredentialServiceBeginQueryTimeNanoseconds( long credentialServiceBeginQueryTimeNanoseconds)83 public void setCredentialServiceBeginQueryTimeNanoseconds( 84 long credentialServiceBeginQueryTimeNanoseconds) { 85 mCredentialServiceBeginQueryTimeNanoseconds = credentialServiceBeginQueryTimeNanoseconds; 86 } 87 getCredentialServiceStartedTimeNanoseconds()88 public long getCredentialServiceStartedTimeNanoseconds() { 89 return mCredentialServiceStartedTimeNanoseconds; 90 } 91 getCredentialServiceBeginQueryTimeNanoseconds()92 public long getCredentialServiceBeginQueryTimeNanoseconds() { 93 return mCredentialServiceBeginQueryTimeNanoseconds; 94 } 95 96 /* ------ ApiName ------ */ 97 setApiName(int apiName)98 public void setApiName(int apiName) { 99 mApiName = apiName; 100 } 101 getApiName()102 public int getApiName() { 103 return mApiName; 104 } 105 106 /* ------ CallerUid ------ */ 107 setCallerUid(int callerUid)108 public void setCallerUid(int callerUid) { 109 mCallerUid = callerUid; 110 } 111 getCallerUid()112 public int getCallerUid() { 113 return mCallerUid; 114 } 115 116 /* ------ SessionId ------ */ 117 getSessionIdCaller()118 public int getSessionIdCaller() { 119 return mSessionIdCaller; 120 } 121 122 /* ------ Count Request Class Types ------ */ 123 getCountRequestClassType()124 public int getCountRequestClassType() { 125 return mRequestCounts.size(); 126 } 127 128 /* ------ Origin Specified ------ */ 129 setOriginSpecified(boolean originSpecified)130 public void setOriginSpecified(boolean originSpecified) { 131 mOriginSpecified = originSpecified; 132 } 133 isOriginSpecified()134 public boolean isOriginSpecified() { 135 return mOriginSpecified; 136 } 137 138 /* ------ Autofill Integration ------ */ 139 setAutofillSessionId(int autofillSessionId)140 public void setAutofillSessionId(int autofillSessionId) { 141 mAutofillSessionId = autofillSessionId; 142 } 143 getAutofillSessionId()144 public int getAutofillSessionId() { 145 return mAutofillSessionId; 146 } 147 setAutofillRequestId(int autofillRequestId)148 public void setAutofillRequestId(int autofillRequestId) { 149 mAutofillRequestId = autofillRequestId; 150 } 151 getAutofillRequestId()152 public int getAutofillRequestId() { 153 return mAutofillRequestId; 154 } 155 156 /* ------ Unique Request Counts Map Information ------ */ 157 setRequestCounts(Map<String, Integer> requestCounts)158 public void setRequestCounts(Map<String, Integer> requestCounts) { 159 mRequestCounts = requestCounts; 160 } 161 162 /** 163 * Returns the unique, deduped, request classtypes for logging. 164 * @return a string array for deduped classtypes 165 */ getUniqueRequestStrings()166 public String[] getUniqueRequestStrings() { 167 String[] result = new String[mRequestCounts.keySet().size()]; 168 mRequestCounts.keySet().toArray(result); 169 return result; 170 } 171 172 /** 173 * Returns the unique, deduped, request classtype counts for logging. 174 * @return a string array for deduped classtype counts 175 */ getUniqueRequestCounts()176 public int[] getUniqueRequestCounts() { 177 return mRequestCounts.values().stream().mapToInt(Integer::intValue).toArray(); 178 } 179 180 /* ------ API Prepared ------ */ 181 setApiUsedPrepareFlow(boolean apiUsedPrepareFlow)182 public void setApiUsedPrepareFlow(boolean apiUsedPrepareFlow) { 183 mApiUsedPrepareFlow = apiUsedPrepareFlow; 184 } 185 186 /** 187 * @return a boolean indicating if this API call utilized a prepare flow 188 */ hasApiUsedPrepareFlow()189 public boolean hasApiUsedPrepareFlow() { 190 return mApiUsedPrepareFlow; 191 } 192 } 193