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