Merge branch 'minor_fixes' of https://github.com/sitsofe/fio
[fio.git] / os / os-freebsd.h
... / ...
CommitLineData
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_RAND
20#define FIO_USE_GENERIC_INIT_RANDOM_STATE
21#define FIO_HAVE_CHARDEV_SIZE
22#define FIO_HAVE_FS_STAT
23#define FIO_HAVE_TRIM
24#define FIO_HAVE_GETTID
25#define FIO_HAVE_CPU_AFFINITY
26#define FIO_HAVE_SHM_ATTACH_REMOVED
27
28#define OS_MAP_ANON MAP_ANON
29
30#define fio_swap16(x) bswap16(x)
31#define fio_swap32(x) bswap32(x)
32#define fio_swap64(x) bswap64(x)
33
34typedef off_t off64_t;
35
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))
40#define fio_cpu_isset(mask, cpu) CPU_ISSET((cpu), (mask))
41#define fio_cpu_count(mask) CPU_COUNT((mask))
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
66static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
67{
68 off_t size;
69
70 if (!ioctl(f->fd, DIOCGMEDIASIZE, &size)) {
71 *bytes = size;
72 return 0;
73 }
74
75 *bytes = 0;
76 return errno;
77}
78
79static inline int chardev_size(struct fio_file *f, unsigned long long *bytes)
80{
81 return blockdev_size(f, bytes);
82}
83
84static inline int blockdev_invalidate_cache(struct fio_file *f)
85{
86 return ENOTSUP;
87}
88
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
99static inline int gettid(void)
100{
101 long lwpid;
102
103 thr_self(&lwpid);
104 return (int) lwpid;
105}
106
107static inline unsigned long long get_fs_free_size(const char *path)
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
120static inline int os_trim(int fd, unsigned long long start,
121 unsigned long long len)
122{
123 off_t range[2];
124
125 range[0] = start;
126 range[1] = len;
127
128 if (!ioctl(fd, DIOCGDELETE, range))
129 return 0;
130
131 return errno;
132}
133
134#ifdef MADV_FREE
135#define FIO_MADV_FREE MADV_FREE
136#endif
137
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
149#endif