|
47 | 47 | #include "llvm/Support/Process.h" |
48 | 48 | #include "llvm/Support/TarWriter.h" |
49 | 49 | #include "llvm/Support/TargetSelect.h" |
| 50 | +#include "llvm/Support/Threading.h" |
50 | 51 | #include "llvm/Support/TimeProfiler.h" |
51 | 52 | #include "llvm/TargetParser/Host.h" |
52 | 53 | #include "llvm/TextAPI/Architecture.h" |
@@ -283,95 +284,75 @@ static void saveThinArchiveToRepro(ArchiveFile const *file) { |
283 | 284 | ": Archive::children failed: " + toString(std::move(e))); |
284 | 285 | } |
285 | 286 |
|
286 | | -typedef struct { |
| 287 | +class DeferredFile { |
| 288 | +public: |
287 | 289 | StringRef path; |
288 | 290 | bool isLazy; |
289 | 291 | std::optional<MemoryBufferRef> buffer; |
290 | 292 | const char *start; |
291 | 293 | size_t size; |
292 | | -} DeferredFile; |
293 | | -typedef std::vector<DeferredFile> DeferredFiles; |
| 294 | +}; |
| 295 | +using DeferredFiles = std::vector<DeferredFile>; |
294 | 296 |
|
295 | | -#ifndef _WIN32 |
296 | | -typedef struct { |
| 297 | +class PageInState { |
297 | 298 | 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 | + |
| 302 | +public: |
| 303 | + PageInState(DeferredFiles &deferred, std::mutex *busy) { |
| 304 | + this->deferred = deferred; |
| 305 | + this->busy = busy; |
| 306 | + pageSize = llvm::sys::Process::getPageSizeEstimate(); |
| 307 | + } |
| 308 | + |
| 309 | + // Most input files have been mapped but not yet paged in. |
| 310 | + // This code forces the page-ins on multiple threads so |
| 311 | + // the process is not stalled waiting on disk buffer i/o. |
| 312 | + void multiThreadedPageInBackground() { |
| 313 | + static size_t totalBytes; |
| 314 | + |
| 315 | + parallelFor(0, config->readThreads, [&](size_t I) { |
| 316 | + while (true) { |
| 317 | + mutex.lock(); |
| 318 | + if (counter >= deferred.size()) { |
| 319 | + mutex.unlock(); |
| 320 | + return; |
| 321 | + } |
| 322 | + DeferredFile &file = deferred[counter]; |
| 323 | + totalBytes += file.size; |
| 324 | + counter += 1; |
| 325 | + mutex.unlock(); |
| 326 | + |
| 327 | + int t = 0; // Reference each page to load it into memory. |
| 328 | + for (const char *page = file.start, *end = page + file.size; page < end; |
| 329 | + page += pageSize) |
| 330 | + t += *page; |
| 331 | + total += t; // Avoids the loop being optimised out. |
| 332 | + } |
| 333 | + }); |
339 | 334 |
|
340 | | - for (int t = 0; t < config->readThreads; t++) |
341 | | - pthread_join(running[t], nullptr); |
| 335 | + if (getenv("LLD_MULTI_THREAD_PAGE")) |
| 336 | + llvm::dbgs() << "multiThreadedPageIn " << totalBytes << "/" |
| 337 | + << deferred.size() << "\n"; |
342 | 338 |
|
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 |
| 339 | + busy->unlock(); |
| 340 | + delete this; |
| 341 | + } |
| 342 | +}; |
348 | 343 |
|
349 | 344 | static void multiThreadedPageIn(DeferredFiles deferred) { |
350 | | -#ifndef _WIN32 |
351 | | - static pthread_t running; |
352 | | - static pthread_mutex_t busy; |
| 345 | + static std::thread *running; |
| 346 | + static std::mutex busy; |
353 | 347 |
|
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 |
| 348 | + busy.lock(); |
| 349 | + if (running) { |
| 350 | + running->join(); |
| 351 | + delete running; |
| 352 | + } |
| 353 | + |
| 354 | + running = new std::thread(&PageInState::multiThreadedPageInBackground, |
| 355 | + new PageInState(deferred, &busy)); |
375 | 356 | } |
376 | 357 |
|
377 | 358 | static InputFile *processFile(std::optional<MemoryBufferRef> buffer, |
@@ -1432,8 +1413,7 @@ static void createFiles(const InputArgList &args) { |
1432 | 1413 | } |
1433 | 1414 |
|
1434 | 1415 | // flush threads |
1435 | | - deferredFiles.clear(); |
1436 | | - multiThreadedPageIn(deferredFiles); |
| 1416 | + multiThreadedPageIn(DeferredFiles()); |
1437 | 1417 | } |
1438 | 1418 | } |
1439 | 1419 |
|
|
0 commit comments