Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 1 | //===- llvm-prof.cpp - Read in and process llvmprof.out data files --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This tools is meant for use with the various LLVM profiling instrumentation |
| 11 | // passes. It reads in the data file produced by executing an instrumented |
| 12 | // program, and outputs a nice report. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chris Lattner | e436779 | 2003-10-28 20:13:07 +0000 | [diff] [blame] | 16 | #include "ProfileInfo.h" |
Chris Lattner | 7a78d81 | 2003-10-28 21:08:18 +0000 | [diff] [blame^] | 17 | #include "llvm/Function.h" |
Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 18 | #include "llvm/Bytecode/Reader.h" |
| 19 | #include "Support/CommandLine.h" |
Chris Lattner | e436779 | 2003-10-28 20:13:07 +0000 | [diff] [blame] | 20 | #include <iostream> |
Chris Lattner | 7a78d81 | 2003-10-28 21:08:18 +0000 | [diff] [blame^] | 21 | #include <cstdio> |
Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 22 | |
| 23 | namespace { |
| 24 | cl::opt<std::string> |
| 25 | BytecodeFile(cl::Positional, cl::desc("<program bytecode file>"), |
| 26 | cl::Required); |
| 27 | |
| 28 | cl::opt<std::string> |
| 29 | ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"), |
| 30 | cl::Optional, cl::init("llvmprof.out")); |
| 31 | } |
| 32 | |
Chris Lattner | 7a78d81 | 2003-10-28 21:08:18 +0000 | [diff] [blame^] | 33 | // PairSecondSort - A sorting predicate to sort by the second element of a pair. |
| 34 | template<class T> |
| 35 | struct PairSecondSort |
| 36 | : public std::binary_function<std::pair<T, unsigned>, |
| 37 | std::pair<T, unsigned>, bool> { |
| 38 | bool operator()(const std::pair<T, unsigned> &LHS, |
| 39 | const std::pair<T, unsigned> &RHS) const { |
| 40 | return LHS.second < RHS.second; |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | |
Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 45 | int main(int argc, char **argv) { |
| 46 | cl::ParseCommandLineOptions(argc, argv, " llvm profile dump decoder\n"); |
Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 47 | |
Chris Lattner | e436779 | 2003-10-28 20:13:07 +0000 | [diff] [blame] | 48 | // Read in the bytecode file... |
| 49 | std::string ErrorMessage; |
Chris Lattner | 7a78d81 | 2003-10-28 21:08:18 +0000 | [diff] [blame^] | 50 | Module *M = ParseBytecodeFile(BytecodeFile, &ErrorMessage); |
| 51 | if (M == 0) { |
Chris Lattner | e436779 | 2003-10-28 20:13:07 +0000 | [diff] [blame] | 52 | std::cerr << argv[0] << ": " << BytecodeFile << ": " << ErrorMessage |
| 53 | << "\n"; |
| 54 | return 1; |
| 55 | } |
Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 56 | |
Chris Lattner | e436779 | 2003-10-28 20:13:07 +0000 | [diff] [blame] | 57 | // Read the profiling information |
Chris Lattner | 7a78d81 | 2003-10-28 21:08:18 +0000 | [diff] [blame^] | 58 | ProfileInfo PI(argv[0], ProfileDataFile, *M); |
Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 7a78d81 | 2003-10-28 21:08:18 +0000 | [diff] [blame^] | 60 | // Output a report. Eventually, there will be multiple reports selectable on |
| 61 | // the command line, for now, just keep things simple. |
| 62 | |
| 63 | // Emit the most frequent function table... |
| 64 | std::vector<std::pair<Function*, unsigned> > FunctionCounts; |
| 65 | PI.getFunctionCounts(FunctionCounts); |
| 66 | |
| 67 | // Sort by the frequency, backwards. |
| 68 | std::sort(FunctionCounts.begin(), FunctionCounts.end(), |
| 69 | std::not2(PairSecondSort<Function*>())); |
| 70 | |
| 71 | unsigned TotalExecutions = 0; |
| 72 | for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) |
| 73 | TotalExecutions += FunctionCounts[i].second; |
| 74 | |
| 75 | // Print out the function frequencies... |
| 76 | printf(" ## Frequency\n"); |
| 77 | for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) |
| 78 | printf("%3d. %5d/%d %s\n", i, FunctionCounts[i].second, TotalExecutions, |
| 79 | FunctionCounts[i].first->getName().c_str()); |
Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 80 | return 0; |
| 81 | } |