Fix compile warning on OSX
[fio.git] / os / os-mac.h
CommitLineData
2afd826b
JA
1#ifndef FIO_OS_APPLE_H
2#define FIO_OS_APPLE_H
3
4#include <errno.h>
7e8ad197 5#include <fcntl.h>
c64646f0 6#include <sys/disk.h>
2afd826b 7#include <sys/sysctl.h>
9b836561
BC
8#include <sys/time.h>
9#include <unistd.h>
10#include <signal.h>
3dee087c 11#include <mach/mach_init.h>
2afd826b 12
e2e58886
JA
13#include "../file.h"
14
2afd826b
JA
15#ifndef CLOCK_MONOTONIC
16#define CLOCK_MONOTONIC 1
17#endif
18
19#ifndef CLOCK_REALTIME
20#define CLOCK_REALTIME 1
21#endif
22
23#define FIO_HAVE_POSIXAIO
ecc314ba 24#define FIO_HAVE_CLOCK_MONOTONIC
53531370 25#define FIO_USE_GENERIC_RAND
e8d588e4 26#define FIO_HAVE_GETTID
2afd826b
JA
27
28#define OS_MAP_ANON MAP_ANON
29
fca70358
JA
30/*
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
331539ac 36typedef off_t off64_t;
2afd826b 37
9b836561
BC
38/* 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
55static inline int timer_create(clockid_t clockid, struct sigevent *restrict evp,
56 timer_t *restrict timerid)
57{
58 int current_timer = num_timers;
59 fio_timers[current_timer] = *evp;
60 num_timers++;
61
62 *timerid = current_timer;
63 return 0;
64}
65
66static void sig_alrm(int signum)
67{
68 union sigval sv;
69
70 for (int i = 0; i < num_timers; i++) {
71 if (fio_timers[i].sigev_notify_function == NULL)
72 continue;
73
74 if (fio_timers[i].sigev_notify == SIGEV_THREAD)
75 fio_timers[i].sigev_notify_function(sv);
76 else if (fio_timers[i].sigev_notify == SIGEV_SIGNAL)
77 kill(getpid(), fio_timers[i].sigev_signo);
78 }
79}
80
81static inline int timer_settime(timer_t timerid, int flags,
82 const struct itimerspec *value, struct itimerspec *ovalue)
83{
84 struct sigaction sa;
85 struct itimerval tv;
86 struct itimerval tv_out;
87 int rc;
88
89 tv.it_interval.tv_sec = value->it_interval.tv_sec;
90 tv.it_interval.tv_usec = value->it_interval.tv_nsec / 1000;
91
92 tv.it_value.tv_sec = value->it_value.tv_sec;
93 tv.it_value.tv_usec = value->it_value.tv_nsec / 1000;
94
95 sa.sa_handler = sig_alrm;
96 sigemptyset(&sa.sa_mask);
97 sa.sa_flags = 0;
98
99 rc = sigaction(SIGALRM, &sa, NULL);
100
101 if (!rc)
102 rc = setitimer(ITIMER_REAL, &tv, &tv_out);
103
104 if (!rc && ovalue != NULL) {
105 ovalue->it_interval.tv_sec = tv_out.it_interval.tv_sec;
106 ovalue->it_interval.tv_nsec = tv_out.it_interval.tv_usec * 1000;
107 ovalue->it_value.tv_sec = tv_out.it_value.tv_sec;
108 ovalue->it_value.tv_nsec = tv_out.it_value.tv_usec * 1000;
109 }
110
111 return rc;
112}
113
114static inline int timer_delete(timer_t timer)
115{
116 return 0;
117}
118
7e8ad197
SN
119#define FIO_OS_DIRECTIO
120static inline int fio_set_odirect(int fd)
121{
122 if (fcntl(fd, F_NOCACHE, 1) == -1)
123 return errno;
124 return 0;
125}
126
c64646f0
SN
127static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
128{
129 uint64_t temp = 1;
130 if (ioctl(f->fd, DKIOCGETBLOCKCOUNT, bytes) == -1)
131 return errno;
132 if (ioctl(f->fd, DKIOCGETBLOCKSIZE, &temp) == -1)
133 return errno;
134 (*bytes) *= temp;
135 return 0;
136}
137
9b836561 138static inline int blockdev_invalidate_cache(struct fio_file *f)
2afd826b
JA
139{
140 return EINVAL;
141}
142
143static inline unsigned long long os_phys_mem(void)
144{
145 int mib[2] = { CTL_HW, HW_PHYSMEM };
146 unsigned long long mem;
147 size_t len = sizeof(mem);
148
149 sysctl(mib, 2, &mem, &len, NULL, 0);
150 return mem;
151}
e8d588e4
JA
152
153static inline int gettid(void)
154{
155 return mach_thread_self();
156}
2afd826b 157#endif