Index: llvm/include/llvm/DWARFLinker/DWARFLinker.h =================================================================== --- llvm/include/llvm/DWARFLinker/DWARFLinker.h +++ llvm/include/llvm/DWARFLinker/DWARFLinker.h @@ -260,6 +260,9 @@ /// Allows to generate log of linking process to the standard output. void setVerbosity(bool Verbose) { Options.Verbose = Verbose; } + /// Print statistics to standard output. + void setStatistics(bool Statistics) { Options.Statistics = Statistics; } + /// Do not emit linked dwarf info. void setNoOutput(bool NoOut) { Options.NoOutput = NoOut; } @@ -556,9 +559,10 @@ /// Construct the output DIE tree by cloning the DIEs we /// chose to keep above. If there are no valid relocs, then there's /// nothing to clone/emit. - void cloneAllCompileUnits(DWARFContext &DwarfContext, const DwarfFile &File, - OffsetsStringPool &StringPool, - bool IsLittleEndian); + uint64_t cloneAllCompileUnits(DWARFContext &DwarfContext, + const DwarfFile &File, + OffsetsStringPool &StringPool, + bool IsLittleEndian); private: using AttributeSpec = DWARFAbbreviationDeclaration::AttributeSpec; @@ -757,6 +761,9 @@ /// Generate processing log to the standard output. bool Verbose = false; + /// Print statistics. + bool Statistics = false; + /// Skip emitting output bool NoOutput = false; Index: llvm/lib/DWARFLinker/DWARFLinker.cpp =================================================================== --- llvm/lib/DWARFLinker/DWARFLinker.cpp +++ llvm/lib/DWARFLinker/DWARFLinker.cpp @@ -25,6 +25,7 @@ #include "llvm/Support/Error.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorOr.h" +#include "llvm/Support/FormatVariadic.h" #include "llvm/Support/LEB128.h" #include "llvm/Support/Path.h" #include "llvm/Support/ThreadPool.h" @@ -32,6 +33,21 @@ namespace llvm { +/// Hold the input and output of the debug info size in bytes. +struct DebugInfoSize { + uint64_t Input; + uint64_t Output; +}; + +/// Compute the total size of the debug info. +static uint64_t getDebugInfoSize(DWARFContext &Dwarf) { + uint64_t Size = 0; + for (auto &Unit : Dwarf.compile_units()) { + Size += Unit->getLength(); + } + return Size; +} + /// Similar to DWARFUnitSection::getUnitForOffset(), but returning our /// CompileUnit object instead. static CompileUnit *getUnitForOffset(const UnitListTy &Units, uint64_t Offset) { @@ -2071,12 +2087,13 @@ return Error::success(); } -void DWARFLinker::DIECloner::cloneAllCompileUnits(DWARFContext &DwarfContext, - const DwarfFile &File, - OffsetsStringPool &StringPool, - bool IsLittleEndian) { +uint64_t DWARFLinker::DIECloner::cloneAllCompileUnits( + DWARFContext &DwarfContext, const DwarfFile &File, + OffsetsStringPool &StringPool, bool IsLittleEndian) { uint64_t OutputDebugInfoSize = Linker.Options.NoOutput ? 0 : Emitter->getDebugInfoSectionSize(); + const uint64_t StartOutputDebugInfoSize = OutputDebugInfoSize; + for (auto &CurrentUnit : CompileUnits) { auto InputDIE = CurrentUnit->getOrigUnit().getUnitDIE(); CurrentUnit->setStartOffset(OutputDebugInfoSize); @@ -2141,6 +2158,8 @@ CurrentUnit->computeNextUnitOffset()); } } + + return OutputDebugInfoSize - StartOutputDebugInfoSize; } void DWARFLinker::updateAccelKind(DWARFContext &Dwarf) { @@ -2393,6 +2412,9 @@ } }; + // For each object file map how many bytes were emitted. + StringMap SizeByObject; + // And then the remaining work in serial again. // Note, although this loop runs in serial, it can run in parallel with // the analyzeContextInfo loop so long as we process files with indices >= @@ -2425,11 +2447,14 @@ // need to reset the NextValidReloc index to the beginning. if (OptContext.File.Addresses->hasValidRelocs() || LLVM_UNLIKELY(Options.Update)) { - DIECloner(*this, TheDwarfEmitter, OptContext.File, DIEAlloc, - OptContext.CompileUnits, Options.Update) - .cloneAllCompileUnits(*OptContext.File.Dwarf, OptContext.File, - OffsetsStringPool, - OptContext.File.Dwarf->isLittleEndian()); + SizeByObject[OptContext.File.FileName].Input = + getDebugInfoSize(*OptContext.File.Dwarf); + SizeByObject[OptContext.File.FileName].Output = + DIECloner(*this, TheDwarfEmitter, OptContext.File, DIEAlloc, + OptContext.CompileUnits, Options.Update) + .cloneAllCompileUnits(*OptContext.File.Dwarf, OptContext.File, + OffsetsStringPool, + OptContext.File.Dwarf->isLittleEndian()); } if (!Options.NoOutput && !OptContext.CompileUnits.empty() && LLVM_LIKELY(!Options.Update)) @@ -2505,6 +2530,52 @@ Pool.wait(); } + if (Options.Statistics) { + // Create a vector sorted in descending order by output size. + std::vector> Sorted; + for (auto &E : SizeByObject) + Sorted.emplace_back(E.first(), E.second); + sort(Sorted.begin(), Sorted.end(), [](auto &LHS, auto &RHS) { + return LHS.second.Output > RHS.second.Output; + }); + + auto ComputePercentange = [](int64_t Input, int64_t Output) -> float { + const float Difference = std::abs(Input - Output); + const float Sum = Input + Output; + if (Sum == 0) + return 0; + return (Difference / (Sum / 2)); + }; + + int64_t InputTotal = 0; + int64_t OutputTotal = 0; + const char *FormatStr = "{0,-47} {1,10} -> {2,-10} {3,-6:P}\n"; + + // Print header. + outs() << "----------------------------------------------------------------" + "---------------\n"; + outs() << "Object File Bytes Input -> " + "Output Change\n"; + outs() << "----------------------------------------------------------------" + "---------------\n"; + + // Print body. + for (auto &E : Sorted) { + InputTotal += E.second.Input; + OutputTotal += E.second.Output; + llvm::outs() << formatv( + FormatStr, sys::path::filename(E.first).take_back(47), E.second.Input, + E.second.Output, ComputePercentange(E.second.Input, E.second.Output)); + } + // Print total and footer. + outs() << "----------------------------------------------------------------" + "---------------\n"; + llvm::outs() << formatv(FormatStr, "Total", InputTotal, OutputTotal, + ComputePercentange(InputTotal, OutputTotal)); + outs() << "----------------------------------------------------------------" + "---------------\n\n"; + } + return true; } Index: llvm/test/tools/dsymutil/X86/statistics.test =================================================================== --- /dev/null +++ llvm/test/tools/dsymutil/X86/statistics.test @@ -0,0 +1,21 @@ +# RUN: dsymutil -statistics -oso-prepend-path=%p/.. %p/../Inputs/basic.macho.x86_64 %p/../Inputs/basic-archive.macho.x86_64 %p/../Inputs/basic-lto.macho.x86_64 %p/../Inputs/basic-lto-dw4.macho.x86_64 -o %t 2>&1 | FileCheck %s +# +# CHECK: ------------------------------------------------------------------------------- +# CHECK-NEXT: Object File Bytes Input -> Output Change +# CHECK-NEXT: ------------------------------------------------------------------------------- +# CHECK-DAG: basic2.macho.x86_64.o {{[0-9]+}} -> {{[0-9]+}} {{[0-9]+}}.{{[0-9]+}}% +# CHECK-DAG: basic3.macho.x86_64.o {{[0-9]+}} -> {{[0-9]+}} {{[0-9]+}}.{{[0-9]+}}% +# CHECK-DAG: basic1.macho.x86_64.o {{[0-9]+}} -> {{[0-9]+}} {{[0-9]+}}.{{[0-9]+}}% +# CHECK: ------------------------------------------------------------------------------- +# CHECK-NEXT: Total {{[0-9]+}} -> {{[0-9]+}} {{[0-9]+}}.{{[0-9]+}}% +# CHECK-NEXT: ------------------------------------------------------------------------------- + +--- +triple: 'x86_64-apple-darwin' +objects: + - filename: invalid.o + timestamp: 1518197670 + symbols: + - { sym: _main, objAddr: 0x0000000000000010, binAddr: 0x0000000100000FB0, size: 0x00000008 } + - { sym: _g, objAddr: 0x0000000000000000, binAddr: 0x0000000100000FA0, size: 0x00000010 } +... Index: llvm/test/tools/dsymutil/cmdline.test =================================================================== --- llvm/test/tools/dsymutil/cmdline.test +++ llvm/test/tools/dsymutil/cmdline.test @@ -18,6 +18,7 @@ HELP: -papertrail HELP: -remarks-output-format HELP: -remarks-prepend-path +HELP: -statistics HELP: -symbol-map HELP: -symtab HELP: {{ -S }} Index: llvm/tools/dsymutil/DwarfLinkerForBinary.cpp =================================================================== --- llvm/tools/dsymutil/DwarfLinkerForBinary.cpp +++ llvm/tools/dsymutil/DwarfLinkerForBinary.cpp @@ -315,6 +315,7 @@ }; GeneralLinker.setVerbosity(Options.Verbose); + GeneralLinker.setStatistics(Options.Statistics); GeneralLinker.setNoOutput(Options.NoOutput); GeneralLinker.setNoODR(Options.NoODR); GeneralLinker.setUpdate(Options.Update); Index: llvm/tools/dsymutil/LinkUtils.h =================================================================== --- llvm/tools/dsymutil/LinkUtils.h +++ llvm/tools/dsymutil/LinkUtils.h @@ -27,6 +27,9 @@ /// Verbosity bool Verbose = false; + /// Statistics + bool Statistics = false; + /// Skip emitting output bool NoOutput = false; Index: llvm/tools/dsymutil/Options.td =================================================================== --- llvm/tools/dsymutil/Options.td +++ llvm/tools/dsymutil/Options.td @@ -24,6 +24,10 @@ HelpText<"Enable verbose mode.">, Group; +def statistics: F<"statistics">, + HelpText<"Print statistics.">, + Group; + def verify: F<"verify">, HelpText<"Run the DWARF verifier on the linked DWARF debug info.">, Group; Index: llvm/tools/dsymutil/dsymutil.cpp =================================================================== --- llvm/tools/dsymutil/dsymutil.cpp +++ llvm/tools/dsymutil/dsymutil.cpp @@ -220,6 +220,7 @@ Options.LinkOpts.NoTimestamp = Args.hasArg(OPT_no_swiftmodule_timestamp); Options.LinkOpts.Update = Args.hasArg(OPT_update); Options.LinkOpts.Verbose = Args.hasArg(OPT_verbose); + Options.LinkOpts.Statistics = Args.hasArg(OPT_statistics); if (Expected AccelKind = getAccelTableKind(Args)) { Options.LinkOpts.TheAccelTableKind = *AccelKind; @@ -548,7 +549,11 @@ // Shared a single binary holder for all the link steps. BinaryHolder BinHolder(Options.LinkOpts.VFS); - ThreadPoolStrategy S = hardware_concurrency(Options.LinkOpts.Threads); + // Statistics only require different architectures to be processed + // sequentially, the link can happen in parallel. Therefore we check it + // hear instead of setting LinkOpts.Threads to 1. + ThreadPoolStrategy S = hardware_concurrency( + Options.LinkOpts.Statistics ? 1 : Options.LinkOpts.Threads); if (Options.LinkOpts.Threads == 0) { // If NumThreads is not specified, create one thread for each input, up to // the number of hardware threads.