d4a9579958ffcd16856b495e9fc79766f2feef69
[fio.git] / os / os-dragonfly.h
1 #ifndef FIO_OS_DRAGONFLY_H
2 #define FIO_OS_DRAGONFLY_H
3
4 #define FIO_OS  os_dragonfly
5
6 #include <errno.h>
7 #include <unistd.h>
8 #include <sys/param.h>
9 #include <sys/sysctl.h>
10 #include <sys/statvfs.h>
11 #include <sys/diskslice.h>
12 #include <sys/ioctl_compat.h>
13 #include <sys/usched.h>
14 #include <sys/resource.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_FS_STAT
22 #define FIO_HAVE_TRIM
23 #define FIO_HAVE_CHARDEV_SIZE
24 #define FIO_HAVE_GETTID
25 #define FIO_HAVE_CPU_AFFINITY
26 #define FIO_HAVE_IOPRIO
27
28 #define OS_MAP_ANON             MAP_ANON
29
30 #ifndef PTHREAD_STACK_MIN
31 #define PTHREAD_STACK_MIN 4096
32 #endif
33
34 #define fio_swap16(x)   bswap16(x)
35 #define fio_swap32(x)   bswap32(x)
36 #define fio_swap64(x)   bswap64(x)
37
38 /* This is supposed to equal (sizeof(cpumask_t)*8) */
39 #define FIO_MAX_CPUS    SMP_MAXCPU
40
41 typedef off_t off64_t;
42 typedef cpumask_t os_cpu_mask_t;
43
44 /*
45  * These macros are copied from sys/cpu/x86_64/include/types.h.
46  * It's okay to copy from arch dependent header because x86_64 is the only
47  * supported arch, and no other arch is going to be supported any time soon.
48  *
49  * These are supposed to be able to be included from userspace by defining
50  * _KERNEL_STRUCTURES, however this scheme is badly broken that enabling it
51  * causes compile-time conflicts with other headers. Although the current
52  * upstream code no longer requires _KERNEL_STRUCTURES, they should be kept
53  * here for compatibility with older versions.
54  */
55 #ifndef CPUMASK_SIMPLE
56 #define CPUMASK_SIMPLE(cpu)             ((uint64_t)1 << (cpu))
57 #define CPUMASK_TESTBIT(val, i)         ((val).ary[((i) >> 6) & 3] & \
58                                          CPUMASK_SIMPLE((i) & 63))
59 #define CPUMASK_ORBIT(mask, i)          ((mask).ary[((i) >> 6) & 3] |= \
60                                          CPUMASK_SIMPLE((i) & 63))
61 #define CPUMASK_NANDBIT(mask, i)        ((mask).ary[((i) >> 6) & 3] &= \
62                                          ~CPUMASK_SIMPLE((i) & 63))
63 #define CPUMASK_ASSZERO(mask)           do {                            \
64                                         (mask).ary[0] = 0;              \
65                                         (mask).ary[1] = 0;              \
66                                         (mask).ary[2] = 0;              \
67                                         (mask).ary[3] = 0;              \
68                                         } while(0)
69 #endif
70
71 /*
72  * Define USCHED_GET_CPUMASK as the macro didn't exist until release 4.5.
73  * usched_set(2) returns EINVAL if the kernel doesn't support it.
74  *
75  * Also note usched_set(2) works only for the current thread regardless of
76  * the command type. It doesn't work against another thread regardless of
77  * a caller's privilege. A caller would generally specify 0 for pid for the
78  * current thread though that's the only choice. See BUGS in usched_set(2).
79  */
80 #ifndef USCHED_GET_CPUMASK
81 #define USCHED_GET_CPUMASK      5
82 #endif
83
84 static inline int fio_cpuset_init(os_cpu_mask_t *mask)
85 {
86         CPUMASK_ASSZERO(*mask);
87         return 0;
88 }
89
90 static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
91 {
92         return 0;
93 }
94
95 static inline void fio_cpu_clear(os_cpu_mask_t *mask, int cpu)
96 {
97         CPUMASK_NANDBIT(*mask, cpu);
98 }
99
100 static inline void fio_cpu_set(os_cpu_mask_t *mask, int cpu)
101 {
102         CPUMASK_ORBIT(*mask, cpu);
103 }
104
105 static inline int fio_cpu_isset(os_cpu_mask_t *mask, int cpu)
106 {
107         if (CPUMASK_TESTBIT(*mask, cpu))
108                 return 1;
109
110         return 0;
111 }
112
113 static inline int fio_cpu_count(os_cpu_mask_t *mask)
114 {
115         int i, n = 0;
116
117         for (i = 0; i < FIO_MAX_CPUS; i++)
118                 if (CPUMASK_TESTBIT(*mask, i))
119                         n++;
120
121         return n;
122 }
123
124 static inline int fio_setaffinity(int pid, os_cpu_mask_t mask)
125 {
126         int i, firstcall = 1;
127
128         /* 0 for the current thread, see BUGS in usched_set(2) */
129         pid = 0;
130
131         for (i = 0; i < FIO_MAX_CPUS; i++) {
132                 if (!CPUMASK_TESTBIT(mask, i))
133                         continue;
134                 if (firstcall) {
135                         if (usched_set(pid, USCHED_SET_CPU, &i, sizeof(int)))
136                                 return -1;
137                         firstcall = 0;
138                 } else {
139                         if (usched_set(pid, USCHED_ADD_CPU, &i, sizeof(int)))
140                                 return -1;
141                 }
142         }
143
144         return 0;
145 }
146
147 static inline int fio_getaffinity(int pid, os_cpu_mask_t *mask)
148 {
149         /* 0 for the current thread, see BUGS in usched_set(2) */
150         pid = 0;
151
152         if (usched_set(pid, USCHED_GET_CPUMASK, mask, sizeof(*mask)))
153                 return -1;
154
155         return 0;
156 }
157
158 /* fio code is Linux based, so rename macros to Linux style */
159 #define IOPRIO_WHO_PROCESS      PRIO_PROCESS
160 #define IOPRIO_WHO_PGRP         PRIO_PGRP
161 #define IOPRIO_WHO_USER         PRIO_USER
162
163 #define IOPRIO_MIN_PRIO         1       /* lowest priority */
164 #define IOPRIO_MAX_PRIO         10      /* highest priority */
165
166 /*
167  * Prototypes declared in sys/sys/resource.h are preventing from defining
168  * ioprio_set() with 4 arguments, so define fio's ioprio_set() as a macro.
169  * Note that there is no idea of class within ioprio_set(2) unlike Linux.
170  */
171 #define ioprio_set(which, who, ioprio_class, ioprio)    \
172         ioprio_set(which, who, ioprio)
173
174 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
175 {
176         struct partinfo pi;
177
178         if (!ioctl(f->fd, DIOCGPART, &pi)) {
179                 *bytes = (unsigned long long) pi.media_size;
180                 return 0;
181         }
182
183         *bytes = 0;
184         return errno;
185 }
186
187 static inline int chardev_size(struct fio_file *f, unsigned long long *bytes)
188 {
189         return blockdev_size(f, bytes);
190 }
191
192 static inline int blockdev_invalidate_cache(struct fio_file *f)
193 {
194         return EINVAL;
195 }
196
197 static inline unsigned long long os_phys_mem(void)
198 {
199         int mib[2] = { CTL_HW, HW_PHYSMEM };
200         uint64_t mem;
201         size_t len = sizeof(mem);
202
203         sysctl(mib, 2, &mem, &len, NULL, 0);
204         return mem;
205 }
206
207 static inline int gettid(void)
208 {
209         return (int) lwp_gettid();
210 }
211
212 static inline unsigned long long get_fs_free_size(const char *path)
213 {
214         unsigned long long ret;
215         struct statvfs s;
216
217         if (statvfs(path, &s) < 0)
218                 return -1ULL;
219
220         ret = s.f_frsize;
221         ret *= (unsigned long long) s.f_bfree;
222         return ret;
223 }
224
225 static inline int os_trim(int fd, unsigned long long start,
226                           unsigned long long len)
227 {
228         off_t range[2];
229
230         range[0] = start;
231         range[1] = len;
232
233         if (!ioctl(fd, IOCTLTRIM, range))
234                 return 0;
235
236         return errno;
237 }
238
239 #ifdef MADV_FREE
240 #define FIO_MADV_FREE   MADV_FREE
241 #endif
242
243 #endif