blob: d202e99dd60a93bc3c11dd0088456bbc1a731adf [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 Axboe53531372009-12-15 10:28:37 +010019#define FIO_USE_GENERIC_RAND
Bruce Cran93bcfd22012-02-20 20:18:19 +010020#define FIO_USE_GENERIC_INIT_RANDOM_STATE
Jens Axboee8d588e2011-07-12 22:33:53 +020021#define FIO_HAVE_GETTID
Jens Axboeb42ffd12011-07-14 09:12:50 +020022#define FIO_HAVE_CHARDEV_SIZE
Jens Axboe2afd8262009-12-14 23:08:42 +010023
24#define OS_MAP_ANON MAP_ANON
25
Jens Axboeff245192011-10-04 14:30:33 +020026#define fio_swap16(x) OSSwapInt16(x)
27#define fio_swap32(x) OSSwapInt32(x)
28#define fio_swap64(x) OSSwapInt64(x)
29
Jens Axboefca70352011-07-06 20:12:54 +020030/*
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
Jens Axboe331539a2010-03-19 21:31:01 +010036typedef off_t off64_t;
Jens Axboe2afd8262009-12-14 23:08:42 +010037
Bruce Cran9b836562011-01-08 19:49:54 +010038/* OS X as of 10.6 doesn't have the timer_* functions.
39 * Emulate the functionality using setitimer and sigaction here
40 */
41
42#define MAX_TIMERS 64
43
44typedef unsigned int clockid_t;
45typedef unsigned int timer_t;
46
47struct itimerspec {
48 struct timespec it_value;
49 struct timespec it_interval;
50};
51
52static struct sigevent fio_timers[MAX_TIMERS];
53static unsigned int num_timers = 0;
54
Bruce Cran9b836562011-01-08 19:49:54 +010055static void sig_alrm(int signum)
56{
57 union sigval sv;
58
59 for (int i = 0; i < num_timers; i++) {
60 if (fio_timers[i].sigev_notify_function == NULL)
61 continue;
62
63 if (fio_timers[i].sigev_notify == SIGEV_THREAD)
64 fio_timers[i].sigev_notify_function(sv);
65 else if (fio_timers[i].sigev_notify == SIGEV_SIGNAL)
66 kill(getpid(), fio_timers[i].sigev_signo);
67 }
68}
69
70static inline int timer_settime(timer_t timerid, int flags,
Jens Axboe0c4ce7c2012-02-03 14:45:38 +010071 const struct itimerspec *value,
72 struct itimerspec *ovalue)
Bruce Cran9b836562011-01-08 19:49:54 +010073{
74 struct sigaction sa;
75 struct itimerval tv;
76 struct itimerval tv_out;
77 int rc;
78
79 tv.it_interval.tv_sec = value->it_interval.tv_sec;
80 tv.it_interval.tv_usec = value->it_interval.tv_nsec / 1000;
81
82 tv.it_value.tv_sec = value->it_value.tv_sec;
83 tv.it_value.tv_usec = value->it_value.tv_nsec / 1000;
84
85 sa.sa_handler = sig_alrm;
86 sigemptyset(&sa.sa_mask);
87 sa.sa_flags = 0;
88
89 rc = sigaction(SIGALRM, &sa, NULL);
90
91 if (!rc)
92 rc = setitimer(ITIMER_REAL, &tv, &tv_out);
93
94 if (!rc && ovalue != NULL) {
95 ovalue->it_interval.tv_sec = tv_out.it_interval.tv_sec;
96 ovalue->it_interval.tv_nsec = tv_out.it_interval.tv_usec * 1000;
97 ovalue->it_value.tv_sec = tv_out.it_value.tv_sec;
98 ovalue->it_value.tv_nsec = tv_out.it_value.tv_usec * 1000;
99 }
100
101 return rc;
102}
103
104static inline int timer_delete(timer_t timer)
105{
106 return 0;
107}
108
Steven Noonan7e8ad192011-04-22 23:08:13 -0700109#define FIO_OS_DIRECTIO
110static inline int fio_set_odirect(int fd)
111{
112 if (fcntl(fd, F_NOCACHE, 1) == -1)
113 return errno;
114 return 0;
115}
116
Steven Noonanc64646f2011-04-22 23:07:46 -0700117static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
118{
Jens Axboe79d73102012-02-01 20:10:12 +0100119 uint32_t block_size;
120 uint64_t block_count;
121
122 if (ioctl(f->fd, DKIOCGETBLOCKCOUNT, &block_count) == -1)
Steven Noonanc64646f2011-04-22 23:07:46 -0700123 return errno;
Jens Axboe79d73102012-02-01 20:10:12 +0100124 if (ioctl(f->fd, DKIOCGETBLOCKSIZE, &block_size) == -1)
Steven Noonanc64646f2011-04-22 23:07:46 -0700125 return errno;
Jens Axboe79d73102012-02-01 20:10:12 +0100126
127 *bytes = block_size;
128 *bytes *= block_count;
129 return 0;
Steven Noonanc64646f2011-04-22 23:07:46 -0700130}
131
Jens Axboeb42ffd12011-07-14 09:12:50 +0200132static inline int chardev_size(struct fio_file *f, unsigned long long *bytes)
133{
134 /*
135 * Could be a raw block device, this is better than just assuming
136 * we can't get the size at all.
137 */
138 if (!blockdev_size(f, bytes))
139 return 0;
140
141 *bytes = -1ULL;
142 return 0;
143}
144
Bruce Cran9b836562011-01-08 19:49:54 +0100145static inline int blockdev_invalidate_cache(struct fio_file *f)
Jens Axboe2afd8262009-12-14 23:08:42 +0100146{
147 return EINVAL;
148}
149
150static inline unsigned long long os_phys_mem(void)
151{
152 int mib[2] = { CTL_HW, HW_PHYSMEM };
153 unsigned long long mem;
154 size_t len = sizeof(mem);
155
156 sysctl(mib, 2, &mem, &len, NULL, 0);
157 return mem;
158}
Jens Axboee8d588e2011-07-12 22:33:53 +0200159
160static inline int gettid(void)
161{
162 return mach_thread_self();
163}
Jens Axboe5351f562013-01-23 14:02:23 -0700164
165/*
166 * For some reason, there's no header definition for fdatasync(), even
167 * if it exists.
168 */
169extern int fdatasync(int fd);
170
Jens Axboe2afd8262009-12-14 23:08:42 +0100171#endif