]> git.proxmox.com Git - rustc.git/blob - src/tools/opt-dist/src/utils/mod.rs
New upstream version 1.75.0+dfsg1
[rustc.git] / src / tools / opt-dist / src / utils / mod.rs
1 use sysinfo::{DiskExt, RefreshKind, System, SystemExt};
2
3 use crate::environment::Environment;
4 use crate::timer::Timer;
5 use crate::utils::io::delete_directory;
6 use humansize::BINARY;
7 use std::time::Duration;
8
9 pub mod artifact_size;
10 pub mod io;
11
12 pub fn format_env_variables() -> String {
13 let vars = std::env::vars().map(|(key, value)| format!("{key}={value}")).collect::<Vec<_>>();
14 vars.join("\n")
15 }
16
17 pub fn print_free_disk_space() -> anyhow::Result<()> {
18 let sys = System::new_with_specifics(RefreshKind::default().with_disks_list().with_disks());
19 let available_space: u64 = sys.disks().iter().map(|d| d.available_space()).sum();
20 let total_space: u64 = sys.disks().iter().map(|d| d.total_space()).sum();
21 let used_space = total_space - available_space;
22
23 log::info!(
24 "Free disk space: {} out of total {} ({:.2}% used)",
25 humansize::format_size(available_space, BINARY),
26 humansize::format_size(total_space, BINARY),
27 (used_space as f64 / total_space as f64) * 100.0
28 );
29 Ok(())
30 }
31
32 pub fn clear_llvm_files(env: &Environment) -> anyhow::Result<()> {
33 // Bootstrap currently doesn't support rebuilding LLVM when PGO options
34 // change (or any other llvm-related options); so just clear out the relevant
35 // directories ourselves.
36 log::info!("Clearing LLVM build files");
37 delete_directory(&env.build_artifacts().join("llvm"))?;
38 delete_directory(&env.build_artifacts().join("lld"))?;
39 Ok(())
40 }
41
42 /// Write the formatted statistics of the timer to a Github Actions summary.
43 pub fn write_timer_to_summary(path: &str, timer: &Timer) -> anyhow::Result<()> {
44 use std::io::Write;
45
46 let mut file = std::fs::File::options().append(true).create(true).open(path)?;
47 writeln!(
48 file,
49 r#"# Step durations
50
51 ```
52 {}
53 ```
54 "#,
55 timer.format_stats()
56 )?;
57 Ok(())
58 }
59
60 /// Wraps all output produced within the `func` closure in a CI output group, if we're running in
61 /// CI.
62 pub fn with_log_group<F: FnOnce() -> R, R>(group: &str, func: F) -> R {
63 if is_in_ci() {
64 println!("::group::{group}");
65 let result = func();
66 println!("::endgroup::");
67 result
68 } else {
69 func()
70 }
71 }
72
73 #[allow(unused)]
74 pub fn retry_action<F: Fn() -> anyhow::Result<R>, R>(
75 action: F,
76 name: &str,
77 count: u64,
78 ) -> anyhow::Result<R> {
79 for attempt in 0..count {
80 match action() {
81 Ok(result) => return Ok(result),
82 Err(error) => {
83 log::error!("Failed to perform action `{name}`, attempt #{attempt}: {error:?}");
84 std::thread::sleep(Duration::from_secs(5));
85 }
86 }
87 }
88 Err(anyhow::anyhow!("Failed to perform action `{name}` after {count} retries"))
89 }
90
91 fn is_in_ci() -> bool {
92 std::env::var("GITHUB_ACTIONS").is_ok()
93 }