3131#include " lld/Common/Reproduce.h"
3232#include " lld/Common/Version.h"
3333#include " llvm/ADT/DenseSet.h"
34+ #include " llvm/ADT/ScopeExit.h"
3435#include " llvm/ADT/StringExtras.h"
3536#include " llvm/ADT/StringRef.h"
3637#include " llvm/BinaryFormat/MachO.h"
4142#include " llvm/Object/Archive.h"
4243#include " llvm/Option/ArgList.h"
4344#include " llvm/Support/CommandLine.h"
45+ #include " llvm/Support/Debug.h"
4446#include " llvm/Support/FileSystem.h"
4547#include " llvm/Support/Parallel.h"
4648#include " llvm/Support/Path.h"
@@ -297,8 +299,24 @@ using DeferredFiles = std::vector<DeferredFile>;
297299// Most input files have been mapped but not yet paged in.
298300// This code forces the page-ins on multiple threads so
299301// the process is not stalled waiting on disk buffer i/o.
300- void multiThreadedPageInBackground (const DeferredFiles &deferred) {
302+ void multiThreadedPageInBackground (DeferredFiles &deferred) {
301303 static const size_t pageSize = Process::getPageSizeEstimate ();
304+ #if 0
305+ ThreadPoolStrategy oldStrategy = llvm::parallel::strategy;
306+ (void)llvm::make_scope_exit([&]() { llvm::parallel::strategy = oldStrategy; });
307+ llvm::parallel::strategy = llvm::hardware_concurrency(config->readThreads);
308+
309+ size_t totalBytes = parallelTransformReduce(deferred, 0,
310+ [](size_t acc, size_t size) { return acc + size; },
311+ [&](DeferredFile &file) {
312+ const StringRef &buffer = file.buffer.getBuffer();
313+ for (const char *page = buffer.data(), *end = page + buffer.size();
314+ page < end; page += pageSize)
315+ LLVM_ATTRIBUTE_UNUSED volatile char t = *page;
316+ return buffer.size();
317+ }
318+ );
319+ #else
302320 static size_t totalBytes = 0 ;
303321 std::atomic_int index = 0 ;
304322
@@ -316,7 +334,7 @@ void multiThreadedPageInBackground(const DeferredFiles &deferred) {
316334 LLVM_ATTRIBUTE_UNUSED volatile char t = *page;
317335 }
318336 });
319-
337+ # endif
320338 if (getenv (" LLD_MULTI_THREAD_PAGE" ))
321339 llvm::dbgs () << " multiThreadedPageIn " << totalBytes << " /"
322340 << deferred.size () << " \n " ;
@@ -337,20 +355,19 @@ static void multiThreadedPageIn(const DeferredFiles &deferred) {
337355 }
338356
339357 if (!deferred.empty ()) {
340- queue.emplace_back (
341- std::unique_ptr<DeferredFiles>(new DeferredFiles (deferred)));
358+ queue.emplace_back (std::make_unique<DeferredFiles>(deferred));
342359 if (!running)
343360 running = new std::thread ([&]() {
344361 while (true ) {
345- mutex.lock ();
346- if (queue.empty ) {
347- mutex.unlock ();
348- break ;
349- }
350- DeferredFiles deferred (*queue.front ());
351- queue.pop_front ();
352- mutex.unlock ();
353- multiThreadedPageInBackground (deferred);
362+ mutex.lock ();
363+ if (queue.empty () ) {
364+ mutex.unlock ();
365+ break ;
366+ }
367+ DeferredFiles deferred (*queue.front ());
368+ queue.pop_front ();
369+ mutex.unlock ();
370+ multiThreadedPageInBackground (deferred);
354371 }
355372 });
356373 }
0 commit comments