Skip to content

Commit 7d6e71d

Browse files
committed
Extra tests CI: improve Rust caching
* Cache more of ~/.cargo Unclear whether this is needed, but matches other examples found online, and doesn't seem to increase (compressed) cache size significantly. * Add month to cache restore-keys Using the month in the cache `restore-keys` in addition to `key` ensures that we *reset* the cache every month, instead of continuing to grow the previous month's cache. * Include hash of the lockfile in cache keys but not restore-keys This ensures we update the cache when our dependencies change. * In extra_tests: Use sccache to cache built rust dependencies Largely cargo-culted from https://www.infinyon.com/blog/2021/04/github-actions-best-practices/#optimizing-rusts-build-speed-with-sccache Only ends up using ~200 MB of cache, and speeds up the build quite a bit.
1 parent a2a4539 commit 7d6e71d

3 files changed

Lines changed: 57 additions & 22 deletions

File tree

.github/workflows/coverage.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ jobs:
7676
uses: actions/cache@v3
7777
with:
7878
path: |
79-
~/.cargo/registry/index/
80-
~/.cargo/registry/cache/
81-
~/.cargo/git/db/
82-
# invalidate the cache once per month
83-
key: cargo-registry-${{ steps.get-month.outputs.month }}
79+
~/.cargo/registry
80+
~/.cargo/git
81+
# key *and* restore-keys include the month to force a monthly reset instead
82+
# of unbounded growth.
83+
key: cargo-registry-${{ steps.get-month.outputs.month }}-${{ hashFiles('src/Cargo.lock') }}
8484
restore-keys: |
85-
cargo-registry-
85+
cargo-registry-${{ steps.get-month.outputs.month }}
8686
8787
- name: Build
8888
run: . ci/container_scripts/build_and_install.sh

.github/workflows/extra_tests.yml

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,18 @@ env:
3333

3434
jobs:
3535
build_shadow:
36+
env:
37+
# used by cargo
38+
RUSTC_WRAPPER: sccache
39+
SCCACHE_CACHE_SIZE: 2G
40+
SCCACHE_DIR: /home/runner/.cache/sccache
41+
# SCCACHE_RECACHE: 1 # Uncomment this to clear cache, then comment it back out
3642
runs-on: ubuntu-20.04
3743
container:
3844
# Should match env.CONTAINER.
3945
image: ubuntu:22.04
4046
steps:
47+
- run: apt-get update
4148
- name: Checkout shadow
4249
uses: actions/checkout@v3
4350
with:
@@ -48,31 +55,59 @@ jobs:
4855
# See https://github.com/shadow/shadow/issues/2166
4956
ref: ${{ github.event.pull_request.head.sha }}
5057
persist-credentials: false
51-
- name: Get month
52-
id: get-month
53-
run: |
54-
echo "month=$(/bin/date -u "+%Y%m")" >> $GITHUB_OUTPUT
5558
- name: Install dependencies
5659
run: |
5760
cd shadow
5861
. ci/container_scripts/install_deps.sh
5962
. ci/container_scripts/install_extra_deps.sh
6063
echo "$HOME/.cargo/bin" >> $GITHUB_PATH
64+
- name: Get month
65+
id: get-month
66+
run: |
67+
echo "month=$(/bin/date -u "+%Y%m")" >> $GITHUB_OUTPUT
6168
- name: Restore cargo registry cache
6269
uses: actions/cache@v3
6370
with:
6471
path: |
65-
~/.cargo/registry/index/
66-
~/.cargo/registry/cache/
67-
~/.cargo/git/db/
68-
# invalidate the cache once per month
69-
key: cargo-registry-${{ steps.get-month.outputs.month }}
72+
~/.cargo/registry
73+
~/.cargo/git
74+
key: cargo-registry-${{ steps.get-month.outputs.month }}-${{ hashFiles('shadow/src/Cargo.lock') }}
7075
restore-keys: |
71-
cargo-registry-
76+
cargo-registry-${{ steps.get-month.outputs.month }}
77+
- name: Get rust version
78+
id: get-rustv
79+
run: |
80+
echo rustv=\"$(rustc --version)\" >> $GITHUB_OUTPUT
81+
- name: Restore sccache cache
82+
uses: actions/cache@v3
83+
continue-on-error: false
84+
with:
85+
path: ${{ env.SCCACHE_DIR }}
86+
key: sccache-${{ steps.get-month.outputs.month }}-${{ steps.get-rustv.outputs.rustv }}-${{ env.CONTAINER }}-${{ env.CC }}-${{ env.BUILDTYPE }}-${{ hashFiles('shadow/src/Cargo.lock') }}
87+
restore-keys: |
88+
sccache-${{ steps.get-month.outputs.month }}-${{ steps.get-rustv.outputs.rustv }}-${{ env.CONTAINER }}-${{ env.CC }}-${{ env.BUILDTYPE }}-
89+
- name: Install sccache
90+
env:
91+
LINK: https://github.com/mozilla/sccache/releases/download
92+
SCCACHE_VERSION: v0.3.1
93+
run: |
94+
apt-get install -y curl
95+
SCCACHE_FILE=sccache-$SCCACHE_VERSION-x86_64-unknown-linux-musl
96+
mkdir -p $HOME/.local/bin
97+
curl -L "$LINK/$SCCACHE_VERSION/$SCCACHE_FILE.tar.gz" | tar xz
98+
mv -f $SCCACHE_FILE/sccache $HOME/.local/bin/sccache
99+
chmod +x $HOME/.local/bin/sccache
100+
echo "$HOME/.local/bin" >> $GITHUB_PATH
101+
- name: Start sccache server
102+
run: sccache --start-server
72103
- name: Build shadow
73104
run: |
74105
cd shadow
75106
. ci/container_scripts/build_and_install.sh
107+
- name: Print sccache stats
108+
run: sccache --show-stats
109+
- name: Stop sccache server
110+
run: sccache --stop-server || true
76111
# We need to wrap in a tarball to preserve permissions.
77112
# We're grabbing the source directory and a lot of the build
78113
# directory here, since there are scripts and generated

.github/workflows/run_tests.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ jobs:
109109
uses: actions/cache@v3
110110
with:
111111
path: |
112-
~/.cargo/registry/index/
113-
~/.cargo/registry/cache/
114-
~/.cargo/git/db/
115-
# invalidate the cache once per month
116-
key: cargo-registry-${{ steps.get-month.outputs.month }}
112+
~/.cargo/registry
113+
~/.cargo/git
114+
# key *and* restore-keys include the month to force a monthly reset instead
115+
# of unbounded growth.
116+
key: cargo-registry-${{ steps.get-month.outputs.month }}-${{ hashFiles('src/Cargo.lock') }}
117117
restore-keys: |
118-
cargo-registry-
118+
cargo-registry-${{ steps.get-month.outputs.month }}
119119
120120
- name: Build
121121
run: . ci/container_scripts/build_and_install.sh

0 commit comments

Comments
 (0)