os-mac.h: implement blockdev_size()
[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>
c64646f0 5#include <sys/disk.h>
2afd826b 6#include <sys/sysctl.h>
9b836561
BC
7#include <sys/time.h>
8#include <unistd.h>
9#include <signal.h>
2afd826b 10
e2e58886
JA
11#include "../file.h"
12
2afd826b
JA
13#ifndef CLOCK_MONOTONIC
14#define CLOCK_MONOTONIC 1
15#endif
16
17#ifndef CLOCK_REALTIME
18#define CLOCK_REALTIME 1
19#endif
20
21#define FIO_HAVE_POSIXAIO
ecc314ba 22#define FIO_HAVE_CLOCK_MONOTONIC
53531370 23#define FIO_USE_GENERIC_RAND
2afd826b
JA
24
25#define OS_MAP_ANON MAP_ANON
26
331539ac 27typedef off_t off64_t;
2afd826b 28
9b836561
BC
29/* OS X as of 10.6 doesn't have the timer_* functions.
30 * Emulate the functionality using setitimer and sigaction here
31 */
32
33#define MAX_TIMERS 64
34
35typedef unsigned int clockid_t;
36typedef unsigned int timer_t;
37
38struct itimerspec {
39 struct timespec it_value;
40 struct timespec it_interval;
41};
42
43static struct sigevent fio_timers[MAX_TIMERS];
44static unsigned int num_timers = 0;
45
46static inline int timer_create(clockid_t clockid, struct sigevent *restrict evp,
47 timer_t *restrict timerid)
48{
49 int current_timer = num_timers;
50 fio_timers[current_timer] = *evp;
51 num_timers++;
52
53 *timerid = current_timer;
54 return 0;
55}
56
57static void sig_alrm(int signum)
58{
59 union sigval sv;
60
61 for (int i = 0; i < num_timers; i++) {
62 if (fio_timers[i].sigev_notify_function == NULL)
63 continue;
64
65 if (fio_timers[i].sigev_notify == SIGEV_THREAD)
66 fio_timers[i].sigev_notify_function(sv);
67 else if (fio_timers[i].sigev_notify == SIGEV_SIGNAL)
68 kill(getpid(), fio_timers[i].sigev_signo);
69 }
70}
71
72static inline int timer_settime(timer_t timerid, int flags,
73 const struct itimerspec *value, struct itimerspec *ovalue)
74{
75 struct sigaction sa;
76 struct itimerval tv;
77 struct itimerval tv_out;
78 int rc;
79
80 tv.it_interval.tv_sec = value->it_interval.tv_sec;
81 tv.it_interval.tv_usec = value->it_interval.tv_nsec / 1000;
82
83 tv.it_value.tv_sec = value->it_value.tv_sec;
84 tv.it_value.tv_usec = value->it_value.tv_nsec / 1000;
85
86 sa.sa_handler = sig_alrm;
87 sigemptyset(&sa.sa_mask);
88 sa.sa_flags = 0;
89
90 rc = sigaction(SIGALRM, &sa, NULL);
91
92 if (!rc)
93 rc = setitimer(ITIMER_REAL, &tv, &tv_out);
94
95 if (!rc && ovalue != NULL) {
96 ovalue->it_interval.tv_sec = tv_out.it_interval.tv_sec;
97 ovalue->it_interval.tv_nsec = tv_out.it_interval.tv_usec * 1000;
98 ovalue->it_value.tv_sec = tv_out.it_value.tv_sec;
99 ovalue->it_value.tv_nsec = tv_out.it_value.tv_usec * 1000;
100 }
101
102 return rc;
103}
104
105static inline int timer_delete(timer_t timer)
106{
107 return 0;
108}
109
c64646f0
SN
110static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
111{
112 uint64_t temp = 1;
113 if (ioctl(f->fd, DKIOCGETBLOCKCOUNT, bytes) == -1)
114 return errno;
115 if (ioctl(f->fd, DKIOCGETBLOCKSIZE, &temp) == -1)
116 return errno;
117 (*bytes) *= temp;
118 return 0;
119}
120
9b836561 121static inline int blockdev_invalidate_cache(struct fio_file *f)
2afd826b
JA
122{
123 return EINVAL;
124}
125
126static inline unsigned long long os_phys_mem(void)
127{
128 int mib[2] = { CTL_HW, HW_PHYSMEM };
129 unsigned long long mem;
130 size_t len = sizeof(mem);
131
132 sysctl(mib, 2, &mem, &len, NULL, 0);
133 return mem;
134}
2afd826b 135#endif