Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 1 | //===- llvm-profdata.cpp - LLVM profile data tool -------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // llvm-profdata merges .profdata files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringRef.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 15 | #include "llvm/IR/LLVMContext.h" |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 16 | #include "llvm/ProfileData/InstrProfReader.h" |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 17 | #include "llvm/ProfileData/InstrProfWriter.h" |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 18 | #include "llvm/ProfileData/SampleProfReader.h" |
| 19 | #include "llvm/ProfileData/SampleProfWriter.h" |
Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CommandLine.h" |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 21 | #include "llvm/Support/FileSystem.h" |
Justin Bogner | 423380f | 2014-03-23 20:43:50 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Format.h" |
Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ManagedStatic.h" |
| 24 | #include "llvm/Support/MemoryBuffer.h" |
Benjamin Kramer | 16132e6 | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Path.h" |
Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 26 | #include "llvm/Support/PrettyStackTrace.h" |
| 27 | #include "llvm/Support/Signals.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
| 29 | |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 30 | #include <set> |
| 31 | |
Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 34 | static void exitWithError(const Twine &Message, |
| 35 | StringRef Whence = "", |
| 36 | StringRef Hint = "") { |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 37 | errs() << "error: "; |
| 38 | if (!Whence.empty()) |
| 39 | errs() << Whence << ": "; |
| 40 | errs() << Message << "\n"; |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 41 | if (!Hint.empty()) |
| 42 | errs() << Hint << "\n"; |
Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 43 | ::exit(1); |
| 44 | } |
| 45 | |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 46 | static void exitWithErrorCode(const std::error_code &Error, |
| 47 | StringRef Whence = "") { |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 48 | if (Error.category() == instrprof_category()) { |
| 49 | instrprof_error instrError = static_cast<instrprof_error>(Error.value()); |
| 50 | if (instrError == instrprof_error::unrecognized_format) { |
| 51 | // Hint for common error of forgetting -sample for sample profiles. |
| 52 | exitWithError(Error.message(), Whence, |
| 53 | "Perhaps you forgot to use the -sample option?"); |
| 54 | } |
| 55 | } |
| 56 | exitWithError(Error.message(), Whence); |
| 57 | } |
| 58 | |
Duncan P. N. Exon Smith | 02b6fa9 | 2015-06-16 00:43:04 +0000 | [diff] [blame] | 59 | namespace { |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 60 | enum ProfileKinds { instr, sample }; |
Duncan P. N. Exon Smith | 02b6fa9 | 2015-06-16 00:43:04 +0000 | [diff] [blame] | 61 | } |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 62 | |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 63 | static void handleMergeWriterError(std::error_code &Error, |
| 64 | StringRef WhenceFile = "", |
| 65 | StringRef WhenceFunction = "", |
| 66 | bool ShowHint = true) |
| 67 | { |
| 68 | if (!WhenceFile.empty()) |
| 69 | errs() << WhenceFile << ": "; |
| 70 | if (!WhenceFunction.empty()) |
| 71 | errs() << WhenceFunction << ": "; |
| 72 | errs() << Error.message() << "\n"; |
| 73 | |
| 74 | if (ShowHint) { |
| 75 | StringRef Hint = ""; |
| 76 | if (Error.category() == instrprof_category()) { |
| 77 | instrprof_error instrError = static_cast<instrprof_error>(Error.value()); |
| 78 | if (instrError == instrprof_error::count_mismatch) { |
| 79 | Hint = "Make sure that all profile data to be merged is generated " \ |
| 80 | "from the same binary."; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if (!Hint.empty()) |
| 85 | errs() << Hint << "\n"; |
| 86 | } |
| 87 | } |
| 88 | |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 89 | static void mergeInstrProfile(const cl::list<std::string> &Inputs, |
| 90 | StringRef OutputFilename) { |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 91 | if (OutputFilename.compare("-") == 0) |
| 92 | exitWithError("Cannot write indexed profdata format to stdout."); |
Justin Bogner | ec49f98 | 2014-03-12 22:00:57 +0000 | [diff] [blame] | 93 | |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 94 | std::error_code EC; |
| 95 | raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None); |
| 96 | if (EC) |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 97 | exitWithErrorCode(EC, OutputFilename); |
Justin Bogner | ec49f98 | 2014-03-12 22:00:57 +0000 | [diff] [blame] | 98 | |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 99 | InstrProfWriter Writer; |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 100 | std::set<std::error_code> WriterErrorCodes; |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 101 | for (const auto &Filename : Inputs) { |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 102 | auto ReaderOrErr = InstrProfReader::create(Filename); |
| 103 | if (std::error_code ec = ReaderOrErr.getError()) |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 104 | exitWithErrorCode(ec, Filename); |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 105 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 106 | auto Reader = std::move(ReaderOrErr.get()); |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 107 | for (auto &I : *Reader) { |
| 108 | if (std::error_code EC = Writer.addRecord(std::move(I))) { |
| 109 | // Only show hint the first time an error occurs. |
| 110 | bool firstTime = WriterErrorCodes.insert(EC).second; |
| 111 | handleMergeWriterError(EC, Filename, I.Name, firstTime); |
| 112 | } |
| 113 | } |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 114 | if (Reader->hasError()) |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 115 | exitWithErrorCode(Reader->getError(), Filename); |
Justin Bogner | bfee8d4 | 2014-03-12 20:14:17 +0000 | [diff] [blame] | 116 | } |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 117 | Writer.write(Output); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 120 | static void mergeSampleProfile(const cl::list<std::string> &Inputs, |
| 121 | StringRef OutputFilename, |
| 122 | sampleprof::SampleProfileFormat OutputFormat) { |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 123 | using namespace sampleprof; |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 124 | auto WriterOrErr = SampleProfileWriter::create(OutputFilename, OutputFormat); |
| 125 | if (std::error_code EC = WriterOrErr.getError()) |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 126 | exitWithErrorCode(EC, OutputFilename); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 127 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 128 | auto Writer = std::move(WriterOrErr.get()); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 129 | StringMap<FunctionSamples> ProfileMap; |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 130 | SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers; |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 131 | for (const auto &Filename : Inputs) { |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 132 | auto ReaderOrErr = |
| 133 | SampleProfileReader::create(Filename, getGlobalContext()); |
| 134 | if (std::error_code EC = ReaderOrErr.getError()) |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 135 | exitWithErrorCode(EC, Filename); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 136 | |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 137 | // We need to keep the readers around until after all the files are |
| 138 | // read so that we do not lose the function names stored in each |
| 139 | // reader's memory. The function names are needed to write out the |
| 140 | // merged profile map. |
| 141 | Readers.push_back(std::move(ReaderOrErr.get())); |
| 142 | const auto Reader = Readers.back().get(); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 143 | if (std::error_code EC = Reader->read()) |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 144 | exitWithErrorCode(EC, Filename); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 145 | |
| 146 | StringMap<FunctionSamples> &Profiles = Reader->getProfiles(); |
| 147 | for (StringMap<FunctionSamples>::iterator I = Profiles.begin(), |
| 148 | E = Profiles.end(); |
| 149 | I != E; ++I) { |
| 150 | StringRef FName = I->first(); |
| 151 | FunctionSamples &Samples = I->second; |
| 152 | ProfileMap[FName].merge(Samples); |
| 153 | } |
| 154 | } |
| 155 | Writer->write(ProfileMap); |
| 156 | } |
| 157 | |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 158 | static int merge_main(int argc, const char *argv[]) { |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 159 | cl::list<std::string> Inputs(cl::Positional, cl::Required, cl::OneOrMore, |
| 160 | cl::desc("<filenames...>")); |
| 161 | |
| 162 | cl::opt<std::string> OutputFilename("output", cl::value_desc("output"), |
| 163 | cl::init("-"), cl::Required, |
| 164 | cl::desc("Output file")); |
| 165 | cl::alias OutputFilenameA("o", cl::desc("Alias for --output"), |
| 166 | cl::aliasopt(OutputFilename)); |
| 167 | cl::opt<ProfileKinds> ProfileKind( |
| 168 | cl::desc("Profile kind:"), cl::init(instr), |
| 169 | cl::values(clEnumVal(instr, "Instrumentation profile (default)"), |
| 170 | clEnumVal(sample, "Sample profile"), clEnumValEnd)); |
| 171 | |
| 172 | cl::opt<sampleprof::SampleProfileFormat> OutputFormat( |
| 173 | cl::desc("Format of output profile (only meaningful with --sample)"), |
| 174 | cl::init(sampleprof::SPF_Binary), |
| 175 | cl::values(clEnumValN(sampleprof::SPF_Binary, "binary", |
| 176 | "Binary encoding (default)"), |
| 177 | clEnumValN(sampleprof::SPF_Text, "text", "Text encoding"), |
| 178 | clEnumValN(sampleprof::SPF_GCC, "gcc", "GCC encoding"), |
| 179 | clEnumValEnd)); |
| 180 | |
| 181 | cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n"); |
| 182 | |
| 183 | if (ProfileKind == instr) |
| 184 | mergeInstrProfile(Inputs, OutputFilename); |
| 185 | else |
| 186 | mergeSampleProfile(Inputs, OutputFilename, OutputFormat); |
Justin Bogner | bfee8d4 | 2014-03-12 20:14:17 +0000 | [diff] [blame] | 187 | |
Justin Bogner | ec49f98 | 2014-03-12 22:00:57 +0000 | [diff] [blame] | 188 | return 0; |
Justin Bogner | bfee8d4 | 2014-03-12 20:14:17 +0000 | [diff] [blame] | 189 | } |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 190 | |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 191 | static int showInstrProfile(std::string Filename, bool ShowCounts, |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 192 | bool ShowIndirectCallTargets, bool ShowAllFunctions, |
| 193 | std::string ShowFunction, raw_fd_ostream &OS) { |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 194 | auto ReaderOrErr = InstrProfReader::create(Filename); |
| 195 | if (std::error_code EC = ReaderOrErr.getError()) |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 196 | exitWithErrorCode(EC, Filename); |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 197 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 198 | auto Reader = std::move(ReaderOrErr.get()); |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 199 | uint64_t MaxFunctionCount = 0, MaxBlockCount = 0; |
| 200 | size_t ShownFunctions = 0, TotalFunctions = 0; |
| 201 | for (const auto &Func : *Reader) { |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 202 | bool Show = |
| 203 | ShowAllFunctions || (!ShowFunction.empty() && |
| 204 | Func.Name.find(ShowFunction) != Func.Name.npos); |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 205 | |
| 206 | ++TotalFunctions; |
Justin Bogner | b59d7c7 | 2014-04-25 02:45:33 +0000 | [diff] [blame] | 207 | assert(Func.Counts.size() > 0 && "function missing entry counter"); |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 208 | if (Func.Counts[0] > MaxFunctionCount) |
| 209 | MaxFunctionCount = Func.Counts[0]; |
| 210 | |
| 211 | if (Show) { |
| 212 | if (!ShownFunctions) |
| 213 | OS << "Counters:\n"; |
| 214 | ++ShownFunctions; |
| 215 | |
| 216 | OS << " " << Func.Name << ":\n" |
Justin Bogner | 423380f | 2014-03-23 20:43:50 +0000 | [diff] [blame] | 217 | << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n" |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 218 | << " Counters: " << Func.Counts.size() << "\n" |
| 219 | << " Function count: " << Func.Counts[0] << "\n"; |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 220 | if (ShowIndirectCallTargets) |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 221 | OS << " Indirect Call Site Count: " |
| 222 | << Func.getNumValueSites(IPVK_IndirectCallTarget) << "\n"; |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | if (Show && ShowCounts) |
| 226 | OS << " Block counts: ["; |
| 227 | for (size_t I = 1, E = Func.Counts.size(); I < E; ++I) { |
| 228 | if (Func.Counts[I] > MaxBlockCount) |
| 229 | MaxBlockCount = Func.Counts[I]; |
| 230 | if (Show && ShowCounts) |
| 231 | OS << (I == 1 ? "" : ", ") << Func.Counts[I]; |
| 232 | } |
| 233 | if (Show && ShowCounts) |
| 234 | OS << "]\n"; |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 235 | |
| 236 | if (Show && ShowIndirectCallTargets) { |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 237 | uint32_t NS = Func.getNumValueSites(IPVK_IndirectCallTarget); |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 238 | OS << " Indirect Target Results: \n"; |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 239 | for (size_t I = 0; I < NS; ++I) { |
| 240 | uint32_t NV = Func.getNumValueDataForSite(IPVK_IndirectCallTarget, I); |
| 241 | std::unique_ptr<InstrProfValueData[]> VD = |
| 242 | Func.getValueForSite(IPVK_IndirectCallTarget, I); |
| 243 | for (uint32_t V = 0; V < NV; V++) { |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 244 | OS << "\t[ " << I << ", "; |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 245 | OS << (const char *)VD[V].Value << ", " << VD[V].Count << " ]\n"; |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | } |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 249 | } |
Justin Bogner | db1225d | 2014-03-23 20:55:53 +0000 | [diff] [blame] | 250 | if (Reader->hasError()) |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 251 | exitWithErrorCode(Reader->getError(), Filename); |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 252 | |
| 253 | if (ShowAllFunctions || !ShowFunction.empty()) |
| 254 | OS << "Functions shown: " << ShownFunctions << "\n"; |
| 255 | OS << "Total functions: " << TotalFunctions << "\n"; |
| 256 | OS << "Maximum function count: " << MaxFunctionCount << "\n"; |
| 257 | OS << "Maximum internal block count: " << MaxBlockCount << "\n"; |
| 258 | return 0; |
| 259 | } |
| 260 | |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 261 | static int showSampleProfile(std::string Filename, bool ShowCounts, |
| 262 | bool ShowAllFunctions, std::string ShowFunction, |
| 263 | raw_fd_ostream &OS) { |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 264 | using namespace sampleprof; |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 265 | auto ReaderOrErr = SampleProfileReader::create(Filename, getGlobalContext()); |
| 266 | if (std::error_code EC = ReaderOrErr.getError()) |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 267 | exitWithErrorCode(EC, Filename); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 268 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 269 | auto Reader = std::move(ReaderOrErr.get()); |
Diego Novillo | c6d032a | 2015-09-17 00:17:21 +0000 | [diff] [blame] | 270 | if (std::error_code EC = Reader->read()) |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 271 | exitWithErrorCode(EC, Filename); |
Diego Novillo | c6d032a | 2015-09-17 00:17:21 +0000 | [diff] [blame] | 272 | |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 273 | if (ShowAllFunctions || ShowFunction.empty()) |
| 274 | Reader->dump(OS); |
| 275 | else |
| 276 | Reader->dumpFunctionProfile(ShowFunction, OS); |
| 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 281 | static int show_main(int argc, const char *argv[]) { |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 282 | cl::opt<std::string> Filename(cl::Positional, cl::Required, |
| 283 | cl::desc("<profdata-file>")); |
| 284 | |
| 285 | cl::opt<bool> ShowCounts("counts", cl::init(false), |
| 286 | cl::desc("Show counter values for shown functions")); |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 287 | cl::opt<bool> ShowIndirectCallTargets( |
| 288 | "ic-targets", cl::init(false), |
| 289 | cl::desc("Show indirect call site target values for shown functions")); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 290 | cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false), |
| 291 | cl::desc("Details for every function")); |
| 292 | cl::opt<std::string> ShowFunction("function", |
| 293 | cl::desc("Details for matching functions")); |
| 294 | |
| 295 | cl::opt<std::string> OutputFilename("output", cl::value_desc("output"), |
| 296 | cl::init("-"), cl::desc("Output file")); |
| 297 | cl::alias OutputFilenameA("o", cl::desc("Alias for --output"), |
| 298 | cl::aliasopt(OutputFilename)); |
| 299 | cl::opt<ProfileKinds> ProfileKind( |
| 300 | cl::desc("Profile kind:"), cl::init(instr), |
| 301 | cl::values(clEnumVal(instr, "Instrumentation profile (default)"), |
| 302 | clEnumVal(sample, "Sample profile"), clEnumValEnd)); |
| 303 | |
| 304 | cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n"); |
| 305 | |
| 306 | if (OutputFilename.empty()) |
| 307 | OutputFilename = "-"; |
| 308 | |
| 309 | std::error_code EC; |
| 310 | raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text); |
| 311 | if (EC) |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 312 | exitWithErrorCode(EC, OutputFilename); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 313 | |
| 314 | if (ShowAllFunctions && !ShowFunction.empty()) |
| 315 | errs() << "warning: -function argument ignored: showing all functions\n"; |
| 316 | |
| 317 | if (ProfileKind == instr) |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 318 | return showInstrProfile(Filename, ShowCounts, ShowIndirectCallTargets, |
| 319 | ShowAllFunctions, ShowFunction, OS); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 320 | else |
| 321 | return showSampleProfile(Filename, ShowCounts, ShowAllFunctions, |
| 322 | ShowFunction, OS); |
| 323 | } |
| 324 | |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 325 | int main(int argc, const char *argv[]) { |
| 326 | // Print a stack trace if we signal out. |
| 327 | sys::PrintStackTraceOnErrorSignal(); |
| 328 | PrettyStackTraceProgram X(argc, argv); |
| 329 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 330 | |
| 331 | StringRef ProgName(sys::path::filename(argv[0])); |
| 332 | if (argc > 1) { |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 333 | int (*func)(int, const char *[]) = nullptr; |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 334 | |
| 335 | if (strcmp(argv[1], "merge") == 0) |
| 336 | func = merge_main; |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 337 | else if (strcmp(argv[1], "show") == 0) |
| 338 | func = show_main; |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 339 | |
| 340 | if (func) { |
| 341 | std::string Invocation(ProgName.str() + " " + argv[1]); |
| 342 | argv[1] = Invocation.c_str(); |
| 343 | return func(argc - 1, argv + 1); |
| 344 | } |
| 345 | |
| 346 | if (strcmp(argv[1], "-h") == 0 || |
| 347 | strcmp(argv[1], "-help") == 0 || |
| 348 | strcmp(argv[1], "--help") == 0) { |
| 349 | |
| 350 | errs() << "OVERVIEW: LLVM profile data tools\n\n" |
| 351 | << "USAGE: " << ProgName << " <command> [args...]\n" |
| 352 | << "USAGE: " << ProgName << " <command> -help\n\n" |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 353 | << "Available commands: merge, show\n"; |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 354 | return 0; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | if (argc < 2) |
| 359 | errs() << ProgName << ": No command specified!\n"; |
| 360 | else |
| 361 | errs() << ProgName << ": Unknown command!\n"; |
| 362 | |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 363 | errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n"; |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 364 | return 1; |
| 365 | } |