| 1 | #ifndef FIO_OS_H |
| 2 | #define FIO_OS_H |
| 3 | |
| 4 | #include <sys/types.h> |
| 5 | #include <sys/socket.h> |
| 6 | #include <fcntl.h> |
| 7 | #include <pthread.h> |
| 8 | #include <unistd.h> |
| 9 | #include <stdlib.h> |
| 10 | |
| 11 | #include "../arch/arch.h" |
| 12 | #include "../lib/types.h" |
| 13 | |
| 14 | enum { |
| 15 | os_linux = 1, |
| 16 | os_aix, |
| 17 | os_freebsd, |
| 18 | os_hpux, |
| 19 | os_mac, |
| 20 | os_netbsd, |
| 21 | os_openbsd, |
| 22 | os_solaris, |
| 23 | os_windows, |
| 24 | os_android, |
| 25 | os_dragonfly, |
| 26 | |
| 27 | os_nr, |
| 28 | }; |
| 29 | |
| 30 | #if defined(__ANDROID__) |
| 31 | #include "os-android.h" |
| 32 | #elif defined(__linux__) |
| 33 | #include "os-linux.h" |
| 34 | #elif defined(__FreeBSD__) |
| 35 | #include "os-freebsd.h" |
| 36 | #elif defined(__OpenBSD__) |
| 37 | #include "os-openbsd.h" |
| 38 | #elif defined(__NetBSD__) |
| 39 | #include "os-netbsd.h" |
| 40 | #elif defined(__sun__) |
| 41 | #include "os-solaris.h" |
| 42 | #elif defined(__APPLE__) |
| 43 | #include "os-mac.h" |
| 44 | #elif defined(_AIX) |
| 45 | #include "os-aix.h" |
| 46 | #elif defined(__hpux) |
| 47 | #include "os-hpux.h" |
| 48 | #elif defined(WIN32) |
| 49 | #include "os-windows.h" |
| 50 | #elif defined (__DragonFly__) |
| 51 | #include "os-dragonfly.h" |
| 52 | #else |
| 53 | #error "unsupported os" |
| 54 | #endif |
| 55 | |
| 56 | #ifdef CONFIG_POSIXAIO |
| 57 | #include <aio.h> |
| 58 | #ifndef FIO_OS_HAVE_AIOCB_TYPEDEF |
| 59 | typedef struct aiocb os_aiocb_t; |
| 60 | #endif |
| 61 | #endif |
| 62 | |
| 63 | #ifdef FIO_HAVE_SGIO |
| 64 | #include <linux/fs.h> |
| 65 | #include <scsi/sg.h> |
| 66 | #endif |
| 67 | |
| 68 | #ifndef CONFIG_STRSEP |
| 69 | #include "../oslib/strsep.h" |
| 70 | #endif |
| 71 | |
| 72 | #ifndef CONFIG_STRLCAT |
| 73 | #include "../oslib/strlcat.h" |
| 74 | #endif |
| 75 | |
| 76 | #ifdef MSG_DONTWAIT |
| 77 | #define OS_MSG_DONTWAIT MSG_DONTWAIT |
| 78 | #endif |
| 79 | |
| 80 | #ifndef POSIX_FADV_DONTNEED |
| 81 | #define POSIX_FADV_DONTNEED (0) |
| 82 | #define POSIX_FADV_SEQUENTIAL (0) |
| 83 | #define POSIX_FADV_RANDOM (0) |
| 84 | #endif |
| 85 | |
| 86 | #ifndef FIO_HAVE_CPU_AFFINITY |
| 87 | #define fio_getaffinity(pid, mask) do { } while (0) |
| 88 | #define fio_cpu_clear(mask, cpu) do { } while (0) |
| 89 | typedef unsigned long os_cpu_mask_t; |
| 90 | |
| 91 | static inline int fio_setaffinity(int pid, os_cpu_mask_t cpumask) |
| 92 | { |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | static inline int fio_cpuset_exit(os_cpu_mask_t *mask) |
| 97 | { |
| 98 | return -1; |
| 99 | } |
| 100 | |
| 101 | static inline int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu_index) |
| 102 | { |
| 103 | return 0; |
| 104 | } |
| 105 | #else |
| 106 | extern int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu); |
| 107 | #endif |
| 108 | |
| 109 | #ifndef FIO_HAVE_IOPRIO |
| 110 | #define ioprio_set(which, who, prioclass, prio) (0) |
| 111 | #endif |
| 112 | |
| 113 | #ifndef FIO_HAVE_ODIRECT |
| 114 | #define OS_O_DIRECT 0 |
| 115 | #else |
| 116 | #define OS_O_DIRECT O_DIRECT |
| 117 | #endif |
| 118 | |
| 119 | #ifdef OS_O_ATOMIC |
| 120 | #define FIO_O_ATOMIC OS_O_ATOMIC |
| 121 | #else |
| 122 | #define FIO_O_ATOMIC 0 |
| 123 | #endif |
| 124 | |
| 125 | #ifndef FIO_HAVE_HUGETLB |
| 126 | #define SHM_HUGETLB 0 |
| 127 | #define MAP_HUGETLB 0 |
| 128 | #ifndef FIO_HUGE_PAGE |
| 129 | #define FIO_HUGE_PAGE 0 |
| 130 | #endif |
| 131 | #else |
| 132 | #ifndef FIO_HUGE_PAGE |
| 133 | #define FIO_HUGE_PAGE 4194304 |
| 134 | #endif |
| 135 | #endif |
| 136 | |
| 137 | #ifndef FIO_HAVE_MMAP_HUGE |
| 138 | #define MAP_HUGETLB 0 |
| 139 | #endif |
| 140 | |
| 141 | #ifndef FIO_O_NOATIME |
| 142 | #define FIO_O_NOATIME 0 |
| 143 | #endif |
| 144 | |
| 145 | #ifndef OS_RAND_MAX |
| 146 | #define OS_RAND_MAX RAND_MAX |
| 147 | #endif |
| 148 | |
| 149 | #ifndef FIO_HAVE_RAWBIND |
| 150 | #define fio_lookup_raw(dev, majdev, mindev) 1 |
| 151 | #endif |
| 152 | |
| 153 | #ifndef FIO_PREFERRED_ENGINE |
| 154 | #define FIO_PREFERRED_ENGINE "sync" |
| 155 | #endif |
| 156 | |
| 157 | #ifndef FIO_OS_PATH_SEPARATOR |
| 158 | #define FIO_OS_PATH_SEPARATOR "/" |
| 159 | #endif |
| 160 | |
| 161 | #ifndef FIO_PREFERRED_CLOCK_SOURCE |
| 162 | #ifdef CONFIG_CLOCK_GETTIME |
| 163 | #define FIO_PREFERRED_CLOCK_SOURCE CS_CGETTIME |
| 164 | #else |
| 165 | #define FIO_PREFERRED_CLOCK_SOURCE CS_GTOD |
| 166 | #endif |
| 167 | #endif |
| 168 | |
| 169 | #ifndef FIO_MAX_JOBS |
| 170 | #define FIO_MAX_JOBS 2048 |
| 171 | #endif |
| 172 | |
| 173 | #ifndef CONFIG_SOCKLEN_T |
| 174 | typedef unsigned int socklen_t; |
| 175 | #endif |
| 176 | |
| 177 | #ifndef FIO_OS_HAS_CTIME_R |
| 178 | #define os_ctime_r(x, y, z) (void) ctime_r((x), (y)) |
| 179 | #endif |
| 180 | |
| 181 | #ifdef FIO_USE_GENERIC_SWAP |
| 182 | static inline uint16_t fio_swap16(uint16_t val) |
| 183 | { |
| 184 | return (val << 8) | (val >> 8); |
| 185 | } |
| 186 | |
| 187 | static inline uint32_t fio_swap32(uint32_t val) |
| 188 | { |
| 189 | val = ((val & 0xff00ff00UL) >> 8) | ((val & 0x00ff00ffUL) << 8); |
| 190 | |
| 191 | return (val >> 16) | (val << 16); |
| 192 | } |
| 193 | |
| 194 | static inline uint64_t fio_swap64(uint64_t val) |
| 195 | { |
| 196 | val = ((val & 0xff00ff00ff00ff00ULL) >> 8) | |
| 197 | ((val & 0x00ff00ff00ff00ffULL) << 8); |
| 198 | val = ((val & 0xffff0000ffff0000ULL) >> 16) | |
| 199 | ((val & 0x0000ffff0000ffffULL) << 16); |
| 200 | |
| 201 | return (val >> 32) | (val << 32); |
| 202 | } |
| 203 | #endif |
| 204 | |
| 205 | #ifndef FIO_HAVE_BYTEORDER_FUNCS |
| 206 | #ifdef CONFIG_LITTLE_ENDIAN |
| 207 | #define __le16_to_cpu(x) (x) |
| 208 | #define __le32_to_cpu(x) (x) |
| 209 | #define __le64_to_cpu(x) (x) |
| 210 | #define __cpu_to_le16(x) (x) |
| 211 | #define __cpu_to_le32(x) (x) |
| 212 | #define __cpu_to_le64(x) (x) |
| 213 | #else |
| 214 | #define __le16_to_cpu(x) fio_swap16(x) |
| 215 | #define __le32_to_cpu(x) fio_swap32(x) |
| 216 | #define __le64_to_cpu(x) fio_swap64(x) |
| 217 | #define __cpu_to_le16(x) fio_swap16(x) |
| 218 | #define __cpu_to_le32(x) fio_swap32(x) |
| 219 | #define __cpu_to_le64(x) fio_swap64(x) |
| 220 | #endif |
| 221 | #endif /* FIO_HAVE_BYTEORDER_FUNCS */ |
| 222 | |
| 223 | #ifdef FIO_INTERNAL |
| 224 | #define le16_to_cpu(val) ({ \ |
| 225 | typecheck(uint16_t, val); \ |
| 226 | __le16_to_cpu(val); \ |
| 227 | }) |
| 228 | #define le32_to_cpu(val) ({ \ |
| 229 | typecheck(uint32_t, val); \ |
| 230 | __le32_to_cpu(val); \ |
| 231 | }) |
| 232 | #define le64_to_cpu(val) ({ \ |
| 233 | typecheck(uint64_t, val); \ |
| 234 | __le64_to_cpu(val); \ |
| 235 | }) |
| 236 | #endif |
| 237 | |
| 238 | #define cpu_to_le16(val) ({ \ |
| 239 | typecheck(uint16_t, val); \ |
| 240 | __cpu_to_le16(val); \ |
| 241 | }) |
| 242 | #define cpu_to_le32(val) ({ \ |
| 243 | typecheck(uint32_t, val); \ |
| 244 | __cpu_to_le32(val); \ |
| 245 | }) |
| 246 | #define cpu_to_le64(val) ({ \ |
| 247 | typecheck(uint64_t, val); \ |
| 248 | __cpu_to_le64(val); \ |
| 249 | }) |
| 250 | |
| 251 | #ifndef FIO_HAVE_BLKTRACE |
| 252 | static inline int is_blktrace(const char *fname, int *need_swap) |
| 253 | { |
| 254 | return 0; |
| 255 | } |
| 256 | struct thread_data; |
| 257 | static inline int load_blktrace(struct thread_data *td, const char *fname, |
| 258 | int need_swap) |
| 259 | { |
| 260 | return 1; |
| 261 | } |
| 262 | #endif |
| 263 | |
| 264 | #define FIO_DEF_CL_SIZE 128 |
| 265 | |
| 266 | static inline int os_cache_line_size(void) |
| 267 | { |
| 268 | #ifdef FIO_HAVE_CL_SIZE |
| 269 | int ret = arch_cache_line_size(); |
| 270 | |
| 271 | if (ret <= 0) |
| 272 | return FIO_DEF_CL_SIZE; |
| 273 | |
| 274 | return ret; |
| 275 | #else |
| 276 | return FIO_DEF_CL_SIZE; |
| 277 | #endif |
| 278 | } |
| 279 | |
| 280 | #ifdef FIO_USE_GENERIC_BDEV_SIZE |
| 281 | static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes) |
| 282 | { |
| 283 | off_t end; |
| 284 | |
| 285 | *bytes = 0; |
| 286 | |
| 287 | end = lseek(f->fd, 0, SEEK_END); |
| 288 | if (end < 0) |
| 289 | return errno; |
| 290 | |
| 291 | *bytes = end; |
| 292 | return 0; |
| 293 | } |
| 294 | #endif |
| 295 | |
| 296 | #ifdef FIO_USE_GENERIC_RAND |
| 297 | typedef unsigned int os_random_state_t; |
| 298 | |
| 299 | static inline void os_random_seed(unsigned long seed, os_random_state_t *rs) |
| 300 | { |
| 301 | srand(seed); |
| 302 | } |
| 303 | |
| 304 | static inline long os_random_long(os_random_state_t *rs) |
| 305 | { |
| 306 | long val; |
| 307 | |
| 308 | val = rand_r(rs); |
| 309 | return val; |
| 310 | } |
| 311 | #endif |
| 312 | |
| 313 | #ifdef FIO_USE_GENERIC_INIT_RANDOM_STATE |
| 314 | extern void td_fill_rand_seeds(struct thread_data *td); |
| 315 | /* |
| 316 | * Initialize the various random states we need (random io, block size ranges, |
| 317 | * read/write mix, etc). |
| 318 | */ |
| 319 | static inline int init_random_state(struct thread_data *td, unsigned long *rand_seeds, int size) |
| 320 | { |
| 321 | int fd; |
| 322 | |
| 323 | fd = open("/dev/urandom", O_RDONLY); |
| 324 | if (fd == -1) { |
| 325 | return 1; |
| 326 | } |
| 327 | |
| 328 | if (read(fd, rand_seeds, size) < size) { |
| 329 | close(fd); |
| 330 | return 1; |
| 331 | } |
| 332 | |
| 333 | close(fd); |
| 334 | td_fill_rand_seeds(td); |
| 335 | return 0; |
| 336 | } |
| 337 | #endif |
| 338 | |
| 339 | #ifndef FIO_HAVE_FS_STAT |
| 340 | static inline unsigned long long get_fs_free_size(const char *path) |
| 341 | { |
| 342 | return 0; |
| 343 | } |
| 344 | #endif |
| 345 | |
| 346 | #ifndef FIO_HAVE_CPU_ONLINE_SYSCONF |
| 347 | static inline unsigned int cpus_online(void) |
| 348 | { |
| 349 | return sysconf(_SC_NPROCESSORS_ONLN); |
| 350 | } |
| 351 | #endif |
| 352 | |
| 353 | #ifndef CPU_COUNT |
| 354 | #ifdef FIO_HAVE_CPU_AFFINITY |
| 355 | static inline int CPU_COUNT(os_cpu_mask_t *mask) |
| 356 | { |
| 357 | int max_cpus = cpus_online(); |
| 358 | int nr_cpus, i; |
| 359 | |
| 360 | for (i = 0, nr_cpus = 0; i < max_cpus; i++) |
| 361 | if (fio_cpu_isset(mask, i)) |
| 362 | nr_cpus++; |
| 363 | |
| 364 | return nr_cpus; |
| 365 | } |
| 366 | #endif |
| 367 | #endif |
| 368 | |
| 369 | #ifndef FIO_HAVE_GETTID |
| 370 | static inline int gettid(void) |
| 371 | { |
| 372 | return getpid(); |
| 373 | } |
| 374 | #endif |
| 375 | |
| 376 | #endif |