Say what each not-cacheable reason means and what to change; explain a low Rust hit rate

Each reason now carries a plain name, meaning, action and a fixable flag (checked
against sccache 0.18.0), shown with its count in the reasons panel, fixable first.
The low-hit-rate problem names the fixable loss and the Rust hash-key inputs.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Sonnet 5.5 committed 2026-11-28 17:45:38 -07:00
1 parent 3ede855fbb
commit 0f4df265a5
8 files changed
+498 -52

No files matched your search

+289
View File
@@ -0,0 +1,289 @@
//! What each "not cacheable" reason means and what to do about it.
//!
//! Every statement here was checked against the sccache 0.18.0 source (the file and line
//! of the code that produces each reason is beside its entry) and, where a command is
//! named, by running it. A reason that is not listed is shown as sccache words it, with
//! "sccache does not say more": we do not guess.
use sccache_dash_api::NotCached;
/// What we know about one reason.
pub struct Advice {
pub name: String,
pub meaning: String,
pub action: String,
pub fixable: bool,
}
fn entry(name: &str, meaning: &str, action: &str, fixable: bool) -> Advice {
Advice {
name: name.to_owned(),
meaning: meaning.to_owned(),
action: action.to_owned(),
fixable,
}
}
/// The advice for `reason`, exactly as sccache's "Non-cacheable reasons" prints it.
pub fn advise(reason: &str) -> Advice {
let expected = |name: &str, meaning: &str, why: &str| {
entry(name, meaning, &format!("Nothing to do, this is expected. {why}"), false)
};
match reason {
// rust.rs:1208 `("incremental", _) => cannot_cache!("incremental")`
"incremental" => entry(
"Incremental compilation is on",
"rustc was told to compile incrementally (-C incremental=...), and sccache 0.18.0 never caches those compiles; its own note says rustc in incremental mode is likely to be faster than fetching from the cache.",
"To cache them, turn incremental off: set CARGO_INCREMENTAL=0 in the environment, or incremental = false under [profile.dev] in Cargo.toml, or incremental = false under [build] in ~/.cargo/config.toml. The trade-off: incremental makes rebuilds of your own crates faster, while the cache helps clean builds and dependency builds.",
true,
),
// rust.rs:1187 and rust.rs:1298: only lib, rlib and staticlib are cached.
"crate-type" => expected(
"Programs and libraries that get linked",
"sccache only caches the crate types lib, rlib and staticlib. Anything else (bin, which includes build scripts and test programs, plus proc-macro, dylib and cdylib), or a call with no crate type, is not cached because rustc runs the system linker and sccache cannot know all of the linker's inputs.",
"Cargo builds each of these in every project, and they cannot be cached.",
),
// rust.rs:1026 `flag!("-", TooHardFlag)`, gcc.rs:190
"-" => expected(
"Source read from standard input",
"The compiler was given a bare - (read the source from standard input), which sccache does not cache.",
"Cargo makes one such call per run to ask rustc about the target it builds for.",
),
// rust.rs:1062 `take_arg!("-o", ..., TooHardPath)`; rust.rs doc: "We don't support `-o file`"
"-o" => entry(
"rustc called with -o",
"rustc was given -o <file>, which sccache 0.18.0 does not cache; it needs --out-dir (cargo passes that for normal builds).",
"Nothing to change in sccache. The tool that called rustc directly would have to use --out-dir instead.",
false,
),
// rust.rs:1280 req!(input) (macro at 1269-1276). Seen in the log as CannotCache(missing input): ["-vV"].
"missing input" => expected(
"rustc call with no source file",
"A rustc call had no source file on its command line, so there was nothing to cache.",
"Cargo starts every run with `rustc -vV` to read the compiler's version, and that is counted here.",
),
// rust.rs:1282 req!(output_dir)
"missing output_dir" => expected(
"rustc call without --out-dir",
"A rustc call had no --out-dir, which sccache requires.",
"It comes from the tool that called rustc; cargo's normal builds pass --out-dir.",
),
// rust.rs:1283 req!(emit)
"missing emit" => expected(
"rustc call without --emit",
"A rustc call had no --emit, which sccache requires.",
"It comes from the tool that called rustc; cargo's normal builds pass --emit.",
),
// rust.rs:1284 req!(crate_name)
"missing crate_name" => expected(
"rustc call without --crate-name",
"A rustc call had no --crate-name, which sccache requires.",
"It comes from the tool that called rustc; cargo's normal builds pass --crate-name.",
),
// rust.rs:1173
"more than one --emit" => expected(
"rustc call with --emit given twice",
"rustc was given --emit more than once, which sccache does not handle.",
"It comes from the tool that called rustc.",
),
// rust.rs:1303 ALLOWED_EMIT = link, metadata, dep-info
"unsupported --emit" => expected(
"rustc asked for extra output",
"rustc was asked to emit something other than link, metadata and dep-info (for example asm or llvm-ir), which sccache does not cache.",
"Those outputs were asked for on purpose.",
),
// rust.rs:1198
"extra-filename" => expected(
"-C extra-filename without a value",
"rustc was given -C extra-filename with no value, which sccache cannot use.",
"It comes from the tool that called rustc.",
),
// rust.rs:1232 ArgTarget::Unsure, rust.rs:955-980
"target unsure" => entry(
"Ambiguous --target",
"The --target value is a plain name, but a file with that name plus .json exists, so sccache cannot tell whether a built-in target or your target file is meant.",
"Pass the target file by its path ending in .json (--target path/to/target.json); sccache then hashes the file's contents.",
true,
),
// rust.rs:1048 `take_arg!("--sysroot", ..., TooHardPath)`
"--sysroot" => entry(
"rustc given --sysroot",
"rustc was given --sysroot, which sccache 0.18.0 does not cache.",
"If it comes from RUSTFLAGS or build.rustflags and you do not need a custom sysroot, remove it.",
true,
),
// rust.rs:1248, gcc.rs:666, msvc.rs:791
"multiple input files" => entry(
"Several source files in one compiler call",
"One compiler call named more than one source file, and sccache caches only one source file per call.",
"If your build passes several files to one compiler call, change it to compile one file per call.",
true,
),
// gcc.rs:362 and diab.rs:206
"@" => expected(
"Option value starting with @",
"An option's value began with @: an @file that sccache could not read or expand, or one attached straight to an option. sccache gives up on both.",
"It comes from how the build passes its flags.",
),
// gcc.rs:466
"-x" => entry(
"Unknown language after -x",
"The call said -x <language> with a language sccache does not know.",
"Use a language sccache knows (c, c++, objective-c, objective-c++, assembler, assembler-with-cpp, cu, cuda, hip, or a header kind) or drop -x.",
true,
),
// gcc.rs:472
"multiple different -arch, and SCCACHE_CACHE_MULTIARCH not set" => entry(
"Several -arch values in one call",
"The call had more than one different -arch, and sccache only caches that when SCCACHE_CACHE_MULTIARCH is set.",
"Set SCCACHE_CACHE_MULTIARCH=1 in the environment sccache's server starts with.",
true,
),
// gcc.rs:674, msvc.rs:802
"no input file" => expected(
"C/C++ call with no source file",
"The compiler call had no source file, so there was nothing to cache.",
"It comes from the tool that called the compiler.",
),
// gcc.rs:688, msvc.rs:799
"unknown source language" => entry(
"Source file type not recognised",
"sccache could not tell the language from the file's extension and the call had no -x.",
"Give the file a standard extension, or add -x c or -x c++ to the call.",
true,
),
// gcc.rs:414
"unknown module output format" => expected(
"Unknown clang module output option",
"A clang module-output option had a value sccache does not understand.",
"Only clang modules builds do this.",
),
// gcc.rs:593-604
"Can't handle complex arguments through clang"
| "Can't handle Raw arguments with -Xclang"
| "Can't handle UnknownFlag arguments with -Xclang" => expected(
"Complicated -Xclang options",
"The call passes options through -Xclang that sccache cannot follow.",
"Remove the -Xclang options if you do not need them.",
),
// msvc.rs:610, msvc.rs:911, msvc.rs:831
"output to nul" | "shared pdb" | "invalid input file" => expected(
"Visual C++ call sccache cannot cache",
"The Visual C++ call writes its output to nul, uses a program database file shared by every compile in the folder (-Zi or -ZI without -Fd), or has an input path with no file name; sccache caches none of these.",
"These come from the call's own options.",
),
// gcc.rs:201-214 TooHardFlag: preprocess-only and dependency-only calls
"-E" | "-M" | "-MM" | "-P" => expected(
"Preprocess-only or dependency-only call",
"The call only preprocesses the source or only writes dependency rules (-E, -M, -MM, -P), so it produces no object file; sccache lists these options as too hard to cache.",
"Your build tool runs these calls on purpose.",
),
// gcc.rs:352, rust.rs:1147, msvc.rs:590
"argument parse" => entry(
"A compiler option sccache could not read",
"sccache could not parse one of the compiler's arguments.",
"sccache does not say more in its counters; run the server with SCCACHE_LOG=debug to see the call.",
false,
),
// gcc.rs:375, clang.rs:192-295, msvc.rs:593: the reason is the option's own name.
other if other.starts_with('-') && other.len() > 1 => entry(
&format!("Option {other} is never cached"),
&format!(
"The compiler call contains {other}, which sccache's option table lists as too hard to cache, so every call with it runs uncached."
),
"If you do not need the option, remove it from the build's flags.",
true,
),
other => entry(
other,
"sccache does not say more.",
"",
false,
),
}
}
/// Dress counted reasons with their advice: fixable ones first, then expected ones, each
/// largest first.
pub fn describe(reasons: &[(String, u64)]) -> Vec<NotCached> {
let mut out: Vec<NotCached> = reasons
.iter()
.map(|(reason, count)| {
let a = advise(reason);
NotCached {
reason: reason.clone(),
count: *count,
name: a.name,
meaning: a.meaning,
action: a.action,
fixable: a.fixable,
}
})
.collect();
out.sort_by(|a, b| {
b.fixable
.cmp(&a.fixable)
.then_with(|| b.count.cmp(&a.count))
.then_with(|| a.reason.cmp(&b.reason))
});
out
}
/// How many compiles were lost to reasons that can be fixed, and the biggest of them.
pub fn fixable_loss(reasons: &[NotCached]) -> Option<(u64, &NotCached)> {
let top = reasons.iter().filter(|r| r.fixable).max_by_key(|r| r.count)?;
Some((reasons.iter().filter(|r| r.fixable).map(|r| r.count).sum(), top))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn incremental_says_how_to_turn_it_off_and_what_it_costs() {
let a = advise("incremental");
assert!(a.fixable);
assert!(a.action.contains("CARGO_INCREMENTAL=0"));
assert!(a.action.contains("[profile.dev]"));
assert!(a.action.contains("[build]"));
assert!(a.action.contains("trade-off"));
}
#[test]
fn crate_type_is_expected_and_names_what_is_cached() {
let a = advise("crate-type");
assert!(!a.fixable);
assert!(a.action.starts_with("Nothing to do"));
assert!(a.meaning.contains("lib, rlib and staticlib"));
}
#[test]
fn an_unknown_reason_is_shown_as_it_came() {
let a = advise("something new in 0.19");
assert_eq!(a.name, "something new in 0.19");
assert_eq!(a.meaning, "sccache does not say more.");
assert!(!a.fixable);
}
#[test]
fn an_option_name_is_a_fixable_reason_but_the_known_dashes_are_not() {
assert!(advise("-fplugin").fixable);
assert!(!advise("-").fixable);
assert!(!advise("-o").fixable);
}
#[test]
fn fixable_reasons_come_first_then_the_biggest() {
let out = describe(&[
("crate-type".to_owned(), 1548),
("-".to_owned(), 156),
("incremental".to_owned(), 1642),
("multiple input files".to_owned(), 63),
]);
let order: Vec<&str> = out.iter().map(|r| r.reason.as_str()).collect();
assert_eq!(order, ["incremental", "multiple input files", "crate-type", "-"]);
let (lost, top) = fixable_loss(&out).unwrap();
assert_eq!((lost, top.reason.as_str()), (1705, "incremental"));
assert!(fixable_loss(&out[2..]).is_none());
}
}
+1
View File
@@ -1,6 +1,7 @@
//! The server half of sccache-dash: it asks sccache for its counters, keeps their history and
//! serves the page that draws them.
pub mod advice;
pub mod app;
pub mod history;
pub mod http;
+63 -14
View File
@@ -2,11 +2,12 @@
//! after every poll and kept; a request never waits for the database.
use sccache_dash_api::{
Bucket, BuildTimes, Language, Problem, Range, Server, Severity, Status, TimeSaved, Timing, View,
Bucket, BuildTimes, Language, NotCached, Problem, Range, Server, Severity, Status, TimeSaved, Timing, View,
};
use sccache_dash_stats::{LanguageHits, Observation, Resampled, Saved};
use serde_json::Value;
use crate::advice;
use crate::app::State;
use crate::history::{BucketLangs, BucketRow, History, Row};
use crate::pretty;
@@ -433,6 +434,35 @@ fn buckets(chart: Chart<'_>, now: i64, builds: &Builds, hit_read: f64, live: boo
out
}
/// What a low hit rate means and what to try. Everything said about why a Rust compile misses
/// is what sccache 0.18.0's hash key for Rust (src/compiler/rust.rs, `generate_hash_key`) is
/// made of, and was checked by building the same crate twice, in two directories, through a
/// throwaway server.
fn low_hit_rate_detail(name: &str, hits: u64, n: u64, reasons: &[NotCached]) -> String {
let mut text = format!(
"Only {}% of {} {name} compiles came from the cache, so most were rebuilt.",
pretty::pct(hits as f64 / n as f64),
pretty::thousands(n)
);
if let Some((lost, top)) = advice::fixable_loss(reasons) {
text.push_str(&format!(
" Not counted in that rate: {} compiles were not cached at all for reasons you can fix; the biggest is \"{}\" ({}). {}",
pretty::thousands(lost),
top.name,
pretty::thousands(top.count),
top.action
));
}
if name == "Rust" {
text.push_str(
" sccache 0.18.0 misses when anything in the compile's hash key differs from an earlier build: the rustc version, every argument (RUSTFLAGS, features, profile, cargo's metadata hash), every CARGO_* environment variable except CARGO_MAKEFLAGS, CARGO_BUILD_JOBS, CARGO_ENCODED_RUSTFLAGS and CARGO_REGISTRIES_*, the environment variables rustc lists in its dep-info, the contents of the source files and dependency libraries, and the directory the compile runs in. So the same crate built in two directories (two git worktrees) misses, and so does a different CARGO_TARGET_DIR set in the environment; the --target-dir flag does not.",
);
} else {
text.push_str(" sccache's counters do not say why a compile missed.");
}
text
}
fn problems(c: &Counters, server: &Server) -> Vec<Problem> {
let t = &c.totals;
let (mut errors, mut warnings) = (Vec::new(), Vec::new());
@@ -492,15 +522,12 @@ fn problems(c: &Counters, server: &Server) -> Vec<Problem> {
.map(|(name, v)| (name, v[0], v[0] + v[1]))
.collect();
low.sort_by(|a, b| (a.1 * b.2).cmp(&(b.1 * a.2)).then_with(|| a.0.cmp(b.0)));
let reasons = advice::describe(&c.not_cached);
for (name, hits, n) in low {
warnings.push(problem(
Severity::Warning,
format!("{name} hit rate {}%", pretty::pct(hits as f64 / n as f64)),
format!(
"Only {}% of {} {name} compiles came from the cache, so most were rebuilt; a different toolchain, compiler flags or environment makes sccache miss, so compare those between two builds.",
pretty::pct(hits as f64 / n as f64),
pretty::thousands(n)
),
low_hit_rate_detail(name, hits, n, &reasons),
));
}
if let (Some(size), Some(max)) = (server.cache_size, server.max_cache_size) {
@@ -678,8 +705,7 @@ fn compute(
.cmp(&(a.hits + a.misses))
.then_with(|| a.name.cmp(&b.name))
});
let mut not_cached = counters.not_cached.clone();
not_cached.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
let not_cached = advice::describe(&counters.not_cached);
let mut view = View {
range: Some(range),
@@ -1052,11 +1078,15 @@ mod tests {
errors: 0
}
);
let order: Vec<&str> = v.not_cached.iter().map(|r| r.reason.as_str()).collect();
assert_eq!(
v.not_cached[0],
("crate-type".to_owned(), 621),
"largest reason first"
order,
["incremental", "multiple input files", "crate-type", "-o", "-", "missing input", "missing emit"],
"reasons you can fix first, then the expected ones, each largest first"
);
assert_eq!((v.not_cached[0].count, v.not_cached[0].fixable), (105, true));
assert_eq!(v.not_cached[2].count, 621);
assert!(!v.not_cached[2].fixable);
assert_eq!(v.raw, serde_json::from_str::<Value>(LIVE).unwrap());
}
@@ -1450,7 +1480,25 @@ mod tests {
);
assert!(rate(vec![("Rust", [4, 46, 0])]).problems[0]
.detail
.starts_with("Only 8% of 50 Rust compiles came from the cache, so most were rebuilt;"));
.starts_with("Only 8% of 50 Rust compiles came from the cache, so most were rebuilt."));
let detail = seen(
|s| {
s.counters.langs = vec![("Rust".to_owned(), [4, 46, 0])];
s.counters.not_cached = vec![("incremental".to_owned(), 30), ("crate-type".to_owned(), 90)];
},
"0.18.0",
)
.problems[0]
.detail
.clone();
assert!(
detail.contains("30 compiles were not cached at all for reasons you can fix")
&& detail.contains("\"Incremental compilation is on\" (30)")
&& detail.contains("CARGO_INCREMENTAL=0")
&& detail.contains("the directory the compile runs in")
&& !detail.contains("Programs and libraries"),
"{detail}"
);
assert!(
rate(vec![("Rust", [19, 30, 0])]).problems.is_empty(),
"49 compiles is too few to say"
@@ -1545,8 +1593,9 @@ mod tests {
assert!(
all.problems
.iter()
.all(|p| p.detail.split(". ").count() == 1 && p.detail.ends_with('.')),
"each detail is one sentence: {:?}",
.all(|p| p.detail.ends_with('.')
&& (p.title.contains("hit rate") || p.detail.split(". ").count() == 1)),
"each detail is one sentence, except the hit-rate one that says what to change: {:?}",
all.problems
);
}