blob: 42783d00ff0cfdab78f75b76efcd09da4b5b12ed [file] [log] [blame]
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +00001//===- 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
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000014#include "llvm/ADT/StringRef.h"
Justin Bognerf8d79192014-03-21 17:24:48 +000015#include "llvm/ProfileData/InstrProfReader.h"
Justin Bognerb9bd7f82014-03-21 17:46:22 +000016#include "llvm/ProfileData/InstrProfWriter.h"
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000017#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"
23
24using namespace llvm;
25
Justin Bognerf8d79192014-03-21 17:24:48 +000026static void exitWithError(const Twine &Message, StringRef Whence = "") {
27 errs() << "error: ";
28 if (!Whence.empty())
29 errs() << Whence << ": ";
30 errs() << Message << "\n";
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000031 ::exit(1);
32}
33
Justin Bogner618bcea2014-03-19 02:20:46 +000034int merge_main(int argc, const char *argv[]) {
Justin Bognerb9bd7f82014-03-21 17:46:22 +000035 cl::list<std::string> Inputs(cl::Positional, cl::Required, cl::OneOrMore,
36 cl::desc("<filenames...>"));
Justin Bogner618bcea2014-03-19 02:20:46 +000037
38 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
39 cl::init("-"),
40 cl::desc("Output file"));
41 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
42 cl::aliasopt(OutputFilename));
Justin Bognerbfee8d42014-03-12 20:14:17 +000043
Justin Bognerec49f982014-03-12 22:00:57 +000044 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
Justin Bognerbfee8d42014-03-12 20:14:17 +000045
Justin Bognerec49f982014-03-12 22:00:57 +000046 if (OutputFilename.empty())
47 OutputFilename = "-";
48
49 std::string ErrorInfo;
NAKAMURA Takumi4baaa6a2014-03-22 05:38:22 +000050 // FIXME: F_Text would be available if line_iterator could accept CRLF.
51 raw_fd_ostream Output(OutputFilename.data(), ErrorInfo, sys::fs::F_None);
Justin Bognerec49f982014-03-12 22:00:57 +000052 if (!ErrorInfo.empty())
53 exitWithError(ErrorInfo, OutputFilename);
54
Justin Bognerb9bd7f82014-03-21 17:46:22 +000055 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);
Justin Bognerf8d79192014-03-21 17:24:48 +000060
Justin Bognerb9bd7f82014-03-21 17:46:22 +000061 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);
Justin Bognerbfee8d42014-03-12 20:14:17 +000066 }
Justin Bognerb9bd7f82014-03-21 17:46:22 +000067 Writer.write(Output);
Justin Bognerbfee8d42014-03-12 20:14:17 +000068
Justin Bognerec49f982014-03-12 22:00:57 +000069 return 0;
Justin Bognerbfee8d42014-03-12 20:14:17 +000070}
Justin Bogner618bcea2014-03-19 02:20:46 +000071
Justin Bogner9af28ef2014-03-21 17:29:44 +000072struct HashPrinter {
73 uint64_t Hash;
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);
81 }
82 OS.write(Buf, 18);
83 }
84};
85static raw_ostream &operator<<(raw_ostream &OS, const HashPrinter &Hash) {
86 Hash.print(OS);
87 return OS;
88}
89
90int show_main(int argc, const char *argv[]) {
91 cl::opt<std::string> Filename(cl::Positional, cl::Required,
92 cl::desc("<profdata-file>"));
93
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"));
100
101 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
102 cl::init("-"),
103 cl::desc("Output file"));
104 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
105 cl::aliasopt(OutputFilename));
106
107 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
108
109 std::unique_ptr<InstrProfReader> Reader;
110 if (error_code EC = InstrProfReader::create(Filename, Reader))
111 exitWithError(EC.message(), Filename);
112
113 if (OutputFilename.empty())
114 OutputFilename = "-";
115
116 std::string ErrorInfo;
117 raw_fd_ostream OS(OutputFilename.data(), ErrorInfo, sys::fs::F_Text);
118 if (!ErrorInfo.empty())
119 exitWithError(ErrorInfo, OutputFilename);
120
121 if (ShowAllFunctions && !ShowFunction.empty())
122 errs() << "warning: -function argument ignored: showing all functions\n";
123
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);
130
131 ++TotalFunctions;
132 if (Func.Counts[0] > MaxFunctionCount)
133 MaxFunctionCount = Func.Counts[0];
134
135 if (Show) {
136 if (!ShownFunctions)
137 OS << "Counters:\n";
138 ++ShownFunctions;
139
140 OS << " " << Func.Name << ":\n"
141 << " Hash: " << HashPrinter(Func.Hash) << "\n"
142 << " Counters: " << Func.Counts.size() << "\n"
143 << " Function count: " << Func.Counts[0] << "\n";
144 }
145
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];
153 }
154 if (Show && ShowCounts)
155 OS << "]\n";
156 }
157
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";
163 return 0;
164}
165
Justin Bogner618bcea2014-03-19 02:20:46 +0000166int 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.
171
172 StringRef ProgName(sys::path::filename(argv[0]));
173 if (argc > 1) {
174 int (*func)(int, const char *[]) = 0;
175
176 if (strcmp(argv[1], "merge") == 0)
177 func = merge_main;
Justin Bogner9af28ef2014-03-21 17:29:44 +0000178 else if (strcmp(argv[1], "show") == 0)
179 func = show_main;
Justin Bogner618bcea2014-03-19 02:20:46 +0000180
181 if (func) {
182 std::string Invocation(ProgName.str() + " " + argv[1]);
183 argv[1] = Invocation.c_str();
184 return func(argc - 1, argv + 1);
185 }
186
187 if (strcmp(argv[1], "-h") == 0 ||
188 strcmp(argv[1], "-help") == 0 ||
189 strcmp(argv[1], "--help") == 0) {
190
191 errs() << "OVERVIEW: LLVM profile data tools\n\n"
192 << "USAGE: " << ProgName << " <command> [args...]\n"
193 << "USAGE: " << ProgName << " <command> -help\n\n"
Justin Bogner9af28ef2014-03-21 17:29:44 +0000194 << "Available commands: merge, show\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000195 return 0;
196 }
197 }
198
199 if (argc < 2)
200 errs() << ProgName << ": No command specified!\n";
201 else
202 errs() << ProgName << ": Unknown command!\n";
203
Justin Bogner9af28ef2014-03-21 17:29:44 +0000204 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000205 return 1;
206}