1 /*
2  * Copyright (C) 2024 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.traceur;
18 
19 import android.os.Build;
20 import android.util.ArraySet;
21 
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.List;
25 import java.util.Set;
26 
27 public class PresetTraceConfigs {
28 
29     private static final List<String> DEFAULT_TRACE_TAGS = Arrays.asList(
30             "aidl", "am", "binder_driver", "camera", "dalvik", "disk", "freq",
31             "gfx", "hal", "idle", "input", "memory", "memreclaim", "network", "power",
32             "res", "sched", "ss", "sync", "thermal", "view", "webview", "wm", "workq");
33 
34     private static final List<String> PERFORMANCE_TRACE_TAGS = DEFAULT_TRACE_TAGS;
35     private static final List<String> UI_TRACE_TAGS = DEFAULT_TRACE_TAGS;
36 
37     private static final List<String> THERMAL_TRACE_TAGS = Arrays.asList(
38             "aidl", "am", "binder_driver", "camera", "dalvik", "disk", "freq",
39             "gfx", "hal", "idle", "input", "memory", "memreclaim", "network", "power",
40             "res", "sched", "ss", "sync", "thermal", "thermal_tj", "view", "webview",
41             "wm", "workq");
42 
43     private static final List<String> BATTERY_TRACE_TAGS = Arrays.asList(
44             "aidl", "am", "binder_driver", "network", "nnapi",
45             "pm", "power", "ss", "thermal", "wm");
46 
47     private static final List<String> USER_BUILD_DISABLED_TRACE_TAGS = Arrays.asList(
48             "workq", "sync");
49 
50     private static Set<String> mDefaultTagList = null;
51     private static Set<String> mPerformanceTagList = null;
52     private static Set<String> mBatteryTagList = null;
53     private static Set<String> mThermalTagList = null;
54     private static Set<String> mUiTagList = null;
55 
getDefaultTags()56     public static Set<String> getDefaultTags() {
57         if (mDefaultTagList == null) {
58             mDefaultTagList = new ArraySet<String>(DEFAULT_TRACE_TAGS);
59             updateTagsIfUserBuild(mDefaultTagList);
60         }
61         return mDefaultTagList;
62     }
63 
getPerformanceTags()64     public static Set<String> getPerformanceTags() {
65         if (mPerformanceTagList == null) {
66             mPerformanceTagList = new ArraySet<String>(PERFORMANCE_TRACE_TAGS);
67             updateTagsIfUserBuild(mPerformanceTagList);
68         }
69         return mPerformanceTagList;
70     }
71 
getBatteryTags()72     public static Set<String> getBatteryTags() {
73         if (mBatteryTagList == null) {
74             mBatteryTagList = new ArraySet<String>(BATTERY_TRACE_TAGS);
75             updateTagsIfUserBuild(mBatteryTagList);
76         }
77         return mBatteryTagList;
78     }
79 
getThermalTags()80     public static Set<String> getThermalTags() {
81         if (mThermalTagList == null) {
82             mThermalTagList = new ArraySet<String>(THERMAL_TRACE_TAGS);
83             updateTagsIfUserBuild(mThermalTagList);
84         }
85         return mThermalTagList;
86     }
87 
getUiTags()88     public static Set<String> getUiTags() {
89         if (mUiTagList == null) {
90             mUiTagList = new ArraySet<String>(UI_TRACE_TAGS);
91             updateTagsIfUserBuild(mUiTagList);
92         }
93         return mUiTagList;
94     }
95 
updateTagsIfUserBuild(Collection<String> tags)96     private static void updateTagsIfUserBuild(Collection<String> tags) {
97         if (Build.TYPE.equals("user")) {
98             tags.removeAll(USER_BUILD_DISABLED_TRACE_TAGS);
99         }
100     }
101 
102     static class TraceOptions {
103         final int bufferSizeKb;
104         final boolean winscope;
105         final boolean apps;
106         final boolean longTrace;
107         final boolean attachToBugreport;
108         final int maxLongTraceSizeMb;
109         final int maxLongTraceDurationMinutes;
110 
TraceOptions(int bufferSizeKb, boolean winscope, boolean apps, boolean longTrace, boolean attachToBugreport, int maxLongTraceSizeMb, int maxLongTraceDurationMinutes)111         TraceOptions(int bufferSizeKb, boolean winscope, boolean apps, boolean longTrace,
112                 boolean attachToBugreport, int maxLongTraceSizeMb,
113                 int maxLongTraceDurationMinutes) {
114             this.bufferSizeKb = bufferSizeKb;
115             this.winscope = winscope;
116             this.apps = apps;
117             this.longTrace = longTrace;
118             this.attachToBugreport = attachToBugreport;
119             this.maxLongTraceSizeMb = maxLongTraceSizeMb;
120             this.maxLongTraceDurationMinutes = maxLongTraceDurationMinutes;
121         }
122     }
123 
124     // Keep in sync with default sizes and durations in buffer_sizes.xml.
125     private static final int DEFAULT_BUFFER_SIZE_KB = 16384;
126     private static final int DEFAULT_MAX_LONG_TRACE_SIZE_MB = 10240;
127     private static final int DEFAULT_MAX_LONG_TRACE_DURATION_MINUTES = 30;
128 
129     private static final TraceOptions DEFAULT_TRACE_OPTIONS =
130             new TraceOptions(DEFAULT_BUFFER_SIZE_KB,
131                     /* winscope */ false,
132                     /* apps */ true,
133                     /* longTrace */ false,
134                     /* attachToBugreport */ true,
135                     DEFAULT_MAX_LONG_TRACE_SIZE_MB,
136                     DEFAULT_MAX_LONG_TRACE_DURATION_MINUTES);
137 
138     private static final TraceOptions PERFORMANCE_TRACE_OPTIONS =
139             new TraceOptions(DEFAULT_BUFFER_SIZE_KB,
140                     /* winscope */ false,
141                     /* apps */ true,
142                     /* longTrace */ false,
143                     /* attachToBugreport */ true,
144                     DEFAULT_MAX_LONG_TRACE_SIZE_MB,
145                     DEFAULT_MAX_LONG_TRACE_DURATION_MINUTES);
146 
147     private static final TraceOptions BATTERY_TRACE_OPTIONS =
148             new TraceOptions(DEFAULT_BUFFER_SIZE_KB,
149                     /* winscope */ false,
150                     /* apps */ false,
151                     /* longTrace */ true,
152                     /* attachToBugreport */ true,
153                     DEFAULT_MAX_LONG_TRACE_SIZE_MB,
154                     DEFAULT_MAX_LONG_TRACE_DURATION_MINUTES);
155 
156     private static final TraceOptions THERMAL_TRACE_OPTIONS =
157             new TraceOptions(DEFAULT_BUFFER_SIZE_KB,
158                     /* winscope */ false,
159                     /* apps */ true,
160                     /* longTrace */ true,
161                     /* attachToBugreport */ true,
162                     DEFAULT_MAX_LONG_TRACE_SIZE_MB,
163                     DEFAULT_MAX_LONG_TRACE_DURATION_MINUTES);
164 
165     private static final TraceOptions UI_TRACE_OPTIONS =
166             new TraceOptions(DEFAULT_BUFFER_SIZE_KB,
167                     /* winscope */ true,
168                     /* apps */ true,
169                     /* longTrace */ true,
170                     /* attachToBugreport */ true,
171                     DEFAULT_MAX_LONG_TRACE_SIZE_MB,
172                     DEFAULT_MAX_LONG_TRACE_DURATION_MINUTES);
173 
getDefaultOptions()174     public static TraceOptions getDefaultOptions() {
175         return DEFAULT_TRACE_OPTIONS;
176     }
177 
getPerformanceOptions()178     public static TraceOptions getPerformanceOptions() {
179         return PERFORMANCE_TRACE_OPTIONS;
180     }
181 
getBatteryOptions()182     public static TraceOptions getBatteryOptions() {
183         return BATTERY_TRACE_OPTIONS;
184     }
185 
getThermalOptions()186     public static TraceOptions getThermalOptions() {
187         return THERMAL_TRACE_OPTIONS;
188     }
189 
getUiOptions()190     public static TraceOptions getUiOptions() {
191         return UI_TRACE_OPTIONS;
192     }
193 
194 }
195