ProfileData: Introduce InstrProfWriter using the naive text format
[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   raw_fd_ostream Output(OutputFilename.data(), ErrorInfo, sys::fs::F_Text);
51   if (!ErrorInfo.empty())
52     exitWithError(ErrorInfo, OutputFilename);
53
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);
59
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);
65   }
66   Writer.write(Output);
67
68   return 0;
69 }
70
71 struct 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 };
84 static raw_ostream &operator<<(raw_ostream &OS, const HashPrinter &Hash) {
85   Hash.print(OS);
86   return OS;
87 }
88
89 int 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
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.
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;
177     else if (strcmp(argv[1], "show") == 0)
178       func = show_main;
179
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"
193              << "Available commands: merge, show\n";
194       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
203   errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";
204   return 1;
205 }