Conditionally enable FIO_HAVE_PSHARED_MUTEX on FreeBSD
[fio.git] / os / os-freebsd.h
1 #ifndef FIO_OS_FREEBSD_H
2 #define FIO_OS_FREEBSD_H
3
4 #define FIO_OS  os_freebsd
5
6 #include <errno.h>
7 #include <sys/sysctl.h>
8 #include <sys/disk.h>
9 #include <sys/thr.h>
10 #include <sys/socket.h>
11 #include <sys/param.h>
12 #include <sys/cpuset.h>
13 #include <sys/statvfs.h>
14
15 #include "../file.h"
16
17 #define FIO_HAVE_ODIRECT
18 #define FIO_USE_GENERIC_RAND
19 #define FIO_USE_GENERIC_INIT_RANDOM_STATE
20 #define FIO_HAVE_CHARDEV_SIZE
21 #define FIO_HAVE_FS_STAT
22 #define FIO_HAVE_TRIM
23 #define FIO_HAVE_GETTID
24 #define FIO_HAVE_CPU_AFFINITY
25 #define FIO_HAVE_SHM_ATTACH_REMOVED
26
27 #if _POSIX_THREAD_PROCESS_SHARED > 0
28 #define FIO_HAVE_PSHARED_MUTEX
29 #endif
30
31 #define OS_MAP_ANON             MAP_ANON
32
33 #define fio_swap16(x)   bswap16(x)
34 #define fio_swap32(x)   bswap32(x)
35 #define fio_swap64(x)   bswap64(x)
36
37 typedef off_t off64_t;
38
39 typedef cpuset_t os_cpu_mask_t;
40
41 #define fio_cpu_clear(mask, cpu)        (void) CPU_CLR((cpu), (mask))
42 #define fio_cpu_set(mask, cpu)          (void) CPU_SET((cpu), (mask))
43 #define fio_cpu_isset(mask, cpu)        CPU_ISSET((cpu), (mask))
44 #define fio_cpu_count(mask)             CPU_COUNT((mask))
45
46 static inline int fio_cpuset_init(os_cpu_mask_t *mask)
47 {
48         CPU_ZERO(mask);
49         return 0;
50 }
51
52 static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
53 {
54         return 0;
55 }
56
57 static inline int fio_setaffinity(int pid, os_cpu_mask_t cpumask)
58 {
59         return cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, pid, sizeof(cpumask), &cpumask);
60 }
61
62 static inline int fio_getaffinity(int pid, os_cpu_mask_t *cpumask)
63 {
64         return cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, pid, sizeof(cpumask), cpumask);
65 }
66
67 #define FIO_MAX_CPUS                    CPU_SETSIZE
68
69 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
70 {
71         off_t size;
72
73         if (!ioctl(f->fd, DIOCGMEDIASIZE, &size)) {
74                 *bytes = size;
75                 return 0;
76         }
77
78         *bytes = 0;
79         return errno;
80 }
81
82 static inline int chardev_size(struct fio_file *f, unsigned long long *bytes)
83 {
84         return blockdev_size(f, bytes);
85 }
86
87 static inline int blockdev_invalidate_cache(struct fio_file *f)
88 {
89         return ENOTSUP;
90 }
91
92 static inline unsigned long long os_phys_mem(void)
93 {
94         int mib[2] = { CTL_HW, HW_PHYSMEM };
95         unsigned long long mem;
96         size_t len = sizeof(mem);
97
98         sysctl(mib, 2, &mem, &len, NULL, 0);
99         return mem;
100 }
101
102 static inline int gettid(void)
103 {
104         long lwpid;
105
106         thr_self(&lwpid);
107         return (int) lwpid;
108 }
109
110 static inline unsigned long long get_fs_free_size(const char *path)
111 {
112         unsigned long long ret;
113         struct statvfs s;
114
115         if (statvfs(path, &s) < 0)
116                 return -1ULL;
117
118         ret = s.f_frsize;
119         ret *= (unsigned long long) s.f_bfree;
120         return ret;
121 }
122
123 static inline int os_trim(int fd, unsigned long long start,
124                           unsigned long long len)
125 {
126         off_t range[2];
127
128         range[0] = start;
129         range[1] = len;
130
131         if (!ioctl(fd, DIOCGDELETE, range))
132                 return 0;
133
134         return errno;
135 }
136
137 #ifdef MADV_FREE
138 #define FIO_MADV_FREE   MADV_FREE
139 #endif
140
141 static inline int shm_attach_to_open_removed(void)
142 {
143         int x;
144         size_t len = sizeof(x);
145
146         if (sysctlbyname("kern.ipc.shm_allow_removed", &x, &len, NULL, 0) < 0)
147                 return 0;
148
149         return x > 0 ? 1 : 0;
150 }
151
152 #endif