Advise the kernel to preload the mapped memory
This commit is contained in:
parent
437e77855a
commit
888db62c80
1 changed files with 11 additions and 0 deletions
11
llama.cpp
11
llama.cpp
|
@ -327,13 +327,24 @@ static void *mmap_file(const char *fname, uint64_t *mm_length) {
|
|||
void *addr = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
|
||||
CloseHandle(hMapping);
|
||||
if (!addr) return 0;
|
||||
// Advise the kernel to preload the mapped memory
|
||||
WIN32_MEMORY_RANGE_ENTRY range;
|
||||
range.VirtualAddress = addr;
|
||||
range.NumberOfBytes = (SIZE_T)length;
|
||||
PrefetchVirtualMemory(GetCurrentProcess(), 1, &range, 0);
|
||||
#else
|
||||
int fd = open(fname, O_RDONLY);
|
||||
if (fd == -1) return 0;
|
||||
int64_t length = lseek(fd, 0, SEEK_END);
|
||||
#ifdef __linux__
|
||||
void *addr = mmap(NULL, length, PROT_READ, MAP_SHARED | MAP_POPULATE, fd, 0);
|
||||
#else // MAP_POPULATE is only supported on Linux
|
||||
void *addr = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, 0);
|
||||
#endif
|
||||
close(fd);
|
||||
if (addr == MAP_FAILED) return 0;
|
||||
// Advise the kernel to preload the mapped memory
|
||||
madvise(addr, length, MADV_WILLNEED);
|
||||
#endif
|
||||
*mm_length = length;
|
||||
return addr;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue