Skip to content

Commit

Permalink
Rollup merge of rust-lang#113685 - Kobzol:opt-dist-binary-sizes, r=Ma…
Browse files Browse the repository at this point in the history
…rk-Simulacrum

Print artifact sizes in `opt-dist`

The Python PGO script printed a nice table of artifact sizes (`librustc_driver.so`, `libLLVM.so`, ...) at the end of the CI run, which was useful to quickly see the sizes of important files. I forgot to port this functionality into the Rust (`opt-dist`) version in rust-lang#112235. This PR fixes that.

r? bootstrap
  • Loading branch information
matthiaskrgr committed Jul 14, 2023
2 parents d5d19f5 + 18305ea commit f021136
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/tools/opt-dist/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use crate::tests::run_tests;
use crate::timer::Timer;
use crate::training::{gather_llvm_bolt_profiles, gather_llvm_profiles, gather_rustc_profiles};
use crate::utils::io::reset_directory;
use crate::utils::{clear_llvm_files, format_env_variables, print_free_disk_space};
use crate::utils::{
clear_llvm_files, format_env_variables, print_binary_sizes, print_free_disk_space,
};

mod environment;
mod exec;
Expand Down Expand Up @@ -170,6 +172,8 @@ fn main() -> anyhow::Result<()> {
log::info!("Timer results\n{}", timer.format_stats());

print_free_disk_space()?;
result.context("Optimized build pipeline has failed")?;
print_binary_sizes(env.as_ref())?;

result.context("Optimized build pipeline has failed")
Ok(())
}
16 changes: 15 additions & 1 deletion src/tools/opt-dist/src/utils/io.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Context;
use camino::Utf8Path;
use camino::{Utf8Path, Utf8PathBuf};
use fs_extra::dir::CopyOptions;
use std::fs::File;

Expand Down Expand Up @@ -46,3 +46,17 @@ pub fn unpack_archive(path: &Utf8Path, dest_dir: &Utf8Path) -> anyhow::Result<()
archive.unpack(dest_dir.as_std_path())?;
Ok(())
}

/// Returns paths in the given `dir` (non-recursively), optionally with the given `suffix`.
/// The `suffix` should contain the leading dot.
pub fn get_files_from_dir(
dir: &Utf8Path,
suffix: Option<&str>,
) -> anyhow::Result<Vec<Utf8PathBuf>> {
let path = format!("{dir}/*{}", suffix.unwrap_or(""));

Ok(glob::glob(&path)?
.into_iter()
.map(|p| p.map(|p| Utf8PathBuf::from_path_buf(p).unwrap()))
.collect::<Result<Vec<_>, _>>()?)
}
26 changes: 24 additions & 2 deletions src/tools/opt-dist/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub mod io;

use crate::environment::Environment;
use crate::utils::io::delete_directory;
use humansize::BINARY;
use crate::utils::io::{delete_directory, get_files_from_dir};
use humansize::{format_size, BINARY};
use sysinfo::{DiskExt, RefreshKind, System, SystemExt};

pub fn format_env_variables() -> String {
Expand All @@ -25,6 +25,28 @@ pub fn print_free_disk_space() -> anyhow::Result<()> {
Ok(())
}

pub fn print_binary_sizes(env: &dyn Environment) -> anyhow::Result<()> {
use std::fmt::Write;

let root = env.build_artifacts().join("stage2");

let mut files = get_files_from_dir(&root.join("bin"), None)?;
files.extend(get_files_from_dir(&root.join("lib"), Some(".so"))?);
files.sort_unstable();

let mut output = String::new();
for file in files {
let size = std::fs::metadata(file.as_std_path())?.len();
let size_formatted = format_size(size, BINARY);
let name = format!("{}:", file.file_name().unwrap());
writeln!(output, "{name:<50}{size_formatted:>10}")?;
}

log::info!("Rustc artifact size\n{output}");

Ok(())
}

pub fn clear_llvm_files(env: &dyn Environment) -> anyhow::Result<()> {
// Bootstrap currently doesn't support rebuilding LLVM when PGO options
// change (or any other llvm-related options); so just clear out the relevant
Expand Down

0 comments on commit f021136

Please sign in to comment.