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