diff options
author | Vedant Kumar <vsk@apple.com> | 2016-07-19 01:17:20 +0000 |
---|---|---|
committer | Vedant Kumar <vsk@apple.com> | 2016-07-19 01:17:20 +0000 |
commit | e3a0bf504859c95513d75df06aca1a6d38c44d60 (patch) | |
tree | b98db977f7a0aa95ee9bde587472456dc8855404 /llvm/tools/llvm-profdata/llvm-profdata.cpp | |
parent | 21ab20e0050d18185f6020a32aadd73c351a7e1d (diff) | |
download | bcm5719-llvm-e3a0bf504859c95513d75df06aca1a6d38c44d60.tar.gz bcm5719-llvm-e3a0bf504859c95513d75df06aca1a6d38c44d60.zip |
Retry: [llvm-profdata] Speed up merging by using a thread pool
Add a "-j" option to llvm-profdata to control the number of threads used.
Auto-detect NumThreads when it isn't specified, and avoid spawning threads when
they wouldn't be beneficial.
I tested this patch using a raw profile produced by clang (147MB). Here is the
time taken to merge 4 copies together on my laptop:
No thread pool: 112.87s user 5.92s system 97% cpu 2:01.08 total
With 2 threads: 134.99s user 26.54s system 164% cpu 1:33.31 total
Changes since the initial commit:
- When handling odd-length inputs, call ThreadPool::wait() before merging the
last profile. Should fix a race/off-by-one (see r275937).
Differential Revision: https://reviews.llvm.org/D22438
llvm-svn: 275938
Diffstat (limited to 'llvm/tools/llvm-profdata/llvm-profdata.cpp')
-rw-r--r-- | llvm/tools/llvm-profdata/llvm-profdata.cpp | 140 |
1 files changed, 117 insertions, 23 deletions
diff --git a/llvm/tools/llvm-profdata/llvm-profdata.cpp b/llvm/tools/llvm-profdata/llvm-profdata.cpp index 8e4b4c3d4ed..26ce4cc234f 100644 --- a/llvm/tools/llvm-profdata/llvm-profdata.cpp +++ b/llvm/tools/llvm-profdata/llvm-profdata.cpp @@ -29,6 +29,7 @@ #include "llvm/Support/Path.h" #include "llvm/Support/PrettyStackTrace.h" #include "llvm/Support/Signals.h" +#include "llvm/Support/ThreadPool.h" #include "llvm/Support/raw_ostream.h" #include <algorithm> @@ -117,9 +118,68 @@ struct WeightedFile { }; typedef SmallVector<WeightedFile, 5> WeightedFileVector; +/// Keep track of merged data and reported errors. +struct WriterContext { + std::mutex Lock; + InstrProfWriter Writer; + Error Err; + StringRef ErrWhence; + std::mutex &ErrLock; + SmallSet<instrprof_error, 4> &WriterErrorCodes; + + WriterContext(bool IsSparse, std::mutex &ErrLock, + SmallSet<instrprof_error, 4> &WriterErrorCodes) + : Lock(), Writer(IsSparse), Err(Error::success()), ErrWhence(""), + ErrLock(ErrLock), WriterErrorCodes(WriterErrorCodes) {} +}; + +/// Load an input into a writer context. +static void loadInput(const WeightedFile &Input, WriterContext *WC) { + std::unique_lock<std::mutex> CtxGuard{WC->Lock}; + + // If there's a pending hard error, don't do more work. + if (WC->Err) + return; + + WC->ErrWhence = Input.Filename; + + auto ReaderOrErr = InstrProfReader::create(Input.Filename); + if ((WC->Err = ReaderOrErr.takeError())) + return; + + auto Reader = std::move(ReaderOrErr.get()); + bool IsIRProfile = Reader->isIRLevelProfile(); + if (WC->Writer.setIsIRLevelProfile(IsIRProfile)) { + WC->Err = make_error<StringError>( + "Merge IR generated profile with Clang generated profile.", + std::error_code()); + return; + } + + for (auto &I : *Reader) { + if (Error E = WC->Writer.addRecord(std::move(I), Input.Weight)) { + // Only show hint the first time an error occurs. + instrprof_error IPE = InstrProfError::take(std::move(E)); + std::unique_lock<std::mutex> ErrGuard{WC->ErrLock}; + bool firstTime = WC->WriterErrorCodes.insert(IPE).second; + handleMergeWriterError(make_error<InstrProfError>(IPE), Input.Filename, + I.Name, firstTime); + } + } + if (Reader->hasError()) + WC->Err = Reader->getError(); +} + +/// Merge the \p Src writer context into \p Dst. +static void mergeWriterContexts(WriterContext *Dst, WriterContext *Src) { + if (Error E = Dst->Writer.mergeRecordsFromWriter(std::move(Src->Writer))) + Dst->Err = std::move(E); +} + static void mergeInstrProfile(const WeightedFileVector &Inputs, StringRef OutputFilename, - ProfileFormat OutputFormat, bool OutputSparse) { + ProfileFormat OutputFormat, bool OutputSparse, + unsigned NumThreads) { if (OutputFilename.compare("-") == 0) exitWithError("Cannot write indexed profdata format to stdout."); @@ -131,30 +191,59 @@ static void mergeInstrProfile(const WeightedFileVector &Inputs, if (EC) exitWithErrorCode(EC, OutputFilename); - InstrProfWriter Writer(OutputSparse); + std::mutex ErrorLock; SmallSet<instrprof_error, 4> WriterErrorCodes; - for (const auto &Input : Inputs) { - auto ReaderOrErr = InstrProfReader::create(Input.Filename); - if (Error E = ReaderOrErr.takeError()) - exitWithError(std::move(E), Input.Filename); - - auto Reader = std::move(ReaderOrErr.get()); - bool IsIRProfile = Reader->isIRLevelProfile(); - if (Writer.setIsIRLevelProfile(IsIRProfile)) - exitWithError("Merge IR generated profile with Clang generated profile."); - - for (auto &I : *Reader) { - if (Error E = Writer.addRecord(std::move(I), Input.Weight)) { - // Only show hint the first time an error occurs. - instrprof_error IPE = InstrProfError::take(std::move(E)); - bool firstTime = WriterErrorCodes.insert(IPE).second; - handleMergeWriterError(make_error<InstrProfError>(IPE), Input.Filename, - I.Name, firstTime); - } + + // If NumThreads is not specified, auto-detect a good default. + if (NumThreads == 0) + NumThreads = std::max(1U, std::min(std::thread::hardware_concurrency(), + unsigned(Inputs.size() / 2))); + + // Initialize the writer contexts. + SmallVector<std::unique_ptr<WriterContext>, 4> Contexts; + for (unsigned I = 0; I < NumThreads; ++I) + Contexts.emplace_back(llvm::make_unique<WriterContext>( + OutputSparse, ErrorLock, WriterErrorCodes)); + + if (NumThreads == 1) { + for (const auto &Input : Inputs) + loadInput(Input, Contexts[0].get()); + } else { + ThreadPool Pool(NumThreads); + + // Load the inputs in parallel (N/NumThreads serial steps). + unsigned Ctx = 0; + for (const auto &Input : Inputs) { + Pool.async(loadInput, Input, Contexts[Ctx].get()); + Ctx = (Ctx + 1) % NumThreads; } - if (Reader->hasError()) - exitWithError(Reader->getError(), Input.Filename); + Pool.wait(); + + // Merge the writer contexts together (~ lg(NumThreads) serial steps). + unsigned Mid = Contexts.size() / 2; + unsigned End = Contexts.size(); + assert(Mid > 0 && "Expected more than one context"); + do { + for (unsigned I = 0; I < Mid; ++I) + Pool.async(mergeWriterContexts, Contexts[I].get(), + Contexts[I + Mid].get()); + Pool.wait(); + if (End & 1) { + Pool.async(mergeWriterContexts, Contexts[0].get(), + Contexts[End - 1].get()); + Pool.wait(); + } + End = Mid; + Mid /= 2; + } while (Mid > 0); } + + // Handle deferred hard errors encountered during merging. + for (std::unique_ptr<WriterContext> &WC : Contexts) + if (WC->Err) + exitWithError(std::move(WC->Err), WC->ErrWhence); + + InstrProfWriter &Writer = Contexts[0]->Writer; if (OutputFormat == PF_Text) Writer.writeText(Output); else @@ -288,6 +377,11 @@ static int merge_main(int argc, const char *argv[]) { clEnumValEnd)); cl::opt<bool> OutputSparse("sparse", cl::init(false), cl::desc("Generate a sparse profile (only meaningful for -instr)")); + cl::opt<unsigned> NumThreads( + "num-threads", cl::init(0), + cl::desc("Number of merge threads to use (default: autodetect)")); + cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"), + cl::aliasopt(NumThreads)); cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n"); @@ -314,7 +408,7 @@ static int merge_main(int argc, const char *argv[]) { if (ProfileKind == instr) mergeInstrProfile(WeightedInputs, OutputFilename, OutputFormat, - OutputSparse); + OutputSparse, NumThreads); else mergeSampleProfile(WeightedInputs, OutputFilename, OutputFormat); |