blob: 6715566a166c2915443db8821f3172a8945c730e [file] [log] [blame]
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +00001//===- llvm-profdata.cpp - LLVM profile data tool -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// llvm-profdata merges .profdata files.
11//
12//===----------------------------------------------------------------------===//
13
Nathan Slingerlandc21a44d2015-11-18 17:10:24 +000014#include "llvm/ADT/SmallSet.h"
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +000015#include "llvm/ADT/SmallVector.h"
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000016#include "llvm/ADT/StringRef.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000017#include "llvm/IR/LLVMContext.h"
Justin Bognerf8d79192014-03-21 17:24:48 +000018#include "llvm/ProfileData/InstrProfReader.h"
Justin Bognerb9bd7f82014-03-21 17:46:22 +000019#include "llvm/ProfileData/InstrProfWriter.h"
Easwaran Ramand68aae22016-02-04 23:34:31 +000020#include "llvm/ProfileData/ProfileCommon.h"
Diego Novillod5336ae2014-11-01 00:56:55 +000021#include "llvm/ProfileData/SampleProfReader.h"
22#include "llvm/ProfileData/SampleProfWriter.h"
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000023#include "llvm/Support/CommandLine.h"
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +000024#include "llvm/Support/Errc.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000025#include "llvm/Support/FileSystem.h"
Justin Bogner423380f2014-03-23 20:43:50 +000026#include "llvm/Support/Format.h"
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000027#include "llvm/Support/ManagedStatic.h"
28#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000029#include "llvm/Support/Path.h"
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000030#include "llvm/Support/PrettyStackTrace.h"
31#include "llvm/Support/Signals.h"
Vedant Kumare3a0bf52016-07-19 01:17:20 +000032#include "llvm/Support/ThreadPool.h"
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000033#include "llvm/Support/raw_ostream.h"
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +000034#include <algorithm>
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000035
36using namespace llvm;
37
Xinliang David Li6f7c19a2015-11-23 20:47:38 +000038enum ProfileFormat { PF_None = 0, PF_Text, PF_Binary, PF_GCC };
39
Diego Novillod3babdb2015-12-14 20:37:15 +000040static void exitWithError(const Twine &Message, StringRef Whence = "",
Nathan Slingerland4f823662015-11-13 03:47:58 +000041 StringRef Hint = "") {
Justin Bognerf8d79192014-03-21 17:24:48 +000042 errs() << "error: ";
43 if (!Whence.empty())
44 errs() << Whence << ": ";
45 errs() << Message << "\n";
Nathan Slingerland4f823662015-11-13 03:47:58 +000046 if (!Hint.empty())
47 errs() << Hint << "\n";
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000048 ::exit(1);
49}
50
Vedant Kumar9152fd12016-05-19 03:54:45 +000051static void exitWithError(Error E, StringRef Whence = "") {
52 if (E.isA<InstrProfError>()) {
53 handleAllErrors(std::move(E), [&](const InstrProfError &IPE) {
54 instrprof_error instrError = IPE.get();
55 StringRef Hint = "";
56 if (instrError == instrprof_error::unrecognized_format) {
57 // Hint for common error of forgetting -sample for sample profiles.
58 Hint = "Perhaps you forgot to use the -sample option?";
59 }
60 exitWithError(IPE.message(), Whence, Hint);
61 });
Nathan Slingerland4f823662015-11-13 03:47:58 +000062 }
Vedant Kumar9152fd12016-05-19 03:54:45 +000063
64 exitWithError(toString(std::move(E)), Whence);
65}
66
67static void exitWithErrorCode(std::error_code EC, StringRef Whence = "") {
68 exitWithError(EC.message(), Whence);
Nathan Slingerland4f823662015-11-13 03:47:58 +000069}
70
Duncan P. N. Exon Smith02b6fa92015-06-16 00:43:04 +000071namespace {
Diego Novillod3babdb2015-12-14 20:37:15 +000072enum ProfileKinds { instr, sample };
Duncan P. N. Exon Smith02b6fa92015-06-16 00:43:04 +000073}
Justin Bogner618bcea2014-03-19 02:20:46 +000074
Vedant Kumar9152fd12016-05-19 03:54:45 +000075static void handleMergeWriterError(Error E, StringRef WhenceFile = "",
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000076 StringRef WhenceFunction = "",
Diego Novillod3babdb2015-12-14 20:37:15 +000077 bool ShowHint = true) {
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000078 if (!WhenceFile.empty())
79 errs() << WhenceFile << ": ";
80 if (!WhenceFunction.empty())
81 errs() << WhenceFunction << ": ";
Vedant Kumar9152fd12016-05-19 03:54:45 +000082
83 auto IPE = instrprof_error::success;
84 E = handleErrors(std::move(E),
85 [&IPE](std::unique_ptr<InstrProfError> E) -> Error {
86 IPE = E->get();
87 return Error(std::move(E));
88 });
89 errs() << toString(std::move(E)) << "\n";
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000090
91 if (ShowHint) {
92 StringRef Hint = "";
Vedant Kumar9152fd12016-05-19 03:54:45 +000093 if (IPE != instrprof_error::success) {
94 switch (IPE) {
Nathan Slingerland11c938d12015-11-17 23:37:09 +000095 case instrprof_error::hash_mismatch:
96 case instrprof_error::count_mismatch:
97 case instrprof_error::value_site_count_mismatch:
Diego Novillod3babdb2015-12-14 20:37:15 +000098 Hint = "Make sure that all profile data to be merged is generated "
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000099 "from the same binary.";
Nathan Slingerland11c938d12015-11-17 23:37:09 +0000100 break;
Nathan Slingerlandb2d95f02015-11-18 00:52:45 +0000101 default:
102 break;
Nathan Slingerlande6e30d52015-11-17 22:08:53 +0000103 }
104 }
105
106 if (!Hint.empty())
107 errs() << Hint << "\n";
108 }
109}
110
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000111struct WeightedFile {
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000112 std::string Filename;
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000113 uint64_t Weight;
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000114};
115typedef SmallVector<WeightedFile, 5> WeightedFileVector;
116
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000117/// Keep track of merged data and reported errors.
118struct WriterContext {
119 std::mutex Lock;
120 InstrProfWriter Writer;
121 Error Err;
122 StringRef ErrWhence;
123 std::mutex &ErrLock;
124 SmallSet<instrprof_error, 4> &WriterErrorCodes;
125
126 WriterContext(bool IsSparse, std::mutex &ErrLock,
127 SmallSet<instrprof_error, 4> &WriterErrorCodes)
128 : Lock(), Writer(IsSparse), Err(Error::success()), ErrWhence(""),
129 ErrLock(ErrLock), WriterErrorCodes(WriterErrorCodes) {}
130};
131
132/// Load an input into a writer context.
133static void loadInput(const WeightedFile &Input, WriterContext *WC) {
134 std::unique_lock<std::mutex> CtxGuard{WC->Lock};
135
136 // If there's a pending hard error, don't do more work.
137 if (WC->Err)
138 return;
139
140 WC->ErrWhence = Input.Filename;
141
142 auto ReaderOrErr = InstrProfReader::create(Input.Filename);
Rong Xu2c684cf2016-10-19 22:51:17 +0000143 if (Error E = ReaderOrErr.takeError()) {
144 // Skip the empty profiles by returning sliently.
145 instrprof_error IPE = InstrProfError::take(std::move(E));
146 if (IPE != instrprof_error::empty_raw_profile)
147 WC->Err = make_error<InstrProfError>(IPE);
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000148 return;
Rong Xu2c684cf2016-10-19 22:51:17 +0000149 }
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000150
151 auto Reader = std::move(ReaderOrErr.get());
152 bool IsIRProfile = Reader->isIRLevelProfile();
153 if (WC->Writer.setIsIRLevelProfile(IsIRProfile)) {
154 WC->Err = make_error<StringError>(
155 "Merge IR generated profile with Clang generated profile.",
156 std::error_code());
157 return;
158 }
159
160 for (auto &I : *Reader) {
Rong Xufe90d862016-10-19 23:31:59 +0000161 const StringRef FuncName = I.Name;
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000162 if (Error E = WC->Writer.addRecord(std::move(I), Input.Weight)) {
163 // Only show hint the first time an error occurs.
164 instrprof_error IPE = InstrProfError::take(std::move(E));
165 std::unique_lock<std::mutex> ErrGuard{WC->ErrLock};
166 bool firstTime = WC->WriterErrorCodes.insert(IPE).second;
167 handleMergeWriterError(make_error<InstrProfError>(IPE), Input.Filename,
Rong Xufe90d862016-10-19 23:31:59 +0000168 FuncName, firstTime);
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000169 }
170 }
171 if (Reader->hasError())
172 WC->Err = Reader->getError();
173}
174
175/// Merge the \p Src writer context into \p Dst.
176static void mergeWriterContexts(WriterContext *Dst, WriterContext *Src) {
177 if (Error E = Dst->Writer.mergeRecordsFromWriter(std::move(Src->Writer)))
178 Dst->Err = std::move(E);
179}
180
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000181static void mergeInstrProfile(const WeightedFileVector &Inputs,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000182 StringRef OutputFilename,
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000183 ProfileFormat OutputFormat, bool OutputSparse,
184 unsigned NumThreads) {
Justin Bognerb7aa2632014-04-18 21:48:40 +0000185 if (OutputFilename.compare("-") == 0)
186 exitWithError("Cannot write indexed profdata format to stdout.");
Justin Bognerec49f982014-03-12 22:00:57 +0000187
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000188 if (OutputFormat != PF_Binary && OutputFormat != PF_Text)
189 exitWithError("Unknown format is specified.");
190
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000191 std::error_code EC;
192 raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None);
193 if (EC)
Nathan Slingerland4f823662015-11-13 03:47:58 +0000194 exitWithErrorCode(EC, OutputFilename);
Justin Bognerec49f982014-03-12 22:00:57 +0000195
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000196 std::mutex ErrorLock;
Vedant Kumar9152fd12016-05-19 03:54:45 +0000197 SmallSet<instrprof_error, 4> WriterErrorCodes;
Justin Bognerf8d79192014-03-21 17:24:48 +0000198
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000199 // If NumThreads is not specified, auto-detect a good default.
200 if (NumThreads == 0)
201 NumThreads = std::max(1U, std::min(std::thread::hardware_concurrency(),
202 unsigned(Inputs.size() / 2)));
Rong Xu33c76c02016-02-10 17:18:30 +0000203
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000204 // Initialize the writer contexts.
205 SmallVector<std::unique_ptr<WriterContext>, 4> Contexts;
206 for (unsigned I = 0; I < NumThreads; ++I)
207 Contexts.emplace_back(llvm::make_unique<WriterContext>(
208 OutputSparse, ErrorLock, WriterErrorCodes));
209
210 if (NumThreads == 1) {
211 for (const auto &Input : Inputs)
212 loadInput(Input, Contexts[0].get());
213 } else {
214 ThreadPool Pool(NumThreads);
215
216 // Load the inputs in parallel (N/NumThreads serial steps).
217 unsigned Ctx = 0;
218 for (const auto &Input : Inputs) {
219 Pool.async(loadInput, Input, Contexts[Ctx].get());
220 Ctx = (Ctx + 1) % NumThreads;
Nathan Slingerlande6e30d52015-11-17 22:08:53 +0000221 }
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000222 Pool.wait();
223
224 // Merge the writer contexts together (~ lg(NumThreads) serial steps).
225 unsigned Mid = Contexts.size() / 2;
226 unsigned End = Contexts.size();
227 assert(Mid > 0 && "Expected more than one context");
228 do {
229 for (unsigned I = 0; I < Mid; ++I)
230 Pool.async(mergeWriterContexts, Contexts[I].get(),
231 Contexts[I + Mid].get());
232 Pool.wait();
233 if (End & 1) {
234 Pool.async(mergeWriterContexts, Contexts[0].get(),
235 Contexts[End - 1].get());
236 Pool.wait();
237 }
238 End = Mid;
239 Mid /= 2;
240 } while (Mid > 0);
Justin Bognerbfee8d42014-03-12 20:14:17 +0000241 }
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000242
243 // Handle deferred hard errors encountered during merging.
244 for (std::unique_ptr<WriterContext> &WC : Contexts)
245 if (WC->Err)
246 exitWithError(std::move(WC->Err), WC->ErrWhence);
247
248 InstrProfWriter &Writer = Contexts[0]->Writer;
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000249 if (OutputFormat == PF_Text)
250 Writer.writeText(Output);
251 else
252 Writer.write(Output);
Diego Novillod5336ae2014-11-01 00:56:55 +0000253}
254
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000255static sampleprof::SampleProfileFormat FormatMap[] = {
256 sampleprof::SPF_None, sampleprof::SPF_Text, sampleprof::SPF_Binary,
257 sampleprof::SPF_GCC};
258
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000259static void mergeSampleProfile(const WeightedFileVector &Inputs,
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000260 StringRef OutputFilename,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000261 ProfileFormat OutputFormat) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000262 using namespace sampleprof;
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000263 auto WriterOrErr =
264 SampleProfileWriter::create(OutputFilename, FormatMap[OutputFormat]);
Diego Novillofcd55602014-11-03 00:51:45 +0000265 if (std::error_code EC = WriterOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000266 exitWithErrorCode(EC, OutputFilename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000267
Diego Novillofcd55602014-11-03 00:51:45 +0000268 auto Writer = std::move(WriterOrErr.get());
Diego Novillod5336ae2014-11-01 00:56:55 +0000269 StringMap<FunctionSamples> ProfileMap;
Diego Novilloaae1ed82015-10-08 19:40:37 +0000270 SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers;
Mehdi Amini03b42e42016-04-14 21:59:01 +0000271 LLVMContext Context;
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000272 for (const auto &Input : Inputs) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000273 auto ReaderOrErr = SampleProfileReader::create(Input.Filename, Context);
Diego Novillofcd55602014-11-03 00:51:45 +0000274 if (std::error_code EC = ReaderOrErr.getError())
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000275 exitWithErrorCode(EC, Input.Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000276
Diego Novilloaae1ed82015-10-08 19:40:37 +0000277 // We need to keep the readers around until after all the files are
278 // read so that we do not lose the function names stored in each
279 // reader's memory. The function names are needed to write out the
280 // merged profile map.
281 Readers.push_back(std::move(ReaderOrErr.get()));
282 const auto Reader = Readers.back().get();
Diego Novillod5336ae2014-11-01 00:56:55 +0000283 if (std::error_code EC = Reader->read())
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000284 exitWithErrorCode(EC, Input.Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000285
286 StringMap<FunctionSamples> &Profiles = Reader->getProfiles();
287 for (StringMap<FunctionSamples>::iterator I = Profiles.begin(),
288 E = Profiles.end();
289 I != E; ++I) {
290 StringRef FName = I->first();
291 FunctionSamples &Samples = I->second;
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000292 sampleprof_error Result = ProfileMap[FName].merge(Samples, Input.Weight);
293 if (Result != sampleprof_error::success) {
294 std::error_code EC = make_error_code(Result);
Vedant Kumar9152fd12016-05-19 03:54:45 +0000295 handleMergeWriterError(errorCodeToError(EC), Input.Filename, FName);
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000296 }
Diego Novillod5336ae2014-11-01 00:56:55 +0000297 }
298 }
299 Writer->write(ProfileMap);
300}
301
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000302static WeightedFile parseWeightedFile(const StringRef &WeightedFilename) {
Vedant Kumar8d0e8612016-06-06 23:43:56 +0000303 StringRef WeightStr, FileName;
304 std::tie(WeightStr, FileName) = WeightedFilename.split(',');
Diego Novillod5336ae2014-11-01 00:56:55 +0000305
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000306 uint64_t Weight;
307 if (WeightStr.getAsInteger(10, Weight) || Weight < 1)
308 exitWithError("Input weight must be a positive integer.");
309
Benjamin Kramer929e7db2016-07-21 14:29:11 +0000310 return {FileName, Weight};
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000311}
312
Vedant Kumarcef43602016-06-07 22:47:31 +0000313static std::unique_ptr<MemoryBuffer>
314getInputFilenamesFileBuf(const StringRef &InputFilenamesFile) {
315 if (InputFilenamesFile == "")
316 return {};
317
318 auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFilenamesFile);
319 if (!BufOrError)
320 exitWithErrorCode(BufOrError.getError(), InputFilenamesFile);
321
322 return std::move(*BufOrError);
323}
324
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000325static void addWeightedInput(WeightedFileVector &WNI, const WeightedFile &WF) {
326 StringRef Filename = WF.Filename;
327 uint64_t Weight = WF.Weight;
Benjamin Kramera81f4722016-07-22 12:39:55 +0000328
329 // If it's STDIN just pass it on.
330 if (Filename == "-") {
331 WNI.push_back({Filename, Weight});
332 return;
333 }
334
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000335 llvm::sys::fs::file_status Status;
336 llvm::sys::fs::status(Filename, Status);
337 if (!llvm::sys::fs::exists(Status))
338 exitWithErrorCode(make_error_code(errc::no_such_file_or_directory),
339 Filename);
340 // If it's a source file, collect it.
341 if (llvm::sys::fs::is_regular_file(Status)) {
Benjamin Kramer929e7db2016-07-21 14:29:11 +0000342 WNI.push_back({Filename, Weight});
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000343 return;
344 }
345
346 if (llvm::sys::fs::is_directory(Status)) {
347 std::error_code EC;
348 for (llvm::sys::fs::recursive_directory_iterator F(Filename, EC), E;
349 F != E && !EC; F.increment(EC)) {
350 if (llvm::sys::fs::is_regular_file(F->path())) {
351 addWeightedInput(WNI, {F->path(), Weight});
352 }
353 }
354 if (EC)
355 exitWithErrorCode(EC, Filename);
356 }
357}
358
Vedant Kumarcef43602016-06-07 22:47:31 +0000359static void parseInputFilenamesFile(MemoryBuffer *Buffer,
360 WeightedFileVector &WFV) {
361 if (!Buffer)
362 return;
363
364 SmallVector<StringRef, 8> Entries;
365 StringRef Data = Buffer->getBuffer();
366 Data.split(Entries, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
367 for (const StringRef &FileWeightEntry : Entries) {
368 StringRef SanitizedEntry = FileWeightEntry.trim(" \t\v\f\r");
369 // Skip comments.
370 if (SanitizedEntry.startswith("#"))
371 continue;
372 // If there's no comma, it's an unweighted profile.
373 else if (SanitizedEntry.find(',') == StringRef::npos)
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000374 addWeightedInput(WFV, {SanitizedEntry, 1});
Vedant Kumarcef43602016-06-07 22:47:31 +0000375 else
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000376 addWeightedInput(WFV, parseWeightedFile(SanitizedEntry));
Vedant Kumarcef43602016-06-07 22:47:31 +0000377 }
378}
379
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000380static int merge_main(int argc, const char *argv[]) {
381 cl::list<std::string> InputFilenames(cl::Positional,
382 cl::desc("<filename...>"));
383 cl::list<std::string> WeightedInputFilenames("weighted-input",
384 cl::desc("<weight>,<filename>"));
Vedant Kumarcef43602016-06-07 22:47:31 +0000385 cl::opt<std::string> InputFilenamesFile(
386 "input-files", cl::init(""),
387 cl::desc("Path to file containing newline-separated "
388 "[<weight>,]<filename> entries"));
389 cl::alias InputFilenamesFileA("f", cl::desc("Alias for --input-files"),
390 cl::aliasopt(InputFilenamesFile));
391 cl::opt<bool> DumpInputFileList(
392 "dump-input-file-list", cl::init(false), cl::Hidden,
393 cl::desc("Dump the list of input files and their weights, then exit"));
Diego Novillod5336ae2014-11-01 00:56:55 +0000394 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
395 cl::init("-"), cl::Required,
396 cl::desc("Output file"));
397 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
398 cl::aliasopt(OutputFilename));
399 cl::opt<ProfileKinds> ProfileKind(
400 cl::desc("Profile kind:"), cl::init(instr),
401 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
Mehdi Amini732afdd2016-10-08 19:41:06 +0000402 clEnumVal(sample, "Sample profile")));
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000403 cl::opt<ProfileFormat> OutputFormat(
404 cl::desc("Format of output profile"), cl::init(PF_Binary),
405 cl::values(clEnumValN(PF_Binary, "binary", "Binary encoding (default)"),
406 clEnumValN(PF_Text, "text", "Text encoding"),
407 clEnumValN(PF_GCC, "gcc",
Mehdi Amini732afdd2016-10-08 19:41:06 +0000408 "GCC encoding (only meaningful for -sample)")));
Vedant Kumar00dab222016-01-29 22:54:45 +0000409 cl::opt<bool> OutputSparse("sparse", cl::init(false),
410 cl::desc("Generate a sparse profile (only meaningful for -instr)"));
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000411 cl::opt<unsigned> NumThreads(
412 "num-threads", cl::init(0),
413 cl::desc("Number of merge threads to use (default: autodetect)"));
414 cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"),
415 cl::aliasopt(NumThreads));
Vedant Kumar00dab222016-01-29 22:54:45 +0000416
Diego Novillod5336ae2014-11-01 00:56:55 +0000417 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
418
Vedant Kumarcef43602016-06-07 22:47:31 +0000419 WeightedFileVector WeightedInputs;
420 for (StringRef Filename : InputFilenames)
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000421 addWeightedInput(WeightedInputs, {Filename, 1});
Vedant Kumarcef43602016-06-07 22:47:31 +0000422 for (StringRef WeightedFilename : WeightedInputFilenames)
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000423 addWeightedInput(WeightedInputs, parseWeightedFile(WeightedFilename));
Vedant Kumarcef43602016-06-07 22:47:31 +0000424
425 // Make sure that the file buffer stays alive for the duration of the
426 // weighted input vector's lifetime.
427 auto Buffer = getInputFilenamesFileBuf(InputFilenamesFile);
428 parseInputFilenamesFile(Buffer.get(), WeightedInputs);
429
430 if (WeightedInputs.empty())
Chandler Carruth0c30f892016-06-04 03:08:01 +0000431 exitWithError("No input files specified. See " +
432 sys::path::filename(argv[0]) + " -help");
433
Vedant Kumarcef43602016-06-07 22:47:31 +0000434 if (DumpInputFileList) {
435 for (auto &WF : WeightedInputs)
436 outs() << WF.Weight << "," << WF.Filename << "\n";
437 return 0;
438 }
Vedant Kumarf771a052016-06-04 00:36:28 +0000439
Diego Novillod5336ae2014-11-01 00:56:55 +0000440 if (ProfileKind == instr)
Vedant Kumar00dab222016-01-29 22:54:45 +0000441 mergeInstrProfile(WeightedInputs, OutputFilename, OutputFormat,
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000442 OutputSparse, NumThreads);
Diego Novillod5336ae2014-11-01 00:56:55 +0000443 else
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000444 mergeSampleProfile(WeightedInputs, OutputFilename, OutputFormat);
Justin Bognerbfee8d42014-03-12 20:14:17 +0000445
Justin Bognerec49f982014-03-12 22:00:57 +0000446 return 0;
Justin Bognerbfee8d42014-03-12 20:14:17 +0000447}
Justin Bogner618bcea2014-03-19 02:20:46 +0000448
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000449static int showInstrProfile(const std::string &Filename, bool ShowCounts,
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000450 bool ShowIndirectCallTargets,
451 bool ShowDetailedSummary,
452 std::vector<uint32_t> DetailedSummaryCutoffs,
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000453 bool ShowAllFunctions,
454 const std::string &ShowFunction, bool TextFormat,
455 raw_fd_ostream &OS) {
Diego Novillofcd55602014-11-03 00:51:45 +0000456 auto ReaderOrErr = InstrProfReader::create(Filename);
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000457 std::vector<uint32_t> Cutoffs = std::move(DetailedSummaryCutoffs);
458 if (ShowDetailedSummary && Cutoffs.empty()) {
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000459 Cutoffs = {800000, 900000, 950000, 990000, 999000, 999900, 999990};
460 }
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000461 InstrProfSummaryBuilder Builder(std::move(Cutoffs));
Vedant Kumar9152fd12016-05-19 03:54:45 +0000462 if (Error E = ReaderOrErr.takeError())
463 exitWithError(std::move(E), Filename);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000464
Diego Novillofcd55602014-11-03 00:51:45 +0000465 auto Reader = std::move(ReaderOrErr.get());
Rong Xu33c76c02016-02-10 17:18:30 +0000466 bool IsIRInstr = Reader->isIRLevelProfile();
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000467 size_t ShownFunctions = 0;
Xinliang David Li872362c2016-05-23 16:36:11 +0000468 uint64_t TotalNumValueSites = 0;
469 uint64_t TotalNumValueSitesWithValueProfile = 0;
470 uint64_t TotalNumValues = 0;
Xinliang David Lideda33c2016-09-20 21:04:22 +0000471 std::vector<unsigned> ICHistogram;
Justin Bogner9af28ef2014-03-21 17:29:44 +0000472 for (const auto &Func : *Reader) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000473 bool Show =
474 ShowAllFunctions || (!ShowFunction.empty() &&
475 Func.Name.find(ShowFunction) != Func.Name.npos);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000476
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000477 bool doTextFormatDump = (Show && ShowCounts && TextFormat);
478
479 if (doTextFormatDump) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000480 InstrProfSymtab &Symtab = Reader->getSymtab();
481 InstrProfWriter::writeRecordInText(Func, Symtab, OS);
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000482 continue;
483 }
484
Justin Bognerb59d7c72014-04-25 02:45:33 +0000485 assert(Func.Counts.size() > 0 && "function missing entry counter");
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000486 Builder.addRecord(Func);
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000487
Justin Bogner9af28ef2014-03-21 17:29:44 +0000488 if (Show) {
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000489
Justin Bogner9af28ef2014-03-21 17:29:44 +0000490 if (!ShownFunctions)
491 OS << "Counters:\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000492
Justin Bogner9af28ef2014-03-21 17:29:44 +0000493 ++ShownFunctions;
494
495 OS << " " << Func.Name << ":\n"
Justin Bogner423380f2014-03-23 20:43:50 +0000496 << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
Rong Xu33c76c02016-02-10 17:18:30 +0000497 << " Counters: " << Func.Counts.size() << "\n";
498 if (!IsIRInstr)
499 OS << " Function count: " << Func.Counts[0] << "\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000500
Justin Bogner9e9a0572015-09-29 22:13:58 +0000501 if (ShowIndirectCallTargets)
Xinliang David Li2004f002015-11-02 05:08:23 +0000502 OS << " Indirect Call Site Count: "
503 << Func.getNumValueSites(IPVK_IndirectCallTarget) << "\n";
Justin Bogner9af28ef2014-03-21 17:29:44 +0000504
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000505 if (ShowCounts) {
506 OS << " Block counts: [";
Rong Xu33c76c02016-02-10 17:18:30 +0000507 size_t Start = (IsIRInstr ? 0 : 1);
508 for (size_t I = Start, E = Func.Counts.size(); I < E; ++I) {
509 OS << (I == Start ? "" : ", ") << Func.Counts[I];
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000510 }
511 OS << "]\n";
512 }
Justin Bogner9e9a0572015-09-29 22:13:58 +0000513
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000514 if (ShowIndirectCallTargets) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000515 InstrProfSymtab &Symtab = Reader->getSymtab();
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000516 uint32_t NS = Func.getNumValueSites(IPVK_IndirectCallTarget);
517 OS << " Indirect Target Results: \n";
Xinliang David Li872362c2016-05-23 16:36:11 +0000518 TotalNumValueSites += NS;
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000519 for (size_t I = 0; I < NS; ++I) {
520 uint32_t NV = Func.getNumValueDataForSite(IPVK_IndirectCallTarget, I);
521 std::unique_ptr<InstrProfValueData[]> VD =
522 Func.getValueForSite(IPVK_IndirectCallTarget, I);
Xinliang David Li872362c2016-05-23 16:36:11 +0000523 TotalNumValues += NV;
Xinliang David Lideda33c2016-09-20 21:04:22 +0000524 if (NV) {
Xinliang David Li872362c2016-05-23 16:36:11 +0000525 TotalNumValueSitesWithValueProfile++;
Xinliang David Lideda33c2016-09-20 21:04:22 +0000526 if (NV > ICHistogram.size())
527 ICHistogram.resize(NV, 0);
528 ICHistogram[NV - 1]++;
529 }
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000530 for (uint32_t V = 0; V < NV; V++) {
531 OS << "\t[ " << I << ", ";
Xinliang David Lia716cc52015-12-20 06:22:13 +0000532 OS << Symtab.getFuncName(VD[V].Value) << ", " << VD[V].Count
533 << " ]\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000534 }
Justin Bogner9e9a0572015-09-29 22:13:58 +0000535 }
536 }
537 }
Justin Bogner9af28ef2014-03-21 17:29:44 +0000538 }
Justin Bognerdb1225d2014-03-23 20:55:53 +0000539 if (Reader->hasError())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000540 exitWithError(Reader->getError(), Filename);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000541
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000542 if (ShowCounts && TextFormat)
543 return 0;
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000544 std::unique_ptr<ProfileSummary> PS(Builder.getSummary());
Justin Bogner9af28ef2014-03-21 17:29:44 +0000545 if (ShowAllFunctions || !ShowFunction.empty())
546 OS << "Functions shown: " << ShownFunctions << "\n";
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000547 OS << "Total functions: " << PS->getNumFunctions() << "\n";
548 OS << "Maximum function count: " << PS->getMaxFunctionCount() << "\n";
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000549 OS << "Maximum internal block count: " << PS->getMaxInternalCount() << "\n";
Xinliang David Li872362c2016-05-23 16:36:11 +0000550 if (ShownFunctions && ShowIndirectCallTargets) {
551 OS << "Total Number of Indirect Call Sites : " << TotalNumValueSites
552 << "\n";
553 OS << "Total Number of Sites With Values : "
554 << TotalNumValueSitesWithValueProfile << "\n";
555 OS << "Total Number of Profiled Values : " << TotalNumValues << "\n";
Xinliang David Lideda33c2016-09-20 21:04:22 +0000556
557 OS << "IC Value histogram : \n\tNumTargets, SiteCount\n";
558 for (unsigned I = 0; I < ICHistogram.size(); I++) {
559 OS << "\t" << I + 1 << ", " << ICHistogram[I] << "\n";
560 }
Xinliang David Li872362c2016-05-23 16:36:11 +0000561 }
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000562
563 if (ShowDetailedSummary) {
564 OS << "Detailed summary:\n";
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000565 OS << "Total number of blocks: " << PS->getNumCounts() << "\n";
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000566 OS << "Total count: " << PS->getTotalCount() << "\n";
567 for (auto Entry : PS->getDetailedSummary()) {
Easwaran Raman43095702016-02-17 18:18:47 +0000568 OS << Entry.NumCounts << " blocks with count >= " << Entry.MinCount
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000569 << " account for "
570 << format("%0.6g", (float)Entry.Cutoff / ProfileSummary::Scale * 100)
571 << " percentage of the total counts.\n";
572 }
573 }
Justin Bogner9af28ef2014-03-21 17:29:44 +0000574 return 0;
575}
576
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000577static int showSampleProfile(const std::string &Filename, bool ShowCounts,
578 bool ShowAllFunctions,
579 const std::string &ShowFunction,
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000580 raw_fd_ostream &OS) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000581 using namespace sampleprof;
Mehdi Amini03b42e42016-04-14 21:59:01 +0000582 LLVMContext Context;
583 auto ReaderOrErr = SampleProfileReader::create(Filename, Context);
Diego Novillofcd55602014-11-03 00:51:45 +0000584 if (std::error_code EC = ReaderOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000585 exitWithErrorCode(EC, Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000586
Diego Novillofcd55602014-11-03 00:51:45 +0000587 auto Reader = std::move(ReaderOrErr.get());
Diego Novilloc6d032a2015-09-17 00:17:21 +0000588 if (std::error_code EC = Reader->read())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000589 exitWithErrorCode(EC, Filename);
Diego Novilloc6d032a2015-09-17 00:17:21 +0000590
Diego Novillod5336ae2014-11-01 00:56:55 +0000591 if (ShowAllFunctions || ShowFunction.empty())
592 Reader->dump(OS);
593 else
594 Reader->dumpFunctionProfile(ShowFunction, OS);
595
596 return 0;
597}
598
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000599static int show_main(int argc, const char *argv[]) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000600 cl::opt<std::string> Filename(cl::Positional, cl::Required,
601 cl::desc("<profdata-file>"));
602
603 cl::opt<bool> ShowCounts("counts", cl::init(false),
604 cl::desc("Show counter values for shown functions"));
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000605 cl::opt<bool> TextFormat(
606 "text", cl::init(false),
607 cl::desc("Show instr profile data in text dump format"));
Justin Bogner9e9a0572015-09-29 22:13:58 +0000608 cl::opt<bool> ShowIndirectCallTargets(
609 "ic-targets", cl::init(false),
610 cl::desc("Show indirect call site target values for shown functions"));
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000611 cl::opt<bool> ShowDetailedSummary("detailed-summary", cl::init(false),
612 cl::desc("Show detailed profile summary"));
613 cl::list<uint32_t> DetailedSummaryCutoffs(
614 cl::CommaSeparated, "detailed-summary-cutoffs",
615 cl::desc(
616 "Cutoff percentages (times 10000) for generating detailed summary"),
617 cl::value_desc("800000,901000,999999"));
Diego Novillod5336ae2014-11-01 00:56:55 +0000618 cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
619 cl::desc("Details for every function"));
620 cl::opt<std::string> ShowFunction("function",
621 cl::desc("Details for matching functions"));
622
623 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
624 cl::init("-"), cl::desc("Output file"));
625 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
626 cl::aliasopt(OutputFilename));
627 cl::opt<ProfileKinds> ProfileKind(
628 cl::desc("Profile kind:"), cl::init(instr),
629 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
Mehdi Amini732afdd2016-10-08 19:41:06 +0000630 clEnumVal(sample, "Sample profile")));
Diego Novillod5336ae2014-11-01 00:56:55 +0000631
632 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
633
634 if (OutputFilename.empty())
635 OutputFilename = "-";
636
637 std::error_code EC;
638 raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text);
639 if (EC)
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000640 exitWithErrorCode(EC, OutputFilename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000641
642 if (ShowAllFunctions && !ShowFunction.empty())
643 errs() << "warning: -function argument ignored: showing all functions\n";
644
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000645 std::vector<uint32_t> Cutoffs(DetailedSummaryCutoffs.begin(),
646 DetailedSummaryCutoffs.end());
Diego Novillod5336ae2014-11-01 00:56:55 +0000647 if (ProfileKind == instr)
Justin Bogner9e9a0572015-09-29 22:13:58 +0000648 return showInstrProfile(Filename, ShowCounts, ShowIndirectCallTargets,
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000649 ShowDetailedSummary, DetailedSummaryCutoffs,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000650 ShowAllFunctions, ShowFunction, TextFormat, OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000651 else
652 return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
653 ShowFunction, OS);
654}
655
Justin Bogner618bcea2014-03-19 02:20:46 +0000656int main(int argc, const char *argv[]) {
657 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +0000658 sys::PrintStackTraceOnErrorSignal(argv[0]);
Justin Bogner618bcea2014-03-19 02:20:46 +0000659 PrettyStackTraceProgram X(argc, argv);
660 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
661
662 StringRef ProgName(sys::path::filename(argv[0]));
663 if (argc > 1) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000664 int (*func)(int, const char *[]) = nullptr;
Justin Bogner618bcea2014-03-19 02:20:46 +0000665
666 if (strcmp(argv[1], "merge") == 0)
667 func = merge_main;
Justin Bogner9af28ef2014-03-21 17:29:44 +0000668 else if (strcmp(argv[1], "show") == 0)
669 func = show_main;
Justin Bogner618bcea2014-03-19 02:20:46 +0000670
671 if (func) {
672 std::string Invocation(ProgName.str() + " " + argv[1]);
673 argv[1] = Invocation.c_str();
674 return func(argc - 1, argv + 1);
675 }
676
Diego Novillod3babdb2015-12-14 20:37:15 +0000677 if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0 ||
Justin Bogner618bcea2014-03-19 02:20:46 +0000678 strcmp(argv[1], "--help") == 0) {
679
680 errs() << "OVERVIEW: LLVM profile data tools\n\n"
681 << "USAGE: " << ProgName << " <command> [args...]\n"
682 << "USAGE: " << ProgName << " <command> -help\n\n"
Justin Bogner253eb172016-08-03 23:10:51 +0000683 << "See each individual command --help for more details.\n"
Justin Bogner9af28ef2014-03-21 17:29:44 +0000684 << "Available commands: merge, show\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000685 return 0;
686 }
687 }
688
689 if (argc < 2)
690 errs() << ProgName << ": No command specified!\n";
691 else
692 errs() << ProgName << ": Unknown command!\n";
693
Justin Bogner9af28ef2014-03-21 17:29:44 +0000694 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000695 return 1;
696}