blob: 9f19b8ab1879b00a24340cb4ec168388a4618be9 [file] [log] [blame]
Jens Axboe2afd8262009-12-14 23:08:42 +01001#ifndef FIO_OS_APPLE_H
2#define FIO_OS_APPLE_H
3
Jens Axboecca84642011-10-07 12:47:57 +02004#define FIO_OS os_mac
5
Jens Axboe2afd8262009-12-14 23:08:42 +01006#include <errno.h>
Steven Noonan7e8ad192011-04-22 23:08:13 -07007#include <fcntl.h>
Steven Noonanc64646f2011-04-22 23:07:46 -07008#include <sys/disk.h>
Jens Axboe2afd8262009-12-14 23:08:42 +01009#include <sys/sysctl.h>
Bruce Cran9b836562011-01-08 19:49:54 +010010#include <sys/time.h>
11#include <unistd.h>
12#include <signal.h>
Jens Axboe3dee0872011-07-14 08:39:19 +020013#include <mach/mach_init.h>
Jens Axboeff245192011-10-04 14:30:33 +020014#include <machine/endian.h>
15#include <libkern/OSByteOrder.h>
Jens Axboe2afd8262009-12-14 23:08:42 +010016
Jens Axboee2e58882011-01-04 08:36:06 +010017#include "../file.h"
18
Jens Axboe2afd8262009-12-14 23:08:42 +010019#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
Bruce Cranecc314b2011-01-04 10:59:30 +010028#define FIO_HAVE_CLOCK_MONOTONIC
Jens Axboe53531372009-12-15 10:28:37 +010029#define FIO_USE_GENERIC_RAND
Jens Axboee8d588e2011-07-12 22:33:53 +020030#define FIO_HAVE_GETTID
Jens Axboeb42ffd12011-07-14 09:12:50 +020031#define FIO_HAVE_CHARDEV_SIZE
Jens Axboe2afd8262009-12-14 23:08:42 +010032
33#define OS_MAP_ANON MAP_ANON
34
Jens Axboeff245192011-10-04 14:30:33 +020035#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
Jens Axboefca70352011-07-06 20:12:54 +020047/*
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
Jens Axboe331539a2010-03-19 21:31:01 +010053typedef off_t off64_t;
Jens Axboe2afd8262009-12-14 23:08:42 +010054
Bruce Cran9b836562011-01-08 19:49:54 +010055/* 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
Steven Noonan7e8ad192011-04-22 23:08:13 -0700136#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
Steven Noonanc64646f2011-04-22 23:07:46 -0700144static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
145{
Jens Axboe79d73102012-02-01 20:10:12 +0100146 uint32_t block_size;
147 uint64_t block_count;
148
149 if (ioctl(f->fd, DKIOCGETBLOCKCOUNT, &block_count) == -1)
Steven Noonanc64646f2011-04-22 23:07:46 -0700150 return errno;
Jens Axboe79d73102012-02-01 20:10:12 +0100151 if (ioctl(f->fd, DKIOCGETBLOCKSIZE, &block_size) == -1)
Steven Noonanc64646f2011-04-22 23:07:46 -0700152 return errno;
Jens Axboe79d73102012-02-01 20:10:12 +0100153
154 *bytes = block_size;
155 *bytes *= block_count;
156 return 0;
Steven Noonanc64646f2011-04-22 23:07:46 -0700157}
158
Jens Axboeb42ffd12011-07-14 09:12:50 +0200159static 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
Bruce Cran9b836562011-01-08 19:49:54 +0100172static inline int blockdev_invalidate_cache(struct fio_file *f)
Jens Axboe2afd8262009-12-14 23:08:42 +0100173{
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}
Jens Axboee8d588e2011-07-12 22:33:53 +0200186
187static inline int gettid(void)
188{
189 return mach_thread_self();
190}
Jens Axboe2afd8262009-12-14 23:08:42 +0100191#endif