blob: dc6cd0a5784d615ff7c2bfaa3f13bd1df0b19e41 [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"
Diego Novillod5336ae2014-11-01 00:56:55 +000020#include "llvm/ProfileData/SampleProfReader.h"
21#include "llvm/ProfileData/SampleProfWriter.h"
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000022#include "llvm/Support/CommandLine.h"
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +000023#include "llvm/Support/Errc.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000024#include "llvm/Support/FileSystem.h"
Justin Bogner423380f2014-03-23 20:43:50 +000025#include "llvm/Support/Format.h"
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000026#include "llvm/Support/ManagedStatic.h"
27#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000028#include "llvm/Support/Path.h"
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000029#include "llvm/Support/PrettyStackTrace.h"
30#include "llvm/Support/Signals.h"
31#include "llvm/Support/raw_ostream.h"
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +000032#include <algorithm>
33#include <tuple>
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000034
35using namespace llvm;
36
Xinliang David Li6f7c19a2015-11-23 20:47:38 +000037enum ProfileFormat { PF_None = 0, PF_Text, PF_Binary, PF_GCC };
38
Diego Novillod3babdb2015-12-14 20:37:15 +000039static void exitWithError(const Twine &Message, StringRef Whence = "",
Nathan Slingerland4f823662015-11-13 03:47:58 +000040 StringRef Hint = "") {
Justin Bognerf8d79192014-03-21 17:24:48 +000041 errs() << "error: ";
42 if (!Whence.empty())
43 errs() << Whence << ": ";
44 errs() << Message << "\n";
Nathan Slingerland4f823662015-11-13 03:47:58 +000045 if (!Hint.empty())
46 errs() << Hint << "\n";
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000047 ::exit(1);
48}
49
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000050static void exitWithErrorCode(const std::error_code &Error,
51 StringRef Whence = "") {
Nathan Slingerland4f823662015-11-13 03:47:58 +000052 if (Error.category() == instrprof_category()) {
53 instrprof_error instrError = static_cast<instrprof_error>(Error.value());
54 if (instrError == instrprof_error::unrecognized_format) {
55 // Hint for common error of forgetting -sample for sample profiles.
56 exitWithError(Error.message(), Whence,
57 "Perhaps you forgot to use the -sample option?");
58 }
59 }
60 exitWithError(Error.message(), Whence);
61}
62
Duncan P. N. Exon Smith02b6fa92015-06-16 00:43:04 +000063namespace {
Diego Novillod3babdb2015-12-14 20:37:15 +000064enum ProfileKinds { instr, sample };
Duncan P. N. Exon Smith02b6fa92015-06-16 00:43:04 +000065}
Justin Bogner618bcea2014-03-19 02:20:46 +000066
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000067static void handleMergeWriterError(std::error_code &Error,
68 StringRef WhenceFile = "",
69 StringRef WhenceFunction = "",
Diego Novillod3babdb2015-12-14 20:37:15 +000070 bool ShowHint = true) {
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000071 if (!WhenceFile.empty())
72 errs() << WhenceFile << ": ";
73 if (!WhenceFunction.empty())
74 errs() << WhenceFunction << ": ";
75 errs() << Error.message() << "\n";
76
77 if (ShowHint) {
78 StringRef Hint = "";
79 if (Error.category() == instrprof_category()) {
80 instrprof_error instrError = static_cast<instrprof_error>(Error.value());
Nathan Slingerland11c938d12015-11-17 23:37:09 +000081 switch (instrError) {
82 case instrprof_error::hash_mismatch:
83 case instrprof_error::count_mismatch:
84 case instrprof_error::value_site_count_mismatch:
Diego Novillod3babdb2015-12-14 20:37:15 +000085 Hint = "Make sure that all profile data to be merged is generated "
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000086 "from the same binary.";
Nathan Slingerland11c938d12015-11-17 23:37:09 +000087 break;
Nathan Slingerlandb2d95f02015-11-18 00:52:45 +000088 default:
89 break;
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000090 }
91 }
92
93 if (!Hint.empty())
94 errs() << Hint << "\n";
95 }
96}
97
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +000098struct WeightedFile {
99 StringRef Filename;
100 uint64_t Weight;
101
102 WeightedFile() {}
103
104 WeightedFile(StringRef F, uint64_t W) : Filename{F}, Weight{W} {}
105};
106typedef SmallVector<WeightedFile, 5> WeightedFileVector;
107
108static void mergeInstrProfile(const WeightedFileVector &Inputs,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000109 StringRef OutputFilename,
110 ProfileFormat OutputFormat) {
Justin Bognerb7aa2632014-04-18 21:48:40 +0000111 if (OutputFilename.compare("-") == 0)
112 exitWithError("Cannot write indexed profdata format to stdout.");
Justin Bognerec49f982014-03-12 22:00:57 +0000113
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000114 if (OutputFormat != PF_Binary && OutputFormat != PF_Text)
115 exitWithError("Unknown format is specified.");
116
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000117 std::error_code EC;
118 raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None);
119 if (EC)
Nathan Slingerland4f823662015-11-13 03:47:58 +0000120 exitWithErrorCode(EC, OutputFilename);
Justin Bognerec49f982014-03-12 22:00:57 +0000121
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000122 InstrProfWriter Writer;
Nathan Slingerlandc21a44d2015-11-18 17:10:24 +0000123 SmallSet<std::error_code, 4> WriterErrorCodes;
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000124 for (const auto &Input : Inputs) {
125 auto ReaderOrErr = InstrProfReader::create(Input.Filename);
Diego Novillofcd55602014-11-03 00:51:45 +0000126 if (std::error_code ec = ReaderOrErr.getError())
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000127 exitWithErrorCode(ec, Input.Filename);
Justin Bognerf8d79192014-03-21 17:24:48 +0000128
Diego Novillofcd55602014-11-03 00:51:45 +0000129 auto Reader = std::move(ReaderOrErr.get());
Nathan Slingerlande6e30d52015-11-17 22:08:53 +0000130 for (auto &I : *Reader) {
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000131 if (std::error_code EC = Writer.addRecord(std::move(I), Input.Weight)) {
Nathan Slingerlande6e30d52015-11-17 22:08:53 +0000132 // Only show hint the first time an error occurs.
133 bool firstTime = WriterErrorCodes.insert(EC).second;
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000134 handleMergeWriterError(EC, Input.Filename, I.Name, firstTime);
Nathan Slingerlande6e30d52015-11-17 22:08:53 +0000135 }
136 }
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000137 if (Reader->hasError())
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000138 exitWithErrorCode(Reader->getError(), Input.Filename);
Justin Bognerbfee8d42014-03-12 20:14:17 +0000139 }
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000140 if (OutputFormat == PF_Text)
141 Writer.writeText(Output);
142 else
143 Writer.write(Output);
Diego Novillod5336ae2014-11-01 00:56:55 +0000144}
145
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000146static sampleprof::SampleProfileFormat FormatMap[] = {
147 sampleprof::SPF_None, sampleprof::SPF_Text, sampleprof::SPF_Binary,
148 sampleprof::SPF_GCC};
149
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000150static void mergeSampleProfile(const WeightedFileVector &Inputs,
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000151 StringRef OutputFilename,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000152 ProfileFormat OutputFormat) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000153 using namespace sampleprof;
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000154 auto WriterOrErr =
155 SampleProfileWriter::create(OutputFilename, FormatMap[OutputFormat]);
Diego Novillofcd55602014-11-03 00:51:45 +0000156 if (std::error_code EC = WriterOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000157 exitWithErrorCode(EC, OutputFilename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000158
Diego Novillofcd55602014-11-03 00:51:45 +0000159 auto Writer = std::move(WriterOrErr.get());
Diego Novillod5336ae2014-11-01 00:56:55 +0000160 StringMap<FunctionSamples> ProfileMap;
Diego Novilloaae1ed82015-10-08 19:40:37 +0000161 SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers;
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000162 for (const auto &Input : Inputs) {
Diego Novillofcd55602014-11-03 00:51:45 +0000163 auto ReaderOrErr =
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000164 SampleProfileReader::create(Input.Filename, getGlobalContext());
Diego Novillofcd55602014-11-03 00:51:45 +0000165 if (std::error_code EC = ReaderOrErr.getError())
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000166 exitWithErrorCode(EC, Input.Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000167
Diego Novilloaae1ed82015-10-08 19:40:37 +0000168 // We need to keep the readers around until after all the files are
169 // read so that we do not lose the function names stored in each
170 // reader's memory. The function names are needed to write out the
171 // merged profile map.
172 Readers.push_back(std::move(ReaderOrErr.get()));
173 const auto Reader = Readers.back().get();
Diego Novillod5336ae2014-11-01 00:56:55 +0000174 if (std::error_code EC = Reader->read())
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000175 exitWithErrorCode(EC, Input.Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000176
177 StringMap<FunctionSamples> &Profiles = Reader->getProfiles();
178 for (StringMap<FunctionSamples>::iterator I = Profiles.begin(),
179 E = Profiles.end();
180 I != E; ++I) {
181 StringRef FName = I->first();
182 FunctionSamples &Samples = I->second;
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000183 sampleprof_error Result = ProfileMap[FName].merge(Samples, Input.Weight);
184 if (Result != sampleprof_error::success) {
185 std::error_code EC = make_error_code(Result);
186 handleMergeWriterError(EC, Input.Filename, FName);
187 }
Diego Novillod5336ae2014-11-01 00:56:55 +0000188 }
189 }
190 Writer->write(ProfileMap);
191}
192
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000193static WeightedFile parseWeightedFile(const StringRef &WeightedFilename) {
194 StringRef WeightStr, FileName;
195 std::tie(WeightStr, FileName) = WeightedFilename.split(',');
Diego Novillod5336ae2014-11-01 00:56:55 +0000196
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000197 uint64_t Weight;
198 if (WeightStr.getAsInteger(10, Weight) || Weight < 1)
199 exitWithError("Input weight must be a positive integer.");
200
201 if (!sys::fs::exists(FileName))
202 exitWithErrorCode(make_error_code(errc::no_such_file_or_directory),
203 FileName);
204
205 return WeightedFile(FileName, Weight);
206}
207
208static int merge_main(int argc, const char *argv[]) {
209 cl::list<std::string> InputFilenames(cl::Positional,
210 cl::desc("<filename...>"));
211 cl::list<std::string> WeightedInputFilenames("weighted-input",
212 cl::desc("<weight>,<filename>"));
Diego Novillod5336ae2014-11-01 00:56:55 +0000213 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
214 cl::init("-"), cl::Required,
215 cl::desc("Output file"));
216 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
217 cl::aliasopt(OutputFilename));
218 cl::opt<ProfileKinds> ProfileKind(
219 cl::desc("Profile kind:"), cl::init(instr),
220 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
221 clEnumVal(sample, "Sample profile"), clEnumValEnd));
222
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000223 cl::opt<ProfileFormat> OutputFormat(
224 cl::desc("Format of output profile"), cl::init(PF_Binary),
225 cl::values(clEnumValN(PF_Binary, "binary", "Binary encoding (default)"),
226 clEnumValN(PF_Text, "text", "Text encoding"),
227 clEnumValN(PF_GCC, "gcc",
228 "GCC encoding (only meaningful for -sample)"),
Diego Novillod5336ae2014-11-01 00:56:55 +0000229 clEnumValEnd));
230
231 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
232
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000233 if (InputFilenames.empty() && WeightedInputFilenames.empty())
234 exitWithError("No input files specified. See " +
235 sys::path::filename(argv[0]) + " -help");
236
237 WeightedFileVector WeightedInputs;
238 for (StringRef Filename : InputFilenames)
239 WeightedInputs.push_back(WeightedFile(Filename, 1));
240 for (StringRef WeightedFilename : WeightedInputFilenames)
241 WeightedInputs.push_back(parseWeightedFile(WeightedFilename));
242
Diego Novillod5336ae2014-11-01 00:56:55 +0000243 if (ProfileKind == instr)
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000244 mergeInstrProfile(WeightedInputs, OutputFilename, OutputFormat);
Diego Novillod5336ae2014-11-01 00:56:55 +0000245 else
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000246 mergeSampleProfile(WeightedInputs, OutputFilename, OutputFormat);
Justin Bognerbfee8d42014-03-12 20:14:17 +0000247
Justin Bognerec49f982014-03-12 22:00:57 +0000248 return 0;
Justin Bognerbfee8d42014-03-12 20:14:17 +0000249}
Justin Bogner618bcea2014-03-19 02:20:46 +0000250
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000251static int showInstrProfile(std::string Filename, bool ShowCounts,
Justin Bogner9e9a0572015-09-29 22:13:58 +0000252 bool ShowIndirectCallTargets, bool ShowAllFunctions,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000253 std::string ShowFunction, bool TextFormat,
254 raw_fd_ostream &OS) {
Diego Novillofcd55602014-11-03 00:51:45 +0000255 auto ReaderOrErr = InstrProfReader::create(Filename);
256 if (std::error_code EC = ReaderOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000257 exitWithErrorCode(EC, Filename);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000258
Diego Novillofcd55602014-11-03 00:51:45 +0000259 auto Reader = std::move(ReaderOrErr.get());
Justin Bogner9af28ef2014-03-21 17:29:44 +0000260 uint64_t MaxFunctionCount = 0, MaxBlockCount = 0;
261 size_t ShownFunctions = 0, TotalFunctions = 0;
262 for (const auto &Func : *Reader) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000263 bool Show =
264 ShowAllFunctions || (!ShowFunction.empty() &&
265 Func.Name.find(ShowFunction) != Func.Name.npos);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000266
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000267 bool doTextFormatDump = (Show && ShowCounts && TextFormat);
268
269 if (doTextFormatDump) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000270 InstrProfSymtab &Symtab = Reader->getSymtab();
271 InstrProfWriter::writeRecordInText(Func, Symtab, OS);
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000272 continue;
273 }
274
Justin Bogner9af28ef2014-03-21 17:29:44 +0000275 ++TotalFunctions;
Justin Bognerb59d7c72014-04-25 02:45:33 +0000276 assert(Func.Counts.size() > 0 && "function missing entry counter");
Justin Bogner9af28ef2014-03-21 17:29:44 +0000277 if (Func.Counts[0] > MaxFunctionCount)
278 MaxFunctionCount = Func.Counts[0];
279
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000280 for (size_t I = 1, E = Func.Counts.size(); I < E; ++I) {
281 if (Func.Counts[I] > MaxBlockCount)
282 MaxBlockCount = Func.Counts[I];
283 }
284
Justin Bogner9af28ef2014-03-21 17:29:44 +0000285 if (Show) {
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000286
Justin Bogner9af28ef2014-03-21 17:29:44 +0000287 if (!ShownFunctions)
288 OS << "Counters:\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000289
Justin Bogner9af28ef2014-03-21 17:29:44 +0000290 ++ShownFunctions;
291
292 OS << " " << Func.Name << ":\n"
Justin Bogner423380f2014-03-23 20:43:50 +0000293 << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
Justin Bogner9af28ef2014-03-21 17:29:44 +0000294 << " Counters: " << Func.Counts.size() << "\n"
295 << " Function count: " << Func.Counts[0] << "\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000296
Justin Bogner9e9a0572015-09-29 22:13:58 +0000297 if (ShowIndirectCallTargets)
Xinliang David Li2004f002015-11-02 05:08:23 +0000298 OS << " Indirect Call Site Count: "
299 << Func.getNumValueSites(IPVK_IndirectCallTarget) << "\n";
Justin Bogner9af28ef2014-03-21 17:29:44 +0000300
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000301 if (ShowCounts) {
302 OS << " Block counts: [";
303 for (size_t I = 1, E = Func.Counts.size(); I < E; ++I) {
304 OS << (I == 1 ? "" : ", ") << Func.Counts[I];
305 }
306 OS << "]\n";
307 }
Justin Bogner9e9a0572015-09-29 22:13:58 +0000308
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000309 if (ShowIndirectCallTargets) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000310 InstrProfSymtab &Symtab = Reader->getSymtab();
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000311 uint32_t NS = Func.getNumValueSites(IPVK_IndirectCallTarget);
312 OS << " Indirect Target Results: \n";
313 for (size_t I = 0; I < NS; ++I) {
314 uint32_t NV = Func.getNumValueDataForSite(IPVK_IndirectCallTarget, I);
315 std::unique_ptr<InstrProfValueData[]> VD =
316 Func.getValueForSite(IPVK_IndirectCallTarget, I);
317 for (uint32_t V = 0; V < NV; V++) {
318 OS << "\t[ " << I << ", ";
Xinliang David Lia716cc52015-12-20 06:22:13 +0000319 OS << Symtab.getFuncName(VD[V].Value) << ", " << VD[V].Count
320 << " ]\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000321 }
Justin Bogner9e9a0572015-09-29 22:13:58 +0000322 }
323 }
324 }
Justin Bogner9af28ef2014-03-21 17:29:44 +0000325 }
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000326
Justin Bognerdb1225d2014-03-23 20:55:53 +0000327 if (Reader->hasError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000328 exitWithErrorCode(Reader->getError(), Filename);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000329
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000330 if (ShowCounts && TextFormat)
331 return 0;
332
Justin Bogner9af28ef2014-03-21 17:29:44 +0000333 if (ShowAllFunctions || !ShowFunction.empty())
334 OS << "Functions shown: " << ShownFunctions << "\n";
335 OS << "Total functions: " << TotalFunctions << "\n";
336 OS << "Maximum function count: " << MaxFunctionCount << "\n";
337 OS << "Maximum internal block count: " << MaxBlockCount << "\n";
338 return 0;
339}
340
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000341static int showSampleProfile(std::string Filename, bool ShowCounts,
342 bool ShowAllFunctions, std::string ShowFunction,
343 raw_fd_ostream &OS) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000344 using namespace sampleprof;
Diego Novillofcd55602014-11-03 00:51:45 +0000345 auto ReaderOrErr = SampleProfileReader::create(Filename, getGlobalContext());
346 if (std::error_code EC = ReaderOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000347 exitWithErrorCode(EC, Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000348
Diego Novillofcd55602014-11-03 00:51:45 +0000349 auto Reader = std::move(ReaderOrErr.get());
Diego Novilloc6d032a2015-09-17 00:17:21 +0000350 if (std::error_code EC = Reader->read())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000351 exitWithErrorCode(EC, Filename);
Diego Novilloc6d032a2015-09-17 00:17:21 +0000352
Diego Novillod5336ae2014-11-01 00:56:55 +0000353 if (ShowAllFunctions || ShowFunction.empty())
354 Reader->dump(OS);
355 else
356 Reader->dumpFunctionProfile(ShowFunction, OS);
357
358 return 0;
359}
360
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000361static int show_main(int argc, const char *argv[]) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000362 cl::opt<std::string> Filename(cl::Positional, cl::Required,
363 cl::desc("<profdata-file>"));
364
365 cl::opt<bool> ShowCounts("counts", cl::init(false),
366 cl::desc("Show counter values for shown functions"));
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000367 cl::opt<bool> TextFormat(
368 "text", cl::init(false),
369 cl::desc("Show instr profile data in text dump format"));
Justin Bogner9e9a0572015-09-29 22:13:58 +0000370 cl::opt<bool> ShowIndirectCallTargets(
371 "ic-targets", cl::init(false),
372 cl::desc("Show indirect call site target values for shown functions"));
Diego Novillod5336ae2014-11-01 00:56:55 +0000373 cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
374 cl::desc("Details for every function"));
375 cl::opt<std::string> ShowFunction("function",
376 cl::desc("Details for matching functions"));
377
378 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
379 cl::init("-"), cl::desc("Output file"));
380 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
381 cl::aliasopt(OutputFilename));
382 cl::opt<ProfileKinds> ProfileKind(
383 cl::desc("Profile kind:"), cl::init(instr),
384 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
385 clEnumVal(sample, "Sample profile"), clEnumValEnd));
386
387 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
388
389 if (OutputFilename.empty())
390 OutputFilename = "-";
391
392 std::error_code EC;
393 raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text);
394 if (EC)
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000395 exitWithErrorCode(EC, OutputFilename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000396
397 if (ShowAllFunctions && !ShowFunction.empty())
398 errs() << "warning: -function argument ignored: showing all functions\n";
399
400 if (ProfileKind == instr)
Justin Bogner9e9a0572015-09-29 22:13:58 +0000401 return showInstrProfile(Filename, ShowCounts, ShowIndirectCallTargets,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000402 ShowAllFunctions, ShowFunction, TextFormat, OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000403 else
404 return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
405 ShowFunction, OS);
406}
407
Justin Bogner618bcea2014-03-19 02:20:46 +0000408int main(int argc, const char *argv[]) {
409 // Print a stack trace if we signal out.
410 sys::PrintStackTraceOnErrorSignal();
411 PrettyStackTraceProgram X(argc, argv);
412 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
413
414 StringRef ProgName(sys::path::filename(argv[0]));
415 if (argc > 1) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000416 int (*func)(int, const char *[]) = nullptr;
Justin Bogner618bcea2014-03-19 02:20:46 +0000417
418 if (strcmp(argv[1], "merge") == 0)
419 func = merge_main;
Justin Bogner9af28ef2014-03-21 17:29:44 +0000420 else if (strcmp(argv[1], "show") == 0)
421 func = show_main;
Justin Bogner618bcea2014-03-19 02:20:46 +0000422
423 if (func) {
424 std::string Invocation(ProgName.str() + " " + argv[1]);
425 argv[1] = Invocation.c_str();
426 return func(argc - 1, argv + 1);
427 }
428
Diego Novillod3babdb2015-12-14 20:37:15 +0000429 if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0 ||
Justin Bogner618bcea2014-03-19 02:20:46 +0000430 strcmp(argv[1], "--help") == 0) {
431
432 errs() << "OVERVIEW: LLVM profile data tools\n\n"
433 << "USAGE: " << ProgName << " <command> [args...]\n"
434 << "USAGE: " << ProgName << " <command> -help\n\n"
Justin Bogner9af28ef2014-03-21 17:29:44 +0000435 << "Available commands: merge, show\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000436 return 0;
437 }
438 }
439
440 if (argc < 2)
441 errs() << ProgName << ": No command specified!\n";
442 else
443 errs() << ProgName << ": Unknown command!\n";
444
Justin Bogner9af28ef2014-03-21 17:29:44 +0000445 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000446 return 1;
447}