]> git.lizzy.rs Git - rust.git/blob - scripts/filter_profile.rs
initial revert
[rust.git] / scripts / filter_profile.rs
1 #!/bin/bash
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"
7 popd
8 PROFILE=$1 OUTPUT=$2 exec $RUSTC -Zunstable-options -Cllvm-args=mode=jit -Cprefer-dynamic $0
9 #*/
10
11 //! This program filters away uninteresting samples and trims uninteresting frames for stackcollapse
12 //! profiles.
13 //!
14 //! Usage: ./filter_profile.rs <profile in stackcollapse format> <output file>
15 //!
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.
18
19 use std::io::Write;
20
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);
27     }
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()
31         .create(true)
32         .write(true)
33         .truncate(true)
34         .open(output_name)?;
35
36     for line in profile.lines() {
37         let mut stack = &line[..line.rfind(" ").unwrap()];
38         let count = &line[line.rfind(" ").unwrap() + 1..];
39
40         // Filter away uninteresting samples
41         if !stack.contains("rustc_codegen_cranelift") {
42             continue;
43         }
44
45         if stack.contains("rustc_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")
48         {
49             continue;
50         }
51
52         // Trim start
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..];
61         }
62
63         if let Some(index) = stack.find("rustc_codegen_cranelift::driver::aot::module_codegen") {
64             stack = &stack[index..];
65         }
66
67         // Trim end
68         const MALLOC: &str = "malloc";
69         if let Some(index) = stack.find(MALLOC) {
70             stack = &stack[..index + MALLOC.len()];
71         }
72
73         const FREE: &str = "free";
74         if let Some(index) = stack.find(FREE) {
75             stack = &stack[..index + FREE.len()];
76         }
77
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()];
81         }
82
83         const COLLECT_AND_PARTITION_MONO_ITEMS: &str =
84             "rustc_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()];
87         }
88
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()];
92         }
93
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()];
97         }
98
99         const ENCODE_METADATA: &str = "rustc_metadata::rmeta::encoder::encode_metadata";
100         if let Some(index) = stack.find(ENCODE_METADATA) {
101             stack = &stack[..index + ENCODE_METADATA.len()];
102         }
103
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()];
107         }
108
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()];
112         }
113
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()];
117         }
118
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")?;
123     }
124
125     Ok(())
126 }