]> git.proxmox.com Git - rustc.git/blob - src/llvm/tools/llvm-profdata/llvm-profdata.cpp
Imported Upstream version 1.0.0+dfsg1
[rustc.git] / src / llvm / tools / llvm-profdata / llvm-profdata.cpp
1 //===- 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
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"
28
29 using namespace llvm;
30
31 static void exitWithError(const Twine &Message, StringRef Whence = "") {
32 errs() << "error: ";
33 if (!Whence.empty())
34 errs() << Whence << ": ";
35 errs() << Message << "\n";
36 ::exit(1);
37 }
38
39 enum ProfileKinds { instr, sample };
40
41 void mergeInstrProfile(cl::list<std::string> Inputs, StringRef OutputFilename) {
42 if (OutputFilename.compare("-") == 0)
43 exitWithError("Cannot write indexed profdata format to stdout.");
44
45 std::error_code EC;
46 raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None);
47 if (EC)
48 exitWithError(EC.message(), OutputFilename);
49
50 InstrProfWriter Writer;
51 for (const auto &Filename : Inputs) {
52 auto ReaderOrErr = InstrProfReader::create(Filename);
53 if (std::error_code ec = ReaderOrErr.getError())
54 exitWithError(ec.message(), Filename);
55
56 auto Reader = std::move(ReaderOrErr.get());
57 for (const auto &I : *Reader)
58 if (std::error_code EC =
59 Writer.addFunctionCounts(I.Name, I.Hash, I.Counts))
60 errs() << Filename << ": " << I.Name << ": " << EC.message() << "\n";
61 if (Reader->hasError())
62 exitWithError(Reader->getError().message(), Filename);
63 }
64 Writer.write(Output);
65 }
66
67 void mergeSampleProfile(cl::list<std::string> Inputs, StringRef OutputFilename,
68 sampleprof::SampleProfileFormat OutputFormat) {
69 using namespace sampleprof;
70 auto WriterOrErr = SampleProfileWriter::create(OutputFilename, OutputFormat);
71 if (std::error_code EC = WriterOrErr.getError())
72 exitWithError(EC.message(), OutputFilename);
73
74 auto Writer = std::move(WriterOrErr.get());
75 StringMap<FunctionSamples> ProfileMap;
76 for (const auto &Filename : Inputs) {
77 auto ReaderOrErr =
78 SampleProfileReader::create(Filename, getGlobalContext());
79 if (std::error_code EC = ReaderOrErr.getError())
80 exitWithError(EC.message(), Filename);
81
82 auto Reader = std::move(ReaderOrErr.get());
83 if (std::error_code EC = Reader->read())
84 exitWithError(EC.message(), Filename);
85
86 StringMap<FunctionSamples> &Profiles = Reader->getProfiles();
87 for (StringMap<FunctionSamples>::iterator I = Profiles.begin(),
88 E = Profiles.end();
89 I != E; ++I) {
90 StringRef FName = I->first();
91 FunctionSamples &Samples = I->second;
92 ProfileMap[FName].merge(Samples);
93 }
94 }
95 Writer->write(ProfileMap);
96 }
97
98 int merge_main(int argc, const char *argv[]) {
99 cl::list<std::string> Inputs(cl::Positional, cl::Required, cl::OneOrMore,
100 cl::desc("<filenames...>"));
101
102 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
103 cl::init("-"), cl::Required,
104 cl::desc("Output file"));
105 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
106 cl::aliasopt(OutputFilename));
107 cl::opt<ProfileKinds> ProfileKind(
108 cl::desc("Profile kind:"), cl::init(instr),
109 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
110 clEnumVal(sample, "Sample profile"), clEnumValEnd));
111
112 cl::opt<sampleprof::SampleProfileFormat> OutputFormat(
113 cl::desc("Format of output profile (only meaningful with --sample)"),
114 cl::init(sampleprof::SPF_Binary),
115 cl::values(clEnumValN(sampleprof::SPF_Binary, "binary",
116 "Binary encoding (default)"),
117 clEnumValN(sampleprof::SPF_Text, "text", "Text encoding"),
118 clEnumValN(sampleprof::SPF_GCC, "gcc", "GCC encoding"),
119 clEnumValEnd));
120
121 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
122
123 if (ProfileKind == instr)
124 mergeInstrProfile(Inputs, OutputFilename);
125 else
126 mergeSampleProfile(Inputs, OutputFilename, OutputFormat);
127
128 return 0;
129 }
130
131 int showInstrProfile(std::string Filename, bool ShowCounts,
132 bool ShowAllFunctions, std::string ShowFunction,
133 raw_fd_ostream &OS) {
134 auto ReaderOrErr = InstrProfReader::create(Filename);
135 if (std::error_code EC = ReaderOrErr.getError())
136 exitWithError(EC.message(), Filename);
137
138 auto Reader = std::move(ReaderOrErr.get());
139 uint64_t MaxFunctionCount = 0, MaxBlockCount = 0;
140 size_t ShownFunctions = 0, TotalFunctions = 0;
141 for (const auto &Func : *Reader) {
142 bool Show =
143 ShowAllFunctions || (!ShowFunction.empty() &&
144 Func.Name.find(ShowFunction) != Func.Name.npos);
145
146 ++TotalFunctions;
147 assert(Func.Counts.size() > 0 && "function missing entry counter");
148 if (Func.Counts[0] > MaxFunctionCount)
149 MaxFunctionCount = Func.Counts[0];
150
151 if (Show) {
152 if (!ShownFunctions)
153 OS << "Counters:\n";
154 ++ShownFunctions;
155
156 OS << " " << Func.Name << ":\n"
157 << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
158 << " Counters: " << Func.Counts.size() << "\n"
159 << " Function count: " << Func.Counts[0] << "\n";
160 }
161
162 if (Show && ShowCounts)
163 OS << " Block counts: [";
164 for (size_t I = 1, E = Func.Counts.size(); I < E; ++I) {
165 if (Func.Counts[I] > MaxBlockCount)
166 MaxBlockCount = Func.Counts[I];
167 if (Show && ShowCounts)
168 OS << (I == 1 ? "" : ", ") << Func.Counts[I];
169 }
170 if (Show && ShowCounts)
171 OS << "]\n";
172 }
173 if (Reader->hasError())
174 exitWithError(Reader->getError().message(), Filename);
175
176 if (ShowAllFunctions || !ShowFunction.empty())
177 OS << "Functions shown: " << ShownFunctions << "\n";
178 OS << "Total functions: " << TotalFunctions << "\n";
179 OS << "Maximum function count: " << MaxFunctionCount << "\n";
180 OS << "Maximum internal block count: " << MaxBlockCount << "\n";
181 return 0;
182 }
183
184 int showSampleProfile(std::string Filename, bool ShowCounts,
185 bool ShowAllFunctions, std::string ShowFunction,
186 raw_fd_ostream &OS) {
187 using namespace sampleprof;
188 auto ReaderOrErr = SampleProfileReader::create(Filename, getGlobalContext());
189 if (std::error_code EC = ReaderOrErr.getError())
190 exitWithError(EC.message(), Filename);
191
192 auto Reader = std::move(ReaderOrErr.get());
193 Reader->read();
194 if (ShowAllFunctions || ShowFunction.empty())
195 Reader->dump(OS);
196 else
197 Reader->dumpFunctionProfile(ShowFunction, OS);
198
199 return 0;
200 }
201
202 int show_main(int argc, const char *argv[]) {
203 cl::opt<std::string> Filename(cl::Positional, cl::Required,
204 cl::desc("<profdata-file>"));
205
206 cl::opt<bool> ShowCounts("counts", cl::init(false),
207 cl::desc("Show counter values for shown functions"));
208 cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
209 cl::desc("Details for every function"));
210 cl::opt<std::string> ShowFunction("function",
211 cl::desc("Details for matching functions"));
212
213 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
214 cl::init("-"), cl::desc("Output file"));
215 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
216 cl::aliasopt(OutputFilename));
217 cl::opt<ProfileKinds> ProfileKind(
218 cl::desc("Profile kind:"), cl::init(instr),
219 cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
220 clEnumVal(sample, "Sample profile"), clEnumValEnd));
221
222 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
223
224 if (OutputFilename.empty())
225 OutputFilename = "-";
226
227 std::error_code EC;
228 raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text);
229 if (EC)
230 exitWithError(EC.message(), OutputFilename);
231
232 if (ShowAllFunctions && !ShowFunction.empty())
233 errs() << "warning: -function argument ignored: showing all functions\n";
234
235 if (ProfileKind == instr)
236 return showInstrProfile(Filename, ShowCounts, ShowAllFunctions,
237 ShowFunction, OS);
238 else
239 return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
240 ShowFunction, OS);
241 }
242
243 int main(int argc, const char *argv[]) {
244 // Print a stack trace if we signal out.
245 sys::PrintStackTraceOnErrorSignal();
246 PrettyStackTraceProgram X(argc, argv);
247 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
248
249 StringRef ProgName(sys::path::filename(argv[0]));
250 if (argc > 1) {
251 int (*func)(int, const char *[]) = nullptr;
252
253 if (strcmp(argv[1], "merge") == 0)
254 func = merge_main;
255 else if (strcmp(argv[1], "show") == 0)
256 func = show_main;
257
258 if (func) {
259 std::string Invocation(ProgName.str() + " " + argv[1]);
260 argv[1] = Invocation.c_str();
261 return func(argc - 1, argv + 1);
262 }
263
264 if (strcmp(argv[1], "-h") == 0 ||
265 strcmp(argv[1], "-help") == 0 ||
266 strcmp(argv[1], "--help") == 0) {
267
268 errs() << "OVERVIEW: LLVM profile data tools\n\n"
269 << "USAGE: " << ProgName << " <command> [args...]\n"
270 << "USAGE: " << ProgName << " <command> -help\n\n"
271 << "Available commands: merge, show\n";
272 return 0;
273 }
274 }
275
276 if (argc < 2)
277 errs() << ProgName << ": No command specified!\n";
278 else
279 errs() << ProgName << ": Unknown command!\n";
280
281 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";
282 return 1;
283 }