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