Skip to content

Commit 3e5da0b

Browse files
committed
Response to first review.
1 parent a8eeead commit 3e5da0b

File tree

2 files changed

+59
-80
lines changed

2 files changed

+59
-80
lines changed

lld/MachO/Driver.cpp

Lines changed: 58 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "llvm/Support/Process.h"
4848
#include "llvm/Support/TarWriter.h"
4949
#include "llvm/Support/TargetSelect.h"
50+
#include "llvm/Support/Threading.h"
5051
#include "llvm/Support/TimeProfiler.h"
5152
#include "llvm/TargetParser/Host.h"
5253
#include "llvm/TextAPI/Architecture.h"
@@ -283,95 +284,74 @@ static void saveThinArchiveToRepro(ArchiveFile const *file) {
283284
": Archive::children failed: " + toString(std::move(e)));
284285
}
285286

286-
typedef struct {
287+
class DeferredFile {
288+
public:
287289
StringRef path;
288290
bool isLazy;
289291
std::optional<MemoryBufferRef> buffer;
290292
const char *start;
291293
size_t size;
292-
} DeferredFile;
293-
typedef std::vector<DeferredFile> DeferredFiles;
294+
};
295+
using DeferredFiles = std::vector<DeferredFile>;
294296

295-
#ifndef _WIN32
296-
typedef struct {
297+
class PageInState {
297298
DeferredFiles deferred;
298-
size_t counter, total, pageSize;
299-
pthread_mutex_t mutex;
300-
} PageInState;
301-
302-
// Most input files have been mapped but not yet paged in.
303-
// This code forces the page-ins on multiple threads so
304-
// the process is not stalled waiting on disk buffer i/o.
305-
static void multiThreadedPageInBackground(PageInState *state) {
306-
#define MaxReadThreads 200
307-
static size_t totalBytes;
308-
309-
pthread_t running[MaxReadThreads];
310-
if (config->readThreads > MaxReadThreads)
311-
config->readThreads = MaxReadThreads;
312-
pthread_mutex_init(&state->mutex, nullptr);
313-
314-
for (int t = 0; t < config->readThreads; t++)
315-
pthread_create(
316-
&running[t], nullptr,
317-
[](void *ptr) -> void * {
318-
PageInState &state = *(PageInState *)ptr;
319-
while (true) {
320-
pthread_mutex_lock(&state.mutex);
321-
if (state.counter >= state.deferred.size()) {
322-
pthread_mutex_unlock(&state.mutex);
323-
return nullptr;
324-
}
325-
DeferredFile &file = state.deferred[state.counter];
326-
state.counter += 1;
327-
pthread_mutex_unlock(&state.mutex);
328-
329-
const char *page = file.start, *end = page + file.size;
330-
totalBytes += end - page;
331-
332-
int t = 0; // Reference each page to load it into memory.
333-
for (; page < end; page += state.pageSize)
334-
t += *page;
335-
state.total += t; // Avoids the loop being optimised out.
336-
}
337-
},
338-
state);
299+
size_t counter = 0, total = 0, pageSize;
300+
std::mutex mutex, *busy;
301+
public:
302+
PageInState(DeferredFiles &deferred, std::mutex *busy) {
303+
this->deferred = deferred;
304+
this->busy = busy;
305+
pageSize = llvm::sys::Process::getPageSizeEstimate();
306+
}
307+
308+
// Most input files have been mapped but not yet paged in.
309+
// This code forces the page-ins on multiple threads so
310+
// the process is not stalled waiting on disk buffer i/o.
311+
void multiThreadedPageInBackground() {
312+
static size_t totalBytes;
313+
314+
parallelFor(0, config->readThreads, [&](size_t I) {
315+
while (true) {
316+
mutex.lock();
317+
if (counter >= deferred.size()) {
318+
mutex.unlock();
319+
return;
320+
}
321+
DeferredFile &file = deferred[counter];
322+
totalBytes += file.size;
323+
counter += 1;
324+
mutex.unlock();
325+
326+
int t = 0; // Reference each page to load it into memory.
327+
for (const char *page = file.start, *end = page + file.size;
328+
page < end; page += pageSize)
329+
t += *page;
330+
total += t; // Avoids the loop being optimised out.
331+
}
332+
});
339333

340-
for (int t = 0; t < config->readThreads; t++)
341-
pthread_join(running[t], nullptr);
334+
if (getenv("LLD_MULTI_THREAD_PAGE"))
335+
llvm::dbgs() << "multiThreadedPageIn " << totalBytes << "/" <<
336+
deferred.size() << "\n";
342337

343-
pthread_mutex_destroy(&state->mutex);
344-
if (getenv("LLD_MULTI_THREAD_PAGE"))
345-
printf("multiThreadedPageIn %ld/%ld\n", totalBytes, state->deferred.size());
346-
}
347-
#endif
338+
busy->unlock();
339+
delete this;
340+
}
341+
};
348342

349343
static void multiThreadedPageIn(DeferredFiles deferred) {
350-
#ifndef _WIN32
351-
static pthread_t running;
352-
static pthread_mutex_t busy;
344+
static std::thread *running;
345+
static std::mutex busy;
353346

354-
if (running)
355-
pthread_join(running, nullptr);
356-
else
357-
pthread_mutex_init(&busy, nullptr);
358-
359-
PageInState *state =
360-
new PageInState{deferred, 0, 0, llvm::sys::Process::getPageSizeEstimate(),
361-
pthread_mutex_t()};
362-
363-
pthread_mutex_lock(&busy);
364-
pthread_create(
365-
&running, nullptr,
366-
[](void *ptr) -> void * {
367-
PageInState *state = (PageInState *)ptr;
368-
multiThreadedPageInBackground(state);
369-
pthread_mutex_unlock(&busy);
370-
delete state;
371-
return nullptr;
372-
},
373-
state);
374-
#endif
347+
busy.lock();
348+
if (running) {
349+
running->join();
350+
delete running;
351+
}
352+
353+
running = new std::thread(&PageInState::multiThreadedPageInBackground,
354+
new PageInState(deferred, &busy));
375355
}
376356

377357
static InputFile *processFile(std::optional<MemoryBufferRef> buffer,
@@ -1432,8 +1412,7 @@ static void createFiles(const InputArgList &args) {
14321412
}
14331413

14341414
// flush threads
1435-
deferredFiles.clear();
1436-
multiThreadedPageIn(deferredFiles);
1415+
multiThreadedPageIn(DeferredFiles());
14371416
}
14381417
}
14391418

lld/MachO/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ def interposable : Flag<["-"], "interposable">,
397397
HelpText<"Indirects access to all exported symbols in an image">,
398398
Group<grp_opts>;
399399
def read_threads : Joined<["--"], "read-threads=">,
400-
HelpText<"Number of threads to use paging in files.">,
400+
HelpText<"Number of threads to use if pro-actively paging in files.">,
401401
Group<grp_lld>;
402402
def order_file : Separate<["-"], "order_file">,
403403
MetaVarName<"<file>">,

0 commit comments

Comments
 (0)