blob: fdf22c3642fd1d076a45cd104ea4e543cf14436e [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
30using namespace llvm;
31
Justin Bognerf8d79192014-03-21 17:24:48 +000032static void exitWithError(const Twine &Message, StringRef Whence = "") {
33 errs() << "error: ";
34 if (!Whence.empty())
35 errs() << Whence << ": ";
36 errs() << Message << "\n";
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000037 ::exit(1);
38}
39
Duncan P. N. Exon Smith02b6fa92015-06-16 00:43:04 +000040namespace {
Diego Novillod5336ae2014-11-01 00:56:55 +000041enum ProfileKinds { instr, sample };
Duncan P. N. Exon Smith02b6fa92015-06-16 00:43:04 +000042}
Justin Bogner618bcea2014-03-19 02:20:46 +000043
Benjamin Kramerf044d3f2015-03-09 16:23:46 +000044static void mergeInstrProfile(const cl::list<std::string> &Inputs,
45 StringRef OutputFilename) {
Justin Bognerb7aa2632014-04-18 21:48:40 +000046 if (OutputFilename.compare("-") == 0)
47 exitWithError("Cannot write indexed profdata format to stdout.");
Justin Bognerec49f982014-03-12 22:00:57 +000048
Rafael Espindola3fd1e992014-08-25 18:16:47 +000049 std::error_code EC;
50 raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None);
51 if (EC)
52 exitWithError(EC.message(), OutputFilename);
Justin Bognerec49f982014-03-12 22:00:57 +000053
Justin Bognerb9bd7f82014-03-21 17:46:22 +000054 InstrProfWriter Writer;
55 for (const auto &Filename : Inputs) {
Diego Novillofcd55602014-11-03 00:51:45 +000056 auto ReaderOrErr = InstrProfReader::create(Filename);
57 if (std::error_code ec = ReaderOrErr.getError())
Justin Bognerb9bd7f82014-03-21 17:46:22 +000058 exitWithError(ec.message(), Filename);
Justin Bognerf8d79192014-03-21 17:24:48 +000059
Diego Novillofcd55602014-11-03 00:51:45 +000060 auto Reader = std::move(ReaderOrErr.get());
Justin Bogner9e9a0572015-09-29 22:13:58 +000061 for (auto &I : *Reader)
62 if (std::error_code EC = Writer.addRecord(std::move(I)))
Justin Bognerb9bd7f82014-03-21 17:46:22 +000063 errs() << Filename << ": " << I.Name << ": " << EC.message() << "\n";
64 if (Reader->hasError())
65 exitWithError(Reader->getError().message(), Filename);
Justin Bognerbfee8d42014-03-12 20:14:17 +000066 }
Justin Bognerb9bd7f82014-03-21 17:46:22 +000067 Writer.write(Output);
Diego Novillod5336ae2014-11-01 00:56:55 +000068}
69
Benjamin Kramerf044d3f2015-03-09 16:23:46 +000070static void mergeSampleProfile(const cl::list<std::string> &Inputs,
71 StringRef OutputFilename,
72 sampleprof::SampleProfileFormat OutputFormat) {
Diego Novillod5336ae2014-11-01 00:56:55 +000073 using namespace sampleprof;
Diego Novillofcd55602014-11-03 00:51:45 +000074 auto WriterOrErr = SampleProfileWriter::create(OutputFilename, OutputFormat);
75 if (std::error_code EC = WriterOrErr.getError())
Diego Novillod5336ae2014-11-01 00:56:55 +000076 exitWithError(EC.message(), OutputFilename);
77
Diego Novillofcd55602014-11-03 00:51:45 +000078 auto Writer = std::move(WriterOrErr.get());
Diego Novillod5336ae2014-11-01 00:56:55 +000079 StringMap<FunctionSamples> ProfileMap;
80 for (const auto &Filename : Inputs) {
Diego Novillofcd55602014-11-03 00:51:45 +000081 auto ReaderOrErr =
82 SampleProfileReader::create(Filename, getGlobalContext());
83 if (std::error_code EC = ReaderOrErr.getError())
Diego Novillod5336ae2014-11-01 00:56:55 +000084 exitWithError(EC.message(), Filename);
85
Diego Novillofcd55602014-11-03 00:51:45 +000086 auto Reader = std::move(ReaderOrErr.get());
Diego Novillod5336ae2014-11-01 00:56:55 +000087 if (std::error_code EC = Reader->read())
88 exitWithError(EC.message(), Filename);
89
90 StringMap<FunctionSamples> &Profiles = Reader->getProfiles();
91 for (StringMap<FunctionSamples>::iterator I = Profiles.begin(),
92 E = Profiles.end();
93 I != E; ++I) {
94 StringRef FName = I->first();
95 FunctionSamples &Samples = I->second;
96 ProfileMap[FName].merge(Samples);
97 }
98 }
99 Writer->write(ProfileMap);
100}
101
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000102static int merge_main(int argc, const char *argv[]) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000103 cl::list<std::string> Inputs(cl::Positional, cl::Required, cl::OneOrMore,
104 cl::desc("<filenames...>"));
105
106 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
107 cl::init("-"), cl::Required,
108 cl::desc("Output file"));
109 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
110 cl::aliasopt(OutputFilename));
111 cl::opt<ProfileKinds> ProfileKind(
112 cl::desc("Profile kind:"), cl::init(instr),
113 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
114 clEnumVal(sample, "Sample profile"), clEnumValEnd));
115
116 cl::opt<sampleprof::SampleProfileFormat> OutputFormat(
117 cl::desc("Format of output profile (only meaningful with --sample)"),
118 cl::init(sampleprof::SPF_Binary),
119 cl::values(clEnumValN(sampleprof::SPF_Binary, "binary",
120 "Binary encoding (default)"),
121 clEnumValN(sampleprof::SPF_Text, "text", "Text encoding"),
122 clEnumValN(sampleprof::SPF_GCC, "gcc", "GCC encoding"),
123 clEnumValEnd));
124
125 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
126
127 if (ProfileKind == instr)
128 mergeInstrProfile(Inputs, OutputFilename);
129 else
130 mergeSampleProfile(Inputs, OutputFilename, OutputFormat);
Justin Bognerbfee8d42014-03-12 20:14:17 +0000131
Justin Bognerec49f982014-03-12 22:00:57 +0000132 return 0;
Justin Bognerbfee8d42014-03-12 20:14:17 +0000133}
Justin Bogner618bcea2014-03-19 02:20:46 +0000134
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000135static int showInstrProfile(std::string Filename, bool ShowCounts,
Justin Bogner9e9a0572015-09-29 22:13:58 +0000136 bool ShowIndirectCallTargets, bool ShowAllFunctions,
137 std::string ShowFunction, raw_fd_ostream &OS) {
Diego Novillofcd55602014-11-03 00:51:45 +0000138 auto ReaderOrErr = InstrProfReader::create(Filename);
139 if (std::error_code EC = ReaderOrErr.getError())
Justin Bogner9af28ef2014-03-21 17:29:44 +0000140 exitWithError(EC.message(), Filename);
141
Diego Novillofcd55602014-11-03 00:51:45 +0000142 auto Reader = std::move(ReaderOrErr.get());
Justin Bogner9af28ef2014-03-21 17:29:44 +0000143 uint64_t MaxFunctionCount = 0, MaxBlockCount = 0;
144 size_t ShownFunctions = 0, TotalFunctions = 0;
145 for (const auto &Func : *Reader) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000146 bool Show =
147 ShowAllFunctions || (!ShowFunction.empty() &&
148 Func.Name.find(ShowFunction) != Func.Name.npos);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000149
150 ++TotalFunctions;
Justin Bognerb59d7c72014-04-25 02:45:33 +0000151 assert(Func.Counts.size() > 0 && "function missing entry counter");
Justin Bogner9af28ef2014-03-21 17:29:44 +0000152 if (Func.Counts[0] > MaxFunctionCount)
153 MaxFunctionCount = Func.Counts[0];
154
155 if (Show) {
156 if (!ShownFunctions)
157 OS << "Counters:\n";
158 ++ShownFunctions;
159
160 OS << " " << Func.Name << ":\n"
Justin Bogner423380f2014-03-23 20:43:50 +0000161 << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
Justin Bogner9af28ef2014-03-21 17:29:44 +0000162 << " Counters: " << Func.Counts.size() << "\n"
163 << " Function count: " << Func.Counts[0] << "\n";
Justin Bogner9e9a0572015-09-29 22:13:58 +0000164 if (ShowIndirectCallTargets)
165 OS << " Indirect Call Site Count: " << Func.IndirectCallSites.size()
166 << "\n";
Justin Bogner9af28ef2014-03-21 17:29:44 +0000167 }
168
169 if (Show && ShowCounts)
170 OS << " Block counts: [";
171 for (size_t I = 1, E = Func.Counts.size(); I < E; ++I) {
172 if (Func.Counts[I] > MaxBlockCount)
173 MaxBlockCount = Func.Counts[I];
174 if (Show && ShowCounts)
175 OS << (I == 1 ? "" : ", ") << Func.Counts[I];
176 }
177 if (Show && ShowCounts)
178 OS << "]\n";
Justin Bogner9e9a0572015-09-29 22:13:58 +0000179
180 if (Show && ShowIndirectCallTargets) {
181 OS << " Indirect Target Results: \n";
182 for (size_t I = 0, E = Func.IndirectCallSites.size(); I < E; ++I) {
183 for (auto V : Func.IndirectCallSites[I].ValueData) {
184 OS << "\t[ " << I << ", ";
185 OS << (const char *)V.first << ", " << V.second << " ]\n";
186 }
187 }
188 }
Justin Bogner9af28ef2014-03-21 17:29:44 +0000189 }
Justin Bognerdb1225d2014-03-23 20:55:53 +0000190 if (Reader->hasError())
191 exitWithError(Reader->getError().message(), Filename);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000192
193 if (ShowAllFunctions || !ShowFunction.empty())
194 OS << "Functions shown: " << ShownFunctions << "\n";
195 OS << "Total functions: " << TotalFunctions << "\n";
196 OS << "Maximum function count: " << MaxFunctionCount << "\n";
197 OS << "Maximum internal block count: " << MaxBlockCount << "\n";
198 return 0;
199}
200
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000201static int showSampleProfile(std::string Filename, bool ShowCounts,
202 bool ShowAllFunctions, std::string ShowFunction,
203 raw_fd_ostream &OS) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000204 using namespace sampleprof;
Diego Novillofcd55602014-11-03 00:51:45 +0000205 auto ReaderOrErr = SampleProfileReader::create(Filename, getGlobalContext());
206 if (std::error_code EC = ReaderOrErr.getError())
Diego Novillod5336ae2014-11-01 00:56:55 +0000207 exitWithError(EC.message(), Filename);
208
Diego Novillofcd55602014-11-03 00:51:45 +0000209 auto Reader = std::move(ReaderOrErr.get());
Diego Novilloc6d032a2015-09-17 00:17:21 +0000210 if (std::error_code EC = Reader->read())
211 exitWithError(EC.message(), Filename);
212
Diego Novillod5336ae2014-11-01 00:56:55 +0000213 if (ShowAllFunctions || ShowFunction.empty())
214 Reader->dump(OS);
215 else
216 Reader->dumpFunctionProfile(ShowFunction, OS);
217
218 return 0;
219}
220
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000221static int show_main(int argc, const char *argv[]) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000222 cl::opt<std::string> Filename(cl::Positional, cl::Required,
223 cl::desc("<profdata-file>"));
224
225 cl::opt<bool> ShowCounts("counts", cl::init(false),
226 cl::desc("Show counter values for shown functions"));
Justin Bogner9e9a0572015-09-29 22:13:58 +0000227 cl::opt<bool> ShowIndirectCallTargets(
228 "ic-targets", cl::init(false),
229 cl::desc("Show indirect call site target values for shown functions"));
Diego Novillod5336ae2014-11-01 00:56:55 +0000230 cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
231 cl::desc("Details for every function"));
232 cl::opt<std::string> ShowFunction("function",
233 cl::desc("Details for matching functions"));
234
235 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
236 cl::init("-"), cl::desc("Output file"));
237 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
238 cl::aliasopt(OutputFilename));
239 cl::opt<ProfileKinds> ProfileKind(
240 cl::desc("Profile kind:"), cl::init(instr),
241 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
242 clEnumVal(sample, "Sample profile"), clEnumValEnd));
243
244 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
245
246 if (OutputFilename.empty())
247 OutputFilename = "-";
248
249 std::error_code EC;
250 raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text);
251 if (EC)
252 exitWithError(EC.message(), OutputFilename);
253
254 if (ShowAllFunctions && !ShowFunction.empty())
255 errs() << "warning: -function argument ignored: showing all functions\n";
256
257 if (ProfileKind == instr)
Justin Bogner9e9a0572015-09-29 22:13:58 +0000258 return showInstrProfile(Filename, ShowCounts, ShowIndirectCallTargets,
259 ShowAllFunctions, ShowFunction, OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000260 else
261 return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
262 ShowFunction, OS);
263}
264
Justin Bogner618bcea2014-03-19 02:20:46 +0000265int main(int argc, const char *argv[]) {
266 // Print a stack trace if we signal out.
267 sys::PrintStackTraceOnErrorSignal();
268 PrettyStackTraceProgram X(argc, argv);
269 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
270
271 StringRef ProgName(sys::path::filename(argv[0]));
272 if (argc > 1) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000273 int (*func)(int, const char *[]) = nullptr;
Justin Bogner618bcea2014-03-19 02:20:46 +0000274
275 if (strcmp(argv[1], "merge") == 0)
276 func = merge_main;
Justin Bogner9af28ef2014-03-21 17:29:44 +0000277 else if (strcmp(argv[1], "show") == 0)
278 func = show_main;
Justin Bogner618bcea2014-03-19 02:20:46 +0000279
280 if (func) {
281 std::string Invocation(ProgName.str() + " " + argv[1]);
282 argv[1] = Invocation.c_str();
283 return func(argc - 1, argv + 1);
284 }
285
286 if (strcmp(argv[1], "-h") == 0 ||
287 strcmp(argv[1], "-help") == 0 ||
288 strcmp(argv[1], "--help") == 0) {
289
290 errs() << "OVERVIEW: LLVM profile data tools\n\n"
291 << "USAGE: " << ProgName << " <command> [args...]\n"
292 << "USAGE: " << ProgName << " <command> -help\n\n"
Justin Bogner9af28ef2014-03-21 17:29:44 +0000293 << "Available commands: merge, show\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000294 return 0;
295 }
296 }
297
298 if (argc < 2)
299 errs() << ProgName << ": No command specified!\n";
300 else
301 errs() << ProgName << ": Unknown command!\n";
302
Justin Bogner9af28ef2014-03-21 17:29:44 +0000303 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000304 return 1;
305}