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