| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.os; |
| 18 |
|
| 19 |
import static android.annotation.SystemApi.Client.MODULE_LIBRARIES; |
| 20 |
|
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.SystemApi; |
| 23 |
import android.compat.annotation.UnsupportedAppUsage; |
| 24 |
|
| 25 |
import dalvik.annotation.optimization.CriticalNative; |
| 26 |
import dalvik.annotation.optimization.FastNative; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public final class Trace { |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
private static final String TAG = "Trace"; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public static final long TRACE_TAG_NEVER = 0; |
| 51 |
|
| 52 |
public static final long TRACE_TAG_ALWAYS = 1L << 0; |
| 53 |
|
| 54 |
public static final long TRACE_TAG_GRAPHICS = 1L << 1; |
| 55 |
|
| 56 |
public static final long TRACE_TAG_INPUT = 1L << 2; |
| 57 |
|
| 58 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 59 |
public static final long TRACE_TAG_VIEW = 1L << 3; |
| 60 |
|
| 61 |
public static final long TRACE_TAG_WEBVIEW = 1L << 4; |
| 62 |
|
| 63 |
public static final long TRACE_TAG_WINDOW_MANAGER = 1L << 5; |
| 64 |
|
| 65 |
public static final long TRACE_TAG_ACTIVITY_MANAGER = 1L << 6; |
| 66 |
|
| 67 |
public static final long TRACE_TAG_SYNC_MANAGER = 1L << 7; |
| 68 |
|
| 69 |
public static final long TRACE_TAG_AUDIO = 1L << 8; |
| 70 |
|
| 71 |
public static final long TRACE_TAG_VIDEO = 1L << 9; |
| 72 |
|
| 73 |
public static final long TRACE_TAG_CAMERA = 1L << 10; |
| 74 |
|
| 75 |
public static final long TRACE_TAG_HAL = 1L << 11; |
| 76 |
|
| 77 |
@UnsupportedAppUsage |
| 78 |
public static final long TRACE_TAG_APP = 1L << 12; |
| 79 |
|
| 80 |
public static final long TRACE_TAG_RESOURCES = 1L << 13; |
| 81 |
|
| 82 |
public static final long TRACE_TAG_DALVIK = 1L << 14; |
| 83 |
|
| 84 |
public static final long TRACE_TAG_RS = 1L << 15; |
| 85 |
|
| 86 |
public static final long TRACE_TAG_BIONIC = 1L << 16; |
| 87 |
|
| 88 |
public static final long TRACE_TAG_POWER = 1L << 17; |
| 89 |
|
| 90 |
public static final long TRACE_TAG_PACKAGE_MANAGER = 1L << 18; |
| 91 |
|
| 92 |
public static final long TRACE_TAG_SYSTEM_SERVER = 1L << 19; |
| 93 |
|
| 94 |
public static final long TRACE_TAG_DATABASE = 1L << 20; |
| 95 |
|
| 96 |
@SystemApi(client = MODULE_LIBRARIES) |
| 97 |
public static final long TRACE_TAG_NETWORK = 1L << 21; |
| 98 |
|
| 99 |
public static final long TRACE_TAG_ADB = 1L << 22; |
| 100 |
|
| 101 |
public static final long TRACE_TAG_VIBRATOR = 1L << 23; |
| 102 |
|
| 103 |
@SystemApi |
| 104 |
public static final long TRACE_TAG_AIDL = 1L << 24; |
| 105 |
|
| 106 |
public static final long TRACE_TAG_NNAPI = 1L << 25; |
| 107 |
|
| 108 |
public static final long TRACE_TAG_RRO = 1L << 26; |
| 109 |
|
| 110 |
public static final long TRACE_TAG_THERMAL = 1L << 27; |
| 111 |
|
| 112 |
private static final long TRACE_TAG_NOT_READY = 1L << 63; |
| 113 |
private static final int MAX_SECTION_NAME_LEN = 127; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
@UnsupportedAppUsage |
| 119 |
private static volatile long sEnabledTags = TRACE_TAG_NOT_READY; |
| 120 |
|
| 121 |
private static int sZygoteDebugFlags = 0; |
| 122 |
|
| 123 |
@UnsupportedAppUsage |
| 124 |
@CriticalNative |
| 125 |
private static native long nativeGetEnabledTags(); |
| 126 |
private static native void nativeSetAppTracingAllowed(boolean allowed); |
| 127 |
private static native void nativeSetTracingEnabled(boolean allowed); |
| 128 |
|
| 129 |
@FastNative |
| 130 |
private static native void nativeTraceCounter(long tag, String name, long value); |
| 131 |
@FastNative |
| 132 |
private static native void nativeTraceBegin(long tag, String name); |
| 133 |
@FastNative |
| 134 |
private static native void nativeTraceEnd(long tag); |
| 135 |
@FastNative |
| 136 |
private static native void nativeAsyncTraceBegin(long tag, String name, int cookie); |
| 137 |
@FastNative |
| 138 |
private static native void nativeAsyncTraceEnd(long tag, String name, int cookie); |
| 139 |
@FastNative |
| 140 |
private static native void nativeAsyncTraceForTrackBegin(long tag, |
| 141 |
String trackName, String name, int cookie); |
| 142 |
@FastNative |
| 143 |
private static native void nativeAsyncTraceForTrackEnd(long tag, |
| 144 |
String trackName, int cookie); |
| 145 |
@FastNative |
| 146 |
private static native void nativeInstant(long tag, String name); |
| 147 |
@FastNative |
| 148 |
private static native void nativeInstantForTrack(long tag, String trackName, String name); |
| 149 |
|
| 150 |
private Trace() { |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
@UnsupportedAppUsage |
| 162 |
@SystemApi(client = MODULE_LIBRARIES) |
| 163 |
public static boolean isTagEnabled(long traceTag) { |
| 164 |
long tags = nativeGetEnabledTags(); |
| 165 |
return (tags & traceTag) != 0; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
@UnsupportedAppUsage |
| 178 |
@SystemApi(client = MODULE_LIBRARIES) |
| 179 |
public static void traceCounter(long traceTag, @NonNull String counterName, int counterValue) { |
| 180 |
if (isTagEnabled(traceTag)) { |
| 181 |
nativeTraceCounter(traceTag, counterName, counterValue); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
@UnsupportedAppUsage |
| 194 |
public static void setAppTracingAllowed(boolean allowed) { |
| 195 |
nativeSetAppTracingAllowed(allowed); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public static void setTracingEnabled(boolean enabled, int debugFlags) { |
| 203 |
nativeSetTracingEnabled(enabled); |
| 204 |
sZygoteDebugFlags = debugFlags; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
@UnsupportedAppUsage |
| 218 |
@SystemApi(client = MODULE_LIBRARIES) |
| 219 |
public static void traceBegin(long traceTag, @NonNull String methodName) { |
| 220 |
if (isTagEnabled(traceTag)) { |
| 221 |
nativeTraceBegin(traceTag, methodName); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
@UnsupportedAppUsage |
| 234 |
@SystemApi(client = MODULE_LIBRARIES) |
| 235 |
public static void traceEnd(long traceTag) { |
| 236 |
if (isTagEnabled(traceTag)) { |
| 237 |
nativeTraceEnd(traceTag); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
@UnsupportedAppUsage |
| 255 |
@SystemApi(client = MODULE_LIBRARIES) |
| 256 |
public static void asyncTraceBegin(long traceTag, @NonNull String methodName, int cookie) { |
| 257 |
if (isTagEnabled(traceTag)) { |
| 258 |
nativeAsyncTraceBegin(traceTag, methodName, cookie); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
@UnsupportedAppUsage |
| 274 |
@SystemApi(client = MODULE_LIBRARIES) |
| 275 |
public static void asyncTraceEnd(long traceTag, @NonNull String methodName, int cookie) { |
| 276 |
if (isTagEnabled(traceTag)) { |
| 277 |
nativeAsyncTraceEnd(traceTag, methodName, cookie); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
public static void asyncTraceForTrackBegin(long traceTag, |
| 298 |
@NonNull String trackName, @NonNull String methodName, int cookie) { |
| 299 |
if (isTagEnabled(traceTag)) { |
| 300 |
nativeAsyncTraceForTrackBegin(traceTag, trackName, methodName, cookie); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
public static void asyncTraceForTrackEnd(long traceTag, |
| 317 |
@NonNull String trackName, int cookie) { |
| 318 |
if (isTagEnabled(traceTag)) { |
| 319 |
nativeAsyncTraceForTrackEnd(traceTag, trackName, cookie); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
@Deprecated |
| 329 |
public static void asyncTraceForTrackEnd(long traceTag, |
| 330 |
@NonNull String trackName, @NonNull String methodName, int cookie) { |
| 331 |
if (isTagEnabled(traceTag)) { |
| 332 |
nativeAsyncTraceForTrackEnd(traceTag, trackName, cookie); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
public static void instant(long traceTag, String methodName) { |
| 344 |
if (methodName == null) { |
| 345 |
throw new IllegalArgumentException("methodName cannot be null"); |
| 346 |
} |
| 347 |
if (isTagEnabled(traceTag)) { |
| 348 |
nativeInstant(traceTag, methodName); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
public static void instantForTrack(long traceTag, String trackName, String methodName) { |
| 361 |
if (trackName == null) { |
| 362 |
throw new IllegalArgumentException("trackName cannot be null"); |
| 363 |
} |
| 364 |
if (methodName == null) { |
| 365 |
throw new IllegalArgumentException("methodName cannot be null"); |
| 366 |
} |
| 367 |
if (isTagEnabled(traceTag)) { |
| 368 |
nativeInstantForTrack(traceTag, trackName, methodName); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
public static boolean isEnabled() { |
| 382 |
return isTagEnabled(TRACE_TAG_APP); |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
public static void beginSection(@NonNull String sectionName) { |
| 397 |
if (isTagEnabled(TRACE_TAG_APP)) { |
| 398 |
if (sectionName.length() > MAX_SECTION_NAME_LEN) { |
| 399 |
throw new IllegalArgumentException("sectionName is too long"); |
| 400 |
} |
| 401 |
nativeTraceBegin(TRACE_TAG_APP, sectionName); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
public static void endSection() { |
| 413 |
if (isTagEnabled(TRACE_TAG_APP)) { |
| 414 |
nativeTraceEnd(TRACE_TAG_APP); |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
public static void beginAsyncSection(@NonNull String methodName, int cookie) { |
| 429 |
asyncTraceBegin(TRACE_TAG_APP, methodName, cookie); |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
public static void endAsyncSection(@NonNull String methodName, int cookie) { |
| 441 |
asyncTraceEnd(TRACE_TAG_APP, methodName, cookie); |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
public static void setCounter(@NonNull String counterName, long counterValue) { |
| 451 |
if (isTagEnabled(TRACE_TAG_APP)) { |
| 452 |
nativeTraceCounter(TRACE_TAG_APP, counterName, counterValue); |
| 453 |
} |
| 454 |
} |
| 455 |
} |