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