@@ -65,8 +65,13 @@ use database::{
65
65
CommitType , Connection , Pool ,
66
66
} ;
67
67
68
+ /// Directory used to cache downloaded Rust toolchains on disk.
68
69
const TOOLCHAIN_CACHE_DIRECTORY : & str = "cache" ;
69
70
71
+ /// Maximum allowed number of toolchains in the toolchain cache directory.
72
+ /// If the directory will have more toolchains, it will be purged.
73
+ const TOOLCHAIN_CACHE_MAX_TOOLCHAINS : usize = 30 ;
74
+
70
75
fn n_normal_benchmarks_remaining ( n : usize ) -> String {
71
76
let suffix = if n == 1 { "" } else { "s" } ;
72
77
format ! ( "{n} normal benchmark{suffix} remaining" )
@@ -1434,6 +1439,8 @@ async fn run_job_queue_benchmarks(
1434
1439
all_compile_benchmarks : Vec < Benchmark > ,
1435
1440
check_git_sha : bool ,
1436
1441
) -> anyhow:: Result < ( ) > {
1442
+ let _ = tidy_toolchain_cache_dir ( ) ;
1443
+
1437
1444
let mut last_request_tag = None ;
1438
1445
1439
1446
while let Some ( ( benchmark_job, artifact_id) ) = conn
@@ -1444,20 +1451,25 @@ async fn run_job_queue_benchmarks(
1444
1451
)
1445
1452
. await ?
1446
1453
{
1454
+ // Are we benchmarking a different benchmark request than in the previous iteration of the
1455
+ // loop?
1456
+ let is_new_request = last_request_tag. is_some ( )
1457
+ && last_request_tag. as_deref ( ) != Some ( benchmark_job. request_tag ( ) ) ;
1458
+ if is_new_request {
1459
+ let _ = tidy_toolchain_cache_dir ( ) ;
1460
+ }
1461
+
1447
1462
// Here we check if we should update our commit SHA, if rustc-perf has been updated.
1448
1463
// We only check for updates when we switch *benchmark requests*, not *benchmark jobs*,
1449
1464
// to avoid changing code in the middle of benchmarking the same request.
1450
1465
// Note that if an update happens, the job that we have just dequeued will have its deque
1451
1466
// counter increased. But since updates are relatively rare, that shouldn't be a big deal,
1452
1467
// it will be dequeued again when the collector starts again.
1453
- if check_git_sha
1454
- && last_request_tag. is_some ( )
1455
- && last_request_tag. as_deref ( ) != Some ( benchmark_job. request_tag ( ) )
1456
- && needs_git_update ( collector)
1457
- {
1468
+ if check_git_sha && is_new_request && needs_git_update ( collector) {
1458
1469
log:: warn!( "Exiting collector to update itself from git." ) ;
1459
1470
return Ok ( ( ) ) ;
1460
1471
}
1472
+
1461
1473
last_request_tag = Some ( benchmark_job. request_tag ( ) . to_string ( ) ) ;
1462
1474
1463
1475
log:: info!( "Dequeued job {benchmark_job:?}, artifact_id {artifact_id:?}" ) ;
@@ -1523,6 +1535,26 @@ async fn run_job_queue_benchmarks(
1523
1535
Ok ( ( ) )
1524
1536
}
1525
1537
1538
+ /// Check the toolchain cache directory and delete it if it grows too large.
1539
+ /// Currently, we just assume that "too large" means "has more than N toolchains".
1540
+ fn tidy_toolchain_cache_dir ( ) -> std:: io:: Result < ( ) > {
1541
+ let dir_count = Path :: new ( TOOLCHAIN_CACHE_DIRECTORY )
1542
+ . read_dir ( ) ?
1543
+ . filter_map ( |e| e. ok ( ) )
1544
+ . filter_map ( |d| d. file_type ( ) . ok ( ) )
1545
+ . filter ( |t| t. is_dir ( ) )
1546
+ . count ( ) ;
1547
+ if dir_count > TOOLCHAIN_CACHE_MAX_TOOLCHAINS {
1548
+ log:: warn!(
1549
+ "Purging toolchain cache directory at {}" ,
1550
+ TOOLCHAIN_CACHE_DIRECTORY . display( )
1551
+ ) ;
1552
+ // Just remove the whole directory, to avoid having to figure out which toolchains are old
1553
+ std:: fs:: remove_dir_all ( TOOLCHAIN_CACHE_DIRECTORY ) ?;
1554
+ }
1555
+ Ok ( ( ) )
1556
+ }
1557
+
1526
1558
/// Returns true if the commit SHA of collector does not match the latest commit SHA of the master
1527
1559
/// branch of https://github.com/rust-lang/rustc-perf.
1528
1560
fn needs_git_update ( collector : & CollectorConfig ) -> bool {
@@ -1606,8 +1638,6 @@ async fn run_benchmark_job(
1606
1638
} ;
1607
1639
// Avoid redownloading the same sysroot multiple times for different jobs, even
1608
1640
// across collector restarts.
1609
-
1610
- // TODO: Periodically clear the cache directory to avoid running out of disk space.
1611
1641
sysroot. preserve ( ) ;
1612
1642
Toolchain :: from_sysroot ( & sysroot, commit. sha . clone ( ) )
1613
1643
}
0 commit comments