@@ -338,34 +338,37 @@ class SerialBackgroundQueue {
338338// This code forces the page-ins on multiple threads so
339339// the process is not stalled waiting on disk buffer i/o.
340340void multiThreadedPageInBackground (DeferredFiles &deferred) {
341- static const size_t largeArchive = 10 * 1024 * 1024 ;
342- #ifndef NDEBUG
343341 using namespace std ::chrono;
344- std::atomic_int numDeferedFilesAdvised = 0 ;
342+ static const size_t pageSize = Process::getPageSizeEstimate ();
343+ static const size_t largeArchive = 10 * 1024 * 1024 ;
345344 static std::atomic_uint64_t totalBytes = 0 ;
345+ std::atomic_int numDeferedFilesAdvised = 0 ;
346346 auto t0 = high_resolution_clock::now ();
347- #endif
348347
349348 auto preloadDeferredFile = [&](const DeferredFile &deferredFile) {
350349 const StringRef &buff = deferredFile.buffer .getBuffer ();
351350 if (buff.size () > largeArchive)
352351 return ;
353- #ifndef NDEBUG
352+ if (((uintptr_t )buff.data () & (pageSize - 1 )))
353+ return ; // Not mmap()'d (not page aligned).
354+
354355 totalBytes += buff.size ();
355356 numDeferedFilesAdvised += 1 ;
356- #endif
357357
358358#if _WIN32
359- static const size_t pageSize = Process::getPageSizeEstimate ();
360359 // Reference all file's mmap'd pages to load them into memory.
361360 for (const char *page = buff.data (), *end = page + buff.size (); page < end;
362361 page += pageSize)
363362 LLVM_ATTRIBUTE_UNUSED volatile char t = *page;
364363#else
365- // Advise that mmap'd files should be loaded into memory.
366- madvise ((void *)buff.data (), buff.size (), MADV_WILLNEED);
364+ if (madvise ((void *)buff.data (), buff.size (), MADV_WILLNEED) < 0 )
365+ #ifndef NDEBUG
366+ llvm::errs () << " madvise() error " << strerror (errno)
367+ #endif
368+ ;
367369#endif
368370 };
371+
369372#if LLVM_ENABLE_THREADS
370373 { // Create scope for waiting for the taskGroup
371374 std::atomic_size_t index = 0 ;
@@ -380,14 +383,16 @@ void multiThreadedPageInBackground(DeferredFiles &deferred) {
380383 }
381384 });
382385 }
386+ #else
387+ for (const auto &file : deferred)
388+ preloadDeferredFile (file);
383389#endif
384- # ifndef NDEBUG
390+
385391 auto dt = high_resolution_clock::now () - t0;
386392 if (Process::GetEnv (" LLD_MULTI_THREAD_PAGE" ))
387393 llvm::dbgs () << " multiThreadedPageIn " << totalBytes << " /"
388394 << numDeferedFilesAdvised << " /" << deferred.size () << " /"
389395 << duration_cast<milliseconds>(dt).count () / 1000 . << " \n " ;
390- #endif
391396}
392397
393398static void multiThreadedPageIn (const DeferredFiles &deferred) {
0 commit comments