Skip to content

Commit 9bc59a6

Browse files
committed
mac: add readahead control to the posix_fadvise() shim
- Add support for POSIX_FADV_NORMAL in the posix_fadvise() shim by just ignoring it - Add support for POSIX_FADV_SEQUENTIAL/POSIX_FADV_RANDOM by mapping them to enable/disable of readahead via fcntl(..., F_RDAHEAD, ...). Because macOS only lets you control readahead at the descriptor level the offset and len values passed will be ignored and range control is not done. The impact of being able to tune readahead can be seen below: ./fio --filename=fio.tmp --stonewall --size=128M --filename=fio.tmp --bs=4k \ --name=precache --rw=read --bs=128k \ --name=cached --rw=read \ --name=uncached-readahead --rw=read --invalidate=1 \ --name=uncached-no-readahead --rw=read --invalidate=1 --fadvise=random [...] cached: (groupid=1, jobs=1): err= 0: pid=32600: Sat May 17 18:49:22 2025 read: IOPS=349k, BW=1362MiB/s (1428MB/s)(128MiB/94msec) -- uncached-readahead: (groupid=2, jobs=1): err= 0: pid=32601: Sat May 17 18:49:22 2025 read: IOPS=315k, BW=1231MiB/s (1291MB/s)(128MiB/104msec) -- uncached-no-readahead: (groupid=3, jobs=1): err= 0: pid=32602: Sat May 17 18:49:22 2025 read: IOPS=40.8k, BW=159MiB/s (167MB/s)(128MiB/803msec) Signed-off-by: Sitsofe Wheeler <sitsofe@yahoo.com>
1 parent 0c6f657 commit 9bc59a6

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

os/mac/posix.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include <errno.h>
2+
#include <fcntl.h>
3+
#include <stdbool.h>
24
#include <stdint.h>
35
#include <string.h>
46
#include <sys/mman.h>
@@ -58,15 +60,30 @@ static int discard_pages(int fd, off_t offset, off_t len)
5860
return 0;
5961
}
6062

63+
static inline int set_readhead(int fd, bool enabled) {
64+
int ret;
65+
66+
ret = fcntl(fd, F_RDAHEAD, enabled ? 1 : 0);
67+
if (ret == -1) {
68+
ret = errno;
69+
}
70+
71+
return ret;
72+
}
73+
6174
int posix_fadvise(int fd, off_t offset, off_t len, int advice)
6275
{
6376
int ret;
6477

6578
switch(advice) {
6679
case POSIX_FADV_NORMAL:
80+
ret = 0;
81+
break;
6782
case POSIX_FADV_RANDOM:
83+
ret = set_readhead(fd, false);
84+
break;
6885
case POSIX_FADV_SEQUENTIAL:
69-
ret = 0;
86+
ret = set_readhead(fd, true);
7087
break;
7188
case POSIX_FADV_DONTNEED:
7289
ret = discard_pages(fd, offset, len);

0 commit comments

Comments
 (0)