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