Skip to content

Commit 3e4ea66

Browse files
Merge pull request swiftlang#404 from adierking/readahead
io: use posix_fadvise() instead of readahead()
2 parents a736ea7 + 4295182 commit 3e4ea66

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ check_function_exists(mach_absolute_time HAVE_MACH_ABSOLUTE_TIME)
189189
check_function_exists(mach_approximate_time HAVE_MACH_APPROXIMATE_TIME)
190190
check_function_exists(mach_port_construct HAVE_MACH_PORT_CONSTRUCT)
191191
check_function_exists(malloc_create_zone HAVE_MALLOC_CREATE_ZONE)
192+
check_function_exists(posix_fadvise HAVE_POSIX_FADVISE)
192193
check_function_exists(pthread_key_init_np HAVE_PTHREAD_KEY_INIT_NP)
193194
check_function_exists(pthread_main_np HAVE_PTHREAD_MAIN_NP)
194195
check_function_exists(pthread_workqueue_setdispatch_np HAVE_PTHREAD_WORKQUEUE_SETDISPATCH_NP)

cmake/config.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@
132132
/* Define if you have the Objective-C runtime */
133133
#cmakedefine HAVE_OBJC
134134

135+
/* Define to 1 if you have the `posix_fadvise' function. */
136+
#cmakedefine HAVE_POSIX_FADVISE
137+
135138
/* Define to 1 if you have the `pthread_key_init_np' function. */
136139
#cmakedefine HAVE_PTHREAD_KEY_INIT_NP
137140

src/io.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@
2020

2121
#include "internal.h"
2222

23-
#if defined(__FreeBSD__)
24-
#include <fcntl.h>
25-
#define F_RDADVISE F_RDAHEAD
26-
#endif
27-
2823
#ifndef DISPATCH_IO_DEBUG
2924
#define DISPATCH_IO_DEBUG DISPATCH_DEBUG
3025
#endif
@@ -2235,9 +2230,8 @@ _dispatch_operation_advise(dispatch_operation_t op, size_t chunk_size)
22352230
(void)chunk_size;
22362231
#else
22372232
if (_dispatch_io_get_error(op, NULL, true)) return;
2238-
#if defined(__linux__) || defined(__FreeBSD__)
2239-
// linux does not support fcntl (F_RDAVISE)
2240-
// define necessary datastructure and use readahead
2233+
#if !defined(F_RDADVISE)
2234+
// Compatibility struct whose values may be passed to posix_fadvise()
22412235
struct radvisory {
22422236
off_t ra_offset;
22432237
int ra_count;
@@ -2262,20 +2256,25 @@ _dispatch_operation_advise(dispatch_operation_t op, size_t chunk_size)
22622256
}
22632257
advise.ra_offset = op->advise_offset;
22642258
op->advise_offset += advise.ra_count;
2265-
#if defined(__linux__)
2266-
_dispatch_io_syscall_switch(err,
2267-
readahead(op->fd_entry->fd, advise.ra_offset, (size_t)advise.ra_count),
2268-
case EINVAL: break; // fd does refer to a non-supported filetype
2269-
default: (void)dispatch_assume_zero(err); break;
2270-
);
2271-
#else
2259+
#if defined(F_RDADVISE)
22722260
_dispatch_io_syscall_switch(err,
22732261
fcntl(op->fd_entry->fd, F_RDADVISE, &advise),
22742262
case EFBIG: break; // advised past the end of the file rdar://10415691
22752263
case ENOTSUP: break; // not all FS support radvise rdar://13484629
22762264
// TODO: set disk status on error
22772265
default: (void)dispatch_assume_zero(err); break;
22782266
);
2267+
#elif defined(HAVE_POSIX_FADVISE)
2268+
err = posix_fadvise(op->fd_entry->fd, advise.ra_offset,
2269+
(off_t)advise.ra_count, POSIX_FADV_WILLNEED);
2270+
switch (err) {
2271+
case 0: break;
2272+
case EINVAL: break; // unsupported advice or file type
2273+
case ESPIPE: break; // fd refers to a pipe or FIFO
2274+
default: (void)dispatch_assume_zero(err); break;
2275+
}
2276+
#else
2277+
#error "_dispatch_operation_advise not implemented on this platform"
22792278
#endif
22802279
#endif
22812280
}

0 commit comments

Comments
 (0)