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