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