Skip to content

Commit

Permalink
Record more artifact sizes during self-profiling.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwoerister committed Nov 8, 2021
1 parent 3e3890c commit fefe1e9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
46 changes: 46 additions & 0 deletions compiler/rustc_codegen_llvm/src/back/write.rs
Expand Up @@ -17,6 +17,7 @@ use rustc_codegen_ssa::back::write::{
};
use rustc_codegen_ssa::traits::*;
use rustc_codegen_ssa::{CompiledModule, ModuleCodegen};
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_data_structures::small_c_str::SmallCStr;
use rustc_errors::{FatalError, Handler, Level};
use rustc_fs_util::{link_or_copy, path_to_c_string};
Expand Down Expand Up @@ -53,6 +54,7 @@ pub fn write_output_file(
output: &Path,
dwo_output: Option<&Path>,
file_type: llvm::FileType,
self_profiler_ref: &SelfProfilerRef,
) -> Result<(), FatalError> {
unsafe {
let output_c = path_to_c_string(output);
Expand All @@ -76,6 +78,19 @@ pub fn write_output_file(
file_type,
)
};

// Record artifact sizes for self-profiling
if result == llvm::LLVMRustResult::Success {
let artifact_kind = match file_type {
llvm::FileType::ObjectFile => "object_file",
llvm::FileType::AssemblyFile => "assembly_file",
};
record_artifact_size(self_profiler_ref, artifact_kind, output);
if let Some(dwo_file) = dwo_output {
record_artifact_size(self_profiler_ref, "dwo_file", dwo_file);
}
}

result.into_result().map_err(|()| {
let msg = format!("could not write output to {}", output.display());
llvm_err(handler, &msg)
Expand Down Expand Up @@ -752,6 +767,14 @@ pub(crate) unsafe fn codegen(
let thin = ThinBuffer::new(llmod);
let data = thin.data();

if let Some(bitcode_filename) = bc_out.file_name() {
cgcx.prof.artifact_size(
"llvm_bitcode",
bitcode_filename.to_string_lossy(),
data.len() as u64,
);
}

if config.emit_bc || config.emit_obj == EmitObj::Bitcode {
let _timer = cgcx.prof.generic_activity_with_arg(
"LLVM_module_codegen_emit_bitcode",
Expand Down Expand Up @@ -812,6 +835,11 @@ pub(crate) unsafe fn codegen(
}

let result = llvm::LLVMRustPrintModule(llmod, out_c.as_ptr(), demangle_callback);

if result == llvm::LLVMRustResult::Success {
record_artifact_size(&cgcx.prof, "llvm_ir", &out);
}

result.into_result().map_err(|()| {
let msg = format!("failed to write LLVM IR to {}", out.display());
llvm_err(diag_handler, &msg)
Expand Down Expand Up @@ -842,6 +870,7 @@ pub(crate) unsafe fn codegen(
&path,
None,
llvm::FileType::AssemblyFile,
&cgcx.prof,
)
})?;
}
Expand Down Expand Up @@ -875,6 +904,7 @@ pub(crate) unsafe fn codegen(
&obj_out,
dwo_out,
llvm::FileType::ObjectFile,
&cgcx.prof,
)
})?;
}
Expand Down Expand Up @@ -1131,3 +1161,19 @@ fn create_msvc_imps(
symbol_name.starts_with(b"__llvm_profile_")
}
}

fn record_artifact_size(
self_profiler_ref: &SelfProfilerRef,
artifact_kind: &'static str,
path: &Path,
) {
// Don't stat the file if we are not going to record its size.
if !self_profiler_ref.enabled() {
return;
}

if let Some(artifact_name) = path.file_name() {
let file_size = std::fs::metadata(path).map(|m| m.len()).unwrap_or(0);
self_profiler_ref.artifact_size(artifact_kind, artifact_name.to_string_lossy(), file_size);
}
}
13 changes: 13 additions & 0 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Expand Up @@ -121,6 +121,19 @@ pub fn link_binary<'a, B: ArchiveBuilder<'a>>(
if sess.opts.json_artifact_notifications {
sess.parse_sess.span_diagnostic.emit_artifact_notification(&out_filename, "link");
}

if sess.prof.enabled() {
if let Some(artifact_name) = out_filename.file_name() {
// Record size for self-profiling
let file_size = std::fs::metadata(&out_filename).map(|m| m.len()).unwrap_or(0);

sess.prof.artifact_size(
"linked_artifact",
artifact_name.to_string_lossy(),
file_size,
);
}
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Expand Up @@ -2187,5 +2187,8 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>) -> EncodedMetadata {
result[header + 2] = (pos >> 8) as u8;
result[header + 3] = (pos >> 0) as u8;

// Record metadata size for self-profiling
tcx.prof.artifact_size("crate_metadata", "crate_metadata", result.len() as u64);

EncodedMetadata { raw_data: result }
}
11 changes: 11 additions & 0 deletions compiler/rustc_monomorphize/src/partitioning/mod.rs
Expand Up @@ -361,6 +361,17 @@ fn collect_and_partition_mono_items<'tcx>(
)
});

if tcx.prof.enabled() {
// Record CGU size estimates for self-profiling.
for cgu in codegen_units {
tcx.prof.artifact_size(
"codegen_unit_size_estimate",
&cgu.name().as_str()[..],
cgu.size_estimate() as u64,
);
}
}

let mono_items: DefIdSet = items
.iter()
.filter_map(|mono_item| match *mono_item {
Expand Down

0 comments on commit fefe1e9

Please sign in to comment.