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