Skip to content

io: use posix_fadvise() instead of readahead() #404

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

Merged
merged 1 commit into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ check_function_exists(mach_absolute_time HAVE_MACH_ABSOLUTE_TIME)
check_function_exists(mach_approximate_time HAVE_MACH_APPROXIMATE_TIME)
check_function_exists(mach_port_construct HAVE_MACH_PORT_CONSTRUCT)
check_function_exists(malloc_create_zone HAVE_MALLOC_CREATE_ZONE)
check_function_exists(posix_fadvise HAVE_POSIX_FADVISE)
check_function_exists(pthread_key_init_np HAVE_PTHREAD_KEY_INIT_NP)
check_function_exists(pthread_main_np HAVE_PTHREAD_MAIN_NP)
check_function_exists(pthread_workqueue_setdispatch_np HAVE_PTHREAD_WORKQUEUE_SETDISPATCH_NP)
Expand Down
3 changes: 3 additions & 0 deletions cmake/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@
/* Define if you have the Objective-C runtime */
#cmakedefine HAVE_OBJC

/* Define to 1 if you have the `posix_fadvise' function. */
#cmakedefine HAVE_POSIX_FADVISE

/* Define to 1 if you have the `pthread_key_init_np' function. */
#cmakedefine HAVE_PTHREAD_KEY_INIT_NP

Expand Down
29 changes: 14 additions & 15 deletions src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@

#include "internal.h"

#if defined(__FreeBSD__)
#include <fcntl.h>
#define F_RDADVISE F_RDAHEAD
#endif

#ifndef DISPATCH_IO_DEBUG
#define DISPATCH_IO_DEBUG DISPATCH_DEBUG
#endif
Expand Down Expand Up @@ -2235,9 +2230,8 @@ _dispatch_operation_advise(dispatch_operation_t op, size_t chunk_size)
(void)chunk_size;
#else
if (_dispatch_io_get_error(op, NULL, true)) return;
#if defined(__linux__) || defined(__FreeBSD__)
// linux does not support fcntl (F_RDAVISE)
// define necessary datastructure and use readahead
#if !defined(F_RDADVISE)
// Compatibility struct whose values may be passed to posix_fadvise()
struct radvisory {
off_t ra_offset;
int ra_count;
Expand All @@ -2262,20 +2256,25 @@ _dispatch_operation_advise(dispatch_operation_t op, size_t chunk_size)
}
advise.ra_offset = op->advise_offset;
op->advise_offset += advise.ra_count;
#if defined(__linux__)
_dispatch_io_syscall_switch(err,
readahead(op->fd_entry->fd, advise.ra_offset, (size_t)advise.ra_count),
case EINVAL: break; // fd does refer to a non-supported filetype
default: (void)dispatch_assume_zero(err); break;
);
#else
#if defined(F_RDADVISE)
_dispatch_io_syscall_switch(err,
fcntl(op->fd_entry->fd, F_RDADVISE, &advise),
case EFBIG: break; // advised past the end of the file rdar://10415691
case ENOTSUP: break; // not all FS support radvise rdar://13484629
// TODO: set disk status on error
default: (void)dispatch_assume_zero(err); break;
);
#elif defined(HAVE_POSIX_FADVISE)
err = posix_fadvise(op->fd_entry->fd, advise.ra_offset,
(off_t)advise.ra_count, POSIX_FADV_WILLNEED);
switch (err) {
case 0: break;
case EINVAL: break; // unsupported advice or file type
case ESPIPE: break; // fd refers to a pipe or FIFO
default: (void)dispatch_assume_zero(err); break;
}
#else
#error "_dispatch_operation_advise not implemented on this platform"
#endif
#endif
}
Expand Down