diff --git a/.github/workflows/baseline-profile.yml b/.github/workflows/baseline-profile.yml new file mode 100644 index 0000000..67e25c1 --- /dev/null +++ b/.github/workflows/baseline-profile.yml @@ -0,0 +1,67 @@ +name: Baseline profile + +# Records a fresh app/src/main/baseline-prof.txt on an emulator and pushes it to the +# ci/baseline-profile branch for review. Run it from the Actions tab or push to profile/. +on: + workflow_dispatch: + push: + branches: ["profile/**"] + +permissions: + contents: write + +jobs: + profile: + if: github.server_url == 'https://github.com' + runs-on: ubuntu-latest + timeout-minutes: 90 + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-java@v4 + with: + distribution: temurin + java-version: 21 + + - uses: gradle/actions/setup-gradle@v4 + + # Rules must use the names from the source, so the profile is recorded on a build without R8 + - name: Build a non-minified release APK + run: | + sed -i 's/isMinifyEnabled = true/isMinifyEnabled = false/; s/isShrinkResources = true/isShrinkResources = false/' app/build.gradle.kts + ./gradlew :app:assembleRelease -q + keytool -genkeypair -keystore "$RUNNER_TEMP/p.jks" -storepass profile -keypass profile -alias p \ + -keyalg RSA -keysize 2048 -validity 365 -dname "CN=profile" >/dev/null 2>&1 + apksigner="$(ls -d "$ANDROID_HOME"/build-tools/*/ | sort -V | tail -n 1)apksigner" + "$apksigner" sign --ks "$RUNNER_TEMP/p.jks" --ks-pass pass:profile --out "$RUNNER_TEMP/app.apk" \ + app/build/outputs/apk/release/app-release-unsigned.apk + git checkout app/build.gradle.kts + + - name: Enable KVM + run: | + echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules + sudo udevadm control --reload-rules + sudo udevadm trigger --name-match=kvm + + - name: Record the profile + uses: reactivecircus/android-emulator-runner@v2 + with: + api-level: 34 + target: google_apis + arch: x86_64 + profile: pixel_6 + emulator-options: -no-snapshot -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none + script: bash scripts/baseline-profile.sh "$RUNNER_TEMP/app.apk" "$RUNNER_TEMP/baseline-prof.txt" + + - name: Push it for review + run: | + sort -u "$RUNNER_TEMP/baseline-prof.txt" > "$RUNNER_TEMP/sorted.txt" + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + git checkout --orphan profile-output + git rm -rq --cached . + cp "$RUNNER_TEMP/sorted.txt" baseline-prof.txt + git add baseline-prof.txt + git commit -qm "Baseline profile recorded from ${GITHUB_SHA::7}" + git push -f origin HEAD:refs/heads/ci/baseline-profile + echo "::notice title=Baseline profile::$(wc -l < baseline-prof.txt) rules pushed to ci/baseline-profile" diff --git a/scripts/baseline-profile.sh b/scripts/baseline-profile.sh new file mode 100755 index 0000000..1bda9ff --- /dev/null +++ b/scripts/baseline-profile.sh @@ -0,0 +1,68 @@ +#!/usr/bin/env bash +# Records which classes and methods the app uses on startup and in each tab, in the text format +# of app/src/main/baseline-prof.txt. Needs a rooted Android 13+ device or emulator (no Play Store). +# Usage: scripts/baseline-profile.sh +set -euo pipefail + +PKG=cc.skysparkle.sparkledns +ACT="$PKG/.MainActivity" +APK="$1" +OUT="$2" + +adb root >/dev/null 2>&1 && sleep 3 +adb wait-for-device +adb uninstall "$PKG" >/dev/null 2>&1 || true +adb install -g "$APK" >/dev/null +read -r W H < <(adb shell wm size | tr -d '\r' | awk -F'[ x]' '/Physical/ {print $3, $4}') + +tap_text() { + local xy + for _ in 1 2 3 4 5; do + adb shell uiautomator dump /sdcard/ui.xml >/dev/null 2>&1 || true + xy="$(adb shell cat /sdcard/ui.xml | python3 -c ' +import re, sys +xml, text = sys.stdin.read(), sys.argv[1] +for node in re.findall(r"]*>", xml): + if re.search(r"(text|content-desc)=\"%s\"" % re.escape(text), node): + x1, y1, x2, y2 = map(int, re.search(r"bounds=\"\[(\d+),(\d+)\]\[(\d+),(\d+)\]\"", node).groups()) + print((x1 + x2) // 2, (y1 + y2) // 2) + break +' "$1")" + if [[ -n "$xy" ]]; then + adb shell input tap $xy + return 0 + fi + sleep 1 + done + echo "::warning::'$1' not found on screen" +} + +swipe() { + adb shell input swipe $((W / 2)) $((H * 3 / 4)) $((W / 2)) $((H / 4)) 200 + sleep 1 + adb shell input swipe $((W / 2)) $((H / 4)) $((W / 2)) $((H * 3 / 4)) 200 + sleep 1 +} + +for _ in 1 2 3; do + adb shell am force-stop "$PKG" + adb shell am start-activity -W -n "$ACT" >/dev/null + sleep 3 + swipe + tap_text "Monitor"; sleep 2 + tap_text "Apps"; sleep 3 + tap_text "System"; sleep 2 + swipe + tap_text "Blocklist"; sleep 2 + swipe + tap_text "About"; sleep 2 + tap_text "Home"; sleep 2 + # Makes ART write the profile now instead of on its own schedule + adb shell "kill -10 \$(pidof $PKG)" + sleep 5 +done + +adb shell pm dump-profiles --dump-classes-and-methods "$PKG" +sleep 2 +adb pull "/data/misc/profman/$PKG-primary.prof.txt" "$OUT" >/dev/null +echo "Profile lines: $(wc -l < "$OUT")"