• 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} with some utility information.
31  *
32  * To enable any tracing log, execute the following command:
33  * $ adb shell setprop log.tag.LAUNCHER_TRACE VERBOSE
34  * $ adb shell setprop log.tag.TAGNAME VERBOSE
35  */
36 public class TraceHelper {
37 
38     private static final boolean ENABLED = isLoggable("LAUNCHER_TRACE", VERBOSE);
39 
40     private static final boolean SYSTEM_TRACE = ENABLED;
41     private static final ArrayMap<String, MutableLong> sUpTimes = ENABLED ? new ArrayMap<>() : null;
42 
beginSection(String sectionName)43     public static void beginSection(String sectionName) {
44         if (ENABLED) {
45             synchronized (sUpTimes) {
46                 MutableLong time = sUpTimes.get(sectionName);
47                 if (time == null) {
48                     time = new MutableLong(isLoggable(sectionName, VERBOSE) ? 0 : -1);
49                     sUpTimes.put(sectionName, time);
50                 }
51                 if (time.value >= 0) {
52                     if (SYSTEM_TRACE) {
53                         Trace.beginSection(sectionName);
54                     }
55                     time.value = SystemClock.uptimeMillis();
56                 }
57             }
58         }
59     }
60 
partitionSection(String sectionName, String partition)61     public static void partitionSection(String sectionName, String partition) {
62         if (ENABLED) {
63             synchronized (sUpTimes) {
64                 MutableLong time = sUpTimes.get(sectionName);
65                 if (time != null && time.value >= 0) {
66 
67                     if (SYSTEM_TRACE) {
68                         Trace.endSection();
69                         Trace.beginSection(sectionName);
70                     }
71 
72                     long now = SystemClock.uptimeMillis();
73                     Log.d(sectionName, partition + " : " + (now - time.value));
74                     time.value = now;
75                 }
76             }
77         }
78     }
79 
endSection(String sectionName)80     public static void endSection(String sectionName) {
81         if (ENABLED) {
82             endSection(sectionName, "End");
83         }
84     }
85 
endSection(String sectionName, String msg)86     public static void endSection(String sectionName, String msg) {
87         if (ENABLED) {
88             synchronized (sUpTimes) {
89                 MutableLong time = sUpTimes.get(sectionName);
90                 if (time != null && time.value >= 0) {
91                     if (SYSTEM_TRACE) {
92                         Trace.endSection();
93                     }
94                     Log.d(sectionName, msg + " : " + (SystemClock.uptimeMillis() - time.value));
95                 }
96             }
97         }
98     }
99 }
100