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 1 commit into
base: master
Choose a base branch
from

Conversation

Developer-Ecosystem-Engineering
Copy link

@Developer-Ecosystem-Engineering Developer-Ecosystem-Engineering commented Mar 12, 2025

The current behavior of flushing the pagecache via posix_fadvise(POSIX_FADV_DONTNEED) is not a valid operation on macOS.

This can result in adverse outcomes when measuring performance. The change will instead flush via mmap and msync, which better reflects the desired behavior on macOS

The current behavior of flushing the pagecache via
posix_fadvise(POSIX_FADV_DONTNEED) is not a valid operation on macOS.
This can result adverse outcomes when measuring performance.
The change will instead flush via mmap and msync, which better reflects
the desired behavior on macOS

Signed-off-by: DeveloperEcosystemEngineering@apple.com
caddr_t *addr;
uint64_t chunk_size = MMAP_CHUNK_SIZE;

printf("fsync'ing file, then invalidating UBC.\n");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover debug statement?

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

#ifdef CONFIG_MAC_FADVISE_DISCARD
/**
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just single * please

#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
Copy link
Owner

@axboe axboe Mar 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#endif /* CONFIG_MAC_FADVISE_DISCARD */

printf("fsync'ing file, then invalidating UBC.\n");
if (fsync(fd) < 0) {
fprintf(stderr, "Cannot fsync file\n");
exit(1);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to return an error here, not exit. It should just return errno, but before you overwrite it by calling fprintf.

if (addr == MAP_FAILED) {
fprintf(stderr, "Failed to mmap (%s), offset = %llu, size = %llu\n",
strerror(errno), offset, mmap_size);
return -1;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should return errno for the failure cases, not -1. For this one and other spots in this function.

}

int posix_fadvise(int fd, off_t offset, off_t len, int advice)
{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably write this function as:

if (advice == POSIX_FADV_DONTNEED)
        return discard_pages(fd, offset, len);
return 0;

and avoid needing 'ret' at all.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well and the return 0 should be return EINVAL or something like that, should not be pretending success for cases it's not handling.

@Developer-Ecosystem-Engineering
Copy link
Author

Thank you for the fast review @axboe, will integrate the requested changes!


if test "$targetos" = "Darwin"; then
output_sym "CONFIG_MAC_FADVISE_DISCARD"
fi
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need another symbol for this? Couldn't you just use if defined(__APPLE__) as is done over in https://github.com/axboe/fio/blob/d2a4910bbefcb3ddecf2868a955ef27ec71286fc/os/os.h#L49C4-L49C25 ?

if (advice == POSIX_FADV_DONTNEED)
ret = discard_pages(fd, offset, len);
return ret;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be abstracted into os-mac.h? Technically os-mac.h could advertise the define CONFIG_POSIX_FADVISE and fake up a posix_fadvise function...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants