blob: 52488af1c919c8fea4f3e803847eb813bb4b6436 [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
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000014#include "llvm/ADT/StringRef.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000015#include "llvm/IR/LLVMContext.h"
Justin Bognerf8d79192014-03-21 17:24:48 +000016#include "llvm/ProfileData/InstrProfReader.h"
Justin Bognerb9bd7f82014-03-21 17:46:22 +000017#include "llvm/ProfileData/InstrProfWriter.h"
Diego Novillod5336ae2014-11-01 00:56:55 +000018#include "llvm/ProfileData/SampleProfReader.h"
19#include "llvm/ProfileData/SampleProfWriter.h"
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000020#include "llvm/Support/CommandLine.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000021#include "llvm/Support/FileSystem.h"
Justin Bogner423380f2014-03-23 20:43:50 +000022#include "llvm/Support/Format.h"
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000023#include "llvm/Support/ManagedStatic.h"
24#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000025#include "llvm/Support/Path.h"
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000026#include "llvm/Support/PrettyStackTrace.h"
27#include "llvm/Support/Signals.h"
28#include "llvm/Support/raw_ostream.h"
29
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000030#include <set>
31
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000032using namespace llvm;
33
Nathan Slingerland4f823662015-11-13 03:47:58 +000034static void exitWithError(const Twine &Message,
35 StringRef Whence = "",
36 StringRef Hint = "") {
Justin Bognerf8d79192014-03-21 17:24:48 +000037 errs() << "error: ";
38 if (!Whence.empty())
39 errs() << Whence << ": ";
40 errs() << Message << "\n";
Nathan Slingerland4f823662015-11-13 03:47:58 +000041 if (!Hint.empty())
42 errs() << Hint << "\n";
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000043 ::exit(1);
44}
45
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000046static void exitWithErrorCode(const std::error_code &Error,
47 StringRef Whence = "") {
Nathan Slingerland4f823662015-11-13 03:47:58 +000048 if (Error.category() == instrprof_category()) {
49 instrprof_error instrError = static_cast<instrprof_error>(Error.value());
50 if (instrError == instrprof_error::unrecognized_format) {
51 // Hint for common error of forgetting -sample for sample profiles.
52 exitWithError(Error.message(), Whence,
53 "Perhaps you forgot to use the -sample option?");
54 }
55 }
56 exitWithError(Error.message(), Whence);
57}
58
Duncan P. N. Exon Smith02b6fa92015-06-16 00:43:04 +000059namespace {
Nathan Slingerland4f823662015-11-13 03:47:58 +000060 enum ProfileKinds { instr, sample };
Duncan P. N. Exon Smith02b6fa92015-06-16 00:43:04 +000061}
Justin Bogner618bcea2014-03-19 02:20:46 +000062
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000063static void handleMergeWriterError(std::error_code &Error,
64 StringRef WhenceFile = "",
65 StringRef WhenceFunction = "",
66 bool ShowHint = true)
67{
68 if (!WhenceFile.empty())
69 errs() << WhenceFile << ": ";
70 if (!WhenceFunction.empty())
71 errs() << WhenceFunction << ": ";
72 errs() << Error.message() << "\n";
73
74 if (ShowHint) {
75 StringRef Hint = "";
76 if (Error.category() == instrprof_category()) {
77 instrprof_error instrError = static_cast<instrprof_error>(Error.value());
Nathan Slingerland11c938d12015-11-17 23:37:09 +000078 switch (instrError) {
79 case instrprof_error::hash_mismatch:
80 case instrprof_error::count_mismatch:
81 case instrprof_error::value_site_count_mismatch:
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000082 Hint = "Make sure that all profile data to be merged is generated " \
83 "from the same binary.";
Nathan Slingerland11c938d12015-11-17 23:37:09 +000084 break;
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000085 }
86 }
87
88 if (!Hint.empty())
89 errs() << Hint << "\n";
90 }
91}
92
Benjamin Kramerf044d3f2015-03-09 16:23:46 +000093static void mergeInstrProfile(const cl::list<std::string> &Inputs,
94 StringRef OutputFilename) {
Justin Bognerb7aa2632014-04-18 21:48:40 +000095 if (OutputFilename.compare("-") == 0)
96 exitWithError("Cannot write indexed profdata format to stdout.");
Justin Bognerec49f982014-03-12 22:00:57 +000097
Rafael Espindola3fd1e992014-08-25 18:16:47 +000098 std::error_code EC;
99 raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None);
100 if (EC)
Nathan Slingerland4f823662015-11-13 03:47:58 +0000101 exitWithErrorCode(EC, OutputFilename);
Justin Bognerec49f982014-03-12 22:00:57 +0000102
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000103 InstrProfWriter Writer;
Nathan Slingerlande6e30d52015-11-17 22:08:53 +0000104 std::set<std::error_code> WriterErrorCodes;
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000105 for (const auto &Filename : Inputs) {
Diego Novillofcd55602014-11-03 00:51:45 +0000106 auto ReaderOrErr = InstrProfReader::create(Filename);
107 if (std::error_code ec = ReaderOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000108 exitWithErrorCode(ec, Filename);
Justin Bognerf8d79192014-03-21 17:24:48 +0000109
Diego Novillofcd55602014-11-03 00:51:45 +0000110 auto Reader = std::move(ReaderOrErr.get());
Nathan Slingerlande6e30d52015-11-17 22:08:53 +0000111 for (auto &I : *Reader) {
112 if (std::error_code EC = Writer.addRecord(std::move(I))) {
113 // Only show hint the first time an error occurs.
114 bool firstTime = WriterErrorCodes.insert(EC).second;
115 handleMergeWriterError(EC, Filename, I.Name, firstTime);
116 }
117 }
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000118 if (Reader->hasError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000119 exitWithErrorCode(Reader->getError(), Filename);
Justin Bognerbfee8d42014-03-12 20:14:17 +0000120 }
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000121 Writer.write(Output);
Diego Novillod5336ae2014-11-01 00:56:55 +0000122}
123
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000124static void mergeSampleProfile(const cl::list<std::string> &Inputs,
125 StringRef OutputFilename,
126 sampleprof::SampleProfileFormat OutputFormat) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000127 using namespace sampleprof;
Diego Novillofcd55602014-11-03 00:51:45 +0000128 auto WriterOrErr = SampleProfileWriter::create(OutputFilename, OutputFormat);
129 if (std::error_code EC = WriterOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000130 exitWithErrorCode(EC, OutputFilename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000131
Diego Novillofcd55602014-11-03 00:51:45 +0000132 auto Writer = std::move(WriterOrErr.get());
Diego Novillod5336ae2014-11-01 00:56:55 +0000133 StringMap<FunctionSamples> ProfileMap;
Diego Novilloaae1ed82015-10-08 19:40:37 +0000134 SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers;
Diego Novillod5336ae2014-11-01 00:56:55 +0000135 for (const auto &Filename : Inputs) {
Diego Novillofcd55602014-11-03 00:51:45 +0000136 auto ReaderOrErr =
137 SampleProfileReader::create(Filename, getGlobalContext());
138 if (std::error_code EC = ReaderOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000139 exitWithErrorCode(EC, Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000140
Diego Novilloaae1ed82015-10-08 19:40:37 +0000141 // We need to keep the readers around until after all the files are
142 // read so that we do not lose the function names stored in each
143 // reader's memory. The function names are needed to write out the
144 // merged profile map.
145 Readers.push_back(std::move(ReaderOrErr.get()));
146 const auto Reader = Readers.back().get();
Diego Novillod5336ae2014-11-01 00:56:55 +0000147 if (std::error_code EC = Reader->read())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000148 exitWithErrorCode(EC, Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000149
150 StringMap<FunctionSamples> &Profiles = Reader->getProfiles();
151 for (StringMap<FunctionSamples>::iterator I = Profiles.begin(),
152 E = Profiles.end();
153 I != E; ++I) {
154 StringRef FName = I->first();
155 FunctionSamples &Samples = I->second;
156 ProfileMap[FName].merge(Samples);
157 }
158 }
159 Writer->write(ProfileMap);
160}
161
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000162static int merge_main(int argc, const char *argv[]) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000163 cl::list<std::string> Inputs(cl::Positional, cl::Required, cl::OneOrMore,
164 cl::desc("<filenames...>"));
165
166 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
167 cl::init("-"), cl::Required,
168 cl::desc("Output file"));
169 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
170 cl::aliasopt(OutputFilename));
171 cl::opt<ProfileKinds> ProfileKind(
172 cl::desc("Profile kind:"), cl::init(instr),
173 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
174 clEnumVal(sample, "Sample profile"), clEnumValEnd));
175
176 cl::opt<sampleprof::SampleProfileFormat> OutputFormat(
177 cl::desc("Format of output profile (only meaningful with --sample)"),
178 cl::init(sampleprof::SPF_Binary),
179 cl::values(clEnumValN(sampleprof::SPF_Binary, "binary",
180 "Binary encoding (default)"),
181 clEnumValN(sampleprof::SPF_Text, "text", "Text encoding"),
182 clEnumValN(sampleprof::SPF_GCC, "gcc", "GCC encoding"),
183 clEnumValEnd));
184
185 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
186
187 if (ProfileKind == instr)
188 mergeInstrProfile(Inputs, OutputFilename);
189 else
190 mergeSampleProfile(Inputs, OutputFilename, OutputFormat);
Justin Bognerbfee8d42014-03-12 20:14:17 +0000191
Justin Bognerec49f982014-03-12 22:00:57 +0000192 return 0;
Justin Bognerbfee8d42014-03-12 20:14:17 +0000193}
Justin Bogner618bcea2014-03-19 02:20:46 +0000194
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000195static int showInstrProfile(std::string Filename, bool ShowCounts,
Justin Bogner9e9a0572015-09-29 22:13:58 +0000196 bool ShowIndirectCallTargets, bool ShowAllFunctions,
197 std::string ShowFunction, raw_fd_ostream &OS) {
Diego Novillofcd55602014-11-03 00:51:45 +0000198 auto ReaderOrErr = InstrProfReader::create(Filename);
199 if (std::error_code EC = ReaderOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000200 exitWithErrorCode(EC, Filename);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000201
Diego Novillofcd55602014-11-03 00:51:45 +0000202 auto Reader = std::move(ReaderOrErr.get());
Justin Bogner9af28ef2014-03-21 17:29:44 +0000203 uint64_t MaxFunctionCount = 0, MaxBlockCount = 0;
204 size_t ShownFunctions = 0, TotalFunctions = 0;
205 for (const auto &Func : *Reader) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000206 bool Show =
207 ShowAllFunctions || (!ShowFunction.empty() &&
208 Func.Name.find(ShowFunction) != Func.Name.npos);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000209
210 ++TotalFunctions;
Justin Bognerb59d7c72014-04-25 02:45:33 +0000211 assert(Func.Counts.size() > 0 && "function missing entry counter");
Justin Bogner9af28ef2014-03-21 17:29:44 +0000212 if (Func.Counts[0] > MaxFunctionCount)
213 MaxFunctionCount = Func.Counts[0];
214
215 if (Show) {
216 if (!ShownFunctions)
217 OS << "Counters:\n";
218 ++ShownFunctions;
219
220 OS << " " << Func.Name << ":\n"
Justin Bogner423380f2014-03-23 20:43:50 +0000221 << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
Justin Bogner9af28ef2014-03-21 17:29:44 +0000222 << " Counters: " << Func.Counts.size() << "\n"
223 << " Function count: " << Func.Counts[0] << "\n";
Justin Bogner9e9a0572015-09-29 22:13:58 +0000224 if (ShowIndirectCallTargets)
Xinliang David Li2004f002015-11-02 05:08:23 +0000225 OS << " Indirect Call Site Count: "
226 << Func.getNumValueSites(IPVK_IndirectCallTarget) << "\n";
Justin Bogner9af28ef2014-03-21 17:29:44 +0000227 }
228
229 if (Show && ShowCounts)
230 OS << " Block counts: [";
231 for (size_t I = 1, E = Func.Counts.size(); I < E; ++I) {
232 if (Func.Counts[I] > MaxBlockCount)
233 MaxBlockCount = Func.Counts[I];
234 if (Show && ShowCounts)
235 OS << (I == 1 ? "" : ", ") << Func.Counts[I];
236 }
237 if (Show && ShowCounts)
238 OS << "]\n";
Justin Bogner9e9a0572015-09-29 22:13:58 +0000239
240 if (Show && ShowIndirectCallTargets) {
Xinliang David Li2004f002015-11-02 05:08:23 +0000241 uint32_t NS = Func.getNumValueSites(IPVK_IndirectCallTarget);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000242 OS << " Indirect Target Results: \n";
Xinliang David Li2004f002015-11-02 05:08:23 +0000243 for (size_t I = 0; I < NS; ++I) {
244 uint32_t NV = Func.getNumValueDataForSite(IPVK_IndirectCallTarget, I);
245 std::unique_ptr<InstrProfValueData[]> VD =
246 Func.getValueForSite(IPVK_IndirectCallTarget, I);
247 for (uint32_t V = 0; V < NV; V++) {
Justin Bogner9e9a0572015-09-29 22:13:58 +0000248 OS << "\t[ " << I << ", ";
Xinliang David Li2004f002015-11-02 05:08:23 +0000249 OS << (const char *)VD[V].Value << ", " << VD[V].Count << " ]\n";
Justin Bogner9e9a0572015-09-29 22:13:58 +0000250 }
251 }
252 }
Justin Bogner9af28ef2014-03-21 17:29:44 +0000253 }
Justin Bognerdb1225d2014-03-23 20:55:53 +0000254 if (Reader->hasError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000255 exitWithErrorCode(Reader->getError(), Filename);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000256
257 if (ShowAllFunctions || !ShowFunction.empty())
258 OS << "Functions shown: " << ShownFunctions << "\n";
259 OS << "Total functions: " << TotalFunctions << "\n";
260 OS << "Maximum function count: " << MaxFunctionCount << "\n";
261 OS << "Maximum internal block count: " << MaxBlockCount << "\n";
262 return 0;
263}
264
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000265static int showSampleProfile(std::string Filename, bool ShowCounts,
266 bool ShowAllFunctions, std::string ShowFunction,
267 raw_fd_ostream &OS) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000268 using namespace sampleprof;
Diego Novillofcd55602014-11-03 00:51:45 +0000269 auto ReaderOrErr = SampleProfileReader::create(Filename, getGlobalContext());
270 if (std::error_code EC = ReaderOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000271 exitWithErrorCode(EC, Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000272
Diego Novillofcd55602014-11-03 00:51:45 +0000273 auto Reader = std::move(ReaderOrErr.get());
Diego Novilloc6d032a2015-09-17 00:17:21 +0000274 if (std::error_code EC = Reader->read())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000275 exitWithErrorCode(EC, Filename);
Diego Novilloc6d032a2015-09-17 00:17:21 +0000276
Diego Novillod5336ae2014-11-01 00:56:55 +0000277 if (ShowAllFunctions || ShowFunction.empty())
278 Reader->dump(OS);
279 else
280 Reader->dumpFunctionProfile(ShowFunction, OS);
281
282 return 0;
283}
284
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000285static int show_main(int argc, const char *argv[]) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000286 cl::opt<std::string> Filename(cl::Positional, cl::Required,
287 cl::desc("<profdata-file>"));
288
289 cl::opt<bool> ShowCounts("counts", cl::init(false),
290 cl::desc("Show counter values for shown functions"));
Justin Bogner9e9a0572015-09-29 22:13:58 +0000291 cl::opt<bool> ShowIndirectCallTargets(
292 "ic-targets", cl::init(false),
293 cl::desc("Show indirect call site target values for shown functions"));
Diego Novillod5336ae2014-11-01 00:56:55 +0000294 cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
295 cl::desc("Details for every function"));
296 cl::opt<std::string> ShowFunction("function",
297 cl::desc("Details for matching functions"));
298
299 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
300 cl::init("-"), cl::desc("Output file"));
301 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
302 cl::aliasopt(OutputFilename));
303 cl::opt<ProfileKinds> ProfileKind(
304 cl::desc("Profile kind:"), cl::init(instr),
305 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
306 clEnumVal(sample, "Sample profile"), clEnumValEnd));
307
308 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
309
310 if (OutputFilename.empty())
311 OutputFilename = "-";
312
313 std::error_code EC;
314 raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text);
315 if (EC)
Nathan Slingerland4f823662015-11-13 03:47:58 +0000316 exitWithErrorCode(EC, OutputFilename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000317
318 if (ShowAllFunctions && !ShowFunction.empty())
319 errs() << "warning: -function argument ignored: showing all functions\n";
320
321 if (ProfileKind == instr)
Justin Bogner9e9a0572015-09-29 22:13:58 +0000322 return showInstrProfile(Filename, ShowCounts, ShowIndirectCallTargets,
323 ShowAllFunctions, ShowFunction, OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000324 else
325 return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
326 ShowFunction, OS);
327}
328
Justin Bogner618bcea2014-03-19 02:20:46 +0000329int main(int argc, const char *argv[]) {
330 // Print a stack trace if we signal out.
331 sys::PrintStackTraceOnErrorSignal();
332 PrettyStackTraceProgram X(argc, argv);
333 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
334
335 StringRef ProgName(sys::path::filename(argv[0]));
336 if (argc > 1) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000337 int (*func)(int, const char *[]) = nullptr;
Justin Bogner618bcea2014-03-19 02:20:46 +0000338
339 if (strcmp(argv[1], "merge") == 0)
340 func = merge_main;
Justin Bogner9af28ef2014-03-21 17:29:44 +0000341 else if (strcmp(argv[1], "show") == 0)
342 func = show_main;
Justin Bogner618bcea2014-03-19 02:20:46 +0000343
344 if (func) {
345 std::string Invocation(ProgName.str() + " " + argv[1]);
346 argv[1] = Invocation.c_str();
347 return func(argc - 1, argv + 1);
348 }
349
350 if (strcmp(argv[1], "-h") == 0 ||
351 strcmp(argv[1], "-help") == 0 ||
352 strcmp(argv[1], "--help") == 0) {
353
354 errs() << "OVERVIEW: LLVM profile data tools\n\n"
355 << "USAGE: " << ProgName << " <command> [args...]\n"
356 << "USAGE: " << ProgName << " <command> -help\n\n"
Justin Bogner9af28ef2014-03-21 17:29:44 +0000357 << "Available commands: merge, show\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000358 return 0;
359 }
360 }
361
362 if (argc < 2)
363 errs() << ProgName << ": No command specified!\n";
364 else
365 errs() << ProgName << ": Unknown command!\n";
366
Justin Bogner9af28ef2014-03-21 17:29:44 +0000367 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000368 return 1;
369}