-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve flushing pagecache on darwin #1883
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -14,6 +14,11 @@ | |||
#include <machine/endian.h> | ||||
#include <libkern/OSByteOrder.h> | ||||
|
||||
#include <sys/stat.h> | ||||
#include <sys/mman.h> | ||||
#include <stdio.h> | ||||
|
||||
|
||||
#include "../arch/arch.h" | ||||
#include "../file.h" | ||||
|
||||
|
@@ -117,3 +122,60 @@ static inline bool os_cpu_has(cpu_features feature) | |||
} | ||||
|
||||
#endif | ||||
|
||||
#if defined (__APPLE__) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You won't need this guard here as Line 49 in 58818df
|
||||
/* | ||||
* discard_pages should not be run within Rosetta. Native arm64 or x86 only. | ||||
*/ | ||||
|
||||
#define MMAP_CHUNK_SIZE (1024 * 1024 * 1024) | ||||
|
||||
static inline int discard_pages(int fd, off_t offset, off_t size) | ||||
{ | ||||
caddr_t *addr; | ||||
uint64_t chunk_size = MMAP_CHUNK_SIZE; | ||||
|
||||
if (fsync(fd) < 0) { | ||||
int __err = errno; | ||||
fprintf(stderr, "Cannot fsync file\n"); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we would be better off using |
||||
errno = __err; | ||||
return -1; | ||||
} | ||||
|
||||
/* | ||||
* mmap the file in 1GB chunks and msync(MS_INVALIDATE). | ||||
*/ | ||||
while (size > 0) { | ||||
uint64_t mmap_size = MIN(chunk_size, size); | ||||
|
||||
addr = mmap((caddr_t)0, mmap_size, PROT_NONE, MAP_SHARED, fd, offset); | ||||
if (addr == MAP_FAILED) { | ||||
int __map_errno = errno; | ||||
fprintf(stderr, "Failed to mmap (%s), offset = %llu, size = %llu\n", | ||||
strerror(errno), offset, mmap_size); | ||||
errno = __map_errno; | ||||
return -1; | ||||
} | ||||
|
||||
if (msync(addr, mmap_size, MS_INVALIDATE)) { | ||||
int __msync_errno = errno; | ||||
fprintf(stderr, "msync failed to free cache pages.\n"); | ||||
errno = __msync_errno; | ||||
return -1; | ||||
} | ||||
|
||||
/* Destroy the above mappings used to invalidate cache - cleaning up */ | ||||
if (munmap(addr, mmap_size) < 0) { | ||||
int __munmap_errno = errno; | ||||
fprintf(stderr, "munmap failed, error = %d.\n", errno); | ||||
errno = __munmap_errno; | ||||
return -1; | ||||
} | ||||
|
||||
size -= mmap_size; | ||||
offset += mmap_size; | ||||
} | ||||
|
||||
return 0; | ||||
} | ||||
#endif /* defined (__APPLE__) */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might be able to move this into
os/os-mac.h
by doing#define CONFIG_POSIX_FADVISE
over there and providing an implementation in that file...