Fio 2.11
[fio.git] / os / os-mac.h
1 #ifndef FIO_OS_APPLE_H
2 #define FIO_OS_APPLE_H
3
4 #define FIO_OS  os_mac
5
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <sys/disk.h>
9 #include <sys/sysctl.h>
10 #include <sys/time.h>
11 #include <unistd.h>
12 #include <signal.h>
13 #include <mach/mach_init.h>
14 #include <machine/endian.h>
15 #include <libkern/OSByteOrder.h>
16
17 #include "../file.h"
18
19 #define FIO_USE_GENERIC_RAND
20 #define FIO_USE_GENERIC_INIT_RANDOM_STATE
21 #define FIO_HAVE_GETTID
22 #define FIO_HAVE_CHARDEV_SIZE
23
24 #define OS_MAP_ANON             MAP_ANON
25
26 #define fio_swap16(x)   OSSwapInt16(x)
27 #define fio_swap32(x)   OSSwapInt32(x)
28 #define fio_swap64(x)   OSSwapInt64(x)
29
30 /*
31  * OSX has a pitifully small shared memory segment by default,
32  * so default to a lower number of max jobs supported
33  */
34 #define FIO_MAX_JOBS            128
35
36 typedef off_t off64_t;
37
38 typedef unsigned int clockid_t;
39
40 #define FIO_OS_DIRECTIO
41 static inline int fio_set_odirect(int fd)
42 {
43         if (fcntl(fd, F_NOCACHE, 1) == -1)
44                 return errno;
45         return 0;
46 }
47
48 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
49 {
50         uint32_t block_size;
51         uint64_t block_count;
52
53         if (ioctl(f->fd, DKIOCGETBLOCKCOUNT, &block_count) == -1)
54                 return errno;
55         if (ioctl(f->fd, DKIOCGETBLOCKSIZE, &block_size) == -1)
56                 return errno;
57
58         *bytes = block_size;
59         *bytes *= block_count;
60         return 0;
61 }
62
63 static inline int chardev_size(struct fio_file *f, unsigned long long *bytes)
64 {
65         /*
66          * Could be a raw block device, this is better than just assuming
67          * we can't get the size at all.
68          */
69         if (!blockdev_size(f, bytes))
70                 return 0;
71
72         *bytes = -1ULL;
73         return 0;
74 }
75
76 static inline int blockdev_invalidate_cache(struct fio_file *f)
77 {
78         return EINVAL;
79 }
80
81 static inline unsigned long long os_phys_mem(void)
82 {
83         int mib[2] = { CTL_HW, HW_PHYSMEM };
84         unsigned long long mem;
85         size_t len = sizeof(mem);
86
87         sysctl(mib, 2, &mem, &len, NULL, 0);
88         return mem;
89 }
90
91 static inline int gettid(void)
92 {
93         return mach_thread_self();
94 }
95
96 /*
97  * For some reason, there's no header definition for fdatasync(), even
98  * if it exists.
99  */
100 extern int fdatasync(int fd);
101
102 #endif