Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -2997,7 +2997,9 @@ fi
print_config "timerfd_create" "$timerfd_create"

#############################################################################

if test "$targetos" = "Darwin"; then
output_sym "CONFIG_MAC_FADVISE_DISCARD"
fi
if test "$wordsize" = "64" ; then
output_sym "CONFIG_64BIT"
elif test "$wordsize" = "32" ; then
Expand Down
62 changes: 62 additions & 0 deletions helpers.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include <errno.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdio.h>

#include "helpers.h"

Expand Down Expand Up @@ -26,9 +29,68 @@ int sync_file_range(int fd, uint64_t offset, uint64_t nbytes,
}
#endif

#ifdef CONFIG_MAC_FADVISE_DISCARD
/**
* CONFIG_MAC_FADVISE_DISCARD should not be run within Rosetta. Native arm64 or x86 only.
*/

#define MMAP_CHUNK_SIZE (1024 * 1024 * 1024)

int discard_pages(int fd, off_t offset, off_t size)
{
caddr_t *addr;
uint64_t chunk_size = MMAP_CHUNK_SIZE;

printf("fsync'ing file, then invalidating UBC.\n");
if (fsync(fd) < 0) {
fprintf(stderr, "Cannot fsync file\n");
exit(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) {
fprintf(stderr, "Failed to mmap (%s), offset = %llu, size = %llu\n",
strerror(errno), offset, mmap_size);
return -1;
}

if (msync(addr, mmap_size, MS_INVALIDATE)) {
fprintf(stderr, "msync failed to free cache pages.\n");
return -1;
}

/* Destroy the above mappings used to invalidate cache - cleaning up */
if (munmap(addr, mmap_size) < 0) {
fprintf(stderr, "munmap failed, error = %d.\n", errno);
return -1;
}

size -= mmap_size;
offset += mmap_size;
}

return 0;
}

int posix_fadvise(int fd, off_t offset, off_t len, int advice)
{
int ret = 0;

if (advice == POSIX_FADV_DONTNEED)
ret = discard_pages(fd, offset, len);
return ret;
}
#else
#ifndef CONFIG_POSIX_FADVISE
int posix_fadvise(int fd, off_t offset, off_t len, int advice)
{
return 0;
}
#endif
#endif //CONFIG_MAC_FADVISE_DISCARD