Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1 | #ifndef FIO_OS_H |
| 2 | #define FIO_OS_H |
| 3 | |
Jens Axboe | a14ca44 | 2009-12-20 22:24:10 +0100 | [diff] [blame] | 4 | #include <sys/types.h> |
Bruce Cran | 03e20d6 | 2011-01-02 20:14:54 +0100 | [diff] [blame] | 5 | #include <pthread.h> |
Jens Axboe | a14ca44 | 2009-12-20 22:24:10 +0100 | [diff] [blame] | 6 | #include <unistd.h> |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 7 | #include <stdlib.h> |
Jens Axboe | a14ca44 | 2009-12-20 22:24:10 +0100 | [diff] [blame] | 8 | |
Jens Axboe | cca8464 | 2011-10-07 12:47:57 +0200 | [diff] [blame] | 9 | enum { |
| 10 | os_linux = 1, |
| 11 | os_aix, |
| 12 | os_freebsd, |
| 13 | os_hpux, |
| 14 | os_mac, |
| 15 | os_netbsd, |
| 16 | os_solaris, |
| 17 | os_windows, |
| 18 | |
| 19 | os_nr, |
| 20 | }; |
| 21 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 22 | #if defined(__linux__) |
| 23 | #include "os-linux.h" |
| 24 | #elif defined(__FreeBSD__) |
| 25 | #include "os-freebsd.h" |
YAMAMOTO Takashi | 7452440 | 2010-05-12 15:06:46 +0200 | [diff] [blame] | 26 | #elif defined(__NetBSD__) |
| 27 | #include "os-netbsd.h" |
Jens Axboe | 2c0ecd2 | 2006-06-08 13:25:41 +0200 | [diff] [blame] | 28 | #elif defined(__sun__) |
| 29 | #include "os-solaris.h" |
Jens Axboe | 2afd826 | 2009-12-14 23:08:42 +0100 | [diff] [blame] | 30 | #elif defined(__APPLE__) |
| 31 | #include "os-mac.h" |
Cigy Cyriac | bf2e821 | 2010-08-10 19:51:11 -0400 | [diff] [blame] | 32 | #elif defined(_AIX) |
| 33 | #include "os-aix.h" |
Jens Axboe | c00a228 | 2011-07-08 20:56:06 +0200 | [diff] [blame] | 34 | #elif defined(__hpux) |
| 35 | #include "os-hpux.h" |
Bruce Cran | 03e20d6 | 2011-01-02 20:14:54 +0100 | [diff] [blame] | 36 | #elif defined(__CYGWIN__) |
| 37 | #include "os-windows.h" |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 38 | #else |
| 39 | #error "unsupported os" |
| 40 | #endif |
| 41 | |
| 42 | #ifdef FIO_HAVE_LIBAIO |
| 43 | #include <libaio.h> |
| 44 | #endif |
| 45 | |
| 46 | #ifdef FIO_HAVE_POSIXAIO |
| 47 | #include <aio.h> |
Jens Axboe | e97c144 | 2011-09-21 09:38:01 +0200 | [diff] [blame] | 48 | #ifndef FIO_OS_HAVE_AIOCB_TYPEDEF |
| 49 | typedef struct aiocb os_aiocb_t; |
| 50 | #endif |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 51 | #endif |
| 52 | |
| 53 | #ifdef FIO_HAVE_SGIO |
| 54 | #include <linux/fs.h> |
| 55 | #include <scsi/sg.h> |
| 56 | #endif |
| 57 | |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 58 | #ifndef FIO_HAVE_STRSEP |
Jens Axboe | 00fb3c8 | 2008-05-30 22:17:45 +0200 | [diff] [blame] | 59 | #include "../lib/strsep.h" |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 60 | #endif |
| 61 | |
Jens Axboe | 8e239ca | 2010-08-11 10:29:12 -0400 | [diff] [blame] | 62 | #ifdef MSG_DONTWAIT |
| 63 | #define OS_MSG_DONTWAIT MSG_DONTWAIT |
| 64 | #endif |
| 65 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 66 | #ifndef FIO_HAVE_FADVISE |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 67 | #define posix_fadvise(fd, off, len, advice) (0) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 68 | |
Shawn Lewis | 4d8947d | 2007-07-25 07:51:58 +0200 | [diff] [blame] | 69 | #ifndef POSIX_FADV_DONTNEED |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 70 | #define POSIX_FADV_DONTNEED (0) |
| 71 | #define POSIX_FADV_SEQUENTIAL (0) |
| 72 | #define POSIX_FADV_RANDOM (0) |
Shawn Lewis | 4d8947d | 2007-07-25 07:51:58 +0200 | [diff] [blame] | 73 | #endif |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 74 | #endif /* FIO_HAVE_FADVISE */ |
| 75 | |
| 76 | #ifndef FIO_HAVE_CPU_AFFINITY |
Jens Axboe | e8462bd | 2009-07-06 12:59:04 +0200 | [diff] [blame] | 77 | #define fio_setaffinity(pid, mask) (0) |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 78 | #define fio_getaffinity(pid, mask) do { } while (0) |
| 79 | #define fio_cpu_clear(mask, cpu) do { } while (0) |
Jens Axboe | 85daf2c | 2009-01-05 09:40:13 +0100 | [diff] [blame] | 80 | #define fio_cpuset_exit(mask) (-1) |
YAMAMOTO Takashi | 7452440 | 2010-05-12 15:06:46 +0200 | [diff] [blame] | 81 | typedef unsigned long os_cpu_mask_t; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 82 | #endif |
| 83 | |
| 84 | #ifndef FIO_HAVE_IOPRIO |
| 85 | #define ioprio_set(which, who, prio) (0) |
| 86 | #endif |
| 87 | |
Jens Axboe | 2c0ecd2 | 2006-06-08 13:25:41 +0200 | [diff] [blame] | 88 | #ifndef FIO_HAVE_ODIRECT |
Jens Axboe | 7d42476 | 2006-06-08 13:26:31 +0200 | [diff] [blame] | 89 | #define OS_O_DIRECT 0 |
| 90 | #else |
| 91 | #define OS_O_DIRECT O_DIRECT |
Jens Axboe | 2c0ecd2 | 2006-06-08 13:25:41 +0200 | [diff] [blame] | 92 | #endif |
| 93 | |
Jens Axboe | 74b025b | 2006-12-19 15:18:14 +0100 | [diff] [blame] | 94 | #ifndef FIO_HAVE_HUGETLB |
| 95 | #define SHM_HUGETLB 0 |
Shawn Lewis | 4d8947d | 2007-07-25 07:51:58 +0200 | [diff] [blame] | 96 | #ifndef FIO_HUGE_PAGE |
Jens Axboe | 74b025b | 2006-12-19 15:18:14 +0100 | [diff] [blame] | 97 | #define FIO_HUGE_PAGE 0 |
Shawn Lewis | 4d8947d | 2007-07-25 07:51:58 +0200 | [diff] [blame] | 98 | #endif |
Jens Axboe | 74b025b | 2006-12-19 15:18:14 +0100 | [diff] [blame] | 99 | #else |
Jens Axboe | cb25df6 | 2007-03-23 08:23:30 +0100 | [diff] [blame] | 100 | #ifndef FIO_HUGE_PAGE |
Jens Axboe | ee0e0a7 | 2007-03-19 10:50:47 +0100 | [diff] [blame] | 101 | #define FIO_HUGE_PAGE 4194304 |
Jens Axboe | 74b025b | 2006-12-19 15:18:14 +0100 | [diff] [blame] | 102 | #endif |
Jens Axboe | cb25df6 | 2007-03-23 08:23:30 +0100 | [diff] [blame] | 103 | #endif |
Jens Axboe | 74b025b | 2006-12-19 15:18:14 +0100 | [diff] [blame] | 104 | |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 105 | #ifndef FIO_O_NOATIME |
| 106 | #define FIO_O_NOATIME 0 |
| 107 | #endif |
| 108 | |
Jens Axboe | dc873b6 | 2008-06-04 20:13:04 +0200 | [diff] [blame] | 109 | #ifndef OS_RAND_MAX |
| 110 | #define OS_RAND_MAX RAND_MAX |
| 111 | #endif |
| 112 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 113 | #ifdef FIO_HAVE_CLOCK_MONOTONIC |
| 114 | #define FIO_TIMER_CLOCK CLOCK_MONOTONIC |
| 115 | #else |
| 116 | #define FIO_TIMER_CLOCK CLOCK_REALTIME |
| 117 | #endif |
| 118 | |
Jens Axboe | 07e5b26 | 2007-04-02 14:46:07 +0200 | [diff] [blame] | 119 | #ifndef FIO_HAVE_RAWBIND |
Jens Axboe | 8cc7afa | 2007-04-17 09:06:43 +0200 | [diff] [blame] | 120 | #define fio_lookup_raw(dev, majdev, mindev) 1 |
Jens Axboe | 07e5b26 | 2007-04-02 14:46:07 +0200 | [diff] [blame] | 121 | #endif |
| 122 | |
Jens Axboe | 58483fa | 2011-01-19 11:09:58 -0700 | [diff] [blame] | 123 | #ifndef FIO_PREFERRED_ENGINE |
| 124 | #define FIO_PREFERRED_ENGINE "sync" |
| 125 | #endif |
| 126 | |
Jens Axboe | fca7035 | 2011-07-06 20:12:54 +0200 | [diff] [blame] | 127 | #ifndef FIO_MAX_JOBS |
| 128 | #define FIO_MAX_JOBS 2048 |
| 129 | #endif |
| 130 | |
Jens Axboe | 5ba13ea | 2011-10-04 23:50:28 +0200 | [diff] [blame] | 131 | #ifndef FIO_OS_HAVE_SOCKLEN_T |
| 132 | typedef socklen_t fio_socklen_t; |
| 133 | #endif |
| 134 | |
Jens Axboe | 901ebe1 | 2011-10-04 23:31:00 +0200 | [diff] [blame] | 135 | #ifdef FIO_USE_GENERIC_SWAP |
Jens Axboe | 9677bec | 2011-10-04 16:02:51 +0200 | [diff] [blame] | 136 | static inline uint16_t fio_swap16(uint16_t val) |
Jens Axboe | 232f9b7 | 2011-10-04 14:45:20 +0200 | [diff] [blame] | 137 | { |
| 138 | return (val << 8) | (val >> 8); |
| 139 | } |
| 140 | |
Jens Axboe | 901ebe1 | 2011-10-04 23:31:00 +0200 | [diff] [blame] | 141 | static inline uint32_t fio_swap32(uint32_t val) |
Jens Axboe | 232f9b7 | 2011-10-04 14:45:20 +0200 | [diff] [blame] | 142 | { |
| 143 | val = ((val & 0xff00ff00UL) >> 8) | ((val & 0x00ff00ffUL) << 8); |
| 144 | |
| 145 | return (val >> 16) | (val << 16); |
| 146 | } |
| 147 | |
Jens Axboe | 901ebe1 | 2011-10-04 23:31:00 +0200 | [diff] [blame] | 148 | static inline uint64_t fio_swap64(uint64_t val) |
Jens Axboe | 232f9b7 | 2011-10-04 14:45:20 +0200 | [diff] [blame] | 149 | { |
| 150 | val = ((val & 0xff00ff00ff00ff00ULL) >> 8) | |
| 151 | ((val & 0x00ff00ff00ff00ffULL) << 8); |
| 152 | val = ((val & 0xffff0000ffff0000ULL) >> 16) | |
| 153 | ((val & 0x0000ffff0000ffffULL) << 16); |
| 154 | |
| 155 | return (val >> 32) | (val << 32); |
| 156 | } |
| 157 | #endif |
| 158 | |
Jens Axboe | 8111092 | 2011-10-05 12:40:49 +0200 | [diff] [blame] | 159 | #ifdef FIO_LITTLE_ENDIAN |
| 160 | #define __le16_to_cpu(x) (x) |
| 161 | #define __le32_to_cpu(x) (x) |
| 162 | #define __le64_to_cpu(x) (x) |
| 163 | #define __cpu_to_le16(x) (x) |
| 164 | #define __cpu_to_le32(x) (x) |
| 165 | #define __cpu_to_le64(x) (x) |
| 166 | #else |
| 167 | #define __le16_to_cpu(x) fio_swap16(x) |
| 168 | #define __le32_to_cpu(x) fio_swap32(x) |
| 169 | #define __le64_to_cpu(x) fio_swap64(x) |
| 170 | #define __cpu_to_le16(x) fio_swap16(x) |
| 171 | #define __cpu_to_le32(x) fio_swap32(x) |
| 172 | #define __cpu_to_le64(x) fio_swap64(x) |
| 173 | #endif |
| 174 | |
| 175 | #define le16_to_cpu(val) ({ \ |
| 176 | uint16_t *__val = &(val); \ |
| 177 | __le16_to_cpu(*__val); \ |
| 178 | }) |
| 179 | #define le32_to_cpu(val) ({ \ |
| 180 | uint32_t *__val = &(val); \ |
| 181 | __le32_to_cpu(*__val); \ |
| 182 | }) |
| 183 | #define le64_to_cpu(val) ({ \ |
| 184 | uint64_t *__val = &(val); \ |
| 185 | __le64_to_cpu(*__val); \ |
| 186 | }) |
| 187 | #define cpu_to_le16(val) ({ \ |
| 188 | uint16_t *__val = &(val); \ |
| 189 | __cpu_to_le16(*__val); \ |
| 190 | }) |
| 191 | #define cpu_to_le32(val) ({ \ |
| 192 | uint32_t *__val = &(val); \ |
| 193 | __cpu_to_le32(*__val); \ |
| 194 | }) |
| 195 | #define cpu_to_le64(val) ({ \ |
| 196 | uint64_t *__val = &(val); \ |
| 197 | __cpu_to_le64(*__val); \ |
| 198 | }) |
| 199 | |
Jens Axboe | 5e62c22 | 2007-05-22 13:27:30 +0200 | [diff] [blame] | 200 | #ifndef FIO_HAVE_BLKTRACE |
| 201 | static inline int is_blktrace(const char *fname) |
| 202 | { |
| 203 | return 0; |
| 204 | } |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 205 | struct thread_data; |
Jens Axboe | 5e62c22 | 2007-05-22 13:27:30 +0200 | [diff] [blame] | 206 | static inline int load_blktrace(struct thread_data *td, const char *fname) |
| 207 | { |
| 208 | return 1; |
| 209 | } |
| 210 | #endif |
| 211 | |
Jens Axboe | eb7ccf3 | 2009-04-29 09:48:04 +0200 | [diff] [blame] | 212 | #define FIO_DEF_CL_SIZE 128 |
| 213 | |
| 214 | static inline int os_cache_line_size(void) |
| 215 | { |
| 216 | #ifdef FIO_HAVE_CL_SIZE |
| 217 | int ret = arch_cache_line_size(); |
| 218 | |
| 219 | if (ret <= 0) |
| 220 | return FIO_DEF_CL_SIZE; |
| 221 | |
| 222 | return ret; |
| 223 | #else |
| 224 | return FIO_DEF_CL_SIZE; |
| 225 | #endif |
| 226 | } |
| 227 | |
Jens Axboe | 792d551 | 2009-09-22 17:12:29 +0200 | [diff] [blame] | 228 | #ifdef FIO_USE_GENERIC_BDEV_SIZE |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 229 | static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes) |
Jens Axboe | 792d551 | 2009-09-22 17:12:29 +0200 | [diff] [blame] | 230 | { |
Jens Axboe | 3b2e146 | 2009-12-15 08:58:10 +0100 | [diff] [blame] | 231 | off_t end; |
Jens Axboe | 792d551 | 2009-09-22 17:12:29 +0200 | [diff] [blame] | 232 | |
Jens Axboe | 3b2e146 | 2009-12-15 08:58:10 +0100 | [diff] [blame] | 233 | *bytes = 0; |
| 234 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 235 | end = lseek(f->fd, 0, SEEK_END); |
Jens Axboe | 792d551 | 2009-09-22 17:12:29 +0200 | [diff] [blame] | 236 | if (end < 0) |
| 237 | return errno; |
| 238 | |
| 239 | *bytes = end; |
| 240 | return 0; |
| 241 | } |
| 242 | #endif |
| 243 | |
Jens Axboe | 5353137 | 2009-12-15 10:28:37 +0100 | [diff] [blame] | 244 | #ifdef FIO_USE_GENERIC_RAND |
| 245 | typedef unsigned int os_random_state_t; |
| 246 | |
| 247 | static inline void os_random_seed(unsigned long seed, os_random_state_t *rs) |
| 248 | { |
| 249 | srand(seed); |
| 250 | } |
| 251 | |
| 252 | static inline long os_random_long(os_random_state_t *rs) |
| 253 | { |
| 254 | long val; |
| 255 | |
| 256 | val = rand_r(rs); |
| 257 | return val; |
| 258 | } |
| 259 | #endif |
| 260 | |
Jens Axboe | 2e3bd4c | 2010-05-17 12:29:57 +0200 | [diff] [blame] | 261 | #ifndef FIO_HAVE_FS_STAT |
| 262 | static inline unsigned long long get_fs_size(const char *path) |
| 263 | { |
| 264 | return 0; |
| 265 | } |
| 266 | #endif |
| 267 | |
Jens Axboe | c00a228 | 2011-07-08 20:56:06 +0200 | [diff] [blame] | 268 | #ifndef FIO_HAVE_CPU_ONLINE_SYSCONF |
| 269 | static inline unsigned int cpus_online(void) |
| 270 | { |
| 271 | return sysconf(_SC_NPROCESSORS_ONLN); |
| 272 | } |
| 273 | #endif |
| 274 | |
Jens Axboe | 47f767c | 2011-07-12 21:17:49 +0200 | [diff] [blame] | 275 | #ifndef FIO_HAVE_GETTID |
| 276 | static inline int gettid(void) |
| 277 | { |
| 278 | return getpid(); |
| 279 | } |
| 280 | #endif |
| 281 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 282 | #endif |