Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 1 | //===- llvm-profdata.cpp - LLVM profile data tool -------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // llvm-profdata merges .profdata files. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Nathan Slingerland | c21a44d | 2015-11-18 17:10:24 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SmallSet.h" |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/SmallVector.h" |
Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringRef.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 16 | #include "llvm/IR/LLVMContext.h" |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 17 | #include "llvm/ProfileData/InstrProfReader.h" |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 18 | #include "llvm/ProfileData/InstrProfWriter.h" |
Easwaran Raman | d68aae2 | 2016-02-04 23:34:31 +0000 | [diff] [blame] | 19 | #include "llvm/ProfileData/ProfileCommon.h" |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 20 | #include "llvm/ProfileData/SampleProfReader.h" |
| 21 | #include "llvm/ProfileData/SampleProfWriter.h" |
Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Errc.h" |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 24 | #include "llvm/Support/FileSystem.h" |
Justin Bogner | 423380f | 2014-03-23 20:43:50 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Format.h" |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 26 | #include "llvm/Support/InitLLVM.h" |
Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 27 | #include "llvm/Support/MemoryBuffer.h" |
Benjamin Kramer | 16132e6 | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Path.h" |
Jonas Devlieghere | e46b756 | 2018-04-18 14:42:33 +0000 | [diff] [blame] | 29 | #include "llvm/Support/WithColor.h" |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ThreadPool.h" |
Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 32 | #include <algorithm> |
Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 33 | |
| 34 | using namespace llvm; |
| 35 | |
Wei Mi | a0c0857 | 2018-06-11 22:40:43 +0000 | [diff] [blame] | 36 | enum ProfileFormat { |
| 37 | PF_None = 0, |
| 38 | PF_Text, |
| 39 | PF_Compact_Binary, |
| 40 | PF_GCC, |
Wei Mi | d9be2c7 | 2018-06-12 05:53:49 +0000 | [diff] [blame] | 41 | PF_Binary |
Wei Mi | a0c0857 | 2018-06-11 22:40:43 +0000 | [diff] [blame] | 42 | }; |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 43 | |
Jonas Devlieghere | e46b756 | 2018-04-18 14:42:33 +0000 | [diff] [blame] | 44 | static void warn(Twine Message, std::string Whence = "", |
Vedant Kumar | 188efda | 2017-11-17 21:18:32 +0000 | [diff] [blame] | 45 | std::string Hint = "") { |
Jonas Devlieghere | e46b756 | 2018-04-18 14:42:33 +0000 | [diff] [blame] | 46 | WithColor::warning(); |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 47 | if (!Whence.empty()) |
| 48 | errs() << Whence << ": "; |
| 49 | errs() << Message << "\n"; |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 50 | if (!Hint.empty()) |
Jonas Devlieghere | e46b756 | 2018-04-18 14:42:33 +0000 | [diff] [blame] | 51 | WithColor::note() << Hint << "\n"; |
Vedant Kumar | 188efda | 2017-11-17 21:18:32 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static void exitWithError(Twine Message, std::string Whence = "", |
| 55 | std::string Hint = "") { |
Jonas Devlieghere | e46b756 | 2018-04-18 14:42:33 +0000 | [diff] [blame] | 56 | WithColor::error(); |
| 57 | if (!Whence.empty()) |
| 58 | errs() << Whence << ": "; |
| 59 | errs() << Message << "\n"; |
| 60 | if (!Hint.empty()) |
| 61 | WithColor::note() << Hint << "\n"; |
Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame] | 62 | ::exit(1); |
| 63 | } |
| 64 | |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 65 | static void exitWithError(Error E, StringRef Whence = "") { |
| 66 | if (E.isA<InstrProfError>()) { |
| 67 | handleAllErrors(std::move(E), [&](const InstrProfError &IPE) { |
| 68 | instrprof_error instrError = IPE.get(); |
| 69 | StringRef Hint = ""; |
| 70 | if (instrError == instrprof_error::unrecognized_format) { |
| 71 | // Hint for common error of forgetting -sample for sample profiles. |
| 72 | Hint = "Perhaps you forgot to use the -sample option?"; |
| 73 | } |
| 74 | exitWithError(IPE.message(), Whence, Hint); |
| 75 | }); |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 76 | } |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 77 | |
| 78 | exitWithError(toString(std::move(E)), Whence); |
| 79 | } |
| 80 | |
| 81 | static void exitWithErrorCode(std::error_code EC, StringRef Whence = "") { |
| 82 | exitWithError(EC.message(), Whence); |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Duncan P. N. Exon Smith | 02b6fa9 | 2015-06-16 00:43:04 +0000 | [diff] [blame] | 85 | namespace { |
Diego Novillo | d3babdb | 2015-12-14 20:37:15 +0000 | [diff] [blame] | 86 | enum ProfileKinds { instr, sample }; |
Duncan P. N. Exon Smith | 02b6fa9 | 2015-06-16 00:43:04 +0000 | [diff] [blame] | 87 | } |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 88 | |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 89 | static void handleMergeWriterError(Error E, StringRef WhenceFile = "", |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 90 | StringRef WhenceFunction = "", |
Diego Novillo | d3babdb | 2015-12-14 20:37:15 +0000 | [diff] [blame] | 91 | bool ShowHint = true) { |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 92 | if (!WhenceFile.empty()) |
| 93 | errs() << WhenceFile << ": "; |
| 94 | if (!WhenceFunction.empty()) |
| 95 | errs() << WhenceFunction << ": "; |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 96 | |
| 97 | auto IPE = instrprof_error::success; |
| 98 | E = handleErrors(std::move(E), |
| 99 | [&IPE](std::unique_ptr<InstrProfError> E) -> Error { |
| 100 | IPE = E->get(); |
| 101 | return Error(std::move(E)); |
| 102 | }); |
| 103 | errs() << toString(std::move(E)) << "\n"; |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 104 | |
| 105 | if (ShowHint) { |
| 106 | StringRef Hint = ""; |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 107 | if (IPE != instrprof_error::success) { |
| 108 | switch (IPE) { |
Nathan Slingerland | 11c938d1 | 2015-11-17 23:37:09 +0000 | [diff] [blame] | 109 | case instrprof_error::hash_mismatch: |
| 110 | case instrprof_error::count_mismatch: |
| 111 | case instrprof_error::value_site_count_mismatch: |
Diego Novillo | d3babdb | 2015-12-14 20:37:15 +0000 | [diff] [blame] | 112 | Hint = "Make sure that all profile data to be merged is generated " |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 113 | "from the same binary."; |
Nathan Slingerland | 11c938d1 | 2015-11-17 23:37:09 +0000 | [diff] [blame] | 114 | break; |
Nathan Slingerland | b2d95f0 | 2015-11-18 00:52:45 +0000 | [diff] [blame] | 115 | default: |
| 116 | break; |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | if (!Hint.empty()) |
| 121 | errs() << Hint << "\n"; |
| 122 | } |
| 123 | } |
| 124 | |
Richard Smith | 3164fcf | 2018-09-13 20:22:02 +0000 | [diff] [blame] | 125 | namespace { |
| 126 | /// A remapper from original symbol names to new symbol names based on a file |
| 127 | /// containing a list of mappings from old name to new name. |
| 128 | class SymbolRemapper { |
| 129 | std::unique_ptr<MemoryBuffer> File; |
| 130 | DenseMap<StringRef, StringRef> RemappingTable; |
| 131 | |
| 132 | public: |
| 133 | /// Build a SymbolRemapper from a file containing a list of old/new symbols. |
| 134 | static std::unique_ptr<SymbolRemapper> create(StringRef InputFile) { |
| 135 | auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFile); |
| 136 | if (!BufOrError) |
| 137 | exitWithErrorCode(BufOrError.getError(), InputFile); |
| 138 | |
| 139 | auto Remapper = llvm::make_unique<SymbolRemapper>(); |
| 140 | Remapper->File = std::move(BufOrError.get()); |
| 141 | |
| 142 | for (line_iterator LineIt(*Remapper->File, /*SkipBlanks=*/true, '#'); |
| 143 | !LineIt.is_at_eof(); ++LineIt) { |
| 144 | std::pair<StringRef, StringRef> Parts = LineIt->split(' '); |
| 145 | if (Parts.first.empty() || Parts.second.empty() || |
| 146 | Parts.second.count(' ')) { |
| 147 | exitWithError("unexpected line in remapping file", |
| 148 | (InputFile + ":" + Twine(LineIt.line_number())).str(), |
| 149 | "expected 'old_symbol new_symbol'"); |
| 150 | } |
| 151 | Remapper->RemappingTable.insert(Parts); |
| 152 | } |
| 153 | return Remapper; |
| 154 | } |
| 155 | |
| 156 | /// Attempt to map the given old symbol into a new symbol. |
| 157 | /// |
| 158 | /// \return The new symbol, or \p Name if no such symbol was found. |
| 159 | StringRef operator()(StringRef Name) { |
| 160 | StringRef New = RemappingTable.lookup(Name); |
| 161 | return New.empty() ? Name : New; |
| 162 | } |
| 163 | }; |
| 164 | } |
| 165 | |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 166 | struct WeightedFile { |
Xinliang David Li | 9a1bfcf | 2016-07-20 22:24:52 +0000 | [diff] [blame] | 167 | std::string Filename; |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 168 | uint64_t Weight; |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 169 | }; |
| 170 | typedef SmallVector<WeightedFile, 5> WeightedFileVector; |
| 171 | |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 172 | /// Keep track of merged data and reported errors. |
| 173 | struct WriterContext { |
| 174 | std::mutex Lock; |
| 175 | InstrProfWriter Writer; |
| 176 | Error Err; |
Vedant Kumar | faaa42a | 2017-11-17 02:58:23 +0000 | [diff] [blame] | 177 | std::string ErrWhence; |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 178 | std::mutex &ErrLock; |
| 179 | SmallSet<instrprof_error, 4> &WriterErrorCodes; |
| 180 | |
| 181 | WriterContext(bool IsSparse, std::mutex &ErrLock, |
| 182 | SmallSet<instrprof_error, 4> &WriterErrorCodes) |
| 183 | : Lock(), Writer(IsSparse), Err(Error::success()), ErrWhence(""), |
| 184 | ErrLock(ErrLock), WriterErrorCodes(WriterErrorCodes) {} |
| 185 | }; |
| 186 | |
Vedant Kumar | 188efda | 2017-11-17 21:18:32 +0000 | [diff] [blame] | 187 | /// Determine whether an error is fatal for profile merging. |
| 188 | static bool isFatalError(instrprof_error IPE) { |
| 189 | switch (IPE) { |
| 190 | default: |
| 191 | return true; |
| 192 | case instrprof_error::success: |
| 193 | case instrprof_error::eof: |
| 194 | case instrprof_error::unknown_function: |
| 195 | case instrprof_error::hash_mismatch: |
| 196 | case instrprof_error::count_mismatch: |
| 197 | case instrprof_error::counter_overflow: |
| 198 | case instrprof_error::value_site_count_mismatch: |
| 199 | return false; |
| 200 | } |
| 201 | } |
| 202 | |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 203 | /// Load an input into a writer context. |
Richard Smith | 3164fcf | 2018-09-13 20:22:02 +0000 | [diff] [blame] | 204 | static void loadInput(const WeightedFile &Input, SymbolRemapper *Remapper, |
| 205 | WriterContext *WC) { |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 206 | std::unique_lock<std::mutex> CtxGuard{WC->Lock}; |
| 207 | |
| 208 | // If there's a pending hard error, don't do more work. |
| 209 | if (WC->Err) |
| 210 | return; |
| 211 | |
Vedant Kumar | faaa42a | 2017-11-17 02:58:23 +0000 | [diff] [blame] | 212 | // Copy the filename, because llvm::ThreadPool copied the input "const |
| 213 | // WeightedFile &" by value, making a reference to the filename within it |
| 214 | // invalid outside of this packaged task. |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 215 | WC->ErrWhence = Input.Filename; |
| 216 | |
| 217 | auto ReaderOrErr = InstrProfReader::create(Input.Filename); |
Rong Xu | 2c684cf | 2016-10-19 22:51:17 +0000 | [diff] [blame] | 218 | if (Error E = ReaderOrErr.takeError()) { |
| 219 | // Skip the empty profiles by returning sliently. |
| 220 | instrprof_error IPE = InstrProfError::take(std::move(E)); |
| 221 | if (IPE != instrprof_error::empty_raw_profile) |
| 222 | WC->Err = make_error<InstrProfError>(IPE); |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 223 | return; |
Rong Xu | 2c684cf | 2016-10-19 22:51:17 +0000 | [diff] [blame] | 224 | } |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 225 | |
| 226 | auto Reader = std::move(ReaderOrErr.get()); |
| 227 | bool IsIRProfile = Reader->isIRLevelProfile(); |
| 228 | if (WC->Writer.setIsIRLevelProfile(IsIRProfile)) { |
| 229 | WC->Err = make_error<StringError>( |
| 230 | "Merge IR generated profile with Clang generated profile.", |
| 231 | std::error_code()); |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | for (auto &I : *Reader) { |
Richard Smith | 3164fcf | 2018-09-13 20:22:02 +0000 | [diff] [blame] | 236 | if (Remapper) |
| 237 | I.Name = (*Remapper)(I.Name); |
Rong Xu | fe90d86 | 2016-10-19 23:31:59 +0000 | [diff] [blame] | 238 | const StringRef FuncName = I.Name; |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 239 | bool Reported = false; |
| 240 | WC->Writer.addRecord(std::move(I), Input.Weight, [&](Error E) { |
| 241 | if (Reported) { |
| 242 | consumeError(std::move(E)); |
| 243 | return; |
| 244 | } |
| 245 | Reported = true; |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 246 | // Only show hint the first time an error occurs. |
| 247 | instrprof_error IPE = InstrProfError::take(std::move(E)); |
| 248 | std::unique_lock<std::mutex> ErrGuard{WC->ErrLock}; |
| 249 | bool firstTime = WC->WriterErrorCodes.insert(IPE).second; |
| 250 | handleMergeWriterError(make_error<InstrProfError>(IPE), Input.Filename, |
Rong Xu | fe90d86 | 2016-10-19 23:31:59 +0000 | [diff] [blame] | 251 | FuncName, firstTime); |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 252 | }); |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 253 | } |
Vedant Kumar | 188efda | 2017-11-17 21:18:32 +0000 | [diff] [blame] | 254 | if (Reader->hasError()) { |
| 255 | if (Error E = Reader->getError()) { |
| 256 | instrprof_error IPE = InstrProfError::take(std::move(E)); |
| 257 | if (isFatalError(IPE)) |
| 258 | WC->Err = make_error<InstrProfError>(IPE); |
| 259 | } |
| 260 | } |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /// Merge the \p Src writer context into \p Dst. |
| 264 | static void mergeWriterContexts(WriterContext *Dst, WriterContext *Src) { |
Vedant Kumar | faaa42a | 2017-11-17 02:58:23 +0000 | [diff] [blame] | 265 | // If we've already seen a hard error, continuing with the merge would |
| 266 | // clobber it. |
| 267 | if (Dst->Err || Src->Err) |
| 268 | return; |
| 269 | |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 270 | bool Reported = false; |
| 271 | Dst->Writer.mergeRecordsFromWriter(std::move(Src->Writer), [&](Error E) { |
| 272 | if (Reported) { |
| 273 | consumeError(std::move(E)); |
| 274 | return; |
| 275 | } |
| 276 | Reported = true; |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 277 | Dst->Err = std::move(E); |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 278 | }); |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 281 | static void mergeInstrProfile(const WeightedFileVector &Inputs, |
Richard Smith | 3164fcf | 2018-09-13 20:22:02 +0000 | [diff] [blame] | 282 | SymbolRemapper *Remapper, |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 283 | StringRef OutputFilename, |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 284 | ProfileFormat OutputFormat, bool OutputSparse, |
| 285 | unsigned NumThreads) { |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 286 | if (OutputFilename.compare("-") == 0) |
| 287 | exitWithError("Cannot write indexed profdata format to stdout."); |
Justin Bogner | ec49f98 | 2014-03-12 22:00:57 +0000 | [diff] [blame] | 288 | |
Wei Mi | d9be2c7 | 2018-06-12 05:53:49 +0000 | [diff] [blame] | 289 | if (OutputFormat != PF_Binary && OutputFormat != PF_Compact_Binary && |
Wei Mi | a0c0857 | 2018-06-11 22:40:43 +0000 | [diff] [blame] | 290 | OutputFormat != PF_Text) |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 291 | exitWithError("Unknown format is specified."); |
| 292 | |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 293 | std::error_code EC; |
| 294 | raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None); |
| 295 | if (EC) |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 296 | exitWithErrorCode(EC, OutputFilename); |
Justin Bogner | ec49f98 | 2014-03-12 22:00:57 +0000 | [diff] [blame] | 297 | |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 298 | std::mutex ErrorLock; |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 299 | SmallSet<instrprof_error, 4> WriterErrorCodes; |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 300 | |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 301 | // If NumThreads is not specified, auto-detect a good default. |
| 302 | if (NumThreads == 0) |
Rafael Espindola | 8c0ff95 | 2017-10-04 20:27:01 +0000 | [diff] [blame] | 303 | NumThreads = |
| 304 | std::min(hardware_concurrency(), unsigned((Inputs.size() + 1) / 2)); |
Rong Xu | 33c76c0 | 2016-02-10 17:18:30 +0000 | [diff] [blame] | 305 | |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 306 | // Initialize the writer contexts. |
| 307 | SmallVector<std::unique_ptr<WriterContext>, 4> Contexts; |
| 308 | for (unsigned I = 0; I < NumThreads; ++I) |
| 309 | Contexts.emplace_back(llvm::make_unique<WriterContext>( |
| 310 | OutputSparse, ErrorLock, WriterErrorCodes)); |
| 311 | |
| 312 | if (NumThreads == 1) { |
| 313 | for (const auto &Input : Inputs) |
Richard Smith | 3164fcf | 2018-09-13 20:22:02 +0000 | [diff] [blame] | 314 | loadInput(Input, Remapper, Contexts[0].get()); |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 315 | } else { |
| 316 | ThreadPool Pool(NumThreads); |
| 317 | |
| 318 | // Load the inputs in parallel (N/NumThreads serial steps). |
| 319 | unsigned Ctx = 0; |
| 320 | for (const auto &Input : Inputs) { |
Richard Smith | 3164fcf | 2018-09-13 20:22:02 +0000 | [diff] [blame] | 321 | Pool.async(loadInput, Input, Remapper, Contexts[Ctx].get()); |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 322 | Ctx = (Ctx + 1) % NumThreads; |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 323 | } |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 324 | Pool.wait(); |
| 325 | |
| 326 | // Merge the writer contexts together (~ lg(NumThreads) serial steps). |
| 327 | unsigned Mid = Contexts.size() / 2; |
| 328 | unsigned End = Contexts.size(); |
| 329 | assert(Mid > 0 && "Expected more than one context"); |
| 330 | do { |
| 331 | for (unsigned I = 0; I < Mid; ++I) |
| 332 | Pool.async(mergeWriterContexts, Contexts[I].get(), |
| 333 | Contexts[I + Mid].get()); |
| 334 | Pool.wait(); |
| 335 | if (End & 1) { |
| 336 | Pool.async(mergeWriterContexts, Contexts[0].get(), |
| 337 | Contexts[End - 1].get()); |
| 338 | Pool.wait(); |
| 339 | } |
| 340 | End = Mid; |
| 341 | Mid /= 2; |
| 342 | } while (Mid > 0); |
Justin Bogner | bfee8d4 | 2014-03-12 20:14:17 +0000 | [diff] [blame] | 343 | } |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 344 | |
| 345 | // Handle deferred hard errors encountered during merging. |
Vedant Kumar | 188efda | 2017-11-17 21:18:32 +0000 | [diff] [blame] | 346 | for (std::unique_ptr<WriterContext> &WC : Contexts) { |
| 347 | if (!WC->Err) |
| 348 | continue; |
| 349 | if (!WC->Err.isA<InstrProfError>()) |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 350 | exitWithError(std::move(WC->Err), WC->ErrWhence); |
| 351 | |
Vedant Kumar | 188efda | 2017-11-17 21:18:32 +0000 | [diff] [blame] | 352 | instrprof_error IPE = InstrProfError::take(std::move(WC->Err)); |
| 353 | if (isFatalError(IPE)) |
| 354 | exitWithError(make_error<InstrProfError>(IPE), WC->ErrWhence); |
| 355 | else |
Jonas Devlieghere | e46b756 | 2018-04-18 14:42:33 +0000 | [diff] [blame] | 356 | warn(toString(make_error<InstrProfError>(IPE)), |
Vedant Kumar | 188efda | 2017-11-17 21:18:32 +0000 | [diff] [blame] | 357 | WC->ErrWhence); |
| 358 | } |
| 359 | |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 360 | InstrProfWriter &Writer = Contexts[0]->Writer; |
Vedant Kumar | b5794ca | 2017-06-20 01:38:56 +0000 | [diff] [blame] | 361 | if (OutputFormat == PF_Text) { |
| 362 | if (Error E = Writer.writeText(Output)) |
| 363 | exitWithError(std::move(E)); |
| 364 | } else { |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 365 | Writer.write(Output); |
Vedant Kumar | b5794ca | 2017-06-20 01:38:56 +0000 | [diff] [blame] | 366 | } |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Richard Smith | 3164fcf | 2018-09-13 20:22:02 +0000 | [diff] [blame] | 369 | /// Make a copy of the given function samples with all symbol names remapped |
| 370 | /// by the provided symbol remapper. |
| 371 | static sampleprof::FunctionSamples |
| 372 | remapSamples(const sampleprof::FunctionSamples &Samples, |
| 373 | SymbolRemapper &Remapper, sampleprof_error &Error) { |
| 374 | sampleprof::FunctionSamples Result; |
| 375 | Result.setName(Remapper(Samples.getName())); |
| 376 | Result.addTotalSamples(Samples.getTotalSamples()); |
| 377 | Result.addHeadSamples(Samples.getHeadSamples()); |
| 378 | for (const auto &BodySample : Samples.getBodySamples()) { |
| 379 | Result.addBodySamples(BodySample.first.LineOffset, |
| 380 | BodySample.first.Discriminator, |
| 381 | BodySample.second.getSamples()); |
| 382 | for (const auto &Target : BodySample.second.getCallTargets()) { |
| 383 | Result.addCalledTargetSamples(BodySample.first.LineOffset, |
| 384 | BodySample.first.Discriminator, |
| 385 | Remapper(Target.first()), Target.second); |
| 386 | } |
| 387 | } |
| 388 | for (const auto &CallsiteSamples : Samples.getCallsiteSamples()) { |
| 389 | sampleprof::FunctionSamplesMap &Target = |
| 390 | Result.functionSamplesAt(CallsiteSamples.first); |
| 391 | for (const auto &Callsite : CallsiteSamples.second) { |
| 392 | sampleprof::FunctionSamples Remapped = |
| 393 | remapSamples(Callsite.second, Remapper, Error); |
| 394 | MergeResult(Error, Target[Remapped.getName()].merge(Remapped)); |
| 395 | } |
| 396 | } |
| 397 | return Result; |
| 398 | } |
| 399 | |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 400 | static sampleprof::SampleProfileFormat FormatMap[] = { |
Wei Mi | a0c0857 | 2018-06-11 22:40:43 +0000 | [diff] [blame] | 401 | sampleprof::SPF_None, sampleprof::SPF_Text, sampleprof::SPF_Compact_Binary, |
Wei Mi | d9be2c7 | 2018-06-12 05:53:49 +0000 | [diff] [blame] | 402 | sampleprof::SPF_GCC, sampleprof::SPF_Binary}; |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 403 | |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 404 | static void mergeSampleProfile(const WeightedFileVector &Inputs, |
Richard Smith | 3164fcf | 2018-09-13 20:22:02 +0000 | [diff] [blame] | 405 | SymbolRemapper *Remapper, |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 406 | StringRef OutputFilename, |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 407 | ProfileFormat OutputFormat) { |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 408 | using namespace sampleprof; |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 409 | auto WriterOrErr = |
| 410 | SampleProfileWriter::create(OutputFilename, FormatMap[OutputFormat]); |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 411 | if (std::error_code EC = WriterOrErr.getError()) |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 412 | exitWithErrorCode(EC, OutputFilename); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 413 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 414 | auto Writer = std::move(WriterOrErr.get()); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 415 | StringMap<FunctionSamples> ProfileMap; |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 416 | SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers; |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 417 | LLVMContext Context; |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 418 | for (const auto &Input : Inputs) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 419 | auto ReaderOrErr = SampleProfileReader::create(Input.Filename, Context); |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 420 | if (std::error_code EC = ReaderOrErr.getError()) |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 421 | exitWithErrorCode(EC, Input.Filename); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 422 | |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 423 | // We need to keep the readers around until after all the files are |
| 424 | // read so that we do not lose the function names stored in each |
| 425 | // reader's memory. The function names are needed to write out the |
| 426 | // merged profile map. |
| 427 | Readers.push_back(std::move(ReaderOrErr.get())); |
| 428 | const auto Reader = Readers.back().get(); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 429 | if (std::error_code EC = Reader->read()) |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 430 | exitWithErrorCode(EC, Input.Filename); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 431 | |
| 432 | StringMap<FunctionSamples> &Profiles = Reader->getProfiles(); |
| 433 | for (StringMap<FunctionSamples>::iterator I = Profiles.begin(), |
| 434 | E = Profiles.end(); |
| 435 | I != E; ++I) { |
Richard Smith | 3164fcf | 2018-09-13 20:22:02 +0000 | [diff] [blame] | 436 | sampleprof_error Result = sampleprof_error::success; |
| 437 | FunctionSamples Remapped = |
| 438 | Remapper ? remapSamples(I->second, *Remapper, Result) |
| 439 | : FunctionSamples(); |
| 440 | FunctionSamples &Samples = Remapper ? Remapped : I->second; |
| 441 | StringRef FName = Samples.getName(); |
| 442 | MergeResult(Result, ProfileMap[FName].merge(Samples, Input.Weight)); |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 443 | if (Result != sampleprof_error::success) { |
| 444 | std::error_code EC = make_error_code(Result); |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 445 | handleMergeWriterError(errorCodeToError(EC), Input.Filename, FName); |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 446 | } |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | Writer->write(ProfileMap); |
| 450 | } |
| 451 | |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 452 | static WeightedFile parseWeightedFile(const StringRef &WeightedFilename) { |
Vedant Kumar | 8d0e861 | 2016-06-06 23:43:56 +0000 | [diff] [blame] | 453 | StringRef WeightStr, FileName; |
| 454 | std::tie(WeightStr, FileName) = WeightedFilename.split(','); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 455 | |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 456 | uint64_t Weight; |
| 457 | if (WeightStr.getAsInteger(10, Weight) || Weight < 1) |
| 458 | exitWithError("Input weight must be a positive integer."); |
| 459 | |
Benjamin Kramer | 929e7db | 2016-07-21 14:29:11 +0000 | [diff] [blame] | 460 | return {FileName, Weight}; |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Vedant Kumar | cef4360 | 2016-06-07 22:47:31 +0000 | [diff] [blame] | 463 | static std::unique_ptr<MemoryBuffer> |
| 464 | getInputFilenamesFileBuf(const StringRef &InputFilenamesFile) { |
| 465 | if (InputFilenamesFile == "") |
| 466 | return {}; |
| 467 | |
| 468 | auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFilenamesFile); |
| 469 | if (!BufOrError) |
| 470 | exitWithErrorCode(BufOrError.getError(), InputFilenamesFile); |
| 471 | |
| 472 | return std::move(*BufOrError); |
| 473 | } |
| 474 | |
Xinliang David Li | 9a1bfcf | 2016-07-20 22:24:52 +0000 | [diff] [blame] | 475 | static void addWeightedInput(WeightedFileVector &WNI, const WeightedFile &WF) { |
| 476 | StringRef Filename = WF.Filename; |
| 477 | uint64_t Weight = WF.Weight; |
Benjamin Kramer | a81f472 | 2016-07-22 12:39:55 +0000 | [diff] [blame] | 478 | |
| 479 | // If it's STDIN just pass it on. |
| 480 | if (Filename == "-") { |
| 481 | WNI.push_back({Filename, Weight}); |
| 482 | return; |
| 483 | } |
| 484 | |
Xinliang David Li | 9a1bfcf | 2016-07-20 22:24:52 +0000 | [diff] [blame] | 485 | llvm::sys::fs::file_status Status; |
| 486 | llvm::sys::fs::status(Filename, Status); |
| 487 | if (!llvm::sys::fs::exists(Status)) |
| 488 | exitWithErrorCode(make_error_code(errc::no_such_file_or_directory), |
| 489 | Filename); |
| 490 | // If it's a source file, collect it. |
| 491 | if (llvm::sys::fs::is_regular_file(Status)) { |
Benjamin Kramer | 929e7db | 2016-07-21 14:29:11 +0000 | [diff] [blame] | 492 | WNI.push_back({Filename, Weight}); |
Xinliang David Li | 9a1bfcf | 2016-07-20 22:24:52 +0000 | [diff] [blame] | 493 | return; |
| 494 | } |
| 495 | |
| 496 | if (llvm::sys::fs::is_directory(Status)) { |
| 497 | std::error_code EC; |
| 498 | for (llvm::sys::fs::recursive_directory_iterator F(Filename, EC), E; |
| 499 | F != E && !EC; F.increment(EC)) { |
| 500 | if (llvm::sys::fs::is_regular_file(F->path())) { |
| 501 | addWeightedInput(WNI, {F->path(), Weight}); |
| 502 | } |
| 503 | } |
| 504 | if (EC) |
| 505 | exitWithErrorCode(EC, Filename); |
| 506 | } |
| 507 | } |
| 508 | |
Vedant Kumar | cef4360 | 2016-06-07 22:47:31 +0000 | [diff] [blame] | 509 | static void parseInputFilenamesFile(MemoryBuffer *Buffer, |
| 510 | WeightedFileVector &WFV) { |
| 511 | if (!Buffer) |
| 512 | return; |
| 513 | |
| 514 | SmallVector<StringRef, 8> Entries; |
| 515 | StringRef Data = Buffer->getBuffer(); |
| 516 | Data.split(Entries, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false); |
| 517 | for (const StringRef &FileWeightEntry : Entries) { |
| 518 | StringRef SanitizedEntry = FileWeightEntry.trim(" \t\v\f\r"); |
| 519 | // Skip comments. |
| 520 | if (SanitizedEntry.startswith("#")) |
| 521 | continue; |
| 522 | // If there's no comma, it's an unweighted profile. |
| 523 | else if (SanitizedEntry.find(',') == StringRef::npos) |
Xinliang David Li | 9a1bfcf | 2016-07-20 22:24:52 +0000 | [diff] [blame] | 524 | addWeightedInput(WFV, {SanitizedEntry, 1}); |
Vedant Kumar | cef4360 | 2016-06-07 22:47:31 +0000 | [diff] [blame] | 525 | else |
Xinliang David Li | 9a1bfcf | 2016-07-20 22:24:52 +0000 | [diff] [blame] | 526 | addWeightedInput(WFV, parseWeightedFile(SanitizedEntry)); |
Vedant Kumar | cef4360 | 2016-06-07 22:47:31 +0000 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 530 | static int merge_main(int argc, const char *argv[]) { |
| 531 | cl::list<std::string> InputFilenames(cl::Positional, |
| 532 | cl::desc("<filename...>")); |
| 533 | cl::list<std::string> WeightedInputFilenames("weighted-input", |
| 534 | cl::desc("<weight>,<filename>")); |
Vedant Kumar | cef4360 | 2016-06-07 22:47:31 +0000 | [diff] [blame] | 535 | cl::opt<std::string> InputFilenamesFile( |
| 536 | "input-files", cl::init(""), |
| 537 | cl::desc("Path to file containing newline-separated " |
| 538 | "[<weight>,]<filename> entries")); |
| 539 | cl::alias InputFilenamesFileA("f", cl::desc("Alias for --input-files"), |
| 540 | cl::aliasopt(InputFilenamesFile)); |
| 541 | cl::opt<bool> DumpInputFileList( |
| 542 | "dump-input-file-list", cl::init(false), cl::Hidden, |
| 543 | cl::desc("Dump the list of input files and their weights, then exit")); |
Richard Smith | 3164fcf | 2018-09-13 20:22:02 +0000 | [diff] [blame] | 544 | cl::opt<std::string> RemappingFile("remapping-file", cl::value_desc("file"), |
| 545 | cl::desc("Symbol remapping file")); |
| 546 | cl::alias RemappingFileA("r", cl::desc("Alias for --remapping-file"), |
| 547 | cl::aliasopt(RemappingFile)); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 548 | cl::opt<std::string> OutputFilename("output", cl::value_desc("output"), |
| 549 | cl::init("-"), cl::Required, |
| 550 | cl::desc("Output file")); |
| 551 | cl::alias OutputFilenameA("o", cl::desc("Alias for --output"), |
| 552 | cl::aliasopt(OutputFilename)); |
| 553 | cl::opt<ProfileKinds> ProfileKind( |
| 554 | cl::desc("Profile kind:"), cl::init(instr), |
| 555 | cl::values(clEnumVal(instr, "Instrumentation profile (default)"), |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 556 | clEnumVal(sample, "Sample profile"))); |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 557 | cl::opt<ProfileFormat> OutputFormat( |
Wei Mi | d9be2c7 | 2018-06-12 05:53:49 +0000 | [diff] [blame] | 558 | cl::desc("Format of output profile"), cl::init(PF_Binary), |
| 559 | cl::values(clEnumValN(PF_Binary, "binary", "Binary encoding (default)"), |
| 560 | clEnumValN(PF_Compact_Binary, "compbinary", |
| 561 | "Compact binary encoding"), |
| 562 | clEnumValN(PF_Text, "text", "Text encoding"), |
| 563 | clEnumValN(PF_GCC, "gcc", |
| 564 | "GCC encoding (only meaningful for -sample)"))); |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 565 | cl::opt<bool> OutputSparse("sparse", cl::init(false), |
| 566 | cl::desc("Generate a sparse profile (only meaningful for -instr)")); |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 567 | cl::opt<unsigned> NumThreads( |
| 568 | "num-threads", cl::init(0), |
| 569 | cl::desc("Number of merge threads to use (default: autodetect)")); |
| 570 | cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"), |
| 571 | cl::aliasopt(NumThreads)); |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 572 | |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 573 | cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n"); |
| 574 | |
Vedant Kumar | cef4360 | 2016-06-07 22:47:31 +0000 | [diff] [blame] | 575 | WeightedFileVector WeightedInputs; |
| 576 | for (StringRef Filename : InputFilenames) |
Xinliang David Li | 9a1bfcf | 2016-07-20 22:24:52 +0000 | [diff] [blame] | 577 | addWeightedInput(WeightedInputs, {Filename, 1}); |
Vedant Kumar | cef4360 | 2016-06-07 22:47:31 +0000 | [diff] [blame] | 578 | for (StringRef WeightedFilename : WeightedInputFilenames) |
Xinliang David Li | 9a1bfcf | 2016-07-20 22:24:52 +0000 | [diff] [blame] | 579 | addWeightedInput(WeightedInputs, parseWeightedFile(WeightedFilename)); |
Vedant Kumar | cef4360 | 2016-06-07 22:47:31 +0000 | [diff] [blame] | 580 | |
| 581 | // Make sure that the file buffer stays alive for the duration of the |
| 582 | // weighted input vector's lifetime. |
| 583 | auto Buffer = getInputFilenamesFileBuf(InputFilenamesFile); |
| 584 | parseInputFilenamesFile(Buffer.get(), WeightedInputs); |
| 585 | |
| 586 | if (WeightedInputs.empty()) |
Chandler Carruth | 0c30f89 | 2016-06-04 03:08:01 +0000 | [diff] [blame] | 587 | exitWithError("No input files specified. See " + |
| 588 | sys::path::filename(argv[0]) + " -help"); |
| 589 | |
Vedant Kumar | cef4360 | 2016-06-07 22:47:31 +0000 | [diff] [blame] | 590 | if (DumpInputFileList) { |
| 591 | for (auto &WF : WeightedInputs) |
| 592 | outs() << WF.Weight << "," << WF.Filename << "\n"; |
| 593 | return 0; |
| 594 | } |
Vedant Kumar | f771a05 | 2016-06-04 00:36:28 +0000 | [diff] [blame] | 595 | |
Richard Smith | 3164fcf | 2018-09-13 20:22:02 +0000 | [diff] [blame] | 596 | std::unique_ptr<SymbolRemapper> Remapper; |
| 597 | if (!RemappingFile.empty()) |
| 598 | Remapper = SymbolRemapper::create(RemappingFile); |
| 599 | |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 600 | if (ProfileKind == instr) |
Richard Smith | 3164fcf | 2018-09-13 20:22:02 +0000 | [diff] [blame] | 601 | mergeInstrProfile(WeightedInputs, Remapper.get(), OutputFilename, |
| 602 | OutputFormat, OutputSparse, NumThreads); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 603 | else |
Richard Smith | 3164fcf | 2018-09-13 20:22:02 +0000 | [diff] [blame] | 604 | mergeSampleProfile(WeightedInputs, Remapper.get(), OutputFilename, |
| 605 | OutputFormat); |
Justin Bogner | bfee8d4 | 2014-03-12 20:14:17 +0000 | [diff] [blame] | 606 | |
Justin Bogner | ec49f98 | 2014-03-12 22:00:57 +0000 | [diff] [blame] | 607 | return 0; |
Justin Bogner | bfee8d4 | 2014-03-12 20:14:17 +0000 | [diff] [blame] | 608 | } |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 609 | |
Rong Xu | 0cf1f56 | 2017-03-09 19:03:57 +0000 | [diff] [blame] | 610 | typedef struct ValueSitesStats { |
| 611 | ValueSitesStats() |
| 612 | : TotalNumValueSites(0), TotalNumValueSitesWithValueProfile(0), |
| 613 | TotalNumValues(0) {} |
| 614 | uint64_t TotalNumValueSites; |
| 615 | uint64_t TotalNumValueSitesWithValueProfile; |
| 616 | uint64_t TotalNumValues; |
| 617 | std::vector<unsigned> ValueSitesHistogram; |
| 618 | } ValueSitesStats; |
| 619 | |
| 620 | static void traverseAllValueSites(const InstrProfRecord &Func, uint32_t VK, |
| 621 | ValueSitesStats &Stats, raw_fd_ostream &OS, |
Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 622 | InstrProfSymtab *Symtab) { |
Rong Xu | 0cf1f56 | 2017-03-09 19:03:57 +0000 | [diff] [blame] | 623 | uint32_t NS = Func.getNumValueSites(VK); |
| 624 | Stats.TotalNumValueSites += NS; |
| 625 | for (size_t I = 0; I < NS; ++I) { |
| 626 | uint32_t NV = Func.getNumValueDataForSite(VK, I); |
| 627 | std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, I); |
| 628 | Stats.TotalNumValues += NV; |
| 629 | if (NV) { |
| 630 | Stats.TotalNumValueSitesWithValueProfile++; |
| 631 | if (NV > Stats.ValueSitesHistogram.size()) |
| 632 | Stats.ValueSitesHistogram.resize(NV, 0); |
| 633 | Stats.ValueSitesHistogram[NV - 1]++; |
| 634 | } |
Rong Xu | 52aa224 | 2019-01-08 22:41:48 +0000 | [diff] [blame] | 635 | |
| 636 | uint64_t SiteSum = 0; |
| 637 | for (uint32_t V = 0; V < NV; V++) |
| 638 | SiteSum += VD[V].Count; |
| 639 | if (SiteSum == 0) |
| 640 | SiteSum = 1; |
| 641 | |
Rong Xu | 0cf1f56 | 2017-03-09 19:03:57 +0000 | [diff] [blame] | 642 | for (uint32_t V = 0; V < NV; V++) { |
Rong Xu | 52aa224 | 2019-01-08 22:41:48 +0000 | [diff] [blame] | 643 | OS << "\t[ " << format("%2u", I) << ", "; |
Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 644 | if (Symtab == nullptr) |
Rong Xu | 52aa224 | 2019-01-08 22:41:48 +0000 | [diff] [blame] | 645 | OS << format("%4u", VD[V].Value); |
Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 646 | else |
| 647 | OS << Symtab->getFuncName(VD[V].Value); |
Rong Xu | 52aa224 | 2019-01-08 22:41:48 +0000 | [diff] [blame] | 648 | OS << ", " << format("%10" PRId64, VD[V].Count) << " ] (" |
| 649 | << format("%.2f%%", (VD[V].Count * 100.0 / SiteSum)) << ")\n"; |
Rong Xu | 0cf1f56 | 2017-03-09 19:03:57 +0000 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | static void showValueSitesStats(raw_fd_ostream &OS, uint32_t VK, |
| 655 | ValueSitesStats &Stats) { |
| 656 | OS << " Total number of sites: " << Stats.TotalNumValueSites << "\n"; |
| 657 | OS << " Total number of sites with values: " |
| 658 | << Stats.TotalNumValueSitesWithValueProfile << "\n"; |
| 659 | OS << " Total number of profiled values: " << Stats.TotalNumValues << "\n"; |
| 660 | |
| 661 | OS << " Value sites histogram:\n\tNumTargets, SiteCount\n"; |
| 662 | for (unsigned I = 0; I < Stats.ValueSitesHistogram.size(); I++) { |
| 663 | if (Stats.ValueSitesHistogram[I] > 0) |
| 664 | OS << "\t" << I + 1 << ", " << Stats.ValueSitesHistogram[I] << "\n"; |
| 665 | } |
| 666 | } |
| 667 | |
Benjamin Kramer | 1afc1de | 2016-06-17 20:41:14 +0000 | [diff] [blame] | 668 | static int showInstrProfile(const std::string &Filename, bool ShowCounts, |
Xinliang David Li | 801b531 | 2017-07-11 20:30:43 +0000 | [diff] [blame] | 669 | uint32_t TopN, bool ShowIndirectCallTargets, |
| 670 | bool ShowMemOPSizes, bool ShowDetailedSummary, |
Easwaran Raman | 183ebbe | 2016-01-13 21:44:36 +0000 | [diff] [blame] | 671 | std::vector<uint32_t> DetailedSummaryCutoffs, |
Rong Xu | 52aa224 | 2019-01-08 22:41:48 +0000 | [diff] [blame] | 672 | bool ShowAllFunctions, uint64_t ValueCutoff, |
| 673 | bool OnlyListBelow, const std::string &ShowFunction, |
| 674 | bool TextFormat, raw_fd_ostream &OS) { |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 675 | auto ReaderOrErr = InstrProfReader::create(Filename); |
Benjamin Kramer | 1afc1de | 2016-06-17 20:41:14 +0000 | [diff] [blame] | 676 | std::vector<uint32_t> Cutoffs = std::move(DetailedSummaryCutoffs); |
| 677 | if (ShowDetailedSummary && Cutoffs.empty()) { |
Easwaran Raman | 183ebbe | 2016-01-13 21:44:36 +0000 | [diff] [blame] | 678 | Cutoffs = {800000, 900000, 950000, 990000, 999000, 999900, 999990}; |
| 679 | } |
Benjamin Kramer | 1afc1de | 2016-06-17 20:41:14 +0000 | [diff] [blame] | 680 | InstrProfSummaryBuilder Builder(std::move(Cutoffs)); |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 681 | if (Error E = ReaderOrErr.takeError()) |
| 682 | exitWithError(std::move(E), Filename); |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 683 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 684 | auto Reader = std::move(ReaderOrErr.get()); |
Rong Xu | 33c76c0 | 2016-02-10 17:18:30 +0000 | [diff] [blame] | 685 | bool IsIRInstr = Reader->isIRLevelProfile(); |
Easwaran Raman | 183ebbe | 2016-01-13 21:44:36 +0000 | [diff] [blame] | 686 | size_t ShownFunctions = 0; |
Rong Xu | 52aa224 | 2019-01-08 22:41:48 +0000 | [diff] [blame] | 687 | size_t BelowCutoffFunctions = 0; |
Rong Xu | 0cf1f56 | 2017-03-09 19:03:57 +0000 | [diff] [blame] | 688 | int NumVPKind = IPVK_Last - IPVK_First + 1; |
| 689 | std::vector<ValueSitesStats> VPStats(NumVPKind); |
Xinliang David Li | 801b531 | 2017-07-11 20:30:43 +0000 | [diff] [blame] | 690 | |
| 691 | auto MinCmp = [](const std::pair<std::string, uint64_t> &v1, |
| 692 | const std::pair<std::string, uint64_t> &v2) { |
| 693 | return v1.second > v2.second; |
| 694 | }; |
| 695 | |
| 696 | std::priority_queue<std::pair<std::string, uint64_t>, |
| 697 | std::vector<std::pair<std::string, uint64_t>>, |
| 698 | decltype(MinCmp)> |
| 699 | HottestFuncs(MinCmp); |
| 700 | |
Rong Xu | 52aa224 | 2019-01-08 22:41:48 +0000 | [diff] [blame] | 701 | if (!TextFormat && OnlyListBelow) { |
| 702 | OS << "The list of functions with the maximum counter less than " |
| 703 | << ValueCutoff << ":\n"; |
| 704 | } |
| 705 | |
Richard Smith | c6ba9ca | 2018-08-24 01:34:45 +0000 | [diff] [blame] | 706 | // Add marker so that IR-level instrumentation round-trips properly. |
| 707 | if (TextFormat && IsIRInstr) |
| 708 | OS << ":ir\n"; |
| 709 | |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 710 | for (const auto &Func : *Reader) { |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 711 | bool Show = |
| 712 | ShowAllFunctions || (!ShowFunction.empty() && |
| 713 | Func.Name.find(ShowFunction) != Func.Name.npos); |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 714 | |
Richard Smith | c6ba9ca | 2018-08-24 01:34:45 +0000 | [diff] [blame] | 715 | bool doTextFormatDump = (Show && TextFormat); |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 716 | |
| 717 | if (doTextFormatDump) { |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 718 | InstrProfSymtab &Symtab = Reader->getSymtab(); |
David Blaikie | cf9d52c | 2017-07-06 19:00:12 +0000 | [diff] [blame] | 719 | InstrProfWriter::writeRecordInText(Func.Name, Func.Hash, Func, Symtab, |
| 720 | OS); |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 721 | continue; |
| 722 | } |
| 723 | |
Justin Bogner | b59d7c7 | 2014-04-25 02:45:33 +0000 | [diff] [blame] | 724 | assert(Func.Counts.size() > 0 && "function missing entry counter"); |
Easwaran Raman | e5a17e3 | 2016-05-19 21:07:12 +0000 | [diff] [blame] | 725 | Builder.addRecord(Func); |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 726 | |
Rong Xu | 52aa224 | 2019-01-08 22:41:48 +0000 | [diff] [blame] | 727 | uint64_t FuncMax = 0; |
| 728 | uint64_t FuncSum = 0; |
| 729 | for (size_t I = 0, E = Func.Counts.size(); I < E; ++I) { |
| 730 | FuncMax = std::max(FuncMax, Func.Counts[I]); |
| 731 | FuncSum += Func.Counts[I]; |
| 732 | } |
Rong Xu | 7162e16 | 2019-01-08 22:37:12 +0000 | [diff] [blame] | 733 | |
Rong Xu | 52aa224 | 2019-01-08 22:41:48 +0000 | [diff] [blame] | 734 | if (FuncMax < ValueCutoff) { |
| 735 | ++BelowCutoffFunctions; |
| 736 | if (OnlyListBelow) { |
| 737 | OS << " " << Func.Name << ": (Max = " << FuncMax |
| 738 | << " Sum = " << FuncSum << ")\n"; |
| 739 | } |
| 740 | continue; |
| 741 | } else if (OnlyListBelow) |
| 742 | continue; |
| 743 | |
| 744 | if (TopN) { |
Xinliang David Li | 801b531 | 2017-07-11 20:30:43 +0000 | [diff] [blame] | 745 | if (HottestFuncs.size() == TopN) { |
| 746 | if (HottestFuncs.top().second < FuncMax) { |
| 747 | HottestFuncs.pop(); |
| 748 | HottestFuncs.emplace(std::make_pair(std::string(Func.Name), FuncMax)); |
| 749 | } |
| 750 | } else |
| 751 | HottestFuncs.emplace(std::make_pair(std::string(Func.Name), FuncMax)); |
| 752 | } |
| 753 | |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 754 | if (Show) { |
| 755 | if (!ShownFunctions) |
| 756 | OS << "Counters:\n"; |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 757 | |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 758 | ++ShownFunctions; |
| 759 | |
| 760 | OS << " " << Func.Name << ":\n" |
Justin Bogner | 423380f | 2014-03-23 20:43:50 +0000 | [diff] [blame] | 761 | << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n" |
Rong Xu | 33c76c0 | 2016-02-10 17:18:30 +0000 | [diff] [blame] | 762 | << " Counters: " << Func.Counts.size() << "\n"; |
| 763 | if (!IsIRInstr) |
| 764 | OS << " Function count: " << Func.Counts[0] << "\n"; |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 765 | |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 766 | if (ShowIndirectCallTargets) |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 767 | OS << " Indirect Call Site Count: " |
| 768 | << Func.getNumValueSites(IPVK_IndirectCallTarget) << "\n"; |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 769 | |
Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 770 | uint32_t NumMemOPCalls = Func.getNumValueSites(IPVK_MemOPSize); |
| 771 | if (ShowMemOPSizes && NumMemOPCalls > 0) |
| 772 | OS << " Number of Memory Intrinsics Calls: " << NumMemOPCalls |
| 773 | << "\n"; |
| 774 | |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 775 | if (ShowCounts) { |
| 776 | OS << " Block counts: ["; |
Rong Xu | 33c76c0 | 2016-02-10 17:18:30 +0000 | [diff] [blame] | 777 | size_t Start = (IsIRInstr ? 0 : 1); |
| 778 | for (size_t I = Start, E = Func.Counts.size(); I < E; ++I) { |
| 779 | OS << (I == Start ? "" : ", ") << Func.Counts[I]; |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 780 | } |
| 781 | OS << "]\n"; |
| 782 | } |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 783 | |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 784 | if (ShowIndirectCallTargets) { |
Rong Xu | 0cf1f56 | 2017-03-09 19:03:57 +0000 | [diff] [blame] | 785 | OS << " Indirect Target Results:\n"; |
| 786 | traverseAllValueSites(Func, IPVK_IndirectCallTarget, |
| 787 | VPStats[IPVK_IndirectCallTarget], OS, |
Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 788 | &(Reader->getSymtab())); |
| 789 | } |
| 790 | |
| 791 | if (ShowMemOPSizes && NumMemOPCalls > 0) { |
Teresa Johnson | cd2aa0d | 2017-05-24 17:55:25 +0000 | [diff] [blame] | 792 | OS << " Memory Intrinsic Size Results:\n"; |
Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 793 | traverseAllValueSites(Func, IPVK_MemOPSize, VPStats[IPVK_MemOPSize], OS, |
| 794 | nullptr); |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 795 | } |
| 796 | } |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 797 | } |
Justin Bogner | db1225d | 2014-03-23 20:55:53 +0000 | [diff] [blame] | 798 | if (Reader->hasError()) |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 799 | exitWithError(Reader->getError(), Filename); |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 800 | |
Richard Smith | c6ba9ca | 2018-08-24 01:34:45 +0000 | [diff] [blame] | 801 | if (TextFormat) |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 802 | return 0; |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 803 | std::unique_ptr<ProfileSummary> PS(Builder.getSummary()); |
Adam Nemet | 1142b2d | 2017-11-14 16:59:18 +0000 | [diff] [blame] | 804 | OS << "Instrumentation level: " |
| 805 | << (Reader->isIRLevelProfile() ? "IR" : "Front-end") << "\n"; |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 806 | if (ShowAllFunctions || !ShowFunction.empty()) |
| 807 | OS << "Functions shown: " << ShownFunctions << "\n"; |
Easwaran Raman | e5a17e3 | 2016-05-19 21:07:12 +0000 | [diff] [blame] | 808 | OS << "Total functions: " << PS->getNumFunctions() << "\n"; |
Rong Xu | 52aa224 | 2019-01-08 22:41:48 +0000 | [diff] [blame] | 809 | if (ValueCutoff > 0) { |
| 810 | OS << "Number of functions with maximum count (< " << ValueCutoff |
| 811 | << "): " << BelowCutoffFunctions << "\n"; |
| 812 | OS << "Number of functions with maximum count (>= " << ValueCutoff |
| 813 | << "): " << PS->getNumFunctions() - BelowCutoffFunctions << "\n"; |
| 814 | } |
Easwaran Raman | e5a17e3 | 2016-05-19 21:07:12 +0000 | [diff] [blame] | 815 | OS << "Maximum function count: " << PS->getMaxFunctionCount() << "\n"; |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 816 | OS << "Maximum internal block count: " << PS->getMaxInternalCount() << "\n"; |
Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 817 | |
Xinliang David Li | 801b531 | 2017-07-11 20:30:43 +0000 | [diff] [blame] | 818 | if (TopN) { |
| 819 | std::vector<std::pair<std::string, uint64_t>> SortedHottestFuncs; |
| 820 | while (!HottestFuncs.empty()) { |
| 821 | SortedHottestFuncs.emplace_back(HottestFuncs.top()); |
| 822 | HottestFuncs.pop(); |
| 823 | } |
| 824 | OS << "Top " << TopN |
| 825 | << " functions with the largest internal block counts: \n"; |
| 826 | for (auto &hotfunc : llvm::reverse(SortedHottestFuncs)) |
| 827 | OS << " " << hotfunc.first << ", max count = " << hotfunc.second << "\n"; |
| 828 | } |
| 829 | |
Xinliang David Li | 872362c | 2016-05-23 16:36:11 +0000 | [diff] [blame] | 830 | if (ShownFunctions && ShowIndirectCallTargets) { |
Rong Xu | 0cf1f56 | 2017-03-09 19:03:57 +0000 | [diff] [blame] | 831 | OS << "Statistics for indirect call sites profile:\n"; |
| 832 | showValueSitesStats(OS, IPVK_IndirectCallTarget, |
| 833 | VPStats[IPVK_IndirectCallTarget]); |
Xinliang David Li | 872362c | 2016-05-23 16:36:11 +0000 | [diff] [blame] | 834 | } |
Easwaran Raman | 183ebbe | 2016-01-13 21:44:36 +0000 | [diff] [blame] | 835 | |
Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 836 | if (ShownFunctions && ShowMemOPSizes) { |
| 837 | OS << "Statistics for memory intrinsic calls sizes profile:\n"; |
| 838 | showValueSitesStats(OS, IPVK_MemOPSize, VPStats[IPVK_MemOPSize]); |
| 839 | } |
| 840 | |
Easwaran Raman | 183ebbe | 2016-01-13 21:44:36 +0000 | [diff] [blame] | 841 | if (ShowDetailedSummary) { |
| 842 | OS << "Detailed summary:\n"; |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 843 | OS << "Total number of blocks: " << PS->getNumCounts() << "\n"; |
Easwaran Raman | e5a17e3 | 2016-05-19 21:07:12 +0000 | [diff] [blame] | 844 | OS << "Total count: " << PS->getTotalCount() << "\n"; |
| 845 | for (auto Entry : PS->getDetailedSummary()) { |
Easwaran Raman | 4309570 | 2016-02-17 18:18:47 +0000 | [diff] [blame] | 846 | OS << Entry.NumCounts << " blocks with count >= " << Entry.MinCount |
Easwaran Raman | 183ebbe | 2016-01-13 21:44:36 +0000 | [diff] [blame] | 847 | << " account for " |
| 848 | << format("%0.6g", (float)Entry.Cutoff / ProfileSummary::Scale * 100) |
| 849 | << " percentage of the total counts.\n"; |
| 850 | } |
| 851 | } |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 852 | return 0; |
| 853 | } |
| 854 | |
Benjamin Kramer | 1afc1de | 2016-06-17 20:41:14 +0000 | [diff] [blame] | 855 | static int showSampleProfile(const std::string &Filename, bool ShowCounts, |
| 856 | bool ShowAllFunctions, |
| 857 | const std::string &ShowFunction, |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 858 | raw_fd_ostream &OS) { |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 859 | using namespace sampleprof; |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 860 | LLVMContext Context; |
| 861 | auto ReaderOrErr = SampleProfileReader::create(Filename, Context); |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 862 | if (std::error_code EC = ReaderOrErr.getError()) |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 863 | exitWithErrorCode(EC, Filename); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 864 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 865 | auto Reader = std::move(ReaderOrErr.get()); |
Diego Novillo | c6d032a | 2015-09-17 00:17:21 +0000 | [diff] [blame] | 866 | if (std::error_code EC = Reader->read()) |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 867 | exitWithErrorCode(EC, Filename); |
Diego Novillo | c6d032a | 2015-09-17 00:17:21 +0000 | [diff] [blame] | 868 | |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 869 | if (ShowAllFunctions || ShowFunction.empty()) |
| 870 | Reader->dump(OS); |
| 871 | else |
| 872 | Reader->dumpFunctionProfile(ShowFunction, OS); |
| 873 | |
| 874 | return 0; |
| 875 | } |
| 876 | |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 877 | static int show_main(int argc, const char *argv[]) { |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 878 | cl::opt<std::string> Filename(cl::Positional, cl::Required, |
| 879 | cl::desc("<profdata-file>")); |
| 880 | |
| 881 | cl::opt<bool> ShowCounts("counts", cl::init(false), |
| 882 | cl::desc("Show counter values for shown functions")); |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 883 | cl::opt<bool> TextFormat( |
| 884 | "text", cl::init(false), |
| 885 | cl::desc("Show instr profile data in text dump format")); |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 886 | cl::opt<bool> ShowIndirectCallTargets( |
| 887 | "ic-targets", cl::init(false), |
| 888 | cl::desc("Show indirect call site target values for shown functions")); |
Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 889 | cl::opt<bool> ShowMemOPSizes( |
| 890 | "memop-sizes", cl::init(false), |
| 891 | cl::desc("Show the profiled sizes of the memory intrinsic calls " |
| 892 | "for shown functions")); |
Easwaran Raman | 183ebbe | 2016-01-13 21:44:36 +0000 | [diff] [blame] | 893 | cl::opt<bool> ShowDetailedSummary("detailed-summary", cl::init(false), |
| 894 | cl::desc("Show detailed profile summary")); |
| 895 | cl::list<uint32_t> DetailedSummaryCutoffs( |
| 896 | cl::CommaSeparated, "detailed-summary-cutoffs", |
| 897 | cl::desc( |
| 898 | "Cutoff percentages (times 10000) for generating detailed summary"), |
| 899 | cl::value_desc("800000,901000,999999")); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 900 | cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false), |
| 901 | cl::desc("Details for every function")); |
| 902 | cl::opt<std::string> ShowFunction("function", |
| 903 | cl::desc("Details for matching functions")); |
| 904 | |
| 905 | cl::opt<std::string> OutputFilename("output", cl::value_desc("output"), |
| 906 | cl::init("-"), cl::desc("Output file")); |
| 907 | cl::alias OutputFilenameA("o", cl::desc("Alias for --output"), |
| 908 | cl::aliasopt(OutputFilename)); |
| 909 | cl::opt<ProfileKinds> ProfileKind( |
| 910 | cl::desc("Profile kind:"), cl::init(instr), |
| 911 | cl::values(clEnumVal(instr, "Instrumentation profile (default)"), |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 912 | clEnumVal(sample, "Sample profile"))); |
Xinliang David Li | 801b531 | 2017-07-11 20:30:43 +0000 | [diff] [blame] | 913 | cl::opt<uint32_t> TopNFunctions( |
| 914 | "topn", cl::init(0), |
| 915 | cl::desc("Show the list of functions with the largest internal counts")); |
Rong Xu | 52aa224 | 2019-01-08 22:41:48 +0000 | [diff] [blame] | 916 | cl::opt<uint32_t> ValueCutoff( |
| 917 | "value-cutoff", cl::init(0), |
| 918 | cl::desc("Set the count value cutoff. Functions with the maximum count " |
| 919 | "less than this value will not be printed out. (Default is 0)")); |
| 920 | cl::opt<bool> OnlyListBelow( |
| 921 | "list-below-cutoff", cl::init(false), |
| 922 | cl::desc("Only output names of functions whose max count values are " |
| 923 | "below the cutoff value")); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 924 | cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n"); |
| 925 | |
| 926 | if (OutputFilename.empty()) |
| 927 | OutputFilename = "-"; |
| 928 | |
| 929 | std::error_code EC; |
| 930 | raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text); |
| 931 | if (EC) |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 932 | exitWithErrorCode(EC, OutputFilename); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 933 | |
| 934 | if (ShowAllFunctions && !ShowFunction.empty()) |
Jonas Devlieghere | e46b756 | 2018-04-18 14:42:33 +0000 | [diff] [blame] | 935 | WithColor::warning() << "-function argument ignored: showing all functions\n"; |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 936 | |
Easwaran Raman | 183ebbe | 2016-01-13 21:44:36 +0000 | [diff] [blame] | 937 | std::vector<uint32_t> Cutoffs(DetailedSummaryCutoffs.begin(), |
| 938 | DetailedSummaryCutoffs.end()); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 939 | if (ProfileKind == instr) |
Xinliang David Li | 801b531 | 2017-07-11 20:30:43 +0000 | [diff] [blame] | 940 | return showInstrProfile(Filename, ShowCounts, TopNFunctions, |
| 941 | ShowIndirectCallTargets, ShowMemOPSizes, |
| 942 | ShowDetailedSummary, DetailedSummaryCutoffs, |
Rong Xu | 52aa224 | 2019-01-08 22:41:48 +0000 | [diff] [blame] | 943 | ShowAllFunctions, ValueCutoff, OnlyListBelow, |
| 944 | ShowFunction, TextFormat, OS); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 945 | else |
| 946 | return showSampleProfile(Filename, ShowCounts, ShowAllFunctions, |
| 947 | ShowFunction, OS); |
| 948 | } |
| 949 | |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 950 | int main(int argc, const char *argv[]) { |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 951 | InitLLVM X(argc, argv); |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 952 | |
| 953 | StringRef ProgName(sys::path::filename(argv[0])); |
| 954 | if (argc > 1) { |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 955 | int (*func)(int, const char *[]) = nullptr; |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 956 | |
| 957 | if (strcmp(argv[1], "merge") == 0) |
| 958 | func = merge_main; |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 959 | else if (strcmp(argv[1], "show") == 0) |
| 960 | func = show_main; |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 961 | |
| 962 | if (func) { |
| 963 | std::string Invocation(ProgName.str() + " " + argv[1]); |
| 964 | argv[1] = Invocation.c_str(); |
| 965 | return func(argc - 1, argv + 1); |
| 966 | } |
| 967 | |
Diego Novillo | d3babdb | 2015-12-14 20:37:15 +0000 | [diff] [blame] | 968 | if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0 || |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 969 | strcmp(argv[1], "--help") == 0) { |
| 970 | |
| 971 | errs() << "OVERVIEW: LLVM profile data tools\n\n" |
| 972 | << "USAGE: " << ProgName << " <command> [args...]\n" |
| 973 | << "USAGE: " << ProgName << " <command> -help\n\n" |
Justin Bogner | 253eb17 | 2016-08-03 23:10:51 +0000 | [diff] [blame] | 974 | << "See each individual command --help for more details.\n" |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 975 | << "Available commands: merge, show\n"; |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 976 | return 0; |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | if (argc < 2) |
| 981 | errs() << ProgName << ": No command specified!\n"; |
| 982 | else |
| 983 | errs() << ProgName << ": Unknown command!\n"; |
| 984 | |
Justin Bogner | 9af28ef | 2014-03-21 17:29:44 +0000 | [diff] [blame] | 985 | errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n"; |
Justin Bogner | 618bcea | 2014-03-19 02:20:46 +0000 | [diff] [blame] | 986 | return 1; |
| 987 | } |