blob: b2da3c2466430aeefdace1a136d31fa73d1e2ae9 [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;
114
115 WeightedFile() {}
116
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000117 WeightedFile(const std::string &F, uint64_t W) : Filename{F}, Weight{W} {}
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000118};
119typedef SmallVector<WeightedFile, 5> WeightedFileVector;
120
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000121/// Keep track of merged data and reported errors.
122struct WriterContext {
123 std::mutex Lock;
124 InstrProfWriter Writer;
125 Error Err;
126 StringRef ErrWhence;
127 std::mutex &ErrLock;
128 SmallSet<instrprof_error, 4> &WriterErrorCodes;
129
130 WriterContext(bool IsSparse, std::mutex &ErrLock,
131 SmallSet<instrprof_error, 4> &WriterErrorCodes)
132 : Lock(), Writer(IsSparse), Err(Error::success()), ErrWhence(""),
133 ErrLock(ErrLock), WriterErrorCodes(WriterErrorCodes) {}
134};
135
136/// Load an input into a writer context.
137static void loadInput(const WeightedFile &Input, WriterContext *WC) {
138 std::unique_lock<std::mutex> CtxGuard{WC->Lock};
139
140 // If there's a pending hard error, don't do more work.
141 if (WC->Err)
142 return;
143
144 WC->ErrWhence = Input.Filename;
145
146 auto ReaderOrErr = InstrProfReader::create(Input.Filename);
147 if ((WC->Err = ReaderOrErr.takeError()))
148 return;
149
150 auto Reader = std::move(ReaderOrErr.get());
151 bool IsIRProfile = Reader->isIRLevelProfile();
152 if (WC->Writer.setIsIRLevelProfile(IsIRProfile)) {
153 WC->Err = make_error<StringError>(
154 "Merge IR generated profile with Clang generated profile.",
155 std::error_code());
156 return;
157 }
158
159 for (auto &I : *Reader) {
160 if (Error E = WC->Writer.addRecord(std::move(I), Input.Weight)) {
161 // Only show hint the first time an error occurs.
162 instrprof_error IPE = InstrProfError::take(std::move(E));
163 std::unique_lock<std::mutex> ErrGuard{WC->ErrLock};
164 bool firstTime = WC->WriterErrorCodes.insert(IPE).second;
165 handleMergeWriterError(make_error<InstrProfError>(IPE), Input.Filename,
166 I.Name, firstTime);
167 }
168 }
169 if (Reader->hasError())
170 WC->Err = Reader->getError();
171}
172
173/// Merge the \p Src writer context into \p Dst.
174static void mergeWriterContexts(WriterContext *Dst, WriterContext *Src) {
175 if (Error E = Dst->Writer.mergeRecordsFromWriter(std::move(Src->Writer)))
176 Dst->Err = std::move(E);
177}
178
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000179static void mergeInstrProfile(const WeightedFileVector &Inputs,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000180 StringRef OutputFilename,
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000181 ProfileFormat OutputFormat, bool OutputSparse,
182 unsigned NumThreads) {
Justin Bognerb7aa2632014-04-18 21:48:40 +0000183 if (OutputFilename.compare("-") == 0)
184 exitWithError("Cannot write indexed profdata format to stdout.");
Justin Bognerec49f982014-03-12 22:00:57 +0000185
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000186 if (OutputFormat != PF_Binary && OutputFormat != PF_Text)
187 exitWithError("Unknown format is specified.");
188
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000189 std::error_code EC;
190 raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None);
191 if (EC)
Nathan Slingerland4f823662015-11-13 03:47:58 +0000192 exitWithErrorCode(EC, OutputFilename);
Justin Bognerec49f982014-03-12 22:00:57 +0000193
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000194 std::mutex ErrorLock;
Vedant Kumar9152fd12016-05-19 03:54:45 +0000195 SmallSet<instrprof_error, 4> WriterErrorCodes;
Justin Bognerf8d79192014-03-21 17:24:48 +0000196
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000197 // If NumThreads is not specified, auto-detect a good default.
198 if (NumThreads == 0)
199 NumThreads = std::max(1U, std::min(std::thread::hardware_concurrency(),
200 unsigned(Inputs.size() / 2)));
Rong Xu33c76c02016-02-10 17:18:30 +0000201
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000202 // Initialize the writer contexts.
203 SmallVector<std::unique_ptr<WriterContext>, 4> Contexts;
204 for (unsigned I = 0; I < NumThreads; ++I)
205 Contexts.emplace_back(llvm::make_unique<WriterContext>(
206 OutputSparse, ErrorLock, WriterErrorCodes));
207
208 if (NumThreads == 1) {
209 for (const auto &Input : Inputs)
210 loadInput(Input, Contexts[0].get());
211 } else {
212 ThreadPool Pool(NumThreads);
213
214 // Load the inputs in parallel (N/NumThreads serial steps).
215 unsigned Ctx = 0;
216 for (const auto &Input : Inputs) {
217 Pool.async(loadInput, Input, Contexts[Ctx].get());
218 Ctx = (Ctx + 1) % NumThreads;
Nathan Slingerlande6e30d52015-11-17 22:08:53 +0000219 }
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000220 Pool.wait();
221
222 // Merge the writer contexts together (~ lg(NumThreads) serial steps).
223 unsigned Mid = Contexts.size() / 2;
224 unsigned End = Contexts.size();
225 assert(Mid > 0 && "Expected more than one context");
226 do {
227 for (unsigned I = 0; I < Mid; ++I)
228 Pool.async(mergeWriterContexts, Contexts[I].get(),
229 Contexts[I + Mid].get());
230 Pool.wait();
231 if (End & 1) {
232 Pool.async(mergeWriterContexts, Contexts[0].get(),
233 Contexts[End - 1].get());
234 Pool.wait();
235 }
236 End = Mid;
237 Mid /= 2;
238 } while (Mid > 0);
Justin Bognerbfee8d42014-03-12 20:14:17 +0000239 }
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000240
241 // Handle deferred hard errors encountered during merging.
242 for (std::unique_ptr<WriterContext> &WC : Contexts)
243 if (WC->Err)
244 exitWithError(std::move(WC->Err), WC->ErrWhence);
245
246 InstrProfWriter &Writer = Contexts[0]->Writer;
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000247 if (OutputFormat == PF_Text)
248 Writer.writeText(Output);
249 else
250 Writer.write(Output);
Diego Novillod5336ae2014-11-01 00:56:55 +0000251}
252
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000253static sampleprof::SampleProfileFormat FormatMap[] = {
254 sampleprof::SPF_None, sampleprof::SPF_Text, sampleprof::SPF_Binary,
255 sampleprof::SPF_GCC};
256
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000257static void mergeSampleProfile(const WeightedFileVector &Inputs,
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000258 StringRef OutputFilename,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000259 ProfileFormat OutputFormat) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000260 using namespace sampleprof;
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000261 auto WriterOrErr =
262 SampleProfileWriter::create(OutputFilename, FormatMap[OutputFormat]);
Diego Novillofcd55602014-11-03 00:51:45 +0000263 if (std::error_code EC = WriterOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000264 exitWithErrorCode(EC, OutputFilename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000265
Diego Novillofcd55602014-11-03 00:51:45 +0000266 auto Writer = std::move(WriterOrErr.get());
Diego Novillod5336ae2014-11-01 00:56:55 +0000267 StringMap<FunctionSamples> ProfileMap;
Diego Novilloaae1ed82015-10-08 19:40:37 +0000268 SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers;
Mehdi Amini03b42e42016-04-14 21:59:01 +0000269 LLVMContext Context;
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000270 for (const auto &Input : Inputs) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000271 auto ReaderOrErr = SampleProfileReader::create(Input.Filename, Context);
Diego Novillofcd55602014-11-03 00:51:45 +0000272 if (std::error_code EC = ReaderOrErr.getError())
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000273 exitWithErrorCode(EC, Input.Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000274
Diego Novilloaae1ed82015-10-08 19:40:37 +0000275 // We need to keep the readers around until after all the files are
276 // read so that we do not lose the function names stored in each
277 // reader's memory. The function names are needed to write out the
278 // merged profile map.
279 Readers.push_back(std::move(ReaderOrErr.get()));
280 const auto Reader = Readers.back().get();
Diego Novillod5336ae2014-11-01 00:56:55 +0000281 if (std::error_code EC = Reader->read())
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000282 exitWithErrorCode(EC, Input.Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000283
284 StringMap<FunctionSamples> &Profiles = Reader->getProfiles();
285 for (StringMap<FunctionSamples>::iterator I = Profiles.begin(),
286 E = Profiles.end();
287 I != E; ++I) {
288 StringRef FName = I->first();
289 FunctionSamples &Samples = I->second;
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000290 sampleprof_error Result = ProfileMap[FName].merge(Samples, Input.Weight);
291 if (Result != sampleprof_error::success) {
292 std::error_code EC = make_error_code(Result);
Vedant Kumar9152fd12016-05-19 03:54:45 +0000293 handleMergeWriterError(errorCodeToError(EC), Input.Filename, FName);
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000294 }
Diego Novillod5336ae2014-11-01 00:56:55 +0000295 }
296 }
297 Writer->write(ProfileMap);
298}
299
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000300static WeightedFile parseWeightedFile(const StringRef &WeightedFilename) {
Vedant Kumar8d0e8612016-06-06 23:43:56 +0000301 StringRef WeightStr, FileName;
302 std::tie(WeightStr, FileName) = WeightedFilename.split(',');
Diego Novillod5336ae2014-11-01 00:56:55 +0000303
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000304 uint64_t Weight;
305 if (WeightStr.getAsInteger(10, Weight) || Weight < 1)
306 exitWithError("Input weight must be a positive integer.");
307
Vedant Kumar8d0e8612016-06-06 23:43:56 +0000308 return WeightedFile(FileName, Weight);
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000309}
310
Vedant Kumarcef43602016-06-07 22:47:31 +0000311static std::unique_ptr<MemoryBuffer>
312getInputFilenamesFileBuf(const StringRef &InputFilenamesFile) {
313 if (InputFilenamesFile == "")
314 return {};
315
316 auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFilenamesFile);
317 if (!BufOrError)
318 exitWithErrorCode(BufOrError.getError(), InputFilenamesFile);
319
320 return std::move(*BufOrError);
321}
322
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000323static void addWeightedInput(WeightedFileVector &WNI, const WeightedFile &WF) {
324 StringRef Filename = WF.Filename;
325 uint64_t Weight = WF.Weight;
326 llvm::sys::fs::file_status Status;
327 llvm::sys::fs::status(Filename, Status);
328 if (!llvm::sys::fs::exists(Status))
329 exitWithErrorCode(make_error_code(errc::no_such_file_or_directory),
330 Filename);
331 // If it's a source file, collect it.
332 if (llvm::sys::fs::is_regular_file(Status)) {
333 WNI.emplace_back(Filename, Weight);
334 return;
335 }
336
337 if (llvm::sys::fs::is_directory(Status)) {
338 std::error_code EC;
339 for (llvm::sys::fs::recursive_directory_iterator F(Filename, EC), E;
340 F != E && !EC; F.increment(EC)) {
341 if (llvm::sys::fs::is_regular_file(F->path())) {
342 addWeightedInput(WNI, {F->path(), Weight});
343 }
344 }
345 if (EC)
346 exitWithErrorCode(EC, Filename);
347 }
348}
349
Vedant Kumarcef43602016-06-07 22:47:31 +0000350static void parseInputFilenamesFile(MemoryBuffer *Buffer,
351 WeightedFileVector &WFV) {
352 if (!Buffer)
353 return;
354
355 SmallVector<StringRef, 8> Entries;
356 StringRef Data = Buffer->getBuffer();
357 Data.split(Entries, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
358 for (const StringRef &FileWeightEntry : Entries) {
359 StringRef SanitizedEntry = FileWeightEntry.trim(" \t\v\f\r");
360 // Skip comments.
361 if (SanitizedEntry.startswith("#"))
362 continue;
363 // If there's no comma, it's an unweighted profile.
364 else if (SanitizedEntry.find(',') == StringRef::npos)
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000365 addWeightedInput(WFV, {SanitizedEntry, 1});
Vedant Kumarcef43602016-06-07 22:47:31 +0000366 else
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000367 addWeightedInput(WFV, parseWeightedFile(SanitizedEntry));
Vedant Kumarcef43602016-06-07 22:47:31 +0000368 }
369}
370
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000371static int merge_main(int argc, const char *argv[]) {
372 cl::list<std::string> InputFilenames(cl::Positional,
373 cl::desc("<filename...>"));
374 cl::list<std::string> WeightedInputFilenames("weighted-input",
375 cl::desc("<weight>,<filename>"));
Vedant Kumarcef43602016-06-07 22:47:31 +0000376 cl::opt<std::string> InputFilenamesFile(
377 "input-files", cl::init(""),
378 cl::desc("Path to file containing newline-separated "
379 "[<weight>,]<filename> entries"));
380 cl::alias InputFilenamesFileA("f", cl::desc("Alias for --input-files"),
381 cl::aliasopt(InputFilenamesFile));
382 cl::opt<bool> DumpInputFileList(
383 "dump-input-file-list", cl::init(false), cl::Hidden,
384 cl::desc("Dump the list of input files and their weights, then exit"));
Diego Novillod5336ae2014-11-01 00:56:55 +0000385 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
386 cl::init("-"), cl::Required,
387 cl::desc("Output file"));
388 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
389 cl::aliasopt(OutputFilename));
390 cl::opt<ProfileKinds> ProfileKind(
391 cl::desc("Profile kind:"), cl::init(instr),
392 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
393 clEnumVal(sample, "Sample profile"), clEnumValEnd));
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000394 cl::opt<ProfileFormat> OutputFormat(
395 cl::desc("Format of output profile"), cl::init(PF_Binary),
396 cl::values(clEnumValN(PF_Binary, "binary", "Binary encoding (default)"),
397 clEnumValN(PF_Text, "text", "Text encoding"),
398 clEnumValN(PF_GCC, "gcc",
399 "GCC encoding (only meaningful for -sample)"),
Diego Novillod5336ae2014-11-01 00:56:55 +0000400 clEnumValEnd));
Vedant Kumar00dab222016-01-29 22:54:45 +0000401 cl::opt<bool> OutputSparse("sparse", cl::init(false),
402 cl::desc("Generate a sparse profile (only meaningful for -instr)"));
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000403 cl::opt<unsigned> NumThreads(
404 "num-threads", cl::init(0),
405 cl::desc("Number of merge threads to use (default: autodetect)"));
406 cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"),
407 cl::aliasopt(NumThreads));
Vedant Kumar00dab222016-01-29 22:54:45 +0000408
Diego Novillod5336ae2014-11-01 00:56:55 +0000409 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
410
Vedant Kumarcef43602016-06-07 22:47:31 +0000411 WeightedFileVector WeightedInputs;
412 for (StringRef Filename : InputFilenames)
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000413 addWeightedInput(WeightedInputs, {Filename, 1});
Vedant Kumarcef43602016-06-07 22:47:31 +0000414 for (StringRef WeightedFilename : WeightedInputFilenames)
Xinliang David Li9a1bfcf2016-07-20 22:24:52 +0000415 addWeightedInput(WeightedInputs, parseWeightedFile(WeightedFilename));
Vedant Kumarcef43602016-06-07 22:47:31 +0000416
417 // Make sure that the file buffer stays alive for the duration of the
418 // weighted input vector's lifetime.
419 auto Buffer = getInputFilenamesFileBuf(InputFilenamesFile);
420 parseInputFilenamesFile(Buffer.get(), WeightedInputs);
421
422 if (WeightedInputs.empty())
Chandler Carruth0c30f892016-06-04 03:08:01 +0000423 exitWithError("No input files specified. See " +
424 sys::path::filename(argv[0]) + " -help");
425
Vedant Kumarcef43602016-06-07 22:47:31 +0000426 if (DumpInputFileList) {
427 for (auto &WF : WeightedInputs)
428 outs() << WF.Weight << "," << WF.Filename << "\n";
429 return 0;
430 }
Vedant Kumarf771a052016-06-04 00:36:28 +0000431
Diego Novillod5336ae2014-11-01 00:56:55 +0000432 if (ProfileKind == instr)
Vedant Kumar00dab222016-01-29 22:54:45 +0000433 mergeInstrProfile(WeightedInputs, OutputFilename, OutputFormat,
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000434 OutputSparse, NumThreads);
Diego Novillod5336ae2014-11-01 00:56:55 +0000435 else
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000436 mergeSampleProfile(WeightedInputs, OutputFilename, OutputFormat);
Justin Bognerbfee8d42014-03-12 20:14:17 +0000437
Justin Bognerec49f982014-03-12 22:00:57 +0000438 return 0;
Justin Bognerbfee8d42014-03-12 20:14:17 +0000439}
Justin Bogner618bcea2014-03-19 02:20:46 +0000440
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000441static int showInstrProfile(const std::string &Filename, bool ShowCounts,
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000442 bool ShowIndirectCallTargets,
443 bool ShowDetailedSummary,
444 std::vector<uint32_t> DetailedSummaryCutoffs,
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000445 bool ShowAllFunctions,
446 const std::string &ShowFunction, bool TextFormat,
447 raw_fd_ostream &OS) {
Diego Novillofcd55602014-11-03 00:51:45 +0000448 auto ReaderOrErr = InstrProfReader::create(Filename);
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000449 std::vector<uint32_t> Cutoffs = std::move(DetailedSummaryCutoffs);
450 if (ShowDetailedSummary && Cutoffs.empty()) {
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000451 Cutoffs = {800000, 900000, 950000, 990000, 999000, 999900, 999990};
452 }
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000453 InstrProfSummaryBuilder Builder(std::move(Cutoffs));
Vedant Kumar9152fd12016-05-19 03:54:45 +0000454 if (Error E = ReaderOrErr.takeError())
455 exitWithError(std::move(E), Filename);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000456
Diego Novillofcd55602014-11-03 00:51:45 +0000457 auto Reader = std::move(ReaderOrErr.get());
Rong Xu33c76c02016-02-10 17:18:30 +0000458 bool IsIRInstr = Reader->isIRLevelProfile();
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000459 size_t ShownFunctions = 0;
Xinliang David Li872362c2016-05-23 16:36:11 +0000460 uint64_t TotalNumValueSites = 0;
461 uint64_t TotalNumValueSitesWithValueProfile = 0;
462 uint64_t TotalNumValues = 0;
Justin Bogner9af28ef2014-03-21 17:29:44 +0000463 for (const auto &Func : *Reader) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000464 bool Show =
465 ShowAllFunctions || (!ShowFunction.empty() &&
466 Func.Name.find(ShowFunction) != Func.Name.npos);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000467
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000468 bool doTextFormatDump = (Show && ShowCounts && TextFormat);
469
470 if (doTextFormatDump) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000471 InstrProfSymtab &Symtab = Reader->getSymtab();
472 InstrProfWriter::writeRecordInText(Func, Symtab, OS);
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000473 continue;
474 }
475
Justin Bognerb59d7c72014-04-25 02:45:33 +0000476 assert(Func.Counts.size() > 0 && "function missing entry counter");
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000477 Builder.addRecord(Func);
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000478
Justin Bogner9af28ef2014-03-21 17:29:44 +0000479 if (Show) {
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000480
Justin Bogner9af28ef2014-03-21 17:29:44 +0000481 if (!ShownFunctions)
482 OS << "Counters:\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000483
Justin Bogner9af28ef2014-03-21 17:29:44 +0000484 ++ShownFunctions;
485
486 OS << " " << Func.Name << ":\n"
Justin Bogner423380f2014-03-23 20:43:50 +0000487 << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
Rong Xu33c76c02016-02-10 17:18:30 +0000488 << " Counters: " << Func.Counts.size() << "\n";
489 if (!IsIRInstr)
490 OS << " Function count: " << Func.Counts[0] << "\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000491
Justin Bogner9e9a0572015-09-29 22:13:58 +0000492 if (ShowIndirectCallTargets)
Xinliang David Li2004f002015-11-02 05:08:23 +0000493 OS << " Indirect Call Site Count: "
494 << Func.getNumValueSites(IPVK_IndirectCallTarget) << "\n";
Justin Bogner9af28ef2014-03-21 17:29:44 +0000495
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000496 if (ShowCounts) {
497 OS << " Block counts: [";
Rong Xu33c76c02016-02-10 17:18:30 +0000498 size_t Start = (IsIRInstr ? 0 : 1);
499 for (size_t I = Start, E = Func.Counts.size(); I < E; ++I) {
500 OS << (I == Start ? "" : ", ") << Func.Counts[I];
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000501 }
502 OS << "]\n";
503 }
Justin Bogner9e9a0572015-09-29 22:13:58 +0000504
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000505 if (ShowIndirectCallTargets) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000506 InstrProfSymtab &Symtab = Reader->getSymtab();
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000507 uint32_t NS = Func.getNumValueSites(IPVK_IndirectCallTarget);
508 OS << " Indirect Target Results: \n";
Xinliang David Li872362c2016-05-23 16:36:11 +0000509 TotalNumValueSites += NS;
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000510 for (size_t I = 0; I < NS; ++I) {
511 uint32_t NV = Func.getNumValueDataForSite(IPVK_IndirectCallTarget, I);
512 std::unique_ptr<InstrProfValueData[]> VD =
513 Func.getValueForSite(IPVK_IndirectCallTarget, I);
Xinliang David Li872362c2016-05-23 16:36:11 +0000514 TotalNumValues += NV;
515 if (NV)
516 TotalNumValueSitesWithValueProfile++;
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000517 for (uint32_t V = 0; V < NV; V++) {
518 OS << "\t[ " << I << ", ";
Xinliang David Lia716cc52015-12-20 06:22:13 +0000519 OS << Symtab.getFuncName(VD[V].Value) << ", " << VD[V].Count
520 << " ]\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000521 }
Justin Bogner9e9a0572015-09-29 22:13:58 +0000522 }
523 }
524 }
Justin Bogner9af28ef2014-03-21 17:29:44 +0000525 }
Justin Bognerdb1225d2014-03-23 20:55:53 +0000526 if (Reader->hasError())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000527 exitWithError(Reader->getError(), Filename);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000528
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000529 if (ShowCounts && TextFormat)
530 return 0;
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000531 std::unique_ptr<ProfileSummary> PS(Builder.getSummary());
Justin Bogner9af28ef2014-03-21 17:29:44 +0000532 if (ShowAllFunctions || !ShowFunction.empty())
533 OS << "Functions shown: " << ShownFunctions << "\n";
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000534 OS << "Total functions: " << PS->getNumFunctions() << "\n";
535 OS << "Maximum function count: " << PS->getMaxFunctionCount() << "\n";
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000536 OS << "Maximum internal block count: " << PS->getMaxInternalCount() << "\n";
Xinliang David Li872362c2016-05-23 16:36:11 +0000537 if (ShownFunctions && ShowIndirectCallTargets) {
538 OS << "Total Number of Indirect Call Sites : " << TotalNumValueSites
539 << "\n";
540 OS << "Total Number of Sites With Values : "
541 << TotalNumValueSitesWithValueProfile << "\n";
542 OS << "Total Number of Profiled Values : " << TotalNumValues << "\n";
543 }
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000544
545 if (ShowDetailedSummary) {
546 OS << "Detailed summary:\n";
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000547 OS << "Total number of blocks: " << PS->getNumCounts() << "\n";
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000548 OS << "Total count: " << PS->getTotalCount() << "\n";
549 for (auto Entry : PS->getDetailedSummary()) {
Easwaran Raman43095702016-02-17 18:18:47 +0000550 OS << Entry.NumCounts << " blocks with count >= " << Entry.MinCount
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000551 << " account for "
552 << format("%0.6g", (float)Entry.Cutoff / ProfileSummary::Scale * 100)
553 << " percentage of the total counts.\n";
554 }
555 }
Justin Bogner9af28ef2014-03-21 17:29:44 +0000556 return 0;
557}
558
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000559static int showSampleProfile(const std::string &Filename, bool ShowCounts,
560 bool ShowAllFunctions,
561 const std::string &ShowFunction,
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000562 raw_fd_ostream &OS) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000563 using namespace sampleprof;
Mehdi Amini03b42e42016-04-14 21:59:01 +0000564 LLVMContext Context;
565 auto ReaderOrErr = SampleProfileReader::create(Filename, Context);
Diego Novillofcd55602014-11-03 00:51:45 +0000566 if (std::error_code EC = ReaderOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000567 exitWithErrorCode(EC, Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000568
Diego Novillofcd55602014-11-03 00:51:45 +0000569 auto Reader = std::move(ReaderOrErr.get());
Diego Novilloc6d032a2015-09-17 00:17:21 +0000570 if (std::error_code EC = Reader->read())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000571 exitWithErrorCode(EC, Filename);
Diego Novilloc6d032a2015-09-17 00:17:21 +0000572
Diego Novillod5336ae2014-11-01 00:56:55 +0000573 if (ShowAllFunctions || ShowFunction.empty())
574 Reader->dump(OS);
575 else
576 Reader->dumpFunctionProfile(ShowFunction, OS);
577
578 return 0;
579}
580
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000581static int show_main(int argc, const char *argv[]) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000582 cl::opt<std::string> Filename(cl::Positional, cl::Required,
583 cl::desc("<profdata-file>"));
584
585 cl::opt<bool> ShowCounts("counts", cl::init(false),
586 cl::desc("Show counter values for shown functions"));
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000587 cl::opt<bool> TextFormat(
588 "text", cl::init(false),
589 cl::desc("Show instr profile data in text dump format"));
Justin Bogner9e9a0572015-09-29 22:13:58 +0000590 cl::opt<bool> ShowIndirectCallTargets(
591 "ic-targets", cl::init(false),
592 cl::desc("Show indirect call site target values for shown functions"));
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000593 cl::opt<bool> ShowDetailedSummary("detailed-summary", cl::init(false),
594 cl::desc("Show detailed profile summary"));
595 cl::list<uint32_t> DetailedSummaryCutoffs(
596 cl::CommaSeparated, "detailed-summary-cutoffs",
597 cl::desc(
598 "Cutoff percentages (times 10000) for generating detailed summary"),
599 cl::value_desc("800000,901000,999999"));
Diego Novillod5336ae2014-11-01 00:56:55 +0000600 cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
601 cl::desc("Details for every function"));
602 cl::opt<std::string> ShowFunction("function",
603 cl::desc("Details for matching functions"));
604
605 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
606 cl::init("-"), cl::desc("Output file"));
607 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
608 cl::aliasopt(OutputFilename));
609 cl::opt<ProfileKinds> ProfileKind(
610 cl::desc("Profile kind:"), cl::init(instr),
611 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
612 clEnumVal(sample, "Sample profile"), clEnumValEnd));
613
614 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
615
616 if (OutputFilename.empty())
617 OutputFilename = "-";
618
619 std::error_code EC;
620 raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text);
621 if (EC)
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000622 exitWithErrorCode(EC, OutputFilename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000623
624 if (ShowAllFunctions && !ShowFunction.empty())
625 errs() << "warning: -function argument ignored: showing all functions\n";
626
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000627 std::vector<uint32_t> Cutoffs(DetailedSummaryCutoffs.begin(),
628 DetailedSummaryCutoffs.end());
Diego Novillod5336ae2014-11-01 00:56:55 +0000629 if (ProfileKind == instr)
Justin Bogner9e9a0572015-09-29 22:13:58 +0000630 return showInstrProfile(Filename, ShowCounts, ShowIndirectCallTargets,
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000631 ShowDetailedSummary, DetailedSummaryCutoffs,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000632 ShowAllFunctions, ShowFunction, TextFormat, OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000633 else
634 return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
635 ShowFunction, OS);
636}
637
Justin Bogner618bcea2014-03-19 02:20:46 +0000638int main(int argc, const char *argv[]) {
639 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +0000640 sys::PrintStackTraceOnErrorSignal(argv[0]);
Justin Bogner618bcea2014-03-19 02:20:46 +0000641 PrettyStackTraceProgram X(argc, argv);
642 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
643
644 StringRef ProgName(sys::path::filename(argv[0]));
645 if (argc > 1) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000646 int (*func)(int, const char *[]) = nullptr;
Justin Bogner618bcea2014-03-19 02:20:46 +0000647
648 if (strcmp(argv[1], "merge") == 0)
649 func = merge_main;
Justin Bogner9af28ef2014-03-21 17:29:44 +0000650 else if (strcmp(argv[1], "show") == 0)
651 func = show_main;
Justin Bogner618bcea2014-03-19 02:20:46 +0000652
653 if (func) {
654 std::string Invocation(ProgName.str() + " " + argv[1]);
655 argv[1] = Invocation.c_str();
656 return func(argc - 1, argv + 1);
657 }
658
Diego Novillod3babdb2015-12-14 20:37:15 +0000659 if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0 ||
Justin Bogner618bcea2014-03-19 02:20:46 +0000660 strcmp(argv[1], "--help") == 0) {
661
662 errs() << "OVERVIEW: LLVM profile data tools\n\n"
663 << "USAGE: " << ProgName << " <command> [args...]\n"
664 << "USAGE: " << ProgName << " <command> -help\n\n"
Justin Bogner9af28ef2014-03-21 17:29:44 +0000665 << "Available commands: merge, show\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000666 return 0;
667 }
668 }
669
670 if (argc < 2)
671 errs() << ProgName << ": No command specified!\n";
672 else
673 errs() << ProgName << ": Unknown command!\n";
674
Justin Bogner9af28ef2014-03-21 17:29:44 +0000675 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000676 return 1;
677}