Skip to content

Commit a58656b

Browse files
committed
ensure SCCACHE_NO_CACHE calls aren't tracked as an uncacheable compilations in the stats
1 parent 2c91b14 commit a58656b

6 files changed

Lines changed: 57 additions & 40 deletions

File tree

src/cache/cache.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ pub enum Cache {
101101
Hit(CacheRead),
102102
/// Result was not found in cache.
103103
Miss,
104+
/// Do not cache the results of the compilation.
105+
None,
104106
/// Cache entry should be ignored, force compilation.
105107
Recache,
106108
}
@@ -110,6 +112,7 @@ impl fmt::Debug for Cache {
110112
match *self {
111113
Cache::Hit(_) => write!(f, "Cache::Hit(...)"),
112114
Cache::Miss => write!(f, "Cache::Miss"),
115+
Cache::None => write!(f, "Cache::None"),
113116
Cache::Recache => write!(f, "Cache::Recache"),
114117
}
115118
}

src/compiler/c.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,24 +1165,14 @@ impl<T: CommandCreatorSync, I: CCompilerImpl> Compilation<T> for CCompilation<I>
11651165
..
11661166
} = *self;
11671167

1168-
let (command, dist_command, cacheable) = compiler.generate_compile_commands(
1168+
compiler.generate_compile_commands(
11691169
path_transformer,
11701170
executable,
11711171
parsed_args,
11721172
cwd,
11731173
env_vars,
11741174
rewrite_includes_only,
1175-
)?;
1176-
1177-
let force_no_cache = env_vars
1178-
.iter()
1179-
.any(|(k, _v)| k.as_os_str() == "SCCACHE_NO_CACHE");
1180-
1181-
if force_no_cache {
1182-
Ok((command, dist_command, Cacheable::No))
1183-
} else {
1184-
Ok((command, dist_command, cacheable))
1185-
}
1175+
)
11861176
}
11871177

11881178
#[cfg(feature = "dist-client")]

src/compiler/compiler.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,9 @@ where
459459
// If `ForceRecache` is enabled, we won't check the cache.
460460
let start = Instant::now();
461461
let cache_status = async {
462-
if cache_control == CacheControl::ForceRecache {
462+
if cache_control == CacheControl::ForceNoCache {
463+
Ok(Cache::None)
464+
} else if cache_control == CacheControl::ForceRecache {
463465
Ok(Cache::Recache)
464466
} else {
465467
storage.get(&key).await
@@ -519,6 +521,14 @@ where
519521
);
520522
Ok(CacheLookupResult::Miss(MissType::Normal))
521523
}
524+
(Ok(Ok(Cache::None)), duration) => {
525+
debug!(
526+
"[{}]: Cache none in {}",
527+
out_pretty,
528+
fmt_duration_as_secs(&duration)
529+
);
530+
Ok(CacheLookupResult::Miss(MissType::ForcedNoCache))
531+
}
522532
(Ok(Ok(Cache::Recache)), duration) => {
523533
debug!(
524534
"[{}]: Cache recache in {}",
@@ -573,6 +583,15 @@ where
573583
);
574584
return Ok((CompileResult::CompileFailed, compiler_result));
575585
}
586+
if miss_type == MissType::ForcedNoCache {
587+
// Do not cache
588+
debug!(
589+
"[{}]: Compiled in {}, but not caching",
590+
out_pretty,
591+
fmt_duration_as_secs(&duration_compilation)
592+
);
593+
return Ok((CompileResult::NotCached, compiler_result));
594+
}
576595
if cacheable != Cacheable::Yes {
577596
// Not cacheable
578597
debug!(
@@ -991,6 +1010,8 @@ pub enum DistType {
9911010
pub enum MissType {
9921011
/// The compilation was not found in the cache, nothing more.
9931012
Normal,
1013+
/// Do not cache the results of the compilation.
1014+
ForcedNoCache,
9941015
/// Cache lookup was overridden, recompilation was forced.
9951016
ForcedRecache,
9961017
/// Cache took too long to respond.
@@ -1021,6 +1042,8 @@ pub enum CompileResult {
10211042
Duration, // Compilation time
10221043
Pin<Box<dyn Future<Output = Result<CacheWriteInfo>> + Send>>,
10231044
),
1045+
/// Not in cache and do not cache the results of the compilation.
1046+
NotCached,
10241047
/// Not in cache, but the compilation result was determined to be not cacheable.
10251048
NotCacheable,
10261049
/// Not in cache, but compilation failed.
@@ -1045,6 +1068,7 @@ impl fmt::Debug for CompileResult {
10451068
CompileResult::CacheMiss(ref m, ref dt, ref d, _) => {
10461069
write!(f, "CompileResult::CacheMiss({:?}, {:?}, {:?}, _)", d, m, dt)
10471070
}
1071+
CompileResult::NotCached => write!(f, "CompileResult::NotCached"),
10481072
CompileResult::NotCacheable => write!(f, "CompileResult::NotCacheable"),
10491073
CompileResult::CompileFailed => write!(f, "CompileResult::CompileFailed"),
10501074
}
@@ -1060,6 +1084,7 @@ impl PartialEq<CompileResult> for CompileResult {
10601084
(CompileResult::CacheMiss(m, dt, _, _), CompileResult::CacheMiss(n, dt2, _, _)) => {
10611085
m == n && dt == dt2
10621086
}
1087+
(&CompileResult::NotCached, &CompileResult::NotCached) => true,
10631088
(&CompileResult::NotCacheable, &CompileResult::NotCacheable) => true,
10641089
(&CompileResult::CompileFailed, &CompileResult::CompileFailed) => true,
10651090
_ => false,
@@ -1079,6 +1104,8 @@ pub enum Cacheable {
10791104
pub enum CacheControl {
10801105
/// Default caching behavior.
10811106
Default,
1107+
/// Do not cache the results of the compilation.
1108+
ForceNoCache,
10821109
/// Ignore existing cache entries, force recompilation.
10831110
ForceRecache,
10841111
}

src/compiler/rust.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,15 +1746,7 @@ impl<T: CommandCreatorSync> Compilation<T> for RustCompilation {
17461746
})
17471747
})();
17481748

1749-
let force_no_cache = env_vars
1750-
.iter()
1751-
.any(|(k, _v)| k.as_os_str() == "SCCACHE_NO_CACHE");
1752-
1753-
if force_no_cache {
1754-
Ok((CCompileCommand::new(command), dist_command, Cacheable::No))
1755-
} else {
1756-
Ok((CCompileCommand::new(command), dist_command, Cacheable::Yes))
1757-
}
1749+
Ok((CCompileCommand::new(command), dist_command, Cacheable::Yes))
17581750
}
17591751

17601752
#[cfg(feature = "dist-client")]

src/server.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use serde::{Deserialize, Serialize};
4040
use std::cell::Cell;
4141
use std::collections::{HashMap, HashSet};
4242
use std::env;
43-
use std::ffi::{OsStr, OsString};
43+
use std::ffi::OsString;
4444
use std::future::Future;
4545
use std::io::{self, Write};
4646
use std::marker::Unpin;
@@ -1320,11 +1320,13 @@ where
13201320
) -> Result<CompileFinished> {
13211321
self.stats.lock().await.requests_executed += 1;
13221322

1323-
let force_recache = env_vars
1324-
.iter()
1325-
.any(|(k, _v)| k.as_os_str() == OsStr::new("SCCACHE_RECACHE"));
1323+
let force_recache = env_vars.iter().any(|(k, _v)| k == "SCCACHE_RECACHE");
13261324

1327-
let cache_control = if force_recache {
1325+
let force_no_cache = env_vars.iter().any(|(k, _v)| k == "SCCACHE_NO_CACHE");
1326+
1327+
let cache_control = if force_no_cache {
1328+
CacheControl::ForceNoCache
1329+
} else if force_recache {
13281330
CacheControl::ForceRecache
13291331
} else {
13301332
CacheControl::Default
@@ -1389,6 +1391,7 @@ where
13891391
}
13901392
match miss_type {
13911393
MissType::Normal => {}
1394+
MissType::ForcedNoCache => {}
13921395
MissType::ForcedRecache => {
13931396
stats.forced_recaches += 1;
13941397
}
@@ -1404,6 +1407,9 @@ where
14041407
debug!("stats after compile result: {stats:?}");
14051408
cache_write = Some(future);
14061409
}
1410+
CompileResult::NotCached => {
1411+
debug!("[{}]: compile result: not cached", out_pretty);
1412+
}
14071413
CompileResult::NotCacheable => {
14081414
debug!("compile result: not cacheable");
14091415

tests/system.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -601,11 +601,11 @@ fn test_nvcc_cuda_compiles(compiler: &Compiler, tempdir: &Path) {
601601
assert_eq!(1, info.stats.compile_requests);
602602
assert_eq!(4, info.stats.requests_executed);
603603
assert_eq!(0, info.stats.cache_hits.all());
604-
assert_eq!(4, info.stats.cache_misses.all());
604+
assert_eq!(3, info.stats.cache_misses.all());
605605
assert_eq!(&1, info.stats.cache_misses.get("CUDA").unwrap());
606606
assert_eq!(&1, info.stats.cache_misses.get("PTX").unwrap());
607607
assert_eq!(&1, info.stats.cache_misses.get("CUBIN").unwrap());
608-
assert_eq!(&1, info.stats.cache_misses.get("C/C++").unwrap());
608+
assert!(info.stats.cache_misses.get("C/C++").is_none());
609609
let adv_cuda_key = adv_key_kind("cuda", compiler.name);
610610
let adv_ptx_key = adv_key_kind("ptx", compiler.name);
611611
let adv_cubin_key = adv_key_kind("cubin", compiler.name);
@@ -637,14 +637,14 @@ fn test_nvcc_cuda_compiles(compiler: &Compiler, tempdir: &Path) {
637637
assert_eq!(2, info.stats.compile_requests);
638638
assert_eq!(5, info.stats.requests_executed);
639639
assert_eq!(1, info.stats.cache_hits.all());
640-
assert_eq!(4, info.stats.cache_misses.all());
640+
assert_eq!(3, info.stats.cache_misses.all());
641641
assert_eq!(&1, info.stats.cache_hits.get("CUDA").unwrap());
642642
assert!(info.stats.cache_hits.get("PTX").is_none());
643643
assert!(info.stats.cache_hits.get("CUBIN").is_none());
644644
assert_eq!(&1, info.stats.cache_misses.get("CUDA").unwrap());
645645
assert_eq!(&1, info.stats.cache_misses.get("PTX").unwrap());
646646
assert_eq!(&1, info.stats.cache_misses.get("CUBIN").unwrap());
647-
assert_eq!(&1, info.stats.cache_misses.get("C/C++").unwrap());
647+
assert!(info.stats.cache_misses.get("C/C++").is_none());
648648
let adv_cuda_key = adv_key_kind("cuda", compiler.name);
649649
let adv_ptx_key = adv_key_kind("ptx", compiler.name);
650650
let adv_cubin_key = adv_key_kind("cubin", compiler.name);
@@ -681,14 +681,14 @@ fn test_nvcc_cuda_compiles(compiler: &Compiler, tempdir: &Path) {
681681
assert_eq!(3, info.stats.compile_requests);
682682
assert_eq!(9, info.stats.requests_executed);
683683
assert_eq!(2, info.stats.cache_hits.all());
684-
assert_eq!(7, info.stats.cache_misses.all());
684+
assert_eq!(5, info.stats.cache_misses.all());
685685
assert_eq!(&1, info.stats.cache_hits.get("CUDA").unwrap());
686686
assert!(info.stats.cache_hits.get("PTX").is_none());
687687
assert_eq!(&1, info.stats.cache_hits.get("CUBIN").unwrap());
688688
assert_eq!(&2, info.stats.cache_misses.get("CUDA").unwrap());
689689
assert_eq!(&2, info.stats.cache_misses.get("PTX").unwrap());
690690
assert_eq!(&1, info.stats.cache_misses.get("CUBIN").unwrap());
691-
assert_eq!(&2, info.stats.cache_misses.get("C/C++").unwrap());
691+
assert!(info.stats.cache_misses.get("C/C++").is_none());
692692
let adv_cuda_key = adv_key_kind("cuda", compiler.name);
693693
let adv_ptx_key = adv_key_kind("ptx", compiler.name);
694694
let adv_cubin_key = adv_key_kind("cubin", compiler.name);
@@ -723,14 +723,14 @@ fn test_nvcc_cuda_compiles(compiler: &Compiler, tempdir: &Path) {
723723
assert_eq!(4, info.stats.compile_requests);
724724
assert_eq!(11, info.stats.requests_executed);
725725
assert_eq!(3, info.stats.cache_hits.all());
726-
assert_eq!(8, info.stats.cache_misses.all());
726+
assert_eq!(6, info.stats.cache_misses.all());
727727
assert_eq!(&1, info.stats.cache_hits.get("CUDA").unwrap());
728728
assert_eq!(&1, info.stats.cache_hits.get("PTX").unwrap());
729729
assert_eq!(&1, info.stats.cache_hits.get("CUBIN").unwrap());
730730
assert_eq!(&3, info.stats.cache_misses.get("CUDA").unwrap());
731731
assert_eq!(&2, info.stats.cache_misses.get("PTX").unwrap());
732732
assert_eq!(&1, info.stats.cache_misses.get("CUBIN").unwrap());
733-
assert_eq!(&2, info.stats.cache_misses.get("C/C++").unwrap());
733+
assert!(info.stats.cache_misses.get("C/C++").is_none());
734734
let adv_cuda_key = adv_key_kind("cuda", compiler.name);
735735
let adv_ptx_key = adv_key_kind("ptx", compiler.name);
736736
let adv_cubin_key = adv_key_kind("cubin", compiler.name);
@@ -765,14 +765,14 @@ fn test_nvcc_cuda_compiles(compiler: &Compiler, tempdir: &Path) {
765765
assert_eq!(5, info.stats.compile_requests);
766766
assert_eq!(14, info.stats.requests_executed);
767767
assert_eq!(5, info.stats.cache_hits.all());
768-
assert_eq!(9, info.stats.cache_misses.all());
768+
assert_eq!(7, info.stats.cache_misses.all());
769769
assert_eq!(&1, info.stats.cache_hits.get("CUDA").unwrap());
770770
assert_eq!(&2, info.stats.cache_hits.get("PTX").unwrap());
771771
assert_eq!(&2, info.stats.cache_hits.get("CUBIN").unwrap());
772772
assert_eq!(&4, info.stats.cache_misses.get("CUDA").unwrap());
773773
assert_eq!(&2, info.stats.cache_misses.get("PTX").unwrap());
774774
assert_eq!(&1, info.stats.cache_misses.get("CUBIN").unwrap());
775-
assert_eq!(&2, info.stats.cache_misses.get("C/C++").unwrap());
775+
assert!(info.stats.cache_misses.get("C/C++").is_none());
776776
let adv_cuda_key = adv_key_kind("cuda", compiler.name);
777777
let adv_ptx_key = adv_key_kind("ptx", compiler.name);
778778
let adv_cubin_key = adv_key_kind("cubin", compiler.name);
@@ -831,7 +831,6 @@ fn test_nvcc_proper_lang_stat_tracking(compiler: Compiler, tempdir: &Path) {
831831
.args(compile_cmdline(name, &exe, INPUT, OUTPUT, Vec::new()))
832832
.current_dir(tempdir)
833833
.envs(env_vars.clone())
834-
.env("SCCACHE_LOG", "trace")
835834
.assert()
836835
.success();
837836
fs::remove_file(&out_file).unwrap();
@@ -849,11 +848,11 @@ fn test_nvcc_proper_lang_stat_tracking(compiler: Compiler, tempdir: &Path) {
849848
assert_eq!(4, info.stats.compile_requests);
850849
assert_eq!(8, info.stats.requests_executed);
851850
assert_eq!(3, info.stats.cache_hits.all());
852-
assert_eq!(5, info.stats.cache_misses.all());
851+
assert_eq!(3, info.stats.cache_misses.all());
853852
assert_eq!(&1, info.stats.cache_hits.get("C/C++").unwrap());
854853
assert_eq!(&1, info.stats.cache_hits.get("CUDA").unwrap());
855854
assert_eq!(&1, info.stats.cache_hits.get("CUBIN").unwrap());
856-
assert_eq!(&3, info.stats.cache_misses.get("C/C++").unwrap());
855+
assert_eq!(&1, info.stats.cache_misses.get("C/C++").unwrap());
857856
assert_eq!(&1, info.stats.cache_misses.get("CUDA").unwrap());
858857
assert_eq!(&1, info.stats.cache_misses.get("PTX").unwrap());
859858
});

0 commit comments

Comments
 (0)