blob: fdde32ac6bb142f8eab6e10f4bb5db5617dee5ad [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"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000018#include "llvm/Support/FileSystem.h"
Justin Bogner423380f2014-03-23 20:43:50 +000019#include "llvm/Support/Format.h"
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000020#include "llvm/Support/ManagedStatic.h"
21#include "llvm/Support/MemoryBuffer.h"
22#include "llvm/Support/PrettyStackTrace.h"
23#include "llvm/Support/Signals.h"
24#include "llvm/Support/raw_ostream.h"
25
26using namespace llvm;
27
Justin Bognerf8d79192014-03-21 17:24:48 +000028static void exitWithError(const Twine &Message, StringRef Whence = "") {
29 errs() << "error: ";
30 if (!Whence.empty())
31 errs() << Whence << ": ";
32 errs() << Message << "\n";
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000033 ::exit(1);
34}
35
Justin Bogner618bcea2014-03-19 02:20:46 +000036int merge_main(int argc, const char *argv[]) {
Justin Bognerb9bd7f82014-03-21 17:46:22 +000037 cl::list<std::string> Inputs(cl::Positional, cl::Required, cl::OneOrMore,
38 cl::desc("<filenames...>"));
Justin Bogner618bcea2014-03-19 02:20:46 +000039
40 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
41 cl::init("-"),
42 cl::desc("Output file"));
Justin Bognerb7aa2632014-04-18 21:48:40 +000043 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"), cl::Required,
44 cl::aliasopt(OutputFilename));
Justin Bognerbfee8d42014-03-12 20:14:17 +000045
Justin Bognerec49f982014-03-12 22:00:57 +000046 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
Justin Bognerbfee8d42014-03-12 20:14:17 +000047
Justin Bognerb7aa2632014-04-18 21:48:40 +000048 if (OutputFilename.compare("-") == 0)
49 exitWithError("Cannot write indexed profdata format to stdout.");
Justin Bognerec49f982014-03-12 22:00:57 +000050
51 std::string ErrorInfo;
NAKAMURA Takumi4baaa6a2014-03-22 05:38:22 +000052 raw_fd_ostream Output(OutputFilename.data(), ErrorInfo, sys::fs::F_None);
Justin Bognerec49f982014-03-12 22:00:57 +000053 if (!ErrorInfo.empty())
54 exitWithError(ErrorInfo, OutputFilename);
55
Justin Bognerb9bd7f82014-03-21 17:46:22 +000056 InstrProfWriter Writer;
57 for (const auto &Filename : Inputs) {
58 std::unique_ptr<InstrProfReader> Reader;
59 if (error_code ec = InstrProfReader::create(Filename, Reader))
60 exitWithError(ec.message(), Filename);
Justin Bognerf8d79192014-03-21 17:24:48 +000061
Justin Bognerb9bd7f82014-03-21 17:46:22 +000062 for (const auto &I : *Reader)
63 if (error_code EC = Writer.addFunctionCounts(I.Name, I.Hash, I.Counts))
64 errs() << Filename << ": " << I.Name << ": " << EC.message() << "\n";
65 if (Reader->hasError())
66 exitWithError(Reader->getError().message(), Filename);
Justin Bognerbfee8d42014-03-12 20:14:17 +000067 }
Justin Bognerb9bd7f82014-03-21 17:46:22 +000068 Writer.write(Output);
Justin Bognerbfee8d42014-03-12 20:14:17 +000069
Justin Bognerec49f982014-03-12 22:00:57 +000070 return 0;
Justin Bognerbfee8d42014-03-12 20:14:17 +000071}
Justin Bogner618bcea2014-03-19 02:20:46 +000072
Justin Bogner9af28ef2014-03-21 17:29:44 +000073int show_main(int argc, const char *argv[]) {
74 cl::opt<std::string> Filename(cl::Positional, cl::Required,
75 cl::desc("<profdata-file>"));
76
77 cl::opt<bool> ShowCounts("counts", cl::init(false),
78 cl::desc("Show counter values for shown functions"));
79 cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
80 cl::desc("Details for every function"));
81 cl::opt<std::string> ShowFunction("function",
82 cl::desc("Details for matching functions"));
83
84 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
85 cl::init("-"),
86 cl::desc("Output file"));
87 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
88 cl::aliasopt(OutputFilename));
89
90 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
91
92 std::unique_ptr<InstrProfReader> Reader;
93 if (error_code EC = InstrProfReader::create(Filename, Reader))
94 exitWithError(EC.message(), Filename);
95
96 if (OutputFilename.empty())
97 OutputFilename = "-";
98
99 std::string ErrorInfo;
100 raw_fd_ostream OS(OutputFilename.data(), ErrorInfo, sys::fs::F_Text);
101 if (!ErrorInfo.empty())
102 exitWithError(ErrorInfo, OutputFilename);
103
104 if (ShowAllFunctions && !ShowFunction.empty())
105 errs() << "warning: -function argument ignored: showing all functions\n";
106
107 uint64_t MaxFunctionCount = 0, MaxBlockCount = 0;
108 size_t ShownFunctions = 0, TotalFunctions = 0;
109 for (const auto &Func : *Reader) {
110 bool Show = ShowAllFunctions ||
111 (!ShowFunction.empty() &&
112 Func.Name.find(ShowFunction) != Func.Name.npos);
113
114 ++TotalFunctions;
Justin Bognerb59d7c72014-04-25 02:45:33 +0000115 assert(Func.Counts.size() > 0 && "function missing entry counter");
Justin Bogner9af28ef2014-03-21 17:29:44 +0000116 if (Func.Counts[0] > MaxFunctionCount)
117 MaxFunctionCount = Func.Counts[0];
118
119 if (Show) {
120 if (!ShownFunctions)
121 OS << "Counters:\n";
122 ++ShownFunctions;
123
124 OS << " " << Func.Name << ":\n"
Justin Bogner423380f2014-03-23 20:43:50 +0000125 << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
Justin Bogner9af28ef2014-03-21 17:29:44 +0000126 << " Counters: " << Func.Counts.size() << "\n"
127 << " Function count: " << Func.Counts[0] << "\n";
128 }
129
130 if (Show && ShowCounts)
131 OS << " Block counts: [";
132 for (size_t I = 1, E = Func.Counts.size(); I < E; ++I) {
133 if (Func.Counts[I] > MaxBlockCount)
134 MaxBlockCount = Func.Counts[I];
135 if (Show && ShowCounts)
136 OS << (I == 1 ? "" : ", ") << Func.Counts[I];
137 }
138 if (Show && ShowCounts)
139 OS << "]\n";
140 }
Justin Bognerdb1225d2014-03-23 20:55:53 +0000141 if (Reader->hasError())
142 exitWithError(Reader->getError().message(), Filename);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000143
144 if (ShowAllFunctions || !ShowFunction.empty())
145 OS << "Functions shown: " << ShownFunctions << "\n";
146 OS << "Total functions: " << TotalFunctions << "\n";
147 OS << "Maximum function count: " << MaxFunctionCount << "\n";
148 OS << "Maximum internal block count: " << MaxBlockCount << "\n";
149 return 0;
150}
151
Justin Bogner618bcea2014-03-19 02:20:46 +0000152int main(int argc, const char *argv[]) {
153 // Print a stack trace if we signal out.
154 sys::PrintStackTraceOnErrorSignal();
155 PrettyStackTraceProgram X(argc, argv);
156 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
157
158 StringRef ProgName(sys::path::filename(argv[0]));
159 if (argc > 1) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000160 int (*func)(int, const char *[]) = nullptr;
Justin Bogner618bcea2014-03-19 02:20:46 +0000161
162 if (strcmp(argv[1], "merge") == 0)
163 func = merge_main;
Justin Bogner9af28ef2014-03-21 17:29:44 +0000164 else if (strcmp(argv[1], "show") == 0)
165 func = show_main;
Justin Bogner618bcea2014-03-19 02:20:46 +0000166
167 if (func) {
168 std::string Invocation(ProgName.str() + " " + argv[1]);
169 argv[1] = Invocation.c_str();
170 return func(argc - 1, argv + 1);
171 }
172
173 if (strcmp(argv[1], "-h") == 0 ||
174 strcmp(argv[1], "-help") == 0 ||
175 strcmp(argv[1], "--help") == 0) {
176
177 errs() << "OVERVIEW: LLVM profile data tools\n\n"
178 << "USAGE: " << ProgName << " <command> [args...]\n"
179 << "USAGE: " << ProgName << " <command> -help\n\n"
Justin Bogner9af28ef2014-03-21 17:29:44 +0000180 << "Available commands: merge, show\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000181 return 0;
182 }
183 }
184
185 if (argc < 2)
186 errs() << ProgName << ": No command specified!\n";
187 else
188 errs() << ProgName << ": Unknown command!\n";
189
Justin Bogner9af28ef2014-03-21 17:29:44 +0000190 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000191 return 1;
192}