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/ProfileData/InstrProfReader.h"
16 #include "llvm/ProfileData/InstrProfWriter.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/ManagedStatic.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/PrettyStackTrace.h"
21 #include "llvm/Support/Signals.h"
22 #include "llvm/Support/raw_ostream.h"
26 static void exitWithError(const Twine &Message, StringRef Whence = "") {
29 errs() << Whence << ": ";
30 errs() << Message << "\n";
34 int merge_main(int argc, const char *argv[]) {
35 cl::list<std::string> Inputs(cl::Positional, cl::Required, cl::OneOrMore,
36 cl::desc("<filenames...>"));
38 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
40 cl::desc("Output file"));
41 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
42 cl::aliasopt(OutputFilename));
44 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
46 if (OutputFilename.empty())
49 std::string ErrorInfo;
50 // FIXME: F_Text would be available if line_iterator could accept CRLF.
51 raw_fd_ostream Output(OutputFilename.data(), ErrorInfo, sys::fs::F_None);
52 if (!ErrorInfo.empty())
53 exitWithError(ErrorInfo, OutputFilename);
55 InstrProfWriter Writer;
56 for (const auto &Filename : Inputs) {
57 std::unique_ptr<InstrProfReader> Reader;
58 if (error_code ec = InstrProfReader::create(Filename, Reader))
59 exitWithError(ec.message(), Filename);
61 for (const auto &I : *Reader)
62 if (error_code EC = Writer.addFunctionCounts(I.Name, I.Hash, I.Counts))
63 errs() << Filename << ": " << I.Name << ": " << EC.message() << "\n";
64 if (Reader->hasError())
65 exitWithError(Reader->getError().message(), Filename);
74 HashPrinter(uint64_t Hash) : Hash(Hash) {}
75 void print(raw_ostream &OS) const {
76 char Buf[18], *Cur = Buf;
77 *Cur++ = '0'; *Cur++ = 'x';
78 for (unsigned I = 16; I;) {
79 char Digit = 0xF & (Hash >> (--I * 4));
80 *Cur++ = (Digit < 10 ? '0' + Digit : 'A' + Digit - 10);
85 static raw_ostream &operator<<(raw_ostream &OS, const HashPrinter &Hash) {
90 int show_main(int argc, const char *argv[]) {
91 cl::opt<std::string> Filename(cl::Positional, cl::Required,
92 cl::desc("<profdata-file>"));
94 cl::opt<bool> ShowCounts("counts", cl::init(false),
95 cl::desc("Show counter values for shown functions"));
96 cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
97 cl::desc("Details for every function"));
98 cl::opt<std::string> ShowFunction("function",
99 cl::desc("Details for matching functions"));
101 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
103 cl::desc("Output file"));
104 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
105 cl::aliasopt(OutputFilename));
107 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
109 std::unique_ptr<InstrProfReader> Reader;
110 if (error_code EC = InstrProfReader::create(Filename, Reader))
111 exitWithError(EC.message(), Filename);
113 if (OutputFilename.empty())
114 OutputFilename = "-";
116 std::string ErrorInfo;
117 raw_fd_ostream OS(OutputFilename.data(), ErrorInfo, sys::fs::F_Text);
118 if (!ErrorInfo.empty())
119 exitWithError(ErrorInfo, OutputFilename);
121 if (ShowAllFunctions && !ShowFunction.empty())
122 errs() << "warning: -function argument ignored: showing all functions\n";
124 uint64_t MaxFunctionCount = 0, MaxBlockCount = 0;
125 size_t ShownFunctions = 0, TotalFunctions = 0;
126 for (const auto &Func : *Reader) {
127 bool Show = ShowAllFunctions ||
128 (!ShowFunction.empty() &&
129 Func.Name.find(ShowFunction) != Func.Name.npos);
132 if (Func.Counts[0] > MaxFunctionCount)
133 MaxFunctionCount = Func.Counts[0];
140 OS << " " << Func.Name << ":\n"
141 << " Hash: " << HashPrinter(Func.Hash) << "\n"
142 << " Counters: " << Func.Counts.size() << "\n"
143 << " Function count: " << Func.Counts[0] << "\n";
146 if (Show && ShowCounts)
147 OS << " Block counts: [";
148 for (size_t I = 1, E = Func.Counts.size(); I < E; ++I) {
149 if (Func.Counts[I] > MaxBlockCount)
150 MaxBlockCount = Func.Counts[I];
151 if (Show && ShowCounts)
152 OS << (I == 1 ? "" : ", ") << Func.Counts[I];
154 if (Show && ShowCounts)
158 if (ShowAllFunctions || !ShowFunction.empty())
159 OS << "Functions shown: " << ShownFunctions << "\n";
160 OS << "Total functions: " << TotalFunctions << "\n";
161 OS << "Maximum function count: " << MaxFunctionCount << "\n";
162 OS << "Maximum internal block count: " << MaxBlockCount << "\n";
166 int main(int argc, const char *argv[]) {
167 // Print a stack trace if we signal out.
168 sys::PrintStackTraceOnErrorSignal();
169 PrettyStackTraceProgram X(argc, argv);
170 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
172 StringRef ProgName(sys::path::filename(argv[0]));
174 int (*func)(int, const char *[]) = 0;
176 if (strcmp(argv[1], "merge") == 0)
178 else if (strcmp(argv[1], "show") == 0)
182 std::string Invocation(ProgName.str() + " " + argv[1]);
183 argv[1] = Invocation.c_str();
184 return func(argc - 1, argv + 1);
187 if (strcmp(argv[1], "-h") == 0 ||
188 strcmp(argv[1], "-help") == 0 ||
189 strcmp(argv[1], "--help") == 0) {
191 errs() << "OVERVIEW: LLVM profile data tools\n\n"
192 << "USAGE: " << ProgName << " <command> [args...]\n"
193 << "USAGE: " << ProgName << " <command> -help\n\n"
194 << "Available commands: merge, show\n";
200 errs() << ProgName << ": No command specified!\n";
202 errs() << ProgName << ": Unknown command!\n";
204 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";