1 //===- llvm-profdata.cpp - LLVM profile data tool -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // llvm-profdata merges .profdata files.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/ProfileData/InstrProfReader.h"
17 #include "llvm/ProfileData/InstrProfWriter.h"
18 #include "llvm/ProfileData/SampleProfReader.h"
19 #include "llvm/ProfileData/SampleProfWriter.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/FileSystem.h"
22 #include "llvm/Support/Format.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/Path.h"
26 #include "llvm/Support/PrettyStackTrace.h"
27 #include "llvm/Support/Signals.h"
28 #include "llvm/Support/raw_ostream.h"
32 static void exitWithError(const Twine &Message, StringRef Whence = "") {
35 errs() << Whence << ": ";
36 errs() << Message << "\n";
41 enum ProfileKinds { instr, sample };
44 static void mergeInstrProfile(const cl::list<std::string> &Inputs,
45 StringRef OutputFilename) {
46 if (OutputFilename.compare("-") == 0)
47 exitWithError("Cannot write indexed profdata format to stdout.");
50 raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None);
52 exitWithError(EC.message(), OutputFilename);
54 InstrProfWriter Writer;
55 for (const auto &Filename : Inputs) {
56 auto ReaderOrErr = InstrProfReader::create(Filename);
57 if (std::error_code ec = ReaderOrErr.getError())
58 exitWithError(ec.message(), Filename);
60 auto Reader = std::move(ReaderOrErr.get());
61 for (auto &I : *Reader)
62 if (std::error_code EC = Writer.addRecord(std::move(I)))
63 errs() << Filename << ": " << I.Name << ": " << EC.message() << "\n";
64 if (Reader->hasError())
65 exitWithError(Reader->getError().message(), Filename);
70 static void mergeSampleProfile(const cl::list<std::string> &Inputs,
71 StringRef OutputFilename,
72 sampleprof::SampleProfileFormat OutputFormat) {
73 using namespace sampleprof;
74 auto WriterOrErr = SampleProfileWriter::create(OutputFilename, OutputFormat);
75 if (std::error_code EC = WriterOrErr.getError())
76 exitWithError(EC.message(), OutputFilename);
78 auto Writer = std::move(WriterOrErr.get());
79 StringMap<FunctionSamples> ProfileMap;
80 for (const auto &Filename : Inputs) {
82 SampleProfileReader::create(Filename, getGlobalContext());
83 if (std::error_code EC = ReaderOrErr.getError())
84 exitWithError(EC.message(), Filename);
86 auto Reader = std::move(ReaderOrErr.get());
87 if (std::error_code EC = Reader->read())
88 exitWithError(EC.message(), Filename);
90 StringMap<FunctionSamples> &Profiles = Reader->getProfiles();
91 for (StringMap<FunctionSamples>::iterator I = Profiles.begin(),
94 StringRef FName = I->first();
95 FunctionSamples &Samples = I->second;
96 ProfileMap[FName].merge(Samples);
99 Writer->write(ProfileMap);
102 static int merge_main(int argc, const char *argv[]) {
103 cl::list<std::string> Inputs(cl::Positional, cl::Required, cl::OneOrMore,
104 cl::desc("<filenames...>"));
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));
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"),
125 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
127 if (ProfileKind == instr)
128 mergeInstrProfile(Inputs, OutputFilename);
130 mergeSampleProfile(Inputs, OutputFilename, OutputFormat);
135 static int showInstrProfile(std::string Filename, bool ShowCounts,
136 bool ShowIndirectCallTargets, bool ShowAllFunctions,
137 std::string ShowFunction, raw_fd_ostream &OS) {
138 auto ReaderOrErr = InstrProfReader::create(Filename);
139 if (std::error_code EC = ReaderOrErr.getError())
140 exitWithError(EC.message(), Filename);
142 auto Reader = std::move(ReaderOrErr.get());
143 uint64_t MaxFunctionCount = 0, MaxBlockCount = 0;
144 size_t ShownFunctions = 0, TotalFunctions = 0;
145 for (const auto &Func : *Reader) {
147 ShowAllFunctions || (!ShowFunction.empty() &&
148 Func.Name.find(ShowFunction) != Func.Name.npos);
151 assert(Func.Counts.size() > 0 && "function missing entry counter");
152 if (Func.Counts[0] > MaxFunctionCount)
153 MaxFunctionCount = Func.Counts[0];
160 OS << " " << Func.Name << ":\n"
161 << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
162 << " Counters: " << Func.Counts.size() << "\n"
163 << " Function count: " << Func.Counts[0] << "\n";
164 if (ShowIndirectCallTargets)
165 OS << " Indirect Call Site Count: " << Func.IndirectCallSites.size()
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];
177 if (Show && ShowCounts)
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";
190 if (Reader->hasError())
191 exitWithError(Reader->getError().message(), Filename);
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";
201 static int showSampleProfile(std::string Filename, bool ShowCounts,
202 bool ShowAllFunctions, std::string ShowFunction,
203 raw_fd_ostream &OS) {
204 using namespace sampleprof;
205 auto ReaderOrErr = SampleProfileReader::create(Filename, getGlobalContext());
206 if (std::error_code EC = ReaderOrErr.getError())
207 exitWithError(EC.message(), Filename);
209 auto Reader = std::move(ReaderOrErr.get());
210 if (std::error_code EC = Reader->read())
211 exitWithError(EC.message(), Filename);
213 if (ShowAllFunctions || ShowFunction.empty())
216 Reader->dumpFunctionProfile(ShowFunction, OS);
221 static int show_main(int argc, const char *argv[]) {
222 cl::opt<std::string> Filename(cl::Positional, cl::Required,
223 cl::desc("<profdata-file>"));
225 cl::opt<bool> ShowCounts("counts", cl::init(false),
226 cl::desc("Show counter values for shown functions"));
227 cl::opt<bool> ShowIndirectCallTargets(
228 "ic-targets", cl::init(false),
229 cl::desc("Show indirect call site target values for shown functions"));
230 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"));
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));
244 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
246 if (OutputFilename.empty())
247 OutputFilename = "-";
250 raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text);
252 exitWithError(EC.message(), OutputFilename);
254 if (ShowAllFunctions && !ShowFunction.empty())
255 errs() << "warning: -function argument ignored: showing all functions\n";
257 if (ProfileKind == instr)
258 return showInstrProfile(Filename, ShowCounts, ShowIndirectCallTargets,
259 ShowAllFunctions, ShowFunction, OS);
261 return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
265 int 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.
271 StringRef ProgName(sys::path::filename(argv[0]));
273 int (*func)(int, const char *[]) = nullptr;
275 if (strcmp(argv[1], "merge") == 0)
277 else if (strcmp(argv[1], "show") == 0)
281 std::string Invocation(ProgName.str() + " " + argv[1]);
282 argv[1] = Invocation.c_str();
283 return func(argc - 1, argv + 1);
286 if (strcmp(argv[1], "-h") == 0 ||
287 strcmp(argv[1], "-help") == 0 ||
288 strcmp(argv[1], "--help") == 0) {
290 errs() << "OVERVIEW: LLVM profile data tools\n\n"
291 << "USAGE: " << ProgName << " <command> [args...]\n"
292 << "USAGE: " << ProgName << " <command> -help\n\n"
293 << "Available commands: merge, show\n";
299 errs() << ProgName << ": No command specified!\n";
301 errs() << ProgName << ": Unknown command!\n";
303 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";