Jens Axboe | 2afd826 | 2009-12-14 23:08:42 +0100 | [diff] [blame] | 1 | #ifndef FIO_OS_APPLE_H |
| 2 | #define FIO_OS_APPLE_H |
| 3 | |
| 4 | #include <errno.h> |
Steven Noonan | 7e8ad19 | 2011-04-22 23:08:13 -0700 | [diff] [blame] | 5 | #include <fcntl.h> |
Steven Noonan | c64646f | 2011-04-22 23:07:46 -0700 | [diff] [blame] | 6 | #include <sys/disk.h> |
Jens Axboe | 2afd826 | 2009-12-14 23:08:42 +0100 | [diff] [blame] | 7 | #include <sys/sysctl.h> |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 8 | #include <sys/time.h> |
| 9 | #include <unistd.h> |
| 10 | #include <signal.h> |
Jens Axboe | 3dee087 | 2011-07-14 08:39:19 +0200 | [diff] [blame] | 11 | #include <mach/mach_init.h> |
Jens Axboe | 2afd826 | 2009-12-14 23:08:42 +0100 | [diff] [blame] | 12 | |
Jens Axboe | e2e5888 | 2011-01-04 08:36:06 +0100 | [diff] [blame] | 13 | #include "../file.h" |
| 14 | |
Jens Axboe | 2afd826 | 2009-12-14 23:08:42 +0100 | [diff] [blame] | 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 |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 24 | #define FIO_HAVE_CLOCK_MONOTONIC |
Jens Axboe | 5353137 | 2009-12-15 10:28:37 +0100 | [diff] [blame] | 25 | #define FIO_USE_GENERIC_RAND |
Jens Axboe | e8d588e | 2011-07-12 22:33:53 +0200 | [diff] [blame] | 26 | #define FIO_HAVE_GETTID |
Jens Axboe | b42ffd1 | 2011-07-14 09:12:50 +0200 | [diff] [blame] | 27 | #define FIO_HAVE_CHARDEV_SIZE |
Jens Axboe | 2afd826 | 2009-12-14 23:08:42 +0100 | [diff] [blame] | 28 | |
| 29 | #define OS_MAP_ANON MAP_ANON |
| 30 | |
Jens Axboe | fca7035 | 2011-07-06 20:12:54 +0200 | [diff] [blame] | 31 | /* |
| 32 | * OSX has a pitifully small shared memory segment by default, |
| 33 | * so default to a lower number of max jobs supported |
| 34 | */ |
| 35 | #define FIO_MAX_JOBS 128 |
| 36 | |
Jens Axboe | 331539a | 2010-03-19 21:31:01 +0100 | [diff] [blame] | 37 | typedef off_t off64_t; |
Jens Axboe | 2afd826 | 2009-12-14 23:08:42 +0100 | [diff] [blame] | 38 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 39 | /* OS X as of 10.6 doesn't have the timer_* functions. |
| 40 | * Emulate the functionality using setitimer and sigaction here |
| 41 | */ |
| 42 | |
| 43 | #define MAX_TIMERS 64 |
| 44 | |
| 45 | typedef unsigned int clockid_t; |
| 46 | typedef unsigned int timer_t; |
| 47 | |
| 48 | struct itimerspec { |
| 49 | struct timespec it_value; |
| 50 | struct timespec it_interval; |
| 51 | }; |
| 52 | |
| 53 | static struct sigevent fio_timers[MAX_TIMERS]; |
| 54 | static unsigned int num_timers = 0; |
| 55 | |
| 56 | static inline int timer_create(clockid_t clockid, struct sigevent *restrict evp, |
| 57 | timer_t *restrict timerid) |
| 58 | { |
| 59 | int current_timer = num_timers; |
| 60 | fio_timers[current_timer] = *evp; |
| 61 | num_timers++; |
| 62 | |
| 63 | *timerid = current_timer; |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static void sig_alrm(int signum) |
| 68 | { |
| 69 | union sigval sv; |
| 70 | |
| 71 | for (int i = 0; i < num_timers; i++) { |
| 72 | if (fio_timers[i].sigev_notify_function == NULL) |
| 73 | continue; |
| 74 | |
| 75 | if (fio_timers[i].sigev_notify == SIGEV_THREAD) |
| 76 | fio_timers[i].sigev_notify_function(sv); |
| 77 | else if (fio_timers[i].sigev_notify == SIGEV_SIGNAL) |
| 78 | kill(getpid(), fio_timers[i].sigev_signo); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | static inline int timer_settime(timer_t timerid, int flags, |
| 83 | const struct itimerspec *value, struct itimerspec *ovalue) |
| 84 | { |
| 85 | struct sigaction sa; |
| 86 | struct itimerval tv; |
| 87 | struct itimerval tv_out; |
| 88 | int rc; |
| 89 | |
| 90 | tv.it_interval.tv_sec = value->it_interval.tv_sec; |
| 91 | tv.it_interval.tv_usec = value->it_interval.tv_nsec / 1000; |
| 92 | |
| 93 | tv.it_value.tv_sec = value->it_value.tv_sec; |
| 94 | tv.it_value.tv_usec = value->it_value.tv_nsec / 1000; |
| 95 | |
| 96 | sa.sa_handler = sig_alrm; |
| 97 | sigemptyset(&sa.sa_mask); |
| 98 | sa.sa_flags = 0; |
| 99 | |
| 100 | rc = sigaction(SIGALRM, &sa, NULL); |
| 101 | |
| 102 | if (!rc) |
| 103 | rc = setitimer(ITIMER_REAL, &tv, &tv_out); |
| 104 | |
| 105 | if (!rc && ovalue != NULL) { |
| 106 | ovalue->it_interval.tv_sec = tv_out.it_interval.tv_sec; |
| 107 | ovalue->it_interval.tv_nsec = tv_out.it_interval.tv_usec * 1000; |
| 108 | ovalue->it_value.tv_sec = tv_out.it_value.tv_sec; |
| 109 | ovalue->it_value.tv_nsec = tv_out.it_value.tv_usec * 1000; |
| 110 | } |
| 111 | |
| 112 | return rc; |
| 113 | } |
| 114 | |
| 115 | static inline int timer_delete(timer_t timer) |
| 116 | { |
| 117 | return 0; |
| 118 | } |
| 119 | |
Steven Noonan | 7e8ad19 | 2011-04-22 23:08:13 -0700 | [diff] [blame] | 120 | #define FIO_OS_DIRECTIO |
| 121 | static inline int fio_set_odirect(int fd) |
| 122 | { |
| 123 | if (fcntl(fd, F_NOCACHE, 1) == -1) |
| 124 | return errno; |
| 125 | return 0; |
| 126 | } |
| 127 | |
Steven Noonan | c64646f | 2011-04-22 23:07:46 -0700 | [diff] [blame] | 128 | static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes) |
| 129 | { |
| 130 | uint64_t temp = 1; |
| 131 | if (ioctl(f->fd, DKIOCGETBLOCKCOUNT, bytes) == -1) |
| 132 | return errno; |
| 133 | if (ioctl(f->fd, DKIOCGETBLOCKSIZE, &temp) == -1) |
| 134 | return errno; |
| 135 | (*bytes) *= temp; |
| 136 | return 0; |
| 137 | } |
| 138 | |
Jens Axboe | b42ffd1 | 2011-07-14 09:12:50 +0200 | [diff] [blame] | 139 | static inline int chardev_size(struct fio_file *f, unsigned long long *bytes) |
| 140 | { |
| 141 | /* |
| 142 | * Could be a raw block device, this is better than just assuming |
| 143 | * we can't get the size at all. |
| 144 | */ |
| 145 | if (!blockdev_size(f, bytes)) |
| 146 | return 0; |
| 147 | |
| 148 | *bytes = -1ULL; |
| 149 | return 0; |
| 150 | } |
| 151 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 152 | static inline int blockdev_invalidate_cache(struct fio_file *f) |
Jens Axboe | 2afd826 | 2009-12-14 23:08:42 +0100 | [diff] [blame] | 153 | { |
| 154 | return EINVAL; |
| 155 | } |
| 156 | |
| 157 | static inline unsigned long long os_phys_mem(void) |
| 158 | { |
| 159 | int mib[2] = { CTL_HW, HW_PHYSMEM }; |
| 160 | unsigned long long mem; |
| 161 | size_t len = sizeof(mem); |
| 162 | |
| 163 | sysctl(mib, 2, &mem, &len, NULL, 0); |
| 164 | return mem; |
| 165 | } |
Jens Axboe | e8d588e | 2011-07-12 22:33:53 +0200 | [diff] [blame] | 166 | |
| 167 | static inline int gettid(void) |
| 168 | { |
| 169 | return mach_thread_self(); |
| 170 | } |
Jens Axboe | 2afd826 | 2009-12-14 23:08:42 +0100 | [diff] [blame] | 171 | #endif |