blob: 2e8c640bf0cd5b8d102906720e50fb928adf7f43 [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"
25#include "llvm/Support/PrettyStackTrace.h"
26#include "llvm/Support/Signals.h"
27#include "llvm/Support/raw_ostream.h"
28
29using namespace llvm;
30
Justin Bognerf8d79192014-03-21 17:24:48 +000031static void exitWithError(const Twine &Message, StringRef Whence = "") {
32 errs() << "error: ";
33 if (!Whence.empty())
34 errs() << Whence << ": ";
35 errs() << Message << "\n";
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000036 ::exit(1);
37}
38
Diego Novillod5336ae2014-11-01 00:56:55 +000039enum ProfileKinds { instr, sample };
Justin Bogner618bcea2014-03-19 02:20:46 +000040
Benjamin Kramerf044d3f2015-03-09 16:23:46 +000041static void mergeInstrProfile(const cl::list<std::string> &Inputs,
42 StringRef OutputFilename) {
Justin Bognerb7aa2632014-04-18 21:48:40 +000043 if (OutputFilename.compare("-") == 0)
44 exitWithError("Cannot write indexed profdata format to stdout.");
Justin Bognerec49f982014-03-12 22:00:57 +000045
Rafael Espindola3fd1e992014-08-25 18:16:47 +000046 std::error_code EC;
47 raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None);
48 if (EC)
49 exitWithError(EC.message(), OutputFilename);
Justin Bognerec49f982014-03-12 22:00:57 +000050
Justin Bognerb9bd7f82014-03-21 17:46:22 +000051 InstrProfWriter Writer;
52 for (const auto &Filename : Inputs) {
Diego Novillofcd55602014-11-03 00:51:45 +000053 auto ReaderOrErr = InstrProfReader::create(Filename);
54 if (std::error_code ec = ReaderOrErr.getError())
Justin Bognerb9bd7f82014-03-21 17:46:22 +000055 exitWithError(ec.message(), Filename);
Justin Bognerf8d79192014-03-21 17:24:48 +000056
Diego Novillofcd55602014-11-03 00:51:45 +000057 auto Reader = std::move(ReaderOrErr.get());
Justin Bognerb9bd7f82014-03-21 17:46:22 +000058 for (const auto &I : *Reader)
Rafael Espindola4453e42942014-06-13 03:07:50 +000059 if (std::error_code EC =
60 Writer.addFunctionCounts(I.Name, I.Hash, I.Counts))
Justin Bognerb9bd7f82014-03-21 17:46:22 +000061 errs() << Filename << ": " << I.Name << ": " << EC.message() << "\n";
62 if (Reader->hasError())
63 exitWithError(Reader->getError().message(), Filename);
Justin Bognerbfee8d42014-03-12 20:14:17 +000064 }
Justin Bognerb9bd7f82014-03-21 17:46:22 +000065 Writer.write(Output);
Diego Novillod5336ae2014-11-01 00:56:55 +000066}
67
Benjamin Kramerf044d3f2015-03-09 16:23:46 +000068static void mergeSampleProfile(const cl::list<std::string> &Inputs,
69 StringRef OutputFilename,
70 sampleprof::SampleProfileFormat OutputFormat) {
Diego Novillod5336ae2014-11-01 00:56:55 +000071 using namespace sampleprof;
Diego Novillofcd55602014-11-03 00:51:45 +000072 auto WriterOrErr = SampleProfileWriter::create(OutputFilename, OutputFormat);
73 if (std::error_code EC = WriterOrErr.getError())
Diego Novillod5336ae2014-11-01 00:56:55 +000074 exitWithError(EC.message(), OutputFilename);
75
Diego Novillofcd55602014-11-03 00:51:45 +000076 auto Writer = std::move(WriterOrErr.get());
Diego Novillod5336ae2014-11-01 00:56:55 +000077 StringMap<FunctionSamples> ProfileMap;
78 for (const auto &Filename : Inputs) {
Diego Novillofcd55602014-11-03 00:51:45 +000079 auto ReaderOrErr =
80 SampleProfileReader::create(Filename, getGlobalContext());
81 if (std::error_code EC = ReaderOrErr.getError())
Diego Novillod5336ae2014-11-01 00:56:55 +000082 exitWithError(EC.message(), Filename);
83
Diego Novillofcd55602014-11-03 00:51:45 +000084 auto Reader = std::move(ReaderOrErr.get());
Diego Novillod5336ae2014-11-01 00:56:55 +000085 if (std::error_code EC = Reader->read())
86 exitWithError(EC.message(), Filename);
87
88 StringMap<FunctionSamples> &Profiles = Reader->getProfiles();
89 for (StringMap<FunctionSamples>::iterator I = Profiles.begin(),
90 E = Profiles.end();
91 I != E; ++I) {
92 StringRef FName = I->first();
93 FunctionSamples &Samples = I->second;
94 ProfileMap[FName].merge(Samples);
95 }
96 }
97 Writer->write(ProfileMap);
98}
99
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000100static int merge_main(int argc, const char *argv[]) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000101 cl::list<std::string> Inputs(cl::Positional, cl::Required, cl::OneOrMore,
102 cl::desc("<filenames...>"));
103
104 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
105 cl::init("-"), cl::Required,
106 cl::desc("Output file"));
107 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
108 cl::aliasopt(OutputFilename));
109 cl::opt<ProfileKinds> ProfileKind(
110 cl::desc("Profile kind:"), cl::init(instr),
111 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
112 clEnumVal(sample, "Sample profile"), clEnumValEnd));
113
114 cl::opt<sampleprof::SampleProfileFormat> OutputFormat(
115 cl::desc("Format of output profile (only meaningful with --sample)"),
116 cl::init(sampleprof::SPF_Binary),
117 cl::values(clEnumValN(sampleprof::SPF_Binary, "binary",
118 "Binary encoding (default)"),
119 clEnumValN(sampleprof::SPF_Text, "text", "Text encoding"),
120 clEnumValN(sampleprof::SPF_GCC, "gcc", "GCC encoding"),
121 clEnumValEnd));
122
123 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
124
125 if (ProfileKind == instr)
126 mergeInstrProfile(Inputs, OutputFilename);
127 else
128 mergeSampleProfile(Inputs, OutputFilename, OutputFormat);
Justin Bognerbfee8d42014-03-12 20:14:17 +0000129
Justin Bognerec49f982014-03-12 22:00:57 +0000130 return 0;
Justin Bognerbfee8d42014-03-12 20:14:17 +0000131}
Justin Bogner618bcea2014-03-19 02:20:46 +0000132
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000133static int showInstrProfile(std::string Filename, bool ShowCounts,
134 bool ShowAllFunctions, std::string ShowFunction,
135 raw_fd_ostream &OS) {
Diego Novillofcd55602014-11-03 00:51:45 +0000136 auto ReaderOrErr = InstrProfReader::create(Filename);
137 if (std::error_code EC = ReaderOrErr.getError())
Justin Bogner9af28ef2014-03-21 17:29:44 +0000138 exitWithError(EC.message(), Filename);
139
Diego Novillofcd55602014-11-03 00:51:45 +0000140 auto Reader = std::move(ReaderOrErr.get());
Justin Bogner9af28ef2014-03-21 17:29:44 +0000141 uint64_t MaxFunctionCount = 0, MaxBlockCount = 0;
142 size_t ShownFunctions = 0, TotalFunctions = 0;
143 for (const auto &Func : *Reader) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000144 bool Show =
145 ShowAllFunctions || (!ShowFunction.empty() &&
146 Func.Name.find(ShowFunction) != Func.Name.npos);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000147
148 ++TotalFunctions;
Justin Bognerb59d7c72014-04-25 02:45:33 +0000149 assert(Func.Counts.size() > 0 && "function missing entry counter");
Justin Bogner9af28ef2014-03-21 17:29:44 +0000150 if (Func.Counts[0] > MaxFunctionCount)
151 MaxFunctionCount = Func.Counts[0];
152
153 if (Show) {
154 if (!ShownFunctions)
155 OS << "Counters:\n";
156 ++ShownFunctions;
157
158 OS << " " << Func.Name << ":\n"
Justin Bogner423380f2014-03-23 20:43:50 +0000159 << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
Justin Bogner9af28ef2014-03-21 17:29:44 +0000160 << " Counters: " << Func.Counts.size() << "\n"
161 << " Function count: " << Func.Counts[0] << "\n";
162 }
163
164 if (Show && ShowCounts)
165 OS << " Block counts: [";
166 for (size_t I = 1, E = Func.Counts.size(); I < E; ++I) {
167 if (Func.Counts[I] > MaxBlockCount)
168 MaxBlockCount = Func.Counts[I];
169 if (Show && ShowCounts)
170 OS << (I == 1 ? "" : ", ") << Func.Counts[I];
171 }
172 if (Show && ShowCounts)
173 OS << "]\n";
174 }
Justin Bognerdb1225d2014-03-23 20:55:53 +0000175 if (Reader->hasError())
176 exitWithError(Reader->getError().message(), Filename);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000177
178 if (ShowAllFunctions || !ShowFunction.empty())
179 OS << "Functions shown: " << ShownFunctions << "\n";
180 OS << "Total functions: " << TotalFunctions << "\n";
181 OS << "Maximum function count: " << MaxFunctionCount << "\n";
182 OS << "Maximum internal block count: " << MaxBlockCount << "\n";
183 return 0;
184}
185
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000186static int showSampleProfile(std::string Filename, bool ShowCounts,
187 bool ShowAllFunctions, std::string ShowFunction,
188 raw_fd_ostream &OS) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000189 using namespace sampleprof;
Diego Novillofcd55602014-11-03 00:51:45 +0000190 auto ReaderOrErr = SampleProfileReader::create(Filename, getGlobalContext());
191 if (std::error_code EC = ReaderOrErr.getError())
Diego Novillod5336ae2014-11-01 00:56:55 +0000192 exitWithError(EC.message(), Filename);
193
Diego Novillofcd55602014-11-03 00:51:45 +0000194 auto Reader = std::move(ReaderOrErr.get());
Diego Novillod5336ae2014-11-01 00:56:55 +0000195 Reader->read();
196 if (ShowAllFunctions || ShowFunction.empty())
197 Reader->dump(OS);
198 else
199 Reader->dumpFunctionProfile(ShowFunction, OS);
200
201 return 0;
202}
203
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000204static int show_main(int argc, const char *argv[]) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000205 cl::opt<std::string> Filename(cl::Positional, cl::Required,
206 cl::desc("<profdata-file>"));
207
208 cl::opt<bool> ShowCounts("counts", cl::init(false),
209 cl::desc("Show counter values for shown functions"));
210 cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
211 cl::desc("Details for every function"));
212 cl::opt<std::string> ShowFunction("function",
213 cl::desc("Details for matching functions"));
214
215 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
216 cl::init("-"), cl::desc("Output file"));
217 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
218 cl::aliasopt(OutputFilename));
219 cl::opt<ProfileKinds> ProfileKind(
220 cl::desc("Profile kind:"), cl::init(instr),
221 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
222 clEnumVal(sample, "Sample profile"), clEnumValEnd));
223
224 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
225
226 if (OutputFilename.empty())
227 OutputFilename = "-";
228
229 std::error_code EC;
230 raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text);
231 if (EC)
232 exitWithError(EC.message(), OutputFilename);
233
234 if (ShowAllFunctions && !ShowFunction.empty())
235 errs() << "warning: -function argument ignored: showing all functions\n";
236
237 if (ProfileKind == instr)
238 return showInstrProfile(Filename, ShowCounts, ShowAllFunctions,
239 ShowFunction, OS);
240 else
241 return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
242 ShowFunction, OS);
243}
244
Justin Bogner618bcea2014-03-19 02:20:46 +0000245int main(int argc, const char *argv[]) {
246 // Print a stack trace if we signal out.
247 sys::PrintStackTraceOnErrorSignal();
248 PrettyStackTraceProgram X(argc, argv);
249 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
250
251 StringRef ProgName(sys::path::filename(argv[0]));
252 if (argc > 1) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000253 int (*func)(int, const char *[]) = nullptr;
Justin Bogner618bcea2014-03-19 02:20:46 +0000254
255 if (strcmp(argv[1], "merge") == 0)
256 func = merge_main;
Justin Bogner9af28ef2014-03-21 17:29:44 +0000257 else if (strcmp(argv[1], "show") == 0)
258 func = show_main;
Justin Bogner618bcea2014-03-19 02:20:46 +0000259
260 if (func) {
261 std::string Invocation(ProgName.str() + " " + argv[1]);
262 argv[1] = Invocation.c_str();
263 return func(argc - 1, argv + 1);
264 }
265
266 if (strcmp(argv[1], "-h") == 0 ||
267 strcmp(argv[1], "-help") == 0 ||
268 strcmp(argv[1], "--help") == 0) {
269
270 errs() << "OVERVIEW: LLVM profile data tools\n\n"
271 << "USAGE: " << ProgName << " <command> [args...]\n"
272 << "USAGE: " << ProgName << " <command> -help\n\n"
Justin Bogner9af28ef2014-03-21 17:29:44 +0000273 << "Available commands: merge, show\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000274 return 0;
275 }
276 }
277
278 if (argc < 2)
279 errs() << ProgName << ": No command specified!\n";
280 else
281 errs() << ProgName << ": Unknown command!\n";
282
Justin Bogner9af28ef2014-03-21 17:29:44 +0000283 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000284 return 1;
285}