1 | //===- bolt/tools/heatmap/heatmap.cpp - Profile heatmap visualization tool ===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "bolt/Rewrite/RewriteInstance.h" |
10 | #include "bolt/Utils/CommandLineOpts.h" |
11 | #include "llvm/MC/TargetRegistry.h" |
12 | #include "llvm/Object/Binary.h" |
13 | #include "llvm/Support/CommandLine.h" |
14 | #include "llvm/Support/Errc.h" |
15 | #include "llvm/Support/Error.h" |
16 | #include "llvm/Support/FileSystem.h" |
17 | #include "llvm/Support/Program.h" |
18 | #include "llvm/Support/TargetSelect.h" |
19 | |
20 | using namespace llvm; |
21 | using namespace bolt; |
22 | |
23 | namespace opts { |
24 | |
25 | static cl::OptionCategory *HeatmapCategories[] = {&HeatmapCategory, |
26 | &BoltOutputCategory}; |
27 | |
28 | static cl::opt<std::string> InputFilename(cl::Positional, |
29 | cl::desc("<executable>" ), |
30 | cl::Required, |
31 | cl::cat(HeatmapCategory)); |
32 | |
33 | } // namespace opts |
34 | |
35 | static StringRef ToolName; |
36 | |
37 | static void report_error(StringRef Message, std::error_code EC) { |
38 | assert(EC); |
39 | errs() << ToolName << ": '" << Message << "': " << EC.message() << ".\n" ; |
40 | exit(status: 1); |
41 | } |
42 | |
43 | static void report_error(StringRef Message, Error E) { |
44 | assert(E); |
45 | errs() << ToolName << ": '" << Message << "': " << toString(E: std::move(E)) |
46 | << ".\n" ; |
47 | exit(status: 1); |
48 | } |
49 | |
50 | static std::string GetExecutablePath(const char *Argv0) { |
51 | SmallString<256> ExecutablePath(Argv0); |
52 | // Do a PATH lookup if Argv0 isn't a valid path. |
53 | if (!llvm::sys::fs::exists(Path: ExecutablePath)) |
54 | if (llvm::ErrorOr<std::string> P = |
55 | llvm::sys::findProgramByName(Name: ExecutablePath)) |
56 | ExecutablePath = *P; |
57 | return std::string(ExecutablePath); |
58 | } |
59 | |
60 | int main(int argc, char **argv) { |
61 | cl::HideUnrelatedOptions(Categories: ArrayRef(opts::HeatmapCategories)); |
62 | cl::ParseCommandLineOptions( |
63 | argc, argv, |
64 | Overview: " BOLT Code Heatmap tool\n\n" |
65 | " Produces code heatmaps using sampled profile\n\n" |
66 | |
67 | " Inputs:\n" |
68 | " - Binary (supports BOLT-optimized binaries),\n" |
69 | " - Sampled profile collected from the binary:\n" |
70 | " - perf data or pre-aggregated profile data (instrumentation profile " |
71 | "not supported)\n" |
72 | " - perf data can have basic (IP) or branch-stack (LBR) samples\n\n" |
73 | |
74 | " Outputs:\n" |
75 | " - Heatmaps: colored ASCII (requires a color-capable terminal or a" |
76 | " conversion tool like `aha`)\n" |
77 | " Multiple heatmaps are produced by default with different " |
78 | "granularities (set by `block-size` option)\n" |
79 | " - Section hotness: per-section samples% and utilization%\n" |
80 | " - Cumulative distribution: working set size corresponding to a " |
81 | "given percentile of samples\n" ); |
82 | |
83 | if (opts::PerfData.empty()) { |
84 | errs() << ToolName << ": expected -perfdata=<filename> option.\n" ; |
85 | exit(status: 1); |
86 | } |
87 | |
88 | opts::HeatmapMode = opts::HM_Exclusive; |
89 | opts::AggregateOnly = true; |
90 | if (!sys::fs::exists(Path: opts::InputFilename)) |
91 | report_error(Message: opts::InputFilename, EC: errc::no_such_file_or_directory); |
92 | |
93 | // Output to stdout by default |
94 | if (opts::OutputFilename.empty()) |
95 | opts::OutputFilename = "-" ; |
96 | opts::HeatmapOutput.assign(str: opts::OutputFilename); |
97 | |
98 | // Initialize targets and assembly printers/parsers. |
99 | #define BOLT_TARGET(target) \ |
100 | LLVMInitialize##target##TargetInfo(); \ |
101 | LLVMInitialize##target##TargetMC(); \ |
102 | LLVMInitialize##target##AsmParser(); \ |
103 | LLVMInitialize##target##Disassembler(); \ |
104 | LLVMInitialize##target##Target(); \ |
105 | LLVMInitialize##target##AsmPrinter(); |
106 | |
107 | #include "bolt/Core/TargetConfig.def" |
108 | |
109 | ToolName = argv[0]; |
110 | std::string ToolPath = GetExecutablePath(Argv0: argv[0]); |
111 | Expected<OwningBinary<Binary>> BinaryOrErr = |
112 | createBinary(Path: opts::InputFilename); |
113 | if (Error E = BinaryOrErr.takeError()) |
114 | report_error(Message: opts::InputFilename, E: std::move(E)); |
115 | Binary &Binary = *BinaryOrErr.get().getBinary(); |
116 | |
117 | if (auto *e = dyn_cast<ELFObjectFileBase>(Val: &Binary)) { |
118 | auto RIOrErr = RewriteInstance::create(File: e, Argc: argc, Argv: argv, ToolPath); |
119 | if (Error E = RIOrErr.takeError()) |
120 | report_error(Message: "RewriteInstance" , E: std::move(E)); |
121 | |
122 | RewriteInstance &RI = *RIOrErr.get(); |
123 | if (Error E = RI.setProfile(opts::PerfData)) |
124 | report_error(Message: opts::PerfData, E: std::move(E)); |
125 | |
126 | if (Error E = RI.run()) |
127 | report_error(Message: opts::InputFilename, E: std::move(E)); |
128 | } else { |
129 | report_error(Message: opts::InputFilename, EC: object_error::invalid_file_type); |
130 | } |
131 | |
132 | return EXIT_SUCCESS; |
133 | } |
134 | |