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