• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 java.util.concurrent.ThreadLocalRandom;
23 
24 /** Utility class providing methods for using {@link android.os.Trace}. */
25 public final class Tracing {
26 
27     public static final String RUN_AD_SELECTION = "RunOnDeviceAdSelection";
28     public static final String PERSIST_AD_SELECTION = "PersistOnDeviceAdSelection";
29     public static final String GET_BUYERS_CUSTOM_AUDIENCE = "GetBuyersCustomAudience";
30     public static final String VALIDATE_REQUEST = "ValidateRequest";
31     public static final String GET_BUYER_DECISION_LOGIC = "GetBuyerDecisionLogic";
32     public static final String GET_TRUSTED_BIDDING_SIGNALS = "GetTrustedBiddingSignals";
33     public static final String RUN_BIDDING = "RunBidding";
34     public static final String RUN_BIDDING_PER_CA = "RunBiddingPerCustomAudience";
35     public static final String RUN_AD_SCORING = "RunAdScoring";
36     public static final String GET_AD_SELECTION_LOGIC = "GetAdSelectionLogic";
37     public static final String GET_TRUSTED_SCORING_SIGNALS = "GetTrustedScoringSignals";
38     public static final String SCORE_AD = "ScoreAd";
39     public static final String RUN_OUTCOME_SELECTION = "RunAdOutcomeSelection";
40     public static final String GENERATE_BIDS = "GenerateBids";
41     public static final String FETCH_PAYLOAD = "FetchPayload";
42     public static final String CACHE_GET = "CacheGet";
43     public static final String CACHE_PUT = "CachePut";
44     public static final String HTTP_REQUEST = "HttpRequest";
45     public static final String JSSCRIPTENGINE_CREATE_ISOLATE = "JSScriptEngine#createIsolate";
46     public static final String JSSCRIPTENGINE_EVALUATE_ON_SANDBOX =
47             "JSScriptEngine#evaluateOnSandbox";
48     public static final String JSSCRIPTENGINE_CLOSE_ISOLATE = "JSScriptEngine#closeIsolate";
49 
50     /**
51      * Begins an asynchronous trace and generates random cookie.
52      *
53      * @param sectionName used to identify trace type.
54      * @return unique cookie for identifying trace.
55      */
beginAsyncSection(@onNull String sectionName)56     public static int beginAsyncSection(@NonNull String sectionName) {
57         if (!Trace.isEnabled()) {
58             return -1;
59         }
60         int traceCookie = ThreadLocalRandom.current().nextInt();
61         Trace.beginAsyncSection(sectionName, traceCookie);
62         return traceCookie;
63     }
64 
65     /**
66      * Ends an asynchronous trace section.
67      *
68      * @param sectionName used to identify trace type.
69      * @param traceCookie unique cookie for identifying trace.
70      */
endAsyncSection(@onNull String sectionName, int traceCookie)71     public static void endAsyncSection(@NonNull String sectionName, int traceCookie) {
72         Trace.endAsyncSection(sectionName, traceCookie);
73     }
74 }
75