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/SmallSet.h" 15 #include "llvm/ADT/SmallVector.h" 16 #include "llvm/ADT/StringRef.h" 17 #include "llvm/IR/LLVMContext.h" 18 #include "llvm/ProfileData/InstrProfReader.h" 19 #include "llvm/ProfileData/InstrProfWriter.h" 20 #include "llvm/ProfileData/SampleProfReader.h" 21 #include "llvm/ProfileData/SampleProfWriter.h" 22 #include "llvm/Support/CommandLine.h" 23 #include "llvm/Support/Errc.h" 24 #include "llvm/Support/FileSystem.h" 25 #include "llvm/Support/Format.h" 26 #include "llvm/Support/ManagedStatic.h" 27 #include "llvm/Support/MemoryBuffer.h" 28 #include "llvm/Support/Path.h" 29 #include "llvm/Support/PrettyStackTrace.h" 30 #include "llvm/Support/Signals.h" 31 #include "llvm/Support/raw_ostream.h" 32 #include <algorithm> 33 #include <tuple> 34 35 using namespace llvm; 36 37 enum ProfileFormat { PF_None = 0, PF_Text, PF_Binary, PF_GCC }; 38 exitWithError(const Twine & Message,StringRef Whence="",StringRef Hint="")39 static void exitWithError(const Twine &Message, StringRef Whence = "", 40 StringRef Hint = "") { 41 errs() << "error: "; 42 if (!Whence.empty()) 43 errs() << Whence << ": "; 44 errs() << Message << "\n"; 45 if (!Hint.empty()) 46 errs() << Hint << "\n"; 47 ::exit(1); 48 } 49 exitWithErrorCode(const std::error_code & Error,StringRef Whence="")50 static void exitWithErrorCode(const std::error_code &Error, 51 StringRef Whence = "") { 52 if (Error.category() == instrprof_category()) { 53 instrprof_error instrError = static_cast<instrprof_error>(Error.value()); 54 if (instrError == instrprof_error::unrecognized_format) { 55 // Hint for common error of forgetting -sample for sample profiles. 56 exitWithError(Error.message(), Whence, 57 "Perhaps you forgot to use the -sample option?"); 58 } 59 } 60 exitWithError(Error.message(), Whence); 61 } 62 63 namespace { 64 enum ProfileKinds { instr, sample }; 65 } 66 handleMergeWriterError(std::error_code & Error,StringRef WhenceFile="",StringRef WhenceFunction="",bool ShowHint=true)67 static void handleMergeWriterError(std::error_code &Error, 68 StringRef WhenceFile = "", 69 StringRef WhenceFunction = "", 70 bool ShowHint = true) { 71 if (!WhenceFile.empty()) 72 errs() << WhenceFile << ": "; 73 if (!WhenceFunction.empty()) 74 errs() << WhenceFunction << ": "; 75 errs() << Error.message() << "\n"; 76 77 if (ShowHint) { 78 StringRef Hint = ""; 79 if (Error.category() == instrprof_category()) { 80 instrprof_error instrError = static_cast<instrprof_error>(Error.value()); 81 switch (instrError) { 82 case instrprof_error::hash_mismatch: 83 case instrprof_error::count_mismatch: 84 case instrprof_error::value_site_count_mismatch: 85 Hint = "Make sure that all profile data to be merged is generated " 86 "from the same binary."; 87 break; 88 default: 89 break; 90 } 91 } 92 93 if (!Hint.empty()) 94 errs() << Hint << "\n"; 95 } 96 } 97 98 struct WeightedFile { 99 StringRef Filename; 100 uint64_t Weight; 101 WeightedFileWeightedFile102 WeightedFile() {} 103 WeightedFileWeightedFile104 WeightedFile(StringRef F, uint64_t W) : Filename{F}, Weight{W} {} 105 }; 106 typedef SmallVector<WeightedFile, 5> WeightedFileVector; 107 mergeInstrProfile(const WeightedFileVector & Inputs,StringRef OutputFilename,ProfileFormat OutputFormat)108 static void mergeInstrProfile(const WeightedFileVector &Inputs, 109 StringRef OutputFilename, 110 ProfileFormat OutputFormat) { 111 if (OutputFilename.compare("-") == 0) 112 exitWithError("Cannot write indexed profdata format to stdout."); 113 114 if (OutputFormat != PF_Binary && OutputFormat != PF_Text) 115 exitWithError("Unknown format is specified."); 116 117 std::error_code EC; 118 raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None); 119 if (EC) 120 exitWithErrorCode(EC, OutputFilename); 121 122 InstrProfWriter Writer; 123 SmallSet<std::error_code, 4> WriterErrorCodes; 124 for (const auto &Input : Inputs) { 125 auto ReaderOrErr = InstrProfReader::create(Input.Filename); 126 if (std::error_code ec = ReaderOrErr.getError()) 127 exitWithErrorCode(ec, Input.Filename); 128 129 auto Reader = std::move(ReaderOrErr.get()); 130 for (auto &I : *Reader) { 131 if (std::error_code EC = Writer.addRecord(std::move(I), Input.Weight)) { 132 // Only show hint the first time an error occurs. 133 bool firstTime = WriterErrorCodes.insert(EC).second; 134 handleMergeWriterError(EC, Input.Filename, I.Name, firstTime); 135 } 136 } 137 if (Reader->hasError()) 138 exitWithErrorCode(Reader->getError(), Input.Filename); 139 } 140 if (OutputFormat == PF_Text) 141 Writer.writeText(Output); 142 else 143 Writer.write(Output); 144 } 145 146 static sampleprof::SampleProfileFormat FormatMap[] = { 147 sampleprof::SPF_None, sampleprof::SPF_Text, sampleprof::SPF_Binary, 148 sampleprof::SPF_GCC}; 149 mergeSampleProfile(const WeightedFileVector & Inputs,StringRef OutputFilename,ProfileFormat OutputFormat)150 static void mergeSampleProfile(const WeightedFileVector &Inputs, 151 StringRef OutputFilename, 152 ProfileFormat OutputFormat) { 153 using namespace sampleprof; 154 auto WriterOrErr = 155 SampleProfileWriter::create(OutputFilename, FormatMap[OutputFormat]); 156 if (std::error_code EC = WriterOrErr.getError()) 157 exitWithErrorCode(EC, OutputFilename); 158 159 auto Writer = std::move(WriterOrErr.get()); 160 StringMap<FunctionSamples> ProfileMap; 161 SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers; 162 for (const auto &Input : Inputs) { 163 auto ReaderOrErr = 164 SampleProfileReader::create(Input.Filename, getGlobalContext()); 165 if (std::error_code EC = ReaderOrErr.getError()) 166 exitWithErrorCode(EC, Input.Filename); 167 168 // We need to keep the readers around until after all the files are 169 // read so that we do not lose the function names stored in each 170 // reader's memory. The function names are needed to write out the 171 // merged profile map. 172 Readers.push_back(std::move(ReaderOrErr.get())); 173 const auto Reader = Readers.back().get(); 174 if (std::error_code EC = Reader->read()) 175 exitWithErrorCode(EC, Input.Filename); 176 177 StringMap<FunctionSamples> &Profiles = Reader->getProfiles(); 178 for (StringMap<FunctionSamples>::iterator I = Profiles.begin(), 179 E = Profiles.end(); 180 I != E; ++I) { 181 StringRef FName = I->first(); 182 FunctionSamples &Samples = I->second; 183 sampleprof_error Result = ProfileMap[FName].merge(Samples, Input.Weight); 184 if (Result != sampleprof_error::success) { 185 std::error_code EC = make_error_code(Result); 186 handleMergeWriterError(EC, Input.Filename, FName); 187 } 188 } 189 } 190 Writer->write(ProfileMap); 191 } 192 parseWeightedFile(const StringRef & WeightedFilename)193 static WeightedFile parseWeightedFile(const StringRef &WeightedFilename) { 194 StringRef WeightStr, FileName; 195 std::tie(WeightStr, FileName) = WeightedFilename.split(','); 196 197 uint64_t Weight; 198 if (WeightStr.getAsInteger(10, Weight) || Weight < 1) 199 exitWithError("Input weight must be a positive integer."); 200 201 if (!sys::fs::exists(FileName)) 202 exitWithErrorCode(make_error_code(errc::no_such_file_or_directory), 203 FileName); 204 205 return WeightedFile(FileName, Weight); 206 } 207 merge_main(int argc,const char * argv[])208 static int merge_main(int argc, const char *argv[]) { 209 cl::list<std::string> InputFilenames(cl::Positional, 210 cl::desc("<filename...>")); 211 cl::list<std::string> WeightedInputFilenames("weighted-input", 212 cl::desc("<weight>,<filename>")); 213 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"), 214 cl::init("-"), cl::Required, 215 cl::desc("Output file")); 216 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"), 217 cl::aliasopt(OutputFilename)); 218 cl::opt<ProfileKinds> ProfileKind( 219 cl::desc("Profile kind:"), cl::init(instr), 220 cl::values(clEnumVal(instr, "Instrumentation profile (default)"), 221 clEnumVal(sample, "Sample profile"), clEnumValEnd)); 222 223 cl::opt<ProfileFormat> OutputFormat( 224 cl::desc("Format of output profile"), cl::init(PF_Binary), 225 cl::values(clEnumValN(PF_Binary, "binary", "Binary encoding (default)"), 226 clEnumValN(PF_Text, "text", "Text encoding"), 227 clEnumValN(PF_GCC, "gcc", 228 "GCC encoding (only meaningful for -sample)"), 229 clEnumValEnd)); 230 231 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n"); 232 233 if (InputFilenames.empty() && WeightedInputFilenames.empty()) 234 exitWithError("No input files specified. See " + 235 sys::path::filename(argv[0]) + " -help"); 236 237 WeightedFileVector WeightedInputs; 238 for (StringRef Filename : InputFilenames) 239 WeightedInputs.push_back(WeightedFile(Filename, 1)); 240 for (StringRef WeightedFilename : WeightedInputFilenames) 241 WeightedInputs.push_back(parseWeightedFile(WeightedFilename)); 242 243 if (ProfileKind == instr) 244 mergeInstrProfile(WeightedInputs, OutputFilename, OutputFormat); 245 else 246 mergeSampleProfile(WeightedInputs, OutputFilename, OutputFormat); 247 248 return 0; 249 } 250 showInstrProfile(std::string Filename,bool ShowCounts,bool ShowIndirectCallTargets,bool ShowAllFunctions,std::string ShowFunction,bool TextFormat,raw_fd_ostream & OS)251 static int showInstrProfile(std::string Filename, bool ShowCounts, 252 bool ShowIndirectCallTargets, bool ShowAllFunctions, 253 std::string ShowFunction, bool TextFormat, 254 raw_fd_ostream &OS) { 255 auto ReaderOrErr = InstrProfReader::create(Filename); 256 if (std::error_code EC = ReaderOrErr.getError()) 257 exitWithErrorCode(EC, Filename); 258 259 auto Reader = std::move(ReaderOrErr.get()); 260 uint64_t MaxFunctionCount = 0, MaxBlockCount = 0; 261 size_t ShownFunctions = 0, TotalFunctions = 0; 262 for (const auto &Func : *Reader) { 263 bool Show = 264 ShowAllFunctions || (!ShowFunction.empty() && 265 Func.Name.find(ShowFunction) != Func.Name.npos); 266 267 bool doTextFormatDump = (Show && ShowCounts && TextFormat); 268 269 if (doTextFormatDump) { 270 InstrProfSymtab &Symtab = Reader->getSymtab(); 271 InstrProfWriter::writeRecordInText(Func, Symtab, OS); 272 continue; 273 } 274 275 ++TotalFunctions; 276 assert(Func.Counts.size() > 0 && "function missing entry counter"); 277 if (Func.Counts[0] > MaxFunctionCount) 278 MaxFunctionCount = Func.Counts[0]; 279 280 for (size_t I = 1, E = Func.Counts.size(); I < E; ++I) { 281 if (Func.Counts[I] > MaxBlockCount) 282 MaxBlockCount = Func.Counts[I]; 283 } 284 285 if (Show) { 286 287 if (!ShownFunctions) 288 OS << "Counters:\n"; 289 290 ++ShownFunctions; 291 292 OS << " " << Func.Name << ":\n" 293 << " Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n" 294 << " Counters: " << Func.Counts.size() << "\n" 295 << " Function count: " << Func.Counts[0] << "\n"; 296 297 if (ShowIndirectCallTargets) 298 OS << " Indirect Call Site Count: " 299 << Func.getNumValueSites(IPVK_IndirectCallTarget) << "\n"; 300 301 if (ShowCounts) { 302 OS << " Block counts: ["; 303 for (size_t I = 1, E = Func.Counts.size(); I < E; ++I) { 304 OS << (I == 1 ? "" : ", ") << Func.Counts[I]; 305 } 306 OS << "]\n"; 307 } 308 309 if (ShowIndirectCallTargets) { 310 InstrProfSymtab &Symtab = Reader->getSymtab(); 311 uint32_t NS = Func.getNumValueSites(IPVK_IndirectCallTarget); 312 OS << " Indirect Target Results: \n"; 313 for (size_t I = 0; I < NS; ++I) { 314 uint32_t NV = Func.getNumValueDataForSite(IPVK_IndirectCallTarget, I); 315 std::unique_ptr<InstrProfValueData[]> VD = 316 Func.getValueForSite(IPVK_IndirectCallTarget, I); 317 for (uint32_t V = 0; V < NV; V++) { 318 OS << "\t[ " << I << ", "; 319 OS << Symtab.getFuncName(VD[V].Value) << ", " << VD[V].Count 320 << " ]\n"; 321 } 322 } 323 } 324 } 325 } 326 327 if (Reader->hasError()) 328 exitWithErrorCode(Reader->getError(), Filename); 329 330 if (ShowCounts && TextFormat) 331 return 0; 332 333 if (ShowAllFunctions || !ShowFunction.empty()) 334 OS << "Functions shown: " << ShownFunctions << "\n"; 335 OS << "Total functions: " << TotalFunctions << "\n"; 336 OS << "Maximum function count: " << MaxFunctionCount << "\n"; 337 OS << "Maximum internal block count: " << MaxBlockCount << "\n"; 338 return 0; 339 } 340 showSampleProfile(std::string Filename,bool ShowCounts,bool ShowAllFunctions,std::string ShowFunction,raw_fd_ostream & OS)341 static int showSampleProfile(std::string Filename, bool ShowCounts, 342 bool ShowAllFunctions, std::string ShowFunction, 343 raw_fd_ostream &OS) { 344 using namespace sampleprof; 345 auto ReaderOrErr = SampleProfileReader::create(Filename, getGlobalContext()); 346 if (std::error_code EC = ReaderOrErr.getError()) 347 exitWithErrorCode(EC, Filename); 348 349 auto Reader = std::move(ReaderOrErr.get()); 350 if (std::error_code EC = Reader->read()) 351 exitWithErrorCode(EC, Filename); 352 353 if (ShowAllFunctions || ShowFunction.empty()) 354 Reader->dump(OS); 355 else 356 Reader->dumpFunctionProfile(ShowFunction, OS); 357 358 return 0; 359 } 360 show_main(int argc,const char * argv[])361 static int show_main(int argc, const char *argv[]) { 362 cl::opt<std::string> Filename(cl::Positional, cl::Required, 363 cl::desc("<profdata-file>")); 364 365 cl::opt<bool> ShowCounts("counts", cl::init(false), 366 cl::desc("Show counter values for shown functions")); 367 cl::opt<bool> TextFormat( 368 "text", cl::init(false), 369 cl::desc("Show instr profile data in text dump format")); 370 cl::opt<bool> ShowIndirectCallTargets( 371 "ic-targets", cl::init(false), 372 cl::desc("Show indirect call site target values for shown functions")); 373 cl::opt<bool> ShowAllFunctions("all-functions", cl::init(false), 374 cl::desc("Details for every function")); 375 cl::opt<std::string> ShowFunction("function", 376 cl::desc("Details for matching functions")); 377 378 cl::opt<std::string> OutputFilename("output", cl::value_desc("output"), 379 cl::init("-"), cl::desc("Output file")); 380 cl::alias OutputFilenameA("o", cl::desc("Alias for --output"), 381 cl::aliasopt(OutputFilename)); 382 cl::opt<ProfileKinds> ProfileKind( 383 cl::desc("Profile kind:"), cl::init(instr), 384 cl::values(clEnumVal(instr, "Instrumentation profile (default)"), 385 clEnumVal(sample, "Sample profile"), clEnumValEnd)); 386 387 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n"); 388 389 if (OutputFilename.empty()) 390 OutputFilename = "-"; 391 392 std::error_code EC; 393 raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text); 394 if (EC) 395 exitWithErrorCode(EC, OutputFilename); 396 397 if (ShowAllFunctions && !ShowFunction.empty()) 398 errs() << "warning: -function argument ignored: showing all functions\n"; 399 400 if (ProfileKind == instr) 401 return showInstrProfile(Filename, ShowCounts, ShowIndirectCallTargets, 402 ShowAllFunctions, ShowFunction, TextFormat, OS); 403 else 404 return showSampleProfile(Filename, ShowCounts, ShowAllFunctions, 405 ShowFunction, OS); 406 } 407 main(int argc,const char * argv[])408 int main(int argc, const char *argv[]) { 409 // Print a stack trace if we signal out. 410 sys::PrintStackTraceOnErrorSignal(); 411 PrettyStackTraceProgram X(argc, argv); 412 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 413 414 StringRef ProgName(sys::path::filename(argv[0])); 415 if (argc > 1) { 416 int (*func)(int, const char *[]) = nullptr; 417 418 if (strcmp(argv[1], "merge") == 0) 419 func = merge_main; 420 else if (strcmp(argv[1], "show") == 0) 421 func = show_main; 422 423 if (func) { 424 std::string Invocation(ProgName.str() + " " + argv[1]); 425 argv[1] = Invocation.c_str(); 426 return func(argc - 1, argv + 1); 427 } 428 429 if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0 || 430 strcmp(argv[1], "--help") == 0) { 431 432 errs() << "OVERVIEW: LLVM profile data tools\n\n" 433 << "USAGE: " << ProgName << " <command> [args...]\n" 434 << "USAGE: " << ProgName << " <command> -help\n\n" 435 << "Available commands: merge, show\n"; 436 return 0; 437 } 438 } 439 440 if (argc < 2) 441 errs() << ProgName << ": No command specified!\n"; 442 else 443 errs() << ProgName << ": Unknown command!\n"; 444 445 errs() << "USAGE: " << ProgName << " <merge|show> [args...]\n"; 446 return 1; 447 } 448