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