windowsaio: fix file offset bug
[fio.git] / os / os-mac.h
CommitLineData
2afd826b
JA
1#ifndef FIO_OS_APPLE_H
2#define FIO_OS_APPLE_H
3
cca84643
JA
4#define FIO_OS os_mac
5
2afd826b 6#include <errno.h>
7e8ad197 7#include <fcntl.h>
c64646f0 8#include <sys/disk.h>
2afd826b 9#include <sys/sysctl.h>
9b836561
BC
10#include <sys/time.h>
11#include <unistd.h>
12#include <signal.h>
3dee087c 13#include <mach/mach_init.h>
ff245192
JA
14#include <machine/endian.h>
15#include <libkern/OSByteOrder.h>
2afd826b 16
e2e58886
JA
17#include "../file.h"
18
2afd826b
JA
19#ifndef CLOCK_MONOTONIC
20#define CLOCK_MONOTONIC 1
21#endif
22
23#ifndef CLOCK_REALTIME
24#define CLOCK_REALTIME 1
25#endif
26
27#define FIO_HAVE_POSIXAIO
ecc314ba 28#define FIO_HAVE_CLOCK_MONOTONIC
53531370 29#define FIO_USE_GENERIC_RAND
e8d588e4 30#define FIO_HAVE_GETTID
b42ffd19 31#define FIO_HAVE_CHARDEV_SIZE
2afd826b
JA
32
33#define OS_MAP_ANON MAP_ANON
34
ff245192
JA
35#if defined(__LITTLE_ENDIAN__)
36#define FIO_LITTLE_ENDIAN
37#elif defined(__BIG_ENDIAN__)
38#define FIO_BIG_ENDIAN
39#else
40#error "Undefined byte order"
41#endif
42
43#define fio_swap16(x) OSSwapInt16(x)
44#define fio_swap32(x) OSSwapInt32(x)
45#define fio_swap64(x) OSSwapInt64(x)
46
fca70358
JA
47/*
48 * OSX has a pitifully small shared memory segment by default,
49 * so default to a lower number of max jobs supported
50 */
51#define FIO_MAX_JOBS 128
52
331539ac 53typedef off_t off64_t;
2afd826b 54
9b836561
BC
55/* OS X as of 10.6 doesn't have the timer_* functions.
56 * Emulate the functionality using setitimer and sigaction here
57 */
58
59#define MAX_TIMERS 64
60
61typedef unsigned int clockid_t;
62typedef unsigned int timer_t;
63
64struct itimerspec {
65 struct timespec it_value;
66 struct timespec it_interval;
67};
68
69static struct sigevent fio_timers[MAX_TIMERS];
70static unsigned int num_timers = 0;
71
72static inline int timer_create(clockid_t clockid, struct sigevent *restrict evp,
73 timer_t *restrict timerid)
74{
75 int current_timer = num_timers;
76 fio_timers[current_timer] = *evp;
77 num_timers++;
78
79 *timerid = current_timer;
80 return 0;
81}
82
83static void sig_alrm(int signum)
84{
85 union sigval sv;
86
87 for (int i = 0; i < num_timers; i++) {
88 if (fio_timers[i].sigev_notify_function == NULL)
89 continue;
90
91 if (fio_timers[i].sigev_notify == SIGEV_THREAD)
92 fio_timers[i].sigev_notify_function(sv);
93 else if (fio_timers[i].sigev_notify == SIGEV_SIGNAL)
94 kill(getpid(), fio_timers[i].sigev_signo);
95 }
96}
97
98static inline int timer_settime(timer_t timerid, int flags,
99 const struct itimerspec *value, struct itimerspec *ovalue)
100{
101 struct sigaction sa;
102 struct itimerval tv;
103 struct itimerval tv_out;
104 int rc;
105
106 tv.it_interval.tv_sec = value->it_interval.tv_sec;
107 tv.it_interval.tv_usec = value->it_interval.tv_nsec / 1000;
108
109 tv.it_value.tv_sec = value->it_value.tv_sec;
110 tv.it_value.tv_usec = value->it_value.tv_nsec / 1000;
111
112 sa.sa_handler = sig_alrm;
113 sigemptyset(&sa.sa_mask);
114 sa.sa_flags = 0;
115
116 rc = sigaction(SIGALRM, &sa, NULL);
117
118 if (!rc)
119 rc = setitimer(ITIMER_REAL, &tv, &tv_out);
120
121 if (!rc && ovalue != NULL) {
122 ovalue->it_interval.tv_sec = tv_out.it_interval.tv_sec;
123 ovalue->it_interval.tv_nsec = tv_out.it_interval.tv_usec * 1000;
124 ovalue->it_value.tv_sec = tv_out.it_value.tv_sec;
125 ovalue->it_value.tv_nsec = tv_out.it_value.tv_usec * 1000;
126 }
127
128 return rc;
129}
130
131static inline int timer_delete(timer_t timer)
132{
133 return 0;
134}
135
7e8ad197
SN
136#define FIO_OS_DIRECTIO
137static inline int fio_set_odirect(int fd)
138{
139 if (fcntl(fd, F_NOCACHE, 1) == -1)
140 return errno;
141 return 0;
142}
143
c64646f0
SN
144static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
145{
79d73100
JA
146 uint32_t block_size;
147 uint64_t block_count;
148
149 if (ioctl(f->fd, DKIOCGETBLOCKCOUNT, &block_count) == -1)
c64646f0 150 return errno;
79d73100 151 if (ioctl(f->fd, DKIOCGETBLOCKSIZE, &block_size) == -1)
c64646f0 152 return errno;
79d73100
JA
153
154 *bytes = block_size;
155 *bytes *= block_count;
156 return 0;
c64646f0
SN
157}
158
b42ffd19
JA
159static inline int chardev_size(struct fio_file *f, unsigned long long *bytes)
160{
161 /*
162 * Could be a raw block device, this is better than just assuming
163 * we can't get the size at all.
164 */
165 if (!blockdev_size(f, bytes))
166 return 0;
167
168 *bytes = -1ULL;
169 return 0;
170}
171
9b836561 172static inline int blockdev_invalidate_cache(struct fio_file *f)
2afd826b
JA
173{
174 return EINVAL;
175}
176
177static inline unsigned long long os_phys_mem(void)
178{
179 int mib[2] = { CTL_HW, HW_PHYSMEM };
180 unsigned long long mem;
181 size_t len = sizeof(mem);
182
183 sysctl(mib, 2, &mem, &len, NULL, 0);
184 return mem;
185}
e8d588e4
JA
186
187static inline int gettid(void)
188{
189 return mach_thread_self();
190}
2afd826b 191#endif