blob: 26ce4cc234fc610788a1d4338fc15a23e97d6dfb [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 Lice3f3852016-07-20 21:50:38 +0000112 StringRef Filename;
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000113 uint64_t Weight;
114
115 WeightedFile() {}
116
Xinliang David Lice3f3852016-07-20 21:50:38 +0000117 WeightedFile(StringRef 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
Xinliang David Lice3f3852016-07-20 21:50:38 +0000308 if (!sys::fs::exists(FileName))
309 exitWithErrorCode(make_error_code(errc::no_such_file_or_directory),
310 FileName);
311
Vedant Kumar8d0e8612016-06-06 23:43:56 +0000312 return WeightedFile(FileName, Weight);
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000313}
314
Vedant Kumarcef43602016-06-07 22:47:31 +0000315static std::unique_ptr<MemoryBuffer>
316getInputFilenamesFileBuf(const StringRef &InputFilenamesFile) {
317 if (InputFilenamesFile == "")
318 return {};
319
320 auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFilenamesFile);
321 if (!BufOrError)
322 exitWithErrorCode(BufOrError.getError(), InputFilenamesFile);
323
324 return std::move(*BufOrError);
325}
326
327static void parseInputFilenamesFile(MemoryBuffer *Buffer,
328 WeightedFileVector &WFV) {
329 if (!Buffer)
330 return;
331
332 SmallVector<StringRef, 8> Entries;
333 StringRef Data = Buffer->getBuffer();
334 Data.split(Entries, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
335 for (const StringRef &FileWeightEntry : Entries) {
336 StringRef SanitizedEntry = FileWeightEntry.trim(" \t\v\f\r");
337 // Skip comments.
338 if (SanitizedEntry.startswith("#"))
339 continue;
340 // If there's no comma, it's an unweighted profile.
341 else if (SanitizedEntry.find(',') == StringRef::npos)
Xinliang David Lice3f3852016-07-20 21:50:38 +0000342 WFV.emplace_back(SanitizedEntry, 1);
Vedant Kumarcef43602016-06-07 22:47:31 +0000343 else
Xinliang David Lice3f3852016-07-20 21:50:38 +0000344 WFV.emplace_back(parseWeightedFile(SanitizedEntry));
Vedant Kumarcef43602016-06-07 22:47:31 +0000345 }
346}
347
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000348static int merge_main(int argc, const char *argv[]) {
349 cl::list<std::string> InputFilenames(cl::Positional,
350 cl::desc("<filename...>"));
351 cl::list<std::string> WeightedInputFilenames("weighted-input",
352 cl::desc("<weight>,<filename>"));
Vedant Kumarcef43602016-06-07 22:47:31 +0000353 cl::opt<std::string> InputFilenamesFile(
354 "input-files", cl::init(""),
355 cl::desc("Path to file containing newline-separated "
356 "[<weight>,]<filename> entries"));
357 cl::alias InputFilenamesFileA("f", cl::desc("Alias for --input-files"),
358 cl::aliasopt(InputFilenamesFile));
359 cl::opt<bool> DumpInputFileList(
360 "dump-input-file-list", cl::init(false), cl::Hidden,
361 cl::desc("Dump the list of input files and their weights, then exit"));
Diego Novillod5336ae2014-11-01 00:56:55 +0000362 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
363 cl::init("-"), cl::Required,
364 cl::desc("Output file"));
365 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
366 cl::aliasopt(OutputFilename));
367 cl::opt<ProfileKinds> ProfileKind(
368 cl::desc("Profile kind:"), cl::init(instr),
369 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
370 clEnumVal(sample, "Sample profile"), clEnumValEnd));
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000371 cl::opt<ProfileFormat> OutputFormat(
372 cl::desc("Format of output profile"), cl::init(PF_Binary),
373 cl::values(clEnumValN(PF_Binary, "binary", "Binary encoding (default)"),
374 clEnumValN(PF_Text, "text", "Text encoding"),
375 clEnumValN(PF_GCC, "gcc",
376 "GCC encoding (only meaningful for -sample)"),
Diego Novillod5336ae2014-11-01 00:56:55 +0000377 clEnumValEnd));
Vedant Kumar00dab222016-01-29 22:54:45 +0000378 cl::opt<bool> OutputSparse("sparse", cl::init(false),
379 cl::desc("Generate a sparse profile (only meaningful for -instr)"));
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000380 cl::opt<unsigned> NumThreads(
381 "num-threads", cl::init(0),
382 cl::desc("Number of merge threads to use (default: autodetect)"));
383 cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"),
384 cl::aliasopt(NumThreads));
Vedant Kumar00dab222016-01-29 22:54:45 +0000385
Diego Novillod5336ae2014-11-01 00:56:55 +0000386 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
387
Vedant Kumarcef43602016-06-07 22:47:31 +0000388 WeightedFileVector WeightedInputs;
389 for (StringRef Filename : InputFilenames)
Xinliang David Lice3f3852016-07-20 21:50:38 +0000390 WeightedInputs.emplace_back(Filename, 1);
Vedant Kumarcef43602016-06-07 22:47:31 +0000391 for (StringRef WeightedFilename : WeightedInputFilenames)
Xinliang David Lice3f3852016-07-20 21:50:38 +0000392 WeightedInputs.emplace_back(parseWeightedFile(WeightedFilename));
Vedant Kumarcef43602016-06-07 22:47:31 +0000393
394 // Make sure that the file buffer stays alive for the duration of the
395 // weighted input vector's lifetime.
396 auto Buffer = getInputFilenamesFileBuf(InputFilenamesFile);
397 parseInputFilenamesFile(Buffer.get(), WeightedInputs);
398
399 if (WeightedInputs.empty())
Chandler Carruth0c30f892016-06-04 03:08:01 +0000400 exitWithError("No input files specified. See " +
401 sys::path::filename(argv[0]) + " -help");
402
Vedant Kumarcef43602016-06-07 22:47:31 +0000403 if (DumpInputFileList) {
404 for (auto &WF : WeightedInputs)
405 outs() << WF.Weight << "," << WF.Filename << "\n";
406 return 0;
407 }
Vedant Kumarf771a052016-06-04 00:36:28 +0000408
Diego Novillod5336ae2014-11-01 00:56:55 +0000409 if (ProfileKind == instr)
Vedant Kumar00dab222016-01-29 22:54:45 +0000410 mergeInstrProfile(WeightedInputs, OutputFilename, OutputFormat,
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000411 OutputSparse, NumThreads);
Diego Novillod5336ae2014-11-01 00:56:55 +0000412 else
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000413 mergeSampleProfile(WeightedInputs, OutputFilename, OutputFormat);
Justin Bognerbfee8d42014-03-12 20:14:17 +0000414
Justin Bognerec49f982014-03-12 22:00:57 +0000415 return 0;
Justin Bognerbfee8d42014-03-12 20:14:17 +0000416}
Justin Bogner618bcea2014-03-19 02:20:46 +0000417
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000418static int showInstrProfile(const std::string &Filename, bool ShowCounts,
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000419 bool ShowIndirectCallTargets,
420 bool ShowDetailedSummary,
421 std::vector<uint32_t> DetailedSummaryCutoffs,
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000422 bool ShowAllFunctions,
423 const std::string &ShowFunction, bool TextFormat,
424 raw_fd_ostream &OS) {
Diego Novillofcd55602014-11-03 00:51:45 +0000425 auto ReaderOrErr = InstrProfReader::create(Filename);
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000426 std::vector<uint32_t> Cutoffs = std::move(DetailedSummaryCutoffs);
427 if (ShowDetailedSummary && Cutoffs.empty()) {
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000428 Cutoffs = {800000, 900000, 950000, 990000, 999000, 999900, 999990};
429 }
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000430 InstrProfSummaryBuilder Builder(std::move(Cutoffs));
Vedant Kumar9152fd12016-05-19 03:54:45 +0000431 if (Error E = ReaderOrErr.takeError())
432 exitWithError(std::move(E), Filename);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000433
Diego Novillofcd55602014-11-03 00:51:45 +0000434 auto Reader = std::move(ReaderOrErr.get());
Rong Xu33c76c02016-02-10 17:18:30 +0000435 bool IsIRInstr = Reader->isIRLevelProfile();
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000436 size_t ShownFunctions = 0;
Xinliang David Li872362c2016-05-23 16:36:11 +0000437 uint64_t TotalNumValueSites = 0;
438 uint64_t TotalNumValueSitesWithValueProfile = 0;
439 uint64_t TotalNumValues = 0;
Justin Bogner9af28ef2014-03-21 17:29:44 +0000440 for (const auto &Func : *Reader) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000441 bool Show =
442 ShowAllFunctions || (!ShowFunction.empty() &&
443 Func.Name.find(ShowFunction) != Func.Name.npos);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000444
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000445 bool doTextFormatDump = (Show && ShowCounts && TextFormat);
446
447 if (doTextFormatDump) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000448 InstrProfSymtab &Symtab = Reader->getSymtab();
449 InstrProfWriter::writeRecordInText(Func, Symtab, OS);
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000450 continue;
451 }
452
Justin Bognerb59d7c72014-04-25 02:45:33 +0000453 assert(Func.Counts.size() > 0 && "function missing entry counter");
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000454 Builder.addRecord(Func);
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000455
Justin Bogner9af28ef2014-03-21 17:29:44 +0000456 if (Show) {
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000457
Justin Bogner9af28ef2014-03-21 17:29:44 +0000458 if (!ShownFunctions)
459 OS << "Counters:\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000460
Justin Bogner9af28ef2014-03-21 17:29:44 +0000461 ++ShownFunctions;
462
463 OS << " " << Func.Name << ":\n"
Justin Bogner423380f2014-03-23 20:43:50 +0000464 << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
Rong Xu33c76c02016-02-10 17:18:30 +0000465 << " Counters: " << Func.Counts.size() << "\n";
466 if (!IsIRInstr)
467 OS << " Function count: " << Func.Counts[0] << "\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000468
Justin Bogner9e9a0572015-09-29 22:13:58 +0000469 if (ShowIndirectCallTargets)
Xinliang David Li2004f002015-11-02 05:08:23 +0000470 OS << " Indirect Call Site Count: "
471 << Func.getNumValueSites(IPVK_IndirectCallTarget) << "\n";
Justin Bogner9af28ef2014-03-21 17:29:44 +0000472
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000473 if (ShowCounts) {
474 OS << " Block counts: [";
Rong Xu33c76c02016-02-10 17:18:30 +0000475 size_t Start = (IsIRInstr ? 0 : 1);
476 for (size_t I = Start, E = Func.Counts.size(); I < E; ++I) {
477 OS << (I == Start ? "" : ", ") << Func.Counts[I];
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000478 }
479 OS << "]\n";
480 }
Justin Bogner9e9a0572015-09-29 22:13:58 +0000481
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000482 if (ShowIndirectCallTargets) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000483 InstrProfSymtab &Symtab = Reader->getSymtab();
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000484 uint32_t NS = Func.getNumValueSites(IPVK_IndirectCallTarget);
485 OS << " Indirect Target Results: \n";
Xinliang David Li872362c2016-05-23 16:36:11 +0000486 TotalNumValueSites += NS;
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000487 for (size_t I = 0; I < NS; ++I) {
488 uint32_t NV = Func.getNumValueDataForSite(IPVK_IndirectCallTarget, I);
489 std::unique_ptr<InstrProfValueData[]> VD =
490 Func.getValueForSite(IPVK_IndirectCallTarget, I);
Xinliang David Li872362c2016-05-23 16:36:11 +0000491 TotalNumValues += NV;
492 if (NV)
493 TotalNumValueSitesWithValueProfile++;
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000494 for (uint32_t V = 0; V < NV; V++) {
495 OS << "\t[ " << I << ", ";
Xinliang David Lia716cc52015-12-20 06:22:13 +0000496 OS << Symtab.getFuncName(VD[V].Value) << ", " << VD[V].Count
497 << " ]\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000498 }
Justin Bogner9e9a0572015-09-29 22:13:58 +0000499 }
500 }
501 }
Justin Bogner9af28ef2014-03-21 17:29:44 +0000502 }
Justin Bognerdb1225d2014-03-23 20:55:53 +0000503 if (Reader->hasError())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000504 exitWithError(Reader->getError(), Filename);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000505
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000506 if (ShowCounts && TextFormat)
507 return 0;
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000508 std::unique_ptr<ProfileSummary> PS(Builder.getSummary());
Justin Bogner9af28ef2014-03-21 17:29:44 +0000509 if (ShowAllFunctions || !ShowFunction.empty())
510 OS << "Functions shown: " << ShownFunctions << "\n";
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000511 OS << "Total functions: " << PS->getNumFunctions() << "\n";
512 OS << "Maximum function count: " << PS->getMaxFunctionCount() << "\n";
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000513 OS << "Maximum internal block count: " << PS->getMaxInternalCount() << "\n";
Xinliang David Li872362c2016-05-23 16:36:11 +0000514 if (ShownFunctions && ShowIndirectCallTargets) {
515 OS << "Total Number of Indirect Call Sites : " << TotalNumValueSites
516 << "\n";
517 OS << "Total Number of Sites With Values : "
518 << TotalNumValueSitesWithValueProfile << "\n";
519 OS << "Total Number of Profiled Values : " << TotalNumValues << "\n";
520 }
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000521
522 if (ShowDetailedSummary) {
523 OS << "Detailed summary:\n";
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000524 OS << "Total number of blocks: " << PS->getNumCounts() << "\n";
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000525 OS << "Total count: " << PS->getTotalCount() << "\n";
526 for (auto Entry : PS->getDetailedSummary()) {
Easwaran Raman43095702016-02-17 18:18:47 +0000527 OS << Entry.NumCounts << " blocks with count >= " << Entry.MinCount
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000528 << " account for "
529 << format("%0.6g", (float)Entry.Cutoff / ProfileSummary::Scale * 100)
530 << " percentage of the total counts.\n";
531 }
532 }
Justin Bogner9af28ef2014-03-21 17:29:44 +0000533 return 0;
534}
535
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000536static int showSampleProfile(const std::string &Filename, bool ShowCounts,
537 bool ShowAllFunctions,
538 const std::string &ShowFunction,
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000539 raw_fd_ostream &OS) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000540 using namespace sampleprof;
Mehdi Amini03b42e42016-04-14 21:59:01 +0000541 LLVMContext Context;
542 auto ReaderOrErr = SampleProfileReader::create(Filename, Context);
Diego Novillofcd55602014-11-03 00:51:45 +0000543 if (std::error_code EC = ReaderOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000544 exitWithErrorCode(EC, Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000545
Diego Novillofcd55602014-11-03 00:51:45 +0000546 auto Reader = std::move(ReaderOrErr.get());
Diego Novilloc6d032a2015-09-17 00:17:21 +0000547 if (std::error_code EC = Reader->read())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000548 exitWithErrorCode(EC, Filename);
Diego Novilloc6d032a2015-09-17 00:17:21 +0000549
Diego Novillod5336ae2014-11-01 00:56:55 +0000550 if (ShowAllFunctions || ShowFunction.empty())
551 Reader->dump(OS);
552 else
553 Reader->dumpFunctionProfile(ShowFunction, OS);
554
555 return 0;
556}
557
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000558static int show_main(int argc, const char *argv[]) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000559 cl::opt<std::string> Filename(cl::Positional, cl::Required,
560 cl::desc("<profdata-file>"));
561
562 cl::opt<bool> ShowCounts("counts", cl::init(false),
563 cl::desc("Show counter values for shown functions"));
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000564 cl::opt<bool> TextFormat(
565 "text", cl::init(false),
566 cl::desc("Show instr profile data in text dump format"));
Justin Bogner9e9a0572015-09-29 22:13:58 +0000567 cl::opt<bool> ShowIndirectCallTargets(
568 "ic-targets", cl::init(false),
569 cl::desc("Show indirect call site target values for shown functions"));
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000570 cl::opt<bool> ShowDetailedSummary("detailed-summary", cl::init(false),
571 cl::desc("Show detailed profile summary"));
572 cl::list<uint32_t> DetailedSummaryCutoffs(
573 cl::CommaSeparated, "detailed-summary-cutoffs",
574 cl::desc(
575 "Cutoff percentages (times 10000) for generating detailed summary"),
576 cl::value_desc("800000,901000,999999"));
Diego Novillod5336ae2014-11-01 00:56:55 +0000577 cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
578 cl::desc("Details for every function"));
579 cl::opt<std::string> ShowFunction("function",
580 cl::desc("Details for matching functions"));
581
582 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
583 cl::init("-"), cl::desc("Output file"));
584 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
585 cl::aliasopt(OutputFilename));
586 cl::opt<ProfileKinds> ProfileKind(
587 cl::desc("Profile kind:"), cl::init(instr),
588 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
589 clEnumVal(sample, "Sample profile"), clEnumValEnd));
590
591 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
592
593 if (OutputFilename.empty())
594 OutputFilename = "-";
595
596 std::error_code EC;
597 raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text);
598 if (EC)
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000599 exitWithErrorCode(EC, OutputFilename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000600
601 if (ShowAllFunctions && !ShowFunction.empty())
602 errs() << "warning: -function argument ignored: showing all functions\n";
603
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000604 std::vector<uint32_t> Cutoffs(DetailedSummaryCutoffs.begin(),
605 DetailedSummaryCutoffs.end());
Diego Novillod5336ae2014-11-01 00:56:55 +0000606 if (ProfileKind == instr)
Justin Bogner9e9a0572015-09-29 22:13:58 +0000607 return showInstrProfile(Filename, ShowCounts, ShowIndirectCallTargets,
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000608 ShowDetailedSummary, DetailedSummaryCutoffs,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000609 ShowAllFunctions, ShowFunction, TextFormat, OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000610 else
611 return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
612 ShowFunction, OS);
613}
614
Justin Bogner618bcea2014-03-19 02:20:46 +0000615int main(int argc, const char *argv[]) {
616 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +0000617 sys::PrintStackTraceOnErrorSignal(argv[0]);
Justin Bogner618bcea2014-03-19 02:20:46 +0000618 PrettyStackTraceProgram X(argc, argv);
619 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
620
621 StringRef ProgName(sys::path::filename(argv[0]));
622 if (argc > 1) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000623 int (*func)(int, const char *[]) = nullptr;
Justin Bogner618bcea2014-03-19 02:20:46 +0000624
625 if (strcmp(argv[1], "merge") == 0)
626 func = merge_main;
Justin Bogner9af28ef2014-03-21 17:29:44 +0000627 else if (strcmp(argv[1], "show") == 0)
628 func = show_main;
Justin Bogner618bcea2014-03-19 02:20:46 +0000629
630 if (func) {
631 std::string Invocation(ProgName.str() + " " + argv[1]);
632 argv[1] = Invocation.c_str();
633 return func(argc - 1, argv + 1);
634 }
635
Diego Novillod3babdb2015-12-14 20:37:15 +0000636 if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0 ||
Justin Bogner618bcea2014-03-19 02:20:46 +0000637 strcmp(argv[1], "--help") == 0) {
638
639 errs() << "OVERVIEW: LLVM profile data tools\n\n"
640 << "USAGE: " << ProgName << " <command> [args...]\n"
641 << "USAGE: " << ProgName << " <command> -help\n\n"
Justin Bogner9af28ef2014-03-21 17:29:44 +0000642 << "Available commands: merge, show\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000643 return 0;
644 }
645 }
646
647 if (argc < 2)
648 errs() << ProgName << ": No command specified!\n";
649 else
650 errs() << ProgName << ": Unknown command!\n";
651
Justin Bogner9af28ef2014-03-21 17:29:44 +0000652 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000653 return 1;
654}