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