9 #include <sys/sysctl.h>
13 #include <mach/mach_init.h>
14 #include <machine/endian.h>
15 #include <libkern/OSByteOrder.h>
19 #ifndef CLOCK_MONOTONIC
20 #define CLOCK_MONOTONIC 1
23 #ifndef CLOCK_REALTIME
24 #define CLOCK_REALTIME 1
27 #define FIO_HAVE_POSIXAIO
28 #define FIO_HAVE_CLOCK_MONOTONIC
29 #define FIO_USE_GENERIC_RAND
30 #define FIO_HAVE_GETTID
31 #define FIO_HAVE_CHARDEV_SIZE
33 #define OS_MAP_ANON MAP_ANON
35 #if defined(__LITTLE_ENDIAN__)
36 #define FIO_LITTLE_ENDIAN
37 #elif defined(__BIG_ENDIAN__)
38 #define FIO_BIG_ENDIAN
40 #error "Undefined byte order"
43 #define fio_swap16(x) OSSwapInt16(x)
44 #define fio_swap32(x) OSSwapInt32(x)
45 #define fio_swap64(x) OSSwapInt64(x)
48 * OSX has a pitifully small shared memory segment by default,
49 * so default to a lower number of max jobs supported
51 #define FIO_MAX_JOBS 128
53 typedef off_t off64_t;
55 /* OS X as of 10.6 doesn't have the timer_* functions.
56 * Emulate the functionality using setitimer and sigaction here
61 typedef unsigned int clockid_t;
62 typedef unsigned int timer_t;
65 struct timespec it_value;
66 struct timespec it_interval;
69 static struct sigevent fio_timers[MAX_TIMERS];
70 static unsigned int num_timers = 0;
72 static inline int timer_create(clockid_t clockid, struct sigevent *restrict evp,
73 timer_t *restrict timerid)
75 int current_timer = num_timers;
76 fio_timers[current_timer] = *evp;
79 *timerid = current_timer;
83 static void sig_alrm(int signum)
87 for (int i = 0; i < num_timers; i++) {
88 if (fio_timers[i].sigev_notify_function == NULL)
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);
98 static inline int timer_settime(timer_t timerid, int flags,
99 const struct itimerspec *value, struct itimerspec *ovalue)
103 struct itimerval tv_out;
106 tv.it_interval.tv_sec = value->it_interval.tv_sec;
107 tv.it_interval.tv_usec = value->it_interval.tv_nsec / 1000;
109 tv.it_value.tv_sec = value->it_value.tv_sec;
110 tv.it_value.tv_usec = value->it_value.tv_nsec / 1000;
112 sa.sa_handler = sig_alrm;
113 sigemptyset(&sa.sa_mask);
116 rc = sigaction(SIGALRM, &sa, NULL);
119 rc = setitimer(ITIMER_REAL, &tv, &tv_out);
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;
131 static inline int timer_delete(timer_t timer)
136 #define FIO_OS_DIRECTIO
137 static inline int fio_set_odirect(int fd)
139 if (fcntl(fd, F_NOCACHE, 1) == -1)
144 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
147 uint64_t block_count;
149 if (ioctl(f->fd, DKIOCGETBLOCKCOUNT, &block_count) == -1)
151 if (ioctl(f->fd, DKIOCGETBLOCKSIZE, &block_size) == -1)
155 *bytes *= block_count;
159 static inline int chardev_size(struct fio_file *f, unsigned long long *bytes)
162 * Could be a raw block device, this is better than just assuming
163 * we can't get the size at all.
165 if (!blockdev_size(f, bytes))
172 static inline int blockdev_invalidate_cache(struct fio_file *f)
177 static inline unsigned long long os_phys_mem(void)
179 int mib[2] = { CTL_HW, HW_PHYSMEM };
180 unsigned long long mem;
181 size_t len = sizeof(mem);
183 sysctl(mib, 2, &mem, &len, NULL, 0);
187 static inline int gettid(void)
189 return mach_thread_self();