• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 package com.android.launcher3.util;
17 
18 import static android.util.Log.VERBOSE;
19 import static android.util.Log.isLoggable;
20 
21 import android.os.SystemClock;
22 import android.os.Trace;
23 import android.util.ArrayMap;
24 import android.util.Log;
25 import android.util.MutableLong;
26 
27 import com.android.launcher3.config.FeatureFlags;
28 
29 /**
30  * A wrapper around {@link Trace} to allow easier proguarding for production builds.
31  *
32  * To enable any tracing log, execute the following command:
33  * $ adb shell setprop log.tag.TAGNAME VERBOSE
34  */
35 public class TraceHelper {
36 
37     private static final boolean ENABLED = FeatureFlags.IS_DOGFOOD_BUILD;
38 
39     private static final boolean SYSTEM_TRACE = false;
40     private static final ArrayMap<String, MutableLong> sUpTimes = ENABLED ? new ArrayMap<>() : null;
41 
beginSection(String sectionName)42     public static void beginSection(String sectionName) {
43         if (ENABLED) {
44             MutableLong time = sUpTimes.get(sectionName);
45             if (time == null) {
46                 time = new MutableLong(isLoggable(sectionName, VERBOSE) ? 0 : -1);
47                 sUpTimes.put(sectionName, time);
48             }
49             if (time.value >= 0) {
50                 if (SYSTEM_TRACE) {
51                     Trace.beginSection(sectionName);
52                 }
53                 time.value = SystemClock.uptimeMillis();
54             }
55         }
56     }
57 
partitionSection(String sectionName, String partition)58     public static void partitionSection(String sectionName, String partition) {
59         if (ENABLED) {
60             MutableLong time = sUpTimes.get(sectionName);
61             if (time != null && time.value >= 0) {
62 
63                 if (SYSTEM_TRACE) {
64                     Trace.endSection();
65                     Trace.beginSection(sectionName);
66                 }
67 
68                 long now = SystemClock.uptimeMillis();
69                 Log.d(sectionName, partition + " : " + (now - time.value));
70                 time.value = now;
71             }
72         }
73     }
74 
endSection(String sectionName)75     public static void endSection(String sectionName) {
76         if (ENABLED) {
77             endSection(sectionName, "End");
78         }
79     }
80 
endSection(String sectionName, String msg)81     public static void endSection(String sectionName, String msg) {
82         if (ENABLED) {
83             MutableLong time = sUpTimes.get(sectionName);
84             if (time != null && time.value >= 0) {
85                 if (SYSTEM_TRACE) {
86                     Trace.endSection();
87                 }
88                 Log.d(sectionName, msg + " : " + (SystemClock.uptimeMillis() - time.value));
89             }
90         }
91     }
92 }
93