2 #![forbid(unsafe_code)]/* This line is ignored by bash
3 # This block is ignored by rustc
4 pushd $(dirname "$0")/../
7 PROFILE=$1 OUTPUT=$2 exec $RUSTC $RUSTFLAGS -Cllvm-args=mode=jit -Cprefer-dynamic $0
10 //! This program filters away uninteresting samples and trims uninteresting frames for stackcollapse
13 //! Usage: ./filter_profile.rs <profile in stackcollapse format> <output file>
15 //! This file is specially crafted to be both a valid bash script and valid rust source file. If
16 //! executed as bash script this will run the rust source using cg_clif in JIT mode.
20 fn main() -> Result<(), Box<dyn std::error::Error>> {
21 let profile_name = std::env::var("PROFILE").unwrap();
22 let output_name = std::env::var("OUTPUT").unwrap();
23 if profile_name.is_empty() || output_name.is_empty() {
24 println!("Usage: ./filter_profile.rs <profile in stackcollapse format> <output file>");
25 std::process::exit(1);
27 let profile = std::fs::read_to_string(profile_name)
28 .map_err(|err| format!("Failed to read profile {}", err))?;
29 let mut output = std::fs::OpenOptions::new()
35 for line in profile.lines() {
36 let mut stack = &line[..line.rfind(" ").unwrap()];
37 let count = &line[line.rfind(" ").unwrap() + 1..];
39 // Filter away uninteresting samples
40 if !stack.contains("rustc_codegen_cranelift") {
44 if stack.contains("rustc_mir::monomorphize::partitioning::collect_and_partition_mono_items")
45 || stack.contains("rustc_incremental::assert_dep_graph::assert_dep_graph")
46 || stack.contains("rustc_symbol_mangling::test::report_symbol_names")
52 if let Some(index) = stack.find("rustc_interface::passes::configure_and_expand") {
53 stack = &stack[index..];
54 } else if let Some(index) = stack.find("rustc_interface::passes::analysis") {
55 stack = &stack[index..];
56 } else if let Some(index) = stack.find("rustc_interface::passes::start_codegen") {
57 stack = &stack[index..];
58 } else if let Some(index) = stack.find("rustc_interface::queries::Linker::link") {
59 stack = &stack[index..];
62 if let Some(index) = stack.find("rustc_codegen_cranelift::driver::aot::module_codegen") {
63 stack = &stack[index..];
67 const MALLOC: &str = "malloc";
68 if let Some(index) = stack.find(MALLOC) {
69 stack = &stack[..index + MALLOC.len()];
72 const FREE: &str = "free";
73 if let Some(index) = stack.find(FREE) {
74 stack = &stack[..index + FREE.len()];
77 const TYPECK_ITEM_BODIES: &str = "rustc_typeck::check::typeck_item_bodies";
78 if let Some(index) = stack.find(TYPECK_ITEM_BODIES) {
79 stack = &stack[..index + TYPECK_ITEM_BODIES.len()];
82 const COLLECT_AND_PARTITION_MONO_ITEMS: &str =
83 "rustc_mir::monomorphize::partitioning::collect_and_partition_mono_items";
84 if let Some(index) = stack.find(COLLECT_AND_PARTITION_MONO_ITEMS) {
85 stack = &stack[..index + COLLECT_AND_PARTITION_MONO_ITEMS.len()];
88 const ASSERT_DEP_GRAPH: &str = "rustc_incremental::assert_dep_graph::assert_dep_graph";
89 if let Some(index) = stack.find(ASSERT_DEP_GRAPH) {
90 stack = &stack[..index + ASSERT_DEP_GRAPH.len()];
93 const REPORT_SYMBOL_NAMES: &str = "rustc_symbol_mangling::test::report_symbol_names";
94 if let Some(index) = stack.find(REPORT_SYMBOL_NAMES) {
95 stack = &stack[..index + REPORT_SYMBOL_NAMES.len()];
98 const ENCODE_METADATA: &str = "rustc_middle::ty::context::TyCtxt::encode_metadata";
99 if let Some(index) = stack.find(ENCODE_METADATA) {
100 stack = &stack[..index + ENCODE_METADATA.len()];
103 const SUBST_AND_NORMALIZE_ERASING_REGIONS: &str = "rustc_middle::ty::normalize_erasing_regions::<impl rustc_middle::ty::context::TyCtxt>::subst_and_normalize_erasing_regions";
104 if let Some(index) = stack.find(SUBST_AND_NORMALIZE_ERASING_REGIONS) {
105 stack = &stack[..index + SUBST_AND_NORMALIZE_ERASING_REGIONS.len()];
108 const NORMALIZE_ERASING_LATE_BOUND_REGIONS: &str = "rustc_middle::ty::normalize_erasing_regions::<impl rustc_middle::ty::context::TyCtxt>::normalize_erasing_late_bound_regions";
109 if let Some(index) = stack.find(NORMALIZE_ERASING_LATE_BOUND_REGIONS) {
110 stack = &stack[..index + NORMALIZE_ERASING_LATE_BOUND_REGIONS.len()];
113 const INST_BUILD: &str = "<cranelift_frontend::frontend::FuncInstBuilder as cranelift_codegen::ir::builder::InstBuilderBase>::build";
114 if let Some(index) = stack.find(INST_BUILD) {
115 stack = &stack[..index + INST_BUILD.len()];
118 output.write_all(stack.as_bytes())?;
119 output.write_all(&*b" ")?;
120 output.write_all(count.as_bytes())?;
121 output.write_all(&*b"\n")?;