blob: c7760866b8096f585bac701d7bfb43855d094369 [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"
32#include "llvm/Support/raw_ostream.h"
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +000033#include <algorithm>
34#include <tuple>
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
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000051static void exitWithErrorCode(const std::error_code &Error,
52 StringRef Whence = "") {
Nathan Slingerland4f823662015-11-13 03:47:58 +000053 if (Error.category() == instrprof_category()) {
54 instrprof_error instrError = static_cast<instrprof_error>(Error.value());
55 if (instrError == instrprof_error::unrecognized_format) {
56 // Hint for common error of forgetting -sample for sample profiles.
57 exitWithError(Error.message(), Whence,
58 "Perhaps you forgot to use the -sample option?");
59 }
60 }
61 exitWithError(Error.message(), Whence);
62}
63
Duncan P. N. Exon Smith02b6fa92015-06-16 00:43:04 +000064namespace {
Diego Novillod3babdb2015-12-14 20:37:15 +000065enum ProfileKinds { instr, sample };
Duncan P. N. Exon Smith02b6fa92015-06-16 00:43:04 +000066}
Justin Bogner618bcea2014-03-19 02:20:46 +000067
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000068static void handleMergeWriterError(std::error_code &Error,
69 StringRef WhenceFile = "",
70 StringRef WhenceFunction = "",
Diego Novillod3babdb2015-12-14 20:37:15 +000071 bool ShowHint = true) {
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000072 if (!WhenceFile.empty())
73 errs() << WhenceFile << ": ";
74 if (!WhenceFunction.empty())
75 errs() << WhenceFunction << ": ";
76 errs() << Error.message() << "\n";
77
78 if (ShowHint) {
79 StringRef Hint = "";
80 if (Error.category() == instrprof_category()) {
81 instrprof_error instrError = static_cast<instrprof_error>(Error.value());
Nathan Slingerland11c938d12015-11-17 23:37:09 +000082 switch (instrError) {
83 case instrprof_error::hash_mismatch:
84 case instrprof_error::count_mismatch:
85 case instrprof_error::value_site_count_mismatch:
Diego Novillod3babdb2015-12-14 20:37:15 +000086 Hint = "Make sure that all profile data to be merged is generated "
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000087 "from the same binary.";
Nathan Slingerland11c938d12015-11-17 23:37:09 +000088 break;
Nathan Slingerlandb2d95f02015-11-18 00:52:45 +000089 default:
90 break;
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000091 }
92 }
93
94 if (!Hint.empty())
95 errs() << Hint << "\n";
96 }
97}
98
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +000099struct WeightedFile {
100 StringRef Filename;
101 uint64_t Weight;
102
103 WeightedFile() {}
104
105 WeightedFile(StringRef F, uint64_t W) : Filename{F}, Weight{W} {}
106};
107typedef SmallVector<WeightedFile, 5> WeightedFileVector;
108
109static void mergeInstrProfile(const WeightedFileVector &Inputs,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000110 StringRef OutputFilename,
Vedant Kumar00dab222016-01-29 22:54:45 +0000111 ProfileFormat OutputFormat, bool OutputSparse) {
Justin Bognerb7aa2632014-04-18 21:48:40 +0000112 if (OutputFilename.compare("-") == 0)
113 exitWithError("Cannot write indexed profdata format to stdout.");
Justin Bognerec49f982014-03-12 22:00:57 +0000114
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000115 if (OutputFormat != PF_Binary && OutputFormat != PF_Text)
116 exitWithError("Unknown format is specified.");
117
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000118 std::error_code EC;
119 raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None);
120 if (EC)
Nathan Slingerland4f823662015-11-13 03:47:58 +0000121 exitWithErrorCode(EC, OutputFilename);
Justin Bognerec49f982014-03-12 22:00:57 +0000122
Vedant Kumar00dab222016-01-29 22:54:45 +0000123 InstrProfWriter Writer(OutputSparse);
Nathan Slingerlandc21a44d2015-11-18 17:10:24 +0000124 SmallSet<std::error_code, 4> WriterErrorCodes;
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000125 for (const auto &Input : Inputs) {
126 auto ReaderOrErr = InstrProfReader::create(Input.Filename);
Diego Novillofcd55602014-11-03 00:51:45 +0000127 if (std::error_code ec = ReaderOrErr.getError())
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000128 exitWithErrorCode(ec, Input.Filename);
Justin Bognerf8d79192014-03-21 17:24:48 +0000129
Diego Novillofcd55602014-11-03 00:51:45 +0000130 auto Reader = std::move(ReaderOrErr.get());
Rong Xu33c76c02016-02-10 17:18:30 +0000131 bool IsIRProfile = Reader->isIRLevelProfile();
132 if (Writer.setIsIRLevelProfile(IsIRProfile))
133 exitWithError("Merge IR generated profile with Clang generated profile.");
134
Nathan Slingerlande6e30d52015-11-17 22:08:53 +0000135 for (auto &I : *Reader) {
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000136 if (std::error_code EC = Writer.addRecord(std::move(I), Input.Weight)) {
Nathan Slingerlande6e30d52015-11-17 22:08:53 +0000137 // Only show hint the first time an error occurs.
138 bool firstTime = WriterErrorCodes.insert(EC).second;
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000139 handleMergeWriterError(EC, Input.Filename, I.Name, firstTime);
Nathan Slingerlande6e30d52015-11-17 22:08:53 +0000140 }
141 }
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000142 if (Reader->hasError())
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000143 exitWithErrorCode(Reader->getError(), Input.Filename);
Justin Bognerbfee8d42014-03-12 20:14:17 +0000144 }
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000145 if (OutputFormat == PF_Text)
146 Writer.writeText(Output);
147 else
148 Writer.write(Output);
Diego Novillod5336ae2014-11-01 00:56:55 +0000149}
150
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000151static sampleprof::SampleProfileFormat FormatMap[] = {
152 sampleprof::SPF_None, sampleprof::SPF_Text, sampleprof::SPF_Binary,
153 sampleprof::SPF_GCC};
154
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000155static void mergeSampleProfile(const WeightedFileVector &Inputs,
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000156 StringRef OutputFilename,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000157 ProfileFormat OutputFormat) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000158 using namespace sampleprof;
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000159 auto WriterOrErr =
160 SampleProfileWriter::create(OutputFilename, FormatMap[OutputFormat]);
Diego Novillofcd55602014-11-03 00:51:45 +0000161 if (std::error_code EC = WriterOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000162 exitWithErrorCode(EC, OutputFilename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000163
Diego Novillofcd55602014-11-03 00:51:45 +0000164 auto Writer = std::move(WriterOrErr.get());
Diego Novillod5336ae2014-11-01 00:56:55 +0000165 StringMap<FunctionSamples> ProfileMap;
Diego Novilloaae1ed82015-10-08 19:40:37 +0000166 SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers;
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000167 for (const auto &Input : Inputs) {
Diego Novillofcd55602014-11-03 00:51:45 +0000168 auto ReaderOrErr =
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000169 SampleProfileReader::create(Input.Filename, getGlobalContext());
Diego Novillofcd55602014-11-03 00:51:45 +0000170 if (std::error_code EC = ReaderOrErr.getError())
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000171 exitWithErrorCode(EC, Input.Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000172
Diego Novilloaae1ed82015-10-08 19:40:37 +0000173 // We need to keep the readers around until after all the files are
174 // read so that we do not lose the function names stored in each
175 // reader's memory. The function names are needed to write out the
176 // merged profile map.
177 Readers.push_back(std::move(ReaderOrErr.get()));
178 const auto Reader = Readers.back().get();
Diego Novillod5336ae2014-11-01 00:56:55 +0000179 if (std::error_code EC = Reader->read())
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000180 exitWithErrorCode(EC, Input.Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000181
182 StringMap<FunctionSamples> &Profiles = Reader->getProfiles();
183 for (StringMap<FunctionSamples>::iterator I = Profiles.begin(),
184 E = Profiles.end();
185 I != E; ++I) {
186 StringRef FName = I->first();
187 FunctionSamples &Samples = I->second;
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000188 sampleprof_error Result = ProfileMap[FName].merge(Samples, Input.Weight);
189 if (Result != sampleprof_error::success) {
190 std::error_code EC = make_error_code(Result);
191 handleMergeWriterError(EC, Input.Filename, FName);
192 }
Diego Novillod5336ae2014-11-01 00:56:55 +0000193 }
194 }
195 Writer->write(ProfileMap);
196}
197
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000198static WeightedFile parseWeightedFile(const StringRef &WeightedFilename) {
199 StringRef WeightStr, FileName;
200 std::tie(WeightStr, FileName) = WeightedFilename.split(',');
Diego Novillod5336ae2014-11-01 00:56:55 +0000201
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000202 uint64_t Weight;
203 if (WeightStr.getAsInteger(10, Weight) || Weight < 1)
204 exitWithError("Input weight must be a positive integer.");
205
206 if (!sys::fs::exists(FileName))
207 exitWithErrorCode(make_error_code(errc::no_such_file_or_directory),
208 FileName);
209
210 return WeightedFile(FileName, Weight);
211}
212
213static int merge_main(int argc, const char *argv[]) {
214 cl::list<std::string> InputFilenames(cl::Positional,
215 cl::desc("<filename...>"));
216 cl::list<std::string> WeightedInputFilenames("weighted-input",
217 cl::desc("<weight>,<filename>"));
Diego Novillod5336ae2014-11-01 00:56:55 +0000218 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
219 cl::init("-"), cl::Required,
220 cl::desc("Output file"));
221 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
222 cl::aliasopt(OutputFilename));
223 cl::opt<ProfileKinds> ProfileKind(
224 cl::desc("Profile kind:"), cl::init(instr),
225 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
226 clEnumVal(sample, "Sample profile"), clEnumValEnd));
227
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000228 cl::opt<ProfileFormat> OutputFormat(
229 cl::desc("Format of output profile"), cl::init(PF_Binary),
230 cl::values(clEnumValN(PF_Binary, "binary", "Binary encoding (default)"),
231 clEnumValN(PF_Text, "text", "Text encoding"),
232 clEnumValN(PF_GCC, "gcc",
233 "GCC encoding (only meaningful for -sample)"),
Diego Novillod5336ae2014-11-01 00:56:55 +0000234 clEnumValEnd));
235
Vedant Kumar00dab222016-01-29 22:54:45 +0000236 cl::opt<bool> OutputSparse("sparse", cl::init(false),
237 cl::desc("Generate a sparse profile (only meaningful for -instr)"));
238
Diego Novillod5336ae2014-11-01 00:56:55 +0000239 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
240
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000241 if (InputFilenames.empty() && WeightedInputFilenames.empty())
242 exitWithError("No input files specified. See " +
243 sys::path::filename(argv[0]) + " -help");
244
245 WeightedFileVector WeightedInputs;
246 for (StringRef Filename : InputFilenames)
247 WeightedInputs.push_back(WeightedFile(Filename, 1));
248 for (StringRef WeightedFilename : WeightedInputFilenames)
249 WeightedInputs.push_back(parseWeightedFile(WeightedFilename));
250
Diego Novillod5336ae2014-11-01 00:56:55 +0000251 if (ProfileKind == instr)
Vedant Kumar00dab222016-01-29 22:54:45 +0000252 mergeInstrProfile(WeightedInputs, OutputFilename, OutputFormat,
253 OutputSparse);
Diego Novillod5336ae2014-11-01 00:56:55 +0000254 else
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000255 mergeSampleProfile(WeightedInputs, OutputFilename, OutputFormat);
Justin Bognerbfee8d42014-03-12 20:14:17 +0000256
Justin Bognerec49f982014-03-12 22:00:57 +0000257 return 0;
Justin Bognerbfee8d42014-03-12 20:14:17 +0000258}
Justin Bogner618bcea2014-03-19 02:20:46 +0000259
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000260static int showInstrProfile(std::string Filename, bool ShowCounts,
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000261 bool ShowIndirectCallTargets,
262 bool ShowDetailedSummary,
263 std::vector<uint32_t> DetailedSummaryCutoffs,
264 bool ShowAllFunctions, std::string ShowFunction,
265 bool TextFormat, raw_fd_ostream &OS) {
Diego Novillofcd55602014-11-03 00:51:45 +0000266 auto ReaderOrErr = InstrProfReader::create(Filename);
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000267 std::vector<uint32_t> Cutoffs(DetailedSummaryCutoffs);
268 if (ShowDetailedSummary && DetailedSummaryCutoffs.empty()) {
269 Cutoffs = {800000, 900000, 950000, 990000, 999000, 999900, 999990};
270 }
271 ProfileSummary PS(Cutoffs);
Diego Novillofcd55602014-11-03 00:51:45 +0000272 if (std::error_code EC = ReaderOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000273 exitWithErrorCode(EC, Filename);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000274
Diego Novillofcd55602014-11-03 00:51:45 +0000275 auto Reader = std::move(ReaderOrErr.get());
Rong Xu33c76c02016-02-10 17:18:30 +0000276 bool IsIRInstr = Reader->isIRLevelProfile();
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000277 size_t ShownFunctions = 0;
Justin Bogner9af28ef2014-03-21 17:29:44 +0000278 for (const auto &Func : *Reader) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000279 bool Show =
280 ShowAllFunctions || (!ShowFunction.empty() &&
281 Func.Name.find(ShowFunction) != Func.Name.npos);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000282
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000283 bool doTextFormatDump = (Show && ShowCounts && TextFormat);
284
285 if (doTextFormatDump) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000286 InstrProfSymtab &Symtab = Reader->getSymtab();
287 InstrProfWriter::writeRecordInText(Func, Symtab, OS);
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000288 continue;
289 }
290
Justin Bognerb59d7c72014-04-25 02:45:33 +0000291 assert(Func.Counts.size() > 0 && "function missing entry counter");
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000292 PS.addRecord(Func);
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000293
Justin Bogner9af28ef2014-03-21 17:29:44 +0000294 if (Show) {
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000295
Justin Bogner9af28ef2014-03-21 17:29:44 +0000296 if (!ShownFunctions)
297 OS << "Counters:\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000298
Justin Bogner9af28ef2014-03-21 17:29:44 +0000299 ++ShownFunctions;
300
301 OS << " " << Func.Name << ":\n"
Justin Bogner423380f2014-03-23 20:43:50 +0000302 << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
Rong Xu33c76c02016-02-10 17:18:30 +0000303 << " Counters: " << Func.Counts.size() << "\n";
304 if (!IsIRInstr)
305 OS << " Function count: " << Func.Counts[0] << "\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000306
Justin Bogner9e9a0572015-09-29 22:13:58 +0000307 if (ShowIndirectCallTargets)
Xinliang David Li2004f002015-11-02 05:08:23 +0000308 OS << " Indirect Call Site Count: "
309 << Func.getNumValueSites(IPVK_IndirectCallTarget) << "\n";
Justin Bogner9af28ef2014-03-21 17:29:44 +0000310
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000311 if (ShowCounts) {
312 OS << " Block counts: [";
Rong Xu33c76c02016-02-10 17:18:30 +0000313 size_t Start = (IsIRInstr ? 0 : 1);
314 for (size_t I = Start, E = Func.Counts.size(); I < E; ++I) {
315 OS << (I == Start ? "" : ", ") << Func.Counts[I];
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000316 }
317 OS << "]\n";
318 }
Justin Bogner9e9a0572015-09-29 22:13:58 +0000319
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000320 if (ShowIndirectCallTargets) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000321 InstrProfSymtab &Symtab = Reader->getSymtab();
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000322 uint32_t NS = Func.getNumValueSites(IPVK_IndirectCallTarget);
323 OS << " Indirect Target Results: \n";
324 for (size_t I = 0; I < NS; ++I) {
325 uint32_t NV = Func.getNumValueDataForSite(IPVK_IndirectCallTarget, I);
326 std::unique_ptr<InstrProfValueData[]> VD =
327 Func.getValueForSite(IPVK_IndirectCallTarget, I);
328 for (uint32_t V = 0; V < NV; V++) {
329 OS << "\t[ " << I << ", ";
Xinliang David Lia716cc52015-12-20 06:22:13 +0000330 OS << Symtab.getFuncName(VD[V].Value) << ", " << VD[V].Count
331 << " ]\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000332 }
Justin Bogner9e9a0572015-09-29 22:13:58 +0000333 }
334 }
335 }
Justin Bogner9af28ef2014-03-21 17:29:44 +0000336 }
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000337
Justin Bognerdb1225d2014-03-23 20:55:53 +0000338 if (Reader->hasError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000339 exitWithErrorCode(Reader->getError(), Filename);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000340
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000341 if (ShowCounts && TextFormat)
342 return 0;
343
Justin Bogner9af28ef2014-03-21 17:29:44 +0000344 if (ShowAllFunctions || !ShowFunction.empty())
345 OS << "Functions shown: " << ShownFunctions << "\n";
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000346 OS << "Total functions: " << PS.getNumFunctions() << "\n";
347 OS << "Maximum function count: " << PS.getMaxFunctionCount() << "\n";
Xinliang David Li6ed987d2016-01-16 05:29:49 +0000348 OS << "Maximum internal block count: " << PS.getMaxInternalBlockCount() << "\n";
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000349
350 if (ShowDetailedSummary) {
351 OS << "Detailed summary:\n";
352 OS << "Total number of blocks: " << PS.getNumBlocks() << "\n";
353 OS << "Total count: " << PS.getTotalCount() << "\n";
354 for (auto Entry : PS.getDetailedSummary()) {
355 OS << Entry.NumBlocks << " blocks with count >= " << Entry.MinBlockCount
356 << " account for "
357 << format("%0.6g", (float)Entry.Cutoff / ProfileSummary::Scale * 100)
358 << " percentage of the total counts.\n";
359 }
360 }
Justin Bogner9af28ef2014-03-21 17:29:44 +0000361 return 0;
362}
363
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000364static int showSampleProfile(std::string Filename, bool ShowCounts,
365 bool ShowAllFunctions, std::string ShowFunction,
366 raw_fd_ostream &OS) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000367 using namespace sampleprof;
Diego Novillofcd55602014-11-03 00:51:45 +0000368 auto ReaderOrErr = SampleProfileReader::create(Filename, getGlobalContext());
369 if (std::error_code EC = ReaderOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000370 exitWithErrorCode(EC, Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000371
Diego Novillofcd55602014-11-03 00:51:45 +0000372 auto Reader = std::move(ReaderOrErr.get());
Diego Novilloc6d032a2015-09-17 00:17:21 +0000373 if (std::error_code EC = Reader->read())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000374 exitWithErrorCode(EC, Filename);
Diego Novilloc6d032a2015-09-17 00:17:21 +0000375
Diego Novillod5336ae2014-11-01 00:56:55 +0000376 if (ShowAllFunctions || ShowFunction.empty())
377 Reader->dump(OS);
378 else
379 Reader->dumpFunctionProfile(ShowFunction, OS);
380
381 return 0;
382}
383
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000384static int show_main(int argc, const char *argv[]) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000385 cl::opt<std::string> Filename(cl::Positional, cl::Required,
386 cl::desc("<profdata-file>"));
387
388 cl::opt<bool> ShowCounts("counts", cl::init(false),
389 cl::desc("Show counter values for shown functions"));
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000390 cl::opt<bool> TextFormat(
391 "text", cl::init(false),
392 cl::desc("Show instr profile data in text dump format"));
Justin Bogner9e9a0572015-09-29 22:13:58 +0000393 cl::opt<bool> ShowIndirectCallTargets(
394 "ic-targets", cl::init(false),
395 cl::desc("Show indirect call site target values for shown functions"));
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000396 cl::opt<bool> ShowDetailedSummary("detailed-summary", cl::init(false),
397 cl::desc("Show detailed profile summary"));
398 cl::list<uint32_t> DetailedSummaryCutoffs(
399 cl::CommaSeparated, "detailed-summary-cutoffs",
400 cl::desc(
401 "Cutoff percentages (times 10000) for generating detailed summary"),
402 cl::value_desc("800000,901000,999999"));
Diego Novillod5336ae2014-11-01 00:56:55 +0000403 cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
404 cl::desc("Details for every function"));
405 cl::opt<std::string> ShowFunction("function",
406 cl::desc("Details for matching functions"));
407
408 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
409 cl::init("-"), cl::desc("Output file"));
410 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
411 cl::aliasopt(OutputFilename));
412 cl::opt<ProfileKinds> ProfileKind(
413 cl::desc("Profile kind:"), cl::init(instr),
414 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
415 clEnumVal(sample, "Sample profile"), clEnumValEnd));
416
417 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
418
419 if (OutputFilename.empty())
420 OutputFilename = "-";
421
422 std::error_code EC;
423 raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text);
424 if (EC)
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000425 exitWithErrorCode(EC, OutputFilename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000426
427 if (ShowAllFunctions && !ShowFunction.empty())
428 errs() << "warning: -function argument ignored: showing all functions\n";
429
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000430 std::vector<uint32_t> Cutoffs(DetailedSummaryCutoffs.begin(),
431 DetailedSummaryCutoffs.end());
Diego Novillod5336ae2014-11-01 00:56:55 +0000432 if (ProfileKind == instr)
Justin Bogner9e9a0572015-09-29 22:13:58 +0000433 return showInstrProfile(Filename, ShowCounts, ShowIndirectCallTargets,
Easwaran Raman183ebbe2016-01-13 21:44:36 +0000434 ShowDetailedSummary, DetailedSummaryCutoffs,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000435 ShowAllFunctions, ShowFunction, TextFormat, OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000436 else
437 return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
438 ShowFunction, OS);
439}
440
Justin Bogner618bcea2014-03-19 02:20:46 +0000441int main(int argc, const char *argv[]) {
442 // Print a stack trace if we signal out.
443 sys::PrintStackTraceOnErrorSignal();
444 PrettyStackTraceProgram X(argc, argv);
445 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
446
447 StringRef ProgName(sys::path::filename(argv[0]));
448 if (argc > 1) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000449 int (*func)(int, const char *[]) = nullptr;
Justin Bogner618bcea2014-03-19 02:20:46 +0000450
451 if (strcmp(argv[1], "merge") == 0)
452 func = merge_main;
Justin Bogner9af28ef2014-03-21 17:29:44 +0000453 else if (strcmp(argv[1], "show") == 0)
454 func = show_main;
Justin Bogner618bcea2014-03-19 02:20:46 +0000455
456 if (func) {
457 std::string Invocation(ProgName.str() + " " + argv[1]);
458 argv[1] = Invocation.c_str();
459 return func(argc - 1, argv + 1);
460 }
461
Diego Novillod3babdb2015-12-14 20:37:15 +0000462 if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0 ||
Justin Bogner618bcea2014-03-19 02:20:46 +0000463 strcmp(argv[1], "--help") == 0) {
464
465 errs() << "OVERVIEW: LLVM profile data tools\n\n"
466 << "USAGE: " << ProgName << " <command> [args...]\n"
467 << "USAGE: " << ProgName << " <command> -help\n\n"
Justin Bogner9af28ef2014-03-21 17:29:44 +0000468 << "Available commands: merge, show\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000469 return 0;
470 }
471 }
472
473 if (argc < 2)
474 errs() << ProgName << ": No command specified!\n";
475 else
476 errs() << ProgName << ": Unknown command!\n";
477
Justin Bogner9af28ef2014-03-21 17:29:44 +0000478 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000479 return 1;
480}