blob: 0d3cae3cb5e43e1a7d5e5a97fb6b604d2d60ac50 [file] [log] [blame]
Jens Axboe2afd8262009-12-14 23:08:42 +01001#ifndef FIO_OS_APPLE_H
2#define FIO_OS_APPLE_H
3
4#include <errno.h>
Steven Noonan7e8ad192011-04-22 23:08:13 -07005#include <fcntl.h>
Steven Noonanc64646f2011-04-22 23:07:46 -07006#include <sys/disk.h>
Jens Axboe2afd8262009-12-14 23:08:42 +01007#include <sys/sysctl.h>
Bruce Cran9b836562011-01-08 19:49:54 +01008#include <sys/time.h>
9#include <unistd.h>
10#include <signal.h>
Jens Axboe2afd8262009-12-14 23:08:42 +010011
Jens Axboee2e58882011-01-04 08:36:06 +010012#include "../file.h"
13
Jens Axboe2afd8262009-12-14 23:08:42 +010014#ifndef CLOCK_MONOTONIC
15#define CLOCK_MONOTONIC 1
16#endif
17
18#ifndef CLOCK_REALTIME
19#define CLOCK_REALTIME 1
20#endif
21
22#define FIO_HAVE_POSIXAIO
Bruce Cranecc314b2011-01-04 10:59:30 +010023#define FIO_HAVE_CLOCK_MONOTONIC
Jens Axboe53531372009-12-15 10:28:37 +010024#define FIO_USE_GENERIC_RAND
Jens Axboee8d588e2011-07-12 22:33:53 +020025#define FIO_HAVE_GETTID
Jens Axboe2afd8262009-12-14 23:08:42 +010026
27#define OS_MAP_ANON MAP_ANON
28
Jens Axboefca70352011-07-06 20:12:54 +020029/*
30 * OSX has a pitifully small shared memory segment by default,
31 * so default to a lower number of max jobs supported
32 */
33#define FIO_MAX_JOBS 128
34
Jens Axboe331539a2010-03-19 21:31:01 +010035typedef off_t off64_t;
Jens Axboe2afd8262009-12-14 23:08:42 +010036
Bruce Cran9b836562011-01-08 19:49:54 +010037/* OS X as of 10.6 doesn't have the timer_* functions.
38 * Emulate the functionality using setitimer and sigaction here
39 */
40
41#define MAX_TIMERS 64
42
43typedef unsigned int clockid_t;
44typedef unsigned int timer_t;
45
46struct itimerspec {
47 struct timespec it_value;
48 struct timespec it_interval;
49};
50
51static struct sigevent fio_timers[MAX_TIMERS];
52static unsigned int num_timers = 0;
53
54static inline int timer_create(clockid_t clockid, struct sigevent *restrict evp,
55 timer_t *restrict timerid)
56{
57 int current_timer = num_timers;
58 fio_timers[current_timer] = *evp;
59 num_timers++;
60
61 *timerid = current_timer;
62 return 0;
63}
64
65static void sig_alrm(int signum)
66{
67 union sigval sv;
68
69 for (int i = 0; i < num_timers; i++) {
70 if (fio_timers[i].sigev_notify_function == NULL)
71 continue;
72
73 if (fio_timers[i].sigev_notify == SIGEV_THREAD)
74 fio_timers[i].sigev_notify_function(sv);
75 else if (fio_timers[i].sigev_notify == SIGEV_SIGNAL)
76 kill(getpid(), fio_timers[i].sigev_signo);
77 }
78}
79
80static inline int timer_settime(timer_t timerid, int flags,
81 const struct itimerspec *value, struct itimerspec *ovalue)
82{
83 struct sigaction sa;
84 struct itimerval tv;
85 struct itimerval tv_out;
86 int rc;
87
88 tv.it_interval.tv_sec = value->it_interval.tv_sec;
89 tv.it_interval.tv_usec = value->it_interval.tv_nsec / 1000;
90
91 tv.it_value.tv_sec = value->it_value.tv_sec;
92 tv.it_value.tv_usec = value->it_value.tv_nsec / 1000;
93
94 sa.sa_handler = sig_alrm;
95 sigemptyset(&sa.sa_mask);
96 sa.sa_flags = 0;
97
98 rc = sigaction(SIGALRM, &sa, NULL);
99
100 if (!rc)
101 rc = setitimer(ITIMER_REAL, &tv, &tv_out);
102
103 if (!rc && ovalue != NULL) {
104 ovalue->it_interval.tv_sec = tv_out.it_interval.tv_sec;
105 ovalue->it_interval.tv_nsec = tv_out.it_interval.tv_usec * 1000;
106 ovalue->it_value.tv_sec = tv_out.it_value.tv_sec;
107 ovalue->it_value.tv_nsec = tv_out.it_value.tv_usec * 1000;
108 }
109
110 return rc;
111}
112
113static inline int timer_delete(timer_t timer)
114{
115 return 0;
116}
117
Steven Noonan7e8ad192011-04-22 23:08:13 -0700118#define FIO_OS_DIRECTIO
119static inline int fio_set_odirect(int fd)
120{
121 if (fcntl(fd, F_NOCACHE, 1) == -1)
122 return errno;
123 return 0;
124}
125
Steven Noonanc64646f2011-04-22 23:07:46 -0700126static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
127{
128 uint64_t temp = 1;
129 if (ioctl(f->fd, DKIOCGETBLOCKCOUNT, bytes) == -1)
130 return errno;
131 if (ioctl(f->fd, DKIOCGETBLOCKSIZE, &temp) == -1)
132 return errno;
133 (*bytes) *= temp;
134 return 0;
135}
136
Bruce Cran9b836562011-01-08 19:49:54 +0100137static inline int blockdev_invalidate_cache(struct fio_file *f)
Jens Axboe2afd8262009-12-14 23:08:42 +0100138{
139 return EINVAL;
140}
141
142static inline unsigned long long os_phys_mem(void)
143{
144 int mib[2] = { CTL_HW, HW_PHYSMEM };
145 unsigned long long mem;
146 size_t len = sizeof(mem);
147
148 sysctl(mib, 2, &mem, &len, NULL, 0);
149 return mem;
150}
Jens Axboee8d588e2011-07-12 22:33:53 +0200151
152static inline int gettid(void)
153{
154 return mach_thread_self();
155}
Jens Axboe2afd8262009-12-14 23:08:42 +0100156#endif