shm: have os remove shared memory if fio dies unexpectedly
[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 /* Only have attach-to-open-removed when kern.ipc.shm_allow_removed is 1 */
26 #undef  FIO_HAVE_SHM_ATTACH_REMOVED
27
28
29 #define OS_MAP_ANON             MAP_ANON
30
31 #define fio_swap16(x)   bswap16(x)
32 #define fio_swap32(x)   bswap32(x)
33 #define fio_swap64(x)   bswap64(x)
34
35 typedef off_t off64_t;
36
37 typedef cpuset_t os_cpu_mask_t;
38
39 #define fio_cpu_clear(mask, cpu)        (void) CPU_CLR((cpu), (mask))
40 #define fio_cpu_set(mask, cpu)          (void) CPU_SET((cpu), (mask))
41 #define fio_cpu_isset(mask, cpu)        CPU_ISSET((cpu), (mask))
42 #define fio_cpu_count(mask)             CPU_COUNT((mask))
43
44 static inline int fio_cpuset_init(os_cpu_mask_t *mask)
45 {
46         CPU_ZERO(mask);
47         return 0;
48 }
49
50 static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
51 {
52         return 0;
53 }
54
55 static inline int fio_setaffinity(int pid, os_cpu_mask_t cpumask)
56 {
57         return cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, pid, sizeof(cpumask), &cpumask);
58 }
59
60 static inline int fio_getaffinity(int pid, os_cpu_mask_t *cpumask)
61 {
62         return cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, pid, sizeof(cpumask), cpumask);
63 }
64
65 #define FIO_MAX_CPUS                    CPU_SETSIZE
66
67 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
68 {
69         off_t size;
70
71         if (!ioctl(f->fd, DIOCGMEDIASIZE, &size)) {
72                 *bytes = size;
73                 return 0;
74         }
75
76         *bytes = 0;
77         return errno;
78 }
79
80 static inline int chardev_size(struct fio_file *f, unsigned long long *bytes)
81 {
82         return blockdev_size(f, bytes);
83 }
84
85 static inline int blockdev_invalidate_cache(struct fio_file *f)
86 {
87         return EINVAL;
88 }
89
90 static inline unsigned long long os_phys_mem(void)
91 {
92         int mib[2] = { CTL_HW, HW_PHYSMEM };
93         unsigned long long mem;
94         size_t len = sizeof(mem);
95
96         sysctl(mib, 2, &mem, &len, NULL, 0);
97         return mem;
98 }
99
100 static inline int gettid(void)
101 {
102         long lwpid;
103
104         thr_self(&lwpid);
105         return (int) lwpid;
106 }
107
108 static inline unsigned long long get_fs_free_size(const char *path)
109 {
110         unsigned long long ret;
111         struct statvfs s;
112
113         if (statvfs(path, &s) < 0)
114                 return -1ULL;
115
116         ret = s.f_frsize;
117         ret *= (unsigned long long) s.f_bfree;
118         return ret;
119 }
120
121 static inline int os_trim(int fd, unsigned long long start,
122                           unsigned long long len)
123 {
124         off_t range[2];
125
126         range[0] = start;
127         range[1] = len;
128
129         if (!ioctl(fd, DIOCGDELETE, range))
130                 return 0;
131
132         return errno;
133 }
134
135 #ifdef MADV_FREE
136 #define FIO_MADV_FREE   MADV_FREE
137 #endif
138
139 #endif