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 (const auto &I : *Reader)
62 if (std::error_code EC =
63 Writer.addFunctionCounts(I.Name, I.Hash, I.Counts))
64 errs() << Filename << ": " << I.Name << ": " << EC.message() << "\n";
65 if (Reader->hasError())
66 exitWithError(Reader->getError().message(), Filename);
71 static void mergeSampleProfile(const cl::list<std::string> &Inputs,
72 StringRef OutputFilename,
73 sampleprof::SampleProfileFormat OutputFormat) {
74 using namespace sampleprof;
75 auto WriterOrErr = SampleProfileWriter::create(OutputFilename, OutputFormat);
76 if (std::error_code EC = WriterOrErr.getError())
77 exitWithError(EC.message(), OutputFilename);
79 auto Writer = std::move(WriterOrErr.get());
80 StringMap<FunctionSamples> ProfileMap;
81 for (const auto &Filename : Inputs) {
83 SampleProfileReader::create(Filename, getGlobalContext());
84 if (std::error_code EC = ReaderOrErr.getError())
85 exitWithError(EC.message(), Filename);
87 auto Reader = std::move(ReaderOrErr.get());
88 if (std::error_code EC = Reader->read())
89 exitWithError(EC.message(), Filename);
91 StringMap<FunctionSamples> &Profiles = Reader->getProfiles();
92 for (StringMap<FunctionSamples>::iterator I = Profiles.begin(),
95 StringRef FName = I->first();
96 FunctionSamples &Samples = I->second;
97 ProfileMap[FName].merge(Samples);
100 Writer->write(ProfileMap);
103 static int merge_main(int argc, const char *argv[]) {
104 cl::list<std::string> Inputs(cl::Positional, cl::Required, cl::OneOrMore,
105 cl::desc("<filenames...>"));
107 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
108 cl::init("-"), cl::Required,
109 cl::desc("Output file"));
110 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
111 cl::aliasopt(OutputFilename));
112 cl::opt<ProfileKinds> ProfileKind(
113 cl::desc("Profile kind:"), cl::init(instr),
114 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
115 clEnumVal(sample, "Sample profile"), clEnumValEnd));
117 cl::opt<sampleprof::SampleProfileFormat> OutputFormat(
118 cl::desc("Format of output profile (only meaningful with --sample)"),
119 cl::init(sampleprof::SPF_Binary),
120 cl::values(clEnumValN(sampleprof::SPF_Binary, "binary",
121 "Binary encoding (default)"),
122 clEnumValN(sampleprof::SPF_Text, "text", "Text encoding"),
123 clEnumValN(sampleprof::SPF_GCC, "gcc", "GCC encoding"),
126 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
128 if (ProfileKind == instr)
129 mergeInstrProfile(Inputs, OutputFilename);
131 mergeSampleProfile(Inputs, OutputFilename, OutputFormat);
136 static int showInstrProfile(std::string Filename, bool ShowCounts,
137 bool ShowAllFunctions, std::string ShowFunction,
138 raw_fd_ostream &OS) {
139 auto ReaderOrErr = InstrProfReader::create(Filename);
140 if (std::error_code EC = ReaderOrErr.getError())
141 exitWithError(EC.message(), Filename);
143 auto Reader = std::move(ReaderOrErr.get());
144 uint64_t MaxFunctionCount = 0, MaxBlockCount = 0;
145 size_t ShownFunctions = 0, TotalFunctions = 0;
146 for (const auto &Func : *Reader) {
148 ShowAllFunctions || (!ShowFunction.empty() &&
149 Func.Name.find(ShowFunction) != Func.Name.npos);
152 assert(Func.Counts.size() > 0 && "function missing entry counter");
153 if (Func.Counts[0] > MaxFunctionCount)
154 MaxFunctionCount = Func.Counts[0];
161 OS << " " << Func.Name << ":\n"
162 << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
163 << " Counters: " << Func.Counts.size() << "\n"
164 << " Function count: " << Func.Counts[0] << "\n";
167 if (Show && ShowCounts)
168 OS << " Block counts: [";
169 for (size_t I = 1, E = Func.Counts.size(); I < E; ++I) {
170 if (Func.Counts[I] > MaxBlockCount)
171 MaxBlockCount = Func.Counts[I];
172 if (Show && ShowCounts)
173 OS << (I == 1 ? "" : ", ") << Func.Counts[I];
175 if (Show && ShowCounts)
178 if (Reader->hasError())
179 exitWithError(Reader->getError().message(), Filename);
181 if (ShowAllFunctions || !ShowFunction.empty())
182 OS << "Functions shown: " << ShownFunctions << "\n";
183 OS << "Total functions: " << TotalFunctions << "\n";
184 OS << "Maximum function count: " << MaxFunctionCount << "\n";
185 OS << "Maximum internal block count: " << MaxBlockCount << "\n";
189 static int showSampleProfile(std::string Filename, bool ShowCounts,
190 bool ShowAllFunctions, std::string ShowFunction,
191 raw_fd_ostream &OS) {
192 using namespace sampleprof;
193 auto ReaderOrErr = SampleProfileReader::create(Filename, getGlobalContext());
194 if (std::error_code EC = ReaderOrErr.getError())
195 exitWithError(EC.message(), Filename);
197 auto Reader = std::move(ReaderOrErr.get());
198 if (std::error_code EC = Reader->read())
199 exitWithError(EC.message(), Filename);
201 if (ShowAllFunctions || ShowFunction.empty())
204 Reader->dumpFunctionProfile(ShowFunction, OS);
209 static int show_main(int argc, const char *argv[]) {
210 cl::opt<std::string> Filename(cl::Positional, cl::Required,
211 cl::desc("<profdata-file>"));
213 cl::opt<bool> ShowCounts("counts", cl::init(false),
214 cl::desc("Show counter values for shown functions"));
215 cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
216 cl::desc("Details for every function"));
217 cl::opt<std::string> ShowFunction("function",
218 cl::desc("Details for matching functions"));
220 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
221 cl::init("-"), cl::desc("Output file"));
222 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
223 cl::aliasopt(OutputFilename));
224 cl::opt<ProfileKinds> ProfileKind(
225 cl::desc("Profile kind:"), cl::init(instr),
226 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
227 clEnumVal(sample, "Sample profile"), clEnumValEnd));
229 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
231 if (OutputFilename.empty())
232 OutputFilename = "-";
235 raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text);
237 exitWithError(EC.message(), OutputFilename);
239 if (ShowAllFunctions && !ShowFunction.empty())
240 errs() << "warning: -function argument ignored: showing all functions\n";
242 if (ProfileKind == instr)
243 return showInstrProfile(Filename, ShowCounts, ShowAllFunctions,
246 return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
250 int main(int argc, const char *argv[]) {
251 // Print a stack trace if we signal out.
252 sys::PrintStackTraceOnErrorSignal();
253 PrettyStackTraceProgram X(argc, argv);
254 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
256 StringRef ProgName(sys::path::filename(argv[0]));
258 int (*func)(int, const char *[]) = nullptr;
260 if (strcmp(argv[1], "merge") == 0)
262 else if (strcmp(argv[1], "show") == 0)
266 std::string Invocation(ProgName.str() + " " + argv[1]);
267 argv[1] = Invocation.c_str();
268 return func(argc - 1, argv + 1);
271 if (strcmp(argv[1], "-h") == 0 ||
272 strcmp(argv[1], "-help") == 0 ||
273 strcmp(argv[1], "--help") == 0) {
275 errs() << "OVERVIEW: LLVM profile data tools\n\n"
276 << "USAGE: " << ProgName << " <command> [args...]\n"
277 << "USAGE: " << ProgName << " <command> -help\n\n"
278 << "Available commands: merge, show\n";
284 errs() << ProgName << ": No command specified!\n";
286 errs() << ProgName << ": Unknown command!\n";
288 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";