1 /*
2  * Copyright (C) 2023 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 android.os.profiling;
18 
19 import android.provider.DeviceConfig;
20 
21 /**
22  * Wrapper class for retrieving device configuration values.
23  */
24 public final class DeviceConfigHelper {
25 
26     // Begin section: Testing specific constants
27     // Values here can only be accessed with {@link #getTestBoolean}.
28 
29     // Namespace for testing only that is not registered on the server side
30     public static final String NAMESPACE_TESTING = "profiling_testing";
31 
32     // Configs for testing only.
33     public static final String RATE_LIMITER_DISABLE_PROPERTY = "rate_limiter.disabled";
34     public static final String DISABLE_DELETE_UNREDACTED_TRACE =
35             "delete_unredacted_trace.disabled";
36 
37     // End section: Testing specific constants
38 
39     // Begin section: Server registered constants
40     // Values here can be accessed with all getters except {@link #getTestBoolean}.
41 
42     // Namespace for server configureable values
43     public static final String NAMESPACE = "profiling";
44     // System trace
45     public static final String KILLSWITCH_SYSTEM_TRACE = "killswitch_system_trace";
46     public static final String COST_SYSTEM_TRACE = "cost_system_trace";
47     public static final String SYSTEM_TRACE_DURATION_MS_DEFAULT =
48             "system_trace_duration_ms_default";
49     public static final String SYSTEM_TRACE_DURATION_MS_MIN = "system_trace_duration_ms_min";
50     public static final String SYSTEM_TRACE_DURATION_MS_MAX = "system_trace_duration_ms_max";
51     public static final String SYSTEM_TRACE_SIZE_KB_DEFAULT = "system_trace_size_kb_default";
52     public static final String SYSTEM_TRACE_SIZE_KB_MIN = "system_trace_size_kb_min";
53     public static final String SYSTEM_TRACE_SIZE_KB_MAX = "system_trace_size_kb_max";
54 
55     // Heap Profile
56     public static final String KILLSWITCH_HEAP_PROFILE = "killswitch_heap_profile";
57     public static final String COST_HEAP_PROFILE = "cost_heap_profile";
58     public static final String HEAP_PROFILE_TRACK_JAVA_ALLOCATIONS_DEFAULT =
59             "heap_profile_track_java_allocations_default";
60     public static final String HEAP_PROFILE_FLUSH_TIMEOUT_MS_DEFAULT =
61             "heap_profile_flush_timeout_ms_default";
62     public static final String HEAP_PROFILE_DURATION_MS_DEFAULT =
63             "heap_profile_duration_ms_default";
64     public static final String HEAP_PROFILE_DURATION_MS_MIN = "heap_profile_duration_ms_min";
65     public static final String HEAP_PROFILE_DURATION_MS_MAX = "heap_profile_duration_ms_max";
66     public static final String HEAP_PROFILE_SIZE_KB_DEFAULT = "heap_profile_size_kb_default";
67     public static final String HEAP_PROFILE_SIZE_KB_MIN = "heap_profile_size_kb_min";
68     public static final String HEAP_PROFILE_SIZE_KB_MAX = "heap_profile_size_kb_max";
69     public static final String HEAP_PROFILE_SAMPLING_INTERVAL_BYTES_DEFAULT =
70             "heap_profile_sampling_interval_bytes_default";
71     public static final String HEAP_PROFILE_SAMPLING_INTERVAL_BYTES_MIN =
72             "heap_profile_sampling_interval_bytes_min";
73     public static final String HEAP_PROFILE_SAMPLING_INTERVAL_BYTES_MAX =
74             "heap_profile_sampling_interval_bytes_max";
75 
76     // Java Heap Dump
77     public static final String KILLSWITCH_JAVA_HEAP_DUMP = "killswitch_java_heap_dump";
78     public static final String COST_JAVA_HEAP_DUMP = "cost_java_heap_dump";
79     public static final String JAVA_HEAP_DUMP_DURATION_MS_DEFAULT =
80             "java_heap_dump_duration_ms_default";
81     public static final String JAVA_HEAP_DUMP_DATA_SOURCE_STOP_TIMEOUT_MS_DEFAULT =
82             "java_heap_dump_data_source_stop_timeout_ms_default";
83     public static final String JAVA_HEAP_DUMP_SIZE_KB_DEFAULT = "java_heap_dump_size_kb_default";
84     public static final String JAVA_HEAP_DUMP_SIZE_KB_MIN = "java_heap_dump_size_kb_min";
85     public static final String JAVA_HEAP_DUMP_SIZE_KB_MAX = "java_heap_dump_size_kb_max";
86 
87     // Stack Sampling
88     public static final String KILLSWITCH_STACK_SAMPLING = "killswitch_stack_sampling";
89     public static final String COST_STACK_SAMPLING = "cost_stack_sampling";
90     public static final String STACK_SAMPLING_FLUSH_TIMEOUT_MS_DEFAULT =
91             "stack_sampling_flush_timeout_ms_default";
92     public static final String STACK_SAMPLING_DURATION_MS_DEFAULT =
93             "stack_sampling_duration_ms_default";
94     public static final String STACK_SAMPLING_DURATION_MS_MIN = "stack_sampling_duration_ms_min";
95     public static final String STACK_SAMPLING_DURATION_MS_MAX = "stack_sampling_duration_ms_max";
96     public static final String STACK_SAMPLING_SAMPLING_SIZE_KB_DEFAULT =
97             "stack_sampling_size_kb_default";
98     public static final String STACK_SAMPLING_SAMPLING_SIZE_KB_MIN = "stack_sampling_size_kb_min";
99     public static final String STACK_SAMPLING_SAMPLING_SIZE_KB_MAX = "stack_sampling_size_kb_max";
100     public static final String STACK_SAMPLING_FREQUENCY_DEFAULT =
101             "stack_sampling_frequency_default";
102     public static final String STACK_SAMPLING_FREQUENCY_MIN = "stack_sampling_frequency_min";
103     public static final String STACK_SAMPLING_FREQUENCY_MAX = "stack_sampling_frequency_max";
104 
105     // Rate limiter configs
106     public static final String PERSIST_TO_DISK_FREQUENCY_MS = "persist_to_disk_frequency_ms";
107     public static final String MAX_COST_SYSTEM_1_HOUR = "max_cost_system_1_hour";
108     public static final String MAX_COST_PROCESS_1_HOUR = "max_cost_process_1_hour";
109     public static final String MAX_COST_SYSTEM_24_HOUR = "max_cost_system_24_hour";
110     public static final String MAX_COST_PROCESS_24_HOUR = "max_cost_process_24_hour";
111     public static final String MAX_COST_SYSTEM_7_DAY = "max_cost_system_7_day";
112     public static final String MAX_COST_PROCESS_7_DAY = "max_cost_process_7_day";
113 
114     // Perfetto configs
115     public static final String PERFETTO_DESTROY_TIMEOUT_MS = "perfetto_destroy_timeout_ms";
116 
117     // General configs
118     public static final String MAX_RESULT_REDELIVERY_COUNT = "max_result_redelivery_count";
119     public static final String CLEAR_TEMPORARY_DIRECTORY_FREQUENCY_MS =
120             "clear_temporary_directory_frequency_ms";
121     public static final String CLEAR_TEMPORARY_DIRECTORY_BOOT_DELAY_MS =
122             "clear_temporary_directory_boot_delay_ms";
123 
124     // Post Processing Configs
125     public static final String PROFILING_RECHECK_DELAY_MS = "profiling_recheck_delay_ms";
126 
127     // Redaction configs
128     public static final String REDACTION_CHECK_FREQUENCY_MS = "redaction_check_frequency_ms";
129     public static final String REDACTION_MAX_RUNTIME_ALLOTTED_MS =
130             "redaction_max_runtime_allotted_ms";
131 
132     // End section: Server registered constants
133 
134     /**
135      * Get string param for provided device config name from server side device config namespace
136      * or return default if unavailable for any reason.
137      */
getString(String name, String defaultValue)138     public static String getString(String name, String defaultValue) {
139         return DeviceConfig.getString(NAMESPACE, name, defaultValue);
140     }
141 
142     /**
143      * Get boolean param for provided device config name from server side device config namespace
144      * or return default if unavailable for any reason.
145      */
getBoolean(String name, boolean defaultValue)146     public static boolean getBoolean(String name, boolean defaultValue) {
147         return DeviceConfig.getBoolean(NAMESPACE, name, defaultValue);
148     }
149 
150     /**
151      * Get int param for provided device config name from server side device config namespace
152      * or return default if unavailable for any reason.
153      */
getInt(String name, int defaultValue)154     public static int getInt(String name, int defaultValue) {
155         return DeviceConfig.getInt(NAMESPACE, name, defaultValue);
156     }
157 
158     /**
159      * Get long param for provided device config name from server side device config namespace
160      * or return default if unavailable for any reason.
161      */
getLong(String name, long defaultValue)162     public static long getLong(String name, long defaultValue) {
163         return DeviceConfig.getLong(NAMESPACE, name, defaultValue);
164     }
165 
166     /**
167      * Get boolean param for provided device config name from test only device config namespace
168      * or return default if unavailable for any reason.
169      */
getTestBoolean(String name, boolean defaultValue)170     public static boolean getTestBoolean(String name, boolean defaultValue) {
171         return DeviceConfig.getBoolean(NAMESPACE_TESTING, name, defaultValue);
172     }
173 
174     /** Get all properties related to Java Heap Dump configuration. */
getAllJavaHeapDumpProperties()175     public static DeviceConfig.Properties getAllJavaHeapDumpProperties() {
176         return DeviceConfig.getProperties(NAMESPACE,
177                 KILLSWITCH_JAVA_HEAP_DUMP,
178                 JAVA_HEAP_DUMP_DURATION_MS_DEFAULT,
179                 JAVA_HEAP_DUMP_DATA_SOURCE_STOP_TIMEOUT_MS_DEFAULT,
180                 JAVA_HEAP_DUMP_SIZE_KB_DEFAULT,
181                 JAVA_HEAP_DUMP_SIZE_KB_MIN,
182                 JAVA_HEAP_DUMP_SIZE_KB_MAX);
183     }
184 
185     /** Get all properties related to Heap Profile configuration. */
getAllHeapProfileProperties()186     public static DeviceConfig.Properties getAllHeapProfileProperties() {
187         return DeviceConfig.getProperties(NAMESPACE,
188                 KILLSWITCH_HEAP_PROFILE,
189                 HEAP_PROFILE_TRACK_JAVA_ALLOCATIONS_DEFAULT,
190                 HEAP_PROFILE_FLUSH_TIMEOUT_MS_DEFAULT,
191                 HEAP_PROFILE_DURATION_MS_DEFAULT,
192                 HEAP_PROFILE_DURATION_MS_MIN,
193                 HEAP_PROFILE_DURATION_MS_MAX,
194                 HEAP_PROFILE_SIZE_KB_DEFAULT,
195                 HEAP_PROFILE_SIZE_KB_MIN,
196                 HEAP_PROFILE_SIZE_KB_MAX,
197                 HEAP_PROFILE_SAMPLING_INTERVAL_BYTES_DEFAULT,
198                 HEAP_PROFILE_SAMPLING_INTERVAL_BYTES_MIN,
199                 HEAP_PROFILE_SAMPLING_INTERVAL_BYTES_MAX);
200     }
201 
202     /** Get all properties related to Stack Sampling configuration. */
getAllStackSamplingProperties()203     public static DeviceConfig.Properties getAllStackSamplingProperties() {
204         return DeviceConfig.getProperties(NAMESPACE,
205                 KILLSWITCH_STACK_SAMPLING,
206                 STACK_SAMPLING_FLUSH_TIMEOUT_MS_DEFAULT,
207                 STACK_SAMPLING_DURATION_MS_DEFAULT,
208                 STACK_SAMPLING_DURATION_MS_MIN,
209                 STACK_SAMPLING_DURATION_MS_MAX,
210                 STACK_SAMPLING_SAMPLING_SIZE_KB_DEFAULT,
211                 STACK_SAMPLING_SAMPLING_SIZE_KB_MIN,
212                 STACK_SAMPLING_SAMPLING_SIZE_KB_MAX,
213                 STACK_SAMPLING_FREQUENCY_DEFAULT,
214                 STACK_SAMPLING_FREQUENCY_MIN,
215                 STACK_SAMPLING_FREQUENCY_MAX);
216     }
217 
218     /** Get all properties related to System Trace configuration. */
getAllSystemTraceProperties()219     public static DeviceConfig.Properties getAllSystemTraceProperties() {
220         return DeviceConfig.getProperties(NAMESPACE,
221                 KILLSWITCH_SYSTEM_TRACE,
222                 SYSTEM_TRACE_DURATION_MS_DEFAULT,
223                 SYSTEM_TRACE_DURATION_MS_MIN,
224                 SYSTEM_TRACE_DURATION_MS_MAX,
225                 SYSTEM_TRACE_SIZE_KB_DEFAULT,
226                 SYSTEM_TRACE_SIZE_KB_MIN,
227                 SYSTEM_TRACE_SIZE_KB_MAX);
228     }
229 
230     /** Get all properties related to rate limiter. */
getAllRateLimiterProperties()231     public static DeviceConfig.Properties getAllRateLimiterProperties() {
232         return DeviceConfig.getProperties(NAMESPACE,
233                 MAX_COST_PROCESS_1_HOUR,
234                 MAX_COST_SYSTEM_1_HOUR,
235                 MAX_COST_PROCESS_24_HOUR,
236                 MAX_COST_SYSTEM_24_HOUR,
237                 MAX_COST_PROCESS_7_DAY,
238                 MAX_COST_SYSTEM_7_DAY,
239                 COST_JAVA_HEAP_DUMP,
240                 COST_HEAP_PROFILE,
241                 COST_STACK_SAMPLING,
242                 COST_SYSTEM_TRACE,
243                 PERSIST_TO_DISK_FREQUENCY_MS);
244     }
245 
246 }
247