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 raw_fd_ostream Output(OutputFilename.data(), ErrorInfo, sys::fs::F_Text);
51 if (!ErrorInfo.empty())
52 exitWithError(ErrorInfo, OutputFilename);
54 InstrProfWriter Writer;
55 for (const auto &Filename : Inputs) {
56 std::unique_ptr<InstrProfReader> Reader;
57 if (error_code ec = InstrProfReader::create(Filename, Reader))
58 exitWithError(ec.message(), Filename);
60 for (const auto &I : *Reader)
61 if (error_code EC = Writer.addFunctionCounts(I.Name, I.Hash, I.Counts))
62 errs() << Filename << ": " << I.Name << ": " << EC.message() << "\n";
63 if (Reader->hasError())
64 exitWithError(Reader->getError().message(), Filename);
73 HashPrinter(uint64_t Hash) : Hash(Hash) {}
74 void print(raw_ostream &OS) const {
75 char Buf[18], *Cur = Buf;
76 *Cur++ = '0'; *Cur++ = 'x';
77 for (unsigned I = 16; I;) {
78 char Digit = 0xF & (Hash >> (--I * 4));
79 *Cur++ = (Digit < 10 ? '0' + Digit : 'A' + Digit - 10);
84 static raw_ostream &operator<<(raw_ostream &OS, const HashPrinter &Hash) {
89 int show_main(int argc, const char *argv[]) {
90 cl::opt<std::string> Filename(cl::Positional, cl::Required,
91 cl::desc("<profdata-file>"));
93 cl::opt<bool> ShowCounts("counts", cl::init(false),
94 cl::desc("Show counter values for shown functions"));
95 cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
96 cl::desc("Details for every function"));
97 cl::opt<std::string> ShowFunction("function",
98 cl::desc("Details for matching functions"));
100 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
102 cl::desc("Output file"));
103 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
104 cl::aliasopt(OutputFilename));
106 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
108 std::unique_ptr<InstrProfReader> Reader;
109 if (error_code EC = InstrProfReader::create(Filename, Reader))
110 exitWithError(EC.message(), Filename);
112 if (OutputFilename.empty())
113 OutputFilename = "-";
115 std::string ErrorInfo;
116 raw_fd_ostream OS(OutputFilename.data(), ErrorInfo, sys::fs::F_Text);
117 if (!ErrorInfo.empty())
118 exitWithError(ErrorInfo, OutputFilename);
120 if (ShowAllFunctions && !ShowFunction.empty())
121 errs() << "warning: -function argument ignored: showing all functions\n";
123 uint64_t MaxFunctionCount = 0, MaxBlockCount = 0;
124 size_t ShownFunctions = 0, TotalFunctions = 0;
125 for (const auto &Func : *Reader) {
126 bool Show = ShowAllFunctions ||
127 (!ShowFunction.empty() &&
128 Func.Name.find(ShowFunction) != Func.Name.npos);
131 if (Func.Counts[0] > MaxFunctionCount)
132 MaxFunctionCount = Func.Counts[0];
139 OS << " " << Func.Name << ":\n"
140 << " Hash: " << HashPrinter(Func.Hash) << "\n"
141 << " Counters: " << Func.Counts.size() << "\n"
142 << " Function count: " << Func.Counts[0] << "\n";
145 if (Show && ShowCounts)
146 OS << " Block counts: [";
147 for (size_t I = 1, E = Func.Counts.size(); I < E; ++I) {
148 if (Func.Counts[I] > MaxBlockCount)
149 MaxBlockCount = Func.Counts[I];
150 if (Show && ShowCounts)
151 OS << (I == 1 ? "" : ", ") << Func.Counts[I];
153 if (Show && ShowCounts)
157 if (ShowAllFunctions || !ShowFunction.empty())
158 OS << "Functions shown: " << ShownFunctions << "\n";
159 OS << "Total functions: " << TotalFunctions << "\n";
160 OS << "Maximum function count: " << MaxFunctionCount << "\n";
161 OS << "Maximum internal block count: " << MaxBlockCount << "\n";
165 int main(int argc, const char *argv[]) {
166 // Print a stack trace if we signal out.
167 sys::PrintStackTraceOnErrorSignal();
168 PrettyStackTraceProgram X(argc, argv);
169 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
171 StringRef ProgName(sys::path::filename(argv[0]));
173 int (*func)(int, const char *[]) = 0;
175 if (strcmp(argv[1], "merge") == 0)
177 else if (strcmp(argv[1], "show") == 0)
181 std::string Invocation(ProgName.str() + " " + argv[1]);
182 argv[1] = Invocation.c_str();
183 return func(argc - 1, argv + 1);
186 if (strcmp(argv[1], "-h") == 0 ||
187 strcmp(argv[1], "-help") == 0 ||
188 strcmp(argv[1], "--help") == 0) {
190 errs() << "OVERVIEW: LLVM profile data tools\n\n"
191 << "USAGE: " << ProgName << " <command> [args...]\n"
192 << "USAGE: " << ProgName << " <command> -help\n\n"
193 << "Available commands: merge, show\n";
199 errs() << ProgName << ": No command specified!\n";
201 errs() << ProgName << ": Unknown command!\n";
203 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";