1# ANGLE Restricted Traces 2 3The files in this directory are traces of real applications. We host them 4internally because they may contain third party IP which we don't want 5to share publicly. 6 7## Accessing the traces 8 9In order to compile and run with these, you must be granted access by Google, 10then authenticate with [CIPD](CIPD). Googlers, use your @google account. 11``` 12cipd auth-login 13``` 14Add the following to ANGLE's .gclient file: 15``` 16 "custom_vars": { 17 "checkout_angle_restricted_traces": True 18 }, 19``` 20Then use gclient to pull down binary files from CIPD. 21``` 22gclient sync -D 23``` 24This should result in a number of directories created in `src/tests/restricted_traces` that contain 25the trace files listed in [restricted_traces.json](restricted_traces.json): 26``` 27$ ls -d src/tests/restricted_traces/*/ 28src/tests/restricted_traces/aliexpress/ 29src/tests/restricted_traces/angry_birds_2_1500/ 30src/tests/restricted_traces/arena_of_valor/ 31src/tests/restricted_traces/asphalt_8/ 32src/tests/restricted_traces/brawl_stars/ 33src/tests/restricted_traces/bus_simulator_indonesia/ 34src/tests/restricted_traces/candy_crush_500/ 35src/tests/restricted_traces/clash_of_clans/ 36src/tests/restricted_traces/clash_royale/ 37src/tests/restricted_traces/cod_mobile/ 38... 39``` 40 41[CIPD]: https://chromium.googlesource.com/infra/luci/luci-go/+/main/cipd/README.md 42 43## Building the trace tests 44 45To build for Android, follow the steps in [DevSetupAndroid.md](../../../doc/DevSetupAndroid.md) 46(Recommend using the [`Performance`](../../../doc/DevSetupAndroid.md#performance-config) arguments 47for best performance) 48 49To build for Desktop, follow the steps in [DevSetup.md](../../../doc/DevSetup.md) 50 51When that is working, add the following GN arg to your setup: 52``` 53build_angle_trace_perf_tests = true 54``` 55To build the angle_perftests: 56``` 57autoninja -C out/<config> angle_perftests 58``` 59Run them like so: 60``` 61out/<config>/angle_perftests --gtest_filter=TracePerfTest* 62``` 63 64# Capturing and adding new Android traces 65 66Generally we want to use a Debug setup for recording new traces. That allows us to see asserts and 67errors if the tracer needs to be improved. 68Add the following GN arg to your Debug setup: 69``` 70angle_with_capture_by_default = true 71``` 72 73After [building](../../../doc/DevSetupAndroid.md#building-angle-for-android) and 74[installing](../../../doc/DevSetupAndroid.md#install-the-angle-apk) the APK with the above arg, 75we're ready to start capturing. 76 77## Determine the target app 78 79We first need to identify which application we want to trace. That can generally be done by 80looking at the web-based Play Store entry for your app. For instance, Angry Birds 2 is listed 81here: https://play.google.com/store/apps/details?id=com.rovio.baba 82 83If there is no Play Store entry for your app, there are a couple of ways you can determine the 84app's name. 85 86If you have a userdebug build of Android, you can check logcat when you launch the application. 87You should see an entry like this: 88``` 89GraphicsEnvironment: ANGLE Developer option for 'com.rovio.baba' set to: 'default' 90``` 91If you just have an APK, you can use the following command to find the package name: 92``` 93$ aapt dump badging angry_birds_2.apk | grep package 94package: name='com.rovio.baba' versionCode='24900001' versionName='2.49.1' platformBuildVersionName='' 95``` 96You can also just guess at the package name, then check your device to see if it is installed. Keep 97trying combinations until you find it: 98``` 99$ adb shell pm list packages | grep rovio 100package:com.rovio.baba 101``` 102Track the package name for use in later steps: 103``` 104export PACKAGE_NAME=com.rovio.baba 105``` 106 107## Choose a trace name 108 109Next, we need to chose a name for the trace. Choose something simple that identifies the app, then use snake 110case. This will be the name of the trace files, including the trace directory. Changing this value later is possible, 111but not recommended. 112``` 113export LABEL=angry_birds_2 114``` 115 116## Opt the application into ANGLE 117 118Next, opt the application into using your ANGLE with capture enabled by default: 119``` 120adb shell settings put global angle_debug_package org.chromium.angle 121adb shell settings put global angle_gl_driver_selection_pkgs $PACKAGE_NAME 122adb shell settings put global angle_gl_driver_selection_values angle 123``` 124 125## Set up some Capture/Replay properties 126 127We also need to set some debug properties used by the tracer. 128 129Ensure frame capture is enabled. This might be redundant, but ensure the property isn't set to 130zero, which disables frame capture. 131``` 132adb shell setprop debug.angle.capture.enabled 1 133``` 134Empty the start and end frames. Again, this might be redundant, but it is less confusing. 135``` 136adb shell setprop debug.angle.capture.frame_start '""' 137adb shell setprop debug.angle.capture.frame_end '""' 138``` 139Set the label to be used in the trace files 140``` 141adb shell setprop debug.angle.capture.label $LABEL 142``` 143Set a trigger value to be used by the tracer. This should be set to the *number of frames* you want 144to capture. We typically use 10 to get an idea of how a scene is running, but some workloads 145require more. Use your discretion here: 146``` 147adb shell setprop debug.angle.capture.trigger 10 148``` 149 150## Create output location 151 152We need to write out the trace file in a location accessible by the app. We use the app's data 153storage on sdcard, but create a subfolder to isolate ANGLE's files: 154``` 155adb shell mkdir -p /sdcard/Android/data/$PACKAGE_NAME/angle_capture 156``` 157 158## Start the target app 159 160From here, you can start the application. You should see logcat entries like the following, 161indicating that we've succesfully turned on capturing: 162``` 163ANGLE : INFO: Capture trigger detected, disabling capture start/end frame. 164ANGLE : INFO: Limiting binary format support count to zero while FrameCapture enabled 165ANGLE : INFO: Limiting image unit count to 8 while FrameCapture enabled 166ANGLE : INFO: Setting uniform buffer offset alignment to 256 while FrameCapture enabled 167ANGLE : INFO: Disabling GL_EXT_map_buffer_range and GL_OES_mapbuffer during capture, which are not supported on some native drivers 168ANGLE : INFO: Disabling GL_CHROMIUM_bind_uniform_location during capture, which is not supported on native drivers 169ANGLE : INFO: Disabling GL_NV_shader_noperspective_interpolation during capture, which is not supported on some native drivers 170ANGLE : INFO: Limiting draw buffer count to 4 while FrameCapture enabled 171``` 172## Trigger the capture 173 174When you have reached the content in your application that you want to record, set the trigger 175value to zero: 176``` 177adb shell setprop debug.angle.capture.trigger 0 178``` 179In logcat we'll see another entry corresponding to this: 180``` 181ANGLE : INFO: Capture triggered after frame 30440 for 10 frames 182``` 183The app may pause briefly when the capture is completing. You can check its progress by looking at 184the file system: 185``` 186adb shell ls -la /sdcard/Android/data/$PACKAGE_NAME/angle_capture 187``` 188Allow the app to run until the `*angledata.gz` file is non-zero and no longer growing. The app 189should continue rendering after that: 190``` 191$ adb shell ls -s -w 1 /sdcard/Android/data/$PACKAGE_NAME/angle_capture 19230528 angry_birds_2_capture_context1.angledata.gz 193 8 angry_birds_2_capture_context1.cpp 194 4 angry_birds_2_capture_context1_files.txt 195 768 angry_birds_2_capture_context1_frame001.cpp 196 100 angry_birds_2_capture_context1_frame002.cpp 197 100 angry_birds_2_capture_context1_frame003.cpp 198 100 angry_birds_2_capture_context1_frame004.cpp 199 100 angry_birds_2_capture_context1_frame005.cpp 200 104 angry_birds_2_capture_context1_frame006.cpp 201 100 angry_birds_2_capture_context1_frame007.cpp 202 100 angry_birds_2_capture_context1_frame008.cpp 203 100 angry_birds_2_capture_context1_frame009.cpp 204 100 angry_birds_2_capture_context1_frame010.cpp 205 120 angry_birds_2_capture_context1_frame011.cpp 206 8 angry_birds_2_capture_context1.h 207``` 208Note, you may see multiple contexts captured in the output. When this happens, look at the size of 209the files. The larger files should be the context you care about it. You should move or delete the 210other context files. 211 212## Pull the trace files 213 214Next, we want to pull those files over to the host and run some scripts. 215``` 216cd $CHROMIUM_SRC/third_party/angle/src/tests/restricted_traces 217mkdir -p $LABEL 218adb pull /sdcard/Android/data/$PACKAGE_NAME/angle_capture/. $LABEL/ 219``` 220 221## Add the new trace to the JSON list 222 223The list of traces is tracked in [restricted_traces.json](restricted_traces.json). Manually add your 224new trace to this list. Use version "1" for the trace version. 225 226On Linux, you can also use a tool called `jq` to update the list. This ensures we get them in 227alphabetical order with no duplicates. It can also be done by hand if you are unable to install it, 228for some reason. 229``` 230sudo apt-get install jq 231``` 232Then run the following command: 233``` 234export VERSION=1 235jq ".traces = (.traces + [\"$LABEL $VERSION\"] | unique)" restricted_traces.json | sponge restricted_traces.json 236``` 237 238## Run code auto-generation 239 240The [`gen_restricted_traces`](gen_restricted_traces.py) script auto-generates entries 241in our checkout dependencies to sync restricted trace data on checkout. To trigger 242code generation run the following from the angle root folder: 243``` 244python ./scripts/run_code_generation.py 245``` 246After this you should be able to `git diff` and see changes in the following files: 247 248 * `DEPS` 249 * `scripts/code_generation_hashes/restricted_traces.json` 250 * `src/tests/restricted_traces/restricted_traces.json` (this is the file you originally modified) 251 252Note the absence of the traces themselves listed above. They are automatically 253ignored by [`.gitignore`](.gitignore) since they won't be checked in directly 254to the repo. 255 256## Upload your trace to CIPD 257 258Once you feel good about your trace, you can upload it to our collection of traces. This can only 259be done by Googlers with write access to the trace CIPD prefix. If you need write access contact 260someone listed in the `OWNERS` file. 261 262``` 263./sync_restricted_traces_to_cipd.py 264``` 265 266## Upload your CL 267 268Ensure your current working directory is up-to-date, and upload: 269 270``` 271git cl upload 272``` 273 274You're now ready to run your new trace on CI! 275 276# Upgrading existing traces 277 278With tracer updates sometimes we want to re-run tracing to upgrade the trace file format or to 279take advantage of new tracer improvements. The [`retrace_restricted_traces`](retrace_restricted_traces.py) 280script allows us to re-run tracing using [SwiftShader](https://swiftshader.googlesource.com/SwiftShader) 281on a desktop machine. As of writing we require re-tracing on a Windows machine because of size 282limitations with a Linux app window. 283 284## Prep work: Back up existing traces 285 286This will save the original traces in a temporary folder if you need to revert to the prior trace format: 287 288``` 289py ./src/tests/restricted_traces/retrace_restricted_traces.py backup "*" 290``` 291 292*Note: on Linux, remove the command `py` prefix to the Python scripts.* 293 294This will save the traces to `./retrace-backups`. At any time you can revert the trace files by running: 295 296``` 297py ./src/tests/restricted_traces/retrace_restricted_traces.py restore "*" 298``` 299 300## Part 1: Sanity Check with T-Rex 301 302First we'll retrace a single app to verify the workflow is intact. Please 303ensure you replace the specified variables with paths that work on your 304configuration and checkout: 305 306### Step 1/3: Capture T-Rex with Validation 307 308``` 309export TRACE_GN_PATH=out/Debug 310export TRACE_NAME=trex_200 311py ./src/tests/restricted_traces/retrace_restricted_traces.py upgrade $TRACE_GN_PATH retrace-wip -f $TRACE_NAME --validation --limit 3 312``` 313 314The `--validation` flag will turn on additional validation checks in the 315trace. The `--limit 3` flag forces a maximum of 3 frames of tracing so the 316test will run more quickly. The trace will end up in the `retrace-wip` 317folder. 318 319### Step 2/3: Validate T-Rex 320 321The command below will update your copy of the trace, rebuild, the run the 322test suite with validation enabled: 323 324``` 325py ./src/tests/restricted_traces/retrace_restricted_traces.py validate $TRACE_GN_PATH retrace-wip $TRACE_NAME 326``` 327 328If the trace failed validation, see the section below on diagnosing tracer 329errors. Otherwise proceed with the steps below. 330 331### Step 3/3: Restore the Canonical T-Rex Trace 332 333``` 334py ./src/tests/restricted_traces/retrace_restricted_traces.py restore $TRACE_NAME 335``` 336 337## Part 2: Do a limited trace upgrade with validation enabled 338 339### Step 1/3: Upgrade all traces with a limit of 3 frames 340 341``` 342py ./src/tests/restricted_traces/retrace_restricted_traces.py upgrade $TRACE_GN_PATH retrace-wip --validation --limit 3 --no-overwrite 343``` 344 345If this process gets interrupted, re-run the upgrade command. The 346`--no-overwrite` argument will ensure it will complete eventually. 347 348If any traces failed to upgrade, see the section below on diagnosing tracer 349errors. Otherwise proceed with the steps below. 350 351### Step 2/3: Validate all upgraded traces 352 353``` 354py ./src/tests/restricted_traces/retrace_restricted_traces.py validate $TRACE_GN_PATH retrace-wip "*" 355``` 356 357If any traces failed validation, see the section below on diagnosing tracer 358errors. Otherwise proceed with the steps below. 359 360### Step 3/3: Restore all traces 361 362``` 363py ./src/tests/restricted_traces/retrace_restricted_traces.py restore "*" 364``` 365 366## Part 3: Do the full trace upgrade 367 368``` 369rm -rf retrace-wip 370py ./src/tests/restricted_traces/retrace_restricted_traces.py upgrade $TRACE_GN_PATH retrace-wip --no-overwrite 371``` 372 373If this process gets interrupted, re-run the upgrade command. The 374`--no-overwrite` argument will ensure it will complete eventually. 375 376If any traces failed to upgrade, see the section below on diagnosing tracer 377errors. Otherwise proceed with the steps below. 378 379## Part 4: Test the upgraded traces under an experimental prefix (slow) 380 381Because there still may be trace errors undetected by validation, we first 382upload the traces to a temporary CIPD path for testing. After a successful 383run on the CQ, we will then upload them to the main ANGLE prefix. 384 385To enable the experimental prefix, edit 386[`restricted_traces.json`](restricted_traces.json) to use a version 387number beginning with 'x'. For example: 388 389``` 390 "traces": [ 391 "aliexpress x1", 392 "among_us x1", 393 "angry_birds_2_1500 x1", 394 "arena_of_valor x1", 395 "asphalt_8 x1", 396 "avakin_life x1", 397... and so on ... 398``` 399 400Then run: 401 402``` 403py ./src/tests/restricted_traces/retrace_restricted_traces.py restore -o retrace-wip "*" 404py ./src/tests/restricted_traces/sync_restricted_traces_to_cipd.py 405py ./scripts/run_code_generation.py 406``` 407 408The restore command will copy the new traces from the `retrace-wip` directory 409into the trace folder before we call the sync script. 410 411After these commands complete succesfully, create and upload a CL as normal. 412Run CQ +1 Dry-Run. If you find a test regression, see the section below on 413diagnosing tracer errors. Otherwise proceed with the steps below. 414 415## Part 5: Upload the verified traces to CIPD under the stable prefix 416 417Now that you've validated the traces on the CQ, update 418[`restricted_traces.json`](restricted_traces.json) to remove the 'x' prefix 419and incrementing the version of the traces (skipping versions if you prefer) 420and then run: 421 422``` 423py ./src/tests/restricted_traces/sync_restricted_traces_to_cipd.py 424py ./scripts/run_code_generation.py 425``` 426 427Then create and upload a CL as normal. Congratulations, you've finished the 428trace upgrade! 429 430# Diagnosing and fixing tracer errors 431 432## Debugging a crash or GLES error 433 434Ensure you're building ANGLE in Debug. Then look in the retrace script output 435to find the exact command line and environment variables the script uses to 436produce the failure. For example: 437 438``` 439INFO:root:ANGLE_CAPTURE_LABEL=trex_200 ANGLE_CAPTURE_OUT_DIR=C:\src\angle\retrace-wip\trex_200 ANGLE_CAPTURE_FRAME_START=2 ANGLE_CAPTURE_FRAME_END=4 ANGLE_CAPTURE_VALIDATION=1 ANGLE_FEATURE_OVERRIDES_ENABLED=allocateNonZeroMemory:forceInitShaderVariables ANGLE_CAPTURE_TRIM_ENABLED=1 out/Debug\angle_perftests.exe --gtest_filter=TracePerfTest.Run/vulkan_swiftshader_trex_200 --max-steps-performed 3 --retrace-mode --enable-all-trace-tests 440``` 441 442Once you can reproduce the issue you can use a debugger or other standard 443debugging processes to find the root cause and a fix. 444 445## Debugging a serialization difference 446 447If you encouter a serialization mismatch in the retrace, you can find the 448complete serialization output by looking in the retrace script output. ANGLE 449saves the complete serialization file contents on any mismatch. You can 450inspect and diff these files in a text editor to help diagnose what objects 451are faulty. 452 453If the mismatch is with a Buffer or Texture object content, you can manually 454edit the `frame_capture_utils.cpp` file to force some or all of the objects 455to serialize their entire contents. This can help show what kind of pixel or 456data differences might be causing the issue. For example, change this line: 457 458``` 459json->addBlob("data", dataPtr->data(), dataPtr->size()); 460``` 461 462to 463 464``` 465json->addBlobWithMax("data", dataPtr->data(), dataPtr->size(), 1000000); 466``` 467 468Note: in the future, we might make this option exposed via an envioronment 469variable, or even allow serialization of entire data blocks in text-encoded 470form that could be decoded to separate files. 471 472If you still can't determine what code might be causing the state difference, 473we can insert finer-grained serialization checkpoints to "bisect" where the 474coding mismatch is happening. It is not possible to force checkpoints after 475every GLES call, because serialization and validation is so prohibitively 476expensive. ANGLE instead has feature in the tracer that allows us to 477precisely control where the tracer inserts and validates the checkpoints, by 478using a boolean expression language. 479 480The retrace script command `--validation-expr` allows us to specify a C-like 481expression that determines when to add serialization checkpoints. For 482example, we can specify this validation expression: 483 484``` 485((frame == 2) && (call < 1189) && (call > 1100) && ((call % 5) == 0)) 486``` 487 488Using this expression will insert a serialization checkpoint in the second 489frame, on every 5th captured call, and when the captured call count is 490between 1101 and 1188. Here the `call` keyword denotes the call counter, 491which resets to 1 every frame, and increments by 1 with every captured GLES 492API call. The `frame` keyword denotes the frame counter, which starts at 1 493and increments by 1 every captured frame. The expression syntax supports all 494common C boolean operators. 495 496By finding a starting and ending frame range, and narrowing this range through 497experimentation, you can pinpoint the exact call that triggers the 498serialization mismatch, and then diagnose and fix the root cause. In some 499cases you can use RenderDoc or other frame debugging tools to inspect 500resource states before/after the bad call once you have found it. 501 502See also: [`http://crrev.com/c/3136094`](http://crrev.com/c/3136094) 503 504## Debugging a pixel test failure without a serialization mismatch 505 506Sometimes you manage to complete validation and upload, just to find a golden 507image pixel difference that manifests in some trace configurations. These 508problems can be harder to root cause. For instance, some configurations may 509render undefined pixels that are in practice well-defined on most GLES 510implementations. 511 512The pixel differences can also be a product of mismatched state even if the 513trace validation says all states are matched. Because ANGLE's GLES state 514serialization is incomplete, it can help to check the state serialization 515logic and add missing features as necessary. 516