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