llvm-profdata: Avoid F_Text in "merge" for now, since "llvm-profdata show" is confuse...
[oota-llvm.git] / 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/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"
23
24 using namespace llvm;
25
26 static void exitWithError(const Twine &Message, StringRef Whence = "") {
27   errs() << "error: ";
28   if (!Whence.empty())
29     errs() << Whence << ": ";
30   errs() << Message << "\n";
31   ::exit(1);
32 }
33
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...>"));
37
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));
43
44   cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
45
46   if (OutputFilename.empty())
47     OutputFilename = "-";
48
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);
54
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);
60
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);
66   }
67   Writer.write(Output);
68
69   return 0;
70 }
71
72 struct 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 };
85 static raw_ostream &operator<<(raw_ostream &OS, const HashPrinter &Hash) {
86   Hash.print(OS);
87   return OS;
88 }
89
90 int 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
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.
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;
178     else if (strcmp(argv[1], "show") == 0)
179       func = show_main;
180
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"
194              << "Available commands: merge, show\n";
195       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
204   errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";
205   return 1;
206 }