]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/scripts/filter_profile.rs
Merge commit '63734fcdd718cca089f84c42f3a42c0096cfd431' into sync_cg_clif-2022-05-15
[rust.git] / compiler / rustc_codegen_cranelift / scripts / filter_profile.rs
1 #!/usr/bin/env bash
2 #![forbid(unsafe_code)]/* This line is ignored by bash
3 # This block is ignored by rustc
4 pushd $(dirname "$0")/../
5 RUSTC="$(pwd)/build/rustc-clif"
6 popd
7 PROFILE=$1 OUTPUT=$2 exec $RUSTC -Zunstable-options -Cllvm-args=mode=jit -Cprefer-dynamic $0
8 #*/
9
10 //! This program filters away uninteresting samples and trims uninteresting frames for stackcollapse
11 //! profiles.
12 //!
13 //! Usage: ./filter_profile.rs <profile in stackcollapse format> <output file>
14 //!
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.
17
18 use std::io::Write;
19
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);
26     }
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()
30         .create(true)
31         .write(true)
32         .truncate(true)
33         .open(output_name)?;
34
35     for line in profile.lines() {
36         let mut stack = &line[..line.rfind(" ").unwrap()];
37         let count = &line[line.rfind(" ").unwrap() + 1..];
38
39         // Filter away uninteresting samples
40         if !stack.contains("rustc_codegen_cranelift") {
41             continue;
42         }
43
44         if stack.contains("rustc_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")
47         {
48             continue;
49         }
50
51         // Trim start
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..];
60         }
61
62         if let Some(index) = stack.find("rustc_codegen_cranelift::driver::aot::module_codegen") {
63             stack = &stack[index..];
64         }
65
66         // Trim end
67         const MALLOC: &str = "malloc";
68         if let Some(index) = stack.find(MALLOC) {
69             stack = &stack[..index + MALLOC.len()];
70         }
71
72         const FREE: &str = "free";
73         if let Some(index) = stack.find(FREE) {
74             stack = &stack[..index + FREE.len()];
75         }
76
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()];
80         }
81
82         const COLLECT_AND_PARTITION_MONO_ITEMS: &str =
83             "rustc_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()];
86         }
87
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()];
91         }
92
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()];
96         }
97
98         const ENCODE_METADATA: &str = "rustc_metadata::rmeta::encoder::encode_metadata";
99         if let Some(index) = stack.find(ENCODE_METADATA) {
100             stack = &stack[..index + ENCODE_METADATA.len()];
101         }
102
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()];
106         }
107
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()];
111         }
112
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()];
116         }
117
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")?;
122     }
123
124     Ok(())
125 }