Various fixes/updates
[fio.git] / os / os-mac.h
1 #ifndef FIO_OS_APPLE_H
2 #define FIO_OS_APPLE_H
3
4 #include <errno.h>
5 #include <sys/sysctl.h>
6 #include <sys/time.h>
7 #include <unistd.h>
8 #include <signal.h>
9
10 #include "../file.h"
11
12 #ifndef CLOCK_MONOTONIC
13 #define CLOCK_MONOTONIC 1
14 #endif
15
16 #ifndef CLOCK_REALTIME
17 #define CLOCK_REALTIME 1
18 #endif
19
20 #define FIO_HAVE_POSIXAIO
21 #define FIO_HAVE_CLOCK_MONOTONIC
22 #define FIO_USE_GENERIC_BDEV_SIZE
23 #define FIO_USE_GENERIC_RAND
24
25 #define OS_MAP_ANON             MAP_ANON
26
27 typedef off_t off64_t;
28
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
35 typedef unsigned int clockid_t;
36 typedef unsigned int timer_t;
37
38 struct itimerspec {
39         struct timespec it_value;
40         struct timespec it_interval;
41 };
42
43 static struct sigevent fio_timers[MAX_TIMERS];
44 static unsigned int num_timers = 0;
45
46 static 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
57 static 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
72 static 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
105 static inline int timer_delete(timer_t timer)
106 {
107         return 0;
108 }
109
110 static inline int blockdev_invalidate_cache(struct fio_file *f)
111 {
112         return EINVAL;
113 }
114
115 static inline unsigned long long os_phys_mem(void)
116 {
117         int mib[2] = { CTL_HW, HW_PHYSMEM };
118         unsigned long long mem;
119         size_t len = sizeof(mem);
120
121         sysctl(mib, 2, &mem, &len, NULL, 0);
122         return mem;
123 }
124 #endif