blob: 9669c7dfa45f332ddd85891750eba2923cc5b4e3 [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"
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000015#include "llvm/ADT/StringRef.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000016#include "llvm/IR/LLVMContext.h"
Justin Bognerf8d79192014-03-21 17:24:48 +000017#include "llvm/ProfileData/InstrProfReader.h"
Justin Bognerb9bd7f82014-03-21 17:46:22 +000018#include "llvm/ProfileData/InstrProfWriter.h"
Diego Novillod5336ae2014-11-01 00:56:55 +000019#include "llvm/ProfileData/SampleProfReader.h"
20#include "llvm/ProfileData/SampleProfWriter.h"
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000021#include "llvm/Support/CommandLine.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000022#include "llvm/Support/FileSystem.h"
Justin Bogner423380f2014-03-23 20:43:50 +000023#include "llvm/Support/Format.h"
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000024#include "llvm/Support/ManagedStatic.h"
25#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000026#include "llvm/Support/Path.h"
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000027#include "llvm/Support/PrettyStackTrace.h"
28#include "llvm/Support/Signals.h"
29#include "llvm/Support/raw_ostream.h"
30
31using namespace llvm;
32
Nathan Slingerland4f823662015-11-13 03:47:58 +000033static void exitWithError(const Twine &Message,
34 StringRef Whence = "",
35 StringRef Hint = "") {
Justin Bognerf8d79192014-03-21 17:24:48 +000036 errs() << "error: ";
37 if (!Whence.empty())
38 errs() << Whence << ": ";
39 errs() << Message << "\n";
Nathan Slingerland4f823662015-11-13 03:47:58 +000040 if (!Hint.empty())
41 errs() << Hint << "\n";
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000042 ::exit(1);
43}
44
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000045static void exitWithErrorCode(const std::error_code &Error,
46 StringRef Whence = "") {
Nathan Slingerland4f823662015-11-13 03:47:58 +000047 if (Error.category() == instrprof_category()) {
48 instrprof_error instrError = static_cast<instrprof_error>(Error.value());
49 if (instrError == instrprof_error::unrecognized_format) {
50 // Hint for common error of forgetting -sample for sample profiles.
51 exitWithError(Error.message(), Whence,
52 "Perhaps you forgot to use the -sample option?");
53 }
54 }
55 exitWithError(Error.message(), Whence);
56}
57
Duncan P. N. Exon Smith02b6fa92015-06-16 00:43:04 +000058namespace {
Nathan Slingerland4f823662015-11-13 03:47:58 +000059 enum ProfileKinds { instr, sample };
Duncan P. N. Exon Smith02b6fa92015-06-16 00:43:04 +000060}
Justin Bogner618bcea2014-03-19 02:20:46 +000061
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000062static void handleMergeWriterError(std::error_code &Error,
63 StringRef WhenceFile = "",
64 StringRef WhenceFunction = "",
65 bool ShowHint = true)
66{
67 if (!WhenceFile.empty())
68 errs() << WhenceFile << ": ";
69 if (!WhenceFunction.empty())
70 errs() << WhenceFunction << ": ";
71 errs() << Error.message() << "\n";
72
73 if (ShowHint) {
74 StringRef Hint = "";
75 if (Error.category() == instrprof_category()) {
76 instrprof_error instrError = static_cast<instrprof_error>(Error.value());
Nathan Slingerland11c938d12015-11-17 23:37:09 +000077 switch (instrError) {
78 case instrprof_error::hash_mismatch:
79 case instrprof_error::count_mismatch:
80 case instrprof_error::value_site_count_mismatch:
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000081 Hint = "Make sure that all profile data to be merged is generated " \
82 "from the same binary.";
Nathan Slingerland11c938d12015-11-17 23:37:09 +000083 break;
Nathan Slingerlandb2d95f02015-11-18 00:52:45 +000084 default:
85 break;
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000086 }
87 }
88
89 if (!Hint.empty())
90 errs() << Hint << "\n";
91 }
92}
93
Benjamin Kramerf044d3f2015-03-09 16:23:46 +000094static void mergeInstrProfile(const cl::list<std::string> &Inputs,
95 StringRef OutputFilename) {
Justin Bognerb7aa2632014-04-18 21:48:40 +000096 if (OutputFilename.compare("-") == 0)
97 exitWithError("Cannot write indexed profdata format to stdout.");
Justin Bognerec49f982014-03-12 22:00:57 +000098
Rafael Espindola3fd1e992014-08-25 18:16:47 +000099 std::error_code EC;
100 raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None);
101 if (EC)
Nathan Slingerland4f823662015-11-13 03:47:58 +0000102 exitWithErrorCode(EC, OutputFilename);
Justin Bognerec49f982014-03-12 22:00:57 +0000103
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000104 InstrProfWriter Writer;
Nathan Slingerlandc21a44d2015-11-18 17:10:24 +0000105 SmallSet<std::error_code, 4> WriterErrorCodes;
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000106 for (const auto &Filename : Inputs) {
Diego Novillofcd55602014-11-03 00:51:45 +0000107 auto ReaderOrErr = InstrProfReader::create(Filename);
108 if (std::error_code ec = ReaderOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000109 exitWithErrorCode(ec, Filename);
Justin Bognerf8d79192014-03-21 17:24:48 +0000110
Diego Novillofcd55602014-11-03 00:51:45 +0000111 auto Reader = std::move(ReaderOrErr.get());
Nathan Slingerlande6e30d52015-11-17 22:08:53 +0000112 for (auto &I : *Reader) {
113 if (std::error_code EC = Writer.addRecord(std::move(I))) {
114 // Only show hint the first time an error occurs.
115 bool firstTime = WriterErrorCodes.insert(EC).second;
116 handleMergeWriterError(EC, Filename, I.Name, firstTime);
117 }
118 }
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000119 if (Reader->hasError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000120 exitWithErrorCode(Reader->getError(), Filename);
Justin Bognerbfee8d42014-03-12 20:14:17 +0000121 }
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000122 Writer.write(Output);
Diego Novillod5336ae2014-11-01 00:56:55 +0000123}
124
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000125static void mergeSampleProfile(const cl::list<std::string> &Inputs,
126 StringRef OutputFilename,
127 sampleprof::SampleProfileFormat OutputFormat) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000128 using namespace sampleprof;
Diego Novillofcd55602014-11-03 00:51:45 +0000129 auto WriterOrErr = SampleProfileWriter::create(OutputFilename, OutputFormat);
130 if (std::error_code EC = WriterOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000131 exitWithErrorCode(EC, OutputFilename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000132
Diego Novillofcd55602014-11-03 00:51:45 +0000133 auto Writer = std::move(WriterOrErr.get());
Diego Novillod5336ae2014-11-01 00:56:55 +0000134 StringMap<FunctionSamples> ProfileMap;
Diego Novilloaae1ed82015-10-08 19:40:37 +0000135 SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers;
Diego Novillod5336ae2014-11-01 00:56:55 +0000136 for (const auto &Filename : Inputs) {
Diego Novillofcd55602014-11-03 00:51:45 +0000137 auto ReaderOrErr =
138 SampleProfileReader::create(Filename, getGlobalContext());
139 if (std::error_code EC = ReaderOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000140 exitWithErrorCode(EC, Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000141
Diego Novilloaae1ed82015-10-08 19:40:37 +0000142 // We need to keep the readers around until after all the files are
143 // read so that we do not lose the function names stored in each
144 // reader's memory. The function names are needed to write out the
145 // merged profile map.
146 Readers.push_back(std::move(ReaderOrErr.get()));
147 const auto Reader = Readers.back().get();
Diego Novillod5336ae2014-11-01 00:56:55 +0000148 if (std::error_code EC = Reader->read())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000149 exitWithErrorCode(EC, Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000150
151 StringMap<FunctionSamples> &Profiles = Reader->getProfiles();
152 for (StringMap<FunctionSamples>::iterator I = Profiles.begin(),
153 E = Profiles.end();
154 I != E; ++I) {
155 StringRef FName = I->first();
156 FunctionSamples &Samples = I->second;
157 ProfileMap[FName].merge(Samples);
158 }
159 }
160 Writer->write(ProfileMap);
161}
162
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000163static int merge_main(int argc, const char *argv[]) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000164 cl::list<std::string> Inputs(cl::Positional, cl::Required, cl::OneOrMore,
165 cl::desc("<filenames...>"));
166
167 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
168 cl::init("-"), cl::Required,
169 cl::desc("Output file"));
170 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
171 cl::aliasopt(OutputFilename));
172 cl::opt<ProfileKinds> ProfileKind(
173 cl::desc("Profile kind:"), cl::init(instr),
174 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
175 clEnumVal(sample, "Sample profile"), clEnumValEnd));
176
177 cl::opt<sampleprof::SampleProfileFormat> OutputFormat(
178 cl::desc("Format of output profile (only meaningful with --sample)"),
179 cl::init(sampleprof::SPF_Binary),
180 cl::values(clEnumValN(sampleprof::SPF_Binary, "binary",
181 "Binary encoding (default)"),
182 clEnumValN(sampleprof::SPF_Text, "text", "Text encoding"),
183 clEnumValN(sampleprof::SPF_GCC, "gcc", "GCC encoding"),
184 clEnumValEnd));
185
186 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
187
188 if (ProfileKind == instr)
189 mergeInstrProfile(Inputs, OutputFilename);
190 else
191 mergeSampleProfile(Inputs, OutputFilename, OutputFormat);
Justin Bognerbfee8d42014-03-12 20:14:17 +0000192
Justin Bognerec49f982014-03-12 22:00:57 +0000193 return 0;
Justin Bognerbfee8d42014-03-12 20:14:17 +0000194}
Justin Bogner618bcea2014-03-19 02:20:46 +0000195
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000196static int showInstrProfile(std::string Filename, bool ShowCounts,
Justin Bogner9e9a0572015-09-29 22:13:58 +0000197 bool ShowIndirectCallTargets, bool ShowAllFunctions,
198 std::string ShowFunction, raw_fd_ostream &OS) {
Diego Novillofcd55602014-11-03 00:51:45 +0000199 auto ReaderOrErr = InstrProfReader::create(Filename);
200 if (std::error_code EC = ReaderOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000201 exitWithErrorCode(EC, Filename);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000202
Diego Novillofcd55602014-11-03 00:51:45 +0000203 auto Reader = std::move(ReaderOrErr.get());
Justin Bogner9af28ef2014-03-21 17:29:44 +0000204 uint64_t MaxFunctionCount = 0, MaxBlockCount = 0;
205 size_t ShownFunctions = 0, TotalFunctions = 0;
206 for (const auto &Func : *Reader) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000207 bool Show =
208 ShowAllFunctions || (!ShowFunction.empty() &&
209 Func.Name.find(ShowFunction) != Func.Name.npos);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000210
211 ++TotalFunctions;
Justin Bognerb59d7c72014-04-25 02:45:33 +0000212 assert(Func.Counts.size() > 0 && "function missing entry counter");
Justin Bogner9af28ef2014-03-21 17:29:44 +0000213 if (Func.Counts[0] > MaxFunctionCount)
214 MaxFunctionCount = Func.Counts[0];
215
216 if (Show) {
217 if (!ShownFunctions)
218 OS << "Counters:\n";
219 ++ShownFunctions;
220
221 OS << " " << Func.Name << ":\n"
Justin Bogner423380f2014-03-23 20:43:50 +0000222 << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
Justin Bogner9af28ef2014-03-21 17:29:44 +0000223 << " Counters: " << Func.Counts.size() << "\n"
224 << " Function count: " << Func.Counts[0] << "\n";
Justin Bogner9e9a0572015-09-29 22:13:58 +0000225 if (ShowIndirectCallTargets)
Xinliang David Li2004f002015-11-02 05:08:23 +0000226 OS << " Indirect Call Site Count: "
227 << Func.getNumValueSites(IPVK_IndirectCallTarget) << "\n";
Justin Bogner9af28ef2014-03-21 17:29:44 +0000228 }
229
230 if (Show && ShowCounts)
231 OS << " Block counts: [";
232 for (size_t I = 1, E = Func.Counts.size(); I < E; ++I) {
233 if (Func.Counts[I] > MaxBlockCount)
234 MaxBlockCount = Func.Counts[I];
235 if (Show && ShowCounts)
236 OS << (I == 1 ? "" : ", ") << Func.Counts[I];
237 }
238 if (Show && ShowCounts)
239 OS << "]\n";
Justin Bogner9e9a0572015-09-29 22:13:58 +0000240
241 if (Show && ShowIndirectCallTargets) {
Xinliang David Li2004f002015-11-02 05:08:23 +0000242 uint32_t NS = Func.getNumValueSites(IPVK_IndirectCallTarget);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000243 OS << " Indirect Target Results: \n";
Xinliang David Li2004f002015-11-02 05:08:23 +0000244 for (size_t I = 0; I < NS; ++I) {
245 uint32_t NV = Func.getNumValueDataForSite(IPVK_IndirectCallTarget, I);
246 std::unique_ptr<InstrProfValueData[]> VD =
247 Func.getValueForSite(IPVK_IndirectCallTarget, I);
248 for (uint32_t V = 0; V < NV; V++) {
Justin Bogner9e9a0572015-09-29 22:13:58 +0000249 OS << "\t[ " << I << ", ";
Xinliang David Li2004f002015-11-02 05:08:23 +0000250 OS << (const char *)VD[V].Value << ", " << VD[V].Count << " ]\n";
Justin Bogner9e9a0572015-09-29 22:13:58 +0000251 }
252 }
253 }
Justin Bogner9af28ef2014-03-21 17:29:44 +0000254 }
Justin Bognerdb1225d2014-03-23 20:55:53 +0000255 if (Reader->hasError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000256 exitWithErrorCode(Reader->getError(), Filename);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000257
258 if (ShowAllFunctions || !ShowFunction.empty())
259 OS << "Functions shown: " << ShownFunctions << "\n";
260 OS << "Total functions: " << TotalFunctions << "\n";
261 OS << "Maximum function count: " << MaxFunctionCount << "\n";
262 OS << "Maximum internal block count: " << MaxBlockCount << "\n";
263 return 0;
264}
265
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000266static int showSampleProfile(std::string Filename, bool ShowCounts,
267 bool ShowAllFunctions, std::string ShowFunction,
268 raw_fd_ostream &OS) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000269 using namespace sampleprof;
Diego Novillofcd55602014-11-03 00:51:45 +0000270 auto ReaderOrErr = SampleProfileReader::create(Filename, getGlobalContext());
271 if (std::error_code EC = ReaderOrErr.getError())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000272 exitWithErrorCode(EC, Filename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000273
Diego Novillofcd55602014-11-03 00:51:45 +0000274 auto Reader = std::move(ReaderOrErr.get());
Diego Novilloc6d032a2015-09-17 00:17:21 +0000275 if (std::error_code EC = Reader->read())
Nathan Slingerland4f823662015-11-13 03:47:58 +0000276 exitWithErrorCode(EC, Filename);
Diego Novilloc6d032a2015-09-17 00:17:21 +0000277
Diego Novillod5336ae2014-11-01 00:56:55 +0000278 if (ShowAllFunctions || ShowFunction.empty())
279 Reader->dump(OS);
280 else
281 Reader->dumpFunctionProfile(ShowFunction, OS);
282
283 return 0;
284}
285
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000286static int show_main(int argc, const char *argv[]) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000287 cl::opt<std::string> Filename(cl::Positional, cl::Required,
288 cl::desc("<profdata-file>"));
289
290 cl::opt<bool> ShowCounts("counts", cl::init(false),
291 cl::desc("Show counter values for shown functions"));
Justin Bogner9e9a0572015-09-29 22:13:58 +0000292 cl::opt<bool> ShowIndirectCallTargets(
293 "ic-targets", cl::init(false),
294 cl::desc("Show indirect call site target values for shown functions"));
Diego Novillod5336ae2014-11-01 00:56:55 +0000295 cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
296 cl::desc("Details for every function"));
297 cl::opt<std::string> ShowFunction("function",
298 cl::desc("Details for matching functions"));
299
300 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
301 cl::init("-"), cl::desc("Output file"));
302 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
303 cl::aliasopt(OutputFilename));
304 cl::opt<ProfileKinds> ProfileKind(
305 cl::desc("Profile kind:"), cl::init(instr),
306 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
307 clEnumVal(sample, "Sample profile"), clEnumValEnd));
308
309 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
310
311 if (OutputFilename.empty())
312 OutputFilename = "-";
313
314 std::error_code EC;
315 raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text);
316 if (EC)
Nathan Slingerland4f823662015-11-13 03:47:58 +0000317 exitWithErrorCode(EC, OutputFilename);
Diego Novillod5336ae2014-11-01 00:56:55 +0000318
319 if (ShowAllFunctions && !ShowFunction.empty())
320 errs() << "warning: -function argument ignored: showing all functions\n";
321
322 if (ProfileKind == instr)
Justin Bogner9e9a0572015-09-29 22:13:58 +0000323 return showInstrProfile(Filename, ShowCounts, ShowIndirectCallTargets,
324 ShowAllFunctions, ShowFunction, OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000325 else
326 return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
327 ShowFunction, OS);
328}
329
Justin Bogner618bcea2014-03-19 02:20:46 +0000330int main(int argc, const char *argv[]) {
331 // Print a stack trace if we signal out.
332 sys::PrintStackTraceOnErrorSignal();
333 PrettyStackTraceProgram X(argc, argv);
334 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
335
336 StringRef ProgName(sys::path::filename(argv[0]));
337 if (argc > 1) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000338 int (*func)(int, const char *[]) = nullptr;
Justin Bogner618bcea2014-03-19 02:20:46 +0000339
340 if (strcmp(argv[1], "merge") == 0)
341 func = merge_main;
Justin Bogner9af28ef2014-03-21 17:29:44 +0000342 else if (strcmp(argv[1], "show") == 0)
343 func = show_main;
Justin Bogner618bcea2014-03-19 02:20:46 +0000344
345 if (func) {
346 std::string Invocation(ProgName.str() + " " + argv[1]);
347 argv[1] = Invocation.c_str();
348 return func(argc - 1, argv + 1);
349 }
350
351 if (strcmp(argv[1], "-h") == 0 ||
352 strcmp(argv[1], "-help") == 0 ||
353 strcmp(argv[1], "--help") == 0) {
354
355 errs() << "OVERVIEW: LLVM profile data tools\n\n"
356 << "USAGE: " << ProgName << " <command> [args...]\n"
357 << "USAGE: " << ProgName << " <command> -help\n\n"
Justin Bogner9af28ef2014-03-21 17:29:44 +0000358 << "Available commands: merge, show\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000359 return 0;
360 }
361 }
362
363 if (argc < 2)
364 errs() << ProgName << ": No command specified!\n";
365 else
366 errs() << ProgName << ": Unknown command!\n";
367
Justin Bogner9af28ef2014-03-21 17:29:44 +0000368 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000369 return 1;
370}