| 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 | #include <errno.h> |
| 11 | |
| 12 | #include "../arch/arch.h" /* IWYU pragma: export */ |
| 13 | #include "../lib/types.h" |
| 14 | |
| 15 | enum { |
| 16 | os_linux = 1, |
| 17 | os_aix, |
| 18 | os_freebsd, |
| 19 | os_hpux, |
| 20 | os_mac, |
| 21 | os_netbsd, |
| 22 | os_openbsd, |
| 23 | os_solaris, |
| 24 | os_windows, |
| 25 | os_android, |
| 26 | os_dragonfly, |
| 27 | |
| 28 | os_nr, |
| 29 | }; |
| 30 | |
| 31 | typedef enum { |
| 32 | CPU_ARM64_CRC32C, |
| 33 | } cpu_features; |
| 34 | |
| 35 | /* IWYU pragma: begin_exports */ |
| 36 | #if defined(__linux__) |
| 37 | #include "os-linux.h" |
| 38 | #elif defined(__FreeBSD__) |
| 39 | #include "os-freebsd.h" |
| 40 | #elif defined(__OpenBSD__) |
| 41 | #include "os-openbsd.h" |
| 42 | #elif defined(__NetBSD__) |
| 43 | #include "os-netbsd.h" |
| 44 | #elif defined(__sun__) |
| 45 | #include "os-solaris.h" |
| 46 | #elif defined(__APPLE__) |
| 47 | #include "os-mac.h" |
| 48 | #elif defined(_AIX) |
| 49 | #include "os-aix.h" |
| 50 | #elif defined(__hpux) |
| 51 | #include "os-hpux.h" |
| 52 | #elif defined(WIN32) |
| 53 | #include "os-windows.h" |
| 54 | #elif defined (__DragonFly__) |
| 55 | #include "os-dragonfly.h" |
| 56 | #else |
| 57 | #error "unsupported os" |
| 58 | #endif |
| 59 | |
| 60 | #ifndef EDQUOT |
| 61 | #define EDQUOT EIO |
| 62 | #endif |
| 63 | |
| 64 | #ifdef CONFIG_POSIXAIO |
| 65 | #include <aio.h> |
| 66 | #ifndef FIO_OS_HAVE_AIOCB_TYPEDEF |
| 67 | typedef struct aiocb os_aiocb_t; |
| 68 | #endif |
| 69 | #endif |
| 70 | |
| 71 | #ifndef CONFIG_STRSEP |
| 72 | #include "../oslib/strsep.h" |
| 73 | #endif |
| 74 | |
| 75 | #ifndef CONFIG_STRLCAT |
| 76 | #include "../oslib/strlcat.h" |
| 77 | #endif |
| 78 | /* IWYU pragma: end_exports */ |
| 79 | |
| 80 | #ifdef MSG_DONTWAIT |
| 81 | #define OS_MSG_DONTWAIT MSG_DONTWAIT |
| 82 | #endif |
| 83 | |
| 84 | #ifndef POSIX_FADV_DONTNEED |
| 85 | #define POSIX_FADV_DONTNEED (0) |
| 86 | #define POSIX_FADV_SEQUENTIAL (0) |
| 87 | #define POSIX_FADV_RANDOM (0) |
| 88 | #define POSIX_FADV_NORMAL (0) |
| 89 | #endif |
| 90 | |
| 91 | #ifndef FIO_HAVE_CPU_AFFINITY |
| 92 | #define fio_cpu_clear(mask, cpu) do { } while (0) |
| 93 | typedef unsigned long os_cpu_mask_t; |
| 94 | |
| 95 | static inline int fio_setaffinity(int pid, os_cpu_mask_t cpumask) |
| 96 | { |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static inline int fio_getaffinity(int pid, os_cpu_mask_t *cpumask) |
| 101 | { |
| 102 | return -1; |
| 103 | } |
| 104 | |
| 105 | static inline int fio_cpuset_exit(os_cpu_mask_t *mask) |
| 106 | { |
| 107 | return -1; |
| 108 | } |
| 109 | |
| 110 | static inline int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu_index) |
| 111 | { |
| 112 | return 0; |
| 113 | } |
| 114 | #else |
| 115 | extern int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu); |
| 116 | #endif |
| 117 | |
| 118 | #ifndef FIO_HAVE_IOPRIO_CLASS |
| 119 | #define ioprio_class(prio) 0 |
| 120 | #define ioprio_value_is_class_rt(prio) (false) |
| 121 | #define IOPRIO_MIN_PRIO_CLASS 0 |
| 122 | #define IOPRIO_MAX_PRIO_CLASS 0 |
| 123 | #define ioprio_hint(prio) 0 |
| 124 | #define IOPRIO_MIN_PRIO_HINT 0 |
| 125 | #define IOPRIO_MAX_PRIO_HINT 0 |
| 126 | #endif |
| 127 | #ifndef FIO_HAVE_IOPRIO |
| 128 | #define ioprio_value(prioclass, prio, priohint) (0) |
| 129 | #define ioprio(ioprio) 0 |
| 130 | #define ioprio_set(which, who, prioclass, prio, priohint) (0) |
| 131 | #define IOPRIO_MIN_PRIO 0 |
| 132 | #define IOPRIO_MAX_PRIO 0 |
| 133 | #endif |
| 134 | |
| 135 | #ifndef FIO_HAVE_ODIRECT |
| 136 | #define OS_O_DIRECT 0 |
| 137 | #else |
| 138 | #define OS_O_DIRECT O_DIRECT |
| 139 | #endif |
| 140 | |
| 141 | #ifndef FIO_HAVE_HUGETLB |
| 142 | #define SHM_HUGETLB 0 |
| 143 | #define MAP_HUGETLB 0 |
| 144 | #ifndef FIO_HUGE_PAGE |
| 145 | #define FIO_HUGE_PAGE 0 |
| 146 | #endif |
| 147 | #else |
| 148 | #ifndef FIO_HUGE_PAGE |
| 149 | #define FIO_HUGE_PAGE 4194304 |
| 150 | #endif |
| 151 | #endif |
| 152 | |
| 153 | #ifndef FIO_HAVE_MMAP_HUGE |
| 154 | #define MAP_HUGETLB 0 |
| 155 | #endif |
| 156 | |
| 157 | #ifndef FIO_O_NOATIME |
| 158 | #define FIO_O_NOATIME 0 |
| 159 | #endif |
| 160 | |
| 161 | #ifndef OS_RAND_MAX |
| 162 | #define OS_RAND_MAX RAND_MAX |
| 163 | #endif |
| 164 | |
| 165 | #ifndef FIO_PREFERRED_ENGINE |
| 166 | #define FIO_PREFERRED_ENGINE "psync" |
| 167 | #endif |
| 168 | |
| 169 | #ifndef FIO_OS_PATH_SEPARATOR |
| 170 | #define FIO_OS_PATH_SEPARATOR '/' |
| 171 | #endif |
| 172 | |
| 173 | #ifndef FIO_PREFERRED_CLOCK_SOURCE |
| 174 | #ifdef CONFIG_CLOCK_GETTIME |
| 175 | #define FIO_PREFERRED_CLOCK_SOURCE CS_CGETTIME |
| 176 | #else |
| 177 | #define FIO_PREFERRED_CLOCK_SOURCE CS_GTOD |
| 178 | #endif |
| 179 | #endif |
| 180 | |
| 181 | #ifndef CONFIG_SOCKLEN_T |
| 182 | typedef unsigned int socklen_t; |
| 183 | #endif |
| 184 | |
| 185 | #ifndef FIO_OS_HAS_CTIME_R |
| 186 | #define os_ctime_r(x, y, z) (void) ctime_r((x), (y)) |
| 187 | #endif |
| 188 | |
| 189 | #ifdef FIO_USE_GENERIC_SWAP |
| 190 | static inline uint16_t fio_swap16(uint16_t val) |
| 191 | { |
| 192 | return (val << 8) | (val >> 8); |
| 193 | } |
| 194 | |
| 195 | static inline uint32_t fio_swap32(uint32_t val) |
| 196 | { |
| 197 | val = ((val & 0xff00ff00UL) >> 8) | ((val & 0x00ff00ffUL) << 8); |
| 198 | |
| 199 | return (val >> 16) | (val << 16); |
| 200 | } |
| 201 | |
| 202 | static inline uint64_t fio_swap64(uint64_t val) |
| 203 | { |
| 204 | val = ((val & 0xff00ff00ff00ff00ULL) >> 8) | |
| 205 | ((val & 0x00ff00ff00ff00ffULL) << 8); |
| 206 | val = ((val & 0xffff0000ffff0000ULL) >> 16) | |
| 207 | ((val & 0x0000ffff0000ffffULL) << 16); |
| 208 | |
| 209 | return (val >> 32) | (val << 32); |
| 210 | } |
| 211 | #endif |
| 212 | |
| 213 | #ifndef FIO_HAVE_BYTEORDER_FUNCS |
| 214 | #ifdef CONFIG_LITTLE_ENDIAN |
| 215 | #define __be16_to_cpu(x) fio_swap16(x) |
| 216 | #define __be32_to_cpu(x) fio_swap32(x) |
| 217 | #define __be64_to_cpu(x) fio_swap64(x) |
| 218 | #define __le16_to_cpu(x) (x) |
| 219 | #define __le32_to_cpu(x) (x) |
| 220 | #define __le64_to_cpu(x) (x) |
| 221 | #define __cpu_to_be16(x) fio_swap16(x) |
| 222 | #define __cpu_to_be32(x) fio_swap32(x) |
| 223 | #define __cpu_to_be64(x) fio_swap64(x) |
| 224 | #define __cpu_to_le16(x) (x) |
| 225 | #define __cpu_to_le32(x) (x) |
| 226 | #define __cpu_to_le64(x) (x) |
| 227 | #else |
| 228 | #define __be16_to_cpu(x) (x) |
| 229 | #define __be32_to_cpu(x) (x) |
| 230 | #define __be64_to_cpu(x) (x) |
| 231 | #define __le16_to_cpu(x) fio_swap16(x) |
| 232 | #define __le32_to_cpu(x) fio_swap32(x) |
| 233 | #define __le64_to_cpu(x) fio_swap64(x) |
| 234 | #define __cpu_to_be16(x) (x) |
| 235 | #define __cpu_to_be32(x) (x) |
| 236 | #define __cpu_to_be64(x) (x) |
| 237 | #define __cpu_to_le16(x) fio_swap16(x) |
| 238 | #define __cpu_to_le32(x) fio_swap32(x) |
| 239 | #define __cpu_to_le64(x) fio_swap64(x) |
| 240 | #endif |
| 241 | #endif /* FIO_HAVE_BYTEORDER_FUNCS */ |
| 242 | |
| 243 | #ifdef FIO_INTERNAL |
| 244 | #define be16_to_cpu(val) ({ \ |
| 245 | typecheck(uint16_t, val); \ |
| 246 | __be16_to_cpu(val); \ |
| 247 | }) |
| 248 | #define be32_to_cpu(val) ({ \ |
| 249 | typecheck(uint32_t, val); \ |
| 250 | __be32_to_cpu(val); \ |
| 251 | }) |
| 252 | #define be64_to_cpu(val) ({ \ |
| 253 | typecheck(uint64_t, val); \ |
| 254 | __be64_to_cpu(val); \ |
| 255 | }) |
| 256 | #define le16_to_cpu(val) ({ \ |
| 257 | typecheck(uint16_t, val); \ |
| 258 | __le16_to_cpu(val); \ |
| 259 | }) |
| 260 | #define le32_to_cpu(val) ({ \ |
| 261 | typecheck(uint32_t, val); \ |
| 262 | __le32_to_cpu(val); \ |
| 263 | }) |
| 264 | #define le64_to_cpu(val) ({ \ |
| 265 | typecheck(uint64_t, val); \ |
| 266 | __le64_to_cpu(val); \ |
| 267 | }) |
| 268 | #endif |
| 269 | |
| 270 | #define cpu_to_be16(val) ({ \ |
| 271 | typecheck(uint16_t, val); \ |
| 272 | __cpu_to_be16(val); \ |
| 273 | }) |
| 274 | #define cpu_to_be32(val) ({ \ |
| 275 | typecheck(uint32_t, val); \ |
| 276 | __cpu_to_be32(val); \ |
| 277 | }) |
| 278 | #define cpu_to_be64(val) ({ \ |
| 279 | typecheck(uint64_t, val); \ |
| 280 | __cpu_to_be64(val); \ |
| 281 | }) |
| 282 | #define cpu_to_le16(val) ({ \ |
| 283 | typecheck(uint16_t, val); \ |
| 284 | __cpu_to_le16(val); \ |
| 285 | }) |
| 286 | #define cpu_to_le32(val) ({ \ |
| 287 | typecheck(uint32_t, val); \ |
| 288 | __cpu_to_le32(val); \ |
| 289 | }) |
| 290 | #define cpu_to_le64(val) ({ \ |
| 291 | typecheck(uint64_t, val); \ |
| 292 | __cpu_to_le64(val); \ |
| 293 | }) |
| 294 | |
| 295 | #define FIO_DEF_CL_SIZE 128 |
| 296 | |
| 297 | static inline int os_cache_line_size(void) |
| 298 | { |
| 299 | #ifdef FIO_HAVE_CL_SIZE |
| 300 | int ret = arch_cache_line_size(); |
| 301 | |
| 302 | if (ret <= 0) |
| 303 | return FIO_DEF_CL_SIZE; |
| 304 | |
| 305 | return ret; |
| 306 | #else |
| 307 | return FIO_DEF_CL_SIZE; |
| 308 | #endif |
| 309 | } |
| 310 | |
| 311 | #ifdef FIO_USE_GENERIC_BDEV_SIZE |
| 312 | static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes) |
| 313 | { |
| 314 | off_t end; |
| 315 | |
| 316 | *bytes = 0; |
| 317 | |
| 318 | end = lseek(f->fd, 0, SEEK_END); |
| 319 | if (end < 0) |
| 320 | return errno; |
| 321 | |
| 322 | *bytes = end; |
| 323 | return 0; |
| 324 | } |
| 325 | #endif |
| 326 | |
| 327 | #ifdef FIO_USE_GENERIC_INIT_RANDOM_STATE |
| 328 | static inline int init_random_seeds(uint64_t *rand_seeds, int size) |
| 329 | { |
| 330 | int fd; |
| 331 | |
| 332 | fd = open("/dev/urandom", O_RDONLY); |
| 333 | if (fd == -1) { |
| 334 | return 1; |
| 335 | } |
| 336 | |
| 337 | if (read(fd, rand_seeds, size) < size) { |
| 338 | close(fd); |
| 339 | return 1; |
| 340 | } |
| 341 | |
| 342 | close(fd); |
| 343 | return 0; |
| 344 | } |
| 345 | #endif |
| 346 | |
| 347 | #ifndef FIO_HAVE_FS_STAT |
| 348 | static inline unsigned long long get_fs_free_size(const char *path) |
| 349 | { |
| 350 | return 0; |
| 351 | } |
| 352 | #endif |
| 353 | |
| 354 | #ifndef FIO_HAVE_CPU_CONF_SYSCONF |
| 355 | static inline unsigned int cpus_configured(void) |
| 356 | { |
| 357 | int nr_cpus = sysconf(_SC_NPROCESSORS_CONF); |
| 358 | |
| 359 | return nr_cpus >= 1 ? nr_cpus : 1; |
| 360 | } |
| 361 | #endif |
| 362 | |
| 363 | #ifndef CPU_COUNT |
| 364 | #ifdef FIO_HAVE_CPU_AFFINITY |
| 365 | static inline int CPU_COUNT(os_cpu_mask_t *mask) |
| 366 | { |
| 367 | int max_cpus = cpus_configured(); |
| 368 | int nr_cpus, i; |
| 369 | |
| 370 | for (i = 0, nr_cpus = 0; i < max_cpus; i++) |
| 371 | if (fio_cpu_isset(mask, i)) |
| 372 | nr_cpus++; |
| 373 | |
| 374 | return nr_cpus; |
| 375 | } |
| 376 | #endif |
| 377 | #endif |
| 378 | |
| 379 | #ifndef FIO_HAVE_GETTID |
| 380 | #ifndef CONFIG_HAVE_GETTID |
| 381 | static inline int gettid(void) |
| 382 | { |
| 383 | return getpid(); |
| 384 | } |
| 385 | #endif |
| 386 | #endif |
| 387 | |
| 388 | #ifndef FIO_HAVE_SHM_ATTACH_REMOVED |
| 389 | static inline int shm_attach_to_open_removed(void) |
| 390 | { |
| 391 | return 0; |
| 392 | } |
| 393 | #endif |
| 394 | |
| 395 | #ifndef FIO_HAVE_NATIVE_FALLOCATE |
| 396 | static inline bool fio_fallocate(struct fio_file *f, uint64_t offset, uint64_t len) |
| 397 | { |
| 398 | errno = ENOSYS; |
| 399 | return false; |
| 400 | } |
| 401 | #endif |
| 402 | |
| 403 | #if defined(CONFIG_POSIX_FALLOCATE) || defined(FIO_HAVE_NATIVE_FALLOCATE) |
| 404 | # define FIO_HAVE_DEFAULT_FALLOCATE |
| 405 | #endif |
| 406 | |
| 407 | #ifndef FIO_HAVE_CPU_HAS |
| 408 | static inline bool os_cpu_has(cpu_features feature) |
| 409 | { |
| 410 | return false; |
| 411 | } |
| 412 | #endif |
| 413 | |
| 414 | #ifndef FIO_EMULATED_MKDIR_TWO |
| 415 | # define fio_mkdir(path, mode) mkdir(path, mode) |
| 416 | #endif |
| 417 | |
| 418 | #ifdef _SC_CLK_TCK |
| 419 | static inline void os_clk_tck(long *clk_tck) |
| 420 | { |
| 421 | *clk_tck = sysconf(_SC_CLK_TCK); |
| 422 | } |
| 423 | #else |
| 424 | extern void os_clk_tck(long *clk_tck); |
| 425 | #endif |
| 426 | |
| 427 | #endif /* FIO_OS_H */ |