| 1 | //===- llvm-profgen.cpp - LLVM SPGO profile generation tool -----*- C++ -*-===// |
| 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 | // llvm-profgen generates SPGO profiles from perf script ouput. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "ErrorHandling.h" |
| 14 | #include "PerfReader.h" |
| 15 | #include "ProfileGenerator.h" |
| 16 | #include "ProfiledBinary.h" |
| 17 | #include "llvm/DebugInfo/Symbolize/SymbolizableModule.h" |
| 18 | #include "llvm/Support/CommandLine.h" |
| 19 | #include "llvm/Support/FileSystem.h" |
| 20 | #include "llvm/Support/InitLLVM.h" |
| 21 | #include "llvm/Support/TargetSelect.h" |
| 22 | #include "llvm/Support/VirtualFileSystem.h" |
| 23 | |
| 24 | static cl::OptionCategory ProfGenCategory("ProfGen Options" ); |
| 25 | |
| 26 | static cl::opt<std::string> PerfScriptFilename( |
| 27 | "perfscript" , cl::value_desc("perfscript" ), |
| 28 | cl::desc("Path of perf-script trace created by Linux perf tool with " |
| 29 | "`script` command(the raw perf.data should be profiled with -b)" ), |
| 30 | cl::cat(ProfGenCategory)); |
| 31 | static cl::alias PSA("ps" , cl::desc("Alias for --perfscript" ), |
| 32 | cl::aliasopt(PerfScriptFilename)); |
| 33 | |
| 34 | static cl::opt<std::string> PerfDataFilename( |
| 35 | "perfdata" , cl::value_desc("perfdata" ), |
| 36 | cl::desc("Path of raw perf data created by Linux perf tool (it should be " |
| 37 | "profiled with -b)" ), |
| 38 | cl::cat(ProfGenCategory)); |
| 39 | static cl::alias PDA("pd" , cl::desc("Alias for --perfdata" ), |
| 40 | cl::aliasopt(PerfDataFilename)); |
| 41 | |
| 42 | static cl::opt<std::string> UnsymbolizedProfFilename( |
| 43 | "unsymbolized-profile" , cl::value_desc("unsymbolized profile" ), |
| 44 | cl::desc("Path of the unsymbolized profile created by " |
| 45 | "`llvm-profgen` with `--skip-symbolization`" ), |
| 46 | cl::cat(ProfGenCategory)); |
| 47 | static cl::alias UPA("up" , cl::desc("Alias for --unsymbolized-profile" ), |
| 48 | cl::aliasopt(UnsymbolizedProfFilename)); |
| 49 | |
| 50 | static cl::opt<std::string> SampleProfFilename( |
| 51 | "llvm-sample-profile" , cl::value_desc("llvm sample profile" ), |
| 52 | cl::desc("Path of the LLVM sample profile" ), cl::cat(ProfGenCategory)); |
| 53 | |
| 54 | static cl::opt<std::string> |
| 55 | BinaryPath("binary" , cl::value_desc("binary" ), cl::Required, |
| 56 | cl::desc("Path of profiled executable binary." ), |
| 57 | cl::cat(ProfGenCategory)); |
| 58 | |
| 59 | static cl::opt<uint32_t> |
| 60 | ProcessId("pid" , cl::value_desc("process Id" ), cl::init(Val: 0), |
| 61 | cl::desc("Process Id for the profiled executable binary." ), |
| 62 | cl::cat(ProfGenCategory)); |
| 63 | |
| 64 | static cl::opt<std::string> DebugBinPath( |
| 65 | "debug-binary" , cl::value_desc("debug-binary" ), |
| 66 | cl::desc("Path of debug info binary, llvm-profgen will load the DWARF info " |
| 67 | "from it instead of the executable binary." ), |
| 68 | cl::cat(ProfGenCategory)); |
| 69 | |
| 70 | extern cl::opt<bool> ShowDisassemblyOnly; |
| 71 | extern cl::opt<bool> ShowSourceLocations; |
| 72 | extern cl::opt<bool> SkipSymbolization; |
| 73 | |
| 74 | using namespace llvm; |
| 75 | using namespace sampleprof; |
| 76 | |
| 77 | // Validate the command line input. |
| 78 | static void validateCommandLine() { |
| 79 | // Allow the missing perfscript if we only use to show binary disassembly. |
| 80 | if (!ShowDisassemblyOnly) { |
| 81 | // Validate input profile is provided only once |
| 82 | bool HasPerfData = PerfDataFilename.getNumOccurrences() > 0; |
| 83 | bool HasPerfScript = PerfScriptFilename.getNumOccurrences() > 0; |
| 84 | bool HasUnsymbolizedProfile = |
| 85 | UnsymbolizedProfFilename.getNumOccurrences() > 0; |
| 86 | bool HasSampleProfile = SampleProfFilename.getNumOccurrences() > 0; |
| 87 | uint16_t S = |
| 88 | HasPerfData + HasPerfScript + HasUnsymbolizedProfile + HasSampleProfile; |
| 89 | if (S != 1) { |
| 90 | std::string Msg = |
| 91 | S > 1 |
| 92 | ? "`--perfscript`, `--perfdata` and `--unsymbolized-profile` " |
| 93 | "cannot be used together." |
| 94 | : "Perf input file is missing, please use one of `--perfscript`, " |
| 95 | "`--perfdata` and `--unsymbolized-profile` for the input." ; |
| 96 | exitWithError(Message: Msg); |
| 97 | } |
| 98 | |
| 99 | auto CheckFileExists = [](bool H, StringRef File) { |
| 100 | if (H && !llvm::sys::fs::exists(Path: File)) { |
| 101 | std::string Msg = "Input perf file(" + File.str() + ") doesn't exist." ; |
| 102 | exitWithError(Message: Msg); |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | CheckFileExists(HasPerfData, PerfDataFilename); |
| 107 | CheckFileExists(HasPerfScript, PerfScriptFilename); |
| 108 | CheckFileExists(HasUnsymbolizedProfile, UnsymbolizedProfFilename); |
| 109 | CheckFileExists(HasSampleProfile, SampleProfFilename); |
| 110 | } |
| 111 | |
| 112 | if (!llvm::sys::fs::exists(Path: BinaryPath)) { |
| 113 | std::string Msg = "Input binary(" + BinaryPath + ") doesn't exist." ; |
| 114 | exitWithError(Message: Msg); |
| 115 | } |
| 116 | |
| 117 | if (CSProfileGenerator::MaxCompressionSize < -1) { |
| 118 | exitWithError(Message: "Value of --compress-recursion should >= -1" ); |
| 119 | } |
| 120 | if (ShowSourceLocations && !ShowDisassemblyOnly) { |
| 121 | exitWithError(Message: "--show-source-locations should work together with " |
| 122 | "--show-disassembly-only!" ); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | static PerfInputFile getPerfInputFile() { |
| 127 | PerfInputFile File; |
| 128 | if (PerfDataFilename.getNumOccurrences()) { |
| 129 | File.InputFile = PerfDataFilename; |
| 130 | File.Format = PerfFormat::PerfData; |
| 131 | } else if (PerfScriptFilename.getNumOccurrences()) { |
| 132 | File.InputFile = PerfScriptFilename; |
| 133 | File.Format = PerfFormat::PerfScript; |
| 134 | } else if (UnsymbolizedProfFilename.getNumOccurrences()) { |
| 135 | File.InputFile = UnsymbolizedProfFilename; |
| 136 | File.Format = PerfFormat::UnsymbolizedProfile; |
| 137 | } |
| 138 | return File; |
| 139 | } |
| 140 | |
| 141 | int main(int argc, const char *argv[]) { |
| 142 | InitLLVM X(argc, argv); |
| 143 | |
| 144 | // Initialize targets and assembly printers/parsers. |
| 145 | InitializeAllTargetInfos(); |
| 146 | InitializeAllTargetMCs(); |
| 147 | InitializeAllDisassemblers(); |
| 148 | |
| 149 | cl::HideUnrelatedOptions(Categories: {&ProfGenCategory, &getColorCategory()}); |
| 150 | cl::ParseCommandLineOptions(argc, argv, Overview: "llvm SPGO profile generator\n" ); |
| 151 | validateCommandLine(); |
| 152 | |
| 153 | // Load symbols and disassemble the code of a given binary. |
| 154 | std::unique_ptr<ProfiledBinary> Binary = |
| 155 | std::make_unique<ProfiledBinary>(args&: BinaryPath, args&: DebugBinPath); |
| 156 | if (ShowDisassemblyOnly) |
| 157 | return EXIT_SUCCESS; |
| 158 | |
| 159 | if (SampleProfFilename.getNumOccurrences()) { |
| 160 | LLVMContext Context; |
| 161 | auto FS = vfs::getRealFileSystem(); |
| 162 | auto ReaderOrErr = |
| 163 | SampleProfileReader::create(Filename: SampleProfFilename, C&: Context, FS&: *FS); |
| 164 | std::unique_ptr<sampleprof::SampleProfileReader> Reader = |
| 165 | std::move(ReaderOrErr.get()); |
| 166 | Reader->read(); |
| 167 | std::unique_ptr<ProfileGeneratorBase> Generator = |
| 168 | ProfileGeneratorBase::create(Binary: Binary.get(), ProfileMap&: Reader->getProfiles(), |
| 169 | profileIsCS: Reader->profileIsCS()); |
| 170 | Generator->generateProfile(); |
| 171 | Generator->write(); |
| 172 | } else { |
| 173 | std::optional<uint32_t> PIDFilter; |
| 174 | if (ProcessId.getNumOccurrences()) |
| 175 | PIDFilter = ProcessId; |
| 176 | PerfInputFile PerfFile = getPerfInputFile(); |
| 177 | std::unique_ptr<PerfReaderBase> Reader = |
| 178 | PerfReaderBase::create(Binary: Binary.get(), PerfInput&: PerfFile, PIDFilter); |
| 179 | // Parse perf events and samples |
| 180 | Reader->parsePerfTraces(); |
| 181 | |
| 182 | if (SkipSymbolization) |
| 183 | return EXIT_SUCCESS; |
| 184 | |
| 185 | std::unique_ptr<ProfileGeneratorBase> Generator = |
| 186 | ProfileGeneratorBase::create(Binary: Binary.get(), Counters: &Reader->getSampleCounters(), |
| 187 | profileIsCS: Reader->profileIsCS()); |
| 188 | Generator->generateProfile(); |
| 189 | Generator->write(); |
| 190 | } |
| 191 | |
| 192 | return EXIT_SUCCESS; |
| 193 | } |
| 194 | |