blob: ce9ae9ae4f62d1c6f433cc268465e449eb118b12 [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;
50 raw_fd_ostream Output(OutputFilename.data(), ErrorInfo, sys::fs::F_Text);
51 if (!ErrorInfo.empty())
52 exitWithError(ErrorInfo, OutputFilename);
53
Justin Bognerb9bd7f82014-03-21 17:46:22 +000054 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);
Justin Bognerf8d79192014-03-21 17:24:48 +000059
Justin Bognerb9bd7f82014-03-21 17:46:22 +000060 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);
Justin Bognerbfee8d42014-03-12 20:14:17 +000065 }
Justin Bognerb9bd7f82014-03-21 17:46:22 +000066 Writer.write(Output);
Justin Bognerbfee8d42014-03-12 20:14:17 +000067
Justin Bognerec49f982014-03-12 22:00:57 +000068 return 0;
Justin Bognerbfee8d42014-03-12 20:14:17 +000069}
Justin Bogner618bcea2014-03-19 02:20:46 +000070
Justin Bogner9af28ef2014-03-21 17:29:44 +000071struct HashPrinter {
72 uint64_t Hash;
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);
80 }
81 OS.write(Buf, 18);
82 }
83};
84static raw_ostream &operator<<(raw_ostream &OS, const HashPrinter &Hash) {
85 Hash.print(OS);
86 return OS;
87}
88
89int show_main(int argc, const char *argv[]) {
90 cl::opt<std::string> Filename(cl::Positional, cl::Required,
91 cl::desc("<profdata-file>"));
92
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"));
99
100 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
101 cl::init("-"),
102 cl::desc("Output file"));
103 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
104 cl::aliasopt(OutputFilename));
105
106 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
107
108 std::unique_ptr<InstrProfReader> Reader;
109 if (error_code EC = InstrProfReader::create(Filename, Reader))
110 exitWithError(EC.message(), Filename);
111
112 if (OutputFilename.empty())
113 OutputFilename = "-";
114
115 std::string ErrorInfo;
116 raw_fd_ostream OS(OutputFilename.data(), ErrorInfo, sys::fs::F_Text);
117 if (!ErrorInfo.empty())
118 exitWithError(ErrorInfo, OutputFilename);
119
120 if (ShowAllFunctions && !ShowFunction.empty())
121 errs() << "warning: -function argument ignored: showing all functions\n";
122
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);
129
130 ++TotalFunctions;
131 if (Func.Counts[0] > MaxFunctionCount)
132 MaxFunctionCount = Func.Counts[0];
133
134 if (Show) {
135 if (!ShownFunctions)
136 OS << "Counters:\n";
137 ++ShownFunctions;
138
139 OS << " " << Func.Name << ":\n"
140 << " Hash: " << HashPrinter(Func.Hash) << "\n"
141 << " Counters: " << Func.Counts.size() << "\n"
142 << " Function count: " << Func.Counts[0] << "\n";
143 }
144
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];
152 }
153 if (Show && ShowCounts)
154 OS << "]\n";
155 }
156
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";
162 return 0;
163}
164
Justin Bogner618bcea2014-03-19 02:20:46 +0000165int 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.
170
171 StringRef ProgName(sys::path::filename(argv[0]));
172 if (argc > 1) {
173 int (*func)(int, const char *[]) = 0;
174
175 if (strcmp(argv[1], "merge") == 0)
176 func = merge_main;
Justin Bogner9af28ef2014-03-21 17:29:44 +0000177 else if (strcmp(argv[1], "show") == 0)
178 func = show_main;
Justin Bogner618bcea2014-03-19 02:20:46 +0000179
180 if (func) {
181 std::string Invocation(ProgName.str() + " " + argv[1]);
182 argv[1] = Invocation.c_str();
183 return func(argc - 1, argv + 1);
184 }
185
186 if (strcmp(argv[1], "-h") == 0 ||
187 strcmp(argv[1], "-help") == 0 ||
188 strcmp(argv[1], "--help") == 0) {
189
190 errs() << "OVERVIEW: LLVM profile data tools\n\n"
191 << "USAGE: " << ProgName << " <command> [args...]\n"
192 << "USAGE: " << ProgName << " <command> -help\n\n"
Justin Bogner9af28ef2014-03-21 17:29:44 +0000193 << "Available commands: merge, show\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000194 return 0;
195 }
196 }
197
198 if (argc < 2)
199 errs() << ProgName << ": No command specified!\n";
200 else
201 errs() << ProgName << ": Unknown command!\n";
202
Justin Bogner9af28ef2014-03-21 17:29:44 +0000203 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000204 return 1;
205}