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