llvm-profdata.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. #include "llvm/ADT/StringRef.h"
  14. #include "llvm/IR/LLVMContext.h"
  15. #include "llvm/ProfileData/InstrProfReader.h"
  16. #include "llvm/ProfileData/InstrProfWriter.h"
  17. #include "llvm/ProfileData/SampleProfReader.h"
  18. #include "llvm/ProfileData/SampleProfWriter.h"
  19. #include "llvm/Support/CommandLine.h"
  20. #include "llvm/Support/FileSystem.h"
  21. #include "llvm/Support/Format.h"
  22. #include "llvm/Support/ManagedStatic.h"
  23. #include "llvm/Support/MemoryBuffer.h"
  24. #include "llvm/Support/Path.h"
  25. #include "llvm/Support/PrettyStackTrace.h"
  26. #include "llvm/Support/Signals.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. using namespace llvm;
  29. static void exitWithError(const Twine &Message, StringRef Whence = "") {
  30. errs() << "error: ";
  31. if (!Whence.empty())
  32. errs() << Whence << ": ";
  33. errs() << Message << "\n";
  34. ::exit(1);
  35. }
  36. namespace {
  37. enum ProfileKinds { instr, sample };
  38. }
  39. static void mergeInstrProfile(const cl::list<std::string> &Inputs,
  40. StringRef OutputFilename) {
  41. if (OutputFilename.compare("-") == 0)
  42. exitWithError("Cannot write indexed profdata format to stdout.");
  43. std::error_code EC;
  44. raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None);
  45. if (EC)
  46. exitWithError(EC.message(), OutputFilename);
  47. InstrProfWriter Writer;
  48. for (const auto &Filename : Inputs) {
  49. auto ReaderOrErr = InstrProfReader::create(Filename);
  50. if (std::error_code ec = ReaderOrErr.getError())
  51. exitWithError(ec.message(), Filename);
  52. auto Reader = std::move(ReaderOrErr.get());
  53. for (const auto &I : *Reader)
  54. if (std::error_code EC =
  55. Writer.addFunctionCounts(I.Name, I.Hash, I.Counts))
  56. errs() << Filename << ": " << I.Name << ": " << EC.message() << "\n";
  57. if (Reader->hasError())
  58. exitWithError(Reader->getError().message(), Filename);
  59. }
  60. Writer.write(Output);
  61. }
  62. static void mergeSampleProfile(const cl::list<std::string> &Inputs,
  63. StringRef OutputFilename,
  64. sampleprof::SampleProfileFormat OutputFormat) {
  65. using namespace sampleprof;
  66. auto WriterOrErr = SampleProfileWriter::create(OutputFilename, OutputFormat);
  67. if (std::error_code EC = WriterOrErr.getError())
  68. exitWithError(EC.message(), OutputFilename);
  69. auto Writer = std::move(WriterOrErr.get());
  70. StringMap<FunctionSamples> ProfileMap;
  71. for (const auto &Filename : Inputs) {
  72. auto ReaderOrErr =
  73. SampleProfileReader::create(Filename, getGlobalContext());
  74. if (std::error_code EC = ReaderOrErr.getError())
  75. exitWithError(EC.message(), Filename);
  76. auto Reader = std::move(ReaderOrErr.get());
  77. if (std::error_code EC = Reader->read())
  78. exitWithError(EC.message(), Filename);
  79. StringMap<FunctionSamples> &Profiles = Reader->getProfiles();
  80. for (StringMap<FunctionSamples>::iterator I = Profiles.begin(),
  81. E = Profiles.end();
  82. I != E; ++I) {
  83. StringRef FName = I->first();
  84. FunctionSamples &Samples = I->second;
  85. ProfileMap[FName].merge(Samples);
  86. }
  87. }
  88. Writer->write(ProfileMap);
  89. }
  90. static int merge_main(int argc, const char *argv[]) {
  91. cl::list<std::string> Inputs(cl::Positional, cl::Required, cl::OneOrMore,
  92. cl::desc("<filenames...>"));
  93. cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
  94. cl::init("-"), cl::Required,
  95. cl::desc("Output file"));
  96. cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
  97. cl::aliasopt(OutputFilename));
  98. cl::opt<ProfileKinds> ProfileKind(
  99. cl::desc("Profile kind:"), cl::init(instr),
  100. cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
  101. clEnumVal(sample, "Sample profile"), clEnumValEnd));
  102. cl::opt<sampleprof::SampleProfileFormat> OutputFormat(
  103. cl::desc("Format of output profile (only meaningful with --sample)"),
  104. cl::init(sampleprof::SPF_Binary),
  105. cl::values(clEnumValN(sampleprof::SPF_Binary, "binary",
  106. "Binary encoding (default)"),
  107. clEnumValN(sampleprof::SPF_Text, "text", "Text encoding"),
  108. clEnumValN(sampleprof::SPF_GCC, "gcc", "GCC encoding"),
  109. clEnumValEnd));
  110. cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
  111. if (ProfileKind == instr)
  112. mergeInstrProfile(Inputs, OutputFilename);
  113. else
  114. mergeSampleProfile(Inputs, OutputFilename, OutputFormat);
  115. return 0;
  116. }
  117. static int showInstrProfile(std::string Filename, bool ShowCounts,
  118. bool ShowAllFunctions, std::string ShowFunction,
  119. raw_fd_ostream &OS) {
  120. auto ReaderOrErr = InstrProfReader::create(Filename);
  121. if (std::error_code EC = ReaderOrErr.getError())
  122. exitWithError(EC.message(), Filename);
  123. auto Reader = std::move(ReaderOrErr.get());
  124. uint64_t MaxFunctionCount = 0, MaxBlockCount = 0;
  125. size_t ShownFunctions = 0, TotalFunctions = 0;
  126. for (const auto &Func : *Reader) {
  127. bool Show =
  128. ShowAllFunctions || (!ShowFunction.empty() &&
  129. Func.Name.find(ShowFunction) != Func.Name.npos);
  130. ++TotalFunctions;
  131. assert(Func.Counts.size() > 0 && "function missing entry counter");
  132. if (Func.Counts[0] > MaxFunctionCount)
  133. MaxFunctionCount = Func.Counts[0];
  134. if (Show) {
  135. if (!ShownFunctions)
  136. OS << "Counters:\n";
  137. ++ShownFunctions;
  138. OS << " " << Func.Name << ":\n"
  139. << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
  140. << " Counters: " << Func.Counts.size() << "\n"
  141. << " Function count: " << Func.Counts[0] << "\n";
  142. }
  143. if (Show && ShowCounts)
  144. OS << " Block counts: [";
  145. for (size_t I = 1, E = Func.Counts.size(); I < E; ++I) {
  146. if (Func.Counts[I] > MaxBlockCount)
  147. MaxBlockCount = Func.Counts[I];
  148. if (Show && ShowCounts)
  149. OS << (I == 1 ? "" : ", ") << Func.Counts[I];
  150. }
  151. if (Show && ShowCounts)
  152. OS << "]\n";
  153. }
  154. if (Reader->hasError())
  155. exitWithError(Reader->getError().message(), Filename);
  156. if (ShowAllFunctions || !ShowFunction.empty())
  157. OS << "Functions shown: " << ShownFunctions << "\n";
  158. OS << "Total functions: " << TotalFunctions << "\n";
  159. OS << "Maximum function count: " << MaxFunctionCount << "\n";
  160. OS << "Maximum internal block count: " << MaxBlockCount << "\n";
  161. return 0;
  162. }
  163. static int showSampleProfile(std::string Filename, bool ShowCounts,
  164. bool ShowAllFunctions, std::string ShowFunction,
  165. raw_fd_ostream &OS) {
  166. using namespace sampleprof;
  167. auto ReaderOrErr = SampleProfileReader::create(Filename, getGlobalContext());
  168. if (std::error_code EC = ReaderOrErr.getError())
  169. exitWithError(EC.message(), Filename);
  170. auto Reader = std::move(ReaderOrErr.get());
  171. Reader->read();
  172. if (ShowAllFunctions || ShowFunction.empty())
  173. Reader->dump(OS);
  174. else
  175. Reader->dumpFunctionProfile(ShowFunction, OS);
  176. return 0;
  177. }
  178. static int show_main(int argc, const char *argv[]) {
  179. cl::opt<std::string> Filename(cl::Positional, cl::Required,
  180. cl::desc("<profdata-file>"));
  181. cl::opt<bool> ShowCounts("counts", cl::init(false),
  182. cl::desc("Show counter values for shown functions"));
  183. cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false),
  184. cl::desc("Details for every function"));
  185. cl::opt<std::string> ShowFunction("function",
  186. cl::desc("Details for matching functions"));
  187. cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
  188. cl::init("-"), cl::desc("Output file"));
  189. cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
  190. cl::aliasopt(OutputFilename));
  191. cl::opt<ProfileKinds> ProfileKind(
  192. cl::desc("Profile kind:"), cl::init(instr),
  193. cl::values(clEnumVal(instr, "Instrumentation profile (default)"),
  194. clEnumVal(sample, "Sample profile"), clEnumValEnd));
  195. cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
  196. if (OutputFilename.empty())
  197. OutputFilename = "-";
  198. std::error_code EC;
  199. raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text);
  200. if (EC)
  201. exitWithError(EC.message(), OutputFilename);
  202. if (ShowAllFunctions && !ShowFunction.empty())
  203. errs() << "warning: -function argument ignored: showing all functions\n";
  204. if (ProfileKind == instr)
  205. return showInstrProfile(Filename, ShowCounts, ShowAllFunctions,
  206. ShowFunction, OS);
  207. else
  208. return showSampleProfile(Filename, ShowCounts, ShowAllFunctions,
  209. ShowFunction, OS);
  210. }
  211. int main(int argc, const char *argv[]) {
  212. // Print a stack trace if we signal out.
  213. sys::PrintStackTraceOnErrorSignal();
  214. PrettyStackTraceProgram X(argc, argv);
  215. llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
  216. StringRef ProgName(sys::path::filename(argv[0]));
  217. if (argc > 1) {
  218. int (*func)(int, const char *[]) = nullptr;
  219. if (strcmp(argv[1], "merge") == 0)
  220. func = merge_main;
  221. else if (strcmp(argv[1], "show") == 0)
  222. func = show_main;
  223. if (func) {
  224. std::string Invocation(ProgName.str() + " " + argv[1]);
  225. argv[1] = Invocation.c_str();
  226. return func(argc - 1, argv + 1);
  227. }
  228. if (strcmp(argv[1], "-h") == 0 ||
  229. strcmp(argv[1], "-help") == 0 ||
  230. strcmp(argv[1], "--help") == 0) {
  231. errs() << "OVERVIEW: LLVM profile data tools\n\n"
  232. << "USAGE: " << ProgName << " <command> [args...]\n"
  233. << "USAGE: " << ProgName << " <command> -help\n\n"
  234. << "Available commands: merge, show\n";
  235. return 0;
  236. }
  237. }
  238. if (argc < 2)
  239. errs() << ProgName << ": No command specified!\n";
  240. else
  241. errs() << ProgName << ": Unknown command!\n";
  242. errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n";
  243. return 1;
  244. }