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