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" |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 32 | #include "llvm/Support/ThreadPool.h" |
Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 34 | #include <algorithm> |
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 | |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 51 | static void exitWithError(Error E, StringRef Whence = "") { |
| 52 | if (E.isA<InstrProfError>()) { |
| 53 | handleAllErrors(std::move(E), [&](const InstrProfError &IPE) { |
| 54 | instrprof_error instrError = IPE.get(); |
| 55 | StringRef Hint = ""; |
| 56 | if (instrError == instrprof_error::unrecognized_format) { |
| 57 | // Hint for common error of forgetting -sample for sample profiles. |
| 58 | Hint = "Perhaps you forgot to use the -sample option?"; |
| 59 | } |
| 60 | exitWithError(IPE.message(), Whence, Hint); |
| 61 | }); |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 62 | } |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 63 | |
| 64 | exitWithError(toString(std::move(E)), Whence); |
| 65 | } |
| 66 | |
| 67 | static void exitWithErrorCode(std::error_code EC, StringRef Whence = "") { |
| 68 | exitWithError(EC.message(), Whence); |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Duncan P. N. Exon Smith | 02b6fa9 | 2015-06-16 00:43:04 +0000 | [diff] [blame] | 71 | namespace { |
Diego Novillo | d3babdb | 2015-12-14 20:37:15 +0000 | [diff] [blame] | 72 | enum ProfileKinds { instr, sample }; |
Duncan P. N. Exon Smith | 02b6fa9 | 2015-06-16 00:43:04 +0000 | [diff] [blame] | 73 | } |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 74 | |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 75 | static void handleMergeWriterError(Error E, StringRef WhenceFile = "", |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 76 | StringRef WhenceFunction = "", |
Diego Novillo | d3babdb | 2015-12-14 20:37:15 +0000 | [diff] [blame] | 77 | bool ShowHint = true) { |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 78 | if (!WhenceFile.empty()) |
| 79 | errs() << WhenceFile << ": "; |
| 80 | if (!WhenceFunction.empty()) |
| 81 | errs() << WhenceFunction << ": "; |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 82 | |
| 83 | auto IPE = instrprof_error::success; |
| 84 | E = handleErrors(std::move(E), |
| 85 | [&IPE](std::unique_ptr<InstrProfError> E) -> Error { |
| 86 | IPE = E->get(); |
| 87 | return Error(std::move(E)); |
| 88 | }); |
| 89 | errs() << toString(std::move(E)) << "\n"; |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 90 | |
| 91 | if (ShowHint) { |
| 92 | StringRef Hint = ""; |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 93 | if (IPE != instrprof_error::success) { |
| 94 | switch (IPE) { |
Nathan Slingerland | 11c938d1 | 2015-11-17 23:37:09 +0000 | [diff] [blame] | 95 | case instrprof_error::hash_mismatch: |
| 96 | case instrprof_error::count_mismatch: |
| 97 | case instrprof_error::value_site_count_mismatch: |
Diego Novillo | d3babdb | 2015-12-14 20:37:15 +0000 | [diff] [blame] | 98 | Hint = "Make sure that all profile data to be merged is generated " |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 99 | "from the same binary."; |
Nathan Slingerland | 11c938d1 | 2015-11-17 23:37:09 +0000 | [diff] [blame] | 100 | break; |
Nathan Slingerland | b2d95f0 | 2015-11-18 00:52:45 +0000 | [diff] [blame] | 101 | default: |
| 102 | break; |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | if (!Hint.empty()) |
| 107 | errs() << Hint << "\n"; |
| 108 | } |
| 109 | } |
| 110 | |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 111 | struct WeightedFile { |
Xinliang David Li | 9a1bfcf | 2016-07-20 22:24:52 +0000 | [diff] [blame] | 112 | std::string Filename; |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 113 | uint64_t Weight; |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 114 | }; |
| 115 | typedef SmallVector<WeightedFile, 5> WeightedFileVector; |
| 116 | |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 117 | /// Keep track of merged data and reported errors. |
| 118 | struct WriterContext { |
| 119 | std::mutex Lock; |
| 120 | InstrProfWriter Writer; |
| 121 | Error Err; |
| 122 | StringRef ErrWhence; |
| 123 | std::mutex &ErrLock; |
| 124 | SmallSet<instrprof_error, 4> &WriterErrorCodes; |
| 125 | |
| 126 | WriterContext(bool IsSparse, std::mutex &ErrLock, |
| 127 | SmallSet<instrprof_error, 4> &WriterErrorCodes) |
| 128 | : Lock(), Writer(IsSparse), Err(Error::success()), ErrWhence(""), |
| 129 | ErrLock(ErrLock), WriterErrorCodes(WriterErrorCodes) {} |
| 130 | }; |
| 131 | |
| 132 | /// Load an input into a writer context. |
| 133 | static void loadInput(const WeightedFile &Input, WriterContext *WC) { |
| 134 | std::unique_lock<std::mutex> CtxGuard{WC->Lock}; |
| 135 | |
| 136 | // If there's a pending hard error, don't do more work. |
| 137 | if (WC->Err) |
| 138 | return; |
| 139 | |
| 140 | WC->ErrWhence = Input.Filename; |
| 141 | |
| 142 | auto ReaderOrErr = InstrProfReader::create(Input.Filename); |
Rong Xu | 2c684cf | 2016-10-19 22:51:17 +0000 | [diff] [blame^] | 143 | if (Error E = ReaderOrErr.takeError()) { |
| 144 | // Skip the empty profiles by returning sliently. |
| 145 | instrprof_error IPE = InstrProfError::take(std::move(E)); |
| 146 | if (IPE != instrprof_error::empty_raw_profile) |
| 147 | WC->Err = make_error<InstrProfError>(IPE); |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 148 | return; |
Rong Xu | 2c684cf | 2016-10-19 22:51:17 +0000 | [diff] [blame^] | 149 | } |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 150 | |
| 151 | auto Reader = std::move(ReaderOrErr.get()); |
| 152 | bool IsIRProfile = Reader->isIRLevelProfile(); |
| 153 | if (WC->Writer.setIsIRLevelProfile(IsIRProfile)) { |
| 154 | WC->Err = make_error<StringError>( |
| 155 | "Merge IR generated profile with Clang generated profile.", |
| 156 | std::error_code()); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | for (auto &I : *Reader) { |
| 161 | if (Error E = WC->Writer.addRecord(std::move(I), Input.Weight)) { |
| 162 | // Only show hint the first time an error occurs. |
| 163 | instrprof_error IPE = InstrProfError::take(std::move(E)); |
| 164 | std::unique_lock<std::mutex> ErrGuard{WC->ErrLock}; |
| 165 | bool firstTime = WC->WriterErrorCodes.insert(IPE).second; |
| 166 | handleMergeWriterError(make_error<InstrProfError>(IPE), Input.Filename, |
| 167 | I.Name, firstTime); |
| 168 | } |
| 169 | } |
| 170 | if (Reader->hasError()) |
| 171 | WC->Err = Reader->getError(); |
| 172 | } |
| 173 | |
| 174 | /// Merge the \p Src writer context into \p Dst. |
| 175 | static void mergeWriterContexts(WriterContext *Dst, WriterContext *Src) { |
| 176 | if (Error E = Dst->Writer.mergeRecordsFromWriter(std::move(Src->Writer))) |
| 177 | Dst->Err = std::move(E); |
| 178 | } |
| 179 | |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 180 | static void mergeInstrProfile(const WeightedFileVector &Inputs, |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 181 | StringRef OutputFilename, |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 182 | ProfileFormat OutputFormat, bool OutputSparse, |
| 183 | unsigned NumThreads) { |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 184 | if (OutputFilename.compare("-") == 0) |
| 185 | exitWithError("Cannot write indexed profdata format to stdout."); |
Justin Bogner | ec49f98 | 2014-03-12 22:00:57 +0000 | [diff] [blame] | 186 | |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 187 | if (OutputFormat != PF_Binary && OutputFormat != PF_Text) |
| 188 | exitWithError("Unknown format is specified."); |
| 189 | |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 190 | std::error_code EC; |
| 191 | raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None); |
| 192 | if (EC) |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 193 | exitWithErrorCode(EC, OutputFilename); |
Justin Bogner | ec49f98 | 2014-03-12 22:00:57 +0000 | [diff] [blame] | 194 | |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 195 | std::mutex ErrorLock; |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 196 | SmallSet<instrprof_error, 4> WriterErrorCodes; |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 197 | |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 198 | // If NumThreads is not specified, auto-detect a good default. |
| 199 | if (NumThreads == 0) |
| 200 | NumThreads = std::max(1U, std::min(std::thread::hardware_concurrency(), |
| 201 | unsigned(Inputs.size() / 2))); |
Rong Xu | 33c76c0 | 2016-02-10 17:18:30 +0000 | [diff] [blame] | 202 | |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 203 | // Initialize the writer contexts. |
| 204 | SmallVector<std::unique_ptr<WriterContext>, 4> Contexts; |
| 205 | for (unsigned I = 0; I < NumThreads; ++I) |
| 206 | Contexts.emplace_back(llvm::make_unique<WriterContext>( |
| 207 | OutputSparse, ErrorLock, WriterErrorCodes)); |
| 208 | |
| 209 | if (NumThreads == 1) { |
| 210 | for (const auto &Input : Inputs) |
| 211 | loadInput(Input, Contexts[0].get()); |
| 212 | } else { |
| 213 | ThreadPool Pool(NumThreads); |
| 214 | |
| 215 | // Load the inputs in parallel (N/NumThreads serial steps). |
| 216 | unsigned Ctx = 0; |
| 217 | for (const auto &Input : Inputs) { |
| 218 | Pool.async(loadInput, Input, Contexts[Ctx].get()); |
| 219 | Ctx = (Ctx + 1) % NumThreads; |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 220 | } |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 221 | Pool.wait(); |
| 222 | |
| 223 | // Merge the writer contexts together (~ lg(NumThreads) serial steps). |
| 224 | unsigned Mid = Contexts.size() / 2; |
| 225 | unsigned End = Contexts.size(); |
| 226 | assert(Mid > 0 && "Expected more than one context"); |
| 227 | do { |
| 228 | for (unsigned I = 0; I < Mid; ++I) |
| 229 | Pool.async(mergeWriterContexts, Contexts[I].get(), |
| 230 | Contexts[I + Mid].get()); |
| 231 | Pool.wait(); |
| 232 | if (End & 1) { |
| 233 | Pool.async(mergeWriterContexts, Contexts[0].get(), |
| 234 | Contexts[End - 1].get()); |
| 235 | Pool.wait(); |
| 236 | } |
| 237 | End = Mid; |
| 238 | Mid /= 2; |
| 239 | } while (Mid > 0); |
Justin Bogner | bfee8d4 | 2014-03-12 20:14:17 +0000 | [diff] [blame] | 240 | } |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 241 | |
| 242 | // Handle deferred hard errors encountered during merging. |
| 243 | for (std::unique_ptr<WriterContext> &WC : Contexts) |
| 244 | if (WC->Err) |
| 245 | exitWithError(std::move(WC->Err), WC->ErrWhence); |
| 246 | |
| 247 | InstrProfWriter &Writer = Contexts[0]->Writer; |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 248 | if (OutputFormat == PF_Text) |
| 249 | Writer.writeText(Output); |
| 250 | else |
| 251 | Writer.write(Output); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 254 | static sampleprof::SampleProfileFormat FormatMap[] = { |
| 255 | sampleprof::SPF_None, sampleprof::SPF_Text, sampleprof::SPF_Binary, |
| 256 | sampleprof::SPF_GCC}; |
| 257 | |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 258 | static void mergeSampleProfile(const WeightedFileVector &Inputs, |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 259 | StringRef OutputFilename, |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 260 | ProfileFormat OutputFormat) { |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 261 | using namespace sampleprof; |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 262 | auto WriterOrErr = |
| 263 | SampleProfileWriter::create(OutputFilename, FormatMap[OutputFormat]); |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 264 | if (std::error_code EC = WriterOrErr.getError()) |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 265 | exitWithErrorCode(EC, OutputFilename); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 266 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 267 | auto Writer = std::move(WriterOrErr.get()); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 268 | StringMap<FunctionSamples> ProfileMap; |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 269 | SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers; |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 270 | LLVMContext Context; |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 271 | for (const auto &Input : Inputs) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 272 | auto ReaderOrErr = SampleProfileReader::create(Input.Filename, Context); |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 273 | if (std::error_code EC = ReaderOrErr.getError()) |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 274 | exitWithErrorCode(EC, Input.Filename); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 275 | |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 276 | // We need to keep the readers around until after all the files are |
| 277 | // read so that we do not lose the function names stored in each |
| 278 | // reader's memory. The function names are needed to write out the |
| 279 | // merged profile map. |
| 280 | Readers.push_back(std::move(ReaderOrErr.get())); |
| 281 | const auto Reader = Readers.back().get(); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 282 | if (std::error_code EC = Reader->read()) |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 283 | exitWithErrorCode(EC, Input.Filename); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 284 | |
| 285 | StringMap<FunctionSamples> &Profiles = Reader->getProfiles(); |
| 286 | for (StringMap<FunctionSamples>::iterator I = Profiles.begin(), |
| 287 | E = Profiles.end(); |
| 288 | I != E; ++I) { |
| 289 | StringRef FName = I->first(); |
| 290 | FunctionSamples &Samples = I->second; |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 291 | sampleprof_error Result = ProfileMap[FName].merge(Samples, Input.Weight); |
| 292 | if (Result != sampleprof_error::success) { |
| 293 | std::error_code EC = make_error_code(Result); |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 294 | handleMergeWriterError(errorCodeToError(EC), Input.Filename, FName); |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 295 | } |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | Writer->write(ProfileMap); |
| 299 | } |
| 300 | |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 301 | static WeightedFile parseWeightedFile(const StringRef &WeightedFilename) { |
Vedant Kumar | 8d0e861 | 2016-06-06 23:43:56 +0000 | [diff] [blame] | 302 | StringRef WeightStr, FileName; |
| 303 | std::tie(WeightStr, FileName) = WeightedFilename.split(','); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 304 | |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 305 | uint64_t Weight; |
| 306 | if (WeightStr.getAsInteger(10, Weight) || Weight < 1) |
| 307 | exitWithError("Input weight must be a positive integer."); |
| 308 | |
Benjamin Kramer | 929e7db | 2016-07-21 14:29:11 +0000 | [diff] [blame] | 309 | return {FileName, Weight}; |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Vedant Kumar | cef4360 | 2016-06-07 22:47:31 +0000 | [diff] [blame] | 312 | static std::unique_ptr<MemoryBuffer> |
| 313 | getInputFilenamesFileBuf(const StringRef &InputFilenamesFile) { |
| 314 | if (InputFilenamesFile == "") |
| 315 | return {}; |
| 316 | |
| 317 | auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFilenamesFile); |
| 318 | if (!BufOrError) |
| 319 | exitWithErrorCode(BufOrError.getError(), InputFilenamesFile); |
| 320 | |
| 321 | return std::move(*BufOrError); |
| 322 | } |
| 323 | |
Xinliang David Li | 9a1bfcf | 2016-07-20 22:24:52 +0000 | [diff] [blame] | 324 | static void addWeightedInput(WeightedFileVector &WNI, const WeightedFile &WF) { |
| 325 | StringRef Filename = WF.Filename; |
| 326 | uint64_t Weight = WF.Weight; |
Benjamin Kramer | a81f472 | 2016-07-22 12:39:55 +0000 | [diff] [blame] | 327 | |
| 328 | // If it's STDIN just pass it on. |
| 329 | if (Filename == "-") { |
| 330 | WNI.push_back({Filename, Weight}); |
| 331 | return; |
| 332 | } |
| 333 | |
Xinliang David Li | 9a1bfcf | 2016-07-20 22:24:52 +0000 | [diff] [blame] | 334 | llvm::sys::fs::file_status Status; |
| 335 | llvm::sys::fs::status(Filename, Status); |
| 336 | if (!llvm::sys::fs::exists(Status)) |
| 337 | exitWithErrorCode(make_error_code(errc::no_such_file_or_directory), |
| 338 | Filename); |
| 339 | // If it's a source file, collect it. |
| 340 | if (llvm::sys::fs::is_regular_file(Status)) { |
Benjamin Kramer | 929e7db | 2016-07-21 14:29:11 +0000 | [diff] [blame] | 341 | WNI.push_back({Filename, Weight}); |
Xinliang David Li | 9a1bfcf | 2016-07-20 22:24:52 +0000 | [diff] [blame] | 342 | return; |
| 343 | } |
| 344 | |
| 345 | if (llvm::sys::fs::is_directory(Status)) { |
| 346 | std::error_code EC; |
| 347 | for (llvm::sys::fs::recursive_directory_iterator F(Filename, EC), E; |
| 348 | F != E && !EC; F.increment(EC)) { |
| 349 | if (llvm::sys::fs::is_regular_file(F->path())) { |
| 350 | addWeightedInput(WNI, {F->path(), Weight}); |
| 351 | } |
| 352 | } |
| 353 | if (EC) |
| 354 | exitWithErrorCode(EC, Filename); |
| 355 | } |
| 356 | } |
| 357 | |
Vedant Kumar | cef4360 | 2016-06-07 22:47:31 +0000 | [diff] [blame] | 358 | static void parseInputFilenamesFile(MemoryBuffer *Buffer, |
| 359 | WeightedFileVector &WFV) { |
| 360 | if (!Buffer) |
| 361 | return; |
| 362 | |
| 363 | SmallVector<StringRef, 8> Entries; |
| 364 | StringRef Data = Buffer->getBuffer(); |
| 365 | Data.split(Entries, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false); |
| 366 | for (const StringRef &FileWeightEntry : Entries) { |
| 367 | StringRef SanitizedEntry = FileWeightEntry.trim(" \t\v\f\r"); |
| 368 | // Skip comments. |
| 369 | if (SanitizedEntry.startswith("#")) |
| 370 | continue; |
| 371 | // If there's no comma, it's an unweighted profile. |
| 372 | else if (SanitizedEntry.find(',') == StringRef::npos) |
Xinliang David Li | 9a1bfcf | 2016-07-20 22:24:52 +0000 | [diff] [blame] | 373 | addWeightedInput(WFV, {SanitizedEntry, 1}); |
Vedant Kumar | cef4360 | 2016-06-07 22:47:31 +0000 | [diff] [blame] | 374 | else |
Xinliang David Li | 9a1bfcf | 2016-07-20 22:24:52 +0000 | [diff] [blame] | 375 | addWeightedInput(WFV, parseWeightedFile(SanitizedEntry)); |
Vedant Kumar | cef4360 | 2016-06-07 22:47:31 +0000 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 379 | static int merge_main(int argc, const char *argv[]) { |
| 380 | cl::list<std::string> InputFilenames(cl::Positional, |
| 381 | cl::desc("<filename...>")); |
| 382 | cl::list<std::string> WeightedInputFilenames("weighted-input", |
| 383 | cl::desc("<weight>,<filename>")); |
Vedant Kumar | cef4360 | 2016-06-07 22:47:31 +0000 | [diff] [blame] | 384 | cl::opt<std::string> InputFilenamesFile( |
| 385 | "input-files", cl::init(""), |
| 386 | cl::desc("Path to file containing newline-separated " |
| 387 | "[<weight>,]<filename> entries")); |
| 388 | cl::alias InputFilenamesFileA("f", cl::desc("Alias for --input-files"), |
| 389 | cl::aliasopt(InputFilenamesFile)); |
| 390 | cl::opt<bool> DumpInputFileList( |
| 391 | "dump-input-file-list", cl::init(false), cl::Hidden, |
| 392 | cl::desc("Dump the list of input files and their weights, then exit")); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 393 | cl::opt<std::string> OutputFilename("output", cl::value_desc("output"), |
| 394 | cl::init("-"), cl::Required, |
| 395 | cl::desc("Output file")); |
| 396 | cl::alias OutputFilenameA("o", cl::desc("Alias for --output"), |
| 397 | cl::aliasopt(OutputFilename)); |
| 398 | cl::opt<ProfileKinds> ProfileKind( |
| 399 | cl::desc("Profile kind:"), cl::init(instr), |
| 400 | cl::values(clEnumVal(instr, "Instrumentation profile (default)"), |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 401 | clEnumVal(sample, "Sample profile"))); |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 402 | cl::opt<ProfileFormat> OutputFormat( |
| 403 | cl::desc("Format of output profile"), cl::init(PF_Binary), |
| 404 | cl::values(clEnumValN(PF_Binary, "binary", "Binary encoding (default)"), |
| 405 | clEnumValN(PF_Text, "text", "Text encoding"), |
| 406 | clEnumValN(PF_GCC, "gcc", |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 407 | "GCC encoding (only meaningful for -sample)"))); |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 408 | cl::opt<bool> OutputSparse("sparse", cl::init(false), |
| 409 | cl::desc("Generate a sparse profile (only meaningful for -instr)")); |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 410 | cl::opt<unsigned> NumThreads( |
| 411 | "num-threads", cl::init(0), |
| 412 | cl::desc("Number of merge threads to use (default: autodetect)")); |
| 413 | cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"), |
| 414 | cl::aliasopt(NumThreads)); |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 415 | |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 416 | cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n"); |
| 417 | |
Vedant Kumar | cef4360 | 2016-06-07 22:47:31 +0000 | [diff] [blame] | 418 | WeightedFileVector WeightedInputs; |
| 419 | for (StringRef Filename : InputFilenames) |
Xinliang David Li | 9a1bfcf | 2016-07-20 22:24:52 +0000 | [diff] [blame] | 420 | addWeightedInput(WeightedInputs, {Filename, 1}); |
Vedant Kumar | cef4360 | 2016-06-07 22:47:31 +0000 | [diff] [blame] | 421 | for (StringRef WeightedFilename : WeightedInputFilenames) |
Xinliang David Li | 9a1bfcf | 2016-07-20 22:24:52 +0000 | [diff] [blame] | 422 | addWeightedInput(WeightedInputs, parseWeightedFile(WeightedFilename)); |
Vedant Kumar | cef4360 | 2016-06-07 22:47:31 +0000 | [diff] [blame] | 423 | |
| 424 | // Make sure that the file buffer stays alive for the duration of the |
| 425 | // weighted input vector's lifetime. |
| 426 | auto Buffer = getInputFilenamesFileBuf(InputFilenamesFile); |
| 427 | parseInputFilenamesFile(Buffer.get(), WeightedInputs); |
| 428 | |
| 429 | if (WeightedInputs.empty()) |
Chandler Carruth | 0c30f89 | 2016-06-04 03:08:01 +0000 | [diff] [blame] | 430 | exitWithError("No input files specified. See " + |
| 431 | sys::path::filename(argv[0]) + " -help"); |
| 432 | |
Vedant Kumar | cef4360 | 2016-06-07 22:47:31 +0000 | [diff] [blame] | 433 | if (DumpInputFileList) { |
| 434 | for (auto &WF : WeightedInputs) |
| 435 | outs() << WF.Weight << "," << WF.Filename << "\n"; |
| 436 | return 0; |
| 437 | } |
Vedant Kumar | f771a05 | 2016-06-04 00:36:28 +0000 | [diff] [blame] | 438 | |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 439 | if (ProfileKind == instr) |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 440 | mergeInstrProfile(WeightedInputs, OutputFilename, OutputFormat, |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 441 | OutputSparse, NumThreads); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 442 | else |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 443 | mergeSampleProfile(WeightedInputs, OutputFilename, OutputFormat); |
Justin Bogner | bfee8d4 | 2014-03-12 20:14:17 +0000 | [diff] [blame] | 444 | |
Justin Bogner | ec49f98 | 2014-03-12 22:00:57 +0000 | [diff] [blame] | 445 | return 0; |
Justin Bogner | bfee8d4 | 2014-03-12 20:14:17 +0000 | [diff] [blame] | 446 | } |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 447 | |
Benjamin Kramer | 1afc1de | 2016-06-17 20:41:14 +0000 | [diff] [blame] | 448 | static int showInstrProfile(const std::string &Filename, bool ShowCounts, |
Easwaran Raman | 183ebbe | 2016-01-13 21:44:36 +0000 | [diff] [blame] | 449 | bool ShowIndirectCallTargets, |
| 450 | bool ShowDetailedSummary, |
| 451 | std::vector<uint32_t> DetailedSummaryCutoffs, |
Benjamin Kramer | 1afc1de | 2016-06-17 20:41:14 +0000 | [diff] [blame] | 452 | bool ShowAllFunctions, |
| 453 | const std::string &ShowFunction, bool TextFormat, |
| 454 | raw_fd_ostream &OS) { |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 455 | auto ReaderOrErr = InstrProfReader::create(Filename); |
Benjamin Kramer | 1afc1de | 2016-06-17 20:41:14 +0000 | [diff] [blame] | 456 | std::vector<uint32_t> Cutoffs = std::move(DetailedSummaryCutoffs); |
| 457 | if (ShowDetailedSummary && Cutoffs.empty()) { |
Easwaran Raman | 183ebbe | 2016-01-13 21:44:36 +0000 | [diff] [blame] | 458 | Cutoffs = {800000, 900000, 950000, 990000, 999000, 999900, 999990}; |
| 459 | } |
Benjamin Kramer | 1afc1de | 2016-06-17 20:41:14 +0000 | [diff] [blame] | 460 | InstrProfSummaryBuilder Builder(std::move(Cutoffs)); |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 461 | if (Error E = ReaderOrErr.takeError()) |
| 462 | exitWithError(std::move(E), Filename); |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 463 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 464 | auto Reader = std::move(ReaderOrErr.get()); |
Rong Xu | 33c76c0 | 2016-02-10 17:18:30 +0000 | [diff] [blame] | 465 | bool IsIRInstr = Reader->isIRLevelProfile(); |
Easwaran Raman | 183ebbe | 2016-01-13 21:44:36 +0000 | [diff] [blame] | 466 | size_t ShownFunctions = 0; |
Xinliang David Li | 872362c | 2016-05-23 16:36:11 +0000 | [diff] [blame] | 467 | uint64_t TotalNumValueSites = 0; |
| 468 | uint64_t TotalNumValueSitesWithValueProfile = 0; |
| 469 | uint64_t TotalNumValues = 0; |
Xinliang David Li | deda33c | 2016-09-20 21:04:22 +0000 | [diff] [blame] | 470 | std::vector<unsigned> ICHistogram; |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 471 | for (const auto &Func : *Reader) { |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 472 | bool Show = |
| 473 | ShowAllFunctions || (!ShowFunction.empty() && |
| 474 | Func.Name.find(ShowFunction) != Func.Name.npos); |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 475 | |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 476 | bool doTextFormatDump = (Show && ShowCounts && TextFormat); |
| 477 | |
| 478 | if (doTextFormatDump) { |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 479 | InstrProfSymtab &Symtab = Reader->getSymtab(); |
| 480 | InstrProfWriter::writeRecordInText(Func, Symtab, OS); |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 481 | continue; |
| 482 | } |
| 483 | |
Justin Bogner | b59d7c7 | 2014-04-25 02:45:33 +0000 | [diff] [blame] | 484 | assert(Func.Counts.size() > 0 && "function missing entry counter"); |
Easwaran Raman | e5a17e3 | 2016-05-19 21:07:12 +0000 | [diff] [blame] | 485 | Builder.addRecord(Func); |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 486 | |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 487 | if (Show) { |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 488 | |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 489 | if (!ShownFunctions) |
| 490 | OS << "Counters:\n"; |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 491 | |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 492 | ++ShownFunctions; |
| 493 | |
| 494 | OS << " " << Func.Name << ":\n" |
Justin Bogner | 423380f | 2014-03-23 20:43:50 +0000 | [diff] [blame] | 495 | << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n" |
Rong Xu | 33c76c0 | 2016-02-10 17:18:30 +0000 | [diff] [blame] | 496 | << " Counters: " << Func.Counts.size() << "\n"; |
| 497 | if (!IsIRInstr) |
| 498 | OS << " Function count: " << Func.Counts[0] << "\n"; |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 499 | |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 500 | if (ShowIndirectCallTargets) |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 501 | OS << " Indirect Call Site Count: " |
| 502 | << Func.getNumValueSites(IPVK_IndirectCallTarget) << "\n"; |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 503 | |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 504 | if (ShowCounts) { |
| 505 | OS << " Block counts: ["; |
Rong Xu | 33c76c0 | 2016-02-10 17:18:30 +0000 | [diff] [blame] | 506 | size_t Start = (IsIRInstr ? 0 : 1); |
| 507 | for (size_t I = Start, E = Func.Counts.size(); I < E; ++I) { |
| 508 | OS << (I == Start ? "" : ", ") << Func.Counts[I]; |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 509 | } |
| 510 | OS << "]\n"; |
| 511 | } |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 512 | |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 513 | if (ShowIndirectCallTargets) { |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 514 | InstrProfSymtab &Symtab = Reader->getSymtab(); |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 515 | uint32_t NS = Func.getNumValueSites(IPVK_IndirectCallTarget); |
| 516 | OS << " Indirect Target Results: \n"; |
Xinliang David Li | 872362c | 2016-05-23 16:36:11 +0000 | [diff] [blame] | 517 | TotalNumValueSites += NS; |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 518 | for (size_t I = 0; I < NS; ++I) { |
| 519 | uint32_t NV = Func.getNumValueDataForSite(IPVK_IndirectCallTarget, I); |
| 520 | std::unique_ptr<InstrProfValueData[]> VD = |
| 521 | Func.getValueForSite(IPVK_IndirectCallTarget, I); |
Xinliang David Li | 872362c | 2016-05-23 16:36:11 +0000 | [diff] [blame] | 522 | TotalNumValues += NV; |
Xinliang David Li | deda33c | 2016-09-20 21:04:22 +0000 | [diff] [blame] | 523 | if (NV) { |
Xinliang David Li | 872362c | 2016-05-23 16:36:11 +0000 | [diff] [blame] | 524 | TotalNumValueSitesWithValueProfile++; |
Xinliang David Li | deda33c | 2016-09-20 21:04:22 +0000 | [diff] [blame] | 525 | if (NV > ICHistogram.size()) |
| 526 | ICHistogram.resize(NV, 0); |
| 527 | ICHistogram[NV - 1]++; |
| 528 | } |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 529 | for (uint32_t V = 0; V < NV; V++) { |
| 530 | OS << "\t[ " << I << ", "; |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 531 | OS << Symtab.getFuncName(VD[V].Value) << ", " << VD[V].Count |
| 532 | << " ]\n"; |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 533 | } |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | } |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 537 | } |
Justin Bogner | db1225d | 2014-03-23 20:55:53 +0000 | [diff] [blame] | 538 | if (Reader->hasError()) |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 539 | exitWithError(Reader->getError(), Filename); |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 540 | |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 541 | if (ShowCounts && TextFormat) |
| 542 | return 0; |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 543 | std::unique_ptr<ProfileSummary> PS(Builder.getSummary()); |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 544 | if (ShowAllFunctions || !ShowFunction.empty()) |
| 545 | OS << "Functions shown: " << ShownFunctions << "\n"; |
Easwaran Raman | e5a17e3 | 2016-05-19 21:07:12 +0000 | [diff] [blame] | 546 | OS << "Total functions: " << PS->getNumFunctions() << "\n"; |
| 547 | OS << "Maximum function count: " << PS->getMaxFunctionCount() << "\n"; |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 548 | OS << "Maximum internal block count: " << PS->getMaxInternalCount() << "\n"; |
Xinliang David Li | 872362c | 2016-05-23 16:36:11 +0000 | [diff] [blame] | 549 | if (ShownFunctions && ShowIndirectCallTargets) { |
| 550 | OS << "Total Number of Indirect Call Sites : " << TotalNumValueSites |
| 551 | << "\n"; |
| 552 | OS << "Total Number of Sites With Values : " |
| 553 | << TotalNumValueSitesWithValueProfile << "\n"; |
| 554 | OS << "Total Number of Profiled Values : " << TotalNumValues << "\n"; |
Xinliang David Li | deda33c | 2016-09-20 21:04:22 +0000 | [diff] [blame] | 555 | |
| 556 | OS << "IC Value histogram : \n\tNumTargets, SiteCount\n"; |
| 557 | for (unsigned I = 0; I < ICHistogram.size(); I++) { |
| 558 | OS << "\t" << I + 1 << ", " << ICHistogram[I] << "\n"; |
| 559 | } |
Xinliang David Li | 872362c | 2016-05-23 16:36:11 +0000 | [diff] [blame] | 560 | } |
Easwaran Raman | 183ebbe | 2016-01-13 21:44:36 +0000 | [diff] [blame] | 561 | |
| 562 | if (ShowDetailedSummary) { |
| 563 | OS << "Detailed summary:\n"; |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 564 | OS << "Total number of blocks: " << PS->getNumCounts() << "\n"; |
Easwaran Raman | e5a17e3 | 2016-05-19 21:07:12 +0000 | [diff] [blame] | 565 | OS << "Total count: " << PS->getTotalCount() << "\n"; |
| 566 | for (auto Entry : PS->getDetailedSummary()) { |
Easwaran Raman | 4309570 | 2016-02-17 18:18:47 +0000 | [diff] [blame] | 567 | OS << Entry.NumCounts << " blocks with count >= " << Entry.MinCount |
Easwaran Raman | 183ebbe | 2016-01-13 21:44:36 +0000 | [diff] [blame] | 568 | << " account for " |
| 569 | << format("%0.6g", (float)Entry.Cutoff / ProfileSummary::Scale * 100) |
| 570 | << " percentage of the total counts.\n"; |
| 571 | } |
| 572 | } |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 573 | return 0; |
| 574 | } |
| 575 | |
Benjamin Kramer | 1afc1de | 2016-06-17 20:41:14 +0000 | [diff] [blame] | 576 | static int showSampleProfile(const std::string &Filename, bool ShowCounts, |
| 577 | bool ShowAllFunctions, |
| 578 | const std::string &ShowFunction, |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 579 | raw_fd_ostream &OS) { |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 580 | using namespace sampleprof; |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 581 | LLVMContext Context; |
| 582 | auto ReaderOrErr = SampleProfileReader::create(Filename, Context); |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 583 | if (std::error_code EC = ReaderOrErr.getError()) |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 584 | exitWithErrorCode(EC, Filename); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 585 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 586 | auto Reader = std::move(ReaderOrErr.get()); |
Diego Novillo | c6d032a | 2015-09-17 00:17:21 +0000 | [diff] [blame] | 587 | if (std::error_code EC = Reader->read()) |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 588 | exitWithErrorCode(EC, Filename); |
Diego Novillo | c6d032a | 2015-09-17 00:17:21 +0000 | [diff] [blame] | 589 | |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 590 | if (ShowAllFunctions || ShowFunction.empty()) |
| 591 | Reader->dump(OS); |
| 592 | else |
| 593 | Reader->dumpFunctionProfile(ShowFunction, OS); |
| 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 598 | static int show_main(int argc, const char *argv[]) { |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 599 | cl::opt<std::string> Filename(cl::Positional, cl::Required, |
| 600 | cl::desc("<profdata-file>")); |
| 601 | |
| 602 | cl::opt<bool> ShowCounts("counts", cl::init(false), |
| 603 | cl::desc("Show counter values for shown functions")); |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 604 | cl::opt<bool> TextFormat( |
| 605 | "text", cl::init(false), |
| 606 | cl::desc("Show instr profile data in text dump format")); |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 607 | cl::opt<bool> ShowIndirectCallTargets( |
| 608 | "ic-targets", cl::init(false), |
| 609 | cl::desc("Show indirect call site target values for shown functions")); |
Easwaran Raman | 183ebbe | 2016-01-13 21:44:36 +0000 | [diff] [blame] | 610 | cl::opt<bool> ShowDetailedSummary("detailed-summary", cl::init(false), |
| 611 | cl::desc("Show detailed profile summary")); |
| 612 | cl::list<uint32_t> DetailedSummaryCutoffs( |
| 613 | cl::CommaSeparated, "detailed-summary-cutoffs", |
| 614 | cl::desc( |
| 615 | "Cutoff percentages (times 10000) for generating detailed summary"), |
| 616 | cl::value_desc("800000,901000,999999")); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 617 | cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false), |
| 618 | cl::desc("Details for every function")); |
| 619 | cl::opt<std::string> ShowFunction("function", |
| 620 | cl::desc("Details for matching functions")); |
| 621 | |
| 622 | cl::opt<std::string> OutputFilename("output", cl::value_desc("output"), |
| 623 | cl::init("-"), cl::desc("Output file")); |
| 624 | cl::alias OutputFilenameA("o", cl::desc("Alias for --output"), |
| 625 | cl::aliasopt(OutputFilename)); |
| 626 | cl::opt<ProfileKinds> ProfileKind( |
| 627 | cl::desc("Profile kind:"), cl::init(instr), |
| 628 | cl::values(clEnumVal(instr, "Instrumentation profile (default)"), |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 629 | clEnumVal(sample, "Sample profile"))); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 630 | |
| 631 | cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n"); |
| 632 | |
| 633 | if (OutputFilename.empty()) |
| 634 | OutputFilename = "-"; |
| 635 | |
| 636 | std::error_code EC; |
| 637 | raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text); |
| 638 | if (EC) |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 639 | exitWithErrorCode(EC, OutputFilename); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 640 | |
| 641 | if (ShowAllFunctions && !ShowFunction.empty()) |
| 642 | errs() << "warning: -function argument ignored: showing all functions\n"; |
| 643 | |
Easwaran Raman | 183ebbe | 2016-01-13 21:44:36 +0000 | [diff] [blame] | 644 | std::vector<uint32_t> Cutoffs(DetailedSummaryCutoffs.begin(), |
| 645 | DetailedSummaryCutoffs.end()); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 646 | if (ProfileKind == instr) |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 647 | return showInstrProfile(Filename, ShowCounts, ShowIndirectCallTargets, |
Easwaran Raman | 183ebbe | 2016-01-13 21:44:36 +0000 | [diff] [blame] | 648 | ShowDetailedSummary, DetailedSummaryCutoffs, |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 649 | ShowAllFunctions, ShowFunction, TextFormat, OS); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 650 | else |
| 651 | return showSampleProfile(Filename, ShowCounts, ShowAllFunctions, |
| 652 | ShowFunction, OS); |
| 653 | } |
| 654 | |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 655 | int main(int argc, const char *argv[]) { |
| 656 | // Print a stack trace if we signal out. |
Richard Smith | 2ad6d48 | 2016-06-09 00:53:21 +0000 | [diff] [blame] | 657 | sys::PrintStackTraceOnErrorSignal(argv[0]); |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 658 | PrettyStackTraceProgram X(argc, argv); |
| 659 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 660 | |
| 661 | StringRef ProgName(sys::path::filename(argv[0])); |
| 662 | if (argc > 1) { |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 663 | int (*func)(int, const char *[]) = nullptr; |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 664 | |
| 665 | if (strcmp(argv[1], "merge") == 0) |
| 666 | func = merge_main; |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 667 | else if (strcmp(argv[1], "show") == 0) |
| 668 | func = show_main; |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 669 | |
| 670 | if (func) { |
| 671 | std::string Invocation(ProgName.str() + " " + argv[1]); |
| 672 | argv[1] = Invocation.c_str(); |
| 673 | return func(argc - 1, argv + 1); |
| 674 | } |
| 675 | |
Diego Novillo | d3babdb | 2015-12-14 20:37:15 +0000 | [diff] [blame] | 676 | if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0 || |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 677 | strcmp(argv[1], "--help") == 0) { |
| 678 | |
| 679 | errs() << "OVERVIEW: LLVM profile data tools\n\n" |
| 680 | << "USAGE: " << ProgName << " <command> [args...]\n" |
| 681 | << "USAGE: " << ProgName << " <command> -help\n\n" |
Justin Bogner | 253eb17 | 2016-08-03 23:10:51 +0000 | [diff] [blame] | 682 | << "See each individual command --help for more details.\n" |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 683 | << "Available commands: merge, show\n"; |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 684 | return 0; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | if (argc < 2) |
| 689 | errs() << ProgName << ": No command specified!\n"; |
| 690 | else |
| 691 | errs() << ProgName << ": Unknown command!\n"; |
| 692 | |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 693 | errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n"; |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 694 | return 1; |
| 695 | } |