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