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/PrettyStackTrace.h"
26 #include "llvm/Support/Signals.h"
27 #include "llvm/Support/raw_ostream.h"
31 static void exitWithError(const Twine &Message, StringRef Whence = "") {
34 errs() << Whence << ": ";
35 errs() << Message << "\n";
39 enum ProfileKinds { instr, sample };
41 void mergeInstrProfile(const cl::list<std::string> &Inputs,
42 StringRef OutputFilename) {
43 if (OutputFilename.compare("-") == 0)
44 exitWithError("Cannot write indexed profdata format to stdout.");
47 raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None);
49 exitWithError(EC.message(), OutputFilename);
51 InstrProfWriter Writer;
52 for (const auto &Filename : Inputs) {
53 auto ReaderOrErr = InstrProfReader::create(Filename);
54 if (std::error_code ec = ReaderOrErr.getError())
55 exitWithError(ec.message(), Filename);
57 auto Reader = std::move(ReaderOrErr.get());
58 for (const auto &I : *Reader)
59 if (std::error_code EC =
60 Writer.addFunctionCounts(I.Name, I.Hash, I.Counts))
61 errs() << Filename << ": " << I.Name << ": " << EC.message() << "\n";
62 if (Reader->hasError())
63 exitWithError(Reader->getError().message(), Filename);
68 void mergeSampleProfile(const cl::list<std::string> &Inputs,
69 StringRef OutputFilename,
70 sampleprof::SampleProfileFormat OutputFormat) {
71 using namespace sampleprof;
72 auto WriterOrErr = SampleProfileWriter::create(OutputFilename, OutputFormat);
73 if (std::error_code EC = WriterOrErr.getError())
74 exitWithError(EC.message(), OutputFilename);
76 auto Writer = std::move(WriterOrErr.get());
77 StringMap<FunctionSamples> ProfileMap;
78 for (const auto &Filename : Inputs) {
80 SampleProfileReader::create(Filename, getGlobalContext());
81 if (std::error_code EC = ReaderOrErr.getError())
82 exitWithError(EC.message(), Filename);
84 auto Reader = std::move(ReaderOrErr.get());
85 if (std::error_code EC = Reader->read())
86 exitWithError(EC.message(), Filename);
88 StringMap<FunctionSamples> &Profiles = Reader->getProfiles();
89 for (StringMap<FunctionSamples>::iterator I = Profiles.begin(),
92 StringRef FName = I->first();
93 FunctionSamples &Samples = I->second;
94 ProfileMap[FName].merge(Samples);
97 Writer->write(ProfileMap);
100 int merge_main(int argc, const char *argv[]) {
101 cl::list<std::string> Inputs(cl::Positional, cl::Required, cl::OneOrMore,
102 cl::desc("<filenames...>"));
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));
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"),
123 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
125 if (ProfileKind == instr)
126 mergeInstrProfile(Inputs, OutputFilename);
128 mergeSampleProfile(Inputs, OutputFilename, OutputFormat);
133 int showInstrProfile(std::string Filename, bool ShowCounts,
134 bool ShowAllFunctions, std::string ShowFunction,
135 raw_fd_ostream &OS) {
136 auto ReaderOrErr = InstrProfReader::create(Filename);
137 if (std::error_code EC = ReaderOrErr.getError())
138 exitWithError(EC.message(), Filename);
140 auto Reader = std::move(ReaderOrErr.get());
141 uint64_t MaxFunctionCount = 0, MaxBlockCount = 0;
142 size_t ShownFunctions = 0, TotalFunctions = 0;
143 for (const auto &Func : *Reader) {
145 ShowAllFunctions || (!ShowFunction.empty() &&
146 Func.Name.find(ShowFunction) != Func.Name.npos);
149 assert(Func.Counts.size() > 0 && "function missing entry counter");
150 if (Func.Counts[0] > MaxFunctionCount)
151 MaxFunctionCount = Func.Counts[0];
158 OS << " " << Func.Name << ":\n"
159 << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
160 << " Counters: " << Func.Counts.size() << "\n"
161 << " Function count: " << Func.Counts[0] << "\n";
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];
172 if (Show && ShowCounts)
175 if (Reader->hasError())
176 exitWithError(Reader->getError().message(), Filename);
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";
186 int showSampleProfile(std::string Filename, bool ShowCounts,
187 bool ShowAllFunctions, std::string ShowFunction,
188 raw_fd_ostream &OS) {
189 using namespace sampleprof;
190 auto ReaderOrErr = SampleProfileReader::create(Filename, getGlobalContext());
191 if (std::error_code EC = ReaderOrErr.getError())
192 exitWithError(EC.message(), Filename);
194 auto Reader = std::move(ReaderOrErr.get());
196 if (ShowAllFunctions || ShowFunction.empty())
199 Reader->dumpFunctionProfile(ShowFunction, OS);
204 int show_main(int argc, const char *argv[]) {
205 cl::opt<std::string> Filename(cl::Positional, cl::Required,
206 cl::desc("<profdata-file>"));
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"));
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));
224 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
226 if (OutputFilename.empty())
227 OutputFilename = "-";
230 raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text);
232 exitWithError(EC.message(), OutputFilename);
234 if (ShowAllFunctions && !ShowFunction.empty())
235 errs() << "warning: -function argument ignored: showing all functions\n";
237 if (ProfileKind == instr)
238 return showInstrProfile(Filename, ShowCounts, ShowAllFunctions,
241 return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
245 int 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.
251 StringRef ProgName(sys::path::filename(argv[0]));
253 int (*func)(int, const char *[]) = nullptr;
255 if (strcmp(argv[1], "merge") == 0)
257 else if (strcmp(argv[1], "show") == 0)
261 std::string Invocation(ProgName.str() + " " + argv[1]);
262 argv[1] = Invocation.c_str();
263 return func(argc - 1, argv + 1);
266 if (strcmp(argv[1], "-h") == 0 ||
267 strcmp(argv[1], "-help") == 0 ||
268 strcmp(argv[1], "--help") == 0) {
270 errs() << "OVERVIEW: LLVM profile data tools\n\n"
271 << "USAGE: " << ProgName << " <command> [args...]\n"
272 << "USAGE: " << ProgName << " <command> -help\n\n"
273 << "Available commands: merge, show\n";
279 errs() << ProgName << ": No command specified!\n";
281 errs() << ProgName << ": Unknown command!\n";
283 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";