Skip to content

Commit 0687bce

Browse files
committed
Switch to use madvise() to page-in files.
1 parent 6241cb3 commit 0687bce

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

lld/MachO/Driver.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
#include "llvm/TextAPI/Architecture.h"
5454
#include "llvm/TextAPI/PackedVersion.h"
5555

56+
#include <sys/mman.h>
57+
5658
using namespace llvm;
5759
using namespace llvm::MachO;
5860
using namespace llvm::object;
@@ -334,11 +336,10 @@ class SerialBackgroundQueue {
334336
// This code forces the page-ins on multiple threads so
335337
// the process is not stalled waiting on disk buffer i/o.
336338
void multiThreadedPageInBackground(DeferredFiles &deferred) {
337-
static const size_t pageSize = Process::getPageSizeEstimate();
338339
static const size_t largeArchive = 10 * 1024 * 1024;
339340
#ifndef NDEBUG
340341
using namespace std::chrono;
341-
std::atomic_int numDeferedFilesTouched = 0;
342+
std::atomic_int numDeferedFilesAdvised = 0;
342343
static std::atomic_uint64_t totalBytes = 0;
343344
auto t0 = high_resolution_clock::now();
344345
#endif
@@ -349,13 +350,19 @@ void multiThreadedPageInBackground(DeferredFiles &deferred) {
349350
return;
350351
#ifndef NDEBUG
351352
totalBytes += buff.size();
352-
numDeferedFilesTouched += 1;
353+
numDeferedFilesAdvised += 1;
353354
#endif
354355

356+
#if _WIN32
357+
static const size_t pageSize = Process::getPageSizeEstimate();
355358
// Reference all file's mmap'd pages to load them into memory.
356359
for (const char *page = buff.data(), *end = page + buff.size(); page < end;
357360
page += pageSize)
358361
LLVM_ATTRIBUTE_UNUSED volatile char t = *page;
362+
#else
363+
// Advise that mmap'd files should be loaded into memory.
364+
madvise((void *)buff.data(), buff.size(), MADV_WILLNEED);
365+
#endif
359366
};
360367
#if LLVM_ENABLE_THREADS
361368
{ // Create scope for waiting for the taskGroup
@@ -376,7 +383,7 @@ void multiThreadedPageInBackground(DeferredFiles &deferred) {
376383
auto dt = high_resolution_clock::now() - t0;
377384
if (Process::GetEnv("LLD_MULTI_THREAD_PAGE"))
378385
llvm::dbgs() << "multiThreadedPageIn " << totalBytes << "/"
379-
<< numDeferedFilesTouched << "/" << deferred.size() << "/"
386+
<< numDeferedFilesAdvised << "/" << deferred.size() << "/"
380387
<< duration_cast<milliseconds>(dt).count() / 1000. << "\n";
381388
#endif
382389
}

0 commit comments

Comments
 (0)