@@ -283,11 +283,11 @@ static void saveThinArchiveToRepro(ArchiveFile const *file) {
283
283
" : Archive::children failed: " + toString (std::move (e)));
284
284
}
285
285
286
- static InputFile *deferredAddFile (std::optional<MemoryBufferRef> buffer,
287
- StringRef path, LoadType loadType,
288
- bool isLazy = false , bool isExplicit = true ,
289
- bool isBundleLoader = false ,
290
- bool isForceHidden = false ) {
286
+ static InputFile *processFile (std::optional<MemoryBufferRef> buffer,
287
+ StringRef path, LoadType loadType,
288
+ bool isLazy = false , bool isExplicit = true ,
289
+ bool isBundleLoader = false ,
290
+ bool isForceHidden = false ) {
291
291
if (!buffer)
292
292
return nullptr ;
293
293
MemoryBufferRef mbref = *buffer;
@@ -446,8 +446,24 @@ static InputFile *addFile(StringRef path, LoadType loadType,
446
446
bool isLazy = false , bool isExplicit = true ,
447
447
bool isBundleLoader = false ,
448
448
bool isForceHidden = false ) {
449
- return deferredAddFile (readFile (path), path, loadType, isLazy, isExplicit,
450
- isBundleLoader, isForceHidden);
449
+ return processFile (readFile (path), path, loadType, isLazy, isExplicit,
450
+ isBundleLoader, isForceHidden);
451
+ }
452
+
453
+ typedef struct {
454
+ StringRef path;
455
+ LoadType loadType;
456
+ bool isLazy;
457
+ std::optional<MemoryBufferRef> buffer;
458
+ } DeferredFile;
459
+
460
+ static void deferFile (StringRef path, LoadType loadType, bool isLazy,
461
+ std::vector<DeferredFile> &deferred) {
462
+ std::optional<MemoryBufferRef> buffer = readFile (path);
463
+ if (config->readThreads )
464
+ deferred.push_back ({path, loadType, isLazy, buffer});
465
+ else
466
+ processFile (buffer, path, loadType, isLazy);
451
467
}
452
468
453
469
static std::vector<StringRef> missingAutolinkWarnings;
@@ -573,23 +589,14 @@ void macho::resolveLCLinkerOptions() {
573
589
}
574
590
}
575
591
576
- typedef struct {
577
- StringRef path;
578
- std::optional<MemoryBufferRef> buffer;
579
- } DeferredFile;
580
-
581
592
static void addFileList (StringRef path, bool isLazy,
582
593
std::vector<DeferredFile> &deferredFiles) {
583
594
std::optional<MemoryBufferRef> buffer = readFile (path);
584
595
if (!buffer)
585
596
return ;
586
597
MemoryBufferRef mbref = *buffer;
587
598
for (StringRef path : args::getLines (mbref))
588
- if (config->readThreads ) {
589
- StringRef rrpath = rerootPath (path);
590
- deferredFiles.push_back ({rrpath, readFile (rrpath)});
591
- } else
592
- addFile (rerootPath (path), LoadType::CommandLine, isLazy);
599
+ deferFile (rerootPath (path), LoadType::CommandLine, isLazy, deferredFiles);
593
600
}
594
601
595
602
// We expect sub-library names of the form "libfoo", which will match a dylib
@@ -1239,43 +1246,44 @@ static void handleSymbolPatterns(InputArgList &args,
1239
1246
// the process is not stalled waiting on disk buffer i/o.
1240
1247
void multiThreadedPageIn (std::vector<DeferredFile> &deferred, int nthreads) {
1241
1248
#ifndef _WIN32
1249
+ #define MaxReadThreads 200
1242
1250
typedef struct {
1251
+ size_t counter, bytes, total, pageSize;
1243
1252
std::vector<DeferredFile> &deferred;
1244
- size_t counter, total, pageSize;
1245
1253
pthread_mutex_t mutex;
1246
1254
} PageInState;
1247
- PageInState state = {deferred , 0 , 0 ,
1248
- llvm::sys::Process::getPageSizeEstimate (),
1249
- pthread_mutex_t ()};
1255
+ PageInState state = {0 , 0 , 0 ,
1256
+ llvm::sys::Process::getPageSizeEstimate (),
1257
+ deferred, pthread_mutex_t ()};
1250
1258
pthread_mutex_init (&state.mutex , NULL );
1251
1259
1252
- pthread_t running[200 ];
1253
- int maxthreads = sizeof running / sizeof running[0 ];
1254
- if (nthreads > maxthreads)
1255
- nthreads = maxthreads;
1260
+ pthread_t running[MaxReadThreads];
1261
+ if (nthreads > MaxReadThreads)
1262
+ nthreads = MaxReadThreads;
1256
1263
1257
1264
for (int t = 0 ; t < nthreads; t++)
1258
1265
pthread_create (
1259
1266
&running[t], nullptr ,
1260
1267
[](void *ptr) -> void * {
1261
1268
PageInState &state = *(PageInState *)ptr;
1262
- static int total = 0 ;
1263
1269
while (true ) {
1264
1270
pthread_mutex_lock (&state.mutex );
1265
1271
if (state.counter >= state.deferred .size ()) {
1266
1272
pthread_mutex_unlock (&state.mutex );
1267
1273
return nullptr ;
1268
1274
}
1269
- DeferredFile &add = state.deferred [state.counter ];
1275
+ DeferredFile &file = state.deferred [state.counter ];
1270
1276
state.counter += 1 ;
1271
1277
pthread_mutex_unlock (&state.mutex );
1272
1278
1279
+ const char *page = file.buffer ->getBuffer ().data (),
1280
+ *end = page + file.buffer ->getBuffer ().size ();
1281
+ state.bytes += end - page;
1282
+
1273
1283
int t = 0 ; // Reference each page to load it into memory.
1274
- for (const char *page = add.buffer ->getBuffer ().data (),
1275
- *end = page + add.buffer ->getBuffer ().size ();
1276
- page < end; page += state.pageSize )
1284
+ for (; page < end; page += state.pageSize )
1277
1285
t += *page;
1278
- state.total += t; // Avoids whole section being optimised out.
1286
+ state.total += t; // Avoids the loop being optimised out.
1279
1287
}
1280
1288
},
1281
1289
&state);
@@ -1303,12 +1311,8 @@ void createFiles(const InputArgList &args) {
1303
1311
1304
1312
switch (opt.getID ()) {
1305
1313
case OPT_INPUT:
1306
- if (config->readThreads ) {
1307
- StringRef rrpath = rerootPath (arg->getValue ());
1308
- deferredFiles.push_back ({rrpath, readFile (rrpath)});
1309
- break ;
1310
- }
1311
- addFile (rerootPath (arg->getValue ()), LoadType::CommandLine, isLazy);
1314
+ deferFile (rerootPath (arg->getValue ()), LoadType::CommandLine, isLazy,
1315
+ deferredFiles);
1312
1316
break ;
1313
1317
case OPT_needed_library:
1314
1318
if (auto *dylibFile = dyn_cast_or_null<DylibFile>(
@@ -1377,8 +1381,8 @@ void createFiles(const InputArgList &args) {
1377
1381
1378
1382
if (config->readThreads ) {
1379
1383
multiThreadedPageIn (deferredFiles, config->readThreads );
1380
- for (auto &add : deferredFiles)
1381
- deferredAddFile (add .buffer , add .path , LoadType::CommandLine, isLazy);
1384
+ for (auto &file : deferredFiles)
1385
+ processFile (file .buffer , file .path , file. loadType , file. isLazy );
1382
1386
}
1383
1387
}
1384
1388
0 commit comments