Duncan P. N. Exon Smith | 846a627 | 2014-02-17 23:22:49 +0000 | [diff] [blame^] | 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/OwningPtr.h" |
| 15 | #include "llvm/ADT/StringRef.h" |
| 16 | #include "llvm/Support/CommandLine.h" |
| 17 | #include "llvm/Support/ManagedStatic.h" |
| 18 | #include "llvm/Support/MemoryBuffer.h" |
| 19 | #include "llvm/Support/PrettyStackTrace.h" |
| 20 | #include "llvm/Support/Signals.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | static cl::opt<std::string> Filename1(cl::Positional, cl::Required, |
| 26 | cl::desc("file1")); |
| 27 | static cl::opt<std::string> Filename2(cl::Positional, cl::Required, |
| 28 | cl::desc("file2")); |
| 29 | |
| 30 | static cl::opt<std::string> OutputFilename("output", cl::value_desc("output"), |
| 31 | cl::init("-"), |
| 32 | cl::desc("Output file")); |
| 33 | static cl::alias OutputFilenameA("o", cl::desc("Alias for --output"), |
| 34 | cl::aliasopt(OutputFilename)); |
| 35 | |
| 36 | static bool readLine(const char *&Start, const char *End, StringRef &S) { |
| 37 | if (Start == End) |
| 38 | return false; |
| 39 | |
| 40 | for (const char *I = Start; I != End; ++I) { |
| 41 | assert(*I && "unexpected binary data"); |
| 42 | if (*I == '\n') { |
| 43 | S = StringRef(Start, I - Start); |
| 44 | Start = I + 1; |
| 45 | return true; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | S = StringRef(Start, End - Start); |
| 50 | Start = End; |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | static StringRef getWord(const char *&Start, const char *End) { |
| 55 | for (const char *I = Start; I != End; ++I) |
| 56 | if (*I == ' ') { |
| 57 | StringRef S(Start, I - Start); |
| 58 | Start = I + 1; |
| 59 | return S; |
| 60 | } |
| 61 | StringRef S(Start, End - Start); |
| 62 | Start = End; |
| 63 | return S; |
| 64 | } |
| 65 | |
| 66 | static size_t splitWords(const StringRef &Line, std::vector<StringRef> &Words) { |
| 67 | const char *Start = Line.data(); |
| 68 | const char *End = Line.data() + Line.size(); |
| 69 | Words.clear(); |
| 70 | while (Start != End) |
| 71 | Words.push_back(getWord(Start, End)); |
| 72 | return Words.size(); |
| 73 | } |
| 74 | |
| 75 | static bool getNumber(const StringRef &S, uint64_t &N) { |
| 76 | N = 0; |
| 77 | for (StringRef::iterator I = S.begin(), E = S.end(); I != E; ++I) |
| 78 | if (*I >= '0' && *I <= '9') |
| 79 | N = N * 10 + (*I - '0'); |
| 80 | else |
| 81 | return false; |
| 82 | |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | static void exitWithError(const std::string &Message, |
| 87 | const std::string &Filename, int64_t Line = -1) { |
| 88 | errs() << "error: " << Filename; |
| 89 | if (Line >= 0) |
| 90 | errs() << ":" << Line; |
| 91 | errs() << ": " << Message << "\n"; |
| 92 | ::exit(1); |
| 93 | } |
| 94 | |
| 95 | //===----------------------------------------------------------------------===// |
| 96 | int main(int argc, char **argv) { |
| 97 | // Print a stack trace if we signal out. |
| 98 | sys::PrintStackTraceOnErrorSignal(); |
| 99 | PrettyStackTraceProgram X(argc, argv); |
| 100 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 101 | |
| 102 | cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n"); |
| 103 | |
| 104 | OwningPtr<MemoryBuffer> File1; |
| 105 | OwningPtr<MemoryBuffer> File2; |
| 106 | if (error_code ec = MemoryBuffer::getFile(Filename1, File1)) |
| 107 | exitWithError(ec.message(), Filename1); |
| 108 | if (error_code ec = MemoryBuffer::getFile(Filename2, File2)) |
| 109 | exitWithError(ec.message(), Filename2); |
| 110 | |
| 111 | if (OutputFilename.empty()) |
| 112 | OutputFilename = "-"; |
| 113 | |
| 114 | std::string ErrorInfo; |
| 115 | raw_fd_ostream Output(OutputFilename.data(), ErrorInfo); |
| 116 | if (!ErrorInfo.empty()) |
| 117 | exitWithError(ErrorInfo, OutputFilename); |
| 118 | |
| 119 | const char *Start1 = File1->getBufferStart(); |
| 120 | const char *Start2 = File2->getBufferStart(); |
| 121 | const char *End1 = File1->getBufferEnd(); |
| 122 | const char *End2 = File2->getBufferEnd(); |
| 123 | const char *P1 = Start1; |
| 124 | const char *P2 = Start2; |
| 125 | |
| 126 | StringRef Line1, Line2; |
| 127 | int64_t Num = 0; |
| 128 | while (readLine(P1, End1, Line1)) { |
| 129 | ++Num; |
| 130 | if (!readLine(P2, End2, Line2)) |
| 131 | exitWithError("truncated file", Filename2, Num); |
| 132 | |
| 133 | std::vector<StringRef> Words1, Words2; |
| 134 | if (splitWords(Line1, Words1) != splitWords(Line2, Words2)) |
| 135 | exitWithError("data mismatch", Filename2, Num); |
| 136 | |
| 137 | if (Words1.size() > 2) |
| 138 | exitWithError("invalid data", Filename1, Num); |
| 139 | |
| 140 | if (Words1.empty()) { |
| 141 | Output << "\n"; |
| 142 | continue; |
| 143 | } |
| 144 | |
| 145 | if (Words1.size() == 2) { |
| 146 | if (Words1[0] != Words2[0]) |
| 147 | exitWithError("function name mismatch", Filename2, Num); |
| 148 | |
| 149 | uint64_t N1, N2; |
| 150 | if (!getNumber(Words1[1], N1)) |
| 151 | exitWithError("bad function count", Filename1, Num); |
| 152 | if (!getNumber(Words2[1], N2)) |
| 153 | exitWithError("bad function count", Filename2, Num); |
| 154 | |
| 155 | if (N1 != N2) |
| 156 | exitWithError("function count mismatch", Filename2, Num); |
| 157 | |
| 158 | Output << Line1 << "\n"; |
| 159 | continue; |
| 160 | } |
| 161 | |
| 162 | uint64_t N1, N2; |
| 163 | if (!getNumber(Words1[0], N1)) |
| 164 | exitWithError("invalid counter", Filename1, Num); |
| 165 | if (!getNumber(Words2[0], N2)) |
| 166 | exitWithError("invalid counter", Filename2, Num); |
| 167 | |
| 168 | uint64_t Sum = N1 + N2; |
| 169 | if (Sum < N1) |
| 170 | exitWithError("counter overflow", Filename2, Num); |
| 171 | |
| 172 | Output << N1 + N2 << "\n"; |
| 173 | } |
| 174 | if (readLine(P2, End2, Line2)) |
| 175 | exitWithError("truncated file", Filename1, Num + 1); |
| 176 | |
| 177 | return 0; |
| 178 | } |