Skip to content
This repository was archived by the owner on Nov 1, 2023. It is now read-only.

Commit 28e36ef

Browse files
authored
Rust build caching improvements & fixes (#3197)
Caches are getting too big and we are exceeding the 10GB limit, leading to cache churning. 1. Try to make the caches smaller by using `Swatinem/rust-cache`, which is smarter about what gets cached. - After doing this it turns out we don't really need `sccache` any more, it has very little impact upon compile times as the cache hit ratio is low. So remove it, to reduce complexity of build and size of build caches. 2. Also fix artifact caching which had been broken by a version format change (4956cf5).
1 parent b1b7cf0 commit 28e36ef

File tree

6 files changed

+41
-90
lines changed

6 files changed

+41
-90
lines changed

.github/workflows/ci.yml

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ concurrency:
1616

1717
env:
1818
CARGO_TERM_COLOR: always
19-
SCCACHE_DIR: ${{github.workspace}}/sccache/
20-
SCCACHE_CACHE_SIZE: 1G
21-
ACTIONS_CACHE_KEY_DATE: 2023-04-19
19+
ACTIONS_CACHE_KEY_DATE: 2023-06-19
2220
CI: true
2321

2422
jobs:
@@ -54,10 +52,12 @@ jobs:
5452
- name: Get Rust version & build version
5553
shell: bash
5654
run: |
55+
set -x
5756
echo "RUST_VERSION=$(rustc --version)" >> $GITHUB_OUTPUT
5857
VERSION=$(src/ci/get-version.sh)
59-
# it's a release build if version doesn't have a hyphen in it
60-
IS_RELEASE_BUILD=$(if [[ "$VERSION" =~ '-' ]]; then echo 'false'; else echo 'true'; fi)
58+
# it's a release build if version doesn't have a plus in it
59+
# NB: this should stay in sync with version generation in get-version.sh
60+
IS_RELEASE_BUILD=$(if [[ "$VERSION" =~ '+' ]]; then echo 'false'; else echo 'true'; fi)
6161
echo "RELEASE_BUILD=$IS_RELEASE_BUILD" >> $GITHUB_OUTPUT
6262
id: rust-version
6363
- name: Rust artifact cache
@@ -71,35 +71,22 @@ jobs:
7171
path: artifacts
7272
key: agent-artifacts|${{ join(matrix.os, ':') }}|${{steps.rust-version.outputs.RUST_VERSION}}|${{ env.ACTIONS_CACHE_KEY_DATE }}|${{ hashFiles('src/agent/**/*') }}|${{hashFiles('src/ci/agent.sh')}}
7373
# note: also including the ACTIONS_CACHE_KEY_DATE to rebuild if the Prereq Cache is invalidated
74+
- name: Rust build cache
75+
id: rust-build-cache
76+
uses: Swatinem/rust-cache@v2
77+
if: steps.cache-agent-artifacts.outputs.cache-hit != 'true'
78+
with:
79+
key: ${{env.ACTIONS_CACHE_KEY_DATE}} # additional key for cache-busting
80+
workspaces: src/agent
7481
- name: Linux Prereqs
7582
if: runner.os == 'Linux' && steps.cache-agent-artifacts.outputs.cache-hit != 'true'
7683
run: |
7784
sudo apt-get -y update
7885
sudo apt-get -y install libssl-dev libunwind-dev build-essential pkg-config
79-
- name: Rust Prereq Cache
80-
if: steps.cache-agent-artifacts.outputs.cache-hit != 'true'
81-
uses: actions/cache@v3
82-
id: cache-rust-prereqs
83-
with:
84-
path: |
85-
~/.cargo/registry
86-
~/.cargo/git
87-
~/.cargo/bin
88-
key: rust|${{ join(matrix.os, ':') }}|${{steps.rust-version.outputs.RUST_VERSION}}|${{ env.ACTIONS_CACHE_KEY_DATE }}
8986
- name: Install Rust Prereqs
90-
if: steps.cache-rust-prereqs.outputs.cache-hit != 'true' && steps.cache-agent-artifacts.outputs.cache-hit != 'true'
87+
if: steps.rust-build-cache.outputs.cache-hit != 'true' && steps.cache-agent-artifacts.outputs.cache-hit != 'true'
9188
shell: bash
9289
run: src/ci/rust-prereqs.sh
93-
- name: Rust Compile Cache
94-
if: steps.cache-agent-artifacts.outputs.cache-hit != 'true'
95-
uses: actions/cache@v3
96-
with:
97-
path: |
98-
sccache
99-
src/agent/target
100-
key: agent|${{ join(matrix.os, ':') }}|${{steps.rust-version.outputs.RUST_VERSION}}|${{ env.ACTIONS_CACHE_KEY_DATE }}|${{ hashFiles('src/agent/Cargo.lock') }}
101-
restore-keys: |
102-
agent|${{ join(matrix.os, ':') }}|${{steps.rust-version.outputs.RUST_VERSION}}|${{ env.ACTIONS_CACHE_KEY_DATE }}|
10390
- run: src/ci/agent.sh
10491
if: steps.cache-agent-artifacts.outputs.cache-hit != 'true'
10592
shell: bash
@@ -237,29 +224,16 @@ jobs:
237224
runs-on: ubuntu-20.04
238225
steps:
239226
- uses: actions/checkout@v3
240-
- name: Rust Prereq Cache
241-
uses: actions/cache@v3
242-
id: cache-rust-prereqs
227+
- name: Rust build cache
228+
id: rust-build-cache
229+
uses: Swatinem/rust-cache@v2
243230
with:
244-
path: |
245-
~/.cargo/registry
246-
~/.cargo/git
247-
~/.cargo/bin
248-
key: rust-${{ runner.os }}-${{ env.ACTIONS_CACHE_KEY_DATE }}
231+
key: ${{env.ACTIONS_CACHE_KEY_DATE}} # additional key for cache-busting
232+
workspaces: src/proxy-manager
249233
- name: Install Rust Prereqs
250-
if: steps.cache-rust-prereqs.outputs.cache-hit != 'true'
234+
if: steps.rust-build-cache.outputs.cache-hit != 'true'
251235
shell: bash
252236
run: src/ci/rust-prereqs.sh
253-
- name: Rust Compile Cache
254-
uses: actions/cache@v3
255-
with:
256-
path: |
257-
sccache
258-
src/proxy-manager/target
259-
key: proxy-${{ runner.os }}-${{ hashFiles('src/proxy-manager/Cargo.lock') }}-${{ env.ACTIONS_CACHE_KEY_DATE }}
260-
restore-keys: |
261-
proxy-${{ runner.os }}-${{ hashFiles('src/proxy-manager/Cargo.lock') }}-
262-
proxy-${{ runner.os }}-
263237
- run: src/ci/proxy.sh
264238
- uses: actions/upload-artifact@v3
265239
with:
@@ -301,6 +275,7 @@ jobs:
301275

302276
- name: Build Service
303277
run: |
278+
set -x
304279
version=$(./src/ci/get-version.sh)
305280
306281
cd src/ApiService/
@@ -310,10 +285,12 @@ jobs:
310285
echo ${GITHUB_SHA} | tee ApiService/onefuzzlib/git.version
311286
312287
# stamp the build with version
313-
# note that version might have a suffix of '-{sha}' from get-version.sh
314-
if [[ "$version" =~ '-' ]]; then
288+
# note that version might have a suffix of '+{sha}' from get-version.sh
289+
290+
# NB: ensure this stays in sync with get-version.sh
291+
if [[ "$version" =~ '+' ]]; then
315292
# if it has a suffix, split it into two parts
316-
dotnet build -warnaserror --configuration Release /p:VersionPrefix=${version%-*} /p:VersionSuffix=${version#*-}
293+
dotnet build -warnaserror --configuration Release /p:VersionPrefix=${version%+*} /p:VersionSuffix=${version#*+}
317294
else
318295
dotnet build -warnaserror --configuration Release /p:VersionPrefix=${version}
319296
fi

src/agent/onefuzz/src/libfuzzer.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,11 @@ impl LibFuzzer {
173173
Ok(cmd)
174174
}
175175

176-
async fn verify_inner(&self, check_fuzzer_help: bool, inputs: &[&Path]) -> Result<()> {
176+
pub(crate) async fn verify_once(
177+
&self,
178+
check_fuzzer_help: bool,
179+
inputs: &[&Path],
180+
) -> Result<()> {
177181
if check_fuzzer_help {
178182
self.check_help().await?;
179183
}
@@ -222,7 +226,7 @@ impl LibFuzzer {
222226
let mut attempts = 1;
223227
loop {
224228
let result = self
225-
.verify_inner(check_fuzzer_help, inputs.unwrap_or_default())
229+
.verify_once(check_fuzzer_help, inputs.unwrap_or_default())
226230
.await;
227231

228232
match result {
@@ -519,22 +523,22 @@ mod tests {
519523

520524
// verify catching bad exits with -help=1
521525
assert!(
522-
fuzzer.verify(true, None).await.is_err(),
526+
fuzzer.verify_once(true, &[]).await.is_err(),
523527
"checking false with -help=1"
524528
);
525529

526530
// verify catching bad exits with inputs
527531
assert!(
528532
fuzzer
529-
.verify(false, Some(&[temp_setup_dir.path()]))
533+
.verify_once(false, &[temp_setup_dir.path()])
530534
.await
531535
.is_err(),
532536
"checking false with basic input"
533537
);
534538

535539
// verify catching bad exits with no inputs
536540
assert!(
537-
fuzzer.verify(false, None).await.is_err(),
541+
fuzzer.verify_once(false, &[]).await.is_err(),
538542
"checking false without inputs"
539543
);
540544

@@ -553,22 +557,22 @@ mod tests {
553557
);
554558
// verify good exits with -help=1
555559
assert!(
556-
fuzzer.verify(true, None).await.is_ok(),
560+
fuzzer.verify_once(true, &[]).await.is_ok(),
557561
"checking true with -help=1"
558562
);
559563

560564
// verify good exits with inputs
561565
assert!(
562566
fuzzer
563-
.verify(false, Some(&[temp_setup_dir.path()]))
567+
.verify_once(false, &[temp_setup_dir.path()])
564568
.await
565569
.is_ok(),
566570
"checking true with basic inputs"
567571
);
568572

569573
// verify good exits with no inputs
570574
assert!(
571-
fuzzer.verify(false, None).await.is_ok(),
575+
fuzzer.verify_once(false, &[]).await.is_ok(),
572576
"checking true without inputs"
573577
);
574578

src/ci/agent.sh

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,6 @@ exists() {
1111
[ -e "$1" ]
1212
}
1313

14-
SCCACHE=$(which sccache || echo '')
15-
if [ -n "$SCCACHE" ]; then
16-
# only set RUSTC_WRAPPER if sccache exists
17-
export RUSTC_WRAPPER=$SCCACHE
18-
# incremental interferes with (disables) sccache
19-
export CARGO_INCREMENTAL=0
20-
else
21-
# only set CARGO_INCREMENTAL on non-release builds
22-
#
23-
# This speeds up build time, but makes the resulting binaries slightly slower.
24-
# https://doc.rust-lang.org/cargo/reference/profiles.html?highlight=incremental#incremental
25-
if [ "${GITHUB_REF}" != "" ]; then
26-
TAG_VERSION=${GITHUB_REF#refs/tags/}
27-
if [ ${TAG_VERSION} == ${GITHUB_REF} ]; then
28-
export CARGO_INCREMENTAL=1
29-
fi
30-
fi
31-
fi
32-
3314
platform=$(uname --kernel-name --machine)
3415
platform=${platform// /-} # replace spaces with dashes
3516
rel_output_dir="artifacts/agent-$platform"
@@ -45,11 +26,6 @@ cargo clippy --version
4526
cargo fmt --version
4627
cargo license --version
4728

48-
# unless we're doing incremental builds, start clean during CI
49-
if [ X${CARGO_INCREMENTAL} == X ]; then
50-
cargo clean
51-
fi
52-
5329
cargo fmt -- --check
5430

5531
cargo deny -L error check
@@ -69,10 +45,6 @@ cargo llvm-cov nextest --all-targets --locked --workspace --lcov --output-path "
6945
# TODO: once Salvo is integrated, this can get deleted
7046
cargo build --release --locked --manifest-path ./onefuzz-telemetry/Cargo.toml --all-features
7147

72-
if [ -n "$SCCACHE" ]; then
73-
sccache --show-stats
74-
fi
75-
7648
echo "Checking dependencies of binaries"
7749

7850
"$script_dir/check-dependencies.sh"

src/ci/get-version.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ BASE_VERSION=$(cat ${SCRIPT_DIR}/../../CURRENT_VERSION)
1010
BRANCH=$(git rev-parse --abbrev-ref HEAD)
1111
GIT_HASH=$(git rev-parse HEAD)
1212

13+
# NB: ensure this code stays in sync the with version test in
14+
# .github/workflows/ci.yml
15+
1316
if [ "${GITHUB_REF}" != "" ]; then
1417
TAG_VERSION=${GITHUB_REF#refs/tags/}
1518

src/ci/proxy.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
set -ex
77

8-
#export RUSTC_WRAPPER=$(which sccache)
9-
108
mkdir -p artifacts/proxy
119

1210
cd src/proxy-manager

src/ci/rust-prereqs.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,4 @@
55

66
set -ex
77

8-
cargo install --locked sccache cargo-license@0.4.2 cargo-llvm-cov cargo-deny cargo-insta cargo-nextest
9-
10-
# sccache --start-server
11-
# export RUSTC_WRAPPER=$(which sccache)
8+
cargo install --locked cargo-license@0.4.2 cargo-llvm-cov cargo-deny cargo-insta cargo-nextest

0 commit comments

Comments
 (0)