blob: 0bd8e834f457e0f39df3f87b4f8277640a79a9d5 [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"
Justin Bogner423380f2014-03-23 20:43:50 +000018#include "llvm/Support/Format.h"
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000019#include "llvm/Support/ManagedStatic.h"
20#include "llvm/Support/MemoryBuffer.h"
21#include "llvm/Support/PrettyStackTrace.h"
22#include "llvm/Support/Signals.h"
23#include "llvm/Support/raw_ostream.h"
24
25using namespace llvm;
26
Justin Bognerf8d79192014-03-21 17:24:48 +000027static void exitWithError(const Twine &Message, StringRef Whence = "") {
28 errs() << "error: ";
29 if (!Whence.empty())
30 errs() << Whence << ": ";
31 errs() << Message << "\n";
Duncan P. N. Exon Smith846a6272014-02-17 23:22:49 +000032 ::exit(1);
33}
34
Justin Bogner618bcea2014-03-19 02:20:46 +000035int merge_main(int argc, const char *argv[]) {
Justin Bognerb9bd7f82014-03-21 17:46:22 +000036 cl::list<std::string> Inputs(cl::Positional, cl::Required, cl::OneOrMore,
37 cl::desc("<filenames...>"));
Justin Bogner618bcea2014-03-19 02:20:46 +000038
39 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
40 cl::init("-"),
41 cl::desc("Output file"));
Justin Bognerb7aa2632014-04-18 21:48:40 +000042 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"), cl::Required,
43 cl::aliasopt(OutputFilename));
Justin Bognerbfee8d42014-03-12 20:14:17 +000044
Justin Bognerec49f982014-03-12 22:00:57 +000045 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
Justin Bognerbfee8d42014-03-12 20:14:17 +000046
Justin Bognerb7aa2632014-04-18 21:48:40 +000047 if (OutputFilename.compare("-") == 0)
48 exitWithError("Cannot write indexed profdata format to stdout.");
Justin Bognerec49f982014-03-12 22:00:57 +000049
50 std::string ErrorInfo;
NAKAMURA Takumi4baaa6a2014-03-22 05:38:22 +000051 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 +000072int show_main(int argc, const char *argv[]) {
73 cl::opt<std::string> Filename(cl::Positional, cl::Required,
74 cl::desc("<profdata-file>"));
75
76 cl::opt<bool> ShowCounts("counts", cl::init(false),
77 cl::desc("Show counter values for shown functions"));
78 cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
79 cl::desc("Details for every function"));
80 cl::opt<std::string> ShowFunction("function",
81 cl::desc("Details for matching functions"));
82
83 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
84 cl::init("-"),
85 cl::desc("Output file"));
86 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
87 cl::aliasopt(OutputFilename));
88
89 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
90
91 std::unique_ptr<InstrProfReader> Reader;
92 if (error_code EC = InstrProfReader::create(Filename, Reader))
93 exitWithError(EC.message(), Filename);
94
95 if (OutputFilename.empty())
96 OutputFilename = "-";
97
98 std::string ErrorInfo;
99 raw_fd_ostream OS(OutputFilename.data(), ErrorInfo, sys::fs::F_Text);
100 if (!ErrorInfo.empty())
101 exitWithError(ErrorInfo, OutputFilename);
102
103 if (ShowAllFunctions && !ShowFunction.empty())
104 errs() << "warning: -function argument ignored: showing all functions\n";
105
106 uint64_t MaxFunctionCount = 0, MaxBlockCount = 0;
107 size_t ShownFunctions = 0, TotalFunctions = 0;
108 for (const auto &Func : *Reader) {
109 bool Show = ShowAllFunctions ||
110 (!ShowFunction.empty() &&
111 Func.Name.find(ShowFunction) != Func.Name.npos);
112
113 ++TotalFunctions;
114 if (Func.Counts[0] > MaxFunctionCount)
115 MaxFunctionCount = Func.Counts[0];
116
117 if (Show) {
118 if (!ShownFunctions)
119 OS << "Counters:\n";
120 ++ShownFunctions;
121
122 OS << " " << Func.Name << ":\n"
Justin Bogner423380f2014-03-23 20:43:50 +0000123 << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
Justin Bogner9af28ef2014-03-21 17:29:44 +0000124 << " Counters: " << Func.Counts.size() << "\n"
125 << " Function count: " << Func.Counts[0] << "\n";
126 }
127
128 if (Show && ShowCounts)
129 OS << " Block counts: [";
130 for (size_t I = 1, E = Func.Counts.size(); I < E; ++I) {
131 if (Func.Counts[I] > MaxBlockCount)
132 MaxBlockCount = Func.Counts[I];
133 if (Show && ShowCounts)
134 OS << (I == 1 ? "" : ", ") << Func.Counts[I];
135 }
136 if (Show && ShowCounts)
137 OS << "]\n";
138 }
Justin Bognerdb1225d2014-03-23 20:55:53 +0000139 if (Reader->hasError())
140 exitWithError(Reader->getError().message(), Filename);
Justin Bogner9af28ef2014-03-21 17:29:44 +0000141
142 if (ShowAllFunctions || !ShowFunction.empty())
143 OS << "Functions shown: " << ShownFunctions << "\n";
144 OS << "Total functions: " << TotalFunctions << "\n";
145 OS << "Maximum function count: " << MaxFunctionCount << "\n";
146 OS << "Maximum internal block count: " << MaxBlockCount << "\n";
147 return 0;
148}
149
Justin Bogner618bcea2014-03-19 02:20:46 +0000150int main(int argc, const char *argv[]) {
151 // Print a stack trace if we signal out.
152 sys::PrintStackTraceOnErrorSignal();
153 PrettyStackTraceProgram X(argc, argv);
154 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
155
156 StringRef ProgName(sys::path::filename(argv[0]));
157 if (argc > 1) {
158 int (*func)(int, const char *[]) = 0;
159
160 if (strcmp(argv[1], "merge") == 0)
161 func = merge_main;
Justin Bogner9af28ef2014-03-21 17:29:44 +0000162 else if (strcmp(argv[1], "show") == 0)
163 func = show_main;
Justin Bogner618bcea2014-03-19 02:20:46 +0000164
165 if (func) {
166 std::string Invocation(ProgName.str() + " " + argv[1]);
167 argv[1] = Invocation.c_str();
168 return func(argc - 1, argv + 1);
169 }
170
171 if (strcmp(argv[1], "-h") == 0 ||
172 strcmp(argv[1], "-help") == 0 ||
173 strcmp(argv[1], "--help") == 0) {
174
175 errs() << "OVERVIEW: LLVM profile data tools\n\n"
176 << "USAGE: " << ProgName << " <command> [args...]\n"
177 << "USAGE: " << ProgName << " <command> -help\n\n"
Justin Bogner9af28ef2014-03-21 17:29:44 +0000178 << "Available commands: merge, show\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000179 return 0;
180 }
181 }
182
183 if (argc < 2)
184 errs() << ProgName << ": No command specified!\n";
185 else
186 errs() << ProgName << ": Unknown command!\n";
187
Justin Bogner9af28ef2014-03-21 17:29:44 +0000188 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";
Justin Bogner618bcea2014-03-19 02:20:46 +0000189 return 1;
190}