1 /* 2 * Copyright (C) 2022 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.adservices.service.profiling; 18 19 import android.annotation.NonNull; 20 import android.os.Trace; 21 22 import com.android.adservices.LogUtil; 23 24 import java.io.IOException; 25 import java.util.concurrent.ThreadLocalRandom; 26 27 /** Utility class providing methods for using {@link android.os.Trace}. */ 28 public final class Tracing { 29 public static final String DB_CUSTOM_AUDIENCE_TO_JSON = "DBCustomAudience#toJson"; 30 public static final String DB_CUSTOM_AUDIENCE_FROM_JSON = "DBCustomAudience#fromJson"; 31 public static final String FILTERER_FILTER_CA = "FrequencyCapAdFilterer#FilterCustomAudiences"; 32 public static final String FILTERER_FOR_EACH_CA = 33 "FrequencyCapAdFilterer#ForEachCustomAudience"; 34 public static final String FILTERER_FOR_EACH_AD = "FrequencyCapAdFilterer#ForEachAd"; 35 public static final String FILTERER_FREQUENCY_CAP = 36 "FrequencyCapAdFilterer#doesAdPassFrequencyCapFilters"; 37 public static final String FILTERER_FREQUENCY_CAP_WIN = 38 "FrequencyCapAdFilterer#doesAdPassFrequencyCapFiltersForWinType"; 39 public static final String FREQUENCY_CAP_GET_NUM_EVENTS_CA = 40 "FrequencyCapDao#getNumEventsForCustomAudienceAfterTime"; 41 public static final String FREQUENCY_CAP_GET_NUM_EVENTS_BUYER = 42 "FrequencyCapDao#getNumEventsForBuyerAfterTime"; 43 public static final String FILTERER_FREQUENCY_CAP_NON_WIN = 44 "FrequencyCapAdFilterer#doesAdPassFrequencyCapFiltersForNonWinType"; 45 public static final String FILTERER_FILTER_CONTEXTUAL = 46 "FrequencyCapAdFilterer#FilterContextualAds"; 47 public static final String RUN_AD_SELECTION = "RunOnDeviceAdSelection"; 48 public static final String PERSIST_AD_SELECTION = "PersistOnDeviceAdSelection"; 49 public static final String GET_BUYERS_CUSTOM_AUDIENCE = "GetBuyersCustomAudience"; 50 public static final String VALIDATE_REQUEST = "ValidateRequest"; 51 public static final String GET_BUYER_DECISION_LOGIC = "GetBuyerDecisionLogic"; 52 public static final String GET_TRUSTED_BIDDING_SIGNALS = "GetTrustedBiddingSignals"; 53 public static final String RUN_BIDDING = "RunBidding"; 54 public static final String RUN_BIDDING_PER_CA = "RunBiddingPerCustomAudience"; 55 public static final String RUN_AD_SCORING = "RunAdScoring"; 56 public static final String GET_AD_SELECTION_LOGIC = "GetAdSelectionLogic"; 57 public static final String GET_TRUSTED_SCORING_SIGNALS = "GetTrustedScoringSignals"; 58 public static final String SCORE_AD = "ScoreAd"; 59 public static final String RUN_OUTCOME_SELECTION = "RunAdOutcomeSelection"; 60 public static final String GENERATE_BIDS = "GenerateBids"; 61 public static final String FETCH_PAYLOAD = "FetchPayload"; 62 public static final String CACHE_GET = "CacheGet"; 63 public static final String CACHE_PUT = "CachePut"; 64 public static final String HTTP_REQUEST = "HttpRequest"; 65 public static final String JSSCRIPTENGINE_CREATE_ISOLATE = "JSScriptEngine#createIsolate"; 66 public static final String JSSCRIPTENGINE_EVALUATE_ON_SANDBOX = 67 "JSScriptEngine#evaluateOnSandbox"; 68 public static final String JSSCRIPTENGINE_CLOSE_ISOLATE = "JSScriptEngine#closeIsolate"; 69 public static final String PERSIST_AD_SELECTION_RESULT = 70 "AdSelectionServiceImpl#persistAdSelectionResult"; 71 public static final String ORCHESTRATE_PERSIST_AD_SELECTION_RESULT = 72 "PersistAdSelectionResultRunner#orchestratePersistAdSelectionResultRunner"; 73 public static final String PERSIST_AUCTION_RESULTS = 74 "PersistAdSelectionResultRunner#persistAuctionResults"; 75 public static final String OHTTP_DECRYPT_BYTES = "PersistAdSelectionResultRunner#decryptBytes"; 76 public static final String PARSE_AD_SELECTION_RESULT = 77 "PersistAdSelectionResultRunner#parseAdSelectionResult"; 78 public static final String GET_AD_SELECTION_DATA = "AdSelectionServiceImpl#getAdSelectionData"; 79 public static final String GET_BUYERS_CA = "BuyerInputGenerator#getBuyersCustomAudience"; 80 public static final String GET_FILTERED_BUYERS_CA = 81 "BuyerInputGenerator#getFilteredCustomAudiences"; 82 public static final String GET_BUYERS_PS = "BuyerInputGenerator#getBuyersProtectedSignals"; 83 public static final String GET_COMPRESSED_BUYERS_INPUTS = 84 "BuyerInputGenerator#getCompressedBuyerInputs"; 85 public static final String AUCTION_SERVER_GZIP_COMPRESS = 86 "AuctionServerDataCompressorGzip#compress"; 87 public static final String FORMAT_PAYLOAD_V0 = "AuctionServerPayloadFormatterV0#apply"; 88 public static final String FORMAT_PAYLOAD_EXCESSIVE_MAX_SIZE = 89 "AuctionServerPayloadFormatterExcessiveMaxSize#apply"; 90 public static final String CREATE_BUYER_INPUTS = "BuyerInputGenerator#createBuyerInputs"; 91 public static final String CREATE_GET_AD_SELECTION_DATA_PAYLOAD = 92 "GetAdSelectionDataRunner#createPayload"; 93 public static final String ORCHESTRATE_GET_AD_SELECTION_DATA = 94 "GetAdSelectionDataRunner#orchestrateGetAdSelectionDataRunner"; 95 public static final String PERSIST_AD_SELECTION_ID_REQUEST = 96 "GetAdSelectionDataRunner#persistAdSelectionIdRequest"; 97 public static final String GET_LATEST_OHTTP_KEY_CONFIG = 98 "AdSelectionEncryptionKeyManager#getLatestOhttpKeyConfigOfType"; 99 public static final String CREATE_AND_SERIALIZE_REQUEST = 100 "ObliviousHttpEncryptorImpl#createAndSerializeRequest"; 101 public static final String OHTTP_ENCRYPT_BYTES = "ObliviousHttpEncryptorImpl#encryptBytes"; 102 103 private static final String PERFETTO_TRIGGER_COMMAND = "/system/bin/trigger_perfetto"; 104 105 /** 106 * Begins an asynchronous trace and generates random cookie. 107 * 108 * @param sectionName used to identify trace type. 109 * @return unique cookie for identifying trace. 110 */ beginAsyncSection(@onNull String sectionName)111 public static int beginAsyncSection(@NonNull String sectionName) { 112 if (!Trace.isEnabled()) { 113 return -1; 114 } 115 int traceCookie = ThreadLocalRandom.current().nextInt(); 116 Trace.beginAsyncSection(sectionName, traceCookie); 117 return traceCookie; 118 } 119 120 /** 121 * Ends an asynchronous trace section. 122 * 123 * @param sectionName used to identify trace type. 124 * @param traceCookie unique cookie for identifying trace. 125 */ endAsyncSection(@onNull String sectionName, int traceCookie)126 public static void endAsyncSection(@NonNull String sectionName, int traceCookie) { 127 Trace.endAsyncSection(sectionName, traceCookie); 128 } 129 130 /** 131 * Notifies perfetto to start AOT given a trace event. This can be an expensive operation so 132 * only use it to record failures but not general trace events. 133 * 134 * @param triggerEvent name of Perfetto trigger event. 135 */ triggerPerfetto(String triggerEvent)136 public static void triggerPerfetto(String triggerEvent) { 137 try { 138 ProcessBuilder pb = new ProcessBuilder(PERFETTO_TRIGGER_COMMAND, triggerEvent); 139 LogUtil.d("Triggering perfetto with " + triggerEvent); 140 pb.start(); 141 } catch (IOException e) { 142 LogUtil.e("Failed to trigger perfetto with " + triggerEvent, e); 143 } 144 } 145 } 146