fio: fix aio trim completion latencies
[fio.git] / os / os-mac.h
1 #ifndef FIO_OS_APPLE_H
2 #define FIO_OS_APPLE_H
3
4 #define FIO_OS  os_mac
5
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <sys/disk.h>
9 #include <sys/sysctl.h>
10 #include <sys/time.h>
11 #include <unistd.h>
12 #include <signal.h>
13 #include <mach/mach_init.h>
14 #include <machine/endian.h>
15 #include <libkern/OSByteOrder.h>
16
17 #include "../file.h"
18
19 #define FIO_USE_GENERIC_INIT_RANDOM_STATE
20 #define FIO_HAVE_GETTID
21 #define FIO_HAVE_CHARDEV_SIZE
22 #define FIO_HAVE_NATIVE_FALLOCATE
23
24 #define OS_MAP_ANON             MAP_ANON
25
26 #define fio_swap16(x)   OSSwapInt16(x)
27 #define fio_swap32(x)   OSSwapInt32(x)
28 #define fio_swap64(x)   OSSwapInt64(x)
29
30 /*
31  * OSX has a pitifully small shared memory segment by default,
32  * so default to a lower number of max jobs supported
33  */
34 #define FIO_MAX_JOBS            128
35
36 typedef off_t off64_t;
37
38 #ifndef CONFIG_CLOCKID_T
39 typedef unsigned int clockid_t;
40 #endif
41
42 #define FIO_OS_DIRECTIO
43 static inline int fio_set_odirect(struct fio_file *f)
44 {
45         if (fcntl(f->fd, F_NOCACHE, 1) == -1)
46                 return errno;
47         return 0;
48 }
49
50 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
51 {
52         uint32_t block_size;
53         uint64_t block_count;
54
55         if (ioctl(f->fd, DKIOCGETBLOCKCOUNT, &block_count) == -1)
56                 return errno;
57         if (ioctl(f->fd, DKIOCGETBLOCKSIZE, &block_size) == -1)
58                 return errno;
59
60         *bytes = block_size;
61         *bytes *= block_count;
62         return 0;
63 }
64
65 static inline int chardev_size(struct fio_file *f, unsigned long long *bytes)
66 {
67         /*
68          * Could be a raw block device, this is better than just assuming
69          * we can't get the size at all.
70          */
71         if (!blockdev_size(f, bytes))
72                 return 0;
73
74         *bytes = -1ULL;
75         return 0;
76 }
77
78 static inline int blockdev_invalidate_cache(struct fio_file *f)
79 {
80         return ENOTSUP;
81 }
82
83 static inline unsigned long long os_phys_mem(void)
84 {
85         int mib[2] = { CTL_HW, HW_PHYSMEM };
86         unsigned long long mem;
87         size_t len = sizeof(mem);
88
89         sysctl(mib, 2, &mem, &len, NULL, 0);
90         return mem;
91 }
92
93 #ifndef CONFIG_HAVE_GETTID
94 static inline int gettid(void)
95 {
96         return mach_thread_self();
97 }
98 #endif
99
100 /*
101  * For some reason, there's no header definition for fdatasync(), even
102  * if it exists.
103  */
104 extern int fdatasync(int fd);
105
106 static inline bool fio_fallocate(struct fio_file *f, uint64_t offset, uint64_t len)
107 {
108         fstore_t store = {F_ALLOCATEALL, F_PEOFPOSMODE, offset, len};
109         if (fcntl(f->fd, F_PREALLOCATE, &store) != -1) {
110                 if (ftruncate(f->fd, len) == 0)
111                         return true;
112         }
113
114         return false;
115 }
116
117 #endif